source: git/Singular/febase.cc @ 9d72fe

spielwiese
Last change on this file since 9d72fe was 9d72fe, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* new p_Procs stuff git-svn-id: file:///usr/local/Singular/svn/trunk@4559 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.89 2000-08-24 14:42:40 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
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
395void fePrintReportBug(char* msg, char* file, int line)
396{
397  WarnS("YOU HAVE FOUND A BUG IN SINGULAR.");
398  WarnS("Please, email the following output to singular@mathematik.uni-kl.de");
399  Warn("Bug occured at %s:%d", file, line);
400  Warn("Message: %s", msg);
401  Warn("Version: " S_UNAME S_VERSION1 " (%lu) %s ",
402       feVersionId, singular_date);
403}
404
405extern "C" {
406void assume_violation(char* file, int line)
407{
408  fprintf(stderr, "Internal assume violation: file %s line %d\n", file, line);
409  omPrintCurrentBackTrace(stderr);
410}
411}
412
413#ifdef macintosh
414static  int lines = 0;
415static  int cols = 0;
416
417void mwrite(uchar c)
418{
419  if (c == '\n')
420  {
421    cols = 0;
422    if (lines == pagelength)
423    {
424      lines = 0;
425      fputs("pause>\n",stderr);
426      uchar c = fgetc(stdin);
427    }
428    else
429    {
430      lines++;
431      fePutChar(c);
432    }
433  }
434  else
435  {
436    fePutChar(c);
437    cols++;
438    if (cols == colmax)
439    {
440      // cols = 0;   //will be done by mwrite('\n');
441      mwrite('\n');
442    }
443  }
444}
445#endif
446
447// some routines which redirect the output of print to a string
448static char* sprint = NULL;
449void SPrintStart()
450{
451  sprint = omStrDup("");
452}
453
454static void SPrintS(char* s)
455{
456  omCheckAddr(sprint);
457  if (s == NULL) return;
458  int ls = strlen(s);
459  if (ls == 0) return;
460
461  char* ns;
462  int l = strlen(sprint);
463  ns = (char*) omAlloc((l + ls + 1)*sizeof(char));
464  if (l > 0) strcpy(ns, sprint);
465
466  strcpy(&(ns[l]), s);
467  omFree(sprint);
468  sprint = ns;
469  omCheckAddr(sprint);
470}
471
472char* SPrintEnd()
473{
474  char* ns = sprint;
475  sprint = NULL;
476  omCheckAddr(ns);
477  return ns;
478}
479
480// Print routines
481extern "C" {
482void PrintS(char *s)
483{
484  if (sprint != NULL)
485  {
486    SPrintS(s);
487    return;
488  }
489
490  if (feOut) /* do not print when option --no-out was given */
491  {
492
493#ifdef macintosh
494    char c;
495    while ('\0' != (c = *s++))
496    {
497      mwrite(c);
498    }
499#else
500#ifdef HAVE_TCL
501    if (tclmode)
502    {
503      PrintTCLS('N',s);
504    }
505    else
506#endif
507    {
508      fwrite(s,1,strlen(s),stdout);
509      fflush(stdout);
510      if (feProt&PROT_O)
511      {
512        fwrite(s,1,strlen(s),feProtFile);
513      }
514    }
515#endif
516  }
517}
518
519void PrintLn()
520{
521  PrintS("\n");
522}
523
524void Print(char *fmt, ...)
525{
526  if (sprint != NULL)
527  {
528    int ls = strlen(fmt);
529    va_list ap;
530    va_start(ap, fmt);
531    omCheckAddr(sprint);
532    if (fmt != NULL && ls > 0)
533    {
534      char* ns;
535      int l = strlen(sprint);
536      ns = (char*) omAlloc(sizeof(char)*(ls + l + 256));
537      if (l > 0)  strcpy(ns, sprint);
538
539#ifdef HAVE_VSNPRINTF
540      l = vsnprintf(&(ns[l]), ls+255, fmt, ap);
541      assume(l != -1);
542#else
543      vsprintf(&(ns[l]), fmt, ap);
544#endif
545      omCheckAddr(ns);
546      omFree(sprint);
547      sprint = ns;
548    }
549    va_end(ap);
550    return;
551  }
552  if (feOut)
553  {
554    va_list ap;
555    va_start(ap, fmt);
556#ifdef HAVE_TCL
557    if(tclmode)
558#endif
559#if (defined(HAVE_TCL) || defined(macintosh))
560    {
561      char *s=(char *)omAlloc(strlen(fmt)+256);
562      vsprintf(s,fmt, ap);
563#ifdef HAVE_TCL
564      PrintTCLS('N',s);
565#endif
566#ifdef macintosh
567      char c;
568      while ('\0' != (c = *s++))
569      {
570        mwrite(c);
571      }
572      if (feProt&PROT_O)
573      {
574        vfprintf(feProtFile,fmt,ap);
575      }
576#endif
577    }
578#endif
579#if !defined(macintosh) || defined(HAVE_TCL)
580#ifdef HAVE_TCL
581    else
582#endif
583    {
584      vfprintf(stdout, fmt, ap);
585      fflush(stdout);
586      if (feProt&PROT_O)
587      {
588        vfprintf(feProtFile,fmt,ap);
589      }
590    }
591#endif
592    va_end(ap);
593  }
594}
595
596/* end extern "C" */
597}
598
599void monitor(char* s, int mode)
600{
601  if (feProt)
602  {
603    fclose(feProtFile);
604    feProt = 0;
605  }
606  if ((s!=NULL) && (*s!='\0'))
607  {
608    feProtFile = myfopen(s,"w");
609    if (feProtFile==NULL)
610    {
611      Werror("cannot open %s",s);
612      feProt=0;
613    }
614    else
615      feProt = (BOOLEAN)mode;
616  }
617}
618
619
620char* eati(char *s, int *i)
621{
622  int l=0;
623
624  if    (*s >= '0' && *s <= '9')
625  {
626    *i = 0;
627    while (*s >= '0' && *s <= '9')
628    {
629      *i *= 10;
630      *i += *s++ - '0';
631      l++;
632      if ((l>=MAX_INT_LEN)||((*i) <0))
633      {
634        s-=l;
635        Werror("`%s` greater than %d(max. integer representation)",
636                s,MAX_INT_VAL);
637        return s;
638      }
639    }
640  }
641  else *i = 1;
642  return s;
643}
644#else /* ! STANDALONE_PARSER */
645#include <stdio.h>
646
647#endif
648
649#ifndef unix
650// Make sure that mode contains binary option
651FILE* myfopen(char *path, char *mode)
652{
653  char mmode[4];
654  int i;
655  int done = 0;
656
657  for (i=0;;i++)
658  {
659    mmode[i] = mode[i];
660    if (mode[i] == '\0') break;
661    if (mode[i] == 'b') done = 1;
662  }
663
664  if (! done)
665  {
666    mmode[i] = 'b';
667    mmode[i+1] = '\0';
668  }
669  return fopen(path, mmode);
670}
671#endif
672// replace "\r\n" by " \n" and "\r" by "\n"
673
674size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
675{
676  size_t got = fread(ptr, size, nmemb, stream) * size;
677  size_t i;
678
679  for (i=0; i<got; i++)
680  {
681    if ( ((char*) ptr)[i] == '\r')
682    {
683      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
684        ((char*) ptr)[i] = ' ';
685      else
686        ((char*) ptr)[i] = '\n';
687    }
688  }
689  return got;
690}
Note: See TracBrowser for help on using the repository browser.