blob: 61acfdcb5045c06ae69b885f9c8d78f73c6dce6a [file] [log] [blame]
Rob Landley9200e792005-09-15 19:26:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini less implementation for busybox
4 *
Rob Landley9200e792005-09-15 19:26:59 +00005 * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysingerf284c762006-04-16 20:38:26 +00008 */
9
10/*
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000011 * TODO:
12 * - Add more regular expression support - search modifiers, certain matches, etc.
13 * - Add more complex bracket searching - currently, nested brackets are
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000014 * not considered.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000015 * - Add support for "F" as an input. This causes less to act in
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000016 * a similar way to tail -f.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000017 * - Allow horizontal scrolling.
Rob Landley9200e792005-09-15 19:26:59 +000018 *
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000019 * Notes:
20 * - the inp file pointer is used so that keyboard input works after
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000021 * redirected input has been read from stdin
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000022 */
Rob Landley9200e792005-09-15 19:26:59 +000023
Denys Vlasenko4e552a72011-07-25 15:18:20 +020024//config:config LESS
25//config: bool "less"
26//config: default y
27//config: help
28//config: 'less' is a pager, meaning that it displays text files. It possesses
29//config: a wide array of features, and is an improvement over 'more'.
30//config:
31//config:config FEATURE_LESS_MAXLINES
32//config: int "Max number of input lines less will try to eat"
33//config: default 9999999
34//config: depends on LESS
35//config:
36//config:config FEATURE_LESS_BRACKETS
37//config: bool "Enable bracket searching"
38//config: default y
39//config: depends on LESS
40//config: help
41//config: This option adds the capability to search for matching left and right
42//config: brackets, facilitating programming.
43//config:
44//config:config FEATURE_LESS_FLAGS
Denys Vlasenko53772862012-04-17 16:11:25 +020045//config: bool "Enable -m/-M"
Denys Vlasenko4e552a72011-07-25 15:18:20 +020046//config: default y
47//config: depends on LESS
48//config: help
Denys Vlasenko53772862012-04-17 16:11:25 +020049//config: The -M/-m flag enables a more sophisticated status line.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020050//config:
Ron Yorston51aa8612015-07-19 11:12:29 +010051//config:config FEATURE_LESS_TRUNCATE
52//config: bool "Enable -S"
53//config: default y
54//config: depends on LESS
55//config: help
56//config: The -S flag causes long lines to be truncated rather than
57//config: wrapped.
58//config:
Denys Vlasenko4e552a72011-07-25 15:18:20 +020059//config:config FEATURE_LESS_MARKS
60//config: bool "Enable marks"
61//config: default y
62//config: depends on LESS
63//config: help
64//config: Marks enable positions in a file to be stored for easy reference.
65//config:
66//config:config FEATURE_LESS_REGEXP
67//config: bool "Enable regular expressions"
68//config: default y
69//config: depends on LESS
70//config: help
71//config: Enable regular expressions, allowing complex file searches.
72//config:
73//config:config FEATURE_LESS_WINCH
74//config: bool "Enable automatic resizing on window size changes"
75//config: default y
76//config: depends on LESS
77//config: help
78//config: Makes less track window size changes.
79//config:
80//config:config FEATURE_LESS_ASK_TERMINAL
81//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
82//config: default y
83//config: depends on FEATURE_LESS_WINCH
84//config: help
85//config: Makes less track window size changes.
86//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
87//config: this option makes less perform a last-ditch effort to find it:
88//config: position cursor to 999,999 and ask terminal to report real
89//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
90//config:
91//config: This is not clean but helps a lot on serial lines and such.
92//config:
93//config:config FEATURE_LESS_DASHCMD
94//config: bool "Enable flag changes ('-' command)"
95//config: default y
96//config: depends on LESS
97//config: help
98//config: This enables the ability to change command-line flags within
99//config: less itself ('-' keyboard command).
100//config:
101//config:config FEATURE_LESS_LINENUMS
102//config: bool "Enable dynamic switching of line numbers"
103//config: default y
104//config: depends on FEATURE_LESS_DASHCMD
105//config: help
106//config: Enables "-N" command.
107
Pere Orga5bc8c002011-04-11 03:29:49 +0200108//usage:#define less_trivial_usage
Ron Yorston51aa8612015-07-19 11:12:29 +0100109//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm")
110//usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200111//usage:#define less_full_usage "\n\n"
112//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200113//usage: "\n -E Quit once the end of a file is reached"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100114//usage: IF_FEATURE_LESS_REGEXP(
115//usage: "\n -I Ignore case in all searches"
116//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200117//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200118//usage: "\n -M,-m Display status line with line numbers"
119//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200120//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200121//usage: "\n -N Prefix line number to each line"
Ron Yorston51aa8612015-07-19 11:12:29 +0100122//usage: IF_FEATURE_LESS_TRUNCATE(
123//usage: "\n -S Truncate long lines"
124//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200125//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200126
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200127#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000128
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000129#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200130#include "common_bufsiz.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000131#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000132#include "xregex.h"
133#endif
134
Marek Polacek7b181072010-10-28 21:34:56 +0200135
136#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000137/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200138#define HIGHLIGHT ESC"[7m"
139#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200140/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200141#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200142/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200143#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000144
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000145enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000146/* Absolute max of lines eaten */
147 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000148/* This many "after the end" lines we will show (at max) */
149 TILDES = 1,
150};
151
Rob Landley9200e792005-09-15 19:26:59 +0000152/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000153enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000154 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000155 FLAG_M = 1 << 1,
156 FLAG_m = 1 << 2,
157 FLAG_N = 1 << 3,
158 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000159 FLAG_I = 1 << 5,
Ron Yorston51aa8612015-07-19 11:12:29 +0100160 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000161/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000162 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
163};
Rob Landley9200e792005-09-15 19:26:59 +0000164
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000165#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000166enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000167#endif
168
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000169struct globals {
170 int cur_fline; /* signed */
171 int kbd_fd; /* fd to get input from */
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200172 int kbd_fd_orig_flags;
Denis Vlasenko33196372008-02-23 01:25:38 +0000173 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000174/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000175 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000176 unsigned max_fline;
177 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000178 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000179 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000180#if ENABLE_FEATURE_LESS_WINCH
181 unsigned winch_counter;
182#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000183 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000184 ssize_t readpos;
185 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000186 const char **buffer;
187 const char **flines;
188 const char *empty_line_marker;
189 unsigned num_files;
190 unsigned current_file;
191 char *filename;
192 char **files;
Ron Yorston193ba402015-07-21 22:28:09 +0200193#if ENABLE_FEATURE_LESS_FLAGS
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200194 int num_lines; /* a flag if < 0, line count if >= 0 */
195# define REOPEN_AND_COUNT (-1)
196# define REOPEN_STDIN (-2)
197# define NOT_REGULAR_FILE (-3)
Ron Yorston193ba402015-07-21 22:28:09 +0200198#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000199#if ENABLE_FEATURE_LESS_MARKS
200 unsigned num_marks;
201 unsigned mark_lines[15][2];
202#endif
203#if ENABLE_FEATURE_LESS_REGEXP
204 unsigned *match_lines;
205 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000206 int wanted_match; /* signed! */
207 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000208 regex_t pattern;
209 smallint pattern_valid;
210#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200211#if ENABLE_FEATURE_LESS_ASK_TERMINAL
212 smallint winsize_err;
213#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000214 smallint terminated;
215 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000216 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000217};
218#define G (*ptr_to_globals)
219#define cur_fline (G.cur_fline )
220#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000221#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000222#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000223#define max_fline (G.max_fline )
224#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000225#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000226#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000227#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000228/* This one is 100% not cached by compiler on read access */
229#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000230#define eof_error (G.eof_error )
231#define readpos (G.readpos )
232#define readeof (G.readeof )
233#define buffer (G.buffer )
234#define flines (G.flines )
235#define empty_line_marker (G.empty_line_marker )
236#define num_files (G.num_files )
237#define current_file (G.current_file )
238#define filename (G.filename )
239#define files (G.files )
Ron Yorston193ba402015-07-21 22:28:09 +0200240#define num_lines (G.num_lines )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000241#define num_marks (G.num_marks )
242#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000243#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000244#define match_lines (G.match_lines )
245#define match_pos (G.match_pos )
246#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000247#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000248#define pattern (G.pattern )
249#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000250#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000251#define terminated (G.terminated )
252#define term_orig (G.term_orig )
253#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000254#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000255#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000256 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
257 less_gets_pos = -1; \
258 empty_line_marker = "~"; \
259 num_files = 1; \
260 current_file = 1; \
261 eof_error = 1; \
262 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000263 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000264} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000265
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000266/* flines[] are lines read from stdin, each in malloc'ed buffer.
267 * Line numbers are stored as uint32_t prepended to each line.
268 * Pointer is adjusted so that flines[i] points directly past
269 * line number. Accesor: */
270#define MEMPTR(p) ((char*)(p) - 4)
271#define LINENO(p) (*(uint32_t*)((p) - 4))
272
273
Rob Landley9200e792005-09-15 19:26:59 +0000274/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000275static void set_tty_cooked(void)
276{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100277 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000278 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000279}
280
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000281/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000282 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000283static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000284{
Marek Polacek7b181072010-10-28 21:34:56 +0200285 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000286}
287
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000288static void clear_line(void)
289{
Marek Polacek7b181072010-10-28 21:34:56 +0200290 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000291}
292
293static void print_hilite(const char *str)
294{
295 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000296}
297
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000298static void print_statusline(const char *str)
299{
300 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000301 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000302}
303
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000304/* Exit the program gracefully */
305static void less_exit(int code)
306{
307 set_tty_cooked();
Denys Vlasenko7fa799a2016-09-13 21:05:48 +0200308 if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
309 ndelay_off(kbd_fd);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000310 clear_line();
311 if (code < 0)
312 kill_myself_with_sig(- code); /* does not return */
313 exit(code);
314}
315
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000316#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
317 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000318static void re_wrap(void)
319{
320 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000321 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000322 int src_idx;
323 int dst_idx;
324 int new_cur_fline = 0;
325 uint32_t lineno;
326 char linebuf[w + 1];
327 const char **old_flines = flines;
328 const char *s;
329 char **new_flines = NULL;
330 char *d;
331
332 if (option_mask32 & FLAG_N)
333 w -= 8;
334
335 src_idx = 0;
336 dst_idx = 0;
337 s = old_flines[0];
338 lineno = LINENO(s);
339 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000340 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000341 while (1) {
342 *d = *s;
343 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000344 new_line_pos++;
Ron Yorston78cfa002015-07-19 21:41:09 +0100345 if (*d == '\t') { /* tab */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000346 new_line_pos += 7;
Ron Yorston78cfa002015-07-19 21:41:09 +0100347 new_line_pos &= (~7);
348 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000349 s++;
350 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000351 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000352 int sz;
353 /* new line is full, create next one */
354 *d = '\0';
355 next_new:
356 sz = (d - linebuf) + 1; /* + 1: NUL */
357 d = ((char*)xmalloc(sz + 4)) + 4;
358 LINENO(d) = lineno;
359 memcpy(d, linebuf, sz);
360 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
361 new_flines[dst_idx] = d;
362 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000363 if (new_line_pos < w) {
364 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000365 if (src_idx > max_fline)
366 break;
367 lineno = LINENO(s);
368 }
369 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000371 }
372 continue;
373 }
374 /* *d == NUL: old line ended, go to next old one */
375 free(MEMPTR(old_flines[src_idx]));
376 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000377 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000378 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000379 src_idx++;
380 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000381 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000382 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000383 s = old_flines[src_idx];
384 if (lineno != LINENO(s)) {
385 /* this is not a continuation line!
386 * create next _new_ line too */
387 goto next_new;
388 }
389 }
390
391 free(old_flines);
392 flines = (const char **)new_flines;
393
394 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000395 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000396 cur_fline = new_cur_fline;
397 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000398#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000399 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000400#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000401}
402#endif
403
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000404#if ENABLE_FEATURE_LESS_REGEXP
405static void fill_match_lines(unsigned pos);
406#else
407#define fill_match_lines(pos) ((void)0)
408#endif
409
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100410static int at_end(void)
411{
412 return (option_mask32 & FLAG_S)
413 ? !(cur_fline <= max_fline &&
414 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
415 : !(max_fline > cur_fline + max_displayed_line);
416}
417
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000418/* Devilishly complex routine.
419 *
420 * Has to deal with EOF and EPIPE on input,
421 * with line wrapping, with last line not ending in '\n'
422 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000423 * It reads input again if last time we got an EOF (thus supporting
424 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000425 *
426 * Variables used:
427 * flines[] - array of lines already read. Linewrap may cause
428 * one source file line to occupy several flines[n].
429 * flines[max_fline] - last line, possibly incomplete.
430 * terminated - 1 if flines[max_fline] is 'terminated'
431 * (if there was '\n' [which isn't stored itself, we just remember
432 * that it was seen])
433 * max_lineno - last line's number, this one doesn't increment
434 * on line wrap, only on "real" new lines.
435 * readbuf[0..readeof-1] - small preliminary buffer.
436 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000437 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000438 * (takes into account tabs and backspaces)
439 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200440 *
441 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
442 * "/search on very long input" and "reaching max line count" corner cases.
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000443 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000444static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000445{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000446 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000447 int w = width;
448 char last_terminated = terminated;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200449 time_t last_time = 0;
450 int retry_EAGAIN = 2;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000451#if ENABLE_FEATURE_LESS_REGEXP
452 unsigned old_max_fline = max_fline;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000453#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000454
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200455#define readbuf bb_common_bufsiz1
456 setup_common_bufsiz();
457
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200458 /* (careful: max_fline can be -1) */
459 if (max_fline + 1 > MAXLINES)
460 return;
461
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000462 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000463 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000464
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100465 p = current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000466 if (!last_terminated) {
467 const char *cp = flines[max_fline];
Denys Vlasenko865814a2014-09-22 21:17:24 +0200468 p = stpcpy(p, cp);
469 free(MEMPTR(cp));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000470 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000471 } else {
Denys Vlasenko865814a2014-09-22 21:17:24 +0200472 max_fline++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000473 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000474 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000475
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000476 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000477 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000478 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000479 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000480 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000481 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000482 if (readpos >= readeof) {
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200483 int flags = ndelay_on(0);
484
485 while (1) {
486 time_t t;
487
488 errno = 0;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200489 eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200490 if (errno != EAGAIN)
491 break;
492 t = time(NULL);
493 if (t != last_time) {
494 last_time = t;
495 if (--retry_EAGAIN < 0)
496 break;
497 }
498 sched_yield();
499 }
500 fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000501 readpos = 0;
502 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000503 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000504 goto reached_eof;
Denys Vlasenkod6e76722014-09-22 21:14:02 +0200505 retry_EAGAIN = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000506 }
507 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000508 /* backspace? [needed for manpages] */
509 /* <tab><bs> is (a) insane and */
510 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000511 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000512 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000513 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000514 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000515 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000516 continue;
517 }
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100518 {
519 size_t new_last_line_pos = last_line_pos + 1;
520 if (c == '\t') {
521 new_last_line_pos += 7;
522 new_last_line_pos &= (~7);
523 }
524 if ((int)new_last_line_pos > w)
525 break;
526 last_line_pos = new_last_line_pos;
527 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000528 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000529 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000530 if (c == '\n') {
531 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000532 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000533 break;
534 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000535 /* NUL is substituted by '\n'! */
536 if (c == '\0') c = '\n';
537 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000538 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000539 } /* end of "read chars until we have a line" loop */
Denys Vlasenko15943c82014-09-21 22:10:55 +0200540#if 0
541//BUG: also triggers on this:
542// { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
543// (resulting in lost empty line between "foo" and "bar" lines)
544// the "terminated" logic needs fixing (or explaining)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000545 /* Corner case: linewrap with only "" wrapping to next line */
546 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000547 if (!last_terminated && !current_line[0]) {
548 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000549 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000550 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000551 }
Denys Vlasenko15943c82014-09-21 22:10:55 +0200552#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000553 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000554 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000555 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000556
557 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
558 LINENO(flines[max_fline]) = max_lineno;
559 if (terminated)
560 max_lineno++;
561
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000562 if (max_fline >= MAXLINES) {
563 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000564 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000565 }
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100566 if (!at_end()) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000567#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000568 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000569#else
570 if (wanted_match >= num_matches) { /* goto_match called us */
571 fill_match_lines(old_max_fline);
572 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000573 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000574 if (wanted_match < num_matches)
575 break;
576#endif
577 }
578 if (eof_error <= 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000579 break;
580 }
581 max_fline++;
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100582 current_line = ((char*)xmalloc(w + 5)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000583 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000584 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000585 } /* end of "read lines until we reach cur_fline" loop */
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200586
587 if (eof_error < 0) {
588 if (errno == EAGAIN) {
589 eof_error = 1;
590 } else {
591 print_statusline(bb_msg_read_error);
592 }
593 }
Ron Yorston193ba402015-07-21 22:28:09 +0200594#if ENABLE_FEATURE_LESS_FLAGS
595 else if (eof_error == 0)
596 num_lines = max_lineno;
597#endif
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200598
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000599 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000600#if ENABLE_FEATURE_LESS_REGEXP
601 /* prevent us from being stuck in search for a match */
602 wanted_match = -1;
603#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000604#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000605}
606
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000607#if ENABLE_FEATURE_LESS_FLAGS
Ron Yorston193ba402015-07-21 22:28:09 +0200608static int safe_lineno(int fline)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000609{
Ron Yorston193ba402015-07-21 22:28:09 +0200610 if (fline >= max_fline)
611 fline = max_fline - 1;
612
613 /* also catches empty file (max_fline == 0) */
614 if (fline < 0)
615 return 0;
616
617 return LINENO(flines[fline]) + 1;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000618}
619
Ron Yorston159e0322015-07-24 14:27:42 +0100620/* count number of lines in file */
621static void update_num_lines(void)
622{
623 int count, fd;
Ron Yorstonb27cf312015-07-31 17:33:24 +0100624 struct stat stbuf;
Ron Yorston159e0322015-07-24 14:27:42 +0100625 ssize_t len, i;
626 char buf[4096];
Ron Yorston159e0322015-07-24 14:27:42 +0100627
Ron Yorston70b84be2015-07-24 14:28:08 +0100628 /* only do this for regular files */
Ron Yorstonb27cf312015-07-31 17:33:24 +0100629 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
Ron Yorston159e0322015-07-24 14:27:42 +0100630 count = 0;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200631 fd = open("/proc/self/fd/0", O_RDONLY);
632 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
633 /* "filename" is valid only if REOPEN_AND_COUNT */
634 fd = open(filename, O_RDONLY);
635 }
Ron Yorston70b84be2015-07-24 14:28:08 +0100636 if (fd < 0) {
637 /* somebody stole my file! */
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200638 num_lines = NOT_REGULAR_FILE;
Ron Yorston70b84be2015-07-24 14:28:08 +0100639 return;
640 }
Ron Yorstonb27cf312015-07-31 17:33:24 +0100641 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
642 num_lines = NOT_REGULAR_FILE;
643 goto do_close;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200644 }
Ron Yorston159e0322015-07-24 14:27:42 +0100645 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
646 for (i = 0; i < len; ++i) {
647 if (buf[i] == '\n' && ++count == MAXLINES)
648 goto done;
649 }
650 }
651 done:
652 num_lines = count;
Denys Vlasenko9dc526d2015-07-31 16:42:20 +0200653 do_close:
Ron Yorston159e0322015-07-24 14:27:42 +0100654 close(fd);
Ron Yorston159e0322015-07-24 14:27:42 +0100655 }
656}
657
Rob Landley9200e792005-09-15 19:26:59 +0000658/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000659static void m_status_print(void)
660{
Ron Yorston193ba402015-07-21 22:28:09 +0200661 int first, last;
662 unsigned percent;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000663
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200664 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000665 return;
666
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000667 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000668 printf(HIGHLIGHT"%s", filename);
669 if (num_files > 1)
670 printf(" (file %i of %i)", current_file, num_files);
Ron Yorston193ba402015-07-21 22:28:09 +0200671
672 first = safe_lineno(cur_fline);
673 last = (option_mask32 & FLAG_S)
674 ? MIN(first + max_displayed_line, max_lineno)
675 : safe_lineno(cur_fline + max_displayed_line);
676 printf(" lines %i-%i", first, last);
677
Ron Yorston159e0322015-07-24 14:27:42 +0100678 update_num_lines();
Ron Yorston193ba402015-07-21 22:28:09 +0200679 if (num_lines >= 0)
680 printf("/%i", num_lines);
681
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100682 if (at_end()) {
Ron Yorston193ba402015-07-21 22:28:09 +0200683 printf(" (END)");
684 if (num_files > 1 && current_file != num_files)
685 printf(" - next: %s", files[current_file]);
686 } else if (num_lines > 0) {
687 percent = (100 * last + num_lines/2) / num_lines;
688 printf(" %i%%", percent <= 100 ? percent : 100);
689 }
690 printf(NORMAL);
Rob Landley9200e792005-09-15 19:26:59 +0000691}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000692#endif
693
Rob Landley9200e792005-09-15 19:26:59 +0000694/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000695static void status_print(void)
696{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000697 const char *p;
698
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200699 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000700 return;
701
Rob Landley9200e792005-09-15 19:26:59 +0000702 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000703#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000704 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000705 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000706 return;
Rob Landley9200e792005-09-15 19:26:59 +0000707 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000708 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000709#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000710
711 clear_line();
Ron Yorstonad1b4d52015-07-24 14:28:50 +0100712 if (cur_fline && !at_end()) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000713 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000714 return;
715 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000716 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000717 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000718 p = filename;
719 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000720 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000721 p, current_file, num_files);
722 return;
723 }
724 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000725}
726
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000727static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000728 /* NUL: never encountered; TAB: not converted */
729 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000730 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
731 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000732static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100733 /* why 40 instead of 4a below? - it is a replacement for '\n'.
734 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000735 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
736 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
737
Ron Yorstonf06386a2015-07-18 16:20:03 +0100738static void print_lineno(const char *line)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000739{
Ron Yorstonf06386a2015-07-18 16:20:03 +0100740 const char *fmt = " ";
741 unsigned n = n; /* for compiler */
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000742
Ron Yorstonf06386a2015-07-18 16:20:03 +0100743 if (line != empty_line_marker) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000744 /* Width of 7 preserves tab spacing in the text */
745 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000746 n = LINENO(line) + 1;
Ron Yorstonf06386a2015-07-18 16:20:03 +0100747 if (n > 9999999 && MAXLINES > 9999999) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000748 n %= 10000000;
749 fmt = "%07u ";
750 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000751 }
Ron Yorstonf06386a2015-07-18 16:20:03 +0100752 printf(fmt, n);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000753}
754
755
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000756#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000757static void print_found(const char *line)
758{
759 int match_status;
760 int eflags;
761 char *growline;
762 regmatch_t match_structs;
763
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100764 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000765 const char *str = line;
766 char *p = buf;
767 size_t n;
768
769 while (*str) {
770 n = strcspn(str, controls);
771 if (n) {
772 if (!str[n]) break;
773 memcpy(p, str, n);
774 p += n;
775 str += n;
776 }
777 n = strspn(str, controls);
778 memset(p, '.', n);
779 p += n;
780 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000781 }
782 strcpy(p, str);
783
784 /* buf[] holds quarantined version of str */
785
786 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100787 * and NORMAL escape sequences placed around it.
788 * NB: we regex against line, but insert text
789 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000790 str = buf;
791 growline = NULL;
792 eflags = 0;
793 goto start;
794
795 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000796 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200797 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200798 (int)match_structs.rm_so, str,
799 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000800 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000801 free(growline);
802 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000803 str += match_structs.rm_eo;
804 line += match_structs.rm_eo;
805 eflags = REG_NOTBOL;
806 start:
807 /* Most of the time doesn't find the regex, optimize for that */
808 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000809 /* if even "" matches, treat it as "not a match" */
810 if (match_structs.rm_so >= match_structs.rm_eo)
811 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000812 }
813
Ron Yorstonf06386a2015-07-18 16:20:03 +0100814 printf("%s%s\n", growline ? growline : "", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000815 free(growline);
816}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000817#else
818void print_found(const char *line);
819#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000820
821static void print_ascii(const char *str)
822{
Ron Yorston26ccd3d2015-08-04 17:10:37 +0100823 char buf[width+1];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000824 char *p;
825 size_t n;
826
827 while (*str) {
828 n = strcspn(str, controls);
829 if (n) {
830 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000831 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000832 str += n;
833 }
834 n = strspn(str, controls);
835 p = buf;
836 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000837 if (*str == 0x7f)
838 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000839 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000840 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
841 /* I want to know who committed this sin */
842 *p++ = '{';
843 else
844 *p++ = ctrlconv[(unsigned char)*str];
845 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000846 } while (--n);
847 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000848 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000849 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000850 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000851}
852
Rob Landley9200e792005-09-15 19:26:59 +0000853/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000854static void buffer_print(void)
855{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000856 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000857
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000858 move_cursor(0, 0);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200859 for (i = 0; i <= max_displayed_line; i++) {
Ron Yorstonf06386a2015-07-18 16:20:03 +0100860 printf(CLEAR_2_EOL);
861 if (option_mask32 & FLAG_N)
862 print_lineno(buffer[i]);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000863 if (pattern_valid)
864 print_found(buffer[i]);
865 else
866 print_ascii(buffer[i]);
Aaro Koskinen307d26c2014-09-23 22:58:18 +0200867 }
868 if ((option_mask32 & FLAG_E)
869 && eof_error <= 0
870 && (max_fline - cur_fline) <= max_displayed_line
871 ) {
872 less_exit(EXIT_SUCCESS);
873 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000874 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000875}
876
Denis Vlasenkod9083952008-10-24 10:42:21 +0000877static void buffer_fill_and_print(void)
878{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000879 unsigned i;
Ron Yorston51aa8612015-07-19 11:12:29 +0100880#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000881 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000882
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000883 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000884 /* Go back to the beginning of this line */
885 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
886 fpos--;
887 }
888
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000889 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000890 while (i <= max_displayed_line && fpos <= max_fline) {
891 int lineno = LINENO(flines[fpos]);
892 buffer[i] = flines[fpos];
893 i++;
894 do {
895 fpos++;
896 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000897 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000898 && lineno == LINENO(flines[fpos])
899 );
900 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000901#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000902 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
903 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000904 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000905#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000906 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000907 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000908 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000909 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000910}
911
Ron Yorstond542d182015-07-24 14:29:13 +0100912/* move cur_fline to a given line number, reading lines if necessary */
913static void goto_lineno(int target)
914{
915 if (target <= 0 ) {
916 cur_fline = 0;
917 }
918 else if (target > LINENO(flines[cur_fline])) {
919 retry:
920 while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline)
921 ++cur_fline;
922 /* target not reached but more input is available */
923 if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
924 read_lines();
925 goto retry;
926 }
927 }
928 else {
929 /* search backwards through already-read lines */
930 while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
931 --cur_fline;
932 }
933}
934
935static void cap_cur_fline(void)
936{
937 if ((option_mask32 & FLAG_S)) {
938 if (cur_fline > max_fline)
939 cur_fline = max_fline;
940 if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) {
941 goto_lineno(max_lineno - max_displayed_line + TILDES);
942 read_lines();
943 }
944 }
945 else {
946 if (cur_fline + max_displayed_line > max_fline + TILDES)
947 cur_fline = max_fline - max_displayed_line + TILDES;
948 if (cur_fline < 0)
949 cur_fline = 0;
950 }
951}
952
Rob Landley9200e792005-09-15 19:26:59 +0000953/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000954static void buffer_down(int nlines)
955{
Ron Yorstond542d182015-07-24 14:29:13 +0100956 if ((option_mask32 & FLAG_S))
957 goto_lineno(LINENO(flines[cur_fline]) + nlines);
958 else
959 cur_fline += nlines;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000960 read_lines();
Ron Yorstond542d182015-07-24 14:29:13 +0100961 cap_cur_fline();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000962 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000963}
964
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000965static void buffer_up(int nlines)
966{
Ron Yorstond542d182015-07-24 14:29:13 +0100967 if ((option_mask32 & FLAG_S)) {
968 goto_lineno(LINENO(flines[cur_fline]) - nlines);
969 }
970 else {
971 cur_fline -= nlines;
972 if (cur_fline < 0)
973 cur_fline = 0;
974 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000975 read_lines();
976 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000977}
978
Ron Yorstond542d182015-07-24 14:29:13 +0100979/* display a given line where the argument can be either an index into
980 * the flines array or a line number */
981static void buffer_to_line(int linenum, int is_lineno)
982{
983 if (linenum <= 0)
984 cur_fline = 0;
985 else if (is_lineno)
986 goto_lineno(linenum);
987 else
988 cur_fline = linenum;
989 read_lines();
990 cap_cur_fline();
991 buffer_fill_and_print();
992}
993
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000994static void buffer_line(int linenum)
995{
Ron Yorstond542d182015-07-24 14:29:13 +0100996 buffer_to_line(linenum, FALSE);
997}
998
999static void buffer_lineno(int lineno)
1000{
1001 buffer_to_line(lineno, TRUE);
Rob Landley9200e792005-09-15 19:26:59 +00001002}
1003
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001004static void open_file_and_read_lines(void)
1005{
1006 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +00001007 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001008#if ENABLE_FEATURE_LESS_FLAGS
1009 num_lines = REOPEN_AND_COUNT;
1010#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001011 } else {
1012 /* "less" with no arguments in argv[] */
1013 /* For status line only */
1014 filename = xstrdup(bb_msg_standard_input);
Denys Vlasenko9dc526d2015-07-31 16:42:20 +02001015#if ENABLE_FEATURE_LESS_FLAGS
1016 num_lines = REOPEN_STDIN;
1017#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001018 }
1019 readpos = 0;
1020 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +00001021 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001022 terminated = 1;
1023 read_lines();
1024}
1025
1026/* Reinitialize everything for a new file - free the memory and start over */
1027static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001028{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001029 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001030
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001031 if (flines) {
1032 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +00001033 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001034 free(flines);
1035 flines = NULL;
1036 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001037
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001038 max_fline = -1;
1039 cur_fline = 0;
1040 max_lineno = 0;
1041 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001042#if ENABLE_FEATURE_LESS_ASK_TERMINAL
1043 if (G.winsize_err)
1044 printf("\033[999;999H" "\033[6n");
1045#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001046 buffer_fill_and_print();
1047}
1048
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001049static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001050{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001051 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001052 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +00001053 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001054
Denis Vlasenko33196372008-02-23 01:25:38 +00001055 pfd[0].fd = STDIN_FILENO;
1056 pfd[0].events = POLLIN;
1057 pfd[1].fd = kbd_fd;
1058 pfd[1].events = POLLIN;
1059 again:
1060 tcsetattr(kbd_fd, TCSANOW, &term_less);
1061 /* NB: select/poll returns whenever read will not block. Therefore:
1062 * if eof is reached, select/poll will return immediately
1063 * because read will immediately return 0 bytes.
1064 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001065 * (switch fd into O_NONBLOCK'ed mode to avoid it)
1066 */
Denis Vlasenko33196372008-02-23 01:25:38 +00001067 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001068 /* Are we interested in stdin? */
Ron Yorstonad1b4d52015-07-24 14:28:50 +01001069 if (at_end()) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001070 if (eof_error > 0) /* did NOT reach eof yet */
1071 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001072 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001073 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +00001074 if (less_gets_pos >= 0)
1075 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001076 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001077
Denys Vlasenko020f4062009-05-17 16:44:54 +02001078 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001079#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001080 while (1) {
1081 int r;
1082 /* NB: SIGWINCH interrupts poll() */
1083 r = poll(pfd + rd, 2 - rd, -1);
1084 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
1085 return '\\'; /* anything which has no defined function */
1086 if (r) break;
1087 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001088#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001089 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001090#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +00001091 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001092
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001093 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
1094 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001095 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
1096 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +00001097 if (errno == EAGAIN) {
1098 /* No keyboard input available. Since poll() did return,
1099 * we should have input on stdin */
1100 read_lines();
1101 buffer_fill_and_print();
1102 goto again;
1103 }
1104 /* EOF/error (ssh session got killed etc) */
1105 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001106 }
Denis Vlasenko33196372008-02-23 01:25:38 +00001107 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001108 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001109}
1110
Denys Vlasenko020f4062009-05-17 16:44:54 +02001111/* Grab a character from input without requiring the return key.
1112 * May return KEYCODE_xxx values.
1113 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001114static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001115{
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001116 int64_t key64;
1117 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001118
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001119 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001120 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001121 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001122 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +00001123
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001124 /* Discard Ctrl-something chars.
1125 * (checking only lower 32 bits is a size optimization:
1126 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1127 */
1128 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +00001129 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +02001130
1131 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001132}
1133
1134static char* less_gets(int sz)
1135{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001136 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001137 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001138 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001139
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001140 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001141 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001142 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001143 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001144 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001145 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001146 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001147 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001148 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001149 if (c == 0x7f)
1150 c = 8;
1151 if (c == 8 && i) {
1152 printf("\x8 \x8");
1153 i--;
1154 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001155 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001156 continue;
1157 if (i >= width - sz - 1)
1158 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001159 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001160 result[i++] = c;
1161 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001162 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001163}
1164
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001165static void examine_file(void)
1166{
Denis Vlasenko33196372008-02-23 01:25:38 +00001167 char *new_fname;
1168
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001169 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001170 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001171 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001172 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001173 err:
1174 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001175 return;
1176 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001177 if (access(new_fname, R_OK) != 0) {
1178 print_statusline("Cannot read this file");
1179 goto err;
1180 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001181 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001182 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001183 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001184 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001185 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001186 num_files++; */
1187 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001188 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001189 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001190}
1191
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001192/* This function changes the file currently being paged. direction can be one of the following:
1193 * -1: go back one file
1194 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001195 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001196static void change_file(int direction)
1197{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001198 if (current_file != ((direction > 0) ? num_files : 1)) {
1199 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001200 free(filename);
1201 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001202 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001203 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001204 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001205 }
1206}
1207
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001208static void remove_current_file(void)
1209{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001210 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001211
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001212 if (num_files < 2)
1213 return;
1214
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001215 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001216 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001217 for (i = 3; i <= num_files; i++)
1218 files[i - 2] = files[i - 1];
1219 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001220 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001221 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001222 for (i = 2; i <= num_files; i++)
1223 files[i - 2] = files[i - 1];
1224 num_files--;
1225 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001226 }
1227}
1228
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001229static void colon_process(void)
1230{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001231 int keypress;
1232
1233 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001234 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001235
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001236 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001237 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001238 case 'd':
1239 remove_current_file();
1240 break;
1241 case 'e':
1242 examine_file();
1243 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001244#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001245 case 'f':
1246 m_status_print();
1247 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001248#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001249 case 'n':
1250 change_file(1);
1251 break;
1252 case 'p':
1253 change_file(-1);
1254 break;
1255 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001256 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001257 break;
1258 case 'x':
1259 change_file(0);
1260 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001261 }
1262}
1263
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001264#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001265static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001266{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001267 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001268 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001269 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001270 match = 0;
1271 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001272}
1273
Rob Landleya2e98042006-04-18 01:53:41 +00001274static void goto_match(int match)
1275{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001276 if (!pattern_valid)
1277 return;
1278 if (match < 0)
1279 match = 0;
1280 /* Try to find next match if eof isn't reached yet */
1281 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001282 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001283 read_lines();
1284 }
1285 if (num_matches) {
1286 normalize_match_pos(match);
1287 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001288 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001289 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001290 }
Rob Landleya2e98042006-04-18 01:53:41 +00001291}
1292
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001293static void fill_match_lines(unsigned pos)
1294{
1295 if (!pattern_valid)
1296 return;
1297 /* Run the regex on each line of the current file */
1298 while (pos <= max_fline) {
1299 /* If this line matches */
1300 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1301 /* and we didn't match it last time */
1302 && !(num_matches && match_lines[num_matches-1] == pos)
1303 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001304 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001305 match_lines[num_matches++] = pos;
1306 }
1307 pos++;
1308 }
1309}
1310
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001311static void regex_process(void)
1312{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001313 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001314
1315 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001316 free(match_lines);
1317 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001318 match_pos = 0;
1319 num_matches = 0;
1320 if (pattern_valid) {
1321 regfree(&pattern);
1322 pattern_valid = 0;
1323 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001324
Rob Landleya2e98042006-04-18 01:53:41 +00001325 /* Get the uncompiled regular expression from the user */
1326 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001327 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001328 uncomp_regex = less_gets(1);
1329 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001330 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001331 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001332 return;
1333 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001334
Rob Landleya2e98042006-04-18 01:53:41 +00001335 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001336 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001337 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001338 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001339 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001340 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001341 free(err);
1342 return;
1343 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001344
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001345 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001346 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001347 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001348 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001349 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001350 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001351 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001352 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001353 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1354 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001355
1356 /* It's possible that no matches are found yet.
1357 * goto_match() will read input looking for match,
1358 * if needed */
1359 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001360}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001361#endif
1362
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001363static void number_process(int first_digit)
1364{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001365 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001366 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001367 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001368 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001369
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001370 num_input[0] = first_digit;
1371
1372 /* Clear the current line, print a prompt, and then print the digit */
1373 clear_line();
1374 printf(":%c", first_digit);
1375
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001376 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001377 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001378 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001379 keypress = less_getch(i + 1);
Ron Yorstonae1a9e82015-07-21 20:12:31 +01001380 if ((unsigned)keypress > 255 || !isdigit(keypress))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001381 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001382 num_input[i] = keypress;
1383 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001384 i++;
1385 }
1386
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001387 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001388 num = bb_strtou(num_input, NULL, 10);
1389 /* on format error, num == -1 */
1390 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001391 buffer_print();
1392 return;
1393 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001394
1395 /* We now know the number and the letter entered, so we process them */
1396 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001397 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001398 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001399 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001400 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001401 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001402 break;
1403 case 'g': case '<': case 'G': case '>':
Ron Yorstond542d182015-07-24 14:29:13 +01001404 buffer_lineno(num - 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001405 break;
1406 case 'p': case '%':
Ron Yorstond542d182015-07-24 14:29:13 +01001407#if ENABLE_FEATURE_LESS_FLAGS
1408 update_num_lines();
1409 num = num * (num_lines > 0 ? num_lines : max_lineno) / 100;
1410#else
1411 num = num * max_lineno / 100;
1412#endif
1413 buffer_lineno(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001414 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001415#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001416 case 'n':
1417 goto_match(match_pos + num);
1418 break;
1419 case '/':
1420 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1421 regex_process();
1422 break;
1423 case '?':
1424 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1425 regex_process();
1426 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001427#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001428 }
1429}
1430
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001431#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001432static void flag_change(void)
1433{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001434 int keypress;
1435
1436 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001437 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001438 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001439
1440 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001441 case 'M':
1442 option_mask32 ^= FLAG_M;
1443 break;
1444 case 'm':
1445 option_mask32 ^= FLAG_m;
1446 break;
1447 case 'E':
1448 option_mask32 ^= FLAG_E;
1449 break;
1450 case '~':
1451 option_mask32 ^= FLAG_TILDE;
1452 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001453#if ENABLE_FEATURE_LESS_TRUNCATE
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001454 case 'S':
1455 option_mask32 ^= FLAG_S;
1456 buffer_fill_and_print();
1457 break;
Ron Yorston51aa8612015-07-19 11:12:29 +01001458#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001459#if ENABLE_FEATURE_LESS_LINENUMS
1460 case 'N':
1461 option_mask32 ^= FLAG_N;
1462 re_wrap();
1463 buffer_fill_and_print();
1464 break;
1465#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001466 }
1467}
1468
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001469#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001470static void show_flag_status(void)
1471{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001472 int keypress;
1473 int flag_val;
1474
1475 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001476 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001477 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001478
1479 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001480 case 'M':
1481 flag_val = option_mask32 & FLAG_M;
1482 break;
1483 case 'm':
1484 flag_val = option_mask32 & FLAG_m;
1485 break;
1486 case '~':
1487 flag_val = option_mask32 & FLAG_TILDE;
1488 break;
1489 case 'N':
1490 flag_val = option_mask32 & FLAG_N;
1491 break;
1492 case 'E':
1493 flag_val = option_mask32 & FLAG_E;
1494 break;
1495 default:
1496 flag_val = 0;
1497 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001498 }
1499
1500 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001501 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001502}
1503#endif
1504
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001505#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1506
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001507static void save_input_to_file(void)
1508{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001509 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001510 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001511 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001512 FILE *fp;
1513
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001514 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001515 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001516 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001517 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001518 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001519 msg = "Error opening log file";
1520 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001521 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001522 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001523 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001524 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001525 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001526 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001527 ret:
1528 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001529 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001530}
1531
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001532#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001533static void add_mark(void)
1534{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001535 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001536
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001537 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001538 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001539
1540 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001541 /* If we exceed 15 marks, start overwriting previous ones */
1542 if (num_marks == 14)
1543 num_marks = 0;
1544
1545 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001546 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001547 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001548 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001549 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001550 }
1551}
1552
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001553static void goto_mark(void)
1554{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001555 int letter;
1556 int i;
1557
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001558 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001559 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001560 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001561
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001562 if (isalpha(letter)) {
1563 for (i = 0; i <= num_marks; i++)
1564 if (letter == mark_lines[i][0]) {
1565 buffer_line(mark_lines[i][1]);
1566 break;
1567 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001568 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001569 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001570 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001571 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001572}
1573#endif
1574
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001575#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001576static char opp_bracket(char bracket)
1577{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001578 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001579 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1580 bracket++;
1581 case '(': /* ')' == '(' + 1 */
1582 bracket++;
1583 break;
1584 case '}': case ']':
1585 bracket--;
1586 case ')':
1587 bracket--;
1588 break;
1589 };
1590 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001591}
1592
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001593static void match_right_bracket(char bracket)
1594{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001595 unsigned i = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001596
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001597 if (i >= max_fline
1598 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001599 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001600 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001601 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001602 }
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001603
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001604 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001605 for (; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001606 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001607 /*
1608 * Line with matched right bracket becomes
1609 * last visible line
1610 */
1611 buffer_line(i - max_displayed_line);
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001612 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001613 }
1614 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001615 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001616}
1617
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001618static void match_left_bracket(char bracket)
1619{
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001620 int i = cur_fline + max_displayed_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001621
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001622 if (i >= max_fline
1623 || strchr(flines[i], bracket) == NULL
Denys Vlasenko25255962016-08-16 15:09:40 +02001624 ) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001625 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001626 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001627 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001628
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001629 bracket = opp_bracket(bracket);
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001630 for (; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001631 if (strchr(flines[i], bracket) != NULL) {
Denys Vlasenko110c6bb2016-08-16 15:27:30 +02001632 /*
1633 * Line with matched left bracket becomes
1634 * first visible line
1635 */
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001636 buffer_line(i);
1637 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001638 }
1639 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001640 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001641}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001642#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001643
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001644static void keypress_process(int keypress)
1645{
Rob Landley9200e792005-09-15 19:26:59 +00001646 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001647 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001648 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001649 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001650 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001651 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001652 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001653 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001654 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001655 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001656 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001657 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001658 break;
1659 case 'd':
1660 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001661 break;
1662 case 'u':
1663 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001664 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001665 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001666 buffer_line(0);
1667 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001668 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001669 cur_fline = MAXLINES;
1670 read_lines();
1671 buffer_line(cur_fline);
1672 break;
1673 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001674 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001675 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001676#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001677 case 'm':
1678 add_mark();
1679 buffer_print();
1680 break;
1681 case '\'':
1682 goto_mark();
1683 buffer_print();
1684 break;
Rob Landley9200e792005-09-15 19:26:59 +00001685#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001686 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001687 /* TODO: (1) also bind ^R, ^L to this?
1688 * (2) re-measure window size?
1689 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001690 buffer_print();
1691 break;
1692 /*case 'R':
1693 full_repaint();
1694 break;*/
1695 case 's':
1696 save_input_to_file();
1697 break;
1698 case 'E':
1699 examine_file();
1700 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001701#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001702 case '=':
1703 m_status_print();
1704 break;
Rob Landley9200e792005-09-15 19:26:59 +00001705#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001706#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001707 case '/':
1708 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1709 regex_process();
1710 break;
1711 case 'n':
1712 goto_match(match_pos + 1);
1713 break;
1714 case 'N':
1715 goto_match(match_pos - 1);
1716 break;
1717 case '?':
1718 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1719 regex_process();
1720 break;
Rob Landley9200e792005-09-15 19:26:59 +00001721#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001722#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001723 case '-':
1724 flag_change();
1725 buffer_print();
1726 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001727#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001728 case '_':
1729 show_flag_status();
1730 break;
Rob Landley9200e792005-09-15 19:26:59 +00001731#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001732#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001733#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001734 case '{': case '(': case '[':
1735 match_right_bracket(keypress);
1736 break;
1737 case '}': case ')': case ']':
1738 match_left_bracket(keypress);
1739 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001740#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001741 case ':':
1742 colon_process();
1743 break;
Rob Landley9200e792005-09-15 19:26:59 +00001744 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001745
Rob Landley9200e792005-09-15 19:26:59 +00001746 if (isdigit(keypress))
1747 number_process(keypress);
1748}
1749
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001750static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001751{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001752 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001753}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001754
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001755#if ENABLE_FEATURE_LESS_WINCH
1756static void sigwinch_handler(int sig UNUSED_PARAM)
1757{
1758 winch_counter++;
1759}
1760#endif
1761
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001762int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001763int less_main(int argc, char **argv)
1764{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001765 char *tty_name;
1766 int tty_fd;
1767
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001768 INIT_G();
1769
Denys Vlasenko821e6432014-01-22 16:36:22 +01001770 /* TODO: -x: do not interpret backspace, -xx: tab also
1771 * -xxx: newline also
1772 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1773 * -s: condense many empty lines to one
1774 * (used by some setups for manpage display)
1775 */
Ron Yorston51aa8612015-07-19 11:12:29 +01001776 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s");
Rob Landley9200e792005-09-15 19:26:59 +00001777 argc -= optind;
1778 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001779 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001780 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001781
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001782 /* Another popular pager, most, detects when stdout
1783 * is not a tty and turns into cat. This makes sense. */
1784 if (!isatty(STDOUT_FILENO))
1785 return bb_cat(argv);
1786
Rob Landley9200e792005-09-15 19:26:59 +00001787 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001788 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001789 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001790 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001791 bb_show_usage();
1792 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001793 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001794 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001795 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001796
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001797 if (option_mask32 & FLAG_TILDE)
1798 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001799
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001800 /* Some versions of less can survive w/o controlling tty,
1801 * try to do the same. This also allows to specify an alternative
1802 * tty via "less 1<>TTY".
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001803 *
1804 * We prefer not to use STDOUT_FILENO directly,
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001805 * since we want to set this fd to non-blocking mode,
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001806 * and not interfere with other processes which share stdout with us.
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001807 */
1808 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1809 if (tty_name) {
1810 tty_fd = open(tty_name, O_RDONLY);
1811 free(tty_name);
1812 if (tty_fd < 0)
1813 goto try_ctty;
1814 } else {
1815 /* Try controlling tty */
1816 try_ctty:
1817 tty_fd = open(CURRENT_TTY, O_RDONLY);
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001818 if (tty_fd < 0) {
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001819 /* If all else fails, less 481 uses stdout. Mimic that */
Denys Vlasenko3c0e5792016-09-13 20:53:38 +02001820 tty_fd = STDOUT_FILENO;
1821 }
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001822 }
Denys Vlasenko7fa799a2016-09-13 21:05:48 +02001823 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001824 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001825
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001826 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001827 term_less = term_orig;
1828 term_less.c_lflag &= ~(ICANON | ECHO);
1829 term_less.c_iflag &= ~(IXON | ICRNL);
1830 /*term_less.c_oflag &= ~ONLCR;*/
1831 term_less.c_cc[VMIN] = 1;
1832 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001833
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001834 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001835 /* 20: two tabstops + 4 */
1836 if (width < 20 || max_displayed_line < 3)
1837 return bb_cat(argv);
1838 max_displayed_line -= 2;
1839
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001840 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001841 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001842#if ENABLE_FEATURE_LESS_WINCH
1843 signal(SIGWINCH, sigwinch_handler);
1844#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001845
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001846 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001847 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001848 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001849 int64_t keypress;
1850
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001851#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001852 while (WINCH_COUNTER) {
1853 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001854 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001855 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1856 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001857 /* 20: two tabstops + 4 */
1858 if (width < 20)
1859 width = 20;
1860 if (max_displayed_line < 3)
1861 max_displayed_line = 3;
1862 max_displayed_line -= 2;
1863 free(buffer);
1864 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001865 /* Avoid re-wrap and/or redraw if we already know
1866 * we need to do it again. These ops are expensive */
1867 if (WINCH_COUNTER)
1868 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001869 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001870 if (WINCH_COUNTER)
1871 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001872 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001873 /* This took some time. Loop back and check,
1874 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001875 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001876 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001877# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1878 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1879 uint32_t rc = (keypress >> 32);
1880 width = (rc & 0x7fff);
1881 max_displayed_line = ((rc >> 16) & 0x7fff);
1882 goto got_size;
1883 }
1884# endif
1885#else
1886 keypress = less_getch(-1); /* -1: do not position cursor */
1887#endif
Rob Landley9200e792005-09-15 19:26:59 +00001888 keypress_process(keypress);
1889 }
1890}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001891
1892/*
1893Help text of less version 418 is below.
1894If you are implementing something, keeping
1895key and/or command line switch compatibility is a good idea:
1896
1897
1898 SUMMARY OF LESS COMMANDS
1899
1900 Commands marked with * may be preceded by a number, N.
1901 Notes in parentheses indicate the behavior if N is given.
1902 h H Display this help.
1903 q :q Q :Q ZZ Exit.
1904 ---------------------------------------------------------------------------
1905 MOVING
1906 e ^E j ^N CR * Forward one line (or N lines).
1907 y ^Y k ^K ^P * Backward one line (or N lines).
1908 f ^F ^V SPACE * Forward one window (or N lines).
1909 b ^B ESC-v * Backward one window (or N lines).
1910 z * Forward one window (and set window to N).
1911 w * Backward one window (and set window to N).
1912 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1913 d ^D * Forward one half-window (and set half-window to N).
1914 u ^U * Backward one half-window (and set half-window to N).
1915 ESC-) RightArrow * Left one half screen width (or N positions).
1916 ESC-( LeftArrow * Right one half screen width (or N positions).
1917 F Forward forever; like "tail -f".
1918 r ^R ^L Repaint screen.
1919 R Repaint screen, discarding buffered input.
1920 ---------------------------------------------------
1921 Default "window" is the screen height.
1922 Default "half-window" is half of the screen height.
1923 ---------------------------------------------------------------------------
1924 SEARCHING
1925 /pattern * Search forward for (N-th) matching line.
1926 ?pattern * Search backward for (N-th) matching line.
1927 n * Repeat previous search (for N-th occurrence).
1928 N * Repeat previous search in reverse direction.
1929 ESC-n * Repeat previous search, spanning files.
1930 ESC-N * Repeat previous search, reverse dir. & spanning files.
1931 ESC-u Undo (toggle) search highlighting.
1932 ---------------------------------------------------
1933 Search patterns may be modified by one or more of:
1934 ^N or ! Search for NON-matching lines.
1935 ^E or * Search multiple files (pass thru END OF FILE).
1936 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1937 ^K Highlight matches, but don't move (KEEP position).
1938 ^R Don't use REGULAR EXPRESSIONS.
1939 ---------------------------------------------------------------------------
1940 JUMPING
1941 g < ESC-< * Go to first line in file (or line N).
1942 G > ESC-> * Go to last line in file (or line N).
1943 p % * Go to beginning of file (or N percent into file).
1944 t * Go to the (N-th) next tag.
1945 T * Go to the (N-th) previous tag.
1946 { ( [ * Find close bracket } ) ].
1947 } ) ] * Find open bracket { ( [.
1948 ESC-^F <c1> <c2> * Find close bracket <c2>.
1949 ESC-^B <c1> <c2> * Find open bracket <c1>
1950 ---------------------------------------------------
1951 Each "find close bracket" command goes forward to the close bracket
1952 matching the (N-th) open bracket in the top line.
1953 Each "find open bracket" command goes backward to the open bracket
1954 matching the (N-th) close bracket in the bottom line.
1955 m<letter> Mark the current position with <letter>.
1956 '<letter> Go to a previously marked position.
1957 '' Go to the previous position.
1958 ^X^X Same as '.
1959 ---------------------------------------------------
1960 A mark is any upper-case or lower-case letter.
1961 Certain marks are predefined:
1962 ^ means beginning of the file
1963 $ means end of the file
1964 ---------------------------------------------------------------------------
1965 CHANGING FILES
1966 :e [file] Examine a new file.
1967 ^X^V Same as :e.
1968 :n * Examine the (N-th) next file from the command line.
1969 :p * Examine the (N-th) previous file from the command line.
1970 :x * Examine the first (or N-th) file from the command line.
1971 :d Delete the current file from the command line list.
1972 = ^G :f Print current file name.
1973 ---------------------------------------------------------------------------
1974 MISCELLANEOUS COMMANDS
1975 -<flag> Toggle a command line option [see OPTIONS below].
1976 --<name> Toggle a command line option, by name.
1977 _<flag> Display the setting of a command line option.
1978 __<name> Display the setting of an option, by name.
1979 +cmd Execute the less cmd each time a new file is examined.
1980 !command Execute the shell command with $SHELL.
1981 |Xcommand Pipe file between current pos & mark X to shell command.
1982 v Edit the current file with $VISUAL or $EDITOR.
1983 V Print version number of "less".
1984 ---------------------------------------------------------------------------
1985 OPTIONS
1986 Most options may be changed either on the command line,
1987 or from within less by using the - or -- command.
1988 Options may be given in one of two forms: either a single
Maninder Singh97c64912015-05-25 13:46:36 +02001989 character preceded by a -, or a name preceded by --.
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001990 -? ........ --help
1991 Display help (from command line).
1992 -a ........ --search-skip-screen
1993 Forward search skips current screen.
1994 -b [N] .... --buffers=[N]
1995 Number of buffers.
1996 -B ........ --auto-buffers
1997 Don't automatically allocate buffers for pipes.
1998 -c ........ --clear-screen
1999 Repaint by clearing rather than scrolling.
2000 -d ........ --dumb
2001 Dumb terminal.
2002 -D [xn.n] . --color=xn.n
2003 Set screen colors. (MS-DOS only)
2004 -e -E .... --quit-at-eof --QUIT-AT-EOF
2005 Quit at end of file.
2006 -f ........ --force
2007 Force open non-regular files.
2008 -F ........ --quit-if-one-screen
2009 Quit if entire file fits on first screen.
2010 -g ........ --hilite-search
2011 Highlight only last match for searches.
2012 -G ........ --HILITE-SEARCH
2013 Don't highlight any matches for searches.
2014 -h [N] .... --max-back-scroll=[N]
2015 Backward scroll limit.
2016 -i ........ --ignore-case
2017 Ignore case in searches that do not contain uppercase.
2018 -I ........ --IGNORE-CASE
2019 Ignore case in all searches.
2020 -j [N] .... --jump-target=[N]
2021 Screen position of target lines.
2022 -J ........ --status-column
2023 Display a status column at left edge of screen.
2024 -k [file] . --lesskey-file=[file]
2025 Use a lesskey file.
2026 -L ........ --no-lessopen
2027 Ignore the LESSOPEN environment variable.
2028 -m -M .... --long-prompt --LONG-PROMPT
2029 Set prompt style.
2030 -n -N .... --line-numbers --LINE-NUMBERS
2031 Don't use line numbers.
2032 -o [file] . --log-file=[file]
2033 Copy to log file (standard input only).
2034 -O [file] . --LOG-FILE=[file]
2035 Copy to log file (unconditionally overwrite).
2036 -p [pattern] --pattern=[pattern]
2037 Start at pattern (from command line).
2038 -P [prompt] --prompt=[prompt]
2039 Define new prompt.
2040 -q -Q .... --quiet --QUIET --silent --SILENT
2041 Quiet the terminal bell.
2042 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
2043 Output "raw" control characters.
2044 -s ........ --squeeze-blank-lines
2045 Squeeze multiple blank lines.
2046 -S ........ --chop-long-lines
2047 Chop long lines.
2048 -t [tag] .. --tag=[tag]
2049 Find a tag.
2050 -T [tagsfile] --tag-file=[tagsfile]
2051 Use an alternate tags file.
2052 -u -U .... --underline-special --UNDERLINE-SPECIAL
2053 Change handling of backspaces.
2054 -V ........ --version
2055 Display the version number of "less".
2056 -w ........ --hilite-unread
2057 Highlight first new line after forward-screen.
2058 -W ........ --HILITE-UNREAD
2059 Highlight first new line after any forward movement.
2060 -x [N[,...]] --tabs=[N[,...]]
2061 Set tab stops.
2062 -X ........ --no-init
2063 Don't use termcap init/deinit strings.
2064 --no-keypad
2065 Don't use termcap keypad init/deinit strings.
2066 -y [N] .... --max-forw-scroll=[N]
2067 Forward scroll limit.
2068 -z [N] .... --window=[N]
2069 Set size of window.
2070 -" [c[c]] . --quotes=[c[c]]
2071 Set shell quote characters.
2072 -~ ........ --tilde
2073 Don't display tildes after end of file.
2074 -# [N] .... --shift=[N]
2075 Horizontal scroll amount (0 = one half screen width)
2076
2077 ---------------------------------------------------------------------------
2078 LINE EDITING
2079 These keys can be used to edit text being entered
2080 on the "command line" at the bottom of the screen.
2081 RightArrow ESC-l Move cursor right one character.
2082 LeftArrow ESC-h Move cursor left one character.
2083 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
2084 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
2085 HOME ESC-0 Move cursor to start of line.
2086 END ESC-$ Move cursor to end of line.
2087 BACKSPACE Delete char to left of cursor.
2088 DELETE ESC-x Delete char under cursor.
2089 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
2090 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
2091 CNTL-U ESC (MS-DOS only) Delete entire line.
2092 UpArrow ESC-k Retrieve previous command line.
2093 DownArrow ESC-j Retrieve next command line.
2094 TAB Complete filename & cycle.
2095 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
2096 CNTL-L Complete filename, list all.
2097*/