blob: ccdb15fdcaf4066f611afcb5f9104826f3cbb75a [file] [log] [blame]
Rob Landley9200e792005-09-15 19:26:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini less implementation for busybox
4 *
Rob Landley9200e792005-09-15 19:26:59 +00005 * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysingerf284c762006-04-16 20:38:26 +00008 */
9
10/*
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000011 * TODO:
12 * - Add more regular expression support - search modifiers, certain matches, etc.
13 * - Add more complex bracket searching - currently, nested brackets are
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000014 * not considered.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000015 * - Add support for "F" as an input. This causes less to act in
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000016 * a similar way to tail -f.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000017 * - Allow horizontal scrolling.
Rob Landley9200e792005-09-15 19:26:59 +000018 *
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000019 * Notes:
20 * - the inp file pointer is used so that keyboard input works after
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000021 * redirected input has been read from stdin
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000022 */
Rob Landley9200e792005-09-15 19:26:59 +000023
Denys Vlasenko4e552a72011-07-25 15:18:20 +020024//config:config LESS
25//config: bool "less"
26//config: default y
27//config: help
28//config: 'less' is a pager, meaning that it displays text files. It possesses
29//config: a wide array of features, and is an improvement over 'more'.
30//config:
31//config:config FEATURE_LESS_MAXLINES
32//config: int "Max number of input lines less will try to eat"
33//config: default 9999999
34//config: depends on LESS
35//config:
36//config:config FEATURE_LESS_BRACKETS
37//config: bool "Enable bracket searching"
38//config: default y
39//config: depends on LESS
40//config: help
41//config: This option adds the capability to search for matching left and right
42//config: brackets, facilitating programming.
43//config:
44//config:config FEATURE_LESS_FLAGS
Denys Vlasenko53772862012-04-17 16:11:25 +020045//config: bool "Enable -m/-M"
Denys Vlasenko4e552a72011-07-25 15:18:20 +020046//config: default y
47//config: depends on LESS
48//config: help
Denys Vlasenko53772862012-04-17 16:11:25 +020049//config: The -M/-m flag enables a more sophisticated status line.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020050//config:
Ron Yorston51aa8612015-07-19 11:12:29 +010051//config:config FEATURE_LESS_TRUNCATE
52//config: bool "Enable -S"
53//config: default y
54//config: depends on LESS
55//config: help
56//config: The -S flag causes long lines to be truncated rather than
57//config: wrapped.
58//config:
Denys Vlasenko4e552a72011-07-25 15:18:20 +020059//config:config FEATURE_LESS_MARKS
60//config: bool "Enable marks"
61//config: default y
62//config: depends on LESS
63//config: help
64//config: Marks enable positions in a file to be stored for easy reference.
65//config:
66//config:config FEATURE_LESS_REGEXP
67//config: bool "Enable regular expressions"
68//config: default y
69//config: depends on LESS
70//config: help
71//config: Enable regular expressions, allowing complex file searches.
72//config:
73//config:config FEATURE_LESS_WINCH
74//config: bool "Enable automatic resizing on window size changes"
75//config: default y
76//config: depends on LESS
77//config: help
78//config: Makes less track window size changes.
79//config:
80//config:config FEATURE_LESS_ASK_TERMINAL
81//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
82//config: default y
83//config: depends on FEATURE_LESS_WINCH
84//config: help
85//config: Makes less track window size changes.
86//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
87//config: this option makes less perform a last-ditch effort to find it:
88//config: position cursor to 999,999 and ask terminal to report real
89//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
90//config:
91//config: This is not clean but helps a lot on serial lines and such.
92//config:
93//config:config FEATURE_LESS_DASHCMD
94//config: bool "Enable flag changes ('-' command)"
95//config: default y
96//config: depends on LESS
97//config: help
98//config: This enables the ability to change command-line flags within
99//config: less itself ('-' keyboard command).
100//config:
101//config:config FEATURE_LESS_LINENUMS
102//config: bool "Enable dynamic switching of line numbers"
103//config: default y
104//config: depends on FEATURE_LESS_DASHCMD
105//config: help
106//config: Enables "-N" command.
107
Pere Orga5bc8c002011-04-11 03:29:49 +0200108//usage:#define less_trivial_usage
Ron Yorston51aa8612015-07-19 11:12:29 +0100109//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
110//usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200111//usage:#define less_full_usage "\n\n"
112//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200113//usage: "\n -E Quit once the end of a file is reached"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100114//usage: IF_FEATURE_LESS_REGEXP(
115//usage: "\n -I Ignore case in all searches"
116//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200117//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200118//usage: "\n -M,-m Display status line with line numbers"
119//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200120//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200121//usage: "\n -N Prefix line number to each line"
Ron Yorston51aa8612015-07-19 11:12:29 +0100122//usage: IF_FEATURE_LESS_TRUNCATE(
123//usage: "\n -S Truncate long lines"
124//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200125//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200126
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200127#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000128
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000129#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000130#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000131#include "xregex.h"
132#endif
133
Marek Polacek7b181072010-10-28 21:34:56 +0200134
135#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000136/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200137#define HIGHLIGHT ESC"[7m"
138#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200139/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200140#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200141/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200142#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000143
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000144enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000145/* Absolute max of lines eaten */
146 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000147/* This many "after the end" lines we will show (at max) */
148 TILDES = 1,
149};
150
Rob Landley9200e792005-09-15 19:26:59 +0000151/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000152enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000153 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000154 FLAG_M = 1 << 1,
155 FLAG_m = 1 << 2,
156 FLAG_N = 1 << 3,
157 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000158 FLAG_I = 1 << 5,
Ron Yorston51aa8612015-07-19 11:12:29 +0100159 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000160/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000161 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
162};
Rob Landley9200e792005-09-15 19:26:59 +0000163
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000164#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000165enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000166#endif
167
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000168struct globals {
169 int cur_fline; /* signed */
170 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +0000171 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000172/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000173 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000174 unsigned max_fline;
175 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000176 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000177 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000178#if ENABLE_FEATURE_LESS_WINCH
179 unsigned winch_counter;
180#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000181 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000182 ssize_t readpos;
183 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000184 const char **buffer;
185 const char **flines;
186 const char *empty_line_marker;
187 unsigned num_files;
188 unsigned current_file;
189 char *filename;
190 char **files;
Ron Yorston193ba402015-07-21 22:28:09 +0200191#if ENABLE_FEATURE_LESS_FLAGS
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200192 int num_lines; /* a flag if < 0, line count if >= 0 */
193# define REOPEN_AND_COUNT (-1)
194# define REOPEN_STDIN (-2)
195# define NOT_REGULAR_FILE (-3)
Ron Yorston193ba402015-07-21 22:28:09 +0200196#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000197#if ENABLE_FEATURE_LESS_MARKS
198 unsigned num_marks;
199 unsigned mark_lines[15][2];
200#endif
201#if ENABLE_FEATURE_LESS_REGEXP
202 unsigned *match_lines;
203 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000204 int wanted_match; /* signed! */
205 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000206 regex_t pattern;
207 smallint pattern_valid;
208#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200209#if ENABLE_FEATURE_LESS_ASK_TERMINAL
210 smallint winsize_err;
211#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000212 smallint terminated;
213 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000214 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000215};
216#define G (*ptr_to_globals)
217#define cur_fline (G.cur_fline )
218#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000219#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000220#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000221#define max_fline (G.max_fline )
222#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000223#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000224#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000225#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000226/* This one is 100% not cached by compiler on read access */
227#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000228#define eof_error (G.eof_error )
229#define readpos (G.readpos )
230#define readeof (G.readeof )
231#define buffer (G.buffer )
232#define flines (G.flines )
233#define empty_line_marker (G.empty_line_marker )
234#define num_files (G.num_files )
235#define current_file (G.current_file )
236#define filename (G.filename )
237#define files (G.files )
Ron Yorston193ba402015-07-21 22:28:09 +0200238#define num_lines (G.num_lines )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000239#define num_marks (G.num_marks )
240#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000241#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000242#define match_lines (G.match_lines )
243#define match_pos (G.match_pos )
244#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000245#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000246#define pattern (G.pattern )
247#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000248#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000249#define terminated (G.terminated )
250#define term_orig (G.term_orig )
251#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000252#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000253#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000254 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
255 less_gets_pos = -1; \
256 empty_line_marker = "~"; \
257 num_files = 1; \
258 current_file = 1; \
259 eof_error = 1; \
260 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000261 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000262} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000263
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000264/* 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. Accesor: */
268#define MEMPTR(p) ((char*)(p) - 4)
269#define LINENO(p) (*(uint32_t*)((p) - 4))
270
271
Rob Landley9200e792005-09-15 19:26:59 +0000272/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000273static void set_tty_cooked(void)
274{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100275 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000276 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000277}
278
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000279/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000280 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000281static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000282{
Marek Polacek7b181072010-10-28 21:34:56 +0200283 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000284}
285
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000286static void clear_line(void)
287{
Marek Polacek7b181072010-10-28 21:34:56 +0200288 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000289}
290
291static void print_hilite(const char *str)
292{
293 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000294}
295
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000296static void print_statusline(const char *str)
297{
298 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000299 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000300}
301
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000302/* Exit the program gracefully */
303static void less_exit(int code)
304{
305 set_tty_cooked();
306 clear_line();
307 if (code < 0)
308 kill_myself_with_sig(- code); /* does not return */
309 exit(code);
310}
311
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000312#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
313 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000314static void re_wrap(void)
315{
316 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000317 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000318 int src_idx;
319 int dst_idx;
320 int new_cur_fline = 0;
321 uint32_t lineno;
322 char linebuf[w + 1];
323 const char **old_flines = flines;
324 const char *s;
325 char **new_flines = NULL;
326 char *d;
327
328 if (option_mask32 & FLAG_N)
329 w -= 8;
330
331 src_idx = 0;
332 dst_idx = 0;
333 s = old_flines[0];
334 lineno = LINENO(s);
335 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000336 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000337 while (1) {
338 *d = *s;
339 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000340 new_line_pos++;
Ron Yorston78cfa002015-07-19 21:41:09 +0100341 if (*d == '\t') { /* tab */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000342 new_line_pos += 7;
Ron Yorston78cfa002015-07-19 21:41:09 +0100343 new_line_pos &= (~7);
344 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000345 s++;
346 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000347 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000348 int sz;
349 /* new line is full, create next one */
350 *d = '\0';
351 next_new:
352 sz = (d - linebuf) + 1; /* + 1: NUL */
353 d = ((char*)xmalloc(sz + 4)) + 4;
354 LINENO(d) = lineno;
355 memcpy(d, linebuf, sz);
356 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
357 new_flines[dst_idx] = d;
358 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000359 if (new_line_pos < w) {
360 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000361 if (src_idx > max_fline)
362 break;
363 lineno = LINENO(s);
364 }
365 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000366 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000367 }
368 continue;
369 }
370 /* *d == NUL: old line ended, go to next old one */
371 free(MEMPTR(old_flines[src_idx]));
372 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000373 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000374 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000375 src_idx++;
376 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000377 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000378 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000379 s = old_flines[src_idx];
380 if (lineno != LINENO(s)) {
381 /* this is not a continuation line!
382 * create next _new_ line too */
383 goto next_new;
384 }
385 }
386
387 free(old_flines);
388 flines = (const char **)new_flines;
389
390 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000391 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000392 cur_fline = new_cur_fline;
393 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000394#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000395 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000396#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000397}
398#endif
399
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000400#if ENABLE_FEATURE_LESS_REGEXP
401static void fill_match_lines(unsigned pos);
402#else
403#define fill_match_lines(pos) ((void)0)
404#endif
405
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100406static int at_end(void)
407{
408 return (option_mask32 & FLAG_S)
409 ? !(cur_fline <= max_fline &&
410 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
411 : !(max_fline > cur_fline + max_displayed_line);
412}
413
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000414/* Devilishly complex routine.
415 *
416 * Has to deal with EOF and EPIPE on input,
417 * with line wrapping, with last line not ending in '\n'
418 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000419 * It reads input again if last time we got an EOF (thus supporting
420 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000421 *
422 * Variables used:
423 * flines[] - array of lines already read. Linewrap may cause
424 * one source file line to occupy several flines[n].
425 * flines[max_fline] - last line, possibly incomplete.
426 * terminated - 1 if flines[max_fline] is 'terminated'
427 * (if there was '\n' [which isn't stored itself, we just remember
428 * that it was seen])
429 * max_lineno - last line's number, this one doesn't increment
430 * on line wrap, only on "real" new lines.
431 * readbuf[0..readeof-1] - small preliminary buffer.
432 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000433 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000434 * (takes into account tabs and backspaces)
435 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200436 *
437 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
438 * "/search on very long input" and "reaching max line count" corner cases.
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000439 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000440static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000441{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000442#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000443 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000444 int w = width;
445 char last_terminated = terminated;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200446 time_t last_time = 0;
447 int retry_EAGAIN = 2;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000448#if ENABLE_FEATURE_LESS_REGEXP
449 unsigned old_max_fline = max_fline;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000450#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000451
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200452 /* (careful: max_fline can be -1) */
453 if (max_fline + 1 > MAXLINES)
454 return;
455
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000456 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000457 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000458
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100459 p = current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000460 if (!last_terminated) {
461 const char *cp = flines[max_fline];
Denys Vlasenko865814a2014-09-22 21:17:24 +0200462 p = stpcpy(p, cp);
463 free(MEMPTR(cp));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000464 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000465 } else {
Denys Vlasenko865814a2014-09-22 21:17:24 +0200466 max_fline++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000467 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000468 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000469
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000470 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000471 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000472 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000473 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000474 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000475 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000476 if (readpos >= readeof) {
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200477 int flags = ndelay_on(0);
478
479 while (1) {
480 time_t t;
481
482 errno = 0;
483 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
484 if (errno != EAGAIN)
485 break;
486 t = time(NULL);
487 if (t != last_time) {
488 last_time = t;
489 if (--retry_EAGAIN < 0)
490 break;
491 }
492 sched_yield();
493 }
494 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000495 readpos = 0;
496 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000497 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000498 goto reached_eof;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200499 retry_EAGAIN = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000500 }
501 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000502 /* backspace? [needed for manpages] */
503 /* <tab><bs> is (a) insane and */
504 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000505 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000506 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000507 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000508 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000509 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000510 continue;
511 }
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100512 {
513 size_t new_last_line_pos = last_line_pos + 1;
514 if (c == '\t') {
515 new_last_line_pos += 7;
516 new_last_line_pos &= (~7);
517 }
518 if ((int)new_last_line_pos > w)
519 break;
520 last_line_pos = new_last_line_pos;
521 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000522 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000523 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000524 if (c == '\n') {
525 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000526 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000527 break;
528 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000529 /* NUL is substituted by '\n'! */
530 if (c == '\0') c = '\n';
531 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000532 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000533 } /* end of "read chars until we have a line" loop */
Denys Vlasenko15943c82014-09-21 22:10:55 +0200534#if 0
535//BUG: also triggers on this:
536// { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
537// (resulting in lost empty line between "foo" and "bar" lines)
538// the "terminated" logic needs fixing (or explaining)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000539 /* Corner case: linewrap with only "" wrapping to next line */
540 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000541 if (!last_terminated && !current_line[0]) {
542 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000543 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000544 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000545 }
Denys Vlasenko15943c82014-09-21 22:10:55 +0200546#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000547 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000548 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000549 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000550
551 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
552 LINENO(flines[max_fline]) = max_lineno;
553 if (terminated)
554 max_lineno++;
555
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000556 if (max_fline >= MAXLINES) {
557 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000558 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000559 }
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100560 if (!at_end()) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000561#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000562 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000563#else
564 if (wanted_match >= num_matches) { /* goto_match called us */
565 fill_match_lines(old_max_fline);
566 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000567 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000568 if (wanted_match < num_matches)
569 break;
570#endif
571 }
572 if (eof_error <= 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000573 break;
574 }
575 max_fline++;
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100576 current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000577 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000578 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000579 } /* end of "read lines until we reach cur_fline" loop */
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200580
581 if (eof_error < 0) {
582 if (errno == EAGAIN) {
583 eof_error = 1;
584 } else {
585 print_statusline(bb_msg_read_error);
586 }
587 }
Ron Yorston193ba402015-07-21 22:28:09 +0200588#if ENABLE_FEATURE_LESS_FLAGS
589 else if (eof_error == 0)
590 num_lines = max_lineno;
591#endif
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200592
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000593 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000594#if ENABLE_FEATURE_LESS_REGEXP
595 /* prevent us from being stuck in search for a match */
596 wanted_match = -1;
597#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000598#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000599}
600
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000601#if ENABLE_FEATURE_LESS_FLAGS
Ron Yorston193ba402015-07-21 22:28:09 +0200602static int safe_lineno(int fline)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000603{
Ron Yorston193ba402015-07-21 22:28:09 +0200604 if (fline >= max_fline)
605 fline = max_fline - 1;
606
607 /* also catches empty file (max_fline == 0) */
608 if (fline < 0)
609 return 0;
610
611 return LINENO(flines[fline]) + 1;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000612}
613
Ron Yorston159e0322015-07-24 14:27:42 +0100614/* count number of lines in file */
615static void update_num_lines(void)
616{
617 int count, fd;
Ron Yorstonb27cf312015-07-31 17:33:24 +0100618 struct stat stbuf;
Ron Yorston159e0322015-07-24 14:27:42 +0100619 ssize_t len, i;
620 char buf[4096];
Ron Yorston159e0322015-07-24 14:27:42 +0100621
Ron Yorston70b84be2015-07-24 14:28:08 +0100622 /* only do this for regular files */
Ron Yorstonb27cf312015-07-31 17:33:24 +0100623 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
Ron Yorston159e0322015-07-24 14:27:42 +0100624 count = 0;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200625 fd = open("/proc/self/fd/0", O_RDONLY);
626 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
627 /* "filename" is valid only if REOPEN_AND_COUNT */
628 fd = open(filename, O_RDONLY);
629 }
Ron Yorston70b84be2015-07-24 14:28:08 +0100630 if (fd < 0) {
631 /* somebody stole my file! */
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200632 num_lines = NOT_REGULAR_FILE;
Ron Yorston70b84be2015-07-24 14:28:08 +0100633 return;
634 }
Ron Yorstonb27cf312015-07-31 17:33:24 +0100635 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
636 num_lines = NOT_REGULAR_FILE;
637 goto do_close;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200638 }
Ron Yorston159e0322015-07-24 14:27:42 +0100639 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
640 for (i = 0; i < len; ++i) {
641 if (buf[i] == '\n' && ++count == MAXLINES)
642 goto done;
643 }
644 }
645 done:
646 num_lines = count;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200647 do_close:
Ron Yorston159e0322015-07-24 14:27:42 +0100648 close(fd);
Ron Yorston159e0322015-07-24 14:27:42 +0100649 }
650}
651
Rob Landley9200e792005-09-15 19:26:59 +0000652/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000653static void m_status_print(void)
654{
Ron Yorston193ba402015-07-21 22:28:09 +0200655 int first, last;
656 unsigned percent;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000657
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200658 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000659 return;
660
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000661 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000662 printf(HIGHLIGHT"%s", filename);
663 if (num_files > 1)
664 printf(" (file %i of %i)", current_file, num_files);
Ron Yorston193ba402015-07-21 22:28:09 +0200665
666 first = safe_lineno(cur_fline);
667 last = (option_mask32 & FLAG_S)
668 ? MIN(first + max_displayed_line, max_lineno)
669 : safe_lineno(cur_fline + max_displayed_line);
670 printf(" lines %i-%i", first, last);
671
Ron Yorston159e0322015-07-24 14:27:42 +0100672 update_num_lines();
Ron Yorston193ba402015-07-21 22:28:09 +0200673 if (num_lines >= 0)
674 printf("/%i", num_lines);
675
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100676 if (at_end()) {
Ron Yorston193ba402015-07-21 22:28:09 +0200677 printf(" (END)");
678 if (num_files > 1 && current_file != num_files)
679 printf(" - next: %s", files[current_file]);
680 } else if (num_lines > 0) {
681 percent = (100 * last + num_lines/2) / num_lines;
682 printf(" %i%%", percent <= 100 ? percent : 100);
683 }
684 printf(NORMAL);
Rob Landley9200e792005-09-15 19:26:59 +0000685}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000686#endif
687
Rob Landley9200e792005-09-15 19:26:59 +0000688/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000689static void status_print(void)
690{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000691 const char *p;
692
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200693 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000694 return;
695
Rob Landley9200e792005-09-15 19:26:59 +0000696 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000697#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000698 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000699 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000700 return;
Rob Landley9200e792005-09-15 19:26:59 +0000701 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000702 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000703#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000704
705 clear_line();
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100706 if (cur_fline && !at_end()) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000707 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000708 return;
709 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000710 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000711 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000712 p = filename;
713 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000714 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000715 p, current_file, num_files);
716 return;
717 }
718 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000719}
720
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000721static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000722 /* NUL: never encountered; TAB: not converted */
723 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000724 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
725 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000726static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100727 /* why 40 instead of 4a below? - it is a replacement for '\n'.
728 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000729 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
730 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
731
Ron Yorstonf06386a2015-07-18 16:20:03 +0100732static void print_lineno(const char *line)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000733{
Ron Yorstonf06386a2015-07-18 16:20:03 +0100734 const char *fmt = " ";
735 unsigned n = n; /* for compiler */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000736
Ron Yorstonf06386a2015-07-18 16:20:03 +0100737 if (line != empty_line_marker) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000738 /* Width of 7 preserves tab spacing in the text */
739 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000740 n = LINENO(line) + 1;
Ron Yorstonf06386a2015-07-18 16:20:03 +0100741 if (n > 9999999 && MAXLINES > 9999999) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000742 n %= 10000000;
743 fmt = "%07u ";
744 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000745 }
Ron Yorstonf06386a2015-07-18 16:20:03 +0100746 printf(fmt, n);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000747}
748
749
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000750#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000751static void print_found(const char *line)
752{
753 int match_status;
754 int eflags;
755 char *growline;
756 regmatch_t match_structs;
757
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100758 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000759 const char *str = line;
760 char *p = buf;
761 size_t n;
762
763 while (*str) {
764 n = strcspn(str, controls);
765 if (n) {
766 if (!str[n]) break;
767 memcpy(p, str, n);
768 p += n;
769 str += n;
770 }
771 n = strspn(str, controls);
772 memset(p, '.', n);
773 p += n;
774 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000775 }
776 strcpy(p, str);
777
778 /* buf[] holds quarantined version of str */
779
780 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100781 * and NORMAL escape sequences placed around it.
782 * NB: we regex against line, but insert text
783 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000784 str = buf;
785 growline = NULL;
786 eflags = 0;
787 goto start;
788
789 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000790 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200791 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200792 (int)match_structs.rm_so, str,
793 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000794 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000795 free(growline);
796 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000797 str += match_structs.rm_eo;
798 line += match_structs.rm_eo;
799 eflags = REG_NOTBOL;
800 start:
801 /* Most of the time doesn't find the regex, optimize for that */
802 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000803 /* if even "" matches, treat it as "not a match" */
804 if (match_structs.rm_so >= match_structs.rm_eo)
805 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000806 }
807
Ron Yorstonf06386a2015-07-18 16:20:03 +0100808 printf("%s%s\n", growline ? growline : "", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000809 free(growline);
810}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000811#else
812void print_found(const char *line);
813#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000814
815static void print_ascii(const char *str)
816{
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100817 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000818 char *p;
819 size_t n;
820
821 while (*str) {
822 n = strcspn(str, controls);
823 if (n) {
824 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000825 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000826 str += n;
827 }
828 n = strspn(str, controls);
829 p = buf;
830 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000831 if (*str == 0x7f)
832 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000833 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000834 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
835 /* I want to know who committed this sin */
836 *p++ = '{';
837 else
838 *p++ = ctrlconv[(unsigned char)*str];
839 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000840 } while (--n);
841 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000842 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000843 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000844 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000845}
846
Rob Landley9200e792005-09-15 19:26:59 +0000847/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000848static void buffer_print(void)
849{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000850 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000851
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000852 move_cursor(0, 0);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200853 for (i = 0; i <= max_displayed_line; i++) {
Ron Yorstonf06386a2015-07-18 16:20:03 +0100854 printf(CLEAR_2_EOL);
855 if (option_mask32 & FLAG_N)
856 print_lineno(buffer[i]);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000857 if (pattern_valid)
858 print_found(buffer[i]);
859 else
860 print_ascii(buffer[i]);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200861 }
862 if ((option_mask32 & FLAG_E)
863 && eof_error <= 0
864 && (max_fline - cur_fline) <= max_displayed_line
865 ) {
866 less_exit(EXIT_SUCCESS);
867 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000868 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000869}
870
Denis Vlasenkod9083952008-10-24 10:42:21 +0000871static void buffer_fill_and_print(void)
872{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000873 unsigned i;
Ron Yorston51aa8612015-07-19 11:12:29 +0100874#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000875 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000876
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000877 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000878 /* Go back to the beginning of this line */
879 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
880 fpos--;
881 }
882
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000883 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000884 while (i <= max_displayed_line && fpos <= max_fline) {
885 int lineno = LINENO(flines[fpos]);
886 buffer[i] = flines[fpos];
887 i++;
888 do {
889 fpos++;
890 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000891 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000892 && lineno == LINENO(flines[fpos])
893 );
894 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000895#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000896 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
897 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000898 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000899#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000900 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000901 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000902 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000903 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000904}
905
Ron Yorstond542d182015-07-24 14:29:13 +0100906/* move cur_fline to a given line number, reading lines if necessary */
907static void goto_lineno(int target)
908{
909 if (target <= 0 ) {
910 cur_fline = 0;
911 }
912 else if (target > LINENO(flines[cur_fline])) {
913 retry:
914 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
915 ++cur_fline;
916 /* target not reached but more input is available */
917 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
918 read_lines();
919 goto retry;
920 }
921 }
922 else {
923 /* search backwards through already-read lines */
924 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
925 --cur_fline;
926 }
927}
928
929static void cap_cur_fline(void)
930{
931 if ((option_mask32 & FLAG_S)) {
932 if (cur_fline > max_fline)
933 cur_fline = max_fline;
934 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
935 goto_lineno(max_lineno - max_displayed_line + TILDES);
936 read_lines();
937 }
938 }
939 else {
940 if (cur_fline + max_displayed_line > max_fline + TILDES)
941 cur_fline = max_fline - max_displayed_line + TILDES;
942 if (cur_fline < 0)
943 cur_fline = 0;
944 }
945}
946
Rob Landley9200e792005-09-15 19:26:59 +0000947/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000948static void buffer_down(int nlines)
949{
Ron Yorstond542d182015-07-24 14:29:13 +0100950 if ((option_mask32 & FLAG_S))
951 goto_lineno(LINENO(flines[cur_fline]) + nlines);
952 else
953 cur_fline += nlines;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000954 read_lines();
Ron Yorstond542d182015-07-24 14:29:13 +0100955 cap_cur_fline();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000956 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000957}
958
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000959static void buffer_up(int nlines)
960{
Ron Yorstond542d182015-07-24 14:29:13 +0100961 if ((option_mask32 & FLAG_S)) {
962 goto_lineno(LINENO(flines[cur_fline]) - nlines);
963 }
964 else {
965 cur_fline -= nlines;
966 if (cur_fline < 0)
967 cur_fline = 0;
968 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000969 read_lines();
970 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000971}
972
Ron Yorstond542d182015-07-24 14:29:13 +0100973/* display a given line where the argument can be either an index into
974 * the flines array or a line number */
975static void buffer_to_line(int linenum, int is_lineno)
976{
977 if (linenum <= 0)
978 cur_fline = 0;
979 else if (is_lineno)
980 goto_lineno(linenum);
981 else
982 cur_fline = linenum;
983 read_lines();
984 cap_cur_fline();
985 buffer_fill_and_print();
986}
987
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000988static void buffer_line(int linenum)
989{
Ron Yorstond542d182015-07-24 14:29:13 +0100990 buffer_to_line(linenum, FALSE);
991}
992
993static void buffer_lineno(int lineno)
994{
995 buffer_to_line(lineno, TRUE);
Rob Landley9200e792005-09-15 19:26:59 +0000996}
997
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000998static void open_file_and_read_lines(void)
999{
1000 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +00001001 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001002#if ENABLE_FEATURE_LESS_FLAGS
1003 num_lines = REOPEN_AND_COUNT;
1004#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001005 } else {
1006 /* "less" with no arguments in argv[] */
1007 /* For status line only */
1008 filename = xstrdup(bb_msg_standard_input);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001009#if ENABLE_FEATURE_LESS_FLAGS
1010 num_lines = REOPEN_STDIN;
1011#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001012 }
1013 readpos = 0;
1014 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +00001015 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001016 terminated = 1;
1017 read_lines();
1018}
1019
1020/* Reinitialize everything for a new file - free the memory and start over */
1021static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001022{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001023 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001024
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001025 if (flines) {
1026 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +00001027 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001028 free(flines);
1029 flines = NULL;
1030 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001031
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001032 max_fline = -1;
1033 cur_fline = 0;
1034 max_lineno = 0;
1035 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001036#if ENABLE_FEATURE_LESS_ASK_TERMINAL
1037 if (G.winsize_err)
1038 printf("\033[999;999H" "\033[6n");
1039#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001040 buffer_fill_and_print();
1041}
1042
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001043static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001044{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001045 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001046 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +00001047 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001048
Denis Vlasenko33196372008-02-23 01:25:38 +00001049 pfd[0].fd = STDIN_FILENO;
1050 pfd[0].events = POLLIN;
1051 pfd[1].fd = kbd_fd;
1052 pfd[1].events = POLLIN;
1053 again:
1054 tcsetattr(kbd_fd, TCSANOW, &term_less);
1055 /* NB: select/poll returns whenever read will not block. Therefore:
1056 * if eof is reached, select/poll will return immediately
1057 * because read will immediately return 0 bytes.
1058 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001059 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1060 */
Denis Vlasenko33196372008-02-23 01:25:38 +00001061 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001062 /* Are we interested in stdin? */
Ron Yorstonad1b4d52015-07-24 14:28:50 +01001063 if (at_end()) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001064 if (eof_error > 0) /* did NOT reach eof yet */
1065 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001066 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001067 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +00001068 if (less_gets_pos >= 0)
1069 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001070 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001071
Denys Vlasenko020f4062009-05-17 16:44:54 +02001072 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001073#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001074 while (1) {
1075 int r;
1076 /* NB: SIGWINCH interrupts poll() */
1077 r = poll(pfd + rd, 2 - rd, -1);
1078 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1079 return '\\'; /* anything which has no defined function */
1080 if (r) break;
1081 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001082#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001083 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001084#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001085 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001086
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001087 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
1088 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001089 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
1090 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +00001091 if (errno == EAGAIN) {
1092 /* No keyboard input available. Since poll() did return,
1093 * we should have input on stdin */
1094 read_lines();
1095 buffer_fill_and_print();
1096 goto again;
1097 }
1098 /* EOF/error (ssh session got killed etc) */
1099 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001100 }
Denis Vlasenko33196372008-02-23 01:25:38 +00001101 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001102 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001103}
1104
Denys Vlasenko020f4062009-05-17 16:44:54 +02001105/* Grab a character from input without requiring the return key.
1106 * May return KEYCODE_xxx values.
1107 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001108static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001109{
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001110 int64_t key64;
1111 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001112
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001113 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001114 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001115 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001116 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +00001117
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001118 /* Discard Ctrl-something chars.
1119 * (checking only lower 32 bits is a size optimization:
1120 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1121 */
1122 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +00001123 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +02001124
1125 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001126}
1127
1128static char* less_gets(int sz)
1129{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001130 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001131 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001132 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001133
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001134 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001135 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001136 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001137 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001138 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001139 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001140 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001141 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001142 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001143 if (c == 0x7f)
1144 c = 8;
1145 if (c == 8 && i) {
1146 printf("\x8 \x8");
1147 i--;
1148 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001149 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001150 continue;
1151 if (i >= width - sz - 1)
1152 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001153 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001154 result[i++] = c;
1155 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001156 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001157}
1158
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001159static void examine_file(void)
1160{
Denis Vlasenko33196372008-02-23 01:25:38 +00001161 char *new_fname;
1162
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001163 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001164 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001165 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001166 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001167 err:
1168 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001169 return;
1170 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001171 if (access(new_fname, R_OK) != 0) {
1172 print_statusline("Cannot read this file");
1173 goto err;
1174 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001175 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001176 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001177 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001178 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001179 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001180 num_files++; */
1181 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001182 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001183 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001184}
1185
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001186/* This function changes the file currently being paged. direction can be one of the following:
1187 * -1: go back one file
1188 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001189 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001190static void change_file(int direction)
1191{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001192 if (current_file != ((direction > 0) ? num_files : 1)) {
1193 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001194 free(filename);
1195 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001196 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001197 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001198 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001199 }
1200}
1201
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001202static void remove_current_file(void)
1203{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001204 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001205
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001206 if (num_files < 2)
1207 return;
1208
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001209 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001210 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001211 for (i = 3; i <= num_files; i++)
1212 files[i - 2] = files[i - 1];
1213 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001214 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001215 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001216 for (i = 2; i <= num_files; i++)
1217 files[i - 2] = files[i - 1];
1218 num_files--;
1219 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001220 }
1221}
1222
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001223static void colon_process(void)
1224{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001225 int keypress;
1226
1227 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001228 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001229
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001230 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001231 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001232 case 'd':
1233 remove_current_file();
1234 break;
1235 case 'e':
1236 examine_file();
1237 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001238#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001239 case 'f':
1240 m_status_print();
1241 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001242#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001243 case 'n':
1244 change_file(1);
1245 break;
1246 case 'p':
1247 change_file(-1);
1248 break;
1249 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001250 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001251 break;
1252 case 'x':
1253 change_file(0);
1254 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001255 }
1256}
1257
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001258#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001259static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001260{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001261 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001262 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001263 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001264 match = 0;
1265 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001266}
1267
Rob Landleya2e98042006-04-18 01:53:41 +00001268static void goto_match(int match)
1269{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001270 if (!pattern_valid)
1271 return;
1272 if (match < 0)
1273 match = 0;
1274 /* Try to find next match if eof isn't reached yet */
1275 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001276 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001277 read_lines();
1278 }
1279 if (num_matches) {
1280 normalize_match_pos(match);
1281 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001282 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001283 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001284 }
Rob Landleya2e98042006-04-18 01:53:41 +00001285}
1286
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001287static void fill_match_lines(unsigned pos)
1288{
1289 if (!pattern_valid)
1290 return;
1291 /* Run the regex on each line of the current file */
1292 while (pos <= max_fline) {
1293 /* If this line matches */
1294 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1295 /* and we didn't match it last time */
1296 && !(num_matches && match_lines[num_matches-1] == pos)
1297 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001298 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001299 match_lines[num_matches++] = pos;
1300 }
1301 pos++;
1302 }
1303}
1304
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001305static void regex_process(void)
1306{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001307 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001308
1309 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001310 free(match_lines);
1311 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001312 match_pos = 0;
1313 num_matches = 0;
1314 if (pattern_valid) {
1315 regfree(&pattern);
1316 pattern_valid = 0;
1317 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001318
Rob Landleya2e98042006-04-18 01:53:41 +00001319 /* Get the uncompiled regular expression from the user */
1320 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001321 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001322 uncomp_regex = less_gets(1);
1323 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001324 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001325 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001326 return;
1327 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001328
Rob Landleya2e98042006-04-18 01:53:41 +00001329 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001330 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001331 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001332 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001333 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001334 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001335 free(err);
1336 return;
1337 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001338
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001339 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001340 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001341 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001342 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001343 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001344 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001345 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001346 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001347 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1348 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001349
1350 /* It's possible that no matches are found yet.
1351 * goto_match() will read input looking for match,
1352 * if needed */
1353 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001354}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001355#endif
1356
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001357static void number_process(int first_digit)
1358{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001359 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001360 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001361 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001362 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001363
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001364 num_input[0] = first_digit;
1365
1366 /* Clear the current line, print a prompt, and then print the digit */
1367 clear_line();
1368 printf(":%c", first_digit);
1369
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001370 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001371 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001372 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001373 keypress = less_getch(i + 1);
Ron Yorstonae1a9e82015-07-21 20:12:31 +01001374 if ((unsigned)keypress > 255 || !isdigit(keypress))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001375 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001376 num_input[i] = keypress;
1377 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001378 i++;
1379 }
1380
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001381 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001382 num = bb_strtou(num_input, NULL, 10);
1383 /* on format error, num == -1 */
1384 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001385 buffer_print();
1386 return;
1387 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001388
1389 /* We now know the number and the letter entered, so we process them */
1390 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001391 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001392 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001393 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001394 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001395 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001396 break;
1397 case 'g': case '<': case 'G': case '>':
Ron Yorstond542d182015-07-24 14:29:13 +01001398 buffer_lineno(num - 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001399 break;
1400 case 'p': case '%':
Ron Yorstond542d182015-07-24 14:29:13 +01001401#if ENABLE_FEATURE_LESS_FLAGS
1402 update_num_lines();
1403 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1404#else
1405 num = num * max_lineno / 100;
1406#endif
1407 buffer_lineno(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001408 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001409#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001410 case 'n':
1411 goto_match(match_pos + num);
1412 break;
1413 case '/':
1414 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1415 regex_process();
1416 break;
1417 case '?':
1418 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1419 regex_process();
1420 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001421#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001422 }
1423}
1424
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001425#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001426static void flag_change(void)
1427{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001428 int keypress;
1429
1430 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001431 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001432 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001433
1434 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001435 case 'M':
1436 option_mask32 ^= FLAG_M;
1437 break;
1438 case 'm':
1439 option_mask32 ^= FLAG_m;
1440 break;
1441 case 'E':
1442 option_mask32 ^= FLAG_E;
1443 break;
1444 case '~':
1445 option_mask32 ^= FLAG_TILDE;
1446 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001447#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001448 case 'S':
1449 option_mask32 ^= FLAG_S;
1450 buffer_fill_and_print();
1451 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001452#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001453#if ENABLE_FEATURE_LESS_LINENUMS
1454 case 'N':
1455 option_mask32 ^= FLAG_N;
1456 re_wrap();
1457 buffer_fill_and_print();
1458 break;
1459#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001460 }
1461}
1462
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001463#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001464static void show_flag_status(void)
1465{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001466 int keypress;
1467 int flag_val;
1468
1469 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001470 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001471 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001472
1473 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001474 case 'M':
1475 flag_val = option_mask32 & FLAG_M;
1476 break;
1477 case 'm':
1478 flag_val = option_mask32 & FLAG_m;
1479 break;
1480 case '~':
1481 flag_val = option_mask32 & FLAG_TILDE;
1482 break;
1483 case 'N':
1484 flag_val = option_mask32 & FLAG_N;
1485 break;
1486 case 'E':
1487 flag_val = option_mask32 & FLAG_E;
1488 break;
1489 default:
1490 flag_val = 0;
1491 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001492 }
1493
1494 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001495 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001496}
1497#endif
1498
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001499#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1500
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001501static void save_input_to_file(void)
1502{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001503 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001504 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001505 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001506 FILE *fp;
1507
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001508 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001509 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001510 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001511 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001512 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001513 msg = "Error opening log file";
1514 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001515 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001516 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001517 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001518 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001519 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001520 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001521 ret:
1522 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001523 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001524}
1525
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001526#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001527static void add_mark(void)
1528{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001529 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001530
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001531 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001532 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001533
1534 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001535 /* If we exceed 15 marks, start overwriting previous ones */
1536 if (num_marks == 14)
1537 num_marks = 0;
1538
1539 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001540 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001541 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001542 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001543 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001544 }
1545}
1546
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001547static void goto_mark(void)
1548{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001549 int letter;
1550 int i;
1551
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001552 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001553 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001554 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001555
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001556 if (isalpha(letter)) {
1557 for (i = 0; i <= num_marks; i++)
1558 if (letter == mark_lines[i][0]) {
1559 buffer_line(mark_lines[i][1]);
1560 break;
1561 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001562 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001563 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001564 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001565 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001566}
1567#endif
1568
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001569#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001570static char opp_bracket(char bracket)
1571{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001572 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001573 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1574 bracket++;
1575 case '(': /* ')' == '(' + 1 */
1576 bracket++;
1577 break;
1578 case '}': case ']':
1579 bracket--;
1580 case ')':
1581 bracket--;
1582 break;
1583 };
1584 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001585}
1586
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001587static void match_right_bracket(char bracket)
1588{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001589 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001590
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001591 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001592 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001593 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001594 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001595 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001596 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001597 if (strchr(flines[i], bracket) != NULL) {
1598 buffer_line(i);
1599 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001600 }
1601 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001602 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001603}
1604
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001605static void match_left_bracket(char bracket)
1606{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001607 int i;
1608
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001609 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001610 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001611 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001612 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001613
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001614 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001615 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001616 if (strchr(flines[i], bracket) != NULL) {
1617 buffer_line(i);
1618 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001619 }
1620 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001621 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001622}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001623#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001624
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001625static void keypress_process(int keypress)
1626{
Rob Landley9200e792005-09-15 19:26:59 +00001627 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001628 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001629 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001630 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001631 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001632 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001633 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001634 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001635 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001636 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001637 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001638 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001639 break;
1640 case 'd':
1641 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001642 break;
1643 case 'u':
1644 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001645 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001646 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001647 buffer_line(0);
1648 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001649 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001650 cur_fline = MAXLINES;
1651 read_lines();
1652 buffer_line(cur_fline);
1653 break;
1654 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001655 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001656 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001657#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001658 case 'm':
1659 add_mark();
1660 buffer_print();
1661 break;
1662 case '\'':
1663 goto_mark();
1664 buffer_print();
1665 break;
Rob Landley9200e792005-09-15 19:26:59 +00001666#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001667 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001668 /* TODO: (1) also bind ^R, ^L to this?
1669 * (2) re-measure window size?
1670 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001671 buffer_print();
1672 break;
1673 /*case 'R':
1674 full_repaint();
1675 break;*/
1676 case 's':
1677 save_input_to_file();
1678 break;
1679 case 'E':
1680 examine_file();
1681 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001682#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001683 case '=':
1684 m_status_print();
1685 break;
Rob Landley9200e792005-09-15 19:26:59 +00001686#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001687#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001688 case '/':
1689 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1690 regex_process();
1691 break;
1692 case 'n':
1693 goto_match(match_pos + 1);
1694 break;
1695 case 'N':
1696 goto_match(match_pos - 1);
1697 break;
1698 case '?':
1699 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1700 regex_process();
1701 break;
Rob Landley9200e792005-09-15 19:26:59 +00001702#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001703#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001704 case '-':
1705 flag_change();
1706 buffer_print();
1707 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001708#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001709 case '_':
1710 show_flag_status();
1711 break;
Rob Landley9200e792005-09-15 19:26:59 +00001712#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001713#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001714#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001715 case '{': case '(': case '[':
1716 match_right_bracket(keypress);
1717 break;
1718 case '}': case ')': case ']':
1719 match_left_bracket(keypress);
1720 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001721#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001722 case ':':
1723 colon_process();
1724 break;
Rob Landley9200e792005-09-15 19:26:59 +00001725 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001726
Rob Landley9200e792005-09-15 19:26:59 +00001727 if (isdigit(keypress))
1728 number_process(keypress);
1729}
1730
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001731static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001732{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001733 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001734}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001735
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001736#if ENABLE_FEATURE_LESS_WINCH
1737static void sigwinch_handler(int sig UNUSED_PARAM)
1738{
1739 winch_counter++;
1740}
1741#endif
1742
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001743int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001744int less_main(int argc, char **argv)
1745{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001746 char *tty_name;
1747 int tty_fd;
1748
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001749 INIT_G();
1750
Denys Vlasenko821e6432014-01-22 16:36:22 +01001751 /* TODO: -x: do not interpret backspace, -xx: tab also
1752 * -xxx: newline also
1753 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1754 * -s: condense many empty lines to one
1755 * (used by some setups for manpage display)
1756 */
Ron Yorston51aa8612015-07-19 11:12:29 +01001757 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s");
Rob Landley9200e792005-09-15 19:26:59 +00001758 argc -= optind;
1759 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001760 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001761 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001762
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001763 /* Another popular pager, most, detects when stdout
1764 * is not a tty and turns into cat. This makes sense. */
1765 if (!isatty(STDOUT_FILENO))
1766 return bb_cat(argv);
1767
Rob Landley9200e792005-09-15 19:26:59 +00001768 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001769 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001770 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001771 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001772 bb_show_usage();
1773 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001774 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001775 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001776 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001777
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001778 if (option_mask32 & FLAG_TILDE)
1779 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001780
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001781 /* Some versions of less can survive w/o controlling tty,
1782 * try to do the same. This also allows to specify an alternative
1783 * tty via "less 1<>TTY".
1784 * We don't try to use STDOUT_FILENO directly,
1785 * since we want to set this fd to non-blocking mode,
1786 * and not bother with restoring it on exit.
1787 */
1788 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1789 if (tty_name) {
1790 tty_fd = open(tty_name, O_RDONLY);
1791 free(tty_name);
1792 if (tty_fd < 0)
1793 goto try_ctty;
1794 } else {
1795 /* Try controlling tty */
1796 try_ctty:
1797 tty_fd = open(CURRENT_TTY, O_RDONLY);
1798 if (tty_fd < 0)
1799 return bb_cat(argv);
1800 }
1801 ndelay_on(tty_fd);
1802 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001803
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001804 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001805 term_less = term_orig;
1806 term_less.c_lflag &= ~(ICANON | ECHO);
1807 term_less.c_iflag &= ~(IXON | ICRNL);
1808 /*term_less.c_oflag &= ~ONLCR;*/
1809 term_less.c_cc[VMIN] = 1;
1810 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001811
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001812 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001813 /* 20: two tabstops + 4 */
1814 if (width < 20 || max_displayed_line < 3)
1815 return bb_cat(argv);
1816 max_displayed_line -= 2;
1817
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001818 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001819 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001820#if ENABLE_FEATURE_LESS_WINCH
1821 signal(SIGWINCH, sigwinch_handler);
1822#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001823
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001824 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001825 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001826 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001827 int64_t keypress;
1828
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001829#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001830 while (WINCH_COUNTER) {
1831 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001832 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001833 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1834 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001835 /* 20: two tabstops + 4 */
1836 if (width < 20)
1837 width = 20;
1838 if (max_displayed_line < 3)
1839 max_displayed_line = 3;
1840 max_displayed_line -= 2;
1841 free(buffer);
1842 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001843 /* Avoid re-wrap and/or redraw if we already know
1844 * we need to do it again. These ops are expensive */
1845 if (WINCH_COUNTER)
1846 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001847 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001848 if (WINCH_COUNTER)
1849 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001850 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001851 /* This took some time. Loop back and check,
1852 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001853 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001854 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001855# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1856 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1857 uint32_t rc = (keypress >> 32);
1858 width = (rc & 0x7fff);
1859 max_displayed_line = ((rc >> 16) & 0x7fff);
1860 goto got_size;
1861 }
1862# endif
1863#else
1864 keypress = less_getch(-1); /* -1: do not position cursor */
1865#endif
Rob Landley9200e792005-09-15 19:26:59 +00001866 keypress_process(keypress);
1867 }
1868}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001869
1870/*
1871Help text of less version 418 is below.
1872If you are implementing something, keeping
1873key and/or command line switch compatibility is a good idea:
1874
1875
1876 SUMMARY OF LESS COMMANDS
1877
1878 Commands marked with * may be preceded by a number, N.
1879 Notes in parentheses indicate the behavior if N is given.
1880 h H Display this help.
1881 q :q Q :Q ZZ Exit.
1882 ---------------------------------------------------------------------------
1883 MOVING
1884 e ^E j ^N CR * Forward one line (or N lines).
1885 y ^Y k ^K ^P * Backward one line (or N lines).
1886 f ^F ^V SPACE * Forward one window (or N lines).
1887 b ^B ESC-v * Backward one window (or N lines).
1888 z * Forward one window (and set window to N).
1889 w * Backward one window (and set window to N).
1890 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1891 d ^D * Forward one half-window (and set half-window to N).
1892 u ^U * Backward one half-window (and set half-window to N).
1893 ESC-) RightArrow * Left one half screen width (or N positions).
1894 ESC-( LeftArrow * Right one half screen width (or N positions).
1895 F Forward forever; like "tail -f".
1896 r ^R ^L Repaint screen.
1897 R Repaint screen, discarding buffered input.
1898 ---------------------------------------------------
1899 Default "window" is the screen height.
1900 Default "half-window" is half of the screen height.
1901 ---------------------------------------------------------------------------
1902 SEARCHING
1903 /pattern * Search forward for (N-th) matching line.
1904 ?pattern * Search backward for (N-th) matching line.
1905 n * Repeat previous search (for N-th occurrence).
1906 N * Repeat previous search in reverse direction.
1907 ESC-n * Repeat previous search, spanning files.
1908 ESC-N * Repeat previous search, reverse dir. & spanning files.
1909 ESC-u Undo (toggle) search highlighting.
1910 ---------------------------------------------------
1911 Search patterns may be modified by one or more of:
1912 ^N or ! Search for NON-matching lines.
1913 ^E or * Search multiple files (pass thru END OF FILE).
1914 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1915 ^K Highlight matches, but don't move (KEEP position).
1916 ^R Don't use REGULAR EXPRESSIONS.
1917 ---------------------------------------------------------------------------
1918 JUMPING
1919 g < ESC-< * Go to first line in file (or line N).
1920 G > ESC-> * Go to last line in file (or line N).
1921 p % * Go to beginning of file (or N percent into file).
1922 t * Go to the (N-th) next tag.
1923 T * Go to the (N-th) previous tag.
1924 { ( [ * Find close bracket } ) ].
1925 } ) ] * Find open bracket { ( [.
1926 ESC-^F <c1> <c2> * Find close bracket <c2>.
1927 ESC-^B <c1> <c2> * Find open bracket <c1>
1928 ---------------------------------------------------
1929 Each "find close bracket" command goes forward to the close bracket
1930 matching the (N-th) open bracket in the top line.
1931 Each "find open bracket" command goes backward to the open bracket
1932 matching the (N-th) close bracket in the bottom line.
1933 m<letter> Mark the current position with <letter>.
1934 '<letter> Go to a previously marked position.
1935 '' Go to the previous position.
1936 ^X^X Same as '.
1937 ---------------------------------------------------
1938 A mark is any upper-case or lower-case letter.
1939 Certain marks are predefined:
1940 ^ means beginning of the file
1941 $ means end of the file
1942 ---------------------------------------------------------------------------
1943 CHANGING FILES
1944 :e [file] Examine a new file.
1945 ^X^V Same as :e.
1946 :n * Examine the (N-th) next file from the command line.
1947 :p * Examine the (N-th) previous file from the command line.
1948 :x * Examine the first (or N-th) file from the command line.
1949 :d Delete the current file from the command line list.
1950 = ^G :f Print current file name.
1951 ---------------------------------------------------------------------------
1952 MISCELLANEOUS COMMANDS
1953 -<flag> Toggle a command line option [see OPTIONS below].
1954 --<name> Toggle a command line option, by name.
1955 _<flag> Display the setting of a command line option.
1956 __<name> Display the setting of an option, by name.
1957 +cmd Execute the less cmd each time a new file is examined.
1958 !command Execute the shell command with $SHELL.
1959 |Xcommand Pipe file between current pos & mark X to shell command.
1960 v Edit the current file with $VISUAL or $EDITOR.
1961 V Print version number of "less".
1962 ---------------------------------------------------------------------------
1963 OPTIONS
1964 Most options may be changed either on the command line,
1965 or from within less by using the - or -- command.
1966 Options may be given in one of two forms: either a single
Maninder Singh97c64912015-05-25 13:46:36 +02001967 character preceded by a -, or a name preceded by --.
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001968 -? ........ --help
1969 Display help (from command line).
1970 -a ........ --search-skip-screen
1971 Forward search skips current screen.
1972 -b [N] .... --buffers=[N]
1973 Number of buffers.
1974 -B ........ --auto-buffers
1975 Don't automatically allocate buffers for pipes.
1976 -c ........ --clear-screen
1977 Repaint by clearing rather than scrolling.
1978 -d ........ --dumb
1979 Dumb terminal.
1980 -D [xn.n] . --color=xn.n
1981 Set screen colors. (MS-DOS only)
1982 -e -E .... --quit-at-eof --QUIT-AT-EOF
1983 Quit at end of file.
1984 -f ........ --force
1985 Force open non-regular files.
1986 -F ........ --quit-if-one-screen
1987 Quit if entire file fits on first screen.
1988 -g ........ --hilite-search
1989 Highlight only last match for searches.
1990 -G ........ --HILITE-SEARCH
1991 Don't highlight any matches for searches.
1992 -h [N] .... --max-back-scroll=[N]
1993 Backward scroll limit.
1994 -i ........ --ignore-case
1995 Ignore case in searches that do not contain uppercase.
1996 -I ........ --IGNORE-CASE
1997 Ignore case in all searches.
1998 -j [N] .... --jump-target=[N]
1999 Screen position of target lines.
2000 -J ........ --status-column
2001 Display a status column at left edge of screen.
2002 -k [file] . --lesskey-file=[file]
2003 Use a lesskey file.
2004 -L ........ --no-lessopen
2005 Ignore the LESSOPEN environment variable.
2006 -m -M .... --long-prompt --LONG-PROMPT
2007 Set prompt style.
2008 -n -N .... --line-numbers --LINE-NUMBERS
2009 Don't use line numbers.
2010 -o [file] . --log-file=[file]
2011 Copy to log file (standard input only).
2012 -O [file] . --LOG-FILE=[file]
2013 Copy to log file (unconditionally overwrite).
2014 -p [pattern] --pattern=[pattern]
2015 Start at pattern (from command line).
2016 -P [prompt] --prompt=[prompt]
2017 Define new prompt.
2018 -q -Q .... --quiet --QUIET --silent --SILENT
2019 Quiet the terminal bell.
2020 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2021 Output "raw" control characters.
2022 -s ........ --squeeze-blank-lines
2023 Squeeze multiple blank lines.
2024 -S ........ --chop-long-lines
2025 Chop long lines.
2026 -t [tag] .. --tag=[tag]
2027 Find a tag.
2028 -T [tagsfile] --tag-file=[tagsfile]
2029 Use an alternate tags file.
2030 -u -U .... --underline-special --UNDERLINE-SPECIAL
2031 Change handling of backspaces.
2032 -V ........ --version
2033 Display the version number of "less".
2034 -w ........ --hilite-unread
2035 Highlight first new line after forward-screen.
2036 -W ........ --HILITE-UNREAD
2037 Highlight first new line after any forward movement.
2038 -x [N[,...]] --tabs=[N[,...]]
2039 Set tab stops.
2040 -X ........ --no-init
2041 Don't use termcap init/deinit strings.
2042 --no-keypad
2043 Don't use termcap keypad init/deinit strings.
2044 -y [N] .... --max-forw-scroll=[N]
2045 Forward scroll limit.
2046 -z [N] .... --window=[N]
2047 Set size of window.
2048 -" [c[c]] . --quotes=[c[c]]
2049 Set shell quote characters.
2050 -~ ........ --tilde
2051 Don't display tildes after end of file.
2052 -# [N] .... --shift=[N]
2053 Horizontal scroll amount (0 = one half screen width)
2054
2055 ---------------------------------------------------------------------------
2056 LINE EDITING
2057 These keys can be used to edit text being entered
2058 on the "command line" at the bottom of the screen.
2059 RightArrow ESC-l Move cursor right one character.
2060 LeftArrow ESC-h Move cursor left one character.
2061 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2062 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2063 HOME ESC-0 Move cursor to start of line.
2064 END ESC-$ Move cursor to end of line.
2065 BACKSPACE Delete char to left of cursor.
2066 DELETE ESC-x Delete char under cursor.
2067 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2068 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2069 CNTL-U ESC (MS-DOS only) Delete entire line.
2070 UpArrow ESC-k Retrieve previous command line.
2071 DownArrow ESC-j Retrieve next command line.
2072 TAB Complete filename & cycle.
2073 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2074 CNTL-L Complete filename, list all.
2075*/