blob: d60e6d920b3163c881de971f204a207ea50bf9ee [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 */
Mike Frysingerf284c762006-04-16 20:38:26 +00009/*
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000010 * TODO:
11 * - Add more regular expression support - search modifiers, certain matches, etc.
12 * - Add more complex bracket searching - currently, nested brackets are
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000013 * not considered.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000014 * - Add support for "F" as an input. This causes less to act in
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000015 * a similar way to tail -f.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000016 * - Allow horizontal scrolling.
Rob Landley9200e792005-09-15 19:26:59 +000017 *
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000018 * Notes:
19 * - the inp file pointer is used so that keyboard input works after
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000020 * redirected input has been read from stdin
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000021 */
Denys Vlasenko4e552a72011-07-25 15:18:20 +020022//config:config LESS
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020023//config: bool "less (15 kb)"
Denys Vlasenko4e552a72011-07-25 15:18:20 +020024//config: default y
25//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//config: 'less' is a pager, meaning that it displays text files. It possesses
27//config: a wide array of features, and is an improvement over 'more'.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020028//config:
29//config:config FEATURE_LESS_MAXLINES
30//config: int "Max number of input lines less will try to eat"
31//config: default 9999999
32//config: depends on LESS
33//config:
34//config:config FEATURE_LESS_BRACKETS
35//config: bool "Enable bracket searching"
36//config: default y
37//config: depends on LESS
38//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: This option adds the capability to search for matching left and right
40//config: brackets, facilitating programming.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020041//config:
42//config:config FEATURE_LESS_FLAGS
Denys Vlasenko53772862012-04-17 16:11:25 +020043//config: bool "Enable -m/-M"
Denys Vlasenko4e552a72011-07-25 15:18:20 +020044//config: default y
45//config: depends on LESS
46//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020047//config: The -M/-m flag enables a more sophisticated status line.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020048//config:
Ron Yorston51aa8612015-07-19 11:12:29 +010049//config:config FEATURE_LESS_TRUNCATE
50//config: bool "Enable -S"
51//config: default y
52//config: depends on LESS
53//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020054//config: The -S flag causes long lines to be truncated rather than
55//config: wrapped.
Ron Yorston51aa8612015-07-19 11:12:29 +010056//config:
Denys Vlasenko4e552a72011-07-25 15:18:20 +020057//config:config FEATURE_LESS_MARKS
58//config: bool "Enable marks"
59//config: default y
60//config: depends on LESS
61//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020062//config: Marks enable positions in a file to be stored for easy reference.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020063//config:
64//config:config FEATURE_LESS_REGEXP
65//config: bool "Enable regular expressions"
66//config: default y
67//config: depends on LESS
68//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020069//config: Enable regular expressions, allowing complex file searches.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020070//config:
71//config:config FEATURE_LESS_WINCH
72//config: bool "Enable automatic resizing on window size changes"
73//config: default y
74//config: depends on LESS
75//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020076//config: Makes less track window size changes.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020077//config:
78//config:config FEATURE_LESS_ASK_TERMINAL
79//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
80//config: default y
81//config: depends on FEATURE_LESS_WINCH
82//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020083//config: Makes less track window size changes.
84//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
85//config: this option makes less perform a last-ditch effort to find it:
86//config: position cursor to 999,999 and ask terminal to report real
87//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
88//config: This is not clean but helps a lot on serial lines and such.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020089//config:
90//config:config FEATURE_LESS_DASHCMD
91//config: bool "Enable flag changes ('-' command)"
92//config: default y
93//config: depends on LESS
94//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020095//config: This enables the ability to change command-line flags within
96//config: less itself ('-' keyboard command).
Denys Vlasenko4e552a72011-07-25 15:18:20 +020097//config:
98//config:config FEATURE_LESS_LINENUMS
Denys Vlasenkof5604222017-01-10 14:58:54 +010099//config: bool "Enable -N (dynamic switching of line numbers)"
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200100//config: default y
101//config: depends on FEATURE_LESS_DASHCMD
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200102
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +0100103//applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP))
104
105//kbuild:lib-$(CONFIG_LESS) += less.o
106
Pere Orga5bc8c002011-04-11 03:29:49 +0200107//usage:#define less_trivial_usage
Ron Yorston51aa8612015-07-19 11:12:29 +0100108//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
109//usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200110//usage:#define less_full_usage "\n\n"
111//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200112//usage: "\n -E Quit once the end of a file is reached"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100113//usage: IF_FEATURE_LESS_REGEXP(
114//usage: "\n -I Ignore case in all searches"
115//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200116//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200117//usage: "\n -M,-m Display status line with line numbers"
118//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200119//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200120//usage: "\n -N Prefix line number to each line"
Ron Yorston51aa8612015-07-19 11:12:29 +0100121//usage: IF_FEATURE_LESS_TRUNCATE(
122//usage: "\n -S Truncate long lines"
123//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200124//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200125
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200126#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000127
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000128#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200129#include "common_bufsiz.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"
Denys Vlasenko8187e012017-09-13 22:48:30 +0200138#define NORMAL ESC"[m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200139/* The escape code to home and clear to the end of screen */
Denys Vlasenko73af7052017-09-13 19:20:27 +0200140#define CLEAR ESC"[H"ESC"[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 */
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200171 int kbd_fd_orig_flags;
Denis Vlasenko33196372008-02-23 01:25:38 +0000172 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000173/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000174 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000175 unsigned max_fline;
176 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000177 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000178 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000179#if ENABLE_FEATURE_LESS_WINCH
180 unsigned winch_counter;
181#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000182 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000183 ssize_t readpos;
184 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000185 const char **buffer;
186 const char **flines;
187 const char *empty_line_marker;
188 unsigned num_files;
189 unsigned current_file;
190 char *filename;
191 char **files;
Ron Yorston193ba402015-07-21 22:28:09 +0200192#if ENABLE_FEATURE_LESS_FLAGS
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200193 int num_lines; /* a flag if < 0, line count if >= 0 */
194# define REOPEN_AND_COUNT (-1)
195# define REOPEN_STDIN (-2)
196# define NOT_REGULAR_FILE (-3)
Ron Yorston193ba402015-07-21 22:28:09 +0200197#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000198#if ENABLE_FEATURE_LESS_MARKS
199 unsigned num_marks;
200 unsigned mark_lines[15][2];
201#endif
202#if ENABLE_FEATURE_LESS_REGEXP
203 unsigned *match_lines;
204 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000205 int wanted_match; /* signed! */
206 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000207 regex_t pattern;
208 smallint pattern_valid;
209#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200210#if ENABLE_FEATURE_LESS_ASK_TERMINAL
211 smallint winsize_err;
212#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000213 smallint terminated;
214 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000215 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000216};
217#define G (*ptr_to_globals)
218#define cur_fline (G.cur_fline )
219#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000220#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000221#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000222#define max_fline (G.max_fline )
223#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000224#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000225#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000226#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000227/* This one is 100% not cached by compiler on read access */
228#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000229#define eof_error (G.eof_error )
230#define readpos (G.readpos )
231#define readeof (G.readeof )
232#define buffer (G.buffer )
233#define flines (G.flines )
234#define empty_line_marker (G.empty_line_marker )
235#define num_files (G.num_files )
236#define current_file (G.current_file )
237#define filename (G.filename )
238#define files (G.files )
Ron Yorston193ba402015-07-21 22:28:09 +0200239#define num_lines (G.num_lines )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000240#define num_marks (G.num_marks )
241#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000242#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000243#define match_lines (G.match_lines )
244#define match_pos (G.match_pos )
245#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000246#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000247#define pattern (G.pattern )
248#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000249#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000250#define terminated (G.terminated )
251#define term_orig (G.term_orig )
252#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000253#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000254#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000255 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
256 less_gets_pos = -1; \
257 empty_line_marker = "~"; \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000258 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
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200267 * line number. Accessor: */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000268#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 */
Denys Vlasenkod54f58d2017-09-14 10:51:12 +0200281static void move_cursor(int line, int col)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000282{
Denys Vlasenkod54f58d2017-09-14 10:51:12 +0200283 printf(ESC"[%u;%uH", line, col);
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();
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200306 if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
307 ndelay_off(kbd_fd);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000308 clear_line();
309 if (code < 0)
310 kill_myself_with_sig(- code); /* does not return */
311 exit(code);
312}
313
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000314#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
315 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000316static void re_wrap(void)
317{
318 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000319 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000320 int src_idx;
321 int dst_idx;
322 int new_cur_fline = 0;
323 uint32_t lineno;
324 char linebuf[w + 1];
325 const char **old_flines = flines;
326 const char *s;
327 char **new_flines = NULL;
328 char *d;
329
330 if (option_mask32 & FLAG_N)
331 w -= 8;
332
333 src_idx = 0;
334 dst_idx = 0;
335 s = old_flines[0];
336 lineno = LINENO(s);
337 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000338 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000339 while (1) {
340 *d = *s;
341 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000342 new_line_pos++;
Ron Yorston78cfa002015-07-19 21:41:09 +0100343 if (*d == '\t') { /* tab */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000344 new_line_pos += 7;
Ron Yorston78cfa002015-07-19 21:41:09 +0100345 new_line_pos &= (~7);
346 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000347 s++;
348 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000349 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000350 int sz;
351 /* new line is full, create next one */
352 *d = '\0';
353 next_new:
354 sz = (d - linebuf) + 1; /* + 1: NUL */
355 d = ((char*)xmalloc(sz + 4)) + 4;
356 LINENO(d) = lineno;
357 memcpy(d, linebuf, sz);
358 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
359 new_flines[dst_idx] = d;
360 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000361 if (new_line_pos < w) {
362 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000363 if (src_idx > max_fline)
364 break;
365 lineno = LINENO(s);
366 }
367 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000368 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000369 }
370 continue;
371 }
372 /* *d == NUL: old line ended, go to next old one */
373 free(MEMPTR(old_flines[src_idx]));
374 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000375 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000376 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000377 src_idx++;
378 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000379 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000380 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000381 s = old_flines[src_idx];
382 if (lineno != LINENO(s)) {
383 /* this is not a continuation line!
384 * create next _new_ line too */
385 goto next_new;
386 }
387 }
388
389 free(old_flines);
390 flines = (const char **)new_flines;
391
392 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000393 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000394 cur_fline = new_cur_fline;
395 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000396#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000397 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000398#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000399}
400#endif
401
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000402#if ENABLE_FEATURE_LESS_REGEXP
403static void fill_match_lines(unsigned pos);
404#else
405#define fill_match_lines(pos) ((void)0)
406#endif
407
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100408static int at_end(void)
409{
410 return (option_mask32 & FLAG_S)
411 ? !(cur_fline <= max_fline &&
412 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
413 : !(max_fline > cur_fline + max_displayed_line);
414}
415
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000416/* Devilishly complex routine.
417 *
418 * Has to deal with EOF and EPIPE on input,
419 * with line wrapping, with last line not ending in '\n'
420 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000421 * It reads input again if last time we got an EOF (thus supporting
422 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000423 *
424 * Variables used:
425 * flines[] - array of lines already read. Linewrap may cause
426 * one source file line to occupy several flines[n].
427 * flines[max_fline] - last line, possibly incomplete.
428 * terminated - 1 if flines[max_fline] is 'terminated'
429 * (if there was '\n' [which isn't stored itself, we just remember
430 * that it was seen])
431 * max_lineno - last line's number, this one doesn't increment
432 * on line wrap, only on "real" new lines.
433 * readbuf[0..readeof-1] - small preliminary buffer.
434 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000435 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000436 * (takes into account tabs and backspaces)
437 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200438 *
439 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
440 * "/search on very long input" and "reaching max line count" corner cases.
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000441 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000442static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000443{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000444 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000445 int w = width;
446 char last_terminated = terminated;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200447 time_t last_time = 0;
448 int retry_EAGAIN = 2;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000449#if ENABLE_FEATURE_LESS_REGEXP
450 unsigned old_max_fline = max_fline;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000451#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000452
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200453#define readbuf bb_common_bufsiz1
454 setup_common_bufsiz();
455
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200456 /* (careful: max_fline can be -1) */
457 if (max_fline + 1 > MAXLINES)
458 return;
459
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000460 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000461 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000462
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100463 p = current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000464 if (!last_terminated) {
465 const char *cp = flines[max_fline];
Denys Vlasenko865814a2014-09-22 21:17:24 +0200466 p = stpcpy(p, cp);
467 free(MEMPTR(cp));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000468 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000469 } else {
Denys Vlasenko865814a2014-09-22 21:17:24 +0200470 max_fline++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000471 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000472 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000473
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000474 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000475 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000476 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000477 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000478 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000479 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000480 if (readpos >= readeof) {
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200481 int flags = ndelay_on(0);
482
483 while (1) {
484 time_t t;
485
486 errno = 0;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200487 eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200488 if (errno != EAGAIN)
489 break;
490 t = time(NULL);
491 if (t != last_time) {
492 last_time = t;
493 if (--retry_EAGAIN < 0)
494 break;
495 }
496 sched_yield();
497 }
498 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000499 readpos = 0;
500 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000501 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000502 goto reached_eof;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200503 retry_EAGAIN = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000504 }
505 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000506 /* backspace? [needed for manpages] */
507 /* <tab><bs> is (a) insane and */
508 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000509 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000510 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000511 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000512 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000513 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000514 continue;
515 }
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100516 {
517 size_t new_last_line_pos = last_line_pos + 1;
518 if (c == '\t') {
519 new_last_line_pos += 7;
520 new_last_line_pos &= (~7);
521 }
522 if ((int)new_last_line_pos > w)
523 break;
524 last_line_pos = new_last_line_pos;
525 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000526 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000527 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000528 if (c == '\n') {
529 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000530 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000531 break;
532 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000533 /* NUL is substituted by '\n'! */
534 if (c == '\0') c = '\n';
535 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000536 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000537 } /* end of "read chars until we have a line" loop */
Denys Vlasenko15943c82014-09-21 22:10:55 +0200538#if 0
539//BUG: also triggers on this:
540// { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
541// (resulting in lost empty line between "foo" and "bar" lines)
542// the "terminated" logic needs fixing (or explaining)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000543 /* Corner case: linewrap with only "" wrapping to next line */
544 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000545 if (!last_terminated && !current_line[0]) {
546 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000547 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000548 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000549 }
Denys Vlasenko15943c82014-09-21 22:10:55 +0200550#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000551 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000552 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000553 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000554
555 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
556 LINENO(flines[max_fline]) = max_lineno;
557 if (terminated)
558 max_lineno++;
559
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000560 if (max_fline >= MAXLINES) {
561 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000562 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000563 }
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100564 if (!at_end()) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000565#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000566 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000567#else
568 if (wanted_match >= num_matches) { /* goto_match called us */
569 fill_match_lines(old_max_fline);
570 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000571 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000572 if (wanted_match < num_matches)
573 break;
574#endif
575 }
576 if (eof_error <= 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000577 break;
578 }
579 max_fline++;
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100580 current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000581 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000582 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000583 } /* end of "read lines until we reach cur_fline" loop */
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200584
585 if (eof_error < 0) {
586 if (errno == EAGAIN) {
587 eof_error = 1;
588 } else {
589 print_statusline(bb_msg_read_error);
590 }
591 }
Ron Yorston193ba402015-07-21 22:28:09 +0200592#if ENABLE_FEATURE_LESS_FLAGS
593 else if (eof_error == 0)
594 num_lines = max_lineno;
595#endif
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200596
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000597 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000598#if ENABLE_FEATURE_LESS_REGEXP
599 /* prevent us from being stuck in search for a match */
600 wanted_match = -1;
601#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000602#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000603}
604
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000605#if ENABLE_FEATURE_LESS_FLAGS
Ron Yorston193ba402015-07-21 22:28:09 +0200606static int safe_lineno(int fline)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000607{
Ron Yorston193ba402015-07-21 22:28:09 +0200608 if (fline >= max_fline)
609 fline = max_fline - 1;
610
611 /* also catches empty file (max_fline == 0) */
612 if (fline < 0)
613 return 0;
614
615 return LINENO(flines[fline]) + 1;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000616}
617
Ron Yorston159e0322015-07-24 14:27:42 +0100618/* count number of lines in file */
619static void update_num_lines(void)
620{
621 int count, fd;
Ron Yorstonb27cf312015-07-31 17:33:24 +0100622 struct stat stbuf;
Ron Yorston159e0322015-07-24 14:27:42 +0100623 ssize_t len, i;
624 char buf[4096];
Ron Yorston159e0322015-07-24 14:27:42 +0100625
Ron Yorston70b84be2015-07-24 14:28:08 +0100626 /* only do this for regular files */
Ron Yorstonb27cf312015-07-31 17:33:24 +0100627 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
Ron Yorston159e0322015-07-24 14:27:42 +0100628 count = 0;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200629 fd = open("/proc/self/fd/0", O_RDONLY);
630 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
631 /* "filename" is valid only if REOPEN_AND_COUNT */
632 fd = open(filename, O_RDONLY);
633 }
Ron Yorston70b84be2015-07-24 14:28:08 +0100634 if (fd < 0) {
635 /* somebody stole my file! */
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200636 num_lines = NOT_REGULAR_FILE;
Ron Yorston70b84be2015-07-24 14:28:08 +0100637 return;
638 }
Ron Yorstonb27cf312015-07-31 17:33:24 +0100639 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
640 num_lines = NOT_REGULAR_FILE;
641 goto do_close;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200642 }
Ron Yorston159e0322015-07-24 14:27:42 +0100643 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
644 for (i = 0; i < len; ++i) {
645 if (buf[i] == '\n' && ++count == MAXLINES)
646 goto done;
647 }
648 }
649 done:
650 num_lines = count;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200651 do_close:
Ron Yorston159e0322015-07-24 14:27:42 +0100652 close(fd);
Ron Yorston159e0322015-07-24 14:27:42 +0100653 }
654}
655
Rob Landley9200e792005-09-15 19:26:59 +0000656/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000657static void m_status_print(void)
658{
Ron Yorston193ba402015-07-21 22:28:09 +0200659 int first, last;
660 unsigned percent;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000661
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200662 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000663 return;
664
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000665 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000666 printf(HIGHLIGHT"%s", filename);
667 if (num_files > 1)
668 printf(" (file %i of %i)", current_file, num_files);
Ron Yorston193ba402015-07-21 22:28:09 +0200669
670 first = safe_lineno(cur_fline);
671 last = (option_mask32 & FLAG_S)
672 ? MIN(first + max_displayed_line, max_lineno)
673 : safe_lineno(cur_fline + max_displayed_line);
674 printf(" lines %i-%i", first, last);
675
Ron Yorston159e0322015-07-24 14:27:42 +0100676 update_num_lines();
Ron Yorston193ba402015-07-21 22:28:09 +0200677 if (num_lines >= 0)
678 printf("/%i", num_lines);
679
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100680 if (at_end()) {
Ron Yorston193ba402015-07-21 22:28:09 +0200681 printf(" (END)");
682 if (num_files > 1 && current_file != num_files)
683 printf(" - next: %s", files[current_file]);
684 } else if (num_lines > 0) {
685 percent = (100 * last + num_lines/2) / num_lines;
686 printf(" %i%%", percent <= 100 ? percent : 100);
687 }
688 printf(NORMAL);
Rob Landley9200e792005-09-15 19:26:59 +0000689}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000690#endif
691
Rob Landley9200e792005-09-15 19:26:59 +0000692/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000693static void status_print(void)
694{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000695 const char *p;
696
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200697 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000698 return;
699
Rob Landley9200e792005-09-15 19:26:59 +0000700 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000701#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000702 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000703 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000704 return;
Rob Landley9200e792005-09-15 19:26:59 +0000705 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000706 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000707#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000708
709 clear_line();
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100710 if (cur_fline && !at_end()) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000711 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000712 return;
713 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000714 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000715 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000716 p = filename;
717 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000718 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000719 p, current_file, num_files);
720 return;
721 }
722 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000723}
724
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000725static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000726 /* NUL: never encountered; TAB: not converted */
727 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000728 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
729 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000730static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100731 /* why 40 instead of 4a below? - it is a replacement for '\n'.
732 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000733 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
734 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
735
Ron Yorstonf06386a2015-07-18 16:20:03 +0100736static void print_lineno(const char *line)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000737{
Ron Yorstonf06386a2015-07-18 16:20:03 +0100738 const char *fmt = " ";
739 unsigned n = n; /* for compiler */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000740
Ron Yorstonf06386a2015-07-18 16:20:03 +0100741 if (line != empty_line_marker) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000742 /* Width of 7 preserves tab spacing in the text */
743 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000744 n = LINENO(line) + 1;
Ron Yorstonf06386a2015-07-18 16:20:03 +0100745 if (n > 9999999 && MAXLINES > 9999999) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000746 n %= 10000000;
747 fmt = "%07u ";
748 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000749 }
Ron Yorstonf06386a2015-07-18 16:20:03 +0100750 printf(fmt, n);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000751}
752
753
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000754#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000755static void print_found(const char *line)
756{
757 int match_status;
758 int eflags;
759 char *growline;
760 regmatch_t match_structs;
761
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100762 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000763 const char *str = line;
764 char *p = buf;
765 size_t n;
766
767 while (*str) {
768 n = strcspn(str, controls);
769 if (n) {
770 if (!str[n]) break;
771 memcpy(p, str, n);
772 p += n;
773 str += n;
774 }
775 n = strspn(str, controls);
776 memset(p, '.', n);
777 p += n;
778 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000779 }
780 strcpy(p, str);
781
782 /* buf[] holds quarantined version of str */
783
784 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100785 * and NORMAL escape sequences placed around it.
786 * NB: we regex against line, but insert text
787 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000788 str = buf;
789 growline = NULL;
790 eflags = 0;
791 goto start;
792
793 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000794 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200795 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200796 (int)match_structs.rm_so, str,
797 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000798 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000799 free(growline);
800 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000801 str += match_structs.rm_eo;
802 line += match_structs.rm_eo;
803 eflags = REG_NOTBOL;
804 start:
805 /* Most of the time doesn't find the regex, optimize for that */
806 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000807 /* if even "" matches, treat it as "not a match" */
808 if (match_structs.rm_so >= match_structs.rm_eo)
809 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000810 }
811
Ron Yorstonf06386a2015-07-18 16:20:03 +0100812 printf("%s%s\n", growline ? growline : "", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000813 free(growline);
814}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000815#else
816void print_found(const char *line);
817#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000818
819static void print_ascii(const char *str)
820{
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100821 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000822 char *p;
823 size_t n;
824
825 while (*str) {
826 n = strcspn(str, controls);
827 if (n) {
828 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000829 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000830 str += n;
831 }
832 n = strspn(str, controls);
833 p = buf;
834 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000835 if (*str == 0x7f)
836 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000837 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000838 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
839 /* I want to know who committed this sin */
840 *p++ = '{';
841 else
842 *p++ = ctrlconv[(unsigned char)*str];
843 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000844 } while (--n);
845 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000846 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000847 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000848 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000849}
850
Rob Landley9200e792005-09-15 19:26:59 +0000851/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000852static void buffer_print(void)
853{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000854 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000855
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000856 move_cursor(0, 0);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200857 for (i = 0; i <= max_displayed_line; i++) {
Ron Yorstonf06386a2015-07-18 16:20:03 +0100858 printf(CLEAR_2_EOL);
859 if (option_mask32 & FLAG_N)
860 print_lineno(buffer[i]);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000861 if (pattern_valid)
862 print_found(buffer[i]);
863 else
864 print_ascii(buffer[i]);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200865 }
866 if ((option_mask32 & FLAG_E)
867 && eof_error <= 0
868 && (max_fline - cur_fline) <= max_displayed_line
869 ) {
870 less_exit(EXIT_SUCCESS);
871 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000872 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000873}
874
Denis Vlasenkod9083952008-10-24 10:42:21 +0000875static void buffer_fill_and_print(void)
876{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000877 unsigned i;
Ron Yorston51aa8612015-07-19 11:12:29 +0100878#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000879 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000880
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000881 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000882 /* Go back to the beginning of this line */
883 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
884 fpos--;
885 }
886
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000887 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000888 while (i <= max_displayed_line && fpos <= max_fline) {
889 int lineno = LINENO(flines[fpos]);
890 buffer[i] = flines[fpos];
891 i++;
892 do {
893 fpos++;
894 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000895 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000896 && lineno == LINENO(flines[fpos])
897 );
898 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000899#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000900 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
901 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000902 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000903#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000904 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000905 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000906 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000907 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000908}
909
Ron Yorstond542d182015-07-24 14:29:13 +0100910/* move cur_fline to a given line number, reading lines if necessary */
911static void goto_lineno(int target)
912{
913 if (target <= 0 ) {
914 cur_fline = 0;
915 }
916 else if (target > LINENO(flines[cur_fline])) {
917 retry:
918 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
919 ++cur_fline;
920 /* target not reached but more input is available */
921 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
922 read_lines();
923 goto retry;
924 }
925 }
926 else {
927 /* search backwards through already-read lines */
928 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
929 --cur_fline;
930 }
931}
932
933static void cap_cur_fline(void)
934{
935 if ((option_mask32 & FLAG_S)) {
936 if (cur_fline > max_fline)
937 cur_fline = max_fline;
938 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
939 goto_lineno(max_lineno - max_displayed_line + TILDES);
940 read_lines();
941 }
942 }
943 else {
944 if (cur_fline + max_displayed_line > max_fline + TILDES)
945 cur_fline = max_fline - max_displayed_line + TILDES;
946 if (cur_fline < 0)
947 cur_fline = 0;
948 }
949}
950
Rob Landley9200e792005-09-15 19:26:59 +0000951/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000952static void buffer_down(int nlines)
953{
Ron Yorstond542d182015-07-24 14:29:13 +0100954 if ((option_mask32 & FLAG_S))
955 goto_lineno(LINENO(flines[cur_fline]) + nlines);
956 else
957 cur_fline += nlines;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000958 read_lines();
Ron Yorstond542d182015-07-24 14:29:13 +0100959 cap_cur_fline();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000960 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000961}
962
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000963static void buffer_up(int nlines)
964{
Ron Yorstond542d182015-07-24 14:29:13 +0100965 if ((option_mask32 & FLAG_S)) {
966 goto_lineno(LINENO(flines[cur_fline]) - nlines);
967 }
968 else {
969 cur_fline -= nlines;
970 if (cur_fline < 0)
971 cur_fline = 0;
972 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000973 read_lines();
974 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000975}
976
Ron Yorstond542d182015-07-24 14:29:13 +0100977/* display a given line where the argument can be either an index into
978 * the flines array or a line number */
979static void buffer_to_line(int linenum, int is_lineno)
980{
981 if (linenum <= 0)
982 cur_fline = 0;
983 else if (is_lineno)
984 goto_lineno(linenum);
985 else
986 cur_fline = linenum;
987 read_lines();
988 cap_cur_fline();
989 buffer_fill_and_print();
990}
991
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000992static void buffer_line(int linenum)
993{
Ron Yorstond542d182015-07-24 14:29:13 +0100994 buffer_to_line(linenum, FALSE);
995}
996
997static void buffer_lineno(int lineno)
998{
999 buffer_to_line(lineno, TRUE);
Rob Landley9200e792005-09-15 19:26:59 +00001000}
1001
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001002static void open_file_and_read_lines(void)
1003{
1004 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +00001005 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001006#if ENABLE_FEATURE_LESS_FLAGS
1007 num_lines = REOPEN_AND_COUNT;
1008#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001009 } else {
1010 /* "less" with no arguments in argv[] */
1011 /* For status line only */
1012 filename = xstrdup(bb_msg_standard_input);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001013#if ENABLE_FEATURE_LESS_FLAGS
1014 num_lines = REOPEN_STDIN;
1015#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001016 }
1017 readpos = 0;
1018 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +00001019 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001020 terminated = 1;
1021 read_lines();
1022}
1023
1024/* Reinitialize everything for a new file - free the memory and start over */
1025static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001026{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001027 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001028
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001029 if (flines) {
1030 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +00001031 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001032 free(flines);
1033 flines = NULL;
1034 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001035
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001036 max_fline = -1;
1037 cur_fline = 0;
1038 max_lineno = 0;
1039 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001040#if ENABLE_FEATURE_LESS_ASK_TERMINAL
1041 if (G.winsize_err)
Denys Vlasenko8187e012017-09-13 22:48:30 +02001042 printf(ESC"[999;999H" ESC"[6n");
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001043#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001044 buffer_fill_and_print();
1045}
1046
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001047static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001048{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001049 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001050 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +00001051 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001052
Denis Vlasenko33196372008-02-23 01:25:38 +00001053 pfd[0].fd = STDIN_FILENO;
1054 pfd[0].events = POLLIN;
1055 pfd[1].fd = kbd_fd;
1056 pfd[1].events = POLLIN;
1057 again:
1058 tcsetattr(kbd_fd, TCSANOW, &term_less);
1059 /* NB: select/poll returns whenever read will not block. Therefore:
1060 * if eof is reached, select/poll will return immediately
1061 * because read will immediately return 0 bytes.
1062 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001063 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1064 */
Denis Vlasenko33196372008-02-23 01:25:38 +00001065 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001066 /* Are we interested in stdin? */
Ron Yorstonad1b4d52015-07-24 14:28:50 +01001067 if (at_end()) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001068 if (eof_error > 0) /* did NOT reach eof yet */
1069 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001070 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001071 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +00001072 if (less_gets_pos >= 0)
1073 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001074 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001075
Denys Vlasenko020f4062009-05-17 16:44:54 +02001076 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001077#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001078 while (1) {
1079 int r;
1080 /* NB: SIGWINCH interrupts poll() */
1081 r = poll(pfd + rd, 2 - rd, -1);
1082 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1083 return '\\'; /* anything which has no defined function */
1084 if (r) break;
1085 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001086#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001087 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001088#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001089 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001090
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001091 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
1092 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001093 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
1094 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +00001095 if (errno == EAGAIN) {
1096 /* No keyboard input available. Since poll() did return,
1097 * we should have input on stdin */
1098 read_lines();
1099 buffer_fill_and_print();
1100 goto again;
1101 }
1102 /* EOF/error (ssh session got killed etc) */
Denys Vlasenko363fb5e2017-09-14 09:42:40 +02001103 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001104 }
Denis Vlasenko33196372008-02-23 01:25:38 +00001105 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001106 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001107}
1108
Denys Vlasenko020f4062009-05-17 16:44:54 +02001109/* Grab a character from input without requiring the return key.
1110 * May return KEYCODE_xxx values.
1111 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001112static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001113{
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001114 int64_t key64;
1115 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001116
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001117 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001118 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001119 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001120 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +00001121
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001122 /* Discard Ctrl-something chars.
1123 * (checking only lower 32 bits is a size optimization:
1124 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1125 */
1126 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +00001127 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +02001128
1129 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001130}
1131
1132static char* less_gets(int sz)
1133{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001134 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001135 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001136 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001137
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001138 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001139 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001140 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001141 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001142 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001143 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001144 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001145 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001146 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001147 if (c == 0x7f)
1148 c = 8;
1149 if (c == 8 && i) {
1150 printf("\x8 \x8");
1151 i--;
1152 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001153 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001154 continue;
1155 if (i >= width - sz - 1)
1156 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001157 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001158 result[i++] = c;
1159 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001160 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001161}
1162
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001163static void examine_file(void)
1164{
Denis Vlasenko33196372008-02-23 01:25:38 +00001165 char *new_fname;
1166
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001167 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001168 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001169 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001170 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001171 err:
1172 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001173 return;
1174 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001175 if (access(new_fname, R_OK) != 0) {
1176 print_statusline("Cannot read this file");
1177 goto err;
1178 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001179 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001180 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001181 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001182 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001183 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001184 num_files++; */
1185 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001186 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001187 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001188}
1189
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001190/* This function changes the file currently being paged. direction can be one of the following:
1191 * -1: go back one file
1192 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001193 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001194static void change_file(int direction)
1195{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001196 if (current_file != ((direction > 0) ? num_files : 1)) {
1197 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001198 free(filename);
1199 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001200 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001201 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001202 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001203 }
1204}
1205
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001206static void remove_current_file(void)
1207{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001208 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001209
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001210 if (num_files < 2)
1211 return;
1212
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001213 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001214 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001215 for (i = 3; i <= num_files; i++)
1216 files[i - 2] = files[i - 1];
1217 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001218 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001219 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001220 for (i = 2; i <= num_files; i++)
1221 files[i - 2] = files[i - 1];
1222 num_files--;
1223 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001224 }
1225}
1226
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001227static void colon_process(void)
1228{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001229 int keypress;
1230
1231 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001232 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001233
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001234 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001235 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001236 case 'd':
1237 remove_current_file();
1238 break;
1239 case 'e':
1240 examine_file();
1241 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001242#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001243 case 'f':
1244 m_status_print();
1245 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001246#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001247 case 'n':
1248 change_file(1);
1249 break;
1250 case 'p':
1251 change_file(-1);
1252 break;
1253 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001254 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001255 break;
1256 case 'x':
1257 change_file(0);
1258 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001259 }
1260}
1261
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001262#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001263static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001264{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001265 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001266 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001267 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001268 match = 0;
1269 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001270}
1271
Rob Landleya2e98042006-04-18 01:53:41 +00001272static void goto_match(int match)
1273{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001274 if (!pattern_valid)
1275 return;
1276 if (match < 0)
1277 match = 0;
1278 /* Try to find next match if eof isn't reached yet */
1279 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001280 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001281 read_lines();
1282 }
1283 if (num_matches) {
1284 normalize_match_pos(match);
1285 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001286 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001287 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001288 }
Rob Landleya2e98042006-04-18 01:53:41 +00001289}
1290
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001291static void fill_match_lines(unsigned pos)
1292{
1293 if (!pattern_valid)
1294 return;
1295 /* Run the regex on each line of the current file */
1296 while (pos <= max_fline) {
1297 /* If this line matches */
1298 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1299 /* and we didn't match it last time */
1300 && !(num_matches && match_lines[num_matches-1] == pos)
1301 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001302 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001303 match_lines[num_matches++] = pos;
1304 }
1305 pos++;
1306 }
1307}
1308
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001309static void regex_process(void)
1310{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001311 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001312
1313 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001314 free(match_lines);
1315 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001316 match_pos = 0;
1317 num_matches = 0;
1318 if (pattern_valid) {
1319 regfree(&pattern);
1320 pattern_valid = 0;
1321 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001322
Rob Landleya2e98042006-04-18 01:53:41 +00001323 /* Get the uncompiled regular expression from the user */
1324 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001325 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001326 uncomp_regex = less_gets(1);
1327 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001328 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001329 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001330 return;
1331 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001332
Rob Landleya2e98042006-04-18 01:53:41 +00001333 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001334 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001335 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001336 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001337 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001338 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001339 free(err);
1340 return;
1341 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001342
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001343 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001344 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001345 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001346 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001347 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001348 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001349 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001350 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001351 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1352 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001353
1354 /* It's possible that no matches are found yet.
1355 * goto_match() will read input looking for match,
1356 * if needed */
1357 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001358}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001359#endif
1360
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001361static void number_process(int first_digit)
1362{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001363 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001364 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001365 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001366 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001367
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001368 num_input[0] = first_digit;
1369
1370 /* Clear the current line, print a prompt, and then print the digit */
1371 clear_line();
1372 printf(":%c", first_digit);
1373
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001374 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001375 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001376 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001377 keypress = less_getch(i + 1);
Ron Yorstonae1a9e82015-07-21 20:12:31 +01001378 if ((unsigned)keypress > 255 || !isdigit(keypress))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001379 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001380 num_input[i] = keypress;
1381 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001382 i++;
1383 }
1384
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001385 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001386 num = bb_strtou(num_input, NULL, 10);
1387 /* on format error, num == -1 */
1388 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001389 buffer_print();
1390 return;
1391 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001392
1393 /* We now know the number and the letter entered, so we process them */
1394 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001395 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001396 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001397 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001398 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001399 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001400 break;
1401 case 'g': case '<': case 'G': case '>':
Ron Yorstond542d182015-07-24 14:29:13 +01001402 buffer_lineno(num - 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001403 break;
1404 case 'p': case '%':
Ron Yorstond542d182015-07-24 14:29:13 +01001405#if ENABLE_FEATURE_LESS_FLAGS
1406 update_num_lines();
1407 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1408#else
1409 num = num * max_lineno / 100;
1410#endif
1411 buffer_lineno(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001412 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001413#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001414 case 'n':
1415 goto_match(match_pos + num);
1416 break;
1417 case '/':
1418 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1419 regex_process();
1420 break;
1421 case '?':
1422 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1423 regex_process();
1424 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001425#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001426 }
1427}
1428
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001429#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001430static void flag_change(void)
1431{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001432 int keypress;
1433
1434 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001435 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001436 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001437
1438 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001439 case 'M':
1440 option_mask32 ^= FLAG_M;
1441 break;
1442 case 'm':
1443 option_mask32 ^= FLAG_m;
1444 break;
1445 case 'E':
1446 option_mask32 ^= FLAG_E;
1447 break;
1448 case '~':
1449 option_mask32 ^= FLAG_TILDE;
1450 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001451#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001452 case 'S':
1453 option_mask32 ^= FLAG_S;
1454 buffer_fill_and_print();
1455 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001456#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001457#if ENABLE_FEATURE_LESS_LINENUMS
1458 case 'N':
1459 option_mask32 ^= FLAG_N;
1460 re_wrap();
1461 buffer_fill_and_print();
1462 break;
1463#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001464 }
1465}
1466
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001467#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001468static void show_flag_status(void)
1469{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001470 int keypress;
1471 int flag_val;
1472
1473 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001474 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001475 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001476
1477 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001478 case 'M':
1479 flag_val = option_mask32 & FLAG_M;
1480 break;
1481 case 'm':
1482 flag_val = option_mask32 & FLAG_m;
1483 break;
1484 case '~':
1485 flag_val = option_mask32 & FLAG_TILDE;
1486 break;
1487 case 'N':
1488 flag_val = option_mask32 & FLAG_N;
1489 break;
1490 case 'E':
1491 flag_val = option_mask32 & FLAG_E;
1492 break;
1493 default:
1494 flag_val = 0;
1495 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001496 }
1497
1498 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001499 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001500}
1501#endif
1502
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001503#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1504
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001505static void save_input_to_file(void)
1506{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001507 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001508 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001509 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001510 FILE *fp;
1511
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001512 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001513 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001514 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001515 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001516 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001517 msg = "Error opening log file";
1518 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001519 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001520 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001521 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001522 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001523 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001524 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001525 ret:
1526 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001527 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001528}
1529
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001530#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001531static void add_mark(void)
1532{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001533 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001534
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001535 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001536 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001537
1538 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001539 /* If we exceed 15 marks, start overwriting previous ones */
1540 if (num_marks == 14)
1541 num_marks = 0;
1542
1543 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001544 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001545 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001546 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001547 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001548 }
1549}
1550
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001551static void goto_mark(void)
1552{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001553 int letter;
1554 int i;
1555
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001556 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001557 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001558 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001559
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001560 if (isalpha(letter)) {
1561 for (i = 0; i <= num_marks; i++)
1562 if (letter == mark_lines[i][0]) {
1563 buffer_line(mark_lines[i][1]);
1564 break;
1565 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001566 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001567 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001568 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001569 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001570}
1571#endif
1572
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001573#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001574static char opp_bracket(char bracket)
1575{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001576 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001577 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1578 bracket++;
1579 case '(': /* ')' == '(' + 1 */
1580 bracket++;
1581 break;
1582 case '}': case ']':
1583 bracket--;
1584 case ')':
1585 bracket--;
1586 break;
1587 };
1588 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001589}
1590
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001591static void match_right_bracket(char bracket)
1592{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001593 unsigned i = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001594
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001595 if (i >= max_fline
1596 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001597 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001598 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001599 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001600 }
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001601
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001602 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001603 for (; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001604 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001605 /*
1606 * Line with matched right bracket becomes
1607 * last visible line
1608 */
1609 buffer_line(i - max_displayed_line);
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001610 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001611 }
1612 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001613 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001614}
1615
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001616static void match_left_bracket(char bracket)
1617{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001618 int i = cur_fline + max_displayed_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001619
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001620 if (i >= max_fline
1621 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001622 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001623 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001624 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001625 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001626
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001627 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001628 for (; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001629 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001630 /*
1631 * Line with matched left bracket becomes
1632 * first visible line
1633 */
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001634 buffer_line(i);
1635 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001636 }
1637 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001638 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001639}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001640#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001641
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001642static void keypress_process(int keypress)
1643{
Rob Landley9200e792005-09-15 19:26:59 +00001644 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001645 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001646 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001647 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001648 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001649 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001650 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001651 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001652 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001653 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001654 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001655 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001656 break;
1657 case 'd':
1658 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001659 break;
1660 case 'u':
1661 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001662 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001663 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001664 buffer_line(0);
1665 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001666 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001667 cur_fline = MAXLINES;
1668 read_lines();
1669 buffer_line(cur_fline);
1670 break;
1671 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001672 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001673 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001674#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001675 case 'm':
1676 add_mark();
1677 buffer_print();
1678 break;
1679 case '\'':
1680 goto_mark();
1681 buffer_print();
1682 break;
Rob Landley9200e792005-09-15 19:26:59 +00001683#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001684 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001685 /* TODO: (1) also bind ^R, ^L to this?
1686 * (2) re-measure window size?
1687 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001688 buffer_print();
1689 break;
1690 /*case 'R':
1691 full_repaint();
1692 break;*/
1693 case 's':
1694 save_input_to_file();
1695 break;
1696 case 'E':
1697 examine_file();
1698 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001699#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001700 case '=':
1701 m_status_print();
1702 break;
Rob Landley9200e792005-09-15 19:26:59 +00001703#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001704#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001705 case '/':
1706 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1707 regex_process();
1708 break;
1709 case 'n':
1710 goto_match(match_pos + 1);
1711 break;
1712 case 'N':
1713 goto_match(match_pos - 1);
1714 break;
1715 case '?':
1716 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1717 regex_process();
1718 break;
Rob Landley9200e792005-09-15 19:26:59 +00001719#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001720#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001721 case '-':
1722 flag_change();
1723 buffer_print();
1724 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001725#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001726 case '_':
1727 show_flag_status();
1728 break;
Rob Landley9200e792005-09-15 19:26:59 +00001729#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001730#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001731#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001732 case '{': case '(': case '[':
1733 match_right_bracket(keypress);
1734 break;
1735 case '}': case ')': case ']':
1736 match_left_bracket(keypress);
1737 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001738#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001739 case ':':
1740 colon_process();
1741 break;
Rob Landley9200e792005-09-15 19:26:59 +00001742 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001743
Rob Landley9200e792005-09-15 19:26:59 +00001744 if (isdigit(keypress))
1745 number_process(keypress);
1746}
1747
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001748static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001749{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001750 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001751}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001752
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001753#if ENABLE_FEATURE_LESS_WINCH
1754static void sigwinch_handler(int sig UNUSED_PARAM)
1755{
1756 winch_counter++;
1757}
1758#endif
1759
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001760int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001761int less_main(int argc, char **argv)
1762{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001763 char *tty_name;
1764 int tty_fd;
1765
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001766 INIT_G();
1767
Denys Vlasenko821e6432014-01-22 16:36:22 +01001768 /* TODO: -x: do not interpret backspace, -xx: tab also
1769 * -xxx: newline also
1770 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1771 * -s: condense many empty lines to one
1772 * (used by some setups for manpage display)
1773 */
Ron Yorston51aa8612015-07-19 11:12:29 +01001774 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s");
Rob Landley9200e792005-09-15 19:26:59 +00001775 argv += optind;
Denys Vlasenko83c99ab2018-01-08 10:27:23 +01001776 num_files = argc - optind;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001777 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001778
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001779 /* Another popular pager, most, detects when stdout
1780 * is not a tty and turns into cat. This makes sense. */
1781 if (!isatty(STDOUT_FILENO))
1782 return bb_cat(argv);
1783
Rob Landley9200e792005-09-15 19:26:59 +00001784 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001785 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001786 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001787 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001788 bb_show_usage();
1789 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001790 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001791 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001792 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001793
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001794 if (option_mask32 & FLAG_TILDE)
1795 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001796
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001797 /* Some versions of less can survive w/o controlling tty,
1798 * try to do the same. This also allows to specify an alternative
1799 * tty via "less 1<>TTY".
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001800 *
1801 * We prefer not to use STDOUT_FILENO directly,
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001802 * since we want to set this fd to non-blocking mode,
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001803 * and not interfere with other processes which share stdout with us.
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001804 */
1805 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1806 if (tty_name) {
1807 tty_fd = open(tty_name, O_RDONLY);
1808 free(tty_name);
1809 if (tty_fd < 0)
1810 goto try_ctty;
1811 } else {
1812 /* Try controlling tty */
1813 try_ctty:
1814 tty_fd = open(CURRENT_TTY, O_RDONLY);
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001815 if (tty_fd < 0) {
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001816 /* If all else fails, less 481 uses stdout. Mimic that */
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001817 tty_fd = STDOUT_FILENO;
1818 }
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001819 }
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001820 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001821 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001822
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02001823 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001824
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02001825 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001826 /* 20: two tabstops + 4 */
1827 if (width < 20 || max_displayed_line < 3)
1828 return bb_cat(argv);
1829 max_displayed_line -= 2;
1830
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001831 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001832 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001833#if ENABLE_FEATURE_LESS_WINCH
1834 signal(SIGWINCH, sigwinch_handler);
1835#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001836
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001837 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001838 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001839 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001840 int64_t keypress;
1841
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001842#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001843 while (WINCH_COUNTER) {
1844 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001845 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001846 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1847 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001848 /* 20: two tabstops + 4 */
1849 if (width < 20)
1850 width = 20;
1851 if (max_displayed_line < 3)
1852 max_displayed_line = 3;
1853 max_displayed_line -= 2;
1854 free(buffer);
1855 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001856 /* Avoid re-wrap and/or redraw if we already know
1857 * we need to do it again. These ops are expensive */
1858 if (WINCH_COUNTER)
1859 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001860 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001861 if (WINCH_COUNTER)
1862 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001863 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001864 /* This took some time. Loop back and check,
1865 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001866 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001867 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001868# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1869 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1870 uint32_t rc = (keypress >> 32);
1871 width = (rc & 0x7fff);
1872 max_displayed_line = ((rc >> 16) & 0x7fff);
1873 goto got_size;
1874 }
1875# endif
1876#else
1877 keypress = less_getch(-1); /* -1: do not position cursor */
1878#endif
Rob Landley9200e792005-09-15 19:26:59 +00001879 keypress_process(keypress);
1880 }
1881}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001882
1883/*
1884Help text of less version 418 is below.
1885If you are implementing something, keeping
1886key and/or command line switch compatibility is a good idea:
1887
1888
1889 SUMMARY OF LESS COMMANDS
1890
1891 Commands marked with * may be preceded by a number, N.
1892 Notes in parentheses indicate the behavior if N is given.
1893 h H Display this help.
1894 q :q Q :Q ZZ Exit.
1895 ---------------------------------------------------------------------------
1896 MOVING
1897 e ^E j ^N CR * Forward one line (or N lines).
1898 y ^Y k ^K ^P * Backward one line (or N lines).
1899 f ^F ^V SPACE * Forward one window (or N lines).
1900 b ^B ESC-v * Backward one window (or N lines).
1901 z * Forward one window (and set window to N).
1902 w * Backward one window (and set window to N).
1903 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1904 d ^D * Forward one half-window (and set half-window to N).
1905 u ^U * Backward one half-window (and set half-window to N).
1906 ESC-) RightArrow * Left one half screen width (or N positions).
1907 ESC-( LeftArrow * Right one half screen width (or N positions).
1908 F Forward forever; like "tail -f".
1909 r ^R ^L Repaint screen.
1910 R Repaint screen, discarding buffered input.
1911 ---------------------------------------------------
1912 Default "window" is the screen height.
1913 Default "half-window" is half of the screen height.
1914 ---------------------------------------------------------------------------
1915 SEARCHING
1916 /pattern * Search forward for (N-th) matching line.
1917 ?pattern * Search backward for (N-th) matching line.
1918 n * Repeat previous search (for N-th occurrence).
1919 N * Repeat previous search in reverse direction.
1920 ESC-n * Repeat previous search, spanning files.
1921 ESC-N * Repeat previous search, reverse dir. & spanning files.
1922 ESC-u Undo (toggle) search highlighting.
1923 ---------------------------------------------------
1924 Search patterns may be modified by one or more of:
1925 ^N or ! Search for NON-matching lines.
1926 ^E or * Search multiple files (pass thru END OF FILE).
1927 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1928 ^K Highlight matches, but don't move (KEEP position).
1929 ^R Don't use REGULAR EXPRESSIONS.
1930 ---------------------------------------------------------------------------
1931 JUMPING
1932 g < ESC-< * Go to first line in file (or line N).
1933 G > ESC-> * Go to last line in file (or line N).
1934 p % * Go to beginning of file (or N percent into file).
1935 t * Go to the (N-th) next tag.
1936 T * Go to the (N-th) previous tag.
1937 { ( [ * Find close bracket } ) ].
1938 } ) ] * Find open bracket { ( [.
1939 ESC-^F <c1> <c2> * Find close bracket <c2>.
1940 ESC-^B <c1> <c2> * Find open bracket <c1>
1941 ---------------------------------------------------
1942 Each "find close bracket" command goes forward to the close bracket
1943 matching the (N-th) open bracket in the top line.
1944 Each "find open bracket" command goes backward to the open bracket
1945 matching the (N-th) close bracket in the bottom line.
1946 m<letter> Mark the current position with <letter>.
1947 '<letter> Go to a previously marked position.
1948 '' Go to the previous position.
1949 ^X^X Same as '.
1950 ---------------------------------------------------
1951 A mark is any upper-case or lower-case letter.
1952 Certain marks are predefined:
1953 ^ means beginning of the file
1954 $ means end of the file
1955 ---------------------------------------------------------------------------
1956 CHANGING FILES
1957 :e [file] Examine a new file.
1958 ^X^V Same as :e.
1959 :n * Examine the (N-th) next file from the command line.
1960 :p * Examine the (N-th) previous file from the command line.
1961 :x * Examine the first (or N-th) file from the command line.
1962 :d Delete the current file from the command line list.
1963 = ^G :f Print current file name.
1964 ---------------------------------------------------------------------------
1965 MISCELLANEOUS COMMANDS
1966 -<flag> Toggle a command line option [see OPTIONS below].
1967 --<name> Toggle a command line option, by name.
1968 _<flag> Display the setting of a command line option.
1969 __<name> Display the setting of an option, by name.
1970 +cmd Execute the less cmd each time a new file is examined.
1971 !command Execute the shell command with $SHELL.
1972 |Xcommand Pipe file between current pos & mark X to shell command.
1973 v Edit the current file with $VISUAL or $EDITOR.
1974 V Print version number of "less".
1975 ---------------------------------------------------------------------------
1976 OPTIONS
1977 Most options may be changed either on the command line,
1978 or from within less by using the - or -- command.
1979 Options may be given in one of two forms: either a single
Maninder Singh97c64912015-05-25 13:46:36 +02001980 character preceded by a -, or a name preceded by --.
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001981 -? ........ --help
1982 Display help (from command line).
1983 -a ........ --search-skip-screen
1984 Forward search skips current screen.
1985 -b [N] .... --buffers=[N]
1986 Number of buffers.
1987 -B ........ --auto-buffers
1988 Don't automatically allocate buffers for pipes.
1989 -c ........ --clear-screen
1990 Repaint by clearing rather than scrolling.
1991 -d ........ --dumb
1992 Dumb terminal.
1993 -D [xn.n] . --color=xn.n
1994 Set screen colors. (MS-DOS only)
1995 -e -E .... --quit-at-eof --QUIT-AT-EOF
1996 Quit at end of file.
1997 -f ........ --force
1998 Force open non-regular files.
1999 -F ........ --quit-if-one-screen
2000 Quit if entire file fits on first screen.
2001 -g ........ --hilite-search
2002 Highlight only last match for searches.
2003 -G ........ --HILITE-SEARCH
2004 Don't highlight any matches for searches.
2005 -h [N] .... --max-back-scroll=[N]
2006 Backward scroll limit.
2007 -i ........ --ignore-case
2008 Ignore case in searches that do not contain uppercase.
2009 -I ........ --IGNORE-CASE
2010 Ignore case in all searches.
2011 -j [N] .... --jump-target=[N]
2012 Screen position of target lines.
2013 -J ........ --status-column
2014 Display a status column at left edge of screen.
2015 -k [file] . --lesskey-file=[file]
2016 Use a lesskey file.
2017 -L ........ --no-lessopen
2018 Ignore the LESSOPEN environment variable.
2019 -m -M .... --long-prompt --LONG-PROMPT
2020 Set prompt style.
2021 -n -N .... --line-numbers --LINE-NUMBERS
2022 Don't use line numbers.
2023 -o [file] . --log-file=[file]
2024 Copy to log file (standard input only).
2025 -O [file] . --LOG-FILE=[file]
2026 Copy to log file (unconditionally overwrite).
2027 -p [pattern] --pattern=[pattern]
2028 Start at pattern (from command line).
2029 -P [prompt] --prompt=[prompt]
2030 Define new prompt.
2031 -q -Q .... --quiet --QUIET --silent --SILENT
2032 Quiet the terminal bell.
2033 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2034 Output "raw" control characters.
2035 -s ........ --squeeze-blank-lines
2036 Squeeze multiple blank lines.
2037 -S ........ --chop-long-lines
2038 Chop long lines.
2039 -t [tag] .. --tag=[tag]
2040 Find a tag.
2041 -T [tagsfile] --tag-file=[tagsfile]
2042 Use an alternate tags file.
2043 -u -U .... --underline-special --UNDERLINE-SPECIAL
2044 Change handling of backspaces.
2045 -V ........ --version
2046 Display the version number of "less".
2047 -w ........ --hilite-unread
2048 Highlight first new line after forward-screen.
2049 -W ........ --HILITE-UNREAD
2050 Highlight first new line after any forward movement.
2051 -x [N[,...]] --tabs=[N[,...]]
2052 Set tab stops.
2053 -X ........ --no-init
2054 Don't use termcap init/deinit strings.
2055 --no-keypad
2056 Don't use termcap keypad init/deinit strings.
2057 -y [N] .... --max-forw-scroll=[N]
2058 Forward scroll limit.
2059 -z [N] .... --window=[N]
2060 Set size of window.
2061 -" [c[c]] . --quotes=[c[c]]
2062 Set shell quote characters.
2063 -~ ........ --tilde
2064 Don't display tildes after end of file.
2065 -# [N] .... --shift=[N]
2066 Horizontal scroll amount (0 = one half screen width)
2067
2068 ---------------------------------------------------------------------------
2069 LINE EDITING
2070 These keys can be used to edit text being entered
2071 on the "command line" at the bottom of the screen.
2072 RightArrow ESC-l Move cursor right one character.
2073 LeftArrow ESC-h Move cursor left one character.
2074 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2075 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2076 HOME ESC-0 Move cursor to start of line.
2077 END ESC-$ Move cursor to end of line.
2078 BACKSPACE Delete char to left of cursor.
2079 DELETE ESC-x Delete char under cursor.
2080 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2081 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2082 CNTL-U ESC (MS-DOS only) Delete entire line.
2083 UpArrow ESC-k Retrieve previous command line.
2084 DownArrow ESC-j Retrieve next command line.
2085 TAB Complete filename & cycle.
2086 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2087 CNTL-L Complete filename, list all.
2088*/