svn commit: trunk/busybox/miscutils

vda at busybox.net vda at busybox.net
Thu Dec 21 17:03:22 UTC 2006


Author: vda
Date: 2006-12-21 09:03:20 -0800 (Thu, 21 Dec 2006)
New Revision: 17034

Log:
less: a few fixes for cases where we overflow status line


Modified:
   trunk/busybox/miscutils/less.c


Changeset:
Modified: trunk/busybox/miscutils/less.c
===================================================================
--- trunk/busybox/miscutils/less.c	2006-12-21 15:23:45 UTC (rev 17033)
+++ trunk/busybox/miscutils/less.c	2006-12-21 17:03:20 UTC (rev 17034)
@@ -148,7 +148,7 @@
 	return input;
 }
 
-static char* tless_gets(void)
+static char* tless_gets(int sz)
 {
 	int c;
 	int i = 0;
@@ -164,11 +164,12 @@
 		}
 		if (c < ' ')
 			continue;
+		if (i >= width - sz - 1)
+			continue; /* len limit */
 		putchar(c);
 		result[i++] = c;
 		result = xrealloc(result, i+1);
 		result[i] = '\0';
-		if (i >= width-1) return result; 
 	}
 }
 
@@ -189,6 +190,12 @@
 	printf(HIGHLIGHT"%s"NORMAL, str);
 }
 
+static void print_statusline(const char *str)
+{
+	clear_line();
+	printf(HIGHLIGHT"%.*s"NORMAL, width-1, str);
+}
+
 static void data_readlines(void)
 {
 	unsigned i, pos;
@@ -277,6 +284,7 @@
 {
 	int percentage;
 
+	clear_line();
 	printf(HIGHLIGHT"%s", filename);
 	if (num_files > 1)
 		printf(" (file %i of %i)", current_file, num_files);
@@ -293,24 +301,8 @@
 	printf("%i%% "NORMAL, percentage);
 }
 
-#if 0
-/* Print a status line if -m was specified */
-static void medium_status_print(void)
-{
-	int percentage;
-
-	percentage = calc_percent();
-	if (!line_pos)
-		printf(HIGHLIGHT"%s %i%% "NORMAL, filename, percentage);
-	else if (line_pos >= num_flines - height + 2)
-		print_hilite("(END)");
-	else
-		printf(HIGHLIGHT"%i%% "NORMAL, percentage);
-}
 #endif
 
-#endif
-
 /* Print the status line */
 static void status_print(void)
 {
@@ -322,12 +314,10 @@
 		m_status_print();
 		return;
 	}
-	//if (option_mask32 & FLAG_m) {
-	//	medium_status_print();
-	//	return;
-	//}
 	/* No flags set */
 #endif
+
+	clear_line();
 	if (line_pos && line_pos < num_flines - height + 2) {
 		putchar(':');
 		return;
@@ -458,7 +448,6 @@
 			print_found(buffer[i]);
 		else
 			print_ascii(buffer[i]);
-	fputs(CLEAR_2_EOL, stdout); /* clears status line */
 	status_print();
 }
 
@@ -557,10 +546,9 @@
 
 static void examine_file(void)
 {
-	clear_line();
-	printf("Examine: ");
+	print_statusline("Examine: ");
 	free(filename);
-	filename = tless_gets();
+	filename = tless_gets(sizeof("Examine: ")-1);
 	/* files start by = argv. why we assume that argv is infinitely long??
 	files[num_files] = filename;
 	current_file = num_files + 1;
@@ -583,8 +571,7 @@
 		filename = xstrdup(files[current_file - 1]);
 		reinitialise();
 	} else {
-		clear_line();
-		print_hilite(direction > 0 ? "No next file" : "No previous file");
+		print_statusline(direction > 0 ? "No next file" : "No previous file");
 	}
 }
 
@@ -613,8 +600,7 @@
 	int keypress;
 
 	/* Clear the current line and print a prompt */
-	clear_line();
-	printf(" :");
+	print_statusline(" :");
 
 	keypress = tless_getch();
 	switch (keypress) {
@@ -626,7 +612,6 @@
 			break;
 #if ENABLE_FEATURE_LESS_FLAGS
 		case 'f':
-			clear_line();
 			m_status_print();
 			break;
 #endif
@@ -681,7 +666,7 @@
 	/* Get the uncompiled regular expression from the user */
 	clear_line();
 	putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
-	uncomp_regex = tless_gets();
+	uncomp_regex = tless_gets(1);
 	if (/*!uncomp_regex ||*/ !uncomp_regex[0]) {
 		free(uncomp_regex);
 		buffer_print();
@@ -692,8 +677,7 @@
 	err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
 	free(uncomp_regex);
 	if (err) {
-		clear_line();
-		fputs(err, stdout);
+		print_statusline(err);
 		free(err);
 		return;
 	}
@@ -862,14 +846,13 @@
 	int i;
 	FILE *fp;
 
-	clear_line();
-	printf("Log file: ");
-	current_line = tless_gets();
+	print_statusline("Log file: ");
+	current_line = tless_gets(sizeof("Log file: ")-1);
 	if (strlen(current_line) > 0) {
 		fp = fopen(current_line, "w");
 		free(current_line);
 		if (!fp) {
-			print_hilite("Error opening log file");
+			print_statusline("Error opening log file");
 			return;
 		}
 		for (i = 0; i < num_flines; i++)
@@ -879,7 +862,7 @@
 		return;
 	}
 	free(current_line);
-	print_hilite("No log file");
+	print_statusline("No log file");
 }
 
 #if ENABLE_FEATURE_LESS_MARKS
@@ -887,8 +870,7 @@
 {
 	int letter;
 
-	clear_line();
-	printf("Mark: ");
+	print_statusline("Mark: ");
 	letter = tless_getch();
 
 	if (isalpha(letter)) {
@@ -901,8 +883,7 @@
 		mark_lines[num_marks][1] = line_pos;
 		num_marks++;
 	} else {
-		clear_line();
-		print_hilite("Invalid mark letter");
+		print_statusline("Invalid mark letter");
 	}
 }
 
@@ -911,8 +892,7 @@
 	int letter;
 	int i;
 
-	clear_line();
-	printf("Go to mark: ");
+	print_statusline("Go to mark: ");
 	letter = tless_getch();
 	clear_line();
 
@@ -923,9 +903,9 @@
 				break;
 			}
 		if (num_marks == 14 && letter != mark_lines[14][0])
-			print_hilite("Mark not set");
+			print_statusline("Mark not set");
 	} else
-		print_hilite("Invalid mark letter");
+		print_statusline("Invalid mark letter");
 }
 #endif
 
@@ -953,10 +933,8 @@
 	int bracket_line = -1;
 	int i;
 
-	clear_line();
-
 	if (strchr(flines[line_pos], bracket) == NULL) {
-		print_hilite("No bracket in top line");
+		print_statusline("No bracket in top line");
 		return;
 	}
 	for (i = line_pos + 1; i < num_flines; i++) {
@@ -966,7 +944,7 @@
 		}
 	}
 	if (bracket_line == -1)
-	print_hilite("No matching bracket found");
+		print_statusline("No matching bracket found");
 	buffer_line(bracket_line - height + 2);
 }
 
@@ -975,13 +953,8 @@
 	int bracket_line = -1;
 	int i;
 
-	clear_line();
-
 	if (strchr(flines[line_pos + height - 2], bracket) == NULL) {
-		print_hilite("No bracket in bottom line");
-		/* ?? */
-		/*printf("%s", flines[line_pos + height]);*/
-		/*sleep(4);*/
+		print_statusline("No bracket in bottom line");
 		return;
 	}
 
@@ -992,7 +965,7 @@
 		}
 	}
 	if (bracket_line == -1)
-		print_hilite("No matching bracket found");
+		print_statusline("No matching bracket found");
 	buffer_line(bracket_line);
 }
 
@@ -1058,7 +1031,6 @@
 			break;
 #if ENABLE_FEATURE_LESS_FLAGS
 		case '=':
-			clear_line();
 			m_status_print();
 			break;
 #endif




More information about the busybox-cvs mailing list