From 3197080cb12560069e34600b6765faacb5990927 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 2 Jun 2014 15:16:18 +0800 Subject: [PATCH] Introduce spat_t as search pattern type and reduce need for NPAT as pattern length constant. --- defines.h | 1 - exec.c | 4 ++-- input.c | 4 ++-- isearch.c | 9 +++++---- main.c | 4 ++-- random.c | 18 +++++++++--------- search.c | 8 ++++---- search.h | 9 ++++++--- 8 files changed, 30 insertions(+), 27 deletions(-) diff --git a/defines.h b/defines.h index f84bd94..b710333 100644 --- a/defines.h +++ b/defines.h @@ -8,7 +8,6 @@ #define ENVFUNC 1 #define NSTRING 128 /* # of bytes, string buffers */ -#define NPAT 128 /* # of bytes, pattern */ #define CONTROL 0x10000000 /* Control flag, or'ed in */ #define META 0x20000000 /* Meta flag, or'ed in */ diff --git a/exec.c b/exec.c index a6130e2..e32d895 100644 --- a/exec.c +++ b/exec.c @@ -72,7 +72,7 @@ static const char *dname[] = { "force" }; -static char golabel[ NPAT] = "" ; /* current line to go to */ +static char golabel[ NSTRING] = "" ; /* current line to go to */ static int execlevel = 0 ; /* execution IF level */ static struct buffer *bstore = NULL ; /* buffer to store macro text to */ static int mstore = FALSE ; /* storing text to macro flag */ @@ -798,7 +798,7 @@ static int dobuf(struct buffer *bp) /* grab label to jump to */ eline = - token(eline, golabel, NPAT); + token( eline, golabel, sizeof golabel) ; linlen = strlen(golabel); glp = hlp->l_fp; while (glp != hlp) { diff --git a/input.c b/input.c index bb98cc8..e599cde 100644 --- a/input.c +++ b/input.c @@ -61,7 +61,7 @@ static const int quotec = 0x11 ; /* quote char during mlreply() */ int mlyesno( const char *prompt) { char c; /* input character */ - char buf[NPAT]; /* prompt to user */ + char buf[ NSTRING] ; /* prompt to user */ for (;;) { /* build and prompt the user */ @@ -69,7 +69,7 @@ int mlyesno( const char *prompt) strcat(buf, " (y/n)? "); mlwrite(buf); - /* get the responce */ + /* get the response */ c = tgetc(); if (c == ectoc(abortc)) /* Bail out! */ diff --git a/isearch.c b/isearch.c index e14988f..258d8ce 100644 --- a/isearch.c +++ b/isearch.c @@ -188,7 +188,7 @@ static int isearch(int f, int n) int cpos; /* character number in search string */ int c; /* current input character */ int expc; /* function expanded input char */ - char pat_save[NPAT]; /* Saved copy of the old pattern str */ + spat_t pat_save ; /* Saved copy of the old pattern str */ struct line *curline; /* Current line on entry */ int curoff; /* Current offset on entry */ int init_direction; /* The initial search direction */ @@ -198,7 +198,7 @@ static int isearch(int f, int n) cmd_reexecute = -1; /* We're not re-executing (yet?) */ cmd_offset = 0; /* Start at the beginning of the buff */ cmd_buff[0] = '\0'; /* Init the command buffer */ - strncpy(pat_save, pat, NPAT); /* Save the old pattern string */ + strncpy( pat_save, pat, sizeof pat_save) ; /* Save the old pattern string */ curline = curwp->w_dotp; /* Save the current line pointer */ curoff = curwp->w_doto; /* Save the current offset */ init_direction = n; /* Save the initial search direction */ @@ -276,7 +276,7 @@ static int isearch(int f, int n) curwp->w_dotp = curline; /* Reset the line pointer */ curwp->w_doto = curoff; /* and the offset */ n = init_direction; /* Reset the search direction */ - strncpy(pat, pat_save, NPAT); /* Restore the old search str */ + strncpy( pat, pat_save, sizeof pat) ; /* Restore the old search str */ cmd_reexecute = 0; /* Start the whole mess over */ goto start_over; /* Let it take care of itself */ @@ -292,7 +292,8 @@ static int isearch(int f, int n) /* I guess we got something to search for, so search for it */ pat[cpos++] = c; /* put the char in the buffer */ - if (cpos >= NPAT) { /* too many chars in string? *//* Yup. Complain about it */ + if (cpos >= sizeof pat) { /* too many chars in string? */ + /* Yup. Complain about it */ mlwrite("? Search string too long"); return TRUE; /* Return an error */ } diff --git a/main.c b/main.c index 6c0ee7b..d7b8f37 100644 --- a/main.c +++ b/main.c @@ -245,8 +245,8 @@ int main(int argc, char **argv) case 's': /* -s for initial search string */ case 'S': searchflag = TRUE; - strncpy( pat, &argv[ carg][ 2], NPAT - 1) ; - pat[ NPAT -1] = 0 ; + strncpy( pat, &argv[ carg][ 2], sizeof pat - 1) ; + pat[ sizeof pat -1] = 0 ; break; case 'v': /* -v for View File */ case 'V': diff --git a/random.c b/random.c index da10dce..55827af 100644 --- a/random.c +++ b/random.c @@ -937,7 +937,7 @@ int adjustmode(int kind, int global) int i; /* loop index */ int status; /* error return on input */ char prompt[50]; /* string to prompt user with */ - char cbuf[NPAT]; /* buffer to recieve mode name into */ + char cbuf[ NSTRING] ; /* buffer to recieve mode name into */ /* build the proper prompt string */ if (global) @@ -952,7 +952,7 @@ int adjustmode(int kind, int global) /* prompt the user and get an answer */ - status = mlreply(prompt, cbuf, NPAT - 1); + status = mlreply( prompt, cbuf, sizeof cbuf - 1) ; if (status != TRUE) return status; @@ -1033,11 +1033,11 @@ int writemsg(int f, int n) char *sp; /* pointer into buf to expand %s */ char *np; /* ptr into nbuf */ int status; - char buf[NPAT]; /* buffer to recieve message into */ - char nbuf[NPAT * 2]; /* buffer to expand string into */ + char buf[ NSTRING] ; /* buffer to recieve message into */ + char nbuf[ NSTRING * 2] ; /* buffer to expand string into */ if ((status = - mlreply("Message to write: ", buf, NPAT - 1)) != TRUE) + mlreply("Message to write: ", buf, sizeof buf - 1)) != TRUE) return status; /* expand all '%' to "%%" so mlwrite won't expect arguments */ @@ -1231,11 +1231,11 @@ int fmatch(int ch) int istring(int f, int n) { int status; /* status return code */ - char tstring[NPAT + 1]; /* string to add */ + char tstring[ NSTRING + 1] ; /* string to add */ /* ask for string to insert */ status = - mlreplyt("String to insert: ", tstring, NPAT, metac); + mlreplyt("String to insert: ", tstring, NSTRING, metac) ; if (status != TRUE) return status; @@ -1259,11 +1259,11 @@ int istring(int f, int n) int ovstring(int f, int n) { int status; /* status return code */ - char tstring[NPAT + 1]; /* string to add */ + char tstring[ NSTRING + 1] ; /* string to add */ /* ask for string to insert */ status = - mlreplyt("String to overwrite: ", tstring, NPAT, metac); + mlreplyt( "String to overwrite: ", tstring, NSTRING, metac) ; if (status != TRUE) return status; diff --git a/search.c b/search.c index c32e56b..db2971a 100644 --- a/search.c +++ b/search.c @@ -87,9 +87,9 @@ char *patmatch = NULL ; static struct line *matchline = NULL; static int matchoff = 0; -char pat[ NPAT] ; /* Search pattern */ -char tap[ NPAT] ; /* Reversed pattern array. */ -char rpat[ NPAT] ; /* replacement pattern */ +spat_t pat ; /* Search pattern */ +spat_t tap ; /* Reversed pattern array. */ +spat_t rpat ; /* replacement pattern */ #if defined(MAGIC) @@ -815,7 +815,7 @@ static int replaces(int kind, int f, int n) int nlflag; /* last char of search string a ? */ int nlrepl; /* was a replace done on the last line? */ char c; /* input char for query */ - char tpat[NPAT]; /* temporary to hold search pattern */ + spat_t tpat ; /* temporary to hold search pattern */ struct line *origline; /* original "." position */ int origoff; /* and offset (for . query option) */ struct line *lastline; /* position of last replace and */ diff --git a/search.h b/search.h index 9b53f32..568d553 100644 --- a/search.h +++ b/search.h @@ -7,12 +7,15 @@ #define BELL 0x07 /* a bell character */ +typedef char spat_t[ 128] ; /* search pattern type */ +#define NPAT sizeof( spat_t) /* # of bytes, pattern */ + extern unsigned int matchlen ; extern char *patmatch ; -extern char pat[] ; /* Search pattern */ -extern char tap[] ; /* Reversed pattern array. */ -extern char rpat[] ; /* replacement pattern */ +extern spat_t pat ; /* Search pattern */ +extern spat_t tap ; /* Reversed pattern array. */ +extern spat_t rpat ; /* replacement pattern */ /* * PTBEG, PTEND, FORWARD, and REVERSE are all toggle-able values for