blob: 045fd2db3db568b615974800c6c05fa399273949 [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
45//config: bool "Enable extra flags"
46//config: default y
47//config: depends on LESS
48//config: help
49//config: The extra flags provided do the following:
50//config:
51//config: The -M flag enables a more sophisticated status line.
52//config: The -m flag enables a simpler status line with a percentage.
53//config:
54//config:config FEATURE_LESS_MARKS
55//config: bool "Enable marks"
56//config: default y
57//config: depends on LESS
58//config: help
59//config: Marks enable positions in a file to be stored for easy reference.
60//config:
61//config:config FEATURE_LESS_REGEXP
62//config: bool "Enable regular expressions"
63//config: default y
64//config: depends on LESS
65//config: help
66//config: Enable regular expressions, allowing complex file searches.
67//config:
68//config:config FEATURE_LESS_WINCH
69//config: bool "Enable automatic resizing on window size changes"
70//config: default y
71//config: depends on LESS
72//config: help
73//config: Makes less track window size changes.
74//config:
75//config:config FEATURE_LESS_ASK_TERMINAL
76//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
77//config: default y
78//config: depends on FEATURE_LESS_WINCH
79//config: help
80//config: Makes less track window size changes.
81//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
82//config: this option makes less perform a last-ditch effort to find it:
83//config: position cursor to 999,999 and ask terminal to report real
84//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
85//config:
86//config: This is not clean but helps a lot on serial lines and such.
87//config:
88//config:config FEATURE_LESS_DASHCMD
89//config: bool "Enable flag changes ('-' command)"
90//config: default y
91//config: depends on LESS
92//config: help
93//config: This enables the ability to change command-line flags within
94//config: less itself ('-' keyboard command).
95//config:
96//config:config FEATURE_LESS_LINENUMS
97//config: bool "Enable dynamic switching of line numbers"
98//config: default y
99//config: depends on FEATURE_LESS_DASHCMD
100//config: help
101//config: Enables "-N" command.
102
Pere Orga5bc8c002011-04-11 03:29:49 +0200103//usage:#define less_trivial_usage
104//usage: "[-EMNmh~I?] [FILE]..."
105//usage:#define less_full_usage "\n\n"
106//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200107//usage: "\n -E Quit once the end of a file is reached"
108//usage: "\n -M,-m Display status line with line numbers"
109//usage: "\n and percentage through the file"
110//usage: "\n -N Prefix line number to each line"
111//usage: "\n -I Ignore case in all searches"
112//usage: "\n -~ Suppress ~s displayed past the end of the file"
113
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200114#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000115
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000116#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000117#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000118#include "xregex.h"
119#endif
120
Marek Polacek7b181072010-10-28 21:34:56 +0200121
122#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000123/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200124#define HIGHLIGHT ESC"[7m"
125#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200126/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200127#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200128/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200129#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000130
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000131enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000132/* Absolute max of lines eaten */
133 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000134/* This many "after the end" lines we will show (at max) */
135 TILDES = 1,
136};
137
Rob Landley9200e792005-09-15 19:26:59 +0000138/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000139enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000140 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000141 FLAG_M = 1 << 1,
142 FLAG_m = 1 << 2,
143 FLAG_N = 1 << 3,
144 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000145 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000146 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000147/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000148 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
149};
Rob Landley9200e792005-09-15 19:26:59 +0000150
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000151#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000152enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000153#endif
154
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000155struct globals {
156 int cur_fline; /* signed */
157 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +0000158 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000159/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000160 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000161 unsigned max_fline;
162 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000163 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000164 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000165#if ENABLE_FEATURE_LESS_WINCH
166 unsigned winch_counter;
167#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000168 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000169 ssize_t readpos;
170 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000171 const char **buffer;
172 const char **flines;
173 const char *empty_line_marker;
174 unsigned num_files;
175 unsigned current_file;
176 char *filename;
177 char **files;
178#if ENABLE_FEATURE_LESS_MARKS
179 unsigned num_marks;
180 unsigned mark_lines[15][2];
181#endif
182#if ENABLE_FEATURE_LESS_REGEXP
183 unsigned *match_lines;
184 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000185 int wanted_match; /* signed! */
186 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000187 regex_t pattern;
188 smallint pattern_valid;
189#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200190#if ENABLE_FEATURE_LESS_ASK_TERMINAL
191 smallint winsize_err;
192#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000193 smallint terminated;
194 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000195 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000196};
197#define G (*ptr_to_globals)
198#define cur_fline (G.cur_fline )
199#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000200#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000201#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000202#define max_fline (G.max_fline )
203#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000204#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000205#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000206#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000207/* This one is 100% not cached by compiler on read access */
208#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000209#define eof_error (G.eof_error )
210#define readpos (G.readpos )
211#define readeof (G.readeof )
212#define buffer (G.buffer )
213#define flines (G.flines )
214#define empty_line_marker (G.empty_line_marker )
215#define num_files (G.num_files )
216#define current_file (G.current_file )
217#define filename (G.filename )
218#define files (G.files )
219#define num_marks (G.num_marks )
220#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000221#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000222#define match_lines (G.match_lines )
223#define match_pos (G.match_pos )
224#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000225#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000226#define pattern (G.pattern )
227#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000228#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000229#define terminated (G.terminated )
230#define term_orig (G.term_orig )
231#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000232#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000233#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000234 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
235 less_gets_pos = -1; \
236 empty_line_marker = "~"; \
237 num_files = 1; \
238 current_file = 1; \
239 eof_error = 1; \
240 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000241 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000242} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000243
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000244/* flines[] are lines read from stdin, each in malloc'ed buffer.
245 * Line numbers are stored as uint32_t prepended to each line.
246 * Pointer is adjusted so that flines[i] points directly past
247 * line number. Accesor: */
248#define MEMPTR(p) ((char*)(p) - 4)
249#define LINENO(p) (*(uint32_t*)((p) - 4))
250
251
Rob Landley9200e792005-09-15 19:26:59 +0000252/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000253static void set_tty_cooked(void)
254{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100255 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000256 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000257}
258
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000259/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000260 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000261static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000262{
Marek Polacek7b181072010-10-28 21:34:56 +0200263 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000264}
265
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000266static void clear_line(void)
267{
Marek Polacek7b181072010-10-28 21:34:56 +0200268 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000269}
270
271static void print_hilite(const char *str)
272{
273 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000274}
275
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000276static void print_statusline(const char *str)
277{
278 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000279 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000280}
281
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000282/* Exit the program gracefully */
283static void less_exit(int code)
284{
285 set_tty_cooked();
286 clear_line();
287 if (code < 0)
288 kill_myself_with_sig(- code); /* does not return */
289 exit(code);
290}
291
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000292#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
293 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000294static void re_wrap(void)
295{
296 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000297 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000298 int src_idx;
299 int dst_idx;
300 int new_cur_fline = 0;
301 uint32_t lineno;
302 char linebuf[w + 1];
303 const char **old_flines = flines;
304 const char *s;
305 char **new_flines = NULL;
306 char *d;
307
308 if (option_mask32 & FLAG_N)
309 w -= 8;
310
311 src_idx = 0;
312 dst_idx = 0;
313 s = old_flines[0];
314 lineno = LINENO(s);
315 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000316 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000317 while (1) {
318 *d = *s;
319 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000320 new_line_pos++;
321 if (*d == '\t') /* tab */
322 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000323 s++;
324 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000325 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000326 int sz;
327 /* new line is full, create next one */
328 *d = '\0';
329 next_new:
330 sz = (d - linebuf) + 1; /* + 1: NUL */
331 d = ((char*)xmalloc(sz + 4)) + 4;
332 LINENO(d) = lineno;
333 memcpy(d, linebuf, sz);
334 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
335 new_flines[dst_idx] = d;
336 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000337 if (new_line_pos < w) {
338 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000339 if (src_idx > max_fline)
340 break;
341 lineno = LINENO(s);
342 }
343 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000344 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000345 }
346 continue;
347 }
348 /* *d == NUL: old line ended, go to next old one */
349 free(MEMPTR(old_flines[src_idx]));
350 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000351 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000352 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000353 src_idx++;
354 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000355 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000356 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000357 s = old_flines[src_idx];
358 if (lineno != LINENO(s)) {
359 /* this is not a continuation line!
360 * create next _new_ line too */
361 goto next_new;
362 }
363 }
364
365 free(old_flines);
366 flines = (const char **)new_flines;
367
368 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000369 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000370 cur_fline = new_cur_fline;
371 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000372#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000373 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000374#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000375}
376#endif
377
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000378#if ENABLE_FEATURE_LESS_REGEXP
379static void fill_match_lines(unsigned pos);
380#else
381#define fill_match_lines(pos) ((void)0)
382#endif
383
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000384/* Devilishly complex routine.
385 *
386 * Has to deal with EOF and EPIPE on input,
387 * with line wrapping, with last line not ending in '\n'
388 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000389 * It reads input again if last time we got an EOF (thus supporting
390 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000391 *
392 * Variables used:
393 * flines[] - array of lines already read. Linewrap may cause
394 * one source file line to occupy several flines[n].
395 * flines[max_fline] - last line, possibly incomplete.
396 * terminated - 1 if flines[max_fline] is 'terminated'
397 * (if there was '\n' [which isn't stored itself, we just remember
398 * that it was seen])
399 * max_lineno - last line's number, this one doesn't increment
400 * on line wrap, only on "real" new lines.
401 * readbuf[0..readeof-1] - small preliminary buffer.
402 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000403 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000404 * (takes into account tabs and backspaces)
405 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
406 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000407static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000408{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000409#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000410 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000411 int w = width;
412 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000413#if ENABLE_FEATURE_LESS_REGEXP
414 unsigned old_max_fline = max_fline;
415 time_t last_time = 0;
416 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
417#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000418
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000419 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000420 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000421
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000422 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000423
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000424 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000425 max_fline += last_terminated;
426 if (!last_terminated) {
427 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000428 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000429 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000430 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000431 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000432 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000433 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000434 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000435
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000436 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000437 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000438 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000439 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000440 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000441 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000442 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000443 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000444 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000445 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000446 readpos = 0;
447 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000448 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000449 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000450 }
451 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000452 /* backspace? [needed for manpages] */
453 /* <tab><bs> is (a) insane and */
454 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000455 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000456 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000457 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000458 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000459 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000460 continue;
461 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000462 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000463 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000464 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000465 new_last_line_pos += 7;
466 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000467 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000468 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000469 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000470 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000471 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000472 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000473 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000474 if (c == '\n') {
475 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000476 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000477 break;
478 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000479 /* NUL is substituted by '\n'! */
480 if (c == '\0') c = '\n';
481 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000482 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000483 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000484 /* Corner case: linewrap with only "" wrapping to next line */
485 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000486 if (!last_terminated && !current_line[0]) {
487 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000488 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000489 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000490 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000491 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000492 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000493 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000494
495 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
496 LINENO(flines[max_fline]) = max_lineno;
497 if (terminated)
498 max_lineno++;
499
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000500 if (max_fline >= MAXLINES) {
501 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000502 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000503 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000504 if (!(option_mask32 & FLAG_S)
505 ? (max_fline > cur_fline + max_displayed_line)
506 : (max_fline >= cur_fline
507 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
508 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000509#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000510 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000511#else
512 if (wanted_match >= num_matches) { /* goto_match called us */
513 fill_match_lines(old_max_fline);
514 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000515 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000516 if (wanted_match < num_matches)
517 break;
518#endif
519 }
520 if (eof_error <= 0) {
521 if (eof_error < 0) {
522 if (errno == EAGAIN) {
523 /* not yet eof or error, reset flag (or else
524 * we will hog CPU - select() will return
525 * immediately */
526 eof_error = 1;
527 } else {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200528 print_statusline(bb_msg_read_error);
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000529 }
530 }
531#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000532 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000533#else
534 if (wanted_match < num_matches) {
535 break;
536 } else { /* goto_match called us */
537 time_t t = time(NULL);
538 if (t != last_time) {
539 last_time = t;
540 if (--seconds_p1 == 0)
541 break;
542 }
543 sched_yield();
544 goto again0; /* go loop again (max 2 seconds) */
545 }
546#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000547 }
548 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000549 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000550 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000551 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000552 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000553 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000554#if ENABLE_FEATURE_LESS_REGEXP
555 /* prevent us from being stuck in search for a match */
556 wanted_match = -1;
557#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000558#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000559}
560
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000561#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000562/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000563 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000564static int calc_percent(void)
565{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000566 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000567 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000568}
569
Rob Landley9200e792005-09-15 19:26:59 +0000570/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000571static void m_status_print(void)
572{
Rob Landley9200e792005-09-15 19:26:59 +0000573 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000574
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200575 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000576 return;
577
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000578 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000579 printf(HIGHLIGHT"%s", filename);
580 if (num_files > 1)
581 printf(" (file %i of %i)", current_file, num_files);
582 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000583 cur_fline + 1, cur_fline + max_displayed_line + 1,
584 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000585 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000586 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000587 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000588 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000589 return;
Rob Landley9200e792005-09-15 19:26:59 +0000590 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000591 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000592 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000593}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000594#endif
595
Rob Landley9200e792005-09-15 19:26:59 +0000596/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000597static void status_print(void)
598{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000599 const char *p;
600
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200601 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000602 return;
603
Rob Landley9200e792005-09-15 19:26:59 +0000604 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000605#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000606 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000607 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000608 return;
Rob Landley9200e792005-09-15 19:26:59 +0000609 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000610 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000611#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000612
613 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000614 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000615 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000616 return;
617 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000618 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000619 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000620 p = filename;
621 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000622 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000623 p, current_file, num_files);
624 return;
625 }
626 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000627}
628
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000629static void cap_cur_fline(int nlines)
630{
631 int diff;
632 if (cur_fline < 0)
633 cur_fline = 0;
634 if (cur_fline + max_displayed_line > max_fline + TILDES) {
635 cur_fline -= nlines;
636 if (cur_fline < 0)
637 cur_fline = 0;
638 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
639 /* As the number of lines requested was too large, we just move
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100640 * to the end of the file */
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000641 if (diff > 0)
642 cur_fline += diff;
643 }
644}
645
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000646static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000647 /* NUL: never encountered; TAB: not converted */
648 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000649 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
650 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000651static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100652 /* why 40 instead of 4a below? - it is a replacement for '\n'.
653 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000654 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
655 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
656
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000657static void lineno_str(char *nbuf9, const char *line)
658{
659 nbuf9[0] = '\0';
660 if (option_mask32 & FLAG_N) {
661 const char *fmt;
662 unsigned n;
663
664 if (line == empty_line_marker) {
665 memset(nbuf9, ' ', 8);
666 nbuf9[8] = '\0';
667 return;
668 }
669 /* Width of 7 preserves tab spacing in the text */
670 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000671 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000672 if (n > 9999999) {
673 n %= 10000000;
674 fmt = "%07u ";
675 }
676 sprintf(nbuf9, fmt, n);
677 }
678}
679
680
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000681#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000682static void print_found(const char *line)
683{
684 int match_status;
685 int eflags;
686 char *growline;
687 regmatch_t match_structs;
688
689 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000690 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000691 const char *str = line;
692 char *p = buf;
693 size_t n;
694
695 while (*str) {
696 n = strcspn(str, controls);
697 if (n) {
698 if (!str[n]) break;
699 memcpy(p, str, n);
700 p += n;
701 str += n;
702 }
703 n = strspn(str, controls);
704 memset(p, '.', n);
705 p += n;
706 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000707 }
708 strcpy(p, str);
709
710 /* buf[] holds quarantined version of str */
711
712 /* Each part of the line that matches has the HIGHLIGHT
713 and NORMAL escape sequences placed around it.
714 NB: we regex against line, but insert text
715 from quarantined copy (buf[]) */
716 str = buf;
717 growline = NULL;
718 eflags = 0;
719 goto start;
720
721 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000722 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200723 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200724 (int)match_structs.rm_so, str,
725 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000726 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000727 free(growline);
728 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000729 str += match_structs.rm_eo;
730 line += match_structs.rm_eo;
731 eflags = REG_NOTBOL;
732 start:
733 /* Most of the time doesn't find the regex, optimize for that */
734 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000735 /* if even "" matches, treat it as "not a match" */
736 if (match_structs.rm_so >= match_structs.rm_eo)
737 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000738 }
739
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000740 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000741 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000742 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000743 return;
744 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000745 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000746 free(growline);
747}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000748#else
749void print_found(const char *line);
750#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000751
752static void print_ascii(const char *str)
753{
754 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000755 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000756 char *p;
757 size_t n;
758
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000759 lineno_str(nbuf9, str);
760 printf(CLEAR_2_EOL"%s", nbuf9);
761
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000762 while (*str) {
763 n = strcspn(str, controls);
764 if (n) {
765 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000766 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000767 str += n;
768 }
769 n = strspn(str, controls);
770 p = buf;
771 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000772 if (*str == 0x7f)
773 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000774 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000775 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
776 /* I want to know who committed this sin */
777 *p++ = '{';
778 else
779 *p++ = ctrlconv[(unsigned char)*str];
780 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000781 } while (--n);
782 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000783 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000784 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000785 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000786}
787
Rob Landley9200e792005-09-15 19:26:59 +0000788/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000789static void buffer_print(void)
790{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000791 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000792
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000793 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000794 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000795 if (pattern_valid)
796 print_found(buffer[i]);
797 else
798 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000799 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000800}
801
Denis Vlasenkod9083952008-10-24 10:42:21 +0000802static void buffer_fill_and_print(void)
803{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000804 unsigned i;
805#if ENABLE_FEATURE_LESS_DASHCMD
806 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000807
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000808 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000809 /* Go back to the beginning of this line */
810 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
811 fpos--;
812 }
813
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000814 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000815 while (i <= max_displayed_line && fpos <= max_fline) {
816 int lineno = LINENO(flines[fpos]);
817 buffer[i] = flines[fpos];
818 i++;
819 do {
820 fpos++;
821 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000822 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000823 && lineno == LINENO(flines[fpos])
824 );
825 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000826#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000827 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
828 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000829 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000830#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000831 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000832 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000833 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000834 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000835}
836
837/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000838static void buffer_down(int nlines)
839{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000840 cur_fline += nlines;
841 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000842 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000843 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000844}
845
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000846static void buffer_up(int nlines)
847{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000848 cur_fline -= nlines;
849 if (cur_fline < 0) cur_fline = 0;
850 read_lines();
851 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000852}
853
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000854static void buffer_line(int linenum)
855{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000856 if (linenum < 0)
857 linenum = 0;
858 cur_fline = linenum;
859 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000860 if (linenum + max_displayed_line > max_fline)
861 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000862 if (linenum < 0)
863 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000864 cur_fline = linenum;
865 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000866}
867
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000868static void open_file_and_read_lines(void)
869{
870 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000871 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000872 } else {
873 /* "less" with no arguments in argv[] */
874 /* For status line only */
875 filename = xstrdup(bb_msg_standard_input);
876 }
877 readpos = 0;
878 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000879 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000880 terminated = 1;
881 read_lines();
882}
883
884/* Reinitialize everything for a new file - free the memory and start over */
885static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000886{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000887 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000888
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000889 if (flines) {
890 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000891 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000892 free(flines);
893 flines = NULL;
894 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000895
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000896 max_fline = -1;
897 cur_fline = 0;
898 max_lineno = 0;
899 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200900#if ENABLE_FEATURE_LESS_ASK_TERMINAL
901 if (G.winsize_err)
902 printf("\033[999;999H" "\033[6n");
903#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000904 buffer_fill_and_print();
905}
906
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200907static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000908{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000909 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200910 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +0000911 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000912
Denis Vlasenko33196372008-02-23 01:25:38 +0000913 pfd[0].fd = STDIN_FILENO;
914 pfd[0].events = POLLIN;
915 pfd[1].fd = kbd_fd;
916 pfd[1].events = POLLIN;
917 again:
918 tcsetattr(kbd_fd, TCSANOW, &term_less);
919 /* NB: select/poll returns whenever read will not block. Therefore:
920 * if eof is reached, select/poll will return immediately
921 * because read will immediately return 0 bytes.
922 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000923 * (switch fd into O_NONBLOCK'ed mode to avoid it)
924 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000925 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000926 /* Are we interested in stdin? */
927//TODO: reuse code for determining this
928 if (!(option_mask32 & FLAG_S)
929 ? !(max_fline > cur_fline + max_displayed_line)
930 : !(max_fline >= cur_fline
931 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000932 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000933 if (eof_error > 0) /* did NOT reach eof yet */
934 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000935 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000936 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000937 if (less_gets_pos >= 0)
938 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100939 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000940
Denys Vlasenko020f4062009-05-17 16:44:54 +0200941 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000942#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000943 while (1) {
944 int r;
945 /* NB: SIGWINCH interrupts poll() */
946 r = poll(pfd + rd, 2 - rd, -1);
947 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
948 return '\\'; /* anything which has no defined function */
949 if (r) break;
950 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000951#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000952 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000953#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000954 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000955
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000956 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
957 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200958 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
959 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +0000960 if (errno == EAGAIN) {
961 /* No keyboard input available. Since poll() did return,
962 * we should have input on stdin */
963 read_lines();
964 buffer_fill_and_print();
965 goto again;
966 }
967 /* EOF/error (ssh session got killed etc) */
968 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000969 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000970 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200971 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000972}
973
Denys Vlasenko020f4062009-05-17 16:44:54 +0200974/* Grab a character from input without requiring the return key.
975 * May return KEYCODE_xxx values.
976 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200977static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000978{
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200979 int64_t key64;
980 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000981
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000982 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000983 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200984 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000985 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000986
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200987 /* Discard Ctrl-something chars.
988 * (checking only lower 32 bits is a size optimization:
989 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
990 */
991 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000992 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +0200993
994 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000995}
996
997static char* less_gets(int sz)
998{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000999 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001000 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001001 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001002
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001003 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001004 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001005 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001006 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001007 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001008 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001009 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001010 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001011 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001012 if (c == 0x7f)
1013 c = 8;
1014 if (c == 8 && i) {
1015 printf("\x8 \x8");
1016 i--;
1017 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001018 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001019 continue;
1020 if (i >= width - sz - 1)
1021 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001022 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001023 result[i++] = c;
1024 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001025 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001026}
1027
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001028static void examine_file(void)
1029{
Denis Vlasenko33196372008-02-23 01:25:38 +00001030 char *new_fname;
1031
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001032 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001033 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001034 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001035 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001036 err:
1037 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001038 return;
1039 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001040 if (access(new_fname, R_OK) != 0) {
1041 print_statusline("Cannot read this file");
1042 goto err;
1043 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001044 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001045 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001046 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001047 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001048 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001049 num_files++; */
1050 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001051 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001052 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001053}
1054
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001055/* This function changes the file currently being paged. direction can be one of the following:
1056 * -1: go back one file
1057 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001058 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001059static void change_file(int direction)
1060{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001061 if (current_file != ((direction > 0) ? num_files : 1)) {
1062 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001063 free(filename);
1064 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001065 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001066 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001067 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001068 }
1069}
1070
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001071static void remove_current_file(void)
1072{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001073 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001074
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001075 if (num_files < 2)
1076 return;
1077
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001078 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001079 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001080 for (i = 3; i <= num_files; i++)
1081 files[i - 2] = files[i - 1];
1082 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001083 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001084 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001085 for (i = 2; i <= num_files; i++)
1086 files[i - 2] = files[i - 1];
1087 num_files--;
1088 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001089 }
1090}
1091
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001092static void colon_process(void)
1093{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001094 int keypress;
1095
1096 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001097 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001098
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001099 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001100 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001101 case 'd':
1102 remove_current_file();
1103 break;
1104 case 'e':
1105 examine_file();
1106 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001107#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001108 case 'f':
1109 m_status_print();
1110 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001111#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001112 case 'n':
1113 change_file(1);
1114 break;
1115 case 'p':
1116 change_file(-1);
1117 break;
1118 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001119 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001120 break;
1121 case 'x':
1122 change_file(0);
1123 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001124 }
1125}
1126
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001127#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001128static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001129{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001130 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001131 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001132 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001133 match = 0;
1134 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001135}
1136
Rob Landleya2e98042006-04-18 01:53:41 +00001137static void goto_match(int match)
1138{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001139 if (!pattern_valid)
1140 return;
1141 if (match < 0)
1142 match = 0;
1143 /* Try to find next match if eof isn't reached yet */
1144 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001145 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001146 read_lines();
1147 }
1148 if (num_matches) {
1149 normalize_match_pos(match);
1150 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001151 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001152 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001153 }
Rob Landleya2e98042006-04-18 01:53:41 +00001154}
1155
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001156static void fill_match_lines(unsigned pos)
1157{
1158 if (!pattern_valid)
1159 return;
1160 /* Run the regex on each line of the current file */
1161 while (pos <= max_fline) {
1162 /* If this line matches */
1163 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1164 /* and we didn't match it last time */
1165 && !(num_matches && match_lines[num_matches-1] == pos)
1166 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001167 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001168 match_lines[num_matches++] = pos;
1169 }
1170 pos++;
1171 }
1172}
1173
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001174static void regex_process(void)
1175{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001176 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001177
1178 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001179 free(match_lines);
1180 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001181 match_pos = 0;
1182 num_matches = 0;
1183 if (pattern_valid) {
1184 regfree(&pattern);
1185 pattern_valid = 0;
1186 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001187
Rob Landleya2e98042006-04-18 01:53:41 +00001188 /* Get the uncompiled regular expression from the user */
1189 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001190 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001191 uncomp_regex = less_gets(1);
1192 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001193 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001194 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001195 return;
1196 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001197
Rob Landleya2e98042006-04-18 01:53:41 +00001198 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001199 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001200 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001201 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001202 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001203 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001204 free(err);
1205 return;
1206 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001207
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001208 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001209 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001210 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001211 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001212 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001213 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001214 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001215 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001216 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1217 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001218
1219 /* It's possible that no matches are found yet.
1220 * goto_match() will read input looking for match,
1221 * if needed */
1222 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001223}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001224#endif
1225
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001226static void number_process(int first_digit)
1227{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001228 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001229 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001230 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001231 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001232
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001233 num_input[0] = first_digit;
1234
1235 /* Clear the current line, print a prompt, and then print the digit */
1236 clear_line();
1237 printf(":%c", first_digit);
1238
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001239 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001240 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001241 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001242 keypress = less_getch(i + 1);
1243 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001244 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001245 num_input[i] = keypress;
1246 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001247 i++;
1248 }
1249
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001250 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001251 num = bb_strtou(num_input, NULL, 10);
1252 /* on format error, num == -1 */
1253 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001254 buffer_print();
1255 return;
1256 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001257
1258 /* We now know the number and the letter entered, so we process them */
1259 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001260 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001261 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001262 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001263 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001264 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001265 break;
1266 case 'g': case '<': case 'G': case '>':
1267 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001268 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001269 buffer_line(num - 1);
1270 break;
1271 case 'p': case '%':
1272 num = num * (max_fline / 100); /* + max_fline / 2; */
1273 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001274 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001275 buffer_line(num);
1276 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001277#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001278 case 'n':
1279 goto_match(match_pos + num);
1280 break;
1281 case '/':
1282 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1283 regex_process();
1284 break;
1285 case '?':
1286 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1287 regex_process();
1288 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001289#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001290 }
1291}
1292
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001293#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001294static void flag_change(void)
1295{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001296 int keypress;
1297
1298 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001299 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001300 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001301
1302 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001303 case 'M':
1304 option_mask32 ^= FLAG_M;
1305 break;
1306 case 'm':
1307 option_mask32 ^= FLAG_m;
1308 break;
1309 case 'E':
1310 option_mask32 ^= FLAG_E;
1311 break;
1312 case '~':
1313 option_mask32 ^= FLAG_TILDE;
1314 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001315 case 'S':
1316 option_mask32 ^= FLAG_S;
1317 buffer_fill_and_print();
1318 break;
1319#if ENABLE_FEATURE_LESS_LINENUMS
1320 case 'N':
1321 option_mask32 ^= FLAG_N;
1322 re_wrap();
1323 buffer_fill_and_print();
1324 break;
1325#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001326 }
1327}
1328
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001329#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001330static void show_flag_status(void)
1331{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001332 int keypress;
1333 int flag_val;
1334
1335 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001336 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001337 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001338
1339 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001340 case 'M':
1341 flag_val = option_mask32 & FLAG_M;
1342 break;
1343 case 'm':
1344 flag_val = option_mask32 & FLAG_m;
1345 break;
1346 case '~':
1347 flag_val = option_mask32 & FLAG_TILDE;
1348 break;
1349 case 'N':
1350 flag_val = option_mask32 & FLAG_N;
1351 break;
1352 case 'E':
1353 flag_val = option_mask32 & FLAG_E;
1354 break;
1355 default:
1356 flag_val = 0;
1357 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001358 }
1359
1360 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001361 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001362}
1363#endif
1364
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001365#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1366
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001367static void save_input_to_file(void)
1368{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001369 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001370 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001371 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001372 FILE *fp;
1373
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001374 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001375 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001376 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001377 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001378 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001379 msg = "Error opening log file";
1380 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001381 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001382 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001383 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001384 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001385 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001386 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001387 ret:
1388 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001389 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001390}
1391
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001392#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001393static void add_mark(void)
1394{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001395 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001396
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001397 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001398 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001399
1400 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001401 /* If we exceed 15 marks, start overwriting previous ones */
1402 if (num_marks == 14)
1403 num_marks = 0;
1404
1405 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001406 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001407 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001408 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001409 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001410 }
1411}
1412
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001413static void goto_mark(void)
1414{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001415 int letter;
1416 int i;
1417
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001418 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001419 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001420 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001421
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001422 if (isalpha(letter)) {
1423 for (i = 0; i <= num_marks; i++)
1424 if (letter == mark_lines[i][0]) {
1425 buffer_line(mark_lines[i][1]);
1426 break;
1427 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001428 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001429 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001430 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001431 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001432}
1433#endif
1434
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001435#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001436static char opp_bracket(char bracket)
1437{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001438 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001439 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1440 bracket++;
1441 case '(': /* ')' == '(' + 1 */
1442 bracket++;
1443 break;
1444 case '}': case ']':
1445 bracket--;
1446 case ')':
1447 bracket--;
1448 break;
1449 };
1450 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001451}
1452
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001453static void match_right_bracket(char bracket)
1454{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001455 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001456
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001457 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001458 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001459 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001460 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001461 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001462 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001463 if (strchr(flines[i], bracket) != NULL) {
1464 buffer_line(i);
1465 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001466 }
1467 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001468 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001469}
1470
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001471static void match_left_bracket(char bracket)
1472{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001473 int i;
1474
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001475 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001476 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001477 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001478 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001479
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001480 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001481 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001482 if (strchr(flines[i], bracket) != NULL) {
1483 buffer_line(i);
1484 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001485 }
1486 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001487 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001488}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001489#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001490
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001491static void keypress_process(int keypress)
1492{
Rob Landley9200e792005-09-15 19:26:59 +00001493 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001494 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001495 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001496 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001497 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001498 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001499 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001500 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001501 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001502 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001503 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001504 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001505 break;
1506 case 'd':
1507 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001508 break;
1509 case 'u':
1510 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001511 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001512 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001513 buffer_line(0);
1514 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001515 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001516 cur_fline = MAXLINES;
1517 read_lines();
1518 buffer_line(cur_fline);
1519 break;
1520 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001521 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001522 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001523#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001524 case 'm':
1525 add_mark();
1526 buffer_print();
1527 break;
1528 case '\'':
1529 goto_mark();
1530 buffer_print();
1531 break;
Rob Landley9200e792005-09-15 19:26:59 +00001532#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001533 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001534 /* TODO: (1) also bind ^R, ^L to this?
1535 * (2) re-measure window size?
1536 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001537 buffer_print();
1538 break;
1539 /*case 'R':
1540 full_repaint();
1541 break;*/
1542 case 's':
1543 save_input_to_file();
1544 break;
1545 case 'E':
1546 examine_file();
1547 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001548#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001549 case '=':
1550 m_status_print();
1551 break;
Rob Landley9200e792005-09-15 19:26:59 +00001552#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001553#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001554 case '/':
1555 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1556 regex_process();
1557 break;
1558 case 'n':
1559 goto_match(match_pos + 1);
1560 break;
1561 case 'N':
1562 goto_match(match_pos - 1);
1563 break;
1564 case '?':
1565 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1566 regex_process();
1567 break;
Rob Landley9200e792005-09-15 19:26:59 +00001568#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001569#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001570 case '-':
1571 flag_change();
1572 buffer_print();
1573 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001574#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001575 case '_':
1576 show_flag_status();
1577 break;
Rob Landley9200e792005-09-15 19:26:59 +00001578#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001579#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001580#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001581 case '{': case '(': case '[':
1582 match_right_bracket(keypress);
1583 break;
1584 case '}': case ')': case ']':
1585 match_left_bracket(keypress);
1586 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001587#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001588 case ':':
1589 colon_process();
1590 break;
Rob Landley9200e792005-09-15 19:26:59 +00001591 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001592
Rob Landley9200e792005-09-15 19:26:59 +00001593 if (isdigit(keypress))
1594 number_process(keypress);
1595}
1596
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001597static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001598{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001599 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001600}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001601
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001602#if ENABLE_FEATURE_LESS_WINCH
1603static void sigwinch_handler(int sig UNUSED_PARAM)
1604{
1605 winch_counter++;
1606}
1607#endif
1608
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001609int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001610int less_main(int argc, char **argv)
1611{
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001612 INIT_G();
1613
Denis Vlasenko95b30712006-12-31 19:23:31 +00001614 /* TODO: -x: do not interpret backspace, -xx: tab also */
1615 /* -xxx: newline also */
1616 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001617 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
Rob Landley9200e792005-09-15 19:26:59 +00001618 argc -= optind;
1619 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001620 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001621 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001622
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001623 /* Another popular pager, most, detects when stdout
1624 * is not a tty and turns into cat. This makes sense. */
1625 if (!isatty(STDOUT_FILENO))
1626 return bb_cat(argv);
1627
Rob Landley9200e792005-09-15 19:26:59 +00001628 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001629 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001630 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001631 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001632 bb_show_usage();
1633 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001634 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001635 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001636 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001637
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001638 if (option_mask32 & FLAG_TILDE)
1639 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001640
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001641 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1642 if (kbd_fd < 0)
1643 return bb_cat(argv);
1644 ndelay_on(kbd_fd);
1645
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001646 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001647 term_less = term_orig;
1648 term_less.c_lflag &= ~(ICANON | ECHO);
1649 term_less.c_iflag &= ~(IXON | ICRNL);
1650 /*term_less.c_oflag &= ~ONLCR;*/
1651 term_less.c_cc[VMIN] = 1;
1652 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001653
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001654 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 +00001655 /* 20: two tabstops + 4 */
1656 if (width < 20 || max_displayed_line < 3)
1657 return bb_cat(argv);
1658 max_displayed_line -= 2;
1659
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001660 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001661 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001662#if ENABLE_FEATURE_LESS_WINCH
1663 signal(SIGWINCH, sigwinch_handler);
1664#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001665
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001666 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001667 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001668 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001669 int64_t keypress;
1670
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001671#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001672 while (WINCH_COUNTER) {
1673 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001674 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001675 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1676 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001677 /* 20: two tabstops + 4 */
1678 if (width < 20)
1679 width = 20;
1680 if (max_displayed_line < 3)
1681 max_displayed_line = 3;
1682 max_displayed_line -= 2;
1683 free(buffer);
1684 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001685 /* Avoid re-wrap and/or redraw if we already know
1686 * we need to do it again. These ops are expensive */
1687 if (WINCH_COUNTER)
1688 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001689 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001690 if (WINCH_COUNTER)
1691 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001692 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001693 /* This took some time. Loop back and check,
1694 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001695 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001696 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001697# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1698 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1699 uint32_t rc = (keypress >> 32);
1700 width = (rc & 0x7fff);
1701 max_displayed_line = ((rc >> 16) & 0x7fff);
1702 goto got_size;
1703 }
1704# endif
1705#else
1706 keypress = less_getch(-1); /* -1: do not position cursor */
1707#endif
Rob Landley9200e792005-09-15 19:26:59 +00001708 keypress_process(keypress);
1709 }
1710}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001711
1712/*
1713Help text of less version 418 is below.
1714If you are implementing something, keeping
1715key and/or command line switch compatibility is a good idea:
1716
1717
1718 SUMMARY OF LESS COMMANDS
1719
1720 Commands marked with * may be preceded by a number, N.
1721 Notes in parentheses indicate the behavior if N is given.
1722 h H Display this help.
1723 q :q Q :Q ZZ Exit.
1724 ---------------------------------------------------------------------------
1725 MOVING
1726 e ^E j ^N CR * Forward one line (or N lines).
1727 y ^Y k ^K ^P * Backward one line (or N lines).
1728 f ^F ^V SPACE * Forward one window (or N lines).
1729 b ^B ESC-v * Backward one window (or N lines).
1730 z * Forward one window (and set window to N).
1731 w * Backward one window (and set window to N).
1732 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1733 d ^D * Forward one half-window (and set half-window to N).
1734 u ^U * Backward one half-window (and set half-window to N).
1735 ESC-) RightArrow * Left one half screen width (or N positions).
1736 ESC-( LeftArrow * Right one half screen width (or N positions).
1737 F Forward forever; like "tail -f".
1738 r ^R ^L Repaint screen.
1739 R Repaint screen, discarding buffered input.
1740 ---------------------------------------------------
1741 Default "window" is the screen height.
1742 Default "half-window" is half of the screen height.
1743 ---------------------------------------------------------------------------
1744 SEARCHING
1745 /pattern * Search forward for (N-th) matching line.
1746 ?pattern * Search backward for (N-th) matching line.
1747 n * Repeat previous search (for N-th occurrence).
1748 N * Repeat previous search in reverse direction.
1749 ESC-n * Repeat previous search, spanning files.
1750 ESC-N * Repeat previous search, reverse dir. & spanning files.
1751 ESC-u Undo (toggle) search highlighting.
1752 ---------------------------------------------------
1753 Search patterns may be modified by one or more of:
1754 ^N or ! Search for NON-matching lines.
1755 ^E or * Search multiple files (pass thru END OF FILE).
1756 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1757 ^K Highlight matches, but don't move (KEEP position).
1758 ^R Don't use REGULAR EXPRESSIONS.
1759 ---------------------------------------------------------------------------
1760 JUMPING
1761 g < ESC-< * Go to first line in file (or line N).
1762 G > ESC-> * Go to last line in file (or line N).
1763 p % * Go to beginning of file (or N percent into file).
1764 t * Go to the (N-th) next tag.
1765 T * Go to the (N-th) previous tag.
1766 { ( [ * Find close bracket } ) ].
1767 } ) ] * Find open bracket { ( [.
1768 ESC-^F <c1> <c2> * Find close bracket <c2>.
1769 ESC-^B <c1> <c2> * Find open bracket <c1>
1770 ---------------------------------------------------
1771 Each "find close bracket" command goes forward to the close bracket
1772 matching the (N-th) open bracket in the top line.
1773 Each "find open bracket" command goes backward to the open bracket
1774 matching the (N-th) close bracket in the bottom line.
1775 m<letter> Mark the current position with <letter>.
1776 '<letter> Go to a previously marked position.
1777 '' Go to the previous position.
1778 ^X^X Same as '.
1779 ---------------------------------------------------
1780 A mark is any upper-case or lower-case letter.
1781 Certain marks are predefined:
1782 ^ means beginning of the file
1783 $ means end of the file
1784 ---------------------------------------------------------------------------
1785 CHANGING FILES
1786 :e [file] Examine a new file.
1787 ^X^V Same as :e.
1788 :n * Examine the (N-th) next file from the command line.
1789 :p * Examine the (N-th) previous file from the command line.
1790 :x * Examine the first (or N-th) file from the command line.
1791 :d Delete the current file from the command line list.
1792 = ^G :f Print current file name.
1793 ---------------------------------------------------------------------------
1794 MISCELLANEOUS COMMANDS
1795 -<flag> Toggle a command line option [see OPTIONS below].
1796 --<name> Toggle a command line option, by name.
1797 _<flag> Display the setting of a command line option.
1798 __<name> Display the setting of an option, by name.
1799 +cmd Execute the less cmd each time a new file is examined.
1800 !command Execute the shell command with $SHELL.
1801 |Xcommand Pipe file between current pos & mark X to shell command.
1802 v Edit the current file with $VISUAL or $EDITOR.
1803 V Print version number of "less".
1804 ---------------------------------------------------------------------------
1805 OPTIONS
1806 Most options may be changed either on the command line,
1807 or from within less by using the - or -- command.
1808 Options may be given in one of two forms: either a single
1809 character preceded by a -, or a name preceeded by --.
1810 -? ........ --help
1811 Display help (from command line).
1812 -a ........ --search-skip-screen
1813 Forward search skips current screen.
1814 -b [N] .... --buffers=[N]
1815 Number of buffers.
1816 -B ........ --auto-buffers
1817 Don't automatically allocate buffers for pipes.
1818 -c ........ --clear-screen
1819 Repaint by clearing rather than scrolling.
1820 -d ........ --dumb
1821 Dumb terminal.
1822 -D [xn.n] . --color=xn.n
1823 Set screen colors. (MS-DOS only)
1824 -e -E .... --quit-at-eof --QUIT-AT-EOF
1825 Quit at end of file.
1826 -f ........ --force
1827 Force open non-regular files.
1828 -F ........ --quit-if-one-screen
1829 Quit if entire file fits on first screen.
1830 -g ........ --hilite-search
1831 Highlight only last match for searches.
1832 -G ........ --HILITE-SEARCH
1833 Don't highlight any matches for searches.
1834 -h [N] .... --max-back-scroll=[N]
1835 Backward scroll limit.
1836 -i ........ --ignore-case
1837 Ignore case in searches that do not contain uppercase.
1838 -I ........ --IGNORE-CASE
1839 Ignore case in all searches.
1840 -j [N] .... --jump-target=[N]
1841 Screen position of target lines.
1842 -J ........ --status-column
1843 Display a status column at left edge of screen.
1844 -k [file] . --lesskey-file=[file]
1845 Use a lesskey file.
1846 -L ........ --no-lessopen
1847 Ignore the LESSOPEN environment variable.
1848 -m -M .... --long-prompt --LONG-PROMPT
1849 Set prompt style.
1850 -n -N .... --line-numbers --LINE-NUMBERS
1851 Don't use line numbers.
1852 -o [file] . --log-file=[file]
1853 Copy to log file (standard input only).
1854 -O [file] . --LOG-FILE=[file]
1855 Copy to log file (unconditionally overwrite).
1856 -p [pattern] --pattern=[pattern]
1857 Start at pattern (from command line).
1858 -P [prompt] --prompt=[prompt]
1859 Define new prompt.
1860 -q -Q .... --quiet --QUIET --silent --SILENT
1861 Quiet the terminal bell.
1862 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1863 Output "raw" control characters.
1864 -s ........ --squeeze-blank-lines
1865 Squeeze multiple blank lines.
1866 -S ........ --chop-long-lines
1867 Chop long lines.
1868 -t [tag] .. --tag=[tag]
1869 Find a tag.
1870 -T [tagsfile] --tag-file=[tagsfile]
1871 Use an alternate tags file.
1872 -u -U .... --underline-special --UNDERLINE-SPECIAL
1873 Change handling of backspaces.
1874 -V ........ --version
1875 Display the version number of "less".
1876 -w ........ --hilite-unread
1877 Highlight first new line after forward-screen.
1878 -W ........ --HILITE-UNREAD
1879 Highlight first new line after any forward movement.
1880 -x [N[,...]] --tabs=[N[,...]]
1881 Set tab stops.
1882 -X ........ --no-init
1883 Don't use termcap init/deinit strings.
1884 --no-keypad
1885 Don't use termcap keypad init/deinit strings.
1886 -y [N] .... --max-forw-scroll=[N]
1887 Forward scroll limit.
1888 -z [N] .... --window=[N]
1889 Set size of window.
1890 -" [c[c]] . --quotes=[c[c]]
1891 Set shell quote characters.
1892 -~ ........ --tilde
1893 Don't display tildes after end of file.
1894 -# [N] .... --shift=[N]
1895 Horizontal scroll amount (0 = one half screen width)
1896
1897 ---------------------------------------------------------------------------
1898 LINE EDITING
1899 These keys can be used to edit text being entered
1900 on the "command line" at the bottom of the screen.
1901 RightArrow ESC-l Move cursor right one character.
1902 LeftArrow ESC-h Move cursor left one character.
1903 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1904 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1905 HOME ESC-0 Move cursor to start of line.
1906 END ESC-$ Move cursor to end of line.
1907 BACKSPACE Delete char to left of cursor.
1908 DELETE ESC-x Delete char under cursor.
1909 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1910 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1911 CNTL-U ESC (MS-DOS only) Delete entire line.
1912 UpArrow ESC-k Retrieve previous command line.
1913 DownArrow ESC-j Retrieve next command line.
1914 TAB Complete filename & cycle.
1915 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1916 CNTL-L Complete filename, list all.
1917*/