source: git/Singular/febase.cc @ 057e93c

spielwiese
Last change on this file since 057e93c was 057e93c, checked in by Hans Schönemann <hannes@…>, 26 years ago
* Fri Feb 27 15:02:10 MET 1998 hannes new input scheme: many modifications to febase.h, febase.inc, febase.cc, scanner.l, grammar.y, iplib.cc, ipshell.{h,cc} git-svn-id: file:///usr/local/Singular/svn/trunk@1183 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.25 1998-02-27 14:06:10 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=strlen(env), ii=ienv;
118  if (ii<idat) ii = idat;
119  if (ii==0)
120  {
121    if (useWerror)
122      Werror("cannot open `%s`",path);
123    return f;
124  }
125  res=(char*) AllocL(ii+ipath+1);
126  if (ienv!=0)
127  {
128    memcpy(res,env,ienv);
129    memcpy(res+ienv,path,ipath);
130    res[ienv+ipath]='\0';
131    f=fopen(res,mode);
132  }
133  if ((f==NULL)&&(idat!=0))
134  {
135    memcpy(res,SINGULAR_DATADIR,idat);
136    memcpy(res+idat,path,ipath);
137    res[idat+ipath]='\0';
138    f=fopen(res,mode);
139  }
140  if (f==NULL)
141  {
142    if (useWerror)
143      Werror("cannot open `%s`",res);
144  }
145  else if (where!=NULL)
146    strcpy(where,res);
147  FreeL(res);
148#else
149  if (where!=NULL) strcpy(where,path);
150  if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.')
151  &&(f==NULL))
152  {
153    char found = 0;
154    #ifdef MSDOS
155      char *env=getenv("SPATH");
156    #else
157      char *env=getenv("SINGULARPATH");
158    #endif
159    char *s;
160    #ifndef macintosh
161    // extend path by SINGULAR_DATADIR
162    s = (char*) AllocL((env != NULL ? strlen(env) : 0)
163                       +strlen(SINGULAR_DATADIR)+2);
164    if (env != NULL)
165    {
166      strcpy(s, env);
167      s[strlen(env)] = FS_SEP;
168      s[strlen(env)+1] = '\0';
169      strcat(s, SINGULAR_DATADIR);
170    }
171    else strcpy(s, SINGULAR_DATADIR);
172    env = s;
173    #endif
174    if (where==NULL) s=(char *)AllocL(250);
175    else             s=where;
176    if (env!=NULL)
177    {
178      char *p,*q;
179      p = env;
180      while( (q=strchr(p, FS_SEP)) != NULL)
181      {
182        *q = '\0';
183        strcpy(s,p);
184        *q = FS_SEP;
185        strcat(s, DIR_SEPP);
186        strcat(s, path);
187        #ifndef macintosh
188          if(!access(s, R_OK)) { found++; break; }
189        #else
190          f=fopen(s,mode);
191          if (f!=NULL)  { found++; fclose(f); break; }
192        #endif
193        p = q+1;
194      }
195      if(!found)
196      {
197        strcpy(s,p);
198        strcat(s, DIR_SEPP);
199        strcat(s, path);
200      }
201      f=fopen(s,mode);
202      if (f!=NULL)
203      {
204        if (where==NULL) FreeL((ADDRESS)s);
205        return f;
206      }
207    }
208    else
209    {
210      if (where!=NULL) strcpy(s/*where*/,path);
211      f=fopen(path,mode);
212    }
213    #ifndef macintosh
214    FreeL(env);
215    #endif
216    if (where==NULL) FreeL((ADDRESS)s);
217  }
218  if ((f==NULL)&&(useWerror))
219    Werror("cannot open `%s`",path);
220#endif
221  return f;
222}
223
224static char * feBufferStart;
225  /* only used in StringSet(S)/StringAppend(S)*/
226char * StringAppend(char *fmt, ...)
227{
228  va_list ap;
229  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
230  int more;
231  va_start(ap, fmt);
232  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
233  {
234    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
235    int l=s-feBuffer;
236    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
237                                                     more);
238    feBufferLength=more;
239    s=feBuffer+l;
240#ifndef BSD_SPRINTF
241    feBufferStart=s;
242#endif
243  }
244#ifdef BSD_SPRINTF
245  vsprintf(s, fmt, ap);
246  while (*s!='\0') s++;
247  feBufferStart =s;
248#else
249  feBufferStart += vsprintf(s, fmt, ap);
250#endif
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 *)ReAlloc((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 * StringSet(char *fmt, ...)
274{
275  va_list ap;
276  char *s = feBuffer;
277  va_start(ap, fmt);
278#ifdef BSD_SPRINTF
279  vsprintf(s, fmt, ap);
280  while (*s!='\0') s++;
281  feBufferStart = s;
282#else
283  feBufferStart = feBuffer + vsprintf(s, fmt, ap);
284#endif
285  va_end(ap);
286  return feBuffer;
287}
288
289char * StringSetS(char *st)
290{
291  int more,l;
292  if ((l=strlen(st))>feBufferLength)
293  {
294    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
295    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
296                                                     more);
297    feBufferLength=more;
298  }
299  strcpy(feBuffer,st);
300  feBufferStart=feBuffer+l;
301  return feBuffer;
302}
303
304void PrintTCLS(char c, char *s)
305{
306#ifndef macintosh
307  int l=strlen(s);
308  if (l>0) PrintTCL(c,l,s);
309#endif
310}
311
312extern "C" {
313void WerrorS(char *s)
314{
315#ifdef HAVE_MPSR
316  if (feBatch)
317  {
318    if (feErrors==NULL)
319    {
320      feErrors=(char *)Alloc(256);
321      feErrorsLen=256;
322      strcpy(feErrors,s);
323    }
324    else
325    {
326      if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen)
327      {
328        feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256);
329        feErrorsLen+=256;
330      }
331      strcat(feErrors,s);
332    }
333    strcat(feErrors,"\n");
334  }
335  else
336#endif
337  {
338#ifdef HAVE_TCL
339    if (tclmode)
340    {
341      //PrintTCLS('E',s);
342      //PrintTCLS('E',"\n");
343      PrintTCLS('N',s);
344      PrintTCLS('N',"\n");
345    }
346    else
347#endif
348    {
349      fwrite("   ? ",1,5,stderr);
350      fwrite(s,1,strlen(s),stderr);
351      fwrite("\n",1,1,stderr);
352      fflush(stderr);
353      if (feProt&PROT_O)
354      {
355        fwrite("   ? ",1,5,feProtFile);
356        fwrite(s,1,strlen(s),feProtFile);
357        fwrite("\n",1,1,feProtFile);
358      }
359    }
360  }
361  errorreported = TRUE;
362}
363
364void Werror(char *fmt, ...)
365{
366  va_list ap;
367  va_start(ap, fmt);
368  char *s=(char *)Alloc(256);
369  vsprintf(s, fmt, ap);
370  WerrorS(s);
371  Free(s,256);
372  va_end(ap);
373}
374}
375
376void WarnS(char *s)
377{
378#ifdef HAVE_TCL
379  if (tclmode)
380  {
381    PrintTCLS('W',s);
382  }
383  else
384#endif
385  {
386    fwrite("// ** ",1,6,stdout);
387    fwrite(s,1,strlen(s),stdout);
388    fwrite("\n",1,1,stdout);
389    fflush(stdout);
390    if (feProt&PROT_O)
391    {
392      fwrite("// ** ",1,6,feProtFile);
393      fwrite(s,1,strlen(s),feProtFile);
394      fwrite("\n",1,1,feProtFile);
395    }
396  }
397}
398
399void Warn(char *fmt, ...)
400{
401  va_list ap;
402  va_start(ap, fmt);
403  char *s=(char *)Alloc(256);
404  vsprintf(s, fmt, ap);
405  WarnS(s);
406  Free(s,256);
407  va_end(ap);
408}
409
410#ifdef macintosh
411static  int lines = 0;
412static  int cols = 0;
413void mwrite(uchar c)
414{
415  if (c == '\n')
416  {
417    cols = 0;
418    if (lines == pagelength)
419    {
420      lines = 0;
421      fePause();
422    }
423    else
424    {
425      lines++;
426      fePutChar(c);
427    }
428  }
429  else
430  {
431    fePutChar(c);
432    cols++;
433    if (cols == colmax)
434    {
435//      cols = 0;   //will be done by mwrite('\n');
436      mwrite('\n');
437    }
438  }
439}
440#endif
441
442void PrintS(char *s)
443{
444#ifdef macintosh
445  char c;
446  while ('\0' != (c = *s++))
447  {
448    mwrite(c);
449  }
450#else
451#ifdef HAVE_TCL
452  if (tclmode)
453  {
454    PrintTCLS('N',s);
455  }
456  else
457#endif
458  {
459    fwrite(s,1,strlen(s),stdout);
460    fflush(stdout);
461    if (feProt&PROT_O)
462    {
463      fwrite(s,1,strlen(s),feProtFile);
464    }
465  }
466#endif
467}
468
469void PrintLn()
470{
471  PrintS("\n");
472}
473
474void Print(char *fmt, ...)
475{
476  va_list ap;
477  va_start(ap, fmt);
478#ifdef HAVE_TCL
479  if(tclmode)
480#endif
481#if (defined(HAVE_TCL) || defined(macintosh))
482  {
483    char *s=(char *)Alloc(strlen(fmt)+256);
484    vsprintf(s,fmt, ap);
485#ifdef HAVE_TCL
486    PrintTCLS('N',s);
487#endif
488#ifdef macintosh
489  char c;
490  while ('\0' != (c = *s++))
491  {
492    mwrite(c);
493  }
494  if (feProt&PROT_O)
495  {
496    vfprintf(feProtFile,fmt,ap);
497  }
498#endif
499  }
500#endif
501#if !defined(macintosh) || defined(HAVE_TCL)
502#ifdef HAVE_TCL
503  else
504#endif
505  {
506    vfprintf(stdout, fmt, ap);
507    fflush(stdout);
508    if (feProt&PROT_O)
509    {
510      vfprintf(feProtFile,fmt,ap);
511    }
512  }
513#endif
514  va_end(ap);
515}
516
517void fePause()
518{
519  uchar c;
520  mflush();
521#ifndef macintosh
522  fputs("pause>",stderr);
523#else
524  fputs("pause>\n",stderr);
525#endif
526  c = fgetc(stdin);
527  if (((c == '\003') || (c == 'C')) || (c == 'c'))
528  {
529    m2_end(1);
530  }
531}
532
533void monitor(char* s, int mode)
534{
535  if (feProt)
536  {
537    fclose(feProtFile);
538  }
539  if ((s!=NULL) && (*s!='\0'))
540  {
541    feProtFile = fopen(s,"w");
542    if (feProtFile==NULL)
543    {
544      Werror("cannot open %s",s);
545    }
546    else
547      feProt = (BOOLEAN)mode;
548  }
549}
550
551
552char* eati(char *s, int *i)
553{
554  int l=0;
555
556  if    (*s >= '0' && *s <= '9')
557  {
558    *i = 0;
559    while (*s >= '0' && *s <= '9')
560    {
561      *i *= 10;
562      *i += *s++ - '0';
563      l++;
564      if ((l>MAX_INT_LEN)||((*i) <0))
565      {
566        s-=l;
567        Werror("`%s` greater than %d(max. integer representation)",
568                s,INT_MAX);
569        return s;
570      }
571    }
572  }
573  else *i = 1;
574  return s;
575}
576
Note: See TracBrowser for help on using the repository browser.