[patch] touchup and minor shrinkage of less

Bernhard Fischer rep.nop at aon.at
Wed Nov 22 06:37:44 PST 2006


Hi,

I did not test the individual features of less if any of them get
pessimized by these patches. Overall, it saves some bytes.

Especially the 01f patch would save alot, if applied throughout the
tree, but each individual place would have to be checked if it is really
benefical.

Apply if you like or let me know if i should apply all of them or some
parts.

cheers,
PS: sizes are in less.txt.diff
-------------- next part --------------
01a: svn version
01b: merge past_eof and inp_stdin into flags.
01c: merge match_backwards into flags.
01d: add handling of <HOME> and <END> keys, remove set_tty_raw, use fflush_stdout_and_exit
01e: rewrite special-key handling to be smaller.
01f: changing the format saves 4 bytes (alot of places would benefit from this)
   text    data     bss     dec     hex filename
   9034       8     648    9690    25da less.o.01a
   9023       8     640    9671    25c7 less.o.01b
   9027       8     636    9671    25c7 less.o.01c
   9068       8     636    9712    25f0 less.o.01d
   8990       8     636    9634    25a2 less.o.01e
   8986       8     636    9630    259e less.o.01f

-------------- next part --------------
--- less.c.01a	2006-11-06 10:13:09.000000000 +0100
+++ less.c	2006-11-22 11:08:26.000000000 +0100
@@ -73,7 +73,6 @@
 static int line_pos;
 static int num_flines;
 static int num_files = 1;
-static int past_eof;
 
 /* Command line options */
 static unsigned flags;
@@ -82,10 +81,12 @@
 #define FLAG_m (1<<2)
 #define FLAG_N (1<<3)
 #define FLAG_TILDE (1<<4)
+/* hijack command line options variable for internal state vars */
 
-/* This is needed so that program behaviour changes when input comes from
-   stdin */
-static int inp_stdin;
+#define LESS_STATE_INP_STDIN (1<<5)
+#define LESS_STATE_PAST_EOF  (1<<6)
+#define LESS_STATE_MATCH_BACKWARDS (1<<7)
+/* INP_STDIN is used to change behaviour when input comes from stdin */
 
 #ifdef CONFIG_FEATURE_LESS_MARKS
 static int mark_lines[15][2];
@@ -200,7 +201,7 @@
 	char current_line[256];
 	FILE *fp;
 
-	fp = (inp_stdin) ? stdin : xfopen(filename, "r");
+	fp = (flags & LESS_STATE_INP_STDIN) ? stdin : xfopen(filename, "r");
 	flines = NULL;
 	for (i = 0; (feof(fp)==0) && (i <= MAXLINES); i++) {
 		strcpy(current_line, "");
@@ -215,12 +216,12 @@
 	/* Reset variables for a new file */
 
 	line_pos = 0;
-	past_eof = 0;
+	flags &= ~LESS_STATE_PAST_EOF;
 
 	fclose(fp);
 
 	if (inp == NULL)
-		inp = (inp_stdin) ? xfopen(CURRENT_TTY, "r") : stdin;
+		inp = (flags & LESS_STATE_INP_STDIN) ? xfopen(CURRENT_TTY, "r") : stdin;
 
 	if (flags & FLAG_N)
 		add_linenumbers();
@@ -240,7 +241,7 @@
 {
 	int percentage;
 
-	if (!past_eof) {
+	if (!(flags & LESS_STATE_PAST_EOF)) {
 		if (!line_pos) {
 			if (num_files > 1)
 				printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT,
@@ -372,7 +373,7 @@
 {
 	int i;
 
-	if (!past_eof) {
+	if (!(flags & LESS_STATE_PAST_EOF)) {
 		if (line_pos + (height - 3) + nlines < num_flines) {
 			line_pos += nlines;
 			for (i = 0; i < (height - 1); i++) {
@@ -403,7 +404,7 @@
 	int i;
 	int tilde_line;
 
-	if (!past_eof) {
+	if (!(flags & LESS_STATE_PAST_EOF)) {
 		if (line_pos - nlines >= 0) {
 			line_pos -= nlines;
 			for (i = 0; i < (height - 1); i++) {
@@ -431,7 +432,7 @@
 		/* Going backwards nlines lines has taken us to a point where
 		   nothing is past the EOF, so we revert to normal. */
 		if (line_pos < num_flines - height + 3) {
-			past_eof = 0;
+			flags &= ~LESS_STATE_PAST_EOF;
 			buffer_up(nlines);
 		}
 		else {
@@ -453,7 +454,7 @@
 static void buffer_line(int linenum)
 {
 	int i;
-	past_eof = 0;
+	flags &= ~LESS_STATE_PAST_EOF;
 
 	if (linenum < 0 || linenum > num_flines) {
 		clear_line();
@@ -477,7 +478,7 @@
 		}
 		line_pos = linenum;
 		/* Set past_eof so buffer_down and buffer_up act differently */
-		past_eof = 1;
+		flags |= LESS_STATE_PAST_EOF;
 		buffer_print();
 	}
 }
@@ -513,7 +514,7 @@
 	current_file = num_files + 1;
 	num_files++;
 
-	inp_stdin = 0;
+	flags &= ~LESS_STATE_INP_STDIN;
 	reinitialise();
 }
 
@@ -1055,7 +1056,7 @@
 			full_repaint();
 			break;
 		case 's':
-			if (inp_stdin)
+			if (flags & LESS_STATE_INP_STDIN)
 				save_input_to_file();
 			break;
 		case 'E':
@@ -1124,14 +1125,14 @@
 
 	if (!num_files) {
 		if (ttyname(STDIN_FILENO) == NULL)
-			inp_stdin = 1;
+			flags |= LESS_STATE_INP_STDIN;
 		else {
 			bb_error_msg("missing filename");
 			bb_show_usage();
 		}
 	}
 
-	strcpy(filename, (inp_stdin) ? bb_msg_standard_input : files[0]);
+	strcpy(filename, (flags & LESS_STATE_INP_STDIN) ? bb_msg_standard_input : files[0]);
 	get_terminal_width_height(0, &width, &height);
 	data_readlines();
 	tcgetattr(fileno(inp), &term_orig);
-------------- next part --------------
--- less.c.01b	2006-11-22 11:08:26.000000000 +0100
+++ less.c	2006-11-22 11:10:55.000000000 +0100
@@ -82,7 +82,6 @@
 #define FLAG_N (1<<3)
 #define FLAG_TILDE (1<<4)
 /* hijack command line options variable for internal state vars */
-
 #define LESS_STATE_INP_STDIN (1<<5)
 #define LESS_STATE_PAST_EOF  (1<<6)
 #define LESS_STATE_MATCH_BACKWARDS (1<<7)
@@ -98,7 +97,6 @@
 static int *match_lines;
 static int match_pos;
 static int num_matches;
-static int match_backwards;
 static regex_t old_pattern;
 #endif
 
@@ -667,13 +665,14 @@
 	regex_t pattern;
 	/* Get the uncompiled regular expression from the user */
 	clear_line();
-	putchar((match_backwards) ? '?' : '/');
+	putchar((flags & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
 	uncomp_regex[0] = 0;
 	fgets(uncomp_regex, sizeof(uncomp_regex), inp);
 
 	if (strlen(uncomp_regex) == 1) {
 		if (num_matches)
-			goto_match(match_backwards ? match_pos - 1 : match_pos + 1);
+			goto_match((flags & LESS_STATE_MATCH_BACKWARDS)
+						? match_pos - 1 : match_pos + 1);
 		else
 			buffer_print();
 		return;
@@ -711,7 +710,7 @@
 
 	num_matches = j;
 	if ((match_lines[0] != -1) && (num_flines > height - 2)) {
-		if (match_backwards) {
+		if (flags & LESS_STATE_MATCH_BACKWARDS) {
 			for (i = 0; i < num_matches; i++) {
 				if (match_lines[i] > line_pos) {
 					match_pos = i - 1;
@@ -777,11 +776,11 @@
 			goto_match(match_pos + num);
 			break;
 		case '/':
-			match_backwards = 0;
+			flags &= ~LESS_STATE_MATCH_BACKWARDS;
 			regex_process();
 			break;
 		case '?':
-			match_backwards = 1;
+			flags |= LESS_STATE_MATCH_BACKWARDS;
 			regex_process();
 			break;
 #endif
@@ -1070,7 +1069,7 @@
 #endif
 #ifdef CONFIG_FEATURE_LESS_REGEXP
 		case '/':
-			match_backwards = 0;
+			flags &= ~LESS_STATE_MATCH_BACKWARDS;
 			regex_process();
 			break;
 		case 'n':
@@ -1080,7 +1079,7 @@
 			goto_match(match_pos - 1);
 			break;
 		case '?':
-			match_backwards = 1;
+			flags |= LESS_STATE_MATCH_BACKWARDS;
 			regex_process();
 			break;
 #endif
-------------- next part --------------
--- less.c.01c	2006-11-22 11:10:55.000000000 +0100
+++ less.c	2006-11-22 11:59:33.000000000 +0100
@@ -44,6 +44,8 @@
 #define REAL_KEY_LEFT 'D'
 #define REAL_PAGE_UP '5'
 #define REAL_PAGE_DOWN '6'
+#define REAL_KEY_HOME '7'
+#define REAL_KEY_END '8'
 
 /* These are the special codes assigned by this program to the special keys */
 #define PAGE_UP 20
@@ -52,6 +54,8 @@
 #define KEY_DOWN 23
 #define KEY_RIGHT 24
 #define KEY_LEFT 25
+#define KEY_HOME 26
+#define KEY_END 27
 
 /* The escape codes for highlighted and normal text */
 #define HIGHLIGHT "\033[7m"
@@ -113,12 +117,6 @@
 	tcsetattr(fileno(inp), TCSANOW, &term_orig);
 }
 
-/* Set terminal input to raw mode  (taken from vi.c) */
-static void set_tty_raw(void)
-{
-	tcsetattr(fileno(inp), TCSANOW, &term_vi);
-}
-
 /* Exit the program gracefully */
 static void tless_exit(int code)
 {
@@ -128,7 +126,7 @@
 		 only termios.h? */
 
 	putchar('\n');
-	exit(code);
+	fflush_stdout_and_exit(code);
 }
 
 /* Grab a character from input without requiring the return key. If the
@@ -137,8 +135,8 @@
 static int tless_getch(void)
 {
 	int input;
-
-	set_tty_raw();
+	/* Set terminal input to raw mode  (taken from vi.c) */
+	tcsetattr(fileno(inp), TCSANOW, &term_vi);
 
 	input = getc(inp);
 	/* Detect escape sequences (i.e. arrow keys) and handle
@@ -159,6 +157,10 @@
 			return PAGE_UP;
 		else if (input == REAL_PAGE_DOWN)
 			return PAGE_DOWN;
+		else if (input == REAL_KEY_HOME)
+			return KEY_HOME;
+		else if (input == REAL_KEY_END)
+			return KEY_END;
 	}
 	/* The input is a normal ASCII value */
 	else {
@@ -1029,10 +1031,10 @@
 			buffer_up((height - 1) / 2);
 			buffer_print();
 			break;
-		case 'g': case 'p': case '<': case '%':
+		case KEY_HOME: case 'g': case 'p': case '<': case '%':
 			buffer_line(0);
 			break;
-		case 'G': case '>':
+		case KEY_END: case 'G': case '>':
 			buffer_line(num_flines - height + 2);
 			break;
 		case 'q': case 'Q':
-------------- next part --------------
--- less.c.01d	2006-11-22 11:59:33.000000000 +0100
+++ less.c	2006-11-22 12:26:16.000000000 +0100
@@ -48,12 +48,12 @@
 #define REAL_KEY_END '8'
 
 /* These are the special codes assigned by this program to the special keys */
-#define PAGE_UP 20
-#define PAGE_DOWN 21
-#define KEY_UP 22
-#define KEY_DOWN 23
-#define KEY_RIGHT 24
-#define KEY_LEFT 25
+#define KEY_UP 20
+#define KEY_DOWN 21
+#define KEY_RIGHT 22
+#define KEY_LEFT 23
+#define PAGE_UP 24
+#define PAGE_DOWN 25
 #define KEY_HOME 26
 #define KEY_END 27
 
@@ -143,24 +143,15 @@
 	   them accordingly */
 
 	if (input == '\033' && getc(inp) == '[') {
+		unsigned int i;
 		input = getc(inp);
 		set_tty_cooked();
-		if (input == REAL_KEY_UP)
-			return KEY_UP;
-		else if (input == REAL_KEY_DOWN)
-			return KEY_DOWN;
-		else if (input == REAL_KEY_RIGHT)
-			return KEY_RIGHT;
-		else if (input == REAL_KEY_LEFT)
-			return KEY_LEFT;
-		else if (input == REAL_PAGE_UP)
-			return PAGE_UP;
-		else if (input == REAL_PAGE_DOWN)
-			return PAGE_DOWN;
-		else if (input == REAL_KEY_HOME)
-			return KEY_HOME;
-		else if (input == REAL_KEY_END)
-			return KEY_END;
+
+		i = input - REAL_KEY_UP;
+		if (i < 4)
+			return 20 + i;
+		else if ((i = input - REAL_PAGE_UP) < 4)
+			return 24 + i;
 	}
 	/* The input is a normal ASCII value */
 	else {
-------------- next part --------------
--- less.c.01e	2006-11-22 12:26:16.000000000 +0100
+++ less.c	2006-11-22 14:39:36.000000000 +0100
@@ -871,7 +871,7 @@
 		buffer_print();
 	}
 	else
-		printf("%sNo log file%s", HIGHLIGHT, NORMAL);
+		printf("%s%s%s", HIGHLIGHT, "No log file", NORMAL);
 }
 
 #ifdef CONFIG_FEATURE_LESS_MARKS


More information about the busybox mailing list