source: git/Singular/febase.cc @ 584f84d

spielwiese
Last change on this file since 584f84d was dd01bf0, checked in by Olaf Bachmann <obachman@…>, 23 years ago
* fixed memory leaks (longalg, NF, syz) * eliminated OPT_MOREPAIS, CANCELUNIT, REDBEST * prepared for tailRings in local case git-svn-id: file:///usr/local/Singular/svn/trunk@4723 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.92 2000-11-14 16:04:51 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    omMarkAsStaticAddr(feBuffer);
220    feBufferLength=more;
221    s=feBuffer+l;
222#ifndef BSD_SPRINTF
223    feBufferStart=s;
224#endif
225  }
226#ifdef BSD_SPRINTF
227  vsprintf(s, fmt, ap);
228  while (*s!='\0') s++;
229  feBufferStart =s;
230#else
231#ifdef HAVE_VSNPRINTF
232  vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap);
233  if (vs == -1)
234  {
235    assume(0);
236    feBufferStart = feBuffer + feBufferLength -1;
237  }
238  else
239  {
240    feBufferStart += vs;
241  }
242#else
243  feBufferStart += vsprintf(s, fmt, ap);
244#endif
245#endif
246  omCheckAddrSize(feBuffer, feBufferLength);
247  va_end(ap);
248  return feBuffer;
249}
250
251char * StringAppendS(char *st)
252{
253  /* feBufferStart is feBuffer + strlen(feBuffer);*/
254  int more,l;
255  int ll=feBufferStart-feBuffer;
256  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
257  {
258    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
259    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
260                                                     more);
261    feBufferLength=more;
262    feBufferStart=feBuffer+ll;
263  }
264  strcat(feBufferStart, st);
265  feBufferStart +=l;
266  return feBuffer;
267}
268
269char * StringSetS(char *st)
270{
271  int more,l;
272  if ((l=strlen(st))>feBufferLength)
273  {
274    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
275    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
276                                                     more);
277    feBufferLength=more;
278  }
279  strcpy(feBuffer,st);
280  feBufferStart=feBuffer+l;
281  return feBuffer;
282}
283
284#ifndef __MWERKS__
285#ifdef HAVE_TCL
286extern "C" {
287void PrintTCLS(const char c, const char *s)
288{
289  int l=strlen(s);
290  if (l>0) PrintTCL(c,l,s);
291}
292}
293#endif
294#endif
295
296extern "C" {
297void WerrorS(const char *s)
298{
299#ifdef HAVE_MPSR
300  if (fe_fgets_stdin==fe_fgets_dummy)
301  {
302    if (feErrors==NULL)
303    {
304      feErrors=(char *)omAlloc(256);
305      feErrorsLen=256;
306      *feErrors = '\0';
307    }
308    else
309    {
310      if (((int)(strlen((char *)s)+ 20 +strlen(feErrors)))>=feErrorsLen)
311      {
312        feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256);
313        feErrorsLen+=256;
314      }
315    }
316    strcat(feErrors, "Singular error: ");
317    strcat(feErrors, (char *)s);
318  }
319  else
320#endif
321  {
322#ifdef HAVE_TCL
323    if (tclmode)
324    {
325      PrintTCLS('E',(char *)s);
326      PrintTCLS('E',"\n");
327    }
328    else
329#endif
330    {
331      fwrite("   ? ",1,5,stderr);
332      fwrite((char *)s,1,strlen((char *)s),stderr);
333      fwrite("\n",1,1,stderr);
334      fflush(stderr);
335      if (feProt&PROT_O)
336      {
337        fwrite("   ? ",1,5,feProtFile);
338        fwrite((char *)s,1,strlen((char *)s),feProtFile);
339        fwrite("\n",1,1,feProtFile);
340      }
341    }
342  }
343  errorreported = TRUE;
344}
345
346void Werror(char *fmt, ...)
347{
348  va_list ap;
349  va_start(ap, fmt);
350  char *s=(char *)omAlloc(256);
351  vsprintf(s, fmt, ap);
352  WerrorS(s);
353  omFreeSize(s,256);
354  va_end(ap);
355}
356}
357
358void WarnS(const char *s)
359{
360  #define warn_str "// ** "
361#ifdef HAVE_TCL
362  if (tclmode)
363  {
364    PrintTCLS('W',warn_str);
365    PrintTCLS('W',s);
366    PrintTCLS('W',"\n");
367  }
368  else
369#endif
370  if (feWarn) /* ignore warnings if option --no-warn was given */
371  {
372    fwrite(warn_str,1,6,stdout);
373    fwrite(s,1,strlen(s),stdout);
374    fwrite("\n",1,1,stdout);
375    fflush(stdout);
376    if (feProt&PROT_O)
377    {
378      fwrite(warn_str,1,6,feProtFile);
379      fwrite(s,1,strlen(s),feProtFile);
380      fwrite("\n",1,1,feProtFile);
381    }
382  }
383}
384
385void Warn(const char *fmt, ...)
386{
387  va_list ap;
388  va_start(ap, fmt);
389  char *s=(char *)omAlloc(256);
390  vsprintf(s, fmt, ap);
391  WarnS(s);
392  omFreeSize(s,256);
393  va_end(ap);
394}
395
396
397#ifdef macintosh
398static  int lines = 0;
399static  int cols = 0;
400
401void mwrite(uchar c)
402{
403  if (c == '\n')
404  {
405    cols = 0;
406    if (lines == pagelength)
407    {
408      lines = 0;
409      fputs("pause>\n",stderr);
410      uchar c = fgetc(stdin);
411    }
412    else
413    {
414      lines++;
415      fePutChar(c);
416    }
417  }
418  else
419  {
420    fePutChar(c);
421    cols++;
422    if (cols == colmax)
423    {
424      // cols = 0;   //will be done by mwrite('\n');
425      mwrite('\n');
426    }
427  }
428}
429#endif
430
431// some routines which redirect the output of print to a string
432static char* sprint = NULL;
433void SPrintStart()
434{
435  sprint = omStrDup("");
436}
437
438static void SPrintS(char* s)
439{
440  omCheckAddr(sprint);
441  if (s == NULL) return;
442  int ls = strlen(s);
443  if (ls == 0) return;
444
445  char* ns;
446  int l = strlen(sprint);
447  ns = (char*) omAlloc((l + ls + 1)*sizeof(char));
448  if (l > 0) strcpy(ns, sprint);
449
450  strcpy(&(ns[l]), s);
451  omFree(sprint);
452  sprint = ns;
453  omCheckAddr(sprint);
454}
455
456char* SPrintEnd()
457{
458  char* ns = sprint;
459  sprint = NULL;
460  omCheckAddr(ns);
461  return ns;
462}
463
464// Print routines
465extern "C" {
466void PrintS(char *s)
467{
468  if (sprint != NULL)
469  {
470    SPrintS(s);
471    return;
472  }
473
474  if (feOut) /* do not print when option --no-out was given */
475  {
476
477#ifdef macintosh
478    char c;
479    while ('\0' != (c = *s++))
480    {
481      mwrite(c);
482    }
483#else
484#ifdef HAVE_TCL
485    if (tclmode)
486    {
487      PrintTCLS('N',s);
488    }
489    else
490#endif
491    {
492      fwrite(s,1,strlen(s),stdout);
493      fflush(stdout);
494      if (feProt&PROT_O)
495      {
496        fwrite(s,1,strlen(s),feProtFile);
497      }
498    }
499#endif
500  }
501}
502
503void PrintLn()
504{
505  PrintS("\n");
506}
507
508void Print(char *fmt, ...)
509{
510  if (sprint != NULL)
511  {
512    int ls = strlen(fmt);
513    va_list ap;
514    va_start(ap, fmt);
515    omCheckAddr(sprint);
516    if (fmt != NULL && ls > 0)
517    {
518      char* ns;
519      int l = strlen(sprint);
520      ns = (char*) omAlloc(sizeof(char)*(ls + l + 256));
521      if (l > 0)  strcpy(ns, sprint);
522
523#ifdef HAVE_VSNPRINTF
524      l = vsnprintf(&(ns[l]), ls+255, fmt, ap);
525      assume(l != -1);
526#else
527      vsprintf(&(ns[l]), fmt, ap);
528#endif
529      omCheckAddr(ns);
530      omFree(sprint);
531      sprint = ns;
532    }
533    va_end(ap);
534    return;
535  }
536  if (feOut)
537  {
538    va_list ap;
539    va_start(ap, fmt);
540#ifdef HAVE_TCL
541    if(tclmode)
542#endif
543#if (defined(HAVE_TCL) || defined(macintosh))
544    {
545      char *s=(char *)omAlloc(strlen(fmt)+256);
546      vsprintf(s,fmt, ap);
547#ifdef HAVE_TCL
548      PrintTCLS('N',s);
549#endif
550#ifdef macintosh
551      char c;
552      while ('\0' != (c = *s++))
553      {
554        mwrite(c);
555      }
556      if (feProt&PROT_O)
557      {
558        vfprintf(feProtFile,fmt,ap);
559      }
560#endif
561    }
562#endif
563#if !defined(macintosh) || defined(HAVE_TCL)
564#ifdef HAVE_TCL
565    else
566#endif
567    {
568      vfprintf(stdout, fmt, ap);
569      fflush(stdout);
570      if (feProt&PROT_O)
571      {
572        vfprintf(feProtFile,fmt,ap);
573      }
574    }
575#endif
576    va_end(ap);
577  }
578}
579
580/* end extern "C" */
581}
582
583void monitor(char* s, int mode)
584{
585  if (feProt)
586  {
587    fclose(feProtFile);
588    feProt = 0;
589  }
590  if ((s!=NULL) && (*s!='\0'))
591  {
592    feProtFile = myfopen(s,"w");
593    if (feProtFile==NULL)
594    {
595      Werror("cannot open %s",s);
596      feProt=0;
597    }
598    else
599      feProt = (BOOLEAN)mode;
600  }
601}
602
603
604char* eati(char *s, int *i)
605{
606  int l=0;
607
608  if    (*s >= '0' && *s <= '9')
609  {
610    *i = 0;
611    while (*s >= '0' && *s <= '9')
612    {
613      *i *= 10;
614      *i += *s++ - '0';
615      l++;
616      if ((l>=MAX_INT_LEN)||((*i) <0))
617      {
618        s-=l;
619        Werror("`%s` greater than %d(max. integer representation)",
620                s,MAX_INT_VAL);
621        return s;
622      }
623    }
624  }
625  else *i = 1;
626  return s;
627}
628#else /* ! STANDALONE_PARSER */
629#include <stdio.h>
630
631#endif
632
633#ifndef unix
634// Make sure that mode contains binary option
635FILE* myfopen(char *path, char *mode)
636{
637  char mmode[4];
638  int i;
639  int done = 0;
640
641  for (i=0;;i++)
642  {
643    mmode[i] = mode[i];
644    if (mode[i] == '\0') break;
645    if (mode[i] == 'b') done = 1;
646  }
647
648  if (! done)
649  {
650    mmode[i] = 'b';
651    mmode[i+1] = '\0';
652  }
653  return fopen(path, mmode);
654}
655#endif
656// replace "\r\n" by " \n" and "\r" by "\n"
657
658size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
659{
660  size_t got = fread(ptr, size, nmemb, stream) * size;
661  size_t i;
662
663  for (i=0; i<got; i++)
664  {
665    if ( ((char*) ptr)[i] == '\r')
666    {
667      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
668        ((char*) ptr)[i] = ' ';
669      else
670        ((char*) ptr)[i] = '\n';
671    }
672  }
673  return got;
674}
Note: See TracBrowser for help on using the repository browser.