From bd6a253f690793b13c97048b3611b33596f15c12 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 26 May 2018 09:37:47 +0100 Subject: more: remove function like preprocessor defines Inlining code using preprocessor function like macros is bad for readability, and prone to errors. Besides this is a pager, who cares if code is few milliseconds slower. Requestee-by: Karel Zak Signed-off-by: Sami Kerola --- text-utils/more.c | 238 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 134 insertions(+), 104 deletions(-) (limited to 'text-utils/more.c') diff --git a/text-utils/more.c b/text-utils/more.c index b937d30a3..e9c6c0549 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -85,28 +85,10 @@ #define VI "vi" /* found on the user's path */ -#define Fopen(s,m) (ctl->Currline = 0, ctl->file_pos=0, fopen(s,m)) -#define Ftell(f) ctl->file_pos -#define Fseek(f,off) (ctl->file_pos=off, fseek(f, off, 0)) -#define Getc(f) (++ctl->file_pos, getc(f)) -#define Ungetc(c,f) (--ctl->file_pos, ungetc(c,f)) -#define putcerr(c) fputc(c, stderr) -#define putserr(s) fputs(s, stderr) -#define putsout(s) fputs(s, stdout) - -#define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) - #define BS "\b" #define BSB "\b \b" #define CARAT "^" - -#define ERASEONECOLUMN(x) \ - do { \ - if (x) \ - putserr(BSB); \ - else \ - putserr(BS); \ - } while(0) +#define RINGBELL '\007' #define TBUFSIZ 1024 #define LINSIZ 256 /* minimal Line buffer size */ @@ -318,6 +300,24 @@ static void argscan(struct more_control *ctl, char *s) } } +static void more_fseek(struct more_control *ctl, FILE *stream, long pos) +{ + ctl->file_pos = pos; + fseek(stream, pos, 0); +} + +static int more_getc(struct more_control *ctl, FILE *stream) +{ + ctl->file_pos++; + return getc(stream); +} + +static int more_ungetc(struct more_control *ctl, int c, FILE *stream) +{ + ctl->file_pos--; + return ungetc(c, stream); +} + /* force clear to end of line */ static void cleareol(struct more_control *ctl) { @@ -373,7 +373,9 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst printf(_("\n*** %s: directory ***\n\n"), fs); return ((FILE *)NULL); } - if ((f = Fopen(fs, "r")) == NULL) { + ctl->Currline = 0; + ctl->file_pos = 0; + if ((f = fopen(fs, "r")) == NULL) { fflush(stdout); warn(_("cannot open %s"), fs); return ((FILE *)NULL); @@ -383,9 +385,9 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst return ((FILE *)NULL); } fcntl(fileno(f), F_SETFD, FD_CLOEXEC); - c = Getc(f); + c = more_getc(ctl, f); *clearfirst = (c == '\f'); - Ungetc(c, f); + more_ungetc(ctl, c, f); if ((ctl->file_size = stbuf.st_size) == 0) ctl->file_size = LONG_MAX; return (f); @@ -426,7 +428,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) size_t mbc_pos = 0; /* Position of the MBC. */ int use_mbc_buffer_flag = 0; /* If 1, mbc has data. */ int break_flag = 0; /* If 1, exit while(). */ - long file_pos_bak = Ftell(f); + long file_pos_bak = ctl->file_pos; memset(&state, '\0', sizeof(mbstate_t)); #endif @@ -435,10 +437,10 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) p = ctl->Line; column = 0; - c = Getc(f); + c = more_getc(ctl, f); if (colflg && c == '\n') { ctl->Currline++; - c = Getc(f); + c = more_getc(ctl, f); } while (p < &ctl->Line[ctl->LineLen - 1]) { #ifdef HAVE_WIDECHAR @@ -462,7 +464,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) file_pos_bak++; if (column >= ctl->Mcol) { - Fseek(f, file_pos_bak); + more_fseek(ctl, f, file_pos_bak); } else { memmove(mbc, mbc + 1, --mbc_pos); if (mbc_pos > 0) { @@ -476,7 +478,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) wc_width = wcwidth(wc); if (column + wc_width > ctl->Mcol) { - Fseek(f, file_pos_bak); + more_fseek(ctl, f, file_pos_bak); break_flag = 1; } else { for (i = 0; p < &ctl->Line[ctl->LineLen - 1] && @@ -490,7 +492,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) if (break_flag || column >= ctl->Mcol) break; - c = Getc(f); + c = more_getc(ctl, f); continue; } #endif /* HAVE_WIDECHAR */ @@ -511,14 +513,14 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) *p++ = c; #if 0 if (c == '\033') { /* ESC */ - c = Getc(f); + c = more_getc(ctl, f); while (c > ' ' && c < '0' && p < &Line[LineLen - 1]) { *p++ = c; - c = Getc(f); + c = more_getc(ctl, f); } if (c >= '0' && c < '\177' && p < &Line[LineLen - 1]) { *p++ = c; - c = Getc(f); + c = more_getc(ctl, f); continue; } } @@ -543,13 +545,13 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) } else if (c == '\b' && column > 0) { column--; } else if (c == '\r') { - int next = Getc(f); + int next = more_getc(ctl, f); if (next == '\n') { p--; ctl->Currline++; break; } - Ungetc(next, f); + more_ungetc(ctl, c, f); column = 0; } else if (c == '\f' && ctl->stop_opt) { p[-1] = '^'; @@ -572,7 +574,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) switch (mblength) { case (size_t)-2: p--; - file_pos_bak = Ftell(f) - 1; + file_pos_bak = ctl->file_pos - 1; state = state_bak; use_mbc_buffer_flag = 1; break; @@ -603,7 +605,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length) * whole multibyte sequence */ break; #endif - c = Getc(f); + c = more_getc(ctl, f); } if (column >= ctl->Mcol && ctl->Mcol > 0) { if (!ctl->Wrap) { @@ -654,12 +656,20 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n, } #endif +static int wouldul(char *s, int n) +{ + if (n < 2) + return 0; + if ((s[0] == '_' && s[1] == '\b') || (s[1] == '\b' && s[2] == '_')) + return 1; + return 0; +} + /* Print a buffer of n characters */ static void prbuf(struct more_control *ctl, register char *s, register int n) { register char c; /* next output character */ register int state; /* next output char's UL state */ -#define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) while (--n >= 0) if (!ctl->ul_opt) @@ -702,7 +712,7 @@ static void prbuf(struct more_control *ctl, register char *s, register int n) putchar(c); #endif /* HAVE_WIDECHAR */ if (state && *ctl->chUL) { - putsout(ctl->chBS); + fputs(ctl->chBS, stdout); putstring(ctl->chUL); } ctl->pstate = state; @@ -717,8 +727,6 @@ static void kill_line(struct more_control *ctl) putchar('\r'); } -#define ringbell() putcerr('\007') - static void prompt(struct more_control *ctl, char *filename) { if (ctl->clreol) @@ -751,7 +759,7 @@ static void prompt(struct more_control *ctl, char *filename) clreos(ctl); fflush(stdout); } else - ringbell(); + fputc(RINGBELL, stderr); ctl->inwait++; } @@ -773,7 +781,7 @@ static void reset_tty(void) global_ctl->otty.c_lflag |= ICANON | ECHO; global_ctl->otty.c_cc[VMIN] = global_ctl->savetty0.c_cc[VMIN]; global_ctl->otty.c_cc[VTIME] = global_ctl->savetty0.c_cc[VTIME]; - stty(fileno(stderr), &global_ctl->savetty0); + tcsetattr(STDERR_FILENO, TCSANOW, &global_ctl->savetty0); } /* Clean up terminal state and exit. Also come here if interrupt signal received */ @@ -796,7 +804,7 @@ static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unus kill_line(global_ctl); fflush(stdout); } else - putcerr('\n'); + fputc('\n', stderr); free(global_ctl->previousre); free(global_ctl->Line); _exit(EXIT_SUCCESS); @@ -856,9 +864,9 @@ static void skipf(struct more_control *ctl, register int nskip) if (ctl->clreol) cleareol(ctl); if (nskip > 0) - putsout(_("...Skipping to file ")); + fputs(_("...Skipping to file "), stdout); else - putsout(_("...Skipping back to file ")); + fputs(_("...Skipping back to file "), stdout); puts(ctl->fnames[ctl->fnum]); if (ctl->clreol) cleareol(ctl); @@ -870,10 +878,10 @@ static void show(struct more_control *ctl, char c) { if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) { c += (c == RUBOUT) ? -0100 : 0100; - putserr(CARAT); + fputs(CARAT, stderr); ctl->promptlen++; } - putcerr(c); + fputc(c, stderr); ctl->promptlen++; } @@ -886,15 +894,23 @@ static void more_error(struct more_control *ctl, char *mess) ctl->promptlen += strlen(mess); if (ctl->Senter && ctl->Sexit) { putp(ctl->Senter); - putsout(mess); + fputs(mess, stdout); putp(ctl->Sexit); } else - putsout(mess); + fputs(mess, stdout); fflush(stdout); ctl->errors++; siglongjmp(ctl->restore, 1); } +static void erase_one_column(struct more_control *ctl) +{ + if (ctl->docrterase) + fputs(BSB, stderr); + else + fputs(BS, stderr); +} + static void ttyin(struct more_control *ctl, char buf[], register int nmax, char pchar) { char *sp; @@ -943,7 +959,7 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char } if (mblength == 1) { - ERASEONECOLUMN(ctl->docrterase); + erase_one_column(ctl); } else { int wc_width; wc_width = wcwidth(wc); @@ -951,7 +967,7 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char (wc_width < 1) ? 1 : wc_width; while (wc_width--) { - ERASEONECOLUMN(ctl->docrterase); + erase_one_column(ctl); } } @@ -963,13 +979,13 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char #endif /* HAVE_WIDECHAR */ { --ctl->promptlen; - ERASEONECOLUMN(ctl->docrterase); + erase_one_column(ctl); --sp; } if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) { --ctl->promptlen; - ERASEONECOLUMN(ctl->docrterase); + erase_one_column(ctl); } continue; } else { @@ -989,7 +1005,7 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char erasep(ctl, 1); else if (ctl->docrtkill) while (ctl->promptlen-- > 1) - putserr(BSB); + fputs(BSB, stderr); ctl->promptlen = 1; } sp = buf; @@ -998,7 +1014,7 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char } if (slash && ((cc_t) c == ctl->otty.c_cc[VKILL] || (cc_t) c == ctl->otty.c_cc[VERASE])) { - ERASEONECOLUMN(ctl->docrterase); + erase_one_column(ctl); --sp; } if (c != '\\') @@ -1006,11 +1022,11 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char *sp++ = c; if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) { c += (c == RUBOUT) ? -0100 : 0100; - putserr(CARAT); + fputs(CARAT, stderr); ctl->promptlen++; } if (c != '\n' && c != ESC) { - putcerr(c); + fputc(c, stderr); ctl->promptlen++; } else break; @@ -1081,7 +1097,7 @@ static void set_tty(struct more_control *ctl) ctl->otty.c_lflag &= ~(ICANON | ECHO); ctl->otty.c_cc[VMIN] = 1; /* read at least 1 char */ ctl->otty.c_cc[VTIME] = 0; /* no timeout */ - stty(fileno(stderr), &ctl->otty); + tcsetattr(STDERR_FILENO, TCSANOW, &ctl->otty); } /* Come here if a quit signal is received */ @@ -1176,7 +1192,7 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...) execvp(cmd, args); errsv = errno; - putserr(_("exec failed\n")); + fputs(_("exec failed\n"), stderr); exit(errsv == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED); } if (id > 0) { @@ -1190,7 +1206,7 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...) if (ctl->catch_susp) signal(SIGTSTP, onsusp); } else - putserr(_("can't fork\n")); + fputs(_("can't fork\n"), stderr); set_tty(ctl); puts("------------------------"); prompt(ctl, filename); @@ -1207,7 +1223,7 @@ static void do_shell(struct more_control *ctl, char *filename) fflush(stdout); ctl->promptlen = 1; if (ctl->lastp) - putsout(ctl->shell_line); + fputs(ctl->shell_line, stdout); else { ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!'); expanded = NULL; @@ -1220,7 +1236,7 @@ static void do_shell(struct more_control *ctl, char *filename) free(expanded); } if (rc < 0) { - putserr(_(" Overflow\n")); + fputs(_(" Overflow\n"), stderr); prompt(ctl, filename); return; } else if (rc > 0) { @@ -1229,7 +1245,7 @@ static void do_shell(struct more_control *ctl, char *filename) } } fflush(stdout); - putcerr('\n'); + fputc('\n', stderr); ctl->promptlen = 0; ctl->shellp = 1; execute(ctl, filename, ctl->shell, ctl->shell, "-c", ctl->shell_line, 0); @@ -1266,7 +1282,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines) return (0); case 'p': if (ctl->no_intty) { - ringbell(); + fputc(RINGBELL, stderr); return (-1); } putchar('\r'); @@ -1282,7 +1298,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines) case 'Q': end_it(0); default: - ringbell(); + fputc(RINGBELL, stderr); return (-1); } } @@ -1293,7 +1309,7 @@ static void skiplns(struct more_control *ctl, register int n, register FILE *f) register int c; while (n > 0) { - while ((c = Getc(f)) != '\n') + while ((c = more_getc(ctl, f)) != '\n') if (c == EOF) return; n--; @@ -1321,7 +1337,7 @@ static void rdline(struct more_control *ctl, register FILE *f) prepare_line_buffer(ctl); p = ctl->Line; - while ((c = Getc(f)) != '\n' && c != EOF + while ((c = more_getc(ctl, f)) != '\n' && c != EOF && (size_t)(p - ctl->Line) < ctl->LineLen - 1) *p++ = c; if (c == '\n') @@ -1339,7 +1355,7 @@ static void home(struct more_control *ctl) * the file */ static void search(struct more_control *ctl, char buf[], FILE *file, register int n) { - long startline = Ftell(file); + long startline = ctl->file_pos; register long line1 = startline; register long line2 = startline; register long line3; @@ -1360,7 +1376,7 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in while (!feof(file)) { line3 = line2; line2 = line1; - line1 = Ftell(file); + line1 = ctl->file_pos; rdline(ctl, file); lncount++; if (regexec(&re, ctl->Line, 0, NULL, 0) == 0) { @@ -1369,12 +1385,12 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in putchar('\n'); if (ctl->clreol) cleareol(ctl); - putsout(_("...skipping\n")); + fputs(_("...skipping\n"), stdout); } if (!ctl->no_intty) { ctl->Currline -= (lncount >= 3 ? 3 : lncount); - Fseek(file, line3); + more_fseek(ctl, file, line3); if (ctl->noscroll) { if (ctl->clreol) { home(ctl); @@ -1401,9 +1417,9 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in if (feof(file)) { if (!ctl->no_intty) { ctl->Currline = saveln; - Fseek(file, startline); + more_fseek(ctl, file, startline); } else { - putsout(_("\nPattern not found\n")); + fputs(_("\nPattern not found\n"), stdout); end_it(0); } free(ctl->previousre); @@ -1426,8 +1442,6 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) int done; char comchar, cmdbuf[INIT_BUF]; -#define ret(val) retval=val;done++;break - done = 0; if (!ctl->errors) prompt(ctl, filename); @@ -1462,7 +1476,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) register int initline; if (ctl->no_intty) { - ringbell(); + fputc(RINGBELL, stderr); return (-1); } @@ -1486,14 +1500,16 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) --initline; if (initline < 0) initline = 0; - Fseek(f, 0L); + more_fseek(ctl, f, 0L); ctl->Currline = 0; /* skiplns() will make Currline correct */ skiplns(ctl, initline, f); if (!ctl->noscroll) { - ret(ctl->dlines + 1); + retval = ctl->dlines + 1; } else { - ret(ctl->dlines); + retval = ctl->dlines; } + done = 1; + break; } case ' ': case 'z': @@ -1501,12 +1517,16 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) nlines = ctl->dlines; else if (comchar == 'z') ctl->dlines = nlines; - ret(nlines); + retval = nlines; + done = 1; + break; case 'd': case ctrl('D'): if (nlines != 0) ctl->nscroll = nlines; - ret(ctl->nscroll); + retval = ctl->nscroll; + done = 1; + break; case 'q': case 'Q': end_it(0); @@ -1531,7 +1551,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) putchar('\n'); while (nlines > 0) { - while ((c = Getc(f)) != '\n') + while ((c = more_getc(ctl, f)) != '\n') if (c == EOF) { retval = 0; done++; @@ -1540,32 +1560,40 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) ctl->Currline++; nlines--; } - ret(ctl->dlines); + retval = ctl->dlines; + done = 1; + break; case '\n': if (nlines != 0) ctl->dlines = nlines; else nlines = 1; - ret(nlines); + retval = nlines; + done = 1; + break; case '\f': if (!ctl->no_intty) { doclear(ctl); - Fseek(f, ctl->screen_start.chrctr); + more_fseek(ctl, f, ctl->screen_start.chrctr); ctl->Currline = ctl->screen_start.line; - ret(ctl->dlines); + retval = ctl->dlines; + done = 1; + break; } else { - ringbell(); + fputc(RINGBELL, stderr); break; } case '\'': if (!ctl->no_intty) { kill_line(ctl); - putsout(_("\n***Back***\n\n")); - Fseek(f, ctl->context.chrctr); + fputs(_("\n***Back***\n\n"), stdout); + more_fseek(ctl, f, ctl->context.chrctr); ctl->Currline = ctl->context.line; - ret(ctl->dlines); + retval = ctl->dlines; + done = 1; + break; } else { - ringbell(); + fputc(RINGBELL, stderr); break; } case '=': @@ -1588,16 +1616,18 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) ctl->promptlen = 1; fflush(stdout); if (ctl->lastp) { - putcerr('\r'); + fputc('\r', stderr); search(ctl, ctl->previousre, f, nlines); } else { ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '/'); - putcerr('\r'); + fputc('\r', stderr); free(ctl->previousre); ctl->previousre = xstrdup(cmdbuf); search(ctl, cmdbuf, f, nlines); } - ret(ctl->dlines - 1); + retval = ctl->dlines - 1; + done = 1; + break; case '!': do_shell(ctl, filename); break; @@ -1605,13 +1635,13 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) case 'h': if (ctl->noscroll) doclear(ctl); - putsout(_("\n" + fputs(_("\n" "Most commands optionally preceded by integer argument k. " "Defaults in brackets.\n" - "Star (*) indicates argument becomes new default.\n")); + "Star (*) indicates argument becomes new default.\n"), stdout); puts("---------------------------------------" "----------------------------------------"); - putsout(_ + fputs(_ (" Display next k lines of text [current screen size]\n" "z Display next k lines of text [current screen size]*\n" " Display next k lines of text [1]*\n" @@ -1630,7 +1660,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) ":n Go to kth next file [1]\n" ":p Go to kth previous file [1]\n" ":f Display current file name and line number\n" - ". Repeat previous command\n")); + ". Repeat previous command\n"), stdout); puts("---------------------------------------" "----------------------------------------"); prompt(ctl, filename); @@ -1694,7 +1724,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f) ("[Press 'h' for instructions.]")); fflush(stdout); } else - ringbell(); + fputc(RINGBELL, stderr); break; } if (done) @@ -1753,7 +1783,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_ ctl->pstate = 0; } fflush(stdout); - if ((c = Getc(f)) == EOF) { + if ((c = more_getc(ctl, f)) == EOF) { if (ctl->clreol) clreos(ctl); return; @@ -1761,7 +1791,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_ if (ctl->Pause && ctl->clreol) clreos(ctl); - Ungetc(c, f); + more_ungetc(ctl, c, f); sigsetjmp(ctl->restore, 1); ctl->Pause = 0; ctl->startup = 0; @@ -1776,7 +1806,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_ doclear(ctl); } ctl->screen_start.line = ctl->Currline; - ctl->screen_start.chrctr = Ftell(f); + ctl->screen_start.chrctr = ctl->file_pos; } } @@ -2053,7 +2083,7 @@ int main(int argc, char **argv) signal(SIGTSTP, onsusp); ctl.catch_susp++; } - stty(fileno(stderr), &ctl.otty); + tcsetattr(STDERR_FILENO, TCSANOW, &ctl.otty); } if (ctl.no_intty) { if (ctl.no_tty) @@ -2118,7 +2148,7 @@ int main(int argc, char **argv) erasep(&ctl, 0); if (ctl.clreol) cleareol(&ctl); - putsout("::::::::::::::"); + fputs("::::::::::::::", stdout); if (ctl.promptlen > 14) erasep(&ctl, 14); putchar('\n'); @@ -2127,7 +2157,7 @@ int main(int argc, char **argv) puts(ctl.fnames[ctl.fnum]); if (ctl.clreol) cleareol(&ctl); - puts("::::::::::::::"); + fputs("::::::::::::::\n", stdout); if (left > ctl.Lpp - 4) left = ctl.Lpp - 4; } -- cgit v1.2.3-55-g7522