source: git/Singular/febase.cc @ 64eef3

fieker-DuValspielwiese
Last change on this file since 64eef3 was ec7aac, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* replaced prProcs by fast poly procs * fixed various memory leaks * added dError stuff git-svn-id: file:///usr/local/Singular/svn/trunk@4565 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.90 2000-09-04 13:38:56 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#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      if (home != NULL)
115      {
116        strcpy(longpath, home);
117        strcat(longpath, &(path[1]));
118        path = longpath;
119      }
120    }
121#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
122    else
123    {
124      char* dir_sep;
125      struct passwd *pw_entry;
126      strcpy (longpath, path);
127      dir_sep = strchr(longpath, DIR_SEP);
128      *dir_sep = '\0';
129      pw_entry = getpwnam(&longpath[1]);
130      if (pw_entry != NULL)
131      {
132        strcpy(longpath, pw_entry->pw_dir);
133        dir_sep = strchr(path, DIR_SEP);
134        strcat(longpath, dir_sep);
135        path = longpath;
136      }
137    }
138#endif
139  }
140  FILE * f=NULL;
141  if (! path_only)
142  {
143    struct stat statbuf;
144    if ((stat(path,&statbuf)==0)
145    && (S_ISREG(statbuf.st_mode)))
146      f = myfopen(path,mode);
147  }
148  if (where!=NULL) strcpy(where,path);
149  if ((*mode=='r') &&
150      (path[0]!=DIR_SEP) &&
151      ! (path[0] == '.' && path[1] == DIR_SEP) &&
152      (f==NULL))
153  {
154    char found = 0;
155    char* spath = feResource('s');
156    char *s;
157
158    if (where==NULL) s=(char *)omAlloc(250);
159    else             s=where;
160
161    if (spath!=NULL)
162    {
163      char *p,*q;
164      p = spath;
165      while( (q=strchr(p, fePathSep)) != NULL)
166      {
167        *q = '\0';
168        strcpy(s,p);
169        *q = fePathSep;
170        strcat(s, DIR_SEPP);
171        strcat(s, path);
172        #ifndef macintosh
173          if(!access(s, R_OK)) { found++; break; }
174        #else
175          f=fopen(s,mode); /* do not need myfopen: we test only the access */
176          if (f!=NULL)  { found++; fclose(f); break; }
177        #endif
178        p = q+1;
179      }
180      if(!found)
181      {
182        strcpy(s,p);
183        strcat(s, DIR_SEPP);
184        strcat(s, path);
185      }
186      f=myfopen(s,mode);
187      if (f!=NULL)
188      {
189        if (where==NULL) omFree((ADDRESS)s);
190        return f;
191      }
192    }
193    else
194    {
195      if (where!=NULL) strcpy(s/*where*/,path);
196      f=myfopen(path,mode);
197    }
198    if (where==NULL) omFree((ADDRESS)s);
199  }
200  if ((f==NULL)&&(useWerror))
201    Werror("cannot open `%s`",path);
202  return f;
203}
204
205static char * feBufferStart;
206  /* only used in StringSet(S)/StringAppend(S)*/
207char * StringAppend(char *fmt, ...)
208{
209  va_list ap;
210  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
211  int more, vs;
212  va_start(ap, fmt);
213  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
214  {
215    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
216    int l=s-feBuffer;
217    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
218                                                     more);
219    feBufferLength=more;
220    s=feBuffer+l;
221#ifndef BSD_SPRINTF
222    feBufferStart=s;
223#endif
224  }
225#ifdef BSD_SPRINTF
226  vsprintf(s, fmt, ap);
227  while (*s!='\0') s++;
228  feBufferStart =s;
229#else
230#ifdef HAVE_VSNPRINTF
231  vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap);
232  if (vs == -1)
233  {
234    assume(0);
235    feBufferStart = feBuffer + feBufferLength -1;
236  }
237  else
238  {
239    feBufferStart += vs;
240  }
241#else
242  feBufferStart += vsprintf(s, fmt, ap);
243#endif
244#endif
245  omCheckAddrSize(feBuffer, feBufferLength);
246  va_end(ap);
247  return feBuffer;
248}
249
250char * StringAppendS(char *st)
251{
252  /* feBufferStart is feBuffer + strlen(feBuffer);*/
253  int more,l;
254  int ll=feBufferStart-feBuffer;
255  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
256  {
257    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
258    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
259                                                     more);
260    feBufferLength=more;
261    feBufferStart=feBuffer+ll;
262  }
263  strcat(feBufferStart, st);
264  feBufferStart +=l;
265  return feBuffer;
266}
267
268char * StringSetS(char *st)
269{
270  int more,l;
271  if ((l=strlen(st))>feBufferLength)
272  {
273    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
274    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
275                                                     more);
276    feBufferLength=more;
277  }
278  strcpy(feBuffer,st);
279  feBufferStart=feBuffer+l;
280  return feBuffer;
281}
282
283#ifndef __MWERKS__
284#ifdef HAVE_TCL
285extern "C" {
286void PrintTCLS(const char c, const char *s)
287{
288  int l=strlen(s);
289  if (l>0) PrintTCL(c,l,s);
290}
291}
292#endif
293#endif
294
295extern "C" {
296void WerrorS(const char *s)
297{
298#ifdef HAVE_MPSR
299  if (fe_fgets_stdin==fe_fgets_dummy)
300  {
301    if (feErrors==NULL)
302    {
303      feErrors=(char *)omAlloc(256);
304      feErrorsLen=256;
305      *feErrors = '\0';
306    }
307    else
308    {
309      if (((int)(strlen((char *)s)+ 20 +strlen(feErrors)))>=feErrorsLen)
310      {
311        feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256);
312        feErrorsLen+=256;
313      }
314    }
315    strcat(feErrors, "Singular error: ");
316    strcat(feErrors, (char *)s);
317  }
318  else
319#endif
320  {
321#ifdef HAVE_TCL
322    if (tclmode)
323    {
324      PrintTCLS('E',(char *)s);
325      PrintTCLS('E',"\n");
326    }
327    else
328#endif
329    {
330      fwrite("   ? ",1,5,stderr);
331      fwrite((char *)s,1,strlen((char *)s),stderr);
332      fwrite("\n",1,1,stderr);
333      fflush(stderr);
334      if (feProt&PROT_O)
335      {
336        fwrite("   ? ",1,5,feProtFile);
337        fwrite((char *)s,1,strlen((char *)s),feProtFile);
338        fwrite("\n",1,1,feProtFile);
339      }
340    }
341  }
342  errorreported = TRUE;
343}
344
345void Werror(char *fmt, ...)
346{
347  va_list ap;
348  va_start(ap, fmt);
349  char *s=(char *)omAlloc(256);
350  vsprintf(s, fmt, ap);
351  WerrorS(s);
352  omFreeSize(s,256);
353  va_end(ap);
354}
355}
356
357void WarnS(const char *s)
358{
359  #define warn_str "// ** "
360#ifdef HAVE_TCL
361  if (tclmode)
362  {
363    PrintTCLS('W',warn_str);
364    PrintTCLS('W',s);
365    PrintTCLS('W',"\n");
366  }
367  else
368#endif
369  if (feWarn) /* ignore warnings if option --no-warn was given */
370  {
371    fwrite(warn_str,1,6,stdout);
372    fwrite(s,1,strlen(s),stdout);
373    fwrite("\n",1,1,stdout);
374    fflush(stdout);
375    if (feProt&PROT_O)
376    {
377      fwrite(warn_str,1,6,feProtFile);
378      fwrite(s,1,strlen(s),feProtFile);
379      fwrite("\n",1,1,feProtFile);
380    }
381  }
382}
383
384void Warn(const char *fmt, ...)
385{
386  va_list ap;
387  va_start(ap, fmt);
388  char *s=(char *)omAlloc(256);
389  vsprintf(s, fmt, ap);
390  WarnS(s);
391  omFreeSize(s,256);
392  va_end(ap);
393}
394
395
396#ifdef macintosh
397static  int lines = 0;
398static  int cols = 0;
399
400void mwrite(uchar c)
401{
402  if (c == '\n')
403  {
404    cols = 0;
405    if (lines == pagelength)
406    {
407      lines = 0;
408      fputs("pause>\n",stderr);
409      uchar c = fgetc(stdin);
410    }
411    else
412    {
413      lines++;
414      fePutChar(c);
415    }
416  }
417  else
418  {
419    fePutChar(c);
420    cols++;
421    if (cols == colmax)
422    {
423      // cols = 0;   //will be done by mwrite('\n');
424      mwrite('\n');
425    }
426  }
427}
428#endif
429
430// some routines which redirect the output of print to a string
431static char* sprint = NULL;
432void SPrintStart()
433{
434  sprint = omStrDup("");
435}
436
437static void SPrintS(char* s)
438{
439  omCheckAddr(sprint);
440  if (s == NULL) return;
441  int ls = strlen(s);
442  if (ls == 0) return;
443
444  char* ns;
445  int l = strlen(sprint);
446  ns = (char*) omAlloc((l + ls + 1)*sizeof(char));
447  if (l > 0) strcpy(ns, sprint);
448
449  strcpy(&(ns[l]), s);
450  omFree(sprint);
451  sprint = ns;
452  omCheckAddr(sprint);
453}
454
455char* SPrintEnd()
456{
457  char* ns = sprint;
458  sprint = NULL;
459  omCheckAddr(ns);
460  return ns;
461}
462
463// Print routines
464extern "C" {
465void PrintS(char *s)
466{
467  if (sprint != NULL)
468  {
469    SPrintS(s);
470    return;
471  }
472
473  if (feOut) /* do not print when option --no-out was given */
474  {
475
476#ifdef macintosh
477    char c;
478    while ('\0' != (c = *s++))
479    {
480      mwrite(c);
481    }
482#else
483#ifdef HAVE_TCL
484    if (tclmode)
485    {
486      PrintTCLS('N',s);
487    }
488    else
489#endif
490    {
491      fwrite(s,1,strlen(s),stdout);
492      fflush(stdout);
493      if (feProt&PROT_O)
494      {
495        fwrite(s,1,strlen(s),feProtFile);
496      }
497    }
498#endif
499  }
500}
501
502void PrintLn()
503{
504  PrintS("\n");
505}
506
507void Print(char *fmt, ...)
508{
509  if (sprint != NULL)
510  {
511    int ls = strlen(fmt);
512    va_list ap;
513    va_start(ap, fmt);
514    omCheckAddr(sprint);
515    if (fmt != NULL && ls > 0)
516    {
517      char* ns;
518      int l = strlen(sprint);
519      ns = (char*) omAlloc(sizeof(char)*(ls + l + 256));
520      if (l > 0)  strcpy(ns, sprint);
521
522#ifdef HAVE_VSNPRINTF
523      l = vsnprintf(&(ns[l]), ls+255, fmt, ap);
524      assume(l != -1);
525#else
526      vsprintf(&(ns[l]), fmt, ap);
527#endif
528      omCheckAddr(ns);
529      omFree(sprint);
530      sprint = ns;
531    }
532    va_end(ap);
533    return;
534  }
535  if (feOut)
536  {
537    va_list ap;
538    va_start(ap, fmt);
539#ifdef HAVE_TCL
540    if(tclmode)
541#endif
542#if (defined(HAVE_TCL) || defined(macintosh))
543    {
544      char *s=(char *)omAlloc(strlen(fmt)+256);
545      vsprintf(s,fmt, ap);
546#ifdef HAVE_TCL
547      PrintTCLS('N',s);
548#endif
549#ifdef macintosh
550      char c;
551      while ('\0' != (c = *s++))
552      {
553        mwrite(c);
554      }
555      if (feProt&PROT_O)
556      {
557        vfprintf(feProtFile,fmt,ap);
558      }
559#endif
560    }
561#endif
562#if !defined(macintosh) || defined(HAVE_TCL)
563#ifdef HAVE_TCL
564    else
565#endif
566    {
567      vfprintf(stdout, fmt, ap);
568      fflush(stdout);
569      if (feProt&PROT_O)
570      {
571        vfprintf(feProtFile,fmt,ap);
572      }
573    }
574#endif
575    va_end(ap);
576  }
577}
578
579/* end extern "C" */
580}
581
582void monitor(char* s, int mode)
583{
584  if (feProt)
585  {
586    fclose(feProtFile);
587    feProt = 0;
588  }
589  if ((s!=NULL) && (*s!='\0'))
590  {
591    feProtFile = myfopen(s,"w");
592    if (feProtFile==NULL)
593    {
594      Werror("cannot open %s",s);
595      feProt=0;
596    }
597    else
598      feProt = (BOOLEAN)mode;
599  }
600}
601
602
603char* eati(char *s, int *i)
604{
605  int l=0;
606
607  if    (*s >= '0' && *s <= '9')
608  {
609    *i = 0;
610    while (*s >= '0' && *s <= '9')
611    {
612      *i *= 10;
613      *i += *s++ - '0';
614      l++;
615      if ((l>=MAX_INT_LEN)||((*i) <0))
616      {
617        s-=l;
618        Werror("`%s` greater than %d(max. integer representation)",
619                s,MAX_INT_VAL);
620        return s;
621      }
622    }
623  }
624  else *i = 1;
625  return s;
626}
627#else /* ! STANDALONE_PARSER */
628#include <stdio.h>
629
630#endif
631
632#ifndef unix
633// Make sure that mode contains binary option
634FILE* myfopen(char *path, char *mode)
635{
636  char mmode[4];
637  int i;
638  int done = 0;
639
640  for (i=0;;i++)
641  {
642    mmode[i] = mode[i];
643    if (mode[i] == '\0') break;
644    if (mode[i] == 'b') done = 1;
645  }
646
647  if (! done)
648  {
649    mmode[i] = 'b';
650    mmode[i+1] = '\0';
651  }
652  return fopen(path, mmode);
653}
654#endif
655// replace "\r\n" by " \n" and "\r" by "\n"
656
657size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
658{
659  size_t got = fread(ptr, size, nmemb, stream) * size;
660  size_t i;
661
662  for (i=0; i<got; i++)
663  {
664    if ( ((char*) ptr)[i] == '\r')
665    {
666      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
667        ((char*) ptr)[i] = ' ';
668      else
669        ((char*) ptr)[i] = '\n';
670    }
671  }
672  return got;
673}
Note: See TracBrowser for help on using the repository browser.