source: git/Singular/fevoices.cc @ f4dd49

fieker-DuValspielwiese
Last change on this file since f4dd49 was f9b0bd, checked in by Hans Schoenemann <hannes@…>, 8 years ago
chg: Print -> PrintS, PrintLn if possible
  • Property mode set to 100644
File size: 15.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: i/o system
6*/
7#include <kernel/mod2.h>
8
9/* I need myfread in standalone_parser */
10#ifndef STANDALONE_PARSER
11
12#include <omalloc/omalloc.h>
13#include <misc/options.h>
14#include <reporter/reporter.h>
15#include <kernel/oswrapper/feread.h>
16#include <Singular/fevoices.h>
17#include <Singular/subexpr.h>
18#include <Singular/ipshell.h>
19#include <Singular/sdb.h>
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <misc/mylimits.h>
24#include <stdarg.h>
25#include <sys/stat.h>
26#include <ctype.h>
27#include <unistd.h>
28
29#ifdef HAVE_PWD_H
30#include <pwd.h>
31#endif
32
33#define fePutChar(c) fputc((unsigned char)(c),stdout)
34/*0 implementation */
35
36
37char fe_promptstr[] ="  ";
38FILE *File_Profiling=NULL;
39
40// output/print buffer:
41#define INITIAL_PRINT_BUFFER 24*1024L
42// line buffer for reading:
43// minimal value for MAX_FILE_BUFFER: 4*4096 - see Tst/Long/gcd0_l.tst
44// this is an upper limit for the size of monomials/numbers read via the interpreter
45#define MAX_FILE_BUFFER 4*4096
46// static long feBufferLength=INITIAL_PRINT_BUFFER;
47static char * feBuffer=(char *)omAlloc(INITIAL_PRINT_BUFFER);
48
49/**************************************************************************
50* handling of 'voices'
51**************************************************************************/
52
53extern int blocknest; /* scaner.l internal */
54
55int    yy_noeof=0;     // the scanner "state"
56int    yy_blocklineno; // to get the lineno of the block start from scanner
57Voice  *currentVoice = NULL;
58// FILE   *feFilePending; /*temp. storage for grammar.y */
59
60//static const char * BT_name[]={"BT_none","BT_break","BT_proc","BT_example",
61//                               "BT_file","BT_execute","BT_if","BT_else"};
62/*2
63* the name of the current 'Voice': the procname (or filename)
64*/
65const char * sNoName_fe="_";
66const char * VoiceName()
67{
68  if ((currentVoice!=NULL)
69  && (currentVoice->filename!=NULL))
70    return currentVoice->filename;
71  return sNoName_fe;
72}
73
74/*2
75* the calling chain of Voices
76*/
77void VoiceBackTrack()
78{
79  Voice *p=currentVoice;
80  while (p->prev!=NULL)
81  {
82    p=p->prev;
83    char *s=p->filename;
84    if (s==NULL)
85      PrintS("-- called from ? --\n");
86    else
87      Print("-- called from %s --\n",s);
88  }
89}
90
91/*2
92* init a new voice similar to the current
93*/
94void Voice::Next()
95{
96  Voice *p=new Voice;
97  // OB: ???
98  // Hmm... when Singular is used as batch file
99  // then this voice is never freed
100  omMarkAsStaticAddr(p);
101  if (currentVoice != NULL)
102  {
103    currentVoice->curr_lineno=yylineno;
104    currentVoice->next=p;
105  }
106  p->prev=currentVoice;
107  currentVoice=p;
108  //PrintS("Next:");
109}
110
111feBufferTypes Voice::Typ()
112{
113  switch(typ)
114  {
115    case BT_proc:
116    case BT_example:
117    case BT_file:
118      return typ;
119    default:
120      if (prev==NULL) return (feBufferTypes)0;
121      return prev->Typ();
122  }
123}
124
125/*2
126* start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile)
127* return FALSE on success, TRUE if an error occurs (file cannot be opened)
128*/
129BOOLEAN newFile(char *fname,FILE* f)
130{
131  currentVoice->Next();
132  //Print(":File%d(%s):%s(%x)\n",
133  //  currentVoice->typ,BT_name[currentVoice->typ],fname,currentVoice);
134  currentVoice->filename   = omStrDup(fname);
135  omMarkAsStaticAddr(currentVoice->filename);
136  if (strcmp(fname,"STDIN") == 0)
137  {
138    currentVoice->files = stdin;
139    currentVoice->sw = BI_stdin;
140    currentVoice->start_lineno = 1;
141  }
142  else
143  {
144    currentVoice->sw = BI_file; /* needed by exitVoice below */
145    if (f!=NULL)
146      currentVoice->files = f;
147    else
148    {
149      currentVoice->files = feFopen(fname,"r",NULL,TRUE);
150      if (currentVoice->files==NULL)
151      {
152        exitVoice();
153        return TRUE;
154      }
155    }
156    currentVoice->start_lineno = 0;
157  }
158  yylineno=currentVoice->start_lineno;
159  //Voice *p=currentVoice;
160  //PrintS("-----------------\ncurr:");
161  //do
162  //{
163  //Print("voice fn:%s\n",p->filename);
164  //p=p->prev;
165  //}
166  //while (p!=NULL);
167  //PrintS("----------------\n");
168  return FALSE;
169}
170
171void newBuffer(char* s, feBufferTypes t, procinfo* pi, int lineno)
172{
173  currentVoice->Next();
174  //Print(":Buffer%d(%s):%s(%x)\n",
175  //  t,BT_name[t],pname,currentVoice);
176  if (pi!=NULL)
177  {
178    long l=strlen(pi->procname);
179    if (pi->libname!=NULL) l+=strlen(pi->libname);
180    currentVoice->filename = (char *)omAlloc(l+3);
181    *currentVoice->filename='\0';
182    if (pi->libname!=NULL) strcat(currentVoice->filename,pi->libname);
183    strcat(currentVoice->filename,"::");
184    strcat(currentVoice->filename,pi->procname);
185    currentVoice->pi       = pi;
186  }
187  else
188  {
189    if(currentVoice->prev!=NULL)
190    {
191      currentVoice->filename = omStrDup(currentVoice->prev->filename);
192      currentVoice->pi       = currentVoice->prev->pi;
193    }
194    else
195    {
196      currentVoice->filename = omStrDup("");
197      currentVoice->pi       = pi;
198    }
199  }
200  currentVoice->buffer   = s;
201  currentVoice->sw       = BI_buffer;
202  currentVoice->typ      = t;
203  switch (t)
204  {
205    case BT_execute:
206                     yylineno-=2;
207                     break;
208    case BT_proc:
209    case BT_example:
210                     currentVoice->oldb=myynewbuffer();
211                     yylineno = lineno+1;
212                     break;
213    case BT_if:
214    case BT_else:
215    case BT_break:
216                     yylineno = yy_blocklineno-1;
217                     break;
218    //case BT_file:
219    default:
220                     yylineno = 1;
221                     break;
222  }
223  //Print("start body (%s) at line %d\n",BT_name[t],yylineno);
224  currentVoice->start_lineno = yylineno;
225  //printf("start buffer typ %d\n",t);
226  //Voice *p=currentVoice;
227  //PrintS("-----------------\ncurr:");
228  //do
229  //{
230  //Print("voice fn:%s\n",p->filename);
231  //p=p->prev;
232  //}
233  //while (p!=NULL);
234  //PrintS("----------------\n");
235}
236
237/*2
238* exit Buffer of type 'typ':
239* returns 1 if buffer type could not be found
240*/
241BOOLEAN exitBuffer(feBufferTypes typ)
242{
243  //printf("exitBuffer: %d(%s),(%x)\n",
244  //  typ,BT_name[typ], currentVoice);
245  //Voice *p=currentVoice;
246  //PrintS("-----------------\ncurr:");
247  //do
248  //{
249  //Print("voice fn:%s\n",p->filename);
250  //p=p->prev;
251  //}
252  //while (p!=NULL);
253  //PrintS("----------------\n");
254  if (typ == BT_break)  // valid inside for, while. may skip if, else
255  {
256    /*4 first check for valid buffer type, skip if/else*/
257    Voice *p=currentVoice;
258    loop
259    {
260      if ((p->typ != BT_if)
261      &&(p->typ != BT_else))
262      {
263        if (p->typ == BT_break /*typ*/)
264        {
265          while (p != currentVoice)
266          {
267            exitVoice();
268          }
269          exitVoice();
270          return FALSE;
271        }
272        else return TRUE;
273      }
274      if (p->prev==NULL) break;
275      p=p->prev;
276    }
277    /*4 break not inside a for/while: return an error*/
278    if (/*typ*/ BT_break != currentVoice->typ) return 1;
279    return exitVoice();
280  }
281
282  if ((typ == BT_proc)
283  || (typ == BT_example))
284  {
285    Voice *p=currentVoice;
286    loop
287    {
288      if ((p->typ == BT_proc)
289      || (p->typ == BT_example))
290      {
291        while (p != currentVoice)
292        {
293          exitVoice();
294        }
295        exitVoice();
296        return FALSE;
297      }
298      if (p->prev==NULL) break;
299      p=p->prev;
300    }
301  }
302  /*4 return not inside a proc: return an error*/
303  return TRUE;
304}
305
306/*2
307* jump to the beginning of a buffer
308*/
309BOOLEAN contBuffer(feBufferTypes typ)
310{
311  //printf("contBuffer: %d(%s),(%x)\n",
312  //  typ,BT_name[typ], currentVoice);
313  if (typ == BT_break)  // valid inside for, while. may skip if, else
314  {
315    // first check for valid buffer type
316    Voice *p=currentVoice;
317    loop
318    {
319      if ((p->typ != BT_if)
320        &&(p->typ != BT_else))
321      {
322        if (p->typ == BT_break /*typ*/)
323        {
324          while (p != currentVoice)
325          {
326            exitVoice();
327          }
328          yylineno = currentVoice->start_lineno;
329          currentVoice->fptr=0;
330          return FALSE;
331        }
332        else return TRUE;
333      }
334      if (p->prev==NULL) break;
335      p=p->prev;
336    }
337  }
338  return TRUE;
339}
340
341/*2
342* leave a voice: kill local variables
343* setup everything from the previous level
344* return 1 if leaving the top level, 0 otherwise
345*/
346BOOLEAN exitVoice()
347{
348  //printf("exitVoice: %d(%s),(%x)\n",
349  //  currentVoice->typ,BT_name[currentVoice->typ], currentVoice);
350  //{
351  //Voice *p=currentVoice;
352  //PrintS("-----------------\ncurr:");
353  //do
354  //{
355  //Print("voice fn:%s\n",p->filename);
356  //p=p->prev;
357  //}
358  //while (p!=NULL);
359  //PrintS("----------------\n");
360  //}
361  if (currentVoice!=NULL)
362  {
363    if (currentVoice->oldb!=NULL)
364    {
365      myyoldbuffer(currentVoice->oldb);
366      currentVoice->oldb=NULL;
367    }
368    if (currentVoice->filename!=NULL)
369    {
370      omFree((ADDRESS)currentVoice->filename);
371      currentVoice->filename=NULL;
372    }
373    if (currentVoice->buffer!=NULL)
374    {
375      omFree((ADDRESS)currentVoice->buffer);
376      currentVoice->buffer=NULL;
377    }
378    if ((currentVoice->prev==NULL)&&(currentVoice->sw==BI_file))
379    {
380      currentVoice->prev=feInitStdin(currentVoice);
381    }
382    if (currentVoice->prev!=NULL)
383    {
384      //printf("exitVoice typ %d(%s)\n",
385      //  currentVoice->typ,BT_name[currentVoice->typ]);
386      if (currentVoice->typ==BT_if)
387      {
388        currentVoice->prev->ifsw=2;
389      }
390      else
391      {
392        currentVoice->prev->ifsw=0;
393      }
394      if ((currentVoice->sw == BI_file)
395      && (currentVoice->files!=NULL))
396      {
397        fclose(currentVoice->files);
398      }
399      yylineno=currentVoice->prev->curr_lineno;
400      currentVoice->prev->next=NULL;
401    }
402    Voice *p=currentVoice->prev;
403    delete currentVoice;
404    currentVoice=p;
405  }
406  return currentVoice==NULL;
407}
408
409/*2
410* set prompt_char
411* only called with currentVoice->sw == BI_stdin
412*/
413static void feShowPrompt(void)
414{
415  fe_promptstr[0]=prompt_char;
416}
417
418/*2
419* print echo (si_echo or TRACE), set my_yylinebuf
420*/
421static int fePrintEcho(char *anf, char */*b*/)
422{
423  char *ss=strrchr(anf,'\n');
424  int len_s;
425  if (ss==NULL)
426  {
427    len_s=strlen(anf);
428  }
429  else
430  {
431    len_s=ss-anf+1;
432  }
433  // my_yylinebuf:
434  int mrc=si_min(len_s,79)-1;
435  strcpy(my_yylinebuf,anf+(len_s-1)-mrc);
436  if (my_yylinebuf[mrc] == '\n') my_yylinebuf[mrc] = '\0';
437  mrc--;
438  // handle echo:
439  if (((si_echo>myynest)
440    && ((currentVoice->typ==BT_proc)
441      || (currentVoice->typ==BT_example)
442      || (currentVoice->typ==BT_file)
443      || (currentVoice->typ==BT_none)
444    )
445    && (strncmp(anf,";return();",10)!=0)
446   )
447  || (traceit&TRACE_SHOW_LINE)
448  || (traceit&TRACE_SHOW_LINE1))
449  {
450    if (currentVoice->typ!=BT_example)
451    {
452      if (currentVoice->filename==NULL)
453        Print("(none) %3d%c ",yylineno,prompt_char);
454      else
455        Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
456     }
457    {
458      fwrite(anf,1,len_s,stdout);
459      mflush();
460    }
461    if (traceit&TRACE_SHOW_LINE)
462    {
463      while(fgetc(stdin)!='\n');
464    }
465  }
466  else if (traceit&TRACE_SHOW_LINENO)
467  {
468    Print("{%d}",yylineno);
469    mflush();
470  }
471  else if (traceit&TRACE_PROFILING)
472  {
473    if (File_Profiling==NULL)
474      File_Profiling=fopen("smon.out","a");
475    if (File_Profiling==NULL)
476      traceit &= (~TRACE_PROFILING);
477    else
478    {
479      if (currentVoice->filename==NULL)
480        fprintf(File_Profiling,"(none) %d\n",yylineno);
481      else
482        fprintf(File_Profiling,"%s %d\n",currentVoice->filename,yylineno);
483    }
484  }
485#ifdef HAVE_SDB
486  if ((blocknest==0)
487  && (currentVoice->pi!=NULL)
488  && (currentVoice->pi->trace_flag!=0))
489  {
490    sdb(currentVoice, anf, len_s);
491  }
492#endif
493  prompt_char = '.';
494  return len_s;
495}
496
497int feReadLine(char* b, int l)
498{
499  char *s=NULL;
500  int offset = 0; /* will not be used if s==NULL*/
501  // try to read from the buffer into b, max l chars
502  if (currentVoice!=NULL)
503  {
504    if((currentVoice->buffer!=NULL)
505    && (currentVoice->buffer[currentVoice->fptr]!='\0'))
506    {
507  NewBuff:
508      register int i=0;
509      long startfptr=currentVoice->fptr;
510      long tmp_ptr=currentVoice->fptr;
511      l--;
512      loop
513      {
514        register char c=
515        b[i]=currentVoice->buffer[tmp_ptr/*currentVoice->fptr*/];
516        i++;
517        if (yy_noeof==noeof_block)
518        {
519          if (c<' ')  yylineno++;
520          else if (c=='}') break;
521        }
522        else
523        {
524          if ((c<' ') ||
525          (c==';') ||
526          (c==')')
527          )
528            break;
529        }
530        if (i>=l) break;
531        tmp_ptr++;/*currentVoice->fptr++;*/
532        if(currentVoice->buffer[tmp_ptr/*currentVoice->fptr*/]=='\0') break;
533      }
534      currentVoice->fptr=tmp_ptr;
535      b[i]='\0';
536      if (currentVoice->sw==BI_buffer)
537      {
538        if (startfptr==0)
539        {
540          char *anf=currentVoice->buffer;
541          const char *ss=strchr(anf,'\n');
542          long len;
543          if (ss==NULL) len=strlen(anf);
544          else          len=ss-anf;
545          char *s=(char *)omAlloc(len+2);
546          strncpy(s,anf,len+2);
547          s[len+1]='\0';
548          fePrintEcho(s,b);
549          omFree((ADDRESS)s);
550        }
551        else if (/*(startfptr>0) &&*/
552        (currentVoice->buffer[startfptr-1]=='\n'))
553        {
554          char *anf=currentVoice->buffer+startfptr;
555          const char *ss=strchr(anf,'\n');
556          long len;
557          if (ss==NULL) len=strlen(anf);
558          else          len=ss-anf;
559          char *s=(char *)omAlloc(len+2);
560          strncpy(s,anf,len+2);
561          s[len+1]='\0';
562          yylineno++;
563          fePrintEcho(s,b);
564          omFree((ADDRESS)s);
565        }
566      }
567      currentVoice->fptr++;
568      return i;
569    }
570    // no buffer there or e-o-buffer or eoln:
571    if (currentVoice->sw!=BI_buffer)
572    {
573      currentVoice->fptr=0;
574      if (currentVoice->buffer==NULL)
575      {
576        currentVoice->buffer=(char *)omAlloc(MAX_FILE_BUFFER-sizeof(ADDRESS));
577        omMarkAsStaticAddr(currentVoice->buffer);
578      }
579    }
580    offset=0;
581  NewRead:
582    yylineno++;
583    if (currentVoice->sw==BI_stdin)
584    {
585      feShowPrompt();
586      s=fe_fgets_stdin(fe_promptstr,
587                       &(currentVoice->buffer[offset]),
588                       omSizeOfAddr(currentVoice->buffer)-1-offset);
589      //int i=0;
590      //if (s!=NULL)
591      //  while((s[i]!='\0') /*&& (i<MAX_FILE_BUFFER)*/) {s[i] &= (char)127;i++;}
592    }
593    else if (currentVoice->sw==BI_file)
594    {
595      fseek(currentVoice->files,currentVoice->ftellptr,SEEK_SET);
596      s=fgets(currentVoice->buffer+offset,(MAX_FILE_BUFFER-1-sizeof(ADDRESS))-offset,
597              currentVoice->files);
598      if (s!=NULL) currentVoice->ftellptr=ftell(currentVoice->files);
599    }
600    //else /* BI_buffer */ s==NULL  => return 0
601    // done by the default return
602  }
603  if (s!=NULL)
604  {
605    // handle prot:
606    if (feProt&SI_PROT_I)
607    {
608      fputs(s,feProtFile);
609    }
610    int rc=fePrintEcho(s,b)+1;
611    //s[strlen(s)+1]='\0'; add an second \0 at the end of the string
612    s[rc]='\0';
613    // handel \\ :
614    rc-=3;
615    if ((s[rc]=='\\')&&(currentVoice->sw!=BI_buffer))
616    {
617      s[rc]='\0';
618      offset+=rc;
619      if (offset<(int)omSizeOfAddr(currentVoice->buffer)) goto NewRead;
620    }
621    goto NewBuff;
622  }
623  /* else if (s==NULL) */
624  {
625    const char *err;
626    switch(yy_noeof)
627    {
628      case noeof_brace:
629      case noeof_block:
630        err="{...}";
631        break;
632      case noeof_asstring:
633        err="till `.`";
634        break;
635      case noeof_string:
636        err="string";
637        break;
638      case noeof_bracket:
639        err="(...)";
640        break;
641      case noeof_procname:
642        err="proc";
643        break;
644      case noeof_comment:
645        err="/*...*/";
646        break;
647      default:
648        return 0;
649    }
650    Werror("premature end of file while reading %s",err);
651    return 0;
652  }
653}
654
655/*2
656* init all data structures
657*/
658#ifndef STDIN_FILENO
659#define STDIN_FILENO 0
660#endif
661Voice * feInitStdin(Voice *pp)
662{
663  Voice *p = new Voice;
664  p->files = stdin;
665  p->sw = (isatty(STDIN_FILENO)) ? BI_stdin : BI_file;
666  if ((pp!=NULL) && (pp->files==stdin))
667  {
668    p->files=freopen("/dev/tty","r",stdin);
669    //stdin=p->files;
670    p->sw = BI_stdin;
671  }
672  p->filename   = omStrDup("STDIN");
673  p->start_lineno   = 1;
674  omMarkAsStaticAddr(p);
675  omMarkAsStaticAddr(p->filename);
676  return p;
677}
678
679
680#else /* ! STANDALONE_PARSER */
681#include <stdio.h>
682
683#endif
684
Note: See TracBrowser for help on using the repository browser.