blob: 77fd5a8deaf3db06a7d0a57813c933642770e3b4 [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
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define less_trivial_usage
25//usage: "[-EMNmh~I?] [FILE]..."
26//usage:#define less_full_usage "\n\n"
27//usage: "View FILE (or stdin) one screenful at a time\n"
28//usage: "\nOptions:"
29//usage: "\n -E Quit once the end of a file is reached"
30//usage: "\n -M,-m Display status line with line numbers"
31//usage: "\n and percentage through the file"
32//usage: "\n -N Prefix line number to each line"
33//usage: "\n -I Ignore case in all searches"
34//usage: "\n -~ Suppress ~s displayed past the end of the file"
35
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020036#include <sched.h> /* sched_yield() */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +000037
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000039#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +000040#include "xregex.h"
41#endif
42
Marek Polacek7b181072010-10-28 21:34:56 +020043
44#define ESC "\033"
Rob Landley9200e792005-09-15 19:26:59 +000045/* The escape codes for highlighted and normal text */
Marek Polacek7b181072010-10-28 21:34:56 +020046#define HIGHLIGHT ESC"[7m"
47#define NORMAL ESC"[0m"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020048/* The escape code to home and clear to the end of screen */
Marek Polacek7b181072010-10-28 21:34:56 +020049#define CLEAR ESC"[H\033[J"
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020050/* The escape code to clear to the end of line */
Marek Polacek7b181072010-10-28 21:34:56 +020051#define CLEAR_2_EOL ESC"[K"
Rob Landley9200e792005-09-15 19:26:59 +000052
Denis Vlasenkof4dff772006-12-24 07:14:17 +000053enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +000054/* Absolute max of lines eaten */
55 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +000056/* This many "after the end" lines we will show (at max) */
57 TILDES = 1,
58};
59
Rob Landley9200e792005-09-15 19:26:59 +000060/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000061enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +000062 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +000063 FLAG_M = 1 << 1,
64 FLAG_m = 1 << 2,
65 FLAG_N = 1 << 3,
66 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +000067 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +000068 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +000069/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000070 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
71};
Rob Landley9200e792005-09-15 19:26:59 +000072
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000073#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +000074enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +000075#endif
76
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000077struct globals {
78 int cur_fline; /* signed */
79 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +000080 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000081/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +000082 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000083 unsigned max_fline;
84 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +000085 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000086 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +000087#if ENABLE_FEATURE_LESS_WINCH
88 unsigned winch_counter;
89#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000090 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +000091 ssize_t readpos;
92 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000093 const char **buffer;
94 const char **flines;
95 const char *empty_line_marker;
96 unsigned num_files;
97 unsigned current_file;
98 char *filename;
99 char **files;
100#if ENABLE_FEATURE_LESS_MARKS
101 unsigned num_marks;
102 unsigned mark_lines[15][2];
103#endif
104#if ENABLE_FEATURE_LESS_REGEXP
105 unsigned *match_lines;
106 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000107 int wanted_match; /* signed! */
108 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000109 regex_t pattern;
110 smallint pattern_valid;
111#endif
112 smallint terminated;
113 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000114 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000115};
116#define G (*ptr_to_globals)
117#define cur_fline (G.cur_fline )
118#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000119#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000120#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000121#define max_fline (G.max_fline )
122#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000123#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000124#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000125#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000126/* This one is 100% not cached by compiler on read access */
127#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000128#define eof_error (G.eof_error )
129#define readpos (G.readpos )
130#define readeof (G.readeof )
131#define buffer (G.buffer )
132#define flines (G.flines )
133#define empty_line_marker (G.empty_line_marker )
134#define num_files (G.num_files )
135#define current_file (G.current_file )
136#define filename (G.filename )
137#define files (G.files )
138#define num_marks (G.num_marks )
139#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000140#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000141#define match_lines (G.match_lines )
142#define match_pos (G.match_pos )
143#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000144#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000145#define pattern (G.pattern )
146#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000147#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000148#define terminated (G.terminated )
149#define term_orig (G.term_orig )
150#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000151#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000152#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000153 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
154 less_gets_pos = -1; \
155 empty_line_marker = "~"; \
156 num_files = 1; \
157 current_file = 1; \
158 eof_error = 1; \
159 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000160 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000161} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000162
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000163/* flines[] are lines read from stdin, each in malloc'ed buffer.
164 * Line numbers are stored as uint32_t prepended to each line.
165 * Pointer is adjusted so that flines[i] points directly past
166 * line number. Accesor: */
167#define MEMPTR(p) ((char*)(p) - 4)
168#define LINENO(p) (*(uint32_t*)((p) - 4))
169
170
Rob Landley9200e792005-09-15 19:26:59 +0000171/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000172static void set_tty_cooked(void)
173{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100174 fflush_all();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000175 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000176}
177
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000178/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000179 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000180static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000181{
Marek Polacek7b181072010-10-28 21:34:56 +0200182 printf(ESC"[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000183}
184
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000185static void clear_line(void)
186{
Marek Polacek7b181072010-10-28 21:34:56 +0200187 printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000188}
189
190static void print_hilite(const char *str)
191{
192 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000193}
194
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000195static void print_statusline(const char *str)
196{
197 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000198 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000199}
200
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000201/* Exit the program gracefully */
202static void less_exit(int code)
203{
204 set_tty_cooked();
205 clear_line();
206 if (code < 0)
207 kill_myself_with_sig(- code); /* does not return */
208 exit(code);
209}
210
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000211#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
212 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000213static void re_wrap(void)
214{
215 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000216 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000217 int src_idx;
218 int dst_idx;
219 int new_cur_fline = 0;
220 uint32_t lineno;
221 char linebuf[w + 1];
222 const char **old_flines = flines;
223 const char *s;
224 char **new_flines = NULL;
225 char *d;
226
227 if (option_mask32 & FLAG_N)
228 w -= 8;
229
230 src_idx = 0;
231 dst_idx = 0;
232 s = old_flines[0];
233 lineno = LINENO(s);
234 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000235 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000236 while (1) {
237 *d = *s;
238 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000239 new_line_pos++;
240 if (*d == '\t') /* tab */
241 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000242 s++;
243 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000244 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000245 int sz;
246 /* new line is full, create next one */
247 *d = '\0';
248 next_new:
249 sz = (d - linebuf) + 1; /* + 1: NUL */
250 d = ((char*)xmalloc(sz + 4)) + 4;
251 LINENO(d) = lineno;
252 memcpy(d, linebuf, sz);
253 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
254 new_flines[dst_idx] = d;
255 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000256 if (new_line_pos < w) {
257 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000258 if (src_idx > max_fline)
259 break;
260 lineno = LINENO(s);
261 }
262 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000263 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000264 }
265 continue;
266 }
267 /* *d == NUL: old line ended, go to next old one */
268 free(MEMPTR(old_flines[src_idx]));
269 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000270 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000271 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000272 src_idx++;
273 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000274 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000275 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000276 s = old_flines[src_idx];
277 if (lineno != LINENO(s)) {
278 /* this is not a continuation line!
279 * create next _new_ line too */
280 goto next_new;
281 }
282 }
283
284 free(old_flines);
285 flines = (const char **)new_flines;
286
287 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000288 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000289 cur_fline = new_cur_fline;
290 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000291#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000292 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000293#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000294}
295#endif
296
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000297#if ENABLE_FEATURE_LESS_REGEXP
298static void fill_match_lines(unsigned pos);
299#else
300#define fill_match_lines(pos) ((void)0)
301#endif
302
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000303/* Devilishly complex routine.
304 *
305 * Has to deal with EOF and EPIPE on input,
306 * with line wrapping, with last line not ending in '\n'
307 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000308 * It reads input again if last time we got an EOF (thus supporting
309 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000310 *
311 * Variables used:
312 * flines[] - array of lines already read. Linewrap may cause
313 * one source file line to occupy several flines[n].
314 * flines[max_fline] - last line, possibly incomplete.
315 * terminated - 1 if flines[max_fline] is 'terminated'
316 * (if there was '\n' [which isn't stored itself, we just remember
317 * that it was seen])
318 * max_lineno - last line's number, this one doesn't increment
319 * on line wrap, only on "real" new lines.
320 * readbuf[0..readeof-1] - small preliminary buffer.
321 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000322 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000323 * (takes into account tabs and backspaces)
324 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
325 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000326static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000327{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000328#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000329 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000330 int w = width;
331 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000332#if ENABLE_FEATURE_LESS_REGEXP
333 unsigned old_max_fline = max_fline;
334 time_t last_time = 0;
335 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
336#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000337
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000338 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000339 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000340
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000341 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000342
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000343 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000344 max_fline += last_terminated;
345 if (!last_terminated) {
346 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000347 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000348 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000349 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000350 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000351 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000352 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000353 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000354
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000355 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000356 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000357 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000358 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000359 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000360 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000361 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000362 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000363 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000364 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000365 readpos = 0;
366 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000367 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000368 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000369 }
370 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000371 /* backspace? [needed for manpages] */
372 /* <tab><bs> is (a) insane and */
373 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000374 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000375 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000376 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000377 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000378 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000379 continue;
380 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000381 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000382 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000383 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000384 new_last_line_pos += 7;
385 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000386 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000387 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000388 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000389 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000390 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000391 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000392 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000393 if (c == '\n') {
394 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000395 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000396 break;
397 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000398 /* NUL is substituted by '\n'! */
399 if (c == '\0') c = '\n';
400 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000401 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000402 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000403 /* Corner case: linewrap with only "" wrapping to next line */
404 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000405 if (!last_terminated && !current_line[0]) {
406 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000407 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000408 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000409 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000410 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000411 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000412 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000413
414 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
415 LINENO(flines[max_fline]) = max_lineno;
416 if (terminated)
417 max_lineno++;
418
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000419 if (max_fline >= MAXLINES) {
420 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000421 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000422 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000423 if (!(option_mask32 & FLAG_S)
424 ? (max_fline > cur_fline + max_displayed_line)
425 : (max_fline >= cur_fline
426 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
427 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000428#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000429 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000430#else
431 if (wanted_match >= num_matches) { /* goto_match called us */
432 fill_match_lines(old_max_fline);
433 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000434 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000435 if (wanted_match < num_matches)
436 break;
437#endif
438 }
439 if (eof_error <= 0) {
440 if (eof_error < 0) {
441 if (errno == EAGAIN) {
442 /* not yet eof or error, reset flag (or else
443 * we will hog CPU - select() will return
444 * immediately */
445 eof_error = 1;
446 } else {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200447 print_statusline(bb_msg_read_error);
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000448 }
449 }
450#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000451 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000452#else
453 if (wanted_match < num_matches) {
454 break;
455 } else { /* goto_match called us */
456 time_t t = time(NULL);
457 if (t != last_time) {
458 last_time = t;
459 if (--seconds_p1 == 0)
460 break;
461 }
462 sched_yield();
463 goto again0; /* go loop again (max 2 seconds) */
464 }
465#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000466 }
467 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000468 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000469 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000470 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000471 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000472 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000473#if ENABLE_FEATURE_LESS_REGEXP
474 /* prevent us from being stuck in search for a match */
475 wanted_match = -1;
476#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000477#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000478}
479
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000480#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000481/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000482 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000483static int calc_percent(void)
484{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000485 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000486 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000487}
488
Rob Landley9200e792005-09-15 19:26:59 +0000489/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000490static void m_status_print(void)
491{
Rob Landley9200e792005-09-15 19:26:59 +0000492 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000493
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200494 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000495 return;
496
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000497 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000498 printf(HIGHLIGHT"%s", filename);
499 if (num_files > 1)
500 printf(" (file %i of %i)", current_file, num_files);
501 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000502 cur_fline + 1, cur_fline + max_displayed_line + 1,
503 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000504 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000505 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000506 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000507 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000508 return;
Rob Landley9200e792005-09-15 19:26:59 +0000509 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000510 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000511 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000512}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000513#endif
514
Rob Landley9200e792005-09-15 19:26:59 +0000515/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000516static void status_print(void)
517{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000518 const char *p;
519
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200520 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
Denis Vlasenko33196372008-02-23 01:25:38 +0000521 return;
522
Rob Landley9200e792005-09-15 19:26:59 +0000523 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000524#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000525 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000526 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000527 return;
Rob Landley9200e792005-09-15 19:26:59 +0000528 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000529 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000530#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000531
532 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000533 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000534 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000535 return;
536 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000537 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000538 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000539 p = filename;
540 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000541 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000542 p, current_file, num_files);
543 return;
544 }
545 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000546}
547
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000548static void cap_cur_fline(int nlines)
549{
550 int diff;
551 if (cur_fline < 0)
552 cur_fline = 0;
553 if (cur_fline + max_displayed_line > max_fline + TILDES) {
554 cur_fline -= nlines;
555 if (cur_fline < 0)
556 cur_fline = 0;
557 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
558 /* As the number of lines requested was too large, we just move
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100559 * to the end of the file */
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000560 if (diff > 0)
561 cur_fline += diff;
562 }
563}
564
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000565static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000566 /* NUL: never encountered; TAB: not converted */
567 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000568 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
569 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000570static const char ctrlconv[] ALIGN1 =
Denys Vlasenkoecd90fd2010-01-30 18:09:18 +0100571 /* why 40 instead of 4a below? - it is a replacement for '\n'.
572 * '\n' is a former NUL - we subst it with @, not J */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000573 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
574 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
575
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000576static void lineno_str(char *nbuf9, const char *line)
577{
578 nbuf9[0] = '\0';
579 if (option_mask32 & FLAG_N) {
580 const char *fmt;
581 unsigned n;
582
583 if (line == empty_line_marker) {
584 memset(nbuf9, ' ', 8);
585 nbuf9[8] = '\0';
586 return;
587 }
588 /* Width of 7 preserves tab spacing in the text */
589 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000590 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000591 if (n > 9999999) {
592 n %= 10000000;
593 fmt = "%07u ";
594 }
595 sprintf(nbuf9, fmt, n);
596 }
597}
598
599
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000600#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000601static void print_found(const char *line)
602{
603 int match_status;
604 int eflags;
605 char *growline;
606 regmatch_t match_structs;
607
608 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000609 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000610 const char *str = line;
611 char *p = buf;
612 size_t n;
613
614 while (*str) {
615 n = strcspn(str, controls);
616 if (n) {
617 if (!str[n]) break;
618 memcpy(p, str, n);
619 p += n;
620 str += n;
621 }
622 n = strspn(str, controls);
623 memset(p, '.', n);
624 p += n;
625 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000626 }
627 strcpy(p, str);
628
629 /* buf[] holds quarantined version of str */
630
631 /* Each part of the line that matches has the HIGHLIGHT
632 and NORMAL escape sequences placed around it.
633 NB: we regex against line, but insert text
634 from quarantined copy (buf[]) */
635 str = buf;
636 growline = NULL;
637 eflags = 0;
638 goto start;
639
640 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000641 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denys Vlasenko90a99042009-09-06 02:36:23 +0200642 growline ? growline : "",
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000643 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000644 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000645 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000646 free(growline);
647 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000648 str += match_structs.rm_eo;
649 line += match_structs.rm_eo;
650 eflags = REG_NOTBOL;
651 start:
652 /* Most of the time doesn't find the regex, optimize for that */
653 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000654 /* if even "" matches, treat it as "not a match" */
655 if (match_structs.rm_so >= match_structs.rm_eo)
656 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000657 }
658
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000659 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000660 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000661 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000662 return;
663 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000664 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000665 free(growline);
666}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000667#else
668void print_found(const char *line);
669#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000670
671static void print_ascii(const char *str)
672{
673 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000674 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000675 char *p;
676 size_t n;
677
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000678 lineno_str(nbuf9, str);
679 printf(CLEAR_2_EOL"%s", nbuf9);
680
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000681 while (*str) {
682 n = strcspn(str, controls);
683 if (n) {
684 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000685 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000686 str += n;
687 }
688 n = strspn(str, controls);
689 p = buf;
690 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000691 if (*str == 0x7f)
692 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000693 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000694 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
695 /* I want to know who committed this sin */
696 *p++ = '{';
697 else
698 *p++ = ctrlconv[(unsigned char)*str];
699 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000700 } while (--n);
701 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000702 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000703 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000704 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000705}
706
Rob Landley9200e792005-09-15 19:26:59 +0000707/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000708static void buffer_print(void)
709{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000710 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000711
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000712 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000713 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000714 if (pattern_valid)
715 print_found(buffer[i]);
716 else
717 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000718 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000719}
720
Denis Vlasenkod9083952008-10-24 10:42:21 +0000721static void buffer_fill_and_print(void)
722{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000723 unsigned i;
724#if ENABLE_FEATURE_LESS_DASHCMD
725 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000726
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000727 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000728 /* Go back to the beginning of this line */
729 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
730 fpos--;
731 }
732
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000733 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000734 while (i <= max_displayed_line && fpos <= max_fline) {
735 int lineno = LINENO(flines[fpos]);
736 buffer[i] = flines[fpos];
737 i++;
738 do {
739 fpos++;
740 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000741 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000742 && lineno == LINENO(flines[fpos])
743 );
744 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000745#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000746 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
747 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000748 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000749#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000750 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000751 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000752 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000753 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000754}
755
756/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000757static void buffer_down(int nlines)
758{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000759 cur_fline += nlines;
760 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000761 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000762 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000763}
764
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000765static void buffer_up(int nlines)
766{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000767 cur_fline -= nlines;
768 if (cur_fline < 0) cur_fline = 0;
769 read_lines();
770 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000771}
772
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000773static void buffer_line(int linenum)
774{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000775 if (linenum < 0)
776 linenum = 0;
777 cur_fline = linenum;
778 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000779 if (linenum + max_displayed_line > max_fline)
780 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000781 if (linenum < 0)
782 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000783 cur_fline = linenum;
784 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000785}
786
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000787static void open_file_and_read_lines(void)
788{
789 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000790 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000791 } else {
792 /* "less" with no arguments in argv[] */
793 /* For status line only */
794 filename = xstrdup(bb_msg_standard_input);
795 }
796 readpos = 0;
797 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000798 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000799 terminated = 1;
800 read_lines();
801}
802
803/* Reinitialize everything for a new file - free the memory and start over */
804static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000805{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000806 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000807
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000808 if (flines) {
809 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000810 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000811 free(flines);
812 flines = NULL;
813 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000814
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000815 max_fline = -1;
816 cur_fline = 0;
817 max_lineno = 0;
818 open_file_and_read_lines();
819 buffer_fill_and_print();
820}
821
Denys Vlasenko020f4062009-05-17 16:44:54 +0200822static int getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000823{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000824 int rd;
Denis Vlasenko33196372008-02-23 01:25:38 +0000825 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000826
Denis Vlasenko33196372008-02-23 01:25:38 +0000827 pfd[0].fd = STDIN_FILENO;
828 pfd[0].events = POLLIN;
829 pfd[1].fd = kbd_fd;
830 pfd[1].events = POLLIN;
831 again:
832 tcsetattr(kbd_fd, TCSANOW, &term_less);
833 /* NB: select/poll returns whenever read will not block. Therefore:
834 * if eof is reached, select/poll will return immediately
835 * because read will immediately return 0 bytes.
836 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000837 * (switch fd into O_NONBLOCK'ed mode to avoid it)
838 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000839 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000840 /* Are we interested in stdin? */
841//TODO: reuse code for determining this
842 if (!(option_mask32 & FLAG_S)
843 ? !(max_fline > cur_fline + max_displayed_line)
844 : !(max_fline >= cur_fline
845 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000846 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000847 if (eof_error > 0) /* did NOT reach eof yet */
848 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000849 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000850 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000851 if (less_gets_pos >= 0)
852 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100853 fflush_all();
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000854
Denys Vlasenko020f4062009-05-17 16:44:54 +0200855 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000856#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000857 while (1) {
858 int r;
859 /* NB: SIGWINCH interrupts poll() */
860 r = poll(pfd + rd, 2 - rd, -1);
861 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
862 return '\\'; /* anything which has no defined function */
863 if (r) break;
864 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000865#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000866 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000867#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000868 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000869
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000870 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
871 * would not block even if there is no input available */
Denys Vlasenko58f108e2010-03-11 21:17:55 +0100872 rd = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
Denis Vlasenko64974792008-10-31 03:04:55 +0000873 if (rd == -1) {
874 if (errno == EAGAIN) {
875 /* No keyboard input available. Since poll() did return,
876 * we should have input on stdin */
877 read_lines();
878 buffer_fill_and_print();
879 goto again;
880 }
881 /* EOF/error (ssh session got killed etc) */
882 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000883 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000884 set_tty_cooked();
885 return rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000886}
887
Denys Vlasenko020f4062009-05-17 16:44:54 +0200888/* Grab a character from input without requiring the return key.
889 * May return KEYCODE_xxx values.
890 * Note that this function works best with raw input. */
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000891static int less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000892{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000893 int i;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000894
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000895 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000896 less_gets_pos = pos;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000897 i = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000898 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000899
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000900 /* Discard Ctrl-something chars */
901 if (i >= 0 && i < ' ' && i != 0x0d && i != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000902 goto again;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000903 return i;
904}
905
906static char* less_gets(int sz)
907{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000908 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000909 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000910 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000911
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000912 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +0000913 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000914 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000915 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +0000916 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +0000917 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000918 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000919 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +0000920 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000921 if (c == 0x7f)
922 c = 8;
923 if (c == 8 && i) {
924 printf("\x8 \x8");
925 i--;
926 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000927 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000928 continue;
929 if (i >= width - sz - 1)
930 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000931 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000932 result[i++] = c;
933 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000934 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000935}
936
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000937static void examine_file(void)
938{
Denis Vlasenko33196372008-02-23 01:25:38 +0000939 char *new_fname;
940
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000941 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000942 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000943 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +0000944 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000945 err:
946 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +0000947 return;
948 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000949 if (access(new_fname, R_OK) != 0) {
950 print_statusline("Cannot read this file");
951 goto err;
952 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000953 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +0000954 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000955 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000956 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000957 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000958 num_files++; */
959 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000960 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000961 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000962}
963
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000964/* This function changes the file currently being paged. direction can be one of the following:
965 * -1: go back one file
966 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000967 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000968static void change_file(int direction)
969{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000970 if (current_file != ((direction > 0) ? num_files : 1)) {
971 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000972 free(filename);
973 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000974 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000975 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000976 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000977 }
978}
979
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000980static void remove_current_file(void)
981{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000982 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000983
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000984 if (num_files < 2)
985 return;
986
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000987 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000988 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000989 for (i = 3; i <= num_files; i++)
990 files[i - 2] = files[i - 1];
991 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000992 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000993 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000994 for (i = 2; i <= num_files; i++)
995 files[i - 2] = files[i - 1];
996 num_files--;
997 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000998 }
999}
1000
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001001static void colon_process(void)
1002{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001003 int keypress;
1004
1005 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001006 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001007
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001008 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001009 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001010 case 'd':
1011 remove_current_file();
1012 break;
1013 case 'e':
1014 examine_file();
1015 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001016#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001017 case 'f':
1018 m_status_print();
1019 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001020#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001021 case 'n':
1022 change_file(1);
1023 break;
1024 case 'p':
1025 change_file(-1);
1026 break;
1027 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001028 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001029 break;
1030 case 'x':
1031 change_file(0);
1032 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001033 }
1034}
1035
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001036#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001037static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001038{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001039 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001040 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001041 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001042 match = 0;
1043 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001044}
1045
Rob Landleya2e98042006-04-18 01:53:41 +00001046static void goto_match(int match)
1047{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001048 if (!pattern_valid)
1049 return;
1050 if (match < 0)
1051 match = 0;
1052 /* Try to find next match if eof isn't reached yet */
1053 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001054 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001055 read_lines();
1056 }
1057 if (num_matches) {
1058 normalize_match_pos(match);
1059 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001060 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001061 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001062 }
Rob Landleya2e98042006-04-18 01:53:41 +00001063}
1064
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001065static void fill_match_lines(unsigned pos)
1066{
1067 if (!pattern_valid)
1068 return;
1069 /* Run the regex on each line of the current file */
1070 while (pos <= max_fline) {
1071 /* If this line matches */
1072 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1073 /* and we didn't match it last time */
1074 && !(num_matches && match_lines[num_matches-1] == pos)
1075 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001076 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001077 match_lines[num_matches++] = pos;
1078 }
1079 pos++;
1080 }
1081}
1082
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001083static void regex_process(void)
1084{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001085 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001086
1087 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001088 free(match_lines);
1089 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001090 match_pos = 0;
1091 num_matches = 0;
1092 if (pattern_valid) {
1093 regfree(&pattern);
1094 pattern_valid = 0;
1095 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001096
Rob Landleya2e98042006-04-18 01:53:41 +00001097 /* Get the uncompiled regular expression from the user */
1098 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001099 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001100 uncomp_regex = less_gets(1);
1101 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001102 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001103 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001104 return;
1105 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001106
Rob Landleya2e98042006-04-18 01:53:41 +00001107 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001108 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001109 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001110 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001111 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001112 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001113 free(err);
1114 return;
1115 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001116
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001117 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001118 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001119 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001120 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001121 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001122 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001123 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001124 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001125 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1126 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001127
1128 /* It's possible that no matches are found yet.
1129 * goto_match() will read input looking for match,
1130 * if needed */
1131 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001132}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001133#endif
1134
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001135static void number_process(int first_digit)
1136{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001137 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001138 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001139 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001140 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001141
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001142 num_input[0] = first_digit;
1143
1144 /* Clear the current line, print a prompt, and then print the digit */
1145 clear_line();
1146 printf(":%c", first_digit);
1147
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001148 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001149 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001150 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001151 keypress = less_getch(i + 1);
1152 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001153 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001154 num_input[i] = keypress;
1155 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001156 i++;
1157 }
1158
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001159 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001160 num = bb_strtou(num_input, NULL, 10);
1161 /* on format error, num == -1 */
1162 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001163 buffer_print();
1164 return;
1165 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001166
1167 /* We now know the number and the letter entered, so we process them */
1168 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001169 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001170 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001171 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001172 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001173 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001174 break;
1175 case 'g': case '<': case 'G': case '>':
1176 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001177 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001178 buffer_line(num - 1);
1179 break;
1180 case 'p': case '%':
1181 num = num * (max_fline / 100); /* + max_fline / 2; */
1182 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001183 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001184 buffer_line(num);
1185 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001186#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001187 case 'n':
1188 goto_match(match_pos + num);
1189 break;
1190 case '/':
1191 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1192 regex_process();
1193 break;
1194 case '?':
1195 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1196 regex_process();
1197 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001198#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001199 }
1200}
1201
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001202#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001203static void flag_change(void)
1204{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001205 int keypress;
1206
1207 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001208 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001209 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001210
1211 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001212 case 'M':
1213 option_mask32 ^= FLAG_M;
1214 break;
1215 case 'm':
1216 option_mask32 ^= FLAG_m;
1217 break;
1218 case 'E':
1219 option_mask32 ^= FLAG_E;
1220 break;
1221 case '~':
1222 option_mask32 ^= FLAG_TILDE;
1223 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001224 case 'S':
1225 option_mask32 ^= FLAG_S;
1226 buffer_fill_and_print();
1227 break;
1228#if ENABLE_FEATURE_LESS_LINENUMS
1229 case 'N':
1230 option_mask32 ^= FLAG_N;
1231 re_wrap();
1232 buffer_fill_and_print();
1233 break;
1234#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001235 }
1236}
1237
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001238#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001239static void show_flag_status(void)
1240{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001241 int keypress;
1242 int flag_val;
1243
1244 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001245 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001246 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001247
1248 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001249 case 'M':
1250 flag_val = option_mask32 & FLAG_M;
1251 break;
1252 case 'm':
1253 flag_val = option_mask32 & FLAG_m;
1254 break;
1255 case '~':
1256 flag_val = option_mask32 & FLAG_TILDE;
1257 break;
1258 case 'N':
1259 flag_val = option_mask32 & FLAG_N;
1260 break;
1261 case 'E':
1262 flag_val = option_mask32 & FLAG_E;
1263 break;
1264 default:
1265 flag_val = 0;
1266 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001267 }
1268
1269 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001270 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001271}
1272#endif
1273
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001274#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1275
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001276static void save_input_to_file(void)
1277{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001278 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001279 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001280 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001281 FILE *fp;
1282
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001283 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001284 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001285 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001286 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001287 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001288 msg = "Error opening log file";
1289 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001290 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001291 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001292 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001293 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001294 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001295 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001296 ret:
1297 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001298 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001299}
1300
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001301#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001302static void add_mark(void)
1303{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001304 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001305
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001306 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001307 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001308
1309 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001310 /* If we exceed 15 marks, start overwriting previous ones */
1311 if (num_marks == 14)
1312 num_marks = 0;
1313
1314 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001315 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001316 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001317 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001318 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001319 }
1320}
1321
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001322static void goto_mark(void)
1323{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001324 int letter;
1325 int i;
1326
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001327 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001328 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001329 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001330
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001331 if (isalpha(letter)) {
1332 for (i = 0; i <= num_marks; i++)
1333 if (letter == mark_lines[i][0]) {
1334 buffer_line(mark_lines[i][1]);
1335 break;
1336 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001337 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001338 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001339 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001340 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001341}
1342#endif
1343
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001344#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001345static char opp_bracket(char bracket)
1346{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001347 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001348 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1349 bracket++;
1350 case '(': /* ')' == '(' + 1 */
1351 bracket++;
1352 break;
1353 case '}': case ']':
1354 bracket--;
1355 case ')':
1356 bracket--;
1357 break;
1358 };
1359 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001360}
1361
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001362static void match_right_bracket(char bracket)
1363{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001364 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001365
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001366 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001367 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001368 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001369 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001370 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001371 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001372 if (strchr(flines[i], bracket) != NULL) {
1373 buffer_line(i);
1374 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001375 }
1376 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001377 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001378}
1379
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001380static void match_left_bracket(char bracket)
1381{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001382 int i;
1383
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001384 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001385 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001386 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001387 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001388
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001389 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001390 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001391 if (strchr(flines[i], bracket) != NULL) {
1392 buffer_line(i);
1393 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001394 }
1395 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001396 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001397}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001398#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001399
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001400static void keypress_process(int keypress)
1401{
Rob Landley9200e792005-09-15 19:26:59 +00001402 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001403 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001404 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001405 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001406 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001407 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001408 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001409 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001410 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001411 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001412 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001413 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001414 break;
1415 case 'd':
1416 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001417 break;
1418 case 'u':
1419 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001420 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001421 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001422 buffer_line(0);
1423 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001424 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001425 cur_fline = MAXLINES;
1426 read_lines();
1427 buffer_line(cur_fline);
1428 break;
1429 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001430 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001431 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001432#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001433 case 'm':
1434 add_mark();
1435 buffer_print();
1436 break;
1437 case '\'':
1438 goto_mark();
1439 buffer_print();
1440 break;
Rob Landley9200e792005-09-15 19:26:59 +00001441#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001442 case 'r': case 'R':
1443 buffer_print();
1444 break;
1445 /*case 'R':
1446 full_repaint();
1447 break;*/
1448 case 's':
1449 save_input_to_file();
1450 break;
1451 case 'E':
1452 examine_file();
1453 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001454#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001455 case '=':
1456 m_status_print();
1457 break;
Rob Landley9200e792005-09-15 19:26:59 +00001458#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001459#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001460 case '/':
1461 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1462 regex_process();
1463 break;
1464 case 'n':
1465 goto_match(match_pos + 1);
1466 break;
1467 case 'N':
1468 goto_match(match_pos - 1);
1469 break;
1470 case '?':
1471 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1472 regex_process();
1473 break;
Rob Landley9200e792005-09-15 19:26:59 +00001474#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001475#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001476 case '-':
1477 flag_change();
1478 buffer_print();
1479 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001480#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001481 case '_':
1482 show_flag_status();
1483 break;
Rob Landley9200e792005-09-15 19:26:59 +00001484#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001485#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001486#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001487 case '{': case '(': case '[':
1488 match_right_bracket(keypress);
1489 break;
1490 case '}': case ')': case ']':
1491 match_left_bracket(keypress);
1492 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001493#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001494 case ':':
1495 colon_process();
1496 break;
Rob Landley9200e792005-09-15 19:26:59 +00001497 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001498
Rob Landley9200e792005-09-15 19:26:59 +00001499 if (isdigit(keypress))
1500 number_process(keypress);
1501}
1502
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001503static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001504{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001505 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001506}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001507
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001508#if ENABLE_FEATURE_LESS_WINCH
1509static void sigwinch_handler(int sig UNUSED_PARAM)
1510{
1511 winch_counter++;
1512}
1513#endif
1514
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001515int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001516int less_main(int argc, char **argv)
1517{
Rob Landley9200e792005-09-15 19:26:59 +00001518 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001519
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001520 INIT_G();
1521
Denis Vlasenko95b30712006-12-31 19:23:31 +00001522 /* TODO: -x: do not interpret backspace, -xx: tab also */
1523 /* -xxx: newline also */
1524 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001525 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
Rob Landley9200e792005-09-15 19:26:59 +00001526 argc -= optind;
1527 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001528 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001529 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001530
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001531 /* Another popular pager, most, detects when stdout
1532 * is not a tty and turns into cat. This makes sense. */
1533 if (!isatty(STDOUT_FILENO))
1534 return bb_cat(argv);
1535
Rob Landley9200e792005-09-15 19:26:59 +00001536 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001537 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001538 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001539 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001540 bb_show_usage();
1541 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001542 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001543 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001544 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001545
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001546 if (option_mask32 & FLAG_TILDE)
1547 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001548
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001549 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1550 if (kbd_fd < 0)
1551 return bb_cat(argv);
1552 ndelay_on(kbd_fd);
1553
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001554 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001555 term_less = term_orig;
1556 term_less.c_lflag &= ~(ICANON | ECHO);
1557 term_less.c_iflag &= ~(IXON | ICRNL);
1558 /*term_less.c_oflag &= ~ONLCR;*/
1559 term_less.c_cc[VMIN] = 1;
1560 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001561
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001562 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1563 /* 20: two tabstops + 4 */
1564 if (width < 20 || max_displayed_line < 3)
1565 return bb_cat(argv);
1566 max_displayed_line -= 2;
1567
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001568 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001569 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001570#if ENABLE_FEATURE_LESS_WINCH
1571 signal(SIGWINCH, sigwinch_handler);
1572#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001573
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001574 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001575 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001576 while (1) {
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001577#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001578 while (WINCH_COUNTER) {
1579 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001580 winch_counter--;
1581 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1582 /* 20: two tabstops + 4 */
1583 if (width < 20)
1584 width = 20;
1585 if (max_displayed_line < 3)
1586 max_displayed_line = 3;
1587 max_displayed_line -= 2;
1588 free(buffer);
1589 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001590 /* Avoid re-wrap and/or redraw if we already know
1591 * we need to do it again. These ops are expensive */
1592 if (WINCH_COUNTER)
1593 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001594 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001595 if (WINCH_COUNTER)
1596 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001597 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001598 /* This took some time. Loop back and check,
1599 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001600 }
1601#endif
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001602 keypress = less_getch(-1); /* -1: do not position cursor */
Rob Landley9200e792005-09-15 19:26:59 +00001603 keypress_process(keypress);
1604 }
1605}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001606
1607/*
1608Help text of less version 418 is below.
1609If you are implementing something, keeping
1610key and/or command line switch compatibility is a good idea:
1611
1612
1613 SUMMARY OF LESS COMMANDS
1614
1615 Commands marked with * may be preceded by a number, N.
1616 Notes in parentheses indicate the behavior if N is given.
1617 h H Display this help.
1618 q :q Q :Q ZZ Exit.
1619 ---------------------------------------------------------------------------
1620 MOVING
1621 e ^E j ^N CR * Forward one line (or N lines).
1622 y ^Y k ^K ^P * Backward one line (or N lines).
1623 f ^F ^V SPACE * Forward one window (or N lines).
1624 b ^B ESC-v * Backward one window (or N lines).
1625 z * Forward one window (and set window to N).
1626 w * Backward one window (and set window to N).
1627 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1628 d ^D * Forward one half-window (and set half-window to N).
1629 u ^U * Backward one half-window (and set half-window to N).
1630 ESC-) RightArrow * Left one half screen width (or N positions).
1631 ESC-( LeftArrow * Right one half screen width (or N positions).
1632 F Forward forever; like "tail -f".
1633 r ^R ^L Repaint screen.
1634 R Repaint screen, discarding buffered input.
1635 ---------------------------------------------------
1636 Default "window" is the screen height.
1637 Default "half-window" is half of the screen height.
1638 ---------------------------------------------------------------------------
1639 SEARCHING
1640 /pattern * Search forward for (N-th) matching line.
1641 ?pattern * Search backward for (N-th) matching line.
1642 n * Repeat previous search (for N-th occurrence).
1643 N * Repeat previous search in reverse direction.
1644 ESC-n * Repeat previous search, spanning files.
1645 ESC-N * Repeat previous search, reverse dir. & spanning files.
1646 ESC-u Undo (toggle) search highlighting.
1647 ---------------------------------------------------
1648 Search patterns may be modified by one or more of:
1649 ^N or ! Search for NON-matching lines.
1650 ^E or * Search multiple files (pass thru END OF FILE).
1651 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1652 ^K Highlight matches, but don't move (KEEP position).
1653 ^R Don't use REGULAR EXPRESSIONS.
1654 ---------------------------------------------------------------------------
1655 JUMPING
1656 g < ESC-< * Go to first line in file (or line N).
1657 G > ESC-> * Go to last line in file (or line N).
1658 p % * Go to beginning of file (or N percent into file).
1659 t * Go to the (N-th) next tag.
1660 T * Go to the (N-th) previous tag.
1661 { ( [ * Find close bracket } ) ].
1662 } ) ] * Find open bracket { ( [.
1663 ESC-^F <c1> <c2> * Find close bracket <c2>.
1664 ESC-^B <c1> <c2> * Find open bracket <c1>
1665 ---------------------------------------------------
1666 Each "find close bracket" command goes forward to the close bracket
1667 matching the (N-th) open bracket in the top line.
1668 Each "find open bracket" command goes backward to the open bracket
1669 matching the (N-th) close bracket in the bottom line.
1670 m<letter> Mark the current position with <letter>.
1671 '<letter> Go to a previously marked position.
1672 '' Go to the previous position.
1673 ^X^X Same as '.
1674 ---------------------------------------------------
1675 A mark is any upper-case or lower-case letter.
1676 Certain marks are predefined:
1677 ^ means beginning of the file
1678 $ means end of the file
1679 ---------------------------------------------------------------------------
1680 CHANGING FILES
1681 :e [file] Examine a new file.
1682 ^X^V Same as :e.
1683 :n * Examine the (N-th) next file from the command line.
1684 :p * Examine the (N-th) previous file from the command line.
1685 :x * Examine the first (or N-th) file from the command line.
1686 :d Delete the current file from the command line list.
1687 = ^G :f Print current file name.
1688 ---------------------------------------------------------------------------
1689 MISCELLANEOUS COMMANDS
1690 -<flag> Toggle a command line option [see OPTIONS below].
1691 --<name> Toggle a command line option, by name.
1692 _<flag> Display the setting of a command line option.
1693 __<name> Display the setting of an option, by name.
1694 +cmd Execute the less cmd each time a new file is examined.
1695 !command Execute the shell command with $SHELL.
1696 |Xcommand Pipe file between current pos & mark X to shell command.
1697 v Edit the current file with $VISUAL or $EDITOR.
1698 V Print version number of "less".
1699 ---------------------------------------------------------------------------
1700 OPTIONS
1701 Most options may be changed either on the command line,
1702 or from within less by using the - or -- command.
1703 Options may be given in one of two forms: either a single
1704 character preceded by a -, or a name preceeded by --.
1705 -? ........ --help
1706 Display help (from command line).
1707 -a ........ --search-skip-screen
1708 Forward search skips current screen.
1709 -b [N] .... --buffers=[N]
1710 Number of buffers.
1711 -B ........ --auto-buffers
1712 Don't automatically allocate buffers for pipes.
1713 -c ........ --clear-screen
1714 Repaint by clearing rather than scrolling.
1715 -d ........ --dumb
1716 Dumb terminal.
1717 -D [xn.n] . --color=xn.n
1718 Set screen colors. (MS-DOS only)
1719 -e -E .... --quit-at-eof --QUIT-AT-EOF
1720 Quit at end of file.
1721 -f ........ --force
1722 Force open non-regular files.
1723 -F ........ --quit-if-one-screen
1724 Quit if entire file fits on first screen.
1725 -g ........ --hilite-search
1726 Highlight only last match for searches.
1727 -G ........ --HILITE-SEARCH
1728 Don't highlight any matches for searches.
1729 -h [N] .... --max-back-scroll=[N]
1730 Backward scroll limit.
1731 -i ........ --ignore-case
1732 Ignore case in searches that do not contain uppercase.
1733 -I ........ --IGNORE-CASE
1734 Ignore case in all searches.
1735 -j [N] .... --jump-target=[N]
1736 Screen position of target lines.
1737 -J ........ --status-column
1738 Display a status column at left edge of screen.
1739 -k [file] . --lesskey-file=[file]
1740 Use a lesskey file.
1741 -L ........ --no-lessopen
1742 Ignore the LESSOPEN environment variable.
1743 -m -M .... --long-prompt --LONG-PROMPT
1744 Set prompt style.
1745 -n -N .... --line-numbers --LINE-NUMBERS
1746 Don't use line numbers.
1747 -o [file] . --log-file=[file]
1748 Copy to log file (standard input only).
1749 -O [file] . --LOG-FILE=[file]
1750 Copy to log file (unconditionally overwrite).
1751 -p [pattern] --pattern=[pattern]
1752 Start at pattern (from command line).
1753 -P [prompt] --prompt=[prompt]
1754 Define new prompt.
1755 -q -Q .... --quiet --QUIET --silent --SILENT
1756 Quiet the terminal bell.
1757 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1758 Output "raw" control characters.
1759 -s ........ --squeeze-blank-lines
1760 Squeeze multiple blank lines.
1761 -S ........ --chop-long-lines
1762 Chop long lines.
1763 -t [tag] .. --tag=[tag]
1764 Find a tag.
1765 -T [tagsfile] --tag-file=[tagsfile]
1766 Use an alternate tags file.
1767 -u -U .... --underline-special --UNDERLINE-SPECIAL
1768 Change handling of backspaces.
1769 -V ........ --version
1770 Display the version number of "less".
1771 -w ........ --hilite-unread
1772 Highlight first new line after forward-screen.
1773 -W ........ --HILITE-UNREAD
1774 Highlight first new line after any forward movement.
1775 -x [N[,...]] --tabs=[N[,...]]
1776 Set tab stops.
1777 -X ........ --no-init
1778 Don't use termcap init/deinit strings.
1779 --no-keypad
1780 Don't use termcap keypad init/deinit strings.
1781 -y [N] .... --max-forw-scroll=[N]
1782 Forward scroll limit.
1783 -z [N] .... --window=[N]
1784 Set size of window.
1785 -" [c[c]] . --quotes=[c[c]]
1786 Set shell quote characters.
1787 -~ ........ --tilde
1788 Don't display tildes after end of file.
1789 -# [N] .... --shift=[N]
1790 Horizontal scroll amount (0 = one half screen width)
1791
1792 ---------------------------------------------------------------------------
1793 LINE EDITING
1794 These keys can be used to edit text being entered
1795 on the "command line" at the bottom of the screen.
1796 RightArrow ESC-l Move cursor right one character.
1797 LeftArrow ESC-h Move cursor left one character.
1798 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1799 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1800 HOME ESC-0 Move cursor to start of line.
1801 END ESC-$ Move cursor to end of line.
1802 BACKSPACE Delete char to left of cursor.
1803 DELETE ESC-x Delete char under cursor.
1804 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1805 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1806 CNTL-U ESC (MS-DOS only) Delete entire line.
1807 UpArrow ESC-k Retrieve previous command line.
1808 DownArrow ESC-j Retrieve next command line.
1809 TAB Complete filename & cycle.
1810 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1811 CNTL-L Complete filename, list all.
1812*/