aboutsummaryrefslogtreecommitdiff
path: root/miscutils/less.c
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2006-04-16 20:34:26 +0000
committerMike Frysinger <vapier@gentoo.org>2006-04-16 20:34:26 +0000
commit3a2b103eea9f87fa476b1f8c4ed4a11b7422eab8 (patch)
tree4ba65ff014a24b1e6295f618aa6fbbcf242a65e6 /miscutils/less.c
parentdbd8c3b2954f22b4d5221aa157153fec6f68ee57 (diff)
downloadbusybox-w32-3a2b103eea9f87fa476b1f8c4ed4a11b7422eab8.tar.gz
busybox-w32-3a2b103eea9f87fa476b1f8c4ed4a11b7422eab8.tar.bz2
busybox-w32-3a2b103eea9f87fa476b1f8c4ed4a11b7422eab8.zip
uncuddle brackets
Diffstat (limited to 'miscutils/less.c')
-rw-r--r--miscutils/less.c147
1 files changed, 78 insertions, 69 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index df55b50ac..ce34701d6 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -130,19 +130,21 @@ static struct termios term_orig, term_vi;
130static FILE *inp; 130static FILE *inp;
131 131
132/* Reset terminal input to normal */ 132/* Reset terminal input to normal */
133static void set_tty_cooked(void) { 133static void set_tty_cooked(void)
134{
134 fflush(stdout); 135 fflush(stdout);
135 tcsetattr(fileno(inp), TCSANOW, &term_orig); 136 tcsetattr(fileno(inp), TCSANOW, &term_orig);
136} 137}
137 138
138/* Set terminal input to raw mode (taken from vi.c) */ 139/* Set terminal input to raw mode (taken from vi.c) */
139static void set_tty_raw(void) { 140static void set_tty_raw(void)
141{
140 tcsetattr(fileno(inp), TCSANOW, &term_vi); 142 tcsetattr(fileno(inp), TCSANOW, &term_vi);
141} 143}
142 144
143/* Exit the program gracefully */ 145/* Exit the program gracefully */
144static void tless_exit(int code) { 146static void tless_exit(int code)
145 147{
146 /* TODO: We really should save the terminal state when we start, 148 /* TODO: We really should save the terminal state when we start,
147 and restore it when we exit. Less does this with the 149 and restore it when we exit. Less does this with the
148 "ti" and "te" termcap commands; can this be done with 150 "ti" and "te" termcap commands; can this be done with
@@ -155,8 +157,8 @@ static void tless_exit(int code) {
155/* Grab a character from input without requiring the return key. If the 157/* Grab a character from input without requiring the return key. If the
156 character is ASCII \033, get more characters and assign certain sequences 158 character is ASCII \033, get more characters and assign certain sequences
157 special return codes. Note that this function works best with raw input. */ 159 special return codes. Note that this function works best with raw input. */
158static int tless_getch(void) { 160static int tless_getch(void)
159 161{
160 int input; 162 int input;
161 163
162 set_tty_raw(); 164 set_tty_raw();
@@ -191,18 +193,20 @@ static int tless_getch(void) {
191 193
192/* Move the cursor to a position (x,y), where (0,0) is the 194/* Move the cursor to a position (x,y), where (0,0) is the
193 top-left corner of the console */ 195 top-left corner of the console */
194static void move_cursor(int x, int y) { 196static void move_cursor(int x, int y)
197{
195 printf("\033[%i;%iH", x, y); 198 printf("\033[%i;%iH", x, y);
196} 199}
197 200
198static void clear_line(void) { 201static void clear_line(void)
202{
199 move_cursor(height, 0); 203 move_cursor(height, 0);
200 printf("\033[K"); 204 printf("\033[K");
201} 205}
202 206
203/* This adds line numbers to every line, as the -N flag necessitates */ 207/* This adds line numbers to every line, as the -N flag necessitates */
204static void add_linenumbers(void) { 208static void add_linenumbers(void)
205 209{
206 char current_line[256]; 210 char current_line[256];
207 int i; 211 int i;
208 212
@@ -212,8 +216,8 @@ static void add_linenumbers(void) {
212 } 216 }
213} 217}
214 218
215static void data_readlines(void) { 219static void data_readlines(void)
216 220{
217 int i; 221 int i;
218 char current_line[256]; 222 char current_line[256];
219 FILE *fp; 223 FILE *fp;
@@ -245,7 +249,8 @@ static void data_readlines(void) {
245} 249}
246 250
247/* Turn a percentage into a line number */ 251/* Turn a percentage into a line number */
248static int reverse_percent(int percentage) { 252static int reverse_percent(int percentage)
253{
249 double linenum = percentage; 254 double linenum = percentage;
250 linenum = ((linenum / 100) * num_flines) - 1; 255 linenum = ((linenum / 100) * num_flines) - 1;
251 return(linenum); 256 return(linenum);
@@ -255,13 +260,14 @@ static int reverse_percent(int percentage) {
255 260
256/* Interestingly, writing calc_percent as a function and not a prototype saves around 32 bytes 261/* Interestingly, writing calc_percent as a function and not a prototype saves around 32 bytes
257 * on my build. */ 262 * on my build. */
258static int calc_percent(void) { 263static int calc_percent(void)
264{
259 return ((100 * (line_pos + height - 2) / num_flines) + 1); 265 return ((100 * (line_pos + height - 2) / num_flines) + 1);
260} 266}
261 267
262/* Print a status line if -M was specified */ 268/* Print a status line if -M was specified */
263static void m_status_print(void) { 269static void m_status_print(void)
264 270{
265 int percentage; 271 int percentage;
266 272
267 if (!past_eof) { 273 if (!past_eof) {
@@ -295,8 +301,8 @@ static void m_status_print(void) {
295} 301}
296 302
297/* Print a status line if -m was specified */ 303/* Print a status line if -m was specified */
298static void medium_status_print(void) { 304static void medium_status_print(void)
299 305{
300 int percentage; 306 int percentage;
301 percentage = calc_percent(); 307 percentage = calc_percent();
302 308
@@ -310,8 +316,8 @@ static void medium_status_print(void) {
310#endif 316#endif
311 317
312/* Print the status line */ 318/* Print the status line */
313static void status_print(void) { 319static void status_print(void)
314 320{
315 /* Change the status if flags have been set */ 321 /* Change the status if flags have been set */
316#ifdef CONFIG_FEATURE_LESS_FLAGS 322#ifdef CONFIG_FEATURE_LESS_FLAGS
317 if (flags & FLAG_M) 323 if (flags & FLAG_M)
@@ -340,8 +346,8 @@ static void status_print(void) {
340} 346}
341 347
342/* Print the buffer */ 348/* Print the buffer */
343static void buffer_print(void) { 349static void buffer_print(void)
344 350{
345 int i; 351 int i;
346 352
347 printf("%s", CLEAR); 353 printf("%s", CLEAR);
@@ -360,8 +366,8 @@ static void buffer_print(void) {
360} 366}
361 367
362/* Initialise the buffer */ 368/* Initialise the buffer */
363static void buffer_init(void) { 369static void buffer_init(void)
364 370{
365 int i; 371 int i;
366 372
367 if(buffer == NULL) { 373 if(buffer == NULL) {
@@ -385,8 +391,8 @@ static void buffer_init(void) {
385} 391}
386 392
387/* Move the buffer up and down in the file in order to scroll */ 393/* Move the buffer up and down in the file in order to scroll */
388static void buffer_down(int nlines) { 394static void buffer_down(int nlines)
389 395{
390 int i; 396 int i;
391 397
392 if (!past_eof) { 398 if (!past_eof) {
@@ -415,8 +421,8 @@ static void buffer_down(int nlines) {
415 } 421 }
416} 422}
417 423
418static void buffer_up(int nlines) { 424static void buffer_up(int nlines)
419 425{
420 int i; 426 int i;
421 int tilde_line; 427 int tilde_line;
422 428
@@ -467,8 +473,8 @@ static void buffer_up(int nlines) {
467 } 473 }
468} 474}
469 475
470static void buffer_line(int linenum) { 476static void buffer_line(int linenum)
471 477{
472 int i; 478 int i;
473 479
474 past_eof = 0; 480 past_eof = 0;
@@ -499,8 +505,8 @@ static void buffer_line(int linenum) {
499} 505}
500 506
501/* Reinitialise everything for a new file - free the memory and start over */ 507/* Reinitialise everything for a new file - free the memory and start over */
502static void reinitialise(void) { 508static void reinitialise(void)
503 509{
504 int i; 510 int i;
505 511
506 for (i = 0; i <= num_flines; i++) 512 for (i = 0; i <= num_flines; i++)
@@ -512,8 +518,8 @@ static void reinitialise(void) {
512 buffer_print(); 518 buffer_print();
513} 519}
514 520
515static void examine_file(void) { 521static void examine_file(void)
516 522{
517 int newline_offset; 523 int newline_offset;
518 524
519 clear_line(); 525 clear_line();
@@ -538,7 +544,8 @@ static void examine_file(void) {
538 * 0: go to the first file 544 * 0: go to the first file
539 * 1: go forward one file 545 * 1: go forward one file
540*/ 546*/
541static void change_file (int direction) { 547static void change_file(int direction)
548{
542 if (current_file != ((direction > 0) ? num_files : 1)) { 549 if (current_file != ((direction > 0) ? num_files : 1)) {
543 current_file = direction ? current_file + direction : 1; 550 current_file = direction ? current_file + direction : 1;
544 strcpy(filename, files[current_file - 1]); 551 strcpy(filename, files[current_file - 1]);
@@ -550,8 +557,8 @@ static void change_file (int direction) {
550 } 557 }
551} 558}
552 559
553static void remove_current_file(void) { 560static void remove_current_file(void)
554 561{
555 int i; 562 int i;
556 563
557 if (current_file != 1) { 564 if (current_file != 1) {
@@ -571,8 +578,8 @@ static void remove_current_file(void) {
571 } 578 }
572} 579}
573 580
574static void colon_process(void) { 581static void colon_process(void)
575 582{
576 int keypress; 583 int keypress;
577 584
578 /* Clear the current line and print a prompt */ 585 /* Clear the current line and print a prompt */
@@ -616,13 +623,14 @@ static void colon_process(void) {
616/* Get a regular expression from the user, and then go through the current 623/* Get a regular expression from the user, and then go through the current
617 file line by line, running a processing regex function on each one. */ 624 file line by line, running a processing regex function on each one. */
618 625
619static char *insert_highlights (char *line, int start, int end) { 626static char *insert_highlights(char *line, int start, int end)
620 627{
621 return bb_xasprintf("%.*s%s%.*s%s%s", start, line, HIGHLIGHT, 628 return bb_xasprintf("%.*s%s%.*s%s%s", start, line, HIGHLIGHT,
622 end - start, line + start, NORMAL, line + end); 629 end - start, line + start, NORMAL, line + end);
623} 630}
624 631
625static char *process_regex_on_line(char *line, regex_t *pattern) { 632static char *process_regex_on_line(char *line, regex_t *pattern)
633{
626 /* This function takes the regex and applies it to the line. 634 /* This function takes the regex and applies it to the line.
627 Each part of the line that matches has the HIGHLIGHT 635 Each part of the line that matches has the HIGHLIGHT
628 and NORMAL escape sequences placed around it by 636 and NORMAL escape sequences placed around it by
@@ -658,8 +666,8 @@ static char *process_regex_on_line(char *line, regex_t *pattern) {
658 return line2; 666 return line2;
659} 667}
660 668
661static void regex_process(void) { 669static void regex_process(void)
662 670{
663 char uncomp_regex[100]; 671 char uncomp_regex[100];
664 char current_line[256]; 672 char current_line[256];
665 int i; 673 int i;
@@ -708,8 +716,8 @@ static void regex_process(void) {
708 buffer_init(); 716 buffer_init();
709} 717}
710 718
711static void goto_match(int match) { 719static void goto_match(int match)
712 720{
713 /* This goes to a specific match - all line positions of matches are 721 /* This goes to a specific match - all line positions of matches are
714 stored within the match_lines[] array. */ 722 stored within the match_lines[] array. */
715 if ((match < num_matches) && (match >= 0)) { 723 if ((match < num_matches) && (match >= 0)) {
@@ -718,8 +726,8 @@ static void goto_match(int match) {
718 } 726 }
719} 727}
720 728
721static void search_backwards(void) { 729static void search_backwards(void)
722 730{
723 int current_linepos = line_pos; 731 int current_linepos = line_pos;
724 int i; 732 int i;
725 733
@@ -740,8 +748,8 @@ static void search_backwards(void) {
740} 748}
741#endif 749#endif
742 750
743static void number_process(int first_digit) { 751static void number_process(int first_digit)
744 752{
745 int i = 1; 753 int i = 1;
746 int num; 754 int num;
747 char num_input[80]; 755 char num_input[80];
@@ -803,8 +811,8 @@ END:
803} 811}
804 812
805#ifdef CONFIG_FEATURE_LESS_FLAGCS 813#ifdef CONFIG_FEATURE_LESS_FLAGCS
806static void flag_change(void) { 814static void flag_change(void)
807 815{
808 int keypress; 816 int keypress;
809 817
810 clear_line(); 818 clear_line();
@@ -829,8 +837,8 @@ static void flag_change(void) {
829 } 837 }
830} 838}
831 839
832static void show_flag_status(void) { 840static void show_flag_status(void)
833 841{
834 int keypress; 842 int keypress;
835 int flag_val; 843 int flag_val;
836 844
@@ -864,8 +872,8 @@ static void show_flag_status(void) {
864} 872}
865#endif 873#endif
866 874
867static void full_repaint(void) { 875static void full_repaint(void)
868 876{
869 int temp_line_pos = line_pos; 877 int temp_line_pos = line_pos;
870 data_readlines(); 878 data_readlines();
871 buffer_init(); 879 buffer_init();
@@ -874,8 +882,8 @@ static void full_repaint(void) {
874} 882}
875 883
876 884
877static void save_input_to_file(void) { 885static void save_input_to_file(void)
878 886{
879 char current_line[256]; 887 char current_line[256];
880 int i; 888 int i;
881 FILE *fp; 889 FILE *fp;
@@ -896,8 +904,8 @@ static void save_input_to_file(void) {
896} 904}
897 905
898#ifdef CONFIG_FEATURE_LESS_MARKS 906#ifdef CONFIG_FEATURE_LESS_MARKS
899static void add_mark(void) { 907static void add_mark(void)
900 908{
901 int letter; 909 int letter;
902 int mark_line; 910 int mark_line;
903 911
@@ -922,8 +930,8 @@ static void add_mark(void) {
922 } 930 }
923} 931}
924 932
925static void goto_mark(void) { 933static void goto_mark(void)
926 934{
927 int letter; 935 int letter;
928 int i; 936 int i;
929 937
@@ -949,8 +957,8 @@ static void goto_mark(void) {
949 957
950#ifdef CONFIG_FEATURE_LESS_BRACKETS 958#ifdef CONFIG_FEATURE_LESS_BRACKETS
951 959
952static char opp_bracket (char bracket) { 960static char opp_bracket(char bracket)
953 961{
954 switch (bracket) { 962 switch (bracket) {
955 case '{': case '[': 963 case '{': case '[':
956 return bracket + 2; 964 return bracket + 2;
@@ -970,8 +978,8 @@ static char opp_bracket (char bracket) {
970 } 978 }
971} 979}
972 980
973static void match_right_bracket(char bracket) { 981static void match_right_bracket(char bracket)
974 982{
975 int bracket_line = -1; 983 int bracket_line = -1;
976 int i; 984 int i;
977 985
@@ -995,8 +1003,8 @@ static void match_right_bracket(char bracket) {
995 } 1003 }
996} 1004}
997 1005
998static void match_left_bracket (char bracket) { 1006static void match_left_bracket(char bracket)
999 1007{
1000 int bracket_line = -1; 1008 int bracket_line = -1;
1001 int i; 1009 int i;
1002 1010
@@ -1025,7 +1033,8 @@ static void match_left_bracket (char bracket) {
1025 1033
1026#endif /* CONFIG_FEATURE_LESS_BRACKETS */ 1034#endif /* CONFIG_FEATURE_LESS_BRACKETS */
1027 1035
1028static void keypress_process(int keypress) { 1036static void keypress_process(int keypress)
1037{
1029 switch (keypress) { 1038 switch (keypress) {
1030 case KEY_DOWN: case 'e': case 'j': case '\015': 1039 case KEY_DOWN: case 'e': case 'j': case '\015':
1031 buffer_down(1); 1040 buffer_down(1);