Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- text-utils/more.c | 206 ++++++++++++++++++++++++++++++----------------------- 1 files changed, 117 insertions(+), 89 deletions(-) diff --git a/text-utils/more.c b/text-utils/more.c index 929e4a0..add2b94 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -94,7 +94,7 @@ void error (char *mess); void do_shell (char *filename); int colon (char *filename, int cmd, int nlines); int expand (char **outbuf, char *inbuf); -void argscan(char *s,char *argv0); +void argscan(char *s); void rdline (register FILE *f); void copy_file(register FILE *f); void search(char buf[], FILE *file, register int n); @@ -113,7 +113,7 @@ int get_line(register FILE *f, int *length); void prbuf (register char *s, register int n); void execute (char *filename, char *cmd, ...); FILE *checkf (char *, int *); -int prepare_line_buffer(void); +void prepare_line_buffer(void); #define TBUFSIZ 1024 #define LINSIZ 256 /* minimal Line buffer size */ @@ -183,9 +183,31 @@ extern char PC; /* pad character */ #if defined(HAVE_NCURSES_H) || defined(HAVE_NCURSES_NCURSES_H) # include <term.h> /* include after <curses.h> */ +#define TERM_AUTO_RIGHT_MARGIN "am" +#define TERM_CEOL "xhp" +#define TERM_CLEAR "clear" +#define TERM_CLEAR_TO_LINE_END "el" +#define TERM_CLEAR_TO_SCREEN_END "ed" +#define TERM_COLS "cols" +#define TERM_CURSOR_ADDRESS "cup" +#define TERM_EAT_NEW_LINE "xenl" +#define TERM_ENTER_UNDERLINE "smul" +#define TERM_EXIT_STANDARD_MODE "rmso" +#define TERM_EXIT_UNDERLINE "rmul" +#define TERM_HARD_COPY "hc" +#define TERM_HOME "home" +#define TERM_LINE_DOWN "cud1" +#define TERM_LINES "lines" +#define TERM_OVER_STRIKE "os" +#define TERM_PAD_CHAR "pad" +#define TERM_STANDARD_MODE "smso" +#define TERM_STD_MODE_GLITCH "xmc" +#define TERM_UNDERLINE_CHAR "uc" +#define TERM_UNDERLINE "ul" + static void my_putstring(char *s) { - tputs (s, 1, putchar); /* putp(s); */ + tputs (s, fileno(stdout), putchar); /* putp(s); */ } static void @@ -194,18 +216,18 @@ my_setupterm(char *term, int fildes, int *errret) { } static int -my_tgetnum(char *s, char *ss) { - return tigetnum(ss); +my_tgetnum(char *s) { + return tigetnum(s); } static int -my_tgetflag(char *s, char *ss) { - return tigetflag(ss); +my_tgetflag(char *s) { + return tigetflag(s); } static char * -my_tgetstr(char *s, char *ss) { - return tigetstr(ss); +my_tgetstr(char *s) { + return tigetstr(s); } static char * @@ -217,13 +239,35 @@ my_tgoto(char *cap, int col, int row) { #include <termcap.h> +#define TERM_AUTO_RIGHT_MARGIN "am" +#define TERM_CEOL "xs" +#define TERM_CLEAR "cl" +#define TERM_CLEAR_TO_LINE_END "ce" +#define TERM_CLEAR_TO_SCREEN_END "cd" +#define TERM_COLS "co" +#define TERM_CURSOR_ADDRESS "cm" +#define TERM_EAT_NEW_LINE "xn" +#define TERM_ENTER_UNDERLINE "us" +#define TERM_EXIT_STANDARD_MODE "se" +#define TERM_EXIT_UNDERLINE "ue" +#define TERM_HARD_COPY "hc" +#define TERM_HOME "ho" +#define TERM_LINE_DOWN "le" +#define TERM_LINES "li" +#define TERM_OVER_STRIKE "os" +#define TERM_PAD_CHAR "pc" +#define TERM_STANDARD_MODE "so" +#define TERM_STD_MODE_GLITCH "sg" +#define TERM_UNDERLINE_CHAR "uc" +#define TERM_UNDERLINE "ul" + char termbuffer[4096]; char tcbuffer[4096]; char *strbuf = termbuffer; static void my_putstring(char *s) { - tputs (s, 1, putchar); + tputs (s, fileno(stdout), putchar); } static void @@ -232,17 +276,17 @@ my_setupterm(char *term, int fildes, int *errret) { } static int -my_tgetnum(char *s, char *ss) { +my_tgetnum(char *s) { return tgetnum(s); } static int -my_tgetflag(char *s, char *ss) { +my_tgetflag(char *s) { return tgetflag(s); } static char * -my_tgetstr(char *s, char *ss) { +my_tgetstr(char *s) { return tgetstr(s, &strbuf); } @@ -254,17 +298,10 @@ my_tgoto(char *cap, int col, int row) { #endif /* HAVE_LIBTERMCAP */ static void -idummy(int *kk) {} - -static void -Fdummy(FILE **ff) {} - -static void -usage(char *s) { - char *p = strrchr(s, '/'); +usage() { fprintf(stderr, _("usage: %s [-dflpcsu] [+linenum | +/pattern] name1 name2 ...\n"), - p ? p + 1 : s); + program_invocation_short_name); } int main(int argc, char **argv) { @@ -284,27 +321,19 @@ int main(int argc, char **argv) { bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - /* avoid gcc complaints about register variables that - may be clobbered by a longjmp, by forcing our variables here - to be non-register */ - Fdummy(&f); idummy(&left); idummy(&prnames); - idummy(&initopt); idummy(&srchopt); idummy(&initline); - nfiles = argc; fnames = argv; setlocale(LC_ALL, ""); initterm (); - if (prepare_line_buffer()) { - fprintf(stderr, _("failed to initialize line buffer\n")); - exit(1); - } + prepare_line_buffer(); + nscroll = Lpp/2 - 1; if (nscroll <= 0) nscroll = 1; - if((s = getenv("MORE")) != NULL) argscan(s,argv[0]); + if((s = getenv("MORE")) != NULL) argscan(s); while (--nfiles > 0) { if ((ch = (*++fnames)[0]) == '-') { - argscan(*fnames+1,argv[0]); + argscan(*fnames+1); } else if (ch == '+') { s = *fnames; @@ -340,8 +369,8 @@ int main(int argc, char **argv) { if (nfiles > 1) prnames++; if (!no_intty && nfiles == 0) { - usage(argv[0]); - exit(1); + usage(); + exit(EXIT_FAILURE); } else f = stdin; @@ -447,10 +476,10 @@ int main(int argc, char **argv) { firstf = 0; } reset_tty (); - exit(0); + exit(EXIT_SUCCESS); } -void argscan(char *s, char *argv0) { +void argscan(char *s) { int seen_num = 0; while (*s != '\0') { @@ -490,9 +519,9 @@ void argscan(char *s, char *argv0) { break; default: fprintf(stderr, - _("%s: unknown option \"-%c\"\n"), argv0, *s); - usage(argv0); - exit(1); + _("%s: unknown option \"-%c\"\n"), program_invocation_short_name, *s); + usage(); + exit(EXIT_FAILURE); break; } s++; @@ -658,6 +687,8 @@ void screen (register FILE *f, register int num_lines) */ void onquit(int dummy) { + /* to get rid of compiler warning 'unused parameter' */ + dummy = dummy; signal(SIGQUIT, SIG_IGN); if (!inwait) { putchar ('\n'); @@ -683,6 +714,8 @@ void onquit(int dummy) { void chgwinsz(int dummy) { struct winsize win; + /* to get rid of compiler warning 'unused parameter' */ + dummy = dummy; (void) signal(SIGWINCH, SIG_IGN); if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { if (win.ws_row != 0) { @@ -704,6 +737,8 @@ void chgwinsz(int dummy) { */ void end_it (int dummy) { + /* to get rid of compiler warning 'unused parameter' */ + dummy = dummy; reset_tty (); if (clreol) { putchar ('\r'); @@ -716,7 +751,7 @@ void end_it (int dummy) { } else putcerr('\n'); - _exit(0); + _exit(EXIT_SUCCESS); } void copy_file(register FILE *f) { @@ -781,30 +816,20 @@ static void prompt (char *filename) inwait++; } -int prepare_line_buffer(void) +void prepare_line_buffer(void) { char *nline; size_t nsz = Mcol * 4; if (LineLen >= nsz) - return 0; + return; if (nsz < LINSIZ) nsz = LINSIZ; - nline = realloc(Line, nsz); - if (nline) { - Line = nline; - LineLen = nsz; - return 0; - } - - /* error() uses siglongjmp(), we want to return from this - * function when the Line buffer is initilized first time in main() - */ - if (Line) - error(_("out of memory")); - return -1; + nline = xrealloc(Line, nsz); + Line = nline; + LineLen = nsz; } /* @@ -819,7 +844,7 @@ int get_line(register FILE *f, int *length) static int colflg; #ifdef HAVE_WIDECHAR - int i; + size_t i; wchar_t wc; int wc_width; mbstate_t state, state_bak; /* Current status of the stream. */ @@ -1629,7 +1654,7 @@ void search(char buf[], FILE *file, register int n) void execute (char *filename, char *cmd, ...) { int id; - int n; + int n, fd; va_list argp; char * arg; char ** args; @@ -1639,9 +1664,10 @@ void execute (char *filename, char *cmd, ...) reset_tty (); for (n = 10; (id = fork ()) < 0 && n > 0; n--) sleep (5); - if (id == 0) { - if (!isatty(0)) { - close(0); + fd = fileno(stdin); + if (id == fd) { + if (!isatty(fd)) { + close(fd); open("/dev/tty", 0); } @@ -1669,7 +1695,7 @@ void execute (char *filename, char *cmd, ...) execvp (cmd, args); putserr(_("exec failed\n")); - exit (1); + exit (EXIT_FAILURE); } if (id > 0) { signal (SIGINT, SIG_IGN); @@ -1760,7 +1786,7 @@ retry: */ if ((tgrp = tcgetpgrp(fileno(stdout))) < 0) { perror("tcgetpgrp"); - exit(1); + exit(EXIT_FAILURE); } if (tgrp != getpgrp(0)) { kill(0, SIGTTOU); @@ -1771,7 +1797,7 @@ retry: if ((term = getenv("TERM")) == 0) { dumb++; ul_opt = 0; } - my_setupterm(term, 1, &ret); + my_setupterm(term, fileno(stdout), &ret); if (ret <= 0) { dumb++; ul_opt = 0; } @@ -1779,35 +1805,35 @@ retry: #ifdef TIOCGWINSZ if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { #endif - Lpp = my_tgetnum("li","lines"); - Mcol = my_tgetnum("co","cols"); + Lpp = my_tgetnum(TERM_LINES); + Mcol = my_tgetnum(TERM_COLS); #ifdef TIOCGWINSZ } else { if ((Lpp = win.ws_row) == 0) - Lpp = my_tgetnum("li","lines"); + Lpp = my_tgetnum(TERM_LINES); if ((Mcol = win.ws_col) == 0) - Mcol = my_tgetnum("co","cols"); + Mcol = my_tgetnum(TERM_COLS); } #endif - if ((Lpp <= 0) || my_tgetflag("hc","hc")) { + if ((Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) { hard++; /* Hard copy terminal */ Lpp = 24; } - if (my_tgetflag("xn","xenl")) + if (my_tgetflag(TERM_EAT_NEW_LINE)) eatnl++; /* Eat newline at last column + 1; dec, concept */ if (Mcol <= 0) Mcol = 80; if (tailequ (fnames[0], "page")) noscroll++; - Wrap = my_tgetflag("am","am"); - bad_so = my_tgetflag ("xs","xhp"); - eraseln = my_tgetstr("ce","el"); - Clear = my_tgetstr("cl","clear"); - Senter = my_tgetstr("so","smso"); - Sexit = my_tgetstr("se","rmso"); - if ((soglitch = my_tgetnum("sg","xmc")) < 0) + Wrap = my_tgetflag(TERM_AUTO_RIGHT_MARGIN); + bad_so = my_tgetflag (TERM_CEOL); + eraseln = my_tgetstr(TERM_CLEAR_TO_LINE_END); + Clear = my_tgetstr(TERM_CLEAR); + Senter = my_tgetstr(TERM_STANDARD_MODE); + Sexit = my_tgetstr(TERM_EXIT_STANDARD_MODE); + if ((soglitch = my_tgetnum(TERM_STD_MODE_GLITCH)) < 0) soglitch = 0; /* @@ -1818,12 +1844,12 @@ retry: * isn't available, settle for standout sequence. */ - if (my_tgetflag("ul","ul") || my_tgetflag("os","os")) + if (my_tgetflag(TERM_UNDERLINE) || my_tgetflag(TERM_OVER_STRIKE)) ul_opt = 0; - if ((chUL = my_tgetstr("uc","uc")) == NULL ) + if ((chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL ) chUL = ""; - if (((ULenter = my_tgetstr("us","smul")) == NULL || - (ULexit = my_tgetstr("ue","rmul")) == NULL) && !*chUL) { + if (((ULenter = my_tgetstr(TERM_ENTER_UNDERLINE)) == NULL || + (ULexit = my_tgetstr(TERM_EXIT_UNDERLINE)) == NULL) && !*chUL) { if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { ULenter = ""; ULexit = ""; @@ -1833,18 +1859,18 @@ retry: ulglitch = 0; } - if ((padstr = my_tgetstr("pc","pad")) != NULL) + if ((padstr = my_tgetstr(TERM_PAD_CHAR)) != NULL) PC = *padstr; - Home = my_tgetstr("ho","home"); + Home = my_tgetstr(TERM_HOME); if (Home == 0 || *Home == '\0') { - if ((cursorm = my_tgetstr("cm","cup")) != NULL) { + if ((cursorm = my_tgetstr(TERM_CURSOR_ADDRESS)) != NULL) { const char *t = (const char *)my_tgoto(cursorm, 0, 0); xstrncpy(cursorhome, t, sizeof(cursorhome)); Home = cursorhome; } } - EodClr = my_tgetstr("cd","ed"); - if ((chBS = my_tgetstr("le","cub1")) == NULL) + EodClr = my_tgetstr(TERM_CLEAR_TO_SCREEN_END); + if ((chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL) chBS = "\b"; } @@ -2106,7 +2132,7 @@ reset_tty () { if (no_tty) return; if (pstate) { - tputs(ULexit, 1, ourputch); /* putchar - if that isnt a macro */ + tputs(ULexit, fileno(stdout), ourputch); /* putchar - if that isnt a macro */ fflush(stdout); pstate = 0; } @@ -2124,7 +2150,7 @@ void rdline (register FILE *f) prepare_line_buffer(); p = Line; - while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LineLen - 1) + while ((c = Getc (f)) != '\n' && c != EOF && (size_t) (p - Line) < LineLen - 1) *p++ = c; if (c == '\n') Currline++; @@ -2136,6 +2162,8 @@ void rdline (register FILE *f) void onsusp (int dummy) { sigset_t signals, oldmask; + /* to get rid of compiler warning 'unused parameter' */ + dummy = dummy; /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ signal(SIGTTOU, SIG_IGN); reset_tty (); -- 1.7.3.5 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html