Changeset d931f8 in git for Singular/febase.cc


Ignore:
Timestamp:
Sep 9, 1997, 11:15:03 AM (27 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
e6da16580a6a7ea5df19aa6083e6487b4e69477f
Parents:
de12f8a7cc8001c77e0e08efe38b6febf34b82af
Message:
* hannes: flex-staff is now in febase.inc
      changed tutorial (doc/examples.doc, version-no: doc/tutor.tex)


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

Legend:

Unmodified
Added
Removed
  • Singular/febase.cc

    rde12f8 rd931f8  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: febase.cc,v 1.21 1997-07-16 11:31:41 Singular Exp $ */
     4/* $Id: febase.cc,v 1.22 1997-09-09 09:15:02 Singular Exp $ */
    55/*
    6 * ABSTRACT: i/o system, handling of 'voices'
     6* ABSTRACT: i/o system
    77*/
    88
     
    3535#endif
    3636
    37 class 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 
    65 extern FILE* yyin;
    66 
    6737#define INITIAL_PRINT_BUFFER 24*1024
    6838static int feBufferLength=INITIAL_PRINT_BUFFER;
    6939static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER);
    7040
    71 #define START_LEVMAX 32
    72 int     levmax       = START_LEVMAX;
    73 Voices *currentVoice = NULL;
    74 Voices *FileAttribs  =(Voices *)Alloc0(START_LEVMAX*sizeof(Voices));
    75 short  *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         */
    8041int     si_echo = 0;
    8142int     printlevel = 0;
    82 int     fileVoice = 0;
    8343#ifndef macintosh
    8444int     pagelength = 24;
     
    8747#endif
    8848int     colmax = 80;
    89 int     voice = 0;
    90 int     inputswitch = 0;
    91 int     blocklineno = 0;
    9249char    prompt_char = '>'; /*1 either '>' or '.'*/
    9350BITSET  verbose = 1
     
    11976*/
    12077
     78#include "febase.inc"
     79
    12180/*2
    12281* fopen, but use 'SINGULARPATH' from environment and SINGULARDATADIR
     
    222181}
    223182
    224 /*2
    225 * the name of the current 'voice': the procname (or filename)
    226 */
    227 const char * VoiceName()
    228 {
    229   if (FileAttribs[fileVoice].filename!=NULL)
    230     return FileAttribs[fileVoice].filename;
    231   return sNoName;
    232 }
    233 
    234 /*2
    235 * the name of the 'voice' number 'i': the procname (or filename)
    236 */
    237 const char * VoiceName(int i)
    238 {
    239   if (FileAttribs[i].filename!=NULL)
    240     return FileAttribs[i].filename;
    241   return sNoName;
    242 }
    243 
    244 /*2
    245 * the type of the current voice:BT_proc, BT_example, BT_file
    246 */
    247 int VoiceType()
    248 {
    249   int i=fileVoice;
    250   while ((FileAttribs[i].typ!=BT_proc)
    251   &&(FileAttribs[i].typ!=BT_example)
    252   &&(FileAttribs[i].typ!=BT_file)
    253   &&(i>0))
    254     i--;
    255   return FileAttribs[i].typ;
    256 }
    257 /*2
    258 * start the file 'fname' (STDIN is stdin) in the current voice (cf.newVoice)
    259 */
    260 Voices * Voices::VFile(char* fname)
    261 {
    262   if (strcmp(fname,"STDIN") == 0)
    263   {
    264     yyin = stdin;
    265     v_inputswitch = 0;
    266   }
    267   else
    268   {
    269     yyin = feFopen(fname,"r",NULL,TRUE);
    270     v_inputswitch = -1;
    271   }
    272   files      = yyin;
    273   filename   = mstrdup(fname);
    274   v_echo     = si_echo;
    275   fileVoice  = voice;
    276   yylineno   = 1;
    277   if (files==NULL)
    278   {
    279     inputswitch = 0;
    280     exitVoice();
    281     return NULL;
    282   }
    283   inputswitch= v_inputswitch;
    284   return this;
    285 }
    286 
    287 /*3
    288 * increment voice counter, allocate new memory
    289 */
    290 static inline void inc_voice()
    291 {
    292   voice++;
    293   if (voice >= levmax)
    294   {
    295     FileAttribs=(Voices *)ReAlloc(FileAttribs,
    296                           levmax*sizeof(Voices),
    297                           (levmax+16)*sizeof(Voices));
    298     memset(&FileAttribs[levmax],0,16*sizeof(Voices));
    299     ifswitch=(short *)ReAlloc(ifswitch,
    300                           levmax*sizeof(short),
    301                           (levmax+16)*sizeof(short));
    302     memset(&ifswitch[levmax],0,16*sizeof(short));
    303     levmax+=16;
    304   }
    305 }
    306 
    307 /*2
    308 * init a new voice similiar to the current
    309 */
    310 void Voices::Next()
    311 {
    312   v_oldlineno = yylineno;
    313   v_echo      = si_echo;
    314   v_fileVoice = fileVoice;
    315   inc_voice();
    316 
    317   currentVoice = &FileAttribs[voice];
    318   currentVoice->Init();
    319 }
    320 
    321 /*2
    322 * start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile)
    323 */
    324 int newVoice(char* s)
    325 {
    326   currentVoice->Next();
    327   return (int)currentVoice->VFile((char *)s);
    328 }
    329 
    330 void newBuffer(char* s, int t, char* pname)
    331 {
    332   currentVoice->Next();
    333   currentVoice->Buffer(s,t);
    334   if (pname) currentVoice->filename = mstrdup(pname);
    335   //printf("start buffer %d typ %d\n",voice,t);
    336 }
    337 
    338 Voices * Voices::Buffer(char* buf, int t)
    339 {
    340   inputswitch = v_inputswitch = t;
    341   buffer      = buf;
    342   typ         = t;
    343   //si_echo        = 0;
    344   switch (t)
    345   {
    346     case BT_example:
    347     case BT_proc:    v_lineno = yylineno; ::yylineno = 3;     break;
    348     case BT_file:    v_lineno = yylineno; ::yylineno = 1;     break;
    349     case BT_if:
    350     case BT_else:    ::yylineno = v_lineno = blocklineno - 1; break;
    351     case BT_break:   ::yylineno = v_lineno = blocklineno - 2; break;
    352   }
    353   return this;
    354 }
    355 
    356 /*2
    357 * after leaving a voice:
    358 * setup everything from the this level
    359 */
    360 int Voices::Exit()
    361 {
    362   if (voice >= 0)
    363   {
    364     si_echo          = v_echo;
    365     fileVoice     = v_fileVoice;
    366     yyin          = files;
    367     yylineno      = v_oldlineno;
    368     inputswitch   = v_inputswitch;
    369     return 0;
    370   }
    371   //Print("Exit:%d\n",voice);
    372   return 1;
    373 }
    374 
    375 /*2
    376 * exit Buffer of type 'typ':
    377 * returns 1 if buffer type could not be found
    378 */
    379 int exitBuffer(int typ)
    380 {
    381   //printf("exitBuffer: %d\n",typ);
    382   if (typ == BT_break)  // valid inside for, while. may skip if, else
    383   {
    384     /*4 first check for valid buffer type, skip if/else*/
    385     for (int i=voice; i>0; i--)
    386     {
    387       if ((FileAttribs[i].typ == BT_if)
    388         ||(FileAttribs[i].typ == BT_else)) continue;
    389       if (FileAttribs[i].typ == BT_break /*typ*/)
    390       {
    391         while ((/*typ*/ BT_break != currentVoice->typ)
    392         && (voice > 0))
    393         {
    394           exitVoice();
    395         }
    396         return exitVoice();
    397       }
    398       else return 1;
    399     }
    400     /*4 break not inside a for/while: return an error*/
    401     if (/*typ*/ BT_break != currentVoice->typ) return 1;
    402     return exitVoice();
    403   }
    404 
    405   if ((typ == BT_proc)
    406   || (typ == BT_example))
    407   {
    408     for (int i=voice; i>0; i--)
    409     {
    410       if (FileAttribs[i].typ == 0) break;
    411       if ((FileAttribs[i].typ == BT_proc)
    412       || (FileAttribs[i].typ == BT_example))
    413       {
    414         while ((BT_proc != currentVoice->typ)
    415           && (BT_example != currentVoice->typ)
    416         && (voice > 0))
    417         {
    418           exitVoice();
    419         }
    420         return exitVoice();
    421       }
    422     }
    423   }
    424   /*4 return not inside a proc: return an error*/
    425   return 1;
    426 }
    427 
    428 /*2
    429 * jump to the beginning of a buffer
    430 */
    431 int contBuffer(int typ)
    432 {
    433   if (typ == BT_break)  // valid inside for, while. may skip if, else
    434   {
    435     // first check for valid buffer type
    436     for (int i=voice; i>0; i--)
    437     {
    438       if ((FileAttribs[i].typ == BT_if)
    439         ||(FileAttribs[i].typ == BT_else)) continue;
    440       if (FileAttribs[i].typ == BT_break /*typ*/)
    441       {
    442         while (/*typ*/ BT_break != currentVoice->typ && (voice > i))
    443         {
    444           exitVoice();
    445         }
    446         currentVoice->fptr = 0L;
    447         yylineno = currentVoice->v_lineno;
    448         return 0;
    449       }
    450       else return 1;
    451     }
    452   }
    453   return 1;
    454 }
    455 
    456 /*2
    457 * leave a voice: kill local variables
    458 * setup everything from the previous level (via Exit)
    459 */
    460 int exitVoice()
    461 {
    462   if (voice <= 0)   
    463   {
    464     if (feBatch) return 1;
    465     else m2_end(0);
    466   } 
    467   //printf("exitVoice %d, typ %d\n",voice,FileAttribs[voice].typ);
    468   if (FileAttribs[voice].typ==BT_if)
    469   {
    470     ifswitch[voice-1]=2;
    471   }
    472   else
    473   {
    474     ifswitch[voice-1]=0;
    475     //if ((FileAttribs[voice].typ==BT_proc)
    476     //||(FileAttribs[voice].typ==BT_example)
    477     //||(FileAttribs[voice].typ==0))
    478     //{
    479     //  killlocals(myynest);
    480     //  printf("killlocals %d\n",myynest);
    481     //}
    482   }
    483   if (inputswitch == -1)
    484   {
    485     fclose(yyin);
    486   }
    487   else if (inputswitch > 0)
    488   {
    489     if (FileAttribs[voice].filename!=NULL)
    490     {
    491       FreeL((ADDRESS)FileAttribs[voice].filename);
    492       FileAttribs[voice].filename=NULL;
    493     }
    494     if (FileAttribs[voice].buffer!=NULL)
    495     {
    496       FreeL((ADDRESS)FileAttribs[voice].buffer);
    497       FileAttribs[voice].buffer=NULL;
    498     }
    499   }
    500   currentVoice = &FileAttribs[--voice];
    501   return currentVoice->Exit();
    502 }
    503 
    504 int readbuf(char* buf, int l)
    505 {
    506   char *s;
    507   char * t = buf;
    508   int i = 0;
    509   long fl = currentVoice->fptr;
    510   if (fl == -1L)
    511   {
    512     t[0] = '\0';
    513     exitVoice();
    514     return 0;
    515   }
    516 
    517   s = currentVoice->buffer + fl;
    518   while (l > 0)
    519   {
    520     fl++;
    521     i++;
    522     l--;
    523     *t++ = *s;
    524     if (*s == '\n')
    525     {
    526       *t = '\0';
    527       if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE)
    528       || (traceit&TRACE_SHOW_LINE1))
    529       {
    530         if (currentVoice->filename==NULL)
    531           Print("(none) %3d%c ",yylineno,prompt_char);
    532         else if (VoiceType()!=BT_example)
    533           Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
    534         prompt_char = '.';
    535       }
    536       if (*(s+1) == '\0')
    537       {
    538         currentVoice->fptr = -1;
    539         FreeL((ADDRESS)(currentVoice->buffer));
    540         currentVoice->buffer=NULL;
    541         FileAttribs[voice].buffer = NULL;
    542         exitVoice();
    543       }
    544       else
    545         currentVoice->fptr = fl;
    546       return i;
    547     }
    548     else if (*s == '\0')
    549     {
    550       if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE)
    551       || (traceit&TRACE_SHOW_LINE1))
    552       {
    553         if (currentVoice->filename==NULL)
    554           Print("(none) %3d%c ",yylineno,prompt_char);
    555         else
    556           Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char);
    557         prompt_char = '.';
    558       }
    559       currentVoice->fptr = -1;
    560       FreeL((ADDRESS)(currentVoice->buffer));
    561       currentVoice->buffer = NULL;
    562       exitVoice();
    563       return i-1;
    564     }
    565     s++;
    566   }
    567   currentVoice->fptr = fl;
    568   return i;
    569 }
    570 
    571 /*2
    572 * init all data structures
    573 */
    574 void I_FEbase(void)
    575 {
    576   currentVoice = FileAttribs[0].VFile("STDIN");
    577 }
    578 
    579183static char * feBufferStart;
    580184  /* only used in StringSet(S)/StringAppend(S)*/
Note: See TracChangeset for help on using the changeset viewer.