blob: bd855066fbd2b6ee5b7d73cfd7c7782a02787950 [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 *
Mike Frysingerf284c762006-04-16 20:38:26 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 */
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
Denis Vlasenko18d6fc12007-03-08 17:52:36 +000024#include <sched.h> /* sched_yield() */
25
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000027#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +000028#include "xregex.h"
29#endif
30
Rob Landley9200e792005-09-15 19:26:59 +000031/* The escape codes for highlighted and normal text */
32#define HIGHLIGHT "\033[7m"
33#define NORMAL "\033[0m"
Rob Landley9200e792005-09-15 19:26:59 +000034/* The escape code to clear the screen */
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +000035#define CLEAR "\033[H\033[J"
Denis Vlasenkoe865e812006-12-21 13:24:58 +000036/* The escape code to clear to end of line */
37#define CLEAR_2_EOL "\033[K"
Rob Landley9200e792005-09-15 19:26:59 +000038
Denis Vlasenkof4dff772006-12-24 07:14:17 +000039enum {
Denis Vlasenkof4dff772006-12-24 07:14:17 +000040/* Absolute max of lines eaten */
41 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
Denis Vlasenkof4dff772006-12-24 07:14:17 +000042/* This many "after the end" lines we will show (at max) */
43 TILDES = 1,
44};
45
Rob Landley9200e792005-09-15 19:26:59 +000046/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000047enum {
Denis Vlasenko53c80f02008-10-24 22:43:27 +000048 FLAG_E = 1 << 0,
Denis Vlasenkof4dff772006-12-24 07:14:17 +000049 FLAG_M = 1 << 1,
50 FLAG_m = 1 << 2,
51 FLAG_N = 1 << 3,
52 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +000053 FLAG_I = 1 << 5,
Denis Vlasenko53c80f02008-10-24 22:43:27 +000054 FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +000055/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000056 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
57};
Rob Landley9200e792005-09-15 19:26:59 +000058
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000059#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +000060enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +000061#endif
62
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000063struct globals {
64 int cur_fline; /* signed */
65 int kbd_fd; /* fd to get input from */
Denis Vlasenko33196372008-02-23 01:25:38 +000066 int less_gets_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000067/* last position in last line, taking into account tabs */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +000068 size_t last_line_pos;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000069 unsigned max_fline;
70 unsigned max_lineno; /* this one tracks linewrap */
Denis Vlasenko53c80f02008-10-24 22:43:27 +000071 unsigned max_displayed_line;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000072 unsigned width;
Denis Vlasenko53c80f02008-10-24 22:43:27 +000073#if ENABLE_FEATURE_LESS_WINCH
74 unsigned winch_counter;
75#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000076 ssize_t eof_error; /* eof if 0, error if < 0 */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +000077 ssize_t readpos;
78 ssize_t readeof; /* must be signed */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000079 const char **buffer;
80 const char **flines;
81 const char *empty_line_marker;
82 unsigned num_files;
83 unsigned current_file;
84 char *filename;
85 char **files;
86#if ENABLE_FEATURE_LESS_MARKS
87 unsigned num_marks;
88 unsigned mark_lines[15][2];
89#endif
90#if ENABLE_FEATURE_LESS_REGEXP
91 unsigned *match_lines;
92 int match_pos; /* signed! */
Denis Vlasenko107fe7c2008-03-17 08:38:45 +000093 int wanted_match; /* signed! */
94 int num_matches;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000095 regex_t pattern;
96 smallint pattern_valid;
97#endif
98 smallint terminated;
99 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000100 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000101};
102#define G (*ptr_to_globals)
103#define cur_fline (G.cur_fline )
104#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000105#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000106#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000107#define max_fline (G.max_fline )
108#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000109#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000110#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000111#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000112/* This one is 100% not cached by compiler on read access */
113#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000114#define eof_error (G.eof_error )
115#define readpos (G.readpos )
116#define readeof (G.readeof )
117#define buffer (G.buffer )
118#define flines (G.flines )
119#define empty_line_marker (G.empty_line_marker )
120#define num_files (G.num_files )
121#define current_file (G.current_file )
122#define filename (G.filename )
123#define files (G.files )
124#define num_marks (G.num_marks )
125#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000126#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000127#define match_lines (G.match_lines )
128#define match_pos (G.match_pos )
129#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000130#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000131#define pattern (G.pattern )
132#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000133#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000134#define terminated (G.terminated )
135#define term_orig (G.term_orig )
136#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000137#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000138#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000139 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
140 less_gets_pos = -1; \
141 empty_line_marker = "~"; \
142 num_files = 1; \
143 current_file = 1; \
144 eof_error = 1; \
145 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000146 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000147} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000148
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000149/* flines[] are lines read from stdin, each in malloc'ed buffer.
150 * Line numbers are stored as uint32_t prepended to each line.
151 * Pointer is adjusted so that flines[i] points directly past
152 * line number. Accesor: */
153#define MEMPTR(p) ((char*)(p) - 4)
154#define LINENO(p) (*(uint32_t*)((p) - 4))
155
156
Rob Landley9200e792005-09-15 19:26:59 +0000157/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000158static void set_tty_cooked(void)
159{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000160 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000161 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000162}
163
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000164/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000165 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000166static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000167{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000168 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000169}
170
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000171static void clear_line(void)
172{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000173 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000174}
175
176static void print_hilite(const char *str)
177{
178 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000179}
180
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000181static void print_statusline(const char *str)
182{
183 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000184 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000185}
186
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000187/* Exit the program gracefully */
188static void less_exit(int code)
189{
190 set_tty_cooked();
191 clear_line();
192 if (code < 0)
193 kill_myself_with_sig(- code); /* does not return */
194 exit(code);
195}
196
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000197#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
198 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000199static void re_wrap(void)
200{
201 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000202 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000203 int src_idx;
204 int dst_idx;
205 int new_cur_fline = 0;
206 uint32_t lineno;
207 char linebuf[w + 1];
208 const char **old_flines = flines;
209 const char *s;
210 char **new_flines = NULL;
211 char *d;
212
213 if (option_mask32 & FLAG_N)
214 w -= 8;
215
216 src_idx = 0;
217 dst_idx = 0;
218 s = old_flines[0];
219 lineno = LINENO(s);
220 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000221 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000222 while (1) {
223 *d = *s;
224 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000225 new_line_pos++;
226 if (*d == '\t') /* tab */
227 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000228 s++;
229 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000230 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000231 int sz;
232 /* new line is full, create next one */
233 *d = '\0';
234 next_new:
235 sz = (d - linebuf) + 1; /* + 1: NUL */
236 d = ((char*)xmalloc(sz + 4)) + 4;
237 LINENO(d) = lineno;
238 memcpy(d, linebuf, sz);
239 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
240 new_flines[dst_idx] = d;
241 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000242 if (new_line_pos < w) {
243 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000244 if (src_idx > max_fline)
245 break;
246 lineno = LINENO(s);
247 }
248 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000249 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000250 }
251 continue;
252 }
253 /* *d == NUL: old line ended, go to next old one */
254 free(MEMPTR(old_flines[src_idx]));
255 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000256 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000257 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000258 src_idx++;
259 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000260 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000261 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000262 s = old_flines[src_idx];
263 if (lineno != LINENO(s)) {
264 /* this is not a continuation line!
265 * create next _new_ line too */
266 goto next_new;
267 }
268 }
269
270 free(old_flines);
271 flines = (const char **)new_flines;
272
273 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000274 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000275 cur_fline = new_cur_fline;
276 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000277#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000278 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000279#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000280}
281#endif
282
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000283#if ENABLE_FEATURE_LESS_REGEXP
284static void fill_match_lines(unsigned pos);
285#else
286#define fill_match_lines(pos) ((void)0)
287#endif
288
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000289/* Devilishly complex routine.
290 *
291 * Has to deal with EOF and EPIPE on input,
292 * with line wrapping, with last line not ending in '\n'
293 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000294 * It reads input again if last time we got an EOF (thus supporting
295 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000296 *
297 * Variables used:
298 * flines[] - array of lines already read. Linewrap may cause
299 * one source file line to occupy several flines[n].
300 * flines[max_fline] - last line, possibly incomplete.
301 * terminated - 1 if flines[max_fline] is 'terminated'
302 * (if there was '\n' [which isn't stored itself, we just remember
303 * that it was seen])
304 * max_lineno - last line's number, this one doesn't increment
305 * on line wrap, only on "real" new lines.
306 * readbuf[0..readeof-1] - small preliminary buffer.
307 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000308 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000309 * (takes into account tabs and backspaces)
310 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
311 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000312static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000313{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000314#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000315 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000316 int w = width;
317 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000318#if ENABLE_FEATURE_LESS_REGEXP
319 unsigned old_max_fline = max_fline;
320 time_t last_time = 0;
321 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
322#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000323
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000324 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000325 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000326
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000327 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000328
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000329 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000330 max_fline += last_terminated;
331 if (!last_terminated) {
332 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000333 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000334 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000335 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000336 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000337 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000338 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000339 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000340
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000341 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000342 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000343 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000344 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000345 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000346 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000347 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000348 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000349 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000350 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000351 readpos = 0;
352 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000353 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000354 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000355 }
356 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000357 /* backspace? [needed for manpages] */
358 /* <tab><bs> is (a) insane and */
359 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000360 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000361 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000362 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000363 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000364 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000365 continue;
366 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000367 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000368 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000369 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 new_last_line_pos += 7;
371 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000372 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000373 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000374 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000375 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000376 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000377 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000378 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000379 if (c == '\n') {
380 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000381 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000382 break;
383 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000384 /* NUL is substituted by '\n'! */
385 if (c == '\0') c = '\n';
386 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000387 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000388 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000389 /* Corner case: linewrap with only "" wrapping to next line */
390 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000391 if (!last_terminated && !current_line[0]) {
392 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000393 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000394 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000395 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000396 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000397 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000398 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000399
400 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
401 LINENO(flines[max_fline]) = max_lineno;
402 if (terminated)
403 max_lineno++;
404
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000405 if (max_fline >= MAXLINES) {
406 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000407 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000408 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000409 if (!(option_mask32 & FLAG_S)
410 ? (max_fline > cur_fline + max_displayed_line)
411 : (max_fline >= cur_fline
412 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
413 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000414#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000415 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000416#else
417 if (wanted_match >= num_matches) { /* goto_match called us */
418 fill_match_lines(old_max_fline);
419 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000420 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000421 if (wanted_match < num_matches)
422 break;
423#endif
424 }
425 if (eof_error <= 0) {
426 if (eof_error < 0) {
427 if (errno == EAGAIN) {
428 /* not yet eof or error, reset flag (or else
429 * we will hog CPU - select() will return
430 * immediately */
431 eof_error = 1;
432 } else {
433 print_statusline("read error");
434 }
435 }
436#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000437 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000438#else
439 if (wanted_match < num_matches) {
440 break;
441 } else { /* goto_match called us */
442 time_t t = time(NULL);
443 if (t != last_time) {
444 last_time = t;
445 if (--seconds_p1 == 0)
446 break;
447 }
448 sched_yield();
449 goto again0; /* go loop again (max 2 seconds) */
450 }
451#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000452 }
453 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000454 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000455 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000456 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000457 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000458 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000459#if ENABLE_FEATURE_LESS_REGEXP
460 /* prevent us from being stuck in search for a match */
461 wanted_match = -1;
462#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000463#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000464}
465
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000466#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000467/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000468 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000469static int calc_percent(void)
470{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000471 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000472 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000473}
474
Rob Landley9200e792005-09-15 19:26:59 +0000475/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000476static void m_status_print(void)
477{
Rob Landley9200e792005-09-15 19:26:59 +0000478 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000479
Denis Vlasenko33196372008-02-23 01:25:38 +0000480 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
481 return;
482
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000483 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000484 printf(HIGHLIGHT"%s", filename);
485 if (num_files > 1)
486 printf(" (file %i of %i)", current_file, num_files);
487 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000488 cur_fline + 1, cur_fline + max_displayed_line + 1,
489 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000490 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000491 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000492 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000493 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000494 return;
Rob Landley9200e792005-09-15 19:26:59 +0000495 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000496 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000497 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000498}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000499#endif
500
Rob Landley9200e792005-09-15 19:26:59 +0000501/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000502static void status_print(void)
503{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000504 const char *p;
505
Denis Vlasenko33196372008-02-23 01:25:38 +0000506 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
507 return;
508
Rob Landley9200e792005-09-15 19:26:59 +0000509 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000510#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000511 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000512 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000513 return;
Rob Landley9200e792005-09-15 19:26:59 +0000514 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000515 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000516#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000517
518 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000519 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000520 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000521 return;
522 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000523 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000524 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000525 p = filename;
526 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000527 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000528 p, current_file, num_files);
529 return;
530 }
531 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000532}
533
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000534static void cap_cur_fline(int nlines)
535{
536 int diff;
537 if (cur_fline < 0)
538 cur_fline = 0;
539 if (cur_fline + max_displayed_line > max_fline + TILDES) {
540 cur_fline -= nlines;
541 if (cur_fline < 0)
542 cur_fline = 0;
543 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
544 /* As the number of lines requested was too large, we just move
545 to the end of the file */
546 if (diff > 0)
547 cur_fline += diff;
548 }
549}
550
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000551static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000552 /* NUL: never encountered; TAB: not converted */
553 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000554 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
555 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000556static const char ctrlconv[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000557 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000558 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
559 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
560
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000561static void lineno_str(char *nbuf9, const char *line)
562{
563 nbuf9[0] = '\0';
564 if (option_mask32 & FLAG_N) {
565 const char *fmt;
566 unsigned n;
567
568 if (line == empty_line_marker) {
569 memset(nbuf9, ' ', 8);
570 nbuf9[8] = '\0';
571 return;
572 }
573 /* Width of 7 preserves tab spacing in the text */
574 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000575 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000576 if (n > 9999999) {
577 n %= 10000000;
578 fmt = "%07u ";
579 }
580 sprintf(nbuf9, fmt, n);
581 }
582}
583
584
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000585#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000586static void print_found(const char *line)
587{
588 int match_status;
589 int eflags;
590 char *growline;
591 regmatch_t match_structs;
592
593 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000594 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000595 const char *str = line;
596 char *p = buf;
597 size_t n;
598
599 while (*str) {
600 n = strcspn(str, controls);
601 if (n) {
602 if (!str[n]) break;
603 memcpy(p, str, n);
604 p += n;
605 str += n;
606 }
607 n = strspn(str, controls);
608 memset(p, '.', n);
609 p += n;
610 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000611 }
612 strcpy(p, str);
613
614 /* buf[] holds quarantined version of str */
615
616 /* Each part of the line that matches has the HIGHLIGHT
617 and NORMAL escape sequences placed around it.
618 NB: we regex against line, but insert text
619 from quarantined copy (buf[]) */
620 str = buf;
621 growline = NULL;
622 eflags = 0;
623 goto start;
624
625 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000626 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000627 growline ? : "",
628 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000629 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000630 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000631 free(growline);
632 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000633 str += match_structs.rm_eo;
634 line += match_structs.rm_eo;
635 eflags = REG_NOTBOL;
636 start:
637 /* Most of the time doesn't find the regex, optimize for that */
638 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000639 /* if even "" matches, treat it as "not a match" */
640 if (match_structs.rm_so >= match_structs.rm_eo)
641 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000642 }
643
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000644 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000645 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000646 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000647 return;
648 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000649 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000650 free(growline);
651}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000652#else
653void print_found(const char *line);
654#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000655
656static void print_ascii(const char *str)
657{
658 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000659 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000660 char *p;
661 size_t n;
662
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000663 lineno_str(nbuf9, str);
664 printf(CLEAR_2_EOL"%s", nbuf9);
665
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000666 while (*str) {
667 n = strcspn(str, controls);
668 if (n) {
669 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000670 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000671 str += n;
672 }
673 n = strspn(str, controls);
674 p = buf;
675 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000676 if (*str == 0x7f)
677 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000678 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000679 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
680 /* I want to know who committed this sin */
681 *p++ = '{';
682 else
683 *p++ = ctrlconv[(unsigned char)*str];
684 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000685 } while (--n);
686 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000687 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000688 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000689 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000690}
691
Rob Landley9200e792005-09-15 19:26:59 +0000692/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000693static void buffer_print(void)
694{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000695 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000696
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000697 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000698 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000699 if (pattern_valid)
700 print_found(buffer[i]);
701 else
702 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000703 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000704}
705
Denis Vlasenkod9083952008-10-24 10:42:21 +0000706static void buffer_fill_and_print(void)
707{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000708 unsigned i;
709#if ENABLE_FEATURE_LESS_DASHCMD
710 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000711
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000712 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000713 /* Go back to the beginning of this line */
714 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
715 fpos--;
716 }
717
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000718 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000719 while (i <= max_displayed_line && fpos <= max_fline) {
720 int lineno = LINENO(flines[fpos]);
721 buffer[i] = flines[fpos];
722 i++;
723 do {
724 fpos++;
725 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000726 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000727 && lineno == LINENO(flines[fpos])
728 );
729 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000730#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000731 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
732 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000733 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000734#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000735 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000736 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000737 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000738 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000739}
740
741/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000742static void buffer_down(int nlines)
743{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000744 cur_fline += nlines;
745 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000746 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000747 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000748}
749
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000750static void buffer_up(int nlines)
751{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000752 cur_fline -= nlines;
753 if (cur_fline < 0) cur_fline = 0;
754 read_lines();
755 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000756}
757
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000758static void buffer_line(int linenum)
759{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000760 if (linenum < 0)
761 linenum = 0;
762 cur_fline = linenum;
763 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000764 if (linenum + max_displayed_line > max_fline)
765 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000766 if (linenum < 0)
767 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000768 cur_fline = linenum;
769 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000770}
771
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000772static void open_file_and_read_lines(void)
773{
774 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000775 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000776 } else {
777 /* "less" with no arguments in argv[] */
778 /* For status line only */
779 filename = xstrdup(bb_msg_standard_input);
780 }
781 readpos = 0;
782 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000783 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000784 terminated = 1;
785 read_lines();
786}
787
788/* Reinitialize everything for a new file - free the memory and start over */
789static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000790{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000791 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000792
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000793 if (flines) {
794 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000795 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000796 free(flines);
797 flines = NULL;
798 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000799
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000800 max_fline = -1;
801 cur_fline = 0;
802 max_lineno = 0;
803 open_file_and_read_lines();
804 buffer_fill_and_print();
805}
806
Denys Vlasenko020f4062009-05-17 16:44:54 +0200807static int getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000808{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000809 int rd;
Denis Vlasenko33196372008-02-23 01:25:38 +0000810 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000811
Denis Vlasenko33196372008-02-23 01:25:38 +0000812 pfd[0].fd = STDIN_FILENO;
813 pfd[0].events = POLLIN;
814 pfd[1].fd = kbd_fd;
815 pfd[1].events = POLLIN;
816 again:
817 tcsetattr(kbd_fd, TCSANOW, &term_less);
818 /* NB: select/poll returns whenever read will not block. Therefore:
819 * if eof is reached, select/poll will return immediately
820 * because read will immediately return 0 bytes.
821 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000822 * (switch fd into O_NONBLOCK'ed mode to avoid it)
823 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000824 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000825 /* Are we interested in stdin? */
826//TODO: reuse code for determining this
827 if (!(option_mask32 & FLAG_S)
828 ? !(max_fline > cur_fline + max_displayed_line)
829 : !(max_fline >= cur_fline
830 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000831 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000832 if (eof_error > 0) /* did NOT reach eof yet */
833 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000834 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000835 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000836 if (less_gets_pos >= 0)
837 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
838 fflush(stdout);
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000839
Denys Vlasenko020f4062009-05-17 16:44:54 +0200840 if (kbd_input[0] == 0) { /* if nothing is buffered */
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000841#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000842 while (1) {
843 int r;
844 /* NB: SIGWINCH interrupts poll() */
845 r = poll(pfd + rd, 2 - rd, -1);
846 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
847 return '\\'; /* anything which has no defined function */
848 if (r) break;
849 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000850#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000851 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000852#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000853 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000854
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000855 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
856 * would not block even if there is no input available */
Denys Vlasenko020f4062009-05-17 16:44:54 +0200857 rd = read_key(kbd_fd, kbd_input);
Denis Vlasenko64974792008-10-31 03:04:55 +0000858 if (rd == -1) {
859 if (errno == EAGAIN) {
860 /* No keyboard input available. Since poll() did return,
861 * we should have input on stdin */
862 read_lines();
863 buffer_fill_and_print();
864 goto again;
865 }
866 /* EOF/error (ssh session got killed etc) */
867 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000868 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000869 set_tty_cooked();
870 return rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000871}
872
Denys Vlasenko020f4062009-05-17 16:44:54 +0200873/* Grab a character from input without requiring the return key.
874 * May return KEYCODE_xxx values.
875 * Note that this function works best with raw input. */
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000876static int less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000877{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000878 int i;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000879
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000880 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000881 less_gets_pos = pos;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000882 i = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000883 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000884
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000885 /* Discard Ctrl-something chars */
886 if (i >= 0 && i < ' ' && i != 0x0d && i != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000887 goto again;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000888 return i;
889}
890
891static char* less_gets(int sz)
892{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000893 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000894 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000895 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000896
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000897 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +0000898 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000899 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000900 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +0000901 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +0000902 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000903 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000904 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +0000905 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000906 if (c == 0x7f)
907 c = 8;
908 if (c == 8 && i) {
909 printf("\x8 \x8");
910 i--;
911 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000912 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000913 continue;
914 if (i >= width - sz - 1)
915 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000916 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000917 result[i++] = c;
918 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000919 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000920}
921
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000922static void examine_file(void)
923{
Denis Vlasenko33196372008-02-23 01:25:38 +0000924 char *new_fname;
925
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000926 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000927 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000928 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +0000929 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000930 err:
931 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +0000932 return;
933 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000934 if (access(new_fname, R_OK) != 0) {
935 print_statusline("Cannot read this file");
936 goto err;
937 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000938 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +0000939 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000940 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000941 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000942 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000943 num_files++; */
944 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000945 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000946 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000947}
948
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000949/* This function changes the file currently being paged. direction can be one of the following:
950 * -1: go back one file
951 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000952 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000953static void change_file(int direction)
954{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000955 if (current_file != ((direction > 0) ? num_files : 1)) {
956 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000957 free(filename);
958 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000959 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000960 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000961 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000962 }
963}
964
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000965static void remove_current_file(void)
966{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000967 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000968
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000969 if (num_files < 2)
970 return;
971
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000972 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000973 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000974 for (i = 3; i <= num_files; i++)
975 files[i - 2] = files[i - 1];
976 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000977 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000978 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000979 for (i = 2; i <= num_files; i++)
980 files[i - 2] = files[i - 1];
981 num_files--;
982 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000983 }
984}
985
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000986static void colon_process(void)
987{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000988 int keypress;
989
990 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000991 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000992
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000993 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000994 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000995 case 'd':
996 remove_current_file();
997 break;
998 case 'e':
999 examine_file();
1000 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001001#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001002 case 'f':
1003 m_status_print();
1004 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001005#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001006 case 'n':
1007 change_file(1);
1008 break;
1009 case 'p':
1010 change_file(-1);
1011 break;
1012 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001013 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001014 break;
1015 case 'x':
1016 change_file(0);
1017 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001018 }
1019}
1020
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001021#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001022static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001023{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001024 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001025 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001026 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001027 match = 0;
1028 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001029}
1030
Rob Landleya2e98042006-04-18 01:53:41 +00001031static void goto_match(int match)
1032{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001033 if (!pattern_valid)
1034 return;
1035 if (match < 0)
1036 match = 0;
1037 /* Try to find next match if eof isn't reached yet */
1038 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001039 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001040 read_lines();
1041 }
1042 if (num_matches) {
1043 normalize_match_pos(match);
1044 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001045 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001046 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001047 }
Rob Landleya2e98042006-04-18 01:53:41 +00001048}
1049
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001050static void fill_match_lines(unsigned pos)
1051{
1052 if (!pattern_valid)
1053 return;
1054 /* Run the regex on each line of the current file */
1055 while (pos <= max_fline) {
1056 /* If this line matches */
1057 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1058 /* and we didn't match it last time */
1059 && !(num_matches && match_lines[num_matches-1] == pos)
1060 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001061 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001062 match_lines[num_matches++] = pos;
1063 }
1064 pos++;
1065 }
1066}
1067
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001068static void regex_process(void)
1069{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001070 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001071
1072 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001073 free(match_lines);
1074 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001075 match_pos = 0;
1076 num_matches = 0;
1077 if (pattern_valid) {
1078 regfree(&pattern);
1079 pattern_valid = 0;
1080 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001081
Rob Landleya2e98042006-04-18 01:53:41 +00001082 /* Get the uncompiled regular expression from the user */
1083 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001084 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001085 uncomp_regex = less_gets(1);
1086 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001087 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001088 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001089 return;
1090 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001091
Rob Landleya2e98042006-04-18 01:53:41 +00001092 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001093 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001094 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001095 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001096 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001097 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001098 free(err);
1099 return;
1100 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001101
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001102 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001103 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001104 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001105 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001106 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001107 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001108 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001109 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001110 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1111 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001112
1113 /* It's possible that no matches are found yet.
1114 * goto_match() will read input looking for match,
1115 * if needed */
1116 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001117}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001118#endif
1119
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001120static void number_process(int first_digit)
1121{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001122 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001123 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001124 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001125 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001126
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001127 num_input[0] = first_digit;
1128
1129 /* Clear the current line, print a prompt, and then print the digit */
1130 clear_line();
1131 printf(":%c", first_digit);
1132
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001133 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001134 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001135 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001136 keypress = less_getch(i + 1);
1137 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001138 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001139 num_input[i] = keypress;
1140 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001141 i++;
1142 }
1143
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001144 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001145 num = bb_strtou(num_input, NULL, 10);
1146 /* on format error, num == -1 */
1147 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001148 buffer_print();
1149 return;
1150 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001151
1152 /* We now know the number and the letter entered, so we process them */
1153 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001154 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001155 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001156 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001157 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001158 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001159 break;
1160 case 'g': case '<': case 'G': case '>':
1161 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001162 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001163 buffer_line(num - 1);
1164 break;
1165 case 'p': case '%':
1166 num = num * (max_fline / 100); /* + max_fline / 2; */
1167 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001168 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001169 buffer_line(num);
1170 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001171#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001172 case 'n':
1173 goto_match(match_pos + num);
1174 break;
1175 case '/':
1176 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1177 regex_process();
1178 break;
1179 case '?':
1180 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1181 regex_process();
1182 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001183#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001184 }
1185}
1186
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001187#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001188static void flag_change(void)
1189{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001190 int keypress;
1191
1192 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001193 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001194 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001195
1196 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001197 case 'M':
1198 option_mask32 ^= FLAG_M;
1199 break;
1200 case 'm':
1201 option_mask32 ^= FLAG_m;
1202 break;
1203 case 'E':
1204 option_mask32 ^= FLAG_E;
1205 break;
1206 case '~':
1207 option_mask32 ^= FLAG_TILDE;
1208 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001209 case 'S':
1210 option_mask32 ^= FLAG_S;
1211 buffer_fill_and_print();
1212 break;
1213#if ENABLE_FEATURE_LESS_LINENUMS
1214 case 'N':
1215 option_mask32 ^= FLAG_N;
1216 re_wrap();
1217 buffer_fill_and_print();
1218 break;
1219#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001220 }
1221}
1222
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001223#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001224static void show_flag_status(void)
1225{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001226 int keypress;
1227 int flag_val;
1228
1229 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001230 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001231 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001232
1233 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001234 case 'M':
1235 flag_val = option_mask32 & FLAG_M;
1236 break;
1237 case 'm':
1238 flag_val = option_mask32 & FLAG_m;
1239 break;
1240 case '~':
1241 flag_val = option_mask32 & FLAG_TILDE;
1242 break;
1243 case 'N':
1244 flag_val = option_mask32 & FLAG_N;
1245 break;
1246 case 'E':
1247 flag_val = option_mask32 & FLAG_E;
1248 break;
1249 default:
1250 flag_val = 0;
1251 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001252 }
1253
1254 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001255 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001256}
1257#endif
1258
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001259#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1260
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001261static void save_input_to_file(void)
1262{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001263 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001264 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001265 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001266 FILE *fp;
1267
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001268 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001269 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001270 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001271 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001272 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001273 msg = "Error opening log file";
1274 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001275 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001276 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001277 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001278 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001279 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001280 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001281 ret:
1282 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001283 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001284}
1285
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001286#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001287static void add_mark(void)
1288{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001289 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001290
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001291 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001292 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001293
1294 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001295 /* If we exceed 15 marks, start overwriting previous ones */
1296 if (num_marks == 14)
1297 num_marks = 0;
1298
1299 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001300 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001301 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001302 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001303 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001304 }
1305}
1306
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001307static void goto_mark(void)
1308{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001309 int letter;
1310 int i;
1311
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001312 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001313 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001314 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001315
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001316 if (isalpha(letter)) {
1317 for (i = 0; i <= num_marks; i++)
1318 if (letter == mark_lines[i][0]) {
1319 buffer_line(mark_lines[i][1]);
1320 break;
1321 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001322 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001323 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001324 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001325 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001326}
1327#endif
1328
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001329#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001330static char opp_bracket(char bracket)
1331{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001332 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001333 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1334 bracket++;
1335 case '(': /* ')' == '(' + 1 */
1336 bracket++;
1337 break;
1338 case '}': case ']':
1339 bracket--;
1340 case ')':
1341 bracket--;
1342 break;
1343 };
1344 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001345}
1346
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001347static void match_right_bracket(char bracket)
1348{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001349 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001350
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001351 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001352 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001353 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001354 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001355 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001356 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001357 if (strchr(flines[i], bracket) != NULL) {
1358 buffer_line(i);
1359 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001360 }
1361 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001362 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001363}
1364
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001365static void match_left_bracket(char bracket)
1366{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001367 int i;
1368
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001369 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001370 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001371 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001372 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001373
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001374 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001375 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001376 if (strchr(flines[i], bracket) != NULL) {
1377 buffer_line(i);
1378 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001379 }
1380 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001381 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001382}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001383#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001384
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001385static void keypress_process(int keypress)
1386{
Rob Landley9200e792005-09-15 19:26:59 +00001387 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001388 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001389 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001390 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001391 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001392 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001393 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001394 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001395 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001396 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001397 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001398 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001399 break;
1400 case 'd':
1401 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001402 break;
1403 case 'u':
1404 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001405 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001406 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001407 buffer_line(0);
1408 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001409 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001410 cur_fline = MAXLINES;
1411 read_lines();
1412 buffer_line(cur_fline);
1413 break;
1414 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001415 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001416 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001417#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001418 case 'm':
1419 add_mark();
1420 buffer_print();
1421 break;
1422 case '\'':
1423 goto_mark();
1424 buffer_print();
1425 break;
Rob Landley9200e792005-09-15 19:26:59 +00001426#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001427 case 'r': case 'R':
1428 buffer_print();
1429 break;
1430 /*case 'R':
1431 full_repaint();
1432 break;*/
1433 case 's':
1434 save_input_to_file();
1435 break;
1436 case 'E':
1437 examine_file();
1438 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001439#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001440 case '=':
1441 m_status_print();
1442 break;
Rob Landley9200e792005-09-15 19:26:59 +00001443#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001444#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001445 case '/':
1446 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1447 regex_process();
1448 break;
1449 case 'n':
1450 goto_match(match_pos + 1);
1451 break;
1452 case 'N':
1453 goto_match(match_pos - 1);
1454 break;
1455 case '?':
1456 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1457 regex_process();
1458 break;
Rob Landley9200e792005-09-15 19:26:59 +00001459#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001460#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001461 case '-':
1462 flag_change();
1463 buffer_print();
1464 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001465#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001466 case '_':
1467 show_flag_status();
1468 break;
Rob Landley9200e792005-09-15 19:26:59 +00001469#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001470#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001471#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001472 case '{': case '(': case '[':
1473 match_right_bracket(keypress);
1474 break;
1475 case '}': case ')': case ']':
1476 match_left_bracket(keypress);
1477 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001478#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001479 case ':':
1480 colon_process();
1481 break;
Rob Landley9200e792005-09-15 19:26:59 +00001482 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001483
Rob Landley9200e792005-09-15 19:26:59 +00001484 if (isdigit(keypress))
1485 number_process(keypress);
1486}
1487
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001488static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001489{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001490 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001491}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001492
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001493#if ENABLE_FEATURE_LESS_WINCH
1494static void sigwinch_handler(int sig UNUSED_PARAM)
1495{
1496 winch_counter++;
1497}
1498#endif
1499
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001500int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001501int less_main(int argc, char **argv)
1502{
Rob Landley9200e792005-09-15 19:26:59 +00001503 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001504
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001505 INIT_G();
1506
Denis Vlasenko95b30712006-12-31 19:23:31 +00001507 /* TODO: -x: do not interpret backspace, -xx: tab also */
1508 /* -xxx: newline also */
1509 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001510 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
Rob Landley9200e792005-09-15 19:26:59 +00001511 argc -= optind;
1512 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001513 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001514 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001515
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001516 /* Another popular pager, most, detects when stdout
1517 * is not a tty and turns into cat. This makes sense. */
1518 if (!isatty(STDOUT_FILENO))
1519 return bb_cat(argv);
1520
Rob Landley9200e792005-09-15 19:26:59 +00001521 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001522 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001523 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001524 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001525 bb_show_usage();
1526 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001527 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001528 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001529 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001530
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001531 if (option_mask32 & FLAG_TILDE)
1532 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001533
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001534 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1535 if (kbd_fd < 0)
1536 return bb_cat(argv);
1537 ndelay_on(kbd_fd);
1538
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001539 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001540 term_less = term_orig;
1541 term_less.c_lflag &= ~(ICANON | ECHO);
1542 term_less.c_iflag &= ~(IXON | ICRNL);
1543 /*term_less.c_oflag &= ~ONLCR;*/
1544 term_less.c_cc[VMIN] = 1;
1545 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001546
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001547 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1548 /* 20: two tabstops + 4 */
1549 if (width < 20 || max_displayed_line < 3)
1550 return bb_cat(argv);
1551 max_displayed_line -= 2;
1552
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001553 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001554 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001555#if ENABLE_FEATURE_LESS_WINCH
1556 signal(SIGWINCH, sigwinch_handler);
1557#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001558
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001559 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001560 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001561 while (1) {
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001562#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001563 while (WINCH_COUNTER) {
1564 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001565 winch_counter--;
1566 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1567 /* 20: two tabstops + 4 */
1568 if (width < 20)
1569 width = 20;
1570 if (max_displayed_line < 3)
1571 max_displayed_line = 3;
1572 max_displayed_line -= 2;
1573 free(buffer);
1574 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001575 /* Avoid re-wrap and/or redraw if we already know
1576 * we need to do it again. These ops are expensive */
1577 if (WINCH_COUNTER)
1578 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001579 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001580 if (WINCH_COUNTER)
1581 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001582 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001583 /* This took some time. Loop back and check,
1584 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001585 }
1586#endif
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001587 keypress = less_getch(-1); /* -1: do not position cursor */
Rob Landley9200e792005-09-15 19:26:59 +00001588 keypress_process(keypress);
1589 }
1590}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001591
1592/*
1593Help text of less version 418 is below.
1594If you are implementing something, keeping
1595key and/or command line switch compatibility is a good idea:
1596
1597
1598 SUMMARY OF LESS COMMANDS
1599
1600 Commands marked with * may be preceded by a number, N.
1601 Notes in parentheses indicate the behavior if N is given.
1602 h H Display this help.
1603 q :q Q :Q ZZ Exit.
1604 ---------------------------------------------------------------------------
1605 MOVING
1606 e ^E j ^N CR * Forward one line (or N lines).
1607 y ^Y k ^K ^P * Backward one line (or N lines).
1608 f ^F ^V SPACE * Forward one window (or N lines).
1609 b ^B ESC-v * Backward one window (or N lines).
1610 z * Forward one window (and set window to N).
1611 w * Backward one window (and set window to N).
1612 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1613 d ^D * Forward one half-window (and set half-window to N).
1614 u ^U * Backward one half-window (and set half-window to N).
1615 ESC-) RightArrow * Left one half screen width (or N positions).
1616 ESC-( LeftArrow * Right one half screen width (or N positions).
1617 F Forward forever; like "tail -f".
1618 r ^R ^L Repaint screen.
1619 R Repaint screen, discarding buffered input.
1620 ---------------------------------------------------
1621 Default "window" is the screen height.
1622 Default "half-window" is half of the screen height.
1623 ---------------------------------------------------------------------------
1624 SEARCHING
1625 /pattern * Search forward for (N-th) matching line.
1626 ?pattern * Search backward for (N-th) matching line.
1627 n * Repeat previous search (for N-th occurrence).
1628 N * Repeat previous search in reverse direction.
1629 ESC-n * Repeat previous search, spanning files.
1630 ESC-N * Repeat previous search, reverse dir. & spanning files.
1631 ESC-u Undo (toggle) search highlighting.
1632 ---------------------------------------------------
1633 Search patterns may be modified by one or more of:
1634 ^N or ! Search for NON-matching lines.
1635 ^E or * Search multiple files (pass thru END OF FILE).
1636 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1637 ^K Highlight matches, but don't move (KEEP position).
1638 ^R Don't use REGULAR EXPRESSIONS.
1639 ---------------------------------------------------------------------------
1640 JUMPING
1641 g < ESC-< * Go to first line in file (or line N).
1642 G > ESC-> * Go to last line in file (or line N).
1643 p % * Go to beginning of file (or N percent into file).
1644 t * Go to the (N-th) next tag.
1645 T * Go to the (N-th) previous tag.
1646 { ( [ * Find close bracket } ) ].
1647 } ) ] * Find open bracket { ( [.
1648 ESC-^F <c1> <c2> * Find close bracket <c2>.
1649 ESC-^B <c1> <c2> * Find open bracket <c1>
1650 ---------------------------------------------------
1651 Each "find close bracket" command goes forward to the close bracket
1652 matching the (N-th) open bracket in the top line.
1653 Each "find open bracket" command goes backward to the open bracket
1654 matching the (N-th) close bracket in the bottom line.
1655 m<letter> Mark the current position with <letter>.
1656 '<letter> Go to a previously marked position.
1657 '' Go to the previous position.
1658 ^X^X Same as '.
1659 ---------------------------------------------------
1660 A mark is any upper-case or lower-case letter.
1661 Certain marks are predefined:
1662 ^ means beginning of the file
1663 $ means end of the file
1664 ---------------------------------------------------------------------------
1665 CHANGING FILES
1666 :e [file] Examine a new file.
1667 ^X^V Same as :e.
1668 :n * Examine the (N-th) next file from the command line.
1669 :p * Examine the (N-th) previous file from the command line.
1670 :x * Examine the first (or N-th) file from the command line.
1671 :d Delete the current file from the command line list.
1672 = ^G :f Print current file name.
1673 ---------------------------------------------------------------------------
1674 MISCELLANEOUS COMMANDS
1675 -<flag> Toggle a command line option [see OPTIONS below].
1676 --<name> Toggle a command line option, by name.
1677 _<flag> Display the setting of a command line option.
1678 __<name> Display the setting of an option, by name.
1679 +cmd Execute the less cmd each time a new file is examined.
1680 !command Execute the shell command with $SHELL.
1681 |Xcommand Pipe file between current pos & mark X to shell command.
1682 v Edit the current file with $VISUAL or $EDITOR.
1683 V Print version number of "less".
1684 ---------------------------------------------------------------------------
1685 OPTIONS
1686 Most options may be changed either on the command line,
1687 or from within less by using the - or -- command.
1688 Options may be given in one of two forms: either a single
1689 character preceded by a -, or a name preceeded by --.
1690 -? ........ --help
1691 Display help (from command line).
1692 -a ........ --search-skip-screen
1693 Forward search skips current screen.
1694 -b [N] .... --buffers=[N]
1695 Number of buffers.
1696 -B ........ --auto-buffers
1697 Don't automatically allocate buffers for pipes.
1698 -c ........ --clear-screen
1699 Repaint by clearing rather than scrolling.
1700 -d ........ --dumb
1701 Dumb terminal.
1702 -D [xn.n] . --color=xn.n
1703 Set screen colors. (MS-DOS only)
1704 -e -E .... --quit-at-eof --QUIT-AT-EOF
1705 Quit at end of file.
1706 -f ........ --force
1707 Force open non-regular files.
1708 -F ........ --quit-if-one-screen
1709 Quit if entire file fits on first screen.
1710 -g ........ --hilite-search
1711 Highlight only last match for searches.
1712 -G ........ --HILITE-SEARCH
1713 Don't highlight any matches for searches.
1714 -h [N] .... --max-back-scroll=[N]
1715 Backward scroll limit.
1716 -i ........ --ignore-case
1717 Ignore case in searches that do not contain uppercase.
1718 -I ........ --IGNORE-CASE
1719 Ignore case in all searches.
1720 -j [N] .... --jump-target=[N]
1721 Screen position of target lines.
1722 -J ........ --status-column
1723 Display a status column at left edge of screen.
1724 -k [file] . --lesskey-file=[file]
1725 Use a lesskey file.
1726 -L ........ --no-lessopen
1727 Ignore the LESSOPEN environment variable.
1728 -m -M .... --long-prompt --LONG-PROMPT
1729 Set prompt style.
1730 -n -N .... --line-numbers --LINE-NUMBERS
1731 Don't use line numbers.
1732 -o [file] . --log-file=[file]
1733 Copy to log file (standard input only).
1734 -O [file] . --LOG-FILE=[file]
1735 Copy to log file (unconditionally overwrite).
1736 -p [pattern] --pattern=[pattern]
1737 Start at pattern (from command line).
1738 -P [prompt] --prompt=[prompt]
1739 Define new prompt.
1740 -q -Q .... --quiet --QUIET --silent --SILENT
1741 Quiet the terminal bell.
1742 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1743 Output "raw" control characters.
1744 -s ........ --squeeze-blank-lines
1745 Squeeze multiple blank lines.
1746 -S ........ --chop-long-lines
1747 Chop long lines.
1748 -t [tag] .. --tag=[tag]
1749 Find a tag.
1750 -T [tagsfile] --tag-file=[tagsfile]
1751 Use an alternate tags file.
1752 -u -U .... --underline-special --UNDERLINE-SPECIAL
1753 Change handling of backspaces.
1754 -V ........ --version
1755 Display the version number of "less".
1756 -w ........ --hilite-unread
1757 Highlight first new line after forward-screen.
1758 -W ........ --HILITE-UNREAD
1759 Highlight first new line after any forward movement.
1760 -x [N[,...]] --tabs=[N[,...]]
1761 Set tab stops.
1762 -X ........ --no-init
1763 Don't use termcap init/deinit strings.
1764 --no-keypad
1765 Don't use termcap keypad init/deinit strings.
1766 -y [N] .... --max-forw-scroll=[N]
1767 Forward scroll limit.
1768 -z [N] .... --window=[N]
1769 Set size of window.
1770 -" [c[c]] . --quotes=[c[c]]
1771 Set shell quote characters.
1772 -~ ........ --tilde
1773 Don't display tildes after end of file.
1774 -# [N] .... --shift=[N]
1775 Horizontal scroll amount (0 = one half screen width)
1776
1777 ---------------------------------------------------------------------------
1778 LINE EDITING
1779 These keys can be used to edit text being entered
1780 on the "command line" at the bottom of the screen.
1781 RightArrow ESC-l Move cursor right one character.
1782 LeftArrow ESC-h Move cursor left one character.
1783 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1784 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1785 HOME ESC-0 Move cursor to start of line.
1786 END ESC-$ Move cursor to end of line.
1787 BACKSPACE Delete char to left of cursor.
1788 DELETE ESC-x Delete char under cursor.
1789 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1790 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1791 CNTL-U ESC (MS-DOS only) Delete entire line.
1792 UpArrow ESC-k Retrieve previous command line.
1793 DownArrow ESC-j Retrieve next command line.
1794 TAB Complete filename & cycle.
1795 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1796 CNTL-L Complete filename, list all.
1797*/