2013-05-20 01:16:08 -04:00
|
|
|
/* exec.c -- implements exec.h */
|
|
|
|
#include "exec.h"
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* This file is for bindable functions dealing with execution of commands,
|
|
|
|
command lines, buffers, files and startup files.
|
|
|
|
|
|
|
|
written 1986 by Daniel Lawrence
|
|
|
|
modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2021-09-09 22:10:19 -04:00
|
|
|
#include <assert.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-05-28 01:25:14 -04:00
|
|
|
#include "buffer.h"
|
2013-05-28 01:04:39 -04:00
|
|
|
#include "bind.h"
|
2021-08-24 00:17:40 -04:00
|
|
|
#include "defines.h" /* malloc/allocate, free/release */
|
2013-05-28 01:35:09 -04:00
|
|
|
#include "eval.h"
|
2013-05-25 00:28:17 -04:00
|
|
|
#include "file.h"
|
2013-06-10 06:14:09 -04:00
|
|
|
#include "flook.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2021-09-07 22:58:53 -04:00
|
|
|
#include "list.h"
|
2021-08-11 05:02:19 -04:00
|
|
|
#include "mlout.h"
|
2013-10-10 00:33:13 -04:00
|
|
|
#include "random.h"
|
2019-06-19 07:49:20 -04:00
|
|
|
#include "util.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-01-13 03:42:14 -05:00
|
|
|
static char *execstr = NULL ; /* pointer to string to execute */
|
2021-08-11 05:02:19 -04:00
|
|
|
boolean clexec = FALSE ; /* command line execution flag */
|
2013-10-08 04:56:47 -04:00
|
|
|
|
2021-09-13 00:33:32 -04:00
|
|
|
|
2015-01-09 04:53:29 -05:00
|
|
|
/* Directive definitions */
|
2021-09-13 00:33:32 -04:00
|
|
|
#define DBREAK 0
|
|
|
|
#define DELSE 1
|
|
|
|
#define DENDIF 2
|
|
|
|
#define DENDM 3
|
|
|
|
#define DENDWHILE 4
|
|
|
|
#define DFORCE 5
|
|
|
|
#define DGOTO 6
|
|
|
|
#define DGOSUB 7
|
|
|
|
#define DIF 8
|
|
|
|
#define DRETURN 9
|
|
|
|
#define DWHILE 10
|
|
|
|
|
|
|
|
/* directive name table: holds the names of all the directives.... */
|
|
|
|
static const char *dname[] = {
|
|
|
|
"break", "else", "endif", "endm", "endwhile",
|
|
|
|
"force", "goto", "gosub", "if", "return",
|
|
|
|
"while"
|
|
|
|
} ;
|
2013-09-23 08:29:49 -04:00
|
|
|
|
2021-09-13 00:33:32 -04:00
|
|
|
#define NUMDIRS ARRAY_SIZE( dname)
|
2013-09-23 08:29:49 -04:00
|
|
|
|
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
typedef struct caller {
|
|
|
|
struct caller *next ;
|
|
|
|
line_p caller ;
|
|
|
|
} *caller_p ;
|
|
|
|
|
2013-09-23 08:29:49 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
typedef struct while_block {
|
2021-09-07 22:58:53 -04:00
|
|
|
struct while_block *w_next ; /* next while */
|
2021-08-11 05:02:19 -04:00
|
|
|
line_p w_begin ; /* ptr to !while statement */
|
|
|
|
line_p w_end ; /* ptr to the !endwhile statement */
|
|
|
|
int w_type ; /* block type */
|
|
|
|
} *while_p ;
|
2013-09-23 08:29:49 -04:00
|
|
|
|
2015-01-09 04:53:29 -05:00
|
|
|
#define BTWHILE 1
|
|
|
|
#define BTBREAK 2
|
2013-09-23 08:29:49 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
static char golabel[ NSTRING] = "" ; /* current line to go to */
|
|
|
|
static buffer_p bstore = NULL ; /* buffer to store macro text to */
|
2021-09-05 00:01:02 -04:00
|
|
|
static int storing_f = FALSE ; /* storing text to macro flag */
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
static int dobuf( buffer_p bp) ;
|
2015-08-31 23:29:08 -04:00
|
|
|
static int macarg( char *tok, int toksz) ;
|
2013-09-23 08:29:49 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Execute a named command even if it is not bound. */
|
|
|
|
BINDABLE( namedcmd) {
|
|
|
|
/* prompt the user to type a named command */
|
|
|
|
mloutstr( "execute-named-cmd: ");
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* and now get the function name to execute */
|
2021-07-29 21:30:12 -04:00
|
|
|
nbind_p nbp = getname() ;
|
2021-07-20 20:16:11 -04:00
|
|
|
if( nbp == NULL) /* abort */
|
2021-08-11 05:02:19 -04:00
|
|
|
return ABORT ;
|
2021-07-20 20:16:11 -04:00
|
|
|
|
2021-07-19 03:39:00 -04:00
|
|
|
fnp_t kfunc = nbp->n_func ;
|
2021-08-11 05:02:19 -04:00
|
|
|
if( kfunc == NULL)
|
|
|
|
return mloutfail( "(No such function)") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-07-19 23:24:32 -04:00
|
|
|
if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW))
|
2021-07-19 03:39:00 -04:00
|
|
|
return rdonly() ;
|
|
|
|
|
2015-01-09 04:53:29 -05:00
|
|
|
/* and then execute the command */
|
2021-08-11 05:02:19 -04:00
|
|
|
return kfunc( f, n) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* docmd:
|
2021-09-11 00:38:56 -04:00
|
|
|
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:
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* {# arg} <command-name> {<argument string(s)>}
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* char *cline; command line to execute
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2014-06-06 03:09:08 -04:00
|
|
|
static int docmd( char *cline) {
|
2021-09-05 00:01:02 -04:00
|
|
|
char tkn[ NSTRING] ; /* next token off of command line */
|
2021-09-11 00:38:56 -04:00
|
|
|
int status ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
char *oldestr = execstr ; /* save last ptr to string to execute */
|
|
|
|
execstr = cline ; /* and set this one as current */
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* first set up the default command values */
|
2021-08-11 05:02:19 -04:00
|
|
|
int f = FALSE ;
|
|
|
|
int n = 1 ;
|
|
|
|
lastflag = thisflag ;
|
|
|
|
thisflag = 0 ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-11 00:38:56 -04:00
|
|
|
for( ;;) {
|
|
|
|
/* evaluate next token */
|
|
|
|
status = macarg( tkn, sizeof tkn) ;
|
|
|
|
if( status != TRUE || !*tkn) {
|
|
|
|
execstr = oldestr ;
|
|
|
|
return status ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* proceed if it is a command */
|
|
|
|
if( is_it_cmd( tkn))
|
|
|
|
break ;
|
|
|
|
|
|
|
|
/* otherwise set as argument of coming command */
|
|
|
|
f = TRUE ;
|
|
|
|
n = atoi( tkn) ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* and match the token to see if it exists */
|
2021-07-29 21:30:12 -04:00
|
|
|
nbind_p nbp = fncmatch( tkn) ;
|
2021-07-19 03:39:00 -04:00
|
|
|
fnp_t fnc = nbp->n_func ;
|
|
|
|
if( fnc == NULL) {
|
2021-08-11 05:02:19 -04:00
|
|
|
execstr = oldestr ;
|
|
|
|
return mloutfail( "(No such Function)") ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:24:32 -04:00
|
|
|
if( (bind_tag( nbp) & 1) && (curbp->b_mode & MDVIEW))
|
2021-07-19 03:39:00 -04:00
|
|
|
status = rdonly() ;
|
|
|
|
else {
|
|
|
|
/* save the arguments and go execute the command */
|
2021-09-05 00:01:02 -04:00
|
|
|
boolean oldcle = clexec ; /* save old clexec flag */
|
|
|
|
clexec = TRUE ; /* in cline execution */
|
|
|
|
status = fnc( f, n) ; /* call the function */
|
|
|
|
clexec = oldcle ; /* restore clexec flag */
|
|
|
|
execstr = oldestr ;
|
2021-07-19 03:39:00 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
cmdstatus = status ; /* save the status */
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
|
2021-09-11 00:38:56 -04:00
|
|
|
/* execcmd:
|
|
|
|
* Execute a command line command to be typed in by the user
|
|
|
|
*
|
|
|
|
* int f, n; default Flag and Numeric argument
|
|
|
|
*/
|
|
|
|
BINDABLE( execcmd) {
|
|
|
|
char *cmdstr ; /* string holding command to execute */
|
|
|
|
|
|
|
|
/* get the line wanted */
|
|
|
|
int status = newmlarg( &cmdstr, "execute-command-line: ", 0) ;
|
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
|
|
|
|
|
|
|
while( status == TRUE && n-- > 0)
|
|
|
|
status = docmd( cmdstr) ;
|
|
|
|
|
|
|
|
free( cmdstr) ;
|
|
|
|
return status ;
|
|
|
|
}
|
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* new token:
|
2015-01-09 04:53:29 -05:00
|
|
|
* chop a token off a string
|
|
|
|
* return a pointer past the token
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-09-27 07:19:55 -04:00
|
|
|
* char *src in, source string
|
|
|
|
* char **tokref out, destination of newly allocated token string
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2015-09-27 07:19:55 -04:00
|
|
|
static char *newtoken( char *src, char **tokref) {
|
2021-08-11 05:02:19 -04:00
|
|
|
char *tok = malloc( NSTRING) ;
|
|
|
|
int size = (tok == NULL) ? 0 : NSTRING ;
|
|
|
|
int idx = 0 ; /* insertion point into token string */
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* first scan past any whitespace in the source string */
|
2021-08-11 05:02:19 -04:00
|
|
|
while( *src == ' ' || *src == '\t')
|
|
|
|
++src ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* scan through the source string */
|
2021-09-07 22:58:53 -04:00
|
|
|
boolean quote_f = FALSE ; /* is the current string quoted? */
|
2021-09-11 00:38:56 -04:00
|
|
|
char c = *src ;
|
|
|
|
for( ; c ; c = *++src) {
|
2021-09-07 22:58:53 -04:00
|
|
|
/* process special characters */
|
|
|
|
if( c == '~') {
|
|
|
|
c = *++src ;
|
|
|
|
if( c == 0)
|
|
|
|
break ;
|
|
|
|
|
|
|
|
switch( c) {
|
2015-01-09 04:53:29 -05:00
|
|
|
case 'r':
|
2021-09-07 22:58:53 -04:00
|
|
|
c = 13 ;
|
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
case 'n':
|
2021-09-07 22:58:53 -04:00
|
|
|
c = 10 ;
|
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
case 't':
|
2021-09-07 22:58:53 -04:00
|
|
|
c = 9 ;
|
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
case 'b':
|
2021-09-07 22:58:53 -04:00
|
|
|
c = 8 ;
|
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
case 'f':
|
2021-09-07 22:58:53 -04:00
|
|
|
c = 12 ;
|
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-11 00:38:56 -04:00
|
|
|
/* check for the end of the token: EOS, space or comment */
|
2021-09-07 22:58:53 -04:00
|
|
|
if( quote_f) {
|
|
|
|
if( c == '"')
|
|
|
|
break ;
|
|
|
|
} else if( c == ' ' || c == '\t')
|
|
|
|
break ;
|
2021-09-11 00:38:56 -04:00
|
|
|
else if( c == '#' || c == ';') {
|
|
|
|
/* comments act like EOS */
|
|
|
|
c = 0 ;
|
|
|
|
break ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* set quote mode if quote found */
|
|
|
|
if( c == '"')
|
|
|
|
quote_f = TRUE ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
2015-09-27 07:19:55 -04:00
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* record the character */
|
2015-09-27 07:19:55 -04:00
|
|
|
if( idx < size - 1)
|
|
|
|
tok[ idx++] = c ;
|
|
|
|
else if( size > 1) {
|
|
|
|
char *tmptok ;
|
|
|
|
|
|
|
|
tmptok = malloc( size + 32) ;
|
|
|
|
if( tmptok == NULL)
|
2021-09-07 22:58:53 -04:00
|
|
|
size = 0 ; /* can't store more */
|
2015-09-27 07:19:55 -04:00
|
|
|
else {
|
|
|
|
memcpy( tmptok, tok, idx) ;
|
|
|
|
free( tok) ;
|
|
|
|
tok = tmptok ;
|
|
|
|
size += 32 ;
|
|
|
|
tok[ idx++] = c ;
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
|
2017-05-05 20:51:02 -04:00
|
|
|
if( tok != NULL)
|
|
|
|
tok[ idx] = 0 ;
|
2015-09-27 07:19:55 -04:00
|
|
|
|
2017-05-05 20:51:02 -04:00
|
|
|
*tokref = tok ;
|
2021-09-11 00:38:56 -04:00
|
|
|
return src + (c != 0) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2015-09-27 07:19:55 -04:00
|
|
|
static char *token( char *srcstr, char *tok, int maxtoksize) {
|
|
|
|
char *newtok ;
|
|
|
|
|
|
|
|
srcstr = newtoken( srcstr, &newtok) ;
|
|
|
|
if( newtok == NULL)
|
|
|
|
tok[ 0] = 0 ;
|
|
|
|
else {
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( tok, newtok, maxtoksize) ;
|
2015-09-27 07:19:55 -04:00
|
|
|
free( newtok) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return srcstr ;
|
|
|
|
}
|
|
|
|
|
2015-01-13 03:42:14 -05:00
|
|
|
void gettoken( char *tok, int maxtoksize) {
|
|
|
|
execstr = token( execstr, tok, maxtoksize) ;
|
|
|
|
}
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
static char *getnewtoken( void) {
|
|
|
|
char *tok ;
|
|
|
|
|
|
|
|
execstr = newtoken( execstr, &tok) ;
|
|
|
|
return tok ;
|
2015-09-27 07:19:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 04:34:15 -04:00
|
|
|
boolean gettokval( char *tok, int size) {
|
|
|
|
char *tmpbuf ;
|
|
|
|
|
|
|
|
/* grab token and advance past */
|
2015-09-27 09:11:51 -04:00
|
|
|
tmpbuf = getnewtoken() ;
|
2015-09-27 07:19:55 -04:00
|
|
|
if( tmpbuf == NULL)
|
|
|
|
return FALSE ;
|
2015-08-25 04:34:15 -04:00
|
|
|
|
|
|
|
/* evaluate it */
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( tok, getval( tmpbuf), size) ;
|
2015-08-25 04:34:15 -04:00
|
|
|
free( tmpbuf) ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
char *getnewtokval( void) {
|
|
|
|
char *tmpbuf ;
|
|
|
|
char *valbuf ;
|
|
|
|
|
|
|
|
/* grab token and advance past */
|
|
|
|
tmpbuf = getnewtoken() ;
|
|
|
|
if( tmpbuf == NULL)
|
|
|
|
return NULL ;
|
|
|
|
|
|
|
|
/* evaluate it */
|
2021-07-19 04:36:14 -04:00
|
|
|
const char *tmpval = getval( tmpbuf) ;
|
2015-09-27 09:11:51 -04:00
|
|
|
valbuf = malloc( strlen( tmpval) + 1 ) ;
|
|
|
|
if( valbuf != NULL)
|
|
|
|
strcpy( valbuf, tmpval) ;
|
|
|
|
|
|
|
|
free( tmpbuf) ;
|
|
|
|
return valbuf ;
|
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* get a macro line argument
|
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* char *tok; buffer to place argument
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2015-08-31 23:29:08 -04:00
|
|
|
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 ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
|
|
|
|
/* storemac:
|
|
|
|
* Set up a macro buffer and start recording all command lines until !endm
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f; default flag
|
|
|
|
* int n; macro number to use
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2019-06-19 08:52:21 -04:00
|
|
|
static char macbufname[] = "*Macro xx*" ;
|
|
|
|
#define MACDIGITPOS 7
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
static boolean setstore( char *bufname) {
|
|
|
|
/* set up the new macro buffer */
|
|
|
|
bstore = bfind( bufname, TRUE, BFINVS) ;
|
|
|
|
if( bstore == NULL) {
|
|
|
|
storing_f = FALSE ; /* should be already the case as we are executing */
|
|
|
|
return mloutfail( "Can not create macro") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and make sure it is empty */
|
|
|
|
bclear( bstore) ;
|
|
|
|
|
|
|
|
/* start recording */
|
|
|
|
storing_f = TRUE ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBINDABLE( storemac) {
|
2021-08-11 05:02:19 -04:00
|
|
|
/* must have a numeric argument to this function */
|
|
|
|
if( f == FALSE)
|
|
|
|
return mloutfail( "No macro number specified");
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* range check the macro number */
|
|
|
|
if( n < 1 || n > 40)
|
|
|
|
return mloutfail( "Macro number out of range") ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* construct the macro buffer name */
|
2019-06-19 08:52:21 -04:00
|
|
|
macbufname[ MACDIGITPOS] = '0' + (n / 10) ;
|
|
|
|
macbufname[ MACDIGITPOS + 1] = '0' + (n % 10) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
return setstore( macbufname) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
|
|
|
|
/* exec -- execute a buffer
|
2019-06-19 08:52:21 -04:00
|
|
|
** common to execute buffer, procedure and macro
|
|
|
|
*/
|
|
|
|
static int exec( int n, char *bufname, char *errstr) {
|
|
|
|
/* find the pointer to that buffer */
|
2021-08-11 05:02:19 -04:00
|
|
|
buffer_p bp = bfind( bufname, FALSE, 0) ;
|
2019-06-19 08:52:21 -04:00
|
|
|
if( bp == NULL) {
|
2021-08-11 05:02:19 -04:00
|
|
|
mloutfmt( "No such %s", errstr) ;
|
|
|
|
return FALSE ;
|
|
|
|
}
|
2019-06-19 08:52:21 -04:00
|
|
|
|
|
|
|
/* and now execute it as asked */
|
|
|
|
int status = TRUE ;
|
|
|
|
while( status == TRUE && n-- > 0)
|
|
|
|
status = dobuf( bp) ;
|
|
|
|
|
|
|
|
return status ;
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
|
|
|
|
/* storeproc:
|
|
|
|
* Set up a procedure buffer and start recording all command lines until !endm
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f; default flag
|
|
|
|
* int n; macro number to use
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( storeproc) {
|
2015-10-05 01:27:45 -04:00
|
|
|
bname_t bname ; /* name of buffer to use */
|
|
|
|
char *name ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* a numeric argument means it is a numbered macro */
|
2015-10-05 01:27:45 -04:00
|
|
|
if( f == TRUE)
|
|
|
|
return storemac( f, n) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* get the name of the procedure */
|
2021-08-11 05:02:19 -04:00
|
|
|
int status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* construct the macro buffer name */
|
2015-10-05 01:27:45 -04:00
|
|
|
bname[ 0] = '*';
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( &bname[ 1], name, sizeof bname - 2) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
strcat( bname, "*") ;
|
|
|
|
free( name) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
return setstore( bname) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
|
|
|
|
/* execproc:
|
2015-01-09 04:53:29 -05:00
|
|
|
* Execute a procedure
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f, n; default flag and numeric arg
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( execproc) {
|
|
|
|
bname_t bufn ; /* name of buffer to execute */
|
2015-10-05 01:27:45 -04:00
|
|
|
char *name ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* find out what buffer the user wants to execute */
|
2021-08-11 05:02:19 -04:00
|
|
|
int status = newmlarg( &name, "execute-procedure: ", sizeof bufn - 2) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
/* construct the buffer name */
|
2015-10-05 01:27:45 -04:00
|
|
|
bufn[ 0] = '*' ;
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( &bufn[ 1], name, sizeof bufn - 2) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
strcat( bufn, "*") ;
|
|
|
|
free( name) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2019-06-19 08:52:21 -04:00
|
|
|
return exec( n, bufn, "procedure") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* execbuf:
|
2015-01-09 04:53:29 -05:00
|
|
|
* Execute the contents of a buffer of commands
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f, n; default flag and numeric arg
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( execbuf) {
|
2015-10-05 01:27:45 -04:00
|
|
|
char *bufn ; /* name of buffer to execute */
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* find out what buffer the user wants to execute */
|
2021-09-05 00:01:02 -04:00
|
|
|
int status = newmlarg( &bufn, "execute-buffer: ", sizeof( bname_t)) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2019-06-19 08:52:21 -04:00
|
|
|
status = exec( n, bufn, "buffer") ;
|
|
|
|
free( bufn) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
static boolean storeline( char *eline, int linlen) {
|
|
|
|
/* allocate the space for the line */
|
|
|
|
line_p mp = lalloc( linlen) ;
|
|
|
|
if( mp == NULL)
|
|
|
|
return mloutfail( "Out of memory while storing macro") ;
|
|
|
|
|
|
|
|
/* copy the text into the new line */
|
|
|
|
memcpy( mp->l_text, eline, linlen) ; /* lalloc has set lp->l_used */
|
|
|
|
|
|
|
|
/* attach the line to the end of the buffer */
|
|
|
|
bstore->b_linep->l_bp->l_fp = mp ;
|
|
|
|
mp->l_bp = bstore->b_linep->l_bp ;
|
|
|
|
bstore->b_linep->l_bp = mp ;
|
|
|
|
mp->l_fp = bstore->b_linep ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* dobuf:
|
2021-09-05 00:01:02 -04:00
|
|
|
* execute the contents of the buffer pointed to by the passed BP
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* Directives start with a "!" and include:
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* !endm End a macro
|
|
|
|
* !if (cond) conditional execution
|
|
|
|
* !else
|
|
|
|
* !endif
|
|
|
|
* !return Return (terminating current macro)
|
|
|
|
* !goto <label> Jump to a label in the current macro
|
|
|
|
* !force Force macro to continue...even if command fails
|
|
|
|
* !while (cond) Execute a loop if the condition is true
|
|
|
|
* !endwhile
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2021-09-05 00:01:02 -04:00
|
|
|
* Line Labels begin with a "*" or ':' as the first nonblank char, like:
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2021-09-05 00:01:02 -04:00
|
|
|
* *LBL01 or :LBL01
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2021-08-11 05:02:19 -04:00
|
|
|
* buffer_p bp; buffer to execute
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
static int dobuf( buffer_p bp) {
|
|
|
|
while_p whtemp ; /* temporary ptr to a struct while_block */
|
|
|
|
char *eline ; /* text of line to execute */
|
|
|
|
char tkn[ NSTRING] ; /* buffer to evaluate an expression in */
|
2021-09-09 22:10:19 -04:00
|
|
|
int w_type ; /* either while or break */
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* clear IF level flags/while ptr */
|
|
|
|
while_p whlist = NULL ; /* ptr to !WHILE list */
|
|
|
|
while_p scanner = NULL ; /* ptr during scan */
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* range of lines with labels, initially no lines in range */
|
|
|
|
line_p firstlbl = NULL ;
|
|
|
|
line_p eolbl = firstlbl ;
|
|
|
|
|
|
|
|
/* parse the buffer to execute, building WHILE header blocks */
|
|
|
|
storing_f = FALSE ;
|
|
|
|
const line_p hlp = bp->b_linep ; /* pointer to line header */
|
2021-08-11 05:02:19 -04:00
|
|
|
for( line_p lp = hlp->l_fp ; lp != hlp ; lp = lp->l_fp) {
|
|
|
|
/* scan the current line */
|
|
|
|
eline = lp->l_text ;
|
2021-09-05 00:01:02 -04:00
|
|
|
char const *eol = &eline[ lp->l_used] ;
|
|
|
|
for( ; eline < eol ; eline++)
|
|
|
|
if( *eline != ' ' && *eline != '\t')
|
|
|
|
break ;
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* delete blank and comment lines */
|
2021-09-05 00:01:02 -04:00
|
|
|
if( eline == eol || *eline == '#' || *eline == ';') {
|
2021-09-07 22:58:53 -04:00
|
|
|
lp = lp->l_bp ;
|
|
|
|
lfree( lp->l_fp) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
continue ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* remove leading spaces */
|
|
|
|
if( eline != lp->l_text) {
|
|
|
|
int size = lp->l_used = eol - eline ;
|
2022-08-25 23:16:45 -04:00
|
|
|
memmove( lp->l_text, eline, size) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
eline = lp->l_text ;
|
|
|
|
eol = &lp->l_text[ size] ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* handle storing */
|
|
|
|
if( storing_f) {
|
|
|
|
if( !strncmp( eline, "!endm", 5)) {
|
|
|
|
bstore = NULL ;
|
|
|
|
storing_f = FALSE ;
|
2021-09-09 22:10:19 -04:00
|
|
|
} else if( bstore && !storeline( lp->l_text, lp->l_used))
|
2021-09-05 00:01:02 -04:00
|
|
|
goto failexit ;
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* remove line */
|
|
|
|
lp = lp->l_bp ;
|
|
|
|
lfree( lp->l_fp) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
continue ;
|
|
|
|
}
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* process labels, update the range of lines containing labels */
|
|
|
|
if( *eline == ':' || *eline == '*') {
|
|
|
|
if( firstlbl == NULL)
|
|
|
|
firstlbl = lp ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
eolbl = lp->l_fp ;
|
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process directives, skip others */
|
|
|
|
if( *eline != '!')
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
if( !strncmp( eline, "!store", 6)) {
|
2021-09-09 22:10:19 -04:00
|
|
|
/* turn line into a string */
|
|
|
|
int size = lp->l_used - 6 ;
|
|
|
|
if( size)
|
2022-08-25 23:16:45 -04:00
|
|
|
memmove( lp->l_text, &eline[ 6], size) ;
|
2021-09-09 22:10:19 -04:00
|
|
|
|
|
|
|
eline = lp->l_text ;
|
|
|
|
eline[ size] = 0 ;
|
|
|
|
lp->l_used = size ;
|
|
|
|
|
|
|
|
/* get procedure or macro name */
|
|
|
|
token( eline, &tkn[ 1], sizeof tkn - 1) ;
|
|
|
|
char c = tkn[ 1] ;
|
2021-09-11 00:38:56 -04:00
|
|
|
if( !c) {
|
2021-09-09 22:10:19 -04:00
|
|
|
/* no name */
|
|
|
|
storing_f = TRUE ; /* just throw away the lines (no bstore) */
|
|
|
|
} else if( c >= '1' && c <= '9') { /* number >= 1 */
|
|
|
|
/* macro number */
|
|
|
|
if( !storemac( TRUE, atoi( &tkn[ 1])))
|
|
|
|
goto failexit ;
|
|
|
|
} else {
|
|
|
|
/* whatever */
|
|
|
|
*tkn = '*' ;
|
|
|
|
strcat( tkn, "*") ;
|
|
|
|
if( !setstore( tkn))
|
|
|
|
goto failexit ;
|
2021-09-05 00:01:02 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
/* remove line */
|
|
|
|
lp = lp->l_bp ;
|
|
|
|
lfree( lp->l_fp) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
continue ;
|
|
|
|
} else if( !strncmp( eline, "!while", 6)) {
|
|
|
|
/* if it is a while directive, make a block... */
|
2021-09-09 22:10:19 -04:00
|
|
|
w_type = BTWHILE ;
|
|
|
|
scan:
|
2021-09-05 00:01:02 -04:00
|
|
|
whtemp = malloc( sizeof *whtemp) ;
|
|
|
|
if( whtemp == NULL) {
|
|
|
|
mloutstr( "%%Out of memory during while scan") ;
|
|
|
|
goto failexit ;
|
2021-09-09 22:10:19 -04:00
|
|
|
}
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
whtemp->w_begin = lp ;
|
2021-09-09 22:10:19 -04:00
|
|
|
whtemp->w_type = w_type ;
|
2021-09-05 00:01:02 -04:00
|
|
|
whtemp->w_next = scanner ;
|
|
|
|
scanner = whtemp ;
|
|
|
|
} else if( !strncmp( eline, "!break", 6)) {
|
|
|
|
/* if is a BREAK directive, make a block... */
|
|
|
|
if( scanner == NULL) {
|
|
|
|
mloutstr( "%%!BREAK outside of any !WHILE loop") ;
|
|
|
|
goto failexit ;
|
|
|
|
}
|
|
|
|
|
2021-09-09 22:10:19 -04:00
|
|
|
w_type = BTBREAK ;
|
|
|
|
goto scan ;
|
2021-09-05 00:01:02 -04:00
|
|
|
} else if( !strncmp( eline, "!endwhile", 9)) {
|
|
|
|
/* if it is an endwhile directive, record the spot... */
|
|
|
|
if( scanner == NULL) {
|
|
|
|
mloutfmt( "%%!ENDWHILE with no preceding !WHILE in '%s'",
|
|
|
|
bp->b_bname) ;
|
|
|
|
goto failexit ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move top records from the scanner list to the whlist until we
|
|
|
|
have moved all BREAK records and one WHILE record */
|
|
|
|
do {
|
|
|
|
scanner->w_end = lp ;
|
|
|
|
whtemp = whlist ;
|
|
|
|
whlist = scanner ;
|
|
|
|
scanner = scanner->w_next ;
|
|
|
|
whlist->w_next = whtemp ;
|
2021-08-11 05:02:19 -04:00
|
|
|
} while( whlist->w_type == BTBREAK) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* check consistency after parsing */
|
|
|
|
if( storing_f) {
|
|
|
|
/* missing !endm */
|
|
|
|
mloutstr( "!store without !endm") ;
|
|
|
|
goto failexit ;
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
if( scanner != NULL) {
|
2021-09-05 00:01:02 -04:00
|
|
|
/* while and endwhile should match! */
|
2021-08-11 05:02:19 -04:00
|
|
|
mloutfmt( "%%!WHILE with no matching !ENDWHILE in '%s'", bp->b_bname) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
failexit:
|
2021-09-07 22:58:53 -04:00
|
|
|
freelist( (list_p) scanner) ;
|
|
|
|
freelist( (list_p) whlist) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
return FALSE ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* execute the parsed buffer */
|
2015-01-09 04:53:29 -05:00
|
|
|
/* let the first command inherit the flags from the last one.. */
|
|
|
|
thisflag = lastflag;
|
|
|
|
|
|
|
|
/* starting at the beginning of the buffer */
|
2021-08-11 05:02:19 -04:00
|
|
|
char *einit = NULL ; /* initial value of eline */
|
2021-09-09 22:10:19 -04:00
|
|
|
int esize = 0 ; /* size available for storage in einit */
|
2021-08-11 05:02:19 -04:00
|
|
|
int status = TRUE ; /* status of command execution */
|
|
|
|
boolean done = FALSE ;
|
2021-09-05 00:01:02 -04:00
|
|
|
int execlevel = 0 ; /* execution IF level */
|
2021-09-07 22:58:53 -04:00
|
|
|
caller_p returnto = NULL ;
|
2021-09-05 00:01:02 -04:00
|
|
|
line_p lp = hlp->l_fp ;
|
|
|
|
for( ; lp != hlp ; lp = lp->l_fp) {
|
2021-09-13 00:33:32 -04:00
|
|
|
/* turns macro line into a string pointed by eline */
|
|
|
|
int length = lp->l_used ;
|
|
|
|
if( length < lp->l_size) {
|
|
|
|
/* there is space for EOS, let's use the line */
|
|
|
|
eline = lp->l_text ;
|
|
|
|
} else {
|
|
|
|
/* make a copy using (pre-)allocated storage */
|
|
|
|
if( esize <= length) {
|
|
|
|
esize = length + 1 ;
|
|
|
|
char *newbuf = realloc( einit, esize) ;
|
|
|
|
if( newbuf == NULL) {
|
|
|
|
status = mloutfail( "%%Out of Memory during macro execution") ;
|
|
|
|
break ;
|
|
|
|
} else
|
|
|
|
einit = newbuf ;
|
|
|
|
}
|
|
|
|
|
|
|
|
eline = einit ;
|
|
|
|
memcpy( eline, lp->l_text, length) ;
|
2021-09-09 22:10:19 -04:00
|
|
|
}
|
2019-06-19 07:49:20 -04:00
|
|
|
|
2021-09-13 00:33:32 -04:00
|
|
|
eline[ length] = 0 ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
|
|
|
#if DEBUGM
|
|
|
|
/* if $debug == TRUE, every line to execute
|
|
|
|
gets echoed and a key needs to be pressed to continue
|
|
|
|
^G will abort the command */
|
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
if( macbug) {
|
2021-09-05 00:01:02 -04:00
|
|
|
/* debug macro name, if levels and lastly the line */
|
|
|
|
int c = mdbugout( "<<<%s:%d:%s>>>", bp->b_bname, execlevel, eline) ;
|
2015-01-15 01:36:19 -05:00
|
|
|
if( c == abortc) {
|
2021-08-11 05:02:19 -04:00
|
|
|
status = FALSE ;
|
|
|
|
break ;
|
2015-01-15 01:36:19 -05:00
|
|
|
} else if( c == metac) {
|
|
|
|
macbug = FALSE ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* if macro store is on, just salt this away */
|
|
|
|
if( storing_f) {
|
|
|
|
if( !strncmp( eline, "!endm", 5)) {
|
|
|
|
storing_f = FALSE ;
|
|
|
|
bstore = NULL ;
|
2021-09-09 22:10:19 -04:00
|
|
|
lp = lp->l_bp ;
|
|
|
|
lfree( lp->l_fp) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
} else {
|
|
|
|
status = storeline( eline, strlen( eline)) ;
|
|
|
|
if( status != TRUE)
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2021-09-09 22:10:19 -04:00
|
|
|
lp = lp->l_bp ;
|
|
|
|
lfree( lp->l_fp) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip labels */
|
|
|
|
if( *eline == '*' || *eline == ':')
|
|
|
|
continue ;
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Parse directives here.... */
|
|
|
|
if( *eline == '!') {
|
|
|
|
/* Find out which directive this is */
|
|
|
|
++eline ;
|
2021-09-13 00:33:32 -04:00
|
|
|
int dirnum = NUMDIRS ;
|
|
|
|
while( --dirnum >= 0) {
|
|
|
|
length = strlen( dname[ dirnum]) ;
|
|
|
|
if( !strncmp( eline, dname[ dirnum], length )) {
|
|
|
|
eline += length ;
|
|
|
|
/* either EOS, comment, space separator or FAIL */
|
|
|
|
if( !*eline || *eline == ';' || *eline == '#')
|
|
|
|
;
|
|
|
|
else if( *eline == ' ' || *eline == '\t') {
|
|
|
|
eline += 1 ;
|
|
|
|
} else
|
|
|
|
dirnum = -1 ;
|
|
|
|
|
|
|
|
execstr = eline ; /* set source of token() and macarg() */
|
|
|
|
break ;
|
|
|
|
}
|
2021-09-09 22:10:19 -04:00
|
|
|
}
|
2021-09-05 00:01:02 -04:00
|
|
|
|
2021-09-13 00:33:32 -04:00
|
|
|
/* now, execute directives */
|
2021-08-11 05:02:19 -04:00
|
|
|
switch( dirnum) {
|
2021-09-05 00:01:02 -04:00
|
|
|
case DENDM:
|
|
|
|
if( execlevel == 0)
|
|
|
|
status = mloutfail( "!endm out of context") ;
|
|
|
|
|
|
|
|
break ;
|
2021-08-11 05:02:19 -04:00
|
|
|
case DIF: /* IF directive */
|
|
|
|
if( execlevel == 0) {
|
2021-09-09 22:10:19 -04:00
|
|
|
/* grab the value of the logical exp */
|
2021-09-05 00:01:02 -04:00
|
|
|
status = macarg( tkn, sizeof tkn) ;
|
|
|
|
if( status == TRUE && stol( tkn) == FALSE)
|
2021-09-09 22:10:19 -04:00
|
|
|
execlevel = 1 ; /* skip to else or endif */
|
2015-01-09 04:53:29 -05:00
|
|
|
} else
|
2021-08-11 05:02:19 -04:00
|
|
|
++execlevel ;
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
break ;
|
2021-09-09 22:10:19 -04:00
|
|
|
case DELSE: /* ELSE directive */
|
|
|
|
if( execlevel == 1)
|
|
|
|
execlevel = 0 ; /* execute the else part */
|
|
|
|
else if( execlevel == 0)
|
|
|
|
execlevel = 1 ; /* skip to endif */
|
|
|
|
/* else keep skipping */
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-09 22:10:19 -04:00
|
|
|
break ;
|
|
|
|
case DENDIF: /* ENDIF directive */
|
|
|
|
if( execlevel)
|
|
|
|
--execlevel ;
|
|
|
|
|
|
|
|
break ;
|
2021-08-11 05:02:19 -04:00
|
|
|
case DWHILE: /* WHILE directive */
|
|
|
|
if( execlevel == 0) {
|
2021-09-09 22:10:19 -04:00
|
|
|
/* grab the value of the logical exp */
|
2021-09-05 00:01:02 -04:00
|
|
|
status = macarg( tkn, sizeof tkn) ;
|
2021-09-09 22:10:19 -04:00
|
|
|
if( status != TRUE || stol( tkn) == TRUE)
|
2021-09-05 00:01:02 -04:00
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:05:06 -05:00
|
|
|
/* fallthrough */
|
2015-01-09 04:53:29 -05:00
|
|
|
case DBREAK: /* BREAK directive */
|
2021-09-09 22:10:19 -04:00
|
|
|
/* jump down to the endwhile */
|
|
|
|
/* find the right while loop */
|
|
|
|
for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next)
|
|
|
|
if( whtemp->w_begin == lp) {
|
|
|
|
/* reset the line pointer back.. */
|
|
|
|
lp = whtemp->w_end ;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( whtemp == NULL)
|
|
|
|
status = mloutfail( "%%Internal While loop error") ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-09 22:10:19 -04:00
|
|
|
break ;
|
|
|
|
case DENDWHILE: /* ENDWHILE directive */
|
|
|
|
assert( execlevel == 0) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
/* find the right while loop */
|
2021-08-11 05:02:19 -04:00
|
|
|
for( whtemp = whlist ; whtemp ; whtemp = whtemp->w_next)
|
2021-09-09 22:10:19 -04:00
|
|
|
if( whtemp->w_type == BTWHILE && whtemp->w_end == lp) {
|
|
|
|
/* reset the line pointer back.. */
|
|
|
|
lp = whtemp->w_begin->l_bp ;
|
2021-08-11 05:02:19 -04:00
|
|
|
break ;
|
2021-09-09 22:10:19 -04:00
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
if( whtemp == NULL)
|
2021-08-11 05:02:19 -04:00
|
|
|
status = mloutfail( "%%Internal While loop error") ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
break ;
|
2021-09-07 22:58:53 -04:00
|
|
|
case DGOSUB: /* GOSUB directive */
|
2021-08-11 05:02:19 -04:00
|
|
|
case DGOTO: /* GOTO directive */
|
|
|
|
/* .....only if we are currently executing */
|
2021-09-05 00:01:02 -04:00
|
|
|
if( execlevel != 0)
|
|
|
|
break ;
|
|
|
|
|
|
|
|
if( firstlbl != NULL) {
|
2021-08-11 05:02:19 -04:00
|
|
|
line_p glp ; /* line to goto */
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
/* grab label to jump to */
|
2015-09-27 07:19:55 -04:00
|
|
|
eline = token( eline, golabel, sizeof golabel) ;
|
2021-09-13 00:33:32 -04:00
|
|
|
length = strlen( golabel) ;
|
2021-09-05 00:01:02 -04:00
|
|
|
for( glp = firstlbl ; glp != eolbl ; glp = glp->l_fp) {
|
|
|
|
char c = *glp->l_text ;
|
|
|
|
if( (c == '*' || c == ':')
|
2021-09-13 00:33:32 -04:00
|
|
|
&& !strncmp( &glp->l_text[ 1], golabel, length)) {
|
2021-09-07 22:58:53 -04:00
|
|
|
if( dirnum == DGOSUB) {
|
|
|
|
caller_p cp = malloc( sizeof *cp) ;
|
|
|
|
if( cp == NULL) {
|
|
|
|
status = mloutfail( "OoM on !gosub") ;
|
|
|
|
break ;
|
|
|
|
} else {
|
|
|
|
cp->caller = lp ;
|
|
|
|
cp->next = returnto ;
|
|
|
|
returnto = cp ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
lp = glp ;
|
2021-08-11 05:02:19 -04:00
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
}
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
if( glp == eolbl)
|
|
|
|
goto nolabel ;
|
|
|
|
} else {
|
|
|
|
nolabel:
|
|
|
|
status = mloutfail( "%%No such label") ;
|
|
|
|
}
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
break ;
|
2015-01-09 04:53:29 -05:00
|
|
|
case DRETURN: /* RETURN directive */
|
2021-09-07 22:58:53 -04:00
|
|
|
if( execlevel == 0) {
|
|
|
|
if( returnto != NULL) {
|
|
|
|
caller_p cp = returnto ;
|
|
|
|
lp = returnto->caller ;
|
|
|
|
returnto = returnto->next ;
|
|
|
|
free( cp) ;
|
2021-09-11 21:34:28 -04:00
|
|
|
} else {
|
|
|
|
lp = lp->l_fp ;
|
2021-09-07 22:58:53 -04:00
|
|
|
done = TRUE ;
|
2021-09-11 21:34:28 -04:00
|
|
|
}
|
2021-09-07 22:58:53 -04:00
|
|
|
}
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
break ;
|
|
|
|
case DFORCE: /* FORCE directive */
|
|
|
|
if( execlevel == 0)
|
|
|
|
docmd( eline) ; /* execute ignoring returned status */
|
2021-09-13 00:33:32 -04:00
|
|
|
|
|
|
|
break ;
|
|
|
|
default:
|
|
|
|
status = mloutfail( "%%Unknown Directive") ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
2021-09-05 00:01:02 -04:00
|
|
|
} else if( execlevel == 0)
|
|
|
|
status = docmd( eline) ; /* execute the statement */
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
if( done || status != TRUE)
|
|
|
|
break ;
|
|
|
|
}
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2021-09-11 21:34:28 -04:00
|
|
|
/* refresh window if buffer is displayed */
|
|
|
|
if( bp->b_nwnd) {
|
2021-09-05 00:01:02 -04:00
|
|
|
for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) {
|
|
|
|
if( wp->w_bufp == bp) {
|
|
|
|
/* and point it */
|
|
|
|
wp->w_dotp = lp ;
|
|
|
|
wp->w_doto = 0 ;
|
|
|
|
wp->w_flag |= WFHARD ;
|
2015-01-09 04:53:29 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-11 21:34:28 -04:00
|
|
|
}
|
2021-09-05 00:01:02 -04:00
|
|
|
|
2021-09-11 21:34:28 -04:00
|
|
|
/* in any case set the buffer . */
|
|
|
|
bp->b_dotp = lp ;
|
|
|
|
bp->b_doto = 0 ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-09-07 22:58:53 -04:00
|
|
|
freelist( (list_p) returnto) ;
|
|
|
|
freelist( (list_p) whlist) ;
|
2021-08-11 05:02:19 -04:00
|
|
|
if( einit)
|
|
|
|
free( einit) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* execute a series of commands in a file
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f, n; default flag and numeric arg to pass on to file
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( execfile) {
|
2015-10-05 01:27:45 -04:00
|
|
|
char *fname ; /* name of file to execute */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-09-05 00:01:02 -04:00
|
|
|
int status = newmlarg( &fname, "execute-file: ", 0) ;
|
2015-10-05 01:27:45 -04:00
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 01:27:45 -04:00
|
|
|
/* look up the path for the file */
|
2021-08-11 05:02:19 -04:00
|
|
|
char *fspec = flook( fname, FALSE) ; /* used to be TRUE, P.K. */
|
2015-10-05 01:27:45 -04:00
|
|
|
free( fname) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 01:27:45 -04:00
|
|
|
/* if it isn't around */
|
|
|
|
if( fspec == NULL)
|
|
|
|
return FALSE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 01:27:45 -04:00
|
|
|
/* otherwise, execute it */
|
|
|
|
while( status == TRUE && n-- > 0)
|
|
|
|
status = dofile( fspec) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2017-05-05 20:51:02 -04:00
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* dofile:
|
2015-01-09 04:53:29 -05:00
|
|
|
* yank a file into a buffer and execute it
|
|
|
|
* if there are no errors, delete the buffer on exit
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* char *fname; file name to execute
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2015-01-16 08:12:27 -05:00
|
|
|
int dofile( const char *fname) {
|
2021-08-11 05:02:19 -04:00
|
|
|
bname_t bname ; /* name of buffer */
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
makename( bname, fname) ; /* derive the name of the buffer */
|
|
|
|
unqname( bname) ; /* make sure we don't stomp things */
|
|
|
|
buffer_p bp = bfind( bname, TRUE, 0) ; /* get the needed buffer */
|
|
|
|
if( bp == NULL)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
bp->b_mode = MDVIEW ; /* mark the buffer as read only */
|
|
|
|
buffer_p cb = curbp ; /* save the old buffer */
|
|
|
|
curbp = bp ; /* make this one current */
|
|
|
|
/* and try to read in the file to execute */
|
|
|
|
int status = readin( fname, FALSE) ;
|
|
|
|
curbp = cb ; /* restore the current buffer */
|
|
|
|
if( status == TRUE) {
|
|
|
|
/* go execute it! */
|
|
|
|
status = dobuf( bp) ;
|
|
|
|
if( status == TRUE && bp->b_nwnd == 0)
|
|
|
|
/* if not displayed, remove the now unneeded buffer and exit */
|
|
|
|
zotbuf( bp) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* cbuf:
|
2015-01-09 04:53:29 -05:00
|
|
|
* Execute the contents of a numbered buffer
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2015-01-09 04:53:29 -05:00
|
|
|
* int f, n; default flag and numeric arg
|
|
|
|
* int bufnum; number of buffer to execute
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2019-06-19 08:52:21 -04:00
|
|
|
static int cbuf( int f, int n, int bufnum) {
|
2015-01-09 04:53:29 -05:00
|
|
|
/* make the buffer name */
|
2019-06-19 08:52:21 -04:00
|
|
|
macbufname[ MACDIGITPOS] = '0' + (bufnum / 10) ;
|
|
|
|
macbufname[ MACDIGITPOS + 1] = '0' + (bufnum % 10) ;
|
2015-01-09 04:53:29 -05:00
|
|
|
|
2019-06-19 08:52:21 -04:00
|
|
|
return exec( n, macbufname, "macro") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 08:52:21 -04:00
|
|
|
/* execute buffer of numbered macro [1..40] */
|
|
|
|
#define cbufnn( nn) \
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( cbuf##nn) { \
|
2019-06-19 08:52:21 -04:00
|
|
|
return cbuf( f, n, nn) ; \
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 08:52:21 -04:00
|
|
|
cbufnn( 1)
|
|
|
|
cbufnn( 2)
|
|
|
|
cbufnn( 3)
|
|
|
|
cbufnn( 4)
|
|
|
|
cbufnn( 5)
|
|
|
|
cbufnn( 6)
|
|
|
|
cbufnn( 7)
|
|
|
|
cbufnn( 8)
|
|
|
|
cbufnn( 9)
|
|
|
|
cbufnn( 10)
|
|
|
|
cbufnn( 11)
|
|
|
|
cbufnn( 12)
|
|
|
|
cbufnn( 13)
|
|
|
|
cbufnn( 14)
|
|
|
|
cbufnn( 15)
|
|
|
|
cbufnn( 16)
|
|
|
|
cbufnn( 17)
|
|
|
|
cbufnn( 18)
|
|
|
|
cbufnn( 19)
|
|
|
|
cbufnn( 20)
|
|
|
|
cbufnn( 21)
|
|
|
|
cbufnn( 22)
|
|
|
|
cbufnn( 23)
|
|
|
|
cbufnn( 24)
|
|
|
|
cbufnn( 25)
|
|
|
|
cbufnn( 26)
|
|
|
|
cbufnn( 27)
|
|
|
|
cbufnn( 28)
|
|
|
|
cbufnn( 29)
|
|
|
|
cbufnn( 30)
|
|
|
|
cbufnn( 31)
|
|
|
|
cbufnn( 32)
|
|
|
|
cbufnn( 33)
|
|
|
|
cbufnn( 34)
|
|
|
|
cbufnn( 35)
|
|
|
|
cbufnn( 36)
|
|
|
|
cbufnn( 37)
|
|
|
|
cbufnn( 38)
|
|
|
|
cbufnn( 39)
|
|
|
|
cbufnn( 40)
|
|
|
|
|
|
|
|
/* end of exec.c */
|