blob: 79732cc416c2acd102205ada61d729a8d44da20d [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?)
54 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 Vlasenkob30418a2007-02-14 20:49:14 +0000791 if (!pattern_valid)
792 return;
793 if (match < 0)
794 match = 0;
795 /* Try to find next match if eof isn't reached yet */
796 if (match >= num_matches && eof_error > 0) {
797 cur_fline = MAXLINES; /* look as far as needed */
798 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000799 cap_cur_fline(cur_fline);
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000800 }
801 if (num_matches) {
802 normalize_match_pos(match);
803 buffer_line(match_lines[match_pos]);
804 }
Rob Landleya2e98042006-04-18 01:53:41 +0000805}
806
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000807static void fill_match_lines(unsigned pos)
808{
809 if (!pattern_valid)
810 return;
811 /* Run the regex on each line of the current file */
812 while (pos <= max_fline) {
813 /* If this line matches */
814 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
815 /* and we didn't match it last time */
816 && !(num_matches && match_lines[num_matches-1] == pos)
817 ) {
818 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
819 match_lines[num_matches++] = pos;
820 }
821 pos++;
822 }
823}
824
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000825static void regex_process(void)
826{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000827 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000828
829 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000830 free(match_lines);
831 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000832 match_pos = 0;
833 num_matches = 0;
834 if (pattern_valid) {
835 regfree(&pattern);
836 pattern_valid = 0;
837 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000838
Rob Landleya2e98042006-04-18 01:53:41 +0000839 /* Get the uncompiled regular expression from the user */
840 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000841 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000842 uncomp_regex = less_gets(1);
843 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000844 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000845 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000846 return;
847 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000848
Rob Landleya2e98042006-04-18 01:53:41 +0000849 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000850 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000851 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000852 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000853 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000854 free(err);
855 return;
856 }
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000857
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000858 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000859 match_pos = 0;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000860 fill_match_lines(0);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000861 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000862 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000863 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000864 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000865 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000866 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
867 match_pos--;
Denis Vlasenkoa1c63122007-03-08 18:12:01 +0000868
869 /* It's possible that no matches are found yet.
870 * goto_match() will read input looking for match,
871 * if needed */
872 goto_match(match_pos);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000873}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000874#endif
875
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000876static void number_process(int first_digit)
877{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000878 int i = 1;
879 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000880 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000881 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000882
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000883 num_input[0] = first_digit;
884
885 /* Clear the current line, print a prompt, and then print the digit */
886 clear_line();
887 printf(":%c", first_digit);
888
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000889 /* Receive input until a letter is given */
890 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000891 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000892 if (!num_input[i] || !isdigit(num_input[i]))
893 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000894 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000895 i++;
896 }
897
898 /* Take the final letter out of the digits string */
899 keypress = num_input[i];
900 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000901 num = bb_strtou(num_input, NULL, 10);
902 /* on format error, num == -1 */
903 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000904 buffer_print();
905 return;
906 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000907
908 /* We now know the number and the letter entered, so we process them */
909 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000910 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
911 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000912 break;
913 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
914 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000915 break;
916 case 'g': case '<': case 'G': case '>':
917 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000918 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000919 buffer_line(num - 1);
920 break;
921 case 'p': case '%':
922 num = num * (max_fline / 100); /* + max_fline / 2; */
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);
926 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000927#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000928 case 'n':
929 goto_match(match_pos + num);
930 break;
931 case '/':
932 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
933 regex_process();
934 break;
935 case '?':
936 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
937 regex_process();
938 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000939#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000940 }
941}
942
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000943#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000944static void flag_change(void)
945{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000946 int keypress;
947
948 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000949 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000950 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000951
952 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000953 case 'M':
954 option_mask32 ^= FLAG_M;
955 break;
956 case 'm':
957 option_mask32 ^= FLAG_m;
958 break;
959 case 'E':
960 option_mask32 ^= FLAG_E;
961 break;
962 case '~':
963 option_mask32 ^= FLAG_TILDE;
964 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000965 }
966}
967
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000968static void show_flag_status(void)
969{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000970 int keypress;
971 int flag_val;
972
973 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000974 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000975 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000976
977 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000978 case 'M':
979 flag_val = option_mask32 & FLAG_M;
980 break;
981 case 'm':
982 flag_val = option_mask32 & FLAG_m;
983 break;
984 case '~':
985 flag_val = option_mask32 & FLAG_TILDE;
986 break;
987 case 'N':
988 flag_val = option_mask32 & FLAG_N;
989 break;
990 case 'E':
991 flag_val = option_mask32 & FLAG_E;
992 break;
993 default:
994 flag_val = 0;
995 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000996 }
997
998 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000999 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001000}
1001#endif
1002
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001003static void save_input_to_file(void)
1004{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001005 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001006 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001007 int i;
1008 FILE *fp;
1009
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001010 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001011 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001012 if (strlen(current_line) > 0) {
1013 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001014 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001015 msg = "Error opening log file";
1016 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001017 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001018 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001019 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001020 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001021 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001022 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001023 ret:
1024 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001025 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001026}
1027
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001028#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001029static void add_mark(void)
1030{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001031 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001032
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001033 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001034 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001035
1036 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001037 /* If we exceed 15 marks, start overwriting previous ones */
1038 if (num_marks == 14)
1039 num_marks = 0;
1040
1041 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001042 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001043 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001044 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001045 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001046 }
1047}
1048
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001049static void goto_mark(void)
1050{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001051 int letter;
1052 int i;
1053
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001054 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001055 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001056 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001057
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001058 if (isalpha(letter)) {
1059 for (i = 0; i <= num_marks; i++)
1060 if (letter == mark_lines[i][0]) {
1061 buffer_line(mark_lines[i][1]);
1062 break;
1063 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001064 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001065 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001066 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001067 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001068}
1069#endif
1070
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001071#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001072static char opp_bracket(char bracket)
1073{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001074 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001075 case '{': case '[':
1076 return bracket + 2;
1077 case '(':
1078 return ')';
1079 case '}': case ']':
1080 return bracket - 2;
1081 case ')':
1082 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001083 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001084 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001085}
1086
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001087static void match_right_bracket(char bracket)
1088{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001089 int bracket_line = -1;
1090 int i;
1091
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001092 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001093 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001094 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001095 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001096 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001097 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1098 bracket_line = i;
1099 break;
1100 }
1101 }
1102 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001103 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001104 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001105}
1106
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001107static void match_left_bracket(char bracket)
1108{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001109 int bracket_line = -1;
1110 int i;
1111
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001112 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001113 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001114 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001115 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001116
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001117 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001118 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1119 bracket_line = i;
1120 break;
1121 }
1122 }
1123 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001124 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001125 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001126}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001127#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001128
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001129static void keypress_process(int keypress)
1130{
Rob Landley9200e792005-09-15 19:26:59 +00001131 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001132 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1133 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001134 break;
1135 case KEY_UP: case 'y': case 'k':
1136 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001137 break;
1138 case PAGE_DOWN: case ' ': case 'z':
1139 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001140 break;
1141 case PAGE_UP: case 'w': case 'b':
1142 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001143 break;
1144 case 'd':
1145 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001146 break;
1147 case 'u':
1148 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001149 break;
1150 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1151 buffer_line(0);
1152 break;
1153 case KEY_END: case 'G': case '>':
1154 cur_fline = MAXLINES;
1155 read_lines();
1156 buffer_line(cur_fline);
1157 break;
1158 case 'q': case 'Q':
1159 less_exit(0);
1160 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001161#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001162 case 'm':
1163 add_mark();
1164 buffer_print();
1165 break;
1166 case '\'':
1167 goto_mark();
1168 buffer_print();
1169 break;
Rob Landley9200e792005-09-15 19:26:59 +00001170#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001171 case 'r': case 'R':
1172 buffer_print();
1173 break;
1174 /*case 'R':
1175 full_repaint();
1176 break;*/
1177 case 's':
1178 save_input_to_file();
1179 break;
1180 case 'E':
1181 examine_file();
1182 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001183#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001184 case '=':
1185 m_status_print();
1186 break;
Rob Landley9200e792005-09-15 19:26:59 +00001187#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001188#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001189 case '/':
1190 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1191 regex_process();
1192 break;
1193 case 'n':
1194 goto_match(match_pos + 1);
1195 break;
1196 case 'N':
1197 goto_match(match_pos - 1);
1198 break;
1199 case '?':
1200 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1201 regex_process();
1202 break;
Rob Landley9200e792005-09-15 19:26:59 +00001203#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001204#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001205 case '-':
1206 flag_change();
1207 buffer_print();
1208 break;
1209 case '_':
1210 show_flag_status();
1211 break;
Rob Landley9200e792005-09-15 19:26:59 +00001212#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001213#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001214 case '{': case '(': case '[':
1215 match_right_bracket(keypress);
1216 break;
1217 case '}': case ')': case ']':
1218 match_left_bracket(keypress);
1219 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001220#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001221 case ':':
1222 colon_process();
1223 break;
Rob Landley9200e792005-09-15 19:26:59 +00001224 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001225
Rob Landley9200e792005-09-15 19:26:59 +00001226 if (isdigit(keypress))
1227 number_process(keypress);
1228}
1229
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001230static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1231{
1232 set_tty_cooked();
1233 exit(1);
1234}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001235
Denis Vlasenko06af2162007-02-03 17:28:39 +00001236int less_main(int argc, char **argv);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001237int less_main(int argc, char **argv)
1238{
Rob Landley9200e792005-09-15 19:26:59 +00001239 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001240
Denis Vlasenko95b30712006-12-31 19:23:31 +00001241 /* TODO: -x: do not interpret backspace, -xx: tab also */
1242 /* -xxx: newline also */
1243 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001244 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001245 argc -= optind;
1246 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001247 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001248 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001249
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001250 /* Another popular pager, most, detects when stdout
1251 * is not a tty and turns into cat. This makes sense. */
1252 if (!isatty(STDOUT_FILENO))
1253 return bb_cat(argv);
1254
Rob Landley9200e792005-09-15 19:26:59 +00001255 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001256 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001257 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001258 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001259 bb_show_usage();
1260 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001261 } else
1262 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001263
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001264 kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001265
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001266 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1267 /* 20: two tabstops + 4 */
1268 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001269 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001270 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001271
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001272 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001273 if (option_mask32 & FLAG_TILDE)
1274 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001275
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001276 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001277 signal(SIGTERM, sig_catcher);
1278 signal(SIGINT, sig_catcher);
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001279 term_vi = term_orig;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001280 term_vi.c_lflag &= ~(ICANON | ECHO);
1281 term_vi.c_iflag &= ~(IXON | ICRNL);
1282 /*term_vi.c_oflag &= ~ONLCR;*/
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001283 term_vi.c_cc[VMIN] = 1;
1284 term_vi.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001285
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001286 /* Want to do it just once, but it doesn't work, */
1287 /* so we are redoing it (see code above). Mystery... */
1288 /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001289
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001290 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001291 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001292 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001293 keypress_process(keypress);
1294 }
1295}