source: git/Singular/febase.cc @ cc94b0a

spielwiese
Last change on this file since cc94b0a was c9ea817, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes/pohl: fixed NULL-dereference in febase.cc (if getenv returns NULL) git-svn-id: file:///usr/local/Singular/svn/trunk@1290 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.26 1998-03-31 07:41:21 Singular Exp $ */
5/*
6* ABSTRACT: i/o system
7*/
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <limits.h>
12#include <stdarg.h>
13#ifndef macintosh
14#include <unistd.h>
15#endif
16#ifdef NeXT
17#include <sys/file.h>
18#endif
19#include "mod2.h"
20#include "tok.h"
21#include "febase.h"
22#include "mmemory.h"
23#include "subexpr.h"
24#include "ipshell.h"
25
26#define fePutChar(c) fputc((uchar)(c),stdout)
27/*0 implementation */
28
29char fe_promptstr[]
30#ifdef macintosh
31                   =" \n";
32#else
33                   ="  ";
34#endif
35
36#define INITIAL_PRINT_BUFFER 24*1024
37static int feBufferLength=INITIAL_PRINT_BUFFER;
38static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER);
39
40int     si_echo = 0;
41int     printlevel = 0;
42#ifndef macintosh
43int     pagelength = 24;
44#else
45int     pagelength = -1;
46#endif
47int     colmax = 80;
48char    prompt_char = '>'; /*1 either '>' or '.'*/
49BITSET  verbose = 1
50                  | Sy_bit(V_REDEFINE)
51                  | Sy_bit(V_LOAD_LIB)
52                  | Sy_bit(V_SHOW_USE)
53                  | Sy_bit(V_PROMPT)
54//                  | Sy_bit(V_DEBUG_MEM)
55                  ;
56BOOLEAN errorreported = FALSE;
57BOOLEAN feBatch;
58char *  feErrors=NULL;
59int     feErrorsLen=0;
60
61BOOLEAN feProt = FALSE;
62FILE*   feProtFile;
63BOOLEAN tclmode=FALSE;
64/* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n
65*  E:l:s  error - not implemented yet (use N)
66*  W:l:s  warning
67*  N:l:s  stdout
68*  Q:0:   quit
69*  P:0:   prompt >
70*  P:1:   prompt .
71*  R:l:<ring-name> ring change
72* plan:
73*  O:l:<option/no-option> option change (option)
74*  V:l:<option/no-option> option change (verbose)
75*/
76
77#include "febase.inc"
78
79/*2
80* fopen, but use 'SINGULARPATH' from environment and SINGULARDATADIR
81* as set by configure
82*/
83#ifdef macintosh
84#  define  FS_SEP ','
85#  define  DIR_SEP ':'
86#  define  DIR_SEPP ":"
87#else
88#ifdef MSDOS
89#  define  FS_SEP ';'
90#  define  DIR_SEP '\\'
91#  define  DIR_SEPP "\\"
92#else
93#ifdef atarist
94#  define  FS_SEP ';'
95#  define  DIR_SEP '\\'
96#  define  DIR_SEPP "\\"
97#else  /* unix */
98#  define  FS_SEP ':'
99#  define  DIR_SEP '/'
100#  define  DIR_SEPP "/"
101#endif  /* atarist */
102#endif  /* MSDOS */
103#endif  /* macintosh */
104
105FILE * feFopen(char *path, char *mode, char *where,int useWerror)
106{
107  FILE * f=fopen(path,mode);
108#ifdef macintosh
109  if (f!=NULL)
110  {
111    if (where!=NULL) strcpy(where,path);
112    return f;
113  }
114  char *res;
115  int idat=strlen(SINGULAR_DATADIR),ipath=strlen(path);
116  char *env=getenv("SINGULARPATH");
117  int ienv=0, ii=0;
118  if (env!=NULL)
119  {
120    ienv=strlen(env);
121    ii=ienv;
122  }
123  if (ii<idat) ii = idat;
124  if (ii==0)
125  {
126    if (useWerror)
127      Werror("cannot open `%s`",path);
128    return f;
129  }
130  res=(char*) AllocL(ii+ipath+1);
131  if (ienv!=0)
132  {
133    memcpy(res,env,ienv);
134    memcpy(res+ienv,path,ipath);
135    res[ienv+ipath]='\0';
136    f=fopen(res,mode);
137  }
138  if ((f==NULL)&&(idat!=0))
139  {
140    memcpy(res,SINGULAR_DATADIR,idat);
141    memcpy(res+idat,path,ipath);
142    res[idat+ipath]='\0';
143    f=fopen(res,mode);
144  }
145  if (f==NULL)
146  {
147    if (useWerror)
148      Werror("cannot open `%s`",res);
149  }
150  else if (where!=NULL)
151    strcpy(where,res);
152  FreeL(res);
153#else
154  if (where!=NULL) strcpy(where,path);
155  if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.')
156  &&(f==NULL))
157  {
158    char found = 0;
159    #ifdef MSDOS
160      char *env=getenv("SPATH");
161    #else
162      char *env=getenv("SINGULARPATH");
163    #endif
164    char *s;
165    #ifndef macintosh
166    // extend path by SINGULAR_DATADIR
167    s = (char*) AllocL((env != NULL ? strlen(env) : 0)
168                       +strlen(SINGULAR_DATADIR)+2);
169    if (env != NULL)
170    {
171      strcpy(s, env);
172      s[strlen(env)] = FS_SEP;
173      s[strlen(env)+1] = '\0';
174      strcat(s, SINGULAR_DATADIR);
175    }
176    else strcpy(s, SINGULAR_DATADIR);
177    env = s;
178    #endif
179    if (where==NULL) s=(char *)AllocL(250);
180    else             s=where;
181    if (env!=NULL)
182    {
183      char *p,*q;
184      p = env;
185      while( (q=strchr(p, FS_SEP)) != NULL)
186      {
187        *q = '\0';
188        strcpy(s,p);
189        *q = FS_SEP;
190        strcat(s, DIR_SEPP);
191        strcat(s, path);
192        #ifndef macintosh
193          if(!access(s, R_OK)) { found++; break; }
194        #else
195          f=fopen(s,mode);
196          if (f!=NULL)  { found++; fclose(f); break; }
197        #endif
198        p = q+1;
199      }
200      if(!found)
201      {
202        strcpy(s,p);
203        strcat(s, DIR_SEPP);
204        strcat(s, path);
205      }
206      f=fopen(s,mode);
207      if (f!=NULL)
208      {
209        if (where==NULL) FreeL((ADDRESS)s);
210        return f;
211      }
212    }
213    else
214    {
215      if (where!=NULL) strcpy(s/*where*/,path);
216      f=fopen(path,mode);
217    }
218    #ifndef macintosh
219    FreeL(env);
220    #endif
221    if (where==NULL) FreeL((ADDRESS)s);
222  }
223  if ((f==NULL)&&(useWerror))
224    Werror("cannot open `%s`",path);
225#endif
226  return f;
227}
228
229static char * feBufferStart;
230  /* only used in StringSet(S)/StringAppend(S)*/
231char * StringAppend(char *fmt, ...)
232{
233  va_list ap;
234  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
235  int more;
236  va_start(ap, fmt);
237  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
238  {
239    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
240    int l=s-feBuffer;
241    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
242                                                     more);
243    feBufferLength=more;
244    s=feBuffer+l;
245#ifndef BSD_SPRINTF
246    feBufferStart=s;
247#endif
248  }
249#ifdef BSD_SPRINTF
250  vsprintf(s, fmt, ap);
251  while (*s!='\0') s++;
252  feBufferStart =s;
253#else
254  feBufferStart += vsprintf(s, fmt, ap);
255#endif
256  va_end(ap);
257  return feBuffer;
258}
259
260char * StringAppendS(char *st)
261{
262  /* feBufferStart is feBuffer + strlen(feBuffer);*/
263  int more,l;
264  int ll=feBufferStart-feBuffer;
265  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
266  {
267    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
268    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
269                                                     more);
270    feBufferLength=more;
271    feBufferStart=feBuffer+ll;
272  }
273  strcat(feBufferStart, st);
274  feBufferStart +=l;
275  return feBuffer;
276}
277
278char * StringSet(char *fmt, ...)
279{
280  va_list ap;
281  char *s = feBuffer;
282  va_start(ap, fmt);
283#ifdef BSD_SPRINTF
284  vsprintf(s, fmt, ap);
285  while (*s!='\0') s++;
286  feBufferStart = s;
287#else
288  feBufferStart = feBuffer + vsprintf(s, fmt, ap);
289#endif
290  va_end(ap);
291  return feBuffer;
292}
293
294char * StringSetS(char *st)
295{
296  int more,l;
297  if ((l=strlen(st))>feBufferLength)
298  {
299    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
300    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
301                                                     more);
302    feBufferLength=more;
303  }
304  strcpy(feBuffer,st);
305  feBufferStart=feBuffer+l;
306  return feBuffer;
307}
308
309void PrintTCLS(char c, char *s)
310{
311#ifndef macintosh
312  int l=strlen(s);
313  if (l>0) PrintTCL(c,l,s);
314#endif
315}
316
317extern "C" {
318void WerrorS(char *s)
319{
320#ifdef HAVE_MPSR
321  if (feBatch)
322  {
323    if (feErrors==NULL)
324    {
325      feErrors=(char *)Alloc(256);
326      feErrorsLen=256;
327      strcpy(feErrors,s);
328    }
329    else
330    {
331      if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen)
332      {
333        feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256);
334        feErrorsLen+=256;
335      }
336      strcat(feErrors,s);
337    }
338    strcat(feErrors,"\n");
339  }
340  else
341#endif
342  {
343#ifdef HAVE_TCL
344    if (tclmode)
345    {
346      //PrintTCLS('E',s);
347      //PrintTCLS('E',"\n");
348      PrintTCLS('N',s);
349      PrintTCLS('N',"\n");
350    }
351    else
352#endif
353    {
354      fwrite("   ? ",1,5,stderr);
355      fwrite(s,1,strlen(s),stderr);
356      fwrite("\n",1,1,stderr);
357      fflush(stderr);
358      if (feProt&PROT_O)
359      {
360        fwrite("   ? ",1,5,feProtFile);
361        fwrite(s,1,strlen(s),feProtFile);
362        fwrite("\n",1,1,feProtFile);
363      }
364    }
365  }
366  errorreported = TRUE;
367}
368
369void Werror(char *fmt, ...)
370{
371  va_list ap;
372  va_start(ap, fmt);
373  char *s=(char *)Alloc(256);
374  vsprintf(s, fmt, ap);
375  WerrorS(s);
376  Free(s,256);
377  va_end(ap);
378}
379}
380
381void WarnS(char *s)
382{
383#ifdef HAVE_TCL
384  if (tclmode)
385  {
386    PrintTCLS('W',s);
387  }
388  else
389#endif
390  {
391    fwrite("// ** ",1,6,stdout);
392    fwrite(s,1,strlen(s),stdout);
393    fwrite("\n",1,1,stdout);
394    fflush(stdout);
395    if (feProt&PROT_O)
396    {
397      fwrite("// ** ",1,6,feProtFile);
398      fwrite(s,1,strlen(s),feProtFile);
399      fwrite("\n",1,1,feProtFile);
400    }
401  }
402}
403
404void Warn(char *fmt, ...)
405{
406  va_list ap;
407  va_start(ap, fmt);
408  char *s=(char *)Alloc(256);
409  vsprintf(s, fmt, ap);
410  WarnS(s);
411  Free(s,256);
412  va_end(ap);
413}
414
415#ifdef macintosh
416static  int lines = 0;
417static  int cols = 0;
418void mwrite(uchar c)
419{
420  if (c == '\n')
421  {
422    cols = 0;
423    if (lines == pagelength)
424    {
425      lines = 0;
426      fePause();
427    }
428    else
429    {
430      lines++;
431      fePutChar(c);
432    }
433  }
434  else
435  {
436    fePutChar(c);
437    cols++;
438    if (cols == colmax)
439    {
440//      cols = 0;   //will be done by mwrite('\n');
441      mwrite('\n');
442    }
443  }
444}
445#endif
446
447void PrintS(char *s)
448{
449#ifdef macintosh
450  char c;
451  while ('\0' != (c = *s++))
452  {
453    mwrite(c);
454  }
455#else
456#ifdef HAVE_TCL
457  if (tclmode)
458  {
459    PrintTCLS('N',s);
460  }
461  else
462#endif
463  {
464    fwrite(s,1,strlen(s),stdout);
465    fflush(stdout);
466    if (feProt&PROT_O)
467    {
468      fwrite(s,1,strlen(s),feProtFile);
469    }
470  }
471#endif
472}
473
474void PrintLn()
475{
476  PrintS("\n");
477}
478
479void Print(char *fmt, ...)
480{
481  va_list ap;
482  va_start(ap, fmt);
483#ifdef HAVE_TCL
484  if(tclmode)
485#endif
486#if (defined(HAVE_TCL) || defined(macintosh))
487  {
488    char *s=(char *)Alloc(strlen(fmt)+256);
489    vsprintf(s,fmt, ap);
490#ifdef HAVE_TCL
491    PrintTCLS('N',s);
492#endif
493#ifdef macintosh
494  char c;
495  while ('\0' != (c = *s++))
496  {
497    mwrite(c);
498  }
499  if (feProt&PROT_O)
500  {
501    vfprintf(feProtFile,fmt,ap);
502  }
503#endif
504  }
505#endif
506#if !defined(macintosh) || defined(HAVE_TCL)
507#ifdef HAVE_TCL
508  else
509#endif
510  {
511    vfprintf(stdout, fmt, ap);
512    fflush(stdout);
513    if (feProt&PROT_O)
514    {
515      vfprintf(feProtFile,fmt,ap);
516    }
517  }
518#endif
519  va_end(ap);
520}
521
522void fePause()
523{
524  uchar c;
525  mflush();
526#ifndef macintosh
527  fputs("pause>",stderr);
528#else
529  fputs("pause>\n",stderr);
530#endif
531  c = fgetc(stdin);
532  if (((c == '\003') || (c == 'C')) || (c == 'c'))
533  {
534    m2_end(1);
535  }
536}
537
538void monitor(char* s, int mode)
539{
540  if (feProt)
541  {
542    fclose(feProtFile);
543  }
544  if ((s!=NULL) && (*s!='\0'))
545  {
546    feProtFile = fopen(s,"w");
547    if (feProtFile==NULL)
548    {
549      Werror("cannot open %s",s);
550    }
551    else
552      feProt = (BOOLEAN)mode;
553  }
554}
555
556
557char* eati(char *s, int *i)
558{
559  int l=0;
560
561  if    (*s >= '0' && *s <= '9')
562  {
563    *i = 0;
564    while (*s >= '0' && *s <= '9')
565    {
566      *i *= 10;
567      *i += *s++ - '0';
568      l++;
569      if ((l>MAX_INT_LEN)||((*i) <0))
570      {
571        s-=l;
572        Werror("`%s` greater than %d(max. integer representation)",
573                s,INT_MAX);
574        return s;
575      }
576    }
577  }
578  else *i = 1;
579  return s;
580}
581
Note: See TracBrowser for help on using the repository browser.