blob: 9b2c43bd57909afe6e9b5d21e229f33de9c499aa [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
Denis Vlasenkof4dff772006-12-24 07:14:17 +000031/* FIXME: currently doesn't work right */
32#undef ENABLE_FEATURE_LESS_FLAGCS
33#define ENABLE_FEATURE_LESS_FLAGCS 0
Rob Landley9200e792005-09-15 19:26:59 +000034
35/* The escape codes for highlighted and normal text */
36#define HIGHLIGHT "\033[7m"
37#define NORMAL "\033[0m"
Rob Landley9200e792005-09-15 19:26:59 +000038/* The escape code to clear the screen */
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +000039#define CLEAR "\033[H\033[J"
Denis Vlasenkoe865e812006-12-21 13:24:58 +000040/* The escape code to clear to end of line */
41#define CLEAR_2_EOL "\033[K"
Rob Landley9200e792005-09-15 19:26:59 +000042
Denis Vlasenkof4dff772006-12-24 07:14:17 +000043/* These are the escape sequences corresponding to special keys */
44enum {
45 REAL_KEY_UP = 'A',
46 REAL_KEY_DOWN = 'B',
47 REAL_KEY_RIGHT = 'C',
48 REAL_KEY_LEFT = 'D',
49 REAL_PAGE_UP = '5',
50 REAL_PAGE_DOWN = '6',
Denis Vlasenkof5a15762007-03-09 08:55:23 +000051 REAL_KEY_HOME = '7', // vt100? linux vt? or what?
Denis Vlasenkof4dff772006-12-24 07:14:17 +000052 REAL_KEY_END = '8',
Denis Vlasenkof5a15762007-03-09 08:55:23 +000053 REAL_KEY_HOME_ALT = '1', // ESC [1~ (vt100? linux vt? or what?)
Denis Vlasenkoc86e0522007-03-20 11:30:28 +000054 REAL_KEY_END_ALT = '4', // ESC [4~
Denis Vlasenkob30418a2007-02-14 20:49:14 +000055 REAL_KEY_HOME_XTERM = 'H',
56 REAL_KEY_END_XTERM = 'F',
Rob Landley9200e792005-09-15 19:26:59 +000057
Denis Vlasenkof4dff772006-12-24 07:14:17 +000058/* These are the special codes assigned by this program to the special keys */
59 KEY_UP = 20,
60 KEY_DOWN = 21,
61 KEY_RIGHT = 22,
62 KEY_LEFT = 23,
63 PAGE_UP = 24,
64 PAGE_DOWN = 25,
65 KEY_HOME = 26,
66 KEY_END = 27,
67
68/* Absolute max of lines eaten */
69 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
70
71/* This many "after the end" lines we will show (at max) */
72 TILDES = 1,
73};
74
Rob Landley9200e792005-09-15 19:26:59 +000075/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000076enum {
77 FLAG_E = 1,
78 FLAG_M = 1 << 1,
79 FLAG_m = 1 << 2,
80 FLAG_N = 1 << 3,
81 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +000082/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000083 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
84};
Rob Landley9200e792005-09-15 19:26:59 +000085
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000086#if !ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3bba5452006-12-30 17:57:03 +000087enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +000088#endif
89
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +000090struct globals {
91 int cur_fline; /* signed */
92 int kbd_fd; /* fd to get input from */
93/* last position in last line, taking into account tabs */
94 size_t linepos;
95 unsigned max_displayed_line;
96 unsigned max_fline;
97 unsigned max_lineno; /* this one tracks linewrap */
98 unsigned width;
99 ssize_t eof_error; /* eof if 0, error if < 0 */
100 size_t readpos;
101 size_t readeof;
102 const char **buffer;
103 const char **flines;
104 const char *empty_line_marker;
105 unsigned num_files;
106 unsigned current_file;
107 char *filename;
108 char **files;
109#if ENABLE_FEATURE_LESS_MARKS
110 unsigned num_marks;
111 unsigned mark_lines[15][2];
112#endif
113#if ENABLE_FEATURE_LESS_REGEXP
114 unsigned *match_lines;
115 int match_pos; /* signed! */
116 unsigned num_matches;
117 regex_t pattern;
118 smallint pattern_valid;
119#endif
120 smallint terminated;
121 struct termios term_orig, term_less;
122};
123#define G (*ptr_to_globals)
124#define cur_fline (G.cur_fline )
125#define kbd_fd (G.kbd_fd )
126#define linepos (G.linepos )
127#define max_displayed_line (G.max_displayed_line)
128#define max_fline (G.max_fline )
129#define max_lineno (G.max_lineno )
130#define width (G.width )
131#define eof_error (G.eof_error )
132#define readpos (G.readpos )
133#define readeof (G.readeof )
134#define buffer (G.buffer )
135#define flines (G.flines )
136#define empty_line_marker (G.empty_line_marker )
137#define num_files (G.num_files )
138#define current_file (G.current_file )
139#define filename (G.filename )
140#define files (G.files )
141#define num_marks (G.num_marks )
142#define mark_lines (G.mark_lines )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000143#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000144#define match_lines (G.match_lines )
145#define match_pos (G.match_pos )
146#define num_matches (G.num_matches )
147#define pattern (G.pattern )
148#define pattern_valid (G.pattern_valid )
Denis Vlasenko342b0ab2007-05-31 23:06:18 +0000149#endif
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000150#define terminated (G.terminated )
151#define term_orig (G.term_orig )
152#define term_less (G.term_less )
153#define INIT_G() do { \
154 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
155 empty_line_marker = "~"; \
156 num_files = 1; \
157 current_file = 1; \
158 eof_error = 1; \
159 terminated = 1; \
160 } while (0)
Rob Landley9200e792005-09-15 19:26:59 +0000161
162/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000163static void set_tty_cooked(void)
164{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000165 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000166 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000167}
168
Rob Landley9200e792005-09-15 19:26:59 +0000169/* Exit the program gracefully */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000170static void less_exit(int code)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000171{
Rob Landley9200e792005-09-15 19:26:59 +0000172 /* TODO: We really should save the terminal state when we start,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000173 * and restore it when we exit. Less does this with the
174 * "ti" and "te" termcap commands; can this be done with
175 * only termios.h? */
Rob Landley9200e792005-09-15 19:26:59 +0000176 putchar('\n');
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000177 fflush_stdout_and_exit(code);
Rob Landley9200e792005-09-15 19:26:59 +0000178}
179
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000180/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000181 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000182static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000183{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000184 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000185}
186
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000187static void clear_line(void)
188{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000189 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000190}
191
192static void print_hilite(const char *str)
193{
194 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000195}
196
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000197static void print_statusline(const char *str)
198{
199 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000200 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000201}
202
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000203#if ENABLE_FEATURE_LESS_REGEXP
204static void fill_match_lines(unsigned pos);
205#else
206#define fill_match_lines(pos) ((void)0)
207#endif
208
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000209/* Devilishly complex routine.
210 *
211 * Has to deal with EOF and EPIPE on input,
212 * with line wrapping, with last line not ending in '\n'
213 * (possibly not ending YET!), with backspace and tabs.
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000214 * It reads input again if last time we got an EOF (thus supporting
215 * growing files) or EPIPE (watching output of slow process like make).
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000216 *
217 * Variables used:
218 * flines[] - array of lines already read. Linewrap may cause
219 * one source file line to occupy several flines[n].
220 * flines[max_fline] - last line, possibly incomplete.
221 * terminated - 1 if flines[max_fline] is 'terminated'
222 * (if there was '\n' [which isn't stored itself, we just remember
223 * that it was seen])
224 * max_lineno - last line's number, this one doesn't increment
225 * on line wrap, only on "real" new lines.
226 * readbuf[0..readeof-1] - small preliminary buffer.
227 * readbuf[readpos] - next character to add to current line.
228 * linepos - screen line position of next char to be read
229 * (takes into account tabs and backspaces)
230 * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
231 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000232static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000233{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000234#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000235 char *current_line, *p;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000236 USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000237 int w = width;
238 char last_terminated = terminated;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000239
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000240 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000241 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000242
243 current_line = xmalloc(w);
244 p = current_line;
245 max_fline += last_terminated;
246 if (!last_terminated) {
247 const char *cp = flines[max_fline];
248 if (option_mask32 & FLAG_N)
249 cp += 8;
250 strcpy(current_line, cp);
251 p += strlen(current_line);
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000252 /* linepos is still valid from previous read_lines() */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000253 } else {
254 linepos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000255 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000256
257 while (1) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000258 again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000259 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000260 terminated = 0;
261 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000262 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000263 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000264 if (readpos >= readeof) {
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000265 smallint yielded = 0;
266
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000267 ndelay_on(0);
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000268 read_again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000269 eof_error = safe_read(0, readbuf, sizeof(readbuf));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000270 readpos = 0;
271 readeof = eof_error;
272 if (eof_error < 0) {
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000273 if (errno == EAGAIN && !yielded) {
274 /* We can hit EAGAIN while searching for regexp match.
275 * Yield is not 100% reliable solution in general,
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000276 * but for less it should be good enough -
277 * we give stdin supplier some CPU time to produce
278 * more input. We do it just once.
279 * Currently, we do not stop when we found the Nth
280 * occurrence we were looking for. We read till end
281 * (or double EAGAIN). TODO? */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000282 sched_yield();
283 yielded = 1;
284 goto read_again;
285 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000286 readeof = 0;
287 if (errno != EAGAIN)
288 print_statusline("read error");
289 }
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000290 ndelay_off(0);
291
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000292 if (eof_error <= 0) {
293 goto reached_eof;
294 }
295 }
296 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000297 /* backspace? [needed for manpages] */
298 /* <tab><bs> is (a) insane and */
299 /* (b) harder to do correctly, so we refuse to do it */
300 if (c == '\x8' && linepos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000301 readpos++; /* eat it */
Denis Vlasenko95b30712006-12-31 19:23:31 +0000302 linepos--;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000303 /* was buggy (p could end up <= current_line)... */
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000304 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000305 continue;
306 }
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000307 {
308 size_t new_linepos = linepos + 1;
309 if (c == '\t') {
310 new_linepos += 7;
311 new_linepos &= (~7);
312 }
313 if (new_linepos >= w)
314 break;
315 linepos = new_linepos;
316 }
Denis Vlasenko95b30712006-12-31 19:23:31 +0000317 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000318 readpos++;
Denis Vlasenko22a9a3c2007-05-31 15:56:10 +0000319 if (c == '\n') {
320 terminated = 1;
321 linepos = 0;
322 break;
323 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000324 /* NUL is substituted by '\n'! */
325 if (c == '\0') c = '\n';
326 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000327 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000328 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000329 /* Corner case: linewrap with only "" wrapping to next line */
330 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000331 if (!last_terminated && !current_line[0]) {
332 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000333 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000334 goto again;
335 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000336 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000337 last_terminated = terminated;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000338 flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000339 if (option_mask32 & FLAG_N) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000340 /* Width of 7 preserves tab spacing in the text */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000341 flines[max_fline] = xasprintf(
342 (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
343 max_lineno % 10000000, current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000344 free(current_line);
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000345 if (terminated)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000346 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000347 } else {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000348 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000349 }
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000350 if (max_fline >= MAXLINES) {
351 eof_error = 0; /* Pretend we saw EOF */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000352 break;
Denis Vlasenkofdcfa2a2007-05-31 23:55:39 +0000353 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000354 if (max_fline > cur_fline + max_displayed_line)
355 break;
356 if (eof_error <= 0) {
357 if (eof_error < 0 && errno == EAGAIN) {
358 /* not yet eof or error, reset flag (or else
359 * we will hog CPU - select() will return
360 * immediately */
361 eof_error = 1;
362 }
363 break;
364 }
365 max_fline++;
366 current_line = xmalloc(w);
367 p = current_line;
368 linepos = 0;
Rob Landley9200e792005-09-15 19:26:59 +0000369 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000370 fill_match_lines(old_max_fline);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000371#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000372}
373
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000374#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000375/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000376 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000377static int calc_percent(void)
378{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000379 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000380 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000381}
382
Rob Landley9200e792005-09-15 19:26:59 +0000383/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000384static void m_status_print(void)
385{
Rob Landley9200e792005-09-15 19:26:59 +0000386 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000387
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000388 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000389 printf(HIGHLIGHT"%s", filename);
390 if (num_files > 1)
391 printf(" (file %i of %i)", current_file, num_files);
392 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000393 cur_fline + 1, cur_fline + max_displayed_line + 1,
394 max_fline + 1);
395 if (cur_fline >= max_fline - max_displayed_line) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000396 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000397 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000398 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000399 return;
Rob Landley9200e792005-09-15 19:26:59 +0000400 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000401 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000402 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000403}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000404#endif
405
Rob Landley9200e792005-09-15 19:26:59 +0000406/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000407static void status_print(void)
408{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000409 const char *p;
410
Rob Landley9200e792005-09-15 19:26:59 +0000411 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000412#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000413 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000414 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000415 return;
Rob Landley9200e792005-09-15 19:26:59 +0000416 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000417 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000418#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000419
420 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000421 if (cur_fline && cur_fline < max_fline - max_displayed_line) {
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000422 putchar(':');
423 return;
424 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000425 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000426 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000427 p = filename;
428 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000429 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000430 p, current_file, num_files);
431 return;
432 }
433 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000434}
435
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000436static void cap_cur_fline(int nlines)
437{
438 int diff;
439 if (cur_fline < 0)
440 cur_fline = 0;
441 if (cur_fline + max_displayed_line > max_fline + TILDES) {
442 cur_fline -= nlines;
443 if (cur_fline < 0)
444 cur_fline = 0;
445 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
446 /* As the number of lines requested was too large, we just move
447 to the end of the file */
448 if (diff > 0)
449 cur_fline += diff;
450 }
451}
452
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000453static const char controls[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000454 /* NUL: never encountered; TAB: not converted */
455 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000456 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
457 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000458static const char ctrlconv[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000459 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000460 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
461 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
462
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000463#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000464static void print_found(const char *line)
465{
466 int match_status;
467 int eflags;
468 char *growline;
469 regmatch_t match_structs;
470
471 char buf[width];
472 const char *str = line;
473 char *p = buf;
474 size_t n;
475
476 while (*str) {
477 n = strcspn(str, controls);
478 if (n) {
479 if (!str[n]) break;
480 memcpy(p, str, n);
481 p += n;
482 str += n;
483 }
484 n = strspn(str, controls);
485 memset(p, '.', n);
486 p += n;
487 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000488 }
489 strcpy(p, str);
490
491 /* buf[] holds quarantined version of str */
492
493 /* Each part of the line that matches has the HIGHLIGHT
494 and NORMAL escape sequences placed around it.
495 NB: we regex against line, but insert text
496 from quarantined copy (buf[]) */
497 str = buf;
498 growline = NULL;
499 eflags = 0;
500 goto start;
501
502 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000503 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000504 growline ? : "",
505 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000506 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000507 str + match_structs.rm_so);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000508 free(growline); growline = new;
509 str += match_structs.rm_eo;
510 line += match_structs.rm_eo;
511 eflags = REG_NOTBOL;
512 start:
513 /* Most of the time doesn't find the regex, optimize for that */
514 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
515 }
516
517 if (!growline) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000518 printf(CLEAR_2_EOL"%s\n", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000519 return;
520 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000521 printf(CLEAR_2_EOL"%s%s\n", growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000522 free(growline);
523}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000524#else
525void print_found(const char *line);
526#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000527
528static void print_ascii(const char *str)
529{
530 char buf[width];
531 char *p;
532 size_t n;
533
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000534 printf(CLEAR_2_EOL);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000535 while (*str) {
536 n = strcspn(str, controls);
537 if (n) {
538 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000539 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000540 str += n;
541 }
542 n = strspn(str, controls);
543 p = buf;
544 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000545 if (*str == 0x7f)
546 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000547 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000548 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
549 /* I want to know who committed this sin */
550 *p++ = '{';
551 else
552 *p++ = ctrlconv[(unsigned char)*str];
553 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000554 } while (--n);
555 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000556 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000557 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000558 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000559}
560
Rob Landley9200e792005-09-15 19:26:59 +0000561/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000562static void buffer_print(void)
563{
Rob Landley9200e792005-09-15 19:26:59 +0000564 int i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000565
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000566 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000567 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000568 if (pattern_valid)
569 print_found(buffer[i]);
570 else
571 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000572 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000573}
574
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000575static void buffer_fill_and_print(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000576{
Rob Landley9200e792005-09-15 19:26:59 +0000577 int i;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000578 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
579 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000580 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000581 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000582 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000583 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000584 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000585}
586
587/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000588static void buffer_down(int nlines)
589{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000590 cur_fline += nlines;
591 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000592 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000593 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000594}
595
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000596static void buffer_up(int nlines)
597{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000598 cur_fline -= nlines;
599 if (cur_fline < 0) cur_fline = 0;
600 read_lines();
601 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000602}
603
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000604static void buffer_line(int linenum)
605{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000606 if (linenum < 0)
607 linenum = 0;
608 cur_fline = linenum;
609 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000610 if (linenum + max_displayed_line > max_fline)
611 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000612 if (linenum < 0)
613 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000614 cur_fline = linenum;
615 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000616}
617
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000618static void open_file_and_read_lines(void)
619{
620 if (filename) {
621 int fd = xopen(filename, O_RDONLY);
622 dup2(fd, 0);
623 if (fd) close(fd);
624 } else {
625 /* "less" with no arguments in argv[] */
626 /* For status line only */
627 filename = xstrdup(bb_msg_standard_input);
628 }
629 readpos = 0;
630 readeof = 0;
631 linepos = 0;
632 terminated = 1;
633 read_lines();
634}
635
636/* Reinitialize everything for a new file - free the memory and start over */
637static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000638{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000639 int i;
640
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000641 if (flines) {
642 for (i = 0; i <= max_fline; i++)
643 free((void*)(flines[i]));
644 free(flines);
645 flines = NULL;
646 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000647
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000648 max_fline = -1;
649 cur_fline = 0;
650 max_lineno = 0;
651 open_file_and_read_lines();
652 buffer_fill_and_print();
653}
654
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000655static void getch_nowait(char* input, int sz)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000656{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000657 ssize_t rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000658 fd_set readfds;
659 again:
660 fflush(stdout);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000661
662 /* NB: select returns whenever read will not block. Therefore:
663 * (a) with O_NONBLOCK'ed fds select will return immediately
664 * (b) if eof is reached, select will also return
665 * because read will immediately return 0 bytes.
666 * Even if select says that input is available, read CAN block
667 * (switch fd into O_NONBLOCK'ed mode to avoid it)
668 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000669 FD_ZERO(&readfds);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000670 if (max_fline <= cur_fline + max_displayed_line
671 && eof_error > 0 /* did NOT reach eof yet */
672 ) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000673 /* We are interested in stdin */
674 FD_SET(0, &readfds);
675 }
676 FD_SET(kbd_fd, &readfds);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000677 tcsetattr(kbd_fd, TCSANOW, &term_less);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000678 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
679
680 input[0] = '\0';
681 ndelay_on(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000682 rd = read(kbd_fd, input, sz);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000683 ndelay_off(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000684 if (rd < 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000685 /* No keyboard input, but we have input on stdin! */
686 if (errno != EAGAIN) /* Huh?? */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000687 return;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000688 read_lines();
689 buffer_fill_and_print();
690 goto again;
691 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000692}
693
694/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000695 * character is ASCII \033, get more characters and assign certain sequences
696 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000697static int less_getch(void)
698{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000699 char input[16];
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000700 unsigned i;
701 again:
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000702 getch_nowait(input, sizeof(input));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000703 /* Detect escape sequences (i.e. arrow keys) and handle
704 * them accordingly */
705
706 if (input[0] == '\033' && input[1] == '[') {
707 set_tty_cooked();
708 i = input[2] - REAL_KEY_UP;
709 if (i < 4)
710 return 20 + i;
711 i = input[2] - REAL_PAGE_UP;
712 if (i < 4)
713 return 24 + i;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000714 if (input[2] == REAL_KEY_HOME_XTERM)
715 return KEY_HOME;
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000716 if (input[2] == REAL_KEY_HOME_ALT)
717 return KEY_HOME;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000718 if (input[2] == REAL_KEY_END_XTERM)
719 return KEY_END;
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000720 if (input[2] == REAL_KEY_END_ALT)
721 return KEY_END;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000722 return 0;
723 }
724 /* Reject almost all control chars */
725 i = input[0];
726 if (i < ' ' && i != 0x0d && i != 8) goto again;
727 set_tty_cooked();
728 return i;
729}
730
731static char* less_gets(int sz)
732{
733 char c;
734 int i = 0;
735 char *result = xzalloc(1);
736 while (1) {
737 fflush(stdout);
738
739 /* I be damned if I know why is it needed *repeatedly*,
740 * but it is needed. Is it because of stdio? */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +0000741 tcsetattr(kbd_fd, TCSANOW, &term_less);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000742
743 read(kbd_fd, &c, 1);
744 if (c == 0x0d)
745 return result;
746 if (c == 0x7f)
747 c = 8;
748 if (c == 8 && i) {
749 printf("\x8 \x8");
750 i--;
751 }
752 if (c < ' ')
753 continue;
754 if (i >= width - sz - 1)
755 continue; /* len limit */
756 putchar(c);
757 result[i++] = c;
758 result = xrealloc(result, i+1);
759 result[i] = '\0';
760 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000761}
762
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000763static void examine_file(void)
764{
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000765 print_statusline("Examine: ");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000766 free(filename);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000767 filename = less_gets(sizeof("Examine: ")-1);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000768 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000769 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000770 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000771 num_files++; */
772 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000773 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000774 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000775}
776
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000777/* This function changes the file currently being paged. direction can be one of the following:
778 * -1: go back one file
779 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000780 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000781static void change_file(int direction)
782{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000783 if (current_file != ((direction > 0) ? num_files : 1)) {
784 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000785 free(filename);
786 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000787 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000788 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000789 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000790 }
791}
792
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000793static void remove_current_file(void)
794{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000795 int i;
796
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000797 if (num_files < 2)
798 return;
799
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000800 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000801 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000802 for (i = 3; i <= num_files; i++)
803 files[i - 2] = files[i - 1];
804 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000805 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000806 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000807 for (i = 2; i <= num_files; i++)
808 files[i - 2] = files[i - 1];
809 num_files--;
810 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000811 }
812}
813
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000814static void colon_process(void)
815{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000816 int keypress;
817
818 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000819 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000820
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000821 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000822 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000823 case 'd':
824 remove_current_file();
825 break;
826 case 'e':
827 examine_file();
828 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000829#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000830 case 'f':
831 m_status_print();
832 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000833#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000834 case 'n':
835 change_file(1);
836 break;
837 case 'p':
838 change_file(-1);
839 break;
840 case 'q':
841 less_exit(0);
842 break;
843 case 'x':
844 change_file(0);
845 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000846 }
847}
848
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000849#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000850static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000851{
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000852 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000853 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000854 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000855 match = 0;
856 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000857}
858
Rob Landleya2e98042006-04-18 01:53:41 +0000859static void goto_match(int match)
860{
Denis Vlasenko8465a992007-05-09 18:32:54 +0000861 int sv;
862
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000863 if (!pattern_valid)
864 return;
865 if (match < 0)
866 match = 0;
Denis Vlasenko8465a992007-05-09 18:32:54 +0000867 sv = cur_fline;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000868 /* Try to find next match if eof isn't reached yet */
869 if (match >= num_matches && eof_error > 0) {
870 cur_fline = MAXLINES; /* look as far as needed */
871 read_lines();
872 }
873 if (num_matches) {
Denis Vlasenko8465a992007-05-09 18:32:54 +0000874 cap_cur_fline(cur_fline);
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000875 normalize_match_pos(match);
876 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +0000877 } else {
878 cur_fline = sv;
879 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000880 }
Rob Landleya2e98042006-04-18 01:53:41 +0000881}
882
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000883static void fill_match_lines(unsigned pos)
884{
885 if (!pattern_valid)
886 return;
887 /* Run the regex on each line of the current file */
888 while (pos <= max_fline) {
889 /* If this line matches */
890 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
891 /* and we didn't match it last time */
892 && !(num_matches && match_lines[num_matches-1] == pos)
893 ) {
894 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
895 match_lines[num_matches++] = pos;
896 }
897 pos++;
898 }
899}
900
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000901static void regex_process(void)
902{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000903 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000904
905 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000906 free(match_lines);
907 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000908 match_pos = 0;
909 num_matches = 0;
910 if (pattern_valid) {
911 regfree(&pattern);
912 pattern_valid = 0;
913 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000914
Rob Landleya2e98042006-04-18 01:53:41 +0000915 /* Get the uncompiled regular expression from the user */
916 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000917 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000918 uncomp_regex = less_gets(1);
919 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000920 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000921 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000922 return;
923 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000924
Rob Landleya2e98042006-04-18 01:53:41 +0000925 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000926 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000927 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000928 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000929 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000930 free(err);
931 return;
932 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000933
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000934 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000935 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000936 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000937 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000938 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000939 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000940 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000941 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000942 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
943 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000944
945 /* It's possible that no matches are found yet.
946 * goto_match() will read input looking for match,
947 * if needed */
948 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000949}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000950#endif
951
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000952static void number_process(int first_digit)
953{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000954 int i = 1;
955 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000956 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000957 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000958
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000959 num_input[0] = first_digit;
960
961 /* Clear the current line, print a prompt, and then print the digit */
962 clear_line();
963 printf(":%c", first_digit);
964
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000965 /* Receive input until a letter is given */
966 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000967 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000968 if (!num_input[i] || !isdigit(num_input[i]))
969 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000970 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000971 i++;
972 }
973
974 /* Take the final letter out of the digits string */
975 keypress = num_input[i];
976 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000977 num = bb_strtou(num_input, NULL, 10);
978 /* on format error, num == -1 */
979 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000980 buffer_print();
981 return;
982 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000983
984 /* We now know the number and the letter entered, so we process them */
985 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000986 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
987 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000988 break;
989 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
990 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000991 break;
992 case 'g': case '<': case 'G': case '>':
993 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000994 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000995 buffer_line(num - 1);
996 break;
997 case 'p': case '%':
998 num = num * (max_fline / 100); /* + max_fline / 2; */
999 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +00001000 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001001 buffer_line(num);
1002 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001003#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001004 case 'n':
1005 goto_match(match_pos + num);
1006 break;
1007 case '/':
1008 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1009 regex_process();
1010 break;
1011 case '?':
1012 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1013 regex_process();
1014 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001015#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001016 }
1017}
1018
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001019#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001020static void flag_change(void)
1021{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001022 int keypress;
1023
1024 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001025 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001026 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001027
1028 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001029 case 'M':
1030 option_mask32 ^= FLAG_M;
1031 break;
1032 case 'm':
1033 option_mask32 ^= FLAG_m;
1034 break;
1035 case 'E':
1036 option_mask32 ^= FLAG_E;
1037 break;
1038 case '~':
1039 option_mask32 ^= FLAG_TILDE;
1040 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001041 }
1042}
1043
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001044static void show_flag_status(void)
1045{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001046 int keypress;
1047 int flag_val;
1048
1049 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001050 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001051 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001052
1053 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001054 case 'M':
1055 flag_val = option_mask32 & FLAG_M;
1056 break;
1057 case 'm':
1058 flag_val = option_mask32 & FLAG_m;
1059 break;
1060 case '~':
1061 flag_val = option_mask32 & FLAG_TILDE;
1062 break;
1063 case 'N':
1064 flag_val = option_mask32 & FLAG_N;
1065 break;
1066 case 'E':
1067 flag_val = option_mask32 & FLAG_E;
1068 break;
1069 default:
1070 flag_val = 0;
1071 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001072 }
1073
1074 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001075 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001076}
1077#endif
1078
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001079static void save_input_to_file(void)
1080{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001081 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001082 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001083 int i;
1084 FILE *fp;
1085
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001086 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001087 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001088 if (strlen(current_line) > 0) {
1089 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001090 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001091 msg = "Error opening log file";
1092 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001093 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001094 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001095 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001096 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001097 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001098 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001099 ret:
1100 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001101 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001102}
1103
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001104#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001105static void add_mark(void)
1106{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001107 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001108
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001109 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001110 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001111
1112 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001113 /* If we exceed 15 marks, start overwriting previous ones */
1114 if (num_marks == 14)
1115 num_marks = 0;
1116
1117 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001118 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001119 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001120 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001121 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001122 }
1123}
1124
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001125static void goto_mark(void)
1126{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001127 int letter;
1128 int i;
1129
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001130 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001131 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001132 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001133
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001134 if (isalpha(letter)) {
1135 for (i = 0; i <= num_marks; i++)
1136 if (letter == mark_lines[i][0]) {
1137 buffer_line(mark_lines[i][1]);
1138 break;
1139 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001140 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001141 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001142 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001143 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001144}
1145#endif
1146
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001147#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001148static char opp_bracket(char bracket)
1149{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001150 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001151 case '{': case '[':
1152 return bracket + 2;
1153 case '(':
1154 return ')';
1155 case '}': case ']':
1156 return bracket - 2;
1157 case ')':
1158 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001159 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001160 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001161}
1162
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001163static void match_right_bracket(char bracket)
1164{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001165 int bracket_line = -1;
1166 int i;
1167
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001168 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001169 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001170 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001171 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001172 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001173 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1174 bracket_line = i;
1175 break;
1176 }
1177 }
1178 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001179 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001180 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001181}
1182
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001183static void match_left_bracket(char bracket)
1184{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001185 int bracket_line = -1;
1186 int i;
1187
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001188 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001189 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001190 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001191 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001192
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001193 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001194 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1195 bracket_line = i;
1196 break;
1197 }
1198 }
1199 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001200 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001201 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001202}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001203#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001204
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001205static void keypress_process(int keypress)
1206{
Rob Landley9200e792005-09-15 19:26:59 +00001207 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001208 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1209 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001210 break;
1211 case KEY_UP: case 'y': case 'k':
1212 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001213 break;
1214 case PAGE_DOWN: case ' ': case 'z':
1215 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001216 break;
1217 case PAGE_UP: case 'w': case 'b':
1218 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001219 break;
1220 case 'd':
1221 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001222 break;
1223 case 'u':
1224 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001225 break;
1226 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1227 buffer_line(0);
1228 break;
1229 case KEY_END: case 'G': case '>':
1230 cur_fline = MAXLINES;
1231 read_lines();
1232 buffer_line(cur_fline);
1233 break;
1234 case 'q': case 'Q':
1235 less_exit(0);
1236 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001237#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001238 case 'm':
1239 add_mark();
1240 buffer_print();
1241 break;
1242 case '\'':
1243 goto_mark();
1244 buffer_print();
1245 break;
Rob Landley9200e792005-09-15 19:26:59 +00001246#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001247 case 'r': case 'R':
1248 buffer_print();
1249 break;
1250 /*case 'R':
1251 full_repaint();
1252 break;*/
1253 case 's':
1254 save_input_to_file();
1255 break;
1256 case 'E':
1257 examine_file();
1258 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001259#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001260 case '=':
1261 m_status_print();
1262 break;
Rob Landley9200e792005-09-15 19:26:59 +00001263#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001264#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001265 case '/':
1266 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1267 regex_process();
1268 break;
1269 case 'n':
1270 goto_match(match_pos + 1);
1271 break;
1272 case 'N':
1273 goto_match(match_pos - 1);
1274 break;
1275 case '?':
1276 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1277 regex_process();
1278 break;
Rob Landley9200e792005-09-15 19:26:59 +00001279#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001280#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001281 case '-':
1282 flag_change();
1283 buffer_print();
1284 break;
1285 case '_':
1286 show_flag_status();
1287 break;
Rob Landley9200e792005-09-15 19:26:59 +00001288#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001289#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001290 case '{': case '(': case '[':
1291 match_right_bracket(keypress);
1292 break;
1293 case '}': case ')': case ']':
1294 match_left_bracket(keypress);
1295 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001296#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001297 case ':':
1298 colon_process();
1299 break;
Rob Landley9200e792005-09-15 19:26:59 +00001300 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001301
Rob Landley9200e792005-09-15 19:26:59 +00001302 if (isdigit(keypress))
1303 number_process(keypress);
1304}
1305
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001306static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1307{
1308 set_tty_cooked();
1309 exit(1);
1310}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001311
Denis Vlasenko06af2162007-02-03 17:28:39 +00001312int less_main(int argc, char **argv);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001313int less_main(int argc, char **argv)
1314{
Rob Landley9200e792005-09-15 19:26:59 +00001315 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001316
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001317 INIT_G();
1318
Denis Vlasenko95b30712006-12-31 19:23:31 +00001319 /* TODO: -x: do not interpret backspace, -xx: tab also */
1320 /* -xxx: newline also */
1321 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001322 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001323 argc -= optind;
1324 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001325 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001326 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001327
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001328 /* Another popular pager, most, detects when stdout
1329 * is not a tty and turns into cat. This makes sense. */
1330 if (!isatty(STDOUT_FILENO))
1331 return bb_cat(argv);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +00001332 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1333 if (kbd_fd < 0)
1334 return bb_cat(argv);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001335
Rob Landley9200e792005-09-15 19:26:59 +00001336 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001337 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001338 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001339 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001340 bb_show_usage();
1341 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001342 } else
1343 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001344
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001345 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1346 /* 20: two tabstops + 4 */
1347 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001348 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001349 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001350
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001351 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001352 if (option_mask32 & FLAG_TILDE)
1353 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001354
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001355 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001356 signal(SIGTERM, sig_catcher);
1357 signal(SIGINT, sig_catcher);
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001358 term_less = term_orig;
1359 term_less.c_lflag &= ~(ICANON | ECHO);
1360 term_less.c_iflag &= ~(IXON | ICRNL);
1361 /*term_less.c_oflag &= ~ONLCR;*/
1362 term_less.c_cc[VMIN] = 1;
1363 term_less.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001364
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001365 /* Want to do it just once, but it doesn't work, */
1366 /* so we are redoing it (see code above). Mystery... */
Denis Vlasenkoa1d24a02007-05-31 15:55:03 +00001367 /*tcsetattr(kbd_fd, TCSANOW, &term_less);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001368
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001369 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001370 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001371 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001372 keypress_process(keypress);
1373 }
1374}