source: git/Singular/febase.cc @ 97454d

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