source: git/Singular/febase.cc @ 0da09b5

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