blob: 20da022086953385a9ae3cf42823f4d3c212e1c7 [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000024#include "busybox.h"
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000025#if ENABLE_FEATURE_LESS_REGEXP
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +000026#include "xregex.h"
27#endif
28
Denis Vlasenkof4dff772006-12-24 07:14:17 +000029/* FIXME: currently doesn't work right */
30#undef ENABLE_FEATURE_LESS_FLAGCS
31#define ENABLE_FEATURE_LESS_FLAGCS 0
Rob Landley9200e792005-09-15 19:26:59 +000032
33/* The escape codes for highlighted and normal text */
34#define HIGHLIGHT "\033[7m"
35#define NORMAL "\033[0m"
Rob Landley9200e792005-09-15 19:26:59 +000036/* The escape code to clear the screen */
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +000037#define CLEAR "\033[H\033[J"
Denis Vlasenkoe865e812006-12-21 13:24:58 +000038/* The escape code to clear to end of line */
39#define CLEAR_2_EOL "\033[K"
Rob Landley9200e792005-09-15 19:26:59 +000040
Denis Vlasenkof4dff772006-12-24 07:14:17 +000041/* These are the escape sequences corresponding to special keys */
42enum {
43 REAL_KEY_UP = 'A',
44 REAL_KEY_DOWN = 'B',
45 REAL_KEY_RIGHT = 'C',
46 REAL_KEY_LEFT = 'D',
47 REAL_PAGE_UP = '5',
48 REAL_PAGE_DOWN = '6',
49 REAL_KEY_HOME = '7',
50 REAL_KEY_END = '8',
Rob Landley9200e792005-09-15 19:26:59 +000051
Denis Vlasenkof4dff772006-12-24 07:14:17 +000052/* These are the special codes assigned by this program to the special keys */
53 KEY_UP = 20,
54 KEY_DOWN = 21,
55 KEY_RIGHT = 22,
56 KEY_LEFT = 23,
57 PAGE_UP = 24,
58 PAGE_DOWN = 25,
59 KEY_HOME = 26,
60 KEY_END = 27,
61
62/* Absolute max of lines eaten */
63 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
64
65/* This many "after the end" lines we will show (at max) */
66 TILDES = 1,
67};
68
69static unsigned max_displayed_line;
70static unsigned width;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000071static const char *empty_line_marker = "~";
Rob Landley9200e792005-09-15 19:26:59 +000072
Denis Vlasenkof4dff772006-12-24 07:14:17 +000073static char *filename;
74static char **files;
75static unsigned num_files = 1;
76static unsigned current_file = 1;
77static const char **buffer;
78static const char **flines;
79static int cur_fline; /* signed */
80static unsigned max_fline;
81static unsigned max_lineno; /* this one tracks linewrap */
82
83static ssize_t eof_error = 1; /* eof if 0, error if < 0 */
84static char terminated = 1;
85static size_t readpos;
86static size_t readeof;
87/* last position in last line, taking into account tabs */
88static size_t linepos;
89
Rob Landley9200e792005-09-15 19:26:59 +000090/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000091enum {
92 FLAG_E = 1,
93 FLAG_M = 1 << 1,
94 FLAG_m = 1 << 2,
95 FLAG_N = 1 << 3,
96 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +000097/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000098 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
99};
Rob Landley9200e792005-09-15 19:26:59 +0000100
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000101#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000102static unsigned mark_lines[15][2];
103static unsigned num_marks;
Rob Landley9200e792005-09-15 19:26:59 +0000104#endif
105
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000106#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000107static unsigned *match_lines;
108static int match_pos; /* signed! */
109static unsigned num_matches;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000110static regex_t pattern;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000111static unsigned pattern_valid;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000112#else
113enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000114#endif
115
Rob Landley9200e792005-09-15 19:26:59 +0000116static struct termios term_orig, term_vi;
117
118/* File pointer to get input from */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000119static int kbd_fd;
Rob Landley9200e792005-09-15 19:26:59 +0000120
121/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000122static void set_tty_cooked(void)
123{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000124 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000125 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000126}
127
Rob Landley9200e792005-09-15 19:26:59 +0000128/* Exit the program gracefully */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000129static void less_exit(int code)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000130{
Rob Landley9200e792005-09-15 19:26:59 +0000131 /* TODO: We really should save the terminal state when we start,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000132 * and restore it when we exit. Less does this with the
133 * "ti" and "te" termcap commands; can this be done with
134 * only termios.h? */
Rob Landley9200e792005-09-15 19:26:59 +0000135 putchar('\n');
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000136 fflush_stdout_and_exit(code);
Rob Landley9200e792005-09-15 19:26:59 +0000137}
138
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000139/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000140 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000141static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000142{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000143 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000144}
145
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000146static void clear_line(void)
147{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000148 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000149}
150
151static void print_hilite(const char *str)
152{
153 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000154}
155
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000156static void print_statusline(const char *str)
157{
158 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000159 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000160}
161
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000162#if ENABLE_FEATURE_LESS_REGEXP
163static void fill_match_lines(unsigned pos);
164#else
165#define fill_match_lines(pos) ((void)0)
166#endif
167
168
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000169static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000170{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000171#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000172 char *current_line, *p;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000173 USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000174 int w = width;
175 char last_terminated = terminated;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000176
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000177 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000178 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000179
180 current_line = xmalloc(w);
181 p = current_line;
182 max_fline += last_terminated;
183 if (!last_terminated) {
184 const char *cp = flines[max_fline];
185 if (option_mask32 & FLAG_N)
186 cp += 8;
187 strcpy(current_line, cp);
188 p += strlen(current_line);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000189 } else {
190 linepos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000191 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000192
193 while (1) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000194 again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000195 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000196 terminated = 0;
197 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000198 char c;
199 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000200 ndelay_on(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000201 eof_error = safe_read(0, readbuf, sizeof(readbuf));
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000202 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000203 readpos = 0;
204 readeof = eof_error;
205 if (eof_error < 0) {
206 readeof = 0;
207 if (errno != EAGAIN)
208 print_statusline("read error");
209 }
210 if (eof_error <= 0) {
211 goto reached_eof;
212 }
213 }
214 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000215 /* backspace? [needed for manpages] */
216 /* <tab><bs> is (a) insane and */
217 /* (b) harder to do correctly, so we refuse to do it */
218 if (c == '\x8' && linepos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000219 readpos++; /* eat it */
Denis Vlasenko95b30712006-12-31 19:23:31 +0000220 linepos--;
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000221 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000222 continue;
223 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000224 if (c == '\t')
225 linepos += (linepos^7) & 7;
226 linepos++;
227 if (linepos >= w)
228 break;
Denis Vlasenko95b30712006-12-31 19:23:31 +0000229 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000230 readpos++;
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000231 if (c == '\n') { terminated = 1; break; }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000232 /* NUL is substituted by '\n'! */
233 if (c == '\0') c = '\n';
234 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000235 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000236 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000237 /* Corner case: linewrap with only "" wrapping to next line */
238 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000239 if (!last_terminated && !current_line[0]) {
240 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000241 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000242 goto again;
243 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000244 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000245 last_terminated = terminated;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000246 flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000247 if (option_mask32 & FLAG_N) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000248 /* Width of 7 preserves tab spacing in the text */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000249 flines[max_fline] = xasprintf(
250 (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
251 max_lineno % 10000000, current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000252 free(current_line);
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000253 if (terminated)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000254 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000255 } else {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000256 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000257 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000258 if (max_fline >= MAXLINES)
259 break;
260 if (max_fline > cur_fline + max_displayed_line)
261 break;
262 if (eof_error <= 0) {
263 if (eof_error < 0 && errno == EAGAIN) {
264 /* not yet eof or error, reset flag (or else
265 * we will hog CPU - select() will return
266 * immediately */
267 eof_error = 1;
268 }
269 break;
270 }
271 max_fline++;
272 current_line = xmalloc(w);
273 p = current_line;
274 linepos = 0;
Rob Landley9200e792005-09-15 19:26:59 +0000275 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000276 fill_match_lines(old_max_fline);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000277#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000278}
279
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000280#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000281/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000282 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000283static int calc_percent(void)
284{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000285 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000286 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000287}
288
Rob Landley9200e792005-09-15 19:26:59 +0000289/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000290static void m_status_print(void)
291{
Rob Landley9200e792005-09-15 19:26:59 +0000292 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000293
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000294 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000295 printf(HIGHLIGHT"%s", filename);
296 if (num_files > 1)
297 printf(" (file %i of %i)", current_file, num_files);
298 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000299 cur_fline + 1, cur_fline + max_displayed_line + 1,
300 max_fline + 1);
301 if (cur_fline >= max_fline - max_displayed_line) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000302 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000303 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000304 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000305 return;
Rob Landley9200e792005-09-15 19:26:59 +0000306 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000307 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000308 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000309}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000310#endif
311
Rob Landley9200e792005-09-15 19:26:59 +0000312/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000313static void status_print(void)
314{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000315 const char *p;
316
Rob Landley9200e792005-09-15 19:26:59 +0000317 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000318#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000319 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000320 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000321 return;
Rob Landley9200e792005-09-15 19:26:59 +0000322 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000323 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000324#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000325
326 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000327 if (cur_fline && cur_fline < max_fline - max_displayed_line) {
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000328 putchar(':');
329 return;
330 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000331 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000332 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000333 p = filename;
334 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000335 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000336 p, current_file, num_files);
337 return;
338 }
339 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000340}
341
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000342static char controls[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000343 /* NUL: never encountered; TAB: not converted */
344 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000345 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
346 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
347static char ctrlconv[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000348 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000349 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
350 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
351
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000352#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000353static void print_found(const char *line)
354{
355 int match_status;
356 int eflags;
357 char *growline;
358 regmatch_t match_structs;
359
360 char buf[width];
361 const char *str = line;
362 char *p = buf;
363 size_t n;
364
365 while (*str) {
366 n = strcspn(str, controls);
367 if (n) {
368 if (!str[n]) break;
369 memcpy(p, str, n);
370 p += n;
371 str += n;
372 }
373 n = strspn(str, controls);
374 memset(p, '.', n);
375 p += n;
376 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000377 }
378 strcpy(p, str);
379
380 /* buf[] holds quarantined version of str */
381
382 /* Each part of the line that matches has the HIGHLIGHT
383 and NORMAL escape sequences placed around it.
384 NB: we regex against line, but insert text
385 from quarantined copy (buf[]) */
386 str = buf;
387 growline = NULL;
388 eflags = 0;
389 goto start;
390
391 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000392 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000393 growline ? : "",
394 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000395 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000396 str + match_structs.rm_so);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000397 free(growline); growline = new;
398 str += match_structs.rm_eo;
399 line += match_structs.rm_eo;
400 eflags = REG_NOTBOL;
401 start:
402 /* Most of the time doesn't find the regex, optimize for that */
403 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
404 }
405
406 if (!growline) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000407 printf(CLEAR_2_EOL"%s\n", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000408 return;
409 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000410 printf(CLEAR_2_EOL"%s%s\n", growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000411 free(growline);
412}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000413#else
414void print_found(const char *line);
415#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000416
417static void print_ascii(const char *str)
418{
419 char buf[width];
420 char *p;
421 size_t n;
422
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000423 printf(CLEAR_2_EOL);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000424 while (*str) {
425 n = strcspn(str, controls);
426 if (n) {
427 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000428 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000429 str += n;
430 }
431 n = strspn(str, controls);
432 p = buf;
433 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000434 if (*str == 0x7f)
435 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000436 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000437 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
438 /* I want to know who committed this sin */
439 *p++ = '{';
440 else
441 *p++ = ctrlconv[(unsigned char)*str];
442 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000443 } while (--n);
444 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000445 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000446 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000447 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000448}
449
Rob Landley9200e792005-09-15 19:26:59 +0000450/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000451static void buffer_print(void)
452{
Rob Landley9200e792005-09-15 19:26:59 +0000453 int i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000454
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000455 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000456 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000457 if (pattern_valid)
458 print_found(buffer[i]);
459 else
460 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000461 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000462}
463
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000464static void buffer_fill_and_print(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000465{
Rob Landley9200e792005-09-15 19:26:59 +0000466 int i;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000467 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
468 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000469 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000470 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000471 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000472 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000473 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000474}
475
476/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000477static void buffer_down(int nlines)
478{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000479 int diff;
480 cur_fline += nlines;
481 read_lines();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000482
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000483 if (cur_fline + max_displayed_line > max_fline + TILDES) {
484 cur_fline -= nlines;
485 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000486 /* As the number of lines requested was too large, we just move
487 to the end of the file */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000488 if (diff > 0)
489 cur_fline += diff;
Rob Landley9200e792005-09-15 19:26:59 +0000490 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000491 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000492}
493
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000494static void buffer_up(int nlines)
495{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000496 cur_fline -= nlines;
497 if (cur_fline < 0) cur_fline = 0;
498 read_lines();
499 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000500}
501
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000502static void buffer_line(int linenum)
503{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000504 if (linenum < 0)
505 linenum = 0;
506 cur_fline = linenum;
507 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000508 if (linenum + max_displayed_line > max_fline)
509 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000510 cur_fline = linenum;
511 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000512}
513
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000514static void open_file_and_read_lines(void)
515{
516 if (filename) {
517 int fd = xopen(filename, O_RDONLY);
518 dup2(fd, 0);
519 if (fd) close(fd);
520 } else {
521 /* "less" with no arguments in argv[] */
522 /* For status line only */
523 filename = xstrdup(bb_msg_standard_input);
524 }
525 readpos = 0;
526 readeof = 0;
527 linepos = 0;
528 terminated = 1;
529 read_lines();
530}
531
532/* Reinitialize everything for a new file - free the memory and start over */
533static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000534{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000535 int i;
536
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000537 if (flines) {
538 for (i = 0; i <= max_fline; i++)
539 free((void*)(flines[i]));
540 free(flines);
541 flines = NULL;
542 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000543
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000544 max_fline = -1;
545 cur_fline = 0;
546 max_lineno = 0;
547 open_file_and_read_lines();
548 buffer_fill_and_print();
549}
550
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000551static void getch_nowait(char* input, int sz)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000552{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000553 ssize_t rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000554 fd_set readfds;
555 again:
556 fflush(stdout);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000557
558 /* NB: select returns whenever read will not block. Therefore:
559 * (a) with O_NONBLOCK'ed fds select will return immediately
560 * (b) if eof is reached, select will also return
561 * because read will immediately return 0 bytes.
562 * Even if select says that input is available, read CAN block
563 * (switch fd into O_NONBLOCK'ed mode to avoid it)
564 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000565 FD_ZERO(&readfds);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000566 if (max_fline <= cur_fline + max_displayed_line
567 && eof_error > 0 /* did NOT reach eof yet */
568 ) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000569 /* We are interested in stdin */
570 FD_SET(0, &readfds);
571 }
572 FD_SET(kbd_fd, &readfds);
573 tcsetattr(kbd_fd, TCSANOW, &term_vi);
574 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
575
576 input[0] = '\0';
577 ndelay_on(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000578 rd = read(kbd_fd, input, sz);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000579 ndelay_off(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000580 if (rd < 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000581 /* No keyboard input, but we have input on stdin! */
582 if (errno != EAGAIN) /* Huh?? */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000583 return;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000584 read_lines();
585 buffer_fill_and_print();
586 goto again;
587 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000588}
589
590/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000591 * character is ASCII \033, get more characters and assign certain sequences
592 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000593static int less_getch(void)
594{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000595 char input[16];
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000596 unsigned i;
597 again:
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000598 getch_nowait(input, sizeof(input));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000599 /* Detect escape sequences (i.e. arrow keys) and handle
600 * them accordingly */
601
602 if (input[0] == '\033' && input[1] == '[') {
603 set_tty_cooked();
604 i = input[2] - REAL_KEY_UP;
605 if (i < 4)
606 return 20 + i;
607 i = input[2] - REAL_PAGE_UP;
608 if (i < 4)
609 return 24 + i;
610 return 0;
611 }
612 /* Reject almost all control chars */
613 i = input[0];
614 if (i < ' ' && i != 0x0d && i != 8) goto again;
615 set_tty_cooked();
616 return i;
617}
618
619static char* less_gets(int sz)
620{
621 char c;
622 int i = 0;
623 char *result = xzalloc(1);
624 while (1) {
625 fflush(stdout);
626
627 /* I be damned if I know why is it needed *repeatedly*,
628 * but it is needed. Is it because of stdio? */
629 tcsetattr(kbd_fd, TCSANOW, &term_vi);
630
631 read(kbd_fd, &c, 1);
632 if (c == 0x0d)
633 return result;
634 if (c == 0x7f)
635 c = 8;
636 if (c == 8 && i) {
637 printf("\x8 \x8");
638 i--;
639 }
640 if (c < ' ')
641 continue;
642 if (i >= width - sz - 1)
643 continue; /* len limit */
644 putchar(c);
645 result[i++] = c;
646 result = xrealloc(result, i+1);
647 result[i] = '\0';
648 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000649}
650
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000651static void examine_file(void)
652{
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000653 print_statusline("Examine: ");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000654 free(filename);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000655 filename = less_gets(sizeof("Examine: ")-1);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000656 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000657 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000658 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000659 num_files++; */
660 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000661 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000662 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000663}
664
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000665/* This function changes the file currently being paged. direction can be one of the following:
666 * -1: go back one file
667 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000668 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000669static void change_file(int direction)
670{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000671 if (current_file != ((direction > 0) ? num_files : 1)) {
672 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000673 free(filename);
674 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000675 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000676 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000677 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000678 }
679}
680
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000681static void remove_current_file(void)
682{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000683 int i;
684
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000685 if (num_files < 2)
686 return;
687
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000688 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000689 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000690 for (i = 3; i <= num_files; i++)
691 files[i - 2] = files[i - 1];
692 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000693 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000694 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000695 for (i = 2; i <= num_files; i++)
696 files[i - 2] = files[i - 1];
697 num_files--;
698 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000699 }
700}
701
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000702static void colon_process(void)
703{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000704 int keypress;
705
706 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000707 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000708
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000709 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000710 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000711 case 'd':
712 remove_current_file();
713 break;
714 case 'e':
715 examine_file();
716 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000717#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000718 case 'f':
719 m_status_print();
720 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000721#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000722 case 'n':
723 change_file(1);
724 break;
725 case 'p':
726 change_file(-1);
727 break;
728 case 'q':
729 less_exit(0);
730 break;
731 case 'x':
732 change_file(0);
733 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000734 }
735}
736
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000737#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000738static int normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000739{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000740 match_pos = match;
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000741 if (match >= num_matches)
742 match_pos = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000743 if (match < 0)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000744 match_pos = 0;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000745 return match_pos;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000746}
747
Rob Landleya2e98042006-04-18 01:53:41 +0000748static void goto_match(int match)
749{
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000750 if (num_matches)
751 buffer_line(match_lines[normalize_match_pos(match)]);
Rob Landleya2e98042006-04-18 01:53:41 +0000752}
753
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000754static void fill_match_lines(unsigned pos)
755{
756 if (!pattern_valid)
757 return;
758 /* Run the regex on each line of the current file */
759 while (pos <= max_fline) {
760 /* If this line matches */
761 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
762 /* and we didn't match it last time */
763 && !(num_matches && match_lines[num_matches-1] == pos)
764 ) {
765 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
766 match_lines[num_matches++] = pos;
767 }
768 pos++;
769 }
770}
771
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000772static void regex_process(void)
773{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000774 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000775
776 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000777 free(match_lines);
778 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000779 match_pos = 0;
780 num_matches = 0;
781 if (pattern_valid) {
782 regfree(&pattern);
783 pattern_valid = 0;
784 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000785
Rob Landleya2e98042006-04-18 01:53:41 +0000786 /* Get the uncompiled regular expression from the user */
787 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000788 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000789 uncomp_regex = less_gets(1);
790 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000791 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000792 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000793 return;
794 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000795
Rob Landleya2e98042006-04-18 01:53:41 +0000796 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000797 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000798 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000799 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000800 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000801 free(err);
802 return;
803 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000804 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000805 match_pos = 0;
Rob Landleya2e98042006-04-18 01:53:41 +0000806
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000807 fill_match_lines(0);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000808
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000809 if (num_matches == 0 || max_fline <= max_displayed_line) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000810 buffer_print();
811 return;
812 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000813 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000814 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000815 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000816 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000817 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000818 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
819 match_pos--;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000820 buffer_line(match_lines[normalize_match_pos(match_pos)]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000821}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000822#endif
823
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000824static void number_process(int first_digit)
825{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000826 int i = 1;
827 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000828 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000829 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000830
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000831 num_input[0] = first_digit;
832
833 /* Clear the current line, print a prompt, and then print the digit */
834 clear_line();
835 printf(":%c", first_digit);
836
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000837 /* Receive input until a letter is given */
838 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000839 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000840 if (!num_input[i] || !isdigit(num_input[i]))
841 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000842 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000843 i++;
844 }
845
846 /* Take the final letter out of the digits string */
847 keypress = num_input[i];
848 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000849 num = bb_strtou(num_input, NULL, 10);
850 /* on format error, num == -1 */
851 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000852 buffer_print();
853 return;
854 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000855
856 /* We now know the number and the letter entered, so we process them */
857 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000858 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
859 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000860 break;
861 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
862 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000863 break;
864 case 'g': case '<': case 'G': case '>':
865 cur_fline = num + max_displayed_line;
866 read_lines();
867 buffer_line(num - 1);
868 break;
869 case 'p': case '%':
870 num = num * (max_fline / 100); /* + max_fline / 2; */
871 cur_fline = num + max_displayed_line;
872 read_lines();
873 buffer_line(num);
874 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000875#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000876 case 'n':
877 goto_match(match_pos + num);
878 break;
879 case '/':
880 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
881 regex_process();
882 break;
883 case '?':
884 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
885 regex_process();
886 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000887#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000888 }
889}
890
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000891#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000892static void flag_change(void)
893{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000894 int keypress;
895
896 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000897 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000898 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000899
900 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000901 case 'M':
902 option_mask32 ^= FLAG_M;
903 break;
904 case 'm':
905 option_mask32 ^= FLAG_m;
906 break;
907 case 'E':
908 option_mask32 ^= FLAG_E;
909 break;
910 case '~':
911 option_mask32 ^= FLAG_TILDE;
912 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000913 }
914}
915
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000916static void show_flag_status(void)
917{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000918 int keypress;
919 int flag_val;
920
921 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000922 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000923 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000924
925 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000926 case 'M':
927 flag_val = option_mask32 & FLAG_M;
928 break;
929 case 'm':
930 flag_val = option_mask32 & FLAG_m;
931 break;
932 case '~':
933 flag_val = option_mask32 & FLAG_TILDE;
934 break;
935 case 'N':
936 flag_val = option_mask32 & FLAG_N;
937 break;
938 case 'E':
939 flag_val = option_mask32 & FLAG_E;
940 break;
941 default:
942 flag_val = 0;
943 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000944 }
945
946 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000947 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000948}
949#endif
950
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000951static void save_input_to_file(void)
952{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000953 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000954 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000955 int i;
956 FILE *fp;
957
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000958 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000959 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000960 if (strlen(current_line) > 0) {
961 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000962 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000963 msg = "Error opening log file";
964 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000965 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000966 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000967 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000968 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000969 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000970 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000971 ret:
972 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000973 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000974}
975
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000976#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000977static void add_mark(void)
978{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000979 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000980
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000981 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000982 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000983
984 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000985 /* If we exceed 15 marks, start overwriting previous ones */
986 if (num_marks == 14)
987 num_marks = 0;
988
989 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000990 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000991 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000992 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000993 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000994 }
995}
996
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000997static void goto_mark(void)
998{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000999 int letter;
1000 int i;
1001
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001002 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001003 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001004 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001005
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001006 if (isalpha(letter)) {
1007 for (i = 0; i <= num_marks; i++)
1008 if (letter == mark_lines[i][0]) {
1009 buffer_line(mark_lines[i][1]);
1010 break;
1011 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001012 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001013 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001014 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001015 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001016}
1017#endif
1018
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001019#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001020static char opp_bracket(char bracket)
1021{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001022 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001023 case '{': case '[':
1024 return bracket + 2;
1025 case '(':
1026 return ')';
1027 case '}': case ']':
1028 return bracket - 2;
1029 case ')':
1030 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001031 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001032 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001033}
1034
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001035static void match_right_bracket(char bracket)
1036{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001037 int bracket_line = -1;
1038 int i;
1039
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001040 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001041 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001042 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001043 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001044 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001045 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1046 bracket_line = i;
1047 break;
1048 }
1049 }
1050 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001051 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001052 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001053}
1054
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001055static void match_left_bracket(char bracket)
1056{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001057 int bracket_line = -1;
1058 int i;
1059
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001060 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001061 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001062 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001063 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001064
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001065 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001066 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1067 bracket_line = i;
1068 break;
1069 }
1070 }
1071 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001072 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001073 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001074}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001075#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001076
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001077static void keypress_process(int keypress)
1078{
Rob Landley9200e792005-09-15 19:26:59 +00001079 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001080 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1081 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001082 break;
1083 case KEY_UP: case 'y': case 'k':
1084 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001085 break;
1086 case PAGE_DOWN: case ' ': case 'z':
1087 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001088 break;
1089 case PAGE_UP: case 'w': case 'b':
1090 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001091 break;
1092 case 'd':
1093 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001094 break;
1095 case 'u':
1096 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001097 break;
1098 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1099 buffer_line(0);
1100 break;
1101 case KEY_END: case 'G': case '>':
1102 cur_fline = MAXLINES;
1103 read_lines();
1104 buffer_line(cur_fline);
1105 break;
1106 case 'q': case 'Q':
1107 less_exit(0);
1108 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001109#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001110 case 'm':
1111 add_mark();
1112 buffer_print();
1113 break;
1114 case '\'':
1115 goto_mark();
1116 buffer_print();
1117 break;
Rob Landley9200e792005-09-15 19:26:59 +00001118#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001119 case 'r': case 'R':
1120 buffer_print();
1121 break;
1122 /*case 'R':
1123 full_repaint();
1124 break;*/
1125 case 's':
1126 save_input_to_file();
1127 break;
1128 case 'E':
1129 examine_file();
1130 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001131#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001132 case '=':
1133 m_status_print();
1134 break;
Rob Landley9200e792005-09-15 19:26:59 +00001135#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001136#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001137 case '/':
1138 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1139 regex_process();
1140 break;
1141 case 'n':
1142 goto_match(match_pos + 1);
1143 break;
1144 case 'N':
1145 goto_match(match_pos - 1);
1146 break;
1147 case '?':
1148 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1149 regex_process();
1150 break;
Rob Landley9200e792005-09-15 19:26:59 +00001151#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001152#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001153 case '-':
1154 flag_change();
1155 buffer_print();
1156 break;
1157 case '_':
1158 show_flag_status();
1159 break;
Rob Landley9200e792005-09-15 19:26:59 +00001160#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001161#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001162 case '{': case '(': case '[':
1163 match_right_bracket(keypress);
1164 break;
1165 case '}': case ')': case ']':
1166 match_left_bracket(keypress);
1167 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001168#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001169 case ':':
1170 colon_process();
1171 break;
Rob Landley9200e792005-09-15 19:26:59 +00001172 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001173
Rob Landley9200e792005-09-15 19:26:59 +00001174 if (isdigit(keypress))
1175 number_process(keypress);
1176}
1177
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001178static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1179{
1180 set_tty_cooked();
1181 exit(1);
1182}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001183
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001184int less_main(int argc, char **argv)
1185{
Rob Landley9200e792005-09-15 19:26:59 +00001186 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001187
Denis Vlasenko95b30712006-12-31 19:23:31 +00001188 /* TODO: -x: do not interpret backspace, -xx: tab also */
1189 /* -xxx: newline also */
1190 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001191 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001192 argc -= optind;
1193 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001194 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001195 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001196
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001197 /* Another popular pager, most, detects when stdout
1198 * is not a tty and turns into cat. This makes sense. */
1199 if (!isatty(STDOUT_FILENO))
1200 return bb_cat(argv);
1201
Rob Landley9200e792005-09-15 19:26:59 +00001202 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001203 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001204 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001205 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001206 bb_show_usage();
1207 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001208 } else
1209 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001210
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001211 kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001212
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001213 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1214 /* 20: two tabstops + 4 */
1215 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001216 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001217 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001218
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001219 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001220 if (option_mask32 & FLAG_TILDE)
1221 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001222
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001223 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001224 signal(SIGTERM, sig_catcher);
1225 signal(SIGINT, sig_catcher);
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001226 term_vi = term_orig;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001227 term_vi.c_lflag &= ~(ICANON | ECHO);
1228 term_vi.c_iflag &= ~(IXON | ICRNL);
1229 /*term_vi.c_oflag &= ~ONLCR;*/
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001230 term_vi.c_cc[VMIN] = 1;
1231 term_vi.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001232
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001233 /* Want to do it just once, but it doesn't work, */
1234 /* so we are redoing it (see code above). Mystery... */
1235 /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001236
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001237 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001238 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001239 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001240 keypress_process(keypress);
1241 }
1242}