blob: 8a0525cb7b523c00d48d157e56e844c011eae4b7 [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 Vlasenkob097a842018-12-28 03:20:17 +010023//config: bool "less (16 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 Vlasenko8528d3d2018-01-22 14:38:51 +0100102//config:
103//config:config FEATURE_LESS_RAW
104//config: bool "Enable -R ('raw control characters')"
105//config: default y
106//config: depends on FEATURE_LESS_DASHCMD
107//config: help
108//config: This is essential for less applet to work with tools that use colors
109//config: and paging, such as git, systemd tools or nmcli.
110//config:
111//config:config FEATURE_LESS_ENV
112//config: bool "Take options from $LESS environment variable"
113//config: default y
114//config: depends on FEATURE_LESS_DASHCMD
115//config: help
116//config: This is essential for less applet to work with tools that use colors
117//config: and paging, such as git, systemd tools or nmcli.
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200118
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +0100119//applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP))
120
121//kbuild:lib-$(CONFIG_LESS) += less.o
122
Pere Orga5bc8c002011-04-11 03:29:49 +0200123//usage:#define less_trivial_usage
Aaro Koskinen36941502018-04-15 01:24:24 +0300124//usage: "[-EF" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
Denys Vlasenko14bc9652018-01-24 16:47:08 +0100125//usage: "N" IF_FEATURE_LESS_TRUNCATE("S") IF_FEATURE_LESS_RAW("R") "h~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200126//usage:#define less_full_usage "\n\n"
127//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200128//usage: "\n -E Quit once the end of a file is reached"
Aaro Koskinen36941502018-04-15 01:24:24 +0300129//usage: "\n -F Quit if entire file fits on first screen"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100130//usage: IF_FEATURE_LESS_REGEXP(
131//usage: "\n -I Ignore case in all searches"
132//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200133//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200134//usage: "\n -M,-m Display status line with line numbers"
135//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200136//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200137//usage: "\n -N Prefix line number to each line"
Ron Yorston51aa8612015-07-19 11:12:29 +0100138//usage: IF_FEATURE_LESS_TRUNCATE(
139//usage: "\n -S Truncate long lines"
140//usage: )
Denys Vlasenko8528d3d2018-01-22 14:38:51 +0100141//usage: IF_FEATURE_LESS_RAW(
142//usage: "\n -R Remove color escape codes in input"
143//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200144//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200145
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200146#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000147
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000148#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200149#include "common_bufsiz.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000150#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000151#include "xregex.h"
152#endif
153
Marek Polacek7b181072010-10-28 21:34:56 +0200154
155#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000156/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200157#define HIGHLIGHT ESC"[7m"
Denys Vlasenko8187e012017-09-13 22:48:30 +0200158#define NORMAL ESC"[m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200159/* The escape code to home and clear to the end of screen */
Denys Vlasenko73af7052017-09-13 19:20:27 +0200160#define CLEAR ESC"[H"ESC"[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200161/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200162#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000163
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000164enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000165/* Absolute max of lines eaten */
166 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000167/* This many "after the end" lines we will show (at max) */
168 TILDES = 1,
169};
170
Rob Landley9200e792005-09-15 19:26:59 +0000171/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000172enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000173 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000174 FLAG_M = 1 << 1,
175 FLAG_m = 1 << 2,
176 FLAG_N = 1 << 3,
177 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000178 FLAG_I = 1 << 5,
Aaro Koskinen36941502018-04-15 01:24:24 +0300179 FLAG_F = 1 << 6,
180 FLAG_S = (1 << 7) * ENABLE_FEATURE_LESS_TRUNCATE,
181 FLAG_R = (1 << 8) * ENABLE_FEATURE_LESS_RAW,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000182/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000183 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
184};
Rob Landley9200e792005-09-15 19:26:59 +0000185
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000186#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000187enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000188#endif
189
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000190struct globals {
191 int cur_fline; /* signed */
192 int kbd_fd; /* fd to get input from */
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200193 int kbd_fd_orig_flags;
Denis Vlasenko33196372008-02-23 01:25:38 +0000194 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000195/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000196 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000197 unsigned max_fline;
198 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000199 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000200 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000201#if ENABLE_FEATURE_LESS_WINCH
202 unsigned winch_counter;
203#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000204 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000205 ssize_t readpos;
206 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000207 const char **buffer;
208 const char **flines;
209 const char *empty_line_marker;
210 unsigned num_files;
211 unsigned current_file;
212 char *filename;
213 char **files;
Ron Yorston193ba402015-07-21 22:28:09 +0200214#if ENABLE_FEATURE_LESS_FLAGS
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200215 int num_lines; /* a flag if < 0, line count if >= 0 */
216# define REOPEN_AND_COUNT (-1)
217# define REOPEN_STDIN (-2)
218# define NOT_REGULAR_FILE (-3)
Ron Yorston193ba402015-07-21 22:28:09 +0200219#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000220#if ENABLE_FEATURE_LESS_MARKS
221 unsigned num_marks;
222 unsigned mark_lines[15][2];
223#endif
224#if ENABLE_FEATURE_LESS_REGEXP
225 unsigned *match_lines;
226 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000227 int wanted_match; /* signed! */
228 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000229 regex_t pattern;
230 smallint pattern_valid;
231#endif
Denys Vlasenko8528d3d2018-01-22 14:38:51 +0100232#if ENABLE_FEATURE_LESS_RAW
233 smallint in_escape;
234#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200235#if ENABLE_FEATURE_LESS_ASK_TERMINAL
236 smallint winsize_err;
237#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000238 smallint terminated;
239 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000240 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000241};
242#define G (*ptr_to_globals)
243#define cur_fline (G.cur_fline )
244#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000245#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000246#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000247#define max_fline (G.max_fline )
248#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000249#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000250#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000251#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000252/* This one is 100% not cached by compiler on read access */
253#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000254#define eof_error (G.eof_error )
255#define readpos (G.readpos )
256#define readeof (G.readeof )
257#define buffer (G.buffer )
258#define flines (G.flines )
259#define empty_line_marker (G.empty_line_marker )
260#define num_files (G.num_files )
261#define current_file (G.current_file )
262#define filename (G.filename )
263#define files (G.files )
Ron Yorston193ba402015-07-21 22:28:09 +0200264#define num_lines (G.num_lines )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000265#define num_marks (G.num_marks )
266#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000267#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000268#define match_lines (G.match_lines )
269#define match_pos (G.match_pos )
270#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000271#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000272#define pattern (G.pattern )
273#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000274#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000275#define terminated (G.terminated )
276#define term_orig (G.term_orig )
277#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000278#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000279#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000280 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
281 less_gets_pos = -1; \
282 empty_line_marker = "~"; \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000283 current_file = 1; \
284 eof_error = 1; \
285 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000286 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000287} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000288
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000289/* flines[] are lines read from stdin, each in malloc'ed buffer.
290 * Line numbers are stored as uint32_t prepended to each line.
291 * Pointer is adjusted so that flines[i] points directly past
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200292 * line number. Accessor: */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000293#define MEMPTR(p) ((char*)(p) - 4)
294#define LINENO(p) (*(uint32_t*)((p) - 4))
295
296
Rob Landley9200e792005-09-15 19:26:59 +0000297/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000298static void set_tty_cooked(void)
299{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100300 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000301 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000302}
303
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000304/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000305 top-left corner of the console */
Denys Vlasenkod54f58d2017-09-14 10:51:12 +0200306static void move_cursor(int line, int col)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000307{
Denys Vlasenkod54f58d2017-09-14 10:51:12 +0200308 printf(ESC"[%u;%uH", line, col);
Rob Landley9200e792005-09-15 19:26:59 +0000309}
310
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000311static void clear_line(void)
312{
Marek Polacek7b181072010-10-28 21:34:56 +0200313 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000314}
315
316static void print_hilite(const char *str)
317{
318 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000319}
320
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000321static void print_statusline(const char *str)
322{
323 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000324 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000325}
326
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000327/* Exit the program gracefully */
Denys Vlasenko076f5e02022-01-05 22:04:21 +0100328static void restore_tty(void)
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000329{
330 set_tty_cooked();
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200331 if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
332 ndelay_off(kbd_fd);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000333 clear_line();
Denys Vlasenko076f5e02022-01-05 22:04:21 +0100334}
335
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100336static NOINLINE void less_exit(void)
Denys Vlasenko076f5e02022-01-05 22:04:21 +0100337{
338 restore_tty();
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100339 exit_SUCCESS();
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000340}
341
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000342#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
343 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000344static void re_wrap(void)
345{
346 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000347 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000348 int src_idx;
349 int dst_idx;
350 int new_cur_fline = 0;
351 uint32_t lineno;
352 char linebuf[w + 1];
353 const char **old_flines = flines;
354 const char *s;
355 char **new_flines = NULL;
356 char *d;
357
358 if (option_mask32 & FLAG_N)
359 w -= 8;
360
361 src_idx = 0;
362 dst_idx = 0;
363 s = old_flines[0];
364 lineno = LINENO(s);
365 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000366 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000367 while (1) {
368 *d = *s;
369 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 new_line_pos++;
Ron Yorston78cfa002015-07-19 21:41:09 +0100371 if (*d == '\t') { /* tab */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000372 new_line_pos += 7;
Ron Yorston78cfa002015-07-19 21:41:09 +0100373 new_line_pos &= (~7);
374 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000375 s++;
376 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000377 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000378 int sz;
379 /* new line is full, create next one */
380 *d = '\0';
381 next_new:
382 sz = (d - linebuf) + 1; /* + 1: NUL */
383 d = ((char*)xmalloc(sz + 4)) + 4;
384 LINENO(d) = lineno;
385 memcpy(d, linebuf, sz);
386 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
387 new_flines[dst_idx] = d;
388 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000389 if (new_line_pos < w) {
390 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000391 if (src_idx > max_fline)
392 break;
393 lineno = LINENO(s);
394 }
395 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000396 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000397 }
398 continue;
399 }
400 /* *d == NUL: old line ended, go to next old one */
401 free(MEMPTR(old_flines[src_idx]));
402 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000403 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000404 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000405 src_idx++;
406 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000407 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000408 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000409 s = old_flines[src_idx];
410 if (lineno != LINENO(s)) {
411 /* this is not a continuation line!
412 * create next _new_ line too */
413 goto next_new;
414 }
415 }
416
417 free(old_flines);
418 flines = (const char **)new_flines;
419
420 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000421 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000422 cur_fline = new_cur_fline;
423 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000424#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000425 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000426#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000427}
428#endif
429
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000430#if ENABLE_FEATURE_LESS_REGEXP
431static void fill_match_lines(unsigned pos);
432#else
433#define fill_match_lines(pos) ((void)0)
434#endif
435
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100436static int at_end(void)
437{
438 return (option_mask32 & FLAG_S)
439 ? !(cur_fline <= max_fline &&
440 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
441 : !(max_fline > cur_fline + max_displayed_line);
442}
443
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000444/* Devilishly complex routine.
445 *
446 * Has to deal with EOF and EPIPE on input,
447 * with line wrapping, with last line not ending in '\n'
448 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000449 * It reads input again if last time we got an EOF (thus supporting
450 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000451 *
452 * Variables used:
453 * flines[] - array of lines already read. Linewrap may cause
454 * one source file line to occupy several flines[n].
455 * flines[max_fline] - last line, possibly incomplete.
456 * terminated - 1 if flines[max_fline] is 'terminated'
457 * (if there was '\n' [which isn't stored itself, we just remember
458 * that it was seen])
459 * max_lineno - last line's number, this one doesn't increment
460 * on line wrap, only on "real" new lines.
461 * readbuf[0..readeof-1] - small preliminary buffer.
462 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000463 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000464 * (takes into account tabs and backspaces)
465 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200466 *
467 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
468 * "/search on very long input" and "reaching max line count" corner cases.
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000469 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000470static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000471{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000472 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000473 int w = width;
474 char last_terminated = terminated;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200475 time_t last_time = 0;
476 int retry_EAGAIN = 2;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000477#if ENABLE_FEATURE_LESS_REGEXP
478 unsigned old_max_fline = max_fline;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000479#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000480
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200481#define readbuf bb_common_bufsiz1
482 setup_common_bufsiz();
483
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200484 /* (careful: max_fline can be -1) */
485 if (max_fline + 1 > MAXLINES)
486 return;
487
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000488 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000489 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000490
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100491 p = current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000492 if (!last_terminated) {
493 const char *cp = flines[max_fline];
Denys Vlasenko865814a2014-09-22 21:17:24 +0200494 p = stpcpy(p, cp);
495 free(MEMPTR(cp));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000496 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000497 } else {
Denys Vlasenko865814a2014-09-22 21:17:24 +0200498 max_fline++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000499 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000500 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000501
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000502 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000503 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000504 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000505 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000506 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000507 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000508 if (readpos >= readeof) {
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200509 int flags = ndelay_on(0);
510
511 while (1) {
512 time_t t;
513
514 errno = 0;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200515 eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200516 if (errno != EAGAIN)
517 break;
518 t = time(NULL);
519 if (t != last_time) {
520 last_time = t;
521 if (--retry_EAGAIN < 0)
522 break;
523 }
524 sched_yield();
525 }
526 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000527 readpos = 0;
528 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000529 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000530 goto reached_eof;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200531 retry_EAGAIN = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000532 }
533 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000534 /* backspace? [needed for manpages] */
535 /* <tab><bs> is (a) insane and */
536 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000537 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000538 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000539 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000540 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000541 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000542 continue;
543 }
Denys Vlasenko8528d3d2018-01-22 14:38:51 +0100544#if ENABLE_FEATURE_LESS_RAW
545 if (option_mask32 & FLAG_R) {
546 if (c == '\033')
547 goto discard;
548 if (G.in_escape) {
549 if (isdigit(c)
550 || c == '['
551 || c == ';'
552 || c == 'm'
553 ) {
554 discard:
555 G.in_escape = (c != 'm');
556 readpos++;
557 continue;
558 }
559 /* Hmm, unexpected end of "ESC [ N ; N m" sequence */
560 G.in_escape = 0;
561 }
562 }
563#endif
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100564 {
565 size_t new_last_line_pos = last_line_pos + 1;
566 if (c == '\t') {
567 new_last_line_pos += 7;
568 new_last_line_pos &= (~7);
569 }
570 if ((int)new_last_line_pos > w)
571 break;
572 last_line_pos = new_last_line_pos;
573 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000574 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000575 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000576 if (c == '\n') {
577 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000578 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000579 break;
580 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000581 /* NUL is substituted by '\n'! */
582 if (c == '\0') c = '\n';
583 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000584 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000585 } /* end of "read chars until we have a line" loop */
Denys Vlasenko15943c82014-09-21 22:10:55 +0200586#if 0
587//BUG: also triggers on this:
588// { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
589// (resulting in lost empty line between "foo" and "bar" lines)
590// the "terminated" logic needs fixing (or explaining)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000591 /* Corner case: linewrap with only "" wrapping to next line */
592 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000593 if (!last_terminated && !current_line[0]) {
594 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000595 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000596 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000597 }
Denys Vlasenko15943c82014-09-21 22:10:55 +0200598#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000599 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000600 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000601 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000602
603 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
604 LINENO(flines[max_fline]) = max_lineno;
605 if (terminated)
606 max_lineno++;
607
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000608 if (max_fline >= MAXLINES) {
609 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000610 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000611 }
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100612 if (!at_end()) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000613#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000614 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000615#else
616 if (wanted_match >= num_matches) { /* goto_match called us */
617 fill_match_lines(old_max_fline);
618 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000619 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000620 if (wanted_match < num_matches)
621 break;
622#endif
623 }
624 if (eof_error <= 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000625 break;
626 }
627 max_fline++;
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100628 current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000629 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000630 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000631 } /* end of "read lines until we reach cur_fline" loop */
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200632
633 if (eof_error < 0) {
634 if (errno == EAGAIN) {
635 eof_error = 1;
636 } else {
637 print_statusline(bb_msg_read_error);
638 }
639 }
Ron Yorston193ba402015-07-21 22:28:09 +0200640#if ENABLE_FEATURE_LESS_FLAGS
641 else if (eof_error == 0)
642 num_lines = max_lineno;
643#endif
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200644
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000645 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000646#if ENABLE_FEATURE_LESS_REGEXP
647 /* prevent us from being stuck in search for a match */
648 wanted_match = -1;
649#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000650#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000651}
652
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000653#if ENABLE_FEATURE_LESS_FLAGS
Ron Yorston193ba402015-07-21 22:28:09 +0200654static int safe_lineno(int fline)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000655{
Ron Yorston193ba402015-07-21 22:28:09 +0200656 if (fline >= max_fline)
657 fline = max_fline - 1;
658
659 /* also catches empty file (max_fline == 0) */
660 if (fline < 0)
661 return 0;
662
663 return LINENO(flines[fline]) + 1;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000664}
665
Ron Yorston159e0322015-07-24 14:27:42 +0100666/* count number of lines in file */
667static void update_num_lines(void)
668{
669 int count, fd;
Ron Yorstonb27cf312015-07-31 17:33:24 +0100670 struct stat stbuf;
Ron Yorston159e0322015-07-24 14:27:42 +0100671 ssize_t len, i;
672 char buf[4096];
Ron Yorston159e0322015-07-24 14:27:42 +0100673
Ron Yorston70b84be2015-07-24 14:28:08 +0100674 /* only do this for regular files */
Ron Yorstonb27cf312015-07-31 17:33:24 +0100675 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
Ron Yorston159e0322015-07-24 14:27:42 +0100676 count = 0;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200677 fd = open("/proc/self/fd/0", O_RDONLY);
678 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
679 /* "filename" is valid only if REOPEN_AND_COUNT */
680 fd = open(filename, O_RDONLY);
681 }
Ron Yorston70b84be2015-07-24 14:28:08 +0100682 if (fd < 0) {
683 /* somebody stole my file! */
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200684 num_lines = NOT_REGULAR_FILE;
Ron Yorston70b84be2015-07-24 14:28:08 +0100685 return;
686 }
Ron Yorstonb27cf312015-07-31 17:33:24 +0100687 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
688 num_lines = NOT_REGULAR_FILE;
689 goto do_close;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200690 }
Ron Yorston159e0322015-07-24 14:27:42 +0100691 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
692 for (i = 0; i < len; ++i) {
693 if (buf[i] == '\n' && ++count == MAXLINES)
694 goto done;
695 }
696 }
697 done:
698 num_lines = count;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200699 do_close:
Ron Yorston159e0322015-07-24 14:27:42 +0100700 close(fd);
Ron Yorston159e0322015-07-24 14:27:42 +0100701 }
702}
703
Rob Landley9200e792005-09-15 19:26:59 +0000704/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000705static void m_status_print(void)
706{
Ron Yorston193ba402015-07-21 22:28:09 +0200707 int first, last;
708 unsigned percent;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000709
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200710 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000711 return;
712
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000713 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000714 printf(HIGHLIGHT"%s", filename);
715 if (num_files > 1)
716 printf(" (file %i of %i)", current_file, num_files);
Ron Yorston193ba402015-07-21 22:28:09 +0200717
718 first = safe_lineno(cur_fline);
719 last = (option_mask32 & FLAG_S)
720 ? MIN(first + max_displayed_line, max_lineno)
721 : safe_lineno(cur_fline + max_displayed_line);
722 printf(" lines %i-%i", first, last);
723
Ron Yorston159e0322015-07-24 14:27:42 +0100724 update_num_lines();
Ron Yorston193ba402015-07-21 22:28:09 +0200725 if (num_lines >= 0)
726 printf("/%i", num_lines);
727
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100728 if (at_end()) {
Ron Yorston193ba402015-07-21 22:28:09 +0200729 printf(" (END)");
730 if (num_files > 1 && current_file != num_files)
731 printf(" - next: %s", files[current_file]);
732 } else if (num_lines > 0) {
733 percent = (100 * last + num_lines/2) / num_lines;
734 printf(" %i%%", percent <= 100 ? percent : 100);
735 }
736 printf(NORMAL);
Rob Landley9200e792005-09-15 19:26:59 +0000737}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000738#endif
739
Rob Landley9200e792005-09-15 19:26:59 +0000740/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000741static void status_print(void)
742{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000743 const char *p;
744
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200745 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000746 return;
747
Rob Landley9200e792005-09-15 19:26:59 +0000748 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000749#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000750 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000751 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000752 return;
Rob Landley9200e792005-09-15 19:26:59 +0000753 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000754 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000755#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000756
757 clear_line();
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100758 if (cur_fline && !at_end()) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000759 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000760 return;
761 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000762 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000763 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000764 p = filename;
765 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000766 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000767 p, current_file, num_files);
768 return;
769 }
770 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000771}
772
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000773static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000774 /* NUL: never encountered; TAB: not converted */
775 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000776 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
777 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000778static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100779 /* why 40 instead of 4a below? - it is a replacement for '\n'.
780 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000781 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
782 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
783
Ron Yorstonf06386a2015-07-18 16:20:03 +0100784static void print_lineno(const char *line)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000785{
Ron Yorstonf06386a2015-07-18 16:20:03 +0100786 const char *fmt = " ";
787 unsigned n = n; /* for compiler */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000788
Ron Yorstonf06386a2015-07-18 16:20:03 +0100789 if (line != empty_line_marker) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000790 /* Width of 7 preserves tab spacing in the text */
791 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000792 n = LINENO(line) + 1;
Ron Yorstonf06386a2015-07-18 16:20:03 +0100793 if (n > 9999999 && MAXLINES > 9999999) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000794 n %= 10000000;
795 fmt = "%07u ";
796 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000797 }
Ron Yorstonf06386a2015-07-18 16:20:03 +0100798 printf(fmt, n);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000799}
800
801
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000802#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000803static void print_found(const char *line)
804{
805 int match_status;
806 int eflags;
807 char *growline;
808 regmatch_t match_structs;
809
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100810 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000811 const char *str = line;
812 char *p = buf;
813 size_t n;
814
815 while (*str) {
816 n = strcspn(str, controls);
817 if (n) {
818 if (!str[n]) break;
819 memcpy(p, str, n);
820 p += n;
821 str += n;
822 }
823 n = strspn(str, controls);
824 memset(p, '.', n);
825 p += n;
826 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000827 }
828 strcpy(p, str);
829
830 /* buf[] holds quarantined version of str */
831
832 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100833 * and NORMAL escape sequences placed around it.
834 * NB: we regex against line, but insert text
835 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000836 str = buf;
837 growline = NULL;
838 eflags = 0;
839 goto start;
840
841 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000842 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200843 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200844 (int)match_structs.rm_so, str,
845 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000846 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000847 free(growline);
848 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000849 str += match_structs.rm_eo;
850 line += match_structs.rm_eo;
851 eflags = REG_NOTBOL;
852 start:
853 /* Most of the time doesn't find the regex, optimize for that */
854 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000855 /* if even "" matches, treat it as "not a match" */
856 if (match_structs.rm_so >= match_structs.rm_eo)
857 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000858 }
859
Ron Yorstonf06386a2015-07-18 16:20:03 +0100860 printf("%s%s\n", growline ? growline : "", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000861 free(growline);
862}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000863#else
864void print_found(const char *line);
865#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000866
867static void print_ascii(const char *str)
868{
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100869 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000870 char *p;
871 size_t n;
872
873 while (*str) {
874 n = strcspn(str, controls);
875 if (n) {
876 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000877 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000878 str += n;
879 }
880 n = strspn(str, controls);
881 p = buf;
882 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000883 if (*str == 0x7f)
884 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000885 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000886 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
887 /* I want to know who committed this sin */
888 *p++ = '{';
889 else
890 *p++ = ctrlconv[(unsigned char)*str];
891 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000892 } while (--n);
893 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000894 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000895 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000896 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000897}
898
Rob Landley9200e792005-09-15 19:26:59 +0000899/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000900static void buffer_print(void)
901{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000902 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000903
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000904 move_cursor(0, 0);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200905 for (i = 0; i <= max_displayed_line; i++) {
Ron Yorstonf06386a2015-07-18 16:20:03 +0100906 printf(CLEAR_2_EOL);
907 if (option_mask32 & FLAG_N)
908 print_lineno(buffer[i]);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000909 if (pattern_valid)
910 print_found(buffer[i]);
911 else
912 print_ascii(buffer[i]);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200913 }
Aaro Koskinen36941502018-04-15 01:24:24 +0300914 if ((option_mask32 & (FLAG_E|FLAG_F))
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200915 && eof_error <= 0
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200916 ) {
Aaro Koskinen36941502018-04-15 01:24:24 +0300917 i = option_mask32 & FLAG_F ? 0 : cur_fline;
918 if (max_fline - i <= max_displayed_line)
Denys Vlasenko076f5e02022-01-05 22:04:21 +0100919 less_exit();
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200920 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000921 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000922}
923
Denis Vlasenkod9083952008-10-24 10:42:21 +0000924static void buffer_fill_and_print(void)
925{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000926 unsigned i;
Ron Yorston51aa8612015-07-19 11:12:29 +0100927#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000928 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000929
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000930 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000931 /* Go back to the beginning of this line */
932 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
933 fpos--;
934 }
935
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000936 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000937 while (i <= max_displayed_line && fpos <= max_fline) {
938 int lineno = LINENO(flines[fpos]);
939 buffer[i] = flines[fpos];
940 i++;
941 do {
942 fpos++;
943 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000944 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000945 && lineno == LINENO(flines[fpos])
946 );
947 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000948#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000949 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
950 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000951 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000952#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000953 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000954 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000955 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000956 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000957}
958
Ron Yorstond542d182015-07-24 14:29:13 +0100959/* move cur_fline to a given line number, reading lines if necessary */
960static void goto_lineno(int target)
961{
962 if (target <= 0 ) {
963 cur_fline = 0;
964 }
965 else if (target > LINENO(flines[cur_fline])) {
966 retry:
967 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
968 ++cur_fline;
969 /* target not reached but more input is available */
970 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
971 read_lines();
972 goto retry;
973 }
974 }
975 else {
976 /* search backwards through already-read lines */
977 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
978 --cur_fline;
979 }
980}
981
982static void cap_cur_fline(void)
983{
984 if ((option_mask32 & FLAG_S)) {
985 if (cur_fline > max_fline)
986 cur_fline = max_fline;
987 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
988 goto_lineno(max_lineno - max_displayed_line + TILDES);
989 read_lines();
990 }
991 }
992 else {
993 if (cur_fline + max_displayed_line > max_fline + TILDES)
994 cur_fline = max_fline - max_displayed_line + TILDES;
995 if (cur_fline < 0)
996 cur_fline = 0;
997 }
998}
999
Rob Landley9200e792005-09-15 19:26:59 +00001000/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001001static void buffer_down(int nlines)
1002{
Ron Yorstond542d182015-07-24 14:29:13 +01001003 if ((option_mask32 & FLAG_S))
1004 goto_lineno(LINENO(flines[cur_fline]) + nlines);
1005 else
1006 cur_fline += nlines;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001007 read_lines();
Ron Yorstond542d182015-07-24 14:29:13 +01001008 cap_cur_fline();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001009 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +00001010}
1011
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001012static void buffer_up(int nlines)
1013{
Ron Yorstond542d182015-07-24 14:29:13 +01001014 if ((option_mask32 & FLAG_S)) {
1015 goto_lineno(LINENO(flines[cur_fline]) - nlines);
1016 }
1017 else {
1018 cur_fline -= nlines;
1019 if (cur_fline < 0)
1020 cur_fline = 0;
1021 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001022 read_lines();
1023 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +00001024}
1025
Ron Yorstond542d182015-07-24 14:29:13 +01001026/* display a given line where the argument can be either an index into
1027 * the flines array or a line number */
1028static void buffer_to_line(int linenum, int is_lineno)
1029{
1030 if (linenum <= 0)
1031 cur_fline = 0;
1032 else if (is_lineno)
1033 goto_lineno(linenum);
1034 else
1035 cur_fline = linenum;
1036 read_lines();
1037 cap_cur_fline();
1038 buffer_fill_and_print();
1039}
1040
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001041static void buffer_line(int linenum)
1042{
Ron Yorstond542d182015-07-24 14:29:13 +01001043 buffer_to_line(linenum, FALSE);
1044}
1045
1046static void buffer_lineno(int lineno)
1047{
1048 buffer_to_line(lineno, TRUE);
Rob Landley9200e792005-09-15 19:26:59 +00001049}
1050
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001051static void open_file_and_read_lines(void)
1052{
1053 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +00001054 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001055#if ENABLE_FEATURE_LESS_FLAGS
1056 num_lines = REOPEN_AND_COUNT;
1057#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001058 } else {
1059 /* "less" with no arguments in argv[] */
1060 /* For status line only */
1061 filename = xstrdup(bb_msg_standard_input);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001062#if ENABLE_FEATURE_LESS_FLAGS
1063 num_lines = REOPEN_STDIN;
1064#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001065 }
1066 readpos = 0;
1067 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +00001068 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001069 terminated = 1;
1070 read_lines();
1071}
1072
1073/* Reinitialize everything for a new file - free the memory and start over */
1074static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001075{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001076 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001077
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001078 if (flines) {
1079 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +00001080 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001081 free(flines);
1082 flines = NULL;
1083 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001084
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001085 max_fline = -1;
1086 cur_fline = 0;
1087 max_lineno = 0;
1088 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001089#if ENABLE_FEATURE_LESS_ASK_TERMINAL
1090 if (G.winsize_err)
Denys Vlasenko8187e012017-09-13 22:48:30 +02001091 printf(ESC"[999;999H" ESC"[6n");
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001092#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001093 buffer_fill_and_print();
1094}
1095
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001096static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001097{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001098 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001099 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +00001100 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001101
Denis Vlasenko33196372008-02-23 01:25:38 +00001102 pfd[0].fd = STDIN_FILENO;
1103 pfd[0].events = POLLIN;
1104 pfd[1].fd = kbd_fd;
1105 pfd[1].events = POLLIN;
1106 again:
1107 tcsetattr(kbd_fd, TCSANOW, &term_less);
1108 /* NB: select/poll returns whenever read will not block. Therefore:
1109 * if eof is reached, select/poll will return immediately
1110 * because read will immediately return 0 bytes.
1111 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001112 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1113 */
Denis Vlasenko33196372008-02-23 01:25:38 +00001114 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001115 /* Are we interested in stdin? */
Ron Yorstonad1b4d52015-07-24 14:28:50 +01001116 if (at_end()) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001117 if (eof_error > 0) /* did NOT reach eof yet */
1118 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001119 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001120 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +00001121 if (less_gets_pos >= 0)
1122 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001123 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001124
Denys Vlasenko020f4062009-05-17 16:44:54 +02001125 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001126#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001127 while (1) {
1128 int r;
1129 /* NB: SIGWINCH interrupts poll() */
1130 r = poll(pfd + rd, 2 - rd, -1);
1131 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1132 return '\\'; /* anything which has no defined function */
1133 if (r) break;
1134 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001135#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001136 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001137#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001138 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001139
Denys Vlasenko12566e72022-01-17 03:02:40 +01001140 /* We have kbd_fd in O_NONBLOCK mode, read inside safe_read_key()
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001141 * would not block even if there is no input available */
Denys Vlasenko12566e72022-01-17 03:02:40 +01001142 key64 = safe_read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001143 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +00001144 if (errno == EAGAIN) {
1145 /* No keyboard input available. Since poll() did return,
1146 * we should have input on stdin */
1147 read_lines();
1148 buffer_fill_and_print();
1149 goto again;
1150 }
1151 /* EOF/error (ssh session got killed etc) */
Denys Vlasenko076f5e02022-01-05 22:04:21 +01001152 less_exit();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001153 }
Denis Vlasenko33196372008-02-23 01:25:38 +00001154 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001155 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001156}
1157
Denys Vlasenko020f4062009-05-17 16:44:54 +02001158/* Grab a character from input without requiring the return key.
1159 * May return KEYCODE_xxx values.
1160 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001161static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001162{
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001163 int64_t key64;
1164 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001165
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001166 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001167 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001168 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001169 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +00001170
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001171 /* Discard Ctrl-something chars.
1172 * (checking only lower 32 bits is a size optimization:
1173 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1174 */
1175 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +00001176 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +02001177
1178 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001179}
1180
1181static char* less_gets(int sz)
1182{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001183 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001184 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001185 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001186
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001187 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001188 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001189 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001190 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001191 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001192 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001193 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001194 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001195 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001196 if (c == 0x7f)
1197 c = 8;
1198 if (c == 8 && i) {
1199 printf("\x8 \x8");
1200 i--;
1201 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001202 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001203 continue;
1204 if (i >= width - sz - 1)
1205 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001206 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001207 result[i++] = c;
1208 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001209 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001210}
1211
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001212static void examine_file(void)
1213{
Denis Vlasenko33196372008-02-23 01:25:38 +00001214 char *new_fname;
1215
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001216 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001217 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001218 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001219 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001220 err:
1221 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001222 return;
1223 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001224 if (access(new_fname, R_OK) != 0) {
1225 print_statusline("Cannot read this file");
1226 goto err;
1227 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001228 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001229 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001230 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001231 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001232 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001233 num_files++; */
1234 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001235 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001236 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001237}
1238
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001239/* This function changes the file currently being paged. direction can be one of the following:
1240 * -1: go back one file
1241 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001242 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001243static void change_file(int direction)
1244{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001245 if (current_file != ((direction > 0) ? num_files : 1)) {
1246 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001247 free(filename);
1248 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001249 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001250 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001251 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001252 }
1253}
1254
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001255static void remove_current_file(void)
1256{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001257 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001258
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001259 if (num_files < 2)
1260 return;
1261
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001262 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001263 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001264 for (i = 3; i <= num_files; i++)
1265 files[i - 2] = files[i - 1];
1266 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001267 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001268 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001269 for (i = 2; i <= num_files; i++)
1270 files[i - 2] = files[i - 1];
1271 num_files--;
1272 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001273 }
1274}
1275
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001276static void colon_process(void)
1277{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001278 int keypress;
1279
1280 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001281 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001282
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001283 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001284 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001285 case 'd':
1286 remove_current_file();
1287 break;
1288 case 'e':
1289 examine_file();
1290 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001291#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001292 case 'f':
1293 m_status_print();
1294 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001295#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001296 case 'n':
1297 change_file(1);
1298 break;
1299 case 'p':
1300 change_file(-1);
1301 break;
1302 case 'q':
Denys Vlasenko076f5e02022-01-05 22:04:21 +01001303 less_exit();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001304 break;
1305 case 'x':
1306 change_file(0);
1307 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001308 }
1309}
1310
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001311#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001312static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001313{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001314 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001315 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001316 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001317 match = 0;
1318 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001319}
1320
Rob Landleya2e98042006-04-18 01:53:41 +00001321static void goto_match(int match)
1322{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001323 if (!pattern_valid)
1324 return;
1325 if (match < 0)
1326 match = 0;
1327 /* Try to find next match if eof isn't reached yet */
1328 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001329 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001330 read_lines();
1331 }
1332 if (num_matches) {
1333 normalize_match_pos(match);
1334 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001335 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001336 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001337 }
Rob Landleya2e98042006-04-18 01:53:41 +00001338}
1339
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001340static void fill_match_lines(unsigned pos)
1341{
1342 if (!pattern_valid)
1343 return;
1344 /* Run the regex on each line of the current file */
1345 while (pos <= max_fline) {
1346 /* If this line matches */
1347 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1348 /* and we didn't match it last time */
1349 && !(num_matches && match_lines[num_matches-1] == pos)
1350 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001351 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001352 match_lines[num_matches++] = pos;
1353 }
1354 pos++;
1355 }
1356}
1357
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001358static void regex_process(void)
1359{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001360 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001361
1362 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001363 free(match_lines);
1364 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001365 match_pos = 0;
1366 num_matches = 0;
1367 if (pattern_valid) {
1368 regfree(&pattern);
1369 pattern_valid = 0;
1370 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001371
Rob Landleya2e98042006-04-18 01:53:41 +00001372 /* Get the uncompiled regular expression from the user */
1373 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001374 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001375 uncomp_regex = less_gets(1);
1376 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001377 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001378 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001379 return;
1380 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001381
Rob Landleya2e98042006-04-18 01:53:41 +00001382 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001383 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001384 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001385 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001386 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001387 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001388 free(err);
1389 return;
1390 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001391
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001392 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001393 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001394 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001395 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001396 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001397 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001398 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001399 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001400 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1401 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001402
1403 /* It's possible that no matches are found yet.
1404 * goto_match() will read input looking for match,
1405 * if needed */
1406 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001407}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001408#endif
1409
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001410static void number_process(int first_digit)
1411{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001412 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001413 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001414 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001415 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001416
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001417 num_input[0] = first_digit;
1418
1419 /* Clear the current line, print a prompt, and then print the digit */
1420 clear_line();
1421 printf(":%c", first_digit);
1422
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001423 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001424 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001425 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001426 keypress = less_getch(i + 1);
Ron Yorstonae1a9e82015-07-21 20:12:31 +01001427 if ((unsigned)keypress > 255 || !isdigit(keypress))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001428 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001429 num_input[i] = keypress;
1430 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001431 i++;
1432 }
1433
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001434 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001435 num = bb_strtou(num_input, NULL, 10);
1436 /* on format error, num == -1 */
1437 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001438 buffer_print();
1439 return;
1440 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001441
1442 /* We now know the number and the letter entered, so we process them */
1443 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001444 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001445 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001446 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001447 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001448 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001449 break;
1450 case 'g': case '<': case 'G': case '>':
Ron Yorstond542d182015-07-24 14:29:13 +01001451 buffer_lineno(num - 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001452 break;
1453 case 'p': case '%':
Ron Yorstond542d182015-07-24 14:29:13 +01001454#if ENABLE_FEATURE_LESS_FLAGS
1455 update_num_lines();
1456 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1457#else
1458 num = num * max_lineno / 100;
1459#endif
1460 buffer_lineno(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001461 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001462#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001463 case 'n':
1464 goto_match(match_pos + num);
1465 break;
1466 case '/':
1467 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1468 regex_process();
1469 break;
1470 case '?':
1471 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1472 regex_process();
1473 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001474#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001475 }
1476}
1477
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001478#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001479static void flag_change(void)
1480{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001481 int keypress;
1482
1483 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001484 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001485 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001486
1487 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001488 case 'M':
1489 option_mask32 ^= FLAG_M;
1490 break;
1491 case 'm':
1492 option_mask32 ^= FLAG_m;
1493 break;
1494 case 'E':
1495 option_mask32 ^= FLAG_E;
1496 break;
1497 case '~':
1498 option_mask32 ^= FLAG_TILDE;
1499 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001500#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001501 case 'S':
1502 option_mask32 ^= FLAG_S;
1503 buffer_fill_and_print();
1504 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001505#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001506#if ENABLE_FEATURE_LESS_LINENUMS
1507 case 'N':
1508 option_mask32 ^= FLAG_N;
1509 re_wrap();
1510 buffer_fill_and_print();
1511 break;
1512#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001513 }
1514}
1515
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001516#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001517static void show_flag_status(void)
1518{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001519 int keypress;
1520 int flag_val;
1521
1522 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001523 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001524 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001525
1526 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001527 case 'M':
1528 flag_val = option_mask32 & FLAG_M;
1529 break;
1530 case 'm':
1531 flag_val = option_mask32 & FLAG_m;
1532 break;
1533 case '~':
1534 flag_val = option_mask32 & FLAG_TILDE;
1535 break;
1536 case 'N':
1537 flag_val = option_mask32 & FLAG_N;
1538 break;
1539 case 'E':
1540 flag_val = option_mask32 & FLAG_E;
1541 break;
1542 default:
1543 flag_val = 0;
1544 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001545 }
1546
1547 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001548 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001549}
1550#endif
1551
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001552#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1553
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001554static void save_input_to_file(void)
1555{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001556 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001557 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001558 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001559 FILE *fp;
1560
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001561 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001562 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001563 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001564 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001565 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001566 msg = "Error opening log file";
1567 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001568 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001569 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001570 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001571 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001572 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001573 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001574 ret:
1575 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001576 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001577}
1578
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001579#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001580static void add_mark(void)
1581{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001582 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001583
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001584 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001585 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001586
1587 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001588 /* If we exceed 15 marks, start overwriting previous ones */
1589 if (num_marks == 14)
1590 num_marks = 0;
1591
1592 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001593 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001594 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001595 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001596 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001597 }
1598}
1599
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001600static void goto_mark(void)
1601{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001602 int letter;
1603 int i;
1604
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001605 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001606 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001607 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001608
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001609 if (isalpha(letter)) {
1610 for (i = 0; i <= num_marks; i++)
1611 if (letter == mark_lines[i][0]) {
1612 buffer_line(mark_lines[i][1]);
1613 break;
1614 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001615 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001616 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001617 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001618 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001619}
1620#endif
1621
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001622#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001623static char opp_bracket(char bracket)
1624{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001625 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001626 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1627 bracket++;
1628 case '(': /* ')' == '(' + 1 */
1629 bracket++;
1630 break;
1631 case '}': case ']':
1632 bracket--;
1633 case ')':
1634 bracket--;
1635 break;
1636 };
1637 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001638}
1639
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001640static void match_right_bracket(char bracket)
1641{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001642 unsigned i = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001643
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001644 if (i >= max_fline
1645 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001646 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001647 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001648 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001649 }
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001650
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001651 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001652 for (; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001653 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001654 /*
1655 * Line with matched right bracket becomes
1656 * last visible line
1657 */
1658 buffer_line(i - max_displayed_line);
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001659 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001660 }
1661 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001662 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001663}
1664
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001665static void match_left_bracket(char bracket)
1666{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001667 int i = cur_fline + max_displayed_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001668
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001669 if (i >= max_fline
1670 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001671 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001672 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001673 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001674 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001675
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001676 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001677 for (; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001678 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001679 /*
1680 * Line with matched left bracket becomes
1681 * first visible line
1682 */
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001683 buffer_line(i);
1684 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001685 }
1686 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001687 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001688}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001689#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001690
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001691static void keypress_process(int keypress)
1692{
Rob Landley9200e792005-09-15 19:26:59 +00001693 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001694 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001695 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001696 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001697 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001698 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001699 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001700 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001701 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001702 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001703 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001704 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001705 break;
1706 case 'd':
1707 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001708 break;
1709 case 'u':
1710 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001711 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001712 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001713 buffer_line(0);
1714 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001715 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001716 cur_fline = MAXLINES;
1717 read_lines();
1718 buffer_line(cur_fline);
1719 break;
1720 case 'q': case 'Q':
Denys Vlasenko076f5e02022-01-05 22:04:21 +01001721 less_exit();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001722 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001723#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001724 case 'm':
1725 add_mark();
1726 buffer_print();
1727 break;
1728 case '\'':
1729 goto_mark();
1730 buffer_print();
1731 break;
Rob Landley9200e792005-09-15 19:26:59 +00001732#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001733 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001734 /* TODO: (1) also bind ^R, ^L to this?
1735 * (2) re-measure window size?
1736 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001737 buffer_print();
1738 break;
1739 /*case 'R':
1740 full_repaint();
1741 break;*/
1742 case 's':
1743 save_input_to_file();
1744 break;
1745 case 'E':
1746 examine_file();
1747 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001748#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001749 case '=':
1750 m_status_print();
1751 break;
Rob Landley9200e792005-09-15 19:26:59 +00001752#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001753#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001754 case '/':
1755 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1756 regex_process();
1757 break;
1758 case 'n':
1759 goto_match(match_pos + 1);
1760 break;
1761 case 'N':
1762 goto_match(match_pos - 1);
1763 break;
1764 case '?':
1765 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1766 regex_process();
1767 break;
Rob Landley9200e792005-09-15 19:26:59 +00001768#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001769#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001770 case '-':
1771 flag_change();
1772 buffer_print();
1773 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001774#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001775 case '_':
1776 show_flag_status();
1777 break;
Rob Landley9200e792005-09-15 19:26:59 +00001778#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001779#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001780#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001781 case '{': case '(': case '[':
1782 match_right_bracket(keypress);
1783 break;
1784 case '}': case ')': case ']':
1785 match_left_bracket(keypress);
1786 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001787#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001788 case ':':
1789 colon_process();
1790 break;
Rob Landley9200e792005-09-15 19:26:59 +00001791 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001792
Rob Landley9200e792005-09-15 19:26:59 +00001793 if (isdigit(keypress))
1794 number_process(keypress);
1795}
1796
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001797static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001798{
Denys Vlasenko076f5e02022-01-05 22:04:21 +01001799 restore_tty();
1800 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001801}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001802
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001803#if ENABLE_FEATURE_LESS_WINCH
1804static void sigwinch_handler(int sig UNUSED_PARAM)
1805{
1806 winch_counter++;
1807}
1808#endif
1809
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001810int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001811int less_main(int argc, char **argv)
1812{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001813 char *tty_name;
1814 int tty_fd;
1815
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001816 INIT_G();
1817
Denys Vlasenko821e6432014-01-22 16:36:22 +01001818 /* TODO: -x: do not interpret backspace, -xx: tab also
1819 * -xxx: newline also
1820 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1821 * -s: condense many empty lines to one
1822 * (used by some setups for manpage display)
1823 */
Aaro Koskinen36941502018-04-15 01:24:24 +03001824 getopt32(argv, "EMmN~IF"
Denys Vlasenko0e0209a2018-02-27 11:47:51 +01001825 IF_FEATURE_LESS_TRUNCATE("S")
1826 IF_FEATURE_LESS_RAW("R")
Ismael Luceno35727f52021-09-28 21:47:49 +02001827 /*ignored:*/"sX"
Denys Vlasenko0e0209a2018-02-27 11:47:51 +01001828 );
Rob Landley9200e792005-09-15 19:26:59 +00001829 argv += optind;
Denys Vlasenko83c99ab2018-01-08 10:27:23 +01001830 num_files = argc - optind;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001831 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001832
Denys Vlasenko8528d3d2018-01-22 14:38:51 +01001833 /* Tools typically pass LESS="FRSXMK".
1834 * The options we don't understand are ignored. */
1835 if (ENABLE_FEATURE_LESS_ENV) {
1836 char *c = getenv("LESS");
1837 if (c) while (*c) switch (*c++) {
Aaro Koskinen36941502018-04-15 01:24:24 +03001838 case 'F':
1839 option_mask32 |= FLAG_F;
1840 break;
Denys Vlasenko8528d3d2018-01-22 14:38:51 +01001841 case 'M':
1842 option_mask32 |= FLAG_M;
1843 break;
1844 case 'R':
1845 option_mask32 |= FLAG_R;
1846 break;
1847 case 'S':
1848 option_mask32 |= FLAG_S;
1849 break;
1850 default:
1851 break;
1852 }
1853 }
1854
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001855 /* Another popular pager, most, detects when stdout
1856 * is not a tty and turns into cat. This makes sense. */
1857 if (!isatty(STDOUT_FILENO))
1858 return bb_cat(argv);
1859
Rob Landley9200e792005-09-15 19:26:59 +00001860 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001861 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001862 /* Just "less"? No args and no redirection? */
Rob Landley9200e792005-09-15 19:26:59 +00001863 bb_show_usage();
1864 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001865 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001866 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001867 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001868
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001869 if (option_mask32 & FLAG_TILDE)
1870 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001871
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001872 /* Some versions of less can survive w/o controlling tty,
1873 * try to do the same. This also allows to specify an alternative
1874 * tty via "less 1<>TTY".
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001875 *
1876 * We prefer not to use STDOUT_FILENO directly,
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001877 * since we want to set this fd to non-blocking mode,
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001878 * and not interfere with other processes which share stdout with us.
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001879 */
1880 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1881 if (tty_name) {
1882 tty_fd = open(tty_name, O_RDONLY);
1883 free(tty_name);
1884 if (tty_fd < 0)
1885 goto try_ctty;
1886 } else {
1887 /* Try controlling tty */
1888 try_ctty:
1889 tty_fd = open(CURRENT_TTY, O_RDONLY);
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001890 if (tty_fd < 0) {
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001891 /* If all else fails, less 481 uses stdout. Mimic that */
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001892 tty_fd = STDOUT_FILENO;
1893 }
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001894 }
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001895 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001896 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001897
Denys Vlasenko058a1532018-04-16 10:24:48 +02001898 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL_INPUT);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001899
Denys Vlasenkoaaaaaa52017-09-15 17:14:01 +02001900 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 +00001901 /* 20: two tabstops + 4 */
1902 if (width < 20 || max_displayed_line < 3)
1903 return bb_cat(argv);
1904 max_displayed_line -= 2;
1905
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001906 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001907 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001908#if ENABLE_FEATURE_LESS_WINCH
1909 signal(SIGWINCH, sigwinch_handler);
1910#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001911
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001912 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001913 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001914 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001915 int64_t keypress;
1916
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001917#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001918 while (WINCH_COUNTER) {
1919 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001920 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001921 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1922 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001923 /* 20: two tabstops + 4 */
1924 if (width < 20)
1925 width = 20;
1926 if (max_displayed_line < 3)
1927 max_displayed_line = 3;
1928 max_displayed_line -= 2;
1929 free(buffer);
1930 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001931 /* Avoid re-wrap and/or redraw if we already know
1932 * we need to do it again. These ops are expensive */
1933 if (WINCH_COUNTER)
1934 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001935 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001936 if (WINCH_COUNTER)
1937 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001938 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001939 /* This took some time. Loop back and check,
1940 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001941 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001942 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001943# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1944 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1945 uint32_t rc = (keypress >> 32);
1946 width = (rc & 0x7fff);
1947 max_displayed_line = ((rc >> 16) & 0x7fff);
1948 goto got_size;
1949 }
1950# endif
1951#else
1952 keypress = less_getch(-1); /* -1: do not position cursor */
1953#endif
Rob Landley9200e792005-09-15 19:26:59 +00001954 keypress_process(keypress);
1955 }
1956}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001957
1958/*
1959Help text of less version 418 is below.
1960If you are implementing something, keeping
1961key and/or command line switch compatibility is a good idea:
1962
1963
1964 SUMMARY OF LESS COMMANDS
1965
1966 Commands marked with * may be preceded by a number, N.
1967 Notes in parentheses indicate the behavior if N is given.
1968 h H Display this help.
1969 q :q Q :Q ZZ Exit.
1970 ---------------------------------------------------------------------------
1971 MOVING
1972 e ^E j ^N CR * Forward one line (or N lines).
1973 y ^Y k ^K ^P * Backward one line (or N lines).
1974 f ^F ^V SPACE * Forward one window (or N lines).
1975 b ^B ESC-v * Backward one window (or N lines).
1976 z * Forward one window (and set window to N).
1977 w * Backward one window (and set window to N).
1978 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1979 d ^D * Forward one half-window (and set half-window to N).
1980 u ^U * Backward one half-window (and set half-window to N).
1981 ESC-) RightArrow * Left one half screen width (or N positions).
1982 ESC-( LeftArrow * Right one half screen width (or N positions).
1983 F Forward forever; like "tail -f".
1984 r ^R ^L Repaint screen.
1985 R Repaint screen, discarding buffered input.
1986 ---------------------------------------------------
1987 Default "window" is the screen height.
1988 Default "half-window" is half of the screen height.
1989 ---------------------------------------------------------------------------
1990 SEARCHING
1991 /pattern * Search forward for (N-th) matching line.
1992 ?pattern * Search backward for (N-th) matching line.
1993 n * Repeat previous search (for N-th occurrence).
1994 N * Repeat previous search in reverse direction.
1995 ESC-n * Repeat previous search, spanning files.
1996 ESC-N * Repeat previous search, reverse dir. & spanning files.
1997 ESC-u Undo (toggle) search highlighting.
1998 ---------------------------------------------------
1999 Search patterns may be modified by one or more of:
2000 ^N or ! Search for NON-matching lines.
2001 ^E or * Search multiple files (pass thru END OF FILE).
2002 ^F or @ Start search at FIRST file (for /) or last file (for ?).
2003 ^K Highlight matches, but don't move (KEEP position).
2004 ^R Don't use REGULAR EXPRESSIONS.
2005 ---------------------------------------------------------------------------
2006 JUMPING
2007 g < ESC-< * Go to first line in file (or line N).
2008 G > ESC-> * Go to last line in file (or line N).
2009 p % * Go to beginning of file (or N percent into file).
2010 t * Go to the (N-th) next tag.
2011 T * Go to the (N-th) previous tag.
2012 { ( [ * Find close bracket } ) ].
2013 } ) ] * Find open bracket { ( [.
2014 ESC-^F <c1> <c2> * Find close bracket <c2>.
2015 ESC-^B <c1> <c2> * Find open bracket <c1>
2016 ---------------------------------------------------
2017 Each "find close bracket" command goes forward to the close bracket
2018 matching the (N-th) open bracket in the top line.
2019 Each "find open bracket" command goes backward to the open bracket
2020 matching the (N-th) close bracket in the bottom line.
2021 m<letter> Mark the current position with <letter>.
2022 '<letter> Go to a previously marked position.
2023 '' Go to the previous position.
2024 ^X^X Same as '.
2025 ---------------------------------------------------
2026 A mark is any upper-case or lower-case letter.
2027 Certain marks are predefined:
2028 ^ means beginning of the file
2029 $ means end of the file
2030 ---------------------------------------------------------------------------
2031 CHANGING FILES
2032 :e [file] Examine a new file.
2033 ^X^V Same as :e.
2034 :n * Examine the (N-th) next file from the command line.
2035 :p * Examine the (N-th) previous file from the command line.
2036 :x * Examine the first (or N-th) file from the command line.
2037 :d Delete the current file from the command line list.
2038 = ^G :f Print current file name.
2039 ---------------------------------------------------------------------------
2040 MISCELLANEOUS COMMANDS
2041 -<flag> Toggle a command line option [see OPTIONS below].
2042 --<name> Toggle a command line option, by name.
2043 _<flag> Display the setting of a command line option.
2044 __<name> Display the setting of an option, by name.
2045 +cmd Execute the less cmd each time a new file is examined.
2046 !command Execute the shell command with $SHELL.
2047 |Xcommand Pipe file between current pos & mark X to shell command.
2048 v Edit the current file with $VISUAL or $EDITOR.
2049 V Print version number of "less".
2050 ---------------------------------------------------------------------------
2051 OPTIONS
2052 Most options may be changed either on the command line,
2053 or from within less by using the - or -- command.
2054 Options may be given in one of two forms: either a single
Maninder Singh97c64912015-05-25 13:46:36 +02002055 character preceded by a -, or a name preceded by --.
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00002056 -? ........ --help
2057 Display help (from command line).
2058 -a ........ --search-skip-screen
2059 Forward search skips current screen.
2060 -b [N] .... --buffers=[N]
2061 Number of buffers.
2062 -B ........ --auto-buffers
2063 Don't automatically allocate buffers for pipes.
2064 -c ........ --clear-screen
2065 Repaint by clearing rather than scrolling.
2066 -d ........ --dumb
2067 Dumb terminal.
2068 -D [xn.n] . --color=xn.n
2069 Set screen colors. (MS-DOS only)
2070 -e -E .... --quit-at-eof --QUIT-AT-EOF
2071 Quit at end of file.
2072 -f ........ --force
2073 Force open non-regular files.
2074 -F ........ --quit-if-one-screen
2075 Quit if entire file fits on first screen.
2076 -g ........ --hilite-search
2077 Highlight only last match for searches.
2078 -G ........ --HILITE-SEARCH
2079 Don't highlight any matches for searches.
2080 -h [N] .... --max-back-scroll=[N]
2081 Backward scroll limit.
2082 -i ........ --ignore-case
2083 Ignore case in searches that do not contain uppercase.
2084 -I ........ --IGNORE-CASE
2085 Ignore case in all searches.
2086 -j [N] .... --jump-target=[N]
2087 Screen position of target lines.
2088 -J ........ --status-column
2089 Display a status column at left edge of screen.
2090 -k [file] . --lesskey-file=[file]
2091 Use a lesskey file.
2092 -L ........ --no-lessopen
2093 Ignore the LESSOPEN environment variable.
2094 -m -M .... --long-prompt --LONG-PROMPT
2095 Set prompt style.
2096 -n -N .... --line-numbers --LINE-NUMBERS
2097 Don't use line numbers.
2098 -o [file] . --log-file=[file]
2099 Copy to log file (standard input only).
2100 -O [file] . --LOG-FILE=[file]
2101 Copy to log file (unconditionally overwrite).
2102 -p [pattern] --pattern=[pattern]
2103 Start at pattern (from command line).
2104 -P [prompt] --prompt=[prompt]
2105 Define new prompt.
2106 -q -Q .... --quiet --QUIET --silent --SILENT
2107 Quiet the terminal bell.
2108 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2109 Output "raw" control characters.
2110 -s ........ --squeeze-blank-lines
2111 Squeeze multiple blank lines.
2112 -S ........ --chop-long-lines
2113 Chop long lines.
2114 -t [tag] .. --tag=[tag]
2115 Find a tag.
2116 -T [tagsfile] --tag-file=[tagsfile]
2117 Use an alternate tags file.
2118 -u -U .... --underline-special --UNDERLINE-SPECIAL
2119 Change handling of backspaces.
2120 -V ........ --version
2121 Display the version number of "less".
2122 -w ........ --hilite-unread
2123 Highlight first new line after forward-screen.
2124 -W ........ --HILITE-UNREAD
2125 Highlight first new line after any forward movement.
2126 -x [N[,...]] --tabs=[N[,...]]
2127 Set tab stops.
2128 -X ........ --no-init
2129 Don't use termcap init/deinit strings.
2130 --no-keypad
2131 Don't use termcap keypad init/deinit strings.
2132 -y [N] .... --max-forw-scroll=[N]
2133 Forward scroll limit.
2134 -z [N] .... --window=[N]
2135 Set size of window.
2136 -" [c[c]] . --quotes=[c[c]]
2137 Set shell quote characters.
2138 -~ ........ --tilde
2139 Don't display tildes after end of file.
2140 -# [N] .... --shift=[N]
2141 Horizontal scroll amount (0 = one half screen width)
2142
2143 ---------------------------------------------------------------------------
2144 LINE EDITING
2145 These keys can be used to edit text being entered
2146 on the "command line" at the bottom of the screen.
2147 RightArrow ESC-l Move cursor right one character.
2148 LeftArrow ESC-h Move cursor left one character.
2149 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2150 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2151 HOME ESC-0 Move cursor to start of line.
2152 END ESC-$ Move cursor to end of line.
2153 BACKSPACE Delete char to left of cursor.
2154 DELETE ESC-x Delete char under cursor.
2155 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2156 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2157 CNTL-U ESC (MS-DOS only) Delete entire line.
2158 UpArrow ESC-k Retrieve previous command line.
2159 DownArrow ESC-j Retrieve next command line.
2160 TAB Complete filename & cycle.
2161 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2162 CNTL-L Complete filename, list all.
2163*/