blob: 702c4a891450652cc850aec7234f97d6766c6dbc [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;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +000099 smalluint kbd_input_size;
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000100 struct termios term_orig, term_less;
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000101 char kbd_input[KEYCODE_BUFFER_SIZE];
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000102};
103#define G (*ptr_to_globals)
104#define cur_fline (G.cur_fline )
105#define kbd_fd (G.kbd_fd )
Denis Vlasenko33196372008-02-23 01:25:38 +0000106#define less_gets_pos (G.less_gets_pos )
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000107#define last_line_pos (G.last_line_pos )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000108#define max_fline (G.max_fline )
109#define max_lineno (G.max_lineno )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000110#define max_displayed_line (G.max_displayed_line)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000111#define width (G.width )
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000112#define winch_counter (G.winch_counter )
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000113/* This one is 100% not cached by compiler on read access */
114#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000115#define eof_error (G.eof_error )
116#define readpos (G.readpos )
117#define readeof (G.readeof )
118#define buffer (G.buffer )
119#define flines (G.flines )
120#define empty_line_marker (G.empty_line_marker )
121#define num_files (G.num_files )
122#define current_file (G.current_file )
123#define filename (G.filename )
124#define files (G.files )
125#define num_marks (G.num_marks )
126#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000127#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000128#define match_lines (G.match_lines )
129#define match_pos (G.match_pos )
130#define num_matches (G.num_matches )
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000131#define wanted_match (G.wanted_match )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000132#define pattern (G.pattern )
133#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000134#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000135#define terminated (G.terminated )
136#define term_orig (G.term_orig )
137#define term_less (G.term_less )
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000138#define kbd_input_size (G.kbd_input_size )
139#define kbd_input (G.kbd_input )
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000140#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000141 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
142 less_gets_pos = -1; \
143 empty_line_marker = "~"; \
144 num_files = 1; \
145 current_file = 1; \
146 eof_error = 1; \
147 terminated = 1; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000148 IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000149} while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000150
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000151/* flines[] are lines read from stdin, each in malloc'ed buffer.
152 * Line numbers are stored as uint32_t prepended to each line.
153 * Pointer is adjusted so that flines[i] points directly past
154 * line number. Accesor: */
155#define MEMPTR(p) ((char*)(p) - 4)
156#define LINENO(p) (*(uint32_t*)((p) - 4))
157
158
Rob Landley9200e792005-09-15 19:26:59 +0000159/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000160static void set_tty_cooked(void)
161{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000162 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000163 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000164}
165
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000166/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000167 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000168static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000169{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000170 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000171}
172
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000173static void clear_line(void)
174{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000175 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000176}
177
178static void print_hilite(const char *str)
179{
180 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000181}
182
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000183static void print_statusline(const char *str)
184{
185 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000186 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000187}
188
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000189/* Exit the program gracefully */
190static void less_exit(int code)
191{
192 set_tty_cooked();
193 clear_line();
194 if (code < 0)
195 kill_myself_with_sig(- code); /* does not return */
196 exit(code);
197}
198
Denis Vlasenkof3dcd3c2008-10-26 00:24:38 +0000199#if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
200 || ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkod9083952008-10-24 10:42:21 +0000201static void re_wrap(void)
202{
203 int w = width;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000204 int new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000205 int src_idx;
206 int dst_idx;
207 int new_cur_fline = 0;
208 uint32_t lineno;
209 char linebuf[w + 1];
210 const char **old_flines = flines;
211 const char *s;
212 char **new_flines = NULL;
213 char *d;
214
215 if (option_mask32 & FLAG_N)
216 w -= 8;
217
218 src_idx = 0;
219 dst_idx = 0;
220 s = old_flines[0];
221 lineno = LINENO(s);
222 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000223 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000224 while (1) {
225 *d = *s;
226 if (*d != '\0') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000227 new_line_pos++;
228 if (*d == '\t') /* tab */
229 new_line_pos += 7;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000230 s++;
231 d++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000232 if (new_line_pos >= w) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000233 int sz;
234 /* new line is full, create next one */
235 *d = '\0';
236 next_new:
237 sz = (d - linebuf) + 1; /* + 1: NUL */
238 d = ((char*)xmalloc(sz + 4)) + 4;
239 LINENO(d) = lineno;
240 memcpy(d, linebuf, sz);
241 new_flines = xrealloc_vector(new_flines, 8, dst_idx);
242 new_flines[dst_idx] = d;
243 dst_idx++;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000244 if (new_line_pos < w) {
245 /* if we came here thru "goto next_new" */
Denis Vlasenkod9083952008-10-24 10:42:21 +0000246 if (src_idx > max_fline)
247 break;
248 lineno = LINENO(s);
249 }
250 d = linebuf;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000251 new_line_pos = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000252 }
253 continue;
254 }
255 /* *d == NUL: old line ended, go to next old one */
256 free(MEMPTR(old_flines[src_idx]));
257 /* btw, convert cur_fline... */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000258 if (cur_fline == src_idx)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000259 new_cur_fline = dst_idx;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000260 src_idx++;
261 /* no more lines? finish last new line (and exit the loop) */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000262 if (src_idx > max_fline)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000263 goto next_new;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000264 s = old_flines[src_idx];
265 if (lineno != LINENO(s)) {
266 /* this is not a continuation line!
267 * create next _new_ line too */
268 goto next_new;
269 }
270 }
271
272 free(old_flines);
273 flines = (const char **)new_flines;
274
275 max_fline = dst_idx - 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000276 last_line_pos = new_line_pos;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000277 cur_fline = new_cur_fline;
278 /* max_lineno is screen-size independent */
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000279#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000280 pattern_valid = 0;
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000281#endif
Denis Vlasenkod9083952008-10-24 10:42:21 +0000282}
283#endif
284
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000285#if ENABLE_FEATURE_LESS_REGEXP
286static void fill_match_lines(unsigned pos);
287#else
288#define fill_match_lines(pos) ((void)0)
289#endif
290
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000291/* Devilishly complex routine.
292 *
293 * Has to deal with EOF and EPIPE on input,
294 * with line wrapping, with last line not ending in '\n'
295 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000296 * It reads input again if last time we got an EOF (thus supporting
297 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000298 *
299 * Variables used:
300 * flines[] - array of lines already read. Linewrap may cause
301 * one source file line to occupy several flines[n].
302 * flines[max_fline] - last line, possibly incomplete.
303 * terminated - 1 if flines[max_fline] is 'terminated'
304 * (if there was '\n' [which isn't stored itself, we just remember
305 * that it was seen])
306 * max_lineno - last line's number, this one doesn't increment
307 * on line wrap, only on "real" new lines.
308 * readbuf[0..readeof-1] - small preliminary buffer.
309 * readbuf[readpos] - next character to add to current line.
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000310 * last_line_pos - screen line position of next char to be read
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000311 * (takes into account tabs and backspaces)
312 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
313 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000314static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000315{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000316#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000317 char *current_line, *p;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000318 int w = width;
319 char last_terminated = terminated;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000320#if ENABLE_FEATURE_LESS_REGEXP
321 unsigned old_max_fline = max_fline;
322 time_t last_time = 0;
323 unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
324#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000325
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000326 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000327 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000328
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000329 IF_FEATURE_LESS_REGEXP(again0:)
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000330
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000331 p = current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000332 max_fline += last_terminated;
333 if (!last_terminated) {
334 const char *cp = flines[max_fline];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000335 strcpy(p, cp);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000336 p += strlen(current_line);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000337 free(MEMPTR(flines[max_fline]));
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000338 /* last_line_pos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000339 } else {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000340 last_line_pos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000341 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000342
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000343 while (1) { /* read lines until we reach cur_fline or wanted_match */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000344 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000345 terminated = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000346 while (1) { /* read chars until we have a line */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000347 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000348 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000349 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000350 ndelay_on(0);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000351 eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000352 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000353 readpos = 0;
354 readeof = eof_error;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000355 if (eof_error <= 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000356 goto reached_eof;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000357 }
358 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000359 /* backspace? [needed for manpages] */
360 /* <tab><bs> is (a) insane and */
361 /* (b) harder to do correctly, so we refuse to do it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000362 if (c == '\x8' && last_line_pos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000363 readpos++; /* eat it */
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000364 last_line_pos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000365 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000366 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000367 continue;
368 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000369 {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000370 size_t new_last_line_pos = last_line_pos + 1;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000371 if (c == '\t') {
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000372 new_last_line_pos += 7;
373 new_last_line_pos &= (~7);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000374 }
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000375 if ((int)new_last_line_pos >= w)
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000376 break;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000377 last_line_pos = new_last_line_pos;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000378 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000379 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000380 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000381 if (c == '\n') {
382 terminated = 1;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000383 last_line_pos = 0;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000384 break;
385 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000386 /* NUL is substituted by '\n'! */
387 if (c == '\0') c = '\n';
388 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000389 *p = '\0';
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000390 } /* end of "read chars until we have a line" loop */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000391 /* Corner case: linewrap with only "" wrapping to next line */
392 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000393 if (!last_terminated && !current_line[0]) {
394 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000395 max_lineno++;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000396 continue;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000397 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000398 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000399 last_terminated = terminated;
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000400 flines = xrealloc_vector(flines, 8, max_fline);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000401
402 flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
403 LINENO(flines[max_fline]) = max_lineno;
404 if (terminated)
405 max_lineno++;
406
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000407 if (max_fline >= MAXLINES) {
408 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000409 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000410 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000411 if (!(option_mask32 & FLAG_S)
412 ? (max_fline > cur_fline + max_displayed_line)
413 : (max_fline >= cur_fline
414 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
415 ) {
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000416#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000417 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000418#else
419 if (wanted_match >= num_matches) { /* goto_match called us */
420 fill_match_lines(old_max_fline);
421 old_max_fline = max_fline;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000422 }
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000423 if (wanted_match < num_matches)
424 break;
425#endif
426 }
427 if (eof_error <= 0) {
428 if (eof_error < 0) {
429 if (errno == EAGAIN) {
430 /* not yet eof or error, reset flag (or else
431 * we will hog CPU - select() will return
432 * immediately */
433 eof_error = 1;
434 } else {
435 print_statusline("read error");
436 }
437 }
438#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000439 break;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000440#else
441 if (wanted_match < num_matches) {
442 break;
443 } else { /* goto_match called us */
444 time_t t = time(NULL);
445 if (t != last_time) {
446 last_time = t;
447 if (--seconds_p1 == 0)
448 break;
449 }
450 sched_yield();
451 goto again0; /* go loop again (max 2 seconds) */
452 }
453#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000454 }
455 max_fline++;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000456 current_line = ((char*)xmalloc(w + 4)) + 4;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000457 p = current_line;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000458 last_line_pos = 0;
Denis Vlasenko107fe7c2008-03-17 08:38:45 +0000459 } /* end of "read lines until we reach cur_fline" loop */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000460 fill_match_lines(old_max_fline);
Denis Vlasenko24f824e2008-04-13 08:32:51 +0000461#if ENABLE_FEATURE_LESS_REGEXP
462 /* prevent us from being stuck in search for a match */
463 wanted_match = -1;
464#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000465#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000466}
467
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000468#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000469/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000470 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000471static int calc_percent(void)
472{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000473 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000474 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000475}
476
Rob Landley9200e792005-09-15 19:26:59 +0000477/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000478static void m_status_print(void)
479{
Rob Landley9200e792005-09-15 19:26:59 +0000480 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000481
Denis Vlasenko33196372008-02-23 01:25:38 +0000482 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
483 return;
484
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000485 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000486 printf(HIGHLIGHT"%s", filename);
487 if (num_files > 1)
488 printf(" (file %i of %i)", current_file, num_files);
489 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000490 cur_fline + 1, cur_fline + max_displayed_line + 1,
491 max_fline + 1);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000492 if (cur_fline >= (int)(max_fline - max_displayed_line)) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000493 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000494 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000495 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000496 return;
Rob Landley9200e792005-09-15 19:26:59 +0000497 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000498 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000499 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000500}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000501#endif
502
Rob Landley9200e792005-09-15 19:26:59 +0000503/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000504static void status_print(void)
505{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000506 const char *p;
507
Denis Vlasenko33196372008-02-23 01:25:38 +0000508 if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
509 return;
510
Rob Landley9200e792005-09-15 19:26:59 +0000511 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000512#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000513 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000514 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000515 return;
Rob Landley9200e792005-09-15 19:26:59 +0000516 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000517 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000518#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000519
520 clear_line();
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000521 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000522 bb_putchar(':');
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000523 return;
524 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000525 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000526 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000527 p = filename;
528 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000529 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000530 p, current_file, num_files);
531 return;
532 }
533 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000534}
535
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000536static void cap_cur_fline(int nlines)
537{
538 int diff;
539 if (cur_fline < 0)
540 cur_fline = 0;
541 if (cur_fline + max_displayed_line > max_fline + TILDES) {
542 cur_fline -= nlines;
543 if (cur_fline < 0)
544 cur_fline = 0;
545 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
546 /* As the number of lines requested was too large, we just move
547 to the end of the file */
548 if (diff > 0)
549 cur_fline += diff;
550 }
551}
552
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000553static const char controls[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000554 /* NUL: never encountered; TAB: not converted */
555 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000556 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
557 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000558static const char ctrlconv[] ALIGN1 =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000559 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000560 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
561 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
562
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000563static void lineno_str(char *nbuf9, const char *line)
564{
565 nbuf9[0] = '\0';
566 if (option_mask32 & FLAG_N) {
567 const char *fmt;
568 unsigned n;
569
570 if (line == empty_line_marker) {
571 memset(nbuf9, ' ', 8);
572 nbuf9[8] = '\0';
573 return;
574 }
575 /* Width of 7 preserves tab spacing in the text */
576 fmt = "%7u ";
Denis Vlasenkod9083952008-10-24 10:42:21 +0000577 n = LINENO(line) + 1;
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000578 if (n > 9999999) {
579 n %= 10000000;
580 fmt = "%07u ";
581 }
582 sprintf(nbuf9, fmt, n);
583 }
584}
585
586
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000587#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000588static void print_found(const char *line)
589{
590 int match_status;
591 int eflags;
592 char *growline;
593 regmatch_t match_structs;
594
595 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000596 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000597 const char *str = line;
598 char *p = buf;
599 size_t n;
600
601 while (*str) {
602 n = strcspn(str, controls);
603 if (n) {
604 if (!str[n]) break;
605 memcpy(p, str, n);
606 p += n;
607 str += n;
608 }
609 n = strspn(str, controls);
610 memset(p, '.', n);
611 p += n;
612 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000613 }
614 strcpy(p, str);
615
616 /* buf[] holds quarantined version of str */
617
618 /* Each part of the line that matches has the HIGHLIGHT
619 and NORMAL escape sequences placed around it.
620 NB: we regex against line, but insert text
621 from quarantined copy (buf[]) */
622 str = buf;
623 growline = NULL;
624 eflags = 0;
625 goto start;
626
627 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000628 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000629 growline ? : "",
630 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000631 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000632 str + match_structs.rm_so);
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000633 free(growline);
634 growline = new;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000635 str += match_structs.rm_eo;
636 line += match_structs.rm_eo;
637 eflags = REG_NOTBOL;
638 start:
639 /* Most of the time doesn't find the regex, optimize for that */
640 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
Denis Vlasenko3fe4f982008-06-09 16:02:39 +0000641 /* if even "" matches, treat it as "not a match" */
642 if (match_structs.rm_so >= match_structs.rm_eo)
643 match_status = 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000644 }
645
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000646 lineno_str(nbuf9, line);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000647 if (!growline) {
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000648 printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000649 return;
650 }
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000651 printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000652 free(growline);
653}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000654#else
655void print_found(const char *line);
656#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000657
658static void print_ascii(const char *str)
659{
660 char buf[width];
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000661 char nbuf9[9];
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000662 char *p;
663 size_t n;
664
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000665 lineno_str(nbuf9, str);
666 printf(CLEAR_2_EOL"%s", nbuf9);
667
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000668 while (*str) {
669 n = strcspn(str, controls);
670 if (n) {
671 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000672 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000673 str += n;
674 }
675 n = strspn(str, controls);
676 p = buf;
677 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000678 if (*str == 0x7f)
679 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000680 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000681 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
682 /* I want to know who committed this sin */
683 *p++ = '{';
684 else
685 *p++ = ctrlconv[(unsigned char)*str];
686 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000687 } while (--n);
688 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000689 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000690 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000691 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000692}
693
Rob Landley9200e792005-09-15 19:26:59 +0000694/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000695static void buffer_print(void)
696{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000697 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000698
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000699 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000700 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000701 if (pattern_valid)
702 print_found(buffer[i]);
703 else
704 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000705 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000706}
707
Denis Vlasenkod9083952008-10-24 10:42:21 +0000708static void buffer_fill_and_print(void)
709{
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000710 unsigned i;
711#if ENABLE_FEATURE_LESS_DASHCMD
712 int fpos = cur_fline;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000713
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000714 if (option_mask32 & FLAG_S) {
Denis Vlasenkod9083952008-10-24 10:42:21 +0000715 /* Go back to the beginning of this line */
716 while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
717 fpos--;
718 }
719
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000720 i = 0;
Denis Vlasenkod9083952008-10-24 10:42:21 +0000721 while (i <= max_displayed_line && fpos <= max_fline) {
722 int lineno = LINENO(flines[fpos]);
723 buffer[i] = flines[fpos];
724 i++;
725 do {
726 fpos++;
727 } while ((fpos <= max_fline)
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000728 && (option_mask32 & FLAG_S)
Denis Vlasenkod9083952008-10-24 10:42:21 +0000729 && lineno == LINENO(flines[fpos])
730 );
731 }
Denis Vlasenkod9083952008-10-24 10:42:21 +0000732#else
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000733 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
734 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000735 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000736#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000737 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000738 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000739 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000740 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000741}
742
743/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000744static void buffer_down(int nlines)
745{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000746 cur_fline += nlines;
747 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000748 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000749 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000750}
751
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000752static void buffer_up(int nlines)
753{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000754 cur_fline -= nlines;
755 if (cur_fline < 0) cur_fline = 0;
756 read_lines();
757 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000758}
759
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000760static void buffer_line(int linenum)
761{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000762 if (linenum < 0)
763 linenum = 0;
764 cur_fline = linenum;
765 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000766 if (linenum + max_displayed_line > max_fline)
767 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000768 if (linenum < 0)
769 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000770 cur_fline = linenum;
771 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000772}
773
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000774static void open_file_and_read_lines(void)
775{
776 if (filename) {
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000777 xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000778 } else {
779 /* "less" with no arguments in argv[] */
780 /* For status line only */
781 filename = xstrdup(bb_msg_standard_input);
782 }
783 readpos = 0;
784 readeof = 0;
Denis Vlasenkoe1ef89a2008-10-26 16:30:09 +0000785 last_line_pos = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000786 terminated = 1;
787 read_lines();
788}
789
790/* Reinitialize everything for a new file - free the memory and start over */
791static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000792{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000793 unsigned i;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000794
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000795 if (flines) {
796 for (i = 0; i <= max_fline; i++)
Denis Vlasenko8f39b722008-10-23 22:02:30 +0000797 free(MEMPTR(flines[i]));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000798 free(flines);
799 flines = NULL;
800 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000801
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000802 max_fline = -1;
803 cur_fline = 0;
804 max_lineno = 0;
805 open_file_and_read_lines();
806 buffer_fill_and_print();
807}
808
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000809static ssize_t getch_nowait(void)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000810{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000811 int rd;
Denis Vlasenko33196372008-02-23 01:25:38 +0000812 struct pollfd pfd[2];
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000813
Denis Vlasenko33196372008-02-23 01:25:38 +0000814 pfd[0].fd = STDIN_FILENO;
815 pfd[0].events = POLLIN;
816 pfd[1].fd = kbd_fd;
817 pfd[1].events = POLLIN;
818 again:
819 tcsetattr(kbd_fd, TCSANOW, &term_less);
820 /* NB: select/poll returns whenever read will not block. Therefore:
821 * if eof is reached, select/poll will return immediately
822 * because read will immediately return 0 bytes.
823 * Even if select/poll says that input is available, read CAN block
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000824 * (switch fd into O_NONBLOCK'ed mode to avoid it)
825 */
Denis Vlasenko33196372008-02-23 01:25:38 +0000826 rd = 1;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000827 /* Are we interested in stdin? */
828//TODO: reuse code for determining this
829 if (!(option_mask32 & FLAG_S)
830 ? !(max_fline > cur_fline + max_displayed_line)
831 : !(max_fline >= cur_fline
832 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000833 ) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000834 if (eof_error > 0) /* did NOT reach eof yet */
835 rd = 0; /* yes, we are interested in stdin */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000836 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000837 /* Position cursor if line input is done */
Denis Vlasenko33196372008-02-23 01:25:38 +0000838 if (less_gets_pos >= 0)
839 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
840 fflush(stdout);
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000841
842 if (kbd_input_size == 0) {
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000843#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000844 while (1) {
845 int r;
846 /* NB: SIGWINCH interrupts poll() */
847 r = poll(pfd + rd, 2 - rd, -1);
848 if (/*r < 0 && errno == EINTR &&*/ winch_counter)
849 return '\\'; /* anything which has no defined function */
850 if (r) break;
851 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000852#else
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000853 safe_poll(pfd + rd, 2 - rd, -1);
Denis Vlasenko53c80f02008-10-24 22:43:27 +0000854#endif
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000855 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000856
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000857 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
858 * would not block even if there is no input available */
Denis Vlasenkoc51457c2008-11-02 00:55:41 +0000859 rd = read_key(kbd_fd, &kbd_input_size, kbd_input);
Denis Vlasenko64974792008-10-31 03:04:55 +0000860 if (rd == -1) {
861 if (errno == EAGAIN) {
862 /* No keyboard input available. Since poll() did return,
863 * we should have input on stdin */
864 read_lines();
865 buffer_fill_and_print();
866 goto again;
867 }
868 /* EOF/error (ssh session got killed etc) */
869 less_exit(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000870 }
Denis Vlasenko33196372008-02-23 01:25:38 +0000871 set_tty_cooked();
872 return rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000873}
874
875/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000876 * character is ASCII \033, get more characters and assign certain sequences
877 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000878static int less_getch(int pos)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000879{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000880 int i;
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000881
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000882 again:
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000883 less_gets_pos = pos;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000884 i = getch_nowait();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000885 less_gets_pos = -1;
Denis Vlasenko46340e32007-08-03 14:17:21 +0000886
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000887 /* Discard Ctrl-something chars */
888 if (i >= 0 && i < ' ' && i != 0x0d && i != 8)
Denis Vlasenko33196372008-02-23 01:25:38 +0000889 goto again;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000890 return i;
891}
892
893static char* less_gets(int sz)
894{
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000895 int c;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000896 unsigned i = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000897 char *result = xzalloc(1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000898
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000899 while (1) {
Denis Vlasenko46340e32007-08-03 14:17:21 +0000900 c = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000901 less_gets_pos = sz + i;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000902 c = getch_nowait();
Denis Vlasenko33196372008-02-23 01:25:38 +0000903 if (c == 0x0d) {
Denis Vlasenkod553faf2008-02-23 12:22:17 +0000904 result[i] = '\0';
Denis Vlasenko33196372008-02-23 01:25:38 +0000905 less_gets_pos = -1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000906 return result;
Denis Vlasenko33196372008-02-23 01:25:38 +0000907 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000908 if (c == 0x7f)
909 c = 8;
910 if (c == 8 && i) {
911 printf("\x8 \x8");
912 i--;
913 }
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000914 if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000915 continue;
916 if (i >= width - sz - 1)
917 continue; /* len limit */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000918 bb_putchar(c);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000919 result[i++] = c;
920 result = xrealloc(result, i+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000921 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000922}
923
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000924static void examine_file(void)
925{
Denis Vlasenko33196372008-02-23 01:25:38 +0000926 char *new_fname;
927
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000928 print_statusline("Examine: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000929 new_fname = less_gets(sizeof("Examine: ") - 1);
Denis Vlasenko33196372008-02-23 01:25:38 +0000930 if (!new_fname[0]) {
Denis Vlasenko33196372008-02-23 01:25:38 +0000931 status_print();
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000932 err:
933 free(new_fname);
Denis Vlasenko33196372008-02-23 01:25:38 +0000934 return;
935 }
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000936 if (access(new_fname, R_OK) != 0) {
937 print_statusline("Cannot read this file");
938 goto err;
939 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000940 free(filename);
Denis Vlasenko33196372008-02-23 01:25:38 +0000941 filename = new_fname;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000942 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000943 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000944 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000945 num_files++; */
946 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000947 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000948 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000949}
950
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000951/* This function changes the file currently being paged. direction can be one of the following:
952 * -1: go back one file
953 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000954 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000955static void change_file(int direction)
956{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000957 if (current_file != ((direction > 0) ? num_files : 1)) {
958 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000959 free(filename);
960 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000961 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000962 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000963 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000964 }
965}
966
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000967static void remove_current_file(void)
968{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000969 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000970
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000971 if (num_files < 2)
972 return;
973
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000974 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000975 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000976 for (i = 3; i <= num_files; i++)
977 files[i - 2] = files[i - 1];
978 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000979 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000980 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000981 for (i = 2; i <= num_files; i++)
982 files[i - 2] = files[i - 1];
983 num_files--;
984 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000985 }
986}
987
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000988static void colon_process(void)
989{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000990 int keypress;
991
992 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000993 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000994
Denis Vlasenkod2172c02008-02-23 11:54:37 +0000995 keypress = less_getch(2);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000996 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000997 case 'd':
998 remove_current_file();
999 break;
1000 case 'e':
1001 examine_file();
1002 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001003#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001004 case 'f':
1005 m_status_print();
1006 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001007#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001008 case 'n':
1009 change_file(1);
1010 break;
1011 case 'p':
1012 change_file(-1);
1013 break;
1014 case 'q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001015 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001016 break;
1017 case 'x':
1018 change_file(0);
1019 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001020 }
1021}
1022
Denis Vlasenko3bba5452006-12-30 17:57:03 +00001023#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001024static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001025{
Denis Vlasenkof65d1332006-12-21 15:23:45 +00001026 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001027 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001028 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001029 match = 0;
1030 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001031}
1032
Rob Landleya2e98042006-04-18 01:53:41 +00001033static void goto_match(int match)
1034{
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001035 if (!pattern_valid)
1036 return;
1037 if (match < 0)
1038 match = 0;
1039 /* Try to find next match if eof isn't reached yet */
1040 if (match >= num_matches && eof_error > 0) {
Denis Vlasenko24f824e2008-04-13 08:32:51 +00001041 wanted_match = match; /* "I want to read until I see N'th match" */
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001042 read_lines();
1043 }
1044 if (num_matches) {
1045 normalize_match_pos(match);
1046 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +00001047 } else {
Denis Vlasenko8465a992007-05-09 18:32:54 +00001048 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +00001049 }
Rob Landleya2e98042006-04-18 01:53:41 +00001050}
1051
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001052static void fill_match_lines(unsigned pos)
1053{
1054 if (!pattern_valid)
1055 return;
1056 /* Run the regex on each line of the current file */
1057 while (pos <= max_fline) {
1058 /* If this line matches */
1059 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
1060 /* and we didn't match it last time */
1061 && !(num_matches && match_lines[num_matches-1] == pos)
1062 ) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001063 match_lines = xrealloc_vector(match_lines, 4, num_matches);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001064 match_lines[num_matches++] = pos;
1065 }
1066 pos++;
1067 }
1068}
1069
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001070static void regex_process(void)
1071{
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001072 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001073
1074 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001075 free(match_lines);
1076 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001077 match_pos = 0;
1078 num_matches = 0;
1079 if (pattern_valid) {
1080 regfree(&pattern);
1081 pattern_valid = 0;
1082 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001083
Rob Landleya2e98042006-04-18 01:53:41 +00001084 /* Get the uncompiled regular expression from the user */
1085 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001086 bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001087 uncomp_regex = less_gets(1);
1088 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001089 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001090 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +00001091 return;
1092 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001093
Rob Landleya2e98042006-04-18 01:53:41 +00001094 /* Compile the regex and check for errors */
Bernhard Reutner-Fischer48a67732008-09-26 14:10:17 +00001095 err = regcomp_or_errmsg(&pattern, uncomp_regex,
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001096 (option_mask32 & FLAG_I) ? REG_ICASE : 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001097 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001098 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001099 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001100 free(err);
1101 return;
1102 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001103
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001104 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001105 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001106 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001107 while (match_pos < num_matches) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001108 if ((int)match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001109 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001110 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001111 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001112 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
1113 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +00001114
1115 /* It's possible that no matches are found yet.
1116 * goto_match() will read input looking for match,
1117 * if needed */
1118 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001119}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001120#endif
1121
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001122static void number_process(int first_digit)
1123{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001124 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001125 int num;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001126 int keypress;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001127 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +00001128
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001129 num_input[0] = first_digit;
1130
1131 /* Clear the current line, print a prompt, and then print the digit */
1132 clear_line();
1133 printf(":%c", first_digit);
1134
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001135 /* Receive input until a letter is given */
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001136 i = 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001137 while (i < sizeof(num_input)-1) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001138 keypress = less_getch(i + 1);
1139 if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001140 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001141 num_input[i] = keypress;
1142 bb_putchar(keypress);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001143 i++;
1144 }
1145
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001146 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001147 num = bb_strtou(num_input, NULL, 10);
1148 /* on format error, num == -1 */
1149 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +00001150 buffer_print();
1151 return;
1152 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001153
1154 /* We now know the number and the letter entered, so we process them */
1155 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001156 case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001157 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001158 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001159 case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001160 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001161 break;
1162 case 'g': case '<': case 'G': case '>':
1163 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001164 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001165 buffer_line(num - 1);
1166 break;
1167 case 'p': case '%':
1168 num = num * (max_fline / 100); /* + max_fline / 2; */
1169 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001170 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001171 buffer_line(num);
1172 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001173#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001174 case 'n':
1175 goto_match(match_pos + num);
1176 break;
1177 case '/':
1178 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1179 regex_process();
1180 break;
1181 case '?':
1182 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1183 regex_process();
1184 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001185#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001186 }
1187}
1188
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001189#if ENABLE_FEATURE_LESS_DASHCMD
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001190static void flag_change(void)
1191{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001192 int keypress;
1193
1194 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001195 bb_putchar('-');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001196 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001197
1198 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001199 case 'M':
1200 option_mask32 ^= FLAG_M;
1201 break;
1202 case 'm':
1203 option_mask32 ^= FLAG_m;
1204 break;
1205 case 'E':
1206 option_mask32 ^= FLAG_E;
1207 break;
1208 case '~':
1209 option_mask32 ^= FLAG_TILDE;
1210 break;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001211 case 'S':
1212 option_mask32 ^= FLAG_S;
1213 buffer_fill_and_print();
1214 break;
1215#if ENABLE_FEATURE_LESS_LINENUMS
1216 case 'N':
1217 option_mask32 ^= FLAG_N;
1218 re_wrap();
1219 buffer_fill_and_print();
1220 break;
1221#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001222 }
1223}
1224
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001225#ifdef BLOAT
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001226static void show_flag_status(void)
1227{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001228 int keypress;
1229 int flag_val;
1230
1231 clear_line();
Denis Vlasenko4daad902007-09-27 10:20:47 +00001232 bb_putchar('_');
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001233 keypress = less_getch(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001234
1235 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001236 case 'M':
1237 flag_val = option_mask32 & FLAG_M;
1238 break;
1239 case 'm':
1240 flag_val = option_mask32 & FLAG_m;
1241 break;
1242 case '~':
1243 flag_val = option_mask32 & FLAG_TILDE;
1244 break;
1245 case 'N':
1246 flag_val = option_mask32 & FLAG_N;
1247 break;
1248 case 'E':
1249 flag_val = option_mask32 & FLAG_E;
1250 break;
1251 default:
1252 flag_val = 0;
1253 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001254 }
1255
1256 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001257 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001258}
1259#endif
1260
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001261#endif /* ENABLE_FEATURE_LESS_DASHCMD */
1262
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001263static void save_input_to_file(void)
1264{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001265 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001266 char *current_line;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001267 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001268 FILE *fp;
1269
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001270 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001271 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko33196372008-02-23 01:25:38 +00001272 if (current_line[0]) {
Denis Vlasenko5415c852008-07-21 23:05:26 +00001273 fp = fopen_for_write(current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001274 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001275 msg = "Error opening log file";
1276 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001277 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001278 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001279 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001280 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001281 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001282 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001283 ret:
1284 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001285 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001286}
1287
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001288#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001289static void add_mark(void)
1290{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001291 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001292
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001293 print_statusline("Mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001294 letter = less_getch(sizeof("Mark: ") - 1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001295
1296 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001297 /* If we exceed 15 marks, start overwriting previous ones */
1298 if (num_marks == 14)
1299 num_marks = 0;
1300
1301 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001302 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001303 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001304 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001305 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001306 }
1307}
1308
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001309static void goto_mark(void)
1310{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001311 int letter;
1312 int i;
1313
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001314 print_statusline("Go to mark: ");
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001315 letter = less_getch(sizeof("Go to mark: ") - 1);
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001316 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001317
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001318 if (isalpha(letter)) {
1319 for (i = 0; i <= num_marks; i++)
1320 if (letter == mark_lines[i][0]) {
1321 buffer_line(mark_lines[i][1]);
1322 break;
1323 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001324 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001325 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001326 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001327 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001328}
1329#endif
1330
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001331#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001332static char opp_bracket(char bracket)
1333{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001334 switch (bracket) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001335 case '{': case '[': /* '}' == '{' + 2. Same for '[' */
1336 bracket++;
1337 case '(': /* ')' == '(' + 1 */
1338 bracket++;
1339 break;
1340 case '}': case ']':
1341 bracket--;
1342 case ')':
1343 bracket--;
1344 break;
1345 };
1346 return bracket;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001347}
1348
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001349static void match_right_bracket(char bracket)
1350{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001351 unsigned i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001352
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001353 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001354 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001355 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001356 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001357 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001358 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001359 if (strchr(flines[i], bracket) != NULL) {
1360 buffer_line(i);
1361 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001362 }
1363 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001364 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001365}
1366
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001367static void match_left_bracket(char bracket)
1368{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001369 int i;
1370
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001371 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001372 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001373 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001374 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001375
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001376 bracket = opp_bracket(bracket);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001377 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001378 if (strchr(flines[i], bracket) != NULL) {
1379 buffer_line(i);
1380 return;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001381 }
1382 }
Denis Vlasenkob78d1c02008-02-24 19:18:18 +00001383 print_statusline("No matching bracket found");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001384}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001385#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001386
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001387static void keypress_process(int keypress)
1388{
Rob Landley9200e792005-09-15 19:26:59 +00001389 switch (keypress) {
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001390 case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001391 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001392 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001393 case KEYCODE_UP: case 'y': case 'k':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001394 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001395 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001396 case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001397 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001398 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001399 case KEYCODE_PAGEUP: case 'w': case 'b':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001400 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001401 break;
1402 case 'd':
1403 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001404 break;
1405 case 'u':
1406 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001407 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001408 case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001409 buffer_line(0);
1410 break;
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001411 case KEYCODE_END: case 'G': case '>':
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001412 cur_fline = MAXLINES;
1413 read_lines();
1414 buffer_line(cur_fline);
1415 break;
1416 case 'q': case 'Q':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001417 less_exit(EXIT_SUCCESS);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001418 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001419#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001420 case 'm':
1421 add_mark();
1422 buffer_print();
1423 break;
1424 case '\'':
1425 goto_mark();
1426 buffer_print();
1427 break;
Rob Landley9200e792005-09-15 19:26:59 +00001428#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001429 case 'r': case 'R':
1430 buffer_print();
1431 break;
1432 /*case 'R':
1433 full_repaint();
1434 break;*/
1435 case 's':
1436 save_input_to_file();
1437 break;
1438 case 'E':
1439 examine_file();
1440 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001441#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001442 case '=':
1443 m_status_print();
1444 break;
Rob Landley9200e792005-09-15 19:26:59 +00001445#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001446#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001447 case '/':
1448 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1449 regex_process();
1450 break;
1451 case 'n':
1452 goto_match(match_pos + 1);
1453 break;
1454 case 'N':
1455 goto_match(match_pos - 1);
1456 break;
1457 case '?':
1458 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1459 regex_process();
1460 break;
Rob Landley9200e792005-09-15 19:26:59 +00001461#endif
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001462#if ENABLE_FEATURE_LESS_DASHCMD
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001463 case '-':
1464 flag_change();
1465 buffer_print();
1466 break;
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001467#ifdef BLOAT
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001468 case '_':
1469 show_flag_status();
1470 break;
Rob Landley9200e792005-09-15 19:26:59 +00001471#endif
Denis Vlasenko7b32e8f2008-10-26 00:15:36 +00001472#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001473#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001474 case '{': case '(': case '[':
1475 match_right_bracket(keypress);
1476 break;
1477 case '}': case ')': case ']':
1478 match_left_bracket(keypress);
1479 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001480#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001481 case ':':
1482 colon_process();
1483 break;
Rob Landley9200e792005-09-15 19:26:59 +00001484 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001485
Rob Landley9200e792005-09-15 19:26:59 +00001486 if (isdigit(keypress))
1487 number_process(keypress);
1488}
1489
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001490static void sig_catcher(int sig)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001491{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +00001492 less_exit(- sig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001493}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001494
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001495#if ENABLE_FEATURE_LESS_WINCH
1496static void sigwinch_handler(int sig UNUSED_PARAM)
1497{
1498 winch_counter++;
1499}
1500#endif
1501
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001502int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001503int less_main(int argc, char **argv)
1504{
Rob Landley9200e792005-09-15 19:26:59 +00001505 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001506
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001507 INIT_G();
1508
Denis Vlasenko95b30712006-12-31 19:23:31 +00001509 /* TODO: -x: do not interpret backspace, -xx: tab also */
1510 /* -xxx: newline also */
1511 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001512 getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
Rob Landley9200e792005-09-15 19:26:59 +00001513 argc -= optind;
1514 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001515 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001516 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001517
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001518 /* Another popular pager, most, detects when stdout
1519 * is not a tty and turns into cat. This makes sense. */
1520 if (!isatty(STDOUT_FILENO))
1521 return bb_cat(argv);
1522
Rob Landley9200e792005-09-15 19:26:59 +00001523 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001524 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001525 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001526 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001527 bb_show_usage();
1528 }
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001529 } else {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001530 filename = xstrdup(files[0]);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001531 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001532
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001533 if (option_mask32 & FLAG_TILDE)
1534 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001535
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001536 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1537 if (kbd_fd < 0)
1538 return bb_cat(argv);
1539 ndelay_on(kbd_fd);
1540
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001541 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001542 term_less = term_orig;
1543 term_less.c_lflag &= ~(ICANON | ECHO);
1544 term_less.c_iflag &= ~(IXON | ICRNL);
1545 /*term_less.c_oflag &= ~ONLCR;*/
1546 term_less.c_cc[VMIN] = 1;
1547 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001548
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001549 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1550 /* 20: two tabstops + 4 */
1551 if (width < 20 || max_displayed_line < 3)
1552 return bb_cat(argv);
1553 max_displayed_line -= 2;
1554
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001555 /* We want to restore term_orig on exit */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +00001556 bb_signals(BB_FATAL_SIGS, sig_catcher);
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001557#if ENABLE_FEATURE_LESS_WINCH
1558 signal(SIGWINCH, sigwinch_handler);
1559#endif
Denis Vlasenkod553faf2008-02-23 12:22:17 +00001560
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001561 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001562 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001563 while (1) {
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001564#if ENABLE_FEATURE_LESS_WINCH
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001565 while (WINCH_COUNTER) {
1566 again:
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001567 winch_counter--;
1568 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1569 /* 20: two tabstops + 4 */
1570 if (width < 20)
1571 width = 20;
1572 if (max_displayed_line < 3)
1573 max_displayed_line = 3;
1574 max_displayed_line -= 2;
1575 free(buffer);
1576 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001577 /* Avoid re-wrap and/or redraw if we already know
1578 * we need to do it again. These ops are expensive */
1579 if (WINCH_COUNTER)
1580 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001581 re_wrap();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001582 if (WINCH_COUNTER)
1583 goto again;
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001584 buffer_fill_and_print();
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +00001585 /* This took some time. Loop back and check,
1586 * were there another SIGWINCH? */
Denis Vlasenko53c80f02008-10-24 22:43:27 +00001587 }
1588#endif
Denis Vlasenkod2172c02008-02-23 11:54:37 +00001589 keypress = less_getch(-1); /* -1: do not position cursor */
Rob Landley9200e792005-09-15 19:26:59 +00001590 keypress_process(keypress);
1591 }
1592}
Denis Vlasenkoe8efe812008-10-24 11:07:20 +00001593
1594/*
1595Help text of less version 418 is below.
1596If you are implementing something, keeping
1597key and/or command line switch compatibility is a good idea:
1598
1599
1600 SUMMARY OF LESS COMMANDS
1601
1602 Commands marked with * may be preceded by a number, N.
1603 Notes in parentheses indicate the behavior if N is given.
1604 h H Display this help.
1605 q :q Q :Q ZZ Exit.
1606 ---------------------------------------------------------------------------
1607 MOVING
1608 e ^E j ^N CR * Forward one line (or N lines).
1609 y ^Y k ^K ^P * Backward one line (or N lines).
1610 f ^F ^V SPACE * Forward one window (or N lines).
1611 b ^B ESC-v * Backward one window (or N lines).
1612 z * Forward one window (and set window to N).
1613 w * Backward one window (and set window to N).
1614 ESC-SPACE * Forward one window, but don't stop at end-of-file.
1615 d ^D * Forward one half-window (and set half-window to N).
1616 u ^U * Backward one half-window (and set half-window to N).
1617 ESC-) RightArrow * Left one half screen width (or N positions).
1618 ESC-( LeftArrow * Right one half screen width (or N positions).
1619 F Forward forever; like "tail -f".
1620 r ^R ^L Repaint screen.
1621 R Repaint screen, discarding buffered input.
1622 ---------------------------------------------------
1623 Default "window" is the screen height.
1624 Default "half-window" is half of the screen height.
1625 ---------------------------------------------------------------------------
1626 SEARCHING
1627 /pattern * Search forward for (N-th) matching line.
1628 ?pattern * Search backward for (N-th) matching line.
1629 n * Repeat previous search (for N-th occurrence).
1630 N * Repeat previous search in reverse direction.
1631 ESC-n * Repeat previous search, spanning files.
1632 ESC-N * Repeat previous search, reverse dir. & spanning files.
1633 ESC-u Undo (toggle) search highlighting.
1634 ---------------------------------------------------
1635 Search patterns may be modified by one or more of:
1636 ^N or ! Search for NON-matching lines.
1637 ^E or * Search multiple files (pass thru END OF FILE).
1638 ^F or @ Start search at FIRST file (for /) or last file (for ?).
1639 ^K Highlight matches, but don't move (KEEP position).
1640 ^R Don't use REGULAR EXPRESSIONS.
1641 ---------------------------------------------------------------------------
1642 JUMPING
1643 g < ESC-< * Go to first line in file (or line N).
1644 G > ESC-> * Go to last line in file (or line N).
1645 p % * Go to beginning of file (or N percent into file).
1646 t * Go to the (N-th) next tag.
1647 T * Go to the (N-th) previous tag.
1648 { ( [ * Find close bracket } ) ].
1649 } ) ] * Find open bracket { ( [.
1650 ESC-^F <c1> <c2> * Find close bracket <c2>.
1651 ESC-^B <c1> <c2> * Find open bracket <c1>
1652 ---------------------------------------------------
1653 Each "find close bracket" command goes forward to the close bracket
1654 matching the (N-th) open bracket in the top line.
1655 Each "find open bracket" command goes backward to the open bracket
1656 matching the (N-th) close bracket in the bottom line.
1657 m<letter> Mark the current position with <letter>.
1658 '<letter> Go to a previously marked position.
1659 '' Go to the previous position.
1660 ^X^X Same as '.
1661 ---------------------------------------------------
1662 A mark is any upper-case or lower-case letter.
1663 Certain marks are predefined:
1664 ^ means beginning of the file
1665 $ means end of the file
1666 ---------------------------------------------------------------------------
1667 CHANGING FILES
1668 :e [file] Examine a new file.
1669 ^X^V Same as :e.
1670 :n * Examine the (N-th) next file from the command line.
1671 :p * Examine the (N-th) previous file from the command line.
1672 :x * Examine the first (or N-th) file from the command line.
1673 :d Delete the current file from the command line list.
1674 = ^G :f Print current file name.
1675 ---------------------------------------------------------------------------
1676 MISCELLANEOUS COMMANDS
1677 -<flag> Toggle a command line option [see OPTIONS below].
1678 --<name> Toggle a command line option, by name.
1679 _<flag> Display the setting of a command line option.
1680 __<name> Display the setting of an option, by name.
1681 +cmd Execute the less cmd each time a new file is examined.
1682 !command Execute the shell command with $SHELL.
1683 |Xcommand Pipe file between current pos & mark X to shell command.
1684 v Edit the current file with $VISUAL or $EDITOR.
1685 V Print version number of "less".
1686 ---------------------------------------------------------------------------
1687 OPTIONS
1688 Most options may be changed either on the command line,
1689 or from within less by using the - or -- command.
1690 Options may be given in one of two forms: either a single
1691 character preceded by a -, or a name preceeded by --.
1692 -? ........ --help
1693 Display help (from command line).
1694 -a ........ --search-skip-screen
1695 Forward search skips current screen.
1696 -b [N] .... --buffers=[N]
1697 Number of buffers.
1698 -B ........ --auto-buffers
1699 Don't automatically allocate buffers for pipes.
1700 -c ........ --clear-screen
1701 Repaint by clearing rather than scrolling.
1702 -d ........ --dumb
1703 Dumb terminal.
1704 -D [xn.n] . --color=xn.n
1705 Set screen colors. (MS-DOS only)
1706 -e -E .... --quit-at-eof --QUIT-AT-EOF
1707 Quit at end of file.
1708 -f ........ --force
1709 Force open non-regular files.
1710 -F ........ --quit-if-one-screen
1711 Quit if entire file fits on first screen.
1712 -g ........ --hilite-search
1713 Highlight only last match for searches.
1714 -G ........ --HILITE-SEARCH
1715 Don't highlight any matches for searches.
1716 -h [N] .... --max-back-scroll=[N]
1717 Backward scroll limit.
1718 -i ........ --ignore-case
1719 Ignore case in searches that do not contain uppercase.
1720 -I ........ --IGNORE-CASE
1721 Ignore case in all searches.
1722 -j [N] .... --jump-target=[N]
1723 Screen position of target lines.
1724 -J ........ --status-column
1725 Display a status column at left edge of screen.
1726 -k [file] . --lesskey-file=[file]
1727 Use a lesskey file.
1728 -L ........ --no-lessopen
1729 Ignore the LESSOPEN environment variable.
1730 -m -M .... --long-prompt --LONG-PROMPT
1731 Set prompt style.
1732 -n -N .... --line-numbers --LINE-NUMBERS
1733 Don't use line numbers.
1734 -o [file] . --log-file=[file]
1735 Copy to log file (standard input only).
1736 -O [file] . --LOG-FILE=[file]
1737 Copy to log file (unconditionally overwrite).
1738 -p [pattern] --pattern=[pattern]
1739 Start at pattern (from command line).
1740 -P [prompt] --prompt=[prompt]
1741 Define new prompt.
1742 -q -Q .... --quiet --QUIET --silent --SILENT
1743 Quiet the terminal bell.
1744 -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
1745 Output "raw" control characters.
1746 -s ........ --squeeze-blank-lines
1747 Squeeze multiple blank lines.
1748 -S ........ --chop-long-lines
1749 Chop long lines.
1750 -t [tag] .. --tag=[tag]
1751 Find a tag.
1752 -T [tagsfile] --tag-file=[tagsfile]
1753 Use an alternate tags file.
1754 -u -U .... --underline-special --UNDERLINE-SPECIAL
1755 Change handling of backspaces.
1756 -V ........ --version
1757 Display the version number of "less".
1758 -w ........ --hilite-unread
1759 Highlight first new line after forward-screen.
1760 -W ........ --HILITE-UNREAD
1761 Highlight first new line after any forward movement.
1762 -x [N[,...]] --tabs=[N[,...]]
1763 Set tab stops.
1764 -X ........ --no-init
1765 Don't use termcap init/deinit strings.
1766 --no-keypad
1767 Don't use termcap keypad init/deinit strings.
1768 -y [N] .... --max-forw-scroll=[N]
1769 Forward scroll limit.
1770 -z [N] .... --window=[N]
1771 Set size of window.
1772 -" [c[c]] . --quotes=[c[c]]
1773 Set shell quote characters.
1774 -~ ........ --tilde
1775 Don't display tildes after end of file.
1776 -# [N] .... --shift=[N]
1777 Horizontal scroll amount (0 = one half screen width)
1778
1779 ---------------------------------------------------------------------------
1780 LINE EDITING
1781 These keys can be used to edit text being entered
1782 on the "command line" at the bottom of the screen.
1783 RightArrow ESC-l Move cursor right one character.
1784 LeftArrow ESC-h Move cursor left one character.
1785 CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
1786 CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
1787 HOME ESC-0 Move cursor to start of line.
1788 END ESC-$ Move cursor to end of line.
1789 BACKSPACE Delete char to left of cursor.
1790 DELETE ESC-x Delete char under cursor.
1791 CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
1792 CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
1793 CNTL-U ESC (MS-DOS only) Delete entire line.
1794 UpArrow ESC-k Retrieve previous command line.
1795 DownArrow ESC-j Retrieve next command line.
1796 TAB Complete filename & cycle.
1797 SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
1798 CNTL-L Complete filename, list all.
1799*/