blob: 60105f42bfe674d1999959b1fe7ac9815ade76f4 [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 Vlasenko53772862012-04-17 16:11:25 +0200101//usage: "[-E" IF_FEATURE_LESS_FLAGS("Mm") "Nh~I?] [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 Vlasenko53772862012-04-17 16:11:25 +0200105//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200106//usage: "\n -M,-m Display status line with line numbers"
107//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200108//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200109//usage: "\n -N Prefix line number to each line"
110//usage: "\n -I Ignore case in all searches"
Denys Vlasenko53772862012-04-17 16:11:25 +0200111//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200112
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200113#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000114
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000115#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000116#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000117#include "xregex.h"
118#endif
119
Marek Polacek7b181072010-10-28 21:34:56 +0200120
121#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000122/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200123#define HIGHLIGHT ESC"[7m"
124#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200125/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200126#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200127/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200128#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000129
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000130enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000131/* Absolute max of lines eaten */
132 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000133/* This many "after the end" lines we will show (at max) */
134 TILDES = 1,
135};
136
Rob Landley9200e792005-09-15 19:26:59 +0000137/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000138enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000139 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000140 FLAG_M = 1 << 1,
141 FLAG_m = 1 << 2,
142 FLAG_N = 1 << 3,
143 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000144 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000145 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000146/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000147 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
148};
Rob Landley9200e792005-09-15 19:26:59 +0000149
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000150#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000151enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000152#endif
153
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000154struct globals {
155 int cur_fline; /* signed */
156 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +0000157 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000158/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000159 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000160 unsigned max_fline;
161 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000162 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000163 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000164#if ENABLE_FEATURE_LESS_WINCH
165 unsigned winch_counter;
166#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000167 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000168 ssize_t readpos;
169 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000170 const char **buffer;
171 const char **flines;
172 const char *empty_line_marker;
173 unsigned num_files;
174 unsigned current_file;
175 char *filename;
176 char **files;
177#if ENABLE_FEATURE_LESS_MARKS
178 unsigned num_marks;
179 unsigned mark_lines[15][2];
180#endif
181#if ENABLE_FEATURE_LESS_REGEXP
182 unsigned *match_lines;
183 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000184 int wanted_match; /* signed! */
185 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000186 regex_t pattern;
187 smallint pattern_valid;
188#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200189#if ENABLE_FEATURE_LESS_ASK_TERMINAL
190 smallint winsize_err;
191#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000192 smallint terminated;
193 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000194 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000195};
196#define G (*ptr_to_globals)
197#define cur_fline (G.cur_fline )
198#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000199#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000200#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000201#define max_fline (G.max_fline )
202#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000203#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000204#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000205#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000206/* This one is 100% not cached by compiler on read access */
207#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000208#define eof_error (G.eof_error )
209#define readpos (G.readpos )
210#define readeof (G.readeof )
211#define buffer (G.buffer )
212#define flines (G.flines )
213#define empty_line_marker (G.empty_line_marker )
214#define num_files (G.num_files )
215#define current_file (G.current_file )
216#define filename (G.filename )
217#define files (G.files )
218#define num_marks (G.num_marks )
219#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000220#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000221#define match_lines (G.match_lines )
222#define match_pos (G.match_pos )
223#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000224#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000225#define pattern (G.pattern )
226#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000227#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000228#define terminated (G.terminated )
229#define term_orig (G.term_orig )
230#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000231#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000232#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000233 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
234 less_gets_pos = -1; \
235 empty_line_marker = "~"; \
236 num_files = 1; \
237 current_file = 1; \
238 eof_error = 1; \
239 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000240 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000241} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000242
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000243/* flines[] are lines read from stdin, each in malloc'ed buffer.
244 * Line numbers are stored as uint32_t prepended to each line.
245 * Pointer is adjusted so that flines[i] points directly past
246 * line number. Accesor: */
247#define MEMPTR(p) ((char*)(p) - 4)
248#define LINENO(p) (*(uint32_t*)((p) - 4))
249
250
Rob Landley9200e792005-09-15 19:26:59 +0000251/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000252static void set_tty_cooked(void)
253{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100254 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000255 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000256}
257
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000258/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000259 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000260static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000261{
Marek Polacek7b181072010-10-28 21:34:56 +0200262 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000263}
264
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000265static void clear_line(void)
266{
Marek Polacek7b181072010-10-28 21:34:56 +0200267 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000268}
269
270static void print_hilite(const char *str)
271{
272 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000273}
274
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000275static void print_statusline(const char *str)
276{
277 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000278 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000279}
280
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000281/* Exit the program gracefully */
282static void less_exit(int code)
283{
284 set_tty_cooked();
285 clear_line();
286 if (code < 0)
287 kill_myself_with_sig(- code); /* does not return */
288 exit(code);
289}
290
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000291#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
292 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000293static void re_wrap(void)
294{
295 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000296 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000297 int src_idx;
298 int dst_idx;
299 int new_cur_fline = 0;
300 uint32_t lineno;
301 char linebuf[w + 1];
302 const char **old_flines = flines;
303 const char *s;
304 char **new_flines = NULL;
305 char *d;
306
307 if (option_mask32 & FLAG_N)
308 w -= 8;
309
310 src_idx = 0;
311 dst_idx = 0;
312 s = old_flines[0];
313 lineno = LINENO(s);
314 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000315 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000316 while (1) {
317 *d = *s;
318 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000319 new_line_pos++;
320 if (*d == '\t') /* tab */
321 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000322 s++;
323 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000324 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000325 int sz;
326 /* new line is full, create next one */
327 *d = '\0';
328 next_new:
329 sz = (d - linebuf) + 1; /* + 1: NUL */
330 d = ((char*)xmalloc(sz + 4)) + 4;
331 LINENO(d) = lineno;
332 memcpy(d, linebuf, sz);
333 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
334 new_flines[dst_idx] = d;
335 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000336 if (new_line_pos < w) {
337 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000338 if (src_idx > max_fline)
339 break;
340 lineno = LINENO(s);
341 }
342 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000343 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000344 }
345 continue;
346 }
347 /* *d == NUL: old line ended, go to next old one */
348 free(MEMPTR(old_flines[src_idx]));
349 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000350 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000351 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000352 src_idx++;
353 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000354 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000355 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000356 s = old_flines[src_idx];
357 if (lineno != LINENO(s)) {
358 /* this is not a continuation line!
359 * create next _new_ line too */
360 goto next_new;
361 }
362 }
363
364 free(old_flines);
365 flines = (const char **)new_flines;
366
367 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000368 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000369 cur_fline = new_cur_fline;
370 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000371#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000372 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000373#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000374}
375#endif
376
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000377#if ENABLE_FEATURE_LESS_REGEXP
378static void fill_match_lines(unsigned pos);
379#else
380#define fill_match_lines(pos) ((void)0)
381#endif
382
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000383/* Devilishly complex routine.
384 *
385 * Has to deal with EOF and EPIPE on input,
386 * with line wrapping, with last line not ending in '\n'
387 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000388 * It reads input again if last time we got an EOF (thus supporting
389 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000390 *
391 * Variables used:
392 * flines[] - array of lines already read. Linewrap may cause
393 * one source file line to occupy several flines[n].
394 * flines[max_fline] - last line, possibly incomplete.
395 * terminated - 1 if flines[max_fline] is 'terminated'
396 * (if there was '\n' [which isn't stored itself, we just remember
397 * that it was seen])
398 * max_lineno - last line's number, this one doesn't increment
399 * on line wrap, only on "real" new lines.
400 * readbuf[0..readeof-1] - small preliminary buffer.
401 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000402 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000403 * (takes into account tabs and backspaces)
404 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
405 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000406static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000407{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000408#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000409 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000410 int w = width;
411 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000412#if ENABLE_FEATURE_LESS_REGEXP
413 unsigned old_max_fline = max_fline;
414 time_t last_time = 0;
415 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
416#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000417
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000418 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000419 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000420
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000421 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000422
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000423 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000424 max_fline += last_terminated;
425 if (!last_terminated) {
426 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000427 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000428 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000429 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000430 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000431 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000432 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000433 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000434
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000435 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000436 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000437 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000438 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000439 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000440 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000441 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000442 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000443 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000444 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000445 readpos = 0;
446 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000447 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000448 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000449 }
450 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000451 /* backspace? [needed for manpages] */
452 /* <tab><bs> is (a) insane and */
453 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000454 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000455 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000456 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000457 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000458 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000459 continue;
460 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000461 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000462 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000463 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000464 new_last_line_pos += 7;
465 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000466 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000467 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000468 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000469 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000470 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000471 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000472 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000473 if (c == '\n') {
474 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000475 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000476 break;
477 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000478 /* NUL is substituted by '\n'! */
479 if (c == '\0') c = '\n';
480 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000481 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000482 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000483 /* Corner case: linewrap with only "" wrapping to next line */
484 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000485 if (!last_terminated && !current_line[0]) {
486 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000487 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000488 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000489 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000490 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000491 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000492 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000493
494 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
495 LINENO(flines[max_fline]) = max_lineno;
496 if (terminated)
497 max_lineno++;
498
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000499 if (max_fline >= MAXLINES) {
500 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000501 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000502 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000503 if (!(option_mask32 & FLAG_S)
504 ? (max_fline > cur_fline + max_displayed_line)
505 : (max_fline >= cur_fline
506 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
507 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000508#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000509 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000510#else
511 if (wanted_match >= num_matches) { /* goto_match called us */
512 fill_match_lines(old_max_fline);
513 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000514 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000515 if (wanted_match < num_matches)
516 break;
517#endif
518 }
519 if (eof_error <= 0) {
520 if (eof_error < 0) {
521 if (errno == EAGAIN) {
522 /* not yet eof or error, reset flag (or else
523 * we will hog CPU - select() will return
524 * immediately */
525 eof_error = 1;
526 } else {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200527 print_statusline(bb_msg_read_error);
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000528 }
529 }
530#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000531 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000532#else
533 if (wanted_match < num_matches) {
534 break;
535 } else { /* goto_match called us */
536 time_t t = time(NULL);
537 if (t != last_time) {
538 last_time = t;
539 if (--seconds_p1 == 0)
540 break;
541 }
542 sched_yield();
543 goto again0; /* go loop again (max 2 seconds) */
544 }
545#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000546 }
547 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000548 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000549 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000550 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000551 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000552 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000553#if ENABLE_FEATURE_LESS_REGEXP
554 /* prevent us from being stuck in search for a match */
555 wanted_match = -1;
556#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000557#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000558}
559
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000560#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000561/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000562 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000563static int calc_percent(void)
564{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000565 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000566 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000567}
568
Rob Landley9200e792005-09-15 19:26:59 +0000569/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000570static void m_status_print(void)
571{
Rob Landley9200e792005-09-15 19:26:59 +0000572 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000573
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200574 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000575 return;
576
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000577 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000578 printf(HIGHLIGHT"%s", filename);
579 if (num_files > 1)
580 printf(" (file %i of %i)", current_file, num_files);
581 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000582 cur_fline + 1, cur_fline + max_displayed_line + 1,
583 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000584 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000585 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000586 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000587 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000588 return;
Rob Landley9200e792005-09-15 19:26:59 +0000589 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000590 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000591 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000592}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000593#endif
594
Rob Landley9200e792005-09-15 19:26:59 +0000595/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000596static void status_print(void)
597{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000598 const char *p;
599
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200600 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000601 return;
602
Rob Landley9200e792005-09-15 19:26:59 +0000603 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000604#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000605 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000606 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000607 return;
Rob Landley9200e792005-09-15 19:26:59 +0000608 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000609 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000610#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000611
612 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000613 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000614 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000615 return;
616 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000617 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000618 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000619 p = filename;
620 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000621 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000622 p, current_file, num_files);
623 return;
624 }
625 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000626}
627
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000628static void cap_cur_fline(int nlines)
629{
630 int diff;
631 if (cur_fline < 0)
632 cur_fline = 0;
633 if (cur_fline + max_displayed_line > max_fline + TILDES) {
634 cur_fline -= nlines;
635 if (cur_fline < 0)
636 cur_fline = 0;
637 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
638 /* As the number of lines requested was too large, we just move
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100639 * to the end of the file */
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000640 if (diff > 0)
641 cur_fline += diff;
642 }
643}
644
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000645static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000646 /* NUL: never encountered; TAB: not converted */
647 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000648 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
649 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000650static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100651 /* why 40 instead of 4a below? - it is a replacement for '\n'.
652 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000653 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
654 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
655
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000656static void lineno_str(char *nbuf9, const char *line)
657{
658 nbuf9[0] = '\0';
659 if (option_mask32 & FLAG_N) {
660 const char *fmt;
661 unsigned n;
662
663 if (line == empty_line_marker) {
664 memset(nbuf9, ' ', 8);
665 nbuf9[8] = '\0';
666 return;
667 }
668 /* Width of 7 preserves tab spacing in the text */
669 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000670 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000671 if (n > 9999999) {
672 n %= 10000000;
673 fmt = "%07u ";
674 }
675 sprintf(nbuf9, fmt, n);
676 }
677}
678
679
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000680#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000681static void print_found(const char *line)
682{
683 int match_status;
684 int eflags;
685 char *growline;
686 regmatch_t match_structs;
687
688 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000689 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000690 const char *str = line;
691 char *p = buf;
692 size_t n;
693
694 while (*str) {
695 n = strcspn(str, controls);
696 if (n) {
697 if (!str[n]) break;
698 memcpy(p, str, n);
699 p += n;
700 str += n;
701 }
702 n = strspn(str, controls);
703 memset(p, '.', n);
704 p += n;
705 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000706 }
707 strcpy(p, str);
708
709 /* buf[] holds quarantined version of str */
710
711 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100712 * and NORMAL escape sequences placed around it.
713 * NB: we regex against line, but insert text
714 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000715 str = buf;
716 growline = NULL;
717 eflags = 0;
718 goto start;
719
720 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000721 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200722 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200723 (int)match_structs.rm_so, str,
724 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000725 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000726 free(growline);
727 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000728 str += match_structs.rm_eo;
729 line += match_structs.rm_eo;
730 eflags = REG_NOTBOL;
731 start:
732 /* Most of the time doesn't find the regex, optimize for that */
733 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000734 /* if even "" matches, treat it as "not a match" */
735 if (match_structs.rm_so >= match_structs.rm_eo)
736 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000737 }
738
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000739 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000740 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000741 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000742 return;
743 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000744 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000745 free(growline);
746}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000747#else
748void print_found(const char *line);
749#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000750
751static void print_ascii(const char *str)
752{
753 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000754 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000755 char *p;
756 size_t n;
757
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000758 lineno_str(nbuf9, str);
759 printf(CLEAR_2_EOL"%s", nbuf9);
760
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000761 while (*str) {
762 n = strcspn(str, controls);
763 if (n) {
764 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000765 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000766 str += n;
767 }
768 n = strspn(str, controls);
769 p = buf;
770 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000771 if (*str == 0x7f)
772 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000773 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000774 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
775 /* I want to know who committed this sin */
776 *p++ = '{';
777 else
778 *p++ = ctrlconv[(unsigned char)*str];
779 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000780 } while (--n);
781 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000782 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000783 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000784 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000785}
786
Rob Landley9200e792005-09-15 19:26:59 +0000787/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000788static void buffer_print(void)
789{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000790 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000791
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000792 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000793 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000794 if (pattern_valid)
795 print_found(buffer[i]);
796 else
797 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000798 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000799}
800
Denis Vlasenkod9083952008-10-24 10:42:21 +0000801static void buffer_fill_and_print(void)
802{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000803 unsigned i;
804#if ENABLE_FEATURE_LESS_DASHCMD
805 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000806
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000807 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000808 /* Go back to the beginning of this line */
809 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
810 fpos--;
811 }
812
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000813 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000814 while (i <= max_displayed_line && fpos <= max_fline) {
815 int lineno = LINENO(flines[fpos]);
816 buffer[i] = flines[fpos];
817 i++;
818 do {
819 fpos++;
820 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000821 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000822 && lineno == LINENO(flines[fpos])
823 );
824 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000825#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000826 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
827 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000828 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000829#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000830 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000831 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000832 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000833 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000834}
835
836/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000837static void buffer_down(int nlines)
838{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000839 cur_fline += nlines;
840 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000841 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000842 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000843}
844
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000845static void buffer_up(int nlines)
846{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000847 cur_fline -= nlines;
848 if (cur_fline < 0) cur_fline = 0;
849 read_lines();
850 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000851}
852
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000853static void buffer_line(int linenum)
854{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000855 if (linenum < 0)
856 linenum = 0;
857 cur_fline = linenum;
858 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000859 if (linenum + max_displayed_line > max_fline)
860 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000861 if (linenum < 0)
862 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000863 cur_fline = linenum;
864 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000865}
866
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000867static void open_file_and_read_lines(void)
868{
869 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000870 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000871 } else {
872 /* "less" with no arguments in argv[] */
873 /* For status line only */
874 filename = xstrdup(bb_msg_standard_input);
875 }
876 readpos = 0;
877 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000878 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000879 terminated = 1;
880 read_lines();
881}
882
883/* Reinitialize everything for a new file - free the memory and start over */
884static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000885{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000886 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000887
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000888 if (flines) {
889 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000890 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000891 free(flines);
892 flines = NULL;
893 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000894
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000895 max_fline = -1;
896 cur_fline = 0;
897 max_lineno = 0;
898 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200899#if ENABLE_FEATURE_LESS_ASK_TERMINAL
900 if (G.winsize_err)
901 printf("\033[999;999H" "\033[6n");
902#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000903 buffer_fill_and_print();
904}
905
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200906static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000907{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000908 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200909 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +0000910 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000911
Denis Vlasenko33196372008-02-23 01:25:38 +0000912 pfd[0].fd = STDIN_FILENO;
913 pfd[0].events = POLLIN;
914 pfd[1].fd = kbd_fd;
915 pfd[1].events = POLLIN;
916 again:
917 tcsetattr(kbd_fd, TCSANOW, &term_less);
918 /* NB: select/poll returns whenever read will not block. Therefore:
919 * if eof is reached, select/poll will return immediately
920 * because read will immediately return 0 bytes.
921 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000922 * (switch fd into O_NONBLOCK'ed mode to avoid it)
923 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000924 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000925 /* Are we interested in stdin? */
926//TODO: reuse code for determining this
927 if (!(option_mask32 & FLAG_S)
928 ? !(max_fline > cur_fline + max_displayed_line)
929 : !(max_fline >= cur_fline
930 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000931 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000932 if (eof_error > 0) /* did NOT reach eof yet */
933 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000934 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000935 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000936 if (less_gets_pos >= 0)
937 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100938 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000939
Denys Vlasenko020f4062009-05-17 16:44:54 +0200940 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000941#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000942 while (1) {
943 int r;
944 /* NB: SIGWINCH interrupts poll() */
945 r = poll(pfd + rd, 2 - rd, -1);
946 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
947 return '\\'; /* anything which has no defined function */
948 if (r) break;
949 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000950#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000951 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000952#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000953 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000954
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000955 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
956 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200957 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
958 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +0000959 if (errno == EAGAIN) {
960 /* No keyboard input available. Since poll() did return,
961 * we should have input on stdin */
962 read_lines();
963 buffer_fill_and_print();
964 goto again;
965 }
966 /* EOF/error (ssh session got killed etc) */
967 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000968 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000969 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200970 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000971}
972
Denys Vlasenko020f4062009-05-17 16:44:54 +0200973/* Grab a character from input without requiring the return key.
974 * May return KEYCODE_xxx values.
975 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200976static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000977{
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200978 int64_t key64;
979 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000980
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000981 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000982 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200983 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000984 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000985
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200986 /* Discard Ctrl-something chars.
987 * (checking only lower 32 bits is a size optimization:
988 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
989 */
990 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000991 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +0200992
993 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000994}
995
996static char* less_gets(int sz)
997{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000998 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000999 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001000 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001001
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001002 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001003 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001004 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001005 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001006 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001007 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001008 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001009 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001010 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001011 if (c == 0x7f)
1012 c = 8;
1013 if (c == 8 && i) {
1014 printf("\x8 \x8");
1015 i--;
1016 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001017 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001018 continue;
1019 if (i >= width - sz - 1)
1020 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001021 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001022 result[i++] = c;
1023 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001024 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001025}
1026
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001027static void examine_file(void)
1028{
Denis Vlasenko33196372008-02-23 01:25:38 +00001029 char *new_fname;
1030
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001031 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001032 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001033 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001034 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001035 err:
1036 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001037 return;
1038 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001039 if (access(new_fname, R_OK) != 0) {
1040 print_statusline("Cannot read this file");
1041 goto err;
1042 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001043 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001044 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001045 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001046 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001047 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001048 num_files++; */
1049 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001050 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001051 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001052}
1053
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001054/* This function changes the file currently being paged. direction can be one of the following:
1055 * -1: go back one file
1056 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001057 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001058static void change_file(int direction)
1059{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001060 if (current_file != ((direction > 0) ? num_files : 1)) {
1061 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001062 free(filename);
1063 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001064 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001065 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001066 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001067 }
1068}
1069
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001070static void remove_current_file(void)
1071{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001072 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001073
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001074 if (num_files < 2)
1075 return;
1076
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001077 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001078 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001079 for (i = 3; i <= num_files; i++)
1080 files[i - 2] = files[i - 1];
1081 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001082 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001083 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001084 for (i = 2; i <= num_files; i++)
1085 files[i - 2] = files[i - 1];
1086 num_files--;
1087 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001088 }
1089}
1090
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001091static void colon_process(void)
1092{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001093 int keypress;
1094
1095 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001096 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001097
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001098 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001099 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001100 case 'd':
1101 remove_current_file();
1102 break;
1103 case 'e':
1104 examine_file();
1105 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001106#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001107 case 'f':
1108 m_status_print();
1109 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001110#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001111 case 'n':
1112 change_file(1);
1113 break;
1114 case 'p':
1115 change_file(-1);
1116 break;
1117 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001118 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001119 break;
1120 case 'x':
1121 change_file(0);
1122 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001123 }
1124}
1125
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001126#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001127static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001128{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001129 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001130 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001131 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001132 match = 0;
1133 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001134}
1135
Rob Landleya2e98042006-04-18 01:53:41 +00001136static void goto_match(int match)
1137{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001138 if (!pattern_valid)
1139 return;
1140 if (match < 0)
1141 match = 0;
1142 /* Try to find next match if eof isn't reached yet */
1143 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001144 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001145 read_lines();
1146 }
1147 if (num_matches) {
1148 normalize_match_pos(match);
1149 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001150 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001151 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001152 }
Rob Landleya2e98042006-04-18 01:53:41 +00001153}
1154
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001155static void fill_match_lines(unsigned pos)
1156{
1157 if (!pattern_valid)
1158 return;
1159 /* Run the regex on each line of the current file */
1160 while (pos <= max_fline) {
1161 /* If this line matches */
1162 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1163 /* and we didn't match it last time */
1164 && !(num_matches && match_lines[num_matches-1] == pos)
1165 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001166 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001167 match_lines[num_matches++] = pos;
1168 }
1169 pos++;
1170 }
1171}
1172
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001173static void regex_process(void)
1174{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001175 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001176
1177 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001178 free(match_lines);
1179 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001180 match_pos = 0;
1181 num_matches = 0;
1182 if (pattern_valid) {
1183 regfree(&pattern);
1184 pattern_valid = 0;
1185 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001186
Rob Landleya2e98042006-04-18 01:53:41 +00001187 /* Get the uncompiled regular expression from the user */
1188 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001189 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001190 uncomp_regex = less_gets(1);
1191 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001192 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001193 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001194 return;
1195 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001196
Rob Landleya2e98042006-04-18 01:53:41 +00001197 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001198 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001199 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001200 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001201 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001202 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001203 free(err);
1204 return;
1205 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001206
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001207 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001208 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001209 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001210 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001211 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001212 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001213 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001214 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001215 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1216 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001217
1218 /* It's possible that no matches are found yet.
1219 * goto_match() will read input looking for match,
1220 * if needed */
1221 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001222}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001223#endif
1224
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001225static void number_process(int first_digit)
1226{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001227 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001228 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001229 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001230 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001231
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001232 num_input[0] = first_digit;
1233
1234 /* Clear the current line, print a prompt, and then print the digit */
1235 clear_line();
1236 printf(":%c", first_digit);
1237
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001238 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001239 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001240 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001241 keypress = less_getch(i + 1);
1242 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001243 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001244 num_input[i] = keypress;
1245 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001246 i++;
1247 }
1248
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001249 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001250 num = bb_strtou(num_input, NULL, 10);
1251 /* on format error, num == -1 */
1252 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001253 buffer_print();
1254 return;
1255 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001256
1257 /* We now know the number and the letter entered, so we process them */
1258 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001259 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001260 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001261 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001262 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001263 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001264 break;
1265 case 'g': case '<': case 'G': case '>':
1266 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001267 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001268 buffer_line(num - 1);
1269 break;
1270 case 'p': case '%':
1271 num = num * (max_fline / 100); /* + max_fline / 2; */
1272 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001273 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001274 buffer_line(num);
1275 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001276#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001277 case 'n':
1278 goto_match(match_pos + num);
1279 break;
1280 case '/':
1281 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1282 regex_process();
1283 break;
1284 case '?':
1285 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1286 regex_process();
1287 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001288#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001289 }
1290}
1291
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001292#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001293static void flag_change(void)
1294{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001295 int keypress;
1296
1297 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001298 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001299 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001300
1301 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001302 case 'M':
1303 option_mask32 ^= FLAG_M;
1304 break;
1305 case 'm':
1306 option_mask32 ^= FLAG_m;
1307 break;
1308 case 'E':
1309 option_mask32 ^= FLAG_E;
1310 break;
1311 case '~':
1312 option_mask32 ^= FLAG_TILDE;
1313 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001314 case 'S':
1315 option_mask32 ^= FLAG_S;
1316 buffer_fill_and_print();
1317 break;
1318#if ENABLE_FEATURE_LESS_LINENUMS
1319 case 'N':
1320 option_mask32 ^= FLAG_N;
1321 re_wrap();
1322 buffer_fill_and_print();
1323 break;
1324#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001325 }
1326}
1327
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001328#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001329static void show_flag_status(void)
1330{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001331 int keypress;
1332 int flag_val;
1333
1334 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001335 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001336 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001337
1338 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001339 case 'M':
1340 flag_val = option_mask32 & FLAG_M;
1341 break;
1342 case 'm':
1343 flag_val = option_mask32 & FLAG_m;
1344 break;
1345 case '~':
1346 flag_val = option_mask32 & FLAG_TILDE;
1347 break;
1348 case 'N':
1349 flag_val = option_mask32 & FLAG_N;
1350 break;
1351 case 'E':
1352 flag_val = option_mask32 & FLAG_E;
1353 break;
1354 default:
1355 flag_val = 0;
1356 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001357 }
1358
1359 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001360 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001361}
1362#endif
1363
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001364#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1365
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001366static void save_input_to_file(void)
1367{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001368 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001369 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001370 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001371 FILE *fp;
1372
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001373 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001374 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001375 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001376 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001377 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001378 msg = "Error opening log file";
1379 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001380 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001381 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001382 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001383 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001384 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001385 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001386 ret:
1387 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001388 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001389}
1390
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001391#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001392static void add_mark(void)
1393{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001394 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001395
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001396 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001397 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001398
1399 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001400 /* If we exceed 15 marks, start overwriting previous ones */
1401 if (num_marks == 14)
1402 num_marks = 0;
1403
1404 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001405 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001406 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001407 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001408 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001409 }
1410}
1411
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001412static void goto_mark(void)
1413{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001414 int letter;
1415 int i;
1416
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001417 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001418 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001419 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001420
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001421 if (isalpha(letter)) {
1422 for (i = 0; i <= num_marks; i++)
1423 if (letter == mark_lines[i][0]) {
1424 buffer_line(mark_lines[i][1]);
1425 break;
1426 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001427 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001428 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001429 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001430 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001431}
1432#endif
1433
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001434#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001435static char opp_bracket(char bracket)
1436{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001437 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001438 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1439 bracket++;
1440 case '(': /* ')' == '(' + 1 */
1441 bracket++;
1442 break;
1443 case '}': case ']':
1444 bracket--;
1445 case ')':
1446 bracket--;
1447 break;
1448 };
1449 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001450}
1451
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001452static void match_right_bracket(char bracket)
1453{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001454 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001455
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001456 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001457 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001458 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001459 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001460 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001461 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001462 if (strchr(flines[i], bracket) != NULL) {
1463 buffer_line(i);
1464 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001465 }
1466 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001467 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001468}
1469
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001470static void match_left_bracket(char bracket)
1471{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001472 int i;
1473
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001474 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001475 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001476 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001477 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001478
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001479 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001480 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001481 if (strchr(flines[i], bracket) != NULL) {
1482 buffer_line(i);
1483 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001484 }
1485 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001486 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001487}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001488#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001489
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001490static void keypress_process(int keypress)
1491{
Rob Landley9200e792005-09-15 19:26:59 +00001492 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001493 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001494 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001495 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001496 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001497 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001498 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001499 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001500 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001501 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001502 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001503 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001504 break;
1505 case 'd':
1506 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001507 break;
1508 case 'u':
1509 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001510 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001511 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001512 buffer_line(0);
1513 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001514 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001515 cur_fline = MAXLINES;
1516 read_lines();
1517 buffer_line(cur_fline);
1518 break;
1519 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001520 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001521 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001522#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001523 case 'm':
1524 add_mark();
1525 buffer_print();
1526 break;
1527 case '\'':
1528 goto_mark();
1529 buffer_print();
1530 break;
Rob Landley9200e792005-09-15 19:26:59 +00001531#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001532 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001533 /* TODO: (1) also bind ^R, ^L to this?
1534 * (2) re-measure window size?
1535 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001536 buffer_print();
1537 break;
1538 /*case 'R':
1539 full_repaint();
1540 break;*/
1541 case 's':
1542 save_input_to_file();
1543 break;
1544 case 'E':
1545 examine_file();
1546 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001547#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001548 case '=':
1549 m_status_print();
1550 break;
Rob Landley9200e792005-09-15 19:26:59 +00001551#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001552#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001553 case '/':
1554 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1555 regex_process();
1556 break;
1557 case 'n':
1558 goto_match(match_pos + 1);
1559 break;
1560 case 'N':
1561 goto_match(match_pos - 1);
1562 break;
1563 case '?':
1564 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1565 regex_process();
1566 break;
Rob Landley9200e792005-09-15 19:26:59 +00001567#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001568#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001569 case '-':
1570 flag_change();
1571 buffer_print();
1572 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001573#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001574 case '_':
1575 show_flag_status();
1576 break;
Rob Landley9200e792005-09-15 19:26:59 +00001577#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001578#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001579#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001580 case '{': case '(': case '[':
1581 match_right_bracket(keypress);
1582 break;
1583 case '}': case ')': case ']':
1584 match_left_bracket(keypress);
1585 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001586#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001587 case ':':
1588 colon_process();
1589 break;
Rob Landley9200e792005-09-15 19:26:59 +00001590 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001591
Rob Landley9200e792005-09-15 19:26:59 +00001592 if (isdigit(keypress))
1593 number_process(keypress);
1594}
1595
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001596static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001597{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001598 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001599}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001600
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001601#if ENABLE_FEATURE_LESS_WINCH
1602static void sigwinch_handler(int sig UNUSED_PARAM)
1603{
1604 winch_counter++;
1605}
1606#endif
1607
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001608int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001609int less_main(int argc, char **argv)
1610{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001611 char *tty_name;
1612 int tty_fd;
1613
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001614 INIT_G();
1615
Denis Vlasenko95b30712006-12-31 19:23:31 +00001616 /* TODO: -x: do not interpret backspace, -xx: tab also */
1617 /* -xxx: newline also */
1618 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001619 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
Rob Landley9200e792005-09-15 19:26:59 +00001620 argc -= optind;
1621 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001622 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001623 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001624
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001625 /* Another popular pager, most, detects when stdout
1626 * is not a tty and turns into cat. This makes sense. */
1627 if (!isatty(STDOUT_FILENO))
1628 return bb_cat(argv);
1629
Rob Landley9200e792005-09-15 19:26:59 +00001630 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001631 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001632 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001633 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001634 bb_show_usage();
1635 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001636 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001637 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001638 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001639
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001640 if (option_mask32 & FLAG_TILDE)
1641 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001642
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001643 /* Some versions of less can survive w/o controlling tty,
1644 * try to do the same. This also allows to specify an alternative
1645 * tty via "less 1<>TTY".
1646 * We don't try to use STDOUT_FILENO directly,
1647 * since we want to set this fd to non-blocking mode,
1648 * and not bother with restoring it on exit.
1649 */
1650 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1651 if (tty_name) {
1652 tty_fd = open(tty_name, O_RDONLY);
1653 free(tty_name);
1654 if (tty_fd < 0)
1655 goto try_ctty;
1656 } else {
1657 /* Try controlling tty */
1658 try_ctty:
1659 tty_fd = open(CURRENT_TTY, O_RDONLY);
1660 if (tty_fd < 0)
1661 return bb_cat(argv);
1662 }
1663 ndelay_on(tty_fd);
1664 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001665
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001666 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001667 term_less = term_orig;
1668 term_less.c_lflag &= ~(ICANON | ECHO);
1669 term_less.c_iflag &= ~(IXON | ICRNL);
1670 /*term_less.c_oflag &= ~ONLCR;*/
1671 term_less.c_cc[VMIN] = 1;
1672 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001673
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001674 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 +00001675 /* 20: two tabstops + 4 */
1676 if (width < 20 || max_displayed_line < 3)
1677 return bb_cat(argv);
1678 max_displayed_line -= 2;
1679
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001680 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001681 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001682#if ENABLE_FEATURE_LESS_WINCH
1683 signal(SIGWINCH, sigwinch_handler);
1684#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001685
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001686 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001687 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001688 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001689 int64_t keypress;
1690
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001691#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001692 while (WINCH_COUNTER) {
1693 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001694 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001695 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1696 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001697 /* 20: two tabstops + 4 */
1698 if (width < 20)
1699 width = 20;
1700 if (max_displayed_line < 3)
1701 max_displayed_line = 3;
1702 max_displayed_line -= 2;
1703 free(buffer);
1704 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001705 /* Avoid re-wrap and/or redraw if we already know
1706 * we need to do it again. These ops are expensive */
1707 if (WINCH_COUNTER)
1708 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001709 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001710 if (WINCH_COUNTER)
1711 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001712 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001713 /* This took some time. Loop back and check,
1714 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001715 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001716 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001717# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1718 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1719 uint32_t rc = (keypress >> 32);
1720 width = (rc & 0x7fff);
1721 max_displayed_line = ((rc >> 16) & 0x7fff);
1722 goto got_size;
1723 }
1724# endif
1725#else
1726 keypress = less_getch(-1); /* -1: do not position cursor */
1727#endif
Rob Landley9200e792005-09-15 19:26:59 +00001728 keypress_process(keypress);
1729 }
1730}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001731
1732/*
1733Help text of less version 418 is below.
1734If you are implementing something, keeping
1735key and/or command line switch compatibility is a good idea:
1736
1737
1738 SUMMARY OF LESS COMMANDS
1739
1740 Commands marked with * may be preceded by a number, N.
1741 Notes in parentheses indicate the behavior if N is given.
1742 h H Display this help.
1743 q :q Q :Q ZZ Exit.
1744 ---------------------------------------------------------------------------
1745 MOVING
1746 e ^E j ^N CR * Forward one line (or N lines).
1747 y ^Y k ^K ^P * Backward one line (or N lines).
1748 f ^F ^V SPACE * Forward one window (or N lines).
1749 b ^B ESC-v * Backward one window (or N lines).
1750 z * Forward one window (and set window to N).
1751 w * Backward one window (and set window to N).
1752 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1753 d ^D * Forward one half-window (and set half-window to N).
1754 u ^U * Backward one half-window (and set half-window to N).
1755 ESC-) RightArrow * Left one half screen width (or N positions).
1756 ESC-( LeftArrow * Right one half screen width (or N positions).
1757 F Forward forever; like "tail -f".
1758 r ^R ^L Repaint screen.
1759 R Repaint screen, discarding buffered input.
1760 ---------------------------------------------------
1761 Default "window" is the screen height.
1762 Default "half-window" is half of the screen height.
1763 ---------------------------------------------------------------------------
1764 SEARCHING
1765 /pattern * Search forward for (N-th) matching line.
1766 ?pattern * Search backward for (N-th) matching line.
1767 n * Repeat previous search (for N-th occurrence).
1768 N * Repeat previous search in reverse direction.
1769 ESC-n * Repeat previous search, spanning files.
1770 ESC-N * Repeat previous search, reverse dir. & spanning files.
1771 ESC-u Undo (toggle) search highlighting.
1772 ---------------------------------------------------
1773 Search patterns may be modified by one or more of:
1774 ^N or ! Search for NON-matching lines.
1775 ^E or * Search multiple files (pass thru END OF FILE).
1776 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1777 ^K Highlight matches, but don't move (KEEP position).
1778 ^R Don't use REGULAR EXPRESSIONS.
1779 ---------------------------------------------------------------------------
1780 JUMPING
1781 g < ESC-< * Go to first line in file (or line N).
1782 G > ESC-> * Go to last line in file (or line N).
1783 p % * Go to beginning of file (or N percent into file).
1784 t * Go to the (N-th) next tag.
1785 T * Go to the (N-th) previous tag.
1786 { ( [ * Find close bracket } ) ].
1787 } ) ] * Find open bracket { ( [.
1788 ESC-^F <c1> <c2> * Find close bracket <c2>.
1789 ESC-^B <c1> <c2> * Find open bracket <c1>
1790 ---------------------------------------------------
1791 Each "find close bracket" command goes forward to the close bracket
1792 matching the (N-th) open bracket in the top line.
1793 Each "find open bracket" command goes backward to the open bracket
1794 matching the (N-th) close bracket in the bottom line.
1795 m<letter> Mark the current position with <letter>.
1796 '<letter> Go to a previously marked position.
1797 '' Go to the previous position.
1798 ^X^X Same as '.
1799 ---------------------------------------------------
1800 A mark is any upper-case or lower-case letter.
1801 Certain marks are predefined:
1802 ^ means beginning of the file
1803 $ means end of the file
1804 ---------------------------------------------------------------------------
1805 CHANGING FILES
1806 :e [file] Examine a new file.
1807 ^X^V Same as :e.
1808 :n * Examine the (N-th) next file from the command line.
1809 :p * Examine the (N-th) previous file from the command line.
1810 :x * Examine the first (or N-th) file from the command line.
1811 :d Delete the current file from the command line list.
1812 = ^G :f Print current file name.
1813 ---------------------------------------------------------------------------
1814 MISCELLANEOUS COMMANDS
1815 -<flag> Toggle a command line option [see OPTIONS below].
1816 --<name> Toggle a command line option, by name.
1817 _<flag> Display the setting of a command line option.
1818 __<name> Display the setting of an option, by name.
1819 +cmd Execute the less cmd each time a new file is examined.
1820 !command Execute the shell command with $SHELL.
1821 |Xcommand Pipe file between current pos & mark X to shell command.
1822 v Edit the current file with $VISUAL or $EDITOR.
1823 V Print version number of "less".
1824 ---------------------------------------------------------------------------
1825 OPTIONS
1826 Most options may be changed either on the command line,
1827 or from within less by using the - or -- command.
1828 Options may be given in one of two forms: either a single
1829 character preceded by a -, or a name preceeded by --.
1830 -? ........ --help
1831 Display help (from command line).
1832 -a ........ --search-skip-screen
1833 Forward search skips current screen.
1834 -b [N] .... --buffers=[N]
1835 Number of buffers.
1836 -B ........ --auto-buffers
1837 Don't automatically allocate buffers for pipes.
1838 -c ........ --clear-screen
1839 Repaint by clearing rather than scrolling.
1840 -d ........ --dumb
1841 Dumb terminal.
1842 -D [xn.n] . --color=xn.n
1843 Set screen colors. (MS-DOS only)
1844 -e -E .... --quit-at-eof --QUIT-AT-EOF
1845 Quit at end of file.
1846 -f ........ --force
1847 Force open non-regular files.
1848 -F ........ --quit-if-one-screen
1849 Quit if entire file fits on first screen.
1850 -g ........ --hilite-search
1851 Highlight only last match for searches.
1852 -G ........ --HILITE-SEARCH
1853 Don't highlight any matches for searches.
1854 -h [N] .... --max-back-scroll=[N]
1855 Backward scroll limit.
1856 -i ........ --ignore-case
1857 Ignore case in searches that do not contain uppercase.
1858 -I ........ --IGNORE-CASE
1859 Ignore case in all searches.
1860 -j [N] .... --jump-target=[N]
1861 Screen position of target lines.
1862 -J ........ --status-column
1863 Display a status column at left edge of screen.
1864 -k [file] . --lesskey-file=[file]
1865 Use a lesskey file.
1866 -L ........ --no-lessopen
1867 Ignore the LESSOPEN environment variable.
1868 -m -M .... --long-prompt --LONG-PROMPT
1869 Set prompt style.
1870 -n -N .... --line-numbers --LINE-NUMBERS
1871 Don't use line numbers.
1872 -o [file] . --log-file=[file]
1873 Copy to log file (standard input only).
1874 -O [file] . --LOG-FILE=[file]
1875 Copy to log file (unconditionally overwrite).
1876 -p [pattern] --pattern=[pattern]
1877 Start at pattern (from command line).
1878 -P [prompt] --prompt=[prompt]
1879 Define new prompt.
1880 -q -Q .... --quiet --QUIET --silent --SILENT
1881 Quiet the terminal bell.
1882 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1883 Output "raw" control characters.
1884 -s ........ --squeeze-blank-lines
1885 Squeeze multiple blank lines.
1886 -S ........ --chop-long-lines
1887 Chop long lines.
1888 -t [tag] .. --tag=[tag]
1889 Find a tag.
1890 -T [tagsfile] --tag-file=[tagsfile]
1891 Use an alternate tags file.
1892 -u -U .... --underline-special --UNDERLINE-SPECIAL
1893 Change handling of backspaces.
1894 -V ........ --version
1895 Display the version number of "less".
1896 -w ........ --hilite-unread
1897 Highlight first new line after forward-screen.
1898 -W ........ --HILITE-UNREAD
1899 Highlight first new line after any forward movement.
1900 -x [N[,...]] --tabs=[N[,...]]
1901 Set tab stops.
1902 -X ........ --no-init
1903 Don't use termcap init/deinit strings.
1904 --no-keypad
1905 Don't use termcap keypad init/deinit strings.
1906 -y [N] .... --max-forw-scroll=[N]
1907 Forward scroll limit.
1908 -z [N] .... --window=[N]
1909 Set size of window.
1910 -" [c[c]] . --quotes=[c[c]]
1911 Set shell quote characters.
1912 -~ ........ --tilde
1913 Don't display tildes after end of file.
1914 -# [N] .... --shift=[N]
1915 Horizontal scroll amount (0 = one half screen width)
1916
1917 ---------------------------------------------------------------------------
1918 LINE EDITING
1919 These keys can be used to edit text being entered
1920 on the "command line" at the bottom of the screen.
1921 RightArrow ESC-l Move cursor right one character.
1922 LeftArrow ESC-h Move cursor left one character.
1923 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1924 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1925 HOME ESC-0 Move cursor to start of line.
1926 END ESC-$ Move cursor to end of line.
1927 BACKSPACE Delete char to left of cursor.
1928 DELETE ESC-x Delete char under cursor.
1929 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1930 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1931 CNTL-U ESC (MS-DOS only) Delete entire line.
1932 UpArrow ESC-k Retrieve previous command line.
1933 DownArrow ESC-j Retrieve next command line.
1934 TAB Complete filename & cycle.
1935 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1936 CNTL-L Complete filename, list all.
1937*/