Skip to contentSkip to conversation

pull: more changes

PATCH In review Sami Kerola 2 participants 36 messages Feb 22, 2015 – Mar 1, 2015 Util-Linux package

lore
Share & tools
lore
#0 lore raw

cover pull: more changes

Hello,

The following changes are clean ups and improvements to more(1) command. I feel this change set does not sufficiently address all issues, and short comings of more(1). For example, lines that have terminal control characters to emphasis in bold or change color are after this change set still wrapped too early. That issue makes more(1) to be broken pager when viewing manual pages. Getting that issue fixed is my minimum before next util-linux release. Which leads me to bunch of questions;

* Should more(1) have similar to less(1) roll back and forth support?
* When searching should more(1) highlight what it finds?
* Or should more be the lightest weight, slickest, quickest, and least memory consuming pager?
* Does anyone care Posix Correctly(TM)[1] version of more(1)? ** Would include for example ctags support, case insensitive searching, exit rules, output filtering when stdout is not terminal, and so on.
* Any other ideas how & why more(1) could or should be improved?

[1] http://pubs.opengroup.org/onlinepubs/7908799/xcu/more.html

These changes are also available from my github remote repository.

git://github.com/kerolasa/lelux-utiliteetit.git more

Sami Kerola (31):
more: remove function declarations
more: reorder includes, declarations, and global variables
more: add struct more_control and remove global variables
more: remove dead code and useless comments
more: return is statement, not a function
more: use paths.h to find bourne shell and vi editor
more: remove function like preprocessor defines
more: remove pointless functions
more: remove 'register' keywords
more: avoid long else segment in prbuf()
more: move runtime usage output away from command()
more: move editor execution to a function from command()
more: move skipping backwards to a function from command()
more: move skipping forewards to a function from command()
more: check open(3) return value
more: remove unnecessary braces
more: do not call fileno() for std{in,out,err} streams
more: split run time help screen to multiple translatable lines
more: tell in run time help what the 'v' will execute as editor
more: add display_file() to be shared with files and stdin input
more: replace siglongjmp() and signal() calls with signalfd()
more: init function variables at declaration
more: rename variable and function names easier to understand
more: add -i ignore-case posix compliance option
more: add -n <number> posix compliance option
more: add -e posix compliance option
more: fix compiler warnings
more: speed up command_expansion()
more: remove unnecessary variable
more: remove TIOCGWINSZ preprocessor #ifdef checks
more: simplify initterm()

 text-utils/Makemodule.am |    4 +-
 text-utils/more-term.h   |  110 ++
 text-utils/more.1        |   13 +
 text-utils/more.c        | 3141 ++++++++++++++++++++++------------------------
 4 files changed, 1619 insertions(+), 1649 deletions(-)
 create mode 100644 text-utils/more-term.h
Signature · 1 line
2.3.0
#1 lore raw

PATCH 1/31 more: remove function declarations

In preparation to remove of global variables.

---
 text-utils/more.c | 2621 ++++++++++++++++++++++++++---------------------------
 1 file changed, 1292 insertions(+), 1329 deletions(-)
text-utils/more.c +1292 -1329
index 4602cba..dc7aa3f 100644
@@ -89,38 +89,6 @@
 
 #define stty(fd,argp)  tcsetattr(fd,TCSANOW,argp)
 
-/* some function declarations */
-void initterm(void);
-void kill_line(void);
-void doclear(void);
-void cleareol(void);
-void clreos(void);
-void home(void);
-void more_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);
-void rdline(register FILE *f);
-void copy_file(register FILE *f);
-void search(char buf[], FILE *file, register int n);
-void skipf(register int nskip);
-void skiplns(register int n, register FILE *f);
-void screen(register FILE *f, register int num_lines);
-int command(char *filename, register FILE *f);
-void erasep(register int col);
-void show(register char ch);
-void set_tty(void);
-void reset_tty(void);
-void ttyin(char buf[], register int nmax, char pchar);
-int number(char *cmd);
-int readch(void);
-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 *);
-void prepare_line_buffer(void);
-
 #define TBUFSIZ		1024
 #define LINSIZ		256	/* minimal Line buffer size */
 #define ctrl(letter)	(letter & 077)
@@ -140,7 +108,6 @@ struct termios otty, savetty0;
 long file_pos, file_size;
 int fnum, no_intty, no_tty, slow_tty;
 int dum_opt, dlines;
-void onquit(int), onsusp(int), chgwinsz(int), end_it(int);
 int nscroll = SCROLL_LEN;	/* Number of lines scrolled by 'd' */
 int fold_opt = 1;		/* Fold long lines */
 int stop_opt = 1;		/* Stop after form feeds */
@@ -183,13 +150,26 @@ int Wrap = 1;			/* set if automargins */
 int soglitch;			/* terminal has standout mode glitch */
 int ulglitch;			/* terminal has underline mode glitch */
 int pstate = 0;			/* current UL state */
-static int magic(FILE *, char *);
 char *previousre;		/* previous search() buf[] item */
 struct {
 	long chrctr, line;
 } context, screen_start;
 extern char PC;			/* pad character */
 
+static char ch;
+static int lastcmd, lastarg, lastp;
+static int lastcolon;
+char shell_line[SHELL_LINE];
+#define ringbell()	putcerr('\007')
+static char *BS = "\b";
+static char *BSB = "\b \b";
+static char *CARAT = "^";
+#define ERASEONECOLUMN \
+    if (docrterase) \
+	putserr(BSB); \
+    else \
+	putserr(BS);
+
 #ifdef HAVE_NCURSES_H
 # include <ncurses.h>
 #elif defined(HAVE_NCURSES_NCURSES_H)
@@ -337,193 +317,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-int main(int argc, char **argv)
-{
-	FILE *f;
-	char *s;
-	int ch;
-	int left;
-	int prnames = 0;
-	int initopt = 0;
-	int srchopt = 0;
-	int clearit = 0;
-	int initline = 0;
-	char *initbuf = NULL;
-
-	setlocale(LC_ALL, "");
-	bindtextdomain(PACKAGE, LOCALEDIR);
-	textdomain(PACKAGE);
-	atexit(close_stdout);
-
-	nfiles = argc;
-	fnames = argv;
-	setlocale(LC_ALL, "");
-	initterm();
-
-	/* Auto set no scroll on when binary is called page */
-	if (!(strcmp(program_invocation_short_name, "page")))
-		noscroll++;
-
-	prepare_line_buffer();
-
-	nscroll = Lpp / 2 - 1;
-	if (nscroll <= 0)
-		nscroll = 1;
-
-	if ((s = getenv("MORE")) != NULL)
-		argscan(s);
-
-	while (--nfiles > 0) {
-		if ((ch = (*++fnames)[0]) == '-') {
-			argscan(*fnames + 1);
-		} else if (ch == '+') {
-			s = *fnames;
-			if (*++s == '/') {
-				srchopt++;
-				initbuf = xstrdup(s + 1);
-			} else {
-				initopt++;
-				for (initline = 0; *s != '\0'; s++)
-					if (isdigit(*s))
-						initline =
-						    initline * 10 + *s - '0';
-				--initline;
-			}
-		} else
-			break;
-	}
-	/* allow clreol only if Home and eraseln and EodClr strings are
-	 * defined, and in that case, make sure we are in noscroll mode */
-	if (clreol) {
-		if ((Home == NULL) || (*Home == '\0') ||
-		    (eraseln == NULL) || (*eraseln == '\0') ||
-		    (EodClr == NULL) || (*EodClr == '\0'))
-			clreol = 0;
-		else
-			noscroll = 1;
-	}
-	if (dlines == 0)
-		dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
-	left = dlines;
-	if (nfiles > 1)
-		prnames++;
-	if (!no_intty && nfiles == 0)
-		usage(stderr);
-	else
-		f = stdin;
-	if (!no_tty) {
-		signal(SIGQUIT, onquit);
-		signal(SIGINT, end_it);
-#ifdef SIGWINCH
-		signal(SIGWINCH, chgwinsz);
-#endif
-		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
-			signal(SIGTSTP, onsusp);
-			catch_susp++;
-		}
-		stty(fileno(stderr), &otty);
-	}
-	if (no_intty) {
-		if (no_tty)
-			copy_file(stdin);
-		else {
-			if ((ch = Getc(f)) == '\f')
-				doclear();
-			else {
-				Ungetc(ch, f);
-				if (noscroll && (ch != EOF)) {
-					if (clreol)
-						home();
-					else
-						doclear();
-				}
-			}
-			if (srchopt) {
-				free(previousre);
-				previousre = xstrdup(initbuf);
-				search(initbuf, stdin, 1);
-				if (noscroll)
-					left--;
-			} else if (initopt)
-				skiplns(initline, stdin);
-			screen(stdin, left);
-		}
-		no_intty = 0;
-		prnames++;
-		firstf = 0;
-	}
-
-	while (fnum < nfiles) {
-		if ((f = checkf(fnames[fnum], &clearit)) != NULL) {
-			context.line = context.chrctr = 0;
-			Currline = 0;
-			if (firstf)
-				sigsetjmp(restore, 1);
-			if (firstf) {
-				firstf = 0;
-				if (srchopt) {
-					free(previousre);
-					previousre = xstrdup(initbuf);
-					search(initbuf, f, 1);
-					if (noscroll)
-						left--;
-				} else if (initopt)
-					skiplns(initline, f);
-			} else if (fnum < nfiles && !no_tty) {
-				sigsetjmp(restore, 1);
-				left = command(fnames[fnum], f);
-			}
-			if (left != 0) {
-				if ((noscroll || clearit)
-				    && (file_size != LONG_MAX)) {
-					if (clreol)
-						home();
-					else
-						doclear();
-				}
-				if (prnames) {
-					if (bad_so)
-						erasep(0);
-					if (clreol)
-						cleareol();
-					putsout("::::::::::::::");
-					if (promptlen > 14)
-						erasep(14);
-					putchar('\n');
-					if (clreol)
-						cleareol();
-					puts(fnames[fnum]);
-					if (clreol)
-						cleareol();
-					puts("::::::::::::::");
-					if (left > Lpp - 4)
-						left = Lpp - 4;
-				}
-				if (no_tty)
-					copy_file(f);
-				else {
-					within++;
-					screen(f, left);
-					within = 0;
-				}
-			}
-			sigsetjmp(restore, 1);
-			fflush(stdout);
-			fclose(f);
-			screen_start.line = screen_start.chrctr = 0L;
-			context.line = context.chrctr = 0L;
-		}
-		fnum++;
-		firstf = 0;
-	}
-	free(previousre);
-	free(initbuf);
-	free(Line);
-	reset_tty();
-	exit(EXIT_SUCCESS);
-}
-
-void argscan(char *s)
+static void argscan(char *s)
 {
 	int seen_num = 0;
 
@@ -583,9 +377,45 @@ void argscan(char *s)
 	}
 }
 
+/* force clear to end of line */
+static void cleareol(void)
+{
+	my_putstring(eraseln);
+}
+
+/* magic --
+ *	check for file magic numbers.  This code would best be shared
+ *	with the file(1) program or, perhaps, more should not try to be
+ *	so smart. */
+static int magic(FILE *f, char *fs)
+{
+	signed char twobytes[2];
+
+	/* don't try to look ahead if the input is unseekable */
+	if (fseek(f, 0L, SEEK_SET))
+		return 0;
+
+	if (fread(twobytes, 2, 1, f) == 1) {
+		switch (twobytes[0] + (twobytes[1] << 8)) {
+		case 0407:	/* a.out obj */
+		case 0410:	/* a.out exec */
+		case 0413:	/* a.out demand exec */
+		case 0405:
+		case 0411:
+		case 0177545:
+		case 0x457f:	/* simple ELF detection */
+			printf(_("\n******** %s: Not a text file ********\n\n"),
+			       fs);
+			return 1;
+		}
+	}
+	fseek(f, 0L, SEEK_SET);	/* rewind() not necessary */
+	return 0;
+}
+
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
-FILE *checkf(register char *fs, int *clearfirst)
+static FILE *checkf(register char *fs, int *clearfirst)
 {
 	struct stat stbuf;
 	register FILE *f;
@@ -620,222 +450,13 @@ FILE *checkf(register char *fs, int *clearfirst)
 	return (f);
 }
 
-/* magic --
- *	check for file magic numbers.  This code would best be shared
- *	with the file(1) program or, perhaps, more should not try to be
- *	so smart. */
-static int magic(FILE *f, char *fs)
+static void prepare_line_buffer(void)
 {
-	signed char twobytes[2];
+	char *nline;
+	size_t nsz = Mcol * 4;
 
-	/* don't try to look ahead if the input is unseekable */
-	if (fseek(f, 0L, SEEK_SET))
-		return 0;
-
-	if (fread(twobytes, 2, 1, f) == 1) {
-		switch (twobytes[0] + (twobytes[1] << 8)) {
-		case 0407:	/* a.out obj */
-		case 0410:	/* a.out exec */
-		case 0413:	/* a.out demand exec */
-		case 0405:
-		case 0411:
-		case 0177545:
-		case 0x457f:	/* simple ELF detection */
-			printf(_("\n******** %s: Not a text file ********\n\n"),
-			       fs);
-			return 1;
-		}
-	}
-	fseek(f, 0L, SEEK_SET);	/* rewind() not necessary */
-	return 0;
-}
-
-/* Print out the contents of the file f, one screenful at a time. */
-#define STOP -10
-void screen(register FILE *f, register int num_lines)
-{
-	register int c;
-	register int nchars;
-	int length;			/* length of current line */
-	static int prev_len = 1;	/* length of previous line */
-
-	for (;;) {
-		while (num_lines > 0 && !Pause) {
-			if ((nchars = get_line(f, &length)) == EOF) {
-				if (clreol)
-					clreos();
-				return;
-			}
-			if (ssp_opt && length == 0 && prev_len == 0)
-				continue;
-			prev_len = length;
-			if (bad_so
-			    || ((Senter && *Senter == ' ') && (promptlen > 0)))
-				erasep(0);
-			/* must clear before drawing line since tabs on
-			 * some terminals do not erase what they tab
-			 * over. */
-			if (clreol)
-				cleareol();
-			prbuf(Line, length);
-			if (nchars < promptlen)
-				erasep(nchars);	/* erasep () sets promptlen to 0 */
-			else
-				promptlen = 0;
-			/* is this needed?
-			 * if (clreol)
-			 *	cleareol();     * must clear again in case we wrapped *
-			 */
-			if (nchars < Mcol || !fold_opt)
-				prbuf("\n", 1);	/* will turn off UL if necessary */
-			if (nchars == STOP)
-				break;
-			num_lines--;
-		}
-		if (pstate) {
-			my_putstring(ULexit);
-			pstate = 0;
-		}
-		fflush(stdout);
-		if ((c = Getc(f)) == EOF) {
-			if (clreol)
-				clreos();
-			return;
-		}
-
-		if (Pause && clreol)
-			clreos();
-		Ungetc(c, f);
-		sigsetjmp(restore, 1);
-		Pause = 0;
-		startup = 0;
-		if ((num_lines = command(NULL, f)) == 0)
-			return;
-		if (hard && promptlen > 0)
-			erasep(0);
-		if (noscroll && num_lines >= dlines) {
-			if (clreol)
-				home();
-			else
-				doclear();
-		}
-		screen_start.line = Currline;
-		screen_start.chrctr = Ftell(f);
-	}
-}
-
-/* Come here if a quit signal is received */
-void onquit(int dummy __attribute__((__unused__)))
-{
-	signal(SIGQUIT, SIG_IGN);
-	if (!inwait) {
-		putchar('\n');
-		if (!startup) {
-			signal(SIGQUIT, onquit);
-			siglongjmp(restore, 1);
-		} else
-			Pause++;
-	} else if (!dum_opt && notell) {
-		promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
-		notell = 0;
-	}
-	signal(SIGQUIT, onquit);
-}
-
-/* Come here if a signal for a window size change is received */
-#ifdef SIGWINCH
-void chgwinsz(int dummy __attribute__((__unused__)))
-{
-	struct winsize win;
-
-	signal(SIGWINCH, SIG_IGN);
-	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
-		if (win.ws_row != 0) {
-			Lpp = win.ws_row;
-			nscroll = Lpp / 2 - 1;
-			if (nscroll <= 0)
-				nscroll = 1;
-			dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
-		}
-		if (win.ws_col != 0)
-			Mcol = win.ws_col;
-	}
-	signal(SIGWINCH, chgwinsz);
-}
-#endif				/* SIGWINCH */
-
-/* Clean up terminal state and exit. Also come here if interrupt signal received */
-void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
-{
-	reset_tty();
-	if (clreol) {
-		putchar('\r');
-		clreos();
-		fflush(stdout);
-	} else if (!clreol && (promptlen > 0)) {
-		kill_line();
-		fflush(stdout);
-	} else
-		putcerr('\n');
-	free(previousre);
-	free(Line);
-	_exit(EXIT_SUCCESS);
-}
-
-void copy_file(register FILE *f)
-{
-	char buf[BUFSIZ];
-	size_t sz;
-
-	while ((sz = fread(&buf, sizeof(char), sizeof(buf), f)) > 0)
-		fwrite(&buf, sizeof(char), sz, stdout);
-}
-
-#define ringbell()	putcerr('\007')
-
-static void prompt(char *filename)
-{
-	if (clreol)
-		cleareol();
-	else if (promptlen > 0)
-		kill_line();
-	if (!hard) {
-		promptlen = 0;
-		if (Senter && Sexit) {
-			my_putstring(Senter);
-			promptlen += (2 * soglitch);
-		}
-		if (clreol)
-			cleareol();
-		promptlen += printf(_("--More--"));
-		if (filename != NULL) {
-			promptlen += printf(_("(Next file: %s)"), filename);
-		} else if (!no_intty) {
-			promptlen +=
-			    printf("(%d%%)",
-				   (int)((file_pos * 100) / file_size));
-		}
-		if (dum_opt) {
-			promptlen +=
-			    printf(_("[Press space to continue, 'q' to quit.]"));
-		}
-		if (Senter && Sexit)
-			my_putstring(Sexit);
-		if (clreol)
-			clreos();
-		fflush(stdout);
-	} else
-		ringbell();
-	inwait++;
-}
-
-void prepare_line_buffer(void)
-{
-	char *nline;
-	size_t nsz = Mcol * 4;
-
-	if (LineLen >= nsz)
-		return;
+	if (LineLen >= nsz)
+		return;
 
 	if (nsz < LINSIZ)
 		nsz = LINSIZ;
@@ -847,7 +468,7 @@ void prepare_line_buffer(void)
 }
 
 /* Get a logical line */
-int get_line(register FILE *f, int *length)
+static int get_line(register FILE *f, int *length)
 {
 	int c;
 	char *p;
@@ -1058,7 +679,7 @@ int get_line(register FILE *f, int *length)
 }
 
 /* Erase the rest of the prompt, assuming we are starting at column col. */
-void erasep(register int col)
+static void erasep(register int col)
 {
 
 	if (promptlen == 0)
@@ -1076,26 +697,11 @@ void erasep(register int col)
 	promptlen = 0;
 }
 
-/* Erase the current line entirely */
-void kill_line(void)
-{
-	erasep(0);
-	if (!eraseln || dumb)
-		putchar('\r');
-}
-
-/* force clear to end of line */
-void cleareol(void)
-{
-	my_putstring(eraseln);
-}
-
-void clreos(void)
+static void clreos(void)
 {
 	my_putstring(EodClr);
 }
 
-
 static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 				  mbstate_t *mbstate)
 {
@@ -1106,7 +712,7 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 }
 
 /* Print a buffer of n characters */
-void prbuf(register char *s, register int n)
+static void prbuf(register char *s, register int n)
 {
 	register char c;	/* next output character */
 	register int state;	/* next output char's UL state */
@@ -1160,515 +766,414 @@ void prbuf(register char *s, register int n)
 		}
 }
 
-/*  Clear the screen */
-void doclear(void)
+/* Erase the current line entirely */
+static void kill_line(void)
 {
-	if (Clear && !hard) {
-		my_putstring(Clear);
-		/* Put out carriage return so that system doesn't get
-		 * confused by escape sequences when expanding tabs */
+	erasep(0);
+	if (!eraseln || dumb)
 		putchar('\r');
-		promptlen = 0;
-	}
 }
 
-/* Go to home position */
-void home(void)
+static void prompt(char *filename)
 {
-	my_putstring(Home);
+	if (clreol)
+		cleareol();
+	else if (promptlen > 0)
+		kill_line();
+	if (!hard) {
+		promptlen = 0;
+		if (Senter && Sexit) {
+			my_putstring(Senter);
+			promptlen += (2 * soglitch);
+		}
+		if (clreol)
+			cleareol();
+		promptlen += printf(_("--More--"));
+		if (filename != NULL) {
+			promptlen += printf(_("(Next file: %s)"), filename);
+		} else if (!no_intty) {
+			promptlen +=
+			    printf("(%d%%)",
+				   (int)((file_pos * 100) / file_size));
+		}
+		if (dum_opt) {
+			promptlen +=
+			    printf(_("[Press space to continue, 'q' to quit.]"));
+		}
+		if (Senter && Sexit)
+			my_putstring(Sexit);
+		if (clreol)
+			clreos();
+		fflush(stdout);
+	} else
+		ringbell();
+	inwait++;
 }
 
-static int lastcmd, lastarg, lastp;
-static int lastcolon;
-char shell_line[SHELL_LINE];
+static int ourputch(int c)
+{
+	return putc(c, stdout);
+}
 
-/* Read a command and do it.  A command consists of an optional integer
- * argument followed by the command character.  Return the number of
- * lines to display in the next screenful.  If there is nothing more to
- * display in the current file, zero is returned. */
-int command(char *filename, register FILE *f)
+static void reset_tty(void)
 {
-	register int nlines;
-	register int retval = 0;
-	register int c;
-	char colonch;
-	int done;
-	char comchar, cmdbuf[INIT_BUF];
+	if (no_tty)
+		return;
+	if (pstate) {
+		/* putchar - if that isn't a macro */
+		tputs(ULexit, fileno(stdout), ourputch);
+		fflush(stdout);
+		pstate = 0;
+	}
+	otty.c_lflag |= ICANON | ECHO;
+	otty.c_cc[VMIN] = savetty0.c_cc[VMIN];
+	otty.c_cc[VTIME] = savetty0.c_cc[VTIME];
+	stty(fileno(stderr), &savetty0);
+}
 
-#define ret(val) retval=val;done++;break
+/* Clean up terminal state and exit. Also come here if interrupt signal received */
+static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
+{
+	reset_tty();
+	if (clreol) {
+		putchar('\r');
+		clreos();
+		fflush(stdout);
+	} else if (!clreol && (promptlen > 0)) {
+		kill_line();
+		fflush(stdout);
+	} else
+		putcerr('\n');
+	free(previousre);
+	free(Line);
+	_exit(EXIT_SUCCESS);
+}
 
-	done = 0;
-	if (!errors)
-		prompt(filename);
-	else
-		errors = 0;
+static int readch(void)
+{
+	unsigned char c;
+
+	errno = 0;
+	if (read(fileno(stderr), &c, 1) <= 0) {
+		if (errno != EINTR)
+			end_it(0);
+		else
+			c = otty.c_cc[VKILL];
+	}
+	return (c);
+}
+
+/* Read a decimal number from the terminal.  Set cmd to the non-digit
+ * which terminates the number. */
+static int number(char *cmd)
+{
+	register int i;
+
+	i = 0;
+	ch = otty.c_cc[VKILL];
 	for (;;) {
-		nlines = number(&comchar);
-		lastp = colonch = 0;
-		if (comchar == '.') {	/* Repeat last command */
-			lastp++;
-			comchar = lastcmd;
-			nlines = lastarg;
-			if (lastcmd == ':')
-				colonch = lastcolon;
-		}
-		lastcmd = comchar;
-		lastarg = nlines;
-		if ((cc_t) comchar == otty.c_cc[VERASE]) {
-			kill_line();
-			prompt(filename);
-			continue;
-		}
-		switch (comchar) {
-		case ':':
-			retval = colon(filename, colonch, nlines);
-			if (retval >= 0)
-				done++;
+		ch = readch();
+		if (isdigit(ch))
+			i = i * 10 + ch - '0';
+		else if ((cc_t) ch == otty.c_cc[VKILL])
+			i = 0;
+		else {
+			*cmd = ch;
 			break;
-		case 'b':
-		case ctrl('B'):
-			{
-				register int initline;
+		}
+	}
+	return (i);
+}
 
-				if (no_intty) {
-					ringbell();
-					return (-1);
-				}
+/* Skip nskip files in the file list (from the command line).  Nskip may
+ * be negative. */
+static void skipf(register int nskip)
+{
+	if (nskip == 0)
+		return;
+	if (nskip > 0) {
+		if (fnum + nskip > nfiles - 1)
+			nskip = nfiles - fnum - 1;
+	} else if (within)
+		++fnum;
+	fnum += nskip;
+	if (fnum < 0)
+		fnum = 0;
+	puts(_("\n...Skipping "));
+	if (clreol)
+		cleareol();
+	if (nskip > 0)
+		putsout(_("...Skipping to file "));
+	else
+		putsout(_("...Skipping back to file "));
+	puts(fnames[fnum]);
+	if (clreol)
+		cleareol();
+	putchar('\n');
+	--fnum;
+}
 
-				if (nlines == 0)
-					nlines++;
+static void show(char c)
+{
+	if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
+		c += (c == RUBOUT) ? -0100 : 0100;
+		putserr(CARAT);
+		promptlen++;
+	}
+	putcerr(c);
+	promptlen++;
+}
 
-				putchar('\r');
-				erasep(0);
-				putchar('\n');
-				if (clreol)
-					cleareol();
-				printf(P_("...back %d page",
-					"...back %d pages", nlines),
-					nlines);
-				if (clreol)
-					cleareol();
-				putchar('\n');
+static void more_error(char *mess)
+{
+	if (clreol)
+		cleareol();
+	else
+		kill_line();
+	promptlen += strlen(mess);
+	if (Senter && Sexit) {
+		my_putstring(Senter);
+		putsout(mess);
+		my_putstring(Sexit);
+	} else
+		putsout(mess);
+	fflush(stdout);
+	errors++;
+	siglongjmp(restore, 1);
+}
 
-				initline = Currline - dlines * (nlines + 1);
-				if (!noscroll)
-					--initline;
-				if (initline < 0)
-					initline = 0;
-				Fseek(f, 0L);
-				Currline = 0;	/* skiplns() will make Currline correct */
-				skiplns(initline, f);
-				if (!noscroll) {
-					ret(dlines + 1);
-				} else {
-					ret(dlines);
-				}
-			}
-		case ' ':
-		case 'z':
-			if (nlines == 0)
-				nlines = dlines;
-			else if (comchar == 'z')
-				dlines = nlines;
-			ret(nlines);
-		case 'd':
-		case ctrl('D'):
-			if (nlines != 0)
-				nscroll = nlines;
-			ret(nscroll);
-		case 'q':
-		case 'Q':
-			end_it(0);
-		case 's':
-		case 'f':
-		case ctrl('F'):
-			if (nlines == 0)
-				nlines++;
-			if (comchar == 'f')
-				nlines *= dlines;
-			putchar('\r');
-			erasep(0);
-			putchar('\n');
-			if (clreol)
-				cleareol();
-			printf(P_("...skipping %d line",
-				"...skipping %d lines", nlines),
-				nlines);
+static void ttyin(char buf[], register int nmax, char pchar)
+{
+	char *sp;
+	int c;
+	int slash = 0;
+	int maxlen;
 
-			if (clreol)
-				cleareol();
-			putchar('\n');
+	sp = buf;
+	maxlen = 0;
+	while (sp - buf < nmax) {
+		if (promptlen > maxlen)
+			maxlen = promptlen;
+		c = readch();
+		if (c == '\\') {
+			slash++;
+		} else if (((cc_t) c == otty.c_cc[VERASE]) && !slash) {
+			if (sp > buf) {
+#ifdef HAVE_WIDECHAR
+				if (MB_CUR_MAX > 1) {
+					wchar_t wc;
+					size_t pos = 0, mblength;
+					mbstate_t state, state_bak;
 
-			while (nlines > 0) {
-				while ((c = Getc(f)) != '\n')
-					if (c == EOF) {
-						retval = 0;
-						done++;
-						goto endsw;
-					}
-				Currline++;
-				nlines--;
-			}
-			ret(dlines);
-		case '\n':
-			if (nlines != 0)
-				dlines = nlines;
-			else
-				nlines = 1;
-			ret(nlines);
-		case '\f':
-			if (!no_intty) {
-				doclear();
-				Fseek(f, screen_start.chrctr);
-				Currline = screen_start.line;
-				ret(dlines);
-			} else {
-				ringbell();
-				break;
-			}
-		case '\'':
-			if (!no_intty) {
-				kill_line();
-				putsout(_("\n***Back***\n\n"));
-				Fseek(f, context.chrctr);
-				Currline = context.line;
-				ret(dlines);
-			} else {
-				ringbell();
-				break;
-			}
-		case '=':
-			kill_line();
-			promptlen = printf("%d", Currline);
-			fflush(stdout);
-			break;
-		case 'n':
-			if (!previousre) {
-				more_error(_("No previous regular expression"));
-				break;
-			}
-			lastp++;
-			/* fall through */
-		case '/':
-			if (nlines == 0)
-				nlines++;
-			kill_line();
-			putchar('/');
-			promptlen = 1;
-			fflush(stdout);
-			if (lastp) {
-				putcerr('\r');
-				search(previousre, f, nlines);
-			} else {
-				ttyin(cmdbuf, sizeof(cmdbuf) - 2, '/');
-				putcerr('\r');
-				free(previousre);
-				previousre = xstrdup(cmdbuf);
-				search(cmdbuf, f, nlines);
-			}
-			ret(dlines - 1);
-		case '!':
-			do_shell(filename);
-			break;
-		case '?':
-		case 'h':
-			if (noscroll)
-				doclear();
-			putsout(_("\n"
-				  "Most commands optionally preceded by integer argument k.  "
-				  "Defaults in brackets.\n"
-				  "Star (*) indicates argument becomes new default.\n"));
-			puts("---------------------------------------"
-			     "----------------------------------------");
-			putsout(_
-				("<space>                 Display next k lines of text [current screen size]\n"
-				 "z                       Display next k lines of text [current screen size]*\n"
-				 "<return>                Display next k lines of text [1]*\n"
-				 "d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
-				 "q or Q or <interrupt>   Exit from more\n"
-				 "s                       Skip forward k lines of text [1]\n"
-				 "f                       Skip forward k screenfuls of text [1]\n"
-				 "b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
-				 "'                       Go to place where previous search started\n"
-				 "=                       Display current line number\n"
-				 "/<regular expression>   Search for kth occurrence of regular expression [1]\n"
-				 "n                       Search for kth occurrence of last r.e [1]\n"
-				 "!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
-				 "v                       Start up /usr/bin/vi at current line\n"
-				 "ctrl-L                  Redraw screen\n"
-				 ":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"));
-			puts("---------------------------------------"
-			     "----------------------------------------");
-			prompt(filename);
-			break;
-		case 'v':	/* This case should go right before default */
-			if (!no_intty) {
-				/* Earlier: call vi +n file. This also
-				 * works for emacs.  POSIX: call vi -c n
-				 * file (when editor is vi or ex). */
-				char *editor, *p;
-				int n = (Currline - dlines <= 0 ? 1 :
-					 Currline - (dlines + 1) / 2);
-				int split = 0;
+					memset(&state, '\0', sizeof(mbstate_t));
 
-				editor = getenv("VISUAL");
-				if (editor == NULL || *editor == '\0')
-					editor = getenv("EDITOR");
-				if (editor == NULL || *editor == '\0')
-					editor = VI;
+					while (1) {
+						state_bak = state;
+						mblength =
+						    mbrtowc(&wc, buf + pos,
+							    sp - buf, &state);
 
-				p = strrchr(editor, '/');
-				if (p)
-					p++;
-				else
-					p = editor;
-				if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
-					sprintf(cmdbuf, "-c %d", n);
-					split = 1;
-				} else {
-					sprintf(cmdbuf, "+%d", n);
-				}
+						state = (mblength == (size_t)-2
+							 || mblength ==
+							 (size_t)-1) ? state_bak
+						    : state;
+						mblength =
+						    (mblength == (size_t)-2
+						     || mblength == (size_t)-1
+						     || mblength ==
+						     0) ? 1 : mblength;
 
-				kill_line();
-				printf("%s %s %s", editor, cmdbuf,
-				       fnames[fnum]);
-				if (split) {
-					cmdbuf[2] = 0;
-					execute(filename, editor, editor,
-						cmdbuf, cmdbuf + 3,
-						fnames[fnum], (char *)0);
+						if (buf + pos + mblength >= sp)
+							break;
+
+						pos += mblength;
+					}
+
+					if (mblength == 1) {
+					ERASEONECOLUMN} else {
+						int wc_width;
+						wc_width = wcwidth(wc);
+						wc_width =
+						    (wc_width <
+						     1) ? 1 : wc_width;
+						while (wc_width--) {
+						ERASEONECOLUMN}
+					}
+
+					while (mblength--) {
+						--promptlen;
+						--sp;
+					}
 				} else
-					execute(filename, editor, editor,
-						cmdbuf, fnames[fnum],
-						(char *)0);
-				break;
+#endif	/* HAVE_WIDECHAR */
+				{
+					--promptlen;
+					ERASEONECOLUMN-- sp;
+				}
+
+				if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) {
+					--promptlen;
+				ERASEONECOLUMN}
+				continue;
+			} else {
+				if (!eraseln)
+					promptlen = maxlen;
+				siglongjmp(restore, 1);
 			}
-			/* fall through */
-		default:
-			if (dum_opt) {
-				kill_line();
-				if (Senter && Sexit) {
-					my_putstring(Senter);
-					promptlen =
-					    printf(_
-						   ("[Press 'h' for instructions.]"))
-					    + 2 * soglitch;
-					my_putstring(Sexit);
-				} else
-					promptlen =
-					    printf(_
-						   ("[Press 'h' for instructions.]"));
-				fflush(stdout);
-			} else
-				ringbell();
-			break;
+		} else if (((cc_t) c == otty.c_cc[VKILL]) && !slash) {
+			if (hard) {
+				show(c);
+				putchar('\n');
+				putchar(pchar);
+			} else {
+				putchar('\r');
+				putchar(pchar);
+				if (eraseln)
+					erasep(1);
+				else if (docrtkill)
+					while (promptlen-- > 1)
+						putserr(BSB);
+				promptlen = 1;
+			}
+			sp = buf;
+			fflush(stdout);
+			continue;
 		}
-		if (done)
+		if (slash && ((cc_t) c == otty.c_cc[VKILL]
+			      || (cc_t) c == otty.c_cc[VERASE])) {
+			ERASEONECOLUMN-- sp;
+		}
+		if (c != '\\')
+			slash = 0;
+		*sp++ = c;
+		if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
+			c += (c == RUBOUT) ? -0100 : 0100;
+			putserr(CARAT);
+			promptlen++;
+		}
+		if (c != '\n' && c != ESC) {
+			putcerr(c);
+			promptlen++;
+		} else
 			break;
 	}
-	putchar('\r');
- endsw:
-	inwait = 0;
-	notell++;
-	return (retval);
+	*--sp = '\0';
+	if (!eraseln)
+		promptlen = maxlen;
+	if (sp - buf >= nmax - 1)
+		more_error(_("Line too long"));
 }
 
-static char ch;
-/* Execute a colon-prefixed command.  Returns <0 if not a command that
- * should cause more of the file to be printed. */
-int colon(char *filename, int cmd, int nlines)
+/* return: 0 - unchanged, 1 - changed, -1 - overflow (unchanged) */
+static int expand(char **outbuf, char *inbuf)
 {
-	if (cmd == 0)
-		ch = readch();
-	else
-		ch = cmd;
-	lastcolon = ch;
-	switch (ch) {
-	case 'f':
-		kill_line();
-		if (!no_intty)
-			promptlen =
-			    printf(_("\"%s\" line %d"), fnames[fnum], Currline);
-		else
-			promptlen = printf(_("[Not a file] line %d"), Currline);
-		fflush(stdout);
-		return (-1);
-	case 'n':
-		if (nlines == 0) {
-			if (fnum >= nfiles - 1)
-				end_it(0);
-			nlines++;
-		}
-		putchar('\r');
-		erasep(0);
-		skipf(nlines);
-		return (0);
-	case 'p':
-		if (no_intty) {
-			ringbell();
-			return (-1);
+	char *inpstr;
+	char *outstr;
+	char c;
+	char *temp;
+	int changed = 0;
+	int tempsz, xtra, offset;
+
+	xtra = strlen(fnames[fnum]) + strlen(shell_line) + 1;
+	tempsz = 200 + xtra;
+	temp = xmalloc(tempsz);
+	inpstr = inbuf;
+	outstr = temp;
+	while ((c = *inpstr++) != '\0') {
+		offset = outstr - temp;
+		if (tempsz - offset - 1 < xtra) {
+			tempsz += 200 + xtra;
+			temp = xrealloc(temp, tempsz);
+			outstr = temp + offset;
+		}
+		switch (c) {
+		case '%':
+			if (!no_intty) {
+				strcpy(outstr, fnames[fnum]);
+				outstr += strlen(fnames[fnum]);
+				changed++;
+			} else
+				*outstr++ = c;
+			break;
+		case '!':
+			if (!shellp)
+				more_error(_
+					   ("No previous command to substitute for"));
+			strcpy(outstr, shell_line);
+			outstr += strlen(shell_line);
+			changed++;
+			break;
+		case '\\':
+			if (*inpstr == '%' || *inpstr == '!') {
+				*outstr++ = *inpstr++;
+				break;
+			}
+		default:
+			*outstr++ = c;
 		}
-		putchar('\r');
-		erasep(0);
-		if (nlines == 0)
-			nlines++;
-		skipf(-nlines);
-		return (0);
-	case '!':
-		do_shell(filename);
-		return (-1);
-	case 'q':
-	case 'Q':
-		end_it(0);
-	default:
-		ringbell();
-		return (-1);
 	}
+	*outstr++ = '\0';
+	*outbuf = temp;
+	return (changed);
 }
 
-/* Read a decimal number from the terminal.  Set cmd to the non-digit
- * which terminates the number. */
-int number(char *cmd)
+static void set_tty(void)
 {
-	register int i;
+	otty.c_lflag &= ~(ICANON | ECHO);
+	otty.c_cc[VMIN] = 1;	/* read at least 1 char */
+	otty.c_cc[VTIME] = 0;	/* no timeout */
+	stty(fileno(stderr), &otty);
+}
 
-	i = 0;
-	ch = otty.c_cc[VKILL];
-	for (;;) {
-		ch = readch();
-		if (isdigit(ch))
-			i = i * 10 + ch - '0';
-		else if ((cc_t) ch == otty.c_cc[VKILL])
-			i = 0;
-		else {
-			*cmd = ch;
-			break;
-		}
+/* Come here if a quit signal is received */
+static void onquit(int dummy __attribute__((__unused__)))
+{
+	signal(SIGQUIT, SIG_IGN);
+	if (!inwait) {
+		putchar('\n');
+		if (!startup) {
+			signal(SIGQUIT, onquit);
+			siglongjmp(restore, 1);
+		} else
+			Pause++;
+	} else if (!dum_opt && notell) {
+		promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
+		notell = 0;
 	}
-	return (i);
+	signal(SIGQUIT, onquit);
 }
 
-void do_shell(char *filename)
+/* Come here when we get a suspend signal from the terminal */
+static void onsusp(int dummy __attribute__((__unused__)))
 {
-	char cmdbuf[COMMAND_BUF];
-	int rc;
-	char *expanded;
+	sigset_t signals, oldmask;
 
-	kill_line();
-	putchar('!');
-	fflush(stdout);
-	promptlen = 1;
-	if (lastp)
-		putsout(shell_line);
-	else {
-		ttyin(cmdbuf, sizeof(cmdbuf) - 2, '!');
-		expanded = 0;
-		rc = expand(&expanded, cmdbuf);
-		if (expanded) {
-			if (strlen(expanded) < sizeof(shell_line))
-				strcpy(shell_line, expanded);
-			else
-				rc = -1;
-			free(expanded);
-		}
-		if (rc < 0) {
-			putserr(_("  Overflow\n"));
-			prompt(filename);
-			return;
-		} else if (rc > 0) {
-			kill_line();
-			promptlen = printf("!%s", shell_line);
-		}
-	}
+	/* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
+	signal(SIGTTOU, SIG_IGN);
+	reset_tty();
 	fflush(stdout);
-	putcerr('\n');
-	promptlen = 0;
-	shellp = 1;
-	execute(filename, shell, shell, "-c", shell_line, 0);
-}
+	signal(SIGTTOU, SIG_DFL);
+	/* Send the TSTP signal to suspend our process group */
+	signal(SIGTSTP, SIG_DFL);
 
-/* Search for nth occurrence of regular expression contained in buf in
- * the file */
-void search(char buf[], FILE *file, register int n)
-{
-	long startline = Ftell(file);
-	register long line1 = startline;
-	register long line2 = startline;
-	register long line3;
-	register int lncount;
-	int saveln, rc;
-	regex_t re;
+	/* unblock SIGTSTP or we won't be able to suspend ourself */
+	sigemptyset(&signals);
+	sigaddset(&signals, SIGTSTP);
+	sigprocmask(SIG_UNBLOCK, &signals, &oldmask);
 
-	context.line = saveln = Currline;
-	context.chrctr = startline;
-	lncount = 0;
-	if (!buf)
-		goto notfound;
-	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
-		char s[REGERR_BUF];
-		regerror(rc, &re, s, sizeof s);
-		more_error(s);
-	}
-	while (!feof(file)) {
-		line3 = line2;
-		line2 = line1;
-		line1 = Ftell(file);
-		rdline(file);
-		lncount++;
-		if (regexec(&re, Line, 0, NULL, 0) == 0) {
-			if (--n == 0) {
-				if (lncount > 3 || (lncount > 1 && no_intty)) {
-					putchar('\n');
-					if (clreol)
-						cleareol();
-					putsout(_("...skipping\n"));
-				}
-				if (!no_intty) {
-					Currline -=
-					    (lncount >= 3 ? 3 : lncount);
-					Fseek(file, line3);
-					if (noscroll) {
-						if (clreol) {
-							home();
-							cleareol();
-						} else
-							doclear();
-					}
-				} else {
-					kill_line();
-					if (noscroll) {
-						if (clreol) {
-							home();
-							cleareol();
-						} else
-							doclear();
-					}
-					puts(Line);
-				}
-				break;
-			}
-		}
-	}
-	regfree(&re);
-	if (feof(file)) {
-		if (!no_intty) {
-			Currline = saveln;
-			Fseek(file, startline);
-		} else {
-			putsout(_("\nPattern not found\n"));
-			end_it(0);
-		}
-		free(previousre);
-		previousre = NULL;
-notfound:
-		more_error(_("Pattern not found"));
-	}
+	kill(0, SIGTSTP);
+	/* Pause for station break */
+
+	sigprocmask(SIG_SETMASK, &oldmask, NULL);
+
+	/* We're back */
+	signal(SIGTSTP, onsusp);
+	set_tty();
+	if (inwait)
+		siglongjmp(restore, 1);
 }
 
-void execute(char *filename, char *cmd, ...)
+static void execute(char *filename, char *cmd, ...)
 {
 	int id;
 	int n;
@@ -1730,479 +1235,937 @@ void execute(char *filename, char *cmd, ...)
 	prompt(filename);
 }
 
-/* Skip n lines in the file f */
-void skiplns(register int n, register FILE *f)
+static void do_shell(char *filename)
 {
-	register int c;
-
-	while (n > 0) {
-		while ((c = Getc(f)) != '\n')
-			if (c == EOF)
-				return;
-		n--;
-		Currline++;
-	}
-}
+	char cmdbuf[COMMAND_BUF];
+	int rc;
+	char *expanded;
 
-/* Skip nskip files in the file list (from the command line).  Nskip may
- * be negative. */
-void skipf(register int nskip)
-{
-	if (nskip == 0)
-		return;
-	if (nskip > 0) {
-		if (fnum + nskip > nfiles - 1)
-			nskip = nfiles - fnum - 1;
-	} else if (within)
-		++fnum;
-	fnum += nskip;
-	if (fnum < 0)
-		fnum = 0;
-	puts(_("\n...Skipping "));
-	if (clreol)
-		cleareol();
-	if (nskip > 0)
-		putsout(_("...Skipping to file "));
-	else
-		putsout(_("...Skipping back to file "));
-	puts(fnames[fnum]);
-	if (clreol)
-		cleareol();
-	putchar('\n');
-	--fnum;
+	kill_line();
+	putchar('!');
+	fflush(stdout);
+	promptlen = 1;
+	if (lastp)
+		putsout(shell_line);
+	else {
+		ttyin(cmdbuf, sizeof(cmdbuf) - 2, '!');
+		expanded = 0;
+		rc = expand(&expanded, cmdbuf);
+		if (expanded) {
+			if (strlen(expanded) < sizeof(shell_line))
+				strcpy(shell_line, expanded);
+			else
+				rc = -1;
+			free(expanded);
+		}
+		if (rc < 0) {
+			putserr(_("  Overflow\n"));
+			prompt(filename);
+			return;
+		} else if (rc > 0) {
+			kill_line();
+			promptlen = printf("!%s", shell_line);
+		}
+	}
+	fflush(stdout);
+	putcerr('\n');
+	promptlen = 0;
+	shellp = 1;
+	execute(filename, shell, shell, "-c", shell_line, 0);
 }
 
-/*----------------------------- Terminal I/O -------------------------------*/
-void initterm(void)
+/* Execute a colon-prefixed command.  Returns <0 if not a command that
+ * should cause more of the file to be printed. */
+static int colon(char *filename, int cmd, int nlines)
 {
-	int ret;
-	char *padstr;
-	char *term;
-	struct winsize win;
-
-#ifdef do_SIGTTOU
- retry:
-#endif
-
-#ifndef NON_INTERACTIVE_MORE
-	no_tty = tcgetattr(fileno(stdout), &otty);
-#endif
-	if (!no_tty) {
-		docrterase = (otty.c_cc[VERASE] != 255);
-		docrtkill = (otty.c_cc[VKILL] != 255);
-#ifdef do_SIGTTOU
-		{
-			int tgrp;
-			/* Wait until we're in the foreground before we
-			 * save the terminal modes. */
-			if ((tgrp = tcgetpgrp(fileno(stdout))) < 0)
-				err(EXIT_FAILURE, "tcgetpgrp");
-			if (tgrp != getpgrp(0)) {
-				kill(0, SIGTTOU);
-				goto retry;
-			}
-		}
-#endif	/* do_SIGTTOU */
-		if ((term = getenv("TERM")) == NULL) {
-			dumb++;
-			ul_opt = 0;
+	if (cmd == 0)
+		ch = readch();
+	else
+		ch = cmd;
+	lastcolon = ch;
+	switch (ch) {
+	case 'f':
+		kill_line();
+		if (!no_intty)
+			promptlen =
+			    printf(_("\"%s\" line %d"), fnames[fnum], Currline);
+		else
+			promptlen = printf(_("[Not a file] line %d"), Currline);
+		fflush(stdout);
+		return (-1);
+	case 'n':
+		if (nlines == 0) {
+			if (fnum >= nfiles - 1)
+				end_it(0);
+			nlines++;
 		}
-		my_setupterm(term, 1, &ret);
-		if (ret <= 0) {
-			dumb++;
-			ul_opt = 0;
-		} else {
-#ifdef TIOCGWINSZ
-			if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
-#endif
-				Lpp = my_tgetnum(TERM_LINES);
-				Mcol = my_tgetnum(TERM_COLS);
-#ifdef TIOCGWINSZ
-			} else {
-				if ((Lpp = win.ws_row) == 0)
-					Lpp = my_tgetnum(TERM_LINES);
-				if ((Mcol = win.ws_col) == 0)
-					Mcol = my_tgetnum(TERM_COLS);
-			}
-#endif
-			if ((Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) {
-				hard++;	/* Hard copy terminal */
-				Lpp = LINES_PER_PAGE;
-			}
-
-			if (my_tgetflag(TERM_EAT_NEW_LINE))
-				/* Eat newline at last column + 1; dec, concept */
-				eatnl++;
-			if (Mcol <= 0)
-				Mcol = NUM_COLUMNS;
-
-			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;
-
-			/* Set up for underlining:  some terminals don't
-			 * need it; others have start/stop sequences,
-			 * still others have an underline char sequence
-			 * which is assumed to move the cursor forward
-			 * one character.  If underline sequence isn't
-			 * available, settle for standout sequence. */
-			if (my_tgetflag(TERM_UNDERLINE)
-			    || my_tgetflag(TERM_OVER_STRIKE))
-				ul_opt = 0;
-			if ((chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == 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 = "";
-				} else
-					ulglitch = soglitch;
-			} else {
-				ulglitch = 0;
-			}
-
-			if ((padstr = my_tgetstr(TERM_PAD_CHAR)) != NULL)
-				PC = *padstr;
-			Home = my_tgetstr(TERM_HOME);
-			if (Home == NULL || *Home == '\0') {
-				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(TERM_CLEAR_TO_SCREEN_END);
-			if ((chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL)
-				chBS = "\b";
-
+		putchar('\r');
+		erasep(0);
+		skipf(nlines);
+		return (0);
+	case 'p':
+		if (no_intty) {
+			ringbell();
+			return (-1);
 		}
-		if ((shell = getenv("SHELL")) == NULL)
-			shell = "/bin/sh";
-	}
-	no_intty = tcgetattr(fileno(stdin), &otty);
-	tcgetattr(fileno(stderr), &otty);
-	savetty0 = otty;
-	slow_tty = cfgetispeed(&otty) < B1200;
-	hardtabs = (otty.c_oflag & TABDLY) != XTABS;
-	if (!no_tty) {
-		otty.c_lflag &= ~(ICANON | ECHO);
-		otty.c_cc[VMIN] = 1;
-		otty.c_cc[VTIME] = 0;
+		putchar('\r');
+		erasep(0);
+		if (nlines == 0)
+			nlines++;
+		skipf(-nlines);
+		return (0);
+	case '!':
+		do_shell(filename);
+		return (-1);
+	case 'q':
+	case 'Q':
+		end_it(0);
+	default:
+		ringbell();
+		return (-1);
 	}
 }
 
-int readch(void)
+/* Skip n lines in the file f */
+static void skiplns(register int n, register FILE *f)
 {
-	unsigned char c;
+	register int c;
 
-	errno = 0;
-	if (read(fileno(stderr), &c, 1) <= 0) {
-		if (errno != EINTR)
-			end_it(0);
-		else
-			c = otty.c_cc[VKILL];
+	while (n > 0) {
+		while ((c = Getc(f)) != '\n')
+			if (c == EOF)
+				return;
+		n--;
+		Currline++;
 	}
-	return (c);
 }
 
-static char *BS = "\b";
-static char *BSB = "\b \b";
-static char *CARAT = "^";
-#define ERASEONECOLUMN \
-    if (docrterase) \
-	putserr(BSB); \
-    else \
-	putserr(BS);
+/*  Clear the screen */
+static void doclear(void)
+{
+	if (Clear && !hard) {
+		my_putstring(Clear);
+		/* Put out carriage return so that system doesn't get
+		 * confused by escape sequences when expanding tabs */
+		putchar('\r');
+		promptlen = 0;
+	}
+}
 
-void ttyin(char buf[], register int nmax, char pchar)
+static void rdline(register FILE *f)
 {
-	char *sp;
-	int c;
-	int slash = 0;
-	int maxlen;
-
-	sp = buf;
-	maxlen = 0;
-	while (sp - buf < nmax) {
-		if (promptlen > maxlen)
-			maxlen = promptlen;
-		c = readch();
-		if (c == '\\') {
-			slash++;
-		} else if (((cc_t) c == otty.c_cc[VERASE]) && !slash) {
-			if (sp > buf) {
-#ifdef HAVE_WIDECHAR
-				if (MB_CUR_MAX > 1) {
-					wchar_t wc;
-					size_t pos = 0, mblength;
-					mbstate_t state, state_bak;
+	register int c;
+	register char *p;
 
-					memset(&state, '\0', sizeof(mbstate_t));
+	prepare_line_buffer();
 
-					while (1) {
-						state_bak = state;
-						mblength =
-						    mbrtowc(&wc, buf + pos,
-							    sp - buf, &state);
+	p = Line;
+	while ((c = Getc(f)) != '\n' && c != EOF
+	       && (size_t)(p - Line) < LineLen - 1)
+		*p++ = c;
+	if (c == '\n')
+		Currline++;
+	*p = '\0';
+}
 
-						state = (mblength == (size_t)-2
-							 || mblength ==
-							 (size_t)-1) ? state_bak
-						    : state;
-						mblength =
-						    (mblength == (size_t)-2
-						     || mblength == (size_t)-1
-						     || mblength ==
-						     0) ? 1 : mblength;
+/* Go to home position */
+static void home(void)
+{
+	my_putstring(Home);
+}
 
-						if (buf + pos + mblength >= sp)
-							break;
+/* Search for nth occurrence of regular expression contained in buf in
+ * the file */
+static void search(char buf[], FILE *file, register int n)
+{
+	long startline = Ftell(file);
+	register long line1 = startline;
+	register long line2 = startline;
+	register long line3;
+	register int lncount;
+	int saveln, rc;
+	regex_t re;
 
-						pos += mblength;
+	context.line = saveln = Currline;
+	context.chrctr = startline;
+	lncount = 0;
+	if (!buf)
+		goto notfound;
+	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
+		char s[REGERR_BUF];
+		regerror(rc, &re, s, sizeof s);
+		more_error(s);
+	}
+	while (!feof(file)) {
+		line3 = line2;
+		line2 = line1;
+		line1 = Ftell(file);
+		rdline(file);
+		lncount++;
+		if (regexec(&re, Line, 0, NULL, 0) == 0) {
+			if (--n == 0) {
+				if (lncount > 3 || (lncount > 1 && no_intty)) {
+					putchar('\n');
+					if (clreol)
+						cleareol();
+					putsout(_("...skipping\n"));
+				}
+				if (!no_intty) {
+					Currline -=
+					    (lncount >= 3 ? 3 : lncount);
+					Fseek(file, line3);
+					if (noscroll) {
+						if (clreol) {
+							home();
+							cleareol();
+						} else
+							doclear();
 					}
-
-					if (mblength == 1) {
-					ERASEONECOLUMN} else {
-						int wc_width;
-						wc_width = wcwidth(wc);
-						wc_width =
-						    (wc_width <
-						     1) ? 1 : wc_width;
-						while (wc_width--) {
-						ERASEONECOLUMN}
+				} else {
+					kill_line();
+					if (noscroll) {
+						if (clreol) {
+							home();
+							cleareol();
+						} else
+							doclear();
 					}
+					puts(Line);
+				}
+				break;
+			}
+		}
+	}
+	regfree(&re);
+	if (feof(file)) {
+		if (!no_intty) {
+			Currline = saveln;
+			Fseek(file, startline);
+		} else {
+			putsout(_("\nPattern not found\n"));
+			end_it(0);
+		}
+		free(previousre);
+		previousre = NULL;
+notfound:
+		more_error(_("Pattern not found"));
+	}
+}
 
-					while (mblength--) {
-						--promptlen;
-						--sp;
-					}
-				} else
-#endif	/* HAVE_WIDECHAR */
-				{
-					--promptlen;
-					ERASEONECOLUMN-- sp;
+/* Read a command and do it.  A command consists of an optional integer
+ * argument followed by the command character.  Return the number of
+ * lines to display in the next screenful.  If there is nothing more to
+ * display in the current file, zero is returned. */
+static int command(char *filename, register FILE *f)
+{
+	register int nlines;
+	register int retval = 0;
+	register int c;
+	char colonch;
+	int done;
+	char comchar, cmdbuf[INIT_BUF];
+
+#define ret(val) retval=val;done++;break
+
+	done = 0;
+	if (!errors)
+		prompt(filename);
+	else
+		errors = 0;
+	for (;;) {
+		nlines = number(&comchar);
+		lastp = colonch = 0;
+		if (comchar == '.') {	/* Repeat last command */
+			lastp++;
+			comchar = lastcmd;
+			nlines = lastarg;
+			if (lastcmd == ':')
+				colonch = lastcolon;
+		}
+		lastcmd = comchar;
+		lastarg = nlines;
+		if ((cc_t) comchar == otty.c_cc[VERASE]) {
+			kill_line();
+			prompt(filename);
+			continue;
+		}
+		switch (comchar) {
+		case ':':
+			retval = colon(filename, colonch, nlines);
+			if (retval >= 0)
+				done++;
+			break;
+		case 'b':
+		case ctrl('B'):
+			{
+				register int initline;
+
+				if (no_intty) {
+					ringbell();
+					return (-1);
 				}
 
-				if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) {
-					--promptlen;
-				ERASEONECOLUMN}
-				continue;
+				if (nlines == 0)
+					nlines++;
+
+				putchar('\r');
+				erasep(0);
+				putchar('\n');
+				if (clreol)
+					cleareol();
+				printf(P_("...back %d page",
+					"...back %d pages", nlines),
+					nlines);
+				if (clreol)
+					cleareol();
+				putchar('\n');
+
+				initline = Currline - dlines * (nlines + 1);
+				if (!noscroll)
+					--initline;
+				if (initline < 0)
+					initline = 0;
+				Fseek(f, 0L);
+				Currline = 0;	/* skiplns() will make Currline correct */
+				skiplns(initline, f);
+				if (!noscroll) {
+					ret(dlines + 1);
+				} else {
+					ret(dlines);
+				}
+			}
+		case ' ':
+		case 'z':
+			if (nlines == 0)
+				nlines = dlines;
+			else if (comchar == 'z')
+				dlines = nlines;
+			ret(nlines);
+		case 'd':
+		case ctrl('D'):
+			if (nlines != 0)
+				nscroll = nlines;
+			ret(nscroll);
+		case 'q':
+		case 'Q':
+			end_it(0);
+		case 's':
+		case 'f':
+		case ctrl('F'):
+			if (nlines == 0)
+				nlines++;
+			if (comchar == 'f')
+				nlines *= dlines;
+			putchar('\r');
+			erasep(0);
+			putchar('\n');
+			if (clreol)
+				cleareol();
+			printf(P_("...skipping %d line",
+				"...skipping %d lines", nlines),
+				nlines);
+
+			if (clreol)
+				cleareol();
+			putchar('\n');
+
+			while (nlines > 0) {
+				while ((c = Getc(f)) != '\n')
+					if (c == EOF) {
+						retval = 0;
+						done++;
+						goto endsw;
+					}
+				Currline++;
+				nlines--;
+			}
+			ret(dlines);
+		case '\n':
+			if (nlines != 0)
+				dlines = nlines;
+			else
+				nlines = 1;
+			ret(nlines);
+		case '\f':
+			if (!no_intty) {
+				doclear();
+				Fseek(f, screen_start.chrctr);
+				Currline = screen_start.line;
+				ret(dlines);
 			} else {
-				if (!eraseln)
-					promptlen = maxlen;
-				siglongjmp(restore, 1);
+				ringbell();
+				break;
 			}
-		} else if (((cc_t) c == otty.c_cc[VKILL]) && !slash) {
-			if (hard) {
-				show(c);
-				putchar('\n');
-				putchar(pchar);
+		case '\'':
+			if (!no_intty) {
+				kill_line();
+				putsout(_("\n***Back***\n\n"));
+				Fseek(f, context.chrctr);
+				Currline = context.line;
+				ret(dlines);
 			} else {
-				putchar('\r');
-				putchar(pchar);
-				if (eraseln)
-					erasep(1);
-				else if (docrtkill)
-					while (promptlen-- > 1)
-						putserr(BSB);
-				promptlen = 1;
+				ringbell();
+				break;
 			}
-			sp = buf;
+		case '=':
+			kill_line();
+			promptlen = printf("%d", Currline);
 			fflush(stdout);
-			continue;
-		}
-		if (slash && ((cc_t) c == otty.c_cc[VKILL]
-			      || (cc_t) c == otty.c_cc[VERASE])) {
-			ERASEONECOLUMN-- sp;
-		}
-		if (c != '\\')
-			slash = 0;
-		*sp++ = c;
-		if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
-			c += (c == RUBOUT) ? -0100 : 0100;
-			putserr(CARAT);
-			promptlen++;
-		}
-		if (c != '\n' && c != ESC) {
-			putcerr(c);
-			promptlen++;
-		} else
 			break;
-	}
-	*--sp = '\0';
-	if (!eraseln)
-		promptlen = maxlen;
-	if (sp - buf >= nmax - 1)
-		more_error(_("Line too long"));
+		case 'n':
+			if (!previousre) {
+				more_error(_("No previous regular expression"));
+				break;
+			}
+			lastp++;
+			/* fall through */
+		case '/':
+			if (nlines == 0)
+				nlines++;
+			kill_line();
+			putchar('/');
+			promptlen = 1;
+			fflush(stdout);
+			if (lastp) {
+				putcerr('\r');
+				search(previousre, f, nlines);
+			} else {
+				ttyin(cmdbuf, sizeof(cmdbuf) - 2, '/');
+				putcerr('\r');
+				free(previousre);
+				previousre = xstrdup(cmdbuf);
+				search(cmdbuf, f, nlines);
+			}
+			ret(dlines - 1);
+		case '!':
+			do_shell(filename);
+			break;
+		case '?':
+		case 'h':
+			if (noscroll)
+				doclear();
+			putsout(_("\n"
+				  "Most commands optionally preceded by integer argument k.  "
+				  "Defaults in brackets.\n"
+				  "Star (*) indicates argument becomes new default.\n"));
+			puts("---------------------------------------"
+			     "----------------------------------------");
+			putsout(_
+				("<space>                 Display next k lines of text [current screen size]\n"
+				 "z                       Display next k lines of text [current screen size]*\n"
+				 "<return>                Display next k lines of text [1]*\n"
+				 "d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
+				 "q or Q or <interrupt>   Exit from more\n"
+				 "s                       Skip forward k lines of text [1]\n"
+				 "f                       Skip forward k screenfuls of text [1]\n"
+				 "b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
+				 "'                       Go to place where previous search started\n"
+				 "=                       Display current line number\n"
+				 "/<regular expression>   Search for kth occurrence of regular expression [1]\n"
+				 "n                       Search for kth occurrence of last r.e [1]\n"
+				 "!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
+				 "v                       Start up /usr/bin/vi at current line\n"
+				 "ctrl-L                  Redraw screen\n"
+				 ":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"));
+			puts("---------------------------------------"
+			     "----------------------------------------");
+			prompt(filename);
+			break;
+		case 'v':	/* This case should go right before default */
+			if (!no_intty) {
+				/* Earlier: call vi +n file. This also
+				 * works for emacs.  POSIX: call vi -c n
+				 * file (when editor is vi or ex). */
+				char *editor, *p;
+				int n = (Currline - dlines <= 0 ? 1 :
+					 Currline - (dlines + 1) / 2);
+				int split = 0;
+
+				editor = getenv("VISUAL");
+				if (editor == NULL || *editor == '\0')
+					editor = getenv("EDITOR");
+				if (editor == NULL || *editor == '\0')
+					editor = VI;
+
+				p = strrchr(editor, '/');
+				if (p)
+					p++;
+				else
+					p = editor;
+				if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
+					sprintf(cmdbuf, "-c %d", n);
+					split = 1;
+				} else {
+					sprintf(cmdbuf, "+%d", n);
+				}
+
+				kill_line();
+				printf("%s %s %s", editor, cmdbuf,
+				       fnames[fnum]);
+				if (split) {
+					cmdbuf[2] = 0;
+					execute(filename, editor, editor,
+						cmdbuf, cmdbuf + 3,
+						fnames[fnum], (char *)0);
+				} else
+					execute(filename, editor, editor,
+						cmdbuf, fnames[fnum],
+						(char *)0);
+				break;
+			}
+			/* fall through */
+		default:
+			if (dum_opt) {
+				kill_line();
+				if (Senter && Sexit) {
+					my_putstring(Senter);
+					promptlen =
+					    printf(_
+						   ("[Press 'h' for instructions.]"))
+					    + 2 * soglitch;
+					my_putstring(Sexit);
+				} else
+					promptlen =
+					    printf(_
+						   ("[Press 'h' for instructions.]"));
+				fflush(stdout);
+			} else
+				ringbell();
+			break;
+		}
+		if (done)
+			break;
+	}
+	putchar('\r');
+ endsw:
+	inwait = 0;
+	notell++;
+	return (retval);
 }
 
-/* return: 0 - unchanged, 1 - changed, -1 - overflow (unchanged) */
-int expand(char **outbuf, char *inbuf)
+/* Print out the contents of the file f, one screenful at a time. */
+#define STOP -10
+static void screen(register FILE *f, register int num_lines)
 {
-	char *inpstr;
-	char *outstr;
-	char c;
-	char *temp;
-	int changed = 0;
-	int tempsz, xtra, offset;
+	register int c;
+	register int nchars;
+	int length;			/* length of current line */
+	static int prev_len = 1;	/* length of previous line */
 
-	xtra = strlen(fnames[fnum]) + strlen(shell_line) + 1;
-	tempsz = 200 + xtra;
-	temp = xmalloc(tempsz);
-	inpstr = inbuf;
-	outstr = temp;
-	while ((c = *inpstr++) != '\0') {
-		offset = outstr - temp;
-		if (tempsz - offset - 1 < xtra) {
-			tempsz += 200 + xtra;
-			temp = xrealloc(temp, tempsz);
-			outstr = temp + offset;
-		}
-		switch (c) {
-		case '%':
-			if (!no_intty) {
-				strcpy(outstr, fnames[fnum]);
-				outstr += strlen(fnames[fnum]);
-				changed++;
-			} else
-				*outstr++ = c;
-			break;
-		case '!':
-			if (!shellp)
-				more_error(_
-					   ("No previous command to substitute for"));
-			strcpy(outstr, shell_line);
-			outstr += strlen(shell_line);
-			changed++;
-			break;
-		case '\\':
-			if (*inpstr == '%' || *inpstr == '!') {
-				*outstr++ = *inpstr++;
-				break;
+	for (;;) {
+		while (num_lines > 0 && !Pause) {
+			if ((nchars = get_line(f, &length)) == EOF) {
+				if (clreol)
+					clreos();
+				return;
 			}
-		default:
-			*outstr++ = c;
+			if (ssp_opt && length == 0 && prev_len == 0)
+				continue;
+			prev_len = length;
+			if (bad_so
+			    || ((Senter && *Senter == ' ') && (promptlen > 0)))
+				erasep(0);
+			/* must clear before drawing line since tabs on
+			 * some terminals do not erase what they tab
+			 * over. */
+			if (clreol)
+				cleareol();
+			prbuf(Line, length);
+			if (nchars < promptlen)
+				erasep(nchars);	/* erasep () sets promptlen to 0 */
+			else
+				promptlen = 0;
+			/* is this needed?
+			 * if (clreol)
+			 *	cleareol();     * must clear again in case we wrapped *
+			 */
+			if (nchars < Mcol || !fold_opt)
+				prbuf("\n", 1);	/* will turn off UL if necessary */
+			if (nchars == STOP)
+				break;
+			num_lines--;
+		}
+		if (pstate) {
+			my_putstring(ULexit);
+			pstate = 0;
+		}
+		fflush(stdout);
+		if ((c = Getc(f)) == EOF) {
+			if (clreol)
+				clreos();
+			return;
+		}
+
+		if (Pause && clreol)
+			clreos();
+		Ungetc(c, f);
+		sigsetjmp(restore, 1);
+		Pause = 0;
+		startup = 0;
+		if ((num_lines = command(NULL, f)) == 0)
+			return;
+		if (hard && promptlen > 0)
+			erasep(0);
+		if (noscroll && num_lines >= dlines) {
+			if (clreol)
+				home();
+			else
+				doclear();
 		}
+		screen_start.line = Currline;
+		screen_start.chrctr = Ftell(f);
 	}
-	*outstr++ = '\0';
-	*outbuf = temp;
-	return (changed);
 }
 
-void show(char c)
+/* Come here if a signal for a window size change is received */
+#ifdef SIGWINCH
+static void chgwinsz(int dummy __attribute__((__unused__)))
 {
-	if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
-		c += (c == RUBOUT) ? -0100 : 0100;
-		putserr(CARAT);
-		promptlen++;
+	struct winsize win;
+
+	signal(SIGWINCH, SIG_IGN);
+	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
+		if (win.ws_row != 0) {
+			Lpp = win.ws_row;
+			nscroll = Lpp / 2 - 1;
+			if (nscroll <= 0)
+				nscroll = 1;
+			dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
+		}
+		if (win.ws_col != 0)
+			Mcol = win.ws_col;
 	}
-	putcerr(c);
-	promptlen++;
+	signal(SIGWINCH, chgwinsz);
 }
+#endif				/* SIGWINCH */
 
-void more_error(char *mess)
+static void copy_file(register FILE *f)
 {
-	if (clreol)
-		cleareol();
-	else
-		kill_line();
-	promptlen += strlen(mess);
-	if (Senter && Sexit) {
-		my_putstring(Senter);
-		putsout(mess);
-		my_putstring(Sexit);
-	} else
-		putsout(mess);
-	fflush(stdout);
-	errors++;
-	siglongjmp(restore, 1);
+	char buf[BUFSIZ];
+	size_t sz;
+
+	while ((sz = fread(&buf, sizeof(char), sizeof(buf), f)) > 0)
+		fwrite(&buf, sizeof(char), sz, stdout);
 }
 
-void set_tty(void)
+/*----------------------------- Terminal I/O -------------------------------*/
+static void initterm(void)
 {
-	otty.c_lflag &= ~(ICANON | ECHO);
-	otty.c_cc[VMIN] = 1;	/* read at least 1 char */
-	otty.c_cc[VTIME] = 0;	/* no timeout */
-	stty(fileno(stderr), &otty);
-}
+	int ret;
+	char *padstr;
+	char *term;
+	struct winsize win;
+
+#ifdef do_SIGTTOU
+ retry:
+#endif
+
+#ifndef NON_INTERACTIVE_MORE
+	no_tty = tcgetattr(fileno(stdout), &otty);
+#endif
+	if (!no_tty) {
+		docrterase = (otty.c_cc[VERASE] != 255);
+		docrtkill = (otty.c_cc[VKILL] != 255);
+#ifdef do_SIGTTOU
+		{
+			int tgrp;
+			/* Wait until we're in the foreground before we
+			 * save the terminal modes. */
+			if ((tgrp = tcgetpgrp(fileno(stdout))) < 0)
+				err(EXIT_FAILURE, "tcgetpgrp");
+			if (tgrp != getpgrp(0)) {
+				kill(0, SIGTTOU);
+				goto retry;
+			}
+		}
+#endif	/* do_SIGTTOU */
+		if ((term = getenv("TERM")) == NULL) {
+			dumb++;
+			ul_opt = 0;
+		}
+		my_setupterm(term, 1, &ret);
+		if (ret <= 0) {
+			dumb++;
+			ul_opt = 0;
+		} else {
+#ifdef TIOCGWINSZ
+			if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
+#endif
+				Lpp = my_tgetnum(TERM_LINES);
+				Mcol = my_tgetnum(TERM_COLS);
+#ifdef TIOCGWINSZ
+			} else {
+				if ((Lpp = win.ws_row) == 0)
+					Lpp = my_tgetnum(TERM_LINES);
+				if ((Mcol = win.ws_col) == 0)
+					Mcol = my_tgetnum(TERM_COLS);
+			}
+#endif
+			if ((Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) {
+				hard++;	/* Hard copy terminal */
+				Lpp = LINES_PER_PAGE;
+			}
+
+			if (my_tgetflag(TERM_EAT_NEW_LINE))
+				/* Eat newline at last column + 1; dec, concept */
+				eatnl++;
+			if (Mcol <= 0)
+				Mcol = NUM_COLUMNS;
+
+			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;
+
+			/* Set up for underlining:  some terminals don't
+			 * need it; others have start/stop sequences,
+			 * still others have an underline char sequence
+			 * which is assumed to move the cursor forward
+			 * one character.  If underline sequence isn't
+			 * available, settle for standout sequence. */
+			if (my_tgetflag(TERM_UNDERLINE)
+			    || my_tgetflag(TERM_OVER_STRIKE))
+				ul_opt = 0;
+			if ((chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == 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 = "";
+				} else
+					ulglitch = soglitch;
+			} else {
+				ulglitch = 0;
+			}
 
-static int ourputch(int c)
-{
-	return putc(c, stdout);
-}
+			if ((padstr = my_tgetstr(TERM_PAD_CHAR)) != NULL)
+				PC = *padstr;
+			Home = my_tgetstr(TERM_HOME);
+			if (Home == NULL || *Home == '\0') {
+				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(TERM_CLEAR_TO_SCREEN_END);
+			if ((chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL)
+				chBS = "\b";
 
-void reset_tty(void)
-{
-	if (no_tty)
-		return;
-	if (pstate) {
-		/* putchar - if that isn't a macro */
-		tputs(ULexit, fileno(stdout), ourputch);
-		fflush(stdout);
-		pstate = 0;
+		}
+		if ((shell = getenv("SHELL")) == NULL)
+			shell = "/bin/sh";
+	}
+	no_intty = tcgetattr(fileno(stdin), &otty);
+	tcgetattr(fileno(stderr), &otty);
+	savetty0 = otty;
+	slow_tty = cfgetispeed(&otty) < B1200;
+	hardtabs = (otty.c_oflag & TABDLY) != XTABS;
+	if (!no_tty) {
+		otty.c_lflag &= ~(ICANON | ECHO);
+		otty.c_cc[VMIN] = 1;
+		otty.c_cc[VTIME] = 0;
 	}
-	otty.c_lflag |= ICANON | ECHO;
-	otty.c_cc[VMIN] = savetty0.c_cc[VMIN];
-	otty.c_cc[VTIME] = savetty0.c_cc[VTIME];
-	stty(fileno(stderr), &savetty0);
 }
 
-void rdline(register FILE *f)
+int main(int argc, char **argv)
 {
-	register int c;
-	register char *p;
+	FILE *f;
+	char *s;
+	int c;
+	int left;
+	int prnames = 0;
+	int initopt = 0;
+	int srchopt = 0;
+	int clearit = 0;
+	int initline = 0;
+	char *initbuf = NULL;
 
-	prepare_line_buffer();
+	setlocale(LC_ALL, "");
+	bindtextdomain(PACKAGE, LOCALEDIR);
+	textdomain(PACKAGE);
+	atexit(close_stdout);
 
-	p = Line;
-	while ((c = Getc(f)) != '\n' && c != EOF
-	       && (size_t)(p - Line) < LineLen - 1)
-		*p++ = c;
-	if (c == '\n')
-		Currline++;
-	*p = '\0';
-}
+	nfiles = argc;
+	fnames = argv;
+	setlocale(LC_ALL, "");
+	initterm();
 
-/* Come here when we get a suspend signal from the terminal */
-void onsusp(int dummy __attribute__((__unused__)))
-{
-	sigset_t signals, oldmask;
+	/* Auto set no scroll on when binary is called page */
+	if (!(strcmp(program_invocation_short_name, "page")))
+		noscroll++;
 
-	/* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
-	signal(SIGTTOU, SIG_IGN);
-	reset_tty();
-	fflush(stdout);
-	signal(SIGTTOU, SIG_DFL);
-	/* Send the TSTP signal to suspend our process group */
-	signal(SIGTSTP, SIG_DFL);
+	prepare_line_buffer();
 
-	/* unblock SIGTSTP or we won't be able to suspend ourself */
-	sigemptyset(&signals);
-	sigaddset(&signals, SIGTSTP);
-	sigprocmask(SIG_UNBLOCK, &signals, &oldmask);
+	nscroll = Lpp / 2 - 1;
+	if (nscroll <= 0)
+		nscroll = 1;
 
-	kill(0, SIGTSTP);
-	/* Pause for station break */
+	if ((s = getenv("MORE")) != NULL)
+		argscan(s);
 
-	sigprocmask(SIG_SETMASK, &oldmask, NULL);
+	while (--nfiles > 0) {
+		if ((c = (*++fnames)[0]) == '-') {
+			argscan(*fnames + 1);
+		} else if (c == '+') {
+			s = *fnames;
+			if (*++s == '/') {
+				srchopt++;
+				initbuf = xstrdup(s + 1);
+			} else {
+				initopt++;
+				for (initline = 0; *s != '\0'; s++)
+					if (isdigit(*s))
+						initline =
+						    initline * 10 + *s - '0';
+				--initline;
+			}
+		} else
+			break;
+	}
+	/* allow clreol only if Home and eraseln and EodClr strings are
+	 * defined, and in that case, make sure we are in noscroll mode */
+	if (clreol) {
+		if ((Home == NULL) || (*Home == '\0') ||
+		    (eraseln == NULL) || (*eraseln == '\0') ||
+		    (EodClr == NULL) || (*EodClr == '\0'))
+			clreol = 0;
+		else
+			noscroll = 1;
+	}
+	if (dlines == 0)
+		dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
+	left = dlines;
+	if (nfiles > 1)
+		prnames++;
+	if (!no_intty && nfiles == 0)
+		usage(stderr);
+	else
+		f = stdin;
+	if (!no_tty) {
+		signal(SIGQUIT, onquit);
+		signal(SIGINT, end_it);
+#ifdef SIGWINCH
+		signal(SIGWINCH, chgwinsz);
+#endif
+		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
+			signal(SIGTSTP, onsusp);
+			catch_susp++;
+		}
+		stty(fileno(stderr), &otty);
+	}
+	if (no_intty) {
+		if (no_tty)
+			copy_file(stdin);
+		else {
+			if ((c = Getc(f)) == '\f')
+				doclear();
+			else {
+				Ungetc(c, f);
+				if (noscroll && (c != EOF)) {
+					if (clreol)
+						home();
+					else
+						doclear();
+				}
+			}
+			if (srchopt) {
+				free(previousre);
+				previousre = xstrdup(initbuf);
+				search(initbuf, stdin, 1);
+				if (noscroll)
+					left--;
+			} else if (initopt)
+				skiplns(initline, stdin);
+			screen(stdin, left);
+		}
+		no_intty = 0;
+		prnames++;
+		firstf = 0;
+	}
 
-	/* We're back */
-	signal(SIGTSTP, onsusp);
-	set_tty();
-	if (inwait)
-		siglongjmp(restore, 1);
+	while (fnum < nfiles) {
+		if ((f = checkf(fnames[fnum], &clearit)) != NULL) {
+			context.line = context.chrctr = 0;
+			Currline = 0;
+			if (firstf)
+				sigsetjmp(restore, 1);
+			if (firstf) {
+				firstf = 0;
+				if (srchopt) {
+					free(previousre);
+					previousre = xstrdup(initbuf);
+					search(initbuf, f, 1);
+					if (noscroll)
+						left--;
+				} else if (initopt)
+					skiplns(initline, f);
+			} else if (fnum < nfiles && !no_tty) {
+				sigsetjmp(restore, 1);
+				left = command(fnames[fnum], f);
+			}
+			if (left != 0) {
+				if ((noscroll || clearit)
+				    && (file_size != LONG_MAX)) {
+					if (clreol)
+						home();
+					else
+						doclear();
+				}
+				if (prnames) {
+					if (bad_so)
+						erasep(0);
+					if (clreol)
+						cleareol();
+					putsout("::::::::::::::");
+					if (promptlen > 14)
+						erasep(14);
+					putchar('\n');
+					if (clreol)
+						cleareol();
+					puts(fnames[fnum]);
+					if (clreol)
+						cleareol();
+					puts("::::::::::::::");
+					if (left > Lpp - 4)
+						left = Lpp - 4;
+				}
+				if (no_tty)
+					copy_file(f);
+				else {
+					within++;
+					screen(f, left);
+					within = 0;
+				}
+			}
+			sigsetjmp(restore, 1);
+			fflush(stdout);
+			fclose(f);
+			screen_start.line = screen_start.chrctr = 0L;
+			context.line = context.chrctr = 0L;
+		}
+		fnum++;
+		firstf = 0;
+	}
+	free(previousre);
+	free(initbuf);
+	free(Line);
+	reset_tty();
+	exit(EXIT_SUCCESS);
 }
Signature · 1 line
2.3.0
#2 lore raw

PATCH 2/31 more: reorder includes, declarations, and global variables

In reply to Sami Kerola
---
 text-utils/Makemodule.am |   2 +-
 text-utils/more-term.h   | 112 +++++++++++++++++++++++++++++
 text-utils/more.c        | 179 +++++++++--------------------------------------
 3 files changed, 148 insertions(+), 145 deletions(-)
 create mode 100644 text-utils/more-term.h
text-utils/Makemodule.am +1 -1
index bd3d53f..a24c10b 100644
@@ -84,7 +84,7 @@ endif # BUILD_UL
 if BUILD_MORE
 bin_PROGRAMS += more
 dist_man_MANS += text-utils/more.1
-more_SOURCES = text-utils/more.c
+more_SOURCES = text-utils/more.c text-utils/more-term.h
 more_CFLAGS = $(AM_CFLAGS) $(BSD_WARN_CFLAGS)
 more_LDADD = $(LDADD)
 if HAVE_TINFO
text-utils/more-term.h new file +112 -0

112 lines not on this page · raw message

text-utils/more.c +35 -144

223 lines not on this page · raw message

Signature · 1 line
2.3.0
#3 lore raw

PATCH 3/31 more: add struct more_control and remove global variables

In reply to Sami Kerola

Sorry, this is horribly long change, but there does not seem to be any other way to do this sort of globals to control struct changes. To keep the as sane as it can be there are no functional changes in these lines.

---
 text-utils/more.c | 1320 +++++++++++++++++++++++++++--------------------------
 1 file changed, 680 insertions(+), 640 deletions(-)
text-utils/more.c +680 -640
index 3937796..020f9b9 100644
@@ -85,23 +85,23 @@
 
 #define VI	"vi"	/* found on the user's path */
 
-#define Fopen(s,m)	(Currline = 0,file_pos=0,fopen(s,m))
-#define Ftell(f)	file_pos
-#define Fseek(f,off)	(file_pos=off,fseek(f,off,0))
-#define Getc(f)		(++file_pos, getc(f))
-#define Ungetc(c,f)	(--file_pos, ungetc(c,f))
+#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 stty(fd, argp)	tcsetattr(fd, TCSANOW, argp)
 #define ringbell()	putcerr('\007')
 
 static char *BS = "\b";
 static char *BSB = "\b \b";
 static char *CARAT = "^";
 #define ERASEONECOLUMN \
-    if (docrterase) \
+    if (ctl->docrterase) \
 	putserr(BSB); \
     else \
 	putserr(BS);
@@ -122,68 +122,89 @@ static char *CARAT = "^";
 #define REGERR_BUF	NUM_COLUMNS
 #define STOP		-10
 
-struct termios otty, savetty0;
-long file_pos, file_size;
-int fnum, no_intty, no_tty, slow_tty;
-int dum_opt, dlines;
-int nscroll = SCROLL_LEN;	/* Number of lines scrolled by 'd' */
-int fold_opt = 1;		/* Fold long lines */
-int stop_opt = 1;		/* Stop after form feeds */
-int ssp_opt = 0;		/* Suppress white space */
-int ul_opt = 1;			/* Underline as best we can */
-int promptlen;
-int Currline;			/* Line we are currently at */
-int startup = 1;
-int firstf = 1;
-int notell = 1;
-int docrterase = 0;
-int docrtkill = 0;
-int bad_so;			/* True if overwriting does not turn
-				   off standout */
-int inwait, Pause, errors;
-int within;			/* true if we are within a file,
-				   false if we are between files */
-int hard, dumb, noscroll, hardtabs, clreol, eatnl;
-int catch_susp;			/* We should catch the SIGTSTP signal */
-char **fnames;			/* The list of file names */
-int nfiles;			/* Number of files left to process */
-char *shell;			/* The name of the shell to use */
-int shellp;			/* A previous shell command exists */
-sigjmp_buf restore;
-char *Line;			/* Line buffer */
-size_t LineLen;			/* size of Line buffer */
-int Lpp = LINES_PER_PAGE;	/* lines per page */
-char *Clear;			/* clear screen */
-char *eraseln;			/* erase line */
-char *Senter, *Sexit;		/* enter and exit standout mode */
-char *ULenter, *ULexit;		/* enter and exit underline mode */
-char *chUL;			/* underline character */
-char *chBS;			/* backspace character */
-char *Home;			/* go to home */
-char *cursorm;			/* cursor movement */
-char cursorhome[40];		/* contains cursor movement to home */
-char *EodClr;			/* clear rest of screen */
-int Mcol = NUM_COLUMNS;		/* number of columns */
-int Wrap = 1;			/* set if automargins */
-int soglitch;			/* terminal has standout mode glitch */
-int ulglitch;			/* terminal has underline mode glitch */
-int pstate = 0;			/* current UL state */
-char *previousre;		/* previous search() buf[] item */
-struct {
-	long chrctr, line;
-} context, screen_start;
-extern char PC;			/* pad character */
-
-static char ch;
-static int lastcmd, lastarg, lastp;
-static int lastcolon;
-char shell_line[SHELL_LINE];
-
+struct more_control {
+	struct termios otty;		/* output terminal */
+	struct termios savetty0;	/* original terminal settings */
+	long file_pos;			/* file position */
+	long file_size;			/* file size */
+	int fnum;			/* position in argv[] */
+	int nscroll;			/* number of lines scrolled by 'd' */
+	int dlines;			/* screen size in lines */
+	int promptlen;			/* message prompt length */
+	int Currline;			/* line we are currently at */
+	char **fnames;			/* the list of file names */
+	int nfiles;			/* number of files left to process */
+	char *shell;			/* the name of the shell to use */
+	int shellp;			/* does previous shell command exists */
+	sigjmp_buf restore;		/* siglongjmp() destination */
+	char *Line;			/* line buffer */
+	size_t LineLen;			/* size of line buffer */
+	int Lpp;			/* lines per page */
+	char *Clear;			/* clear screen */
+	char *eraseln;			/* erase line */
+	char *Senter;			/* enter standout mode */
+	char *Sexit;			/* exit standout mode */
+	char *ULenter;			/* enter underline mode */
+	char *ULexit;			/* exit underline mode */
+	char *chUL;			/* underline character */
+	char *chBS;			/* backspace character */
+	char *Home;			/* go to screen home position */
+	char *cursorm;			/* cursor move destination */
+	char cursorhome[40];		/* contains cursor movement to home */
+	char *EodClr;			/* clear rest of screen */
+	int Mcol;			/* number of columns */
+	char *previousre;		/* previous search() buf[] item */
+	struct {
+		long chrctr;		/* row number */
+		long line;		/* line number */
+	} context,
+	  screen_start;
+	char PC;			/* pad character (instead of null) */
+	char ch;			/* temporary character. FIXME: move to function scope */
+	int lastcmd;			/* previous more key command */
+	int lastarg;			/* previous key command argument */
+	int lastcolon;			/* is a colon-prefixed key command */
+	char shell_line[SHELL_LINE];
 #if !defined(HAVE_NCURSES_H) && !defined(HAVE_NCURSES_NCURSES_H) && defined(HAVE_LIBTERMCAP)
-char termbuffer[TERMINAL_BUF];
-char tcbuffer[TERMINAL_BUF];
-char *strbuf = termbuffer;
+	char termbuffer[TERMINAL_BUF];	/* FIXME: remove, not in use */
+	char *strbuf = termbuffer;	/* FIXME: remove, not in use */
+	char tcbuffer[TERMINAL_BUF];	/* buffer for my_setupterm() in more-term.h */
 #endif
+	unsigned int
+		bad_so:1,		/* true if overwriting does not turn off standout */
+		catch_susp:1,		/* should SIGTSTP signal be caught */
+		clreol:1,		/* do not scroll, paint each screen from the top */
+		docrterase:1,		/* is erase previous supported */
+		docrtkill:1,		/* is erase input supported */
+		dumb:1,			/* is terminal type known */
+		dum_opt:1,		/* suppress bell */
+		eatnl:1,		/* is newline ignored after 80 cols */
+		errors:1,		/* is an error reported */
+		firstf:1,		/* is the input file the first in list */
+		fold_opt:1,		/* fold long lines */
+		hard:1,			/* is this hard copy terminal */
+		hardtabs:1,		/* print spaces instead of '\t' */
+		inwait:1,		/* is waiting user input */
+		lastp:1,		/* run previous key command */
+		noscroll:1,		/* do not scroll, clear the screen and then display text */
+		notell:1,		/* suppress quit dialog */
+		no_intty:1,		/* is input in interactive mode */
+		no_tty:1,		/* is output in interactive mode */
+		Pause:1,		/* is output paused */
+		pstate:1,		/* is underlining going on */
+		slow_tty:1,		/* is output speed slow. FIXME: remove */
+		soglitch:1,		/* terminal has standout mode glitch */
+		ssp_opt:1,		/* suppress white space */
+		startup:1,		/* is startup completed */
+		stop_opt:1,		/* stop after form feeds */
+		ulglitch:1,		/* terminal is underlining in glitch mode */
+		ul_opt:1,		/* underline as best we can */
+		within:1,		/* true if we are within a file, false if we are between files */
+		Wrap:1;			/* set if automargins */
+};
+/* FIXME: the global_ctl is used in signal handlers, until the signal
+ * handling is corrected to use signalfd().  */
+struct more_control *global_ctl;
 
 static void __attribute__((__noreturn__)) usage(FILE *out)
 {
@@ -209,7 +230,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
-static void argscan(char *s)
+static void argscan(struct more_control *ctl, char *s)
 {
 	int seen_num = 0;
 
@@ -226,31 +247,31 @@ static void argscan(char *s)
 		case '8':
 		case '9':
 			if (!seen_num) {
-				dlines = 0;
+				ctl->dlines = 0;
 				seen_num = 1;
 			}
-			dlines = dlines * 10 + *s - '0';
+			ctl->dlines = ctl->dlines * 10 + *s - '0';
 			break;
 		case 'd':
-			dum_opt = 1;
+			ctl->dum_opt = 1;
 			break;
 		case 'l':
-			stop_opt = 0;
+			ctl->stop_opt = 0;
 			break;
 		case 'f':
-			fold_opt = 0;
+			ctl->fold_opt = 0;
 			break;
 		case 'p':
-			noscroll++;
+			ctl->noscroll = 1;
 			break;
 		case 'c':
-			clreol++;
+			ctl->clreol = 1;
 			break;
 		case 's':
-			ssp_opt = 1;
+			ctl->ssp_opt = 1;
 			break;
 		case 'u':
-			ul_opt = 0;
+			ctl->ul_opt = 0;
 			break;
 		case '-':
 		case ' ':
@@ -270,9 +291,9 @@ static void argscan(char *s)
 }
 
 /* force clear to end of line */
-static void cleareol(void)
+static void cleareol(struct more_control *ctl)
 {
-	my_putstring(eraseln);
+	my_putstring(ctl->eraseln);
 }
 
 /* magic --
@@ -307,7 +328,7 @@ static int magic(FILE *f, char *fs)
 
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
-static FILE *checkf(register char *fs, int *clearfirst)
+static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst)
 {
 	struct stat stbuf;
 	register FILE *f;
@@ -315,8 +336,8 @@ static FILE *checkf(register char *fs, int *clearfirst)
 
 	if (stat(fs, &stbuf) == -1) {
 		fflush(stdout);
-		if (clreol)
-			cleareol();
+		if (ctl->clreol)
+			cleareol(ctl);
 		warn(_("stat of %s failed"), fs);
 		return ((FILE *)NULL);
 	}
@@ -337,30 +358,30 @@ static FILE *checkf(register char *fs, int *clearfirst)
 	c = Getc(f);
 	*clearfirst = (c == '\f');
 	Ungetc(c, f);
-	if ((file_size = stbuf.st_size) == 0)
-		file_size = LONG_MAX;
+	if ((ctl->file_size = stbuf.st_size) == 0)
+		ctl->file_size = LONG_MAX;
 	return (f);
 }
 
-static void prepare_line_buffer(void)
+static void prepare_line_buffer(struct more_control *ctl)
 {
 	char *nline;
-	size_t nsz = Mcol * 4;
+	size_t nsz = ctl->Mcol * 4;
 
-	if (LineLen >= nsz)
+	if (ctl->LineLen >= nsz)
 		return;
 
 	if (nsz < LINSIZ)
 		nsz = LINSIZ;
 
 	/* alloc nsz and extra space for \n\0 */
-	nline = xrealloc(Line, nsz + 2);
-	Line = nline;
-	LineLen = nsz;
+	nline = xrealloc(ctl->Line, nsz + 2);
+	ctl->Line = nline;
+	ctl->LineLen = nsz;
 }
 
 /* Get a logical line */
-static int get_line(register FILE *f, int *length)
+static int get_line(struct more_control *ctl, register FILE *f, int *length)
 {
 	int c;
 	char *p;
@@ -382,18 +403,18 @@ static int get_line(register FILE *f, int *length)
 	memset(&state, '\0', sizeof(mbstate_t));
 #endif
 
-	prepare_line_buffer();
+	prepare_line_buffer(ctl);
 
-	p = Line;
+	p = ctl->Line;
 	column = 0;
 	c = Getc(f);
 	if (colflg && c == '\n') {
-		Currline++;
+		ctl->Currline++;
 		c = Getc(f);
 	}
-	while (p < &Line[LineLen - 1]) {
+	while (p < &ctl->Line[ctl->LineLen - 1]) {
 #ifdef HAVE_WIDECHAR
-		if (fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
+		if (ctl->fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
 			use_mbc_buffer_flag = 0;
 			state_bak = state;
 			mbc[mbc_pos++] = c;
@@ -412,7 +433,7 @@ static int get_line(register FILE *f, int *length)
 				column++;
 				file_pos_bak++;
 
-				if (column >= Mcol) {
+				if (column >= ctl->Mcol) {
 					Fseek(f, file_pos_bak);
 				} else {
 					memmove(mbc, mbc + 1, --mbc_pos);
@@ -426,11 +447,11 @@ static int get_line(register FILE *f, int *length)
 			default:
 				wc_width = wcwidth(wc);
 
-				if (column + wc_width > Mcol) {
+				if (column + wc_width > ctl->Mcol) {
 					Fseek(f, file_pos_bak);
 					break_flag = 1;
 				} else {
-					for (i = 0; p < &Line[LineLen - 1] &&
+					for (i = 0; p < &ctl->Line[ctl->LineLen - 1] &&
 						    i < mbc_pos; i++)
 						*p++ = mbc[i];
 					if (wc_width > 0)
@@ -438,7 +459,7 @@ static int get_line(register FILE *f, int *length)
 				}
 			}
 
-			if (break_flag || column >= Mcol)
+			if (break_flag || column >= ctl->Mcol)
 				break;
 
 			c = Getc(f);
@@ -446,16 +467,16 @@ static int get_line(register FILE *f, int *length)
 		}
 #endif	/* HAVE_WIDECHAR */
 		if (c == EOF) {
-			if (p > Line) {
+			if (p > ctl->Line) {
 				*p = '\0';
-				*length = p - Line;
+				*length = p - ctl->Line;
 				return (column);
 			}
-			*length = p - Line;
+			*length = p - ctl->Line;
 			return (EOF);
 		}
 		if (c == '\n') {
-			Currline++;
+			ctl->Currline++;
 			break;
 		}
 
@@ -475,19 +496,19 @@ static int get_line(register FILE *f, int *length)
 		}
 #endif	/* 0 */
 		if (c == '\t') {
-			if (!hardtabs || (column < promptlen && !hard)) {
-				if (hardtabs && eraseln && !dumb) {
+			if (!ctl->hardtabs || (column < ctl->promptlen && !ctl->hard)) {
+				if (ctl->hardtabs && ctl->eraseln && !ctl->dumb) {
 					column = 1 + (column | 7);
-					my_putstring(eraseln);
-					promptlen = 0;
+					my_putstring(ctl->eraseln);
+					ctl->promptlen = 0;
 				} else {
-					for (--p; p < &Line[LineLen - 1];) {
+					for (--p; p < &ctl->Line[ctl->LineLen - 1];) {
 						*p++ = ' ';
 						if ((++column & 7) == 0)
 							break;
 					}
-					if (column >= promptlen)
-						promptlen = 0;
+					if (column >= ctl->promptlen)
+						ctl->promptlen = 0;
 				}
 			} else
 				column = 1 + (column | 7);
@@ -497,22 +518,22 @@ static int get_line(register FILE *f, int *length)
 			int next = Getc(f);
 			if (next == '\n') {
 				p--;
-				Currline++;
+				ctl->Currline++;
 				break;
 			}
 			Ungetc(next, f);
 			column = 0;
-		} else if (c == '\f' && stop_opt) {
+		} else if (c == '\f' && ctl->stop_opt) {
 			p[-1] = '^';
 			*p++ = 'L';
 			column += 2;
-			Pause++;
+			ctl->Pause = 1;
 		} else if (c == EOF) {
-			*length = p - Line;
+			*length = p - ctl->Line;
 			return (column);
 		} else {
 #ifdef HAVE_WIDECHAR
-			if (fold_opt && MB_CUR_MAX > 1) {
+			if (ctl->fold_opt && MB_CUR_MAX > 1) {
 				memset(mbc, '\0', MB_LEN_MAX);
 				mbc_pos = 0;
 				mbc[mbc_pos++] = c;
@@ -546,52 +567,52 @@ static int get_line(register FILE *f, int *length)
 			}
 		}
 
-		if (column >= Mcol && fold_opt)
+		if (column >= ctl->Mcol && ctl->fold_opt)
 			break;
 #ifdef HAVE_WIDECHAR
-		if (use_mbc_buffer_flag == 0 && p >= &Line[LineLen - 1 - 4])
+		if (use_mbc_buffer_flag == 0 && p >= &ctl->Line[ctl->LineLen - 1 - 4])
 			/* don't read another char if there is no space for
 			 * whole multibyte sequence */
 			break;
 #endif
 		c = Getc(f);
 	}
-	if (column >= Mcol && Mcol > 0) {
-		if (!Wrap) {
+	if (column >= ctl->Mcol && ctl->Mcol > 0) {
+		if (!ctl->Wrap) {
 			*p++ = '\n';
 		}
 	}
-	colflg = column == Mcol && fold_opt;
-	if (colflg && eatnl && Wrap) {
+	colflg = column == ctl->Mcol && ctl->fold_opt;
+	if (colflg && ctl->eatnl && ctl->Wrap) {
 		*p++ = '\n';	/* simulate normal wrap */
 	}
-	*length = p - Line;
+	*length = p - ctl->Line;
 	*p = 0;
 	return (column);
 }
 
 /* Erase the rest of the prompt, assuming we are starting at column col. */
-static void erasep(register int col)
+static void erasep(struct more_control *ctl, register int col)
 {
 
-	if (promptlen == 0)
+	if (ctl->promptlen == 0)
 		return;
-	if (hard) {
+	if (ctl->hard) {
 		putchar('\n');
 	} else {
 		if (col == 0)
 			putchar('\r');
-		if (!dumb && eraseln)
-			my_putstring(eraseln);
+		if (!ctl->dumb && ctl->eraseln)
+			my_putstring(ctl->eraseln);
 		else
-			printf("%*s", promptlen - col, "");
+			printf("%*s", ctl->promptlen - col, "");
 	}
-	promptlen = 0;
+	ctl->promptlen = 0;
 }
 
-static void clreos(void)
+static void clreos(struct more_control *ctl)
 {
-	my_putstring(EodClr);
+	my_putstring(ctl->EodClr);
 }
 
 static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
@@ -604,17 +625,17 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 }
 
 /* Print a buffer of n characters */
-static void prbuf(register char *s, register int n)
+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 (!ul_opt)
+		if (!ctl->ul_opt)
 			putchar(*s++);
 		else {
-			if (*s == ' ' && pstate == 0 && ulglitch
+			if (*s == ' ' && ctl->pstate == 0 && ctl->ulglitch
 			    && wouldul(s + 1, n - 1)) {
 				s++;
 				continue;
@@ -625,15 +646,15 @@ static void prbuf(register char *s, register int n)
 				s += 3;
 			} else
 				c = *s++;
-			if (state != pstate) {
-				if (c == ' ' && state == 0 && ulglitch
+			if (state != ctl->pstate) {
+				if (c == ' ' && state == 0 && ctl->ulglitch
 				    && wouldul(s, n - 1))
 					state = 1;
 				else
-					my_putstring(state ? ULenter : ULexit);
+					my_putstring(state ? ctl->ULenter : ctl->ULexit);
 			}
-			if (c != ' ' || pstate == 0 || state != 0
-			    || ulglitch == 0)
+			if (c != ' ' || ctl->pstate == 0 || state != 0
+			    || ctl->ulglitch == 0)
 #ifdef HAVE_WIDECHAR
 			{
 				wchar_t wc;
@@ -650,56 +671,56 @@ static void prbuf(register char *s, register int n)
 #else
 				putchar(c);
 #endif				/* HAVE_WIDECHAR */
-			if (state && *chUL) {
-				putsout(chBS);
-				my_putstring(chUL);
+			if (state && *ctl->chUL) {
+				putsout(ctl->chBS);
+				my_putstring(ctl->chUL);
 			}
-			pstate = state;
+			ctl->pstate = state;
 		}
 }
 
 /* Erase the current line entirely */
-static void kill_line(void)
+static void kill_line(struct more_control *ctl)
 {
-	erasep(0);
-	if (!eraseln || dumb)
+	erasep(ctl, 0);
+	if (!ctl->eraseln || ctl->dumb)
 		putchar('\r');
 }
 
-static void prompt(char *filename)
+static void prompt(struct more_control *ctl, char *filename)
 {
-	if (clreol)
-		cleareol();
-	else if (promptlen > 0)
-		kill_line();
-	if (!hard) {
-		promptlen = 0;
-		if (Senter && Sexit) {
-			my_putstring(Senter);
-			promptlen += (2 * soglitch);
+	if (ctl->clreol)
+		cleareol(ctl);
+	else if (ctl->promptlen > 0)
+		kill_line(ctl);
+	if (!ctl->hard) {
+		ctl->promptlen = 0;
+		if (ctl->Senter && ctl->Sexit) {
+			my_putstring(ctl->Senter);
+			ctl->promptlen += (2 * ctl->soglitch);
 		}
-		if (clreol)
-			cleareol();
-		promptlen += printf(_("--More--"));
+		if (ctl->clreol)
+			cleareol(ctl);
+		ctl->promptlen += printf(_("--More--"));
 		if (filename != NULL) {
-			promptlen += printf(_("(Next file: %s)"), filename);
-		} else if (!no_intty) {
-			promptlen +=
+			ctl->promptlen += printf(_("(Next file: %s)"), filename);
+		} else if (!ctl->no_intty) {
+			ctl->promptlen +=
 			    printf("(%d%%)",
-				   (int)((file_pos * 100) / file_size));
+				   (int)((ctl->file_pos * 100) / ctl->file_size));
 		}
-		if (dum_opt) {
-			promptlen +=
+		if (ctl->dum_opt) {
+			ctl->promptlen +=
 			    printf(_("[Press space to continue, 'q' to quit.]"));
 		}
-		if (Senter && Sexit)
-			my_putstring(Sexit);
-		if (clreol)
-			clreos();
+		if (ctl->Senter && ctl->Sexit)
+			my_putstring(ctl->Sexit);
+		if (ctl->clreol)
+			clreos(ctl);
 		fflush(stdout);
 	} else
 		ringbell();
-	inwait++;
+	ctl->inwait = 1;
 }
 
 static int ourputch(int c)
@@ -707,41 +728,41 @@ static int ourputch(int c)
 	return putc(c, stdout);
 }
 
-static void reset_tty(void)
+static void reset_tty(struct more_control *ctl)
 {
-	if (no_tty)
+	if (ctl->no_tty)
 		return;
-	if (pstate) {
+	if (ctl->pstate) {
 		/* putchar - if that isn't a macro */
-		tputs(ULexit, fileno(stdout), ourputch);
+		tputs(ctl->ULexit, fileno(stdout), ourputch);
 		fflush(stdout);
-		pstate = 0;
+		ctl->pstate = 0;
 	}
-	otty.c_lflag |= ICANON | ECHO;
-	otty.c_cc[VMIN] = savetty0.c_cc[VMIN];
-	otty.c_cc[VTIME] = savetty0.c_cc[VTIME];
-	stty(fileno(stderr), &savetty0);
+	ctl->otty.c_lflag |= ICANON | ECHO;
+	ctl->otty.c_cc[VMIN] = ctl->savetty0.c_cc[VMIN];
+	ctl->otty.c_cc[VTIME] = ctl->savetty0.c_cc[VTIME];
+	stty(fileno(stderr), &ctl->savetty0);
 }
 
 /* Clean up terminal state and exit. Also come here if interrupt signal received */
 static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
 {
-	reset_tty();
-	if (clreol) {
+	reset_tty(global_ctl);
+	if (global_ctl->clreol) {
 		putchar('\r');
-		clreos();
+		clreos(global_ctl);
 		fflush(stdout);
-	} else if (!clreol && (promptlen > 0)) {
-		kill_line();
+	} else if (!global_ctl->clreol && (global_ctl->promptlen > 0)) {
+		kill_line(global_ctl);
 		fflush(stdout);
 	} else
 		putcerr('\n');
-	free(previousre);
-	free(Line);
+	free(global_ctl->previousre);
+	free(global_ctl->Line);
 	_exit(EXIT_SUCCESS);
 }
 
-static int readch(void)
+static int readch(struct more_control *ctl)
 {
 	unsigned char c;
 
@@ -750,27 +771,27 @@ static int readch(void)
 		if (errno != EINTR)
 			end_it(0);
 		else
-			c = otty.c_cc[VKILL];
+			c = ctl->otty.c_cc[VKILL];
 	}
 	return (c);
 }
 
 /* Read a decimal number from the terminal.  Set cmd to the non-digit
  * which terminates the number. */
-static int number(char *cmd)
+static int number(struct more_control *ctl, char *cmd)
 {
 	register int i;
 
 	i = 0;
-	ch = otty.c_cc[VKILL];
+	ctl->ch = ctl->otty.c_cc[VKILL];
 	for (;;) {
-		ch = readch();
-		if (isdigit(ch))
-			i = i * 10 + ch - '0';
-		else if ((cc_t) ch == otty.c_cc[VKILL])
+		ctl->ch = readch(ctl);
+		if (isdigit(ctl->ch))
+			i = i * 10 + ctl->ch - '0';
+		else if ((cc_t) ctl->ch == ctl->otty.c_cc[VKILL])
 			i = 0;
 		else {
-			*cmd = ch;
+			*cmd = ctl->ch;
 			break;
 		}
 	}
@@ -779,62 +800,62 @@ static int number(char *cmd)
 
 /* Skip nskip files in the file list (from the command line).  Nskip may
  * be negative. */
-static void skipf(register int nskip)
+static void skipf(struct more_control *ctl, register int nskip)
 {
 	if (nskip == 0)
 		return;
 	if (nskip > 0) {
-		if (fnum + nskip > nfiles - 1)
-			nskip = nfiles - fnum - 1;
-	} else if (within)
-		++fnum;
-	fnum += nskip;
-	if (fnum < 0)
-		fnum = 0;
+		if (ctl->fnum + nskip > ctl->nfiles - 1)
+			nskip = ctl->nfiles - ctl->fnum - 1;
+	} else if (ctl->within)
+		ctl->fnum++;
+	ctl->fnum += nskip;
+	if (ctl->fnum < 0)
+		ctl->fnum = 0;
 	puts(_("\n...Skipping "));
-	if (clreol)
-		cleareol();
+	if (ctl->clreol)
+		cleareol(ctl);
 	if (nskip > 0)
 		putsout(_("...Skipping to file "));
 	else
 		putsout(_("...Skipping back to file "));
-	puts(fnames[fnum]);
-	if (clreol)
-		cleareol();
+	puts(ctl->fnames[ctl->fnum]);
+	if (ctl->clreol)
+		cleareol(ctl);
 	putchar('\n');
-	--fnum;
+	ctl->fnum--;
 }
 
-static void show(char c)
+static void show(struct more_control *ctl, char c)
 {
 	if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
 		c += (c == RUBOUT) ? -0100 : 0100;
 		putserr(CARAT);
-		promptlen++;
+		ctl->promptlen++;
 	}
 	putcerr(c);
-	promptlen++;
+	ctl->promptlen++;
 }
 
-static void more_error(char *mess)
+static void more_error(struct more_control *ctl, char *mess)
 {
-	if (clreol)
-		cleareol();
+	if (ctl->clreol)
+		cleareol(ctl);
 	else
-		kill_line();
-	promptlen += strlen(mess);
-	if (Senter && Sexit) {
-		my_putstring(Senter);
+		kill_line(ctl);
+	ctl->promptlen += strlen(mess);
+	if (ctl->Senter && ctl->Sexit) {
+		my_putstring(ctl->Senter);
 		putsout(mess);
-		my_putstring(Sexit);
+		my_putstring(ctl->Sexit);
 	} else
 		putsout(mess);
 	fflush(stdout);
-	errors++;
-	siglongjmp(restore, 1);
+	ctl->errors++;
+	siglongjmp(ctl->restore, 1);
 }
 
-static void ttyin(char buf[], register int nmax, char pchar)
+static void ttyin(struct more_control *ctl, char buf[], register int nmax, char pchar)
 {
 	char *sp;
 	int c;
@@ -844,12 +865,12 @@ static void ttyin(char buf[], register int nmax, char pchar)
 	sp = buf;
 	maxlen = 0;
 	while (sp - buf < nmax) {
-		if (promptlen > maxlen)
-			maxlen = promptlen;
-		c = readch();
+		if (ctl->promptlen > maxlen)
+			maxlen = ctl->promptlen;
+		c = readch(ctl);
 		if (c == '\\') {
 			slash++;
-		} else if (((cc_t) c == otty.c_cc[VERASE]) && !slash) {
+		} else if (((cc_t) c == ctl->otty.c_cc[VERASE]) && !slash) {
 			if (sp > buf) {
 #ifdef HAVE_WIDECHAR
 				if (MB_CUR_MAX > 1) {
@@ -893,46 +914,46 @@ static void ttyin(char buf[], register int nmax, char pchar)
 					}
 
 					while (mblength--) {
-						--promptlen;
+						ctl->promptlen--;
 						--sp;
 					}
 				} else
 #endif	/* HAVE_WIDECHAR */
 				{
-					--promptlen;
+					ctl->promptlen--;
 					ERASEONECOLUMN-- sp;
 				}
 
 				if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) {
-					--promptlen;
+					ctl->promptlen--;
 				ERASEONECOLUMN}
 				continue;
 			} else {
-				if (!eraseln)
-					promptlen = maxlen;
-				siglongjmp(restore, 1);
+				if (!ctl->eraseln)
+					ctl->promptlen = maxlen;
+				siglongjmp(ctl->restore, 1);
 			}
-		} else if (((cc_t) c == otty.c_cc[VKILL]) && !slash) {
-			if (hard) {
-				show(c);
+		} else if (((cc_t) c == ctl->otty.c_cc[VKILL]) && !slash) {
+			if (ctl->hard) {
+				show(ctl, c);
 				putchar('\n');
 				putchar(pchar);
 			} else {
 				putchar('\r');
 				putchar(pchar);
-				if (eraseln)
-					erasep(1);
-				else if (docrtkill)
-					while (promptlen-- > 1)
+				if (ctl->eraseln)
+					erasep(ctl, 1);
+				else if (ctl->docrtkill)
+					while (ctl->promptlen-- > 1)
 						putserr(BSB);
-				promptlen = 1;
+				ctl->promptlen = 1;
 			}
 			sp = buf;
 			fflush(stdout);
 			continue;
 		}
-		if (slash && ((cc_t) c == otty.c_cc[VKILL]
-			      || (cc_t) c == otty.c_cc[VERASE])) {
+		if (slash && ((cc_t) c == ctl->otty.c_cc[VKILL]
+			      || (cc_t) c == ctl->otty.c_cc[VERASE])) {
 			ERASEONECOLUMN-- sp;
 		}
 		if (c != '\\')
@@ -941,23 +962,23 @@ static void ttyin(char buf[], register int nmax, char pchar)
 		if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
 			c += (c == RUBOUT) ? -0100 : 0100;
 			putserr(CARAT);
-			promptlen++;
+			ctl->promptlen++;
 		}
 		if (c != '\n' && c != ESC) {
 			putcerr(c);
-			promptlen++;
+			ctl->promptlen++;
 		} else
 			break;
 	}
 	*--sp = '\0';
-	if (!eraseln)
-		promptlen = maxlen;
+	if (!ctl->eraseln)
+		ctl->promptlen = maxlen;
 	if (sp - buf >= nmax - 1)
-		more_error(_("Line too long"));
+		more_error(ctl, _("Line too long"));
 }
 
 /* return: 0 - unchanged, 1 - changed, -1 - overflow (unchanged) */
-static int expand(char **outbuf, char *inbuf)
+static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 {
 	char *inpstr;
 	char *outstr;
@@ -966,7 +987,7 @@ static int expand(char **outbuf, char *inbuf)
 	int changed = 0;
 	int tempsz, xtra, offset;
 
-	xtra = strlen(fnames[fnum]) + strlen(shell_line) + 1;
+	xtra = strlen(ctl->fnames[ctl->fnum]) + strlen(ctl->shell_line) + 1;
 	tempsz = 200 + xtra;
 	temp = xmalloc(tempsz);
 	inpstr = inbuf;
@@ -980,19 +1001,19 @@ static int expand(char **outbuf, char *inbuf)
 		}
 		switch (c) {
 		case '%':
-			if (!no_intty) {
-				strcpy(outstr, fnames[fnum]);
-				outstr += strlen(fnames[fnum]);
+			if (!ctl->no_intty) {
+				strcpy(outstr, ctl->fnames[ctl->fnum]);
+				outstr += strlen(ctl->fnames[ctl->fnum]);
 				changed++;
 			} else
 				*outstr++ = c;
 			break;
 		case '!':
-			if (!shellp)
-				more_error(_
+			if (!ctl->shellp)
+				more_error(ctl, _
 					   ("No previous command to substitute for"));
-			strcpy(outstr, shell_line);
-			outstr += strlen(shell_line);
+			strcpy(outstr, ctl->shell_line);
+			outstr += strlen(ctl->shell_line);
 			changed++;
 			break;
 		case '\\':
@@ -1009,28 +1030,28 @@ static int expand(char **outbuf, char *inbuf)
 	return (changed);
 }
 
-static void set_tty(void)
+static void set_tty(struct more_control *ctl)
 {
-	otty.c_lflag &= ~(ICANON | ECHO);
-	otty.c_cc[VMIN] = 1;	/* read at least 1 char */
-	otty.c_cc[VTIME] = 0;	/* no timeout */
-	stty(fileno(stderr), &otty);
+	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);
 }
 
 /* Come here if a quit signal is received */
 static void onquit(int dummy __attribute__((__unused__)))
 {
 	signal(SIGQUIT, SIG_IGN);
-	if (!inwait) {
+	if (!global_ctl->inwait) {
 		putchar('\n');
-		if (!startup) {
+		if (!global_ctl->startup) {
 			signal(SIGQUIT, onquit);
-			siglongjmp(restore, 1);
+			siglongjmp(global_ctl->restore, 1);
 		} else
-			Pause++;
-	} else if (!dum_opt && notell) {
-		promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
-		notell = 0;
+			global_ctl->Pause = 1;
+	} else if (!global_ctl->dum_opt && global_ctl->notell) {
+		global_ctl->promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
+		global_ctl->notell = 0;
 	}
 	signal(SIGQUIT, onquit);
 }
@@ -1042,7 +1063,7 @@ static void onsusp(int dummy __attribute__((__unused__)))
 
 	/* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
 	signal(SIGTTOU, SIG_IGN);
-	reset_tty();
+	reset_tty(global_ctl);
 	fflush(stdout);
 	signal(SIGTTOU, SIG_DFL);
 	/* Send the TSTP signal to suspend our process group */
@@ -1060,12 +1081,12 @@ static void onsusp(int dummy __attribute__((__unused__)))
 
 	/* We're back */
 	signal(SIGTSTP, onsusp);
-	set_tty();
-	if (inwait)
-		siglongjmp(restore, 1);
+	set_tty(global_ctl);
+	if (global_ctl->inwait)
+		siglongjmp(global_ctl->restore, 1);
 }
 
-static void execute(char *filename, char *cmd, ...)
+static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 {
 	int id;
 	int n;
@@ -1075,7 +1096,7 @@ static void execute(char *filename, char *cmd, ...)
 	int argcount;
 
 	fflush(stdout);
-	reset_tty();
+	reset_tty(ctl);
 	for (n = 10; (id = fork()) < 0 && n > 0; n--)
 		sleep(5);
 	if (id == 0) {
@@ -1113,101 +1134,101 @@ static void execute(char *filename, char *cmd, ...)
 	if (id > 0) {
 		signal(SIGINT, SIG_IGN);
 		signal(SIGQUIT, SIG_IGN);
-		if (catch_susp)
+		if (ctl->catch_susp)
 			signal(SIGTSTP, SIG_DFL);
 		while (wait(0) > 0) ;
 		signal(SIGINT, end_it);
 		signal(SIGQUIT, onquit);
-		if (catch_susp)
+		if (ctl->catch_susp)
 			signal(SIGTSTP, onsusp);
 	} else
 		putserr(_("can't fork\n"));
-	set_tty();
+	set_tty(ctl);
 	puts("------------------------");
-	prompt(filename);
+	prompt(ctl, filename);
 }
 
-static void do_shell(char *filename)
+static void do_shell(struct more_control *ctl, char *filename)
 {
 	char cmdbuf[COMMAND_BUF];
 	int rc;
 	char *expanded;
 
-	kill_line();
+	kill_line(ctl);
 	putchar('!');
 	fflush(stdout);
-	promptlen = 1;
-	if (lastp)
-		putsout(shell_line);
+	ctl->promptlen = 1;
+	if (ctl->lastp)
+		putsout(ctl->shell_line);
 	else {
-		ttyin(cmdbuf, sizeof(cmdbuf) - 2, '!');
+		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
 		expanded = 0;
-		rc = expand(&expanded, cmdbuf);
+		rc = expand(ctl, &expanded, cmdbuf);
 		if (expanded) {
-			if (strlen(expanded) < sizeof(shell_line))
-				strcpy(shell_line, expanded);
+			if (strlen(expanded) < sizeof(ctl->shell_line))
+				strcpy(ctl->shell_line, expanded);
 			else
 				rc = -1;
 			free(expanded);
 		}
 		if (rc < 0) {
 			putserr(_("  Overflow\n"));
-			prompt(filename);
+			prompt(ctl, filename);
 			return;
 		} else if (rc > 0) {
-			kill_line();
-			promptlen = printf("!%s", shell_line);
+			kill_line(ctl);
+			ctl->promptlen = printf("!%s", ctl->shell_line);
 		}
 	}
 	fflush(stdout);
 	putcerr('\n');
-	promptlen = 0;
-	shellp = 1;
-	execute(filename, shell, shell, "-c", shell_line, 0);
+	ctl->promptlen = 0;
+	ctl->shellp = 1;
+	execute(ctl, filename, ctl->shell, ctl->shell, "-c", ctl->shell_line, 0);
 }
 
 /* Execute a colon-prefixed command.  Returns <0 if not a command that
  * should cause more of the file to be printed. */
-static int colon(char *filename, int cmd, int nlines)
+static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 {
 	if (cmd == 0)
-		ch = readch();
+		ctl->ch = readch(ctl);
 	else
-		ch = cmd;
-	lastcolon = ch;
-	switch (ch) {
+		ctl->ch = cmd;
+	ctl->lastcolon = ctl->ch;
+	switch (ctl->ch) {
 	case 'f':
-		kill_line();
-		if (!no_intty)
-			promptlen =
-			    printf(_("\"%s\" line %d"), fnames[fnum], Currline);
+		kill_line(ctl);
+		if (!ctl->no_intty)
+			ctl->promptlen =
+			    printf(_("\"%s\" line %d"), ctl->fnames[ctl->fnum], ctl->Currline);
 		else
-			promptlen = printf(_("[Not a file] line %d"), Currline);
+			ctl->promptlen = printf(_("[Not a file] line %d"), ctl->Currline);
 		fflush(stdout);
 		return (-1);
 	case 'n':
 		if (nlines == 0) {
-			if (fnum >= nfiles - 1)
+			if (ctl->fnum >= ctl->nfiles - 1)
 				end_it(0);
 			nlines++;
 		}
 		putchar('\r');
-		erasep(0);
-		skipf(nlines);
+		erasep(ctl, 0);
+		skipf(ctl, nlines);
 		return (0);
 	case 'p':
-		if (no_intty) {
+		if (ctl->no_intty) {
 			ringbell();
 			return (-1);
 		}
 		putchar('\r');
-		erasep(0);
+		erasep(ctl, 0);
 		if (nlines == 0)
 			nlines++;
-		skipf(-nlines);
+		skipf(ctl, -nlines);
 		return (0);
 	case '!':
-		do_shell(filename);
+		do_shell(ctl, filename);
 		return (-1);
 	case 'q':
 	case 'Q':
@@ -1219,7 +1240,7 @@ static int colon(char *filename, int cmd, int nlines)
 }
 
 /* Skip n lines in the file f */
-static void skiplns(register int n, register FILE *f)
+static void skiplns(struct more_control *ctl, register int n, register FILE *f)
 {
 	register int c;
 
@@ -1228,47 +1249,47 @@ static void skiplns(register int n, register FILE *f)
 			if (c == EOF)
 				return;
 		n--;
-		Currline++;
+		ctl->Currline++;
 	}
 }
 
 /*  Clear the screen */
-static void doclear(void)
+static void doclear(struct more_control *ctl)
 {
-	if (Clear && !hard) {
-		my_putstring(Clear);
+	if (ctl->Clear && !ctl->hard) {
+		my_putstring(ctl->Clear);
 		/* Put out carriage return so that system doesn't get
 		 * confused by escape sequences when expanding tabs */
 		putchar('\r');
-		promptlen = 0;
+		ctl->promptlen = 0;
 	}
 }
 
-static void rdline(register FILE *f)
+static void rdline(struct more_control *ctl, register FILE *f)
 {
 	register int c;
 	register char *p;
 
-	prepare_line_buffer();
+	prepare_line_buffer(ctl);
 
-	p = Line;
+	p = ctl->Line;
 	while ((c = Getc(f)) != '\n' && c != EOF
-	       && (size_t)(p - Line) < LineLen - 1)
+	       && (size_t)(p - ctl->Line) < ctl->LineLen - 1)
 		*p++ = c;
 	if (c == '\n')
-		Currline++;
+		ctl->Currline++;
 	*p = '\0';
 }
 
 /* Go to home position */
-static void home(void)
+static void home(struct more_control *ctl)
 {
-	my_putstring(Home);
+	my_putstring(ctl->Home);
 }
 
 /* Search for nth occurrence of regular expression contained in buf in
  * the file */
-static void search(char buf[], FILE *file, register int n)
+static void search(struct more_control *ctl, char buf[], FILE *file, register int n)
 {
 	long startline = Ftell(file);
 	register long line1 = startline;
@@ -1278,51 +1299,51 @@ static void search(char buf[], FILE *file, register int n)
 	int saveln, rc;
 	regex_t re;
 
-	context.line = saveln = Currline;
-	context.chrctr = startline;
+	ctl->context.line = saveln = ctl->Currline;
+	ctl->context.chrctr = startline;
 	lncount = 0;
 	if (!buf)
 		goto notfound;
 	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
 		char s[REGERR_BUF];
 		regerror(rc, &re, s, sizeof s);
-		more_error(s);
+		more_error(ctl, s);
 	}
 	while (!feof(file)) {
 		line3 = line2;
 		line2 = line1;
 		line1 = Ftell(file);
-		rdline(file);
+		rdline(ctl, file);
 		lncount++;
-		if (regexec(&re, Line, 0, NULL, 0) == 0) {
+		if (regexec(&re, ctl->Line, 0, NULL, 0) == 0) {
 			if (--n == 0) {
-				if (lncount > 3 || (lncount > 1 && no_intty)) {
+				if (lncount > 3 || (lncount > 1 && ctl->no_intty)) {
 					putchar('\n');
-					if (clreol)
-						cleareol();
+					if (ctl->clreol)
+						cleareol(ctl);
 					putsout(_("...skipping\n"));
 				}
-				if (!no_intty) {
-					Currline -=
+				if (!ctl->no_intty) {
+					ctl->Currline -=
 					    (lncount >= 3 ? 3 : lncount);
 					Fseek(file, line3);
-					if (noscroll) {
-						if (clreol) {
-							home();
-							cleareol();
+					if (ctl->noscroll) {
+						if (ctl->clreol) {
+							home(ctl);
+							cleareol(ctl);
 						} else
-							doclear();
+							doclear(ctl);
 					}
 				} else {
-					kill_line();
-					if (noscroll) {
-						if (clreol) {
-							home();
-							cleareol();
+					kill_line(ctl);
+					if (ctl->noscroll) {
+						if (ctl->clreol) {
+							home(ctl);
+							cleareol(ctl);
 						} else
-							doclear();
+							doclear(ctl);
 					}
-					puts(Line);
+					puts(ctl->Line);
 				}
 				break;
 			}
@@ -1330,17 +1351,17 @@ static void search(char buf[], FILE *file, register int n)
 	}
 	regfree(&re);
 	if (feof(file)) {
-		if (!no_intty) {
-			Currline = saveln;
+		if (!ctl->no_intty) {
+			ctl->Currline = saveln;
 			Fseek(file, startline);
 		} else {
 			putsout(_("\nPattern not found\n"));
 			end_it(0);
 		}
-		free(previousre);
-		previousre = NULL;
+		free(ctl->previousre);
+		ctl->previousre = NULL;
 notfound:
-		more_error(_("Pattern not found"));
+		more_error(ctl, _("Pattern not found"));
 	}
 }
 
@@ -1348,7 +1369,7 @@ notfound:
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
  * display in the current file, zero is returned. */
-static int command(char *filename, register FILE *f)
+static int command(struct more_control *ctl, char *filename, register FILE *f)
 {
 	register int nlines;
 	register int retval = 0;
@@ -1360,30 +1381,30 @@ static int command(char *filename, register FILE *f)
 #define ret(val) retval=val;done++;break
 
 	done = 0;
-	if (!errors)
-		prompt(filename);
+	if (!ctl->errors)
+		prompt(ctl, filename);
 	else
-		errors = 0;
+		ctl->errors = 0;
 	for (;;) {
-		nlines = number(&comchar);
-		lastp = colonch = 0;
+		nlines = number(ctl, &comchar);
+		ctl->lastp = colonch = 0;
 		if (comchar == '.') {	/* Repeat last command */
-			lastp++;
-			comchar = lastcmd;
-			nlines = lastarg;
-			if (lastcmd == ':')
-				colonch = lastcolon;
+			ctl->lastp++;
+			comchar = ctl->lastcmd;
+			nlines = ctl->lastarg;
+			if (ctl->lastcmd == ':')
+				colonch = ctl->lastcolon;
 		}
-		lastcmd = comchar;
-		lastarg = nlines;
-		if ((cc_t) comchar == otty.c_cc[VERASE]) {
-			kill_line();
-			prompt(filename);
+		ctl->lastcmd = comchar;
+		ctl->lastarg = nlines;
+		if ((cc_t) comchar == ctl->otty.c_cc[VERASE]) {
+			kill_line(ctl);
+			prompt(ctl, filename);
 			continue;
 		}
 		switch (comchar) {
 		case ':':
-			retval = colon(filename, colonch, nlines);
+			retval = colon(ctl, filename, colonch, nlines);
 			if (retval >= 0)
 				done++;
 			break;
@@ -1392,7 +1413,7 @@ static int command(char *filename, register FILE *f)
 			{
 				register int initline;
 
-				if (no_intty) {
+				if (ctl->no_intty) {
 					ringbell();
 					return (-1);
 				}
@@ -1401,43 +1422,43 @@ static int command(char *filename, register FILE *f)
 					nlines++;
 
 				putchar('\r');
-				erasep(0);
+				erasep(ctl, 0);
 				putchar('\n');
-				if (clreol)
-					cleareol();
+				if (ctl->clreol)
+					cleareol(ctl);
 				printf(P_("...back %d page",
 					"...back %d pages", nlines),
 					nlines);
-				if (clreol)
-					cleareol();
+				if (ctl->clreol)
+					cleareol(ctl);
 				putchar('\n');
 
-				initline = Currline - dlines * (nlines + 1);
-				if (!noscroll)
+				initline = ctl->Currline - ctl->dlines * (nlines + 1);
+				if (!ctl->noscroll)
 					--initline;
 				if (initline < 0)
 					initline = 0;
 				Fseek(f, 0L);
-				Currline = 0;	/* skiplns() will make Currline correct */
-				skiplns(initline, f);
-				if (!noscroll) {
-					ret(dlines + 1);
+				ctl->Currline = 0;	/* skiplns() will make Currline correct */
+				skiplns(ctl, initline, f);
+				if (!ctl->noscroll) {
+					ret(ctl->dlines + 1);
 				} else {
-					ret(dlines);
+					ret(ctl->dlines);
 				}
 			}
 		case ' ':
 		case 'z':
 			if (nlines == 0)
-				nlines = dlines;
+				nlines = ctl->dlines;
 			else if (comchar == 'z')
-				dlines = nlines;
+				ctl->dlines = nlines;
 			ret(nlines);
 		case 'd':
 		case ctrl('D'):
 			if (nlines != 0)
-				nscroll = nlines;
-			ret(nscroll);
+				ctl->nscroll = nlines;
+			ret(ctl->nscroll);
 		case 'q':
 		case 'Q':
 			end_it(0);
@@ -1447,18 +1468,18 @@ static int command(char *filename, register FILE *f)
 			if (nlines == 0)
 				nlines++;
 			if (comchar == 'f')
-				nlines *= dlines;
+				nlines *= ctl->dlines;
 			putchar('\r');
-			erasep(0);
+			erasep(ctl, 0);
 			putchar('\n');
-			if (clreol)
-				cleareol();
+			if (ctl->clreol)
+				cleareol(ctl);
 			printf(P_("...skipping %d line",
 				"...skipping %d lines", nlines),
 				nlines);
 
-			if (clreol)
-				cleareol();
+			if (ctl->clreol)
+				cleareol(ctl);
 			putchar('\n');
 
 			while (nlines > 0) {
@@ -1468,74 +1489,74 @@ static int command(char *filename, register FILE *f)
 						done++;
 						goto endsw;
 					}
-				Currline++;
+				ctl->Currline++;
 				nlines--;
 			}
-			ret(dlines);
+			ret(ctl->dlines);
 		case '\n':
 			if (nlines != 0)
-				dlines = nlines;
+				ctl->dlines = nlines;
 			else
 				nlines = 1;
 			ret(nlines);
 		case '\f':
-			if (!no_intty) {
-				doclear();
-				Fseek(f, screen_start.chrctr);
-				Currline = screen_start.line;
-				ret(dlines);
+			if (!ctl->no_intty) {
+				doclear(ctl);
+				Fseek(f, ctl->screen_start.chrctr);
+				ctl->Currline = ctl->screen_start.line;
+				ret(ctl->dlines);
 			} else {
 				ringbell();
 				break;
 			}
 		case '\'':
-			if (!no_intty) {
-				kill_line();
+			if (!ctl->no_intty) {
+				kill_line(ctl);
 				putsout(_("\n***Back***\n\n"));
-				Fseek(f, context.chrctr);
-				Currline = context.line;
-				ret(dlines);
+				Fseek(f, ctl->context.chrctr);
+				ctl->Currline = ctl->context.line;
+				ret(lines);
 			} else {
 				ringbell();
 				break;
 			}
 		case '=':
-			kill_line();
-			promptlen = printf("%d", Currline);
+			kill_line(ctl);
+			ctl->promptlen = printf("%d", ctl->Currline);
 			fflush(stdout);
 			break;
 		case 'n':
-			if (!previousre) {
-				more_error(_("No previous regular expression"));
+			if (!ctl->previousre) {
+				more_error(ctl, _("No previous regular expression"));
 				break;
 			}
-			lastp++;
+			ctl->lastp = 1;
 			/* fall through */
 		case '/':
 			if (nlines == 0)
 				nlines++;
-			kill_line();
+			kill_line(ctl);
 			putchar('/');
-			promptlen = 1;
+			ctl->promptlen = 1;
 			fflush(stdout);
-			if (lastp) {
+			if (ctl->lastp) {
 				putcerr('\r');
-				search(previousre, f, nlines);
+				search(ctl, ctl->previousre, f, nlines);
 			} else {
-				ttyin(cmdbuf, sizeof(cmdbuf) - 2, '/');
+				ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '/');
 				putcerr('\r');
-				free(previousre);
-				previousre = xstrdup(cmdbuf);
-				search(cmdbuf, f, nlines);
+				free(ctl->previousre);
+				ctl->previousre = xstrdup(cmdbuf);
+				search(ctl, cmdbuf, f, nlines);
 			}
-			ret(dlines - 1);
+			ret(ctl->dlines - 1);
 		case '!':
-			do_shell(filename);
+			do_shell(ctl, filename);
 			break;
 		case '?':
 		case 'h':
-			if (noscroll)
-				doclear();
+			if (ctl->noscroll)
+				doclear(ctl);
 			putsout(_("\n"
 				  "Most commands optionally preceded by integer argument k.  "
 				  "Defaults in brackets.\n"
@@ -1564,16 +1585,16 @@ static int command(char *filename, register FILE *f)
 				 ".                       Repeat previous command\n"));
 			puts("---------------------------------------"
 			     "----------------------------------------");
-			prompt(filename);
+			prompt(ctl, filename);
 			break;
 		case 'v':	/* This case should go right before default */
-			if (!no_intty) {
+			if (!ctl->no_intty) {
 				/* Earlier: call vi +n file. This also
 				 * works for emacs.  POSIX: call vi -c n
 				 * file (when editor is vi or ex). */
 				char *editor, *p;
-				int n = (Currline - dlines <= 0 ? 1 :
-					 Currline - (dlines + 1) / 2);
+				int n = (ctl->Currline - ctl->dlines <= 0 ? 1 :
+					 ctl->Currline - (ctl->dlines + 1) / 2);
 				int split = 0;
 
 				editor = getenv("VISUAL");
@@ -1594,33 +1615,33 @@ static int command(char *filename, register FILE *f)
 					sprintf(cmdbuf, "+%d", n);
 				}
 
-				kill_line();
+				kill_line(ctl);
 				printf("%s %s %s", editor, cmdbuf,
-				       fnames[fnum]);
+				       ctl->fnames[ctl->fnum]);
 				if (split) {
 					cmdbuf[2] = 0;
-					execute(filename, editor, editor,
+					execute(ctl, filename, editor, editor,
 						cmdbuf, cmdbuf + 3,
-						fnames[fnum], (char *)0);
+						ctl->fnames[ctl->fnum], (char *)0);
 				} else
-					execute(filename, editor, editor,
-						cmdbuf, fnames[fnum],
+					execute(ctl, filename, editor, editor,
+						cmdbuf, ctl->fnames[ctl->fnum],
 						(char *)0);
 				break;
 			}
 			/* fall through */
 		default:
-			if (dum_opt) {
-				kill_line();
-				if (Senter && Sexit) {
-					my_putstring(Senter);
-					promptlen =
+			if (ctl->dum_opt) {
+				kill_line(ctl);
+				if (ctl->Senter && ctl->Sexit) {
+					my_putstring(ctl->Senter);
+					ctl->promptlen =
 					    printf(_
 						   ("[Press 'h' for instructions.]"))
-					    + 2 * soglitch;
-					my_putstring(Sexit);
+					    + 2 * ctl->soglitch;
+					my_putstring(ctl->Sexit);
 				} else
-					promptlen =
+					ctl->promptlen =
 					    printf(_
 						   ("[Press 'h' for instructions.]"));
 				fflush(stdout);
@@ -1633,13 +1654,13 @@ static int command(char *filename, register FILE *f)
 	}
 	putchar('\r');
  endsw:
-	inwait = 0;
-	notell++;
+	ctl->inwait = 0;
+	ctl->notell = 1;
 	return (retval);
 }
 
 /* Print out the contents of the file f, one screenful at a time. */
-static void screen(register FILE *f, register int num_lines)
+static void screen(struct more_control *ctl, register FILE *f, register int num_lines)
 {
 	register int c;
 	register int nchars;
@@ -1647,67 +1668,67 @@ static void screen(register FILE *f, register int num_lines)
 	static int prev_len = 1;	/* length of previous line */
 
 	for (;;) {
-		while (num_lines > 0 && !Pause) {
-			if ((nchars = get_line(f, &length)) == EOF) {
-				if (clreol)
-					clreos();
+		while (num_lines > 0 && !ctl->Pause) {
+			if ((nchars = get_line(ctl, f, &length)) == EOF) {
+				if (ctl->clreol)
+					clreos(ctl);
 				return;
 			}
-			if (ssp_opt && length == 0 && prev_len == 0)
+			if (ctl->ssp_opt && length == 0 && prev_len == 0)
 				continue;
 			prev_len = length;
-			if (bad_so
-			    || ((Senter && *Senter == ' ') && (promptlen > 0)))
-				erasep(0);
+			if (ctl->bad_so
+			    || ((ctl->Senter && *ctl->Senter == ' ') && (ctl->promptlen > 0)))
+				erasep(ctl, 0);
 			/* must clear before drawing line since tabs on
 			 * some terminals do not erase what they tab
 			 * over. */
-			if (clreol)
-				cleareol();
-			prbuf(Line, length);
-			if (nchars < promptlen)
-				erasep(nchars);	/* erasep () sets promptlen to 0 */
+			if (ctl->clreol)
+				cleareol(ctl);
+			prbuf(ctl, ctl->Line, length);
+			if (nchars < ctl->promptlen)
+				erasep(ctl, nchars);	/* erasep () sets promptlen to 0 */
 			else
-				promptlen = 0;
+				ctl->promptlen = 0;
 			/* is this needed?
 			 * if (clreol)
 			 *	cleareol();     * must clear again in case we wrapped *
 			 */
-			if (nchars < Mcol || !fold_opt)
-				prbuf("\n", 1);	/* will turn off UL if necessary */
+			if (nchars < ctl->Mcol || !ctl->fold_opt)
+				prbuf(ctl, "\n", 1);	/* will turn off UL if necessary */
 			if (nchars == STOP)
 				break;
 			num_lines--;
 		}
-		if (pstate) {
-			my_putstring(ULexit);
-			pstate = 0;
+		if (ctl->pstate) {
+			my_putstring(ctl->ULexit);
+			ctl->pstate = 0;
 		}
 		fflush(stdout);
 		if ((c = Getc(f)) == EOF) {
-			if (clreol)
-				clreos();
+			if (ctl->clreol)
+				clreos(ctl);
 			return;
 		}
 
-		if (Pause && clreol)
-			clreos();
+		if (ctl->Pause && ctl->clreol)
+			clreos(ctl);
 		Ungetc(c, f);
-		sigsetjmp(restore, 1);
-		Pause = 0;
-		startup = 0;
-		if ((num_lines = command(NULL, f)) == 0)
+		sigsetjmp(ctl->restore, 1);
+		ctl->Pause = 0;
+		ctl->startup = 0;
+		if ((num_lines = command(ctl, NULL, f)) == 0)
 			return;
-		if (hard && promptlen > 0)
-			erasep(0);
-		if (noscroll && num_lines >= dlines) {
-			if (clreol)
-				home();
+		if (ctl->hard && ctl->promptlen > 0)
+			erasep(ctl, 0);
+		if (ctl->noscroll && num_lines >= ctl->dlines) {
+			if (ctl->clreol)
+				home(ctl);
 			else
-				doclear();
+				doclear(ctl);
 		}
-		screen_start.line = Currline;
-		screen_start.chrctr = Ftell(f);
+		ctl->screen_start.line = ctl->Currline;
+		ctl->screen_start.chrctr = Ftell(f);
 	}
 }
 
@@ -1720,14 +1741,14 @@ static void chgwinsz(int dummy __attribute__((__unused__)))
 	signal(SIGWINCH, SIG_IGN);
 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
 		if (win.ws_row != 0) {
-			Lpp = win.ws_row;
-			nscroll = Lpp / 2 - 1;
-			if (nscroll <= 0)
-				nscroll = 1;
-			dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
+			global_ctl->Lpp = win.ws_row;
+			global_ctl->nscroll = global_ctl->Lpp / 2 - 1;
+			if (global_ctl->nscroll <= 0)
+				global_ctl->nscroll = 1;
+			global_ctl->dlines = global_ctl->Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
 		}
 		if (win.ws_col != 0)
-			Mcol = win.ws_col;
+			global_ctl->Mcol = win.ws_col;
 	}
 	signal(SIGWINCH, chgwinsz);
 }
@@ -1743,9 +1764,9 @@ static void copy_file(register FILE *f)
 }
 
 /*----------------------------- Terminal I/O -------------------------------*/
-static void initterm(void)
+static void initterm(struct more_control *ctl)
 {
-	int ret;
+	int ret, tmp;
 	char *padstr;
 	char *term;
 	struct winsize win;
@@ -1755,11 +1776,11 @@ static void initterm(void)
 #endif
 
 #ifndef NON_INTERACTIVE_MORE
-	no_tty = tcgetattr(fileno(stdout), &otty);
+	ctl->no_tty = tcgetattr(fileno(stdout), &ctl->otty);
 #endif
-	if (!no_tty) {
-		docrterase = (otty.c_cc[VERASE] != 255);
-		docrtkill = (otty.c_cc[VKILL] != 255);
+	if (!ctl->no_tty) {
+		ctl->docrterase = (ctl->otty.c_cc[VERASE] != 255);
+		ctl->docrtkill = (ctl->otty.c_cc[VKILL] != 255);
 #ifdef do_SIGTTOU
 		{
 			int tgrp;
@@ -1774,46 +1795,49 @@ static void initterm(void)
 		}
 #endif	/* do_SIGTTOU */
 		if ((term = getenv("TERM")) == NULL) {
-			dumb++;
-			ul_opt = 0;
+			ctl->dumb = 1;
+			ctl->ul_opt = 0;
 		}
 		my_setupterm(term, 1, &ret);
 		if (ret <= 0) {
-			dumb++;
-			ul_opt = 0;
+			ctl->dumb = 1;
+			ctl->ul_opt = 0;
 		} else {
 #ifdef TIOCGWINSZ
 			if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
 #endif
-				Lpp = my_tgetnum(TERM_LINES);
-				Mcol = my_tgetnum(TERM_COLS);
+				ctl->Lpp = my_tgetnum(TERM_LINES);
+				ctl->Mcol = my_tgetnum(TERM_COLS);
 #ifdef TIOCGWINSZ
 			} else {
-				if ((Lpp = win.ws_row) == 0)
-					Lpp = my_tgetnum(TERM_LINES);
-				if ((Mcol = win.ws_col) == 0)
-					Mcol = my_tgetnum(TERM_COLS);
+				if ((ctl->Lpp = win.ws_row) == 0)
+					ctl->Lpp = my_tgetnum(TERM_LINES);
+				if ((ctl->Mcol = win.ws_col) == 0)
+					ctl->Mcol = my_tgetnum(TERM_COLS);
 			}
 #endif
-			if ((Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) {
-				hard++;	/* Hard copy terminal */
-				Lpp = LINES_PER_PAGE;
+			if ((ctl->Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) {
+				ctl->hard = 1;	/* Hard copy terminal */
+				ctl->Lpp = LINES_PER_PAGE;
 			}
 
 			if (my_tgetflag(TERM_EAT_NEW_LINE))
 				/* Eat newline at last column + 1; dec, concept */
-				eatnl++;
-			if (Mcol <= 0)
-				Mcol = NUM_COLUMNS;
-
-			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;
+				ctl->eatnl = 1;
+			if (ctl->Mcol <= 0)
+				ctl->Mcol = NUM_COLUMNS;
+
+			ctl->Wrap = my_tgetflag(TERM_AUTO_RIGHT_MARGIN);
+			ctl->bad_so = my_tgetflag(TERM_CEOL);
+			ctl->eraseln = my_tgetstr(TERM_CLEAR_TO_LINE_END);
+			ctl->Clear = my_tgetstr(TERM_CLEAR);
+			ctl->Senter = my_tgetstr(TERM_STANDARD_MODE);
+			ctl->Sexit = my_tgetstr(TERM_EXIT_STANDARD_MODE);
+			tmp = my_tgetnum(TERM_STD_MODE_GLITCH);
+			if (tmp < 0)
+				ctl->soglitch = 0;
+			else if (0 < tmp)
+				ctl->soglitch = 1;
 
 			/* Set up for underlining:  some terminals don't
 			 * need it; others have start/stop sequences,
@@ -1823,55 +1847,55 @@ static void initterm(void)
 			 * available, settle for standout sequence. */
 			if (my_tgetflag(TERM_UNDERLINE)
 			    || my_tgetflag(TERM_OVER_STRIKE))
-				ul_opt = 0;
-			if ((chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
-				chUL = "";
-			if (((ULenter =
+				ctl->ul_opt = 0;
+			if ((ctl->chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
+				ctl->chUL = "";
+			if (((ctl->ULenter =
 			      my_tgetstr(TERM_ENTER_UNDERLINE)) == NULL
-			     || (ULexit =
+			     || (ctl->ULexit =
 				 my_tgetstr(TERM_EXIT_UNDERLINE)) == NULL)
-			    && !*chUL) {
-				if ((ULenter = Senter) == NULL
-				    || (ULexit = Sexit) == NULL) {
-					ULenter = "";
-					ULexit = "";
+			    && !*ctl->chUL) {
+				if ((ctl->ULenter = ctl->Senter) == NULL
+				    || (ctl->ULexit = ctl->Sexit) == NULL) {
+					ctl->ULenter = "";
+					ctl->ULexit = "";
 				} else
-					ulglitch = soglitch;
+					ctl->ulglitch = ctl->soglitch;
 			} else {
-				ulglitch = 0;
+				ctl->ulglitch = 0;
 			}
 
 			if ((padstr = my_tgetstr(TERM_PAD_CHAR)) != NULL)
-				PC = *padstr;
-			Home = my_tgetstr(TERM_HOME);
-			if (Home == NULL || *Home == '\0') {
-				if ((cursorm =
+				ctl->PC = *padstr;
+			ctl->Home = my_tgetstr(TERM_HOME);
+			if (ctl->Home == NULL || *ctl->Home == '\0') {
+				if ((ctl->cursorm =
 				     my_tgetstr(TERM_CURSOR_ADDRESS)) != NULL) {
 					const char *t =
-					    (const char *)my_tgoto(cursorm, 0,
+					    (const char *)my_tgoto(ctl->cursorm, 0,
 								   0);
-					xstrncpy(cursorhome, t,
-						 sizeof(cursorhome));
-					Home = cursorhome;
+					xstrncpy(ctl->cursorhome, t,
+						 sizeof(ctl->cursorhome));
+					ctl->Home = ctl->cursorhome;
 				}
 			}
-			EodClr = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
-			if ((chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL)
-				chBS = "\b";
+			ctl->EodClr = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
+			if ((ctl->chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL)
+				ctl->chBS = "\b";
 
 		}
-		if ((shell = getenv("SHELL")) == NULL)
-			shell = "/bin/sh";
+		if ((ctl->shell = getenv("SHELL")) == NULL)
+			ctl->shell = "/bin/sh";
 	}
-	no_intty = tcgetattr(fileno(stdin), &otty);
-	tcgetattr(fileno(stderr), &otty);
-	savetty0 = otty;
-	slow_tty = cfgetispeed(&otty) < B1200;
-	hardtabs = (otty.c_oflag & TABDLY) != XTABS;
-	if (!no_tty) {
-		otty.c_lflag &= ~(ICANON | ECHO);
-		otty.c_cc[VMIN] = 1;
-		otty.c_cc[VTIME] = 0;
+	ctl->no_intty = tcgetattr(fileno(stdin), &ctl->otty);
+	tcgetattr(fileno(stderr), &ctl->otty);
+	ctl->savetty0 = ctl->otty;
+	ctl->slow_tty = cfgetispeed(&ctl->otty) < B1200 ? 1 : 0;
+	ctl->hardtabs = (ctl->otty.c_oflag & TABDLY) != XTABS;
+	if (!ctl->no_tty) {
+		ctl->otty.c_lflag &= ~(ICANON | ECHO);
+		ctl->otty.c_cc[VMIN] = 1;
+		ctl->otty.c_cc[VTIME] = 0;
 	}
 }
 
@@ -1887,35 +1911,49 @@ int main(int argc, char **argv)
 	int clearit = 0;
 	int initline = 0;
 	char *initbuf = NULL;
+	struct more_control ctl = {
+		.firstf = 1,
+		.fold_opt = 1,
+		.notell = 1,
+		.startup = 1,
+		.stop_opt = 1,
+		.ul_opt = 1,
+		.Wrap = 1,
+		.Lpp = LINES_PER_PAGE,
+		.Mcol = NUM_COLUMNS,
+		.nscroll = SCROLL_LEN,
+		0
+	};
+	global_ctl = &ctl;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 	atexit(close_stdout);
 
-	nfiles = argc;
-	fnames = argv;
+	ctl.nfiles = argc;
+	ctl.fnames = argv;
 	setlocale(LC_ALL, "");
-	initterm();
+	initterm(&ctl);
 
 	/* Auto set no scroll on when binary is called page */
 	if (!(strcmp(program_invocation_short_name, "page")))
-		noscroll++;
+		ctl.noscroll = 1;
 
-	prepare_line_buffer();
+	prepare_line_buffer(&ctl);
 
-	nscroll = Lpp / 2 - 1;
-	if (nscroll <= 0)
-		nscroll = 1;
+	ctl.nscroll = ctl.Lpp / 2 - 1;
+	if (ctl.nscroll <= 0)
+		ctl.nscroll = 1;
 
 	if ((s = getenv("MORE")) != NULL)
-		argscan(s);
+		argscan(&ctl, s);
 
-	while (--nfiles > 0) {
-		if ((c = (*++fnames)[0]) == '-') {
-			argscan(*fnames + 1);
+	while (--ctl.nfiles > 0) {
+		if ((c = (*++ctl.fnames)[0]) == '-') {
+			argscan(&ctl, *ctl.fnames + 1);
 		} else if (c == '+') {
-			s = *fnames;
+			s = *ctl.fnames;
 			if (*++s == '/') {
 				srchopt++;
 				initbuf = xstrdup(s + 1);
@@ -1932,24 +1970,24 @@ int main(int argc, char **argv)
 	}
 	/* allow clreol only if Home and eraseln and EodClr strings are
 	 * defined, and in that case, make sure we are in noscroll mode */
-	if (clreol) {
-		if ((Home == NULL) || (*Home == '\0') ||
-		    (eraseln == NULL) || (*eraseln == '\0') ||
-		    (EodClr == NULL) || (*EodClr == '\0'))
-			clreol = 0;
+	if (ctl.clreol) {
+		if ((ctl.Home == NULL) || (*ctl.Home == '\0') ||
+		    (ctl.eraseln == NULL) || (*ctl.eraseln == '\0') ||
+		    (ctl.EodClr == NULL) || (*ctl.EodClr == '\0'))
+			ctl.clreol = 0;
 		else
-			noscroll = 1;
+			ctl.noscroll = 1;
 	}
-	if (dlines == 0)
-		dlines = Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
-	left = dlines;
-	if (nfiles > 1)
+	if (ctl.dlines == 0)
+		ctl.dlines = ctl.Lpp - 1;	/* was: Lpp - (noscroll ? 1 : 2) */
+	left = ctl.dlines;
+	if (ctl.nfiles > 1)
 		prnames++;
-	if (!no_intty && nfiles == 0)
+	if (!ctl.no_intty && ctl.nfiles == 0)
 		usage(stderr);
 	else
 		f = stdin;
-	if (!no_tty) {
+	if (!ctl.no_tty) {
 		signal(SIGQUIT, onquit);
 		signal(SIGINT, end_it);
 #ifdef SIGWINCH
@@ -1957,106 +1995,108 @@ int main(int argc, char **argv)
 #endif
 		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
 			signal(SIGTSTP, onsusp);
-			catch_susp++;
+			ctl.catch_susp = 1;
 		}
-		stty(fileno(stderr), &otty);
+		stty(fileno(stderr), &ctl.otty);
 	}
-	if (no_intty) {
-		if (no_tty)
+	if (ctl.no_intty) {
+		if (ctl.no_tty)
 			copy_file(stdin);
 		else {
-			if ((c = Getc(f)) == '\f')
-				doclear();
+			ctl.file_pos++;
+			if ((c = getc(f)) == '\f')
+				doclear(&ctl);
 			else {
-				Ungetc(c, f);
-				if (noscroll && (c != EOF)) {
-					if (clreol)
-						home();
+				ctl.file_pos--;
+				ungetc(c, f);
+				if (ctl.noscroll && (c != EOF)) {
+					if (ctl.clreol)
+						home(&ctl);
 					else
-						doclear();
+						doclear(&ctl);
 				}
 			}
 			if (srchopt) {
-				free(previousre);
-				previousre = xstrdup(initbuf);
-				search(initbuf, stdin, 1);
-				if (noscroll)
+				free(ctl.previousre);
+				ctl.previousre = xstrdup(initbuf);
+				search(&ctl, initbuf, stdin, 1);
+				if (ctl.noscroll)
 					left--;
 			} else if (initopt)
-				skiplns(initline, stdin);
-			screen(stdin, left);
+				skiplns(&ctl, initline, stdin);
+			screen(&ctl, stdin, left);
 		}
-		no_intty = 0;
+		ctl.no_intty = 0;
 		prnames++;
-		firstf = 0;
+		ctl.firstf = 0;
 	}
 
-	while (fnum < nfiles) {
-		if ((f = checkf(fnames[fnum], &clearit)) != NULL) {
-			context.line = context.chrctr = 0;
-			Currline = 0;
-			if (firstf)
-				sigsetjmp(restore, 1);
-			if (firstf) {
-				firstf = 0;
+	while (ctl.fnum < ctl.nfiles) {
+		if ((f = checkf(&ctl, ctl.fnames[ctl.fnum], &clearit)) != NULL) {
+			ctl.context.line = ctl.context.chrctr = 0;
+			ctl.Currline = 0;
+			if (ctl.firstf)
+				sigsetjmp(ctl.restore, 1);
+			if (ctl.firstf) {
+				ctl.firstf = 0;
 				if (srchopt) {
-					free(previousre);
-					previousre = xstrdup(initbuf);
-					search(initbuf, f, 1);
-					if (noscroll)
+					free(ctl.previousre);
+					ctl.previousre = xstrdup(initbuf);
+					search(&ctl, initbuf, f, 1);
+					if (ctl.noscroll)
 						left--;
 				} else if (initopt)
-					skiplns(initline, f);
-			} else if (fnum < nfiles && !no_tty) {
-				sigsetjmp(restore, 1);
-				left = command(fnames[fnum], f);
+					skiplns(&ctl, initline, f);
+			} else if (ctl.fnum < ctl.nfiles && !ctl.no_tty) {
+				sigsetjmp(ctl.restore, 1);
+				left = command(&ctl, ctl.fnames[ctl.fnum], f);
 			}
 			if (left != 0) {
-				if ((noscroll || clearit)
-				    && (file_size != LONG_MAX)) {
-					if (clreol)
-						home();
+				if ((ctl.noscroll || clearit)
+				    && (ctl.file_size != LONG_MAX)) {
+					if (ctl.clreol)
+						home(&ctl);
 					else
-						doclear();
+						doclear(&ctl);
 				}
 				if (prnames) {
-					if (bad_so)
-						erasep(0);
-					if (clreol)
-						cleareol();
+					if (ctl.bad_so)
+						erasep(&ctl, 0);
+					if (ctl.clreol)
+						cleareol(&ctl);
 					putsout("::::::::::::::");
-					if (promptlen > 14)
-						erasep(14);
+					if (ctl.promptlen > 14)
+						erasep(&ctl, 14);
 					putchar('\n');
-					if (clreol)
-						cleareol();
-					puts(fnames[fnum]);
-					if (clreol)
-						cleareol();
+					if (ctl.clreol)
+						cleareol(&ctl);
+					puts(ctl.fnames[ctl.fnum]);
+					if (ctl.clreol)
+						cleareol(&ctl);
 					puts("::::::::::::::");
-					if (left > Lpp - 4)
-						left = Lpp - 4;
+					if (left > ctl.Lpp - 4)
+						left = ctl.Lpp - 4;
 				}
-				if (no_tty)
+				if (ctl.no_tty)
 					copy_file(f);
 				else {
-					within++;
-					screen(f, left);
-					within = 0;
+					ctl.within = 1;
+					screen(&ctl, f, left);
+					ctl.within = 0;
 				}
 			}
-			sigsetjmp(restore, 1);
+			sigsetjmp(ctl.restore, 1);
 			fflush(stdout);
 			fclose(f);
-			screen_start.line = screen_start.chrctr = 0L;
-			context.line = context.chrctr = 0L;
+			ctl.screen_start.line = ctl.screen_start.chrctr = 0L;
+			ctl.context.line = ctl.context.chrctr = 0L;
 		}
-		fnum++;
-		firstf = 0;
+		ctl.fnum++;
+		ctl.firstf = 0;
 	}
-	free(previousre);
+	free(ctl.previousre);
 	free(initbuf);
-	free(Line);
-	reset_tty();
+	free(ctl.Line);
+	reset_tty(&ctl);
 	exit(EXIT_SUCCESS);
 }
Signature · 1 line
2.3.0
#4 lore raw

PATCH 4/31 more: remove dead code and useless comments

In reply to Sami Kerola

Removed code did not do anything (added features that where never completed?) and comments mostly mislead anyone who read them.

---
 text-utils/more-term.h |   2 -
 text-utils/more.c      | 109 ++++++++++++++-----------------------------------
 2 files changed, 31 insertions(+), 80 deletions(-)
text-utils/more-term.h +0 -2
index f3577f4..b78da5e 100644
@@ -17,7 +17,6 @@
 # define TERM_LINES			"lines"
 # define TERM_LINE_DOWN			"cud1"
 # define TERM_OVER_STRIKE		"os"
-# define TERM_PAD_CHAR			"pad"
 # define TERM_STANDARD_MODE		"smso"
 # define TERM_STD_MODE_GLITCH		"xmc"
 # define TERM_UNDERLINE			"ul"
@@ -72,7 +71,6 @@ static inline char *my_tgoto(char *cap, int col, int row)
 # define TERM_LINES			"li"
 # define TERM_LINE_DOWN			"le"
 # define TERM_OVER_STRIKE		"os"
-# define TERM_PAD_CHAR			"pc"
 # define TERM_STANDARD_MODE		"so"
 # define TERM_STD_MODE_GLITCH		"sg"
 # define TERM_UNDERLINE			"ul"
text-utils/more.c +31 -78

291 lines not on this page · raw message

Signature · 1 line
2.3.0
#5 lore raw

PATCH 5/31 more: return is statement, not a function

In reply to Sami Kerola
---
 text-utils/more.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
text-utils/more.c +21 -21
index 7743570..940d429 100644
@@ -334,20 +334,20 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst
 		if (ctl->clreol)
 			cleareol(ctl);
 		warn(_("stat of %s failed"), fs);
-		return ((FILE *)NULL);
+		return NULL;
 	}
 	if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
 		printf(_("\n*** %s: directory ***\n\n"), fs);
-		return ((FILE *)NULL);
+		return NULL;
 	}
 	if ((f = Fopen(fs, "r")) == NULL) {
 		fflush(stdout);
 		warn(_("cannot open %s"), fs);
-		return ((FILE *)NULL);
+		return NULL;
 	}
 	if (magic(f, fs)) {
 		fclose(f);
-		return ((FILE *)NULL);
+		return NULL;
 	}
 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
 	c = Getc(f);
@@ -355,7 +355,7 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst
 	Ungetc(c, f);
 	if ((ctl->file_size = stbuf.st_size) == 0)
 		ctl->file_size = LONG_MAX;
-	return (f);
+	return f;
 }
 
 static void prepare_line_buffer(struct more_control *ctl)
@@ -462,10 +462,10 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length)
 			if (p > ctl->Line) {
 				*p = '\0';
 				*length = p - ctl->Line;
-				return (column);
+				return column;
 			}
 			*length = p - ctl->Line;
-			return (EOF);
+			return EOF;
 		}
 		if (c == '\n') {
 			ctl->Currline++;
@@ -508,7 +508,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length)
 			ctl->Pause = 1;
 		} else if (c == EOF) {
 			*length = p - ctl->Line;
-			return (column);
+			return column;
 		} else {
 #ifdef HAVE_WIDECHAR
 			if (ctl->fold_opt && MB_CUR_MAX > 1) {
@@ -566,7 +566,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length)
 	}
 	*length = p - ctl->Line;
 	*p = 0;
-	return (column);
+	return column;
 }
 
 /* Erase the rest of the prompt, assuming we are starting at column col. */
@@ -751,7 +751,7 @@ static int readch(struct more_control *ctl)
 		else
 			c = ctl->otty.c_cc[VKILL];
 	}
-	return (c);
+	return c;
 }
 
 /* Read a decimal number from the terminal.  Set cmd to the non-digit
@@ -773,7 +773,7 @@ static int number(struct more_control *ctl, char *cmd)
 			break;
 		}
 	}
-	return (i);
+	return i;
 }
 
 /* Skip nskip files in the file list (from the command line).  Nskip may
@@ -1003,7 +1003,7 @@ static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 	}
 	*outstr++ = '\0';
 	*outbuf = temp;
-	return (changed);
+	return changed;
 }
 
 static void set_tty(struct more_control *ctl)
@@ -1183,7 +1183,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 		else
 			ctl->promptlen = printf(_("[Not a file] line %d"), ctl->Currline);
 		fflush(stdout);
-		return (-1);
+		return -1;
 	case 'n':
 		if (nlines == 0) {
 			if (ctl->fnum >= ctl->nfiles - 1)
@@ -1193,27 +1193,27 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 		putchar('\r');
 		erasep(ctl, 0);
 		skipf(ctl, nlines);
-		return (0);
+		return 0;
 	case 'p':
 		if (ctl->no_intty) {
 			ringbell();
-			return (-1);
+			return -1;
 		}
 		putchar('\r');
 		erasep(ctl, 0);
 		if (nlines == 0)
 			nlines++;
 		skipf(ctl, -nlines);
-		return (0);
+		return 0;
 	case '!':
 		do_shell(ctl, filename);
-		return (-1);
+		return -1;
 	case 'q':
 	case 'Q':
 		end_it(0);
 	default:
 		ringbell();
-		return (-1);
+		return -1;
 	}
 }
 
@@ -1393,7 +1393,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 
 				if (ctl->no_intty) {
 					ringbell();
-					return (-1);
+					return -1;
 				}
 
 				if (nlines == 0)
@@ -1633,7 +1633,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
  endsw:
 	ctl->inwait = 0;
 	ctl->notell = 1;
-	return (retval);
+	return retval;
 }
 
 /* Print out the contents of the file f, one screenful at a time. */
@@ -2051,5 +2051,5 @@ int main(int argc, char **argv)
 	free(initbuf);
 	free(ctl.Line);
 	reset_tty(&ctl);
-	exit(EXIT_SUCCESS);
+	return EXIT_SUCCESS;
 }
Signature · 1 line
2.3.0
#6 lore raw

PATCH 6/31 more: use paths.h to find bourne shell and vi editor

In reply to Sami Kerola
---
 text-utils/more.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
text-utils/more.c +3 -4
index 940d429..ba5459a 100644
@@ -46,6 +46,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <paths.h>
 #include <regex.h>
 #include <setjmp.h>
 #include <signal.h>
@@ -83,8 +84,6 @@
 # define XTABS	TAB3
 #endif
 
-#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))
@@ -1578,7 +1577,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 				if (editor == NULL || *editor == '\0')
 					editor = getenv("EDITOR");
 				if (editor == NULL || *editor == '\0')
-					editor = VI;
+					editor = _PATH_VI;
 
 				p = strrchr(editor, '/');
 				if (p)
@@ -1839,7 +1838,7 @@ static void initterm(struct more_control *ctl)
 
 		}
 		if ((ctl->shell = getenv("SHELL")) == NULL)
-			ctl->shell = "/bin/sh";
+			ctl->shell = _PATH_BSHELL;
 	}
 	ctl->no_intty = tcgetattr(fileno(stdin), &ctl->otty);
 	tcgetattr(fileno(stderr), &ctl->otty);
Signature · 1 line
2.3.0
#7 lore raw

PATCH 7/31 more: remove function like preprocessor defines

In reply to Sami Kerola

This sort of weird abstraction makes code readability worse without much of any sort of gain.

---
 text-utils/more.c | 266 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 147 insertions(+), 119 deletions(-)
text-utils/more.c +147 -119
index ba5459a..01c999e 100644
@@ -84,27 +84,6 @@
 # define XTABS	TAB3
 #endif
 
-#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 ringbell()	putcerr('\007')
-
-static char *BS = "\b";
-static char *BSB = "\b \b";
-static char *CARAT = "^";
-#define ERASEONECOLUMN \
-    if (ctl->docrterase) \
-	putserr(BSB); \
-    else \
-	putserr(BS);
-
 #define TBUFSIZ		1024
 #define LINSIZ		256	/* minimal Line buffer size */
 #define ctrl(letter)	(letter & 077)
@@ -320,6 +299,24 @@ static int magic(FILE *f, char *fs)
 	return 0;
 }
 
+static void set_pos_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);
+}
+
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
 static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst)
@@ -339,7 +336,9 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst
 		printf(_("\n*** %s: directory ***\n\n"), fs);
 		return 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 NULL;
@@ -349,9 +348,9 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst
 		return 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;
@@ -390,7 +389,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 state);
 #endif
@@ -398,10 +397,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
@@ -424,9 +423,9 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length)
 				column++;
 				file_pos_bak++;
 
-				if (column >= ctl->Mcol) {
-					Fseek(f, file_pos_bak);
-				} else {
+				if (column >= ctl->Mcol)
+					set_pos_fseek(ctl, f, file_pos_bak);
+				else {
 					memmove(mbc, mbc + 1, --mbc_pos);
 					if (mbc_pos > 0) {
 						mbc[mbc_pos] = '\0';
@@ -439,7 +438,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);
+					set_pos_fseek(ctl, f, file_pos_bak);
 					break_flag = 1;
 				} else {
 					for (i = 0; p < &ctl->Line[ctl->LineLen - 1] &&
@@ -453,7 +452,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 */
@@ -492,13 +491,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, next, f);
 			column = 0;
 		} else if (c == '\f' && ctl->stop_opt) {
 			p[-1] = '^';
@@ -521,7 +520,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;
@@ -552,7 +551,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) {
@@ -601,12 +600,20 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 	return mblength;
 }
 
+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)
@@ -650,7 +657,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);
 				my_putstring(ctl->chUL);
 			}
 			ctl->pstate = state;
@@ -697,7 +704,7 @@ static void prompt(struct more_control *ctl, char *filename)
 			clreos(ctl);
 		fflush(stdout);
 	} else
-		ringbell();
+		fputc('\a', stderr);
 	ctl->inwait = 1;
 }
 
@@ -718,7 +725,7 @@ static void reset_tty(struct more_control *ctl)
 	ctl->otty.c_lflag |= ICANON | ECHO;
 	ctl->otty.c_cc[VMIN] = ctl->savetty0.c_cc[VMIN];
 	ctl->otty.c_cc[VTIME] = ctl->savetty0.c_cc[VTIME];
-	stty(fileno(stderr), &ctl->savetty0);
+	tcsetattr(STDERR_FILENO, TCSANOW, &ctl->savetty0);
 }
 
 /* Clean up terminal state and exit. Also come here if interrupt signal received */
@@ -733,7 +740,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);
@@ -793,9 +800,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);
@@ -807,10 +814,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("^", stderr);
 		ctl->promptlen++;
 	}
-	putcerr(c);
+	fputc(c, stderr);
 	ctl->promptlen++;
 }
 
@@ -823,15 +830,23 @@ static void more_error(struct more_control *ctl, char *mess)
 	ctl->promptlen += strlen(mess);
 	if (ctl->Senter && ctl->Sexit) {
 		my_putstring(ctl->Senter);
-		putsout(mess);
+		fputs(mess, stdout);
 		my_putstring(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("\b \b", stderr);
+	else
+		fputs("\b", stderr);
+}
+
 static void ttyin(struct more_control *ctl, char buf[], register int nmax, char pchar)
 {
 	char *sp;
@@ -878,15 +893,16 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char
 						pos += mblength;
 					}
 
-					if (mblength == 1) {
-					ERASEONECOLUMN} else {
+					if (mblength == 1)
+						erase_one_column(ctl);
+					else {
 						int wc_width;
 						wc_width = wcwidth(wc);
 						wc_width =
 						    (wc_width <
 						     1) ? 1 : wc_width;
-						while (wc_width--) {
-						ERASEONECOLUMN}
+						while (wc_width--)
+							erase_one_column(ctl);
 					}
 
 					while (mblength--) {
@@ -897,12 +913,14 @@ static void ttyin(struct more_control *ctl, char buf[], register int nmax, char
 #endif	/* HAVE_WIDECHAR */
 				{
 					ctl->promptlen--;
-					ERASEONECOLUMN-- sp;
+					erase_one_column(ctl);
+					sp--;
 				}
 
 				if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) {
 					ctl->promptlen--;
-				ERASEONECOLUMN}
+					erase_one_column(ctl);
+				}
 				continue;
 			} else {
 				if (!ctl->eraseln)
@@ -921,7 +939,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("\b \b", stderr);
 				ctl->promptlen = 1;
 			}
 			sp = buf;
@@ -930,18 +948,19 @@ 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-- sp;
+			erase_one_column(ctl);
+			sp--;
 		}
 		if (c != '\\')
 			slash = 0;
 		*sp++ = c;
 		if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
 			c += (c == RUBOUT) ? -0100 : 0100;
-			putserr(CARAT);
+			fputs("^", stderr);
 			ctl->promptlen++;
 		}
 		if (c != '\n' && c != ESC) {
-			putcerr(c);
+			fputc(c, stderr);
 			ctl->promptlen++;
 		} else
 			break;
@@ -1010,7 +1029,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 */
@@ -1103,7 +1122,7 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 		va_end(argp);
 
 		execvp(cmd, args);
-		putserr(_("exec failed\n"));
+		fputs(_("exec failed\n"), stderr);
 		exit(EXIT_FAILURE);
 	}
 	if (id > 0) {
@@ -1117,7 +1136,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);
@@ -1134,7 +1153,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 = 0;
@@ -1147,7 +1166,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) {
@@ -1156,7 +1175,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);
@@ -1195,7 +1214,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 		return 0;
 	case 'p':
 		if (ctl->no_intty) {
-			ringbell();
+			fputc('\a', stderr);
 			return -1;
 		}
 		putchar('\r');
@@ -1211,7 +1230,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 	case 'Q':
 		end_it(0);
 	default:
-		ringbell();
+		fputc('\a', stderr);
 		return -1;
 	}
 }
@@ -1222,7 +1241,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--;
@@ -1250,7 +1269,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')
@@ -1268,7 +1287,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;
@@ -1289,7 +1308,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) {
@@ -1298,12 +1317,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);
+					set_pos_fseek(ctl, file, line3);
 					if (ctl->noscroll) {
 						if (ctl->clreol) {
 							home(ctl);
@@ -1330,9 +1349,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);
+			set_pos_fseek(ctl, file, startline);
 		} else {
-			putsout(_("\nPattern not found\n"));
+			fputs(_("\nPattern not found\n"), stdout);
 			end_it(0);
 		}
 		free(ctl->previousre);
@@ -1352,17 +1371,14 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 	register int retval = 0;
 	register int c;
 	char colonch;
-	int done;
+	int done = 0;
 	char comchar, cmdbuf[INIT_BUF];
 
-#define ret(val) retval=val;done++;break
-
-	done = 0;
 	if (!ctl->errors)
 		prompt(ctl, filename);
 	else
 		ctl->errors = 0;
-	for (;;) {
+	while (!done) {
 		nlines = number(ctl, &comchar);
 		ctl->lastp = colonch = 0;
 		if (comchar == '.') {	/* Repeat last command */
@@ -1391,7 +1407,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 				register int initline;
 
 				if (ctl->no_intty) {
-					ringbell();
+					fputc('\a', stderr);
 					return -1;
 				}
 
@@ -1415,13 +1431,17 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 					--initline;
 				if (initline < 0)
 					initline = 0;
-				Fseek(f, 0L);
+				set_pos_fseek(ctl, f, 0);
 				ctl->Currline = 0;	/* skiplns() will make Currline correct */
 				skiplns(ctl, initline, f);
 				if (!ctl->noscroll) {
-					ret(ctl->dlines + 1);
+					retval = ctl->dlines + 1;
+					done = 1;
+					break;
 				}
-				ret(ctl->dlines);
+				retval = ctl->dlines;
+				done = 1;
+				break;
 			}
 		case ' ':
 		case 'z':
@@ -1429,12 +1449,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);
@@ -1459,7 +1483,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++;
@@ -1468,34 +1492,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);
+				set_pos_fseek(ctl, f, ctl->screen_start.chrctr);
 				ctl->Currline = ctl->screen_start.line;
-				ret(ctl->dlines);
-			} else {
-				ringbell();
+				retval = ctl->dlines;
+				done = 1;
 				break;
 			}
+			fputc('\a', 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);
+				set_pos_fseek(ctl, f, ctl->context.chrctr);
 				ctl->Currline = ctl->context.line;
-				ret(lines);
-			} else {
-				ringbell();
+				retval = lines;
+				done = 1;
 				break;
 			}
+			fputc('\a', stderr);
+			break;
 		case '=':
 			kill_line(ctl);
 			ctl->promptlen = printf("%d", ctl->Currline);
@@ -1516,16 +1546,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;
@@ -1533,13 +1565,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"));
-			puts("---------------------------------------"
-			     "----------------------------------------");
-			putsout(_
+				  "Star (*) indicates argument becomes new default.\n"), stdout);
+			fputs("---------------------------------------"
+			     "----------------------------------------", stdout);
+			fputs(_
 				("<space>                 Display next k lines of text [current screen size]\n"
 				 "z                       Display next k lines of text [current screen size]*\n"
 				 "<return>                Display next k lines of text [1]*\n"
@@ -1558,9 +1590,9 @@ 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"));
-			puts("---------------------------------------"
-			     "----------------------------------------");
+				 ".                       Repeat previous command\n"), stdout);
+			fputs("---------------------------------------"
+			      "----------------------------------------", stdout);
 			prompt(ctl, filename);
 			break;
 		case 'v':	/* This case should go right before default */
@@ -1622,11 +1654,9 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 						   ("[Press 'h' for instructions.]"));
 				fflush(stdout);
 			} else
-				ringbell();
+				fputc('\a', stderr);
 			break;
 		}
-		if (done)
-			break;
 	}
 	putchar('\r');
  endsw:
@@ -1677,7 +1707,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;
@@ -1685,7 +1715,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;
@@ -1700,7 +1730,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;
 	}
 }
 
@@ -1949,18 +1979,16 @@ int main(int argc, char **argv)
 			signal(SIGTSTP, onsusp);
 			ctl.catch_susp = 1;
 		}
-		stty(fileno(stderr), &ctl.otty);
+		tcsetattr(STDERR_FILENO, TCSANOW, &ctl.otty);
 	}
 	if (ctl.no_intty) {
 		if (ctl.no_tty)
 			copy_file(stdin);
 		else {
-			ctl.file_pos++;
-			if ((c = getc(f)) == '\f')
+			if ((c = more_getc(&ctl, f)) == '\f')
 				doclear(&ctl);
 			else {
-				ctl.file_pos--;
-				ungetc(c, f);
+				more_ungetc(&ctl, c, f);
 				if (ctl.noscroll && (c != EOF)) {
 					if (ctl.clreol)
 						home(&ctl);
@@ -2016,7 +2044,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');
Signature · 1 line
2.3.0
#8 lore raw

PATCH 8/31 more: remove pointless functions

In reply to Sami Kerola
---
 text-utils/more.c | 78 ++++++++++++++++++++-----------------------------------
 1 file changed, 28 insertions(+), 50 deletions(-)
text-utils/more.c +28 -50
index 01c999e..52ec23f 100644
@@ -263,12 +263,6 @@ static void argscan(struct more_control *ctl, char *s)
 	}
 }
 
-/* force clear to end of line */
-static void cleareol(struct more_control *ctl)
-{
-	my_putstring(ctl->eraseln);
-}
-
 /* magic --
  *	check for file magic numbers.  This code would best be shared
  *	with the file(1) program or, perhaps, more should not try to be
@@ -328,7 +322,7 @@ static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst
 	if (stat(fs, &stbuf) == -1) {
 		fflush(stdout);
 		if (ctl->clreol)
-			cleareol(ctl);
+			my_putstring(ctl->eraseln);
 		warn(_("stat of %s failed"), fs);
 		return NULL;
 	}
@@ -586,11 +580,6 @@ static void erasep(struct more_control *ctl, register int col)
 	ctl->promptlen = 0;
 }
 
-static void clreos(struct more_control *ctl)
-{
-	my_putstring(ctl->EodClr);
-}
-
 static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 				  mbstate_t *mbstate)
 {
@@ -675,7 +664,7 @@ static void kill_line(struct more_control *ctl)
 static void prompt(struct more_control *ctl, char *filename)
 {
 	if (ctl->clreol)
-		cleareol(ctl);
+		my_putstring(ctl->eraseln);
 	else if (ctl->promptlen > 0)
 		kill_line(ctl);
 	if (!ctl->hard) {
@@ -685,7 +674,7 @@ static void prompt(struct more_control *ctl, char *filename)
 			ctl->promptlen += (2 * ctl->soglitch);
 		}
 		if (ctl->clreol)
-			cleareol(ctl);
+			my_putstring(ctl->eraseln);
 		ctl->promptlen += printf(_("--More--"));
 		if (filename != NULL) {
 			ctl->promptlen += printf(_("(Next file: %s)"), filename);
@@ -701,24 +690,19 @@ static void prompt(struct more_control *ctl, char *filename)
 		if (ctl->Senter && ctl->Sexit)
 			my_putstring(ctl->Sexit);
 		if (ctl->clreol)
-			clreos(ctl);
+			my_putstring(ctl->EodClr);
 		fflush(stdout);
 	} else
 		fputc('\a', stderr);
 	ctl->inwait = 1;
 }
 
-static int ourputch(int c)
-{
-	return putc(c, stdout);
-}
-
 static void reset_tty(struct more_control *ctl)
 {
 	if (ctl->no_tty)
 		return;
 	if (ctl->pstate) {
-		tputs(ctl->ULexit, fileno(stdout), ourputch);
+		tputs(ctl->ULexit, fileno(stdout), putchar);
 		fflush(stdout);
 		ctl->pstate = 0;
 	}
@@ -734,7 +718,7 @@ static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unus
 	reset_tty(global_ctl);
 	if (global_ctl->clreol) {
 		putchar('\r');
-		clreos(global_ctl);
+		my_putstring(global_ctl->EodClr);
 		fflush(stdout);
 	} else if (!global_ctl->clreol && (global_ctl->promptlen > 0)) {
 		kill_line(global_ctl);
@@ -798,14 +782,14 @@ static void skipf(struct more_control *ctl, register int nskip)
 		ctl->fnum = 0;
 	puts(_("\n...Skipping "));
 	if (ctl->clreol)
-		cleareol(ctl);
+		my_putstring(ctl->eraseln);
 	if (nskip > 0)
 		fputs(_("...Skipping to file "), stdout);
 	else
 		fputs(_("...Skipping back to file "), stdout);
 	puts(ctl->fnames[ctl->fnum]);
 	if (ctl->clreol)
-		cleareol(ctl);
+		my_putstring(ctl->eraseln);
 	putchar('\n');
 	ctl->fnum--;
 }
@@ -824,7 +808,7 @@ static void show(struct more_control *ctl, char c)
 static void more_error(struct more_control *ctl, char *mess)
 {
 	if (ctl->clreol)
-		cleareol(ctl);
+		my_putstring(ctl->eraseln);
 	else
 		kill_line(ctl);
 	ctl->promptlen += strlen(mess);
@@ -1277,12 +1261,6 @@ static void rdline(struct more_control *ctl, register FILE *f)
 	*p = '\0';
 }
 
-/* Go to home position */
-static void home(struct more_control *ctl)
-{
-	my_putstring(ctl->Home);
-}
-
 /* Search for nth occurrence of regular expression contained in buf in
  * the file */
 static void search(struct more_control *ctl, char buf[], FILE *file, register int n)
@@ -1316,7 +1294,7 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in
 				if (lncount > 3 || (lncount > 1 && ctl->no_intty)) {
 					putchar('\n');
 					if (ctl->clreol)
-						cleareol(ctl);
+						my_putstring(ctl->eraseln);
 					fputs(_("...skipping\n"), stdout);
 				}
 				if (!ctl->no_intty) {
@@ -1325,8 +1303,8 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in
 					set_pos_fseek(ctl, file, line3);
 					if (ctl->noscroll) {
 						if (ctl->clreol) {
-							home(ctl);
-							cleareol(ctl);
+							my_putstring(ctl->Home);
+							my_putstring(ctl->eraseln);
 						} else
 							doclear(ctl);
 					}
@@ -1334,8 +1312,8 @@ static void search(struct more_control *ctl, char buf[], FILE *file, register in
 					kill_line(ctl);
 					if (ctl->noscroll) {
 						if (ctl->clreol) {
-							home(ctl);
-							cleareol(ctl);
+							my_putstring(ctl->Home);
+							my_putstring(ctl->eraseln);
 						} else
 							doclear(ctl);
 					}
@@ -1418,12 +1396,12 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 				erasep(ctl, 0);
 				putchar('\n');
 				if (ctl->clreol)
-					cleareol(ctl);
+					my_putstring(ctl->eraseln);
 				printf(P_("...back %d page",
 					"...back %d pages", nlines),
 					nlines);
 				if (ctl->clreol)
-					cleareol(ctl);
+					my_putstring(ctl->eraseln);
 				putchar('\n');
 
 				initline = ctl->Currline - ctl->dlines * (nlines + 1);
@@ -1473,13 +1451,13 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 			erasep(ctl, 0);
 			putchar('\n');
 			if (ctl->clreol)
-				cleareol(ctl);
+				my_putstring(ctl->eraseln);
 			printf(P_("...skipping %d line",
 				"...skipping %d lines", nlines),
 				nlines);
 
 			if (ctl->clreol)
-				cleareol(ctl);
+				my_putstring(ctl->eraseln);
 			putchar('\n');
 
 			while (nlines > 0) {
@@ -1677,7 +1655,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_
 		while (num_lines > 0 && !ctl->Pause) {
 			if ((nchars = get_line(ctl, f, &length)) == EOF) {
 				if (ctl->clreol)
-					clreos(ctl);
+					my_putstring(ctl->EodClr);
 				return;
 			}
 			if (ctl->ssp_opt && length == 0 && prev_len == 0)
@@ -1690,7 +1668,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_
 			 * some terminals do not erase what they tab
 			 * over. */
 			if (ctl->clreol)
-				cleareol(ctl);
+				my_putstring(ctl->eraseln);
 			prbuf(ctl, ctl->Line, length);
 			if (nchars < ctl->promptlen)
 				erasep(ctl, nchars);	/* erasep () sets promptlen to 0 */
@@ -1709,12 +1687,12 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_
 		fflush(stdout);
 		if ((c = more_getc(ctl, f)) == EOF) {
 			if (ctl->clreol)
-				clreos(ctl);
+				my_putstring(ctl->EodClr);
 			return;
 		}
 
 		if (ctl->Pause && ctl->clreol)
-			clreos(ctl);
+			my_putstring(ctl->EodClr);
 		more_ungetc(ctl, c, f);
 		sigsetjmp(ctl->restore, 1);
 		ctl->Pause = 0;
@@ -1725,7 +1703,7 @@ static void screen(struct more_control *ctl, register FILE *f, register int num_
 			erasep(ctl, 0);
 		if (ctl->noscroll && num_lines >= ctl->dlines) {
 			if (ctl->clreol)
-				home(ctl);
+				my_putstring(ctl->Home);
 			else
 				doclear(ctl);
 		}
@@ -1991,7 +1969,7 @@ int main(int argc, char **argv)
 				more_ungetc(&ctl, c, f);
 				if (ctl.noscroll && (c != EOF)) {
 					if (ctl.clreol)
-						home(&ctl);
+						my_putstring(ctl.Home);
 					else
 						doclear(&ctl);
 				}
@@ -2035,7 +2013,7 @@ int main(int argc, char **argv)
 				if ((ctl.noscroll || clearit)
 				    && (ctl.file_size != LONG_MAX)) {
 					if (ctl.clreol)
-						home(&ctl);
+						my_putstring(ctl.Home);
 					else
 						doclear(&ctl);
 				}
@@ -2043,16 +2021,16 @@ int main(int argc, char **argv)
 					if (ctl.bad_so)
 						erasep(&ctl, 0);
 					if (ctl.clreol)
-						cleareol(&ctl);
+						my_putstring(ctl.eraseln);
 					fputs("::::::::::::::", stdout);
 					if (ctl.promptlen > 14)
 						erasep(&ctl, 14);
 					putchar('\n');
 					if (ctl.clreol)
-						cleareol(&ctl);
+						my_putstring(ctl.eraseln);
 					puts(ctl.fnames[ctl.fnum]);
 					if (ctl.clreol)
-						cleareol(&ctl);
+						my_putstring(ctl.eraseln);
 					puts("::::::::::::::");
 					if (left > ctl.Lpp - 4)
 						left = ctl.Lpp - 4;
Signature · 1 line
2.3.0
#9 lore raw

PATCH 9/31 more: remove 'register' keywords

In reply to Sami Kerola

More is text pager these sorts of speed-ups does not make sense.

---
 text-utils/more.c | 58 +++++++++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 29 deletions(-)
text-utils/more.c +29 -29
index 52ec23f..651a34e 100644
@@ -313,10 +313,10 @@ static int more_ungetc(struct more_control *ctl, int c, FILE *stream)
 
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
-static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst)
+static FILE *checkf(struct more_control *ctl, char *fs, int *clearfirst)
 {
 	struct stat stbuf;
-	register FILE *f;
+	FILE *f;
 	int c;
 
 	if (stat(fs, &stbuf) == -1) {
@@ -366,7 +366,7 @@ static void prepare_line_buffer(struct more_control *ctl)
 }
 
 /* Get a logical line */
-static int get_line(struct more_control *ctl, register FILE *f, int *length)
+static int get_line(struct more_control *ctl, FILE *f, int *length)
 {
 	int c;
 	char *p;
@@ -562,7 +562,7 @@ static int get_line(struct more_control *ctl, register FILE *f, int *length)
 }
 
 /* Erase the rest of the prompt, assuming we are starting at column col. */
-static void erasep(struct more_control *ctl, register int col)
+static void erasep(struct more_control *ctl, int col)
 {
 
 	if (ctl->promptlen == 0)
@@ -599,10 +599,10 @@ static int wouldul(char *s, int n)
 }
 
 /* Print a buffer of n characters */
-static void prbuf(struct more_control *ctl, register char *s, register int n)
+static void prbuf(struct more_control *ctl, char *s, int n)
 {
-	register char c;	/* next output character */
-	register int state;	/* next output char's UL state */
+	char c;	/* next output character */
+	int state;	/* next output char's UL state */
 
 	while (--n >= 0)
 		if (!ctl->ul_opt)
@@ -748,7 +748,7 @@ static int readch(struct more_control *ctl)
  * which terminates the number. */
 static int number(struct more_control *ctl, char *cmd)
 {
-	register int i;
+	int i;
 	char ch;
 
 	i = 0;
@@ -768,7 +768,7 @@ static int number(struct more_control *ctl, char *cmd)
 
 /* Skip nskip files in the file list (from the command line).  Nskip may
  * be negative. */
-static void skipf(struct more_control *ctl, register int nskip)
+static void skipf(struct more_control *ctl, int nskip)
 {
 	if (nskip == 0)
 		return;
@@ -831,7 +831,7 @@ static void erase_one_column(struct more_control *ctl)
 		fputs("\b", stderr);
 }
 
-static void ttyin(struct more_control *ctl, char buf[], register int nmax, char pchar)
+static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 {
 	char *sp;
 	int c;
@@ -1220,9 +1220,9 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 }
 
 /* Skip n lines in the file f */
-static void skiplns(struct more_control *ctl, register int n, register FILE *f)
+static void skiplns(struct more_control *ctl, int n, FILE *f)
 {
-	register int c;
+	int c;
 
 	while (n > 0) {
 		while ((c = more_getc(ctl, f)) != '\n')
@@ -1245,10 +1245,10 @@ static void doclear(struct more_control *ctl)
 	}
 }
 
-static void rdline(struct more_control *ctl, register FILE *f)
+static void rdline(struct more_control *ctl, FILE *f)
 {
-	register int c;
-	register char *p;
+	int c;
+	char *p;
 
 	prepare_line_buffer(ctl);
 
@@ -1263,13 +1263,13 @@ static void rdline(struct more_control *ctl, register FILE *f)
 
 /* Search for nth occurrence of regular expression contained in buf in
  * the file */
-static void search(struct more_control *ctl, char buf[], FILE *file, register int n)
+static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 {
 	long startline = ctl->file_pos;
-	register long line1 = startline;
-	register long line2 = startline;
-	register long line3;
-	register int lncount;
+	long line1 = startline;
+	long line2 = startline;
+	long line3;
+	int lncount;
 	int saveln, rc;
 	regex_t re;
 
@@ -1343,11 +1343,11 @@ notfound:
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
  * display in the current file, zero is returned. */
-static int command(struct more_control *ctl, char *filename, register FILE *f)
+static int command(struct more_control *ctl, char *filename, FILE *f)
 {
-	register int nlines;
-	register int retval = 0;
-	register int c;
+	int nlines;
+	int retval = 0;
+	int c;
 	char colonch;
 	int done = 0;
 	char comchar, cmdbuf[INIT_BUF];
@@ -1382,7 +1382,7 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 		case 'b':
 		case ctrl('B'):
 			{
-				register int initline;
+				int initline;
 
 				if (ctl->no_intty) {
 					fputc('\a', stderr);
@@ -1644,10 +1644,10 @@ static int command(struct more_control *ctl, char *filename, register FILE *f)
 }
 
 /* Print out the contents of the file f, one screenful at a time. */
-static void screen(struct more_control *ctl, register FILE *f, register int num_lines)
+static void screen(struct more_control *ctl, FILE *f, int num_lines)
 {
-	register int c;
-	register int nchars;
+	int c;
+	int nchars;
 	int length;			/* length of current line */
 	static int prev_len = 1;	/* length of previous line */
 
@@ -1734,7 +1734,7 @@ static void chgwinsz(int dummy __attribute__((__unused__)))
 }
 #endif				/* SIGWINCH */
 
-static void copy_file(register FILE *f)
+static void copy_file(FILE *f)
 {
 	char buf[BUFSIZ];
 	size_t sz;
Signature · 1 line
2.3.0
#10 lore raw

PATCH 10/31 more: avoid long else segment in prbuf()

In reply to Sami Kerola

The if segment can be ended to 'continue' to allow nicer indentation.

---
 text-utils/more.c | 90 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 46 insertions(+), 44 deletions(-)
text-utils/more.c +46 -44
index 651a34e..c8b28a1 100644
@@ -601,56 +601,58 @@ static int wouldul(char *s, int n)
 /* Print a buffer of n characters */
 static void prbuf(struct more_control *ctl, char *s, int n)
 {
-	char c;	/* next output character */
-	int state;	/* next output char's UL state */
+	char c;			/* next output character */
+	int state;		/* next output char's UL state */
 
-	while (--n >= 0)
-		if (!ctl->ul_opt)
+	while (--n >= 0) {
+		if (!ctl->ul_opt) {
 			putchar(*s++);
-		else {
-			if (*s == ' ' && ctl->pstate == 0 && ctl->ulglitch
-			    && wouldul(s + 1, n - 1)) {
-				s++;
-				continue;
-			}
-			if ((state = wouldul(s, n)) != 0) {
-				c = (*s == '_') ? s[2] : *s;
-				n -= 2;
-				s += 3;
-			} else
-				c = *s++;
-			if (state != ctl->pstate) {
-				if (c == ' ' && state == 0 && ctl->ulglitch
-				    && wouldul(s, n - 1))
-					state = 1;
-				else
-					my_putstring(state ? ctl->ULenter : ctl->ULexit);
-			}
-			if (c != ' ' || ctl->pstate == 0 || state != 0
-			    || ctl->ulglitch == 0)
+			continue;
+		}
+		if (*s == ' ' && ctl->pstate == 0 && ctl->ulglitch
+		    && wouldul(s + 1, n - 1)) {
+			s++;
+			continue;
+		}
+		if ((state = wouldul(s, n)) != 0) {
+			c = (*s == '_') ? s[2] : *s;
+			n -= 2;
+			s += 3;
+		} else
+			c = *s++;
+		if (state != ctl->pstate) {
+			if (c == ' ' && state == 0 && ctl->ulglitch
+			    && wouldul(s, n - 1))
+				state = 1;
+			else
+				my_putstring(state ? ctl->ULenter : ctl->
+					     ULexit);
+		}
+		if (c != ' ' || ctl->pstate == 0 || state != 0
+		    || ctl->ulglitch == 0)
 #ifdef HAVE_WIDECHAR
-			{
-				wchar_t wc;
-				size_t mblength;
-				mbstate_t mbstate;
-
-				memset(&mbstate, 0, sizeof mbstate);
-				s--;
-				n++;
-				mblength = xmbrtowc(&wc, s, n, &mbstate);
-				while (mblength--)
-					putchar(*s++);
-				n += mblength;
-			}
+		{
+			wchar_t wc;
+			size_t mblength;
+			mbstate_t mbstate;
+
+			memset(&mbstate, 0, sizeof mbstate);
+			s--;
+			n++;
+			mblength = xmbrtowc(&wc, s, n, &mbstate);
+			while (mblength--)
+				putchar(*s++);
+			n += mblength;
+		}
 #else
-				putchar(c);
+			putchar(c);
 #endif				/* HAVE_WIDECHAR */
-			if (state && *ctl->chUL) {
-				fputs(ctl->chBS, stdout);
-				my_putstring(ctl->chUL);
-			}
-			ctl->pstate = state;
+		if (state && *ctl->chUL) {
+			fputs(ctl->chBS, stdout);
+			my_putstring(ctl->chUL);
 		}
+		ctl->pstate = state;
+	}
 }
 
 /* Erase the current line entirely */
Signature · 1 line
2.3.0
#11 lore raw

PATCH 11/31 more: move runtime usage output away from command()

In reply to Sami Kerola

The command() function is too long.

---
 text-utils/more.c | 58 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 30 insertions(+), 28 deletions(-)
text-utils/more.c +30 -28
index c8b28a1..6b07d95 100644
@@ -1341,6 +1341,35 @@ notfound:
 	}
 }
 
+static void runtime_usage(void)
+{
+	fputs(_("\n"
+		"Most commands optionally preceded by integer argument k.  "
+		"Defaults in brackets.\n"
+		"Star (*) indicates argument becomes new default.\n"), stdout);
+	fputs(	"-------------------------------------------------------------------------------", stdout);
+	fputs(_("<space>                 Display next k lines of text [current screen size]\n"
+		"z                       Display next k lines of text [current screen size]*\n"
+		"<return>                Display next k lines of text [1]*\n"
+		"d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
+		"q or Q or <interrupt>   Exit from more\n"
+		"s                       Skip forward k lines of text [1]\n"
+		"f                       Skip forward k screenfuls of text [1]\n"
+		"b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
+		"'                       Go to place where previous search started\n"
+		"=                       Display current line number\n"
+		"/<regular expression>   Search for kth occurrence of regular expression [1]\n"
+		"n                       Search for kth occurrence of last r.e [1]\n"
+		"!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
+		"v                       Start up /usr/bin/vi at current line\n"
+		"ctrl-L                  Redraw screen\n"
+		":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"), stdout);
+	fputs(	"-------------------------------------------------------------------------------", stdout);
+}
+
 /* Read a command and do it.  A command consists of an optional integer
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
@@ -1545,34 +1574,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		case 'h':
 			if (ctl->noscroll)
 				doclear(ctl);
-			fputs(_("\n"
-				  "Most commands optionally preceded by integer argument k.  "
-				  "Defaults in brackets.\n"
-				  "Star (*) indicates argument becomes new default.\n"), stdout);
-			fputs("---------------------------------------"
-			     "----------------------------------------", stdout);
-			fputs(_
-				("<space>                 Display next k lines of text [current screen size]\n"
-				 "z                       Display next k lines of text [current screen size]*\n"
-				 "<return>                Display next k lines of text [1]*\n"
-				 "d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
-				 "q or Q or <interrupt>   Exit from more\n"
-				 "s                       Skip forward k lines of text [1]\n"
-				 "f                       Skip forward k screenfuls of text [1]\n"
-				 "b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
-				 "'                       Go to place where previous search started\n"
-				 "=                       Display current line number\n"
-				 "/<regular expression>   Search for kth occurrence of regular expression [1]\n"
-				 "n                       Search for kth occurrence of last r.e [1]\n"
-				 "!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
-				 "v                       Start up /usr/bin/vi at current line\n"
-				 "ctrl-L                  Redraw screen\n"
-				 ":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"), stdout);
-			fputs("---------------------------------------"
-			      "----------------------------------------", stdout);
+			runtime_usage();
 			prompt(ctl, filename);
 			break;
 		case 'v':	/* This case should go right before default */
Signature · 1 line
2.3.0
#12 lore raw

PATCH 12/31 more: move editor execution to a function from command()

In reply to Sami Kerola

The command() function is too long.

---
 text-utils/more.c | 72 ++++++++++++++++++++++++++-----------------------------
 1 file changed, 34 insertions(+), 38 deletions(-)
text-utils/more.c +34 -38
index 6b07d95..0825cdb 100644
@@ -1370,6 +1370,39 @@ static void runtime_usage(void)
 	fputs(	"-------------------------------------------------------------------------------", stdout);
 }
 
+static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filename)
+{
+	char *editor, *p;
+	int n = (ctl->Currline - ctl->dlines <= 0 ? 1 : ctl->Currline - (ctl->dlines + 1) / 2);
+	int split = 0;
+
+	editor = getenv("VISUAL");
+	if (editor == NULL || *editor == '\0')
+		editor = getenv("EDITOR");
+	if (editor == NULL || *editor == '\0')
+		editor = _PATH_VI;
+	p = strrchr(editor, '/');
+	if (p)
+		p++;
+	else
+		p = editor;
+	/* Earlier: call vi +n file.  This also works for emacs.  POSIX:
+	 * call vi -c n file (when editor is vi or ex).  */
+	if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
+		sprintf(cmdbuf, "-c %d", n);
+		split = 1;
+	} else
+		sprintf(cmdbuf, "+%d", n);
+	kill_line(ctl);
+	printf("%s %s %s", editor, cmdbuf, ctl->fnames[ctl->fnum]);
+	if (split) {
+		cmdbuf[2] = 0;
+		execute(ctl, filename, editor, editor,
+			cmdbuf, cmdbuf + 3, ctl->fnames[ctl->fnum], (char *)0);
+	} else
+		execute(ctl, filename, editor, editor, cmdbuf, ctl->fnames[ctl->fnum], (char *)0);
+}
+
 /* Read a command and do it.  A command consists of an optional integer
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
@@ -1579,44 +1612,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case 'v':	/* This case should go right before default */
 			if (!ctl->no_intty) {
-				/* Earlier: call vi +n file. This also
-				 * works for emacs.  POSIX: call vi -c n
-				 * file (when editor is vi or ex). */
-				char *editor, *p;
-				int n = (ctl->Currline - ctl->dlines <= 0 ? 1 :
-					 ctl->Currline - (ctl->dlines + 1) / 2);
-				int split = 0;
-
-				editor = getenv("VISUAL");
-				if (editor == NULL || *editor == '\0')
-					editor = getenv("EDITOR");
-				if (editor == NULL || *editor == '\0')
-					editor = _PATH_VI;
-
-				p = strrchr(editor, '/');
-				if (p)
-					p++;
-				else
-					p = editor;
-				if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
-					sprintf(cmdbuf, "-c %d", n);
-					split = 1;
-				} else {
-					sprintf(cmdbuf, "+%d", n);
-				}
-
-				kill_line(ctl);
-				printf("%s %s %s", editor, cmdbuf,
-				       ctl->fnames[ctl->fnum]);
-				if (split) {
-					cmdbuf[2] = 0;
-					execute(ctl, filename, editor, editor,
-						cmdbuf, cmdbuf + 3,
-						ctl->fnames[ctl->fnum], (char *)0);
-				} else
-					execute(ctl, filename, editor, editor,
-						cmdbuf, ctl->fnames[ctl->fnum],
-						(char *)0);
+				execute_editor(ctl, cmdbuf, filename);
 				break;
 			}
 			/* fall through */
Signature · 1 line
2.3.0
#13 lore raw

PATCH 13/31 more: move skipping backwards to a function from command()

In reply to Sami Kerola

The command() function is too long.

---
 text-utils/more.c | 73 ++++++++++++++++++++++++++-----------------------------
 1 file changed, 34 insertions(+), 39 deletions(-)
text-utils/more.c +34 -39
index 0825cdb..a502716 100644
@@ -1403,6 +1403,34 @@ static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filenam
 		execute(ctl, filename, editor, editor, cmdbuf, ctl->fnames[ctl->fnum], (char *)0);
 }
 
+static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
+{
+	int initline;
+
+	if (nlines == 0)
+		nlines++;
+	putchar('\r');
+	erasep(ctl, 0);
+	putchar('\n');
+	if (ctl->clreol)
+		my_putstring(ctl->eraseln);
+	printf(P_("...back %d page", "...back %d pages", nlines), nlines);
+	if (ctl->clreol)
+		my_putstring(ctl->eraseln);
+	putchar('\n');
+	initline = ctl->Currline - ctl->dlines * (nlines + 1);
+	if (!ctl->noscroll)
+		--initline;
+	if (initline < 0)
+		initline = 0;
+	set_pos_fseek(ctl, f, 0);
+	ctl->Currline = 0;	/* skiplns() will make Currline correct */
+	skiplns(ctl, initline, f);
+	if (!ctl->noscroll)
+		return ctl->dlines + 1;
+	return ctl->dlines;
+}
+
 /* Read a command and do it.  A command consists of an optional integer
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
@@ -1445,46 +1473,13 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case 'b':
 		case ctrl('B'):
-			{
-				int initline;
-
-				if (ctl->no_intty) {
-					fputc('\a', stderr);
-					return -1;
-				}
-
-				if (nlines == 0)
-					nlines++;
-
-				putchar('\r');
-				erasep(ctl, 0);
-				putchar('\n');
-				if (ctl->clreol)
-					my_putstring(ctl->eraseln);
-				printf(P_("...back %d page",
-					"...back %d pages", nlines),
-					nlines);
-				if (ctl->clreol)
-					my_putstring(ctl->eraseln);
-				putchar('\n');
-
-				initline = ctl->Currline - ctl->dlines * (nlines + 1);
-				if (!ctl->noscroll)
-					--initline;
-				if (initline < 0)
-					initline = 0;
-				set_pos_fseek(ctl, f, 0);
-				ctl->Currline = 0;	/* skiplns() will make Currline correct */
-				skiplns(ctl, initline, f);
-				if (!ctl->noscroll) {
-					retval = ctl->dlines + 1;
-					done = 1;
-					break;
-				}
-				retval = ctl->dlines;
-				done = 1;
-				break;
+			if (ctl->no_intty) {
+				fputc('\a', stderr);
+				return -1;
 			}
+			retval = skip_backwards(ctl, f, nlines);
+			done = 1;
+			break;
 		case ' ':
 		case 'z':
 			if (nlines == 0)
Signature · 1 line
2.3.0
#14 lore raw

PATCH 14/31 more: move skipping forewards to a function from command()

In reply to Sami Kerola

The command() function was too long.

---
 text-utils/more.c | 60 +++++++++++++++++++++++++++----------------------------
 1 file changed, 30 insertions(+), 30 deletions(-)
text-utils/more.c +30 -30
index a502716..0c1bc96 100644
@@ -1431,6 +1431,32 @@ static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
 	return ctl->dlines;
 }
 
+static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char comchar)
+{
+	if (nlines == 0)
+		nlines++;
+	if (comchar == 'f')
+		nlines *= ctl->dlines;
+	putchar('\r');
+	erasep(ctl, 0);
+	putchar('\n');
+	if (ctl->clreol)
+		my_putstring(ctl->eraseln);
+	printf(P_("...skipping %d line", "...skipping %d lines", nlines), nlines);
+	if (ctl->clreol)
+		my_putstring(ctl->eraseln);
+	putchar('\n');
+	while (nlines > 0) {
+		int c;
+		while ((c = more_getc(ctl, f)) != '\n')
+			if (c == EOF)
+				return 0;
+		ctl->Currline++;
+		nlines--;
+	}
+	return 1;
+}
+
 /* Read a command and do it.  A command consists of an optional integer
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
@@ -1439,7 +1465,6 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 {
 	int nlines;
 	int retval = 0;
-	int c;
 	char colonch;
 	int done = 0;
 	char comchar, cmdbuf[INIT_BUF];
@@ -1502,34 +1527,10 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		case 's':
 		case 'f':
 		case ctrl('F'):
-			if (nlines == 0)
-				nlines++;
-			if (comchar == 'f')
-				nlines *= ctl->dlines;
-			putchar('\r');
-			erasep(ctl, 0);
-			putchar('\n');
-			if (ctl->clreol)
-				my_putstring(ctl->eraseln);
-			printf(P_("...skipping %d line",
-				"...skipping %d lines", nlines),
-				nlines);
-
-			if (ctl->clreol)
-				my_putstring(ctl->eraseln);
-			putchar('\n');
-
-			while (nlines > 0) {
-				while ((c = more_getc(ctl, f)) != '\n')
-					if (c == EOF) {
-						retval = 0;
-						done++;
-						goto endsw;
-					}
-				ctl->Currline++;
-				nlines--;
-			}
-			retval = ctl->dlines;
+			if (skip_forwards(ctl, f, nlines, comchar))
+				retval = ctl->dlines;
+			else
+				retval = 0;
 			done = 1;
 			break;
 		case '\n':
@@ -1632,7 +1633,6 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		}
 	}
 	putchar('\r');
- endsw:
 	ctl->inwait = 0;
 	ctl->notell = 1;
 	return retval;
Signature · 1 line
2.3.0
#15 lore raw

PATCH 15/31 more: check open(3) return value

In reply to Sami Kerola
---
 text-utils/more.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
text-utils/more.c +5 -1
index 0c1bc96..2813ce7 100644
@@ -1082,7 +1082,10 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 	if (id == 0) {
 		if (!isatty(STDIN_FILENO)) {
 			close(STDIN_FILENO);
-			open("/dev/tty", 0);
+			if (open("/dev/tty", 0) < 0) {
+				fprintf(stderr, _("cannot open %s"), "/dev/tty\n");
+				goto err;
+			}
 		}
 
 		va_start(argp, cmd);
@@ -1125,6 +1128,7 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 		fputs(_("can't fork\n"), stderr);
 	set_tty(ctl);
 	puts("------------------------");
+err:
 	prompt(ctl, filename);
 }
 
Signature · 1 line
2.3.0
#16 lore raw

PATCH 16/31 more: remove unnecessary braces

In reply to Sami Kerola
---
 text-utils/more.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
text-utils/more.c +9 -12
index 2813ce7..bf3ab9c 100644
@@ -547,15 +547,12 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 #endif
 		c = more_getc(ctl, f);
 	}
-	if (column >= ctl->Mcol && ctl->Mcol > 0) {
-		if (!ctl->Wrap) {
+	if (column >= ctl->Mcol && ctl->Mcol > 0)
+		if (!ctl->Wrap)
 			*p++ = '\n';
-		}
-	}
 	colflg = column == ctl->Mcol && ctl->fold_opt;
-	if (colflg && ctl->eatnl && ctl->Wrap) {
+	if (colflg && ctl->eatnl && ctl->Wrap)
 		*p++ = '\n';	/* simulate normal wrap */
-	}
 	*length = p - ctl->Line;
 	*p = 0;
 	return column;
@@ -567,9 +564,9 @@ static void erasep(struct more_control *ctl, int col)
 
 	if (ctl->promptlen == 0)
 		return;
-	if (ctl->hard) {
+	if (ctl->hard)
 		putchar('\n');
-	} else {
+	else {
 		if (col == 0)
 			putchar('\r');
 		if (!ctl->dumb && ctl->eraseln)
@@ -678,9 +675,9 @@ static void prompt(struct more_control *ctl, char *filename)
 		if (ctl->clreol)
 			my_putstring(ctl->eraseln);
 		ctl->promptlen += printf(_("--More--"));
-		if (filename != NULL) {
+		if (filename != NULL)
 			ctl->promptlen += printf(_("(Next file: %s)"), filename);
-		} else if (!ctl->no_intty) {
+		else if (!ctl->no_intty) {
 			ctl->promptlen +=
 			    printf("(%d%%)",
 				   (int)((ctl->file_pos * 100) / ctl->file_size));
@@ -846,9 +843,9 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 		if (ctl->promptlen > maxlen)
 			maxlen = ctl->promptlen;
 		c = readch(ctl);
-		if (c == '\\') {
+		if (c == '\\')
 			slash++;
-		} else if (((cc_t) c == ctl->otty.c_cc[VERASE]) && !slash) {
+		else if (((cc_t) c == ctl->otty.c_cc[VERASE]) && !slash) {
 			if (sp > buf) {
 #ifdef HAVE_WIDECHAR
 				if (MB_CUR_MAX > 1) {
Signature · 1 line
2.3.0
#17 lore raw

PATCH 17/31 more: do not call fileno() for std{in,out,err} streams

In reply to Sami Kerola

File descriptor numbers are well known for these, use them from unistd.h

---
 text-utils/more-term.h |  4 ++--
 text-utils/more.c      | 14 +++++++-------
 2 files changed, 9 insertions(+), 9 deletions(-)
text-utils/more-term.h +2 -2
index b78da5e..31b8c02 100644
@@ -24,7 +24,7 @@
 
 static inline void my_putstring(char *s)
 {
-	tputs(s, fileno(stdout), putchar);	/* putp(s); */
+	tputs(s, STDOUT_FILENO, putchar);	/* putp(s); */
 }
 
 static inline void my_setupterm(char *term, int fildes, int *errret)
@@ -78,7 +78,7 @@ static inline char *my_tgoto(char *cap, int col, int row)
 
 static inline void my_putstring(char *s)
 {
-	tputs(s, fileno(stdout), putchar);
+	tputs(s, STDOUT_FILENO, putchar);
 }
 
 static inline void my_setupterm(char *term, int fildes
text-utils/more.c +7 -7
index bf3ab9c..1afb593 100644
@@ -701,7 +701,7 @@ static void reset_tty(struct more_control *ctl)
 	if (ctl->no_tty)
 		return;
 	if (ctl->pstate) {
-		tputs(ctl->ULexit, fileno(stdout), putchar);
+		tputs(ctl->ULexit, STDOUT_FILENO, putchar);
 		fflush(stdout);
 		ctl->pstate = 0;
 	}
@@ -734,7 +734,7 @@ static int readch(struct more_control *ctl)
 	unsigned char c;
 
 	errno = 0;
-	if (read(fileno(stderr), &c, 1) <= 0) {
+	if (read(STDERR_FILENO, &c, 1) <= 0) {
 		if (errno != EINTR)
 			end_it(0);
 		else
@@ -1715,7 +1715,7 @@ static void chgwinsz(int dummy __attribute__((__unused__)))
 	struct winsize win;
 
 	signal(SIGWINCH, SIG_IGN);
-	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
+	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) {
 		if (win.ws_row != 0) {
 			global_ctl->Lpp = win.ws_row;
 			global_ctl->nscroll = global_ctl->Lpp / 2 - 1;
@@ -1748,7 +1748,7 @@ static void initterm(struct more_control *ctl)
 #endif
 
 #ifndef NON_INTERACTIVE_MORE
-	ctl->no_tty = tcgetattr(fileno(stdout), &ctl->otty);
+	ctl->no_tty = tcgetattr(STDOUT_FILENO, &ctl->otty);
 #endif
 	if (!ctl->no_tty) {
 		ctl->docrterase = (ctl->otty.c_cc[VERASE] != 255);
@@ -1763,7 +1763,7 @@ static void initterm(struct more_control *ctl)
 			ctl->ul_opt = 0;
 		} else {
 #ifdef TIOCGWINSZ
-			if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
+			if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
 #endif
 				ctl->Lpp = my_tgetnum(TERM_LINES);
 				ctl->Mcol = my_tgetnum(TERM_COLS);
@@ -1844,8 +1844,8 @@ static void initterm(struct more_control *ctl)
 		if ((ctl->shell = getenv("SHELL")) == NULL)
 			ctl->shell = _PATH_BSHELL;
 	}
-	ctl->no_intty = tcgetattr(fileno(stdin), &ctl->otty);
-	tcgetattr(fileno(stderr), &ctl->otty);
+	ctl->no_intty = tcgetattr(STDIN_FILENO, &ctl->otty);
+	tcgetattr(STDERR_FILENO, &ctl->otty);
 	ctl->savetty0 = ctl->otty;
 	ctl->hardtabs = (ctl->otty.c_oflag & TABDLY) != XTABS;
 	if (!ctl->no_tty) {
Signature · 1 line
2.3.0
#18 lore raw

PATCH 18/31 more: split run time help screen to multiple translatable lines

In reply to Sami Kerola
---
 text-utils/more.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
text-utils/more.c +25 -25
index 1afb593..3e7f5e7 100644
@@ -1344,31 +1344,31 @@ notfound:
 
 static void runtime_usage(void)
 {
-	fputs(_("\n"
-		"Most commands optionally preceded by integer argument k.  "
-		"Defaults in brackets.\n"
-		"Star (*) indicates argument becomes new default.\n"), stdout);
-	fputs(	"-------------------------------------------------------------------------------", stdout);
-	fputs(_("<space>                 Display next k lines of text [current screen size]\n"
-		"z                       Display next k lines of text [current screen size]*\n"
-		"<return>                Display next k lines of text [1]*\n"
-		"d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"
-		"q or Q or <interrupt>   Exit from more\n"
-		"s                       Skip forward k lines of text [1]\n"
-		"f                       Skip forward k screenfuls of text [1]\n"
-		"b or ctrl-B             Skip backwards k screenfuls of text [1]\n"
-		"'                       Go to place where previous search started\n"
-		"=                       Display current line number\n"
-		"/<regular expression>   Search for kth occurrence of regular expression [1]\n"
-		"n                       Search for kth occurrence of last r.e [1]\n"
-		"!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"
-		"v                       Start up /usr/bin/vi at current line\n"
-		"ctrl-L                  Redraw screen\n"
-		":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"), stdout);
-	fputs(	"-------------------------------------------------------------------------------", stdout);
+	fputs(  "\n", stdout);
+	fputs(_("Most commands optionally preceded by integer argument k.\n"), stdout);
+	fputs(_("Defaults in brackets.\n"), stdout);
+	fputs(_("Star (*) indicates argument becomes new default.\n"), stdout);
+	fputs(  "-------------------------------------------------------------------------------\n", stdout);
+	fputs(_("<space>                 Display next k lines of text [current screen size]\n"), stdout);
+	fputs(_("z                       Display next k lines of text [current screen size]*\n"), stdout);
+	fputs(_("<return>                Display next k lines of text [1]*\n"), stdout);
+	fputs(_("d or ctrl-D             Scroll k lines [current scroll size, initially 11]*\n"), stdout);
+	fputs(_("q or Q or <interrupt>   Exit from more\n"), stdout);
+	fputs(_("s                       Skip forward k lines of text [1]\n"), stdout);
+	fputs(_("f                       Skip forward k screenfuls of text [1]\n"), stdout);
+	fputs(_("b or ctrl-B             Skip backwards k screenfuls of text [1]\n"), stdout);
+	fputs(_("'                       Go to place where previous search started\n"), stdout);
+	fputs(_("=                       Display current line number\n"), stdout);
+	fputs(_("/<regular expression>   Search for kth occurrence of regular expression [1]\n"), stdout);
+	fputs(_("n                       Search for kth occurrence of last r.e [1]\n"), stdout);
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);
+	fputs(_("v                       Start up /usr/bin/vi at current line\n"), stdout);
+	fputs(_("ctrl-L                  Redraw screen\n"), stdout);
+	fputs(_(":n                      Go to kth next file [1]\n"), stdout);
+	fputs(_(":p                      Go to kth previous file [1]\n"), stdout);
+	fputs(_(":f                      Display current file name and line number\n"), stdout);
+	fputs(_(".                       Repeat previous command\n"), stdout);
+	fputs(  "-------------------------------------------------------------------------------\n", stdout);
 }
 
 static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filename)
Signature · 1 line
2.3.0
#19 lore raw

Re: [PATCH 18/31] more: split run time help screen to multiple translatable lines

text-utils/more.c notfound: · line 1350

Quoted 9 lines · Sami Kerola
text-utils/more.c +4 -4
-	fputs(_("\n"
-		"Most commands optionally preceded by integer argument k.  "
-		"Defaults in brackets.\n"
-		"Star (*) indicates argument becomes new default.\n"), stdout);
[...]
+	fputs(  "\n", stdout);
+	fputs(_("Most commands optionally preceded by integer argument k.\n"), stdout);
+	fputs(_("Defaults in brackets.\n"), stdout);
+	fputs(_("Star (*) indicates argument becomes new default.\n"), stdout);

These three sentences are somewhat interrelated, so it is better to keep them in a single translatable string, as it was. It would have been nice to take out the leading newline, but it's not worth invalidating all the existing translations. So I suggest keeping the first fputs() unchanged. Splitting up all the other lines is fine.

text-utils/more.c notfound: · line 1356

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("q or Q or <interrupt>   Exit from more\n"), stdout);

Interrupt is always ctrl-C, no? If so, then better use that, as it matches the representation of the other ctrl combos.

text-utils/more.c notfound: · line 1362

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("/<regular expression>   Search for kth occurrence of regular expression [1]\n"), stdout);

In order to be able to reduce the amount of whitespace between key name and explanation in the whole help screen, I would suggest replacing <regular expression> with <regexp>, which is a very common abbreviation.

text-utils/more.c notfound: · line 1363

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("n                       Search for kth occurrence of last r.e [1]\n"), stdout);

s/r.e/regexp/

text-utils/more.c notfound: · line 1364

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);

Hmm... Are there certain circumstances where ! doesn't work and :! is needed? If not, I would suggest dropping the second form, to reduce the space between keys and explanations. A help screen doesn't have to tell everything, that's what the man page is for.

text-utils/more.c notfound: · line 1366

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("ctrl-L                  Redraw screen\n"), stdout);

For all key explanations: start them with a lowercase letter, as they don't end in a period either.

Benno

Signature · 1 line
http://www.fastmail.com - Choose from over 50 domains or use your own
#20 lore raw

Re: [PATCH 18/31] more: split run time help screen to multiple translatable lines

Quoted 40 lines · Benno Schulenberg

text-utils/more.c notfound: · line 1350

Quoted 9 lines · Sami Kerola
text-utils/more.c +4 -4
-	fputs(_("\n"
-		"Most commands optionally preceded by integer argument k.  "
-		"Defaults in brackets.\n"
-		"Star (*) indicates argument becomes new default.\n"), stdout);
[...]
+	fputs(  "\n", stdout);
+	fputs(_("Most commands optionally preceded by integer argument k.\n"), stdout);
+	fputs(_("Defaults in brackets.\n"), stdout);
+	fputs(_("Star (*) indicates argument becomes new default.\n"), stdout);

These three sentences are somewhat interrelated, so it is better to keep them in a single translatable string, as it was. It would have been nice to take out the leading newline, but it's not worth invalidating all the existing translations. So I suggest keeping the first fputs() unchanged. Splitting up all the other lines is fine.

text-utils/more.c notfound: · line 1356

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("q or Q or <interrupt>   Exit from more\n"), stdout);

Interrupt is always ctrl-C, no? If so, then better use that, as it matches the representation of the other ctrl combos.

text-utils/more.c notfound: · line 1362

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("/<regular expression>   Search for kth occurrence of regular expression [1]\n"), stdout);

In order to be able to reduce the amount of whitespace between key name and explanation in the whole help screen, I would suggest replacing <regular expression> with <regexp>, which is a very common abbreviation.

text-utils/more.c notfound: · line 1363

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("n                       Search for kth occurrence of last r.e [1]\n"), stdout);

s/r.e/regexp/

text-utils/more.c notfound: · line 1364

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);

Hmm... Are there certain circumstances where ! doesn't work and :! is needed? If not, I would suggest dropping the second form, to reduce the space between keys and explanations. A help screen doesn't have to tell everything, that's what the man page is for.

Quoted 4 lines

text-utils/more.c notfound: · line 1366

Quoted 1 line
text-utils/more.c +1 -0
+	fputs(_("ctrl-L                  Redraw screen\n"), stdout);

For all key explanations: start them with a lowercase letter, as they don't end in a period either.

Thanks Benno,

Here is updated version of the change.

https://github.com/kerolasa/lelux-utiliteetit/commit/613c30239c6a284914fb70f7f7e96c44896f6414

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#21 lore raw

PATCH 19/31 more: tell in run time help what the 'v' will execute as editor

In reply to Sami Kerola
---
 text-utils/more.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
text-utils/more.c +15 -7
index 3e7f5e7..524ce44 100644
@@ -1342,6 +1342,18 @@ notfound:
 	}
 }
 
+static char *find_editor(void)
+{
+	char *editor;
+
+	editor = getenv("VISUAL");
+	if (editor == NULL || *editor == '\0')
+		editor = getenv("EDITOR");
+	if (editor == NULL || *editor == '\0')
+		editor = _PATH_VI;
+	return editor;
+}
+
 static void runtime_usage(void)
 {
 	fputs(  "\n", stdout);
@@ -1361,8 +1373,8 @@ static void runtime_usage(void)
 	fputs(_("=                       Display current line number\n"), stdout);
 	fputs(_("/<regular expression>   Search for kth occurrence of regular expression [1]\n"), stdout);
 	fputs(_("n                       Search for kth occurrence of last r.e [1]\n"), stdout);
-	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);
-	fputs(_("v                       Start up /usr/bin/vi at current line\n"), stdout);
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);	fprintf(stdout, _(
+		"v                       Start up '%s' at current line\n"), find_editor());
 	fputs(_("ctrl-L                  Redraw screen\n"), stdout);
 	fputs(_(":n                      Go to kth next file [1]\n"), stdout);
 	fputs(_(":p                      Go to kth previous file [1]\n"), stdout);
@@ -1377,11 +1389,7 @@ static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filenam
 	int n = (ctl->Currline - ctl->dlines <= 0 ? 1 : ctl->Currline - (ctl->dlines + 1) / 2);
 	int split = 0;
 
-	editor = getenv("VISUAL");
-	if (editor == NULL || *editor == '\0')
-		editor = getenv("EDITOR");
-	if (editor == NULL || *editor == '\0')
-		editor = _PATH_VI;
+	editor = find_editor();
 	p = strrchr(editor, '/');
 	if (p)
 		p++;
Signature · 1 line
2.3.0
#22 lore raw

Re: [PATCH 19/31] more: tell in run time help what the 'v' will execute as editor

text-utils/more.c static void runtime_usage(void) · line 1377

Quoted 4 lines · Sami Kerola
text-utils/more.c +2 -2
-	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);
-	fputs(_("v                       Start up /usr/bin/vi at current line\n"), stdout);
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);	fprintf(stdout, _(
+		"v                       Start up '%s' at current line\n"), find_editor());

Forgot a newline after the semicolon before the fprinf()?

Benno

Signature · 1 line
http://www.fastmail.com - Access your email from home and the web
#23 lore raw

Re: [PATCH 19/31] more: tell in run time help what the 'v' will execute as editor

Quoted 7 lines · Benno Schulenberg

text-utils/more.c static void runtime_usage(void) · line 1377

Quoted 4 lines · Sami Kerola
text-utils/more.c +2 -2
-	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);
-	fputs(_("v                       Start up /usr/bin/vi at current line\n"), stdout);
+	fputs(_("!<cmd> or :!<cmd>       Execute <cmd> in a subshell\n"), stdout);	fprintf(stdout, _(
+		"v                       Start up '%s' at current line\n"), find_editor());

Forgot a newline after the semicolon before the fprinf()?

Thanks Benno,

I tried to make print out block to be continuous, but that broke style as you pointed out. Updated version tries to combine style and overall print out block readability - while still taking tiny indentation liberties.

https://github.com/kerolasa/lelux-utiliteetit/commit/1e5b8bd88200bc0c7454030b99ad91482131f3a9

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#24 lore raw

PATCH 20/31 more: add display_file() to be shared with files and stdin input

In reply to Sami Kerola

This allows removing two code segments from main() that perform the same tasks.

---
 text-utils/more.c | 201 ++++++++++++++++++++++++------------------------------
 1 file changed, 91 insertions(+), 110 deletions(-)
text-utils/more.c +91 -110
index 524ce44..5586e69 100644
@@ -109,6 +109,7 @@ struct more_control {
 	int nscroll;			/* number of lines scrolled by 'd' */
 	int dlines;			/* screen size in lines */
 	int promptlen;			/* message prompt length */
+	int jumpline;			/* line number to jump */
 	int Currline;			/* line we are currently at */
 	char **fnames;			/* the list of file names */
 	int nfiles;			/* number of files left to process */
@@ -148,6 +149,7 @@ struct more_control {
 		bad_so:1,		/* true if overwriting does not turn off standout */
 		catch_susp:1,		/* should SIGTSTP signal be caught */
 		clreol:1,		/* do not scroll, paint each screen from the top */
+		clearfirst:1,		/* is first character in file \f */
 		docrterase:1,		/* is erase previous supported */
 		docrtkill:1,		/* is erase input supported */
 		dumb:1,			/* is terminal type known */
@@ -158,6 +160,7 @@ struct more_control {
 		fold_opt:1,		/* fold long lines */
 		hard:1,			/* is this hard copy terminal */
 		hardtabs:1,		/* print spaces instead of '\t' */
+		jumpopt:1,		/* is jumpline defined */
 		inwait:1,		/* is waiting user input */
 		lastp:1,		/* run previous key command */
 		noscroll:1,		/* do not scroll, clear the screen and then display text */
@@ -165,8 +168,10 @@ struct more_control {
 		no_intty:1,		/* is input in interactive mode */
 		no_tty:1,		/* is output in interactive mode */
 		Pause:1,		/* is output paused */
+		prnames:1,		/* print file name banner */
 		pstate:1,		/* is underlining going on */
 		soglitch:1,		/* terminal has standout mode glitch */
+		srchopt:1,		/* is init search pattern defined */
 		ssp_opt:1,		/* suppress white space */
 		startup:1,		/* is startup completed */
 		stop_opt:1,		/* stop after form feeds */
@@ -313,7 +318,7 @@ static int more_ungetc(struct more_control *ctl, int c, FILE *stream)
 
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
-static FILE *checkf(struct more_control *ctl, char *fs, int *clearfirst)
+static FILE *checkf(struct more_control *ctl, char *fs)
 {
 	struct stat stbuf;
 	FILE *f;
@@ -330,8 +335,6 @@ static FILE *checkf(struct more_control *ctl, char *fs, int *clearfirst)
 		printf(_("\n*** %s: directory ***\n\n"), fs);
 		return NULL;
 	}
-	ctl->Currline = 0;
-	ctl->file_pos = 0;
 	if ((f = fopen(fs, "r")) == NULL) {
 		fflush(stdout);
 		warn(_("cannot open %s"), fs);
@@ -343,7 +346,7 @@ static FILE *checkf(struct more_control *ctl, char *fs, int *clearfirst)
 	}
 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
 	c = more_getc(ctl, f);
-	*clearfirst = (c == '\f');
+	ctl->clearfirst = (c == '\f');
 	more_ungetc(ctl, c, f);
 	if ((ctl->file_size = stbuf.st_size) == 0)
 		ctl->file_size = LONG_MAX;
@@ -1223,9 +1226,9 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 }
 
 /* Skip n lines in the file f */
-static void skiplns(struct more_control *ctl, int n, FILE *f)
+static void skiplns(struct more_control *ctl, FILE *f)
 {
-	int c;
+	int c, n = ctl->jumpline;
 
 	while (n > 0) {
 		while ((c = more_getc(ctl, f)) != '\n')
@@ -1414,8 +1417,6 @@ static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filenam
 
 static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
 {
-	int initline;
-
 	if (nlines == 0)
 		nlines++;
 	putchar('\r');
@@ -1427,14 +1428,14 @@ static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
 	if (ctl->clreol)
 		my_putstring(ctl->eraseln);
 	putchar('\n');
-	initline = ctl->Currline - ctl->dlines * (nlines + 1);
+	ctl->jumpline = ctl->Currline - ctl->dlines * (nlines + 1);
 	if (!ctl->noscroll)
-		--initline;
-	if (initline < 0)
-		initline = 0;
+		ctl->jumpline--;
+	if (ctl->jumpline < 0)
+		ctl->jumpline = 0;
 	set_pos_fseek(ctl, f, 0);
 	ctl->Currline = 0;	/* skiplns() will make Currline correct */
-	skiplns(ctl, initline, f);
+	skiplns(ctl, f);
 	if (!ctl->noscroll)
 		return ctl->dlines + 1;
 	return ctl->dlines;
@@ -1863,17 +1864,74 @@ static void initterm(struct more_control *ctl)
 	}
 }
 
+static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int left)
+{
+	ctl->context.line = ctl->context.chrctr = 0;
+	ctl->Currline = 0;
+	ctl->file_pos = 0;
+	if (ctl->firstf)
+		sigsetjmp(ctl->restore, 1);
+	if (ctl->firstf) {
+		ctl->firstf = 0;
+		if (ctl->srchopt) {
+			free(ctl->previousre);
+			ctl->previousre = xstrdup(initbuf);
+			search(ctl, initbuf, f, 1);
+			if (ctl->noscroll)
+				left--;
+		} else if (ctl->jumpopt)
+			skiplns(ctl, f);
+	} else if (ctl->fnum < ctl->nfiles && !ctl->no_tty) {
+		sigsetjmp(ctl->restore, 1);
+		left = command(ctl, ctl->fnames[ctl->fnum], f);
+	}
+	if (left != 0) {
+		if ((ctl->noscroll || ctl->clearfirst)
+		    && (ctl->file_size != LONG_MAX)) {
+			if (ctl->clreol)
+				my_putstring(ctl->Home);
+			else
+				doclear(ctl);
+		}
+		if (ctl->prnames) {
+			if (ctl->bad_so)
+				erasep(ctl, 0);
+			if (ctl->clreol)
+				my_putstring(ctl->eraseln);
+			fputs("::::::::::::::", stdout);
+			if (ctl->promptlen > 14)
+				erasep(ctl, 14);
+			putchar('\n');
+			if (ctl->clreol)
+				my_putstring(ctl->eraseln);
+			puts(ctl->fnames[ctl->fnum]);
+			if (ctl->clreol)
+				my_putstring(ctl->eraseln);
+			puts("::::::::::::::");
+			if (left > ctl->Lpp - 4)
+				left = ctl->Lpp - 4;
+		}
+		if (ctl->no_tty)
+			copy_file(f);
+		else {
+			ctl->within = 1;
+			screen(ctl, f, left);
+			ctl->within = 0;
+		}
+	}
+	sigsetjmp(ctl->restore, 1);
+	fflush(stdout);
+	fclose(f);
+	ctl->screen_start.line = ctl->screen_start.chrctr = 0L;
+	ctl->context.line = ctl->context.chrctr = 0L;
+}
+
 int main(int argc, char **argv)
 {
 	FILE *f;
 	char *s;
 	int c;
 	int left;
-	int prnames = 0;
-	int initopt = 0;
-	int srchopt = 0;
-	int clearit = 0;
-	int initline = 0;
 	char *initbuf = NULL;
 	struct more_control ctl = {
 		.firstf = 1,
@@ -1919,15 +1977,15 @@ int main(int argc, char **argv)
 		} else if (c == '+') {
 			s = *ctl.fnames;
 			if (*++s == '/') {
-				srchopt++;
+				ctl.srchopt = 1;
 				initbuf = xstrdup(s + 1);
 			} else {
-				initopt++;
-				for (initline = 0; *s != '\0'; s++)
+				ctl.jumpopt = 1;
+				for (ctl.jumpline = 0; *s != '\0'; s++)
 					if (isdigit(*s))
-						initline =
-						    initline * 10 + *s - '0';
-				--initline;
+						ctl.jumpline =
+						    ctl.jumpline * 10 + *s - '0';
+				ctl.jumpline--;
 			}
 		} else
 			break;
@@ -1946,11 +2004,9 @@ int main(int argc, char **argv)
 		ctl.dlines = ctl.Lpp - 1;
 	left = ctl.dlines;
 	if (ctl.nfiles > 1)
-		prnames++;
+		ctl.prnames = 1;
 	if (!ctl.no_intty && ctl.nfiles == 0)
 		usage(stderr);
-	else
-		f = stdin;
 	if (!ctl.no_tty) {
 		signal(SIGQUIT, onquit);
 		signal(SIGINT, end_it);
@@ -1967,93 +2023,18 @@ int main(int argc, char **argv)
 		if (ctl.no_tty)
 			copy_file(stdin);
 		else {
-			if ((c = more_getc(&ctl, f)) == '\f')
-				doclear(&ctl);
-			else {
-				more_ungetc(&ctl, c, f);
-				if (ctl.noscroll && (c != EOF)) {
-					if (ctl.clreol)
-						my_putstring(ctl.Home);
-					else
-						doclear(&ctl);
-				}
-			}
-			if (srchopt) {
-				free(ctl.previousre);
-				ctl.previousre = xstrdup(initbuf);
-				search(&ctl, initbuf, stdin, 1);
-				if (ctl.noscroll)
-					left--;
-			} else if (initopt)
-				skiplns(&ctl, initline, stdin);
-			screen(&ctl, stdin, left);
+			f = stdin;
+			display_file(&ctl, f, initbuf, left);
 		}
-		ctl.no_intty = 0;
-		prnames++;
 		ctl.firstf = 0;
+		ctl.no_intty = 0;
+		ctl.prnames = 1;
 	}
 
-	while (ctl.fnum < ctl.nfiles) {
-		if ((f = checkf(&ctl, ctl.fnames[ctl.fnum], &clearit)) != NULL) {
-			ctl.context.line = ctl.context.chrctr = 0;
-			ctl.Currline = 0;
-			if (ctl.firstf)
-				sigsetjmp(ctl.restore, 1);
-			if (ctl.firstf) {
-				ctl.firstf = 0;
-				if (srchopt) {
-					free(ctl.previousre);
-					ctl.previousre = xstrdup(initbuf);
-					search(&ctl, initbuf, f, 1);
-					if (ctl.noscroll)
-						left--;
-				} else if (initopt)
-					skiplns(&ctl, initline, f);
-			} else if (ctl.fnum < ctl.nfiles && !ctl.no_tty) {
-				sigsetjmp(ctl.restore, 1);
-				left = command(&ctl, ctl.fnames[ctl.fnum], f);
-			}
-			if (left != 0) {
-				if ((ctl.noscroll || clearit)
-				    && (ctl.file_size != LONG_MAX)) {
-					if (ctl.clreol)
-						my_putstring(ctl.Home);
-					else
-						doclear(&ctl);
-				}
-				if (prnames) {
-					if (ctl.bad_so)
-						erasep(&ctl, 0);
-					if (ctl.clreol)
-						my_putstring(ctl.eraseln);
-					fputs("::::::::::::::", stdout);
-					if (ctl.promptlen > 14)
-						erasep(&ctl, 14);
-					putchar('\n');
-					if (ctl.clreol)
-						my_putstring(ctl.eraseln);
-					puts(ctl.fnames[ctl.fnum]);
-					if (ctl.clreol)
-						my_putstring(ctl.eraseln);
-					puts("::::::::::::::");
-					if (left > ctl.Lpp - 4)
-						left = ctl.Lpp - 4;
-				}
-				if (ctl.no_tty)
-					copy_file(f);
-				else {
-					ctl.within = 1;
-					screen(&ctl, f, left);
-					ctl.within = 0;
-				}
-			}
-			sigsetjmp(ctl.restore, 1);
-			fflush(stdout);
-			fclose(f);
-			ctl.screen_start.line = ctl.screen_start.chrctr = 0L;
-			ctl.context.line = ctl.context.chrctr = 0L;
-		}
-		ctl.fnum++;
+	for (/* nothing */; ctl.fnum < ctl.nfiles; ctl.fnum++) {
+		if ((f = checkf(&ctl, ctl.fnames[ctl.fnum])) == NULL)
+			continue;
+		display_file(&ctl, f, initbuf, left);
 		ctl.firstf = 0;
 	}
 	free(ctl.previousre);
Signature · 1 line
2.3.0
#25 lore raw

PATCH 21/31 more: replace siglongjmp() and signal() calls with signalfd()

In reply to Sami Kerola
Quoted 1 line

From man siglongjmp(3) 'longjmp() and siglongjmp() make programs hard to

understand and maintain. If possible, an alternative should be used.'

---
 text-utils/more.c | 246 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 133 insertions(+), 113 deletions(-)
text-utils/more.c +133 -113
index 5586e69..9a3d96b 100644
@@ -43,12 +43,13 @@
  *	modified mem allocation handling for util-linux
  */
 
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <poll.h>
 #include <regex.h>
-#include <setjmp.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -57,6 +58,7 @@
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/param.h>
+#include <sys/signalfd.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <termios.h>
@@ -72,6 +74,7 @@
 
 #include "closestream.h"
 #include "nls.h"
+#include "rpmatch.h"
 #include "strutils.h"
 #include "widechar.h"
 #include "xalloc.h"
@@ -99,6 +102,7 @@
 #define COMMAND_BUF	200
 #define REGERR_BUF	NUM_COLUMNS
 #define STOP		-10
+#define SEARCH_TIMEOUT	10
 
 struct more_control {
 	struct termios otty;		/* output terminal */
@@ -115,7 +119,7 @@ struct more_control {
 	int nfiles;			/* number of files left to process */
 	char *shell;			/* the name of the shell to use */
 	int shellp;			/* does previous shell command exists */
-	sigjmp_buf restore;		/* siglongjmp() destination */
+	int sigfd;			/* signalfd() file descriptor */
 	char *Line;			/* line buffer */
 	size_t LineLen;			/* size of line buffer */
 	int Lpp;			/* lines per page */
@@ -161,7 +165,6 @@ struct more_control {
 		hard:1,			/* is this hard copy terminal */
 		hardtabs:1,		/* print spaces instead of '\t' */
 		jumpopt:1,		/* is jumpline defined */
-		inwait:1,		/* is waiting user input */
 		lastp:1,		/* run previous key command */
 		noscroll:1,		/* do not scroll, clear the screen and then display text */
 		notell:1,		/* suppress quit dialog */
@@ -172,17 +175,14 @@ struct more_control {
 		pstate:1,		/* is underlining going on */
 		soglitch:1,		/* terminal has standout mode glitch */
 		srchopt:1,		/* is init search pattern defined */
+		search_called:1,	/* previous more command was a search */
 		ssp_opt:1,		/* suppress white space */
-		startup:1,		/* is startup completed */
 		stop_opt:1,		/* stop after form feeds */
 		ulglitch:1,		/* terminal is underlining in glitch mode */
 		ul_opt:1,		/* underline as best we can */
 		within:1,		/* true if we are within a file, false if we are between files */
 		Wrap:1;			/* set if automargins */
 };
-/* FIXME: the global_ctl is used in signal handlers, until the signal
- * handling is corrected to use signalfd().  */
-struct more_control *global_ctl;
 
 static void __attribute__((__noreturn__)) usage(FILE *out)
 {
@@ -696,7 +696,6 @@ static void prompt(struct more_control *ctl, char *filename)
 		fflush(stdout);
 	} else
 		fputc('\a', stderr);
-	ctl->inwait = 1;
 }
 
 static void reset_tty(struct more_control *ctl)
@@ -715,20 +714,20 @@ static void reset_tty(struct more_control *ctl)
 }
 
 /* Clean up terminal state and exit. Also come here if interrupt signal received */
-static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
+static void __attribute__((__noreturn__)) end_it(struct more_control *ctl)
 {
-	reset_tty(global_ctl);
-	if (global_ctl->clreol) {
+	reset_tty(ctl);
+	if (ctl->clreol) {
 		putchar('\r');
-		my_putstring(global_ctl->EodClr);
+		my_putstring(ctl->EodClr);
 		fflush(stdout);
-	} else if (!global_ctl->clreol && (global_ctl->promptlen > 0)) {
-		kill_line(global_ctl);
+	} else if (!ctl->clreol && (ctl->promptlen > 0)) {
+		kill_line(ctl);
 		fflush(stdout);
 	} else
 		fputc('\n', stderr);
-	free(global_ctl->previousre);
-	free(global_ctl->Line);
+	free(ctl->previousre);
+	free(ctl->Line);
 	_exit(EXIT_SUCCESS);
 }
 
@@ -739,7 +738,7 @@ static int readch(struct more_control *ctl)
 	errno = 0;
 	if (read(STDERR_FILENO, &c, 1) <= 0) {
 		if (errno != EINTR)
-			end_it(0);
+			end_it(ctl);
 		else
 			c = ctl->otty.c_cc[VKILL];
 	}
@@ -822,7 +821,6 @@ static void more_error(struct more_control *ctl, char *mess)
 		fputs(mess, stdout);
 	fflush(stdout);
 	ctl->errors++;
-	siglongjmp(ctl->restore, 1);
 }
 
 static void erase_one_column(struct more_control *ctl)
@@ -911,7 +909,6 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 			} else {
 				if (!ctl->eraseln)
 					ctl->promptlen = maxlen;
-				siglongjmp(ctl->restore, 1);
 			}
 		} else if (((cc_t) c == ctl->otty.c_cc[VKILL]) && !slash) {
 			if (ctl->hard) {
@@ -1019,51 +1016,23 @@ static void set_tty(struct more_control *ctl)
 }
 
 /* Come here if a quit signal is received */
-static void onquit(int dummy __attribute__((__unused__)))
+static void onquit(struct more_control *ctl)
 {
-	signal(SIGQUIT, SIG_IGN);
-	if (!global_ctl->inwait) {
-		putchar('\n');
-		if (!global_ctl->startup) {
-			signal(SIGQUIT, onquit);
-			siglongjmp(global_ctl->restore, 1);
-		} else
-			global_ctl->Pause = 1;
-	} else if (!global_ctl->dum_opt && global_ctl->notell) {
-		global_ctl->promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
-		global_ctl->notell = 0;
-	}
-	signal(SIGQUIT, onquit);
+	if (!ctl->dum_opt && ctl->notell) {
+		ctl->promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
+		ctl->notell = 0;
+	} else
+		end_it(ctl);
 }
 
 /* Come here when we get a suspend signal from the terminal */
-static void onsusp(int dummy __attribute__((__unused__)))
+static void onsusp(struct more_control *ctl)
 {
-	sigset_t signals, oldmask;
-
-	/* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
-	signal(SIGTTOU, SIG_IGN);
-	reset_tty(global_ctl);
+	reset_tty(ctl);
 	fflush(stdout);
-	signal(SIGTTOU, SIG_DFL);
-	/* Send the TSTP signal to suspend our process group */
-	signal(SIGTSTP, SIG_DFL);
-
-	/* unblock SIGTSTP or we won't be able to suspend ourself */
-	sigemptyset(&signals);
-	sigaddset(&signals, SIGTSTP);
-	sigprocmask(SIG_UNBLOCK, &signals, &oldmask);
-
-	kill(0, SIGTSTP);
-	/* Pause for station break */
-
-	sigprocmask(SIG_SETMASK, &oldmask, NULL);
-
+	kill(0, SIGSTOP);
 	/* We're back */
-	signal(SIGTSTP, onsusp);
-	set_tty(global_ctl);
-	if (global_ctl->inwait)
-		siglongjmp(global_ctl->restore, 1);
+	set_tty(ctl);
 }
 
 static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
@@ -1115,15 +1084,8 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 		exit(EXIT_FAILURE);
 	}
 	if (id > 0) {
-		signal(SIGINT, SIG_IGN);
-		signal(SIGQUIT, SIG_IGN);
-		if (ctl->catch_susp)
-			signal(SIGTSTP, SIG_DFL);
 		while (wait(0) > 0)
 			/* nothing */ ;
-		signal(SIGQUIT, onquit);
-		if (ctl->catch_susp)
-			signal(SIGTSTP, onsusp);
 	} else
 		fputs(_("can't fork\n"), stderr);
 	set_tty(ctl);
@@ -1195,7 +1157,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 	case 'n':
 		if (nlines == 0) {
 			if (ctl->fnum >= ctl->nfiles - 1)
-				end_it(0);
+				end_it(ctl);
 			nlines++;
 		}
 		putchar('\r');
@@ -1218,7 +1180,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 		return -1;
 	case 'q':
 	case 'Q':
-		end_it(0);
+		end_it(ctl);
 	default:
 		fputc('\a', stderr);
 		return -1;
@@ -1267,6 +1229,26 @@ static void rdline(struct more_control *ctl, FILE *f)
 	*p = '\0';
 }
 
+volatile sig_atomic_t alarm_received;
+
+static void sig_alarm_handler(int sig __attribute__((__unused__)))
+{
+	alarm_received = 1;
+}
+
+static int stop_search(struct more_control *ctl)
+{
+	char buf[2];
+
+	ctl->promptlen = printf(_("No search results in %d seconds, stop searching?"), SEARCH_TIMEOUT);
+	buf[0] = getchar();
+	buf[1] = '\0';
+	erasep(ctl, 0);
+	alarm(SEARCH_TIMEOUT);
+	alarm_received = 0;
+	return rpmatch(buf);
+}
+
 /* Search for nth occurrence of regular expression contained in buf in
  * the file */
 static void search(struct more_control *ctl, char buf[], FILE *file, int n)
@@ -1288,8 +1270,14 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 		char s[REGERR_BUF];
 		regerror(rc, &re, s, sizeof s);
 		more_error(ctl, s);
+		return;
 	}
+	alarm_received = 0;
+	signal(SIGALRM, sig_alarm_handler);
+	alarm(SEARCH_TIMEOUT);
 	while (!feof(file)) {
+		if (alarm_received && stop_search(ctl))
+			break;
 		line3 = line2;
 		line2 = line1;
 		line1 = ctl->file_pos;
@@ -1329,6 +1317,8 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 			}
 		}
 	}
+	alarm(0);
+	signal(SIGALRM, SIG_DFL);
 	regfree(&re);
 	if (feof(file)) {
 		if (!ctl->no_intty) {
@@ -1336,7 +1326,7 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 			set_pos_fseek(ctl, file, startline);
 		} else {
 			fputs(_("\nPattern not found\n"), stdout);
-			end_it(0);
+			end_it(ctl);
 		}
 		free(ctl->previousre);
 		ctl->previousre = NULL;
@@ -1467,6 +1457,26 @@ static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char com
 	return 1;
 }
 
+/* Come here if a signal for a window size change is received */
+#ifdef SIGWINCH
+static void chgwinsz(struct more_control *ctl)
+{
+	struct winsize win;
+
+	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) {
+		if (win.ws_row != 0) {
+			ctl->Lpp = win.ws_row;
+			ctl->nscroll = ctl->Lpp / 2 - 1;
+			if (ctl->nscroll <= 0)
+				ctl->nscroll = 1;
+			ctl->dlines = ctl->Lpp - 1;
+		}
+		if (win.ws_col != 0)
+			ctl->Mcol = win.ws_col;
+	}
+}
+#endif				/* SIGWINCH */
+
 /* Read a command and do it.  A command consists of an optional integer
  * argument followed by the command character.  Return the number of
  * lines to display in the next screenful.  If there is nothing more to
@@ -1478,12 +1488,51 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 	char colonch;
 	int done = 0;
 	char comchar, cmdbuf[INIT_BUF];
+	struct pollfd pfd[2];
 
+	pfd[0].fd = ctl->sigfd;
+	pfd[0].events = POLLIN | POLLERR | POLLHUP;
+	pfd[1].fd = STDIN_FILENO;
+	pfd[1].events = POLLIN;
 	if (!ctl->errors)
 		prompt(ctl, filename);
 	else
 		ctl->errors = 0;
+	ctl->search_called = 0;
 	while (!done) {
+		if (poll(pfd, 2, -1) < 0) {
+			if (errno == EAGAIN)
+				continue;
+			more_error(ctl, _("poll failed"));
+			continue;
+		}
+		if (pfd[0].revents != 0) {
+			struct signalfd_siginfo info;
+			ssize_t sz;
+
+			sz = read(pfd[0].fd, &info, sizeof(info));
+			assert(sz == sizeof(info));
+			switch (info.ssi_signo) {
+			case SIGINT:
+				end_it(ctl);
+				break;
+			case SIGQUIT:
+				onquit(ctl);
+				break;
+			case SIGTSTP:
+				onsusp(ctl);
+				break;
+#ifdef SIGWINCH
+			case SIGWINCH:
+				chgwinsz(ctl);
+				break;
+#endif
+			default:
+				abort();
+			}
+		}
+		if (pfd[1].revents == 0)
+			continue;
 		nlines = number(ctl, &comchar);
 		ctl->lastp = colonch = 0;
 		if (comchar == '.') {	/* Repeat last command */
@@ -1533,7 +1582,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case 'q':
 		case 'Q':
-			end_it(0);
+			end_it(ctl);
 		case 's':
 		case 'f':
 		case ctrl('F'):
@@ -1587,6 +1636,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			ctl->lastp = 1;
 			/* fall through */
 		case '/':
+			ctl->search_called = 1;
 			if (nlines == 0)
 				nlines++;
 			kill_line(ctl);
@@ -1643,7 +1693,6 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		}
 	}
 	putchar('\r');
-	ctl->inwait = 0;
 	ctl->notell = 1;
 	return retval;
 }
@@ -1699,11 +1748,11 @@ static void screen(struct more_control *ctl, FILE *f, int num_lines)
 		if (ctl->Pause && ctl->clreol)
 			my_putstring(ctl->EodClr);
 		more_ungetc(ctl, c, f);
-		sigsetjmp(ctl->restore, 1);
 		ctl->Pause = 0;
-		ctl->startup = 0;
-		if ((num_lines = command(ctl, NULL, f)) == 0)
-			return;
+		do {
+			if ((num_lines = command(ctl, NULL, f)) == 0)
+				return;
+		} while (ctl->search_called && !ctl->previousre);
 		if (ctl->hard && ctl->promptlen > 0)
 			erasep(ctl, 0);
 		if (ctl->noscroll && num_lines >= ctl->dlines) {
@@ -1717,28 +1766,6 @@ static void screen(struct more_control *ctl, FILE *f, int num_lines)
 	}
 }
 
-/* Come here if a signal for a window size change is received */
-#ifdef SIGWINCH
-static void chgwinsz(int dummy __attribute__((__unused__)))
-{
-	struct winsize win;
-
-	signal(SIGWINCH, SIG_IGN);
-	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) {
-		if (win.ws_row != 0) {
-			global_ctl->Lpp = win.ws_row;
-			global_ctl->nscroll = global_ctl->Lpp / 2 - 1;
-			if (global_ctl->nscroll <= 0)
-				global_ctl->nscroll = 1;
-			global_ctl->dlines = global_ctl->Lpp - 1;
-		}
-		if (win.ws_col != 0)
-			global_ctl->Mcol = win.ws_col;
-	}
-	signal(SIGWINCH, chgwinsz);
-}
-#endif				/* SIGWINCH */
-
 static void copy_file(FILE *f)
 {
 	char buf[BUFSIZ];
@@ -1869,8 +1896,6 @@ static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int l
 	ctl->context.line = ctl->context.chrctr = 0;
 	ctl->Currline = 0;
 	ctl->file_pos = 0;
-	if (ctl->firstf)
-		sigsetjmp(ctl->restore, 1);
 	if (ctl->firstf) {
 		ctl->firstf = 0;
 		if (ctl->srchopt) {
@@ -1881,10 +1906,8 @@ static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int l
 				left--;
 		} else if (ctl->jumpopt)
 			skiplns(ctl, f);
-	} else if (ctl->fnum < ctl->nfiles && !ctl->no_tty) {
-		sigsetjmp(ctl->restore, 1);
+	} else if (ctl->fnum < ctl->nfiles && !ctl->no_tty)
 		left = command(ctl, ctl->fnames[ctl->fnum], f);
-	}
 	if (left != 0) {
 		if ((ctl->noscroll || ctl->clearfirst)
 		    && (ctl->file_size != LONG_MAX)) {
@@ -1919,7 +1942,6 @@ static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int l
 			ctl->within = 0;
 		}
 	}
-	sigsetjmp(ctl->restore, 1);
 	fflush(stdout);
 	fclose(f);
 	ctl->screen_start.line = ctl->screen_start.chrctr = 0L;
@@ -1933,11 +1955,11 @@ int main(int argc, char **argv)
 	int c;
 	int left;
 	char *initbuf = NULL;
+	sigset_t sigset;
 	struct more_control ctl = {
 		.firstf = 1,
 		.fold_opt = 1,
 		.notell = 1,
-		.startup = 1,
 		.stop_opt = 1,
 		.ul_opt = 1,
 		.Wrap = 1,
@@ -1946,7 +1968,6 @@ int main(int argc, char **argv)
 		.nscroll = SCROLL_LEN,
 		0
 	};
-	global_ctl = &ctl;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -2007,18 +2028,17 @@ int main(int argc, char **argv)
 		ctl.prnames = 1;
 	if (!ctl.no_intty && ctl.nfiles == 0)
 		usage(stderr);
-	if (!ctl.no_tty) {
-		signal(SIGQUIT, onquit);
-		signal(SIGINT, end_it);
+	if (!ctl.no_tty)
+		tcsetattr(STDERR_FILENO, TCSANOW, &ctl.otty);
+	sigemptyset(&sigset);
+	sigaddset(&sigset, SIGINT);
+	sigaddset(&sigset, SIGQUIT);
+	sigaddset(&sigset, SIGTSTP);
 #ifdef SIGWINCH
-		signal(SIGWINCH, chgwinsz);
+	sigaddset(&sigset, SIGWINCH);
 #endif
-		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
-			signal(SIGTSTP, onsusp);
-			ctl.catch_susp = 1;
-		}
-		tcsetattr(STDERR_FILENO, TCSANOW, &ctl.otty);
-	}
+	sigprocmask(SIG_BLOCK, &sigset, NULL);
+	ctl.sigfd = signalfd(-1, &sigset, SFD_CLOEXEC);
 	if (ctl.no_intty) {
 		if (ctl.no_tty)
 			copy_file(stdin);
Signature · 1 line
2.3.0
#26 lore raw

PATCH 22/31 more: init function variables at declaration

In reply to Sami Kerola

Two lines expressions, such as example below, is more readable when done with a single line that combines declaration and init.

int i;
i = 0;

---
 text-utils/more.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
text-utils/more.c +8 -16
index 9a3d96b..e9c123c 100644
@@ -372,8 +372,8 @@ static void prepare_line_buffer(struct more_control *ctl)
 static int get_line(struct more_control *ctl, FILE *f, int *length)
 {
 	int c;
-	char *p;
-	int column;
+	char *p = ctl->Line;
+	int column = 0;
 	static int colflg;
 
 #ifdef HAVE_WIDECHAR
@@ -392,8 +392,6 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 #endif
 	prepare_line_buffer(ctl);
 
-	p = ctl->Line;
-	column = 0;
 	c = more_getc(ctl, f);
 	if (colflg && c == '\n') {
 		ctl->Currline++;
@@ -749,10 +747,9 @@ static int readch(struct more_control *ctl)
  * which terminates the number. */
 static int number(struct more_control *ctl, char *cmd)
 {
-	int i;
+	int i = 0;
 	char ch;
 
-	i = 0;
 	for (;;) {
 		ch = readch(ctl);
 		if (isdigit(ch))
@@ -833,13 +830,11 @@ static void erase_one_column(struct more_control *ctl)
 
 static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 {
-	char *sp;
+	char *sp = buf;
 	int c;
 	int slash = 0;
-	int maxlen;
+	int maxlen = 0;
 
-	sp = buf;
-	maxlen = 0;
 	while (sp - buf < nmax) {
 		if (ctl->promptlen > maxlen)
 			maxlen = ctl->promptlen;
@@ -957,7 +952,7 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 
 static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 {
-	char *inpstr;
+	char *inpstr = inbuf;
 	char *outstr;
 	char c;
 	char *temp;
@@ -967,7 +962,6 @@ static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 	xtra = strlen(ctl->fnames[ctl->fnum]) + strlen(ctl->shell_line) + 1;
 	tempsz = 200 + xtra;
 	temp = xmalloc(tempsz);
-	inpstr = inbuf;
 	outstr = temp;
 	while ((c = *inpstr++) != '\0') {
 		offset = outstr - temp;
@@ -1098,7 +1092,7 @@ static void do_shell(struct more_control *ctl, char *filename)
 {
 	char cmdbuf[COMMAND_BUF];
 	int rc;
-	char *expanded;
+	char *expanded = NULL;
 
 	kill_line(ctl);
 	putchar('!');
@@ -1108,7 +1102,6 @@ static void do_shell(struct more_control *ctl, char *filename)
 		fputs(ctl->shell_line, stdout);
 	else {
 		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
-		expanded = 0;
 		rc = expand(ctl, &expanded, cmdbuf);
 		if (expanded) {
 			if (strlen(expanded) < sizeof(ctl->shell_line))
@@ -1257,13 +1250,12 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 	long line1 = startline;
 	long line2 = startline;
 	long line3;
-	int lncount;
+	int lncount = 0;
 	int saveln, rc;
 	regex_t re;
 
 	ctl->context.line = saveln = ctl->Currline;
 	ctl->context.chrctr = startline;
-	lncount = 0;
 	if (!buf)
 		goto notfound;
 	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
Signature · 1 line
2.3.0
#27 lore raw

PATCH 23/31 more: rename variable and function names easier to understand

In reply to Sami Kerola
---
 text-utils/more.c | 714 +++++++++++++++++++++++++++---------------------------
 1 file changed, 357 insertions(+), 357 deletions(-)
text-utils/more.c +357 -357
index e9c123c..15fd3a0 100644
@@ -106,39 +106,39 @@
 
 struct more_control {
 	struct termios otty;		/* output terminal */
-	struct termios savetty0;	/* original terminal settings */
+	struct termios orig_tty;	/* original terminal settings */
 	long file_pos;			/* file position */
 	long file_size;			/* file size */
-	int fnum;			/* position in argv[] */
+	int argv_position;		/* position in argv[] */
 	int nscroll;			/* number of lines scrolled by 'd' */
-	int dlines;			/* screen size in lines */
+	int lines_per_screen;		/* screen size in lines */
 	int promptlen;			/* message prompt length */
 	int jumpline;			/* line number to jump */
-	int Currline;			/* line we are currently at */
+	int current_line;		/* line we are currently at */
 	char **fnames;			/* the list of file names */
 	int nfiles;			/* number of files left to process */
 	char *shell;			/* the name of the shell to use */
 	int shellp;			/* does previous shell command exists */
 	int sigfd;			/* signalfd() file descriptor */
-	char *Line;			/* line buffer */
-	size_t LineLen;			/* size of line buffer */
-	int Lpp;			/* lines per page */
-	char *Clear;			/* clear screen */
+	char *linebuf;			/* line buffer */
+	size_t linesz;			/* size of line buffer */
+	int lines_per_page;		/* lines per page */
+	char *clear;			/* clear screen */
 	char *eraseln;			/* erase line */
-	char *Senter;			/* enter standout mode */
-	char *Sexit;			/* exit standout mode */
-	char *ULenter;			/* enter underline mode */
-	char *ULexit;			/* exit underline mode */
-	char *chUL;			/* underline character */
-	char *chBS;			/* backspace character */
-	char *Home;			/* go to screen home position */
+	char *std_enter;		/* enter standout mode */
+	char *std_exit;			/* exit standout mode */
+	char *underline_enter;		/* enter underline mode */
+	char *underline_exit;		/* exit underline mode */
+	char *underlining_char;		/* underline character */
+	char *backspace_char;		/* backspace character */
+	char *go_home;			/* go to screen home position */
 	char *cursorm;			/* cursor move destination */
 	char cursorhome[40];		/* contains cursor movement to home */
-	char *EodClr;			/* clear rest of screen */
-	int Mcol;			/* number of columns */
+	char *end_clear;		/* clear rest of screen */
+	int ncolumns;			/* number of columns */
 	char *previousre;		/* previous search() buf[] item */
 	struct {
-		long chrctr;		/* row number */
+		long nrow;		/* row number */
 		long line;		/* line number */
 	} context,
 	  screen_start;
@@ -152,36 +152,36 @@ struct more_control {
 	unsigned int
 		bad_so:1,		/* true if overwriting does not turn off standout */
 		catch_susp:1,		/* should SIGTSTP signal be caught */
-		clreol:1,		/* do not scroll, paint each screen from the top */
 		clearfirst:1,		/* is first character in file \f */
+		clreol_opt:1,		/* do not scroll, paint each screen from the top */
 		docrterase:1,		/* is erase previous supported */
 		docrtkill:1,		/* is erase input supported */
 		dumb:1,			/* is terminal type known */
-		dum_opt:1,		/* suppress bell */
 		eatnl:1,		/* is newline ignored after 80 cols */
 		errors:1,		/* is an error reported */
-		firstf:1,		/* is the input file the first in list */
+		first_file:1,		/* is the input file the first in list */
 		fold_opt:1,		/* fold long lines */
-		hard:1,			/* is this hard copy terminal */
+		hard_term:1,		/* is this hard copy terminal */
 		hardtabs:1,		/* print spaces instead of '\t' */
 		jumpopt:1,		/* is jumpline defined */
-		lastp:1,		/* run previous key command */
-		noscroll:1,		/* do not scroll, clear the screen and then display text */
-		notell:1,		/* suppress quit dialog */
+		no_bell:1,		/* suppress bell */
 		no_intty:1,		/* is input in interactive mode */
 		no_tty:1,		/* is output in interactive mode */
-		Pause:1,		/* is output paused */
-		prnames:1,		/* print file name banner */
-		pstate:1,		/* is underlining going on */
-		soglitch:1,		/* terminal has standout mode glitch */
-		srchopt:1,		/* is init search pattern defined */
+		noscroll_opt:1,		/* do not scroll, clear the screen and then display text */
+		notell:1,		/* suppress quit dialog */
+		pause:1,		/* is output paused */
+		print_names:1,		/* print file name banner */
+		rerun_command:1,	/* run previous key command */
 		search_called:1,	/* previous more command was a search */
-		ssp_opt:1,		/* suppress white space */
+		search_opt:1,		/* is init search pattern defined */
+		squeeze_opt:1,		/* suppress white space */
+		stdout_glitch:1,	/* terminal has standout mode glitch */
 		stop_opt:1,		/* stop after form feeds */
-		ulglitch:1,		/* terminal is underlining in glitch mode */
+		ul_glitch:1,		/* terminal is underlining in glitch mode */
 		ul_opt:1,		/* underline as best we can */
+		underlining:1,		/* is underlining going on */
 		within:1,		/* true if we are within a file, false if we are between files */
-		Wrap:1;			/* set if automargins */
+		wrap_margin:1;		/* set if automargins */
 };
 
 static void __attribute__((__noreturn__)) usage(FILE *out)
@@ -225,13 +225,13 @@ static void argscan(struct more_control *ctl, char *s)
 		case '8':
 		case '9':
 			if (!seen_num) {
-				ctl->dlines = 0;
+				ctl->lines_per_screen = 0;
 				seen_num = 1;
 			}
-			ctl->dlines = ctl->dlines * 10 + *s - '0';
+			ctl->lines_per_screen = ctl->lines_per_screen * 10 + *s - '0';
 			break;
 		case 'd':
-			ctl->dum_opt = 1;
+			ctl->no_bell = 1;
 			break;
 		case 'l':
 			ctl->stop_opt = 0;
@@ -240,13 +240,13 @@ static void argscan(struct more_control *ctl, char *s)
 			ctl->fold_opt = 0;
 			break;
 		case 'p':
-			ctl->noscroll = 1;
+			ctl->noscroll_opt = 1;
 			break;
 		case 'c':
-			ctl->clreol = 1;
+			ctl->clreol_opt = 1;
 			break;
 		case 's':
-			ctl->ssp_opt = 1;
+			ctl->squeeze_opt = 1;
 			break;
 		case 'u':
 			ctl->ul_opt = 0;
@@ -268,11 +268,11 @@ static void argscan(struct more_control *ctl, char *s)
 	}
 }
 
-/* magic --
+/* check_magic --
  *	check for file magic numbers.  This code would best be shared
  *	with the file(1) program or, perhaps, more should not try to be
  *	so smart. */
-static int magic(FILE *f, char *fs)
+static int check_magic(FILE *f, char *fs)
 {
 	signed char twobytes[2];
 
@@ -298,7 +298,7 @@ static int magic(FILE *f, char *fs)
 	return 0;
 }
 
-static void set_pos_fseek(struct more_control *ctl, FILE *stream, long pos)
+static void more_fseek(struct more_control *ctl, FILE *stream, long pos)
 {
 	ctl->file_pos = pos;
 	fseek(stream, pos, 0);
@@ -318,7 +318,7 @@ static int more_ungetc(struct more_control *ctl, int c, FILE *stream)
 
 /* Check whether the file named by fs is an ASCII file which the user may
  * access.  If it is, return the opened file.  Otherwise return NULL. */
-static FILE *checkf(struct more_control *ctl, char *fs)
+static FILE *more_fopen(struct more_control *ctl, char *fs)
 {
 	struct stat stbuf;
 	FILE *f;
@@ -326,7 +326,7 @@ static FILE *checkf(struct more_control *ctl, char *fs)
 
 	if (stat(fs, &stbuf) == -1) {
 		fflush(stdout);
-		if (ctl->clreol)
+		if (ctl->clreol_opt)
 			my_putstring(ctl->eraseln);
 		warn(_("stat of %s failed"), fs);
 		return NULL;
@@ -340,7 +340,7 @@ static FILE *checkf(struct more_control *ctl, char *fs)
 		warn(_("cannot open %s"), fs);
 		return NULL;
 	}
-	if (magic(f, fs)) {
+	if (check_magic(f, fs)) {
 		fclose(f);
 		return NULL;
 	}
@@ -355,24 +355,24 @@ static FILE *checkf(struct more_control *ctl, char *fs)
 
 static void prepare_line_buffer(struct more_control *ctl)
 {
-	size_t nsz = ctl->Mcol * 4;
+	size_t nsz = ctl->ncolumns * 4;
 
-	if (ctl->LineLen >= nsz)
+	if (ctl->linesz >= nsz)
 		return;
 
 	if (nsz < LINSIZ)
 		nsz = LINSIZ;
 
 	/* alloc nsz and extra space for \n\0 */
-	ctl->Line = xrealloc(ctl->Line, nsz + 2);
-	ctl->LineLen = nsz;
+	ctl->linebuf = xrealloc(ctl->linebuf, nsz + 2);
+	ctl->linesz = nsz;
 }
 
 /* Get a logical line */
 static int get_line(struct more_control *ctl, FILE *f, int *length)
 {
 	int c;
-	char *p = ctl->Line;
+	char *p = ctl->linebuf;
 	int column = 0;
 	static int colflg;
 
@@ -394,10 +394,10 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 
 	c = more_getc(ctl, f);
 	if (colflg && c == '\n') {
-		ctl->Currline++;
+		ctl->current_line++;
 		c = more_getc(ctl, f);
 	}
-	while (p < &ctl->Line[ctl->LineLen - 1]) {
+	while (p < &ctl->linebuf[ctl->linesz - 1]) {
 #ifdef HAVE_WIDECHAR
 		if (ctl->fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
 			use_mbc_buffer_flag = 0;
@@ -418,8 +418,8 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 				column++;
 				file_pos_bak++;
 
-				if (column >= ctl->Mcol)
-					set_pos_fseek(ctl, f, file_pos_bak);
+				if (column >= ctl->ncolumns)
+					more_fseek(ctl, f, file_pos_bak);
 				else {
 					memmove(mbc, mbc + 1, --mbc_pos);
 					if (mbc_pos > 0) {
@@ -432,11 +432,11 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 			default:
 				wc_width = wcwidth(wc);
 
-				if (column + wc_width > ctl->Mcol) {
-					set_pos_fseek(ctl, f, file_pos_bak);
+				if (column + wc_width > ctl->ncolumns) {
+					more_fseek(ctl, f, file_pos_bak);
 					break_flag = 1;
 				} else {
-					for (i = 0; p < &ctl->Line[ctl->LineLen - 1] &&
+					for (i = 0; p < &ctl->linebuf[ctl->linesz - 1] &&
 						    i < mbc_pos; i++)
 						*p++ = mbc[i];
 					if (wc_width > 0)
@@ -444,7 +444,7 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 				}
 			}
 
-			if (break_flag || column >= ctl->Mcol)
+			if (break_flag || column >= ctl->ncolumns)
 				break;
 
 			c = more_getc(ctl, f);
@@ -452,28 +452,28 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 		}
 #endif	/* HAVE_WIDECHAR */
 		if (c == EOF) {
-			if (p > ctl->Line) {
+			if (p > ctl->linebuf) {
 				*p = '\0';
-				*length = p - ctl->Line;
+				*length = p - ctl->linebuf;
 				return column;
 			}
-			*length = p - ctl->Line;
+			*length = p - ctl->linebuf;
 			return EOF;
 		}
 		if (c == '\n') {
-			ctl->Currline++;
+			ctl->current_line++;
 			break;
 		}
 
 		*p++ = c;
 		if (c == '\t') {
-			if (!ctl->hardtabs || (column < ctl->promptlen && !ctl->hard)) {
+			if (!ctl->hardtabs || (column < ctl->promptlen && !ctl->hard_term)) {
 				if (ctl->hardtabs && ctl->eraseln && !ctl->dumb) {
 					column = 1 + (column | 7);
 					my_putstring(ctl->eraseln);
 					ctl->promptlen = 0;
 				} else {
-					for (--p; p < &ctl->Line[ctl->LineLen - 1];) {
+					for (--p; p < &ctl->linebuf[ctl->linesz - 1];) {
 						*p++ = ' ';
 						if ((++column & 7) == 0)
 							break;
@@ -489,7 +489,7 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 			int next = more_getc(ctl, f);
 			if (next == '\n') {
 				p--;
-				ctl->Currline++;
+				ctl->current_line++;
 				break;
 			}
 			more_ungetc(ctl, next, f);
@@ -498,9 +498,9 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 			p[-1] = '^';
 			*p++ = 'L';
 			column += 2;
-			ctl->Pause = 1;
+			ctl->pause = 1;
 		} else if (c == EOF) {
-			*length = p - ctl->Line;
+			*length = p - ctl->linebuf;
 			return column;
 		} else {
 #ifdef HAVE_WIDECHAR
@@ -538,34 +538,34 @@ static int get_line(struct more_control *ctl, FILE *f, int *length)
 			}
 		}
 
-		if (column >= ctl->Mcol && ctl->fold_opt)
+		if (column >= ctl->ncolumns && ctl->fold_opt)
 			break;
 #ifdef HAVE_WIDECHAR
-		if (use_mbc_buffer_flag == 0 && p >= &ctl->Line[ctl->LineLen - 1 - 4])
+		if (use_mbc_buffer_flag == 0 && p >= &ctl->linebuf[ctl->linesz - 1 - 4])
 			/* don't read another char if there is no space for
 			 * whole multibyte sequence */
 			break;
 #endif
 		c = more_getc(ctl, f);
 	}
-	if (column >= ctl->Mcol && ctl->Mcol > 0)
-		if (!ctl->Wrap)
+	if (column >= ctl->ncolumns && ctl->ncolumns > 0)
+		if (!ctl->wrap_margin)
 			*p++ = '\n';
-	colflg = column == ctl->Mcol && ctl->fold_opt;
-	if (colflg && ctl->eatnl && ctl->Wrap)
+	colflg = column == ctl->ncolumns && ctl->fold_opt;
+	if (colflg && ctl->eatnl && ctl->wrap_margin)
 		*p++ = '\n';	/* simulate normal wrap */
-	*length = p - ctl->Line;
+	*length = p - ctl->linebuf;
 	*p = 0;
 	return column;
 }
 
 /* Erase the rest of the prompt, assuming we are starting at column col. */
-static void erasep(struct more_control *ctl, int col)
+static void erase_prompt(struct more_control *ctl, int col)
 {
 
 	if (ctl->promptlen == 0)
 		return;
-	if (ctl->hard)
+	if (ctl->hard_term)
 		putchar('\n');
 	else {
 		if (col == 0)
@@ -587,7 +587,7 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 	return mblength;
 }
 
-static int wouldul(char *s, int n)
+static int would_underline(char *s, int n)
 {
 	if (n < 2)
 		return 0;
@@ -597,7 +597,7 @@ static int wouldul(char *s, int n)
 }
 
 /* Print a buffer of n characters */
-static void prbuf(struct more_control *ctl, char *s, int n)
+static void print_buffer(struct more_control *ctl, char *s, int n)
 {
 	char c;			/* next output character */
 	int state;		/* next output char's UL state */
@@ -607,27 +607,27 @@ static void prbuf(struct more_control *ctl, char *s, int n)
 			putchar(*s++);
 			continue;
 		}
-		if (*s == ' ' && ctl->pstate == 0 && ctl->ulglitch
-		    && wouldul(s + 1, n - 1)) {
+		if (*s == ' ' && ctl->underlining == 0 && ctl->ul_glitch
+		    && would_underline(s + 1, n - 1)) {
 			s++;
 			continue;
 		}
-		if ((state = wouldul(s, n)) != 0) {
+		if ((state = would_underline(s, n)) != 0) {
 			c = (*s == '_') ? s[2] : *s;
 			n -= 2;
 			s += 3;
 		} else
 			c = *s++;
-		if (state != ctl->pstate) {
-			if (c == ' ' && state == 0 && ctl->ulglitch
-			    && wouldul(s, n - 1))
+		if (state != ctl->underlining) {
+			if (c == ' ' && state == 0 && ctl->ul_glitch
+			    && would_underline(s, n - 1))
 				state = 1;
 			else
-				my_putstring(state ? ctl->ULenter : ctl->
-					     ULexit);
+				my_putstring(state ? ctl->underline_enter : ctl->
+					     underline_exit);
 		}
-		if (c != ' ' || ctl->pstate == 0 || state != 0
-		    || ctl->ulglitch == 0)
+		if (c != ' ' || ctl->underlining == 0 || state != 0
+		    || ctl->ul_glitch == 0)
 #ifdef HAVE_WIDECHAR
 		{
 			wchar_t wc;
@@ -645,35 +645,35 @@ static void prbuf(struct more_control *ctl, char *s, int n)
 #else
 			putchar(c);
 #endif				/* HAVE_WIDECHAR */
-		if (state && *ctl->chUL) {
-			fputs(ctl->chBS, stdout);
-			my_putstring(ctl->chUL);
+		if (state && *ctl->underlining_char) {
+			fputs(ctl->backspace_char, stdout);
+			my_putstring(ctl->underlining_char);
 		}
-		ctl->pstate = state;
+		ctl->underlining = state;
 	}
 }
 
 /* Erase the current line entirely */
 static void kill_line(struct more_control *ctl)
 {
-	erasep(ctl, 0);
+	erase_prompt(ctl, 0);
 	if (!ctl->eraseln || ctl->dumb)
 		putchar('\r');
 }
 
-static void prompt(struct more_control *ctl, char *filename)
+static void output_prompt(struct more_control *ctl, char *filename)
 {
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	else if (ctl->promptlen > 0)
 		kill_line(ctl);
-	if (!ctl->hard) {
+	if (!ctl->hard_term) {
 		ctl->promptlen = 0;
-		if (ctl->Senter && ctl->Sexit) {
-			my_putstring(ctl->Senter);
-			ctl->promptlen += (2 * ctl->soglitch);
+		if (ctl->std_enter && ctl->std_exit) {
+			my_putstring(ctl->std_enter);
+			ctl->promptlen += (2 * ctl->stdout_glitch);
 		}
-		if (ctl->clreol)
+		if (ctl->clreol_opt)
 			my_putstring(ctl->eraseln);
 		ctl->promptlen += printf(_("--More--"));
 		if (filename != NULL)
@@ -683,14 +683,14 @@ static void prompt(struct more_control *ctl, char *filename)
 			    printf("(%d%%)",
 				   (int)((ctl->file_pos * 100) / ctl->file_size));
 		}
-		if (ctl->dum_opt) {
+		if (ctl->no_bell) {
 			ctl->promptlen +=
 			    printf(_("[Press space to continue, 'q' to quit.]"));
 		}
-		if (ctl->Senter && ctl->Sexit)
-			my_putstring(ctl->Sexit);
-		if (ctl->clreol)
-			my_putstring(ctl->EodClr);
+		if (ctl->std_enter && ctl->std_exit)
+			my_putstring(ctl->std_exit);
+		if (ctl->clreol_opt)
+			my_putstring(ctl->end_clear);
 		fflush(stdout);
 	} else
 		fputc('\a', stderr);
@@ -700,43 +700,43 @@ static void reset_tty(struct more_control *ctl)
 {
 	if (ctl->no_tty)
 		return;
-	if (ctl->pstate) {
-		tputs(ctl->ULexit, STDOUT_FILENO, putchar);
+	if (ctl->underlining) {
+		tputs(ctl->underline_exit, STDOUT_FILENO, putchar);
 		fflush(stdout);
-		ctl->pstate = 0;
+		ctl->underlining = 0;
 	}
 	ctl->otty.c_lflag |= ICANON | ECHO;
-	ctl->otty.c_cc[VMIN] = ctl->savetty0.c_cc[VMIN];
-	ctl->otty.c_cc[VTIME] = ctl->savetty0.c_cc[VTIME];
-	tcsetattr(STDERR_FILENO, TCSANOW, &ctl->savetty0);
+	ctl->otty.c_cc[VMIN] = ctl->orig_tty.c_cc[VMIN];
+	ctl->otty.c_cc[VTIME] = ctl->orig_tty.c_cc[VTIME];
+	tcsetattr(STDERR_FILENO, TCSANOW, &ctl->orig_tty);
 }
 
 /* Clean up terminal state and exit. Also come here if interrupt signal received */
-static void __attribute__((__noreturn__)) end_it(struct more_control *ctl)
+static void __attribute__((__noreturn__)) exit_more(struct more_control *ctl)
 {
 	reset_tty(ctl);
-	if (ctl->clreol) {
+	if (ctl->clreol_opt) {
 		putchar('\r');
-		my_putstring(ctl->EodClr);
+		my_putstring(ctl->end_clear);
 		fflush(stdout);
-	} else if (!ctl->clreol && (ctl->promptlen > 0)) {
+	} else if (!ctl->clreol_opt && (ctl->promptlen > 0)) {
 		kill_line(ctl);
 		fflush(stdout);
 	} else
 		fputc('\n', stderr);
 	free(ctl->previousre);
-	free(ctl->Line);
+	free(ctl->linebuf);
 	_exit(EXIT_SUCCESS);
 }
 
-static int readch(struct more_control *ctl)
+static int read_char(struct more_control *ctl)
 {
 	unsigned char c;
 
 	errno = 0;
 	if (read(STDERR_FILENO, &c, 1) <= 0) {
 		if (errno != EINTR)
-			end_it(ctl);
+			exit_more(ctl);
 		else
 			c = ctl->otty.c_cc[VKILL];
 	}
@@ -745,13 +745,13 @@ static int readch(struct more_control *ctl)
 
 /* Read a decimal number from the terminal.  Set cmd to the non-digit
  * which terminates the number. */
-static int number(struct more_control *ctl, char *cmd)
+static int read_number(struct more_control *ctl, char *cmd)
 {
 	int i = 0;
 	char ch;
 
 	for (;;) {
-		ch = readch(ctl);
+		ch = read_char(ctl);
 		if (isdigit(ch))
 			i = i * 10 + ch - '0';
 		else if ((cc_t)ch == ctl->otty.c_cc[VKILL])
@@ -766,30 +766,30 @@ static int number(struct more_control *ctl, char *cmd)
 
 /* Skip nskip files in the file list (from the command line).  Nskip may
  * be negative. */
-static void skipf(struct more_control *ctl, int nskip)
+static void change_file(struct more_control *ctl, int nskip)
 {
 	if (nskip == 0)
 		return;
 	if (nskip > 0) {
-		if (ctl->fnum + nskip > ctl->nfiles - 1)
-			nskip = ctl->nfiles - ctl->fnum - 1;
+		if (ctl->argv_position + nskip > ctl->nfiles - 1)
+			nskip = ctl->nfiles - ctl->argv_position - 1;
 	} else if (ctl->within)
-		ctl->fnum++;
-	ctl->fnum += nskip;
-	if (ctl->fnum < 0)
-		ctl->fnum = 0;
+		ctl->argv_position++;
+	ctl->argv_position += nskip;
+	if (ctl->argv_position < 0)
+		ctl->argv_position = 0;
 	puts(_("\n...Skipping "));
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	if (nskip > 0)
 		fputs(_("...Skipping to file "), stdout);
 	else
 		fputs(_("...Skipping back to file "), stdout);
-	puts(ctl->fnames[ctl->fnum]);
-	if (ctl->clreol)
+	puts(ctl->fnames[ctl->argv_position]);
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	putchar('\n');
-	ctl->fnum--;
+	ctl->argv_position--;
 }
 
 static void show(struct more_control *ctl, char c)
@@ -805,15 +805,15 @@ static void show(struct more_control *ctl, char c)
 
 static void more_error(struct more_control *ctl, char *mess)
 {
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	else
 		kill_line(ctl);
 	ctl->promptlen += strlen(mess);
-	if (ctl->Senter && ctl->Sexit) {
-		my_putstring(ctl->Senter);
+	if (ctl->std_enter && ctl->std_exit) {
+		my_putstring(ctl->std_enter);
 		fputs(mess, stdout);
-		my_putstring(ctl->Sexit);
+		my_putstring(ctl->std_exit);
 	} else
 		fputs(mess, stdout);
 	fflush(stdout);
@@ -838,7 +838,7 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 	while (sp - buf < nmax) {
 		if (ctl->promptlen > maxlen)
 			maxlen = ctl->promptlen;
-		c = readch(ctl);
+		c = read_char(ctl);
 		if (c == '\\')
 			slash++;
 		else if (((cc_t) c == ctl->otty.c_cc[VERASE]) && !slash) {
@@ -906,7 +906,7 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 					ctl->promptlen = maxlen;
 			}
 		} else if (((cc_t) c == ctl->otty.c_cc[VKILL]) && !slash) {
-			if (ctl->hard) {
+			if (ctl->hard_term) {
 				show(ctl, c);
 				putchar('\n');
 				putchar(pchar);
@@ -914,7 +914,7 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 				putchar('\r');
 				putchar(pchar);
 				if (ctl->eraseln)
-					erasep(ctl, 1);
+					erase_prompt(ctl, 1);
 				else if (ctl->docrtkill)
 					while (ctl->promptlen-- > 1)
 						fputs("\b \b", stderr);
@@ -950,7 +950,7 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 		more_error(ctl, _("Line too long"));
 }
 
-static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
+static int command_expansion(struct more_control *ctl, char **outbuf, char *inbuf)
 {
 	char *inpstr = inbuf;
 	char *outstr;
@@ -959,7 +959,7 @@ static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 	int changed = 0;
 	int tempsz, xtra, offset;
 
-	xtra = strlen(ctl->fnames[ctl->fnum]) + strlen(ctl->shell_line) + 1;
+	xtra = strlen(ctl->fnames[ctl->argv_position]) + strlen(ctl->shell_line) + 1;
 	tempsz = 200 + xtra;
 	temp = xmalloc(tempsz);
 	outstr = temp;
@@ -973,8 +973,8 @@ static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
 		switch (c) {
 		case '%':
 			if (!ctl->no_intty) {
-				strcpy(outstr, ctl->fnames[ctl->fnum]);
-				outstr += strlen(ctl->fnames[ctl->fnum]);
+				strcpy(outstr, ctl->fnames[ctl->argv_position]);
+				outstr += strlen(ctl->fnames[ctl->argv_position]);
 				changed = 1;
 			} else
 				*outstr++ = c;
@@ -1010,17 +1010,17 @@ static void set_tty(struct more_control *ctl)
 }
 
 /* Come here if a quit signal is received */
-static void onquit(struct more_control *ctl)
+static void quit_more(struct more_control *ctl)
 {
-	if (!ctl->dum_opt && ctl->notell) {
+	if (!ctl->no_bell && ctl->notell) {
 		ctl->promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
 		ctl->notell = 0;
 	} else
-		end_it(ctl);
+		exit_more(ctl);
 }
 
 /* Come here when we get a suspend signal from the terminal */
-static void onsusp(struct more_control *ctl)
+static void suspend_more(struct more_control *ctl)
 {
 	reset_tty(ctl);
 	fflush(stdout);
@@ -1085,7 +1085,7 @@ static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
 	set_tty(ctl);
 	puts("------------------------");
 err:
-	prompt(ctl, filename);
+	output_prompt(ctl, filename);
 }
 
 static void do_shell(struct more_control *ctl, char *filename)
@@ -1098,11 +1098,11 @@ static void do_shell(struct more_control *ctl, char *filename)
 	putchar('!');
 	fflush(stdout);
 	ctl->promptlen = 1;
-	if (ctl->lastp)
+	if (ctl->rerun_command)
 		fputs(ctl->shell_line, stdout);
 	else {
 		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
-		rc = expand(ctl, &expanded, cmdbuf);
+		rc = command_expansion(ctl, &expanded, cmdbuf);
 		if (expanded) {
 			if (strlen(expanded) < sizeof(ctl->shell_line))
 				strcpy(ctl->shell_line, expanded);
@@ -1112,7 +1112,7 @@ static void do_shell(struct more_control *ctl, char *filename)
 		}
 		if (rc < 0) {
 			fputs(_("  Overflow\n"), stderr);
-			prompt(ctl, filename);
+			output_prompt(ctl, filename);
 			return;
 		} else if (rc > 0) {
 			kill_line(ctl);
@@ -1128,12 +1128,12 @@ static void do_shell(struct more_control *ctl, char *filename)
 
 /* Execute a colon-prefixed command.  Returns <0 if not a command that
  * should cause more of the file to be printed. */
-static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
+static int colon_command(struct more_control *ctl, char *filename, int cmd, int nlines)
 {
 	char ch;
 
 	if (cmd == 0)
-		ch = readch(ctl);
+		ch = read_char(ctl);
 	else
 		ch = cmd;
 	ctl->lastcolon = ch;
@@ -1142,20 +1142,20 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 		kill_line(ctl);
 		if (!ctl->no_intty)
 			ctl->promptlen =
-			    printf(_("\"%s\" line %d"), ctl->fnames[ctl->fnum], ctl->Currline);
+			    printf(_("\"%s\" line %d"), ctl->fnames[ctl->argv_position], ctl->current_line);
 		else
-			ctl->promptlen = printf(_("[Not a file] line %d"), ctl->Currline);
+			ctl->promptlen = printf(_("[Not a file] line %d"), ctl->current_line);
 		fflush(stdout);
 		return -1;
 	case 'n':
 		if (nlines == 0) {
-			if (ctl->fnum >= ctl->nfiles - 1)
-				end_it(ctl);
+			if (ctl->argv_position >= ctl->nfiles - 1)
+				exit_more(ctl);
 			nlines++;
 		}
 		putchar('\r');
-		erasep(ctl, 0);
-		skipf(ctl, nlines);
+		erase_prompt(ctl, 0);
+		change_file(ctl, nlines);
 		return 0;
 	case 'p':
 		if (ctl->no_intty) {
@@ -1163,17 +1163,17 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 			return -1;
 		}
 		putchar('\r');
-		erasep(ctl, 0);
+		erase_prompt(ctl, 0);
 		if (nlines == 0)
 			nlines++;
-		skipf(ctl, -nlines);
+		change_file(ctl, -nlines);
 		return 0;
 	case '!':
 		do_shell(ctl, filename);
 		return -1;
 	case 'q':
 	case 'Q':
-		end_it(ctl);
+		exit_more(ctl);
 	default:
 		fputc('\a', stderr);
 		return -1;
@@ -1181,7 +1181,7 @@ static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
 }
 
 /* Skip n lines in the file f */
-static void skiplns(struct more_control *ctl, FILE *f)
+static void skip_lines(struct more_control *ctl, FILE *f)
 {
 	int c, n = ctl->jumpline;
 
@@ -1190,15 +1190,15 @@ static void skiplns(struct more_control *ctl, FILE *f)
 			if (c == EOF)
 				return;
 		n--;
-		ctl->Currline++;
+		ctl->current_line++;
 	}
 }
 
 /*  Clear the screen */
-static void doclear(struct more_control *ctl)
+static void clear_tty(struct more_control *ctl)
 {
-	if (ctl->Clear && !ctl->hard) {
-		my_putstring(ctl->Clear);
+	if (ctl->clear && !ctl->hard_term) {
+		my_putstring(ctl->clear);
 		/* Put out carriage return so that system doesn't get
 		 * confused by escape sequences when expanding tabs */
 		putchar('\r');
@@ -1206,19 +1206,19 @@ static void doclear(struct more_control *ctl)
 	}
 }
 
-static void rdline(struct more_control *ctl, FILE *f)
+static void read_line(struct more_control *ctl, FILE *f)
 {
 	int c;
 	char *p;
 
 	prepare_line_buffer(ctl);
 
-	p = ctl->Line;
+	p = ctl->linebuf;
 	while ((c = more_getc(ctl, f)) != '\n' && c != EOF
-	       && (size_t)(p - ctl->Line) < ctl->LineLen - 1)
+	       && (size_t)(p - ctl->linebuf) < ctl->linesz - 1)
 		*p++ = c;
 	if (c == '\n')
-		ctl->Currline++;
+		ctl->current_line++;
 	*p = '\0';
 }
 
@@ -1236,7 +1236,7 @@ static int stop_search(struct more_control *ctl)
 	ctl->promptlen = printf(_("No search results in %d seconds, stop searching?"), SEARCH_TIMEOUT);
 	buf[0] = getchar();
 	buf[1] = '\0';
-	erasep(ctl, 0);
+	erase_prompt(ctl, 0);
 	alarm(SEARCH_TIMEOUT);
 	alarm_received = 0;
 	return rpmatch(buf);
@@ -1254,8 +1254,8 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 	int saveln, rc;
 	regex_t re;
 
-	ctl->context.line = saveln = ctl->Currline;
-	ctl->context.chrctr = startline;
+	ctl->context.line = saveln = ctl->current_line;
+	ctl->context.nrow = startline;
 	if (!buf)
 		goto notfound;
 	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
@@ -1273,37 +1273,37 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 		line3 = line2;
 		line2 = line1;
 		line1 = ctl->file_pos;
-		rdline(ctl, file);
+		read_line(ctl, file);
 		lncount++;
-		if (regexec(&re, ctl->Line, 0, NULL, 0) == 0) {
+		if (regexec(&re, ctl->linebuf, 0, NULL, 0) == 0) {
 			if (--n == 0) {
 				if (lncount > 3 || (lncount > 1 && ctl->no_intty)) {
 					putchar('\n');
-					if (ctl->clreol)
+					if (ctl->clreol_opt)
 						my_putstring(ctl->eraseln);
 					fputs(_("...skipping\n"), stdout);
 				}
 				if (!ctl->no_intty) {
-					ctl->Currline -=
+					ctl->current_line -=
 					    (lncount >= 3 ? 3 : lncount);
-					set_pos_fseek(ctl, file, line3);
-					if (ctl->noscroll) {
-						if (ctl->clreol) {
-							my_putstring(ctl->Home);
+					more_fseek(ctl, file, line3);
+					if (ctl->noscroll_opt) {
+						if (ctl->clreol_opt) {
+							my_putstring(ctl->go_home);
 							my_putstring(ctl->eraseln);
 						} else
-							doclear(ctl);
+							clear_tty(ctl);
 					}
 				} else {
 					kill_line(ctl);
-					if (ctl->noscroll) {
-						if (ctl->clreol) {
-							my_putstring(ctl->Home);
+					if (ctl->noscroll_opt) {
+						if (ctl->clreol_opt) {
+							my_putstring(ctl->go_home);
 							my_putstring(ctl->eraseln);
 						} else
-							doclear(ctl);
+							clear_tty(ctl);
 					}
-					puts(ctl->Line);
+					puts(ctl->linebuf);
 				}
 				break;
 			}
@@ -1314,11 +1314,11 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 	regfree(&re);
 	if (feof(file)) {
 		if (!ctl->no_intty) {
-			ctl->Currline = saveln;
-			set_pos_fseek(ctl, file, startline);
+			ctl->current_line = saveln;
+			more_fseek(ctl, file, startline);
 		} else {
 			fputs(_("\nPattern not found\n"), stdout);
-			end_it(ctl);
+			exit_more(ctl);
 		}
 		free(ctl->previousre);
 		ctl->previousre = NULL;
@@ -1371,7 +1371,7 @@ static void runtime_usage(void)
 static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filename)
 {
 	char *editor, *p;
-	int n = (ctl->Currline - ctl->dlines <= 0 ? 1 : ctl->Currline - (ctl->dlines + 1) / 2);
+	int n = (ctl->current_line - ctl->lines_per_screen <= 0 ? 1 : ctl->current_line - (ctl->lines_per_screen + 1) / 2);
 	int split = 0;
 
 	editor = find_editor();
@@ -1388,13 +1388,13 @@ static void execute_editor(struct more_control *ctl, char *cmdbuf, char *filenam
 	} else
 		sprintf(cmdbuf, "+%d", n);
 	kill_line(ctl);
-	printf("%s %s %s", editor, cmdbuf, ctl->fnames[ctl->fnum]);
+	printf("%s %s %s", editor, cmdbuf, ctl->fnames[ctl->argv_position]);
 	if (split) {
 		cmdbuf[2] = 0;
 		execute(ctl, filename, editor, editor,
-			cmdbuf, cmdbuf + 3, ctl->fnames[ctl->fnum], (char *)0);
+			cmdbuf, cmdbuf + 3, ctl->fnames[ctl->argv_position], (char *)0);
 	} else
-		execute(ctl, filename, editor, editor, cmdbuf, ctl->fnames[ctl->fnum], (char *)0);
+		execute(ctl, filename, editor, editor, cmdbuf, ctl->fnames[ctl->argv_position], (char *)0);
 }
 
 static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
@@ -1402,25 +1402,25 @@ static int skip_backwards(struct more_control *ctl, FILE *f, int nlines)
 	if (nlines == 0)
 		nlines++;
 	putchar('\r');
-	erasep(ctl, 0);
+	erase_prompt(ctl, 0);
 	putchar('\n');
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	printf(P_("...back %d page", "...back %d pages", nlines), nlines);
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	putchar('\n');
-	ctl->jumpline = ctl->Currline - ctl->dlines * (nlines + 1);
-	if (!ctl->noscroll)
+	ctl->jumpline = ctl->current_line - ctl->lines_per_screen * (nlines + 1);
+	if (!ctl->noscroll_opt)
 		ctl->jumpline--;
 	if (ctl->jumpline < 0)
 		ctl->jumpline = 0;
-	set_pos_fseek(ctl, f, 0);
-	ctl->Currline = 0;	/* skiplns() will make Currline correct */
-	skiplns(ctl, f);
-	if (!ctl->noscroll)
-		return ctl->dlines + 1;
-	return ctl->dlines;
+	more_fseek(ctl, f, 0);
+	ctl->current_line = 0;	/* skip_lines() will make current_line correct */
+	skip_lines(ctl, f);
+	if (!ctl->noscroll_opt)
+		return ctl->lines_per_screen + 1;
+	return ctl->lines_per_screen;
 }
 
 static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char comchar)
@@ -1428,14 +1428,14 @@ static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char com
 	if (nlines == 0)
 		nlines++;
 	if (comchar == 'f')
-		nlines *= ctl->dlines;
+		nlines *= ctl->lines_per_screen;
 	putchar('\r');
-	erasep(ctl, 0);
+	erase_prompt(ctl, 0);
 	putchar('\n');
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	printf(P_("...skipping %d line", "...skipping %d lines", nlines), nlines);
-	if (ctl->clreol)
+	if (ctl->clreol_opt)
 		my_putstring(ctl->eraseln);
 	putchar('\n');
 	while (nlines > 0) {
@@ -1443,7 +1443,7 @@ static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char com
 		while ((c = more_getc(ctl, f)) != '\n')
 			if (c == EOF)
 				return 0;
-		ctl->Currline++;
+		ctl->current_line++;
 		nlines--;
 	}
 	return 1;
@@ -1451,20 +1451,20 @@ static int skip_forwards(struct more_control *ctl, FILE *f, int nlines, char com
 
 /* Come here if a signal for a window size change is received */
 #ifdef SIGWINCH
-static void chgwinsz(struct more_control *ctl)
+static void change_window_sz(struct more_control *ctl)
 {
 	struct winsize win;
 
 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1) {
 		if (win.ws_row != 0) {
-			ctl->Lpp = win.ws_row;
-			ctl->nscroll = ctl->Lpp / 2 - 1;
+			ctl->lines_per_page = win.ws_row;
+			ctl->nscroll = ctl->lines_per_page / 2 - 1;
 			if (ctl->nscroll <= 0)
 				ctl->nscroll = 1;
-			ctl->dlines = ctl->Lpp - 1;
+			ctl->lines_per_screen = ctl->lines_per_page - 1;
 		}
 		if (win.ws_col != 0)
-			ctl->Mcol = win.ws_col;
+			ctl->ncolumns = win.ws_col;
 	}
 }
 #endif				/* SIGWINCH */
@@ -1487,7 +1487,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 	pfd[1].fd = STDIN_FILENO;
 	pfd[1].events = POLLIN;
 	if (!ctl->errors)
-		prompt(ctl, filename);
+		output_prompt(ctl, filename);
 	else
 		ctl->errors = 0;
 	ctl->search_called = 0;
@@ -1506,17 +1506,17 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			assert(sz == sizeof(info));
 			switch (info.ssi_signo) {
 			case SIGINT:
-				end_it(ctl);
+				exit_more(ctl);
 				break;
 			case SIGQUIT:
-				onquit(ctl);
+				quit_more(ctl);
 				break;
 			case SIGTSTP:
-				onsusp(ctl);
+				suspend_more(ctl);
 				break;
 #ifdef SIGWINCH
 			case SIGWINCH:
-				chgwinsz(ctl);
+				change_window_sz(ctl);
 				break;
 #endif
 			default:
@@ -1525,10 +1525,10 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		}
 		if (pfd[1].revents == 0)
 			continue;
-		nlines = number(ctl, &comchar);
-		ctl->lastp = colonch = 0;
+		nlines = read_number(ctl, &comchar);
+		ctl->rerun_command = colonch = 0;
 		if (comchar == '.') {	/* Repeat last command */
-			ctl->lastp++;
+			ctl->rerun_command++;
 			comchar = ctl->lastcmd;
 			nlines = ctl->lastarg;
 			if (ctl->lastcmd == ':')
@@ -1538,12 +1538,12 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		ctl->lastarg = nlines;
 		if ((cc_t) comchar == ctl->otty.c_cc[VERASE]) {
 			kill_line(ctl);
-			prompt(ctl, filename);
+			output_prompt(ctl, filename);
 			continue;
 		}
 		switch (comchar) {
 		case ':':
-			retval = colon(ctl, filename, colonch, nlines);
+			retval = colon_command(ctl, filename, colonch, nlines);
 			if (retval >= 0)
 				done++;
 			break;
@@ -1559,9 +1559,9 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 		case ' ':
 		case 'z':
 			if (nlines == 0)
-				nlines = ctl->dlines;
+				nlines = ctl->lines_per_screen;
 			else if (comchar == 'z')
-				ctl->dlines = nlines;
+				ctl->lines_per_screen = nlines;
 			retval = nlines;
 			done = 1;
 			break;
@@ -1574,19 +1574,19 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case 'q':
 		case 'Q':
-			end_it(ctl);
+			exit_more(ctl);
 		case 's':
 		case 'f':
 		case ctrl('F'):
 			if (skip_forwards(ctl, f, nlines, comchar))
-				retval = ctl->dlines;
+				retval = ctl->lines_per_screen;
 			else
 				retval = 0;
 			done = 1;
 			break;
 		case '\n':
 			if (nlines != 0)
-				ctl->dlines = nlines;
+				ctl->lines_per_screen = nlines;
 			else
 				nlines = 1;
 			retval = nlines;
@@ -1594,10 +1594,10 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case '\f':
 			if (!ctl->no_intty) {
-				doclear(ctl);
-				set_pos_fseek(ctl, f, ctl->screen_start.chrctr);
-				ctl->Currline = ctl->screen_start.line;
-				retval = ctl->dlines;
+				clear_tty(ctl);
+				more_fseek(ctl, f, ctl->screen_start.nrow);
+				ctl->current_line = ctl->screen_start.line;
+				retval = ctl->lines_per_screen;
 				done = 1;
 				break;
 			}
@@ -1607,8 +1607,8 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			if (!ctl->no_intty) {
 				kill_line(ctl);
 				fputs(_("\n***Back***\n\n"), stdout);
-				set_pos_fseek(ctl, f, ctl->context.chrctr);
-				ctl->Currline = ctl->context.line;
+				more_fseek(ctl, f, ctl->context.nrow);
+				ctl->current_line = ctl->context.line;
 				retval = lines;
 				done = 1;
 				break;
@@ -1617,7 +1617,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case '=':
 			kill_line(ctl);
-			ctl->promptlen = printf("%d", ctl->Currline);
+			ctl->promptlen = printf("%d", ctl->current_line);
 			fflush(stdout);
 			break;
 		case 'n':
@@ -1625,7 +1625,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 				more_error(ctl, _("No previous regular expression"));
 				break;
 			}
-			ctl->lastp = 1;
+			ctl->rerun_command = 1;
 			/* fall through */
 		case '/':
 			ctl->search_called = 1;
@@ -1635,7 +1635,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			putchar('/');
 			ctl->promptlen = 1;
 			fflush(stdout);
-			if (ctl->lastp) {
+			if (ctl->rerun_command) {
 				fputc('\r', stderr);
 				search(ctl, ctl->previousre, f, nlines);
 			} else {
@@ -1645,7 +1645,7 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 				ctl->previousre = xstrdup(cmdbuf);
 				search(ctl, cmdbuf, f, nlines);
 			}
-			retval = ctl->dlines - 1;
+			retval = ctl->lines_per_screen - 1;
 			done = 1;
 			break;
 		case '!':
@@ -1653,10 +1653,10 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			break;
 		case '?':
 		case 'h':
-			if (ctl->noscroll)
-				doclear(ctl);
+			if (ctl->noscroll_opt)
+				clear_tty(ctl);
 			runtime_usage();
-			prompt(ctl, filename);
+			output_prompt(ctl, filename);
 			break;
 		case 'v':	/* This case should go right before default */
 			if (!ctl->no_intty) {
@@ -1665,15 +1665,15 @@ static int command(struct more_control *ctl, char *filename, FILE *f)
 			}
 			/* fall through */
 		default:
-			if (ctl->dum_opt) {
+			if (ctl->no_bell) {
 				kill_line(ctl);
-				if (ctl->Senter && ctl->Sexit) {
-					my_putstring(ctl->Senter);
+				if (ctl->std_enter && ctl->std_exit) {
+					my_putstring(ctl->std_enter);
 					ctl->promptlen =
 					    printf(_
 						   ("[Press 'h' for instructions.]"))
-					    + 2 * ctl->soglitch;
-					my_putstring(ctl->Sexit);
+					    + 2 * ctl->stdout_glitch;
+					my_putstring(ctl->std_exit);
 				} else
 					ctl->promptlen =
 					    printf(_
@@ -1698,63 +1698,63 @@ static void screen(struct more_control *ctl, FILE *f, int num_lines)
 	static int prev_len = 1;	/* length of previous line */
 
 	for (;;) {
-		while (num_lines > 0 && !ctl->Pause) {
+		while (num_lines > 0 && !ctl->pause) {
 			if ((nchars = get_line(ctl, f, &length)) == EOF) {
-				if (ctl->clreol)
-					my_putstring(ctl->EodClr);
+				if (ctl->clreol_opt)
+					my_putstring(ctl->end_clear);
 				return;
 			}
-			if (ctl->ssp_opt && length == 0 && prev_len == 0)
+			if (ctl->squeeze_opt && length == 0 && prev_len == 0)
 				continue;
 			prev_len = length;
 			if (ctl->bad_so
-			    || ((ctl->Senter && *ctl->Senter == ' ') && (ctl->promptlen > 0)))
-				erasep(ctl, 0);
+			    || ((ctl->std_enter && *ctl->std_enter == ' ') && (ctl->promptlen > 0)))
+				erase_prompt(ctl, 0);
 			/* must clear before drawing line since tabs on
 			 * some terminals do not erase what they tab
 			 * over. */
-			if (ctl->clreol)
+			if (ctl->clreol_opt)
 				my_putstring(ctl->eraseln);
-			prbuf(ctl, ctl->Line, length);
+			print_buffer(ctl, ctl->linebuf, length);
 			if (nchars < ctl->promptlen)
-				erasep(ctl, nchars);	/* erasep () sets promptlen to 0 */
+				erase_prompt(ctl, nchars);	/* erase_prompt () sets promptlen to 0 */
 			else
 				ctl->promptlen = 0;
-			if (nchars < ctl->Mcol || !ctl->fold_opt)
-				prbuf(ctl, "\n", 1);	/* will turn off UL if necessary */
+			if (nchars < ctl->ncolumns || !ctl->fold_opt)
+				print_buffer(ctl, "\n", 1);	/* will turn off UL if necessary */
 			if (nchars == STOP)
 				break;
 			num_lines--;
 		}
-		if (ctl->pstate) {
-			my_putstring(ctl->ULexit);
-			ctl->pstate = 0;
+		if (ctl->underlining) {
+			my_putstring(ctl->underline_exit);
+			ctl->underlining = 0;
 		}
 		fflush(stdout);
 		if ((c = more_getc(ctl, f)) == EOF) {
-			if (ctl->clreol)
-				my_putstring(ctl->EodClr);
+			if (ctl->clreol_opt)
+				my_putstring(ctl->end_clear);
 			return;
 		}
 
-		if (ctl->Pause && ctl->clreol)
-			my_putstring(ctl->EodClr);
+		if (ctl->pause && ctl->clreol_opt)
+			my_putstring(ctl->end_clear);
 		more_ungetc(ctl, c, f);
-		ctl->Pause = 0;
+		ctl->pause = 0;
 		do {
 			if ((num_lines = command(ctl, NULL, f)) == 0)
 				return;
 		} while (ctl->search_called && !ctl->previousre);
-		if (ctl->hard && ctl->promptlen > 0)
-			erasep(ctl, 0);
-		if (ctl->noscroll && num_lines >= ctl->dlines) {
-			if (ctl->clreol)
-				my_putstring(ctl->Home);
+		if (ctl->hard_term && ctl->promptlen > 0)
+			erase_prompt(ctl, 0);
+		if (ctl->noscroll_opt && num_lines >= ctl->lines_per_screen) {
+			if (ctl->clreol_opt)
+				my_putstring(ctl->go_home);
 			else
-				doclear(ctl);
+				clear_tty(ctl);
 		}
-		ctl->screen_start.line = ctl->Currline;
-		ctl->screen_start.chrctr = ctl->file_pos;
+		ctl->screen_start.line = ctl->current_line;
+		ctl->screen_start.nrow = ctl->file_pos;
 	}
 }
 
@@ -1793,38 +1793,38 @@ static void initterm(struct more_control *ctl)
 #ifdef TIOCGWINSZ
 			if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
 #endif
-				ctl->Lpp = my_tgetnum(TERM_LINES);
-				ctl->Mcol = my_tgetnum(TERM_COLS);
+				ctl->lines_per_page = my_tgetnum(TERM_LINES);
+				ctl->ncolumns = my_tgetnum(TERM_COLS);
 #ifdef TIOCGWINSZ
 			} else {
-				if ((ctl->Lpp = win.ws_row) == 0)
-					ctl->Lpp = my_tgetnum(TERM_LINES);
-				if ((ctl->Mcol = win.ws_col) == 0)
-					ctl->Mcol = my_tgetnum(TERM_COLS);
+				if ((ctl->lines_per_page = win.ws_row) == 0)
+					ctl->lines_per_page = my_tgetnum(TERM_LINES);
+				if ((ctl->ncolumns = win.ws_col) == 0)
+					ctl->ncolumns = my_tgetnum(TERM_COLS);
 			}
 #endif
-			if ((ctl->Lpp <= 0) || my_tgetflag(TERM_HARD_COPY)) {
-				ctl->hard = 1;
-				ctl->Lpp = LINES_PER_PAGE;
+			if ((ctl->lines_per_page <= 0) || my_tgetflag(TERM_HARD_COPY)) {
+				ctl->hard_term = 1;
+				ctl->lines_per_page = LINES_PER_PAGE;
 			}
 
 			if (my_tgetflag(TERM_EAT_NEW_LINE))
 				/* Eat newline at last column + 1; dec, concept */
 				ctl->eatnl = 1;
-			if (ctl->Mcol <= 0)
-				ctl->Mcol = NUM_COLUMNS;
+			if (ctl->ncolumns <= 0)
+				ctl->ncolumns = NUM_COLUMNS;
 
-			ctl->Wrap = my_tgetflag(TERM_AUTO_RIGHT_MARGIN);
+			ctl->wrap_margin = my_tgetflag(TERM_AUTO_RIGHT_MARGIN);
 			ctl->bad_so = my_tgetflag(TERM_CEOL);
 			ctl->eraseln = my_tgetstr(TERM_CLEAR_TO_LINE_END);
-			ctl->Clear = my_tgetstr(TERM_CLEAR);
-			ctl->Senter = my_tgetstr(TERM_STANDARD_MODE);
-			ctl->Sexit = my_tgetstr(TERM_EXIT_STANDARD_MODE);
+			ctl->clear = my_tgetstr(TERM_CLEAR);
+			ctl->std_enter = my_tgetstr(TERM_STANDARD_MODE);
+			ctl->std_exit = my_tgetstr(TERM_EXIT_STANDARD_MODE);
 			tmp = my_tgetnum(TERM_STD_MODE_GLITCH);
 			if (tmp < 0)
-				ctl->soglitch = 0;
+				ctl->stdout_glitch = 0;
 			else if (0 < tmp)
-				ctl->soglitch = 1;
+				ctl->stdout_glitch = 1;
 
 			/* Set up for underlining:  some terminals don't
 			 * need it; others have start/stop sequences,
@@ -1835,25 +1835,25 @@ static void initterm(struct more_control *ctl)
 			if (my_tgetflag(TERM_UNDERLINE)
 			    || my_tgetflag(TERM_OVER_STRIKE))
 				ctl->ul_opt = 0;
-			if ((ctl->chUL = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
-				ctl->chUL = "";
-			if (((ctl->ULenter =
+			if ((ctl->underlining_char = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
+				ctl->underlining_char = "";
+			if (((ctl->underline_enter =
 			      my_tgetstr(TERM_ENTER_UNDERLINE)) == NULL
-			     || (ctl->ULexit =
+			     || (ctl->underline_exit =
 				 my_tgetstr(TERM_EXIT_UNDERLINE)) == NULL)
-			    && !*ctl->chUL) {
-				if ((ctl->ULenter = ctl->Senter) == NULL
-				    || (ctl->ULexit = ctl->Sexit) == NULL) {
-					ctl->ULenter = "";
-					ctl->ULexit = "";
+			    && !*ctl->underlining_char) {
+				if ((ctl->underline_enter = ctl->std_enter) == NULL
+				    || (ctl->underline_exit = ctl->std_exit) == NULL) {
+					ctl->underline_enter = "";
+					ctl->underline_exit = "";
 				} else
-					ctl->ulglitch = ctl->soglitch;
+					ctl->ul_glitch = ctl->stdout_glitch;
 			} else {
-				ctl->ulglitch = 0;
+				ctl->ul_glitch = 0;
 			}
 
-			ctl->Home = my_tgetstr(TERM_HOME);
-			if (ctl->Home == NULL || *ctl->Home == '\0') {
+			ctl->go_home = my_tgetstr(TERM_HOME);
+			if (ctl->go_home == NULL || *ctl->go_home == '\0') {
 				if ((ctl->cursorm =
 				     my_tgetstr(TERM_CURSOR_ADDRESS)) != NULL) {
 					const char *t =
@@ -1861,12 +1861,12 @@ static void initterm(struct more_control *ctl)
 								   0);
 					xstrncpy(ctl->cursorhome, t,
 						 sizeof(ctl->cursorhome));
-					ctl->Home = ctl->cursorhome;
+					ctl->go_home = ctl->cursorhome;
 				}
 			}
-			ctl->EodClr = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
-			if ((ctl->chBS = my_tgetstr(TERM_LINE_DOWN)) == NULL)
-				ctl->chBS = "\b";
+			ctl->end_clear = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
+			if ((ctl->backspace_char = my_tgetstr(TERM_LINE_DOWN)) == NULL)
+				ctl->backspace_char = "\b";
 
 		}
 		if ((ctl->shell = getenv("SHELL")) == NULL)
@@ -1874,7 +1874,7 @@ static void initterm(struct more_control *ctl)
 	}
 	ctl->no_intty = tcgetattr(STDIN_FILENO, &ctl->otty);
 	tcgetattr(STDERR_FILENO, &ctl->otty);
-	ctl->savetty0 = ctl->otty;
+	ctl->orig_tty = ctl->otty;
 	ctl->hardtabs = (ctl->otty.c_oflag & TABDLY) != XTABS;
 	if (!ctl->no_tty) {
 		ctl->otty.c_lflag &= ~(ICANON | ECHO);
@@ -1885,46 +1885,46 @@ static void initterm(struct more_control *ctl)
 
 static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int left)
 {
-	ctl->context.line = ctl->context.chrctr = 0;
-	ctl->Currline = 0;
+	ctl->context.line = ctl->context.nrow = 0;
+	ctl->current_line = 0;
 	ctl->file_pos = 0;
-	if (ctl->firstf) {
-		ctl->firstf = 0;
-		if (ctl->srchopt) {
+	if (ctl->first_file) {
+		ctl->first_file = 0;
+		if (ctl->search_opt) {
 			free(ctl->previousre);
 			ctl->previousre = xstrdup(initbuf);
 			search(ctl, initbuf, f, 1);
-			if (ctl->noscroll)
+			if (ctl->noscroll_opt)
 				left--;
 		} else if (ctl->jumpopt)
-			skiplns(ctl, f);
-	} else if (ctl->fnum < ctl->nfiles && !ctl->no_tty)
-		left = command(ctl, ctl->fnames[ctl->fnum], f);
+			skip_lines(ctl, f);
+	} else if (ctl->argv_position < ctl->nfiles && !ctl->no_tty)
+		left = command(ctl, ctl->fnames[ctl->argv_position], f);
 	if (left != 0) {
-		if ((ctl->noscroll || ctl->clearfirst)
+		if ((ctl->noscroll_opt || ctl->clearfirst)
 		    && (ctl->file_size != LONG_MAX)) {
-			if (ctl->clreol)
-				my_putstring(ctl->Home);
+			if (ctl->clreol_opt)
+				my_putstring(ctl->go_home);
 			else
-				doclear(ctl);
+				clear_tty(ctl);
 		}
-		if (ctl->prnames) {
+		if (ctl->print_names) {
 			if (ctl->bad_so)
-				erasep(ctl, 0);
-			if (ctl->clreol)
+				erase_prompt(ctl, 0);
+			if (ctl->clreol_opt)
 				my_putstring(ctl->eraseln);
 			fputs("::::::::::::::", stdout);
 			if (ctl->promptlen > 14)
-				erasep(ctl, 14);
+				erase_prompt(ctl, 14);
 			putchar('\n');
-			if (ctl->clreol)
+			if (ctl->clreol_opt)
 				my_putstring(ctl->eraseln);
-			puts(ctl->fnames[ctl->fnum]);
-			if (ctl->clreol)
+			puts(ctl->fnames[ctl->argv_position]);
+			if (ctl->clreol_opt)
 				my_putstring(ctl->eraseln);
 			puts("::::::::::::::");
-			if (left > ctl->Lpp - 4)
-				left = ctl->Lpp - 4;
+			if (left > ctl->lines_per_page - 4)
+				left = ctl->lines_per_page - 4;
 		}
 		if (ctl->no_tty)
 			copy_file(f);
@@ -1936,8 +1936,8 @@ static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int l
 	}
 	fflush(stdout);
 	fclose(f);
-	ctl->screen_start.line = ctl->screen_start.chrctr = 0L;
-	ctl->context.line = ctl->context.chrctr = 0L;
+	ctl->screen_start.line = ctl->screen_start.nrow = 0L;
+	ctl->context.line = ctl->context.nrow = 0L;
 }
 
 int main(int argc, char **argv)
@@ -1949,14 +1949,14 @@ int main(int argc, char **argv)
 	char *initbuf = NULL;
 	sigset_t sigset;
 	struct more_control ctl = {
-		.firstf = 1,
+		.first_file = 1,
 		.fold_opt = 1,
 		.notell = 1,
 		.stop_opt = 1,
 		.ul_opt = 1,
-		.Wrap = 1,
-		.Lpp = LINES_PER_PAGE,
-		.Mcol = NUM_COLUMNS,
+		.wrap_margin = 1,
+		.lines_per_page = LINES_PER_PAGE,
+		.ncolumns = NUM_COLUMNS,
 		.nscroll = SCROLL_LEN,
 		0
 	};
@@ -1973,11 +1973,11 @@ int main(int argc, char **argv)
 
 	/* Auto set no scroll on when binary is called page */
 	if (!(strcmp(program_invocation_short_name, "page")))
-		ctl.noscroll = 1;
+		ctl.noscroll_opt = 1;
 
 	prepare_line_buffer(&ctl);
 
-	ctl.nscroll = ctl.Lpp / 2 - 1;
+	ctl.nscroll = ctl.lines_per_page / 2 - 1;
 	if (ctl.nscroll <= 0)
 		ctl.nscroll = 1;
 
@@ -1990,7 +1990,7 @@ int main(int argc, char **argv)
 		} else if (c == '+') {
 			s = *ctl.fnames;
 			if (*++s == '/') {
-				ctl.srchopt = 1;
+				ctl.search_opt = 1;
 				initbuf = xstrdup(s + 1);
 			} else {
 				ctl.jumpopt = 1;
@@ -2003,21 +2003,21 @@ int main(int argc, char **argv)
 		} else
 			break;
 	}
-	/* allow clreol only if Home and eraseln and EodClr strings are
+	/* allow clreol only if go_home and eraseln and end_clear strings are
 	 * defined, and in that case, make sure we are in noscroll mode */
-	if (ctl.clreol) {
-		if ((ctl.Home == NULL) || (*ctl.Home == '\0') ||
+	if (ctl.clreol_opt) {
+		if ((ctl.go_home == NULL) || (*ctl.go_home == '\0') ||
 		    (ctl.eraseln == NULL) || (*ctl.eraseln == '\0') ||
-		    (ctl.EodClr == NULL) || (*ctl.EodClr == '\0'))
-			ctl.clreol = 0;
+		    (ctl.end_clear == NULL) || (*ctl.end_clear == '\0'))
+			ctl.clreol_opt = 0;
 		else
-			ctl.noscroll = 1;
+			ctl.noscroll_opt = 1;
 	}
-	if (ctl.dlines == 0)
-		ctl.dlines = ctl.Lpp - 1;
-	left = ctl.dlines;
+	if (ctl.lines_per_screen == 0)
+		ctl.lines_per_screen = ctl.lines_per_page - 1;
+	left = ctl.lines_per_screen;
 	if (ctl.nfiles > 1)
-		ctl.prnames = 1;
+		ctl.print_names = 1;
 	if (!ctl.no_intty && ctl.nfiles == 0)
 		usage(stderr);
 	if (!ctl.no_tty)
@@ -2038,20 +2038,20 @@ int main(int argc, char **argv)
 			f = stdin;
 			display_file(&ctl, f, initbuf, left);
 		}
-		ctl.firstf = 0;
+		ctl.first_file = 0;
 		ctl.no_intty = 0;
-		ctl.prnames = 1;
+		ctl.print_names = 1;
 	}
 
-	for (/* nothing */; ctl.fnum < ctl.nfiles; ctl.fnum++) {
-		if ((f = checkf(&ctl, ctl.fnames[ctl.fnum])) == NULL)
+	for (/* nothing */; ctl.argv_position < ctl.nfiles; ctl.argv_position++) {
+		if ((f = more_fopen(&ctl, ctl.fnames[ctl.argv_position])) == NULL)
 			continue;
 		display_file(&ctl, f, initbuf, left);
-		ctl.firstf = 0;
+		ctl.first_file = 0;
 	}
 	free(ctl.previousre);
 	free(initbuf);
-	free(ctl.Line);
+	free(ctl.linebuf);
 	reset_tty(&ctl);
 	return EXIT_SUCCESS;
 }
Signature · 1 line
2.3.0
#28 lore raw

PATCH 24/31 more: add -i ignore-case posix compliance option

In reply to Sami Kerola

Reference: http://pubs.opengroup.org/onlinepubs/7908799/xcu/more.html

---
 text-utils/more.1 |  3 +++
 text-utils/more.c | 10 +++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
text-utils/more.1 +3 -0
index af1fdd4..e109e18 100644
@@ -85,6 +85,9 @@ Squeeze multiple blank lines into one.
 .B \-u
 Suppress underlining.
 .TP
+.B \-i
+Ignore case distinctions in the pattern searches.
+.TP
 .BI \- number
 The screen size to use, in
 .I number
text-utils/more.c +9 -1
index 15fd3a0..d83d5e3 100644
@@ -163,6 +163,7 @@ struct more_control {
 		fold_opt:1,		/* fold long lines */
 		hard_term:1,		/* is this hard copy terminal */
 		hardtabs:1,		/* print spaces instead of '\t' */
+		ignore_case_opt:1,	/* case insensitive pattern search */
 		jumpopt:1,		/* is jumpline defined */
 		no_bell:1,		/* suppress bell */
 		no_intty:1,		/* is input in interactive mode */
@@ -200,6 +201,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	fputs(_(" -p          do not scroll, clean screen and display text\n"), out);
 	fputs(_(" -s          squeeze multiple blank lines into one\n"), out);
 	fputs(_(" -u          suppress underlining\n"), out);
+	fputs(_(" -i          ignore case when searching\n"), out);
 	fputs(_(" -<number>   the number of lines per screenful\n"), out);
 	fputs(_(" +<number>   display file beginning from line number\n"), out);
 	fputs(_(" +/<string>  display file beginning from search string match\n"), out);
@@ -251,6 +253,9 @@ static void argscan(struct more_control *ctl, char *s)
 		case 'u':
 			ctl->ul_opt = 0;
 			break;
+		case 'i':
+			ctl->ignore_case_opt = 1;
+			break;
 		case '-':
 		case ' ':
 		case '\t':
@@ -1252,13 +1257,16 @@ static void search(struct more_control *ctl, char buf[], FILE *file, int n)
 	long line3;
 	int lncount = 0;
 	int saveln, rc;
+	int flags = REG_NOSUB;
 	regex_t re;
 
 	ctl->context.line = saveln = ctl->current_line;
 	ctl->context.nrow = startline;
+	if (ctl->ignore_case_opt)
+		flags |= REG_ICASE;
 	if (!buf)
 		goto notfound;
-	if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
+	if ((rc = regcomp(&re, buf, flags)) != 0) {
 		char s[REGERR_BUF];
 		regerror(rc, &re, s, sizeof s);
 		more_error(ctl, s);
Signature · 1 line
2.3.0
#29 lore raw

PATCH 25/31 more: add -n <number> posix compliance option

In reply to Sami Kerola

The '-n <number>' is an alias of the existing '-number' functionality.

Reference: http://pubs.opengroup.org/onlinepubs/7908799/xcu/more.html

---
 text-utils/Makemodule.am | 2 +-
 text-utils/more.1        | 5 +++++
 text-utils/more.c        | 7 +++++++
 3 files changed, 13 insertions(+), 1 deletion(-)
text-utils/Makemodule.am +1 -1
index a24c10b..9688578 100644
@@ -86,7 +86,7 @@ bin_PROGRAMS += more
 dist_man_MANS += text-utils/more.1
 more_SOURCES = text-utils/more.c text-utils/more-term.h
 more_CFLAGS = $(AM_CFLAGS) $(BSD_WARN_CFLAGS)
-more_LDADD = $(LDADD)
+more_LDADD = $(LDADD) libcommon.la
 if HAVE_TINFO
 more_LDADD += $(TINFO_LIBS)
 more_LDADD += $(TINFO_CFLAGS)
text-utils/more.1 +5 -0
index e109e18..c289393 100644
@@ -93,6 +93,11 @@ The screen size to use, in
 .I number
 of lines.
 .TP
+.BI \-n " number"
+Posix compliant alias of
+.BI \- number
+option.
+.TP
 .BI + number
 Start displaying each file at line
 .IR number .
text-utils/more.c +7 -0
index d83d5e3..c5a41b8 100644
@@ -203,6 +203,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	fputs(_(" -u          suppress underlining\n"), out);
 	fputs(_(" -i          ignore case when searching\n"), out);
 	fputs(_(" -<number>   the number of lines per screenful\n"), out);
+	fputs(_(" -n <number> same as -<number>\n"), out);
 	fputs(_(" +<number>   display file beginning from line number\n"), out);
 	fputs(_(" +/<string>  display file beginning from search string match\n"), out);
 	fputs(_(" -V          display version information and exit\n"), out);
@@ -232,6 +233,12 @@ static void argscan(struct more_control *ctl, char *s)
 			}
 			ctl->lines_per_screen = ctl->lines_per_screen * 10 + *s - '0';
 			break;
+		case 'n':
+			ctl->nfiles--;
+			if (!*++ctl->fnames)
+				errx(EXIT_FAILURE, "%s -- '%c'", _("option requires an argument"), 'n');
+			ctl->lines_per_screen = strtou16_or_err(*ctl->fnames, _("argument error"));
+			break;
 		case 'd':
 			ctl->no_bell = 1;
 			break;
Signature · 1 line
2.3.0
#30 lore raw

PATCH 26/31 more: add -e posix compliance option

In reply to Sami Kerola

Reference: http://pubs.opengroup.org/onlinepubs/7908799/xcu/more.html

---
 text-utils/more.1 | 5 +++++
 text-utils/more.c | 2 ++
 2 files changed, 7 insertions(+)
text-utils/more.1 +5 -0
index c289393..e9350fa 100644
@@ -106,6 +106,11 @@ Start displaying each file at line
 The
 .I string
 to be searched in each file before starting to display it.
+.TP
+.B \-e
+Posix compliance.  This option makes more to exit immediately after
+writing the last line of the last file in the argument list, that is the
+default functionality so silently ignore the option.
 .SH COMMANDS
 Interactive commands for
 .B more
text-utils/more.c +2 -0
index c5a41b8..7915e90 100644
@@ -263,6 +263,8 @@ static void argscan(struct more_control *ctl, char *s)
 		case 'i':
 			ctl->ignore_case_opt = 1;
 			break;
+		case 'e':
+			break;
 		case '-':
 		case ' ':
 		case '\t':
Signature · 1 line
2.3.0
#31 lore raw

PATCH 27/31 more: fix compiler warnings

In reply to Sami Kerola

Later appers when HAVE_WIDECHAR is not true.

warning: switch missing default case [-Wswitch-default]
warning: 'xmbrtowc' defined but not used [-Wunused-function]

---
 text-utils/more.c | 4 ++++
 1 file changed, 4 insertions(+)
text-utils/more.c +4 -0
index 7915e90..8cbc62f 100644
@@ -306,6 +306,8 @@ static int check_magic(FILE *f, char *fs)
 			printf(_("\n******** %s: Not a text file ********\n\n"),
 			       fs);
 			return 1;
+		default:
+			break;
 		}
 	}
 	fseek(f, 0L, SEEK_SET);	/* rewind() not necessary */
@@ -592,6 +594,7 @@ static void erase_prompt(struct more_control *ctl, int col)
 	ctl->promptlen = 0;
 }
 
+#ifdef HAVE_WIDECHAR
 static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 				  mbstate_t *mbstate)
 {
@@ -600,6 +603,7 @@ static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
 		return 1;
 	return mblength;
 }
+#endif
 
 static int would_underline(char *s, int n)
 {
Signature · 1 line
2.3.0
#32 lore raw

PATCH 28/31 more: speed up command_expansion()

In reply to Sami Kerola

Simply copy the command string if there is nothing to expand.

Additionally make variable names easier to understand in command_expansion(), and remove possible buffer overflow in do_shell().

---
 text-utils/more.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
text-utils/more.c +17 -14
index 8cbc62f..84c2108 100644
@@ -971,22 +971,25 @@ static void ttyin(struct more_control *ctl, char buf[], int nmax, char pchar)
 static int command_expansion(struct more_control *ctl, char **outbuf, char *inbuf)
 {
 	char *inpstr = inbuf;
+	char *allocation;
 	char *outstr;
 	char c;
-	char *temp;
 	int changed = 0;
-	int tempsz, xtra, offset;
+	size_t xtra;
+	ptrdiff_t offset;
 
-	xtra = strlen(ctl->fnames[ctl->argv_position]) + strlen(ctl->shell_line) + 1;
-	tempsz = 200 + xtra;
-	temp = xmalloc(tempsz);
-	outstr = temp;
+	if (!strpbrk(inbuf, "%!\\")) {
+		*outbuf = xstrdup(inbuf);
+		return 0;
+	}
+	xtra = strlen(ctl->fnames[ctl->argv_position]) + strlen(ctl->shell_line) + COMMAND_BUF + 1;
+	outstr = allocation = xmalloc(xtra);
 	while ((c = *inpstr++) != '\0') {
-		offset = outstr - temp;
-		if (tempsz - offset - 1 < xtra) {
-			tempsz += 200 + xtra;
-			temp = xrealloc(temp, tempsz);
-			outstr = temp + offset;
+		offset = outstr - allocation;
+		if (xtra - offset - 1 < xtra) {
+			xtra += COMMAND_BUF;
+			allocation = xrealloc(allocation, xtra);
+			outstr = allocation + offset;
 		}
 		switch (c) {
 		case '%':
@@ -1015,7 +1018,7 @@ static int command_expansion(struct more_control *ctl, char **outbuf, char *inbu
 		}
 	}
 	*outstr++ = '\0';
-	*outbuf = temp;
+	*outbuf = allocation;
 	return changed;
 }
 
@@ -1122,8 +1125,8 @@ static void do_shell(struct more_control *ctl, char *filename)
 		ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
 		rc = command_expansion(ctl, &expanded, cmdbuf);
 		if (expanded) {
-			if (strlen(expanded) < sizeof(ctl->shell_line))
-				strcpy(ctl->shell_line, expanded);
+			if (strlen(expanded) < sizeof(ctl->shell_line) - 1)
+				xstrncpy(ctl->shell_line, expanded, sizeof ctl->shell_line);
 			else
 				rc = -1;
 			free(expanded);
Signature · 1 line
2.3.0
#33 lore raw

PATCH 29/31 more: remove unnecessary variable

In reply to Sami Kerola

No longer needed when siglongjmp() magic is gone.

---
 text-utils/more.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
text-utils/more.c +2 -7
index 84c2108..449f9cd 100644
@@ -181,7 +181,6 @@ struct more_control {
 		ul_glitch:1,		/* terminal is underlining in glitch mode */
 		ul_opt:1,		/* underline as best we can */
 		underlining:1,		/* is underlining going on */
-		within:1,		/* true if we are within a file, false if we are between files */
 		wrap_margin:1;		/* set if automargins */
 };
 
@@ -791,8 +790,7 @@ static void change_file(struct more_control *ctl, int nskip)
 	if (nskip > 0) {
 		if (ctl->argv_position + nskip > ctl->nfiles - 1)
 			nskip = ctl->nfiles - ctl->argv_position - 1;
-	} else if (ctl->within)
-		ctl->argv_position++;
+	}
 	ctl->argv_position += nskip;
 	if (ctl->argv_position < 0)
 		ctl->argv_position = 0;
@@ -1952,11 +1950,8 @@ static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int l
 		}
 		if (ctl->no_tty)
 			copy_file(f);
-		else {
-			ctl->within = 1;
+		else
 			screen(ctl, f, left);
-			ctl->within = 0;
-		}
 	}
 	fflush(stdout);
 	fclose(f);
Signature · 1 line
2.3.0
#34 lore raw

PATCH 30/31 more: remove TIOCGWINSZ preprocessor #ifdef checks

In reply to Sami Kerola

The change_window_sz(), that used to be chgwinsz(), has done assumption TIOCGWINSZ without check for long time. It is time to remove the rest of the #ifdefs.

---
 text-utils/more.c | 6 ------
 1 file changed, 6 deletions(-)
text-utils/more.c +0 -6
index 449f9cd..f349109 100644
@@ -1793,9 +1793,7 @@ static void initterm(struct more_control *ctl)
 {
 	int ret, tmp;
 	char *term;
-#ifdef TIOCGWINSZ
 	struct winsize win;
-#endif
 
 #ifndef NON_INTERACTIVE_MORE
 	ctl->no_tty = tcgetattr(STDOUT_FILENO, &ctl->otty);
@@ -1812,19 +1810,15 @@ static void initterm(struct more_control *ctl)
 			ctl->dumb = 1;
 			ctl->ul_opt = 0;
 		} else {
-#ifdef TIOCGWINSZ
 			if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
-#endif
 				ctl->lines_per_page = my_tgetnum(TERM_LINES);
 				ctl->ncolumns = my_tgetnum(TERM_COLS);
-#ifdef TIOCGWINSZ
 			} else {
 				if ((ctl->lines_per_page = win.ws_row) == 0)
 					ctl->lines_per_page = my_tgetnum(TERM_LINES);
 				if ((ctl->ncolumns = win.ws_col) == 0)
 					ctl->ncolumns = my_tgetnum(TERM_COLS);
 			}
-#endif
 			if ((ctl->lines_per_page <= 0) || my_tgetflag(TERM_HARD_COPY)) {
 				ctl->hard_term = 1;
 				ctl->lines_per_page = LINES_PER_PAGE;
Signature · 1 line
2.3.0
#35 lore raw

PATCH 31/31 more: simplify initterm()

In reply to Sami Kerola

If cases without else's does not need nesting.

---
 text-utils/more.c | 167 +++++++++++++++++++++++++-----------------------------
 1 file changed, 76 insertions(+), 91 deletions(-)
text-utils/more.c +76 -91
index f349109..0ab4ade 100644
@@ -1791,103 +1791,13 @@ static void copy_file(FILE *f)
 
 static void initterm(struct more_control *ctl)
 {
-	int ret, tmp;
+	int ret;
 	char *term;
 	struct winsize win;
 
 #ifndef NON_INTERACTIVE_MORE
 	ctl->no_tty = tcgetattr(STDOUT_FILENO, &ctl->otty);
 #endif
-	if (!ctl->no_tty) {
-		ctl->docrterase = (ctl->otty.c_cc[VERASE] != 255);
-		ctl->docrtkill = (ctl->otty.c_cc[VKILL] != 255);
-		if ((term = getenv("TERM")) == NULL) {
-			ctl->dumb = 1;
-			ctl->ul_opt = 0;
-		}
-		my_setupterm(term, 1, &ret);
-		if (ret <= 0) {
-			ctl->dumb = 1;
-			ctl->ul_opt = 0;
-		} else {
-			if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
-				ctl->lines_per_page = my_tgetnum(TERM_LINES);
-				ctl->ncolumns = my_tgetnum(TERM_COLS);
-			} else {
-				if ((ctl->lines_per_page = win.ws_row) == 0)
-					ctl->lines_per_page = my_tgetnum(TERM_LINES);
-				if ((ctl->ncolumns = win.ws_col) == 0)
-					ctl->ncolumns = my_tgetnum(TERM_COLS);
-			}
-			if ((ctl->lines_per_page <= 0) || my_tgetflag(TERM_HARD_COPY)) {
-				ctl->hard_term = 1;
-				ctl->lines_per_page = LINES_PER_PAGE;
-			}
-
-			if (my_tgetflag(TERM_EAT_NEW_LINE))
-				/* Eat newline at last column + 1; dec, concept */
-				ctl->eatnl = 1;
-			if (ctl->ncolumns <= 0)
-				ctl->ncolumns = NUM_COLUMNS;
-
-			ctl->wrap_margin = my_tgetflag(TERM_AUTO_RIGHT_MARGIN);
-			ctl->bad_so = my_tgetflag(TERM_CEOL);
-			ctl->eraseln = my_tgetstr(TERM_CLEAR_TO_LINE_END);
-			ctl->clear = my_tgetstr(TERM_CLEAR);
-			ctl->std_enter = my_tgetstr(TERM_STANDARD_MODE);
-			ctl->std_exit = my_tgetstr(TERM_EXIT_STANDARD_MODE);
-			tmp = my_tgetnum(TERM_STD_MODE_GLITCH);
-			if (tmp < 0)
-				ctl->stdout_glitch = 0;
-			else if (0 < tmp)
-				ctl->stdout_glitch = 1;
-
-			/* Set up for underlining:  some terminals don't
-			 * need it; others have start/stop sequences,
-			 * still others have an underline char sequence
-			 * which is assumed to move the cursor forward
-			 * one character.  If underline sequence isn't
-			 * available, settle for standout sequence. */
-			if (my_tgetflag(TERM_UNDERLINE)
-			    || my_tgetflag(TERM_OVER_STRIKE))
-				ctl->ul_opt = 0;
-			if ((ctl->underlining_char = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
-				ctl->underlining_char = "";
-			if (((ctl->underline_enter =
-			      my_tgetstr(TERM_ENTER_UNDERLINE)) == NULL
-			     || (ctl->underline_exit =
-				 my_tgetstr(TERM_EXIT_UNDERLINE)) == NULL)
-			    && !*ctl->underlining_char) {
-				if ((ctl->underline_enter = ctl->std_enter) == NULL
-				    || (ctl->underline_exit = ctl->std_exit) == NULL) {
-					ctl->underline_enter = "";
-					ctl->underline_exit = "";
-				} else
-					ctl->ul_glitch = ctl->stdout_glitch;
-			} else {
-				ctl->ul_glitch = 0;
-			}
-
-			ctl->go_home = my_tgetstr(TERM_HOME);
-			if (ctl->go_home == NULL || *ctl->go_home == '\0') {
-				if ((ctl->cursorm =
-				     my_tgetstr(TERM_CURSOR_ADDRESS)) != NULL) {
-					const char *t =
-					    (const char *)my_tgoto(ctl->cursorm, 0,
-								   0);
-					xstrncpy(ctl->cursorhome, t,
-						 sizeof(ctl->cursorhome));
-					ctl->go_home = ctl->cursorhome;
-				}
-			}
-			ctl->end_clear = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
-			if ((ctl->backspace_char = my_tgetstr(TERM_LINE_DOWN)) == NULL)
-				ctl->backspace_char = "\b";
-
-		}
-		if ((ctl->shell = getenv("SHELL")) == NULL)
-			ctl->shell = _PATH_BSHELL;
-	}
 	ctl->no_intty = tcgetattr(STDIN_FILENO, &ctl->otty);
 	tcgetattr(STDERR_FILENO, &ctl->otty);
 	ctl->orig_tty = ctl->otty;
@@ -1897,6 +1807,81 @@ static void initterm(struct more_control *ctl)
 		ctl->otty.c_cc[VMIN] = 1;
 		ctl->otty.c_cc[VTIME] = 0;
 	}
+	if (ctl->no_tty)
+		return;
+	ctl->docrterase = (ctl->otty.c_cc[VERASE] != 255);
+	ctl->docrtkill = (ctl->otty.c_cc[VKILL] != 255);
+	if ((term = getenv("TERM")) == NULL) {
+		ctl->dumb = 1;
+		ctl->ul_opt = 0;
+	}
+	if ((ctl->shell = getenv("SHELL")) == NULL)
+		ctl->shell = _PATH_BSHELL;
+	my_setupterm(term, 1, &ret);
+	if (ret < 1) {
+		ctl->dumb = 1;
+		ctl->ul_opt = 0;
+		return;
+	}
+	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) < 0) {
+		ctl->lines_per_page = my_tgetnum(TERM_LINES);
+		ctl->ncolumns = my_tgetnum(TERM_COLS);
+	} else {
+		if ((ctl->lines_per_page = win.ws_row) == 0)
+			ctl->lines_per_page = my_tgetnum(TERM_LINES);
+		if ((ctl->ncolumns = win.ws_col) == 0)
+			ctl->ncolumns = my_tgetnum(TERM_COLS);
+	}
+	if ((ctl->lines_per_page < 1) || my_tgetflag(TERM_HARD_COPY)) {
+		ctl->hard_term = 1;
+		ctl->lines_per_page = LINES_PER_PAGE;
+	}
+	if (ctl->ncolumns < 1)
+		ctl->ncolumns = NUM_COLUMNS;
+	if (my_tgetflag(TERM_EAT_NEW_LINE))
+		/* Eat newline at last column + 1; dec, concept */
+		ctl->eatnl = 1;
+	ctl->wrap_margin = my_tgetflag(TERM_AUTO_RIGHT_MARGIN);
+	ctl->bad_so = my_tgetflag(TERM_CEOL);
+	ctl->eraseln = my_tgetstr(TERM_CLEAR_TO_LINE_END);
+	ctl->clear = my_tgetstr(TERM_CLEAR);
+	ctl->std_enter = my_tgetstr(TERM_STANDARD_MODE);
+	ctl->std_exit = my_tgetstr(TERM_EXIT_STANDARD_MODE);
+	if (0 < my_tgetnum(TERM_STD_MODE_GLITCH))
+		ctl->stdout_glitch = 1;
+	/* Set up for underlining:  some terminals don't need it; others have
+	 * start/stop sequences, still others have an underline char sequence
+	 * which is assumed to move the cursor forward one character.  If
+	 * underline sequence isn't available, settle for standout sequence.
+	 */
+	if (my_tgetflag(TERM_UNDERLINE)
+	    || my_tgetflag(TERM_OVER_STRIKE))
+		ctl->ul_opt = 0;
+	if ((ctl->underlining_char = my_tgetstr(TERM_UNDERLINE_CHAR)) == NULL)
+		ctl->underlining_char = "";
+	if (((ctl->underline_enter = my_tgetstr(TERM_ENTER_UNDERLINE)) == NULL
+	     || (ctl->underline_exit = my_tgetstr(TERM_EXIT_UNDERLINE)) == NULL)
+	    && !*ctl->underlining_char) {
+		if ((ctl->underline_enter = ctl->std_enter) == NULL
+		    || (ctl->underline_exit = ctl->std_exit) == NULL) {
+			ctl->underline_enter = "";
+			ctl->underline_exit = "";
+		} else
+			ctl->ul_glitch = ctl->stdout_glitch;
+	} else {
+		ctl->ul_glitch = 0;
+	}
+	ctl->go_home = my_tgetstr(TERM_HOME);
+	if (ctl->go_home == NULL || *ctl->go_home == '\0') {
+		if ((ctl->cursorm = my_tgetstr(TERM_CURSOR_ADDRESS)) != NULL) {
+			const char *t = my_tgoto(ctl->cursorm, 0, 0);
+			xstrncpy(ctl->cursorhome, t, sizeof(ctl->cursorhome));
+			ctl->go_home = ctl->cursorhome;
+		}
+	}
+	ctl->end_clear = my_tgetstr(TERM_CLEAR_TO_SCREEN_END);
+	if ((ctl->backspace_char = my_tgetstr(TERM_LINE_DOWN)) == NULL)
+		ctl->backspace_char = "\b";
 }
 
 static void display_file(struct more_control *ctl, FILE *f, char *initbuf, int left)
Signature · 1 line
2.3.0