source: git/Singular/febase.cc @ 1caa72

spielwiese
Last change on this file since 1caa72 was 1caa72, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-04-06 Olaf Bachmann <obachman@mathematik.uni-kl.de> * spSpolyLoop.h: neww calling interface for spGetSpolyLoop * kstd1.cc (kNF): moved strat->ak field initailization out of initBuchMora into single routines * febase.cc (feGetSearchPath): added feGetSearchPath; changed algorithm for searching files: $SINGULARPATH -> relative to executable -> burnt-in locations * added find_exec.c to get absolute pathname of executable git-svn-id: file:///usr/local/Singular/svn/trunk@1341 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.27 1998-04-06 17:59:27 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_path(const char* argv0);
106
107#define SINGULAR_RELATIVE_DATA_DIR "LIB"
108
109static char* SearchPath = NULL;
110
111// Return the file search path for singular w.r.t. the following priorities:
112// Env-variables + Relative Data Dir + Burned-in data dir
113char* feGetSearchPath(const char* argv0)
114{   
115  if (SearchPath == NULL) 
116  {
117    char *env = NULL, *sibbling = NULL, *path;
118    int plength = 0, tmp;
119 
120#ifdef MSDOS
121    env=getenv("SPATH");
122#else
123    env=getenv("SINGULARPATH");
124#endif
125 
126    if (argv0 != NULL)
127      sibbling = find_executable_path(argv0);
128 
129    if (env != NULL)
130      plength = strlen(env) + 1;
131
132    if (sibbling != NULL)
133      plength += strlen(sibbling) + strlen(SINGULAR_RELATIVE_DATA_DIR) + 2;
134 
135    plength += strlen(SINGULAR_DATADIR) + 2;
136 
137    path = (char*) AllocL(plength*sizeof(char));
138    SearchPath = path;
139 
140    if (env != NULL)
141    {
142      tmp = strlen(env);
143      memcpy(path, env, tmp);
144      path = &(path[tmp]);
145      *path = FS_SEP;
146      path++;
147    }
148 
149    if (sibbling != NULL)
150    {
151      tmp = strlen(sibbling);
152      memcpy(path, sibbling, tmp);
153      path = &(path[tmp]);
154      *path = DIR_SEP;
155      path++;
156      tmp = strlen(SINGULAR_RELATIVE_DATA_DIR);
157      memcpy(path, SINGULAR_RELATIVE_DATA_DIR, tmp);
158      path = &(path[tmp]);
159      *path = FS_SEP;
160      path++;
161      FreeL(sibbling);
162    }
163 
164    tmp = strlen(SINGULAR_DATADIR);
165    memcpy(path,SINGULAR_DATADIR, tmp);
166    path = &(path[tmp]);
167    *path = '\0';
168  }
169  return SearchPath;
170}
171
172
173FILE * feFopen(char *path, char *mode, char *where,int useWerror)
174{
175  FILE * f=fopen(path,mode);
176#ifdef macintosh
177  if (f!=NULL)
178  {
179    if (where!=NULL) strcpy(where,path);
180    return f;
181  }
182  char *res;
183  int idat=strlen(SINGULAR_DATADIR),ipath=strlen(path);
184  char *env=getenv("SINGULARPATH");
185  int ienv=0, ii=0;
186  if (env!=NULL)
187  {
188    ienv=strlen(env);
189    ii=ienv;
190  }
191  if (ii<idat) ii = idat;
192  if (ii==0)
193  {
194    if (useWerror)
195      Werror("cannot open `%s`",path);
196    return f;
197  }
198  res=(char*) AllocL(ii+ipath+1);
199  if (ienv!=0)
200  {
201    memcpy(res,env,ienv);
202    memcpy(res+ienv,path,ipath);
203    res[ienv+ipath]='\0';
204    f=fopen(res,mode);
205  }
206  if ((f==NULL)&&(idat!=0))
207  {
208    memcpy(res,SINGULAR_DATADIR,idat);
209    memcpy(res+idat,path,ipath);
210    res[idat+ipath]='\0';
211    f=fopen(res,mode);
212  }
213  if (f==NULL)
214  {
215    if (useWerror)
216      Werror("cannot open `%s`",res);
217  }
218  else if (where!=NULL)
219    strcpy(where,res);
220  FreeL(res);
221#else
222  if (where!=NULL) strcpy(where,path);
223  if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.')
224  &&(f==NULL))
225  {
226    char found = 0;
227    char* spath = feGetSearchPath();
228    char *s;
229
230    if (where==NULL) s=(char *)AllocL(250);
231    else             s=where;
232
233    if (spath!=NULL)
234    {
235      char *p,*q;
236      p = spath;
237      while( (q=strchr(p, FS_SEP)) != NULL)
238      {
239        *q = '\0';
240        strcpy(s,p);
241        *q = FS_SEP;
242        strcat(s, DIR_SEPP);
243        strcat(s, path);
244        #ifndef macintosh
245          if(!access(s, R_OK)) { found++; break; }
246        #else
247          f=fopen(s,mode);
248          if (f!=NULL)  { found++; fclose(f); break; }
249        #endif
250        p = q+1;
251      }
252      if(!found)
253      {
254        strcpy(s,p);
255        strcat(s, DIR_SEPP);
256        strcat(s, path);
257      }
258      f=fopen(s,mode);
259      if (f!=NULL)
260      {
261        if (where==NULL) FreeL((ADDRESS)s);
262        return f;
263      }
264    }
265    else
266    {
267      if (where!=NULL) strcpy(s/*where*/,path);
268      f=fopen(path,mode);
269    }
270    if (where==NULL) FreeL((ADDRESS)s);
271  }
272  if ((f==NULL)&&(useWerror))
273    Werror("cannot open `%s`",path);
274#endif
275  return f;
276}
277
278static char * feBufferStart;
279  /* only used in StringSet(S)/StringAppend(S)*/
280char * StringAppend(char *fmt, ...)
281{
282  va_list ap;
283  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
284  int more;
285  va_start(ap, fmt);
286  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
287  {
288    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
289    int l=s-feBuffer;
290    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
291                                                     more);
292    feBufferLength=more;
293    s=feBuffer+l;
294#ifndef BSD_SPRINTF
295    feBufferStart=s;
296#endif
297  }
298#ifdef BSD_SPRINTF
299  vsprintf(s, fmt, ap);
300  while (*s!='\0') s++;
301  feBufferStart =s;
302#else
303  feBufferStart += vsprintf(s, fmt, ap);
304#endif
305  va_end(ap);
306  return feBuffer;
307}
308
309char * StringAppendS(char *st)
310{
311  /* feBufferStart is feBuffer + strlen(feBuffer);*/
312  int more,l;
313  int ll=feBufferStart-feBuffer;
314  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
315  {
316    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
317    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
318                                                     more);
319    feBufferLength=more;
320    feBufferStart=feBuffer+ll;
321  }
322  strcat(feBufferStart, st);
323  feBufferStart +=l;
324  return feBuffer;
325}
326
327char * StringSet(char *fmt, ...)
328{
329  va_list ap;
330  char *s = feBuffer;
331  va_start(ap, fmt);
332#ifdef BSD_SPRINTF
333  vsprintf(s, fmt, ap);
334  while (*s!='\0') s++;
335  feBufferStart = s;
336#else
337  feBufferStart = feBuffer + vsprintf(s, fmt, ap);
338#endif
339  va_end(ap);
340  return feBuffer;
341}
342
343char * StringSetS(char *st)
344{
345  int more,l;
346  if ((l=strlen(st))>feBufferLength)
347  {
348    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
349    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
350                                                     more);
351    feBufferLength=more;
352  }
353  strcpy(feBuffer,st);
354  feBufferStart=feBuffer+l;
355  return feBuffer;
356}
357
358void PrintTCLS(char c, char *s)
359{
360#ifndef macintosh
361  int l=strlen(s);
362  if (l>0) PrintTCL(c,l,s);
363#endif
364}
365
366extern "C" {
367void WerrorS(char *s)
368{
369#ifdef HAVE_MPSR
370  if (feBatch)
371  {
372    if (feErrors==NULL)
373    {
374      feErrors=(char *)Alloc(256);
375      feErrorsLen=256;
376      strcpy(feErrors,s);
377    }
378    else
379    {
380      if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen)
381      {
382        feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256);
383        feErrorsLen+=256;
384      }
385      strcat(feErrors,s);
386    }
387    strcat(feErrors,"\n");
388  }
389  else
390#endif
391  {
392#ifdef HAVE_TCL
393    if (tclmode)
394    {
395      //PrintTCLS('E',s);
396      //PrintTCLS('E',"\n");
397      PrintTCLS('N',s);
398      PrintTCLS('N',"\n");
399    }
400    else
401#endif
402    {
403      fwrite("   ? ",1,5,stderr);
404      fwrite(s,1,strlen(s),stderr);
405      fwrite("\n",1,1,stderr);
406      fflush(stderr);
407      if (feProt&PROT_O)
408      {
409        fwrite("   ? ",1,5,feProtFile);
410        fwrite(s,1,strlen(s),feProtFile);
411        fwrite("\n",1,1,feProtFile);
412      }
413    }
414  }
415  errorreported = TRUE;
416}
417
418void Werror(char *fmt, ...)
419{
420  va_list ap;
421  va_start(ap, fmt);
422  char *s=(char *)Alloc(256);
423  vsprintf(s, fmt, ap);
424  WerrorS(s);
425  Free(s,256);
426  va_end(ap);
427}
428}
429
430void WarnS(char *s)
431{
432#ifdef HAVE_TCL
433  if (tclmode)
434  {
435    PrintTCLS('W',s);
436  }
437  else
438#endif
439  {
440    fwrite("// ** ",1,6,stdout);
441    fwrite(s,1,strlen(s),stdout);
442    fwrite("\n",1,1,stdout);
443    fflush(stdout);
444    if (feProt&PROT_O)
445    {
446      fwrite("// ** ",1,6,feProtFile);
447      fwrite(s,1,strlen(s),feProtFile);
448      fwrite("\n",1,1,feProtFile);
449    }
450  }
451}
452
453void Warn(char *fmt, ...)
454{
455  va_list ap;
456  va_start(ap, fmt);
457  char *s=(char *)Alloc(256);
458  vsprintf(s, fmt, ap);
459  WarnS(s);
460  Free(s,256);
461  va_end(ap);
462}
463
464#ifdef macintosh
465static  int lines = 0;
466static  int cols = 0;
467void mwrite(uchar c)
468{
469  if (c == '\n')
470  {
471    cols = 0;
472    if (lines == pagelength)
473    {
474      lines = 0;
475      fePause();
476    }
477    else
478    {
479      lines++;
480      fePutChar(c);
481    }
482  }
483  else
484  {
485    fePutChar(c);
486    cols++;
487    if (cols == colmax)
488    {
489//      cols = 0;   //will be done by mwrite('\n');
490      mwrite('\n');
491    }
492  }
493}
494#endif
495
496void PrintS(char *s)
497{
498#ifdef macintosh
499  char c;
500  while ('\0' != (c = *s++))
501  {
502    mwrite(c);
503  }
504#else
505#ifdef HAVE_TCL
506  if (tclmode)
507  {
508    PrintTCLS('N',s);
509  }
510  else
511#endif
512  {
513    fwrite(s,1,strlen(s),stdout);
514    fflush(stdout);
515    if (feProt&PROT_O)
516    {
517      fwrite(s,1,strlen(s),feProtFile);
518    }
519  }
520#endif
521}
522
523void PrintLn()
524{
525  PrintS("\n");
526}
527
528void Print(char *fmt, ...)
529{
530  va_list ap;
531  va_start(ap, fmt);
532#ifdef HAVE_TCL
533  if(tclmode)
534#endif
535#if (defined(HAVE_TCL) || defined(macintosh))
536  {
537    char *s=(char *)Alloc(strlen(fmt)+256);
538    vsprintf(s,fmt, ap);
539#ifdef HAVE_TCL
540    PrintTCLS('N',s);
541#endif
542#ifdef macintosh
543  char c;
544  while ('\0' != (c = *s++))
545  {
546    mwrite(c);
547  }
548  if (feProt&PROT_O)
549  {
550    vfprintf(feProtFile,fmt,ap);
551  }
552#endif
553  }
554#endif
555#if !defined(macintosh) || defined(HAVE_TCL)
556#ifdef HAVE_TCL
557  else
558#endif
559  {
560    vfprintf(stdout, fmt, ap);
561    fflush(stdout);
562    if (feProt&PROT_O)
563    {
564      vfprintf(feProtFile,fmt,ap);
565    }
566  }
567#endif
568  va_end(ap);
569}
570
571void fePause()
572{
573  uchar c;
574  mflush();
575#ifndef macintosh
576  fputs("pause>",stderr);
577#else
578  fputs("pause>\n",stderr);
579#endif
580  c = fgetc(stdin);
581  if (((c == '\003') || (c == 'C')) || (c == 'c'))
582  {
583    m2_end(1);
584  }
585}
586
587void monitor(char* s, int mode)
588{
589  if (feProt)
590  {
591    fclose(feProtFile);
592  }
593  if ((s!=NULL) && (*s!='\0'))
594  {
595    feProtFile = fopen(s,"w");
596    if (feProtFile==NULL)
597    {
598      Werror("cannot open %s",s);
599    }
600    else
601      feProt = (BOOLEAN)mode;
602  }
603}
604
605
606char* eati(char *s, int *i)
607{
608  int l=0;
609
610  if    (*s >= '0' && *s <= '9')
611  {
612    *i = 0;
613    while (*s >= '0' && *s <= '9')
614    {
615      *i *= 10;
616      *i += *s++ - '0';
617      l++;
618      if ((l>MAX_INT_LEN)||((*i) <0))
619      {
620        s-=l;
621        Werror("`%s` greater than %d(max. integer representation)",
622                s,INT_MAX);
623        return s;
624      }
625    }
626  }
627  else *i = 1;
628  return s;
629}
630
Note: See TracBrowser for help on using the repository browser.