⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 int_toplevel.c

📁 LastWave
💻 C
📖 第 1 页 / 共 2 页
字号:
/*..........................................................................*//*                                                                          *//*      L a s t W a v e   K e r n e l   3 . 0                               *//*                                                                          *//*      Copyright (C) 1998-2002 Emmanuel Bacry.                             *//*      email : lastwave@cmap.polytechnique.fr                              *//*                                                                          *//*..........................................................................*//*                                                                          *//*      This program is a free software, you can redistribute it and/or     *//*      modify it under the terms of the GNU General Public License as      *//*      published by the Free Software Foundation; either version 2 of the  *//*      License, or (at your option) any later version                      *//*                                                                          *//*      This program is distributed in the hope that it will be useful,     *//*      but WITHOUT ANY WARRANTY; without even the implied warranty of      *//*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *//*      GNU General Public License for more details.                        *//*                                                                          *//*      You should have received a copy of the GNU General Public License   *//*      along with this program (in a file named COPYRIGHT);                *//*      if not, write to the Free Software Foundation, Inc.,                *//*      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA             *//*                                                                          *//*..........................................................................*//****************************************************************************//*                                                                          *//*  int_toplevel.c   Everything on toplevels                                *//*                                                                          *//****************************************************************************/#include "lastwave.h"    /************************************************************************** * * Functions for copying streams  * **************************************************************************/static void Level2ToplevelStreams(LEVEL level, TOPLEVEL toplevel){  stdinStream = CopyStream(&level->in,&toplevel->in);        stdoutStream = CopyStream(&level->out,&toplevel->out);    stderrStream = CopyStream(&level->err,&toplevel->err);  }static void Toplevel2LevelStreams(TOPLEVEL toplevel,LEVEL level){  CopyStream(&toplevel->in,&level->in);    CopyStream(&toplevel->err,&level->err);    CopyStream(&toplevel->out,&level->out);  }/************************************************************************** * * Functions managing the levels  * **************************************************************************/ /* The current level and the first level */LEVEL levelCur = NULL;LEVEL levelFirst = NULL;/* Init the fields of the current level */static void InitLevelFields(int hashSize){  if (levelCur->theVariables == NULL) levelCur->theVariables = NewHashTable(hashSize);  levelCur->theVariables->level = levelCur;    levelCur->cmdNum = 0;  levelCur->nLoops = 0;  levelCur->cmdList = NULL;  levelCur->scriptline = NULL;  levelCur->flagTrace = NO;  levelCur->in = levelCur->out = levelCur->err = NULL;  levelCur->levelVar = levelCur;  levelCur->scommand = NULL;}/* Open a new level associated to the scommand 'sc' */void OpenLevel(LWPROC sc){    if (toplevelCur->nLevel == MaxNumLevels)     Errorf("Too many imbricated function calls (should be less than %d)",MaxNumLevels);      toplevelCur->nLevel++;  levelCur = &(toplevelCur->levels[toplevelCur->nLevel-1]);  InitLevelFields(10);    levelCur->scommand = sc;  sc->nRef++;    Toplevel2LevelStreams(toplevelCur,levelCur);  }/* Close the last level (never called if it is the first one) */void CloseLevel(void){    if (toplevelCur->nLevel == 1) Errorf("Cannot close first level");  ClearHashTable(levelCur->theVariables);   if (levelCur->scommand != NULL) DeleteProc(levelCur->scommand);    CloseStream(levelCur->out);  levelCur->out = NULL;  CloseStream(levelCur->in);  levelCur->in = NULL;  CloseStream(levelCur->err);  levelCur->err = NULL;   toplevelCur->nLevel--;  levelCur = &(toplevelCur->levels[toplevelCur->nLevel-1]);  levelCur->levelVar = levelCur;  Level2ToplevelStreams(levelCur,toplevelCur);  }/*  * Getting a level variable environment from the level number  * *  If 'nlevel' is > 0 then we send back the corresponding level  *      (1 is for global level, 2 is the next one and so on up to toplevelCur->nLevel included) *  If 'nlevel' is <=0 it goes backward starting from 0 which corresponds to the current level */  LEVEL GetLevel(int nlevel){  LEVEL level;  /*   * Absolute level number    */  if (nlevel > 0) {      /* Some basic test for absolute level number */    if (nlevel > toplevelCur->nLevel) {      SetErrorf("Bad level number '%d'",nlevel);      return(NULL);    }        /* Get the corresponding level */    level = &(toplevelCur->levels[nlevel-1]);  }    /*   * Relative level number    */  else {      /* Get the current Level for variables */    level = levelCur;    while (level->levelVar != level) level = level->levelVar;      /* Some basic test for relative level number */    if (-nlevel > level->n) {      SetErrorf("Level '%d' does not exist",nlevel);       return(NULL);    }         /* Get the corresponding level */      level = &(toplevelCur->levels[level->n+nlevel]);  }    /* Get the levelVar */  while (level->levelVar != level) level = level->levelVar;        /* returns it */  return(level);}/* * Parsing a level */char ParseLevel_(char *arg, LEVEL defVal, LEVEL *level){  int l;  char *endp;    *level = defVal;    if (arg == NULL) {    SetErrorf("ParseLevel_() : NULL string cannot be converted to a level");    return(NO);  }    l = strtol(arg,&endp,0);    if (*endp != '\0')  {    SetErrorf("ParseLevel_() : '%s' not a level",arg);    return(NO);  }    *level = GetLevel(l);    if (*level == NULL) {    *level = defVal;    return(NO);  }  else return(YES);  }/* Same as above but generate an error (no default value) */void ParseLevel(char *arg, LEVEL *level){  if (ParseLevel_(arg,NULL,level) == NO) Errorf1("");}/************************************************************************** * * Functions managing the toplevels  * **************************************************************************//* Global variables of the toplevel */int nToplevel = 0;TOPLEVEL theToplevels[MaxNToplevels];TOPLEVEL toplevelCur = NULL;/* Create a new current toplevel */void OpenToplevel(void){  int i;    if (nToplevel == MaxNToplevels) Errorf("Sorry too many toplevels already open");      /* We create the structure */  theToplevels[nToplevel] = (TOPLEVEL) Malloc(sizeof(struct toplevel));  if (theToplevels[nToplevel] == NULL) Errorf("Malloc failed for toplevel");        /* we create the history */  theToplevels[nToplevel]->history = NewHistory();    /* Set the current toplevel and level */        toplevelCur = theToplevels[nToplevel];  levelFirst = levelCur = &(toplevelCur->levels[0]);  /* Init the levels */  for (i=0;i<MaxNumLevels;i++) {    toplevelCur->levels[i].n = i;    toplevelCur->levels[i].levelVar = &(toplevelCur->levels[i]);  }            /* Initialize streams (the order here should correspond the order of the enum in streams.h) */  toplevelCur->nStream = 0;  for (i=0;i<MaxNumStreams;i++) toplevelCur->theStreams[i] = NULL;    /* Create the standard streams (if not done already) */    if (_StdinStream == NULL) {    _StdinStream = OpenBufferStream(1000);    _StdoutStream = OpenFileStream(NULL,"w");    _StderrStream = OpenFileStream(NULL,"w");    _StdnullStream = OpenFileStream(NULL,"w");  }   stdinStream = toplevelCur->in = RefStream(_StdinStream);  stdoutStream = toplevelCur->out = RefStream(_StdoutStream);  stderrStream = toplevelCur->err = RefStream(_StderrStream);  stdnullStream = _StdnullStream;  toplevelCur->sourceFilename = NULL;  toplevelCur->packageName = NULL;    toplevelCur->nEvents = 0;  toplevelCur->lastWindow = NULL;    /* Init the hash tables levels */  for (i=0;i<MaxNumLevels;i++) toplevelCur->levels[i].theVariables = NULL;      /* Initialization of the first level */  toplevelCur->nLevel = 1;  InitLevelFields(100);    Toplevel2LevelStreams(toplevelCur,levelCur);    /* Initialization of the Temporary allocation system */          toplevelCur->nTempAlloc = 0;  toplevelCur->nMarkerTempAlloc = 0;     /* Init the prompt script */  toplevelCur->promptProc = NULL;    /* Init of the terminal line */  InitTerminalInput();  toplevelCur->termMode = CommandTMode;     /* the 'return', 'break' and 'continue' flags */         toplevelCur->flags = 0;      /* Init the last event field */    toplevelCur->lastEvent = NULL;  /* Init the DrawMessage flag */  toplevelCur->flagInDrawMessage = NO;  /* Init the result */  toplevelCur->nResultList = 0;  toplevelCur->nResult = 0;  toplevelCur->resultType = NULL;  toplevelCur->resultContent = NULL;  toplevelCur->flagStoreResult = YES;  toplevelCur->flagSaveError = YES;  toplevelCur->termResultMode = TermResultNormalMode;    /* There's one more toplevel ! */  nToplevel++;  }  /***************************//* Close the last toplevel *//***************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -