source: git/Singular/febase.cc @ 0f678f

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