From 02d12c5d845f08ca03c0c1372dcc72e41ed983e6 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 19 Aug 2015 15:42:16 +0800 Subject: [PATCH 01/27] Allow -x cmdfile as an alternative to @cmdfile options. --- main.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 8f8f3db..4be59e3 100644 --- a/main.c +++ b/main.c @@ -124,7 +124,7 @@ static void usage( void) { " + start at line \n" " --help display this help and exit\n" " --version output version information and exit\n" - " @cmdfile execute command file\n" + " @cmdfile execute startup file\n" " -a|A process error file\n" " -e|E edit file\n" " -g|G go to line \n" @@ -134,6 +134,8 @@ static void usage( void) { " -r|R restrictive use\n" " -s|S search string\n" " -v|V view file\n" + " -x|Xcmdfile\n" + " -x|X cmdfile execute command file\n" , stdout) ; } @@ -254,6 +256,19 @@ int main(int argc, char **argv) case 'V': viewflag = TRUE; break; + case 'x': + case 'X': + if( argv[ carg][ 2]) { /* -Xfilename */ + if( startup( &argv[ carg][ 2]) == TRUE) + startflag = TRUE ; /* don't execute emacs.rc */ + } else if( argv[ carg + 1]) { /* -X filename */ + if( startup( &argv[ carg + 1][ 0]) == TRUE) + startflag = TRUE ; /* don't execute emacs.rc */ + + carg += 1 ; + } + + break ; default: /* unknown switch */ /* ignore this for now */ break; From 6b3061cedbff699d975b25372926bf4714137cfa Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 19 Aug 2015 15:43:50 +0800 Subject: [PATCH 02/27] '#' as an alternative to ';' to start a comment. --- exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec.c b/exec.c index 849e1ca..09a1cd1 100644 --- a/exec.c +++ b/exec.c @@ -635,7 +635,7 @@ static int dobuf(struct buffer *bp) ++eline; /* dump comments and blank lines */ - if (*eline == ';' || *eline == 0) + if (*eline == ';' || *eline == '#' || *eline == 0) goto onward; #if DEBUGM From 1ab601071e94fc08de31670e3fcb6fe13f7341a7 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 19 Aug 2015 15:46:40 +0800 Subject: [PATCH 03/27] In order to support # as comment indicator, buffer content prefix is changed to '='. #buffername becomes =buffername. --- eval.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/eval.c b/eval.c index 00b7a2b..7e5c685 100644 --- a/eval.c +++ b/eval.c @@ -1117,15 +1117,9 @@ static int gettyp( char *token) { /* grab the first char (this is all we need) */ c = *token; - /* no blanks!!! */ - if (c == 0) - return TKNUL; - - /* a numeric literal? */ - if( (c >= '0' && c <= '9') || c == '-') - return TKLIT; - switch (c) { + case 0: /* no blanks!!! */ + return TKNUL ; case '"': return TKSTR; @@ -1133,7 +1127,7 @@ static int gettyp( char *token) { return TKDIR; case '@': return TKARG; - case '#': + case '=': return TKBUF; case '$': return TKENV; @@ -1145,7 +1139,11 @@ static int gettyp( char *token) { return TKLBL; default: - return TKCMD; + /* a numeric literal? */ + if( (c >= '0' && c <= '9') || c == '-') + return TKLIT; + else + return TKCMD; } } From 51967939b88f68c9df2ed55abe82bb482c289b5d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 25 Aug 2015 09:17:41 +0800 Subject: [PATCH 04/27] Harmonize usage of mlreply, mlreplyt and nextarg based on actual needs (input always terminated either on meta or newline characters). --- eval.c | 2 +- exec.c | 2 +- input.c | 24 ++++++------------------ input.h | 5 +++-- random.c | 4 ++-- search.c | 3 ++- 6 files changed, 15 insertions(+), 25 deletions(-) diff --git a/eval.c b/eval.c index 7e5c685..01e2c44 100644 --- a/eval.c +++ b/eval.c @@ -1172,7 +1172,7 @@ char *getval(char *token) strcpy(token, getval(&token[1])); distmp = discmd; /* echo it always! */ discmd = TRUE; - status = getstring(token, buf, NSTRING, ctoec('\n')); + status = getstring( token, buf, NSTRING, nlc) ; discmd = distmp; if (status == ABORT) return errorm; diff --git a/exec.c b/exec.c index 09a1cd1..dec8f3b 100644 --- a/exec.c +++ b/exec.c @@ -298,7 +298,7 @@ int macarg( char *tok, int toksz) savcle = clexec; /* save execution mode */ clexec = TRUE; /* get the argument */ - status = nextarg("", tok, toksz, ctoec('\n')); + status = mlreply( "", tok, toksz) ; clexec = savcle; /* restore execution mode */ return status; } diff --git a/input.c b/input.c index d8637d3..5e4e960 100644 --- a/input.c +++ b/input.c @@ -52,6 +52,8 @@ int ctlxc = CONTROL | 'X' ; /* current control X prefix char */ int reptc = CONTROL | 'U' ; /* current universal repeat char */ int abortc = CONTROL | 'G' ; /* current abort command char */ +const int nlc = CONTROL | 'J' ; /* end of input char */ + static const int quotec = 0x11 ; /* quote char during mlreply() */ static void outstring( char *s) ; @@ -91,14 +93,12 @@ int mlyesno( const char *prompt) * return. Handle erase, kill, and abort keys. */ -int mlreply( const char *prompt, char *buf, int nbuf) -{ - return nextarg(prompt, buf, nbuf, ctoec('\n')); +int mlreply( const char *prompt, char *buf, int nbuf) { + return nextarg( prompt, buf, nbuf, nlc) ; } -int mlreplyt(const char *prompt, char *buf, int nbuf, int eolchar) -{ - return nextarg(prompt, buf, nbuf, eolchar); +int mlreplyt( const char *prompt, char *buf, int nbuf) { + return nextarg( prompt, buf, nbuf, metac) ; } /* @@ -115,18 +115,6 @@ int ectoc(int c) return c; } -/* - * ctoec: - * character to extended character - * pull out the CONTROL and SPEC prefixes (if possible) - */ -int ctoec(int c) -{ - if (c >= 0x00 && c <= 0x1F) - c = CONTROL | (c + '@'); - return c; -} - /* * get a command name from the command line. Command completion means * that pressing a will attempt to complete an unfinished command diff --git a/input.h b/input.h index 03aa106..fffd38c 100644 --- a/input.h +++ b/input.h @@ -14,17 +14,18 @@ extern int kbdm[] ; /* Holds kayboard macro data */ extern int *kbdptr ; /* current position in keyboard buf */ extern int *kbdend ; /* ptr to end of the keyboard */ extern int disinp ; /* display input characters */ + extern int metac; /* current meta character */ extern int ctlxc; /* current control X prefix char */ extern int reptc; /* current universal repeat char */ extern int abortc; /* current abort command char */ +extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; int mlreply( const char *prompt, char *buf, int nbuf) ; -int mlreplyt( const char *prompt, char *buf, int nbuf, int eolchar) ; +int mlreplyt( const char *prompt, char *buf, int nbuf) ; int ectoc( int c) ; -int ctoec( int c) ; fn_t getname( void) ; int tgetc( void) ; int get1key( void) ; diff --git a/random.c b/random.c index 2ecc43a..b791898 100644 --- a/random.c +++ b/random.c @@ -1196,7 +1196,7 @@ int istring(int f, int n) /* ask for string to insert */ status = - mlreplyt("String to insert: ", tstring, sizeof tstring - 1, metac) ; + mlreplyt( "String to insert: ", tstring, sizeof tstring - 1) ; if (status != TRUE) return status; @@ -1224,7 +1224,7 @@ int ovstring(int f, int n) /* ask for string to insert */ status = - mlreplyt( "String to overwrite: ", tstring, NSTRING, metac) ; + mlreplyt( "String to overwrite: ", tstring, NSTRING) ; if (status != TRUE) return status; diff --git a/search.c b/search.c index ee9091f..be4b871 100644 --- a/search.c +++ b/search.c @@ -713,7 +713,8 @@ static int readpattern(char *prompt, char *apat, int srch) * Then, if it's the search string, make a reversed pattern. * *Then*, make the meta-pattern, if we are defined that way. */ - if ((status = mlreplyt(tpat, tpat, NPAT, metac)) == TRUE) { + status = mlreplyt( tpat, tpat, NPAT) ; + if( status == TRUE) { strcpy(apat, tpat); if (srch) { /* If we are doing the search string. */ /* Reverse string copy, and remember From 8d412dc388a0605db1cdb5ed8682e9d0ec4d4293 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 25 Aug 2015 16:34:15 +0800 Subject: [PATCH 05/27] Review file scope of functions dealing with token extraction and token evaluation. --- eval.c | 8 +++----- exec.c | 48 +++++++++++++++++------------------------------- exec.h | 3 +-- input.c | 17 +++++++++++++++++ 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/eval.c b/eval.c index 01e2c44..0a99600 100644 --- a/eval.c +++ b/eval.c @@ -771,7 +771,7 @@ int setvar(int f, int n) /* first get the variable to set.. */ if (clexec == FALSE) { - status = mlreply( "Variable to set: ", var, sizeof var) ; + status = getstring( "Variable to set: ", var, sizeof var, nlc) ; if (status != TRUE) return status; } else { /* macro line argument */ @@ -889,10 +889,8 @@ fvar: var[4] = 0; if (strcmp(&var[1], "ind") == 0) { /* grab token, and eval it */ - gettoken( var, size) ; - strncpy( var, getval( var), size - 1) ; - var[ size - 1] = '\0' ; - goto fvar; + if( TRUE == gettokval( var, size)) + goto fvar ; } } diff --git a/exec.c b/exec.c index dec8f3b..601baac 100644 --- a/exec.c +++ b/exec.c @@ -286,6 +286,23 @@ void gettoken( char *tok, int maxtoksize) { execstr = token( execstr, tok, maxtoksize) ; } +boolean gettokval( char *tok, int size) { + char *tmpbuf ; + + tmpbuf = malloc( size) ; + if( tmpbuf == NULL) + return FALSE ; + + /* grab token and advance past */ + gettoken( tmpbuf, size) ; + + /* evaluate it */ + strncpy( tok, getval( tmpbuf), size - 1) ; + tok[ size - 1] = '\0' ; + free( tmpbuf) ; + return TRUE ; +} + /* * get a macro line argument * @@ -303,37 +320,6 @@ int macarg( char *tok, int toksz) return status; } -/* - * nextarg: - * get the next argument - * - * const char *prompt; prompt to use if we must be interactive - * char *buffer; buffer to put token into - * int size; size of the buffer - * int terminator; terminating char to be used on interactive fetch - */ -int nextarg(const char *prompt, char *buffer, int size, int terminator) -{ - char *tmpbuf ; - - /* if we are interactive, go get it! */ - if (clexec == FALSE) - return getstring(prompt, buffer, size, terminator); - - tmpbuf = malloc( size) ; - if( tmpbuf == NULL) - return FALSE ; - - /* grab token and advance past */ - gettoken( tmpbuf, size) ; - - /* evaluate it */ - strncpy( buffer, getval( tmpbuf), size - 1) ; - buffer[ size - 1] = '\0' ; - free( tmpbuf) ; - return TRUE; -} - /* * storemac: * Set up a macro buffer and flag to store all diff --git a/exec.h b/exec.h index a64c786..8a16448 100644 --- a/exec.h +++ b/exec.h @@ -15,13 +15,12 @@ int execproc( int f, int n) ; extern boolean clexec ; /* command line execution flag */ - void ue_system( const char *cmd) ; int namedcmd( int f, int n) ; int execcmd( int f, int n) ; void gettoken( char *tok, int maxtoksize) ; +boolean gettokval( char *tok, int maxtoksize) ; int macarg( char *tok, int toksz) ; -int nextarg( const char *prompt, char *buffer, int size, int terminator) ; int storemac( int f, int n) ; int execbuf( int f, int n) ; int execfile( int f, int n) ; diff --git a/input.c b/input.c index 5e4e960..6d67643 100644 --- a/input.c +++ b/input.c @@ -85,6 +85,23 @@ int mlyesno( const char *prompt) } } +/* + * nextarg: + * get the next argument + * + * const char *prompt; prompt to use if we must be interactive + * char *buffer; buffer to put token into + * int size; size of the buffer + * int terminator; terminating char to be used on interactive fetch + */ +static int nextarg( const char *prompt, char *buf, int size, int terminator) { + /* if we are interactive, go get it! */ + if( clexec == FALSE) + return getstring( prompt, buf, size, terminator) ; + else + return gettokval( buf, size) ; +} + /* * Write a prompt into the message line, then read back a response. Keep * track of the physical position of the cursor. If we are in a keyboard From c3f4666ff301431c959146a43c38497339e5f30c Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 1 Sep 2015 11:29:08 +0800 Subject: [PATCH 06/27] Review usage of mlreply/ macarg/ gettokval according to execution context. --- bind.c | 2 +- eval.c | 7 ++++--- exec.c | 24 +++++++++++++----------- exec.h | 1 - input.c | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bind.c b/bind.c index 9733fff..10422e9 100644 --- a/bind.c +++ b/bind.c @@ -441,7 +441,7 @@ static unsigned int getckey( int mflag) { if( clexec) { char tok[ NSTRING] ; /* command incoming */ - if( TRUE != macarg( tok, sizeof tok)) /* get the next token */ + if( TRUE != gettokval( tok, sizeof tok)) /* get the next token */ c = 0 ; /* return dummy key on failure */ else c = stock( tok) ; diff --git a/eval.c b/eval.c index 0a99600..671c7aa 100644 --- a/eval.c +++ b/eval.c @@ -344,16 +344,17 @@ static char *gtfun( char *fname) { arg1 = arg2 = NULL ; + assert( clexec == TRUE) ; /* means macarg can be replaced by gettokval */ /* if needed, retrieve the first argument */ if (funcs[fnum].f_type >= MONAMIC) { - if( macarg( argx, sizeof argx) != TRUE) + if( TRUE != gettokval( argx, sizeof argx)) return errorm; /* if needed, retrieve the second argument */ if (funcs[fnum].f_type >= DYNAMIC) { arg1 = malloc( strlen( argx) + 1) ; strcpy( arg1, argx) ; - if( macarg( argx, sizeof argx) != TRUE) { + if( TRUE != gettokval( argx, sizeof argx)) { free( arg1) ; return errorm; } @@ -362,7 +363,7 @@ static char *gtfun( char *fname) { if (funcs[fnum].f_type >= TRINAMIC) { arg2 = malloc( strlen( argx) + 1) ; strcpy( arg2, argx) ; - if( macarg( argx, sizeof argx) != TRUE) { + if( TRUE != gettokval( argx, sizeof argx)) { free( arg1) ; free( arg2) ; return errorm; diff --git a/exec.c b/exec.c index 601baac..afb3933 100644 --- a/exec.c +++ b/exec.c @@ -79,6 +79,7 @@ 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) ; void ue_system( const char *cmd) { int ret ; @@ -179,8 +180,10 @@ static int docmd( char *cline) { /* process leadin argument */ if( !is_it_cmd( tkn)) { f = TRUE; - strncpy( tkn, getval( tkn), sizeof tkn - 1) ; - tkn[ sizeof tkn - 1] = '\0' ; +/* macarg already includes a getval, skip for now + strncpy( tkn, getval( tkn), sizeof tkn - 1) ; + tkn[ sizeof tkn - 1] = '\0' ; +*/ n = atoi(tkn); /* and now get the command to execute */ @@ -308,16 +311,15 @@ boolean gettokval( char *tok, int size) { * * char *tok; buffer to place argument */ -int macarg( char *tok, int toksz) -{ - boolean savcle ; /* buffer to store original clexec */ - int status; +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 = mlreply( "", tok, toksz) ; - clexec = savcle; /* restore execution mode */ - return status; + savcle = clexec ; /* save execution mode */ + clexec = TRUE ; /* get the argument */ + status = gettokval( tok, toksz) ; + clexec = savcle ; /* restore execution mode */ + return status ; } /* diff --git a/exec.h b/exec.h index 8a16448..8250982 100644 --- a/exec.h +++ b/exec.h @@ -20,7 +20,6 @@ int namedcmd( int f, int n) ; int execcmd( int f, int n) ; void gettoken( char *tok, int maxtoksize) ; boolean gettokval( char *tok, int maxtoksize) ; -int macarg( char *tok, int toksz) ; int storemac( int f, int n) ; int execbuf( int f, int n) ; int execfile( int f, int n) ; diff --git a/input.c b/input.c index 6d67643..d86397b 100644 --- a/input.c +++ b/input.c @@ -152,7 +152,7 @@ fn_t getname(void) /* if we are executing a command line get the next arg and match it */ if (clexec) { - if( macarg( buf, sizeof buf) != TRUE) + if( TRUE != gettokval( buf, sizeof buf)) return NULL; return fncmatch(&buf[0]); } From e0550db4d156d9a76a5d78a5bdde6ef470cd3eca Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 18 Sep 2015 17:53:58 +0800 Subject: [PATCH 07/27] $line returns a full copy of the current line instead of truncated at NSTRING. --- line.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/line.c b/line.c index 6aeecf8..587a74d 100644 --- a/line.c +++ b/line.c @@ -619,14 +619,24 @@ char *getctext(void) int size; /* length of line to return */ char *sp; /* string pointer into line */ char *dp; /* string pointer into returned line */ - static char rline[NSTRING]; /* line to return */ + static int rsize = 0 ; + static char *rline ; /* line to return */ /* find the contents of the current line and its length */ lp = curwp->w_dotp; sp = lp->l_text; size = lp->l_used; - if (size >= NSTRING) - size = NSTRING - 1; + if( size >= rsize) { + if( rsize) + free( rline) ; + + rsize = size + 1 ; + rline = malloc( rsize) ; + if( rline == NULL) { + rsize = 0 ; + return "" ; + } + } /* copy it across */ dp = rline; From 2c9cbbfb1e1774e2caa621de525d03d2e54a0157 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 18 Sep 2015 17:55:35 +0800 Subject: [PATCH 08/27] write-message buffer adjust to full width of the terminal. --- eval.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/eval.c b/eval.c index 671c7aa..45e7937 100644 --- a/eval.c +++ b/eval.c @@ -1428,16 +1428,23 @@ int clrmes( int f, int n) { * int f, n; arguments ignored */ int writemsg( int f, int n) { - int status; - char buf[ NSTRING] ; /* buffer to recieve message into */ + int status ; + int size ; + char *buf ; /* buffer to receive message into */ - status = mlreply( "Message to write: ", buf, sizeof buf - 1) ; - if( status != TRUE) - return status ; + size = term.t_ncol + 1 ; + buf = malloc( size) ; + if( buf == NULL) + return FALSE ; + status = mlreply( "Message to write: ", buf, size) ; + if( status == TRUE) /* write the message out */ - mlforce( buf) ; - return TRUE ; + mlforce( buf) ; + + free( buf) ; + return status ; } +/* end of eval.c */ From b5eb424ad0c60d9812d295087eea4051ce47db97 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 27 Sep 2015 19:19:55 +0800 Subject: [PATCH 09/27] Introduce newtoken()/getnewtoken() for full length extraction of tokens. --- exec.c | 78 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/exec.c b/exec.c index afb3933..3f5cc41 100644 --- a/exec.c +++ b/exec.c @@ -212,16 +212,21 @@ static int docmd( char *cline) { } /* - * token: + * new token: * chop a token off a string * return a pointer past the token * - * char *src, *tok; source string, destination token string - * int size; maximum size of token + * char *src in, source string + * char **tokref out, destination of newly allocated token string */ -static char *token( char *src, char *tok, int size) { - int quotef; /* is the current string quoted? */ - char c; /* temporary character */ +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') @@ -230,6 +235,8 @@ static char *token( char *src, char *tok, int size) { /* scan through the source string */ quotef = FALSE; while (*src) { + char c ; /* temporary character */ + /* process special characters */ if (*src == '~') { ++src; @@ -254,9 +261,6 @@ static char *token( char *src, char *tok, int size) { default: c = *(src - 1); } - if (--size > 0) { - *tok++ = c; - } } else { /* check for the end of the token */ if (quotef) { @@ -273,31 +277,67 @@ static char *token( char *src, char *tok, int size) { /* record the character */ c = *src++; - if (--size > 0) - *tok++ = c; } + + 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; - *tok = 0; + + 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 { + strncpy( tok, newtok, maxtoksize - 1) ; + tok[ maxtoksize - 1] = 0 ; + free( newtok) ; + } + + return srcstr ; +} + void gettoken( char *tok, int maxtoksize) { execstr = token( execstr, tok, maxtoksize) ; } +void getnewtoken( char **tokref) { + execstr = newtoken( execstr, tokref) ; +} + boolean gettokval( char *tok, int size) { char *tmpbuf ; - - tmpbuf = malloc( size) ; - if( tmpbuf == NULL) - return FALSE ; /* grab token and advance past */ - gettoken( tmpbuf, size) ; + getnewtoken( &tmpbuf) ; + if( tmpbuf == NULL) + return FALSE ; /* evaluate it */ strncpy( tok, getval( tmpbuf), size - 1) ; @@ -770,10 +810,8 @@ static int dobuf(struct buffer *bp) case DGOTO: /* GOTO directive */ /* .....only if we are currently executing */ if (execlevel == 0) { - /* grab label to jump to */ - eline = - token( eline, golabel, sizeof golabel) ; + eline = token( eline, golabel, sizeof golabel) ; linlen = strlen(golabel); glp = hlp->l_fp; while (glp != hlp) { From cfa5c7fb65b779d163997b593c093407cf6b128f Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 27 Sep 2015 21:11:51 +0800 Subject: [PATCH 10/27] Review getnewtoken, introduce getnewtokval and rewrite function evaluation accordingly. --- bind.c | 12 ++++--- eval.c | 105 +++++++++++++++++++++++++++++---------------------------- exec.c | 29 ++++++++++++++-- exec.h | 1 + 4 files changed, 88 insertions(+), 59 deletions(-) diff --git a/bind.c b/bind.c index 10422e9..342a1ea 100644 --- a/bind.c +++ b/bind.c @@ -11,6 +11,7 @@ */ #include +#include #include #include "estruct.h" @@ -439,12 +440,15 @@ static unsigned int getckey( int mflag) { /* check to see if we are executing a command line */ if( clexec) { - char tok[ NSTRING] ; /* command incoming */ - - if( TRUE != gettokval( tok, sizeof tok)) /* get the next token */ + char *tok ; /* command incoming */ + + tok = getnewtokval() ; /* get the next token */ + if( tok == NULL) c = 0 ; /* return dummy key on failure */ - else + else { c = stock( tok) ; + free( tok) ; + } } else { /* or the normal way */ if( mflag) c = get1key() ; diff --git a/eval.c b/eval.c index 45e7937..79170bc 100644 --- a/eval.c +++ b/eval.c @@ -312,9 +312,9 @@ void varinit(void) */ static char *gtfun( char *fname) { unsigned fnum ; /* index to function to eval */ - char argx[ 512] ; /* last argument, fixed sized allocation */ char *arg1 ; /* value of first argument */ char *arg2 ; /* value of second argument */ + char *arg3 ; /* last argument */ char *retstr ; /* return value */ int low, high ; /* binary search indexes */ @@ -342,28 +342,26 @@ static char *gtfun( char *fname) { if (fnum == ARRAY_SIZE(funcs)) return errorm; - arg1 = arg2 = NULL ; - + arg1 = arg2 = arg3 = NULL ; assert( clexec == TRUE) ; /* means macarg can be replaced by gettokval */ /* if needed, retrieve the first argument */ if (funcs[fnum].f_type >= MONAMIC) { - if( TRUE != gettokval( argx, sizeof argx)) + arg1 = getnewtokval() ; + if( arg1 == NULL) return errorm; /* if needed, retrieve the second argument */ if (funcs[fnum].f_type >= DYNAMIC) { - arg1 = malloc( strlen( argx) + 1) ; - strcpy( arg1, argx) ; - if( TRUE != gettokval( argx, sizeof argx)) { + arg2 = getnewtokval() ; + if( arg2 == NULL) { free( arg1) ; return errorm; } /* if needed, retrieve the third argument */ if (funcs[fnum].f_type >= TRINAMIC) { - arg2 = malloc( strlen( argx) + 1) ; - strcpy( arg2, argx) ; - if( TRUE != gettokval( argx, sizeof argx)) { + arg3 = getnewtokval() ; + if( arg3 == NULL) { free( arg1) ; free( arg2) ; return errorm; @@ -377,30 +375,30 @@ static char *gtfun( char *fname) { int sz ; case UFADD | DYNAMIC: - retstr = i_to_a( atoi( arg1) + atoi( argx)) ; + retstr = i_to_a( atoi( arg1) + atoi( arg2)) ; break ; case UFSUB | DYNAMIC: - retstr = i_to_a( atoi( arg1) - atoi( argx)) ; + retstr = i_to_a( atoi( arg1) - atoi( arg2)) ; break ; case UFTIMES | DYNAMIC: - retstr = i_to_a( atoi( arg1) * atoi( argx)) ; + retstr = i_to_a( atoi( arg1) * atoi( arg2)) ; break ; case UFDIV | DYNAMIC: - sz = atoi( argx) ; + sz = atoi( arg2) ; retstr = (sz == 0) ? errorm : i_to_a( atoi( arg1) / sz) ; break ; case UFMOD | DYNAMIC: - sz = atoi( argx) ; + sz = atoi( arg2) ; retstr = (sz == 0) ? errorm : i_to_a( atoi( arg1) % sz) ; break ; case UFNEG | MONAMIC: - retstr = i_to_a( -atoi( argx)) ; + retstr = i_to_a( -atoi( arg1)) ; break ; case UFCAT | DYNAMIC: { int sz1 ; sz1 = strlen( arg1) ; - sz = sz1 + strlen( argx) + 1 ; + sz = sz1 + strlen( arg2) + 1 ; if( sz > ressize) { free( result) ; result = malloc( sz) ; @@ -408,12 +406,12 @@ static char *gtfun( char *fname) { } strcpy( result, arg1) ; - strcpy( &result[ sz1], argx) ; + strcpy( &result[ sz1], arg2) ; retstr = result ; } break ; case UFLEFT | DYNAMIC: - sz = atoi( argx) ; + sz = atoi( arg2) ; if( sz >= ressize) { free( result) ; result = malloc( sz + 1) ; @@ -425,7 +423,7 @@ static char *gtfun( char *fname) { retstr = result ; break ; case UFRIGHT | DYNAMIC: - sz = atoi( argx) ; + sz = atoi( arg2) ; if( sz >= ressize) { free( result) ; result = malloc( sz + 1) ; @@ -435,7 +433,7 @@ static char *gtfun( char *fname) { retstr = strcpy( result, &arg1[ strlen( arg1) - sz]) ; break ; case UFMID | TRINAMIC: - sz = atoi( argx) ; + sz = atoi( arg3) ; if( sz >= ressize) { free( result) ; result = malloc( sz + 1) ; @@ -447,28 +445,28 @@ static char *gtfun( char *fname) { retstr = result ; break ; case UFNOT | MONAMIC: - retstr = ltos( stol( argx) == FALSE) ; + retstr = ltos( stol( arg1) == FALSE) ; break ; case UFEQUAL | DYNAMIC: - retstr = ltos( atoi( arg1) == atoi( argx)) ; + retstr = ltos( atoi( arg1) == atoi( arg2)) ; break ; case UFLESS | DYNAMIC: - retstr = ltos( atoi( arg1) < atoi( argx)) ; + retstr = ltos( atoi( arg1) < atoi( arg2)) ; break ; case UFGREATER | DYNAMIC: - retstr = ltos( atoi( arg1) > atoi( argx)) ; + retstr = ltos( atoi( arg1) > atoi( arg2)) ; break ; case UFSEQUAL | DYNAMIC: - retstr = ltos( strcmp( arg1, argx) == 0) ; + retstr = ltos( strcmp( arg1, arg2) == 0) ; break ; case UFSLESS | DYNAMIC: - retstr = ltos( strcmp( arg1, argx) < 0) ; + retstr = ltos( strcmp( arg1, arg2) < 0) ; break ; case UFSGREAT | DYNAMIC: - retstr = ltos( strcmp( arg1, argx) > 0) ; + retstr = ltos( strcmp( arg1, arg2) > 0) ; break ; case UFIND | MONAMIC: - retstr = getval( argx) ; + retstr = getval( arg1) ; sz = strlen( retstr) + 1 ; if( sz > ressize) { free( result) ; @@ -479,42 +477,42 @@ static char *gtfun( char *fname) { retstr = strcpy( result, retstr) ; break ; case UFAND | DYNAMIC: - retstr = ltos( stol( arg1) && stol( argx)) ; + retstr = ltos( stol( arg1) && stol( arg2)) ; break ; case UFOR | DYNAMIC: - retstr = ltos( stol( arg1) || stol( argx)) ; + retstr = ltos( stol( arg1) || stol( arg2)) ; break ; case UFLENGTH | MONAMIC: - retstr = i_to_a( strlen( argx)) ; + retstr = i_to_a( strlen( arg1)) ; break ; case UFUPPER | MONAMIC: - sz = strlen( argx) ; + sz = strlen( arg1) ; if( sz >= ressize) { free( result) ; result = malloc( sz + 1) ; ressize = sz + 1 ; } - retstr = mkupper( result, argx) ; + retstr = mkupper( result, arg1) ; break ; case UFLOWER | MONAMIC: - sz = strlen( argx) ; + sz = strlen( arg1) ; if( sz >= ressize) { free( result) ; result = malloc( sz + 1) ; ressize = sz + 1 ; } - strcpy( result, argx) ; /* result is at least as long as argx */ + strcpy( result, arg1) ; /* result is at least as long as arg1 */ retstr = mklower( result) ; break ; case UFTRUTH | MONAMIC: - retstr = ltos( atoi( argx) == 42) ; + retstr = ltos( atoi( arg1) == 42) ; break ; case UFASCII | MONAMIC: { unicode_t c ; - utf8_to_unicode( argx, 0, 4, &c) ; + utf8_to_unicode( arg1, 0, 4, &c) ; retstr = i_to_a( c) ; } @@ -522,7 +520,7 @@ static char *gtfun( char *fname) { case UFCHR | MONAMIC: { unicode_t c ; - c = atoi( argx) ; + c = atoi( arg1) ; if( c > 0x10FFFF) retstr = errorm ; else { @@ -539,7 +537,7 @@ static char *gtfun( char *fname) { retstr = result ; break ; case UFRND | MONAMIC: - sz = abs( atoi( argx)) ; + sz = abs( atoi( arg1)) ; if( sz == 0) sz = ernd() ; else @@ -548,14 +546,14 @@ static char *gtfun( char *fname) { retstr = i_to_a( sz) ; break ; case UFABS | MONAMIC: - retstr = i_to_a( abs( atoi( argx))) ; + retstr = i_to_a( abs( atoi( arg1))) ; break ; case UFSINDEX | DYNAMIC: - retstr = i_to_a( sindex( arg1, argx)) ; + retstr = i_to_a( sindex( arg1, arg2)) ; break ; case UFENV | MONAMIC: #if ENVFUNC - retstr = getenv( argx) ; + retstr = getenv( arg1) ; if( retstr == NULL) retstr = "" ; #else @@ -563,36 +561,39 @@ static char *gtfun( char *fname) { #endif break ; case UFBIND | MONAMIC: - retstr = transbind( argx) ; + retstr = transbind( arg1) ; break ; case UFEXIST | MONAMIC: - retstr = ltos( fexist( argx)) ; + retstr = ltos( fexist( arg1)) ; break ; case UFFIND | MONAMIC: - retstr = flook( argx, TRUE) ; + retstr = flook( arg1, TRUE) ; if( retstr == NULL) retstr = "" ; break ; case UFBAND | DYNAMIC: - retstr = i_to_a( atoi( arg1) & atoi( argx)) ; + retstr = i_to_a( atoi( arg1) & atoi( arg2)) ; break ; case UFBOR | DYNAMIC: - retstr = i_to_a( atoi( arg1) | atoi( argx)) ; + retstr = i_to_a( atoi( arg1) | atoi( arg2)) ; break ; case UFBXOR | DYNAMIC: - retstr = i_to_a( atoi( arg1) ^ atoi( argx)) ; + retstr = i_to_a( atoi( arg1) ^ atoi( arg2)) ; break ; - case UFBNOT | DYNAMIC: - retstr = i_to_a( ~atoi( argx)) ; + case UFBNOT | MONAMIC: + retstr = i_to_a( ~atoi( arg1)) ; break ; case UFXLATE | TRINAMIC: - retstr = xlat( arg1, arg2, argx) ; + retstr = xlat( arg1, arg2, arg3) ; break ; default: assert( FALSE) ; /* never should get here */ retstr = errorm ; } + if( arg3) + free( arg3) ; + if( arg2) free( arg2) ; diff --git a/exec.c b/exec.c index 3f5cc41..0262f08 100644 --- a/exec.c +++ b/exec.c @@ -327,15 +327,18 @@ void gettoken( char *tok, int maxtoksize) { execstr = token( execstr, tok, maxtoksize) ; } -void getnewtoken( char **tokref) { - execstr = newtoken( execstr, tokref) ; +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 */ - getnewtoken( &tmpbuf) ; + tmpbuf = getnewtoken() ; if( tmpbuf == NULL) return FALSE ; @@ -346,6 +349,26 @@ boolean gettokval( char *tok, int size) { 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 * diff --git a/exec.h b/exec.h index 8250982..4db15b5 100644 --- a/exec.h +++ b/exec.h @@ -20,6 +20,7 @@ int namedcmd( int f, int n) ; int execcmd( int f, int n) ; void gettoken( char *tok, int maxtoksize) ; boolean gettokval( char *tok, int maxtoksize) ; +char *getnewtokval( void) ; int storemac( int f, int n) ; int execbuf( int f, int n) ; int execfile( int f, int n) ; From cbbd860bdc8d01e28b9267df8a85fbe21418b811 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Sun, 27 Sep 2015 22:13:20 +0800 Subject: [PATCH 11/27] Rework insert-string for dynamic token input. --- input.c | 24 ++++++++++++++++++++++++ input.h | 1 + random.c | 29 ++++++++++++++++------------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/input.c b/input.c index d86397b..5731ccd 100644 --- a/input.c +++ b/input.c @@ -102,6 +102,26 @@ static int nextarg( const char *prompt, char *buf, int size, int terminator) { return gettokval( buf, size) ; } +static char *newnextarg( const char *prompt, int terminator) { + /* if we are interactive, go get it! */ + if( clexec == FALSE) { + char *buf ; + int size ; + + size = term.t_ncol + 1 ; + buf = malloc( size) ; + if( buf != NULL) { + if( TRUE != getstring( prompt, buf, size, terminator)) { + free( buf) ; + buf = NULL ; + } + } + + return buf ; + } else + return getnewtokval() ; +} + /* * Write a prompt into the message line, then read back a response. Keep * track of the physical position of the cursor. If we are in a keyboard @@ -118,6 +138,10 @@ int mlreplyt( const char *prompt, char *buf, int nbuf) { return nextarg( prompt, buf, nbuf, metac) ; } +char *newmlreplyt( const char *prompt) { + return newnextarg( prompt, metac) ; +} + /* * ectoc: * expanded character to character diff --git a/input.h b/input.h index fffd38c..f163c8d 100644 --- a/input.h +++ b/input.h @@ -25,6 +25,7 @@ extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; int mlreply( const char *prompt, char *buf, int nbuf) ; int mlreplyt( const char *prompt, char *buf, int nbuf) ; +char *newmlreplyt( const char *prompt) ; int ectoc( int c) ; fn_t getname( void) ; int tgetc( void) ; diff --git a/random.c b/random.c index b791898..934b9c3 100644 --- a/random.c +++ b/random.c @@ -12,6 +12,7 @@ */ #include +#include #include #include "basic.h" @@ -1191,24 +1192,26 @@ int fmatch(int ch) */ int istring(int f, int n) { - int status; /* status return code */ - char tstring[ 512] ; /* string to add */ + int status ; /* status return code */ + char *tstring ; /* string to add */ /* ask for string to insert */ - status = - mlreplyt( "String to insert: ", tstring, sizeof tstring - 1) ; - if (status != TRUE) - return status; + tstring = newmlreplyt( "String to insert: ") ; + if( tstring == NULL) + return FALSE ; - if (f == FALSE) - n = 1; - - if (n < 0) - n = -n; + if( f == FALSE) + n = 1 ; + else if( n < 0) + n = -n ; /* insert it */ - while (n-- && (status = linstr(tstring))); - return status; + status = TRUE ; + while( n-- && status) + status = linstr( tstring) ; + + free( tstring) ; + return status ; } /* From 3ffa8967efcad51d9c257638fe0498c281d97fec Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 28 Sep 2015 13:37:22 +0800 Subject: [PATCH 12/27] Align implementation of overwrite-string with insert-string. --- random.c | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/random.c b/random.c index 934b9c3..1dcc0bb 100644 --- a/random.c +++ b/random.c @@ -1184,19 +1184,12 @@ int fmatch(int ch) return TRUE; } -/* - * ask for and insert a string into the current - * buffer at the current point - * - * int f, n; ignored arguments - */ -int istring(int f, int n) -{ +static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { int status ; /* status return code */ char *tstring ; /* string to add */ /* ask for string to insert */ - tstring = newmlreplyt( "String to insert: ") ; + tstring = newmlreplyt( prompt) ; if( tstring == NULL) return FALSE ; @@ -1208,36 +1201,30 @@ int istring(int f, int n) /* insert it */ status = TRUE ; while( n-- && status) - status = linstr( tstring) ; + status = fun( tstring) ; free( tstring) ; return status ; } +/* + * ask for and insert a string into the current + * buffer at the current point + * + * int f, n; ignored arguments + */ +int istring( int f, int n) { + return iovstring( f, n, "String to insert: ", linstr) ; +} + /* * ask for and overwite a string into the current * buffer at the current point * * int f, n; ignored arguments */ -int ovstring(int f, int n) -{ - int status; /* status return code */ - char tstring[ NSTRING + 1] ; /* string to add */ - - /* ask for string to insert */ - status = - mlreplyt( "String to overwrite: ", tstring, NSTRING) ; - if (status != TRUE) - return status; - - if (f == FALSE) - n = 1; - - if (n < 0) - n = -n; - - /* insert it */ - while (n-- && (status = lover(tstring))); - return status; +int ovstring( int f, int n) { + return iovstring( f, n, "String to overwrite: ", lover) ; } + +/* end of random.c */ From ce4d105794eedf219e24070a4bee33caaed5e1b7 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 28 Sep 2015 17:46:00 +0800 Subject: [PATCH 13/27] Allow either dynamic or pre-defined input size from message line. Insure to capture ABORT status when doing input from message line. --- input.c | 49 ++++++++++++++++++++++++++++++------------------- input.h | 7 ++++++- random.c | 14 ++++++++++---- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/input.c b/input.c index 5731ccd..e642a7a 100644 --- a/input.c +++ b/input.c @@ -102,24 +102,35 @@ static int nextarg( const char *prompt, char *buf, int size, int terminator) { return gettokval( buf, size) ; } -static char *newnextarg( const char *prompt, int terminator) { - /* if we are interactive, go get it! */ - if( clexec == FALSE) { - char *buf ; - int size ; +static newarg_t *newnextarg( const char *prompt, int size, int terminator) { + newarg_t *argp ; - size = term.t_ncol + 1 ; - buf = malloc( size) ; - if( buf != NULL) { - if( TRUE != getstring( prompt, buf, size, terminator)) { - free( buf) ; - buf = NULL ; - } - } - - return buf ; - } else - return getnewtokval() ; + argp = malloc( sizeof( newarg_t)) ; + if( argp != NULL) { + /* if we are interactive, go get it! */ + if( clexec == FALSE) { + if( size <= 1) { + size = term.t_ncol - strlen( prompt) + 1 ; + if( size < 24) + size = 24 ; + } + + argp->buf = malloc( size) ; + if( argp->buf != NULL) { + argp->status = getstring( prompt, argp->buf, size, terminator) ; + if( TRUE != argp->status) { + free( argp->buf) ; + argp->buf = NULL ; + } + } else + argp->status = FALSE ; + } else { + argp->buf = getnewtokval() ; + argp->status = (argp->buf == NULL) ? FALSE : TRUE ; + } + } + + return argp ; } /* @@ -138,8 +149,8 @@ int mlreplyt( const char *prompt, char *buf, int nbuf) { return nextarg( prompt, buf, nbuf, metac) ; } -char *newmlreplyt( const char *prompt) { - return newnextarg( prompt, metac) ; +newarg_t *newmlargt( const char *prompt, int size) { + return newnextarg( prompt, size, metac) ; } /* diff --git a/input.h b/input.h index f163c8d..85c1ef4 100644 --- a/input.h +++ b/input.h @@ -4,6 +4,11 @@ #include "bind.h" +typedef struct { + int status ; + char *buf ; +} newarg_t ; + typedef enum { STOP, PLAY, RECORD } kbdstate ; @@ -25,7 +30,7 @@ extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; int mlreply( const char *prompt, char *buf, int nbuf) ; int mlreplyt( const char *prompt, char *buf, int nbuf) ; -char *newmlreplyt( const char *prompt) ; +newarg_t *newmlargt( const char *prompt, int size) ; int ectoc( int c) ; fn_t getname( void) ; int tgetc( void) ; diff --git a/random.c b/random.c index 1dcc0bb..742c465 100644 --- a/random.c +++ b/random.c @@ -1185,22 +1185,28 @@ int fmatch(int ch) } static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { + newarg_t *argp ; int status ; /* status return code */ char *tstring ; /* string to add */ /* ask for string to insert */ - tstring = newmlreplyt( prompt) ; - if( tstring == NULL) + argp = newmlargt( prompt, 0) ; /* grab as big a token as screen allow */ + if( argp == NULL) return FALSE ; + status = argp->status ; + tstring = argp->buf ; + free( argp) ; + if( tstring == NULL) + return status ; + if( f == FALSE) n = 1 ; else if( n < 0) n = -n ; /* insert it */ - status = TRUE ; - while( n-- && status) + while( n-- && status == TRUE) status = fun( tstring) ; free( tstring) ; From 198980b81f1f5bce2eb62bcebad389a4b2487d51 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 29 Sep 2015 10:25:36 +0800 Subject: [PATCH 14/27] Review newmlargt interface and obsolete mlreplyt. --- input.c | 61 +++++++++++++++++++++++++++----------------------------- input.h | 8 +------- search.c | 7 +++++-- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/input.c b/input.c index e642a7a..3de8278 100644 --- a/input.c +++ b/input.c @@ -102,35 +102,36 @@ static int nextarg( const char *prompt, char *buf, int size, int terminator) { return gettokval( buf, size) ; } -static newarg_t *newnextarg( const char *prompt, int size, int terminator) { - newarg_t *argp ; +static int newnextarg( char **outbufref, const char *prompt, int size, + int terminator) { + int status ; + char *buf ; - argp = malloc( sizeof( newarg_t)) ; - if( argp != NULL) { - /* if we are interactive, go get it! */ - if( clexec == FALSE) { - if( size <= 1) { - size = term.t_ncol - strlen( prompt) + 1 ; - if( size < 24) - size = 24 ; - } - - argp->buf = malloc( size) ; - if( argp->buf != NULL) { - argp->status = getstring( prompt, argp->buf, size, terminator) ; - if( TRUE != argp->status) { - free( argp->buf) ; - argp->buf = NULL ; - } - } else - argp->status = FALSE ; - } else { - argp->buf = getnewtokval() ; - argp->status = (argp->buf == NULL) ? FALSE : TRUE ; + /* if we are interactive, go get it! */ + if( clexec == FALSE) { + if( size <= 1) { + size = term.t_ncol - strlen( prompt) + 1 ; + if( size < 24) + size = 24 ; } + + buf = malloc( size) ; + if( buf == NULL) + status = FALSE ; + else { + status = getstring( prompt, buf, size, terminator) ; + if( TRUE != status) { + free( buf) ; + buf = NULL ; + } + } + } else { + buf = getnewtokval() ; + status = (buf == NULL) ? FALSE : TRUE ; } - - return argp ; + + *outbufref = buf ; + return status ; } /* @@ -145,12 +146,8 @@ int mlreply( const char *prompt, char *buf, int nbuf) { return nextarg( prompt, buf, nbuf, nlc) ; } -int mlreplyt( const char *prompt, char *buf, int nbuf) { - return nextarg( prompt, buf, nbuf, metac) ; -} - -newarg_t *newmlargt( const char *prompt, int size) { - return newnextarg( prompt, size, metac) ; +int newmlargt( char **outbufref, const char *prompt, int size) { + return newnextarg( outbufref, prompt, size, metac) ; } /* diff --git a/input.h b/input.h index 85c1ef4..67714e6 100644 --- a/input.h +++ b/input.h @@ -4,11 +4,6 @@ #include "bind.h" -typedef struct { - int status ; - char *buf ; -} newarg_t ; - typedef enum { STOP, PLAY, RECORD } kbdstate ; @@ -29,8 +24,7 @@ extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; int mlreply( const char *prompt, char *buf, int nbuf) ; -int mlreplyt( const char *prompt, char *buf, int nbuf) ; -newarg_t *newmlargt( const char *prompt, int size) ; +int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; fn_t getname( void) ; int tgetc( void) ; diff --git a/search.c b/search.c index be4b871..9d7d081 100644 --- a/search.c +++ b/search.c @@ -705,6 +705,7 @@ static int readpattern(char *prompt, char *apat, int srch) { int status; char tpat[NPAT + 20]; + char *dynpat ; /* dynamically allocated pattern buffer */ setprompt( tpat, NPAT / 2, prompt, apat) ; @@ -713,9 +714,11 @@ static int readpattern(char *prompt, char *apat, int srch) * Then, if it's the search string, make a reversed pattern. * *Then*, make the meta-pattern, if we are defined that way. */ - status = mlreplyt( tpat, tpat, NPAT) ; + status = newmlargt( &dynpat, tpat, NPAT) ; if( status == TRUE) { - strcpy(apat, tpat); + strncpy( apat, dynpat, NPAT - 1) ; + apat[ NPAT - 1] = 0 ; + free( dynpat) ; if (srch) { /* If we are doing the search string. */ /* Reverse string copy, and remember * the length for substitution purposes. From 7f5f0dd7b39cff04aa28db867f2c33d17a03d00f Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 29 Sep 2015 10:43:19 +0800 Subject: [PATCH 15/27] Introduce newmlarg as alternative to mlreply. Rewrite gotoline accordingly. --- basic.c | 21 +++++++++++---------- input.c | 4 ++++ input.h | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/basic.c b/basic.c index 430f887..6a76ebd 100644 --- a/basic.c +++ b/basic.c @@ -90,25 +90,26 @@ int gotoeol(int f, int n) * * @n: The specified line position at the current buffer. */ -int gotoline(int f, int n) -{ - int status ; - char arg[ NSTRING] ; /* Buffer to hold argument. */ - +int gotoline( int f, int n) { /* Get an argument if one doesnt exist. */ if( f == FALSE) { - status = mlreply( "Line to GOTO: ", arg, sizeof arg) ; + int status ; + char *arg ; /* Buffer to hold argument. */ + + status = newmlarg( &arg, "Line to GOTO: ", 0) ; if( status != TRUE) { mloutstr( "(Aborted)") ; return status ; } n = atoi( arg) ; + free( arg) ; } - /* Handle the case where the user may be passed something like this: - * em filename + - * In this case we just go to the end of the buffer. - */ + + /* Handle the case where the user may be passed something like this: + * em filename + + * In this case we just go to the end of the buffer. + */ if (n == 0) return gotoeob(f, n); diff --git a/input.c b/input.c index 3de8278..9b8b13f 100644 --- a/input.c +++ b/input.c @@ -146,6 +146,10 @@ int mlreply( const char *prompt, char *buf, int nbuf) { return nextarg( prompt, buf, nbuf, nlc) ; } +int newmlarg( char **outbufref, const char *prompt, int size) { + return newnextarg( outbufref, prompt, size, nlc) ; +} + int newmlargt( char **outbufref, const char *prompt, int size) { return newnextarg( outbufref, prompt, size, metac) ; } diff --git a/input.h b/input.h index 67714e6..b65f81c 100644 --- a/input.h +++ b/input.h @@ -24,6 +24,7 @@ extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; int mlreply( const char *prompt, char *buf, int nbuf) ; +int newmlarg( char **outbufref, const char *prompt, int size) ; int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; fn_t getname( void) ; From f13ae3957ccb178b054fcf663f2c6cbf83467c39 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 29 Sep 2015 11:43:30 +0800 Subject: [PATCH 16/27] Replace mlreply by newmlarg in eval, allowing user variable length to exceed 255 bytes. --- eval.c | 26 +++++++++++++------------- tststr.cmd | 4 ++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/eval.c b/eval.c index 79170bc..446e02a 100644 --- a/eval.c +++ b/eval.c @@ -769,7 +769,7 @@ int setvar(int f, int n) int status; /* status return */ struct variable_description vd; /* variable num/type */ char var[NVSIZE + 2]; /* name of variable to fetch %1234567890\0 */ - char value[ 2 * NSTRING] ; /* value to set variable to */ + char *value ; /* value to set variable to */ /* first get the variable to set.. */ if (clexec == FALSE) { @@ -792,11 +792,15 @@ int setvar(int f, int n) /* get the value for that variable */ if( f == TRUE) { + value = malloc( NSTRING) ; + if( value == NULL) + return FALSE ; + /* a bit overcautious here in using strncpy */ - strncpy( value, i_to_a( n), sizeof value - 1) ; - value[ sizeof value - 1] = '\0' ; + strncpy( value, i_to_a( n), NSTRING - 1) ; + value[ NSTRING - 1] = '\0' ; } else { - status = mlreply( "Value: ", value, sizeof value); + status = newmlarg( &value, "Value: ", 0) ; if (status != TRUE) return status; } @@ -814,6 +818,7 @@ int setvar(int f, int n) #endif /* and return it */ + free( value) ; return status; } @@ -1430,20 +1435,15 @@ int clrmes( int f, int n) { */ int writemsg( int f, int n) { int status ; - int size ; char *buf ; /* buffer to receive message into */ - size = term.t_ncol + 1 ; - buf = malloc( size) ; - if( buf == NULL) - return FALSE ; - - status = mlreply( "Message to write: ", buf, size) ; - if( status == TRUE) + status = newmlarg( &buf, "Message to write: ", 0) ; + if( status == TRUE) { /* write the message out */ mlforce( buf) ; + free( buf) ; + } - free( buf) ; return status ; } diff --git a/tststr.cmd b/tststr.cmd index 1463032..00d9650 100644 --- a/tststr.cmd +++ b/tststr.cmd @@ -10,6 +10,7 @@ insert-string %mypath newline insert-string &cat "Length of $PATH: " &len $PATH newline +insert-string &cat "Length of %mypath: " &cat &len %mypath ~n ; Insert string with escaped characters insert-string "hello, world~n" newline @@ -35,6 +36,9 @@ set %expect &len %nam newline set %nam &cat %nam %nam set %expect &tim %expect 2 + !if ¬ &les %expect 1024 + !break + !endif !endwhile insert-string %nam newline From d3b02af275b18a5c3401e8234ef3ec01a89645b2 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 29 Sep 2015 15:06:08 +0800 Subject: [PATCH 17/27] Missing random.c from commit 198980b, review of newmlargt interface. --- random.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/random.c b/random.c index 742c465..edd480e 100644 --- a/random.c +++ b/random.c @@ -1185,18 +1185,11 @@ int fmatch(int ch) } static int iovstring( int f, int n, const char *prompt, int (*fun)( char *)) { - newarg_t *argp ; int status ; /* status return code */ char *tstring ; /* string to add */ /* ask for string to insert */ - argp = newmlargt( prompt, 0) ; /* grab as big a token as screen allow */ - if( argp == NULL) - return FALSE ; - - status = argp->status ; - tstring = argp->buf ; - free( argp) ; + status = newmlargt( &tstring, prompt, 0) ; /* grab as big a token as screen allow */ if( tstring == NULL) return status ; From 06b0d3f00dc00ec6b1102a474ca6de10e6ff2732 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 1 Oct 2015 07:26:15 +0800 Subject: [PATCH 18/27] Switch back from termio to posix for Cygwin to be better aligned with Linux. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 4e536cd..21a7489 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ ifeq ($(uname_S),Darwin) DEFINES=-DAUTOCONF -DPOSIX -DSYSV -D_DARWIN_C_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_XOPEN_SOURCE=600 endif ifeq ($(uname_S),CYGWIN) - DEFINES=-DAUTOCONF -DCYGWIN -DSYSV -DPROGRAM=$(PROGRAM) + DEFINES=-DAUTOCONF -DCYGWIN -DPOSIX -DSYSV -DPROGRAM=$(PROGRAM) LIBS=-lcurses endif ifeq ($(uname_S),MINGW32) @@ -176,7 +176,7 @@ names.o: names.c names.h basic.h bind.h bindable.h buffer.h crypt.h \ line.h retcode.h utf8.h display.h estruct.h eval.h exec.h file.h \ isearch.h region.h random.h search.h spawn.h window.h defines.h word.h pklock.o: pklock.c estruct.h pklock.h -posix.o: posix.c +posix.o: posix.c termio.h estruct.h retcode.h utf8.h random.o: random.c random.h basic.h buffer.h crypt.h line.h retcode.h \ utf8.h display.h estruct.h execute.h input.h bind.h search.h terminal.h \ defines.h window.h @@ -190,7 +190,7 @@ spawn.o: spawn.c spawn.h defines.h buffer.h crypt.h line.h retcode.h \ terminal.h window.h tcap.o: tcap.c terminal.h defines.h retcode.h display.h estruct.h \ termio.h -termio.o: termio.c termio.h estruct.h retcode.h utf8.h +termio.o: termio.c utf8.o: utf8.c utf8.h window.o: window.c window.h defines.h buffer.h crypt.h line.h retcode.h \ utf8.h basic.h display.h estruct.h execute.h terminal.h wrapper.h From 9682cdb2d24d3a13c0dad3d666616e8b9f35ec56 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 1 Oct 2015 07:38:41 +0800 Subject: [PATCH 19/27] Maps M-S to hunt-forward to match DOS Alt-S behavior. --- ebind.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ebind.c b/ebind.c index 90b9cc4..7ddf07f 100644 --- a/ebind.c +++ b/ebind.c @@ -301,8 +301,8 @@ struct key_tab keytab[NBINDS] = { {META | 'R', sreplace} , #if PKCODE - {META | 'S', forwsearch} - , /* alternative P.K. */ + {META | 'S', forwhunt} + , #else #if BSD {META | 'S', bktoshell} From 575659b1c127036b3bb2aaff18a2498f0a94c674 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 1 Oct 2015 08:03:14 +0800 Subject: [PATCH 20/27] apropos based on newmlarg (replacement of mlreply). --- bind.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/bind.c b/bind.c index 342a1ea..60a552c 100644 --- a/bind.c +++ b/bind.c @@ -274,23 +274,24 @@ static int unbindchar( unsigned c) { * bring up a fake buffer and list the key bindings * into it with view mode */ -int desbind(int f, int n) +int desbind( int f, int n) { #if APROP -{ - buildlist( "") ; - return TRUE; + return buildlist( "") ; } -int apro(int f, int n) -{ /* Apropos (List functions that match a substring) */ - char mstring[NSTRING]; /* string to match cmd names to */ - int status; /* status return */ +/* Apropos (List functions that match a substring) */ +int apro( int f, int n) { + char *mstring ; /* string to match cmd names to */ + int status ; /* status return */ - status = mlreply("Apropos string: ", mstring, NSTRING - 1); - if (status == ABORT) - return status; + status = newmlarg( &mstring, "Apropos string: ", 0) ; + if( status == TRUE) { + status = buildlist( mstring) ; + free( mstring) ; + } else if( status == FALSE) + status = buildlist( "") ; /* build list of all commands */ - return buildlist( mstring) ; + return status ; } /* @@ -298,9 +299,8 @@ int apro(int f, int n) * * char *mstring; match string if a partial list, "" matches all */ -static int buildlist( char *mstring) +static int buildlist( char *mstring) { #endif -{ struct window *wp; /* scanning pointer to windows */ struct key_tab *ktp; /* pointer into the command table */ struct name_bind *nptr; /* pointer into the name binding table */ From db30d6d734f17d39851cea3a2cc55f931370a75c Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 1 Oct 2015 08:19:39 +0800 Subject: [PATCH 21/27] Mode commands based on newmlarg (replacement of mlreply). --- random.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/random.c b/random.c index edd480e..bb667a7 100644 --- a/random.c +++ b/random.c @@ -944,7 +944,7 @@ static int adjustmode( int kind, int global) { unsigned i ; /* loop index */ int status; /* error return on input */ char prompt[50]; /* string to prompt user with */ - char cbuf[ NSTRING] ; /* buffer to recieve mode name into */ + char *cbuf ; /* buffer to recieve mode name into */ /* build the proper prompt string */ if (global) @@ -959,7 +959,7 @@ static int adjustmode( int kind, int global) { /* prompt the user and get an answer */ - status = mlreply( prompt, cbuf, sizeof cbuf - 1) ; + status = newmlarg( &cbuf, prompt, 0) ; if (status != TRUE) return status; @@ -987,6 +987,7 @@ static int adjustmode( int kind, int global) { curwp->w_flag |= WFCOLR; #endif mlerase(); + free( cbuf) ; return TRUE; } } @@ -1009,11 +1010,13 @@ static int adjustmode( int kind, int global) { if (global == 0) upmode(); mlerase(); /* erase the junk */ + free( cbuf) ; return TRUE; } } mlwrite("No such mode!"); + free( cbuf) ; return FALSE; } From b86ceeaf5e1e8490d22b027d4cd99c5e9fbb48f8 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Thu, 1 Oct 2015 09:11:54 +0800 Subject: [PATCH 22/27] Buffer commands based on newmlarg (replacement of mlreply). --- buffer.c | 102 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/buffer.c b/buffer.c index ed232f8..9b8a892 100644 --- a/buffer.c +++ b/buffer.c @@ -55,17 +55,24 @@ static void l_to_a( char *buf, int width, long num) ; * if the use count is 0. Otherwise, they come * from some other window. */ -int usebuffer(int f, int n) -{ - struct buffer *bp; - int s; - bname_t bufn ; +int usebuffer( int f, int n) { + struct buffer *bp ; + int status ; + char *bufn ; - if ((s = mlreply("Use buffer: ", bufn, sizeof bufn)) != TRUE) - return s; - if ((bp = bfind(bufn, TRUE, 0)) == NULL) - return FALSE; - return swbuffer(bp); +/* Get buffer name */ + status = newmlarg( &bufn, "Use buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; + +/* Find buffer in list */ + bp = bfind( bufn, TRUE, 0) ; + free( bufn) ; + if( bp == NULL) + return FALSE ; + +/* Switch to buffer */ + return swbuffer( bp) ; } /* @@ -164,19 +171,26 @@ int swbuffer(struct buffer *bp) * if the buffer has been changed). Then free the header * line and the buffer header. Bound to "C-X K". */ -int killbuffer(int f, int n) -{ - struct buffer *bp; - int s; - bname_t bufn ; +int killbuffer( int f, int n) { + struct buffer *bp ; + int status ; + char *bufn ; - if ((s = mlreply("Kill buffer: ", bufn, sizeof bufn)) != TRUE) - return s; - if ((bp = bfind(bufn, FALSE, 0)) == NULL) /* Easy if unknown. */ - return TRUE; - if (bp->b_flag & BFINVS) /* Deal with special buffers */ - return TRUE; /* by doing nothing. */ - return zotbuf(bp); +/* Get buffer name */ + status = newmlarg( &bufn, "Kill buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; + +/* Find buffer in list */ + bp = bfind( bufn, FALSE, 0) ; + free( bufn) ; + if( bp == NULL) /* Easy if unknown. */ + return TRUE ; + + if( bp->b_flag & BFINVS) /* Deal with special buffers */ + return TRUE ; /* by doing nothing. */ + + return zotbuf( bp) ; } /* @@ -215,31 +229,37 @@ int zotbuf(struct buffer *bp) * * int f, n; default Flag & Numeric arg */ -int namebuffer(int f, int n) -{ - struct buffer *bp; /* pointer to scan through all buffers */ - bname_t bufn ; /* buffer to hold buffer name */ +int namebuffer( int f, int n) { + struct buffer *bp ; /* pointer to scan through all buffers */ + int status ; + char *bufn ; /* buffer to hold buffer name */ - /* prompt for and get the new buffer name */ - ask:if (mlreply("Change buffer name to: ", bufn, sizeof bufn) != - TRUE) - return FALSE; +/* prompt for and get the new buffer name */ +ask: + status = newmlarg( &bufn, "Change buffer name to: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; - /* and check for duplicates */ - bp = bheadp; - while (bp != NULL) { - if (bp != curbp) { - /* if the names the same */ - if (strcmp(bufn, bp->b_bname) == 0) - goto ask; /* try again */ +/* and check for duplicates */ + bp = bheadp ; + while( bp != NULL) { + if( bp != curbp) { + /* retry if the names are the same */ + if( strcmp( bufn, bp->b_bname) == 0) { + free( bufn) ; + goto ask ; /* try again */ + } } - bp = bp->b_bufp; /* onward */ + + bp = bp->b_bufp ; /* onward */ } - strcpy(curbp->b_bname, bufn); /* copy buffer name to structure */ - curwp->w_flag |= WFMODE; /* make mode line replot */ + strcpy( curbp->b_bname, bufn) ; /* copy buffer name to structure */ + free( bufn) ; + + curwp->w_flag |= WFMODE ; /* make mode line replot */ mloutstr( "") ; /* erase message line */ - return TRUE; + return TRUE ; } /* From b59a47bb3a3a2f8cb8721a010692745649d7fe9a Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 5 Oct 2015 11:34:33 +0800 Subject: [PATCH 23/27] File commands based on newmlarg (replacement of mlreply). --- file.c | 257 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 150 insertions(+), 107 deletions(-) diff --git a/file.c b/file.c index ed868e2..1bf8757 100644 --- a/file.c +++ b/file.c @@ -12,6 +12,7 @@ */ #include +#include #include #include @@ -73,16 +74,20 @@ boolean resterr( void) { * "read a file into the current buffer" code. * Bound to "C-X C-R". */ -int fileread(int f, int n) -{ - int s; - fname_t fname ; +int fileread( int f, int n) { + int status ; + char *fname ; - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if ((s = mlreply("Read file: ", fname, sizeof fname)) != TRUE) - return s; - return readin(fname, TRUE); + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + status = newmlarg( &fname, "Read file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = readin( fname, TRUE) ; + free( fname) ; + } + + return status ; } /* @@ -92,20 +97,26 @@ int fileread(int f, int n) * "insert a file into the current buffer" code. * Bound to "C-X C-I". */ -int insfile(int f, int n) -{ - int s; - fname_t fname ; +int insfile( int f, int n) { + int status ; + char *fname ; - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ - if ((s = mlreply("Insert file: ", fname, sizeof fname)) != TRUE) - return s; - if ((s = ifile(fname)) != TRUE) - return s; - return reposition(TRUE, -1); + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + if( curbp->b_mode & MDVIEW) /* don't allow this command if */ + return rdonly() ; /* we are in read only mode */ + + status = newmlarg( &fname, "Insert file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = ifile( fname) ; + free( fname) ; + } + + if( status != TRUE) + return status ; + + return reposition( TRUE, -1) ; } /* @@ -117,40 +128,49 @@ int insfile(int f, int n) * text, and switch to the new buffer. * Bound to C-X C-F. */ -int filefind(int f, int n) -{ - fname_t fname ; /* file user wishes to find */ - int s; /* status return */ +int filefind( int f, int n) { + char *fname ; /* file user wishes to find */ + int status ; /* status return */ - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if ((s = mlreply("Find file: ", fname, sizeof fname)) != TRUE) - return s; - return getfile(fname, TRUE); + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + status = newmlarg( &fname, "Find file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = getfile( fname, TRUE) ; + free( fname) ; + } + + return status ; } -int viewfile(int f, int n) -{ /* visit a file in VIEW mode */ - fname_t fname ; /* file user wishes to find */ - int s; /* status return */ - struct window *wp; /* scan for windows that need updating */ +int viewfile( int f, int n) { /* visit a file in VIEW mode */ + char *fname ; /* file user wishes to find */ + int status ; /* status return */ - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if ((s = mlreply("View file: ", fname, sizeof fname)) != TRUE) - return s; - s = getfile(fname, FALSE); - if (s) { /* if we succeed, put it in view mode */ - curwp->w_bufp->b_mode |= MDVIEW; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + status = newmlarg( &fname, "View file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = getfile(fname, FALSE) ; + free( fname) ; + } + + if( status == TRUE) { /* if we succeed, put it in view mode */ + struct window *wp ; /* scan for windows that need updating */ + + curwp->w_bufp->b_mode |= MDVIEW ; /* scan through and update mode lines of all windows */ - wp = wheadp; - while (wp != NULL) { - wp->w_flag |= WFMODE; - wp = wp->w_wndp; + wp = wheadp ; + while( wp != NULL) { + wp->w_flag |= WFMODE ; + wp = wp->w_wndp ; } } - return s; + + return status ; } #if CRYPT @@ -165,28 +185,29 @@ void cryptbufferkey( struct buffer *bp) { * int f; default flag * int n; numeric argument */ -int set_encryption_key(int f, int n) -{ - int status; /* return status */ - int odisinp; /* original vlaue of disinp */ - ekey_t key ; /* new encryption string */ +int set_encryption_key( int f, int n) { + int status ; /* return status */ + int odisinp ; /* original value of disinp */ + char *key ; /* new encryption string */ /* turn command input echo off */ - odisinp = disinp; - disinp = FALSE; + odisinp = disinp ; + disinp = FALSE ; /* get the string to use as an encrytion string */ - status = mlreply("Encryption String: ", key, sizeof key - 1); - disinp = odisinp; - if (status != TRUE) - return status; + status = newmlarg( &key, "Encryption String: ", sizeof( ekey_t)) ; + disinp = odisinp ; + if( status != TRUE) + return status ; /* save it off and encrypt it*/ - strcpy(curbp->b_key, key); + strncpy( curbp->b_key, key, sizeof( ekey_t) - 1) ; + curbp->b_key[ sizeof( ekey_t) - 1] = '\0' ; + free( key) ; cryptbufferkey( curbp) ; mloutstr( "") ; /* clear the message line */ - return TRUE; + return TRUE ; } static int resetkey(void) @@ -253,15 +274,22 @@ int getfile( const char *fname, boolean lockfl) } makename(bname, fname); /* New buffer name. */ while ((bp = bfind(bname, FALSE, 0)) != NULL) { + char *new_bname ; + /* old buffer name conflict code */ - s = mlreply("Buffer name: ", bname, sizeof bname); - if (s == ABORT) /* ^G to just quit */ - return s; - if (s == FALSE) { /* CR to clobber it */ - makename(bname, fname); - break; + s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ; + if( s == ABORT) /* ^G to just quit */ + return s ; + else if (s == FALSE) { /* CR to clobber it */ + makename( bname, fname) ; + break ; + } else { /* TRUE */ + strncpy( bname, new_bname, sizeof bname - 1) ; + bname[ sizeof bname - 1] = '\0' ; + free( new_bname) ; } } + if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) { mloutstr( "Cannot create buffer") ; return FALSE; @@ -495,27 +523,35 @@ void unqname(char *name) * is more compatable with Gosling EMACS than * with ITS EMACS. Bound to "C-X C-W". */ -int filewrite(int f, int n) -{ - struct window *wp; - int s; - fname_t fname ; +int filewrite( int f, int n) { + int status ; + char *fname ; - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if ((s = mlreply("Write file: ", fname, sizeof fname)) != TRUE) - return s; - if ((s = writeout(fname)) == TRUE) { - strcpy(curbp->b_fname, fname); - curbp->b_flag &= ~BFCHG; - wp = wheadp; /* Update mode lines. */ - while (wp != NULL) { - if (wp->w_bufp == curbp) - wp->w_flag |= WFMODE; - wp = wp->w_wndp; - } - } - return s; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + status = newmlarg( &fname, "Write file: ", sizeof( fname_t)) ; + if( status == TRUE) { + status = writeout( fname) ; + if( status == TRUE) { + struct window *wp ; + + strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ; + curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ; + curbp->b_flag &= ~BFCHG ; + wp = wheadp ; /* Update mode lines. */ + while( wp != NULL) { + if( wp->w_bufp == curbp) + wp->w_flag |= WFMODE ; + + wp = wp->w_wndp ; + } + } + + free( fname) ; + } + + return status ; } /* @@ -622,28 +658,35 @@ int writeout( const char *fn) * as needing an update. You can type a blank line at the * prompt if you wish. */ -int filename(int f, int n) -{ - struct window *wp; - int s; - fname_t fname ; +int filename( int f, int n) { + struct window *wp ; + int status ; + char *fname ; - if (restflag) /* don't allow this command if restricted */ - return resterr(); - if ((s = mlreply("Name: ", fname, sizeof fname)) == ABORT) - return s; - if (s == FALSE) - strcpy(curbp->b_fname, ""); - else - strcpy(curbp->b_fname, fname); - wp = wheadp; /* Update mode lines. */ - while (wp != NULL) { - if (wp->w_bufp == curbp) - wp->w_flag |= WFMODE; - wp = wp->w_wndp; + if( restflag) /* don't allow this command if restricted */ + return resterr() ; + + status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; + if( status == ABORT) + return status ; + else if( status == FALSE) + curbp->b_fname[ 0] = '\0' ; + else { /* TRUE */ + strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ; + curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ; + free( fname) ; } - curbp->b_mode &= ~MDVIEW; /* no longer read only mode */ - return TRUE; + + wp = wheadp ; /* Update mode lines. */ + while( wp != NULL) { + if( wp->w_bufp == curbp) + wp->w_flag |= WFMODE ; + + wp = wp->w_wndp ; + } + + curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */ + return TRUE ; } /* From bcba236265f6cf0a2eb435891a63056fa2099a0d Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 5 Oct 2015 13:27:45 +0800 Subject: [PATCH 24/27] Exec commands based on newmlarg (replacement of mlreply). --- exec.c | 162 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 85 insertions(+), 77 deletions(-) diff --git a/exec.c b/exec.c index 0262f08..af157ab 100644 --- a/exec.c +++ b/exec.c @@ -120,19 +120,20 @@ static int docmd( char *cline) ; * * int f, n; default Flag and Numeric argument */ -int execcmd(int f, int n) -{ - int status; /* status return */ - char cmdstr[NSTRING]; /* string holding command to execute */ +int execcmd( int f, int n) { + int status ; /* status return */ + char *cmdstr ; /* string holding command to execute */ /* get the line wanted */ - if ((status = mlreply(": ", cmdstr, NSTRING)) != TRUE) - return status; + status = newmlarg( &cmdstr, ": ", 0) ; + if( status != TRUE) + return status ; - execlevel = 0; + execlevel = 0 ; while( status == TRUE && n-- > 0) status = docmd( cmdstr) ; + free( cmdstr) ; return status ; } @@ -439,38 +440,42 @@ int storemac(int f, int n) * 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 */ +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); + if( f == TRUE) + return storemac( f, n) ; /* get the name of the procedure */ - if ((status = - mlreply("Procedure name: ", &bname[1], sizeof bname - 2)) != TRUE) - return status; + status = newmlarg( &name, "Procedure name: ", sizeof bname - 2) ; + if( status != TRUE) + return status ; /* construct the macro buffer name */ - bname[0] = '*'; - strcat(bname, "*"); + bname[ 0] = '*'; + strncpy( &bname[ 1], name, sizeof bname - 3) ; + bname[ sizeof bname - 2] = '\0' ; + strcat( bname, "*") ; + free( name) ; /* set up the new macro buffer */ - if ((bp = bfind(bname, TRUE, BFINVS)) == NULL) { - mlwrite("Can not create macro"); - return FALSE; + bp = bfind( bname, TRUE, BFINVS) ; + if( bp == NULL) { + mlwrite( "Can not create macro") ; + return FALSE ; } /* and make sure it is empty */ - bclear(bp); + bclear( bp) ; /* and set the macro store pointers to it */ - mstore = TRUE; - bstore = bp; - return TRUE; + mstore = TRUE ; + bstore = bp ; + return TRUE ; } /* @@ -479,32 +484,36 @@ int storeproc(int f, int n) * * int f, n; default flag and numeric arg */ -int execproc(int f, int n) -{ - struct buffer *bp; /* ptr to buffer to execute */ - int status; /* status return */ - char bufn[NBUFN + 2]; /* name of buffer to execute */ +int execproc( int f, int n) { + struct buffer *bp ; /* ptr to buffer to execute */ + int status ; /* status return */ + bname_t bufn ; /* name of buffer to execute */ + char *name ; /* find out what buffer the user wants to execute */ - if ((status = - mlreply("Execute procedure: ", &bufn[1], NBUFN)) != TRUE) - return status; + status = newmlarg( &name, "Execute procedure: ", sizeof bufn - 2) ; + if( status != TRUE) + return status ; /* construct the buffer name */ - bufn[0] = '*'; - strcat(bufn, "*"); + bufn[ 0] = '*' ; + strncpy( &bufn[ 1], name, sizeof bufn - 3) ; + bufn[ sizeof bufn - 2] = '\0' ; + strcat( bufn, "*") ; + free( name) ; /* find the pointer to that buffer */ - if ((bp = bfind(bufn, FALSE, 0)) == NULL) { - mlwrite("No such procedure"); - return FALSE; + bp = bfind( bufn, FALSE, 0) ; + if( bp == NULL) { + mlwrite( "No such procedure") ; + return FALSE ; } /* and now execute it as asked */ - while (n-- > 0) - if ((status = dobuf(bp)) != TRUE) - return status; - return TRUE; + while( status == TRUE && n-- > 0) + status = dobuf( bp) ; + + return status ; } #endif @@ -514,27 +523,29 @@ int execproc(int f, int n) * * int f, n; default flag and numeric arg */ -int execbuf(int f, int n) -{ - struct buffer *bp; /* ptr to buffer to execute */ - int status; /* status return */ - bname_t bufn ; /* name of buffer to execute */ +int execbuf( int f, int n) { + struct buffer *bp ; /* ptr to buffer to execute */ + int status ; /* status return */ + char *bufn ; /* name of buffer to execute */ /* find out what buffer the user wants to execute */ - if ((status = mlreply("Execute buffer: ", bufn, sizeof bufn)) != TRUE) - return status; + status = newmlarg( &bufn, "Execute buffer: ", sizeof( bname_t)) ; + if( status != TRUE) + return status ; /* find the pointer to that buffer */ - if ((bp = bfind(bufn, FALSE, 0)) == NULL) { - mlwrite("No such buffer"); - return FALSE; + bp = bfind( bufn, FALSE, 0) ; + free( bufn) ; + if( bp == NULL) { + mlwrite( "No such buffer") ; + return FALSE ; } /* and now execute it as asked */ - while (n-- > 0) - if ((status = dobuf(bp)) != TRUE) - return status; - return TRUE; + while( status == TRUE && n-- > 0) + status = dobuf( bp) ; + + return status ; } /* @@ -949,31 +960,28 @@ static void freewhile(struct while_block *wp) * * int f, n; default flag and numeric arg to pass on to file */ -int execfile(int f, int n) -{ - int status; /* return status of name query */ - char fname[NSTRING]; /* name of file to execute */ - char *fspec; /* full file spec */ +int execfile( int f, int n) { + int status ; /* return status of name query */ + char *fname ; /* name of file to execute */ + char *fspec ; /* full file spec */ - if ((status = - mlreply("File to execute: ", fname, NSTRING - 1)) != TRUE) - return status; + status = newmlarg( &fname, "File to execute: ", 0) ; + if( status != TRUE) + return status ; -#if 1 - /* look up the path for the file */ - fspec = flook(fname, FALSE); /* used to by TRUE, P.K. */ +/* look up the path for the file */ + fspec = flook( fname, FALSE) ; /* used to be TRUE, P.K. */ + free( fname) ; - /* if it isn't around */ - if (fspec == NULL) - return FALSE; +/* if it isn't around */ + if( fspec == NULL) + return FALSE ; -#endif - /* otherwise, execute it */ - while (n-- > 0) - if ((status = dofile(fspec)) != TRUE) - return status; +/* otherwise, execute it */ + while( status == TRUE && n-- > 0) + status = dofile( fspec) ; - return TRUE; + return status ; } /* From 15453a9d5202d610fd8fa9f4808c5b2bc9046220 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 5 Oct 2015 14:06:53 +0800 Subject: [PATCH 25/27] Spawn commands based on newmlarg (replacement of mlreply). --- spawn.c | 238 ++++++++++++++++++-------------------------------------- 1 file changed, 77 insertions(+), 161 deletions(-) diff --git a/spawn.c b/spawn.c index f0207d7..0e3a1ff 100644 --- a/spawn.c +++ b/spawn.c @@ -145,51 +145,24 @@ void rtfrmshell(void) * character to be typed, then mark the screen as garbage so a full repaint is * done. Bound to "C-X !". */ -int spawn(int f, int n) -{ - int s; - char line[NLINE]; +int spawn( int f, int n) { + int s ; + char *line ; /* don't allow this command if restricted */ - if (restflag) + if( restflag) return resterr(); -#if VMS - if ((s = mlreply("!", line, NLINE)) != TRUE) - return s; - movecursor(term.t_nrow, 0); - TTflush(); - s = sys(line); /* Run the command. */ - if (clexec == FALSE) { - mlwrite("\r\n\n(End)"); /* Pause. */ - TTflush(); - tgetc(); - } - sgarbf = TRUE; - return s; -#endif -#if MSDOS - if ((s = mlreply("!", line, NLINE)) != TRUE) - return s; - movecursor(term.t_nrow, 0); - TTkclose(); - shellprog(line); - TTkopen(); - /* if we are interactive, pause here */ - if (clexec == FALSE) { - mlwrite("\r\n(End)"); - tgetc(); - } - sgarbf = TRUE; - return TRUE; -#endif #if V7 | USG | BSD - if ((s = mlreply("!", line, NLINE)) != TRUE) - return s; + s = newmlarg( &line, "!", 0) ; + if( s != TRUE) + return s ; + TTflush(); TTclose(); /* stty to old modes */ TTkclose(); ue_system( line) ; + free( line) ; fflush(stdout); /* to be sure P.K. */ TTopen(); @@ -211,51 +184,25 @@ int spawn(int f, int n) * done. Bound to "C-X $". */ -int execprg(int f, int n) -{ - int s; - char line[NLINE]; +int execprg( int f, int n) { + int s ; + char *line ; /* don't allow this command if restricted */ - if (restflag) - return resterr(); - -#if VMS - if ((s = mlreply("!", line, NLINE)) != TRUE) - return s; - TTflush(); - s = sys(line); /* Run the command. */ - mlwrite("\r\n\n(End)"); /* Pause. */ - TTflush(); - tgetc(); - sgarbf = TRUE; - return s; -#endif - -#if MSDOS - if ((s = mlreply("$", line, NLINE)) != TRUE) - return s; - movecursor(term.t_nrow, 0); - TTkclose(); - execprog(line); - TTkopen(); - /* if we are interactive, pause here */ - if (clexec == FALSE) { - mlwrite("\r\n(End)"); - tgetc(); - } - sgarbf = TRUE; - return TRUE; -#endif + if( restflag) + return resterr() ; #if V7 | USG | BSD - if ((s = mlreply("!", line, NLINE)) != TRUE) - return s; + s = newmlarg( &line, "$", 0) ; + if( s != TRUE) + return s ; + TTputc('\n'); /* Already have '\r' */ TTflush(); TTclose(); /* stty to old modes */ TTkclose(); ue_system( line) ; + free( line) ; fflush(stdout); /* to be sure P.K. */ TTopen(); mlwrite( "(End)") ; /* Pause. */ @@ -270,48 +217,32 @@ int execprg(int f, int n) * Pipe a one line command into a window * Bound to ^X @ */ -int pipecmd(int f, int n) -{ - int s; /* return status from CLI */ - struct window *wp; /* pointer to new window */ - struct buffer *bp; /* pointer to buffer to zot */ - char line[NLINE]; /* command line send to shell */ - static char bname[] = "command"; - - static char filnam[NSTRING] = "command"; - -#if MSDOS - char *tmp; - FILE *fp; - int len; -#endif +int pipecmd( int f, int n) { + int s ; /* return status from CLI */ + struct window *wp ; /* pointer to new window */ + struct buffer *bp ; /* pointer to buffer to zot */ + char *mlarg ; + char *line ; /* command line send to shell */ + static char bname[] = "command" ; + static char filnam[ NSTRING] = "command" ; /* don't allow this command if restricted */ - if (restflag) - return resterr(); - -#if MSDOS - if ((tmp = getenv("TMP")) == NULL - && (tmp = getenv("TEMP")) == NULL) - strcpy(filnam, "command"); - else { - strcpy(filnam, tmp); - len = strlen(tmp); - if (len <= 0 || filnam[len - 1] != '\\' - && filnam[len - 1] != '/') - strcat(filnam, "\\"); - strcat(filnam, "command"); - } -#endif - -#if VMS - mlwrite("Not available under VMS"); - return FALSE; -#endif + if( restflag) + return resterr() ; /* get the command to pipe in */ - if ((s = mlreply("@", line, NLINE)) != TRUE) - return s; + s = newmlarg( &mlarg, "@", 0) ; + if( s != TRUE) + return s ; + + line = malloc( strlen( mlarg) + strlen( filnam) + 2) ; + if( line == NULL) { + free( mlarg) ; + return FALSE ; + } + + strcpy( line, mlarg) ; + free( mlarg) ; /* get rid of the command output buffer if it exists */ if ((bp = bfind(bname, FALSE, 0)) != FALSE) { @@ -332,33 +263,20 @@ int pipecmd(int f, int n) } wp = wp->w_wndp; } - if (zotbuf(bp) != TRUE) - return FALSE; + if( zotbuf( bp) != TRUE) { + free( line) ; + return FALSE ; + } } -#if MSDOS - strcat(line, " >>"); - strcat(line, filnam); - movecursor(term.t_nrow, 0); - TTkclose(); - shellprog(line); - TTkopen(); - sgarbf = TRUE; - if ((fp = fopen(filnam, "r")) == NULL) { - s = FALSE; - } else { - fclose(fp); - s = TRUE; - } -#endif - #if V7 | USG | BSD TTflush(); TTclose(); /* stty to old modes */ TTkclose(); - strcat(line, ">"); - strcat(line, filnam); + strcat( line, ">") ; + strcat( line, filnam) ; ue_system( line) ; + free( line) ; TTopen(); TTkopen(); TTflush(); @@ -394,32 +312,37 @@ int pipecmd(int f, int n) * filter a buffer through an external DOS program * Bound to ^X # */ -int filter_buffer(int f, int n) -{ - int s; /* return status from CLI */ - struct buffer *bp; /* pointer to buffer to zot */ - char line[NLINE]; /* command line send to shell */ +int filter_buffer( int f, int n) { + int s ; /* return status from CLI */ + struct buffer *bp ; /* pointer to buffer to zot */ + char *mlarg ; + char *line ; /* command line send to shell */ fname_t tmpnam ; /* place to store real file name */ - static char bname1[] = "fltinp"; + static char bname1[] = "fltinp" ; - static char filnam1[] = "fltinp"; - static char filnam2[] = "fltout"; + static char filnam1[] = "fltinp" ; + static char filnam2[] = "fltout" ; /* don't allow this command if restricted */ - if (restflag) - return resterr(); + if( restflag) + return resterr() ; - if (curbp->b_mode & MDVIEW) /* don't allow this command if */ - return rdonly(); /* we are in read only mode */ - -#if VMS - mlwrite("Not available under VMS"); - return FALSE; -#endif + if( curbp->b_mode & MDVIEW) /* don't allow this command if */ + return rdonly() ; /* we are in read only mode */ /* get the filter name and its args */ - if ((s = mlreply("#", line, NLINE)) != TRUE) - return s; + s = newmlarg( &mlarg, "#", 0) ; + if( s != TRUE) + return s ; + + line = malloc( strlen( mlarg) + 16 + 1) ; + if( line == NULL) { + free( mlarg) ; + return FALSE ; + } + + strcpy( line, mlarg) ; + free( mlarg) ; /* setup the proper file names */ bp = curbp; @@ -427,20 +350,12 @@ int filter_buffer(int f, int n) strcpy(bp->b_fname, bname1); /* set it to our new one */ /* write it out, checking for errors */ - if (writeout(filnam1) != TRUE) { + if( writeout( filnam1) != TRUE) { mlwrite( "(Cannot write filter file)") ; - strcpy(bp->b_fname, tmpnam); - return FALSE; + strcpy( bp->b_fname, tmpnam) ; + free( line) ; + return FALSE ; } -#if MSDOS - strcat(line, " fltout"); - movecursor(term.t_nrow - 1, 0); - TTkclose(); - shellprog(line); - TTkopen(); - sgarbf = TRUE; - s = TRUE; -#endif #if V7 | USG | BSD TTputc('\n'); /* Already have '\r' */ @@ -449,6 +364,7 @@ int filter_buffer(int f, int n) TTkclose(); strcat(line, " fltout"); ue_system( line) ; + free( line) ; TTopen(); TTkopen(); TTflush(); From 45f138ac07277e32b250af298d3aba3f2d94b9cd Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 5 Oct 2015 14:15:24 +0800 Subject: [PATCH 26/27] Remove mlreply after transition to replacement newmlarg. --- input.c | 24 ++++++------------------ input.h | 1 - 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/input.c b/input.c index 9b8b13f..35b2667 100644 --- a/input.c +++ b/input.c @@ -54,7 +54,7 @@ int abortc = CONTROL | 'G' ; /* current abort command char */ const int nlc = CONTROL | 'J' ; /* end of input char */ -static const int quotec = 0x11 ; /* quote char during mlreply() */ +static const int quotec = 0x11 ; /* quote char during getstring() */ static void outstring( char *s) ; @@ -86,22 +86,14 @@ int mlyesno( const char *prompt) } /* - * nextarg: + * newnextarg: * get the next argument * - * const char *prompt; prompt to use if we must be interactive - * char *buffer; buffer to put token into - * int size; size of the buffer - * int terminator; terminating char to be used on interactive fetch + * char **outbufref ; buffer to put token into + * const char *prompt ; prompt to use if we must be interactive + * int size ; size of the buffer + * int terminator ; terminating char to be used on interactive fetch */ -static int nextarg( const char *prompt, char *buf, int size, int terminator) { - /* if we are interactive, go get it! */ - if( clexec == FALSE) - return getstring( prompt, buf, size, terminator) ; - else - return gettokval( buf, size) ; -} - static int newnextarg( char **outbufref, const char *prompt, int size, int terminator) { int status ; @@ -142,10 +134,6 @@ static int newnextarg( char **outbufref, const char *prompt, int size, * return. Handle erase, kill, and abort keys. */ -int mlreply( const char *prompt, char *buf, int nbuf) { - return nextarg( prompt, buf, nbuf, nlc) ; -} - int newmlarg( char **outbufref, const char *prompt, int size) { return newnextarg( outbufref, prompt, size, nlc) ; } diff --git a/input.h b/input.h index b65f81c..3dd1f1a 100644 --- a/input.h +++ b/input.h @@ -23,7 +23,6 @@ extern const int nlc ; /* end of input char */ int mlyesno( const char *prompt) ; -int mlreply( const char *prompt, char *buf, int nbuf) ; int newmlarg( char **outbufref, const char *prompt, int size) ; int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; From 4918da601cef6b8c9674f19e1fe115109adbd790 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 5 Oct 2015 14:20:15 +0800 Subject: [PATCH 27/27] Increase version number before merge to master branch. --- version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.h b/version.h index f527976..6a5f989 100644 --- a/version.h +++ b/version.h @@ -12,6 +12,6 @@ # define PROGRAM_NAME_PFX "\xC2" # define PROGRAM_NAME_LONG "\xB5""EMACS" /* UTF-8 µEMACS */ -# define VERSION "4.2.0" +# define VERSION "4.2.1" #endif /* VERSION_H_ */