Changeset fdd985e in git for Singular/febase.cc


Ignore:
Timestamp:
Nov 26, 2002, 2:35:10 PM (21 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'b4f17ed1d25f93d46dbe29e4b499baecc2fd51bb')
Children:
a3f03d9761532c4cc928121959a54504dbd25467
Parents:
7194f62c9190280fc120f46ad1d5edbc8993e3fa
Message:
*hannes: merge v-2-0: removed febase.inc


git-svn-id: file:///usr/local/Singular/svn/trunk@6340 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/febase.cc

    r7194f62 rfdd985e  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: febase.cc,v 1.95 2001-08-27 14:46:58 Singular Exp $ */
     4/* $Id: febase.cc,v 1.96 2002-11-26 13:34:21 Singular Exp $ */
    55/*
    66* ABSTRACT: i/o system
     
    1616#include <stdarg.h>
    1717#include <sys/stat.h>
     18#include <ctype.h>
    1819#ifndef __MWERKS__
    1920#include <unistd.h>
     
    3334#include "ipshell.h"
    3435#include "dError.h"
     36#include "sdb.h"
    3537
    3638#define fePutChar(c) fputc((uchar)(c),stdout)
     
    9597*/
    9698
    97 #include "febase.inc"
    98 
     99
     100/**************************************************************************
     101* handling of 'voices'
     102**************************************************************************/
     103
     104extern int blocknest; /* scaner.l internal */
     105
     106int    yy_noeof=0;     // the scanner "state"
     107int    yy_blocklineno; // to get the lineno of the block start from scanner
     108Voice  *currentVoice = NULL;
     109FILE   *feFilePending; /*temp. storage for grammar.y */
     110
     111static char * BT_name[]={"BT_none","BT_break","BT_proc","BT_example",
     112                       "BT_file","BT_execute","BT_if","BT_else"};
     113/*2
     114* the name of the current 'Voice': the procname (or filename)
     115*/
     116const char * VoiceName()
     117{
     118  if ((currentVoice!=NULL)
     119  && (currentVoice->filename!=NULL))
     120    return currentVoice->filename;
     121  return sNoName;
     122}
     123
     124/*2
     125* the calling chain of Voices
     126*/
     127void VoiceBackTrack()
     128{
     129  Voice *p=currentVoice;
     130  while (p->prev!=NULL)
     131  {
     132    p=p->prev;
     133    char *s=p->filename;
     134    if (s==NULL)
     135      PrintS("-- called from ? --\n");
     136    else
     137      Print("-- called from %s --\n",s);
     138  }
     139}
     140
     141/*2
     142* init a new voice similiar to the current
     143*/
     144void Voice::Next()
     145{
     146  Voice *p=new Voice;
     147  // OB: ???
     148  // Hmm... when Singular is used as batch file
     149  // then this voice is never freed
     150  omMarkAsStaticAddr(p);
     151  if (currentVoice != NULL)
     152  {
     153    currentVoice->curr_lineno=yylineno;
     154    currentVoice->next=p;
     155  }
     156  p->prev=currentVoice;
     157  currentVoice=p;
     158  //Print("Next:");
     159}
     160
     161feBufferTypes Voice::Typ()
     162{
     163  switch(typ)
     164  {
     165    case BT_proc:
     166    case BT_example:
     167    case BT_file:
     168      return typ;
     169    default:
     170      if (prev==NULL) return (feBufferTypes)0;
     171      return prev->Typ();
     172  }
     173}
     174
     175/*2
     176* start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile)
     177* return FALSE on success, TRUE if an error occurs (file cannot be opened)
     178*/
     179BOOLEAN newFile(char *fname,FILE* f)
     180{
     181  currentVoice->Next();
     182  //Print(":File%d(%s):%s(%x)\n",
     183  //  currentVoice->typ,BT_name[currentVoice->typ],fname,currentVoice);
     184  currentVoice->filename   = omStrDup(fname);
     185  omMarkAsStaticAddr(currentVoice->filename);
     186  if (strcmp(fname,"STDIN") == 0)
     187  {
     188    currentVoice->files = stdin;
     189    currentVoice->sw = BI_stdin;
     190    currentVoice->start_lineno = 1;
     191  }
     192  else
     193  {
     194    currentVoice->sw = BI_file; /* needed by exitVoice below */
     195    if (f!=NULL)
     196      currentVoice->files = f;
     197    else
     198    {
     199      currentVoice->files = feFopen(fname,"r",NULL,TRUE);
     200      if (currentVoice->files==NULL)
     201      {
     202        exitVoice();
     203        return TRUE;
     204      }
     205    }
     206    currentVoice->start_lineno = 0;
     207  }
     208  yylineno=currentVoice->start_lineno;
     209  //Voice *p=currentVoice;
     210  //Print("-----------------\ncurr:");
     211  //do
     212  //{
     213  //Print("voice fn:%s\n",p->filename);
     214  //p=p->prev;
     215  //}
     216  //while (p!=NULL);
     217  //Print("----------------\n");
     218  return FALSE;
     219}
     220
     221void newBuffer(char* s, feBufferTypes t, procinfo* pi, int lineno)
     222{
     223  currentVoice->Next();
     224  //Print(":Buffer%d(%s):%s(%x)\n",
     225  //  t,BT_name[t],pname,currentVoice);
     226  if (pi!=NULL)
     227  {
     228    int l=strlen(pi->procname);
     229    if (pi->libname!=NULL) l+=strlen(pi->libname);
     230    currentVoice->filename = (char *)omAlloc(l+3);
     231    *currentVoice->filename='\0';
     232    if (pi->libname!=NULL) strcat(currentVoice->filename,pi->libname);
     233    strcat(currentVoice->filename,"::");
     234    strcat(currentVoice->filename,pi->procname);
     235    currentVoice->pi       = pi;
     236  }
     237  else
     238  {
     239    currentVoice->filename = omStrDup(currentVoice->prev->filename);
     240    currentVoice->pi       = currentVoice->prev->pi;
     241  }
     242  currentVoice->buffer   = s;
     243  currentVoice->sw       = BI_buffer;
     244  currentVoice->typ      = t;
     245  switch (t)
     246  {
     247    case BT_execute:
     248                     yylineno-=2;
     249                     break;
     250    case BT_proc:
     251    case BT_example:
     252                     currentVoice->oldb=myynewbuffer();
     253                     yylineno = lineno+1;
     254                     break;
     255    case BT_if:
     256    case BT_else:
     257    case BT_break:
     258                     yylineno = yy_blocklineno-1;
     259                     break;
     260    //case BT_file:
     261    default:
     262                     yylineno = 1;
     263                     break;
     264  }
     265  //Print("start body (%s) at line %d\n",BT_name[t],yylineno);
     266  currentVoice->start_lineno = yylineno;
     267  //printf("start buffer typ %d\n",t);
     268  //Voice *p=currentVoice;
     269  //Print("-----------------\ncurr:");
     270  //do
     271  //{
     272  //Print("voice fn:%s\n",p->filename);
     273  //p=p->prev;
     274  //}
     275  //while (p!=NULL);
     276  //Print("----------------\n");
     277}
     278
     279/*2
     280* exit Buffer of type 'typ':
     281* returns 1 if buffer type could not be found
     282*/
     283BOOLEAN exitBuffer(feBufferTypes typ)
     284{
     285  //printf("exitBuffer: %d(%s),(%x)\n",
     286  //  typ,BT_name[typ], currentVoice);
     287  //Voice *p=currentVoice;
     288  //Print("-----------------\ncurr:");
     289  //do
     290  //{
     291  //Print("voice fn:%s\n",p->filename);
     292  //p=p->prev;
     293  //}
     294  //while (p!=NULL);
     295  //Print("----------------\n");
     296  if (typ == BT_break)  // valid inside for, while. may skip if, else
     297  {
     298    /*4 first check for valid buffer type, skip if/else*/
     299    Voice *p=currentVoice;
     300    loop
     301    {
     302      if ((p->typ != BT_if)
     303      &&(p->typ != BT_else))
     304      {
     305        if (p->typ == BT_break /*typ*/)
     306        {
     307          while (p != currentVoice)
     308          {
     309            exitVoice();
     310          }
     311          exitVoice();
     312          return FALSE;
     313        }
     314        else return TRUE;
     315      }
     316      if (p->prev==NULL) break;
     317      p=p->prev;
     318    }
     319    /*4 break not inside a for/while: return an error*/
     320    if (/*typ*/ BT_break != currentVoice->typ) return 1;
     321    return exitVoice();
     322  }
     323
     324  if ((typ == BT_proc)
     325  || (typ == BT_example))
     326  {
     327    Voice *p=currentVoice;
     328    loop
     329    {
     330      if ((p->typ == BT_proc)
     331      || (p->typ == BT_example))
     332      {
     333        while (p != currentVoice)
     334        {
     335          exitVoice();
     336        }
     337        exitVoice();
     338        return FALSE;
     339      }
     340      if (p->prev==NULL) break;
     341      p=p->prev;
     342    }
     343  }
     344  /*4 return not inside a proc: return an error*/
     345  return TRUE;
     346}
     347
     348/*2
     349* jump to the beginning of a buffer
     350*/
     351BOOLEAN contBuffer(feBufferTypes typ)
     352{
     353  //printf("contBuffer: %d(%s),(%x)\n",
     354  //  typ,BT_name[typ], currentVoice);
     355  if (typ == BT_break)  // valid inside for, while. may skip if, else
     356  {
     357    // first check for valid buffer type
     358    Voice *p=currentVoice;
     359    loop
     360    {
     361      if ((p->typ != BT_if)
     362        &&(p->typ != BT_else))
     363      {
     364        if (p->typ == BT_break /*typ*/)
     365        {
     366          while (p != currentVoice)
     367          {
     368            exitVoice();
     369          }
     370          yylineno = currentVoice->start_lineno;
     371          currentVoice->fptr=0;
     372          return FALSE;
     373        }
     374        else return TRUE;
     375      }
     376      if (p->prev==NULL) break;
     377      p=p->prev;
     378    }
     379  }
     380  return TRUE;
     381}
     382
     383/*2
     384* leave a voice: kill local variables
     385* setup everything from the previous level
     386* return 1 if leaving the top level, 0 otherwise
     387*/
     388BOOLEAN exitVoice()
     389{
     390  //printf("exitVoice: %d(%s),(%x)\n",
     391  //  currentVoice->typ,BT_name[currentVoice->typ], currentVoice);
     392  //{
     393  //Voice *p=currentVoice;
     394  //Print("-----------------\ncurr:");
     395  //do
     396  //{
     397  //Print("voice fn:%s\n",p->filename);
     398  //p=p->prev;
     399  //}
     400  //while (p!=NULL);
     401  //Print("----------------\n");
     402  //}
     403  if (currentVoice!=NULL)
     404  {
     405    if (currentVoice->oldb!=NULL)
     406    {
     407      myyoldbuffer(currentVoice->oldb);
     408      currentVoice->oldb=NULL;
     409    }
     410    if ((currentVoice->prev==NULL)&&(currentVoice->sw==BI_file))
     411    {
     412      currentVoice->prev=feInitStdin(currentVoice);
     413    }
     414    if (currentVoice->prev!=NULL)
     415    {
     416      //printf("exitVoice typ %d(%s)\n",
     417      //  currentVoice->typ,BT_name[currentVoice->typ]);
     418      if (currentVoice->typ==BT_if)
     419      {
     420        currentVoice->prev->ifsw=2;
     421      }
     422      else
     423      {
     424        currentVoice->prev->ifsw=0;
     425      }
     426      if ((currentVoice->sw == BI_file)
     427      && (currentVoice->files!=NULL))
     428      {
     429        fclose(currentVoice->files);
     430      }
     431      if (currentVoice->filename!=NULL)
     432      {
     433        omFree((ADDRESS)currentVoice->filename);
     434        currentVoice->filename=NULL;
     435      }
     436      if (currentVoice->buffer!=NULL)
     437      {
     438        omFree((ADDRESS)currentVoice->buffer);
     439        currentVoice->buffer=NULL;
     440      }
     441      yylineno=currentVoice->prev->curr_lineno;
     442      currentVoice->prev->next=NULL;
     443    }
     444    Voice *p=currentVoice->prev;
     445    delete currentVoice;
     446    currentVoice=p;
     447  }
     448  return currentVoice==NULL;
     449}
     450
     451/*2
     452* set prompt_char
     453* only called with currentVoice->sw == BI_stdin
     454*/
     455static void feShowPrompt(void)
     456{
     457  fe_promptstr[0]=prompt_char;
     458#ifdef macintosh
     459  cols = 0;
     460  printf(fe_promptstr);mflush();
     461#endif
     462}
     463
     464/*2
     465* print echo (si_echo or TRACE), set my_yylinebuf
     466*/
     467static int fePrintEcho(char *anf, char *b)
     468{
     469  char *ss=strrchr(anf,'\n');
     470  int len_s;
     471  if (ss==NULL)
     472  {
     473    len_s=strlen(anf);
     474  }
     475  else
     476  {
     477    len_s=ss-anf+1;
     478  }
     479  // my_yylinebuf:
     480  int mrc=min(len_s,79)-1;
     481  strcpy(my_yylinebuf,anf+(len_s-1)-mrc);
     482  if (my_yylinebuf[mrc] == '\n') my_yylinebuf[mrc] = '\0';
     483  mrc--;
     484  // handle echo:
     485  if (((si_echo>myynest)
     486    && ((currentVoice->typ==BT_proc)
     487      || (currentVoice->typ==BT_example)
     488      || (currentVoice->typ==BT_file)
     489      || (currentVoice->typ==BT_none)
     490    )
     491    && (strncmp(anf,";return();",10)!=0)
     492   )
     493  || (traceit&TRACE_SHOW_LINE)
     494  || (traceit&TRACE_SHOW_LINE1))
     495  {
     496    if (currentVoice->typ!=BT_example)
     497    {
     498      if (currentVoice->filename==NULL)
     499        Print("(none) %3d%c ",yylineno,prompt_char);
     500      else
     501        Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
     502     }
     503    #ifdef HAVE_TCL
     504    if(tclmode)
     505    {
     506      PrintTCL('N',len_s,anf);
     507    }
     508    else
     509    #endif
     510    {
     511      fwrite(anf,1,len_s,stdout);
     512      mflush();
     513    }
     514    if (traceit&TRACE_SHOW_LINE)
     515    {
     516      #ifdef HAVE_TCL
     517      if(!tclmode)
     518      #endif
     519      while(fgetc(stdin)!='\n');
     520    }
     521  }
     522  else if (traceit&TRACE_SHOW_LINENO)
     523  {
     524    Print("{%d}",yylineno);
     525    mflush();
     526  }
     527#ifdef HAVE_SDB
     528  if ((blocknest==0)
     529  && (currentVoice->pi!=NULL)
     530  && (currentVoice->pi->trace_flag!=0))
     531  {
     532    sdb(currentVoice, anf, len_s);
     533  }
     534#endif
     535  prompt_char = '.';
     536  return len_s;
     537}
     538
     539int feReadLine(char* b, int l)
     540{
     541  char *s=NULL;
     542  int offset = 0; /* will not be used if s==NULL*/
     543  // try to read from the buffer into b, max l chars
     544  if (currentVoice!=NULL)
     545  {
     546    if((currentVoice->buffer!=NULL)
     547    && (currentVoice->buffer[currentVoice->fptr]!='\0'))
     548    {
     549  NewBuff:
     550      register int i=0;
     551      long startfptr=currentVoice->fptr;
     552      long tmp_ptr=currentVoice->fptr;
     553      l--;
     554      loop
     555      {
     556        register char c=
     557        b[i]=currentVoice->buffer[tmp_ptr/*currentVoice->fptr*/];
     558        i++;
     559        if (yy_noeof==noeof_block)
     560        {
     561          if (c<' ')  yylineno++;
     562          else if (c=='}') break;
     563        }
     564        else
     565        {
     566          if ((c<' ') ||
     567          (c==';') ||
     568          (c==')')
     569          )
     570            break;
     571        }
     572        if (i>=l) break;
     573        tmp_ptr++;/*currentVoice->fptr++;*/
     574        if(currentVoice->buffer[tmp_ptr/*currentVoice->fptr*/]=='\0') break;
     575      }
     576      currentVoice->fptr=tmp_ptr;
     577      b[i]='\0';
     578      if (currentVoice->sw==BI_buffer)
     579      {
     580        if (startfptr==0)
     581        {
     582          char *anf=currentVoice->buffer;
     583          char *ss=strchr(anf,'\n');
     584          int len;
     585          if (ss==NULL) len=strlen(anf);
     586          else          len=ss-anf;
     587          char *s=(char *)omAlloc(len+2);
     588          strncpy(s,anf,len+2);
     589          s[len+1]='\0';
     590          fePrintEcho(s,b);
     591          omFree((ADDRESS)s);
     592        }
     593        else if (/*(startfptr>0) &&*/
     594        (currentVoice->buffer[startfptr-1]=='\n'))
     595        {
     596          char *anf=currentVoice->buffer+startfptr;
     597          char *ss=strchr(anf,'\n');
     598          int len;
     599          if (ss==NULL) len=strlen(anf);
     600          else          len=ss-anf;
     601          char *s=(char *)omAlloc(len+2);
     602          strncpy(s,anf,len+2);
     603          s[len+1]='\0';
     604          yylineno++;
     605          fePrintEcho(s,b);
     606          omFree((ADDRESS)s);
     607        }
     608      }
     609      currentVoice->fptr++;
     610      return i;
     611    }
     612    // no buffer there or e-o-buffer or eoln:
     613    if (currentVoice->sw!=BI_buffer)
     614    {
     615      currentVoice->fptr=0;
     616      if (currentVoice->buffer==NULL)
     617      {
     618        currentVoice->buffer=(char *)omAlloc(4096-sizeof(ADDRESS));
     619        omMarkAsStaticAddr(currentVoice->buffer);
     620      }
     621    }
     622    offset=0;
     623  NewRead:
     624    yylineno++;
     625    if (currentVoice->sw==BI_stdin)
     626    {
     627      feShowPrompt();
     628      s=fe_fgets_stdin(fe_promptstr,
     629                       &(currentVoice->buffer[offset]),
     630                       (4096-1-sizeof(ADDRESS))-offset);
     631      int i=0;
     632      if (s!=NULL)
     633        while((s[i]!='\0') && (i<4096)) {s[i] &= (char)127;i++;}
     634    }
     635    else if (currentVoice->sw==BI_file)
     636    {
     637      s=fgets(currentVoice->buffer+offset,(4096-1-sizeof(ADDRESS))-offset,
     638              currentVoice->files);
     639    }
     640    //else /* BI_buffer */ s==NULL  => return 0
     641    // done by the default return
     642  }
     643  if (s!=NULL)
     644  {
     645    // handle prot:
     646    if (feProt&PROT_I)
     647    {
     648      fputs(s,feProtFile);
     649    }
     650    int rc=fePrintEcho(s,b)+1;
     651    //s[strlen(s)+1]='\0'; add an second \0 at the end of the string
     652    s[rc]='\0';
     653    // handel \\ :
     654    rc-=3;
     655    if ((s[rc]=='\\')&&(currentVoice->sw!=BI_buffer))
     656    {
     657      s[rc]='\0';
     658      offset+=rc;
     659      if (offset<(int)omSizeOfAddr(currentVoice->buffer)) goto NewRead;
     660    }
     661    goto NewBuff;
     662  }
     663  /* else if (s==NULL) */
     664  {
     665    char *err;
     666    switch(yy_noeof)
     667    {
     668      case noeof_brace:
     669      case noeof_block:
     670        err="{...}";
     671        break;
     672      case noeof_asstring:
     673        err="till `.`";
     674        break;
     675      case noeof_string:
     676        err="string";
     677        break;
     678      case noeof_bracket:
     679        err="(...)";
     680        break;
     681      case noeof_procname:
     682        err="proc";
     683        break;
     684      case noeof_comment:
     685        err="/*...*/";
     686        break;
     687      default:
     688        return 0;
     689    }
     690    Werror("premature end of file while reading %s",err);
     691    return 0;
     692  }
     693}
     694
     695#ifdef __MWERKS__
     696#ifdef __cplusplus
     697extern "C" {
     698#endif
     699#ifdef macintosh
     700int    isatty(int filedes);
     701#else
     702int    _isatty(int filedes);
     703#define isatty  _isatty
     704#endif /* macintosh */
     705#ifdef __cplusplus
     706}
     707#endif
     708#endif
     709/*2
     710* init all data structures
     711*/
     712#ifndef STDIN_FILENO
     713#define STDIN_FILENO 0
     714#endif
     715Voice * feInitStdin(Voice *pp)
     716{
     717  Voice *p = new Voice;
     718  p->files = stdin;
     719  #ifdef HAVE_TCL
     720  p->sw = (tclmode || isatty(STDIN_FILENO)) ? BI_stdin : BI_file;
     721  #else
     722  p->sw = (isatty(STDIN_FILENO)) ? BI_stdin : BI_file;
     723  #endif
     724  if ((pp!=NULL) && (pp->files==stdin))
     725  {
     726    p->files=freopen("/dev/tty","r",stdin);
     727    stdin=p->files;
     728    p->sw = BI_stdin;
     729  }
     730  p->filename   = omStrDup("STDIN");
     731  p->start_lineno   = 1;
     732  omMarkAsStaticAddr(p);
     733  omMarkAsStaticAddr(p->filename);
     734  return p;
     735}
    99736/*****************************************************************
    100737 *
Note: See TracChangeset for help on using the changeset viewer.