1 /* vi: set sw=4 ts=4: */
3 * Mini less implementation for busybox
5 * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
12 * - Add more regular expression support - search modifiers, certain matches, etc.
13 * - Add more complex bracket searching - currently, nested brackets are
15 * - Add support for "F" as an input. This causes less to act in
16 * a similar way to tail -f.
17 * - Allow horizontal scrolling.
20 * - the inp file pointer is used so that keyboard input works after
21 * redirected input has been read from stdin
24 #include <sched.h> /* sched_yield() */
27 #if ENABLE_FEATURE_LESS_REGEXP
31 /* FIXME: currently doesn't work right */
32 #undef ENABLE_FEATURE_LESS_FLAGCS
33 #define ENABLE_FEATURE_LESS_FLAGCS 0
35 /* The escape codes for highlighted and normal text */
36 #define HIGHLIGHT "\033[7m"
37 #define NORMAL "\033[0m"
38 /* The escape code to clear the screen */
39 #define CLEAR "\033[H\033[J"
40 /* The escape code to clear to end of line */
41 #define CLEAR_2_EOL "\033[K"
43 /* These are the escape sequences corresponding to special keys */
51 REAL_KEY_HOME = '7', // vt100? linux vt? or what?
53 REAL_KEY_HOME_ALT = '1', // ESC [1~ (vt100? linux vt? or what?)
54 REAL_KEY_END_ALT = '4', // ESC [4~
55 REAL_KEY_HOME_XTERM = 'H',
56 REAL_KEY_END_XTERM = 'F',
58 /* These are the special codes assigned by this program to the special keys */
68 /* Absolute max of lines eaten */
69 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
71 /* This many "after the end" lines we will show (at max) */
75 /* Command line options */
82 /* hijack command line options variable for internal state vars */
83 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
86 #if !ENABLE_FEATURE_LESS_REGEXP
87 enum { pattern_valid = 0 };
91 int cur_fline; /* signed */
92 int kbd_fd; /* fd to get input from */
93 /* last position in last line, taking into account tabs */
95 unsigned max_displayed_line;
97 unsigned max_lineno; /* this one tracks linewrap */
99 ssize_t eof_error; /* eof if 0, error if < 0 */
104 const char *empty_line_marker;
106 unsigned current_file;
109 #if ENABLE_FEATURE_LESS_MARKS
111 unsigned mark_lines[15][2];
113 #if ENABLE_FEATURE_LESS_REGEXP
114 unsigned *match_lines;
115 int match_pos; /* signed! */
116 unsigned num_matches;
118 smallint pattern_valid;
121 struct termios term_orig, term_less;
123 #define G (*ptr_to_globals)
124 #define cur_fline (G.cur_fline )
125 #define kbd_fd (G.kbd_fd )
126 #define linepos (G.linepos )
127 #define max_displayed_line (G.max_displayed_line)
128 #define max_fline (G.max_fline )
129 #define max_lineno (G.max_lineno )
130 #define width (G.width )
131 #define eof_error (G.eof_error )
132 #define readpos (G.readpos )
133 #define readeof (G.readeof )
134 #define buffer (G.buffer )
135 #define flines (G.flines )
136 #define empty_line_marker (G.empty_line_marker )
137 #define num_files (G.num_files )
138 #define current_file (G.current_file )
139 #define filename (G.filename )
140 #define files (G.files )
141 #define num_marks (G.num_marks )
142 #define mark_lines (G.mark_lines )
143 #define match_lines (G.match_lines )
144 #define match_pos (G.match_pos )
145 #define num_matches (G.num_matches )
146 #define pattern (G.pattern )
147 #define pattern_valid (G.pattern_valid )
148 #define terminated (G.terminated )
149 #define term_orig (G.term_orig )
150 #define term_less (G.term_less )
151 #define INIT_G() do { \
152 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
153 empty_line_marker = "~"; \
160 /* Reset terminal input to normal */
161 static void set_tty_cooked(void)
164 tcsetattr(kbd_fd, TCSANOW, &term_orig);
167 /* Exit the program gracefully */
168 static void less_exit(int code)
170 /* TODO: We really should save the terminal state when we start,
171 * and restore it when we exit. Less does this with the
172 * "ti" and "te" termcap commands; can this be done with
175 fflush_stdout_and_exit(code);
178 /* Move the cursor to a position (x,y), where (0,0) is the
179 top-left corner of the console */
180 static void move_cursor(int line, int row)
182 printf("\033[%u;%uH", line, row);
185 static void clear_line(void)
187 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
190 static void print_hilite(const char *str)
192 printf(HIGHLIGHT"%s"NORMAL, str);
195 static void print_statusline(const char *str)
198 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
201 #if ENABLE_FEATURE_LESS_REGEXP
202 static void fill_match_lines(unsigned pos);
204 #define fill_match_lines(pos) ((void)0)
207 /* Devilishly complex routine.
209 * Has to deal with EOF and EPIPE on input,
210 * with line wrapping, with last line not ending in '\n'
211 * (possibly not ending YET!), with backspace and tabs.
212 * It reads input again if last time we got an EOF (thus supporting
213 * growing files) or EPIPE (watching output of slow process like make).
216 * flines[] - array of lines already read. Linewrap may cause
217 * one source file line to occupy several flines[n].
218 * flines[max_fline] - last line, possibly incomplete.
219 * terminated - 1 if flines[max_fline] is 'terminated'
220 * (if there was '\n' [which isn't stored itself, we just remember
222 * max_lineno - last line's number, this one doesn't increment
223 * on line wrap, only on "real" new lines.
224 * readbuf[0..readeof-1] - small preliminary buffer.
225 * readbuf[readpos] - next character to add to current line.
226 * linepos - screen line position of next char to be read
227 * (takes into account tabs and backspaces)
228 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
230 static void read_lines(void)
232 #define readbuf bb_common_bufsiz1
233 char *current_line, *p;
234 USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;)
236 char last_terminated = terminated;
238 if (option_mask32 & FLAG_N)
241 current_line = xmalloc(w);
243 max_fline += last_terminated;
244 if (!last_terminated) {
245 const char *cp = flines[max_fline];
246 if (option_mask32 & FLAG_N)
248 strcpy(current_line, cp);
249 p += strlen(current_line);
250 /* linepos is still valid from previous read_lines() */
261 /* if no unprocessed chars left, eat more */
262 if (readpos >= readeof) {
263 smallint yielded = 0;
267 eof_error = safe_read(0, readbuf, sizeof(readbuf));
271 if (errno == EAGAIN && !yielded) {
272 /* We can hit EAGAIN while searching for regexp match.
273 * Yield is not 100% reliable solution in general,
274 * but for less it should be good enough -
275 * we give stdin supplier some CPU time to produce
276 * more input. We do it just once.
277 * Currently, we do not stop when we found the Nth
278 * occurrence we were looking for. We read till end
279 * (or double EAGAIN). TODO? */
286 print_statusline("read error");
290 if (eof_error <= 0) {
294 c = readbuf[readpos];
295 /* backspace? [needed for manpages] */
296 /* <tab><bs> is (a) insane and */
297 /* (b) harder to do correctly, so we refuse to do it */
298 if (c == '\x8' && linepos && p[-1] != '\t') {
299 readpos++; /* eat it */
301 /* was buggy (p could end up <= current_line)... */
306 size_t new_linepos = linepos + 1;
311 if (new_linepos >= w)
313 linepos = new_linepos;
315 /* ok, we will eat this char */
322 /* NUL is substituted by '\n'! */
323 if (c == '\0') c = '\n';
327 /* Corner case: linewrap with only "" wrapping to next line */
328 /* Looks ugly on screen, so we do not store this empty line */
329 if (!last_terminated && !current_line[0]) {
335 last_terminated = terminated;
336 flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
337 if (option_mask32 & FLAG_N) {
338 /* Width of 7 preserves tab spacing in the text */
339 flines[max_fline] = xasprintf(
340 (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
341 max_lineno % 10000000, current_line);
346 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
348 if (max_fline >= MAXLINES)
350 if (max_fline > cur_fline + max_displayed_line)
352 if (eof_error <= 0) {
353 if (eof_error < 0 && errno == EAGAIN) {
354 /* not yet eof or error, reset flag (or else
355 * we will hog CPU - select() will return
362 current_line = xmalloc(w);
366 fill_match_lines(old_max_fline);
370 #if ENABLE_FEATURE_LESS_FLAGS
371 /* Interestingly, writing calc_percent as a function saves around 32 bytes
373 static int calc_percent(void)
375 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
376 return p <= 100 ? p : 100;
379 /* Print a status line if -M was specified */
380 static void m_status_print(void)
385 printf(HIGHLIGHT"%s", filename);
387 printf(" (file %i of %i)", current_file, num_files);
388 printf(" lines %i-%i/%i ",
389 cur_fline + 1, cur_fline + max_displayed_line + 1,
391 if (cur_fline >= max_fline - max_displayed_line) {
392 printf("(END)"NORMAL);
393 if (num_files > 1 && current_file != num_files)
394 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
397 percentage = calc_percent();
398 printf("%i%%"NORMAL, percentage);
402 /* Print the status line */
403 static void status_print(void)
407 /* Change the status if flags have been set */
408 #if ENABLE_FEATURE_LESS_FLAGS
409 if (option_mask32 & (FLAG_M|FLAG_m)) {
417 if (cur_fline && cur_fline < max_fline - max_displayed_line) {
425 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
426 p, current_file, num_files);
432 static void cap_cur_fline(int nlines)
437 if (cur_fline + max_displayed_line > max_fline + TILDES) {
441 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
442 /* As the number of lines requested was too large, we just move
443 to the end of the file */
449 static const char controls[] =
450 /* NUL: never encountered; TAB: not converted */
451 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
452 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
453 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
454 static const char ctrlconv[] =
455 /* '\n': it's a former NUL - subst with '@', not 'J' */
456 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
457 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
459 #if ENABLE_FEATURE_LESS_REGEXP
460 static void print_found(const char *line)
465 regmatch_t match_structs;
468 const char *str = line;
473 n = strcspn(str, controls);
480 n = strspn(str, controls);
487 /* buf[] holds quarantined version of str */
489 /* Each part of the line that matches has the HIGHLIGHT
490 and NORMAL escape sequences placed around it.
491 NB: we regex against line, but insert text
492 from quarantined copy (buf[]) */
498 while (match_status == 0) {
499 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
501 match_structs.rm_so, str,
502 match_structs.rm_eo - match_structs.rm_so,
503 str + match_structs.rm_so);
504 free(growline); growline = new;
505 str += match_structs.rm_eo;
506 line += match_structs.rm_eo;
509 /* Most of the time doesn't find the regex, optimize for that */
510 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
514 printf(CLEAR_2_EOL"%s\n", str);
517 printf(CLEAR_2_EOL"%s%s\n", growline, str);
521 void print_found(const char *line);
524 static void print_ascii(const char *str)
532 n = strcspn(str, controls);
535 printf("%.*s", (int) n, str);
538 n = strspn(str, controls);
543 else if (*str == (char)0x9b)
544 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
545 /* I want to know who committed this sin */
548 *p++ = ctrlconv[(unsigned char)*str];
557 /* Print the buffer */
558 static void buffer_print(void)
563 for (i = 0; i <= max_displayed_line; i++)
565 print_found(buffer[i]);
567 print_ascii(buffer[i]);
571 static void buffer_fill_and_print(void)
574 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
575 buffer[i] = flines[cur_fline + i];
577 for (; i <= max_displayed_line; i++) {
578 buffer[i] = empty_line_marker;
583 /* Move the buffer up and down in the file in order to scroll */
584 static void buffer_down(int nlines)
588 cap_cur_fline(nlines);
589 buffer_fill_and_print();
592 static void buffer_up(int nlines)
595 if (cur_fline < 0) cur_fline = 0;
597 buffer_fill_and_print();
600 static void buffer_line(int linenum)
606 if (linenum + max_displayed_line > max_fline)
607 linenum = max_fline - max_displayed_line + TILDES;
611 buffer_fill_and_print();
614 static void open_file_and_read_lines(void)
617 int fd = xopen(filename, O_RDONLY);
621 /* "less" with no arguments in argv[] */
622 /* For status line only */
623 filename = xstrdup(bb_msg_standard_input);
632 /* Reinitialize everything for a new file - free the memory and start over */
633 static void reinitialize(void)
638 for (i = 0; i <= max_fline; i++)
639 free((void*)(flines[i]));
647 open_file_and_read_lines();
648 buffer_fill_and_print();
651 static void getch_nowait(char* input, int sz)
658 /* NB: select returns whenever read will not block. Therefore:
659 * (a) with O_NONBLOCK'ed fds select will return immediately
660 * (b) if eof is reached, select will also return
661 * because read will immediately return 0 bytes.
662 * Even if select says that input is available, read CAN block
663 * (switch fd into O_NONBLOCK'ed mode to avoid it)
666 if (max_fline <= cur_fline + max_displayed_line
667 && eof_error > 0 /* did NOT reach eof yet */
669 /* We are interested in stdin */
672 FD_SET(kbd_fd, &readfds);
673 tcsetattr(kbd_fd, TCSANOW, &term_less);
674 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
678 rd = read(kbd_fd, input, sz);
681 /* No keyboard input, but we have input on stdin! */
682 if (errno != EAGAIN) /* Huh?? */
685 buffer_fill_and_print();
690 /* Grab a character from input without requiring the return key. If the
691 * character is ASCII \033, get more characters and assign certain sequences
692 * special return codes. Note that this function works best with raw input. */
693 static int less_getch(void)
698 getch_nowait(input, sizeof(input));
699 /* Detect escape sequences (i.e. arrow keys) and handle
700 * them accordingly */
702 if (input[0] == '\033' && input[1] == '[') {
704 i = input[2] - REAL_KEY_UP;
707 i = input[2] - REAL_PAGE_UP;
710 if (input[2] == REAL_KEY_HOME_XTERM)
712 if (input[2] == REAL_KEY_HOME_ALT)
714 if (input[2] == REAL_KEY_END_XTERM)
716 if (input[2] == REAL_KEY_END_ALT)
720 /* Reject almost all control chars */
722 if (i < ' ' && i != 0x0d && i != 8) goto again;
727 static char* less_gets(int sz)
731 char *result = xzalloc(1);
735 /* I be damned if I know why is it needed *repeatedly*,
736 * but it is needed. Is it because of stdio? */
737 tcsetattr(kbd_fd, TCSANOW, &term_less);
750 if (i >= width - sz - 1)
751 continue; /* len limit */
754 result = xrealloc(result, i+1);
759 static void examine_file(void)
761 print_statusline("Examine: ");
763 filename = less_gets(sizeof("Examine: ")-1);
764 /* files start by = argv. why we assume that argv is infinitely long??
765 files[num_files] = filename;
766 current_file = num_files + 1;
769 num_files = current_file = 1;
773 /* This function changes the file currently being paged. direction can be one of the following:
774 * -1: go back one file
775 * 0: go to the first file
776 * 1: go forward one file */
777 static void change_file(int direction)
779 if (current_file != ((direction > 0) ? num_files : 1)) {
780 current_file = direction ? current_file + direction : 1;
782 filename = xstrdup(files[current_file - 1]);
785 print_statusline(direction > 0 ? "No next file" : "No previous file");
789 static void remove_current_file(void)
796 if (current_file != 1) {
798 for (i = 3; i <= num_files; i++)
799 files[i - 2] = files[i - 1];
803 for (i = 2; i <= num_files; i++)
804 files[i - 2] = files[i - 1];
810 static void colon_process(void)
814 /* Clear the current line and print a prompt */
815 print_statusline(" :");
817 keypress = less_getch();
820 remove_current_file();
825 #if ENABLE_FEATURE_LESS_FLAGS
845 #if ENABLE_FEATURE_LESS_REGEXP
846 static void normalize_match_pos(int match)
848 if (match >= num_matches)
849 match = num_matches - 1;
855 static void goto_match(int match)
864 /* Try to find next match if eof isn't reached yet */
865 if (match >= num_matches && eof_error > 0) {
866 cur_fline = MAXLINES; /* look as far as needed */
870 cap_cur_fline(cur_fline);
871 normalize_match_pos(match);
872 buffer_line(match_lines[match_pos]);
875 print_statusline("No matches found");
879 static void fill_match_lines(unsigned pos)
883 /* Run the regex on each line of the current file */
884 while (pos <= max_fline) {
885 /* If this line matches */
886 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
887 /* and we didn't match it last time */
888 && !(num_matches && match_lines[num_matches-1] == pos)
890 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
891 match_lines[num_matches++] = pos;
897 static void regex_process(void)
899 char *uncomp_regex, *err;
901 /* Reset variables */
911 /* Get the uncompiled regular expression from the user */
913 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
914 uncomp_regex = less_gets(1);
915 if (!uncomp_regex[0]) {
921 /* Compile the regex and check for errors */
922 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
925 print_statusline(err);
933 while (match_pos < num_matches) {
934 if (match_lines[match_pos] > cur_fline)
938 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
941 /* It's possible that no matches are found yet.
942 * goto_match() will read input looking for match,
944 goto_match(match_pos);
948 static void number_process(int first_digit)
952 char num_input[sizeof(int)*4]; /* more than enough */
955 num_input[0] = first_digit;
957 /* Clear the current line, print a prompt, and then print the digit */
959 printf(":%c", first_digit);
961 /* Receive input until a letter is given */
962 while (i < sizeof(num_input)-1) {
963 num_input[i] = less_getch();
964 if (!num_input[i] || !isdigit(num_input[i]))
966 putchar(num_input[i]);
970 /* Take the final letter out of the digits string */
971 keypress = num_input[i];
973 num = bb_strtou(num_input, NULL, 10);
974 /* on format error, num == -1 */
975 if (num < 1 || num > MAXLINES) {
980 /* We now know the number and the letter entered, so we process them */
982 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
985 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
988 case 'g': case '<': case 'G': case '>':
989 cur_fline = num + max_displayed_line;
991 buffer_line(num - 1);
994 num = num * (max_fline / 100); /* + max_fline / 2; */
995 cur_fline = num + max_displayed_line;
999 #if ENABLE_FEATURE_LESS_REGEXP
1001 goto_match(match_pos + num);
1004 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1008 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1015 #if ENABLE_FEATURE_LESS_FLAGCS
1016 static void flag_change(void)
1022 keypress = less_getch();
1026 option_mask32 ^= FLAG_M;
1029 option_mask32 ^= FLAG_m;
1032 option_mask32 ^= FLAG_E;
1035 option_mask32 ^= FLAG_TILDE;
1040 static void show_flag_status(void)
1047 keypress = less_getch();
1051 flag_val = option_mask32 & FLAG_M;
1054 flag_val = option_mask32 & FLAG_m;
1057 flag_val = option_mask32 & FLAG_TILDE;
1060 flag_val = option_mask32 & FLAG_N;
1063 flag_val = option_mask32 & FLAG_E;
1071 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
1075 static void save_input_to_file(void)
1077 const char *msg = "";
1082 print_statusline("Log file: ");
1083 current_line = less_gets(sizeof("Log file: ")-1);
1084 if (strlen(current_line) > 0) {
1085 fp = fopen(current_line, "w");
1087 msg = "Error opening log file";
1090 for (i = 0; i <= max_fline; i++)
1091 fprintf(fp, "%s\n", flines[i]);
1096 print_statusline(msg);
1100 #if ENABLE_FEATURE_LESS_MARKS
1101 static void add_mark(void)
1105 print_statusline("Mark: ");
1106 letter = less_getch();
1108 if (isalpha(letter)) {
1109 /* If we exceed 15 marks, start overwriting previous ones */
1110 if (num_marks == 14)
1113 mark_lines[num_marks][0] = letter;
1114 mark_lines[num_marks][1] = cur_fline;
1117 print_statusline("Invalid mark letter");
1121 static void goto_mark(void)
1126 print_statusline("Go to mark: ");
1127 letter = less_getch();
1130 if (isalpha(letter)) {
1131 for (i = 0; i <= num_marks; i++)
1132 if (letter == mark_lines[i][0]) {
1133 buffer_line(mark_lines[i][1]);
1136 if (num_marks == 14 && letter != mark_lines[14][0])
1137 print_statusline("Mark not set");
1139 print_statusline("Invalid mark letter");
1143 #if ENABLE_FEATURE_LESS_BRACKETS
1144 static char opp_bracket(char bracket)
1159 static void match_right_bracket(char bracket)
1161 int bracket_line = -1;
1164 if (strchr(flines[cur_fline], bracket) == NULL) {
1165 print_statusline("No bracket in top line");
1168 for (i = cur_fline + 1; i < max_fline; i++) {
1169 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1174 if (bracket_line == -1)
1175 print_statusline("No matching bracket found");
1176 buffer_line(bracket_line - max_displayed_line);
1179 static void match_left_bracket(char bracket)
1181 int bracket_line = -1;
1184 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
1185 print_statusline("No bracket in bottom line");
1189 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
1190 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1195 if (bracket_line == -1)
1196 print_statusline("No matching bracket found");
1197 buffer_line(bracket_line);
1199 #endif /* FEATURE_LESS_BRACKETS */
1201 static void keypress_process(int keypress)
1204 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1207 case KEY_UP: case 'y': case 'k':
1210 case PAGE_DOWN: case ' ': case 'z':
1211 buffer_down(max_displayed_line + 1);
1213 case PAGE_UP: case 'w': case 'b':
1214 buffer_up(max_displayed_line + 1);
1217 buffer_down((max_displayed_line + 1) / 2);
1220 buffer_up((max_displayed_line + 1) / 2);
1222 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1225 case KEY_END: case 'G': case '>':
1226 cur_fline = MAXLINES;
1228 buffer_line(cur_fline);
1233 #if ENABLE_FEATURE_LESS_MARKS
1250 save_input_to_file();
1255 #if ENABLE_FEATURE_LESS_FLAGS
1260 #if ENABLE_FEATURE_LESS_REGEXP
1262 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1266 goto_match(match_pos + 1);
1269 goto_match(match_pos - 1);
1272 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1276 #if ENABLE_FEATURE_LESS_FLAGCS
1285 #if ENABLE_FEATURE_LESS_BRACKETS
1286 case '{': case '(': case '[':
1287 match_right_bracket(keypress);
1289 case '}': case ')': case ']':
1290 match_left_bracket(keypress);
1298 if (isdigit(keypress))
1299 number_process(keypress);
1302 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1308 int less_main(int argc, char **argv);
1309 int less_main(int argc, char **argv)
1315 /* TODO: -x: do not interpret backspace, -xx: tab also */
1316 /* -xxx: newline also */
1317 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
1318 getopt32(argc, argv, "EMmN~");
1324 /* Another popular pager, most, detects when stdout
1325 * is not a tty and turns into cat. This makes sense. */
1326 if (!isatty(STDOUT_FILENO))
1327 return bb_cat(argv);
1328 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1330 return bb_cat(argv);
1333 if (isatty(STDIN_FILENO)) {
1334 /* Just "less"? No args and no redirection? */
1335 bb_error_msg("missing filename");
1339 filename = xstrdup(files[0]);
1341 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1342 /* 20: two tabstops + 4 */
1343 if (width < 20 || max_displayed_line < 3)
1344 bb_error_msg_and_die("too narrow here");
1345 max_displayed_line -= 2;
1347 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
1348 if (option_mask32 & FLAG_TILDE)
1349 empty_line_marker = "";
1351 tcgetattr(kbd_fd, &term_orig);
1352 signal(SIGTERM, sig_catcher);
1353 signal(SIGINT, sig_catcher);
1354 term_less = term_orig;
1355 term_less.c_lflag &= ~(ICANON | ECHO);
1356 term_less.c_iflag &= ~(IXON | ICRNL);
1357 /*term_less.c_oflag &= ~ONLCR;*/
1358 term_less.c_cc[VMIN] = 1;
1359 term_less.c_cc[VTIME] = 0;
1361 /* Want to do it just once, but it doesn't work, */
1362 /* so we are redoing it (see code above). Mystery... */
1363 /*tcsetattr(kbd_fd, TCSANOW, &term_less);*/
1367 keypress = less_getch();
1368 keypress_process(keypress);