blob: 574f222e0f88899dbcc3c9d71e1139bbeaca6678 [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:
51//config:config FEATURE_LESS_MARKS
52//config: bool "Enable marks"
53//config: default y
54//config: depends on LESS
55//config: help
56//config: Marks enable positions in a file to be stored for easy reference.
57//config:
58//config:config FEATURE_LESS_REGEXP
59//config: bool "Enable regular expressions"
60//config: default y
61//config: depends on LESS
62//config: help
63//config: Enable regular expressions, allowing complex file searches.
64//config:
65//config:config FEATURE_LESS_WINCH
66//config: bool "Enable automatic resizing on window size changes"
67//config: default y
68//config: depends on LESS
69//config: help
70//config: Makes less track window size changes.
71//config:
72//config:config FEATURE_LESS_ASK_TERMINAL
73//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
74//config: default y
75//config: depends on FEATURE_LESS_WINCH
76//config: help
77//config: Makes less track window size changes.
78//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
79//config: this option makes less perform a last-ditch effort to find it:
80//config: position cursor to 999,999 and ask terminal to report real
81//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
82//config:
83//config: This is not clean but helps a lot on serial lines and such.
84//config:
85//config:config FEATURE_LESS_DASHCMD
86//config: bool "Enable flag changes ('-' command)"
87//config: default y
88//config: depends on LESS
89//config: help
90//config: This enables the ability to change command-line flags within
91//config: less itself ('-' keyboard command).
92//config:
93//config:config FEATURE_LESS_LINENUMS
94//config: bool "Enable dynamic switching of line numbers"
95//config: default y
96//config: depends on FEATURE_LESS_DASHCMD
97//config: help
98//config: Enables "-N" command.
99
Pere Orga5bc8c002011-04-11 03:29:49 +0200100//usage:#define less_trivial_usage
Denys Vlasenko821e6432014-01-22 16:36:22 +0100101//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") "Nh~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200102//usage:#define less_full_usage "\n\n"
103//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200104//usage: "\n -E Quit once the end of a file is reached"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100105//usage: IF_FEATURE_LESS_REGEXP(
106//usage: "\n -I Ignore case in all searches"
107//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200108//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200109//usage: "\n -M,-m Display status line with line numbers"
110//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200111//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200112//usage: "\n -N Prefix line number to each line"
Denys Vlasenko53772862012-04-17 16:11:25 +0200113//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200114
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200115#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000116
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000117#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000118#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000119#include "xregex.h"
120#endif
121
Marek Polacek7b181072010-10-28 21:34:56 +0200122
123#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000124/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200125#define HIGHLIGHT ESC"[7m"
126#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200127/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200128#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200129/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200130#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000131
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000132enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000133/* Absolute max of lines eaten */
134 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000135/* This many "after the end" lines we will show (at max) */
136 TILDES = 1,
137};
138
Rob Landley9200e792005-09-15 19:26:59 +0000139/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000140enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000141 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000142 FLAG_M = 1 << 1,
143 FLAG_m = 1 << 2,
144 FLAG_N = 1 << 3,
145 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000146 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000147 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000148/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000149 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
150};
Rob Landley9200e792005-09-15 19:26:59 +0000151
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000152#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000153enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000154#endif
155
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000156struct globals {
157 int cur_fline; /* signed */
158 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +0000159 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000160/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000161 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000162 unsigned max_fline;
163 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000164 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000165 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000166#if ENABLE_FEATURE_LESS_WINCH
167 unsigned winch_counter;
168#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000169 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000170 ssize_t readpos;
171 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000172 const char **buffer;
173 const char **flines;
174 const char *empty_line_marker;
175 unsigned num_files;
176 unsigned current_file;
177 char *filename;
178 char **files;
179#if ENABLE_FEATURE_LESS_MARKS
180 unsigned num_marks;
181 unsigned mark_lines[15][2];
182#endif
183#if ENABLE_FEATURE_LESS_REGEXP
184 unsigned *match_lines;
185 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000186 int wanted_match; /* signed! */
187 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000188 regex_t pattern;
189 smallint pattern_valid;
190#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200191#if ENABLE_FEATURE_LESS_ASK_TERMINAL
192 smallint winsize_err;
193#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000194 smallint terminated;
195 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000196 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000197};
198#define G (*ptr_to_globals)
199#define cur_fline (G.cur_fline )
200#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000201#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000202#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000203#define max_fline (G.max_fline )
204#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000205#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000206#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000207#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000208/* This one is 100% not cached by compiler on read access */
209#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000210#define eof_error (G.eof_error )
211#define readpos (G.readpos )
212#define readeof (G.readeof )
213#define buffer (G.buffer )
214#define flines (G.flines )
215#define empty_line_marker (G.empty_line_marker )
216#define num_files (G.num_files )
217#define current_file (G.current_file )
218#define filename (G.filename )
219#define files (G.files )
220#define num_marks (G.num_marks )
221#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000222#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000223#define match_lines (G.match_lines )
224#define match_pos (G.match_pos )
225#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000226#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000227#define pattern (G.pattern )
228#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000229#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000230#define terminated (G.terminated )
231#define term_orig (G.term_orig )
232#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000233#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000234#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000235 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
236 less_gets_pos = -1; \
237 empty_line_marker = "~"; \
238 num_files = 1; \
239 current_file = 1; \
240 eof_error = 1; \
241 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000242 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000243} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000244
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000245/* flines[] are lines read from stdin, each in malloc'ed buffer.
246 * Line numbers are stored as uint32_t prepended to each line.
247 * Pointer is adjusted so that flines[i] points directly past
248 * line number. Accesor: */
249#define MEMPTR(p) ((char*)(p) - 4)
250#define LINENO(p) (*(uint32_t*)((p) - 4))
251
252
Rob Landley9200e792005-09-15 19:26:59 +0000253/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000254static void set_tty_cooked(void)
255{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100256 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000257 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000258}
259
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000260/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000261 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000262static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000263{
Marek Polacek7b181072010-10-28 21:34:56 +0200264 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000265}
266
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000267static void clear_line(void)
268{
Marek Polacek7b181072010-10-28 21:34:56 +0200269 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000270}
271
272static void print_hilite(const char *str)
273{
274 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000275}
276
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000277static void print_statusline(const char *str)
278{
279 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000280 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000281}
282
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000283/* Exit the program gracefully */
284static void less_exit(int code)
285{
286 set_tty_cooked();
287 clear_line();
288 if (code < 0)
289 kill_myself_with_sig(- code); /* does not return */
290 exit(code);
291}
292
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000293#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
294 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000295static void re_wrap(void)
296{
297 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000298 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000299 int src_idx;
300 int dst_idx;
301 int new_cur_fline = 0;
302 uint32_t lineno;
303 char linebuf[w + 1];
304 const char **old_flines = flines;
305 const char *s;
306 char **new_flines = NULL;
307 char *d;
308
309 if (option_mask32 & FLAG_N)
310 w -= 8;
311
312 src_idx = 0;
313 dst_idx = 0;
314 s = old_flines[0];
315 lineno = LINENO(s);
316 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000317 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000318 while (1) {
319 *d = *s;
320 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000321 new_line_pos++;
322 if (*d == '\t') /* tab */
323 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000324 s++;
325 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000326 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000327 int sz;
328 /* new line is full, create next one */
329 *d = '\0';
330 next_new:
331 sz = (d - linebuf) + 1; /* + 1: NUL */
332 d = ((char*)xmalloc(sz + 4)) + 4;
333 LINENO(d) = lineno;
334 memcpy(d, linebuf, sz);
335 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
336 new_flines[dst_idx] = d;
337 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000338 if (new_line_pos < w) {
339 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000340 if (src_idx > max_fline)
341 break;
342 lineno = LINENO(s);
343 }
344 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000345 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000346 }
347 continue;
348 }
349 /* *d == NUL: old line ended, go to next old one */
350 free(MEMPTR(old_flines[src_idx]));
351 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000352 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000353 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000354 src_idx++;
355 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000356 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000357 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000358 s = old_flines[src_idx];
359 if (lineno != LINENO(s)) {
360 /* this is not a continuation line!
361 * create next _new_ line too */
362 goto next_new;
363 }
364 }
365
366 free(old_flines);
367 flines = (const char **)new_flines;
368
369 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000371 cur_fline = new_cur_fline;
372 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000373#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000374 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000375#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000376}
377#endif
378
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000379#if ENABLE_FEATURE_LESS_REGEXP
380static void fill_match_lines(unsigned pos);
381#else
382#define fill_match_lines(pos) ((void)0)
383#endif
384
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000385/* Devilishly complex routine.
386 *
387 * Has to deal with EOF and EPIPE on input,
388 * with line wrapping, with last line not ending in '\n'
389 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000390 * It reads input again if last time we got an EOF (thus supporting
391 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000392 *
393 * Variables used:
394 * flines[] - array of lines already read. Linewrap may cause
395 * one source file line to occupy several flines[n].
396 * flines[max_fline] - last line, possibly incomplete.
397 * terminated - 1 if flines[max_fline] is 'terminated'
398 * (if there was '\n' [which isn't stored itself, we just remember
399 * that it was seen])
400 * max_lineno - last line's number, this one doesn't increment
401 * on line wrap, only on "real" new lines.
402 * readbuf[0..readeof-1] - small preliminary buffer.
403 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000404 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000405 * (takes into account tabs and backspaces)
406 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
407 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000408static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000409{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000410#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000411 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000412 int w = width;
413 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000414#if ENABLE_FEATURE_LESS_REGEXP
415 unsigned old_max_fline = max_fline;
416 time_t last_time = 0;
417 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
418#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000419
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000420 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000421 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000422
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000423 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000424
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000425 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000426 max_fline += last_terminated;
427 if (!last_terminated) {
428 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000429 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000430 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000431 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000432 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000433 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000434 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000435 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000436
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000437 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000438 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000439 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000440 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000441 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000442 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000443 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000444 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000445 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000446 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000447 readpos = 0;
448 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000449 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000450 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000451 }
452 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000453 /* backspace? [needed for manpages] */
454 /* <tab><bs> is (a) insane and */
455 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000456 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000457 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000458 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000459 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000460 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000461 continue;
462 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000463 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000464 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000465 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000466 new_last_line_pos += 7;
467 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000468 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000469 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000470 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000471 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000472 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000473 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000474 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000475 if (c == '\n') {
476 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000477 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000478 break;
479 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000480 /* NUL is substituted by '\n'! */
481 if (c == '\0') c = '\n';
482 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000483 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000484 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000485 /* Corner case: linewrap with only "" wrapping to next line */
486 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000487 if (!last_terminated && !current_line[0]) {
488 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000489 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000490 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000491 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000492 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000493 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000494 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000495
496 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
497 LINENO(flines[max_fline]) = max_lineno;
498 if (terminated)
499 max_lineno++;
500
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000501 if (max_fline >= MAXLINES) {
502 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000503 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000504 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000505 if (!(option_mask32 & FLAG_S)
506 ? (max_fline > cur_fline + max_displayed_line)
507 : (max_fline >= cur_fline
508 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
509 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000510#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000511 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000512#else
513 if (wanted_match >= num_matches) { /* goto_match called us */
514 fill_match_lines(old_max_fline);
515 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000516 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000517 if (wanted_match < num_matches)
518 break;
519#endif
520 }
521 if (eof_error <= 0) {
522 if (eof_error < 0) {
523 if (errno == EAGAIN) {
524 /* not yet eof or error, reset flag (or else
525 * we will hog CPU - select() will return
526 * immediately */
527 eof_error = 1;
528 } else {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200529 print_statusline(bb_msg_read_error);
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000530 }
531 }
532#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000533 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000534#else
535 if (wanted_match < num_matches) {
536 break;
537 } else { /* goto_match called us */
538 time_t t = time(NULL);
539 if (t != last_time) {
540 last_time = t;
541 if (--seconds_p1 == 0)
542 break;
543 }
544 sched_yield();
545 goto again0; /* go loop again (max 2 seconds) */
546 }
547#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000548 }
549 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000550 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000551 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000552 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000553 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000554 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000555#if ENABLE_FEATURE_LESS_REGEXP
556 /* prevent us from being stuck in search for a match */
557 wanted_match = -1;
558#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000559#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000560}
561
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000562#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000563/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000564 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000565static int calc_percent(void)
566{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000567 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000568 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000569}
570
Rob Landley9200e792005-09-15 19:26:59 +0000571/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000572static void m_status_print(void)
573{
Rob Landley9200e792005-09-15 19:26:59 +0000574 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000575
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200576 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000577 return;
578
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000579 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000580 printf(HIGHLIGHT"%s", filename);
581 if (num_files > 1)
582 printf(" (file %i of %i)", current_file, num_files);
583 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000584 cur_fline + 1, cur_fline + max_displayed_line + 1,
585 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000586 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000587 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000588 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000589 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000590 return;
Rob Landley9200e792005-09-15 19:26:59 +0000591 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000592 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000593 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000594}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000595#endif
596
Rob Landley9200e792005-09-15 19:26:59 +0000597/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000598static void status_print(void)
599{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000600 const char *p;
601
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200602 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000603 return;
604
Rob Landley9200e792005-09-15 19:26:59 +0000605 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000606#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000607 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000608 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000609 return;
Rob Landley9200e792005-09-15 19:26:59 +0000610 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000611 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000612#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000613
614 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000615 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000616 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000617 return;
618 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000619 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000620 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000621 p = filename;
622 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000623 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000624 p, current_file, num_files);
625 return;
626 }
627 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000628}
629
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000630static void cap_cur_fline(int nlines)
631{
632 int diff;
633 if (cur_fline < 0)
634 cur_fline = 0;
635 if (cur_fline + max_displayed_line > max_fline + TILDES) {
636 cur_fline -= nlines;
637 if (cur_fline < 0)
638 cur_fline = 0;
639 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
640 /* As the number of lines requested was too large, we just move
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100641 * to the end of the file */
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000642 if (diff > 0)
643 cur_fline += diff;
644 }
645}
646
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000647static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000648 /* NUL: never encountered; TAB: not converted */
649 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000650 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
651 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000652static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100653 /* why 40 instead of 4a below? - it is a replacement for '\n'.
654 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000655 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
656 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
657
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000658static void lineno_str(char *nbuf9, const char *line)
659{
660 nbuf9[0] = '\0';
661 if (option_mask32 & FLAG_N) {
662 const char *fmt;
663 unsigned n;
664
665 if (line == empty_line_marker) {
666 memset(nbuf9, ' ', 8);
667 nbuf9[8] = '\0';
668 return;
669 }
670 /* Width of 7 preserves tab spacing in the text */
671 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000672 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000673 if (n > 9999999) {
674 n %= 10000000;
675 fmt = "%07u ";
676 }
677 sprintf(nbuf9, fmt, n);
678 }
679}
680
681
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000682#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000683static void print_found(const char *line)
684{
685 int match_status;
686 int eflags;
687 char *growline;
688 regmatch_t match_structs;
689
690 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000691 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000692 const char *str = line;
693 char *p = buf;
694 size_t n;
695
696 while (*str) {
697 n = strcspn(str, controls);
698 if (n) {
699 if (!str[n]) break;
700 memcpy(p, str, n);
701 p += n;
702 str += n;
703 }
704 n = strspn(str, controls);
705 memset(p, '.', n);
706 p += n;
707 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000708 }
709 strcpy(p, str);
710
711 /* buf[] holds quarantined version of str */
712
713 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100714 * and NORMAL escape sequences placed around it.
715 * NB: we regex against line, but insert text
716 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000717 str = buf;
718 growline = NULL;
719 eflags = 0;
720 goto start;
721
722 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000723 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200724 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200725 (int)match_structs.rm_so, str,
726 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000727 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000728 free(growline);
729 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000730 str += match_structs.rm_eo;
731 line += match_structs.rm_eo;
732 eflags = REG_NOTBOL;
733 start:
734 /* Most of the time doesn't find the regex, optimize for that */
735 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000736 /* if even "" matches, treat it as "not a match" */
737 if (match_structs.rm_so >= match_structs.rm_eo)
738 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000739 }
740
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000741 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000742 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000743 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000744 return;
745 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000746 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000747 free(growline);
748}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000749#else
750void print_found(const char *line);
751#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000752
753static void print_ascii(const char *str)
754{
755 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000756 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000757 char *p;
758 size_t n;
759
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000760 lineno_str(nbuf9, str);
761 printf(CLEAR_2_EOL"%s", nbuf9);
762
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000763 while (*str) {
764 n = strcspn(str, controls);
765 if (n) {
766 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000767 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000768 str += n;
769 }
770 n = strspn(str, controls);
771 p = buf;
772 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000773 if (*str == 0x7f)
774 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000775 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000776 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
777 /* I want to know who committed this sin */
778 *p++ = '{';
779 else
780 *p++ = ctrlconv[(unsigned char)*str];
781 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000782 } while (--n);
783 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000784 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000785 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000786 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000787}
788
Rob Landley9200e792005-09-15 19:26:59 +0000789/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000790static void buffer_print(void)
791{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000792 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000793
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000794 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000795 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000796 if (pattern_valid)
797 print_found(buffer[i]);
798 else
799 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000800 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000801}
802
Denis Vlasenkod9083952008-10-24 10:42:21 +0000803static void buffer_fill_and_print(void)
804{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000805 unsigned i;
806#if ENABLE_FEATURE_LESS_DASHCMD
807 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000808
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000809 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000810 /* Go back to the beginning of this line */
811 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
812 fpos--;
813 }
814
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000815 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000816 while (i <= max_displayed_line && fpos <= max_fline) {
817 int lineno = LINENO(flines[fpos]);
818 buffer[i] = flines[fpos];
819 i++;
820 do {
821 fpos++;
822 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000823 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000824 && lineno == LINENO(flines[fpos])
825 );
826 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000827#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000828 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
829 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000830 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000831#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000832 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000833 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000834 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000835 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000836}
837
838/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000839static void buffer_down(int nlines)
840{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000841 cur_fline += nlines;
842 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000843 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000844 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000845}
846
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000847static void buffer_up(int nlines)
848{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000849 cur_fline -= nlines;
850 if (cur_fline < 0) cur_fline = 0;
851 read_lines();
852 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000853}
854
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000855static void buffer_line(int linenum)
856{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000857 if (linenum < 0)
858 linenum = 0;
859 cur_fline = linenum;
860 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000861 if (linenum + max_displayed_line > max_fline)
862 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000863 if (linenum < 0)
864 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000865 cur_fline = linenum;
866 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000867}
868
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000869static void open_file_and_read_lines(void)
870{
871 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000872 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000873 } else {
874 /* "less" with no arguments in argv[] */
875 /* For status line only */
876 filename = xstrdup(bb_msg_standard_input);
877 }
878 readpos = 0;
879 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000880 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000881 terminated = 1;
882 read_lines();
883}
884
885/* Reinitialize everything for a new file - free the memory and start over */
886static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000887{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000888 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000889
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000890 if (flines) {
891 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000892 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000893 free(flines);
894 flines = NULL;
895 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000896
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000897 max_fline = -1;
898 cur_fline = 0;
899 max_lineno = 0;
900 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200901#if ENABLE_FEATURE_LESS_ASK_TERMINAL
902 if (G.winsize_err)
903 printf("\033[999;999H" "\033[6n");
904#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000905 buffer_fill_and_print();
906}
907
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200908static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000909{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000910 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200911 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +0000912 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000913
Denis Vlasenko33196372008-02-23 01:25:38 +0000914 pfd[0].fd = STDIN_FILENO;
915 pfd[0].events = POLLIN;
916 pfd[1].fd = kbd_fd;
917 pfd[1].events = POLLIN;
918 again:
919 tcsetattr(kbd_fd, TCSANOW, &term_less);
920 /* NB: select/poll returns whenever read will not block. Therefore:
921 * if eof is reached, select/poll will return immediately
922 * because read will immediately return 0 bytes.
923 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000924 * (switch fd into O_NONBLOCK'ed mode to avoid it)
925 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000926 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000927 /* Are we interested in stdin? */
928//TODO: reuse code for determining this
929 if (!(option_mask32 & FLAG_S)
930 ? !(max_fline > cur_fline + max_displayed_line)
931 : !(max_fline >= cur_fline
932 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000933 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000934 if (eof_error > 0) /* did NOT reach eof yet */
935 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000936 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000937 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000938 if (less_gets_pos >= 0)
939 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100940 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000941
Denys Vlasenko020f4062009-05-17 16:44:54 +0200942 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000943#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000944 while (1) {
945 int r;
946 /* NB: SIGWINCH interrupts poll() */
947 r = poll(pfd + rd, 2 - rd, -1);
948 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
949 return '\\'; /* anything which has no defined function */
950 if (r) break;
951 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000952#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000953 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000954#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000955 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000956
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000957 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
958 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200959 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
960 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +0000961 if (errno == EAGAIN) {
962 /* No keyboard input available. Since poll() did return,
963 * we should have input on stdin */
964 read_lines();
965 buffer_fill_and_print();
966 goto again;
967 }
968 /* EOF/error (ssh session got killed etc) */
969 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000970 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000971 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200972 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000973}
974
Denys Vlasenko020f4062009-05-17 16:44:54 +0200975/* Grab a character from input without requiring the return key.
976 * May return KEYCODE_xxx values.
977 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200978static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000979{
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200980 int64_t key64;
981 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000982
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000983 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000984 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200985 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000986 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000987
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200988 /* Discard Ctrl-something chars.
989 * (checking only lower 32 bits is a size optimization:
990 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
991 */
992 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000993 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +0200994
995 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000996}
997
998static char* less_gets(int sz)
999{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001000 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001001 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001002 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001003
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001004 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001005 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001006 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001007 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001008 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001009 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001010 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001011 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001012 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001013 if (c == 0x7f)
1014 c = 8;
1015 if (c == 8 && i) {
1016 printf("\x8 \x8");
1017 i--;
1018 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001019 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001020 continue;
1021 if (i >= width - sz - 1)
1022 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001023 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001024 result[i++] = c;
1025 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001026 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001027}
1028
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001029static void examine_file(void)
1030{
Denis Vlasenko33196372008-02-23 01:25:38 +00001031 char *new_fname;
1032
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001033 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001034 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001035 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001036 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001037 err:
1038 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001039 return;
1040 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001041 if (access(new_fname, R_OK) != 0) {
1042 print_statusline("Cannot read this file");
1043 goto err;
1044 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001045 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001046 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001047 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001048 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001049 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001050 num_files++; */
1051 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001052 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001053 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001054}
1055
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001056/* This function changes the file currently being paged. direction can be one of the following:
1057 * -1: go back one file
1058 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001059 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001060static void change_file(int direction)
1061{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001062 if (current_file != ((direction > 0) ? num_files : 1)) {
1063 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001064 free(filename);
1065 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001066 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001067 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001068 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001069 }
1070}
1071
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001072static void remove_current_file(void)
1073{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001074 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001075
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001076 if (num_files < 2)
1077 return;
1078
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001079 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001080 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001081 for (i = 3; i <= num_files; i++)
1082 files[i - 2] = files[i - 1];
1083 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001084 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001085 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001086 for (i = 2; i <= num_files; i++)
1087 files[i - 2] = files[i - 1];
1088 num_files--;
1089 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001090 }
1091}
1092
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001093static void colon_process(void)
1094{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001095 int keypress;
1096
1097 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001098 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001099
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001100 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001101 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001102 case 'd':
1103 remove_current_file();
1104 break;
1105 case 'e':
1106 examine_file();
1107 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001108#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001109 case 'f':
1110 m_status_print();
1111 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001112#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001113 case 'n':
1114 change_file(1);
1115 break;
1116 case 'p':
1117 change_file(-1);
1118 break;
1119 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001120 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001121 break;
1122 case 'x':
1123 change_file(0);
1124 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001125 }
1126}
1127
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001128#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001129static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001130{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001131 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001132 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001133 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001134 match = 0;
1135 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001136}
1137
Rob Landleya2e98042006-04-18 01:53:41 +00001138static void goto_match(int match)
1139{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001140 if (!pattern_valid)
1141 return;
1142 if (match < 0)
1143 match = 0;
1144 /* Try to find next match if eof isn't reached yet */
1145 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001146 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001147 read_lines();
1148 }
1149 if (num_matches) {
1150 normalize_match_pos(match);
1151 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001152 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001153 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001154 }
Rob Landleya2e98042006-04-18 01:53:41 +00001155}
1156
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001157static void fill_match_lines(unsigned pos)
1158{
1159 if (!pattern_valid)
1160 return;
1161 /* Run the regex on each line of the current file */
1162 while (pos <= max_fline) {
1163 /* If this line matches */
1164 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1165 /* and we didn't match it last time */
1166 && !(num_matches && match_lines[num_matches-1] == pos)
1167 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001168 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001169 match_lines[num_matches++] = pos;
1170 }
1171 pos++;
1172 }
1173}
1174
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001175static void regex_process(void)
1176{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001177 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001178
1179 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001180 free(match_lines);
1181 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001182 match_pos = 0;
1183 num_matches = 0;
1184 if (pattern_valid) {
1185 regfree(&pattern);
1186 pattern_valid = 0;
1187 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001188
Rob Landleya2e98042006-04-18 01:53:41 +00001189 /* Get the uncompiled regular expression from the user */
1190 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001191 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001192 uncomp_regex = less_gets(1);
1193 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001194 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001195 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001196 return;
1197 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001198
Rob Landleya2e98042006-04-18 01:53:41 +00001199 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001200 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001201 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001202 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001203 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001204 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001205 free(err);
1206 return;
1207 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001208
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001209 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001210 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001211 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001212 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001213 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001214 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001215 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001216 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001217 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1218 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001219
1220 /* It's possible that no matches are found yet.
1221 * goto_match() will read input looking for match,
1222 * if needed */
1223 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001224}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001225#endif
1226
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001227static void number_process(int first_digit)
1228{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001229 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001230 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001231 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001232 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001233
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001234 num_input[0] = first_digit;
1235
1236 /* Clear the current line, print a prompt, and then print the digit */
1237 clear_line();
1238 printf(":%c", first_digit);
1239
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001240 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001241 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001242 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001243 keypress = less_getch(i + 1);
1244 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001245 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001246 num_input[i] = keypress;
1247 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001248 i++;
1249 }
1250
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001251 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001252 num = bb_strtou(num_input, NULL, 10);
1253 /* on format error, num == -1 */
1254 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001255 buffer_print();
1256 return;
1257 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001258
1259 /* We now know the number and the letter entered, so we process them */
1260 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001261 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001262 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001263 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001264 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001265 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001266 break;
1267 case 'g': case '<': case 'G': case '>':
1268 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001269 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001270 buffer_line(num - 1);
1271 break;
1272 case 'p': case '%':
1273 num = num * (max_fline / 100); /* + max_fline / 2; */
1274 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001275 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001276 buffer_line(num);
1277 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001278#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001279 case 'n':
1280 goto_match(match_pos + num);
1281 break;
1282 case '/':
1283 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1284 regex_process();
1285 break;
1286 case '?':
1287 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1288 regex_process();
1289 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001290#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001291 }
1292}
1293
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001294#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001295static void flag_change(void)
1296{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001297 int keypress;
1298
1299 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001300 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001301 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001302
1303 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001304 case 'M':
1305 option_mask32 ^= FLAG_M;
1306 break;
1307 case 'm':
1308 option_mask32 ^= FLAG_m;
1309 break;
1310 case 'E':
1311 option_mask32 ^= FLAG_E;
1312 break;
1313 case '~':
1314 option_mask32 ^= FLAG_TILDE;
1315 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001316 case 'S':
1317 option_mask32 ^= FLAG_S;
1318 buffer_fill_and_print();
1319 break;
1320#if ENABLE_FEATURE_LESS_LINENUMS
1321 case 'N':
1322 option_mask32 ^= FLAG_N;
1323 re_wrap();
1324 buffer_fill_and_print();
1325 break;
1326#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001327 }
1328}
1329
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001330#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001331static void show_flag_status(void)
1332{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001333 int keypress;
1334 int flag_val;
1335
1336 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001337 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001338 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001339
1340 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001341 case 'M':
1342 flag_val = option_mask32 & FLAG_M;
1343 break;
1344 case 'm':
1345 flag_val = option_mask32 & FLAG_m;
1346 break;
1347 case '~':
1348 flag_val = option_mask32 & FLAG_TILDE;
1349 break;
1350 case 'N':
1351 flag_val = option_mask32 & FLAG_N;
1352 break;
1353 case 'E':
1354 flag_val = option_mask32 & FLAG_E;
1355 break;
1356 default:
1357 flag_val = 0;
1358 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001359 }
1360
1361 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001362 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001363}
1364#endif
1365
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001366#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1367
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001368static void save_input_to_file(void)
1369{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001370 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001371 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001372 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001373 FILE *fp;
1374
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001375 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001376 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001377 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001378 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001379 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001380 msg = "Error opening log file";
1381 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001382 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001383 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001384 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001385 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001386 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001387 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001388 ret:
1389 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001390 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001391}
1392
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001393#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001394static void add_mark(void)
1395{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001396 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001397
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001398 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001399 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001400
1401 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001402 /* If we exceed 15 marks, start overwriting previous ones */
1403 if (num_marks == 14)
1404 num_marks = 0;
1405
1406 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001407 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001408 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001409 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001410 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001411 }
1412}
1413
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001414static void goto_mark(void)
1415{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001416 int letter;
1417 int i;
1418
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001419 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001420 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001421 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001422
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001423 if (isalpha(letter)) {
1424 for (i = 0; i <= num_marks; i++)
1425 if (letter == mark_lines[i][0]) {
1426 buffer_line(mark_lines[i][1]);
1427 break;
1428 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001429 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001430 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001431 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001432 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001433}
1434#endif
1435
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001436#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001437static char opp_bracket(char bracket)
1438{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001439 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001440 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1441 bracket++;
1442 case '(': /* ')' == '(' + 1 */
1443 bracket++;
1444 break;
1445 case '}': case ']':
1446 bracket--;
1447 case ')':
1448 bracket--;
1449 break;
1450 };
1451 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001452}
1453
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001454static void match_right_bracket(char bracket)
1455{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001456 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001457
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001458 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001459 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001460 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001461 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001462 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001463 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001464 if (strchr(flines[i], bracket) != NULL) {
1465 buffer_line(i);
1466 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001467 }
1468 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001469 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001470}
1471
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001472static void match_left_bracket(char bracket)
1473{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001474 int i;
1475
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001476 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001477 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001478 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001479 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001480
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001481 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001482 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001483 if (strchr(flines[i], bracket) != NULL) {
1484 buffer_line(i);
1485 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001486 }
1487 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001488 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001489}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001490#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001491
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001492static void keypress_process(int keypress)
1493{
Rob Landley9200e792005-09-15 19:26:59 +00001494 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001495 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001496 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001497 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001498 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001499 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001500 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001501 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001502 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001503 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001504 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001505 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001506 break;
1507 case 'd':
1508 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001509 break;
1510 case 'u':
1511 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001512 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001513 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001514 buffer_line(0);
1515 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001516 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001517 cur_fline = MAXLINES;
1518 read_lines();
1519 buffer_line(cur_fline);
1520 break;
1521 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001522 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001523 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001524#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001525 case 'm':
1526 add_mark();
1527 buffer_print();
1528 break;
1529 case '\'':
1530 goto_mark();
1531 buffer_print();
1532 break;
Rob Landley9200e792005-09-15 19:26:59 +00001533#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001534 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001535 /* TODO: (1) also bind ^R, ^L to this?
1536 * (2) re-measure window size?
1537 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001538 buffer_print();
1539 break;
1540 /*case 'R':
1541 full_repaint();
1542 break;*/
1543 case 's':
1544 save_input_to_file();
1545 break;
1546 case 'E':
1547 examine_file();
1548 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001549#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001550 case '=':
1551 m_status_print();
1552 break;
Rob Landley9200e792005-09-15 19:26:59 +00001553#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001554#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001555 case '/':
1556 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1557 regex_process();
1558 break;
1559 case 'n':
1560 goto_match(match_pos + 1);
1561 break;
1562 case 'N':
1563 goto_match(match_pos - 1);
1564 break;
1565 case '?':
1566 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1567 regex_process();
1568 break;
Rob Landley9200e792005-09-15 19:26:59 +00001569#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001570#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001571 case '-':
1572 flag_change();
1573 buffer_print();
1574 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001575#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001576 case '_':
1577 show_flag_status();
1578 break;
Rob Landley9200e792005-09-15 19:26:59 +00001579#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001580#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001581#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001582 case '{': case '(': case '[':
1583 match_right_bracket(keypress);
1584 break;
1585 case '}': case ')': case ']':
1586 match_left_bracket(keypress);
1587 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001588#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001589 case ':':
1590 colon_process();
1591 break;
Rob Landley9200e792005-09-15 19:26:59 +00001592 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001593
Rob Landley9200e792005-09-15 19:26:59 +00001594 if (isdigit(keypress))
1595 number_process(keypress);
1596}
1597
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001598static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001599{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001600 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001601}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001602
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001603#if ENABLE_FEATURE_LESS_WINCH
1604static void sigwinch_handler(int sig UNUSED_PARAM)
1605{
1606 winch_counter++;
1607}
1608#endif
1609
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001610int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001611int less_main(int argc, char **argv)
1612{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001613 char *tty_name;
1614 int tty_fd;
1615
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001616 INIT_G();
1617
Denys Vlasenko821e6432014-01-22 16:36:22 +01001618 /* TODO: -x: do not interpret backspace, -xx: tab also
1619 * -xxx: newline also
1620 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1621 * -s: condense many empty lines to one
1622 * (used by some setups for manpage display)
1623 */
1624 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S") /*ignored:*/"s");
Rob Landley9200e792005-09-15 19:26:59 +00001625 argc -= optind;
1626 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001627 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001628 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001629
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001630 /* Another popular pager, most, detects when stdout
1631 * is not a tty and turns into cat. This makes sense. */
1632 if (!isatty(STDOUT_FILENO))
1633 return bb_cat(argv);
1634
Rob Landley9200e792005-09-15 19:26:59 +00001635 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001636 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001637 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001638 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001639 bb_show_usage();
1640 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001641 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001642 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001643 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001644
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001645 if (option_mask32 & FLAG_TILDE)
1646 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001647
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001648 /* Some versions of less can survive w/o controlling tty,
1649 * try to do the same. This also allows to specify an alternative
1650 * tty via "less 1<>TTY".
1651 * We don't try to use STDOUT_FILENO directly,
1652 * since we want to set this fd to non-blocking mode,
1653 * and not bother with restoring it on exit.
1654 */
1655 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1656 if (tty_name) {
1657 tty_fd = open(tty_name, O_RDONLY);
1658 free(tty_name);
1659 if (tty_fd < 0)
1660 goto try_ctty;
1661 } else {
1662 /* Try controlling tty */
1663 try_ctty:
1664 tty_fd = open(CURRENT_TTY, O_RDONLY);
1665 if (tty_fd < 0)
1666 return bb_cat(argv);
1667 }
1668 ndelay_on(tty_fd);
1669 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001670
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001671 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001672 term_less = term_orig;
1673 term_less.c_lflag &= ~(ICANON | ECHO);
1674 term_less.c_iflag &= ~(IXON | ICRNL);
1675 /*term_less.c_oflag &= ~ONLCR;*/
1676 term_less.c_cc[VMIN] = 1;
1677 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001678
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001679 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 +00001680 /* 20: two tabstops + 4 */
1681 if (width < 20 || max_displayed_line < 3)
1682 return bb_cat(argv);
1683 max_displayed_line -= 2;
1684
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001685 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001686 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001687#if ENABLE_FEATURE_LESS_WINCH
1688 signal(SIGWINCH, sigwinch_handler);
1689#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001690
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001691 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001692 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001693 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001694 int64_t keypress;
1695
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001696#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001697 while (WINCH_COUNTER) {
1698 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001699 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001700 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1701 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001702 /* 20: two tabstops + 4 */
1703 if (width < 20)
1704 width = 20;
1705 if (max_displayed_line < 3)
1706 max_displayed_line = 3;
1707 max_displayed_line -= 2;
1708 free(buffer);
1709 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001710 /* Avoid re-wrap and/or redraw if we already know
1711 * we need to do it again. These ops are expensive */
1712 if (WINCH_COUNTER)
1713 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001714 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001715 if (WINCH_COUNTER)
1716 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001717 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001718 /* This took some time. Loop back and check,
1719 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001720 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001721 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001722# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1723 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1724 uint32_t rc = (keypress >> 32);
1725 width = (rc & 0x7fff);
1726 max_displayed_line = ((rc >> 16) & 0x7fff);
1727 goto got_size;
1728 }
1729# endif
1730#else
1731 keypress = less_getch(-1); /* -1: do not position cursor */
1732#endif
Rob Landley9200e792005-09-15 19:26:59 +00001733 keypress_process(keypress);
1734 }
1735}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001736
1737/*
1738Help text of less version 418 is below.
1739If you are implementing something, keeping
1740key and/or command line switch compatibility is a good idea:
1741
1742
1743 SUMMARY OF LESS COMMANDS
1744
1745 Commands marked with * may be preceded by a number, N.
1746 Notes in parentheses indicate the behavior if N is given.
1747 h H Display this help.
1748 q :q Q :Q ZZ Exit.
1749 ---------------------------------------------------------------------------
1750 MOVING
1751 e ^E j ^N CR * Forward one line (or N lines).
1752 y ^Y k ^K ^P * Backward one line (or N lines).
1753 f ^F ^V SPACE * Forward one window (or N lines).
1754 b ^B ESC-v * Backward one window (or N lines).
1755 z * Forward one window (and set window to N).
1756 w * Backward one window (and set window to N).
1757 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1758 d ^D * Forward one half-window (and set half-window to N).
1759 u ^U * Backward one half-window (and set half-window to N).
1760 ESC-) RightArrow * Left one half screen width (or N positions).
1761 ESC-( LeftArrow * Right one half screen width (or N positions).
1762 F Forward forever; like "tail -f".
1763 r ^R ^L Repaint screen.
1764 R Repaint screen, discarding buffered input.
1765 ---------------------------------------------------
1766 Default "window" is the screen height.
1767 Default "half-window" is half of the screen height.
1768 ---------------------------------------------------------------------------
1769 SEARCHING
1770 /pattern * Search forward for (N-th) matching line.
1771 ?pattern * Search backward for (N-th) matching line.
1772 n * Repeat previous search (for N-th occurrence).
1773 N * Repeat previous search in reverse direction.
1774 ESC-n * Repeat previous search, spanning files.
1775 ESC-N * Repeat previous search, reverse dir. & spanning files.
1776 ESC-u Undo (toggle) search highlighting.
1777 ---------------------------------------------------
1778 Search patterns may be modified by one or more of:
1779 ^N or ! Search for NON-matching lines.
1780 ^E or * Search multiple files (pass thru END OF FILE).
1781 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1782 ^K Highlight matches, but don't move (KEEP position).
1783 ^R Don't use REGULAR EXPRESSIONS.
1784 ---------------------------------------------------------------------------
1785 JUMPING
1786 g < ESC-< * Go to first line in file (or line N).
1787 G > ESC-> * Go to last line in file (or line N).
1788 p % * Go to beginning of file (or N percent into file).
1789 t * Go to the (N-th) next tag.
1790 T * Go to the (N-th) previous tag.
1791 { ( [ * Find close bracket } ) ].
1792 } ) ] * Find open bracket { ( [.
1793 ESC-^F <c1> <c2> * Find close bracket <c2>.
1794 ESC-^B <c1> <c2> * Find open bracket <c1>
1795 ---------------------------------------------------
1796 Each "find close bracket" command goes forward to the close bracket
1797 matching the (N-th) open bracket in the top line.
1798 Each "find open bracket" command goes backward to the open bracket
1799 matching the (N-th) close bracket in the bottom line.
1800 m<letter> Mark the current position with <letter>.
1801 '<letter> Go to a previously marked position.
1802 '' Go to the previous position.
1803 ^X^X Same as '.
1804 ---------------------------------------------------
1805 A mark is any upper-case or lower-case letter.
1806 Certain marks are predefined:
1807 ^ means beginning of the file
1808 $ means end of the file
1809 ---------------------------------------------------------------------------
1810 CHANGING FILES
1811 :e [file] Examine a new file.
1812 ^X^V Same as :e.
1813 :n * Examine the (N-th) next file from the command line.
1814 :p * Examine the (N-th) previous file from the command line.
1815 :x * Examine the first (or N-th) file from the command line.
1816 :d Delete the current file from the command line list.
1817 = ^G :f Print current file name.
1818 ---------------------------------------------------------------------------
1819 MISCELLANEOUS COMMANDS
1820 -<flag> Toggle a command line option [see OPTIONS below].
1821 --<name> Toggle a command line option, by name.
1822 _<flag> Display the setting of a command line option.
1823 __<name> Display the setting of an option, by name.
1824 +cmd Execute the less cmd each time a new file is examined.
1825 !command Execute the shell command with $SHELL.
1826 |Xcommand Pipe file between current pos & mark X to shell command.
1827 v Edit the current file with $VISUAL or $EDITOR.
1828 V Print version number of "less".
1829 ---------------------------------------------------------------------------
1830 OPTIONS
1831 Most options may be changed either on the command line,
1832 or from within less by using the - or -- command.
1833 Options may be given in one of two forms: either a single
1834 character preceded by a -, or a name preceeded by --.
1835 -? ........ --help
1836 Display help (from command line).
1837 -a ........ --search-skip-screen
1838 Forward search skips current screen.
1839 -b [N] .... --buffers=[N]
1840 Number of buffers.
1841 -B ........ --auto-buffers
1842 Don't automatically allocate buffers for pipes.
1843 -c ........ --clear-screen
1844 Repaint by clearing rather than scrolling.
1845 -d ........ --dumb
1846 Dumb terminal.
1847 -D [xn.n] . --color=xn.n
1848 Set screen colors. (MS-DOS only)
1849 -e -E .... --quit-at-eof --QUIT-AT-EOF
1850 Quit at end of file.
1851 -f ........ --force
1852 Force open non-regular files.
1853 -F ........ --quit-if-one-screen
1854 Quit if entire file fits on first screen.
1855 -g ........ --hilite-search
1856 Highlight only last match for searches.
1857 -G ........ --HILITE-SEARCH
1858 Don't highlight any matches for searches.
1859 -h [N] .... --max-back-scroll=[N]
1860 Backward scroll limit.
1861 -i ........ --ignore-case
1862 Ignore case in searches that do not contain uppercase.
1863 -I ........ --IGNORE-CASE
1864 Ignore case in all searches.
1865 -j [N] .... --jump-target=[N]
1866 Screen position of target lines.
1867 -J ........ --status-column
1868 Display a status column at left edge of screen.
1869 -k [file] . --lesskey-file=[file]
1870 Use a lesskey file.
1871 -L ........ --no-lessopen
1872 Ignore the LESSOPEN environment variable.
1873 -m -M .... --long-prompt --LONG-PROMPT
1874 Set prompt style.
1875 -n -N .... --line-numbers --LINE-NUMBERS
1876 Don't use line numbers.
1877 -o [file] . --log-file=[file]
1878 Copy to log file (standard input only).
1879 -O [file] . --LOG-FILE=[file]
1880 Copy to log file (unconditionally overwrite).
1881 -p [pattern] --pattern=[pattern]
1882 Start at pattern (from command line).
1883 -P [prompt] --prompt=[prompt]
1884 Define new prompt.
1885 -q -Q .... --quiet --QUIET --silent --SILENT
1886 Quiet the terminal bell.
1887 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1888 Output "raw" control characters.
1889 -s ........ --squeeze-blank-lines
1890 Squeeze multiple blank lines.
1891 -S ........ --chop-long-lines
1892 Chop long lines.
1893 -t [tag] .. --tag=[tag]
1894 Find a tag.
1895 -T [tagsfile] --tag-file=[tagsfile]
1896 Use an alternate tags file.
1897 -u -U .... --underline-special --UNDERLINE-SPECIAL
1898 Change handling of backspaces.
1899 -V ........ --version
1900 Display the version number of "less".
1901 -w ........ --hilite-unread
1902 Highlight first new line after forward-screen.
1903 -W ........ --HILITE-UNREAD
1904 Highlight first new line after any forward movement.
1905 -x [N[,...]] --tabs=[N[,...]]
1906 Set tab stops.
1907 -X ........ --no-init
1908 Don't use termcap init/deinit strings.
1909 --no-keypad
1910 Don't use termcap keypad init/deinit strings.
1911 -y [N] .... --max-forw-scroll=[N]
1912 Forward scroll limit.
1913 -z [N] .... --window=[N]
1914 Set size of window.
1915 -" [c[c]] . --quotes=[c[c]]
1916 Set shell quote characters.
1917 -~ ........ --tilde
1918 Don't display tildes after end of file.
1919 -# [N] .... --shift=[N]
1920 Horizontal scroll amount (0 = one half screen width)
1921
1922 ---------------------------------------------------------------------------
1923 LINE EDITING
1924 These keys can be used to edit text being entered
1925 on the "command line" at the bottom of the screen.
1926 RightArrow ESC-l Move cursor right one character.
1927 LeftArrow ESC-h Move cursor left one character.
1928 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1929 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1930 HOME ESC-0 Move cursor to start of line.
1931 END ESC-$ Move cursor to end of line.
1932 BACKSPACE Delete char to left of cursor.
1933 DELETE ESC-x Delete char under cursor.
1934 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1935 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1936 CNTL-U ESC (MS-DOS only) Delete entire line.
1937 UpArrow ESC-k Retrieve previous command line.
1938 DownArrow ESC-j Retrieve next command line.
1939 TAB Complete filename & cycle.
1940 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1941 CNTL-L Complete filename, list all.
1942*/