source: git/Singular/febase.cc @ 52d073

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