source: git/Singular/febase.cc @ a70441f

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