blob: 138bd471153efe8b91a40a6ac6becf8a9586adb0 [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000026#include "busybox.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
75static unsigned max_displayed_line;
76static unsigned width;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000077static const char *empty_line_marker = "~";
Rob Landley9200e792005-09-15 19:26:59 +000078
Denis Vlasenkof4dff772006-12-24 07:14:17 +000079static char *filename;
80static char **files;
81static unsigned num_files = 1;
82static unsigned current_file = 1;
83static const char **buffer;
84static const char **flines;
85static int cur_fline; /* signed */
86static unsigned max_fline;
87static unsigned max_lineno; /* this one tracks linewrap */
88
89static ssize_t eof_error = 1; /* eof if 0, error if < 0 */
90static char terminated = 1;
91static size_t readpos;
92static size_t readeof;
93/* last position in last line, taking into account tabs */
94static size_t linepos;
95
Rob Landley9200e792005-09-15 19:26:59 +000096/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000097enum {
98 FLAG_E = 1,
99 FLAG_M = 1 << 1,
100 FLAG_m = 1 << 2,
101 FLAG_N = 1 << 3,
102 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000103/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000104 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
105};
Rob Landley9200e792005-09-15 19:26:59 +0000106
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000107#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000108static unsigned mark_lines[15][2];
109static unsigned num_marks;
Rob Landley9200e792005-09-15 19:26:59 +0000110#endif
111
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000112#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000113static unsigned *match_lines;
114static int match_pos; /* signed! */
115static unsigned num_matches;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000116static regex_t pattern;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000117static unsigned pattern_valid;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000118#else
119enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000120#endif
121
Rob Landley9200e792005-09-15 19:26:59 +0000122static struct termios term_orig, term_vi;
123
124/* File pointer to get input from */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000125static int kbd_fd;
Rob Landley9200e792005-09-15 19:26:59 +0000126
127/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000128static void set_tty_cooked(void)
129{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000130 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000131 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000132}
133
Rob Landley9200e792005-09-15 19:26:59 +0000134/* Exit the program gracefully */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000135static void less_exit(int code)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000136{
Rob Landley9200e792005-09-15 19:26:59 +0000137 /* TODO: We really should save the terminal state when we start,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000138 * and restore it when we exit. Less does this with the
139 * "ti" and "te" termcap commands; can this be done with
140 * only termios.h? */
Rob Landley9200e792005-09-15 19:26:59 +0000141 putchar('\n');
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000142 fflush_stdout_and_exit(code);
Rob Landley9200e792005-09-15 19:26:59 +0000143}
144
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000145/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000146 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000147static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000148{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000149 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000150}
151
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000152static void clear_line(void)
153{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000154 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000155}
156
157static void print_hilite(const char *str)
158{
159 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000160}
161
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000162static void print_statusline(const char *str)
163{
164 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000165 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000166}
167
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000168#if ENABLE_FEATURE_LESS_REGEXP
169static void fill_match_lines(unsigned pos);
170#else
171#define fill_match_lines(pos) ((void)0)
172#endif
173
174
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000175static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000176{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000177#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000178 char *current_line, *p;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000179 USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000180 int w = width;
181 char last_terminated = terminated;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000182
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000183 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000184 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000185
186 current_line = xmalloc(w);
187 p = current_line;
188 max_fline += last_terminated;
189 if (!last_terminated) {
190 const char *cp = flines[max_fline];
191 if (option_mask32 & FLAG_N)
192 cp += 8;
193 strcpy(current_line, cp);
194 p += strlen(current_line);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000195 } else {
196 linepos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000197 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000198
199 while (1) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000200 again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000201 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000202 terminated = 0;
203 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000204 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000205 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000206 if (readpos >= readeof) {
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000207 smallint yielded = 0;
208
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000209 ndelay_on(0);
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000210 read_again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000211 eof_error = safe_read(0, readbuf, sizeof(readbuf));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000212 readpos = 0;
213 readeof = eof_error;
214 if (eof_error < 0) {
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000215 if (errno == EAGAIN && !yielded) {
216 /* We can hit EAGAIN while searching for regexp match.
217 * Yield is not 100% reliable solution in general,
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000218 * but for less it should be good enough -
219 * we give stdin supplier some CPU time to produce
220 * more input. We do it just once.
221 * Currently, we do not stop when we found the Nth
222 * occurrence we were looking for. We read till end
223 * (or double EAGAIN). TODO? */
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000224 sched_yield();
225 yielded = 1;
226 goto read_again;
227 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000228 readeof = 0;
229 if (errno != EAGAIN)
230 print_statusline("read error");
231 }
Denis Vlasenko18d6fc12007-03-08 17:52:36 +0000232 ndelay_off(0);
233
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000234 if (eof_error <= 0) {
235 goto reached_eof;
236 }
237 }
238 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000239 /* backspace? [needed for manpages] */
240 /* <tab><bs> is (a) insane and */
241 /* (b) harder to do correctly, so we refuse to do it */
242 if (c == '\x8' && linepos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000243 readpos++; /* eat it */
Denis Vlasenko95b30712006-12-31 19:23:31 +0000244 linepos--;
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000245 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000246 continue;
247 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000248 if (c == '\t')
249 linepos += (linepos^7) & 7;
250 linepos++;
251 if (linepos >= w)
252 break;
Denis Vlasenko95b30712006-12-31 19:23:31 +0000253 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000254 readpos++;
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000255 if (c == '\n') { terminated = 1; break; }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000256 /* NUL is substituted by '\n'! */
257 if (c == '\0') c = '\n';
258 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000259 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000260 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000261 /* Corner case: linewrap with only "" wrapping to next line */
262 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000263 if (!last_terminated && !current_line[0]) {
264 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000265 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000266 goto again;
267 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000268 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000269 last_terminated = terminated;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000270 flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000271 if (option_mask32 & FLAG_N) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000272 /* Width of 7 preserves tab spacing in the text */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000273 flines[max_fline] = xasprintf(
274 (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
275 max_lineno % 10000000, current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000276 free(current_line);
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000277 if (terminated)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000278 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000279 } else {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000280 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000281 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000282 if (max_fline >= MAXLINES)
283 break;
284 if (max_fline > cur_fline + max_displayed_line)
285 break;
286 if (eof_error <= 0) {
287 if (eof_error < 0 && errno == EAGAIN) {
288 /* not yet eof or error, reset flag (or else
289 * we will hog CPU - select() will return
290 * immediately */
291 eof_error = 1;
292 }
293 break;
294 }
295 max_fline++;
296 current_line = xmalloc(w);
297 p = current_line;
298 linepos = 0;
Rob Landley9200e792005-09-15 19:26:59 +0000299 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000300 fill_match_lines(old_max_fline);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000301#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000302}
303
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000304#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000305/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000306 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000307static int calc_percent(void)
308{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000309 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000310 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000311}
312
Rob Landley9200e792005-09-15 19:26:59 +0000313/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000314static void m_status_print(void)
315{
Rob Landley9200e792005-09-15 19:26:59 +0000316 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000317
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000318 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000319 printf(HIGHLIGHT"%s", filename);
320 if (num_files > 1)
321 printf(" (file %i of %i)", current_file, num_files);
322 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000323 cur_fline + 1, cur_fline + max_displayed_line + 1,
324 max_fline + 1);
325 if (cur_fline >= max_fline - max_displayed_line) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000326 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000327 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000328 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000329 return;
Rob Landley9200e792005-09-15 19:26:59 +0000330 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000331 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000332 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000333}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000334#endif
335
Rob Landley9200e792005-09-15 19:26:59 +0000336/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000337static void status_print(void)
338{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000339 const char *p;
340
Rob Landley9200e792005-09-15 19:26:59 +0000341 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000342#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000343 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000344 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000345 return;
Rob Landley9200e792005-09-15 19:26:59 +0000346 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000347 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000348#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000349
350 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000351 if (cur_fline && cur_fline < max_fline - max_displayed_line) {
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000352 putchar(':');
353 return;
354 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000355 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000356 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000357 p = filename;
358 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000359 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000360 p, current_file, num_files);
361 return;
362 }
363 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000364}
365
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000366static void cap_cur_fline(int nlines)
367{
368 int diff;
369 if (cur_fline < 0)
370 cur_fline = 0;
371 if (cur_fline + max_displayed_line > max_fline + TILDES) {
372 cur_fline -= nlines;
373 if (cur_fline < 0)
374 cur_fline = 0;
375 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
376 /* As the number of lines requested was too large, we just move
377 to the end of the file */
378 if (diff > 0)
379 cur_fline += diff;
380 }
381}
382
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000383static char controls[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000384 /* NUL: never encountered; TAB: not converted */
385 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000386 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
387 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
388static char ctrlconv[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000389 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000390 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
391 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
392
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000393#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000394static void print_found(const char *line)
395{
396 int match_status;
397 int eflags;
398 char *growline;
399 regmatch_t match_structs;
400
401 char buf[width];
402 const char *str = line;
403 char *p = buf;
404 size_t n;
405
406 while (*str) {
407 n = strcspn(str, controls);
408 if (n) {
409 if (!str[n]) break;
410 memcpy(p, str, n);
411 p += n;
412 str += n;
413 }
414 n = strspn(str, controls);
415 memset(p, '.', n);
416 p += n;
417 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000418 }
419 strcpy(p, str);
420
421 /* buf[] holds quarantined version of str */
422
423 /* Each part of the line that matches has the HIGHLIGHT
424 and NORMAL escape sequences placed around it.
425 NB: we regex against line, but insert text
426 from quarantined copy (buf[]) */
427 str = buf;
428 growline = NULL;
429 eflags = 0;
430 goto start;
431
432 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000433 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000434 growline ? : "",
435 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000436 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000437 str + match_structs.rm_so);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000438 free(growline); growline = new;
439 str += match_structs.rm_eo;
440 line += match_structs.rm_eo;
441 eflags = REG_NOTBOL;
442 start:
443 /* Most of the time doesn't find the regex, optimize for that */
444 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
445 }
446
447 if (!growline) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000448 printf(CLEAR_2_EOL"%s\n", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000449 return;
450 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000451 printf(CLEAR_2_EOL"%s%s\n", growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000452 free(growline);
453}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000454#else
455void print_found(const char *line);
456#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000457
458static void print_ascii(const char *str)
459{
460 char buf[width];
461 char *p;
462 size_t n;
463
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000464 printf(CLEAR_2_EOL);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000465 while (*str) {
466 n = strcspn(str, controls);
467 if (n) {
468 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000469 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000470 str += n;
471 }
472 n = strspn(str, controls);
473 p = buf;
474 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000475 if (*str == 0x7f)
476 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000477 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000478 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
479 /* I want to know who committed this sin */
480 *p++ = '{';
481 else
482 *p++ = ctrlconv[(unsigned char)*str];
483 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000484 } while (--n);
485 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000486 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000487 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000488 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000489}
490
Rob Landley9200e792005-09-15 19:26:59 +0000491/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000492static void buffer_print(void)
493{
Rob Landley9200e792005-09-15 19:26:59 +0000494 int i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000495
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000496 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000497 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000498 if (pattern_valid)
499 print_found(buffer[i]);
500 else
501 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000502 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000503}
504
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000505static void buffer_fill_and_print(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000506{
Rob Landley9200e792005-09-15 19:26:59 +0000507 int i;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000508 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
509 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000510 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000511 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000512 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000513 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000514 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000515}
516
517/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000518static void buffer_down(int nlines)
519{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000520 cur_fline += nlines;
521 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000522 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000523 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000524}
525
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000526static void buffer_up(int nlines)
527{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000528 cur_fline -= nlines;
529 if (cur_fline < 0) cur_fline = 0;
530 read_lines();
531 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000532}
533
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000534static void buffer_line(int linenum)
535{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000536 if (linenum < 0)
537 linenum = 0;
538 cur_fline = linenum;
539 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000540 if (linenum + max_displayed_line > max_fline)
541 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000542 if (linenum < 0)
543 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000544 cur_fline = linenum;
545 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000546}
547
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000548static void open_file_and_read_lines(void)
549{
550 if (filename) {
551 int fd = xopen(filename, O_RDONLY);
552 dup2(fd, 0);
553 if (fd) close(fd);
554 } else {
555 /* "less" with no arguments in argv[] */
556 /* For status line only */
557 filename = xstrdup(bb_msg_standard_input);
558 }
559 readpos = 0;
560 readeof = 0;
561 linepos = 0;
562 terminated = 1;
563 read_lines();
564}
565
566/* Reinitialize everything for a new file - free the memory and start over */
567static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000568{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000569 int i;
570
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000571 if (flines) {
572 for (i = 0; i <= max_fline; i++)
573 free((void*)(flines[i]));
574 free(flines);
575 flines = NULL;
576 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000577
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000578 max_fline = -1;
579 cur_fline = 0;
580 max_lineno = 0;
581 open_file_and_read_lines();
582 buffer_fill_and_print();
583}
584
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000585static void getch_nowait(char* input, int sz)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000586{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000587 ssize_t rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000588 fd_set readfds;
589 again:
590 fflush(stdout);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000591
592 /* NB: select returns whenever read will not block. Therefore:
593 * (a) with O_NONBLOCK'ed fds select will return immediately
594 * (b) if eof is reached, select will also return
595 * because read will immediately return 0 bytes.
596 * Even if select says that input is available, read CAN block
597 * (switch fd into O_NONBLOCK'ed mode to avoid it)
598 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000599 FD_ZERO(&readfds);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000600 if (max_fline <= cur_fline + max_displayed_line
601 && eof_error > 0 /* did NOT reach eof yet */
602 ) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000603 /* We are interested in stdin */
604 FD_SET(0, &readfds);
605 }
606 FD_SET(kbd_fd, &readfds);
607 tcsetattr(kbd_fd, TCSANOW, &term_vi);
608 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
609
610 input[0] = '\0';
611 ndelay_on(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000612 rd = read(kbd_fd, input, sz);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000613 ndelay_off(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000614 if (rd < 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000615 /* No keyboard input, but we have input on stdin! */
616 if (errno != EAGAIN) /* Huh?? */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000617 return;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000618 read_lines();
619 buffer_fill_and_print();
620 goto again;
621 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000622}
623
624/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000625 * character is ASCII \033, get more characters and assign certain sequences
626 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000627static int less_getch(void)
628{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000629 char input[16];
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000630 unsigned i;
631 again:
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000632 getch_nowait(input, sizeof(input));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000633 /* Detect escape sequences (i.e. arrow keys) and handle
634 * them accordingly */
635
636 if (input[0] == '\033' && input[1] == '[') {
637 set_tty_cooked();
638 i = input[2] - REAL_KEY_UP;
639 if (i < 4)
640 return 20 + i;
641 i = input[2] - REAL_PAGE_UP;
642 if (i < 4)
643 return 24 + i;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000644 if (input[2] == REAL_KEY_HOME_XTERM)
645 return KEY_HOME;
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000646 if (input[2] == REAL_KEY_HOME_ALT)
647 return KEY_HOME;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000648 if (input[2] == REAL_KEY_END_XTERM)
649 return KEY_END;
Denis Vlasenkof5a15762007-03-09 08:55:23 +0000650 if (input[2] == REAL_KEY_END_ALT)
651 return KEY_END;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000652 return 0;
653 }
654 /* Reject almost all control chars */
655 i = input[0];
656 if (i < ' ' && i != 0x0d && i != 8) goto again;
657 set_tty_cooked();
658 return i;
659}
660
661static char* less_gets(int sz)
662{
663 char c;
664 int i = 0;
665 char *result = xzalloc(1);
666 while (1) {
667 fflush(stdout);
668
669 /* I be damned if I know why is it needed *repeatedly*,
670 * but it is needed. Is it because of stdio? */
671 tcsetattr(kbd_fd, TCSANOW, &term_vi);
672
673 read(kbd_fd, &c, 1);
674 if (c == 0x0d)
675 return result;
676 if (c == 0x7f)
677 c = 8;
678 if (c == 8 && i) {
679 printf("\x8 \x8");
680 i--;
681 }
682 if (c < ' ')
683 continue;
684 if (i >= width - sz - 1)
685 continue; /* len limit */
686 putchar(c);
687 result[i++] = c;
688 result = xrealloc(result, i+1);
689 result[i] = '\0';
690 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000691}
692
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000693static void examine_file(void)
694{
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000695 print_statusline("Examine: ");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000696 free(filename);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000697 filename = less_gets(sizeof("Examine: ")-1);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000698 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000699 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000700 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000701 num_files++; */
702 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000703 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000704 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000705}
706
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000707/* This function changes the file currently being paged. direction can be one of the following:
708 * -1: go back one file
709 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000710 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000711static void change_file(int direction)
712{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000713 if (current_file != ((direction > 0) ? num_files : 1)) {
714 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000715 free(filename);
716 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000717 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000718 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000719 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000720 }
721}
722
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000723static void remove_current_file(void)
724{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000725 int i;
726
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000727 if (num_files < 2)
728 return;
729
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000730 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000731 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000732 for (i = 3; i <= num_files; i++)
733 files[i - 2] = files[i - 1];
734 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000735 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000736 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000737 for (i = 2; i <= num_files; i++)
738 files[i - 2] = files[i - 1];
739 num_files--;
740 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000741 }
742}
743
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000744static void colon_process(void)
745{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000746 int keypress;
747
748 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000749 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000750
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000751 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000752 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000753 case 'd':
754 remove_current_file();
755 break;
756 case 'e':
757 examine_file();
758 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000759#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000760 case 'f':
761 m_status_print();
762 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000763#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000764 case 'n':
765 change_file(1);
766 break;
767 case 'p':
768 change_file(-1);
769 break;
770 case 'q':
771 less_exit(0);
772 break;
773 case 'x':
774 change_file(0);
775 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000776 }
777}
778
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000779#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000780static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000781{
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000782 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000783 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000784 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000785 match = 0;
786 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000787}
788
Rob Landleya2e98042006-04-18 01:53:41 +0000789static void goto_match(int match)
790{
Denis Vlasenko8465a992007-05-09 18:32:54 +0000791 int sv;
792
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000793 if (!pattern_valid)
794 return;
795 if (match < 0)
796 match = 0;
Denis Vlasenko8465a992007-05-09 18:32:54 +0000797 sv = cur_fline;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000798 /* Try to find next match if eof isn't reached yet */
799 if (match >= num_matches && eof_error > 0) {
800 cur_fline = MAXLINES; /* look as far as needed */
801 read_lines();
802 }
803 if (num_matches) {
Denis Vlasenko8465a992007-05-09 18:32:54 +0000804 cap_cur_fline(cur_fline);
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000805 normalize_match_pos(match);
806 buffer_line(match_lines[match_pos]);
Denis Vlasenko8465a992007-05-09 18:32:54 +0000807 } else {
808 cur_fline = sv;
809 print_statusline("No matches found");
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000810 }
Rob Landleya2e98042006-04-18 01:53:41 +0000811}
812
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000813static void fill_match_lines(unsigned pos)
814{
815 if (!pattern_valid)
816 return;
817 /* Run the regex on each line of the current file */
818 while (pos <= max_fline) {
819 /* If this line matches */
820 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
821 /* and we didn't match it last time */
822 && !(num_matches && match_lines[num_matches-1] == pos)
823 ) {
824 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
825 match_lines[num_matches++] = pos;
826 }
827 pos++;
828 }
829}
830
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000831static void regex_process(void)
832{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000833 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000834
835 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000836 free(match_lines);
837 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000838 match_pos = 0;
839 num_matches = 0;
840 if (pattern_valid) {
841 regfree(&pattern);
842 pattern_valid = 0;
843 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000844
Rob Landleya2e98042006-04-18 01:53:41 +0000845 /* Get the uncompiled regular expression from the user */
846 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000847 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000848 uncomp_regex = less_gets(1);
849 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000850 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000851 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000852 return;
853 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000854
Rob Landleya2e98042006-04-18 01:53:41 +0000855 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000856 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000857 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000858 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000859 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000860 free(err);
861 return;
862 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000863
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000864 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000865 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000866 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000867 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000868 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000869 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000870 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000871 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000872 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
873 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000874
875 /* It's possible that no matches are found yet.
876 * goto_match() will read input looking for match,
877 * if needed */
878 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000879}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000880#endif
881
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000882static void number_process(int first_digit)
883{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000884 int i = 1;
885 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000886 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000887 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000888
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000889 num_input[0] = first_digit;
890
891 /* Clear the current line, print a prompt, and then print the digit */
892 clear_line();
893 printf(":%c", first_digit);
894
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000895 /* Receive input until a letter is given */
896 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000897 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000898 if (!num_input[i] || !isdigit(num_input[i]))
899 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000900 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000901 i++;
902 }
903
904 /* Take the final letter out of the digits string */
905 keypress = num_input[i];
906 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000907 num = bb_strtou(num_input, NULL, 10);
908 /* on format error, num == -1 */
909 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000910 buffer_print();
911 return;
912 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000913
914 /* We now know the number and the letter entered, so we process them */
915 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000916 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
917 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000918 break;
919 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
920 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000921 break;
922 case 'g': case '<': case 'G': case '>':
923 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000924 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000925 buffer_line(num - 1);
926 break;
927 case 'p': case '%':
928 num = num * (max_fline / 100); /* + max_fline / 2; */
929 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000930 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000931 buffer_line(num);
932 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000933#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000934 case 'n':
935 goto_match(match_pos + num);
936 break;
937 case '/':
938 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
939 regex_process();
940 break;
941 case '?':
942 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
943 regex_process();
944 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000945#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000946 }
947}
948
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000949#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000950static void flag_change(void)
951{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000952 int keypress;
953
954 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000955 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000956 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000957
958 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000959 case 'M':
960 option_mask32 ^= FLAG_M;
961 break;
962 case 'm':
963 option_mask32 ^= FLAG_m;
964 break;
965 case 'E':
966 option_mask32 ^= FLAG_E;
967 break;
968 case '~':
969 option_mask32 ^= FLAG_TILDE;
970 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000971 }
972}
973
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000974static void show_flag_status(void)
975{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000976 int keypress;
977 int flag_val;
978
979 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000980 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000981 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000982
983 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000984 case 'M':
985 flag_val = option_mask32 & FLAG_M;
986 break;
987 case 'm':
988 flag_val = option_mask32 & FLAG_m;
989 break;
990 case '~':
991 flag_val = option_mask32 & FLAG_TILDE;
992 break;
993 case 'N':
994 flag_val = option_mask32 & FLAG_N;
995 break;
996 case 'E':
997 flag_val = option_mask32 & FLAG_E;
998 break;
999 default:
1000 flag_val = 0;
1001 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001002 }
1003
1004 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001005 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001006}
1007#endif
1008
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001009static void save_input_to_file(void)
1010{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001011 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001012 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001013 int i;
1014 FILE *fp;
1015
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001016 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001017 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001018 if (strlen(current_line) > 0) {
1019 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001020 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001021 msg = "Error opening log file";
1022 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001023 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001024 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001025 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001026 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001027 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001028 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001029 ret:
1030 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001031 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001032}
1033
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001034#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001035static void add_mark(void)
1036{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001037 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001038
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001039 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001040 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001041
1042 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001043 /* If we exceed 15 marks, start overwriting previous ones */
1044 if (num_marks == 14)
1045 num_marks = 0;
1046
1047 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001048 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001049 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001050 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001051 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001052 }
1053}
1054
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001055static void goto_mark(void)
1056{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001057 int letter;
1058 int i;
1059
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001060 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001061 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001062 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001063
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001064 if (isalpha(letter)) {
1065 for (i = 0; i <= num_marks; i++)
1066 if (letter == mark_lines[i][0]) {
1067 buffer_line(mark_lines[i][1]);
1068 break;
1069 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001070 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001071 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001072 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001073 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001074}
1075#endif
1076
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001077#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001078static char opp_bracket(char bracket)
1079{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001080 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001081 case '{': case '[':
1082 return bracket + 2;
1083 case '(':
1084 return ')';
1085 case '}': case ']':
1086 return bracket - 2;
1087 case ')':
1088 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001089 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001090 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001091}
1092
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001093static void match_right_bracket(char bracket)
1094{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001095 int bracket_line = -1;
1096 int i;
1097
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001098 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001099 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001100 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001101 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001102 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001103 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1104 bracket_line = i;
1105 break;
1106 }
1107 }
1108 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001109 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001110 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001111}
1112
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001113static void match_left_bracket(char bracket)
1114{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001115 int bracket_line = -1;
1116 int i;
1117
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001118 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001119 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001120 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001121 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001122
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001123 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001124 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1125 bracket_line = i;
1126 break;
1127 }
1128 }
1129 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001130 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001131 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001132}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001133#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001134
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001135static void keypress_process(int keypress)
1136{
Rob Landley9200e792005-09-15 19:26:59 +00001137 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001138 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1139 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001140 break;
1141 case KEY_UP: case 'y': case 'k':
1142 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001143 break;
1144 case PAGE_DOWN: case ' ': case 'z':
1145 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001146 break;
1147 case PAGE_UP: case 'w': case 'b':
1148 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001149 break;
1150 case 'd':
1151 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001152 break;
1153 case 'u':
1154 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001155 break;
1156 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1157 buffer_line(0);
1158 break;
1159 case KEY_END: case 'G': case '>':
1160 cur_fline = MAXLINES;
1161 read_lines();
1162 buffer_line(cur_fline);
1163 break;
1164 case 'q': case 'Q':
1165 less_exit(0);
1166 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001167#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001168 case 'm':
1169 add_mark();
1170 buffer_print();
1171 break;
1172 case '\'':
1173 goto_mark();
1174 buffer_print();
1175 break;
Rob Landley9200e792005-09-15 19:26:59 +00001176#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001177 case 'r': case 'R':
1178 buffer_print();
1179 break;
1180 /*case 'R':
1181 full_repaint();
1182 break;*/
1183 case 's':
1184 save_input_to_file();
1185 break;
1186 case 'E':
1187 examine_file();
1188 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001189#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001190 case '=':
1191 m_status_print();
1192 break;
Rob Landley9200e792005-09-15 19:26:59 +00001193#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001194#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001195 case '/':
1196 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1197 regex_process();
1198 break;
1199 case 'n':
1200 goto_match(match_pos + 1);
1201 break;
1202 case 'N':
1203 goto_match(match_pos - 1);
1204 break;
1205 case '?':
1206 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1207 regex_process();
1208 break;
Rob Landley9200e792005-09-15 19:26:59 +00001209#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001210#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001211 case '-':
1212 flag_change();
1213 buffer_print();
1214 break;
1215 case '_':
1216 show_flag_status();
1217 break;
Rob Landley9200e792005-09-15 19:26:59 +00001218#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001219#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001220 case '{': case '(': case '[':
1221 match_right_bracket(keypress);
1222 break;
1223 case '}': case ')': case ']':
1224 match_left_bracket(keypress);
1225 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001226#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001227 case ':':
1228 colon_process();
1229 break;
Rob Landley9200e792005-09-15 19:26:59 +00001230 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001231
Rob Landley9200e792005-09-15 19:26:59 +00001232 if (isdigit(keypress))
1233 number_process(keypress);
1234}
1235
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001236static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1237{
1238 set_tty_cooked();
1239 exit(1);
1240}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001241
Denis Vlasenko06af2162007-02-03 17:28:39 +00001242int less_main(int argc, char **argv);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001243int less_main(int argc, char **argv)
1244{
Rob Landley9200e792005-09-15 19:26:59 +00001245 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001246
Denis Vlasenko95b30712006-12-31 19:23:31 +00001247 /* TODO: -x: do not interpret backspace, -xx: tab also */
1248 /* -xxx: newline also */
1249 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001250 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001251 argc -= optind;
1252 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001253 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001254 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001255
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001256 /* Another popular pager, most, detects when stdout
1257 * is not a tty and turns into cat. This makes sense. */
1258 if (!isatty(STDOUT_FILENO))
1259 return bb_cat(argv);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +00001260 kbd_fd = open(CURRENT_TTY, O_RDONLY);
1261 if (kbd_fd < 0)
1262 return bb_cat(argv);
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001263
Rob Landley9200e792005-09-15 19:26:59 +00001264 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001265 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001266 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001267 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001268 bb_show_usage();
1269 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001270 } else
1271 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001272
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001273 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1274 /* 20: two tabstops + 4 */
1275 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001276 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001277 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001278
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001279 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001280 if (option_mask32 & FLAG_TILDE)
1281 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001282
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001283 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001284 signal(SIGTERM, sig_catcher);
1285 signal(SIGINT, sig_catcher);
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001286 term_vi = term_orig;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001287 term_vi.c_lflag &= ~(ICANON | ECHO);
1288 term_vi.c_iflag &= ~(IXON | ICRNL);
1289 /*term_vi.c_oflag &= ~ONLCR;*/
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001290 term_vi.c_cc[VMIN] = 1;
1291 term_vi.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001292
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001293 /* Want to do it just once, but it doesn't work, */
1294 /* so we are redoing it (see code above). Mystery... */
1295 /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001296
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001297 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001298 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001299 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001300 keypress_process(keypress);
1301 }
1302}