/* exec.c -- implements exec.h */ #include "exec.h" /* exec.c * * This file is for functions dealing with execution of * commands, command lines, buffers, files and startup files. * * written 1986 by Daniel Lawrence * modified by Petri Kutvonen */ #include #include #include #include "buffer.h" #include "bind.h" #include "display.h" #include "estruct.h" #include "eval.h" #include "file.h" #include "flook.h" #include "input.h" #include "line.h" #include "random.h" #include "util.h" #include "window.h" static char *execstr = NULL ; /* pointer to string to execute */ boolean clexec = FALSE ; /* command line execution flag */ /* Directive definitions */ #define DIF 0 #define DELSE 1 #define DENDIF 2 #define DGOTO 3 #define DRETURN 4 #define DENDM 5 #define DWHILE 6 #define DENDWHILE 7 #define DBREAK 8 #define DFORCE 9 #define NUMDIRS 10 /* The !WHILE directive in the execution language needs to * stack references to pending whiles. These are stored linked * to each currently open procedure via a linked list of * the following structure. */ struct while_block { struct line *w_begin; /* ptr to !while statement */ struct line *w_end; /* ptr to the !endwhile statement */ int w_type; /* block type */ struct while_block *w_next; /* next while */ }; #define BTWHILE 1 #define BTBREAK 2 /* directive name table: This holds the names of all the directives.... */ static const char *dname[] = { "if", "else", "endif", "goto", "return", "endm", "while", "endwhile", "break", "force" }; static char golabel[ NSTRING] = "" ; /* current line to go to */ static int execlevel = 0 ; /* execution IF level */ static struct buffer *bstore = NULL ; /* buffer to store macro text to */ static int mstore = FALSE ; /* storing text to macro flag */ static int dobuf( struct buffer *bp) ; static void freewhile( struct while_block *wp) ; static int macarg( char *tok, int toksz) ; /* * Execute a named command even if it is not bound. */ int namedcmd(int f, int n) { fn_t kfunc; /* ptr to the requexted function to bind to */ /* prompt the user to type a named command */ mlwrite(": "); /* and now get the function name to execute */ kfunc = getname(); if (kfunc == NULL) { mlwrite("(No such function)"); return FALSE; } /* and then execute the command */ return kfunc(f, n); } static int docmd( char *cline) ; /* * execcmd: * Execute a command line command to be typed in * by the user * * int f, n; default Flag and Numeric argument */ int execcmd( int f, int n) { int status ; /* status return */ char *cmdstr ; /* string holding command to execute */ /* get the line wanted */ status = newmlarg( &cmdstr, ": ", 0) ; if( status != TRUE) return status ; execlevel = 0 ; while( status == TRUE && n-- > 0) status = docmd( cmdstr) ; free( cmdstr) ; return status ; } /* * docmd: * take a passed string as a command line and translate * it to be executed as a command. This function will be * used by execute-command-line and by all source and * startup files. Lastflag/thisflag is also updated. * * format of the command line is: * * {# arg} {} * * char *cline; command line to execute */ static int docmd( char *cline) { int f; /* default argument flag */ int n; /* numeric repeat value */ fn_t fnc; /* function to execute */ int status; /* return status of function */ boolean oldcle ; /* old contents of clexec flag */ char *oldestr; /* original exec string */ char tkn[NSTRING]; /* next token off of command line */ /* if we are scanning and not executing..go back here */ if (execlevel) return TRUE; oldestr = execstr; /* save last ptr to string to execute */ execstr = cline; /* and set this one as current */ /* first set up the default command values */ f = FALSE; n = 1; lastflag = thisflag; thisflag = 0; status = macarg( tkn, sizeof tkn) ; if( status != TRUE) { /* and grab the first token */ execstr = oldestr; return status; } /* process leadin argument */ if( !is_it_cmd( tkn)) { f = TRUE; /* macarg already includes a getval, skip for now mystrscpy( tkn, getval( tkn), sizeof tkn) ; */ n = atoi(tkn); /* and now get the command to execute */ status = macarg( tkn, sizeof tkn) ; if( status != TRUE) { execstr = oldestr; return status; } } /* and match the token to see if it exists */ if ((fnc = fncmatch(tkn)) == NULL) { mlwrite("(No such Function)"); execstr = oldestr; return FALSE; } /* save the arguments and go execute the command */ oldcle = clexec; /* save old clexec flag */ clexec = TRUE; /* in cline execution */ status = (*fnc) (f, n); /* call the function */ cmdstatus = status; /* save the status */ clexec = oldcle; /* restore clexec flag */ execstr = oldestr; return status; } /* * new token: * chop a token off a string * return a pointer past the token * * char *src in, source string * char **tokref out, destination of newly allocated token string */ static char *newtoken( char *src, char **tokref) { boolean quotef ; /* is the current string quoted? */ char *tok ; /* allocated string */ int size ; /* allocated size */ int idx = 0 ; /* insertion point into token string */ tok = malloc( NSTRING) ; size = (tok == NULL) ? 0 : NSTRING ; /* first scan past any whitespace in the source string */ while (*src == ' ' || *src == '\t') ++src; /* scan through the source string */ quotef = FALSE; while (*src) { char c ; /* temporary character */ /* process special characters */ if (*src == '~') { ++src; if (*src == 0) break; switch (*src++) { case 'r': c = 13; break; case 'n': c = 10; break; case 't': c = 9; break; case 'b': c = 8; break; case 'f': c = 12; break; default: c = *(src - 1); } } else { /* check for the end of the token */ if (quotef) { if (*src == '"') break; } else { if (*src == ' ' || *src == '\t') break; } /* set quote mode if quote found */ if (*src == '"') quotef = TRUE; /* record the character */ c = *src++; } if( idx < size - 1) tok[ idx++] = c ; else if( size > 1) { char *tmptok ; tmptok = malloc( size + 32) ; if( tmptok == NULL) size = 0 ; else { memcpy( tmptok, tok, idx) ; free( tok) ; tok = tmptok ; size += 32 ; tok[ idx++] = c ; } } } /* terminate the token and exit */ if (*src) ++src; if( tok != NULL) tok[ idx] = 0 ; *tokref = tok ; return src; } static char *token( char *srcstr, char *tok, int maxtoksize) { char *newtok ; srcstr = newtoken( srcstr, &newtok) ; if( newtok == NULL) tok[ 0] = 0 ; else { mystrscpy( tok, newtok, maxtoksize) ; free( newtok) ; } return srcstr ; } void gettoken( char *tok, int maxtoksize) { execstr = token( execstr, tok, maxtoksize) ; } static char *getnewtoken( void) { char *tok ; execstr = newtoken( execstr, &tok) ; return tok ; } boolean gettokval( char *tok, int size) { char *tmpbuf ; /* grab token and advance past */ tmpbuf = getnewtoken() ; if( tmpbuf == NULL) return FALSE ; /* evaluate it */ mystrscpy( tok, getval( tmpbuf), size) ; free( tmpbuf) ; return TRUE ; } char *getnewtokval( void) { char *tmpbuf ; char *tmpval ; char *valbuf ; /* grab token and advance past */ tmpbuf = getnewtoken() ; if( tmpbuf == NULL) return NULL ; /* evaluate it */ tmpval = getval( tmpbuf) ; valbuf = malloc( strlen( tmpval) + 1 ) ; if( valbuf != NULL) strcpy( valbuf, tmpval) ; free( tmpbuf) ; return valbuf ; } /* * get a macro line argument * * char *tok; buffer to place argument */ static int macarg( char *tok, int toksz) { int status ; boolean savcle ; /* buffer to store original clexec */ savcle = clexec ; /* save execution mode */ clexec = TRUE ; /* get the argument */ status = gettokval( tok, toksz) ; clexec = savcle ; /* restore execution mode */ return status ; } /* * storemac: * Set up a macro buffer and flag to store all * executed command lines there * * int f; default flag * int n; macro number to use */ static char macbufname[] = "*Macro xx*" ; #define MACDIGITPOS 7 int storemac(int f, int n) { struct buffer *bp; /* pointer to macro buffer */ /* must have a numeric argument to this function */ if (f == FALSE) { mlwrite("No macro specified"); return FALSE; } /* range check the macro number */ if (n < 1 || n > 40) { mlwrite("Macro number out of range"); return FALSE; } /* construct the macro buffer name */ macbufname[ MACDIGITPOS] = '0' + (n / 10) ; macbufname[ MACDIGITPOS + 1] = '0' + (n % 10) ; /* set up the new macro buffer */ bp = bfind( macbufname, TRUE, BFINVS) ; if( bp == NULL) { mlwrite("Can not create macro"); return FALSE; } /* and make sure it is empty */ bclear(bp); /* and set the macro store pointers to it */ mstore = TRUE; bstore = bp; return TRUE; } /* ** exec -- execute a buffer ** common to execute buffer, procedure and macro */ static int exec( int n, char *bufname, char *errstr) { struct buffer *bp ; /* ptr to buffer to execute */ /* find the pointer to that buffer */ bp = bfind( bufname, FALSE, 0) ; if( bp == NULL) { mlwrite( "No such %s", errstr) ; return FALSE ; } /* and now execute it as asked */ int status = TRUE ; while( status == TRUE && n-- > 0) status = dobuf( bp) ; return status ; } #if PROC /* * storeproc: * Set up a procedure buffer and flag to store all * executed command lines there * * int f; default flag * int n; macro number to use */ int storeproc( int f, int n) { struct buffer *bp ; /* pointer to macro buffer */ int status ; /* return status */ bname_t bname ; /* name of buffer to use */ char *name ; /* a numeric argument means its a numbered macro */ if( f == TRUE) return storemac( f, n) ; /* get the name of the procedure */ status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ; if( status != TRUE) return status ; /* construct the macro buffer name */ bname[ 0] = '*'; mystrscpy( &bname[ 1], name, sizeof bname - 2) ; strcat( bname, "*") ; free( name) ; /* set up the new macro buffer */ bp = bfind( bname, TRUE, BFINVS) ; if( bp == NULL) { mlwrite( "Can not create macro") ; return FALSE ; } /* and make sure it is empty */ bclear( bp) ; /* and set the macro store pointers to it */ mstore = TRUE ; bstore = bp ; return TRUE ; } /* * execproc: * Execute a procedure * * int f, n; default flag and numeric arg */ int execproc( int f, int n) { int status ; /* status return */ bname_t bufn ; /* name of buffer to execute */ char *name ; /* find out what buffer the user wants to execute */ status = newmlarg( &name, "Execute procedure: ", sizeof bufn - 2) ; if( status != TRUE) return status ; /* construct the buffer name */ bufn[ 0] = '*' ; mystrscpy( &bufn[ 1], name, sizeof bufn - 2) ; strcat( bufn, "*") ; free( name) ; return exec( n, bufn, "procedure") ; } #endif /* * execbuf: * Execute the contents of a buffer of commands * * int f, n; default flag and numeric arg */ int execbuf( int f, int n) { int status ; /* status return */ char *bufn ; /* name of buffer to execute */ /* find out what buffer the user wants to execute */ status = newmlarg( &bufn, "Execute buffer: ", sizeof( bname_t)) ; if( status != TRUE) return status ; status = exec( n, bufn, "buffer") ; free( bufn) ; return status ; } /* * dobuf: * execute the contents of the buffer pointed to * by the passed BP * * Directives start with a "!" and include: * * !endm End a macro * !if (cond) conditional execution * !else * !endif * !return Return (terminating current macro) * !goto