blob: d84df469c008892c12a39e7b5ab9176f977d3656 [file] [log] [blame]
Rob Landley9200e792005-09-15 19:26:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini less implementation for busybox
4 *
Rob Landley9200e792005-09-15 19:26:59 +00005 * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysingerf284c762006-04-16 20:38:26 +00008 */
9
10/*
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000011 * TODO:
12 * - Add more regular expression support - search modifiers, certain matches, etc.
13 * - Add more complex bracket searching - currently, nested brackets are
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000014 * not considered.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000015 * - Add support for "F" as an input. This causes less to act in
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000016 * a similar way to tail -f.
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000017 * - Allow horizontal scrolling.
Rob Landley9200e792005-09-15 19:26:59 +000018 *
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000019 * Notes:
20 * - the inp file pointer is used so that keyboard input works after
Denis Vlasenko5a4f0992006-12-25 01:23:02 +000021 * redirected input has been read from stdin
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000022 */
Rob Landley9200e792005-09-15 19:26:59 +000023
Denys Vlasenko4e552a72011-07-25 15:18:20 +020024//config:config LESS
25//config: bool "less"
26//config: default y
27//config: help
28//config: 'less' is a pager, meaning that it displays text files. It possesses
29//config: a wide array of features, and is an improvement over 'more'.
30//config:
31//config:config FEATURE_LESS_MAXLINES
32//config: int "Max number of input lines less will try to eat"
33//config: default 9999999
34//config: depends on LESS
35//config:
36//config:config FEATURE_LESS_BRACKETS
37//config: bool "Enable bracket searching"
38//config: default y
39//config: depends on LESS
40//config: help
41//config: This option adds the capability to search for matching left and right
42//config: brackets, facilitating programming.
43//config:
44//config:config FEATURE_LESS_FLAGS
Denys Vlasenko53772862012-04-17 16:11:25 +020045//config: bool "Enable -m/-M"
Denys Vlasenko4e552a72011-07-25 15:18:20 +020046//config: default y
47//config: depends on LESS
48//config: help
Denys Vlasenko53772862012-04-17 16:11:25 +020049//config: The -M/-m flag enables a more sophisticated status line.
Denys Vlasenko4e552a72011-07-25 15:18:20 +020050//config:
51//config:config FEATURE_LESS_MARKS
52//config: bool "Enable marks"
53//config: default y
54//config: depends on LESS
55//config: help
56//config: Marks enable positions in a file to be stored for easy reference.
57//config:
58//config:config FEATURE_LESS_REGEXP
59//config: bool "Enable regular expressions"
60//config: default y
61//config: depends on LESS
62//config: help
63//config: Enable regular expressions, allowing complex file searches.
64//config:
65//config:config FEATURE_LESS_WINCH
66//config: bool "Enable automatic resizing on window size changes"
67//config: default y
68//config: depends on LESS
69//config: help
70//config: Makes less track window size changes.
71//config:
72//config:config FEATURE_LESS_ASK_TERMINAL
73//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
74//config: default y
75//config: depends on FEATURE_LESS_WINCH
76//config: help
77//config: Makes less track window size changes.
78//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
79//config: this option makes less perform a last-ditch effort to find it:
80//config: position cursor to 999,999 and ask terminal to report real
81//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
82//config:
83//config: This is not clean but helps a lot on serial lines and such.
84//config:
85//config:config FEATURE_LESS_DASHCMD
86//config: bool "Enable flag changes ('-' command)"
87//config: default y
88//config: depends on LESS
89//config: help
90//config: This enables the ability to change command-line flags within
91//config: less itself ('-' keyboard command).
92//config:
93//config:config FEATURE_LESS_LINENUMS
94//config: bool "Enable dynamic switching of line numbers"
95//config: default y
96//config: depends on FEATURE_LESS_DASHCMD
97//config: help
98//config: Enables "-N" command.
99
Pere Orga5bc8c002011-04-11 03:29:49 +0200100//usage:#define less_trivial_usage
Denys Vlasenko821e6432014-01-22 16:36:22 +0100101//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") "Nh~] [FILE]..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200102//usage:#define less_full_usage "\n\n"
103//usage: "View FILE (or stdin) one screenful at a time\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200104//usage: "\n -E Quit once the end of a file is reached"
Denys Vlasenko821e6432014-01-22 16:36:22 +0100105//usage: IF_FEATURE_LESS_REGEXP(
106//usage: "\n -I Ignore case in all searches"
107//usage: )
Denys Vlasenko53772862012-04-17 16:11:25 +0200108//usage: IF_FEATURE_LESS_FLAGS(
Pere Orga5bc8c002011-04-11 03:29:49 +0200109//usage: "\n -M,-m Display status line with line numbers"
110//usage: "\n and percentage through the file"
Denys Vlasenko53772862012-04-17 16:11:25 +0200111//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +0200112//usage: "\n -N Prefix line number to each line"
Denys Vlasenko53772862012-04-17 16:11:25 +0200113//usage: "\n -~ Suppress ~s displayed past EOF"
Pere Orga5bc8c002011-04-11 03:29:49 +0200114
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200115#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000116
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000117#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000118#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000119#include "xregex.h"
120#endif
121
Marek Polacek7b181072010-10-28 21:34:56 +0200122
123#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +0000124/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +0200125#define HIGHLIGHT ESC"[7m"
126#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200127/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +0200128#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200129/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +0200130#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +0000131
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000132enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000133/* Absolute max of lines eaten */
134 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000135/* This many "after the end" lines we will show (at max) */
136 TILDES = 1,
137};
138
Rob Landley9200e792005-09-15 19:26:59 +0000139/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000140enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000141 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000142 FLAG_M = 1 << 1,
143 FLAG_m = 1 << 2,
144 FLAG_N = 1 << 3,
145 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +0000146 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000147 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000148/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000149 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
150};
Rob Landley9200e792005-09-15 19:26:59 +0000151
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000152#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000153enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000154#endif
155
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000156struct globals {
157 int cur_fline; /* signed */
158 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +0000159 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000160/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000161 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000162 unsigned max_fline;
163 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000164 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000165 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000166#if ENABLE_FEATURE_LESS_WINCH
167 unsigned winch_counter;
168#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000169 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000170 ssize_t readpos;
171 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000172 const char **buffer;
173 const char **flines;
174 const char *empty_line_marker;
175 unsigned num_files;
176 unsigned current_file;
177 char *filename;
178 char **files;
179#if ENABLE_FEATURE_LESS_MARKS
180 unsigned num_marks;
181 unsigned mark_lines[15][2];
182#endif
183#if ENABLE_FEATURE_LESS_REGEXP
184 unsigned *match_lines;
185 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000186 int wanted_match; /* signed! */
187 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000188 regex_t pattern;
189 smallint pattern_valid;
190#endif
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200191#if ENABLE_FEATURE_LESS_ASK_TERMINAL
192 smallint winsize_err;
193#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000194 smallint terminated;
195 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000196 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000197};
198#define G (*ptr_to_globals)
199#define cur_fline (G.cur_fline )
200#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000201#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000202#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000203#define max_fline (G.max_fline )
204#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000205#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000206#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000207#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000208/* This one is 100% not cached by compiler on read access */
209#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000210#define eof_error (G.eof_error )
211#define readpos (G.readpos )
212#define readeof (G.readeof )
213#define buffer (G.buffer )
214#define flines (G.flines )
215#define empty_line_marker (G.empty_line_marker )
216#define num_files (G.num_files )
217#define current_file (G.current_file )
218#define filename (G.filename )
219#define files (G.files )
220#define num_marks (G.num_marks )
221#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000222#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000223#define match_lines (G.match_lines )
224#define match_pos (G.match_pos )
225#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000226#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000227#define pattern (G.pattern )
228#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000229#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000230#define terminated (G.terminated )
231#define term_orig (G.term_orig )
232#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000233#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000234#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000235 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
236 less_gets_pos = -1; \
237 empty_line_marker = "~"; \
238 num_files = 1; \
239 current_file = 1; \
240 eof_error = 1; \
241 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000242 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000243} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000244
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000245/* flines[] are lines read from stdin, each in malloc'ed buffer.
246 * Line numbers are stored as uint32_t prepended to each line.
247 * Pointer is adjusted so that flines[i] points directly past
248 * line number. Accesor: */
249#define MEMPTR(p) ((char*)(p) - 4)
250#define LINENO(p) (*(uint32_t*)((p) - 4))
251
252
Rob Landley9200e792005-09-15 19:26:59 +0000253/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000254static void set_tty_cooked(void)
255{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100256 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000257 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000258}
259
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000260/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000261 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000262static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000263{
Marek Polacek7b181072010-10-28 21:34:56 +0200264 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000265}
266
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000267static void clear_line(void)
268{
Marek Polacek7b181072010-10-28 21:34:56 +0200269 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000270}
271
272static void print_hilite(const char *str)
273{
274 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000275}
276
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000277static void print_statusline(const char *str)
278{
279 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000280 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000281}
282
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000283/* Exit the program gracefully */
284static void less_exit(int code)
285{
286 set_tty_cooked();
287 clear_line();
288 if (code < 0)
289 kill_myself_with_sig(- code); /* does not return */
290 exit(code);
291}
292
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000293#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
294 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000295static void re_wrap(void)
296{
297 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000298 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000299 int src_idx;
300 int dst_idx;
301 int new_cur_fline = 0;
302 uint32_t lineno;
303 char linebuf[w + 1];
304 const char **old_flines = flines;
305 const char *s;
306 char **new_flines = NULL;
307 char *d;
308
309 if (option_mask32 & FLAG_N)
310 w -= 8;
311
312 src_idx = 0;
313 dst_idx = 0;
314 s = old_flines[0];
315 lineno = LINENO(s);
316 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000317 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000318 while (1) {
319 *d = *s;
320 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000321 new_line_pos++;
322 if (*d == '\t') /* tab */
323 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000324 s++;
325 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000326 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000327 int sz;
328 /* new line is full, create next one */
329 *d = '\0';
330 next_new:
331 sz = (d - linebuf) + 1; /* + 1: NUL */
332 d = ((char*)xmalloc(sz + 4)) + 4;
333 LINENO(d) = lineno;
334 memcpy(d, linebuf, sz);
335 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
336 new_flines[dst_idx] = d;
337 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000338 if (new_line_pos < w) {
339 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000340 if (src_idx > max_fline)
341 break;
342 lineno = LINENO(s);
343 }
344 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000345 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000346 }
347 continue;
348 }
349 /* *d == NUL: old line ended, go to next old one */
350 free(MEMPTR(old_flines[src_idx]));
351 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000352 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000353 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000354 src_idx++;
355 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000356 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000357 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000358 s = old_flines[src_idx];
359 if (lineno != LINENO(s)) {
360 /* this is not a continuation line!
361 * create next _new_ line too */
362 goto next_new;
363 }
364 }
365
366 free(old_flines);
367 flines = (const char **)new_flines;
368
369 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000371 cur_fline = new_cur_fline;
372 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000373#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000374 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000375#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000376}
377#endif
378
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000379#if ENABLE_FEATURE_LESS_REGEXP
380static void fill_match_lines(unsigned pos);
381#else
382#define fill_match_lines(pos) ((void)0)
383#endif
384
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000385/* Devilishly complex routine.
386 *
387 * Has to deal with EOF and EPIPE on input,
388 * with line wrapping, with last line not ending in '\n'
389 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000390 * It reads input again if last time we got an EOF (thus supporting
391 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000392 *
393 * Variables used:
394 * flines[] - array of lines already read. Linewrap may cause
395 * one source file line to occupy several flines[n].
396 * flines[max_fline] - last line, possibly incomplete.
397 * terminated - 1 if flines[max_fline] is 'terminated'
398 * (if there was '\n' [which isn't stored itself, we just remember
399 * that it was seen])
400 * max_lineno - last line's number, this one doesn't increment
401 * on line wrap, only on "real" new lines.
402 * readbuf[0..readeof-1] - small preliminary buffer.
403 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000404 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000405 * (takes into account tabs and backspaces)
406 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200407 *
408 * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
409 * "/search on very long input" and "reaching max line count" corner cases.
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000410 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000411static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000412{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000413#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000414 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000415 int w = width;
416 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000417#if ENABLE_FEATURE_LESS_REGEXP
418 unsigned old_max_fline = max_fline;
419 time_t last_time = 0;
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200420 int had_progress = 2;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000421#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000422
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200423 /* (careful: max_fline can be -1) */
424 if (max_fline + 1 > MAXLINES)
425 return;
426
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000427 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000428 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000429
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000430 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000431
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000432 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000433 max_fline += last_terminated;
434 if (!last_terminated) {
435 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000436 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000437 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000438 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000439 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000440 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000441 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000442 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000443
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000444 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000445 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000446 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000447 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000448 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000449 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000450 if (readpos >= readeof) {
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200451 errno = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000452 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000453 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000454 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000455 readpos = 0;
456 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000457 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000458 goto reached_eof;
Denys Vlasenkofe336832014-05-02 07:18:55 +0200459 IF_FEATURE_LESS_REGEXP(had_progress = 1;)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000460 }
461 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000462 /* backspace? [needed for manpages] */
463 /* <tab><bs> is (a) insane and */
464 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000465 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000466 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000467 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000468 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000469 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000470 continue;
471 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000472 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000473 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000474 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000475 new_last_line_pos += 7;
476 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000477 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000478 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000479 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000480 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000481 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000482 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000483 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000484 if (c == '\n') {
485 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000486 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000487 break;
488 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000489 /* NUL is substituted by '\n'! */
490 if (c == '\0') c = '\n';
491 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000492 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000493 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000494 /* Corner case: linewrap with only "" wrapping to next line */
495 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000496 if (!last_terminated && !current_line[0]) {
497 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000498 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000499 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000500 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000501 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000502 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000503 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000504
505 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
506 LINENO(flines[max_fline]) = max_lineno;
507 if (terminated)
508 max_lineno++;
509
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000510 if (max_fline >= MAXLINES) {
511 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000512 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000513 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000514 if (!(option_mask32 & FLAG_S)
515 ? (max_fline > cur_fline + max_displayed_line)
516 : (max_fline >= cur_fline
517 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
518 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000519#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000520 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000521#else
522 if (wanted_match >= num_matches) { /* goto_match called us */
523 fill_match_lines(old_max_fline);
524 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000525 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000526 if (wanted_match < num_matches)
527 break;
528#endif
529 }
530 if (eof_error <= 0) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000531#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;
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200536 } /* else: goto_match() called us */
537 if (errno == EAGAIN) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000538 time_t t = time(NULL);
539 if (t != last_time) {
540 last_time = t;
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200541 if (--had_progress < 0)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000542 break;
543 }
544 sched_yield();
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200545 goto again0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000546 }
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200547 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000548#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000549 }
550 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000551 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000552 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000553 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000554 } /* end of "read lines until we reach cur_fline" loop */
Denys Vlasenko69b114f2014-04-07 23:32:29 +0200555
556 if (eof_error < 0) {
557 if (errno == EAGAIN) {
558 eof_error = 1;
559 } else {
560 print_statusline(bb_msg_read_error);
561 }
562 }
563
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000564 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000565#if ENABLE_FEATURE_LESS_REGEXP
566 /* prevent us from being stuck in search for a match */
567 wanted_match = -1;
568#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000569#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000570}
571
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000572#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000573/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000574 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000575static int calc_percent(void)
576{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000577 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000578 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000579}
580
Rob Landley9200e792005-09-15 19:26:59 +0000581/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000582static void m_status_print(void)
583{
Rob Landley9200e792005-09-15 19:26:59 +0000584 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000585
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200586 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000587 return;
588
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000589 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000590 printf(HIGHLIGHT"%s", filename);
591 if (num_files > 1)
592 printf(" (file %i of %i)", current_file, num_files);
593 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000594 cur_fline + 1, cur_fline + max_displayed_line + 1,
595 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000596 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000597 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000598 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000599 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000600 return;
Rob Landley9200e792005-09-15 19:26:59 +0000601 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000602 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000603 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000604}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000605#endif
606
Rob Landley9200e792005-09-15 19:26:59 +0000607/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000608static void status_print(void)
609{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000610 const char *p;
611
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200612 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000613 return;
614
Rob Landley9200e792005-09-15 19:26:59 +0000615 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000616#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000617 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000618 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000619 return;
Rob Landley9200e792005-09-15 19:26:59 +0000620 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000621 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000622#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000623
624 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000625 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000626 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000627 return;
628 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000629 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000630 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000631 p = filename;
632 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000633 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000634 p, current_file, num_files);
635 return;
636 }
637 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000638}
639
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000640static void cap_cur_fline(int nlines)
641{
642 int diff;
643 if (cur_fline < 0)
644 cur_fline = 0;
645 if (cur_fline + max_displayed_line > max_fline + TILDES) {
646 cur_fline -= nlines;
647 if (cur_fline < 0)
648 cur_fline = 0;
649 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
650 /* As the number of lines requested was too large, we just move
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100651 * to the end of the file */
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000652 if (diff > 0)
653 cur_fline += diff;
654 }
655}
656
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000657static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000658 /* NUL: never encountered; TAB: not converted */
659 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000660 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
661 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000662static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100663 /* why 40 instead of 4a below? - it is a replacement for '\n'.
664 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000665 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
666 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
667
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000668static void lineno_str(char *nbuf9, const char *line)
669{
670 nbuf9[0] = '\0';
671 if (option_mask32 & FLAG_N) {
672 const char *fmt;
673 unsigned n;
674
675 if (line == empty_line_marker) {
676 memset(nbuf9, ' ', 8);
677 nbuf9[8] = '\0';
678 return;
679 }
680 /* Width of 7 preserves tab spacing in the text */
681 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000682 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000683 if (n > 9999999) {
684 n %= 10000000;
685 fmt = "%07u ";
686 }
687 sprintf(nbuf9, fmt, n);
688 }
689}
690
691
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000692#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000693static void print_found(const char *line)
694{
695 int match_status;
696 int eflags;
697 char *growline;
698 regmatch_t match_structs;
699
700 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000701 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000702 const char *str = line;
703 char *p = buf;
704 size_t n;
705
706 while (*str) {
707 n = strcspn(str, controls);
708 if (n) {
709 if (!str[n]) break;
710 memcpy(p, str, n);
711 p += n;
712 str += n;
713 }
714 n = strspn(str, controls);
715 memset(p, '.', n);
716 p += n;
717 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000718 }
719 strcpy(p, str);
720
721 /* buf[] holds quarantined version of str */
722
723 /* Each part of the line that matches has the HIGHLIGHT
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100724 * and NORMAL escape sequences placed around it.
725 * NB: we regex against line, but insert text
726 * from quarantined copy (buf[]) */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000727 str = buf;
728 growline = NULL;
729 eflags = 0;
730 goto start;
731
732 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000733 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200734 growline ? growline : "",
Denys Vlasenko24915112011-08-28 05:31:49 +0200735 (int)match_structs.rm_so, str,
736 (int)(match_structs.rm_eo - match_structs.rm_so),
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000737 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000738 free(growline);
739 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000740 str += match_structs.rm_eo;
741 line += match_structs.rm_eo;
742 eflags = REG_NOTBOL;
743 start:
744 /* Most of the time doesn't find the regex, optimize for that */
745 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000746 /* if even "" matches, treat it as "not a match" */
747 if (match_structs.rm_so >= match_structs.rm_eo)
748 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000749 }
750
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000751 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000752 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000753 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000754 return;
755 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000756 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000757 free(growline);
758}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000759#else
760void print_found(const char *line);
761#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000762
763static void print_ascii(const char *str)
764{
765 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000766 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000767 char *p;
768 size_t n;
769
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000770 lineno_str(nbuf9, str);
771 printf(CLEAR_2_EOL"%s", nbuf9);
772
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000773 while (*str) {
774 n = strcspn(str, controls);
775 if (n) {
776 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000777 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000778 str += n;
779 }
780 n = strspn(str, controls);
781 p = buf;
782 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000783 if (*str == 0x7f)
784 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000785 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000786 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
787 /* I want to know who committed this sin */
788 *p++ = '{';
789 else
790 *p++ = ctrlconv[(unsigned char)*str];
791 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000792 } while (--n);
793 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000794 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000795 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000796 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000797}
798
Rob Landley9200e792005-09-15 19:26:59 +0000799/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000800static void buffer_print(void)
801{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000802 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000803
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000804 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000805 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000806 if (pattern_valid)
807 print_found(buffer[i]);
808 else
809 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000810 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000811}
812
Denis Vlasenkod9083952008-10-24 10:42:21 +0000813static void buffer_fill_and_print(void)
814{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000815 unsigned i;
816#if ENABLE_FEATURE_LESS_DASHCMD
817 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000818
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000819 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000820 /* Go back to the beginning of this line */
821 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
822 fpos--;
823 }
824
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000825 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000826 while (i <= max_displayed_line && fpos <= max_fline) {
827 int lineno = LINENO(flines[fpos]);
828 buffer[i] = flines[fpos];
829 i++;
830 do {
831 fpos++;
832 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000833 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000834 && lineno == LINENO(flines[fpos])
835 );
836 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000837#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000838 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
839 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000840 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000841#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000842 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000843 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000844 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000845 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000846}
847
848/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000849static void buffer_down(int nlines)
850{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000851 cur_fline += nlines;
852 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000853 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000854 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000855}
856
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000857static void buffer_up(int nlines)
858{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000859 cur_fline -= nlines;
860 if (cur_fline < 0) cur_fline = 0;
861 read_lines();
862 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000863}
864
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000865static void buffer_line(int linenum)
866{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000867 if (linenum < 0)
868 linenum = 0;
869 cur_fline = linenum;
870 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000871 if (linenum + max_displayed_line > max_fline)
872 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000873 if (linenum < 0)
874 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000875 cur_fline = linenum;
876 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000877}
878
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000879static void open_file_and_read_lines(void)
880{
881 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000882 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000883 } else {
884 /* "less" with no arguments in argv[] */
885 /* For status line only */
886 filename = xstrdup(bb_msg_standard_input);
887 }
888 readpos = 0;
889 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000890 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000891 terminated = 1;
892 read_lines();
893}
894
895/* Reinitialize everything for a new file - free the memory and start over */
896static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000897{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000898 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000899
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000900 if (flines) {
901 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000902 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000903 free(flines);
904 flines = NULL;
905 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000906
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000907 max_fline = -1;
908 cur_fline = 0;
909 max_lineno = 0;
910 open_file_and_read_lines();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200911#if ENABLE_FEATURE_LESS_ASK_TERMINAL
912 if (G.winsize_err)
913 printf("\033[999;999H" "\033[6n");
914#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000915 buffer_fill_and_print();
916}
917
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200918static int64_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000919{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000920 int rd;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200921 int64_t key64;
Denis Vlasenko33196372008-02-23 01:25:38 +0000922 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000923
Denis Vlasenko33196372008-02-23 01:25:38 +0000924 pfd[0].fd = STDIN_FILENO;
925 pfd[0].events = POLLIN;
926 pfd[1].fd = kbd_fd;
927 pfd[1].events = POLLIN;
928 again:
929 tcsetattr(kbd_fd, TCSANOW, &term_less);
930 /* NB: select/poll returns whenever read will not block. Therefore:
931 * if eof is reached, select/poll will return immediately
932 * because read will immediately return 0 bytes.
933 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000934 * (switch fd into O_NONBLOCK'ed mode to avoid it)
935 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000936 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000937 /* Are we interested in stdin? */
938//TODO: reuse code for determining this
939 if (!(option_mask32 & FLAG_S)
940 ? !(max_fline > cur_fline + max_displayed_line)
941 : !(max_fline >= cur_fline
942 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000943 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000944 if (eof_error > 0) /* did NOT reach eof yet */
945 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000946 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000947 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000948 if (less_gets_pos >= 0)
949 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100950 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000951
Denys Vlasenko020f4062009-05-17 16:44:54 +0200952 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000953#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000954 while (1) {
955 int r;
956 /* NB: SIGWINCH interrupts poll() */
957 r = poll(pfd + rd, 2 - rd, -1);
958 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
959 return '\\'; /* anything which has no defined function */
960 if (r) break;
961 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000962#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000963 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000964#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000965 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000966
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000967 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
968 * would not block even if there is no input available */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200969 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
970 if ((int)key64 == -1) {
Denis Vlasenko64974792008-10-31 03:04:55 +0000971 if (errno == EAGAIN) {
972 /* No keyboard input available. Since poll() did return,
973 * we should have input on stdin */
974 read_lines();
975 buffer_fill_and_print();
976 goto again;
977 }
978 /* EOF/error (ssh session got killed etc) */
979 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000980 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000981 set_tty_cooked();
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200982 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000983}
984
Denys Vlasenko020f4062009-05-17 16:44:54 +0200985/* Grab a character from input without requiring the return key.
986 * May return KEYCODE_xxx values.
987 * Note that this function works best with raw input. */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200988static int64_t less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000989{
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200990 int64_t key64;
991 int key;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000992
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000993 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000994 less_gets_pos = pos;
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200995 key = key64 = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000996 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000997
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200998 /* Discard Ctrl-something chars.
999 * (checking only lower 32 bits is a size optimization:
1000 * upper 32 bits are used only by KEYCODE_CURSOR_POS)
1001 */
1002 if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +00001003 goto again;
Denys Vlasenko4f541682011-08-16 01:53:12 +02001004
1005 return key64;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001006}
1007
1008static char* less_gets(int sz)
1009{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001010 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001011 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001012 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001013
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001014 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +00001015 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001016 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001017 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +00001018 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001019 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +00001020 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001021 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +00001022 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001023 if (c == 0x7f)
1024 c = 8;
1025 if (c == 8 && i) {
1026 printf("\x8 \x8");
1027 i--;
1028 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001029 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001030 continue;
1031 if (i >= width - sz - 1)
1032 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +00001033 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001034 result[i++] = c;
1035 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001036 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001037}
1038
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001039static void examine_file(void)
1040{
Denis Vlasenko33196372008-02-23 01:25:38 +00001041 char *new_fname;
1042
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001043 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001044 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001045 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +00001046 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001047 err:
1048 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +00001049 return;
1050 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001051 if (access(new_fname, R_OK) != 0) {
1052 print_statusline("Cannot read this file");
1053 goto err;
1054 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001055 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +00001056 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001057 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001058 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001059 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001060 num_files++; */
1061 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +00001062 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001063 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001064}
1065
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001066/* This function changes the file currently being paged. direction can be one of the following:
1067 * -1: go back one file
1068 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +00001069 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001070static void change_file(int direction)
1071{
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001072 if (current_file != ((direction > 0) ? num_files : 1)) {
1073 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001074 free(filename);
1075 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001076 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001077 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001078 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001079 }
1080}
1081
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001082static void remove_current_file(void)
1083{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001084 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001085
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001086 if (num_files < 2)
1087 return;
1088
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001089 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001090 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001091 for (i = 3; i <= num_files; i++)
1092 files[i - 2] = files[i - 1];
1093 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001094 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001095 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001096 for (i = 2; i <= num_files; i++)
1097 files[i - 2] = files[i - 1];
1098 num_files--;
1099 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001100 }
1101}
1102
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001103static void colon_process(void)
1104{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001105 int keypress;
1106
1107 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001108 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001109
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001110 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001111 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001112 case 'd':
1113 remove_current_file();
1114 break;
1115 case 'e':
1116 examine_file();
1117 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001118#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001119 case 'f':
1120 m_status_print();
1121 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001122#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001123 case 'n':
1124 change_file(1);
1125 break;
1126 case 'p':
1127 change_file(-1);
1128 break;
1129 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001130 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001131 break;
1132 case 'x':
1133 change_file(0);
1134 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001135 }
1136}
1137
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001138#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001139static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001140{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001141 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001142 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001143 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001144 match = 0;
1145 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001146}
1147
Rob Landleya2e98042006-04-18 01:53:41 +00001148static void goto_match(int match)
1149{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001150 if (!pattern_valid)
1151 return;
1152 if (match < 0)
1153 match = 0;
1154 /* Try to find next match if eof isn't reached yet */
1155 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001156 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001157 read_lines();
1158 }
1159 if (num_matches) {
1160 normalize_match_pos(match);
1161 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001162 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001163 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001164 }
Rob Landleya2e98042006-04-18 01:53:41 +00001165}
1166
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001167static void fill_match_lines(unsigned pos)
1168{
1169 if (!pattern_valid)
1170 return;
1171 /* Run the regex on each line of the current file */
1172 while (pos <= max_fline) {
1173 /* If this line matches */
1174 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1175 /* and we didn't match it last time */
1176 && !(num_matches && match_lines[num_matches-1] == pos)
1177 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001178 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001179 match_lines[num_matches++] = pos;
1180 }
1181 pos++;
1182 }
1183}
1184
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001185static void regex_process(void)
1186{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001187 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001188
1189 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001190 free(match_lines);
1191 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001192 match_pos = 0;
1193 num_matches = 0;
1194 if (pattern_valid) {
1195 regfree(&pattern);
1196 pattern_valid = 0;
1197 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001198
Rob Landleya2e98042006-04-18 01:53:41 +00001199 /* Get the uncompiled regular expression from the user */
1200 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001201 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001202 uncomp_regex = less_gets(1);
1203 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001204 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001205 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001206 return;
1207 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001208
Rob Landleya2e98042006-04-18 01:53:41 +00001209 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001210 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001211 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001212 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001213 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001214 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001215 free(err);
1216 return;
1217 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001218
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001219 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001220 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001221 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001222 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001223 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001224 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001225 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001226 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001227 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1228 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001229
1230 /* It's possible that no matches are found yet.
1231 * goto_match() will read input looking for match,
1232 * if needed */
1233 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001234}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001235#endif
1236
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001237static void number_process(int first_digit)
1238{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001239 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001240 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001241 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001242 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001243
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001244 num_input[0] = first_digit;
1245
1246 /* Clear the current line, print a prompt, and then print the digit */
1247 clear_line();
1248 printf(":%c", first_digit);
1249
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001250 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001251 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001252 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001253 keypress = less_getch(i + 1);
1254 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001255 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001256 num_input[i] = keypress;
1257 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001258 i++;
1259 }
1260
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001261 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001262 num = bb_strtou(num_input, NULL, 10);
1263 /* on format error, num == -1 */
1264 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001265 buffer_print();
1266 return;
1267 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001268
1269 /* We now know the number and the letter entered, so we process them */
1270 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001271 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001272 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001273 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001274 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001275 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001276 break;
1277 case 'g': case '<': case 'G': case '>':
1278 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001279 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001280 buffer_line(num - 1);
1281 break;
1282 case 'p': case '%':
1283 num = num * (max_fline / 100); /* + max_fline / 2; */
1284 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001285 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001286 buffer_line(num);
1287 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001288#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001289 case 'n':
1290 goto_match(match_pos + num);
1291 break;
1292 case '/':
1293 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1294 regex_process();
1295 break;
1296 case '?':
1297 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1298 regex_process();
1299 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001300#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001301 }
1302}
1303
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001304#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001305static void flag_change(void)
1306{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001307 int keypress;
1308
1309 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001310 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001311 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001312
1313 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001314 case 'M':
1315 option_mask32 ^= FLAG_M;
1316 break;
1317 case 'm':
1318 option_mask32 ^= FLAG_m;
1319 break;
1320 case 'E':
1321 option_mask32 ^= FLAG_E;
1322 break;
1323 case '~':
1324 option_mask32 ^= FLAG_TILDE;
1325 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001326 case 'S':
1327 option_mask32 ^= FLAG_S;
1328 buffer_fill_and_print();
1329 break;
1330#if ENABLE_FEATURE_LESS_LINENUMS
1331 case 'N':
1332 option_mask32 ^= FLAG_N;
1333 re_wrap();
1334 buffer_fill_and_print();
1335 break;
1336#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001337 }
1338}
1339
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001340#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001341static void show_flag_status(void)
1342{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001343 int keypress;
1344 int flag_val;
1345
1346 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001347 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001348 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001349
1350 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001351 case 'M':
1352 flag_val = option_mask32 & FLAG_M;
1353 break;
1354 case 'm':
1355 flag_val = option_mask32 & FLAG_m;
1356 break;
1357 case '~':
1358 flag_val = option_mask32 & FLAG_TILDE;
1359 break;
1360 case 'N':
1361 flag_val = option_mask32 & FLAG_N;
1362 break;
1363 case 'E':
1364 flag_val = option_mask32 & FLAG_E;
1365 break;
1366 default:
1367 flag_val = 0;
1368 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001369 }
1370
1371 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001372 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001373}
1374#endif
1375
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001376#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1377
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001378static void save_input_to_file(void)
1379{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001380 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001381 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001382 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001383 FILE *fp;
1384
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001385 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001386 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001387 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001388 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001389 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001390 msg = "Error opening log file";
1391 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001392 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001393 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001394 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001395 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001396 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001397 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001398 ret:
1399 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001400 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001401}
1402
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001403#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001404static void add_mark(void)
1405{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001406 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001407
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001408 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001409 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001410
1411 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001412 /* If we exceed 15 marks, start overwriting previous ones */
1413 if (num_marks == 14)
1414 num_marks = 0;
1415
1416 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001417 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001418 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001419 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001420 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001421 }
1422}
1423
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001424static void goto_mark(void)
1425{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001426 int letter;
1427 int i;
1428
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001429 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001430 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001431 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001432
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001433 if (isalpha(letter)) {
1434 for (i = 0; i <= num_marks; i++)
1435 if (letter == mark_lines[i][0]) {
1436 buffer_line(mark_lines[i][1]);
1437 break;
1438 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001439 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001440 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001441 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001442 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001443}
1444#endif
1445
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001446#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001447static char opp_bracket(char bracket)
1448{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001449 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001450 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1451 bracket++;
1452 case '(': /* ')' == '(' + 1 */
1453 bracket++;
1454 break;
1455 case '}': case ']':
1456 bracket--;
1457 case ')':
1458 bracket--;
1459 break;
1460 };
1461 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001462}
1463
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001464static void match_right_bracket(char bracket)
1465{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001466 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001467
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001468 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001469 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001470 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001471 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001472 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001473 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001474 if (strchr(flines[i], bracket) != NULL) {
1475 buffer_line(i);
1476 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001477 }
1478 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001479 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001480}
1481
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001482static void match_left_bracket(char bracket)
1483{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001484 int i;
1485
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001486 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001487 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001488 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001489 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001490
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001491 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001492 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001493 if (strchr(flines[i], bracket) != NULL) {
1494 buffer_line(i);
1495 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001496 }
1497 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001498 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001499}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001500#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001501
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001502static void keypress_process(int keypress)
1503{
Rob Landley9200e792005-09-15 19:26:59 +00001504 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001505 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001506 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001507 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001508 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001509 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001510 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001511 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001512 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001513 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001514 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001515 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001516 break;
1517 case 'd':
1518 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001519 break;
1520 case 'u':
1521 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001522 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001523 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001524 buffer_line(0);
1525 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001526 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001527 cur_fline = MAXLINES;
1528 read_lines();
1529 buffer_line(cur_fline);
1530 break;
1531 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001532 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001533 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001534#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001535 case 'm':
1536 add_mark();
1537 buffer_print();
1538 break;
1539 case '\'':
1540 goto_mark();
1541 buffer_print();
1542 break;
Rob Landley9200e792005-09-15 19:26:59 +00001543#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001544 case 'r': case 'R':
Denys Vlasenko2ef42142011-07-25 15:23:52 +02001545 /* TODO: (1) also bind ^R, ^L to this?
1546 * (2) re-measure window size?
1547 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001548 buffer_print();
1549 break;
1550 /*case 'R':
1551 full_repaint();
1552 break;*/
1553 case 's':
1554 save_input_to_file();
1555 break;
1556 case 'E':
1557 examine_file();
1558 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001559#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001560 case '=':
1561 m_status_print();
1562 break;
Rob Landley9200e792005-09-15 19:26:59 +00001563#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001564#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001565 case '/':
1566 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1567 regex_process();
1568 break;
1569 case 'n':
1570 goto_match(match_pos + 1);
1571 break;
1572 case 'N':
1573 goto_match(match_pos - 1);
1574 break;
1575 case '?':
1576 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1577 regex_process();
1578 break;
Rob Landley9200e792005-09-15 19:26:59 +00001579#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001580#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001581 case '-':
1582 flag_change();
1583 buffer_print();
1584 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001585#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001586 case '_':
1587 show_flag_status();
1588 break;
Rob Landley9200e792005-09-15 19:26:59 +00001589#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001590#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001591#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001592 case '{': case '(': case '[':
1593 match_right_bracket(keypress);
1594 break;
1595 case '}': case ')': case ']':
1596 match_left_bracket(keypress);
1597 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001598#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001599 case ':':
1600 colon_process();
1601 break;
Rob Landley9200e792005-09-15 19:26:59 +00001602 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001603
Rob Landley9200e792005-09-15 19:26:59 +00001604 if (isdigit(keypress))
1605 number_process(keypress);
1606}
1607
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001608static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001609{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001610 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001611}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001612
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001613#if ENABLE_FEATURE_LESS_WINCH
1614static void sigwinch_handler(int sig UNUSED_PARAM)
1615{
1616 winch_counter++;
1617}
1618#endif
1619
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001620int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001621int less_main(int argc, char **argv)
1622{
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001623 char *tty_name;
1624 int tty_fd;
1625
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001626 INIT_G();
1627
Denys Vlasenko821e6432014-01-22 16:36:22 +01001628 /* TODO: -x: do not interpret backspace, -xx: tab also
1629 * -xxx: newline also
1630 * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
1631 * -s: condense many empty lines to one
1632 * (used by some setups for manpage display)
1633 */
1634 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S") /*ignored:*/"s");
Rob Landley9200e792005-09-15 19:26:59 +00001635 argc -= optind;
1636 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001637 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001638 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001639
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001640 /* Another popular pager, most, detects when stdout
1641 * is not a tty and turns into cat. This makes sense. */
1642 if (!isatty(STDOUT_FILENO))
1643 return bb_cat(argv);
1644
Rob Landley9200e792005-09-15 19:26:59 +00001645 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001646 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001647 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001648 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001649 bb_show_usage();
1650 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001651 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001652 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001653 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001654
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001655 if (option_mask32 & FLAG_TILDE)
1656 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001657
Denys Vlasenkocc1c9ca2013-08-04 17:41:19 +02001658 /* Some versions of less can survive w/o controlling tty,
1659 * try to do the same. This also allows to specify an alternative
1660 * tty via "less 1<>TTY".
1661 * We don't try to use STDOUT_FILENO directly,
1662 * since we want to set this fd to non-blocking mode,
1663 * and not bother with restoring it on exit.
1664 */
1665 tty_name = xmalloc_ttyname(STDOUT_FILENO);
1666 if (tty_name) {
1667 tty_fd = open(tty_name, O_RDONLY);
1668 free(tty_name);
1669 if (tty_fd < 0)
1670 goto try_ctty;
1671 } else {
1672 /* Try controlling tty */
1673 try_ctty:
1674 tty_fd = open(CURRENT_TTY, O_RDONLY);
1675 if (tty_fd < 0)
1676 return bb_cat(argv);
1677 }
1678 ndelay_on(tty_fd);
1679 kbd_fd = tty_fd; /* save in a global */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001680
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001681 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001682 term_less = term_orig;
1683 term_less.c_lflag &= ~(ICANON | ECHO);
1684 term_less.c_iflag &= ~(IXON | ICRNL);
1685 /*term_less.c_oflag &= ~ONLCR;*/
1686 term_less.c_cc[VMIN] = 1;
1687 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001688
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001689 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 +00001690 /* 20: two tabstops + 4 */
1691 if (width < 20 || max_displayed_line < 3)
1692 return bb_cat(argv);
1693 max_displayed_line -= 2;
1694
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001695 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001696 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001697#if ENABLE_FEATURE_LESS_WINCH
1698 signal(SIGWINCH, sigwinch_handler);
1699#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001700
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001701 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001702 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001703 while (1) {
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001704 int64_t keypress;
1705
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001706#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001707 while (WINCH_COUNTER) {
1708 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001709 winch_counter--;
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001710 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1711 IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001712 /* 20: two tabstops + 4 */
1713 if (width < 20)
1714 width = 20;
1715 if (max_displayed_line < 3)
1716 max_displayed_line = 3;
1717 max_displayed_line -= 2;
1718 free(buffer);
1719 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001720 /* Avoid re-wrap and/or redraw if we already know
1721 * we need to do it again. These ops are expensive */
1722 if (WINCH_COUNTER)
1723 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001724 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001725 if (WINCH_COUNTER)
1726 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001727 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001728 /* This took some time. Loop back and check,
1729 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001730 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001731 keypress = less_getch(-1); /* -1: do not position cursor */
Denys Vlasenko4e552a72011-07-25 15:18:20 +02001732# if ENABLE_FEATURE_LESS_ASK_TERMINAL
1733 if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
1734 uint32_t rc = (keypress >> 32);
1735 width = (rc & 0x7fff);
1736 max_displayed_line = ((rc >> 16) & 0x7fff);
1737 goto got_size;
1738 }
1739# endif
1740#else
1741 keypress = less_getch(-1); /* -1: do not position cursor */
1742#endif
Rob Landley9200e792005-09-15 19:26:59 +00001743 keypress_process(keypress);
1744 }
1745}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001746
1747/*
1748Help text of less version 418 is below.
1749If you are implementing something, keeping
1750key and/or command line switch compatibility is a good idea:
1751
1752
1753 SUMMARY OF LESS COMMANDS
1754
1755 Commands marked with * may be preceded by a number, N.
1756 Notes in parentheses indicate the behavior if N is given.
1757 h H Display this help.
1758 q :q Q :Q ZZ Exit.
1759 ---------------------------------------------------------------------------
1760 MOVING
1761 e ^E j ^N CR * Forward one line (or N lines).
1762 y ^Y k ^K ^P * Backward one line (or N lines).
1763 f ^F ^V SPACE * Forward one window (or N lines).
1764 b ^B ESC-v * Backward one window (or N lines).
1765 z * Forward one window (and set window to N).
1766 w * Backward one window (and set window to N).
1767 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1768 d ^D * Forward one half-window (and set half-window to N).
1769 u ^U * Backward one half-window (and set half-window to N).
1770 ESC-) RightArrow * Left one half screen width (or N positions).
1771 ESC-( LeftArrow * Right one half screen width (or N positions).
1772 F Forward forever; like "tail -f".
1773 r ^R ^L Repaint screen.
1774 R Repaint screen, discarding buffered input.
1775 ---------------------------------------------------
1776 Default "window" is the screen height.
1777 Default "half-window" is half of the screen height.
1778 ---------------------------------------------------------------------------
1779 SEARCHING
1780 /pattern * Search forward for (N-th) matching line.
1781 ?pattern * Search backward for (N-th) matching line.
1782 n * Repeat previous search (for N-th occurrence).
1783 N * Repeat previous search in reverse direction.
1784 ESC-n * Repeat previous search, spanning files.
1785 ESC-N * Repeat previous search, reverse dir. & spanning files.
1786 ESC-u Undo (toggle) search highlighting.
1787 ---------------------------------------------------
1788 Search patterns may be modified by one or more of:
1789 ^N or ! Search for NON-matching lines.
1790 ^E or * Search multiple files (pass thru END OF FILE).
1791 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1792 ^K Highlight matches, but don't move (KEEP position).
1793 ^R Don't use REGULAR EXPRESSIONS.
1794 ---------------------------------------------------------------------------
1795 JUMPING
1796 g < ESC-< * Go to first line in file (or line N).
1797 G > ESC-> * Go to last line in file (or line N).
1798 p % * Go to beginning of file (or N percent into file).
1799 t * Go to the (N-th) next tag.
1800 T * Go to the (N-th) previous tag.
1801 { ( [ * Find close bracket } ) ].
1802 } ) ] * Find open bracket { ( [.
1803 ESC-^F <c1> <c2> * Find close bracket <c2>.
1804 ESC-^B <c1> <c2> * Find open bracket <c1>
1805 ---------------------------------------------------
1806 Each "find close bracket" command goes forward to the close bracket
1807 matching the (N-th) open bracket in the top line.
1808 Each "find open bracket" command goes backward to the open bracket
1809 matching the (N-th) close bracket in the bottom line.
1810 m<letter> Mark the current position with <letter>.
1811 '<letter> Go to a previously marked position.
1812 '' Go to the previous position.
1813 ^X^X Same as '.
1814 ---------------------------------------------------
1815 A mark is any upper-case or lower-case letter.
1816 Certain marks are predefined:
1817 ^ means beginning of the file
1818 $ means end of the file
1819 ---------------------------------------------------------------------------
1820 CHANGING FILES
1821 :e [file] Examine a new file.
1822 ^X^V Same as :e.
1823 :n * Examine the (N-th) next file from the command line.
1824 :p * Examine the (N-th) previous file from the command line.
1825 :x * Examine the first (or N-th) file from the command line.
1826 :d Delete the current file from the command line list.
1827 = ^G :f Print current file name.
1828 ---------------------------------------------------------------------------
1829 MISCELLANEOUS COMMANDS
1830 -<flag> Toggle a command line option [see OPTIONS below].
1831 --<name> Toggle a command line option, by name.
1832 _<flag> Display the setting of a command line option.
1833 __<name> Display the setting of an option, by name.
1834 +cmd Execute the less cmd each time a new file is examined.
1835 !command Execute the shell command with $SHELL.
1836 |Xcommand Pipe file between current pos & mark X to shell command.
1837 v Edit the current file with $VISUAL or $EDITOR.
1838 V Print version number of "less".
1839 ---------------------------------------------------------------------------
1840 OPTIONS
1841 Most options may be changed either on the command line,
1842 or from within less by using the - or -- command.
1843 Options may be given in one of two forms: either a single
1844 character preceded by a -, or a name preceeded by --.
1845 -? ........ --help
1846 Display help (from command line).
1847 -a ........ --search-skip-screen
1848 Forward search skips current screen.
1849 -b [N] .... --buffers=[N]
1850 Number of buffers.
1851 -B ........ --auto-buffers
1852 Don't automatically allocate buffers for pipes.
1853 -c ........ --clear-screen
1854 Repaint by clearing rather than scrolling.
1855 -d ........ --dumb
1856 Dumb terminal.
1857 -D [xn.n] . --color=xn.n
1858 Set screen colors. (MS-DOS only)
1859 -e -E .... --quit-at-eof --QUIT-AT-EOF
1860 Quit at end of file.
1861 -f ........ --force
1862 Force open non-regular files.
1863 -F ........ --quit-if-one-screen
1864 Quit if entire file fits on first screen.
1865 -g ........ --hilite-search
1866 Highlight only last match for searches.
1867 -G ........ --HILITE-SEARCH
1868 Don't highlight any matches for searches.
1869 -h [N] .... --max-back-scroll=[N]
1870 Backward scroll limit.
1871 -i ........ --ignore-case
1872 Ignore case in searches that do not contain uppercase.
1873 -I ........ --IGNORE-CASE
1874 Ignore case in all searches.
1875 -j [N] .... --jump-target=[N]
1876 Screen position of target lines.
1877 -J ........ --status-column
1878 Display a status column at left edge of screen.
1879 -k [file] . --lesskey-file=[file]
1880 Use a lesskey file.
1881 -L ........ --no-lessopen
1882 Ignore the LESSOPEN environment variable.
1883 -m -M .... --long-prompt --LONG-PROMPT
1884 Set prompt style.
1885 -n -N .... --line-numbers --LINE-NUMBERS
1886 Don't use line numbers.
1887 -o [file] . --log-file=[file]
1888 Copy to log file (standard input only).
1889 -O [file] . --LOG-FILE=[file]
1890 Copy to log file (unconditionally overwrite).
1891 -p [pattern] --pattern=[pattern]
1892 Start at pattern (from command line).
1893 -P [prompt] --prompt=[prompt]
1894 Define new prompt.
1895 -q -Q .... --quiet --QUIET --silent --SILENT
1896 Quiet the terminal bell.
1897 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1898 Output "raw" control characters.
1899 -s ........ --squeeze-blank-lines
1900 Squeeze multiple blank lines.
1901 -S ........ --chop-long-lines
1902 Chop long lines.
1903 -t [tag] .. --tag=[tag]
1904 Find a tag.
1905 -T [tagsfile] --tag-file=[tagsfile]
1906 Use an alternate tags file.
1907 -u -U .... --underline-special --UNDERLINE-SPECIAL
1908 Change handling of backspaces.
1909 -V ........ --version
1910 Display the version number of "less".
1911 -w ........ --hilite-unread
1912 Highlight first new line after forward-screen.
1913 -W ........ --HILITE-UNREAD
1914 Highlight first new line after any forward movement.
1915 -x [N[,...]] --tabs=[N[,...]]
1916 Set tab stops.
1917 -X ........ --no-init
1918 Don't use termcap init/deinit strings.
1919 --no-keypad
1920 Don't use termcap keypad init/deinit strings.
1921 -y [N] .... --max-forw-scroll=[N]
1922 Forward scroll limit.
1923 -z [N] .... --window=[N]
1924 Set size of window.
1925 -" [c[c]] . --quotes=[c[c]]
1926 Set shell quote characters.
1927 -~ ........ --tilde
1928 Don't display tildes after end of file.
1929 -# [N] .... --shift=[N]
1930 Horizontal scroll amount (0 = one half screen width)
1931
1932 ---------------------------------------------------------------------------
1933 LINE EDITING
1934 These keys can be used to edit text being entered
1935 on the "command line" at the bottom of the screen.
1936 RightArrow ESC-l Move cursor right one character.
1937 LeftArrow ESC-h Move cursor left one character.
1938 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1939 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1940 HOME ESC-0 Move cursor to start of line.
1941 END ESC-$ Move cursor to end of line.
1942 BACKSPACE Delete char to left of cursor.
1943 DELETE ESC-x Delete char under cursor.
1944 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1945 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1946 CNTL-U ESC (MS-DOS only) Delete entire line.
1947 UpArrow ESC-k Retrieve previous command line.
1948 DownArrow ESC-j Retrieve next command line.
1949 TAB Complete filename & cycle.
1950 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1951 CNTL-L Complete filename, list all.
1952*/