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 GPLv2 or later, see file LICENSE in this source tree.
11 * - Add more regular expression support - search modifiers, certain matches, etc.
12 * - Add more complex bracket searching - currently, nested brackets are
14 * - Add support for "F" as an input. This causes less to act in
15 * a similar way to tail -f.
16 * - Allow horizontal scrolling.
19 * - the inp file pointer is used so that keyboard input works after
20 * redirected input has been read from stdin
23 //config: bool "less (15 kb)"
26 //config: 'less' is a pager, meaning that it displays text files. It possesses
27 //config: a wide array of features, and is an improvement over 'more'.
29 //config:config FEATURE_LESS_MAXLINES
30 //config: int "Max number of input lines less will try to eat"
31 //config: default 9999999
32 //config: depends on LESS
34 //config:config FEATURE_LESS_BRACKETS
35 //config: bool "Enable bracket searching"
37 //config: depends on LESS
39 //config: This option adds the capability to search for matching left and right
40 //config: brackets, facilitating programming.
42 //config:config FEATURE_LESS_FLAGS
43 //config: bool "Enable -m/-M"
45 //config: depends on LESS
47 //config: The -M/-m flag enables a more sophisticated status line.
49 //config:config FEATURE_LESS_TRUNCATE
50 //config: bool "Enable -S"
52 //config: depends on LESS
54 //config: The -S flag causes long lines to be truncated rather than
57 //config:config FEATURE_LESS_MARKS
58 //config: bool "Enable marks"
60 //config: depends on LESS
62 //config: Marks enable positions in a file to be stored for easy reference.
64 //config:config FEATURE_LESS_REGEXP
65 //config: bool "Enable regular expressions"
67 //config: depends on LESS
69 //config: Enable regular expressions, allowing complex file searches.
71 //config:config FEATURE_LESS_WINCH
72 //config: bool "Enable automatic resizing on window size changes"
74 //config: depends on LESS
76 //config: Makes less track window size changes.
78 //config:config FEATURE_LESS_ASK_TERMINAL
79 //config: bool "Use 'tell me cursor position' ESC sequence to measure window"
81 //config: depends on FEATURE_LESS_WINCH
83 //config: Makes less track window size changes.
84 //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
85 //config: this option makes less perform a last-ditch effort to find it:
86 //config: position cursor to 999,999 and ask terminal to report real
87 //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
88 //config: This is not clean but helps a lot on serial lines and such.
90 //config:config FEATURE_LESS_DASHCMD
91 //config: bool "Enable flag changes ('-' command)"
93 //config: depends on LESS
95 //config: This enables the ability to change command-line flags within
96 //config: less itself ('-' keyboard command).
98 //config:config FEATURE_LESS_LINENUMS
99 //config: bool "Enable -N (dynamic switching of line numbers)"
101 //config: depends on FEATURE_LESS_DASHCMD
103 //applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP))
105 //kbuild:lib-$(CONFIG_LESS) += less.o
107 //usage:#define less_trivial_usage
108 //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
109 //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..."
110 //usage:#define less_full_usage "\n\n"
111 //usage: "View FILE (or stdin) one screenful at a time\n"
112 //usage: "\n -E Quit once the end of a file is reached"
113 //usage: IF_FEATURE_LESS_REGEXP(
114 //usage: "\n -I Ignore case in all searches"
116 //usage: IF_FEATURE_LESS_FLAGS(
117 //usage: "\n -M,-m Display status line with line numbers"
118 //usage: "\n and percentage through the file"
120 //usage: "\n -N Prefix line number to each line"
121 //usage: IF_FEATURE_LESS_TRUNCATE(
122 //usage: "\n -S Truncate long lines"
124 //usage: "\n -~ Suppress ~s displayed past EOF"
126 #include <sched.h> /* sched_yield() */
129 #include "common_bufsiz.h"
130 #if ENABLE_FEATURE_LESS_REGEXP
136 /* The escape codes for highlighted and normal text */
137 #define HIGHLIGHT ESC"[7m"
138 #define NORMAL ESC"[m"
139 /* The escape code to home and clear to the end of screen */
140 #define CLEAR ESC"[H"ESC"[J"
141 /* The escape code to clear to the end of line */
142 #define CLEAR_2_EOL ESC"[K"
145 /* Absolute max of lines eaten */
146 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
147 /* This many "after the end" lines we will show (at max) */
151 /* Command line options */
159 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE,
160 /* hijack command line options variable for internal state vars */
161 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
164 #if !ENABLE_FEATURE_LESS_REGEXP
165 enum { pattern_valid = 0 };
169 int cur_fline; /* signed */
170 int kbd_fd; /* fd to get input from */
171 int kbd_fd_orig_flags;
173 /* last position in last line, taking into account tabs */
174 size_t last_line_pos;
176 unsigned max_lineno; /* this one tracks linewrap */
177 unsigned max_displayed_line;
179 #if ENABLE_FEATURE_LESS_WINCH
180 unsigned winch_counter;
182 ssize_t eof_error; /* eof if 0, error if < 0 */
184 ssize_t readeof; /* must be signed */
187 const char *empty_line_marker;
189 unsigned current_file;
192 #if ENABLE_FEATURE_LESS_FLAGS
193 int num_lines; /* a flag if < 0, line count if >= 0 */
194 # define REOPEN_AND_COUNT (-1)
195 # define REOPEN_STDIN (-2)
196 # define NOT_REGULAR_FILE (-3)
198 #if ENABLE_FEATURE_LESS_MARKS
200 unsigned mark_lines[15][2];
202 #if ENABLE_FEATURE_LESS_REGEXP
203 unsigned *match_lines;
204 int match_pos; /* signed! */
205 int wanted_match; /* signed! */
208 smallint pattern_valid;
210 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
211 smallint winsize_err;
214 struct termios term_orig, term_less;
215 char kbd_input[KEYCODE_BUFFER_SIZE];
217 #define G (*ptr_to_globals)
218 #define cur_fline (G.cur_fline )
219 #define kbd_fd (G.kbd_fd )
220 #define less_gets_pos (G.less_gets_pos )
221 #define last_line_pos (G.last_line_pos )
222 #define max_fline (G.max_fline )
223 #define max_lineno (G.max_lineno )
224 #define max_displayed_line (G.max_displayed_line)
225 #define width (G.width )
226 #define winch_counter (G.winch_counter )
227 /* This one is 100% not cached by compiler on read access */
228 #define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
229 #define eof_error (G.eof_error )
230 #define readpos (G.readpos )
231 #define readeof (G.readeof )
232 #define buffer (G.buffer )
233 #define flines (G.flines )
234 #define empty_line_marker (G.empty_line_marker )
235 #define num_files (G.num_files )
236 #define current_file (G.current_file )
237 #define filename (G.filename )
238 #define files (G.files )
239 #define num_lines (G.num_lines )
240 #define num_marks (G.num_marks )
241 #define mark_lines (G.mark_lines )
242 #if ENABLE_FEATURE_LESS_REGEXP
243 #define match_lines (G.match_lines )
244 #define match_pos (G.match_pos )
245 #define num_matches (G.num_matches )
246 #define wanted_match (G.wanted_match )
247 #define pattern (G.pattern )
248 #define pattern_valid (G.pattern_valid )
250 #define terminated (G.terminated )
251 #define term_orig (G.term_orig )
252 #define term_less (G.term_less )
253 #define kbd_input (G.kbd_input )
254 #define INIT_G() do { \
255 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
256 less_gets_pos = -1; \
257 empty_line_marker = "~"; \
261 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
264 /* flines[] are lines read from stdin, each in malloc'ed buffer.
265 * Line numbers are stored as uint32_t prepended to each line.
266 * Pointer is adjusted so that flines[i] points directly past
267 * line number. Accessor: */
268 #define MEMPTR(p) ((char*)(p) - 4)
269 #define LINENO(p) (*(uint32_t*)((p) - 4))
272 /* Reset terminal input to normal */
273 static void set_tty_cooked(void)
276 tcsetattr(kbd_fd, TCSANOW, &term_orig);
279 /* Move the cursor to a position (x,y), where (0,0) is the
280 top-left corner of the console */
281 static void move_cursor(int line, int col)
283 printf(ESC"[%u;%uH", line, col);
286 static void clear_line(void)
288 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
291 static void print_hilite(const char *str)
293 printf(HIGHLIGHT"%s"NORMAL, str);
296 static void print_statusline(const char *str)
299 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
302 /* Exit the program gracefully */
303 static void less_exit(int code)
306 if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
310 kill_myself_with_sig(- code); /* does not return */
314 #if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
315 || ENABLE_FEATURE_LESS_WINCH
316 static void re_wrap(void)
322 int new_cur_fline = 0;
325 const char **old_flines = flines;
327 char **new_flines = NULL;
330 if (option_mask32 & FLAG_N)
343 if (*d == '\t') { /* tab */
345 new_line_pos &= (~7);
349 if (new_line_pos >= w) {
351 /* new line is full, create next one */
354 sz = (d - linebuf) + 1; /* + 1: NUL */
355 d = ((char*)xmalloc(sz + 4)) + 4;
357 memcpy(d, linebuf, sz);
358 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
359 new_flines[dst_idx] = d;
361 if (new_line_pos < w) {
362 /* if we came here thru "goto next_new" */
363 if (src_idx > max_fline)
372 /* *d == NUL: old line ended, go to next old one */
373 free(MEMPTR(old_flines[src_idx]));
374 /* btw, convert cur_fline... */
375 if (cur_fline == src_idx)
376 new_cur_fline = dst_idx;
378 /* no more lines? finish last new line (and exit the loop) */
379 if (src_idx > max_fline)
381 s = old_flines[src_idx];
382 if (lineno != LINENO(s)) {
383 /* this is not a continuation line!
384 * create next _new_ line too */
390 flines = (const char **)new_flines;
392 max_fline = dst_idx - 1;
393 last_line_pos = new_line_pos;
394 cur_fline = new_cur_fline;
395 /* max_lineno is screen-size independent */
396 #if ENABLE_FEATURE_LESS_REGEXP
402 #if ENABLE_FEATURE_LESS_REGEXP
403 static void fill_match_lines(unsigned pos);
405 #define fill_match_lines(pos) ((void)0)
408 static int at_end(void)
410 return (option_mask32 & FLAG_S)
411 ? !(cur_fline <= max_fline &&
412 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
413 : !(max_fline > cur_fline + max_displayed_line);
416 /* Devilishly complex routine.
418 * Has to deal with EOF and EPIPE on input,
419 * with line wrapping, with last line not ending in '\n'
420 * (possibly not ending YET!), with backspace and tabs.
421 * It reads input again if last time we got an EOF (thus supporting
422 * growing files) or EPIPE (watching output of slow process like make).
425 * flines[] - array of lines already read. Linewrap may cause
426 * one source file line to occupy several flines[n].
427 * flines[max_fline] - last line, possibly incomplete.
428 * terminated - 1 if flines[max_fline] is 'terminated'
429 * (if there was '\n' [which isn't stored itself, we just remember
431 * max_lineno - last line's number, this one doesn't increment
432 * on line wrap, only on "real" new lines.
433 * readbuf[0..readeof-1] - small preliminary buffer.
434 * readbuf[readpos] - next character to add to current line.
435 * last_line_pos - screen line position of next char to be read
436 * (takes into account tabs and backspaces)
437 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
439 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
440 * "/search on very long input" and "reaching max line count" corner cases.
442 static void read_lines(void)
444 char *current_line, *p;
446 char last_terminated = terminated;
447 time_t last_time = 0;
448 int retry_EAGAIN = 2;
449 #if ENABLE_FEATURE_LESS_REGEXP
450 unsigned old_max_fline = max_fline;
453 #define readbuf bb_common_bufsiz1
454 setup_common_bufsiz();
456 /* (careful: max_fline can be -1) */
457 if (max_fline + 1 > MAXLINES)
460 if (option_mask32 & FLAG_N)
463 p = current_line = ((char*)xmalloc(w + 5)) + 4;
464 if (!last_terminated) {
465 const char *cp = flines[max_fline];
468 /* last_line_pos is still valid from previous read_lines() */
474 while (1) { /* read lines until we reach cur_fline or wanted_match */
477 while (1) { /* read chars until we have a line */
479 /* if no unprocessed chars left, eat more */
480 if (readpos >= readeof) {
481 int flags = ndelay_on(0);
487 eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
491 if (t != last_time) {
493 if (--retry_EAGAIN < 0)
498 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
505 c = readbuf[readpos];
506 /* backspace? [needed for manpages] */
507 /* <tab><bs> is (a) insane and */
508 /* (b) harder to do correctly, so we refuse to do it */
509 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
510 readpos++; /* eat it */
512 /* was buggy (p could end up <= current_line)... */
517 size_t new_last_line_pos = last_line_pos + 1;
519 new_last_line_pos += 7;
520 new_last_line_pos &= (~7);
522 if ((int)new_last_line_pos > w)
524 last_line_pos = new_last_line_pos;
526 /* ok, we will eat this char */
533 /* NUL is substituted by '\n'! */
534 if (c == '\0') c = '\n';
537 } /* end of "read chars until we have a line" loop */
539 //BUG: also triggers on this:
540 // { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
541 // (resulting in lost empty line between "foo" and "bar" lines)
542 // the "terminated" logic needs fixing (or explaining)
543 /* Corner case: linewrap with only "" wrapping to next line */
544 /* Looks ugly on screen, so we do not store this empty line */
545 if (!last_terminated && !current_line[0]) {
552 last_terminated = terminated;
553 flines = xrealloc_vector(flines, 8, max_fline);
555 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
556 LINENO(flines[max_fline]) = max_lineno;
560 if (max_fline >= MAXLINES) {
561 eof_error = 0; /* Pretend we saw EOF */
565 #if !ENABLE_FEATURE_LESS_REGEXP
568 if (wanted_match >= num_matches) { /* goto_match called us */
569 fill_match_lines(old_max_fline);
570 old_max_fline = max_fline;
572 if (wanted_match < num_matches)
576 if (eof_error <= 0) {
580 current_line = ((char*)xmalloc(w + 5)) + 4;
583 } /* end of "read lines until we reach cur_fline" loop */
586 if (errno == EAGAIN) {
589 print_statusline(bb_msg_read_error);
592 #if ENABLE_FEATURE_LESS_FLAGS
593 else if (eof_error == 0)
594 num_lines = max_lineno;
597 fill_match_lines(old_max_fline);
598 #if ENABLE_FEATURE_LESS_REGEXP
599 /* prevent us from being stuck in search for a match */
605 #if ENABLE_FEATURE_LESS_FLAGS
606 static int safe_lineno(int fline)
608 if (fline >= max_fline)
609 fline = max_fline - 1;
611 /* also catches empty file (max_fline == 0) */
615 return LINENO(flines[fline]) + 1;
618 /* count number of lines in file */
619 static void update_num_lines(void)
626 /* only do this for regular files */
627 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
629 fd = open("/proc/self/fd/0", O_RDONLY);
630 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
631 /* "filename" is valid only if REOPEN_AND_COUNT */
632 fd = open(filename, O_RDONLY);
635 /* somebody stole my file! */
636 num_lines = NOT_REGULAR_FILE;
639 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
640 num_lines = NOT_REGULAR_FILE;
643 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
644 for (i = 0; i < len; ++i) {
645 if (buf[i] == '\n' && ++count == MAXLINES)
656 /* Print a status line if -M was specified */
657 static void m_status_print(void)
662 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
666 printf(HIGHLIGHT"%s", filename);
668 printf(" (file %i of %i)", current_file, num_files);
670 first = safe_lineno(cur_fline);
671 last = (option_mask32 & FLAG_S)
672 ? MIN(first + max_displayed_line, max_lineno)
673 : safe_lineno(cur_fline + max_displayed_line);
674 printf(" lines %i-%i", first, last);
678 printf("/%i", num_lines);
682 if (num_files > 1 && current_file != num_files)
683 printf(" - next: %s", files[current_file]);
684 } else if (num_lines > 0) {
685 percent = (100 * last + num_lines/2) / num_lines;
686 printf(" %i%%", percent <= 100 ? percent : 100);
692 /* Print the status line */
693 static void status_print(void)
697 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
700 /* Change the status if flags have been set */
701 #if ENABLE_FEATURE_LESS_FLAGS
702 if (option_mask32 & (FLAG_M|FLAG_m)) {
710 if (cur_fline && !at_end()) {
718 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
719 p, current_file, num_files);
725 static const char controls[] ALIGN1 =
726 /* NUL: never encountered; TAB: not converted */
727 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
728 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
729 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
730 static const char ctrlconv[] ALIGN1 =
731 /* why 40 instead of 4a below? - it is a replacement for '\n'.
732 * '\n' is a former NUL - we subst it with @, not J */
733 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
734 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
736 static void print_lineno(const char *line)
738 const char *fmt = " ";
739 unsigned n = n; /* for compiler */
741 if (line != empty_line_marker) {
742 /* Width of 7 preserves tab spacing in the text */
744 n = LINENO(line) + 1;
745 if (n > 9999999 && MAXLINES > 9999999) {
754 #if ENABLE_FEATURE_LESS_REGEXP
755 static void print_found(const char *line)
760 regmatch_t match_structs;
763 const char *str = line;
768 n = strcspn(str, controls);
775 n = strspn(str, controls);
782 /* buf[] holds quarantined version of str */
784 /* Each part of the line that matches has the HIGHLIGHT
785 * and NORMAL escape sequences placed around it.
786 * NB: we regex against line, but insert text
787 * from quarantined copy (buf[]) */
793 while (match_status == 0) {
794 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
795 growline ? growline : "",
796 (int)match_structs.rm_so, str,
797 (int)(match_structs.rm_eo - match_structs.rm_so),
798 str + match_structs.rm_so);
801 str += match_structs.rm_eo;
802 line += match_structs.rm_eo;
805 /* Most of the time doesn't find the regex, optimize for that */
806 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
807 /* if even "" matches, treat it as "not a match" */
808 if (match_structs.rm_so >= match_structs.rm_eo)
812 printf("%s%s\n", growline ? growline : "", str);
816 void print_found(const char *line);
819 static void print_ascii(const char *str)
826 n = strcspn(str, controls);
829 printf("%.*s", (int) n, str);
832 n = strspn(str, controls);
837 else if (*str == (char)0x9b)
838 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
839 /* I want to know who committed this sin */
842 *p++ = ctrlconv[(unsigned char)*str];
851 /* Print the buffer */
852 static void buffer_print(void)
857 for (i = 0; i <= max_displayed_line; i++) {
859 if (option_mask32 & FLAG_N)
860 print_lineno(buffer[i]);
862 print_found(buffer[i]);
864 print_ascii(buffer[i]);
866 if ((option_mask32 & FLAG_E)
868 && (max_fline - cur_fline) <= max_displayed_line
870 less_exit(EXIT_SUCCESS);
875 static void buffer_fill_and_print(void)
878 #if ENABLE_FEATURE_LESS_TRUNCATE
879 int fpos = cur_fline;
881 if (option_mask32 & FLAG_S) {
882 /* Go back to the beginning of this line */
883 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
888 while (i <= max_displayed_line && fpos <= max_fline) {
889 int lineno = LINENO(flines[fpos]);
890 buffer[i] = flines[fpos];
894 } while ((fpos <= max_fline)
895 && (option_mask32 & FLAG_S)
896 && lineno == LINENO(flines[fpos])
900 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
901 buffer[i] = flines[cur_fline + i];
904 for (; i <= max_displayed_line; i++) {
905 buffer[i] = empty_line_marker;
910 /* move cur_fline to a given line number, reading lines if necessary */
911 static void goto_lineno(int target)
916 else if (target > LINENO(flines[cur_fline])) {
918 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
920 /* target not reached but more input is available */
921 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
927 /* search backwards through already-read lines */
928 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
933 static void cap_cur_fline(void)
935 if ((option_mask32 & FLAG_S)) {
936 if (cur_fline > max_fline)
937 cur_fline = max_fline;
938 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
939 goto_lineno(max_lineno - max_displayed_line + TILDES);
944 if (cur_fline + max_displayed_line > max_fline + TILDES)
945 cur_fline = max_fline - max_displayed_line + TILDES;
951 /* Move the buffer up and down in the file in order to scroll */
952 static void buffer_down(int nlines)
954 if ((option_mask32 & FLAG_S))
955 goto_lineno(LINENO(flines[cur_fline]) + nlines);
960 buffer_fill_and_print();
963 static void buffer_up(int nlines)
965 if ((option_mask32 & FLAG_S)) {
966 goto_lineno(LINENO(flines[cur_fline]) - nlines);
974 buffer_fill_and_print();
977 /* display a given line where the argument can be either an index into
978 * the flines array or a line number */
979 static void buffer_to_line(int linenum, int is_lineno)
984 goto_lineno(linenum);
989 buffer_fill_and_print();
992 static void buffer_line(int linenum)
994 buffer_to_line(linenum, FALSE);
997 static void buffer_lineno(int lineno)
999 buffer_to_line(lineno, TRUE);
1002 static void open_file_and_read_lines(void)
1005 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
1006 #if ENABLE_FEATURE_LESS_FLAGS
1007 num_lines = REOPEN_AND_COUNT;
1010 /* "less" with no arguments in argv[] */
1011 /* For status line only */
1012 filename = xstrdup(bb_msg_standard_input);
1013 #if ENABLE_FEATURE_LESS_FLAGS
1014 num_lines = REOPEN_STDIN;
1024 /* Reinitialize everything for a new file - free the memory and start over */
1025 static void reinitialize(void)
1030 for (i = 0; i <= max_fline; i++)
1031 free(MEMPTR(flines[i]));
1039 open_file_and_read_lines();
1040 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
1042 printf(ESC"[999;999H" ESC"[6n");
1044 buffer_fill_and_print();
1047 static int64_t getch_nowait(void)
1051 struct pollfd pfd[2];
1053 pfd[0].fd = STDIN_FILENO;
1054 pfd[0].events = POLLIN;
1056 pfd[1].events = POLLIN;
1058 tcsetattr(kbd_fd, TCSANOW, &term_less);
1059 /* NB: select/poll returns whenever read will not block. Therefore:
1060 * if eof is reached, select/poll will return immediately
1061 * because read will immediately return 0 bytes.
1062 * Even if select/poll says that input is available, read CAN block
1063 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1066 /* Are we interested in stdin? */
1068 if (eof_error > 0) /* did NOT reach eof yet */
1069 rd = 0; /* yes, we are interested in stdin */
1071 /* Position cursor if line input is done */
1072 if (less_gets_pos >= 0)
1073 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
1076 if (kbd_input[0] == 0) { /* if nothing is buffered */
1077 #if ENABLE_FEATURE_LESS_WINCH
1080 /* NB: SIGWINCH interrupts poll() */
1081 r = poll(pfd + rd, 2 - rd, -1);
1082 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1083 return '\\'; /* anything which has no defined function */
1087 safe_poll(pfd + rd, 2 - rd, -1);
1091 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
1092 * would not block even if there is no input available */
1093 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
1094 if ((int)key64 == -1) {
1095 if (errno == EAGAIN) {
1096 /* No keyboard input available. Since poll() did return,
1097 * we should have input on stdin */
1099 buffer_fill_and_print();
1102 /* EOF/error (ssh session got killed etc) */
1103 less_exit(EXIT_SUCCESS);
1109 /* Grab a character from input without requiring the return key.
1110 * May return KEYCODE_xxx values.
1111 * Note that this function works best with raw input. */
1112 static int64_t less_getch(int pos)
1118 less_gets_pos = pos;
1119 key = key64 = getch_nowait();
1122 /* Discard Ctrl-something chars.
1123 * (checking only lower 32 bits is a size optimization:
1124 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1126 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
1132 static char* less_gets(int sz)
1136 char *result = xzalloc(1);
1140 less_gets_pos = sz + i;
1153 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
1155 if (i >= width - sz - 1)
1156 continue; /* len limit */
1159 result = xrealloc(result, i+1);
1163 static void examine_file(void)
1167 print_statusline("Examine: ");
1168 new_fname = less_gets(sizeof("Examine: ") - 1);
1169 if (!new_fname[0]) {
1175 if (access(new_fname, R_OK) != 0) {
1176 print_statusline("Cannot read this file");
1180 filename = new_fname;
1181 /* files start by = argv. why we assume that argv is infinitely long??
1182 files[num_files] = filename;
1183 current_file = num_files + 1;
1185 files[0] = filename;
1186 num_files = current_file = 1;
1190 /* This function changes the file currently being paged. direction can be one of the following:
1191 * -1: go back one file
1192 * 0: go to the first file
1193 * 1: go forward one file */
1194 static void change_file(int direction)
1196 if (current_file != ((direction > 0) ? num_files : 1)) {
1197 current_file = direction ? current_file + direction : 1;
1199 filename = xstrdup(files[current_file - 1]);
1202 print_statusline(direction > 0 ? "No next file" : "No previous file");
1206 static void remove_current_file(void)
1213 if (current_file != 1) {
1215 for (i = 3; i <= num_files; i++)
1216 files[i - 2] = files[i - 1];
1220 for (i = 2; i <= num_files; i++)
1221 files[i - 2] = files[i - 1];
1227 static void colon_process(void)
1231 /* Clear the current line and print a prompt */
1232 print_statusline(" :");
1234 keypress = less_getch(2);
1237 remove_current_file();
1242 #if ENABLE_FEATURE_LESS_FLAGS
1254 less_exit(EXIT_SUCCESS);
1262 #if ENABLE_FEATURE_LESS_REGEXP
1263 static void normalize_match_pos(int match)
1265 if (match >= num_matches)
1266 match = num_matches - 1;
1272 static void goto_match(int match)
1278 /* Try to find next match if eof isn't reached yet */
1279 if (match >= num_matches && eof_error > 0) {
1280 wanted_match = match; /* "I want to read until I see N'th match" */
1284 normalize_match_pos(match);
1285 buffer_line(match_lines[match_pos]);
1287 print_statusline("No matches found");
1291 static void fill_match_lines(unsigned pos)
1295 /* Run the regex on each line of the current file */
1296 while (pos <= max_fline) {
1297 /* If this line matches */
1298 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1299 /* and we didn't match it last time */
1300 && !(num_matches && match_lines[num_matches-1] == pos)
1302 match_lines = xrealloc_vector(match_lines, 4, num_matches);
1303 match_lines[num_matches++] = pos;
1309 static void regex_process(void)
1311 char *uncomp_regex, *err;
1313 /* Reset variables */
1318 if (pattern_valid) {
1323 /* Get the uncompiled regular expression from the user */
1325 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
1326 uncomp_regex = less_gets(1);
1327 if (!uncomp_regex[0]) {
1333 /* Compile the regex and check for errors */
1334 err = regcomp_or_errmsg(&pattern, uncomp_regex,
1335 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
1338 print_statusline(err);
1345 fill_match_lines(0);
1346 while (match_pos < num_matches) {
1347 if ((int)match_lines[match_pos] > cur_fline)
1351 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1354 /* It's possible that no matches are found yet.
1355 * goto_match() will read input looking for match,
1357 goto_match(match_pos);
1361 static void number_process(int first_digit)
1366 char num_input[sizeof(int)*4]; /* more than enough */
1368 num_input[0] = first_digit;
1370 /* Clear the current line, print a prompt, and then print the digit */
1372 printf(":%c", first_digit);
1374 /* Receive input until a letter is given */
1376 while (i < sizeof(num_input)-1) {
1377 keypress = less_getch(i + 1);
1378 if ((unsigned)keypress > 255 || !isdigit(keypress))
1380 num_input[i] = keypress;
1381 bb_putchar(keypress);
1385 num_input[i] = '\0';
1386 num = bb_strtou(num_input, NULL, 10);
1387 /* on format error, num == -1 */
1388 if (num < 1 || num > MAXLINES) {
1393 /* We now know the number and the letter entered, so we process them */
1395 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
1398 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
1401 case 'g': case '<': case 'G': case '>':
1402 buffer_lineno(num - 1);
1405 #if ENABLE_FEATURE_LESS_FLAGS
1407 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1409 num = num * max_lineno / 100;
1413 #if ENABLE_FEATURE_LESS_REGEXP
1415 goto_match(match_pos + num);
1418 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1422 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1429 #if ENABLE_FEATURE_LESS_DASHCMD
1430 static void flag_change(void)
1436 keypress = less_getch(1);
1440 option_mask32 ^= FLAG_M;
1443 option_mask32 ^= FLAG_m;
1446 option_mask32 ^= FLAG_E;
1449 option_mask32 ^= FLAG_TILDE;
1451 #if ENABLE_FEATURE_LESS_TRUNCATE
1453 option_mask32 ^= FLAG_S;
1454 buffer_fill_and_print();
1457 #if ENABLE_FEATURE_LESS_LINENUMS
1459 option_mask32 ^= FLAG_N;
1461 buffer_fill_and_print();
1468 static void show_flag_status(void)
1475 keypress = less_getch(1);
1479 flag_val = option_mask32 & FLAG_M;
1482 flag_val = option_mask32 & FLAG_m;
1485 flag_val = option_mask32 & FLAG_TILDE;
1488 flag_val = option_mask32 & FLAG_N;
1491 flag_val = option_mask32 & FLAG_E;
1499 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
1503 #endif /* ENABLE_FEATURE_LESS_DASHCMD */
1505 static void save_input_to_file(void)
1507 const char *msg = "";
1512 print_statusline("Log file: ");
1513 current_line = less_gets(sizeof("Log file: ")-1);
1514 if (current_line[0]) {
1515 fp = fopen_for_write(current_line);
1517 msg = "Error opening log file";
1520 for (i = 0; i <= max_fline; i++)
1521 fprintf(fp, "%s\n", flines[i]);
1526 print_statusline(msg);
1530 #if ENABLE_FEATURE_LESS_MARKS
1531 static void add_mark(void)
1535 print_statusline("Mark: ");
1536 letter = less_getch(sizeof("Mark: ") - 1);
1538 if (isalpha(letter)) {
1539 /* If we exceed 15 marks, start overwriting previous ones */
1540 if (num_marks == 14)
1543 mark_lines[num_marks][0] = letter;
1544 mark_lines[num_marks][1] = cur_fline;
1547 print_statusline("Invalid mark letter");
1551 static void goto_mark(void)
1556 print_statusline("Go to mark: ");
1557 letter = less_getch(sizeof("Go to mark: ") - 1);
1560 if (isalpha(letter)) {
1561 for (i = 0; i <= num_marks; i++)
1562 if (letter == mark_lines[i][0]) {
1563 buffer_line(mark_lines[i][1]);
1566 if (num_marks == 14 && letter != mark_lines[14][0])
1567 print_statusline("Mark not set");
1569 print_statusline("Invalid mark letter");
1573 #if ENABLE_FEATURE_LESS_BRACKETS
1574 static char opp_bracket(char bracket)
1577 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1579 case '(': /* ')' == '(' + 1 */
1591 static void match_right_bracket(char bracket)
1593 unsigned i = cur_fline;
1596 || strchr(flines[i], bracket) == NULL
1598 print_statusline("No bracket in top line");
1602 bracket = opp_bracket(bracket);
1603 for (; i < max_fline; i++) {
1604 if (strchr(flines[i], bracket) != NULL) {
1606 * Line with matched right bracket becomes
1609 buffer_line(i - max_displayed_line);
1613 print_statusline("No matching bracket found");
1616 static void match_left_bracket(char bracket)
1618 int i = cur_fline + max_displayed_line;
1621 || strchr(flines[i], bracket) == NULL
1623 print_statusline("No bracket in bottom line");
1627 bracket = opp_bracket(bracket);
1628 for (; i >= 0; i--) {
1629 if (strchr(flines[i], bracket) != NULL) {
1631 * Line with matched left bracket becomes
1632 * first visible line
1638 print_statusline("No matching bracket found");
1640 #endif /* FEATURE_LESS_BRACKETS */
1642 static void keypress_process(int keypress)
1645 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
1648 case KEYCODE_UP: case 'y': case 'k':
1651 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
1652 buffer_down(max_displayed_line + 1);
1654 case KEYCODE_PAGEUP: case 'w': case 'b':
1655 buffer_up(max_displayed_line + 1);
1658 buffer_down((max_displayed_line + 1) / 2);
1661 buffer_up((max_displayed_line + 1) / 2);
1663 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
1666 case KEYCODE_END: case 'G': case '>':
1667 cur_fline = MAXLINES;
1669 buffer_line(cur_fline);
1672 less_exit(EXIT_SUCCESS);
1674 #if ENABLE_FEATURE_LESS_MARKS
1685 /* TODO: (1) also bind ^R, ^L to this?
1686 * (2) re-measure window size?
1694 save_input_to_file();
1699 #if ENABLE_FEATURE_LESS_FLAGS
1704 #if ENABLE_FEATURE_LESS_REGEXP
1706 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1710 goto_match(match_pos + 1);
1713 goto_match(match_pos - 1);
1716 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1720 #if ENABLE_FEATURE_LESS_DASHCMD
1731 #if ENABLE_FEATURE_LESS_BRACKETS
1732 case '{': case '(': case '[':
1733 match_right_bracket(keypress);
1735 case '}': case ')': case ']':
1736 match_left_bracket(keypress);
1744 if (isdigit(keypress))
1745 number_process(keypress);
1748 static void sig_catcher(int sig)
1753 #if ENABLE_FEATURE_LESS_WINCH
1754 static void sigwinch_handler(int sig UNUSED_PARAM)
1760 int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1761 int less_main(int argc, char **argv)
1768 /* TODO: -x: do not interpret backspace, -xx: tab also
1769 * -xxx: newline also
1770 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1771 * -s: condense many empty lines to one
1772 * (used by some setups for manpage display)
1774 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s");
1776 num_files = argc - optind;
1779 /* Another popular pager, most, detects when stdout
1780 * is not a tty and turns into cat. This makes sense. */
1781 if (!isatty(STDOUT_FILENO))
1782 return bb_cat(argv);
1785 if (isatty(STDIN_FILENO)) {
1786 /* Just "less"? No args and no redirection? */
1787 bb_error_msg("missing filename");
1791 filename = xstrdup(files[0]);
1794 if (option_mask32 & FLAG_TILDE)
1795 empty_line_marker = "";
1797 /* Some versions of less can survive w/o controlling tty,
1798 * try to do the same. This also allows to specify an alternative
1799 * tty via "less 1<>TTY".
1801 * We prefer not to use STDOUT_FILENO directly,
1802 * since we want to set this fd to non-blocking mode,
1803 * and not interfere with other processes which share stdout with us.
1805 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1807 tty_fd = open(tty_name, O_RDONLY);
1812 /* Try controlling tty */
1814 tty_fd = open(CURRENT_TTY, O_RDONLY);
1816 /* If all else fails, less 481 uses stdout. Mimic that */
1817 tty_fd = STDOUT_FILENO;
1820 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
1821 kbd_fd = tty_fd; /* save in a global */
1823 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL);
1825 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
1826 /* 20: two tabstops + 4 */
1827 if (width < 20 || max_displayed_line < 3)
1828 return bb_cat(argv);
1829 max_displayed_line -= 2;
1831 /* We want to restore term_orig on exit */
1832 bb_signals(BB_FATAL_SIGS, sig_catcher);
1833 #if ENABLE_FEATURE_LESS_WINCH
1834 signal(SIGWINCH, sigwinch_handler);
1837 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
1842 #if ENABLE_FEATURE_LESS_WINCH
1843 while (WINCH_COUNTER) {
1846 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1847 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
1848 /* 20: two tabstops + 4 */
1851 if (max_displayed_line < 3)
1852 max_displayed_line = 3;
1853 max_displayed_line -= 2;
1855 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
1856 /* Avoid re-wrap and/or redraw if we already know
1857 * we need to do it again. These ops are expensive */
1863 buffer_fill_and_print();
1864 /* This took some time. Loop back and check,
1865 * were there another SIGWINCH? */
1867 keypress = less_getch(-1); /* -1: do not position cursor */
1868 # if ENABLE_FEATURE_LESS_ASK_TERMINAL
1869 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1870 uint32_t rc = (keypress >> 32);
1871 width = (rc & 0x7fff);
1872 max_displayed_line = ((rc >> 16) & 0x7fff);
1877 keypress = less_getch(-1); /* -1: do not position cursor */
1879 keypress_process(keypress);
1884 Help text of less version 418 is below.
1885 If you are implementing something, keeping
1886 key and/or command line switch compatibility is a good idea:
1889 SUMMARY OF LESS COMMANDS
1891 Commands marked with * may be preceded by a number, N.
1892 Notes in parentheses indicate the behavior if N is given.
1893 h H Display this help.
1895 ---------------------------------------------------------------------------
1897 e ^E j ^N CR * Forward one line (or N lines).
1898 y ^Y k ^K ^P * Backward one line (or N lines).
1899 f ^F ^V SPACE * Forward one window (or N lines).
1900 b ^B ESC-v * Backward one window (or N lines).
1901 z * Forward one window (and set window to N).
1902 w * Backward one window (and set window to N).
1903 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1904 d ^D * Forward one half-window (and set half-window to N).
1905 u ^U * Backward one half-window (and set half-window to N).
1906 ESC-) RightArrow * Left one half screen width (or N positions).
1907 ESC-( LeftArrow * Right one half screen width (or N positions).
1908 F Forward forever; like "tail -f".
1909 r ^R ^L Repaint screen.
1910 R Repaint screen, discarding buffered input.
1911 ---------------------------------------------------
1912 Default "window" is the screen height.
1913 Default "half-window" is half of the screen height.
1914 ---------------------------------------------------------------------------
1916 /pattern * Search forward for (N-th) matching line.
1917 ?pattern * Search backward for (N-th) matching line.
1918 n * Repeat previous search (for N-th occurrence).
1919 N * Repeat previous search in reverse direction.
1920 ESC-n * Repeat previous search, spanning files.
1921 ESC-N * Repeat previous search, reverse dir. & spanning files.
1922 ESC-u Undo (toggle) search highlighting.
1923 ---------------------------------------------------
1924 Search patterns may be modified by one or more of:
1925 ^N or ! Search for NON-matching lines.
1926 ^E or * Search multiple files (pass thru END OF FILE).
1927 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1928 ^K Highlight matches, but don't move (KEEP position).
1929 ^R Don't use REGULAR EXPRESSIONS.
1930 ---------------------------------------------------------------------------
1932 g < ESC-< * Go to first line in file (or line N).
1933 G > ESC-> * Go to last line in file (or line N).
1934 p % * Go to beginning of file (or N percent into file).
1935 t * Go to the (N-th) next tag.
1936 T * Go to the (N-th) previous tag.
1937 { ( [ * Find close bracket } ) ].
1938 } ) ] * Find open bracket { ( [.
1939 ESC-^F <c1> <c2> * Find close bracket <c2>.
1940 ESC-^B <c1> <c2> * Find open bracket <c1>
1941 ---------------------------------------------------
1942 Each "find close bracket" command goes forward to the close bracket
1943 matching the (N-th) open bracket in the top line.
1944 Each "find open bracket" command goes backward to the open bracket
1945 matching the (N-th) close bracket in the bottom line.
1946 m<letter> Mark the current position with <letter>.
1947 '<letter> Go to a previously marked position.
1948 '' Go to the previous position.
1950 ---------------------------------------------------
1951 A mark is any upper-case or lower-case letter.
1952 Certain marks are predefined:
1953 ^ means beginning of the file
1954 $ means end of the file
1955 ---------------------------------------------------------------------------
1957 :e [file] Examine a new file.
1959 :n * Examine the (N-th) next file from the command line.
1960 :p * Examine the (N-th) previous file from the command line.
1961 :x * Examine the first (or N-th) file from the command line.
1962 :d Delete the current file from the command line list.
1963 = ^G :f Print current file name.
1964 ---------------------------------------------------------------------------
1965 MISCELLANEOUS COMMANDS
1966 -<flag> Toggle a command line option [see OPTIONS below].
1967 --<name> Toggle a command line option, by name.
1968 _<flag> Display the setting of a command line option.
1969 __<name> Display the setting of an option, by name.
1970 +cmd Execute the less cmd each time a new file is examined.
1971 !command Execute the shell command with $SHELL.
1972 |Xcommand Pipe file between current pos & mark X to shell command.
1973 v Edit the current file with $VISUAL or $EDITOR.
1974 V Print version number of "less".
1975 ---------------------------------------------------------------------------
1977 Most options may be changed either on the command line,
1978 or from within less by using the - or -- command.
1979 Options may be given in one of two forms: either a single
1980 character preceded by a -, or a name preceded by --.
1982 Display help (from command line).
1983 -a ........ --search-skip-screen
1984 Forward search skips current screen.
1985 -b [N] .... --buffers=[N]
1987 -B ........ --auto-buffers
1988 Don't automatically allocate buffers for pipes.
1989 -c ........ --clear-screen
1990 Repaint by clearing rather than scrolling.
1993 -D [xn.n] . --color=xn.n
1994 Set screen colors. (MS-DOS only)
1995 -e -E .... --quit-at-eof --QUIT-AT-EOF
1996 Quit at end of file.
1998 Force open non-regular files.
1999 -F ........ --quit-if-one-screen
2000 Quit if entire file fits on first screen.
2001 -g ........ --hilite-search
2002 Highlight only last match for searches.
2003 -G ........ --HILITE-SEARCH
2004 Don't highlight any matches for searches.
2005 -h [N] .... --max-back-scroll=[N]
2006 Backward scroll limit.
2007 -i ........ --ignore-case
2008 Ignore case in searches that do not contain uppercase.
2009 -I ........ --IGNORE-CASE
2010 Ignore case in all searches.
2011 -j [N] .... --jump-target=[N]
2012 Screen position of target lines.
2013 -J ........ --status-column
2014 Display a status column at left edge of screen.
2015 -k [file] . --lesskey-file=[file]
2017 -L ........ --no-lessopen
2018 Ignore the LESSOPEN environment variable.
2019 -m -M .... --long-prompt --LONG-PROMPT
2021 -n -N .... --line-numbers --LINE-NUMBERS
2022 Don't use line numbers.
2023 -o [file] . --log-file=[file]
2024 Copy to log file (standard input only).
2025 -O [file] . --LOG-FILE=[file]
2026 Copy to log file (unconditionally overwrite).
2027 -p [pattern] --pattern=[pattern]
2028 Start at pattern (from command line).
2029 -P [prompt] --prompt=[prompt]
2031 -q -Q .... --quiet --QUIET --silent --SILENT
2032 Quiet the terminal bell.
2033 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2034 Output "raw" control characters.
2035 -s ........ --squeeze-blank-lines
2036 Squeeze multiple blank lines.
2037 -S ........ --chop-long-lines
2039 -t [tag] .. --tag=[tag]
2041 -T [tagsfile] --tag-file=[tagsfile]
2042 Use an alternate tags file.
2043 -u -U .... --underline-special --UNDERLINE-SPECIAL
2044 Change handling of backspaces.
2045 -V ........ --version
2046 Display the version number of "less".
2047 -w ........ --hilite-unread
2048 Highlight first new line after forward-screen.
2049 -W ........ --HILITE-UNREAD
2050 Highlight first new line after any forward movement.
2051 -x [N[,...]] --tabs=[N[,...]]
2053 -X ........ --no-init
2054 Don't use termcap init/deinit strings.
2056 Don't use termcap keypad init/deinit strings.
2057 -y [N] .... --max-forw-scroll=[N]
2058 Forward scroll limit.
2059 -z [N] .... --window=[N]
2061 -" [c[c]] . --quotes=[c[c]]
2062 Set shell quote characters.
2064 Don't display tildes after end of file.
2065 -# [N] .... --shift=[N]
2066 Horizontal scroll amount (0 = one half screen width)
2068 ---------------------------------------------------------------------------
2070 These keys can be used to edit text being entered
2071 on the "command line" at the bottom of the screen.
2072 RightArrow ESC-l Move cursor right one character.
2073 LeftArrow ESC-h Move cursor left one character.
2074 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2075 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2076 HOME ESC-0 Move cursor to start of line.
2077 END ESC-$ Move cursor to end of line.
2078 BACKSPACE Delete char to left of cursor.
2079 DELETE ESC-x Delete char under cursor.
2080 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2081 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2082 CNTL-U ESC (MS-DOS only) Delete entire line.
2083 UpArrow ESC-k Retrieve previous command line.
2084 DownArrow ESC-j Retrieve next command line.
2085 TAB Complete filename & cycle.
2086 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2087 CNTL-L Complete filename, list all.