source: git/Singular/febase.cc @ f7b5a1

fieker-DuValspielwiese
Last change on this file since f7b5a1 was f7b5a1, checked in by Olaf Bachmann <obachman@…>, 27 years ago
obachman: * small changes in Makefile.in and configure.in git-svn-id: file:///usr/local/Singular/svn/trunk@208 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 20.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.11 1997-04-29 19:54:48 obachman Exp $ */
5/*
6* ABSTRACT: i/o system, handling of 'voices'
7*/
8
9#include <stdlib.h>
10#include <string.h>
11#include <stdio.h>
12#include <limits.h>
13#include <stdarg.h>
14#ifndef macintosh
15#include <unistd.h>
16#endif
17#ifdef NeXT
18#include <sys/file.h>
19#endif
20#include "mod2.h"
21#include "tok.h"
22#include "febase.h"
23#include "mmemory.h"
24#include "subexpr.h"
25#include "ipshell.h"
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
37class Voices
38{
39  private:
40    void Init()
41    {
42      memset(this,0,sizeof(*this));
43      v_lineno = 1;
44    }
45  public:
46    int    v_lineno;        // lineno, to restore in recursion
47    int    v_oldlineno;     // lineno, to restore at exit
48    int    typ;             // buffer type: see BT_..
49    int    v_echo;          // echo, to restore in recursion
50    int    v_fileVoice;     // to be restored in recursion
51    int    v_inputswitch;   // ??
52    FILE * files;           // file handle
53    long   fptr;            // file | buffer seek
54    char*  filename;        // file name
55    char * buffer;          // buffer pointer
56
57  void Next();
58  Voices() { Init(); }
59  Voices * VFile(char* fname);
60  Voices * Buffer(char* buf, int t);
61  int Exit();
62} ;
63
64
65extern FILE* yyin;
66
67#define INITIAL_PRINT_BUFFER 24*1024
68static int feBufferLength=INITIAL_PRINT_BUFFER;
69static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER);
70
71#define START_LEVMAX 32
72int     levmax       = START_LEVMAX;
73Voices *currentVoice = NULL;
74Voices *FileAttribs  =(Voices *)Alloc(START_LEVMAX*sizeof(Voices));
75short  *ifswitch     =(short *)Alloc0(START_LEVMAX*sizeof(short));
76        /*1 ifswitch==0: no if statement, else is invalid
77        *           ==1: if (0) processed, execute else
78        *           ==2: if (1) processed, else allowed but not executed
79        */
80int     si_echo = 0;
81int     printlevel = 0;
82int     fileVoice = 0;
83#ifndef macintosh
84int     pagelength = 24;
85#else
86int     pagelength = -1;
87#endif
88int     colmax = 80;
89int     voice = 0;
90int     inputswitch = 0;
91int     blocklineno = 0;
92char    prompt_char = '>'; /*1 either '>' or '.'*/
93BITSET  verbose = 1
94                  | Sy_bit(V_REDEFINE)
95                  | Sy_bit(V_LOAD_LIB)
96                  | Sy_bit(V_SHOW_USE)
97                  | Sy_bit(V_PROMPT)
98//                  | Sy_bit(V_DEBUG_MEM)
99                  ;
100BOOLEAN errorreported = FALSE;
101BOOLEAN feBatch;
102char *  feErrors=NULL;
103int     feErrorsLen=0;
104
105BOOLEAN feProt = FALSE;
106FILE*   feProtFile;
107BOOLEAN tclmode=FALSE;
108/* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n
109*  E:l:s  error - not implemented yet (use N)
110*  W:l:s  warning
111*  N:l:s  stdout
112*  Q:0:   quit
113*  P:0:   prompt >
114*  P:1:   prompt .
115*  R:l:<ring-name> ring change
116* plan:
117*  O:l:<option/no-option> option change (option)
118*  V:l:<option/no-option> option change (verbose)
119*/
120
121/*2 fopen, but use 'SINGULARPATH' from environment and SINGULARDATADIR
122* as set by configure
123*  */
124#ifdef macintosh
125#  define  FS_SEP ','
126#  define  DIR_SEP ':'
127#  define  DIR_SEPP ":"
128#else
129#ifdef MSDOS
130#  define  FS_SEP ';'
131#  define  DIR_SEP '\\'
132#  define  DIR_SEPP "\\"
133#else
134#ifdef atarist
135#  define  FS_SEP ';'
136#  define  DIR_SEP '\\'
137#  define  DIR_SEPP "\\"
138#else  /* unix */
139#  define  FS_SEP ':'
140#  define  DIR_SEP '/'
141#  define  DIR_SEPP "/"
142#endif  /* atarist */
143#endif  /* MSDOS */
144#endif  /* macintosh */
145
146FILE * feFopen(char *path, char *mode, char *where,int useWerror)
147{
148  if (where!=NULL) strcpy(where,path);
149  if ((*mode=='a') ||(*mode=='w') || (path[0]==DIR_SEP)||(path[0]=='.'))
150    return fopen(path,mode);
151  char found = 0;
152  FILE * f=NULL;
153#ifdef MSDOS
154  char *env=getenv("SPATH");
155#else
156  char *env=getenv("SINGULARPATH");
157#endif
158  char *s;
159  if (where==NULL) s=(char *)AllocL(250);
160  else             s=where;
161  if (env!=NULL)
162  {
163    char *p,*q;
164    p = env;
165    while( (q=strchr(p, FS_SEP)) != NULL)
166    {
167      *q = '\0';
168      strcpy(s,p);
169      *q = FS_SEP;
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);
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=fopen(s,mode);
187    if (f!=NULL)
188    {
189      if (where==NULL) FreeL((ADDRESS)s);
190      return f;
191    }
192  }
193  else
194  {
195    if (where!=NULL) strcpy(s/*where*/,path);
196    f=fopen(path,mode);
197  }
198#ifndef macintosh
199  if (f==NULL)
200  {
201    char* ss = s;
202    int need_len = strlen(path) + strlen(SINGULAR_DATADIR) + 2;
203   
204    if (where == NULL)
205    {
206      if (need_len > 250) ss = (char *) AllocL(need_len);
207      strcpy(ss,s);
208    }
209    else
210    {
211      if (need_len > strlen(where)) ss = (char *) AllocL(need_len);
212      strcpy(ss, s);
213    }
214    strcpy(ss,SINGULAR_DATADIR);
215    strcat(ss, DIR_SEPP);
216    strcat(ss,path);
217    f=fopen(ss,mode);
218    if (ss != s) FreeL((ADDRESS)ss);
219  }
220#endif
221  if (where==NULL) FreeL((ADDRESS)s);
222  if ((f==NULL)&&(useWerror))
223    Werror("cannot open `%s`",path);
224  return f;
225}
226
227/*2
228* the name of the current 'voice': the procname (or filename)
229*/
230const char * VoiceName()
231{
232  if (FileAttribs[fileVoice].filename!=NULL)
233    return FileAttribs[fileVoice].filename;
234  return sNoName;
235}
236
237/*2
238* the name of the 'voice' number 'i': the procname (or filename)
239*/
240const char * VoiceName(int i)
241{
242  if (FileAttribs[i].filename!=NULL)
243    return FileAttribs[i].filename;
244  return sNoName;
245}
246
247/*2
248* the type of the current voice:BT_proc, BT_example, BT_file
249*/
250int VoiceType()
251{
252  int i=fileVoice;
253  while ((FileAttribs[i].typ!=BT_proc)
254  &&(FileAttribs[i].typ!=BT_example)
255  &&(FileAttribs[i].typ!=BT_file)
256  &&(i>0))
257    i--;
258  return FileAttribs[i].typ;
259}
260/*2
261* start the file 'fname' (STDIN is stdin) in the current voice (cf.newVoice)
262*/
263Voices * Voices::VFile(char* fname)
264{
265  if (strcmp(fname,"STDIN") == 0)
266  {
267    yyin = stdin;
268    v_inputswitch = 0;
269  }
270  else
271  {
272    yyin = feFopen(fname,"r",NULL,TRUE);
273    v_inputswitch = -1;
274  }
275  files      = yyin;
276  filename   = mstrdup(fname);
277  v_echo     = si_echo;
278  fileVoice  = voice;
279  yylineno   = 1;
280  if (files==NULL)
281  {
282    inputswitch = 0;
283    exitVoice();
284    return NULL;
285  }
286  inputswitch= v_inputswitch;
287  return this;
288}
289
290/*3
291* increment voice counter, allocate new memory
292*/
293static inline void inc_voice()
294{
295  voice++;
296  if (voice >= levmax)
297  {
298    FileAttribs=(Voices *)ReAlloc(FileAttribs,
299                          levmax*sizeof(Voices),
300                          (levmax+16)*sizeof(Voices));
301    ifswitch=(short *)ReAlloc(ifswitch,
302                          levmax*sizeof(short),
303                          (levmax+16)*sizeof(short));
304    memset(&ifswitch[levmax],0,16*sizeof(short));
305    levmax+=16;
306  }
307}
308
309/*2
310* init a new voice similiar to the current
311*/
312void Voices::Next()
313{
314  v_oldlineno = yylineno;
315  v_echo      = si_echo;
316  v_fileVoice = fileVoice;
317  inc_voice();
318
319  currentVoice = &FileAttribs[voice];
320  currentVoice->Init();
321}
322
323/*2
324* start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile)
325*/
326int newVoice(char* s)
327{
328  currentVoice->Next();
329  return (int)currentVoice->VFile((char *)s);
330}
331
332void newBuffer(char* s, int t, char* pname)
333{
334  currentVoice->Next();
335  currentVoice->Buffer(s,t);
336  if (pname) currentVoice->filename = mstrdup(pname);
337  //printf("start buffer %d typ %d\n",voice,t);
338}
339
340Voices * Voices::Buffer(char* buf, int t)
341{
342  inputswitch = v_inputswitch = t;
343  buffer      = buf;
344  typ         = t;
345  //si_echo        = 0;
346  switch (t)
347  {
348    case BT_example:
349    case BT_proc:    v_lineno = yylineno; ::yylineno = 3;     break;
350    case BT_file:    v_lineno = yylineno; ::yylineno = 1;     break;
351    case BT_if:
352    case BT_else:    ::yylineno = v_lineno = blocklineno - 1; break;
353    case BT_break:   ::yylineno = v_lineno = blocklineno - 2; break;
354  }
355  return this;
356}
357
358/*2
359* after leaving a voice:
360* setup everything from the this level
361*/
362int Voices::Exit()
363{
364  if (voice >= 0)
365  {
366    si_echo          = v_echo;
367    fileVoice     = v_fileVoice;
368    yyin          = files;
369    yylineno      = v_oldlineno;
370    inputswitch   = v_inputswitch;
371    return 0;
372  }
373  //Print("Exit:%d\n",voice);
374  return 1;
375}
376
377/*2
378* exit Buffer of type 'typ':
379* returns 1 if buffer type could not be found
380*/
381int exitBuffer(int typ)
382{
383  //printf("exitBuffer: %d\n",typ);
384  if (typ == BT_break)  // valid inside for, while. may skip if, else
385  {
386    /*4 first check for valid buffer type, skip if/else*/
387    for (int i=voice; i>0; i--)
388    {
389      if ((FileAttribs[i].typ == BT_if)
390        ||(FileAttribs[i].typ == BT_else)) continue;
391      if (FileAttribs[i].typ == BT_break /*typ*/)
392      {
393        while ((/*typ*/ BT_break != currentVoice->typ)
394        && (voice > 0))
395        {
396          exitVoice();
397        }
398        return exitVoice();
399      }
400      else return 1;
401    }
402    /*4 break not inside a for/while: return an error*/
403    if (/*typ*/ BT_break != currentVoice->typ) return 1;
404    return exitVoice();
405  }
406
407  if ((typ == BT_proc)
408  || (typ == BT_example))
409  {
410    for (int i=voice; i>0; i--)
411    {
412      if (FileAttribs[i].typ == 0) break;
413      if ((FileAttribs[i].typ == BT_proc)
414      || (FileAttribs[i].typ == BT_example))
415      {
416        while ((BT_proc != currentVoice->typ)
417          && (BT_example != currentVoice->typ)
418        && (voice > 0))
419        {
420          exitVoice();
421        }
422        return exitVoice();
423      }
424    }
425  }
426  /*4 return not inside a proc: return an error*/
427  return 1;
428}
429
430/*2
431* jump to the beginning of a buffer
432*/
433int contBuffer(int typ)
434{
435  if (typ == BT_break)  // valid inside for, while. may skip if, else
436  {
437    // first check for valid buffer type
438    for (int i=voice; i>0; i--)
439    {
440      if ((FileAttribs[i].typ == BT_if)
441        ||(FileAttribs[i].typ == BT_else)) continue;
442      if (FileAttribs[i].typ == BT_break /*typ*/)
443      {
444        while (/*typ*/ BT_break != currentVoice->typ && (voice > i))
445        {
446          exitVoice();
447        }
448        currentVoice->fptr = 0L;
449        yylineno = currentVoice->v_lineno;
450        return 0;
451      }
452      else return 1;
453    }
454  }
455  return 1;
456}
457
458/*2
459* leave a file voice
460*/
461int exitFile()
462{
463  int oldswitch;
464
465  while ((voice > 0) && (inputswitch > 0))
466  {
467    exitVoice();
468  }
469  // now we have left all if-, else-, while-, for-, proc-levels
470  // inside this file;
471  // if the file is the terminal (inputswitch == 0) and
472  // voice >0, so return 1 else return 0
473  // (used for EXIT_CMD in CNTRLC-C-handling)
474  oldswitch = inputswitch;
475  exitVoice();
476  #ifdef SIC
477  return 1;
478  #else
479  if ((oldswitch)||(myynest<0)) return 0;
480  else return 1;
481  #endif
482}
483
484/*2
485* leave a voice: kill local variables
486* setup everything from the previous level (via Exit)
487*/
488int exitVoice()
489{
490  if (voice <= 0)   m2_end(0);
491  //printf("exitVoice %d, typ %d\n",voice,FileAttribs[voice].typ);
492  if (FileAttribs[voice].typ==BT_if)
493  {
494    ifswitch[voice-1]=2;
495  }
496  else
497  {
498    ifswitch[voice-1]=0;
499    //if ((FileAttribs[voice].typ==BT_proc)
500    //||(FileAttribs[voice].typ==BT_example)
501    //||(FileAttribs[voice].typ==0))
502    //{
503    //  killlocals(myynest);
504    //  printf("killlocals %d\n",myynest);
505    //}
506  }
507  if (inputswitch == -1)
508  {
509    fclose(yyin);
510  }
511  else if (inputswitch > 0)
512  {
513    if (FileAttribs[voice].filename!=NULL)
514    {
515      FreeL((ADDRESS)FileAttribs[voice].filename);
516      FileAttribs[voice].filename=NULL;
517    }
518    if (FileAttribs[voice].buffer!=NULL)
519    {
520      FreeL((ADDRESS)FileAttribs[voice].buffer);
521      FileAttribs[voice].buffer=NULL;
522    }
523  }
524  currentVoice = &FileAttribs[--voice];
525  return currentVoice->Exit();
526}
527
528int readbuf(char* buf, int l)
529{
530  char *s;
531  char * t = buf;
532  int i = 0;
533  long fl = currentVoice->fptr;
534  if (fl == -1L)
535  {
536    t[0] = '\0';
537    exitVoice();
538    return 0;
539  }
540
541  s = currentVoice->buffer + fl;
542  while (l > 0)
543  {
544    fl++;
545    i++;
546    l--;
547    *t++ = *s;
548    if (*s == '\n')
549    {
550      *t = '\0';
551      if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE)
552      || (traceit&TRACE_SHOW_LINE1))
553      {
554        if (currentVoice->filename==NULL)
555          Print("(none) %3d%c ",yylineno,prompt_char);
556        else if (VoiceType()!=BT_example)
557          Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
558        prompt_char = '.';
559      }
560      if (*(s+1) == '\0')
561      {
562        currentVoice->fptr = -1;
563        FreeL((ADDRESS)(currentVoice->buffer));
564        currentVoice->buffer=NULL;
565        FileAttribs[voice].buffer = NULL;
566        exitVoice();
567      }
568      else
569        currentVoice->fptr = fl;
570      return i;
571    }
572    else if (*s == '\0')
573    {
574      if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE)
575      || (traceit&TRACE_SHOW_LINE1))
576      {
577        if (currentVoice->filename==NULL)
578          Print("(none) %3d%c ",yylineno,prompt_char);
579        else
580          Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
581        prompt_char = '.';
582      }
583      currentVoice->fptr = -1;
584      FreeL((ADDRESS)(currentVoice->buffer));
585      currentVoice->buffer = NULL;
586      exitVoice();
587      return i-1;
588    }
589    s++;
590  }
591  currentVoice->fptr = fl;
592  return i;
593}
594
595/*2
596* init all data structures
597*/
598void I_FEbase(void)
599{
600  FileAttribs[0].files       = yyin = stdin;
601  FileAttribs[0].filename    = mstrdup("STDIN");
602  yylineno = 1;
603  currentVoice = &FileAttribs[0];
604}
605
606static char * feBufferStart;
607  /* only used in StringSet(S)/StringAppend(S)*/
608char * StringAppend(char *fmt, ...)
609{
610  va_list ap;
611  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
612  int more;
613  va_start(ap, fmt);
614  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
615  {
616    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
617    int l=s-feBuffer;
618    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
619                                                     more);
620    feBufferLength=more;
621    s=feBuffer+l;
622#ifndef BSD_SPRINTF
623    feBufferStart=s;
624#endif
625  }
626#ifdef BSD_SPRINTF
627  vsprintf(s, fmt, ap);
628  while (*s!='\0') s++;
629  feBufferStart =s;
630#else
631  feBufferStart += vsprintf(s, fmt, ap);
632#endif
633  va_end(ap);
634  return feBuffer;
635}
636
637char * StringAppendS(char *st)
638{
639  /* feBufferStart is feBuffer + strlen(feBuffer);*/
640  int more,l;
641  int ll=feBufferStart-feBuffer;
642  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
643  {
644    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
645    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
646                                                     more);
647    feBufferLength=more;
648    feBufferStart=feBuffer+ll;
649  }
650  strcat(feBufferStart, st);
651  feBufferStart +=l;
652  return feBuffer;
653}
654
655char * StringSet(char *fmt, ...)
656{
657  va_list ap;
658  char *s = feBuffer;
659  va_start(ap, fmt);
660#ifdef BSD_SPRINTF
661  vsprintf(s, fmt, ap);
662  while (*s!='\0') s++;
663  feBufferStart = s;
664#else
665  feBufferStart = feBuffer + vsprintf(s, fmt, ap);
666#endif
667  va_end(ap);
668  return feBuffer;
669}
670
671char * StringSetS(char *st)
672{
673  int more,l;
674  if ((l=strlen(st))>feBufferLength)
675  {
676    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
677    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
678                                                     more);
679    feBufferLength=more;
680  }
681  strcpy(feBuffer,st);
682  feBufferStart=feBuffer+l;
683  return feBuffer;
684}
685
686void PrintTCLS(char c, char *s)
687{
688#ifndef macintosh
689  int l=strlen(s);
690  if (l>0) PrintTCL(c,l,s);
691#endif
692}
693
694void WerrorS(char *s)
695{
696#ifdef HAVE_MPSR
697  if (feBatch)
698  {
699    if (feErrors==NULL)
700    {
701      feErrors=(char *)Alloc(256);
702      feErrorsLen=256;
703      strcpy(feErrors,s);
704    }
705    else
706    {
707      if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen)
708      {
709        feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256);
710        feErrorsLen+=256;
711      }
712      strcat(feErrors,s);
713    }
714    strcat(feErrors,"\n");
715  }
716  else
717#endif
718  {
719#ifdef HAVE_TCL
720    if (tclmode)
721    {
722      //PrintTCLS('E',s);
723      //PrintTCLS('E',"\n");
724      PrintTCLS('N',s);
725      PrintTCLS('N',"\n");
726    }
727    else
728#endif
729    {
730      fwrite("   ? ",1,5,stderr);
731      fwrite(s,1,strlen(s),stderr);
732      fwrite("\n",1,1,stderr);
733      fflush(stderr);
734      if (feProt&PROT_O)
735      {
736        fwrite("   ? ",1,5,feProtFile);
737        fwrite(s,1,strlen(s),feProtFile);
738        fwrite("\n",1,1,feProtFile);
739      }
740    }
741  }
742  errorreported = TRUE;
743}
744
745extern "C" {
746void Werror(char *fmt, ...)
747{
748  va_list ap;
749  va_start(ap, fmt);
750  char *s=(char *)Alloc(256);
751  vsprintf(s, fmt, ap);
752  WerrorS(s);
753  Free(s,256);
754  va_end(ap);
755}
756}
757
758void WarnS(char *s)
759{
760#ifdef HAVE_TCL
761  if (tclmode)
762  {
763    PrintTCLS('W',s);
764  }
765  else
766#endif
767  {
768    fwrite("// ** ",1,6,stdout);
769    fwrite(s,1,strlen(s),stdout);
770    fwrite("\n",1,1,stdout);
771    fflush(stdout);
772    if (feProt&PROT_O)
773    {
774      fwrite("// ** ",1,6,feProtFile);
775      fwrite(s,1,strlen(s),feProtFile);
776      fwrite("\n",1,1,feProtFile);
777    }
778  }
779}
780
781void Warn(char *fmt, ...)
782{
783  va_list ap;
784  va_start(ap, fmt);
785  char *s=(char *)Alloc(256);
786  vsprintf(s, fmt, ap);
787  WarnS(s);
788  Free(s,256);
789  va_end(ap);
790}
791
792#ifdef macintosh
793static  int lines = 0;
794static  int cols = 0;
795void mwrite(uchar c)
796{
797  if (c == '\n')
798  {
799    cols = 0;
800    if (lines == pagelength)
801    {
802      lines = 0;
803      fePause();
804    }
805    else
806    {
807      lines++;
808      fePutChar(c);
809    }
810  }
811  else
812  {
813    fePutChar(c);
814    cols++;
815    if (cols == colmax)
816    {
817//      cols = 0;   //will be done by mwrite('\n');
818      mwrite('\n');
819    }
820  }
821}
822#endif
823
824void PrintS(char *s)
825{
826#ifdef macintosh
827  char c;
828  while ('\0' != (c = *s++))
829  {
830    mwrite(c);
831  }
832#else
833#ifdef HAVE_TCL
834  if (tclmode)
835  {
836    PrintTCLS('N',s);
837  }
838  else
839#endif
840  {
841    fwrite(s,1,strlen(s),stdout);
842    fflush(stdout);
843    if (feProt&PROT_O)
844    {
845      fwrite(s,1,strlen(s),feProtFile);
846    }
847  }
848#endif
849}
850
851void Print(char *fmt, ...)
852{
853  va_list ap;
854  va_start(ap, fmt);
855#ifdef HAVE_TCL
856  if(tclmode)
857#endif
858#if (defined(HAVE_TCL) || defined(macintosh))
859  {
860    char *s=(char *)Alloc(strlen(fmt)+256);
861    vsprintf(s,fmt, ap);
862#ifdef HAVE_TCL
863    PrintTCLS('N',s);
864#endif
865#ifdef macintosh
866  char c;
867  while ('\0' != (c = *s++))
868  {
869    mwrite(c);
870  }
871  if (feProt&PROT_O)
872  {
873    vfprintf(feProrFile,fmt,ap);
874  }
875#endif
876  }
877#endif
878#if !defined(macintosh) || defined(HAVE_TCL)
879#ifdef HAVE_TCL
880  else
881#endif
882  {
883    vfprintf(stdout, fmt, ap);
884    fflush(stdout);
885    if (feProt&PROT_O)
886    {
887      vfprintf(feProtFile,fmt,ap);
888    }
889  }
890#endif
891  va_end(ap);
892}
893
894void PrintLn()
895{
896  PrintS("\n");
897}
898
899void fePause()
900{
901  uchar c;
902  mflush();
903#ifndef macintosh
904  fputs("pause>",stderr);
905#else
906  fputs("pause>\n",stderr);
907#endif
908  c = fgetc(stdin);
909  if (((c == '\003') || (c == 'C')) || (c == 'c'))
910  {
911    m2_end(1);
912  }
913}
914
915/*2
916* print input lines (si_echo or TRACE), set prompt_char
917*/
918void showInput(void)
919{
920  if ((inputswitch <= 0) || (traceit&TRACE_SHOW_LINE)
921  || (traceit&TRACE_SHOW_LINE1))
922  {
923    if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE)
924    || (traceit&TRACE_SHOW_LINE1))
925    {
926#ifdef HAVE_TCL
927      if (tclmode)
928      {
929         PrintTCL('P',(prompt_char=='>')? 0 : 1,NULL);
930      }
931      else
932#endif
933      if (BVERBOSE(V_PROMPT))
934      {
935        if (inputswitch == 0)
936        {
937          fe_promptstr[0]=prompt_char;
938#ifndef HAVE_READLINE
939          PrintS(fe_promptstr);
940#endif
941        }
942        else
943        {
944          if (currentVoice->filename==NULL)
945            Print("(none) %3d%c ",yylineno,prompt_char);
946          else
947            Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
948        }
949        prompt_char = '.';
950        mflush();
951      }
952#ifdef macintosh
953      cols = 0;
954#endif
955    }
956  }
957}
958
959void monitor(char* s, int mode)
960{
961  if (feProt)
962  {
963    fclose(feProtFile);
964  }
965  if ((s!=NULL) && (*s!='\0'))
966  {
967    feProtFile = fopen(s,"w");
968    if (feProtFile==NULL)
969    {
970      Werror("cannot open %s",s);
971    }
972    else
973      feProt = (BOOLEAN)mode;
974  }
975}
976
977
978char* eati(char *s, int *i)
979{
980  int l=0;
981
982  if    (*s >= '0' && *s <= '9')
983  {
984    *i = 0;
985    while (*s >= '0' && *s <= '9')
986    {
987      *i *= 10;
988      *i += *s++ - '0';
989      l++;
990      if ((l>MAX_INT_LEN)||((*i) <0))
991      {
992        s-=l;
993        Werror("`%s` greater than %d(max. integer representation)",
994                s,INT_MAX);
995        return s;
996      }
997    }
998  }
999  else *i = 1;
1000  return s;
1001}
1002
Note: See TracBrowser for help on using the repository browser.