From 198980b81f1f5bce2eb62bcebad389a4b2487d51 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 29 Sep 2015 10:25:36 +0800 Subject: [PATCH] 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.