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 //config:config FEATURE_LESS_RAW
104 //config: bool "Enable -R ('raw control characters')"
106 //config: depends on FEATURE_LESS_DASHCMD
108 //config: This is essential for less applet to work with tools that use colors
109 //config: and paging, such as git, systemd tools or nmcli.
111 //config:config FEATURE_LESS_ENV
112 //config: bool "Take options from $LESS environment variable"
114 //config: depends on FEATURE_LESS_DASHCMD
116 //config: This is essential for less applet to work with tools that use colors
117 //config: and paging, such as git, systemd tools or nmcli.
119 //applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP))
121 //kbuild:lib-$(CONFIG_LESS) += less.o
123 //usage:#define less_trivial_usage
124 //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
125 //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") IF_FEATURE_LESS_RAW("R") "h~] [FILE]..."
126 //usage:#define less_full_usage "\n\n"
127 //usage: "View FILE (or stdin) one screenful at a time\n"
128 //usage: "\n -E Quit once the end of a file is reached"
129 //usage: IF_FEATURE_LESS_REGEXP(
130 //usage: "\n -I Ignore case in all searches"
132 //usage: IF_FEATURE_LESS_FLAGS(
133 //usage: "\n -M,-m Display status line with line numbers"
134 //usage: "\n and percentage through the file"
136 //usage: "\n -N Prefix line number to each line"
137 //usage: IF_FEATURE_LESS_TRUNCATE(
138 //usage: "\n -S Truncate long lines"
140 //usage: IF_FEATURE_LESS_RAW(
141 //usage: "\n -R Remove color escape codes in input"
143 //usage: "\n -~ Suppress ~s displayed past EOF"
145 #include <sched.h> /* sched_yield() */
148 #include "common_bufsiz.h"
149 #if ENABLE_FEATURE_LESS_REGEXP
155 /* The escape codes for highlighted and normal text */
156 #define HIGHLIGHT ESC"[7m"
157 #define NORMAL ESC"[m"
158 /* The escape code to home and clear to the end of screen */
159 #define CLEAR ESC"[H"ESC"[J"
160 /* The escape code to clear to the end of line */
161 #define CLEAR_2_EOL ESC"[K"
164 /* Absolute max of lines eaten */
165 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
166 /* This many "after the end" lines we will show (at max) */
170 /* Command line options */
178 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE,
179 FLAG_R = (1 << 7) * ENABLE_FEATURE_LESS_RAW,
180 /* hijack command line options variable for internal state vars */
181 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
184 #if !ENABLE_FEATURE_LESS_REGEXP
185 enum { pattern_valid = 0 };
189 int cur_fline; /* signed */
190 int kbd_fd; /* fd to get input from */
191 int kbd_fd_orig_flags;
193 /* last position in last line, taking into account tabs */
194 size_t last_line_pos;
196 unsigned max_lineno; /* this one tracks linewrap */
197 unsigned max_displayed_line;
199 #if ENABLE_FEATURE_LESS_WINCH
200 unsigned winch_counter;
202 ssize_t eof_error; /* eof if 0, error if < 0 */
204 ssize_t readeof; /* must be signed */
207 const char *empty_line_marker;
209 unsigned current_file;
212 #if ENABLE_FEATURE_LESS_FLAGS
213 int num_lines; /* a flag if < 0, line count if >= 0 */
214 # define REOPEN_AND_COUNT (-1)
215 # define REOPEN_STDIN (-2)
216 # define NOT_REGULAR_FILE (-3)
218 #if ENABLE_FEATURE_LESS_MARKS
220 unsigned mark_lines[15][2];
222 #if ENABLE_FEATURE_LESS_REGEXP
223 unsigned *match_lines;
224 int match_pos; /* signed! */
225 int wanted_match; /* signed! */
228 smallint pattern_valid;
230 #if ENABLE_FEATURE_LESS_RAW
233 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
234 smallint winsize_err;
237 struct termios term_orig, term_less;
238 char kbd_input[KEYCODE_BUFFER_SIZE];
240 #define G (*ptr_to_globals)
241 #define cur_fline (G.cur_fline )
242 #define kbd_fd (G.kbd_fd )
243 #define less_gets_pos (G.less_gets_pos )
244 #define last_line_pos (G.last_line_pos )
245 #define max_fline (G.max_fline )
246 #define max_lineno (G.max_lineno )
247 #define max_displayed_line (G.max_displayed_line)
248 #define width (G.width )
249 #define winch_counter (G.winch_counter )
250 /* This one is 100% not cached by compiler on read access */
251 #define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
252 #define eof_error (G.eof_error )
253 #define readpos (G.readpos )
254 #define readeof (G.readeof )
255 #define buffer (G.buffer )
256 #define flines (G.flines )
257 #define empty_line_marker (G.empty_line_marker )
258 #define num_files (G.num_files )
259 #define current_file (G.current_file )
260 #define filename (G.filename )
261 #define files (G.files )
262 #define num_lines (G.num_lines )
263 #define num_marks (G.num_marks )
264 #define mark_lines (G.mark_lines )
265 #if ENABLE_FEATURE_LESS_REGEXP
266 #define match_lines (G.match_lines )
267 #define match_pos (G.match_pos )
268 #define num_matches (G.num_matches )
269 #define wanted_match (G.wanted_match )
270 #define pattern (G.pattern )
271 #define pattern_valid (G.pattern_valid )
273 #define terminated (G.terminated )
274 #define term_orig (G.term_orig )
275 #define term_less (G.term_less )
276 #define kbd_input (G.kbd_input )
277 #define INIT_G() do { \
278 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
279 less_gets_pos = -1; \
280 empty_line_marker = "~"; \
284 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
287 /* flines[] are lines read from stdin, each in malloc'ed buffer.
288 * Line numbers are stored as uint32_t prepended to each line.
289 * Pointer is adjusted so that flines[i] points directly past
290 * line number. Accessor: */
291 #define MEMPTR(p) ((char*)(p) - 4)
292 #define LINENO(p) (*(uint32_t*)((p) - 4))
295 /* Reset terminal input to normal */
296 static void set_tty_cooked(void)
299 tcsetattr(kbd_fd, TCSANOW, &term_orig);
302 /* Move the cursor to a position (x,y), where (0,0) is the
303 top-left corner of the console */
304 static void move_cursor(int line, int col)
306 printf(ESC"[%u;%uH", line, col);
309 static void clear_line(void)
311 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
314 static void print_hilite(const char *str)
316 printf(HIGHLIGHT"%s"NORMAL, str);
319 static void print_statusline(const char *str)
322 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
325 /* Exit the program gracefully */
326 static void less_exit(int code)
329 if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
333 kill_myself_with_sig(- code); /* does not return */
337 #if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
338 || ENABLE_FEATURE_LESS_WINCH
339 static void re_wrap(void)
345 int new_cur_fline = 0;
348 const char **old_flines = flines;
350 char **new_flines = NULL;
353 if (option_mask32 & FLAG_N)
366 if (*d == '\t') { /* tab */
368 new_line_pos &= (~7);
372 if (new_line_pos >= w) {
374 /* new line is full, create next one */
377 sz = (d - linebuf) + 1; /* + 1: NUL */
378 d = ((char*)xmalloc(sz + 4)) + 4;
380 memcpy(d, linebuf, sz);
381 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
382 new_flines[dst_idx] = d;
384 if (new_line_pos < w) {
385 /* if we came here thru "goto next_new" */
386 if (src_idx > max_fline)
395 /* *d == NUL: old line ended, go to next old one */
396 free(MEMPTR(old_flines[src_idx]));
397 /* btw, convert cur_fline... */
398 if (cur_fline == src_idx)
399 new_cur_fline = dst_idx;
401 /* no more lines? finish last new line (and exit the loop) */
402 if (src_idx > max_fline)
404 s = old_flines[src_idx];
405 if (lineno != LINENO(s)) {
406 /* this is not a continuation line!
407 * create next _new_ line too */
413 flines = (const char **)new_flines;
415 max_fline = dst_idx - 1;
416 last_line_pos = new_line_pos;
417 cur_fline = new_cur_fline;
418 /* max_lineno is screen-size independent */
419 #if ENABLE_FEATURE_LESS_REGEXP
425 #if ENABLE_FEATURE_LESS_REGEXP
426 static void fill_match_lines(unsigned pos);
428 #define fill_match_lines(pos) ((void)0)
431 static int at_end(void)
433 return (option_mask32 & FLAG_S)
434 ? !(cur_fline <= max_fline &&
435 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
436 : !(max_fline > cur_fline + max_displayed_line);
439 /* Devilishly complex routine.
441 * Has to deal with EOF and EPIPE on input,
442 * with line wrapping, with last line not ending in '\n'
443 * (possibly not ending YET!), with backspace and tabs.
444 * It reads input again if last time we got an EOF (thus supporting
445 * growing files) or EPIPE (watching output of slow process like make).
448 * flines[] - array of lines already read. Linewrap may cause
449 * one source file line to occupy several flines[n].
450 * flines[max_fline] - last line, possibly incomplete.
451 * terminated - 1 if flines[max_fline] is 'terminated'
452 * (if there was '\n' [which isn't stored itself, we just remember
454 * max_lineno - last line's number, this one doesn't increment
455 * on line wrap, only on "real" new lines.
456 * readbuf[0..readeof-1] - small preliminary buffer.
457 * readbuf[readpos] - next character to add to current line.
458 * last_line_pos - screen line position of next char to be read
459 * (takes into account tabs and backspaces)
460 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
462 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
463 * "/search on very long input" and "reaching max line count" corner cases.
465 static void read_lines(void)
467 char *current_line, *p;
469 char last_terminated = terminated;
470 time_t last_time = 0;
471 int retry_EAGAIN = 2;
472 #if ENABLE_FEATURE_LESS_REGEXP
473 unsigned old_max_fline = max_fline;
476 #define readbuf bb_common_bufsiz1
477 setup_common_bufsiz();
479 /* (careful: max_fline can be -1) */
480 if (max_fline + 1 > MAXLINES)
483 if (option_mask32 & FLAG_N)
486 p = current_line = ((char*)xmalloc(w + 5)) + 4;
487 if (!last_terminated) {
488 const char *cp = flines[max_fline];
491 /* last_line_pos is still valid from previous read_lines() */
497 while (1) { /* read lines until we reach cur_fline or wanted_match */
500 while (1) { /* read chars until we have a line */
502 /* if no unprocessed chars left, eat more */
503 if (readpos >= readeof) {
504 int flags = ndelay_on(0);
510 eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
514 if (t != last_time) {
516 if (--retry_EAGAIN < 0)
521 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
528 c = readbuf[readpos];
529 /* backspace? [needed for manpages] */
530 /* <tab><bs> is (a) insane and */
531 /* (b) harder to do correctly, so we refuse to do it */
532 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
533 readpos++; /* eat it */
535 /* was buggy (p could end up <= current_line)... */
539 #if ENABLE_FEATURE_LESS_RAW
540 if (option_mask32 & FLAG_R) {
550 G.in_escape = (c != 'm');
554 /* Hmm, unexpected end of "ESC [ N ; N m" sequence */
560 size_t new_last_line_pos = last_line_pos + 1;
562 new_last_line_pos += 7;
563 new_last_line_pos &= (~7);
565 if ((int)new_last_line_pos > w)
567 last_line_pos = new_last_line_pos;
569 /* ok, we will eat this char */
576 /* NUL is substituted by '\n'! */
577 if (c == '\0') c = '\n';
580 } /* end of "read chars until we have a line" loop */
582 //BUG: also triggers on this:
583 // { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
584 // (resulting in lost empty line between "foo" and "bar" lines)
585 // the "terminated" logic needs fixing (or explaining)
586 /* Corner case: linewrap with only "" wrapping to next line */
587 /* Looks ugly on screen, so we do not store this empty line */
588 if (!last_terminated && !current_line[0]) {
595 last_terminated = terminated;
596 flines = xrealloc_vector(flines, 8, max_fline);
598 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
599 LINENO(flines[max_fline]) = max_lineno;
603 if (max_fline >= MAXLINES) {
604 eof_error = 0; /* Pretend we saw EOF */
608 #if !ENABLE_FEATURE_LESS_REGEXP
611 if (wanted_match >= num_matches) { /* goto_match called us */
612 fill_match_lines(old_max_fline);
613 old_max_fline = max_fline;
615 if (wanted_match < num_matches)
619 if (eof_error <= 0) {
623 current_line = ((char*)xmalloc(w + 5)) + 4;
626 } /* end of "read lines until we reach cur_fline" loop */
629 if (errno == EAGAIN) {
632 print_statusline(bb_msg_read_error);
635 #if ENABLE_FEATURE_LESS_FLAGS
636 else if (eof_error == 0)
637 num_lines = max_lineno;
640 fill_match_lines(old_max_fline);
641 #if ENABLE_FEATURE_LESS_REGEXP
642 /* prevent us from being stuck in search for a match */
648 #if ENABLE_FEATURE_LESS_FLAGS
649 static int safe_lineno(int fline)
651 if (fline >= max_fline)
652 fline = max_fline - 1;
654 /* also catches empty file (max_fline == 0) */
658 return LINENO(flines[fline]) + 1;
661 /* count number of lines in file */
662 static void update_num_lines(void)
669 /* only do this for regular files */
670 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
672 fd = open("/proc/self/fd/0", O_RDONLY);
673 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
674 /* "filename" is valid only if REOPEN_AND_COUNT */
675 fd = open(filename, O_RDONLY);
678 /* somebody stole my file! */
679 num_lines = NOT_REGULAR_FILE;
682 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
683 num_lines = NOT_REGULAR_FILE;
686 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
687 for (i = 0; i < len; ++i) {
688 if (buf[i] == '\n' && ++count == MAXLINES)
699 /* Print a status line if -M was specified */
700 static void m_status_print(void)
705 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
709 printf(HIGHLIGHT"%s", filename);
711 printf(" (file %i of %i)", current_file, num_files);
713 first = safe_lineno(cur_fline);
714 last = (option_mask32 & FLAG_S)
715 ? MIN(first + max_displayed_line, max_lineno)
716 : safe_lineno(cur_fline + max_displayed_line);
717 printf(" lines %i-%i", first, last);
721 printf("/%i", num_lines);
725 if (num_files > 1 && current_file != num_files)
726 printf(" - next: %s", files[current_file]);
727 } else if (num_lines > 0) {
728 percent = (100 * last + num_lines/2) / num_lines;
729 printf(" %i%%", percent <= 100 ? percent : 100);
735 /* Print the status line */
736 static void status_print(void)
740 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
743 /* Change the status if flags have been set */
744 #if ENABLE_FEATURE_LESS_FLAGS
745 if (option_mask32 & (FLAG_M|FLAG_m)) {
753 if (cur_fline && !at_end()) {
761 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
762 p, current_file, num_files);
768 static const char controls[] ALIGN1 =
769 /* NUL: never encountered; TAB: not converted */
770 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
771 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
772 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
773 static const char ctrlconv[] ALIGN1 =
774 /* why 40 instead of 4a below? - it is a replacement for '\n'.
775 * '\n' is a former NUL - we subst it with @, not J */
776 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
777 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
779 static void print_lineno(const char *line)
781 const char *fmt = " ";
782 unsigned n = n; /* for compiler */
784 if (line != empty_line_marker) {
785 /* Width of 7 preserves tab spacing in the text */
787 n = LINENO(line) + 1;
788 if (n > 9999999 && MAXLINES > 9999999) {
797 #if ENABLE_FEATURE_LESS_REGEXP
798 static void print_found(const char *line)
803 regmatch_t match_structs;
806 const char *str = line;
811 n = strcspn(str, controls);
818 n = strspn(str, controls);
825 /* buf[] holds quarantined version of str */
827 /* Each part of the line that matches has the HIGHLIGHT
828 * and NORMAL escape sequences placed around it.
829 * NB: we regex against line, but insert text
830 * from quarantined copy (buf[]) */
836 while (match_status == 0) {
837 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
838 growline ? growline : "",
839 (int)match_structs.rm_so, str,
840 (int)(match_structs.rm_eo - match_structs.rm_so),
841 str + match_structs.rm_so);
844 str += match_structs.rm_eo;
845 line += match_structs.rm_eo;
848 /* Most of the time doesn't find the regex, optimize for that */
849 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
850 /* if even "" matches, treat it as "not a match" */
851 if (match_structs.rm_so >= match_structs.rm_eo)
855 printf("%s%s\n", growline ? growline : "", str);
859 void print_found(const char *line);
862 static void print_ascii(const char *str)
869 n = strcspn(str, controls);
872 printf("%.*s", (int) n, str);
875 n = strspn(str, controls);
880 else if (*str == (char)0x9b)
881 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
882 /* I want to know who committed this sin */
885 *p++ = ctrlconv[(unsigned char)*str];
894 /* Print the buffer */
895 static void buffer_print(void)
900 for (i = 0; i <= max_displayed_line; i++) {
902 if (option_mask32 & FLAG_N)
903 print_lineno(buffer[i]);
905 print_found(buffer[i]);
907 print_ascii(buffer[i]);
909 if ((option_mask32 & FLAG_E)
911 && (max_fline - cur_fline) <= max_displayed_line
913 less_exit(EXIT_SUCCESS);
918 static void buffer_fill_and_print(void)
921 #if ENABLE_FEATURE_LESS_TRUNCATE
922 int fpos = cur_fline;
924 if (option_mask32 & FLAG_S) {
925 /* Go back to the beginning of this line */
926 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
931 while (i <= max_displayed_line && fpos <= max_fline) {
932 int lineno = LINENO(flines[fpos]);
933 buffer[i] = flines[fpos];
937 } while ((fpos <= max_fline)
938 && (option_mask32 & FLAG_S)
939 && lineno == LINENO(flines[fpos])
943 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
944 buffer[i] = flines[cur_fline + i];
947 for (; i <= max_displayed_line; i++) {
948 buffer[i] = empty_line_marker;
953 /* move cur_fline to a given line number, reading lines if necessary */
954 static void goto_lineno(int target)
959 else if (target > LINENO(flines[cur_fline])) {
961 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
963 /* target not reached but more input is available */
964 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
970 /* search backwards through already-read lines */
971 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
976 static void cap_cur_fline(void)
978 if ((option_mask32 & FLAG_S)) {
979 if (cur_fline > max_fline)
980 cur_fline = max_fline;
981 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
982 goto_lineno(max_lineno - max_displayed_line + TILDES);
987 if (cur_fline + max_displayed_line > max_fline + TILDES)
988 cur_fline = max_fline - max_displayed_line + TILDES;
994 /* Move the buffer up and down in the file in order to scroll */
995 static void buffer_down(int nlines)
997 if ((option_mask32 & FLAG_S))
998 goto_lineno(LINENO(flines[cur_fline]) + nlines);
1000 cur_fline += nlines;
1003 buffer_fill_and_print();
1006 static void buffer_up(int nlines)
1008 if ((option_mask32 & FLAG_S)) {
1009 goto_lineno(LINENO(flines[cur_fline]) - nlines);
1012 cur_fline -= nlines;
1017 buffer_fill_and_print();
1020 /* display a given line where the argument can be either an index into
1021 * the flines array or a line number */
1022 static void buffer_to_line(int linenum, int is_lineno)
1027 goto_lineno(linenum);
1029 cur_fline = linenum;
1032 buffer_fill_and_print();
1035 static void buffer_line(int linenum)
1037 buffer_to_line(linenum, FALSE);
1040 static void buffer_lineno(int lineno)
1042 buffer_to_line(lineno, TRUE);
1045 static void open_file_and_read_lines(void)
1048 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
1049 #if ENABLE_FEATURE_LESS_FLAGS
1050 num_lines = REOPEN_AND_COUNT;
1053 /* "less" with no arguments in argv[] */
1054 /* For status line only */
1055 filename = xstrdup(bb_msg_standard_input);
1056 #if ENABLE_FEATURE_LESS_FLAGS
1057 num_lines = REOPEN_STDIN;
1067 /* Reinitialize everything for a new file - free the memory and start over */
1068 static void reinitialize(void)
1073 for (i = 0; i <= max_fline; i++)
1074 free(MEMPTR(flines[i]));
1082 open_file_and_read_lines();
1083 #if ENABLE_FEATURE_LESS_ASK_TERMINAL
1085 printf(ESC"[999;999H" ESC"[6n");
1087 buffer_fill_and_print();
1090 static int64_t getch_nowait(void)
1094 struct pollfd pfd[2];
1096 pfd[0].fd = STDIN_FILENO;
1097 pfd[0].events = POLLIN;
1099 pfd[1].events = POLLIN;
1101 tcsetattr(kbd_fd, TCSANOW, &term_less);
1102 /* NB: select/poll returns whenever read will not block. Therefore:
1103 * if eof is reached, select/poll will return immediately
1104 * because read will immediately return 0 bytes.
1105 * Even if select/poll says that input is available, read CAN block
1106 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1109 /* Are we interested in stdin? */
1111 if (eof_error > 0) /* did NOT reach eof yet */
1112 rd = 0; /* yes, we are interested in stdin */
1114 /* Position cursor if line input is done */
1115 if (less_gets_pos >= 0)
1116 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
1119 if (kbd_input[0] == 0) { /* if nothing is buffered */
1120 #if ENABLE_FEATURE_LESS_WINCH
1123 /* NB: SIGWINCH interrupts poll() */
1124 r = poll(pfd + rd, 2 - rd, -1);
1125 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1126 return '\\'; /* anything which has no defined function */
1130 safe_poll(pfd + rd, 2 - rd, -1);
1134 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
1135 * would not block even if there is no input available */
1136 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
1137 if ((int)key64 == -1) {
1138 if (errno == EAGAIN) {
1139 /* No keyboard input available. Since poll() did return,
1140 * we should have input on stdin */
1142 buffer_fill_and_print();
1145 /* EOF/error (ssh session got killed etc) */
1146 less_exit(EXIT_SUCCESS);
1152 /* Grab a character from input without requiring the return key.
1153 * May return KEYCODE_xxx values.
1154 * Note that this function works best with raw input. */
1155 static int64_t less_getch(int pos)
1161 less_gets_pos = pos;
1162 key = key64 = getch_nowait();
1165 /* Discard Ctrl-something chars.
1166 * (checking only lower 32 bits is a size optimization:
1167 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1169 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
1175 static char* less_gets(int sz)
1179 char *result = xzalloc(1);
1183 less_gets_pos = sz + i;
1196 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
1198 if (i >= width - sz - 1)
1199 continue; /* len limit */
1202 result = xrealloc(result, i+1);
1206 static void examine_file(void)
1210 print_statusline("Examine: ");
1211 new_fname = less_gets(sizeof("Examine: ") - 1);
1212 if (!new_fname[0]) {
1218 if (access(new_fname, R_OK) != 0) {
1219 print_statusline("Cannot read this file");
1223 filename = new_fname;
1224 /* files start by = argv. why we assume that argv is infinitely long??
1225 files[num_files] = filename;
1226 current_file = num_files + 1;
1228 files[0] = filename;
1229 num_files = current_file = 1;
1233 /* This function changes the file currently being paged. direction can be one of the following:
1234 * -1: go back one file
1235 * 0: go to the first file
1236 * 1: go forward one file */
1237 static void change_file(int direction)
1239 if (current_file != ((direction > 0) ? num_files : 1)) {
1240 current_file = direction ? current_file + direction : 1;
1242 filename = xstrdup(files[current_file - 1]);
1245 print_statusline(direction > 0 ? "No next file" : "No previous file");
1249 static void remove_current_file(void)
1256 if (current_file != 1) {
1258 for (i = 3; i <= num_files; i++)
1259 files[i - 2] = files[i - 1];
1263 for (i = 2; i <= num_files; i++)
1264 files[i - 2] = files[i - 1];
1270 static void colon_process(void)
1274 /* Clear the current line and print a prompt */
1275 print_statusline(" :");
1277 keypress = less_getch(2);
1280 remove_current_file();
1285 #if ENABLE_FEATURE_LESS_FLAGS
1297 less_exit(EXIT_SUCCESS);
1305 #if ENABLE_FEATURE_LESS_REGEXP
1306 static void normalize_match_pos(int match)
1308 if (match >= num_matches)
1309 match = num_matches - 1;
1315 static void goto_match(int match)
1321 /* Try to find next match if eof isn't reached yet */
1322 if (match >= num_matches && eof_error > 0) {
1323 wanted_match = match; /* "I want to read until I see N'th match" */
1327 normalize_match_pos(match);
1328 buffer_line(match_lines[match_pos]);
1330 print_statusline("No matches found");
1334 static void fill_match_lines(unsigned pos)
1338 /* Run the regex on each line of the current file */
1339 while (pos <= max_fline) {
1340 /* If this line matches */
1341 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1342 /* and we didn't match it last time */
1343 && !(num_matches && match_lines[num_matches-1] == pos)
1345 match_lines = xrealloc_vector(match_lines, 4, num_matches);
1346 match_lines[num_matches++] = pos;
1352 static void regex_process(void)
1354 char *uncomp_regex, *err;
1356 /* Reset variables */
1361 if (pattern_valid) {
1366 /* Get the uncompiled regular expression from the user */
1368 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
1369 uncomp_regex = less_gets(1);
1370 if (!uncomp_regex[0]) {
1376 /* Compile the regex and check for errors */
1377 err = regcomp_or_errmsg(&pattern, uncomp_regex,
1378 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
1381 print_statusline(err);
1388 fill_match_lines(0);
1389 while (match_pos < num_matches) {
1390 if ((int)match_lines[match_pos] > cur_fline)
1394 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1397 /* It's possible that no matches are found yet.
1398 * goto_match() will read input looking for match,
1400 goto_match(match_pos);
1404 static void number_process(int first_digit)
1409 char num_input[sizeof(int)*4]; /* more than enough */
1411 num_input[0] = first_digit;
1413 /* Clear the current line, print a prompt, and then print the digit */
1415 printf(":%c", first_digit);
1417 /* Receive input until a letter is given */
1419 while (i < sizeof(num_input)-1) {
1420 keypress = less_getch(i + 1);
1421 if ((unsigned)keypress > 255 || !isdigit(keypress))
1423 num_input[i] = keypress;
1424 bb_putchar(keypress);
1428 num_input[i] = '\0';
1429 num = bb_strtou(num_input, NULL, 10);
1430 /* on format error, num == -1 */
1431 if (num < 1 || num > MAXLINES) {
1436 /* We now know the number and the letter entered, so we process them */
1438 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
1441 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
1444 case 'g': case '<': case 'G': case '>':
1445 buffer_lineno(num - 1);
1448 #if ENABLE_FEATURE_LESS_FLAGS
1450 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1452 num = num * max_lineno / 100;
1456 #if ENABLE_FEATURE_LESS_REGEXP
1458 goto_match(match_pos + num);
1461 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1465 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1472 #if ENABLE_FEATURE_LESS_DASHCMD
1473 static void flag_change(void)
1479 keypress = less_getch(1);
1483 option_mask32 ^= FLAG_M;
1486 option_mask32 ^= FLAG_m;
1489 option_mask32 ^= FLAG_E;
1492 option_mask32 ^= FLAG_TILDE;
1494 #if ENABLE_FEATURE_LESS_TRUNCATE
1496 option_mask32 ^= FLAG_S;
1497 buffer_fill_and_print();
1500 #if ENABLE_FEATURE_LESS_LINENUMS
1502 option_mask32 ^= FLAG_N;
1504 buffer_fill_and_print();
1511 static void show_flag_status(void)
1518 keypress = less_getch(1);
1522 flag_val = option_mask32 & FLAG_M;
1525 flag_val = option_mask32 & FLAG_m;
1528 flag_val = option_mask32 & FLAG_TILDE;
1531 flag_val = option_mask32 & FLAG_N;
1534 flag_val = option_mask32 & FLAG_E;
1542 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
1546 #endif /* ENABLE_FEATURE_LESS_DASHCMD */
1548 static void save_input_to_file(void)
1550 const char *msg = "";
1555 print_statusline("Log file: ");
1556 current_line = less_gets(sizeof("Log file: ")-1);
1557 if (current_line[0]) {
1558 fp = fopen_for_write(current_line);
1560 msg = "Error opening log file";
1563 for (i = 0; i <= max_fline; i++)
1564 fprintf(fp, "%s\n", flines[i]);
1569 print_statusline(msg);
1573 #if ENABLE_FEATURE_LESS_MARKS
1574 static void add_mark(void)
1578 print_statusline("Mark: ");
1579 letter = less_getch(sizeof("Mark: ") - 1);
1581 if (isalpha(letter)) {
1582 /* If we exceed 15 marks, start overwriting previous ones */
1583 if (num_marks == 14)
1586 mark_lines[num_marks][0] = letter;
1587 mark_lines[num_marks][1] = cur_fline;
1590 print_statusline("Invalid mark letter");
1594 static void goto_mark(void)
1599 print_statusline("Go to mark: ");
1600 letter = less_getch(sizeof("Go to mark: ") - 1);
1603 if (isalpha(letter)) {
1604 for (i = 0; i <= num_marks; i++)
1605 if (letter == mark_lines[i][0]) {
1606 buffer_line(mark_lines[i][1]);
1609 if (num_marks == 14 && letter != mark_lines[14][0])
1610 print_statusline("Mark not set");
1612 print_statusline("Invalid mark letter");
1616 #if ENABLE_FEATURE_LESS_BRACKETS
1617 static char opp_bracket(char bracket)
1620 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1622 case '(': /* ')' == '(' + 1 */
1634 static void match_right_bracket(char bracket)
1636 unsigned i = cur_fline;
1639 || strchr(flines[i], bracket) == NULL
1641 print_statusline("No bracket in top line");
1645 bracket = opp_bracket(bracket);
1646 for (; i < max_fline; i++) {
1647 if (strchr(flines[i], bracket) != NULL) {
1649 * Line with matched right bracket becomes
1652 buffer_line(i - max_displayed_line);
1656 print_statusline("No matching bracket found");
1659 static void match_left_bracket(char bracket)
1661 int i = cur_fline + max_displayed_line;
1664 || strchr(flines[i], bracket) == NULL
1666 print_statusline("No bracket in bottom line");
1670 bracket = opp_bracket(bracket);
1671 for (; i >= 0; i--) {
1672 if (strchr(flines[i], bracket) != NULL) {
1674 * Line with matched left bracket becomes
1675 * first visible line
1681 print_statusline("No matching bracket found");
1683 #endif /* FEATURE_LESS_BRACKETS */
1685 static void keypress_process(int keypress)
1688 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
1691 case KEYCODE_UP: case 'y': case 'k':
1694 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
1695 buffer_down(max_displayed_line + 1);
1697 case KEYCODE_PAGEUP: case 'w': case 'b':
1698 buffer_up(max_displayed_line + 1);
1701 buffer_down((max_displayed_line + 1) / 2);
1704 buffer_up((max_displayed_line + 1) / 2);
1706 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
1709 case KEYCODE_END: case 'G': case '>':
1710 cur_fline = MAXLINES;
1712 buffer_line(cur_fline);
1715 less_exit(EXIT_SUCCESS);
1717 #if ENABLE_FEATURE_LESS_MARKS
1728 /* TODO: (1) also bind ^R, ^L to this?
1729 * (2) re-measure window size?
1737 save_input_to_file();
1742 #if ENABLE_FEATURE_LESS_FLAGS
1747 #if ENABLE_FEATURE_LESS_REGEXP
1749 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1753 goto_match(match_pos + 1);
1756 goto_match(match_pos - 1);
1759 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1763 #if ENABLE_FEATURE_LESS_DASHCMD
1774 #if ENABLE_FEATURE_LESS_BRACKETS
1775 case '{': case '(': case '[':
1776 match_right_bracket(keypress);
1778 case '}': case ')': case ']':
1779 match_left_bracket(keypress);
1787 if (isdigit(keypress))
1788 number_process(keypress);
1791 static void sig_catcher(int sig)
1796 #if ENABLE_FEATURE_LESS_WINCH
1797 static void sigwinch_handler(int sig UNUSED_PARAM)
1803 int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1804 int less_main(int argc, char **argv)
1811 /* TODO: -x: do not interpret backspace, -xx: tab also
1812 * -xxx: newline also
1813 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1814 * -s: condense many empty lines to one
1815 * (used by some setups for manpage display)
1817 getopt32(argv, "EMmN~I"
1818 IF_FEATURE_LESS_TRUNCATE("S")
1819 IF_FEATURE_LESS_RAW("R")
1823 num_files = argc - optind;
1826 /* Tools typically pass LESS="FRSXMK".
1827 * The options we don't understand are ignored. */
1828 if (ENABLE_FEATURE_LESS_ENV) {
1829 char *c = getenv("LESS");
1830 if (c) while (*c) switch (*c++) {
1832 option_mask32 |= FLAG_M;
1835 option_mask32 |= FLAG_R;
1838 option_mask32 |= FLAG_S;
1845 /* Another popular pager, most, detects when stdout
1846 * is not a tty and turns into cat. This makes sense. */
1847 if (!isatty(STDOUT_FILENO))
1848 return bb_cat(argv);
1851 if (isatty(STDIN_FILENO)) {
1852 /* Just "less"? No args and no redirection? */
1853 bb_error_msg("missing filename");
1857 filename = xstrdup(files[0]);
1860 if (option_mask32 & FLAG_TILDE)
1861 empty_line_marker = "";
1863 /* Some versions of less can survive w/o controlling tty,
1864 * try to do the same. This also allows to specify an alternative
1865 * tty via "less 1<>TTY".
1867 * We prefer not to use STDOUT_FILENO directly,
1868 * since we want to set this fd to non-blocking mode,
1869 * and not interfere with other processes which share stdout with us.
1871 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1873 tty_fd = open(tty_name, O_RDONLY);
1878 /* Try controlling tty */
1880 tty_fd = open(CURRENT_TTY, O_RDONLY);
1882 /* If all else fails, less 481 uses stdout. Mimic that */
1883 tty_fd = STDOUT_FILENO;
1886 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
1887 kbd_fd = tty_fd; /* save in a global */
1889 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL);
1891 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
1892 /* 20: two tabstops + 4 */
1893 if (width < 20 || max_displayed_line < 3)
1894 return bb_cat(argv);
1895 max_displayed_line -= 2;
1897 /* We want to restore term_orig on exit */
1898 bb_signals(BB_FATAL_SIGS, sig_catcher);
1899 #if ENABLE_FEATURE_LESS_WINCH
1900 signal(SIGWINCH, sigwinch_handler);
1903 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
1908 #if ENABLE_FEATURE_LESS_WINCH
1909 while (WINCH_COUNTER) {
1912 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1913 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
1914 /* 20: two tabstops + 4 */
1917 if (max_displayed_line < 3)
1918 max_displayed_line = 3;
1919 max_displayed_line -= 2;
1921 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
1922 /* Avoid re-wrap and/or redraw if we already know
1923 * we need to do it again. These ops are expensive */
1929 buffer_fill_and_print();
1930 /* This took some time. Loop back and check,
1931 * were there another SIGWINCH? */
1933 keypress = less_getch(-1); /* -1: do not position cursor */
1934 # if ENABLE_FEATURE_LESS_ASK_TERMINAL
1935 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1936 uint32_t rc = (keypress >> 32);
1937 width = (rc & 0x7fff);
1938 max_displayed_line = ((rc >> 16) & 0x7fff);
1943 keypress = less_getch(-1); /* -1: do not position cursor */
1945 keypress_process(keypress);
1950 Help text of less version 418 is below.
1951 If you are implementing something, keeping
1952 key and/or command line switch compatibility is a good idea:
1955 SUMMARY OF LESS COMMANDS
1957 Commands marked with * may be preceded by a number, N.
1958 Notes in parentheses indicate the behavior if N is given.
1959 h H Display this help.
1961 ---------------------------------------------------------------------------
1963 e ^E j ^N CR * Forward one line (or N lines).
1964 y ^Y k ^K ^P * Backward one line (or N lines).
1965 f ^F ^V SPACE * Forward one window (or N lines).
1966 b ^B ESC-v * Backward one window (or N lines).
1967 z * Forward one window (and set window to N).
1968 w * Backward one window (and set window to N).
1969 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1970 d ^D * Forward one half-window (and set half-window to N).
1971 u ^U * Backward one half-window (and set half-window to N).
1972 ESC-) RightArrow * Left one half screen width (or N positions).
1973 ESC-( LeftArrow * Right one half screen width (or N positions).
1974 F Forward forever; like "tail -f".
1975 r ^R ^L Repaint screen.
1976 R Repaint screen, discarding buffered input.
1977 ---------------------------------------------------
1978 Default "window" is the screen height.
1979 Default "half-window" is half of the screen height.
1980 ---------------------------------------------------------------------------
1982 /pattern * Search forward for (N-th) matching line.
1983 ?pattern * Search backward for (N-th) matching line.
1984 n * Repeat previous search (for N-th occurrence).
1985 N * Repeat previous search in reverse direction.
1986 ESC-n * Repeat previous search, spanning files.
1987 ESC-N * Repeat previous search, reverse dir. & spanning files.
1988 ESC-u Undo (toggle) search highlighting.
1989 ---------------------------------------------------
1990 Search patterns may be modified by one or more of:
1991 ^N or ! Search for NON-matching lines.
1992 ^E or * Search multiple files (pass thru END OF FILE).
1993 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1994 ^K Highlight matches, but don't move (KEEP position).
1995 ^R Don't use REGULAR EXPRESSIONS.
1996 ---------------------------------------------------------------------------
1998 g < ESC-< * Go to first line in file (or line N).
1999 G > ESC-> * Go to last line in file (or line N).
2000 p % * Go to beginning of file (or N percent into file).
2001 t * Go to the (N-th) next tag.
2002 T * Go to the (N-th) previous tag.
2003 { ( [ * Find close bracket } ) ].
2004 } ) ] * Find open bracket { ( [.
2005 ESC-^F <c1> <c2> * Find close bracket <c2>.
2006 ESC-^B <c1> <c2> * Find open bracket <c1>
2007 ---------------------------------------------------
2008 Each "find close bracket" command goes forward to the close bracket
2009 matching the (N-th) open bracket in the top line.
2010 Each "find open bracket" command goes backward to the open bracket
2011 matching the (N-th) close bracket in the bottom line.
2012 m<letter> Mark the current position with <letter>.
2013 '<letter> Go to a previously marked position.
2014 '' Go to the previous position.
2016 ---------------------------------------------------
2017 A mark is any upper-case or lower-case letter.
2018 Certain marks are predefined:
2019 ^ means beginning of the file
2020 $ means end of the file
2021 ---------------------------------------------------------------------------
2023 :e [file] Examine a new file.
2025 :n * Examine the (N-th) next file from the command line.
2026 :p * Examine the (N-th) previous file from the command line.
2027 :x * Examine the first (or N-th) file from the command line.
2028 :d Delete the current file from the command line list.
2029 = ^G :f Print current file name.
2030 ---------------------------------------------------------------------------
2031 MISCELLANEOUS COMMANDS
2032 -<flag> Toggle a command line option [see OPTIONS below].
2033 --<name> Toggle a command line option, by name.
2034 _<flag> Display the setting of a command line option.
2035 __<name> Display the setting of an option, by name.
2036 +cmd Execute the less cmd each time a new file is examined.
2037 !command Execute the shell command with $SHELL.
2038 |Xcommand Pipe file between current pos & mark X to shell command.
2039 v Edit the current file with $VISUAL or $EDITOR.
2040 V Print version number of "less".
2041 ---------------------------------------------------------------------------
2043 Most options may be changed either on the command line,
2044 or from within less by using the - or -- command.
2045 Options may be given in one of two forms: either a single
2046 character preceded by a -, or a name preceded by --.
2048 Display help (from command line).
2049 -a ........ --search-skip-screen
2050 Forward search skips current screen.
2051 -b [N] .... --buffers=[N]
2053 -B ........ --auto-buffers
2054 Don't automatically allocate buffers for pipes.
2055 -c ........ --clear-screen
2056 Repaint by clearing rather than scrolling.
2059 -D [xn.n] . --color=xn.n
2060 Set screen colors. (MS-DOS only)
2061 -e -E .... --quit-at-eof --QUIT-AT-EOF
2062 Quit at end of file.
2064 Force open non-regular files.
2065 -F ........ --quit-if-one-screen
2066 Quit if entire file fits on first screen.
2067 -g ........ --hilite-search
2068 Highlight only last match for searches.
2069 -G ........ --HILITE-SEARCH
2070 Don't highlight any matches for searches.
2071 -h [N] .... --max-back-scroll=[N]
2072 Backward scroll limit.
2073 -i ........ --ignore-case
2074 Ignore case in searches that do not contain uppercase.
2075 -I ........ --IGNORE-CASE
2076 Ignore case in all searches.
2077 -j [N] .... --jump-target=[N]
2078 Screen position of target lines.
2079 -J ........ --status-column
2080 Display a status column at left edge of screen.
2081 -k [file] . --lesskey-file=[file]
2083 -L ........ --no-lessopen
2084 Ignore the LESSOPEN environment variable.
2085 -m -M .... --long-prompt --LONG-PROMPT
2087 -n -N .... --line-numbers --LINE-NUMBERS
2088 Don't use line numbers.
2089 -o [file] . --log-file=[file]
2090 Copy to log file (standard input only).
2091 -O [file] . --LOG-FILE=[file]
2092 Copy to log file (unconditionally overwrite).
2093 -p [pattern] --pattern=[pattern]
2094 Start at pattern (from command line).
2095 -P [prompt] --prompt=[prompt]
2097 -q -Q .... --quiet --QUIET --silent --SILENT
2098 Quiet the terminal bell.
2099 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2100 Output "raw" control characters.
2101 -s ........ --squeeze-blank-lines
2102 Squeeze multiple blank lines.
2103 -S ........ --chop-long-lines
2105 -t [tag] .. --tag=[tag]
2107 -T [tagsfile] --tag-file=[tagsfile]
2108 Use an alternate tags file.
2109 -u -U .... --underline-special --UNDERLINE-SPECIAL
2110 Change handling of backspaces.
2111 -V ........ --version
2112 Display the version number of "less".
2113 -w ........ --hilite-unread
2114 Highlight first new line after forward-screen.
2115 -W ........ --HILITE-UNREAD
2116 Highlight first new line after any forward movement.
2117 -x [N[,...]] --tabs=[N[,...]]
2119 -X ........ --no-init
2120 Don't use termcap init/deinit strings.
2122 Don't use termcap keypad init/deinit strings.
2123 -y [N] .... --max-forw-scroll=[N]
2124 Forward scroll limit.
2125 -z [N] .... --window=[N]
2127 -" [c[c]] . --quotes=[c[c]]
2128 Set shell quote characters.
2130 Don't display tildes after end of file.
2131 -# [N] .... --shift=[N]
2132 Horizontal scroll amount (0 = one half screen width)
2134 ---------------------------------------------------------------------------
2136 These keys can be used to edit text being entered
2137 on the "command line" at the bottom of the screen.
2138 RightArrow ESC-l Move cursor right one character.
2139 LeftArrow ESC-h Move cursor left one character.
2140 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2141 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2142 HOME ESC-0 Move cursor to start of line.
2143 END ESC-$ Move cursor to end of line.
2144 BACKSPACE Delete char to left of cursor.
2145 DELETE ESC-x Delete char under cursor.
2146 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2147 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2148 CNTL-U ESC (MS-DOS only) Delete entire line.
2149 UpArrow ESC-k Retrieve previous command line.
2150 DownArrow ESC-j Retrieve next command line.
2151 TAB Complete filename & cycle.
2152 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2153 CNTL-L Complete filename, list all.