=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mg/search.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- src/usr.bin/mg/search.c 2000/02/25 19:08:50 1.1 +++ src/usr.bin/mg/search.c 2000/04/13 06:12:17 1.2 @@ -13,33 +13,33 @@ #include "macro.h" #endif -#define SRCH_BEGIN (0) /* Search sub-codes. */ +#define SRCH_BEGIN (0) /* Search sub-codes. */ #define SRCH_FORW (-1) #define SRCH_BACK (-2) #define SRCH_NOPR (-3) #define SRCH_ACCM (-4) #define SRCH_MARK (-5) -typedef struct { - int s_code; - LINE *s_dotp; - int s_doto; -} SRCHCOM; +typedef struct { + int s_code; + LINE *s_dotp; + int s_doto; +} SRCHCOM; -static SRCHCOM cmds[NSRCH]; -static int cip; +static SRCHCOM cmds[NSRCH]; +static int cip; -int srch_lastdir = SRCH_NOPR; /* Last search flags. */ +int srch_lastdir = SRCH_NOPR; /* Last search flags. */ -static VOID is_cpush(); -static VOID is_lpush(); -static VOID is_pop(); -static int is_peek(); -static VOID is_undo(); -static int is_find(); -static VOID is_prompt(); -static VOID is_dspl(); -static int eq(); +static VOID is_cpush(); +static VOID is_lpush(); +static VOID is_pop(); +static int is_peek(); +static VOID is_undo(); +static int is_find(); +static VOID is_prompt(); +static VOID is_dspl(); +static int eq(); /* * Search forward. @@ -48,12 +48,12 @@ * matched characters, and display does all the hard stuff. * If not found, it just prints a message. */ -/*ARGSUSED*/ +/* ARGSUSED */ forwsearch(f, n) { - register int s; + register int s; - if ((s=readpattern("Search")) != TRUE) + if ((s = readpattern("Search")) != TRUE) return s; if (forwsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); @@ -70,12 +70,12 @@ * pointing at the first character of the pattern [the last character that * was matched]. */ -/*ARGSUSED*/ +/* ARGSUSED */ backsearch(f, n) { - register int s; + register int s; - if ((s=readpattern("Search backward")) != TRUE) + if ((s = readpattern("Search backward")) != TRUE) return (s); if (backsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); @@ -91,7 +91,7 @@ * has been saved in "srch_lastdir", so you know which way * to go. */ -/*ARGSUSED*/ +/* ARGSUSED */ searchagain(f, n) { if (srch_lastdir == SRCH_FORW) { @@ -116,7 +116,7 @@ * Use incremental searching, initially in the forward direction. * isearch ignores any explicit arguments. */ -/*ARGSUSED*/ +/* ARGSUSED */ forwisearch(f, n) { return isearch(SRCH_FORW); @@ -126,7 +126,7 @@ * Use incremental searching, initially in the reverse direction. * isearch ignores any explicit arguments. */ -/*ARGSUSED*/ +/* ARGSUSED */ backisearch(f, n) { return isearch(SRCH_BACK); @@ -143,22 +143,23 @@ * other ^ exit search, don't set mark * else accumulate into search string */ -isearch(dir) { - register int c; - register LINE *clp; - register int cbo; - register int success; - int pptr; - char opat[NPAT]; - VOID ungetkey(); +isearch(dir) +{ + register int c; + register LINE *clp; + register int cbo; + register int success; + int pptr; + char opat[NPAT]; + VOID ungetkey(); #ifndef NO_MACRO - if(macrodef) { - ewprintf("Can't isearch in macro"); - return FALSE; + if (macrodef) { + ewprintf("Can't isearch in macro"); + return FALSE; } #endif - for (cip=0; cipw_marko = cbo; ewprintf("Mark set"); curwp->w_flag |= WFMOVE; - return TRUE; + return TRUE; } /* and continue */ case CCHR('I'): case CCHR('J'): - addchar: + addchar: if (pptr == -1) pptr = 0; if (pptr == 0) @@ -289,21 +293,24 @@ is_prompt(dir, FALSE, success); } } - /*NOTREACHED*/ + /* NOTREACHED */ } -static VOID -is_cpush(cmd) register int cmd; { +static VOID +is_cpush(cmd) + register int cmd; +{ if (++cip >= NSRCH) cip = 0; cmds[cip].s_code = cmd; } -static VOID -is_lpush() { - register int ctp; +static VOID +is_lpush() +{ + register int ctp; - ctp = cip+1; + ctp = cip + 1; if (ctp >= NSRCH) ctp = 0; cmds[ctp].s_code = SRCH_NOPR; @@ -311,27 +318,32 @@ cmds[ctp].s_dotp = curwp->w_dotp; } -static VOID -is_pop() { +static VOID +is_pop() +{ if (cmds[cip].s_code != SRCH_NOPR) { - curwp->w_doto = cmds[cip].s_doto; - curwp->w_dotp = cmds[cip].s_dotp; + curwp->w_doto = cmds[cip].s_doto; + curwp->w_dotp = cmds[cip].s_dotp; curwp->w_flag |= WFMOVE; cmds[cip].s_code = SRCH_NOPR; } if (--cip <= 0) - cip = NSRCH-1; + cip = NSRCH - 1; } static int -is_peek() { +is_peek() +{ return cmds[cip].s_code; } /* this used to always return TRUE (the return value was checked) */ -static VOID -is_undo(pptr, dir) register int *pptr; register int *dir; { - register int redo = FALSE ; +static VOID +is_undo(pptr, dir) + register int *pptr; + register int *dir; +{ + register int redo = FALSE; switch (cmds[cip].s_code) { case SRCH_BEGIN: case SRCH_NOPR: @@ -358,19 +370,22 @@ break; } is_pop(); - if (redo) is_undo(pptr, dir); + if (redo) + is_undo(pptr, dir); } static int -is_find(dir) register int dir; { - register int plen, odoto; - register LINE *odotp ; +is_find(dir) + register int dir; +{ + register int plen, odoto; + register LINE *odotp; odoto = curwp->w_doto; odotp = curwp->w_dotp; plen = strlen(pat); if (plen != 0) { - if (dir==SRCH_FORW) { + if (dir == SRCH_FORW) { (VOID) backchar(FFARG | FFRAND, plen); if (forwsrch() == FALSE) { curwp->w_doto = odoto; @@ -379,7 +394,7 @@ } return TRUE; } - if (dir==SRCH_BACK) { + if (dir == SRCH_BACK) { (VOID) forwchar(FFARG | FFRAND, plen); if (backsrch() == FALSE) { curwp->w_doto = odoto; @@ -402,8 +417,9 @@ * of the callers looked at the status, so I just * made the checking vanish. */ -static VOID -is_prompt(dir, flag, success) { +static VOID +is_prompt(dir, flag, success) +{ if (dir == SRCH_FORW) { if (success != FALSE) is_dspl("I-search", flag); @@ -414,7 +430,8 @@ is_dspl("I-search backward", flag); else is_dspl("Failing I-search backward", flag); - } else ewprintf("Broken call to is_prompt"); + } else + ewprintf("Broken call to is_prompt"); } /* @@ -422,8 +439,10 @@ * The "prompt" is just a string. The "flag" determines * whether pat should be printed. */ -static VOID -is_dspl(prompt, flag) char *prompt; { +static VOID +is_dspl(prompt, flag) + char *prompt; +{ if (flag != FALSE) ewprintf("%s: ", prompt); @@ -435,23 +454,23 @@ * Query Replace. * Replace strings selectively. Does a search and replace operation. */ -/*ARGSUSED*/ +/* ARGSUSED */ queryrepl(f, n) { - register int s; - register int rcnt = 0; /* Replacements made so far */ - register int plen; /* length of found string */ - char news[NPAT]; /* replacement string */ + register int s; + register int rcnt = 0; /* Replacements made so far */ + register int plen; /* length of found string */ + char news[NPAT]; /* replacement string */ #ifndef NO_MACRO - if(macrodef) { - ewprintf("Can't query replace in macro"); - return FALSE; + if (macrodef) { + ewprintf("Can't query replace in macro"); + return FALSE; } #endif - if ((s=readpattern("Query replace")) != TRUE) + if ((s = readpattern("Query replace")) != TRUE) return (s); - if ((s=ereply("Query replace %s with: ",news, NPAT, pat)) == ABORT) + if ((s = ereply("Query replace %s with: ", news, NPAT, pat)) == ABORT) return (s); if (s == FALSE) news[0] = '\0'; @@ -465,7 +484,7 @@ */ while (forwsrch() == TRUE) { - retry: +retry: update(); switch (getkey(FALSE)) { case ' ': @@ -480,7 +499,7 @@ rcnt++; goto stopsearch; - case CCHR('G'): /* ^G or ESC */ + case CCHR('G'):/* ^G or ESC */ (VOID) ctrlg(FFRAND, 0); case CCHR('['): goto stopsearch; @@ -494,11 +513,11 @@ goto stopsearch; case CCHR('H'): - case CCHR('?'): /* To not replace */ + case CCHR('?'):/* To not replace */ break; default: -ewprintf(" replace, [.] rep-end, don't, [!] repl rest quit"); + ewprintf(" replace, [.] rep-end, don't, [!] repl rest quit"); goto retry; } } @@ -521,19 +540,21 @@ * is notified of the change, and TRUE is returned. If the * string isn't found, FALSE is returned. */ -forwsrch() { - register LINE *clp; - register int cbo; - register LINE *tlp; - register int tbo; - char *pp; - register int c; +forwsrch() +{ + register LINE *clp; + register int cbo; + register LINE *tlp; + register int tbo; + char *pp; + register int c; clp = curwp->w_dotp; cbo = curwp->w_doto; - for(;;) { + for (;;) { if (cbo == llength(clp)) { - if((clp = lforw(clp)) == curbp->b_linep) break; + if ((clp = lforw(clp)) == curbp->b_linep) + break; cbo = 0; c = CCHR('J'); } else @@ -541,7 +562,7 @@ if (eq(c, pat[0]) != FALSE) { tlp = clp; tbo = cbo; - pp = &pat[1]; + pp = &pat[1]; while (*pp != 0) { if (tbo == llength(tlp)) { tlp = lforw(tlp); @@ -554,12 +575,12 @@ if (eq(c, *pp++) == FALSE) goto fail; } - curwp->w_dotp = tlp; - curwp->w_doto = tbo; + curwp->w_dotp = tlp; + curwp->w_doto = tbo; curwp->w_flag |= WFMOVE; return TRUE; } - fail: ; +fail: ; } return FALSE; } @@ -571,17 +592,17 @@ * is notified of the change, and TRUE is returned. If the * string isn't found, FALSE is returned. */ -backsrch() { - register LINE *clp; - register int cbo; - register LINE *tlp; - register int tbo; - register int c; - register char *epp; - register char *pp; +backsrch() +{ + register LINE *clp; + register int cbo; + register LINE *tlp; + register int tbo; + register int c; + register char *epp; + register char *pp; - for (epp = &pat[0]; epp[1] != 0; ++epp) - ; + for (epp = &pat[0]; epp[1] != 0; ++epp); clp = curwp->w_dotp; cbo = curwp->w_doto; for (;;) { @@ -589,38 +610,38 @@ clp = lback(clp); if (clp == curbp->b_linep) return FALSE; - cbo = llength(clp)+1; + cbo = llength(clp) + 1; } if (--cbo == llength(clp)) c = CCHR('J'); else - c = lgetc(clp,cbo); + c = lgetc(clp, cbo); if (eq(c, *epp) != FALSE) { tlp = clp; tbo = cbo; - pp = epp; + pp = epp; while (pp != &pat[0]) { if (tbo == 0) { tlp = lback(tlp); if (tlp == curbp->b_linep) goto fail; - tbo = llength(tlp)+1; + tbo = llength(tlp) + 1; } if (--tbo == llength(tlp)) c = CCHR('J'); else - c = lgetc(tlp,tbo); + c = lgetc(tlp, tbo); if (eq(c, *--pp) == FALSE) goto fail; } - curwp->w_dotp = tlp; - curwp->w_doto = tbo; + curwp->w_dotp = tlp; + curwp->w_doto = tbo; curwp->w_flag |= WFMOVE; return TRUE; } - fail: ; +fail: ; } - /*NOTREACHED*/ + /* NOTREACHED */ } /* @@ -631,13 +652,16 @@ */ static int eq(bc, pc) -register int bc, pc; + register int bc, pc; { bc = CHARMASK(bc); pc = CHARMASK(pc); - if (bc == pc) return TRUE; - if (ISUPPER(bc)) return TOLOWER(bc) == pc; - if (ISUPPER(pc)) return bc == TOLOWER(pc); + if (bc == pc) + return TRUE; + if (ISUPPER(bc)) + return TOLOWER(bc) == pc; + if (ISUPPER(pc)) + return bc == TOLOWER(pc); return FALSE; } @@ -649,16 +673,20 @@ * Display the old pattern, in the style of Jeff Lomicka. There is * some do-it-yourself control expansion. */ -readpattern(prompt) char *prompt; { - register int s; - char tpat[NPAT]; +readpattern(prompt) + char *prompt; +{ + register int s; + char tpat[NPAT]; - if (tpat[0] == '\0') s = ereply("%s: ", tpat, NPAT, prompt); - else s = ereply("%s: (default %s) ", tpat, NPAT, prompt, pat); + if (tpat[0] == '\0') + s = ereply("%s: ", tpat, NPAT, prompt); + else + s = ereply("%s: (default %s) ", tpat, NPAT, prompt, pat); - if (s == TRUE) /* Specified */ + if (s == TRUE) /* Specified */ (VOID) strcpy(pat, tpat); - else if (s==FALSE && pat[0]!=0) /* CR, but old one */ + else if (s == FALSE && pat[0] != 0) /* CR, but old one */ s = TRUE; return s; }