source: git/Singular/febase.cc @ 50cbdc

spielwiese
Last change on this file since 50cbdc was 50cbdc, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: merge-2-0-2 git-svn-id: file:///usr/local/Singular/svn/trunk@5619 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.95 2001-08-27 14:46:58 Singular Exp $ */
5/*
6* ABSTRACT: i/o system
7*/
8
9/* I need myfread in standalone_parser */
10#ifndef STANDALONE_PARSER
11#include "mod2.h"
12
13#include <stdlib.h>
14#include <stdio.h>
15#include <mylimits.h>
16#include <stdarg.h>
17#include <sys/stat.h>
18#ifndef __MWERKS__
19#include <unistd.h>
20#endif
21#ifdef NeXT
22#include <sys/file.h>
23#endif
24
25#ifdef HAVE_PWD_H
26#include "pwd.h"
27#endif
28
29#include "tok.h"
30#include "febase.h"
31#include "omalloc.h"
32#include "subexpr.h"
33#include "ipshell.h"
34#include "dError.h"
35
36#define fePutChar(c) fputc((uchar)(c),stdout)
37/*0 implementation */
38
39char fe_promptstr[]
40#ifdef macintosh
41                   =" \n";
42#else
43                   ="  ";
44#endif
45
46#define INITIAL_PRINT_BUFFER 24*1024
47static int feBufferLength=INITIAL_PRINT_BUFFER;
48static char * feBuffer=(char *)omAlloc(INITIAL_PRINT_BUFFER);
49
50int     si_echo = 0;
51int     printlevel = 0;
52#ifndef macintosh
53int     pagelength = 24;
54#else
55int     pagelength = -1;
56#endif
57int     colmax = 80;
58char    prompt_char = '>'; /*1 either '>' or '.'*/
59extern "C" {
60BITSET  verbose = 1
61                  | Sy_bit(V_REDEFINE)
62                  | Sy_bit(V_LOAD_LIB)
63                  | Sy_bit(V_SHOW_USE)
64                  | Sy_bit(V_PROMPT)
65/*                  | Sy_bit(V_DEBUG_MEM) */
66;}
67BOOLEAN errorreported = FALSE;
68char *  feErrors=NULL;
69int     feErrorsLen=0;
70BOOLEAN feWarn = TRUE;
71BOOLEAN feOut = TRUE;
72
73#ifdef macintosh
74static  int lines = 0;
75static  int cols = 0;
76#endif
77
78const char feNotImplemented[]="not implemented";
79
80BOOLEAN feProt = FALSE;
81FILE*   feProtFile;
82BOOLEAN tclmode=FALSE;
83/* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n
84*  E:l:s  error
85*  W:l:s  warning
86*  N:l:s  stdout
87*  Q:0:   quit
88*  P:l:   prompt > (ring defined)
89*  U:l:   prompt > (no ring defined)
90*  P:l:   prompt .
91*  R:l:<ring-name> ring change
92*  L:l:<lib name> library loaded
93*  O:l:<list of options(space seperated)> option change
94*  M:l:<mem-usage> output from "option(mem)"
95*/
96
97#include "febase.inc"
98
99/*****************************************************************
100 *
101 * File handling
102 *
103 *****************************************************************/
104
105FILE * feFopen(char *path, char *mode, char *where,int useWerror,
106               int path_only)
107{
108  char longpath[MAXPATHLEN];
109  if (path[0]=='~')
110  {
111    if (path[1] == DIR_SEP)
112    {
113      char* home = getenv("HOME");
114#ifdef ix86_Win
115      if ((home==NULL)||(!access(home,X_OK)))
116        home = getenv("SINGHOME");
117#endif
118      if (home != NULL)
119      {
120        strcpy(longpath, home);
121        strcat(longpath, &(path[1]));
122        path = longpath;
123      }
124    }
125#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
126    else
127    {
128      char* dir_sep;
129      struct passwd *pw_entry;
130      strcpy (longpath, path);
131      dir_sep = strchr(longpath, DIR_SEP);
132      *dir_sep = '\0';
133      pw_entry = getpwnam(&longpath[1]);
134      if (pw_entry != NULL)
135      {
136        strcpy(longpath, pw_entry->pw_dir);
137        dir_sep = strchr(path, DIR_SEP);
138        strcat(longpath, dir_sep);
139        path = longpath;
140      }
141    }
142#endif
143  }
144  FILE * f=NULL;
145  if (! path_only)
146  {
147    struct stat statbuf;
148    if ((stat(path,&statbuf)==0)
149    && (S_ISREG(statbuf.st_mode)))
150      f = myfopen(path,mode);
151  }
152  if (where!=NULL) strcpy(where,path);
153  if ((*mode=='r') &&
154      (path[0]!=DIR_SEP) &&
155      ! (path[0] == '.' && path[1] == DIR_SEP) &&
156      (f==NULL))
157  {
158    char found = 0;
159    char* spath = feResource('s');
160    char *s;
161
162    if (where==NULL) s=(char *)omAlloc(250);
163    else             s=where;
164
165    if (spath!=NULL)
166    {
167      char *p,*q;
168      p = spath;
169      while( (q=strchr(p, fePathSep)) != NULL)
170      {
171        *q = '\0';
172        strcpy(s,p);
173        *q = fePathSep;
174        strcat(s, DIR_SEPP);
175        strcat(s, path);
176        #ifndef macintosh
177          if(!access(s, R_OK)) { found++; break; }
178        #else
179          f=fopen(s,mode); /* do not need myfopen: we test only the access */
180          if (f!=NULL)  { found++; fclose(f); break; }
181        #endif
182        p = q+1;
183      }
184      if(!found)
185      {
186        strcpy(s,p);
187        strcat(s, DIR_SEPP);
188        strcat(s, path);
189      }
190      f=myfopen(s,mode);
191      if (f!=NULL)
192      {
193        if (where==NULL) omFree((ADDRESS)s);
194        return f;
195      }
196    }
197    else
198    {
199      if (where!=NULL) strcpy(s/*where*/,path);
200      f=myfopen(path,mode);
201    }
202    if (where==NULL) omFree((ADDRESS)s);
203  }
204  if ((f==NULL)&&(useWerror))
205    Werror("cannot open `%s`",path);
206  return f;
207}
208
209static char * feBufferStart;
210  /* only used in StringSet(S)/StringAppend(S)*/
211char * StringAppend(char *fmt, ...)
212{
213  va_list ap;
214  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
215  int more, vs;
216  va_start(ap, fmt);
217  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
218  {
219    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
220    int l=s-feBuffer;
221    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
222                                                     more);
223    omMarkAsStaticAddr(feBuffer);
224    feBufferLength=more;
225    s=feBuffer+l;
226#ifndef BSD_SPRINTF
227    feBufferStart=s;
228#endif
229  }
230#ifdef BSD_SPRINTF
231  vsprintf(s, fmt, ap);
232  while (*s!='\0') s++;
233  feBufferStart =s;
234#else
235#ifdef HAVE_VSNPRINTF
236  vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap);
237  if (vs == -1)
238  {
239    assume(0);
240    feBufferStart = feBuffer + feBufferLength -1;
241  }
242  else
243  {
244    feBufferStart += vs;
245  }
246#else
247  feBufferStart += vsprintf(s, fmt, ap);
248#endif
249#endif
250  omCheckAddrSize(feBuffer, feBufferLength);
251  va_end(ap);
252  return feBuffer;
253}
254
255char * StringAppendS(char *st)
256{
257  /* feBufferStart is feBuffer + strlen(feBuffer);*/
258  int more,l;
259  int ll=feBufferStart-feBuffer;
260  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
261  {
262    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
263    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
264                                                     more);
265    feBufferLength=more;
266    feBufferStart=feBuffer+ll;
267  }
268  strcat(feBufferStart, st);
269  feBufferStart +=l;
270  return feBuffer;
271}
272
273char * StringSetS(char *st)
274{
275  int more,l;
276  if ((l=strlen(st))>feBufferLength)
277  {
278    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
279    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
280                                                     more);
281    feBufferLength=more;
282  }
283  strcpy(feBuffer,st);
284  feBufferStart=feBuffer+l;
285  return feBuffer;
286}
287
288#ifndef __MWERKS__
289#ifdef HAVE_TCL
290extern "C" {
291void PrintTCLS(const char c, const char *s)
292{
293  int l=strlen(s);
294  if (l>0) PrintTCL(c,l,s);
295}
296}
297#endif
298#endif
299
300extern "C" {
301void WerrorS(const char *s)
302{
303#ifdef HAVE_MPSR
304  if (fe_fgets_stdin==fe_fgets_dummy)
305  {
306    if (feErrors==NULL)
307    {
308      feErrors=(char *)omAlloc(256);
309      feErrorsLen=256;
310      *feErrors = '\0';
311    }
312    else
313    {
314      if (((int)(strlen((char *)s)+ 20 +strlen(feErrors)))>=feErrorsLen)
315      {
316        feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256);
317        feErrorsLen+=256;
318      }
319    }
320    strcat(feErrors, "Singular error: ");
321    strcat(feErrors, (char *)s);
322  }
323  else
324#endif
325  {
326#ifdef HAVE_TCL
327    if (tclmode)
328    {
329      PrintTCLS('E',(char *)s);
330      PrintTCLS('E',"\n");
331    }
332    else
333#endif
334    {
335      fwrite("   ? ",1,5,stderr);
336      fwrite((char *)s,1,strlen((char *)s),stderr);
337      fwrite("\n",1,1,stderr);
338      fflush(stderr);
339      if (feProt&PROT_O)
340      {
341        fwrite("   ? ",1,5,feProtFile);
342        fwrite((char *)s,1,strlen((char *)s),feProtFile);
343        fwrite("\n",1,1,feProtFile);
344      }
345    }
346  }
347  errorreported = TRUE;
348}
349
350void Werror(char *fmt, ...)
351{
352  va_list ap;
353  va_start(ap, fmt);
354  char *s=(char *)omAlloc(256);
355  vsprintf(s, fmt, ap);
356  WerrorS(s);
357  omFreeSize(s,256);
358  va_end(ap);
359}
360
361void WarnS(const char *s)
362{
363  #define warn_str "// ** "
364#ifdef HAVE_TCL
365  if (tclmode)
366  {
367    PrintTCLS('W',warn_str);
368    PrintTCLS('W',s);
369    PrintTCLS('W',"\n");
370  }
371  else
372#endif
373  if (feWarn) /* ignore warnings if option --no-warn was given */
374  {
375    fwrite(warn_str,1,6,stdout);
376    fwrite(s,1,strlen(s),stdout);
377    fwrite("\n",1,1,stdout);
378    fflush(stdout);
379    if (feProt&PROT_O)
380    {
381      fwrite(warn_str,1,6,feProtFile);
382      fwrite(s,1,strlen(s),feProtFile);
383      fwrite("\n",1,1,feProtFile);
384    }
385  }
386}
387} /* end extern "C" */
388
389void Warn(const char *fmt, ...)
390{
391  va_list ap;
392  va_start(ap, fmt);
393  char *s=(char *)omAlloc(256);
394  vsprintf(s, fmt, ap);
395  WarnS(s);
396  omFreeSize(s,256);
397  va_end(ap);
398}
399
400
401#ifdef macintosh
402static  int lines = 0;
403static  int cols = 0;
404
405void mwrite(uchar c)
406{
407  if (c == '\n')
408  {
409    cols = 0;
410    if (lines == pagelength)
411    {
412      lines = 0;
413      fputs("pause>\n",stderr);
414      uchar c = fgetc(stdin);
415    }
416    else
417    {
418      lines++;
419      fePutChar(c);
420    }
421  }
422  else
423  {
424    fePutChar(c);
425    cols++;
426    if (cols == colmax)
427    {
428      // cols = 0;   //will be done by mwrite('\n');
429      mwrite('\n');
430    }
431  }
432}
433#endif
434
435// some routines which redirect the output of print to a string
436static char* sprint = NULL;
437void SPrintStart()
438{
439  sprint = omStrDup("");
440}
441
442static void SPrintS(char* s)
443{
444  omCheckAddr(sprint);
445  if (s == NULL) return;
446  int ls = strlen(s);
447  if (ls == 0) return;
448
449  char* ns;
450  int l = strlen(sprint);
451  ns = (char*) omAlloc((l + ls + 1)*sizeof(char));
452  if (l > 0) strcpy(ns, sprint);
453
454  strcpy(&(ns[l]), s);
455  omFree(sprint);
456  sprint = ns;
457  omCheckAddr(sprint);
458}
459
460char* SPrintEnd()
461{
462  char* ns = sprint;
463  sprint = NULL;
464  omCheckAddr(ns);
465  return ns;
466}
467
468// Print routines
469extern "C" {
470void PrintS(char *s)
471{
472  if (sprint != NULL)
473  {
474    SPrintS(s);
475    return;
476  }
477
478  if (feOut) /* do not print when option --no-out was given */
479  {
480
481#ifdef macintosh
482    char c;
483    while ('\0' != (c = *s++))
484    {
485      mwrite(c);
486    }
487#else
488#ifdef HAVE_TCL
489    if (tclmode)
490    {
491      PrintTCLS('N',s);
492    }
493    else
494#endif
495    {
496      fwrite(s,1,strlen(s),stdout);
497      fflush(stdout);
498      if (feProt&PROT_O)
499      {
500        fwrite(s,1,strlen(s),feProtFile);
501      }
502    }
503#endif
504  }
505}
506
507void PrintLn()
508{
509  PrintS("\n");
510}
511
512void Print(char *fmt, ...)
513{
514  if (sprint != NULL)
515  {
516    int ls = strlen(fmt);
517    va_list ap;
518    va_start(ap, fmt);
519    omCheckAddr(sprint);
520    if (fmt != NULL && ls > 0)
521    {
522      char* ns;
523      int l = strlen(sprint);
524      ns = (char*) omAlloc(sizeof(char)*(ls + l + 256));
525      if (l > 0)  strcpy(ns, sprint);
526
527#ifdef HAVE_VSNPRINTF
528      l = vsnprintf(&(ns[l]), ls+255, fmt, ap);
529      assume(l != -1);
530#else
531      vsprintf(&(ns[l]), fmt, ap);
532#endif
533      omCheckAddr(ns);
534      omFree(sprint);
535      sprint = ns;
536    }
537    va_end(ap);
538    return;
539  }
540  if (feOut)
541  {
542    va_list ap;
543    va_start(ap, fmt);
544#ifdef HAVE_TCL
545    if(tclmode)
546#endif
547#if (defined(HAVE_TCL) || defined(macintosh))
548    {
549      char *s=(char *)omAlloc(strlen(fmt)+256);
550      vsprintf(s,fmt, ap);
551#ifdef HAVE_TCL
552      PrintTCLS('N',s);
553#endif
554#ifdef macintosh
555      char c;
556      while ('\0' != (c = *s++))
557      {
558        mwrite(c);
559      }
560      if (feProt&PROT_O)
561      {
562        vfprintf(feProtFile,fmt,ap);
563      }
564#endif
565    }
566#endif
567#if !defined(macintosh) || defined(HAVE_TCL)
568#ifdef HAVE_TCL
569    else
570#endif
571    {
572      vfprintf(stdout, fmt, ap);
573      fflush(stdout);
574      if (feProt&PROT_O)
575      {
576        vfprintf(feProtFile,fmt,ap);
577      }
578    }
579#endif
580    va_end(ap);
581  }
582}
583
584/* end extern "C" */
585}
586
587void monitor(char* s, int mode)
588{
589  if (feProt)
590  {
591    fclose(feProtFile);
592    feProt = 0;
593  }
594  if ((s!=NULL) && (*s!='\0'))
595  {
596    feProtFile = myfopen(s,"w");
597    if (feProtFile==NULL)
598    {
599      Werror("cannot open %s",s);
600      feProt=0;
601    }
602    else
603      feProt = (BOOLEAN)mode;
604  }
605}
606
607
608char* eati(char *s, int *i)
609{
610  int l=0;
611
612  if    (*s >= '0' && *s <= '9')
613  {
614    *i = 0;
615    while (*s >= '0' && *s <= '9')
616    {
617      *i *= 10;
618      *i += *s++ - '0';
619      l++;
620      if ((l>=MAX_INT_LEN)||((*i) <0))
621      {
622        s-=l;
623        Werror("`%s` greater than %d(max. integer representation)",
624                s,MAX_INT_VAL);
625        return s;
626      }
627    }
628  }
629  else *i = 1;
630  return s;
631}
632#else /* ! STANDALONE_PARSER */
633#include <stdio.h>
634
635#endif
636
637#ifdef ix86_Win
638// Make sure that mode contains binary option
639FILE* myfopen(char *path, char *mode)
640{
641  char mmode[4];
642  int i;
643  int done = 0;
644
645  for (i=0;;i++)
646  {
647    mmode[i] = mode[i];
648    if (mode[i] == '\0') break;
649    if (mode[i] == 'b') done = 1;
650  }
651
652  if (! done)
653  {
654    mmode[i] = 'b';
655    mmode[i+1] = '\0';
656  }
657  return fopen(path, mmode);
658}
659#endif
660// replace "\r\n" by " \n" and "\r" by "\n"
661
662size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
663{
664  size_t got = fread(ptr, size, nmemb, stream) * size;
665  size_t i;
666
667  for (i=0; i<got; i++)
668  {
669    if ( ((char*) ptr)[i] == '\r')
670    {
671      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
672        ((char*) ptr)[i] = ' ';
673      else
674        ((char*) ptr)[i] = '\n';
675    }
676  }
677  return got;
678}
Note: See TracBrowser for help on using the repository browser.