diff options
author | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-19 14:23:46 +0000 |
---|---|---|
committer | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-19 14:23:46 +0000 |
commit | bc37480e9cbbfd773fd8c22a82f200cc05aff873 (patch) | |
tree | e3c485412d2248a5c431c9fcd314a2d0120e0344 /miscutils/less.c | |
parent | b71e6024f9ac1bf2c8068a20c93af6a0be630a11 (diff) | |
download | busybox-w32-bc37480e9cbbfd773fd8c22a82f200cc05aff873.tar.gz busybox-w32-bc37480e9cbbfd773fd8c22a82f200cc05aff873.tar.bz2 busybox-w32-bc37480e9cbbfd773fd8c22a82f200cc05aff873.zip |
1) read from stdin work now
2) destroy segfault in number_process
3) removes dead code
complex patch my and Tito
Diffstat (limited to 'miscutils/less.c')
-rw-r--r-- | miscutils/less.c | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 1b993ea2c..5bf59a130 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
@@ -108,11 +108,6 @@ static unsigned long flags; | |||
108 | /* This is needed so that program behaviour changes when input comes from | 108 | /* This is needed so that program behaviour changes when input comes from |
109 | stdin */ | 109 | stdin */ |
110 | static int inp_stdin; | 110 | static int inp_stdin; |
111 | /* This is required so that when a file is requested to be examined after | ||
112 | input has come from stdin (e.g. dmesg | less), the input stream from | ||
113 | the keyboard still stays the same. If it switched back to stdin, keyboard | ||
114 | input wouldn't work. */ | ||
115 | static int ea_inp_stdin; | ||
116 | 111 | ||
117 | #ifdef CONFIG_FEATURE_LESS_MARKS | 112 | #ifdef CONFIG_FEATURE_LESS_MARKS |
118 | static int mark_lines[15][2]; | 113 | static int mark_lines[15][2]; |
@@ -137,12 +132,12 @@ static FILE *inp; | |||
137 | /* Reset terminal input to normal */ | 132 | /* Reset terminal input to normal */ |
138 | static void set_tty_cooked(void) { | 133 | static void set_tty_cooked(void) { |
139 | fflush(stdout); | 134 | fflush(stdout); |
140 | tcsetattr(0, TCSANOW, &term_orig); | 135 | tcsetattr(fileno(inp), TCSANOW, &term_orig); |
141 | } | 136 | } |
142 | 137 | ||
143 | /* Set terminal input to raw mode (taken from vi.c) */ | 138 | /* Set terminal input to raw mode (taken from vi.c) */ |
144 | static void set_tty_raw(void) { | 139 | static void set_tty_raw(void) { |
145 | tcsetattr(0, TCSANOW, &term_vi); | 140 | tcsetattr(fileno(inp), TCSANOW, &term_vi); |
146 | } | 141 | } |
147 | 142 | ||
148 | /* Exit the program gracefully */ | 143 | /* Exit the program gracefully */ |
@@ -224,35 +219,26 @@ static void data_readlines(void) { | |||
224 | FILE *fp; | 219 | FILE *fp; |
225 | 220 | ||
226 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); | 221 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); |
227 | 222 | flines = NULL; | |
228 | /* First of all, we need to know the number of lines so that flines can be initialised. */ | 223 | for (i = 0; (feof(fp)==0) && (i <= MAXLINES); i++) { |
229 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) | ||
230 | fgets(current_line, 256, fp); | ||
231 | rewind(fp); | ||
232 | /* Initialise fp */ | ||
233 | flines = malloc(i * sizeof(char *)); | ||
234 | |||
235 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) { | ||
236 | strcpy(current_line, ""); | 224 | strcpy(current_line, ""); |
237 | fgets(current_line, 256, fp); | 225 | fgets(current_line, 256, fp); |
238 | bb_xferror(fp, filename); | 226 | if(fp != stdin) |
227 | bb_xferror(fp, filename); | ||
228 | flines = xrealloc(flines, (i+1) * sizeof(char *)); | ||
239 | flines[i] = bb_xstrdup(current_line); | 229 | flines[i] = bb_xstrdup(current_line); |
240 | } | 230 | } |
241 | num_flines = i - 2; | 231 | num_flines = i - 2; |
242 | 232 | ||
243 | /* Reset variables for a new file */ | 233 | /* Reset variables for a new file */ |
244 | 234 | ||
245 | line_pos = 0; | 235 | line_pos = 0; |
246 | past_eof = 0; | 236 | past_eof = 0; |
247 | 237 | ||
248 | fclose(fp); | 238 | fclose(fp); |
249 | 239 | ||
250 | inp = (inp_stdin) ? fopen(CURRENT_TTY, "r") : stdin; | 240 | if(inp == NULL) |
251 | 241 | inp = (inp_stdin) ? fopen(CURRENT_TTY, "r") : stdin; | |
252 | if (ea_inp_stdin) { | ||
253 | fclose(inp); | ||
254 | inp = fopen(CURRENT_TTY, "r"); | ||
255 | } | ||
256 | 242 | ||
257 | if (flags & FLAG_N) | 243 | if (flags & FLAG_N) |
258 | add_linenumbers(); | 244 | add_linenumbers(); |
@@ -544,7 +530,6 @@ static void examine_file(void) { | |||
544 | num_files++; | 530 | num_files++; |
545 | 531 | ||
546 | inp_stdin = 0; | 532 | inp_stdin = 0; |
547 | ea_inp_stdin = 1; | ||
548 | reinitialise(); | 533 | reinitialise(); |
549 | } | 534 | } |
550 | 535 | ||
@@ -757,14 +742,16 @@ static void number_process(int first_digit) { | |||
757 | int num; | 742 | int num; |
758 | char num_input[80]; | 743 | char num_input[80]; |
759 | char keypress; | 744 | char keypress; |
745 | char *endptr; | ||
746 | |||
760 | num_input[0] = first_digit; | 747 | num_input[0] = first_digit; |
761 | 748 | ||
762 | /* Clear the current line, print a prompt, and then print the digit */ | 749 | /* Clear the current line, print a prompt, and then print the digit */ |
763 | clear_line(); | 750 | clear_line(); |
764 | printf(":%c", first_digit); | 751 | printf(":%c", first_digit); |
765 | 752 | ||
766 | /* Receive input until a letter is given */ | 753 | /* Receive input until a letter is given (max 80 chars)*/ |
767 | while((num_input[i] = tless_getch()) && isdigit(num_input[i])) { | 754 | while((i < 80) && (num_input[i] = tless_getch()) && isdigit(num_input[i])) { |
768 | printf("%c", num_input[i]); | 755 | printf("%c", num_input[i]); |
769 | i++; | 756 | i++; |
770 | } | 757 | } |
@@ -772,8 +759,9 @@ static void number_process(int first_digit) { | |||
772 | /* Take the final letter out of the digits string */ | 759 | /* Take the final letter out of the digits string */ |
773 | keypress = num_input[i]; | 760 | keypress = num_input[i]; |
774 | num_input[i] = '\0'; | 761 | num_input[i] = '\0'; |
775 | i--; | 762 | num = strtol(num_input, &endptr, 10); |
776 | num = atoi(num_input); | 763 | if (endptr==num_input || *endptr!='\0' || num < 1 || num > MAXLINES) |
764 | goto END; | ||
777 | 765 | ||
778 | /* We now know the number and the letter entered, so we process them */ | 766 | /* We now know the number and the letter entered, so we process them */ |
779 | switch (keypress) { | 767 | switch (keypress) { |
@@ -806,7 +794,7 @@ static void number_process(int first_digit) { | |||
806 | default: | 794 | default: |
807 | break; | 795 | break; |
808 | } | 796 | } |
809 | 797 | END: | |
810 | buffer_print(); | 798 | buffer_print(); |
811 | } | 799 | } |
812 | 800 | ||
@@ -1165,17 +1153,16 @@ int less_main(int argc, char **argv) { | |||
1165 | } | 1153 | } |
1166 | } | 1154 | } |
1167 | 1155 | ||
1168 | strcpy(filename, (inp_stdin) ? "stdin" : files[0]); | 1156 | strcpy(filename, (inp_stdin) ? bb_msg_standard_input : files[0]); |
1169 | tty_width_height(); | 1157 | tty_width_height(); |
1170 | tcgetattr(0, &term_orig); | 1158 | data_readlines(); |
1159 | tcgetattr(fileno(inp), &term_orig); | ||
1171 | term_vi = term_orig; | 1160 | term_vi = term_orig; |
1172 | term_vi.c_lflag &= (~ICANON & ~ECHO); | 1161 | term_vi.c_lflag &= (~ICANON & ~ECHO); |
1173 | term_vi.c_iflag &= (~IXON & ~ICRNL); | 1162 | term_vi.c_iflag &= (~IXON & ~ICRNL); |
1174 | term_vi.c_oflag &= (~ONLCR); | 1163 | term_vi.c_oflag &= (~ONLCR); |
1175 | term_vi.c_cc[VMIN] = 1; | 1164 | term_vi.c_cc[VMIN] = 1; |
1176 | term_vi.c_cc[VTIME] = 0; | 1165 | term_vi.c_cc[VTIME] = 0; |
1177 | |||
1178 | data_readlines(); | ||
1179 | buffer_init(); | 1166 | buffer_init(); |
1180 | buffer_print(); | 1167 | buffer_print(); |
1181 | 1168 | ||