blob: b0e2754f807c94e9de6b90e7a5c8db0dd9cf6089 [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',
Denis Vlasenkob30418a2007-02-14 20:49:14 +000051 REAL_KEY_HOME_XTERM = 'H',
52 REAL_KEY_END_XTERM = 'F',
Rob Landley9200e792005-09-15 19:26:59 +000053
Denis Vlasenkof4dff772006-12-24 07:14:17 +000054/* These are the special codes assigned by this program to the special keys */
55 KEY_UP = 20,
56 KEY_DOWN = 21,
57 KEY_RIGHT = 22,
58 KEY_LEFT = 23,
59 PAGE_UP = 24,
60 PAGE_DOWN = 25,
61 KEY_HOME = 26,
62 KEY_END = 27,
63
64/* Absolute max of lines eaten */
65 MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
66
67/* This many "after the end" lines we will show (at max) */
68 TILDES = 1,
69};
70
71static unsigned max_displayed_line;
72static unsigned width;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000073static const char *empty_line_marker = "~";
Rob Landley9200e792005-09-15 19:26:59 +000074
Denis Vlasenkof4dff772006-12-24 07:14:17 +000075static char *filename;
76static char **files;
77static unsigned num_files = 1;
78static unsigned current_file = 1;
79static const char **buffer;
80static const char **flines;
81static int cur_fline; /* signed */
82static unsigned max_fline;
83static unsigned max_lineno; /* this one tracks linewrap */
84
85static ssize_t eof_error = 1; /* eof if 0, error if < 0 */
86static char terminated = 1;
87static size_t readpos;
88static size_t readeof;
89/* last position in last line, taking into account tabs */
90static size_t linepos;
91
Rob Landley9200e792005-09-15 19:26:59 +000092/* Command line options */
Denis Vlasenkof4dff772006-12-24 07:14:17 +000093enum {
94 FLAG_E = 1,
95 FLAG_M = 1 << 1,
96 FLAG_m = 1 << 2,
97 FLAG_N = 1 << 3,
98 FLAG_TILDE = 1 << 4,
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +000099/* hijack command line options variable for internal state vars */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000100 LESS_STATE_MATCH_BACKWARDS = 1 << 15,
101};
Rob Landley9200e792005-09-15 19:26:59 +0000102
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000103#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000104static unsigned mark_lines[15][2];
105static unsigned num_marks;
Rob Landley9200e792005-09-15 19:26:59 +0000106#endif
107
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000108#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000109static unsigned *match_lines;
110static int match_pos; /* signed! */
111static unsigned num_matches;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000112static regex_t pattern;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000113static unsigned pattern_valid;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000114#else
115enum { pattern_valid = 0 };
Rob Landley9200e792005-09-15 19:26:59 +0000116#endif
117
Rob Landley9200e792005-09-15 19:26:59 +0000118static struct termios term_orig, term_vi;
119
120/* File pointer to get input from */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000121static int kbd_fd;
Rob Landley9200e792005-09-15 19:26:59 +0000122
123/* Reset terminal input to normal */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000124static void set_tty_cooked(void)
125{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000126 fflush(stdout);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000127 tcsetattr(kbd_fd, TCSANOW, &term_orig);
Rob Landley9200e792005-09-15 19:26:59 +0000128}
129
Rob Landley9200e792005-09-15 19:26:59 +0000130/* Exit the program gracefully */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000131static void less_exit(int code)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000132{
Rob Landley9200e792005-09-15 19:26:59 +0000133 /* TODO: We really should save the terminal state when we start,
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000134 * and restore it when we exit. Less does this with the
135 * "ti" and "te" termcap commands; can this be done with
136 * only termios.h? */
Rob Landley9200e792005-09-15 19:26:59 +0000137 putchar('\n');
Bernhard Reutner-Fischer882e60c2006-12-04 16:04:50 +0000138 fflush_stdout_and_exit(code);
Rob Landley9200e792005-09-15 19:26:59 +0000139}
140
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000141/* Move the cursor to a position (x,y), where (0,0) is the
Rob Landley9200e792005-09-15 19:26:59 +0000142 top-left corner of the console */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000143static void move_cursor(int line, int row)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000144{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000145 printf("\033[%u;%uH", line, row);
Rob Landley9200e792005-09-15 19:26:59 +0000146}
147
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000148static void clear_line(void)
149{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000150 printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000151}
152
153static void print_hilite(const char *str)
154{
155 printf(HIGHLIGHT"%s"NORMAL, str);
Rob Landley9200e792005-09-15 19:26:59 +0000156}
157
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000158static void print_statusline(const char *str)
159{
160 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000161 printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000162}
163
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000164#if ENABLE_FEATURE_LESS_REGEXP
165static void fill_match_lines(unsigned pos);
166#else
167#define fill_match_lines(pos) ((void)0)
168#endif
169
170
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000171static void read_lines(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000172{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000173#define readbuf bb_common_bufsiz1
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000174 char *current_line, *p;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000175 USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000176 int w = width;
177 char last_terminated = terminated;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000178
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000179 if (option_mask32 & FLAG_N)
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000180 w -= 8;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000181
182 current_line = xmalloc(w);
183 p = current_line;
184 max_fline += last_terminated;
185 if (!last_terminated) {
186 const char *cp = flines[max_fline];
187 if (option_mask32 & FLAG_N)
188 cp += 8;
189 strcpy(current_line, cp);
190 p += strlen(current_line);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000191 } else {
192 linepos = 0;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000193 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000194
195 while (1) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000196 again:
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000197 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000198 terminated = 0;
199 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000200 char c;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000201 /* if no unprocessed chars left, eat more */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000202 if (readpos >= readeof) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000203 ndelay_on(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000204 eof_error = safe_read(0, readbuf, sizeof(readbuf));
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000205 ndelay_off(0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000206 readpos = 0;
207 readeof = eof_error;
208 if (eof_error < 0) {
209 readeof = 0;
210 if (errno != EAGAIN)
211 print_statusline("read error");
212 }
213 if (eof_error <= 0) {
214 goto reached_eof;
215 }
216 }
217 c = readbuf[readpos];
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000218 /* backspace? [needed for manpages] */
219 /* <tab><bs> is (a) insane and */
220 /* (b) harder to do correctly, so we refuse to do it */
221 if (c == '\x8' && linepos && p[-1] != '\t') {
Denis Vlasenko95b30712006-12-31 19:23:31 +0000222 readpos++; /* eat it */
Denis Vlasenko95b30712006-12-31 19:23:31 +0000223 linepos--;
Denis Vlasenko50ddabc2006-12-31 19:36:01 +0000224 *--p = '\0';
Denis Vlasenko95b30712006-12-31 19:23:31 +0000225 continue;
226 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000227 if (c == '\t')
228 linepos += (linepos^7) & 7;
229 linepos++;
230 if (linepos >= w)
231 break;
Denis Vlasenko95b30712006-12-31 19:23:31 +0000232 /* ok, we will eat this char */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000233 readpos++;
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000234 if (c == '\n') { terminated = 1; break; }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000235 /* NUL is substituted by '\n'! */
236 if (c == '\0') c = '\n';
237 *p++ = c;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000238 *p = '\0';
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000239 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000240 /* Corner case: linewrap with only "" wrapping to next line */
241 /* Looks ugly on screen, so we do not store this empty line */
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000242 if (!last_terminated && !current_line[0]) {
243 last_terminated = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000244 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000245 goto again;
246 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000247 reached_eof:
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000248 last_terminated = terminated;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000249 flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000250 if (option_mask32 & FLAG_N) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000251 /* Width of 7 preserves tab spacing in the text */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000252 flines[max_fline] = xasprintf(
253 (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
254 max_lineno % 10000000, current_line);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000255 free(current_line);
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000256 if (terminated)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000257 max_lineno++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000258 } else {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000259 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000260 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000261 if (max_fline >= MAXLINES)
262 break;
263 if (max_fline > cur_fline + max_displayed_line)
264 break;
265 if (eof_error <= 0) {
266 if (eof_error < 0 && errno == EAGAIN) {
267 /* not yet eof or error, reset flag (or else
268 * we will hog CPU - select() will return
269 * immediately */
270 eof_error = 1;
271 }
272 break;
273 }
274 max_fline++;
275 current_line = xmalloc(w);
276 p = current_line;
277 linepos = 0;
Rob Landley9200e792005-09-15 19:26:59 +0000278 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000279 fill_match_lines(old_max_fline);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000280#undef readbuf
Rob Landley9200e792005-09-15 19:26:59 +0000281}
282
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000283#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000284/* Interestingly, writing calc_percent as a function saves around 32 bytes
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000285 * on my build. */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000286static int calc_percent(void)
287{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000288 unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000289 return p <= 100 ? p : 100;
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000290}
291
Rob Landley9200e792005-09-15 19:26:59 +0000292/* Print a status line if -M was specified */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000293static void m_status_print(void)
294{
Rob Landley9200e792005-09-15 19:26:59 +0000295 int percentage;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000296
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000297 clear_line();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000298 printf(HIGHLIGHT"%s", filename);
299 if (num_files > 1)
300 printf(" (file %i of %i)", current_file, num_files);
301 printf(" lines %i-%i/%i ",
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000302 cur_fline + 1, cur_fline + max_displayed_line + 1,
303 max_fline + 1);
304 if (cur_fline >= max_fline - max_displayed_line) {
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000305 printf("(END)"NORMAL);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000306 if (num_files > 1 && current_file != num_files)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000307 printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000308 return;
Rob Landley9200e792005-09-15 19:26:59 +0000309 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000310 percentage = calc_percent();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000311 printf("%i%%"NORMAL, percentage);
Rob Landley9200e792005-09-15 19:26:59 +0000312}
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000313#endif
314
Rob Landley9200e792005-09-15 19:26:59 +0000315/* Print the status line */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000316static void status_print(void)
317{
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000318 const char *p;
319
Rob Landley9200e792005-09-15 19:26:59 +0000320 /* Change the status if flags have been set */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000321#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000322 if (option_mask32 & (FLAG_M|FLAG_m)) {
Rob Landley9200e792005-09-15 19:26:59 +0000323 m_status_print();
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000324 return;
Rob Landley9200e792005-09-15 19:26:59 +0000325 }
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000326 /* No flags set */
Rob Landley9200e792005-09-15 19:26:59 +0000327#endif
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000328
329 clear_line();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000330 if (cur_fline && cur_fline < max_fline - max_displayed_line) {
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000331 putchar(':');
332 return;
333 }
Denis Vlasenkod51d14e2006-12-21 13:57:37 +0000334 p = "(END)";
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000335 if (!cur_fline)
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000336 p = filename;
337 if (num_files > 1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000338 printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000339 p, current_file, num_files);
340 return;
341 }
342 print_hilite(p);
Rob Landley9200e792005-09-15 19:26:59 +0000343}
344
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000345static void cap_cur_fline(int nlines)
346{
347 int diff;
348 if (cur_fline < 0)
349 cur_fline = 0;
350 if (cur_fline + max_displayed_line > max_fline + TILDES) {
351 cur_fline -= nlines;
352 if (cur_fline < 0)
353 cur_fline = 0;
354 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
355 /* As the number of lines requested was too large, we just move
356 to the end of the file */
357 if (diff > 0)
358 cur_fline += diff;
359 }
360}
361
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000362static char controls[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000363 /* NUL: never encountered; TAB: not converted */
364 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000365 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
366 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
367static char ctrlconv[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000368 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000369 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
370 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
371
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000372#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000373static void print_found(const char *line)
374{
375 int match_status;
376 int eflags;
377 char *growline;
378 regmatch_t match_structs;
379
380 char buf[width];
381 const char *str = line;
382 char *p = buf;
383 size_t n;
384
385 while (*str) {
386 n = strcspn(str, controls);
387 if (n) {
388 if (!str[n]) break;
389 memcpy(p, str, n);
390 p += n;
391 str += n;
392 }
393 n = strspn(str, controls);
394 memset(p, '.', n);
395 p += n;
396 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000397 }
398 strcpy(p, str);
399
400 /* buf[] holds quarantined version of str */
401
402 /* Each part of the line that matches has the HIGHLIGHT
403 and NORMAL escape sequences placed around it.
404 NB: we regex against line, but insert text
405 from quarantined copy (buf[]) */
406 str = buf;
407 growline = NULL;
408 eflags = 0;
409 goto start;
410
411 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000412 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000413 growline ? : "",
414 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000415 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000416 str + match_structs.rm_so);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000417 free(growline); growline = new;
418 str += match_structs.rm_eo;
419 line += match_structs.rm_eo;
420 eflags = REG_NOTBOL;
421 start:
422 /* Most of the time doesn't find the regex, optimize for that */
423 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
424 }
425
426 if (!growline) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000427 printf(CLEAR_2_EOL"%s\n", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000428 return;
429 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000430 printf(CLEAR_2_EOL"%s%s\n", growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000431 free(growline);
432}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000433#else
434void print_found(const char *line);
435#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000436
437static void print_ascii(const char *str)
438{
439 char buf[width];
440 char *p;
441 size_t n;
442
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000443 printf(CLEAR_2_EOL);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000444 while (*str) {
445 n = strcspn(str, controls);
446 if (n) {
447 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000448 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000449 str += n;
450 }
451 n = strspn(str, controls);
452 p = buf;
453 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000454 if (*str == 0x7f)
455 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000456 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000457 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
458 /* I want to know who committed this sin */
459 *p++ = '{';
460 else
461 *p++ = ctrlconv[(unsigned char)*str];
462 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000463 } while (--n);
464 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000465 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000466 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000467 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000468}
469
Rob Landley9200e792005-09-15 19:26:59 +0000470/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000471static void buffer_print(void)
472{
Rob Landley9200e792005-09-15 19:26:59 +0000473 int i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000474
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000475 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000476 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000477 if (pattern_valid)
478 print_found(buffer[i]);
479 else
480 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000481 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000482}
483
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000484static void buffer_fill_and_print(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000485{
Rob Landley9200e792005-09-15 19:26:59 +0000486 int i;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000487 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
488 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000489 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000490 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000491 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000492 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000493 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000494}
495
496/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000497static void buffer_down(int nlines)
498{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000499 cur_fline += nlines;
500 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000501 cap_cur_fline(nlines);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000502 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000503}
504
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000505static void buffer_up(int nlines)
506{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000507 cur_fline -= nlines;
508 if (cur_fline < 0) cur_fline = 0;
509 read_lines();
510 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000511}
512
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000513static void buffer_line(int linenum)
514{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000515 if (linenum < 0)
516 linenum = 0;
517 cur_fline = linenum;
518 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000519 if (linenum + max_displayed_line > max_fline)
520 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000521 if (linenum < 0)
522 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000523 cur_fline = linenum;
524 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000525}
526
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000527static void open_file_and_read_lines(void)
528{
529 if (filename) {
530 int fd = xopen(filename, O_RDONLY);
531 dup2(fd, 0);
532 if (fd) close(fd);
533 } else {
534 /* "less" with no arguments in argv[] */
535 /* For status line only */
536 filename = xstrdup(bb_msg_standard_input);
537 }
538 readpos = 0;
539 readeof = 0;
540 linepos = 0;
541 terminated = 1;
542 read_lines();
543}
544
545/* Reinitialize everything for a new file - free the memory and start over */
546static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000547{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000548 int i;
549
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000550 if (flines) {
551 for (i = 0; i <= max_fline; i++)
552 free((void*)(flines[i]));
553 free(flines);
554 flines = NULL;
555 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000556
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000557 max_fline = -1;
558 cur_fline = 0;
559 max_lineno = 0;
560 open_file_and_read_lines();
561 buffer_fill_and_print();
562}
563
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000564static void getch_nowait(char* input, int sz)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000565{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000566 ssize_t rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000567 fd_set readfds;
568 again:
569 fflush(stdout);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000570
571 /* NB: select returns whenever read will not block. Therefore:
572 * (a) with O_NONBLOCK'ed fds select will return immediately
573 * (b) if eof is reached, select will also return
574 * because read will immediately return 0 bytes.
575 * Even if select says that input is available, read CAN block
576 * (switch fd into O_NONBLOCK'ed mode to avoid it)
577 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000578 FD_ZERO(&readfds);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000579 if (max_fline <= cur_fline + max_displayed_line
580 && eof_error > 0 /* did NOT reach eof yet */
581 ) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000582 /* We are interested in stdin */
583 FD_SET(0, &readfds);
584 }
585 FD_SET(kbd_fd, &readfds);
586 tcsetattr(kbd_fd, TCSANOW, &term_vi);
587 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
588
589 input[0] = '\0';
590 ndelay_on(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000591 rd = read(kbd_fd, input, sz);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000592 ndelay_off(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000593 if (rd < 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000594 /* No keyboard input, but we have input on stdin! */
595 if (errno != EAGAIN) /* Huh?? */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000596 return;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000597 read_lines();
598 buffer_fill_and_print();
599 goto again;
600 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000601}
602
603/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000604 * character is ASCII \033, get more characters and assign certain sequences
605 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000606static int less_getch(void)
607{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000608 char input[16];
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000609 unsigned i;
610 again:
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000611 getch_nowait(input, sizeof(input));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000612 /* Detect escape sequences (i.e. arrow keys) and handle
613 * them accordingly */
614
615 if (input[0] == '\033' && input[1] == '[') {
616 set_tty_cooked();
617 i = input[2] - REAL_KEY_UP;
618 if (i < 4)
619 return 20 + i;
620 i = input[2] - REAL_PAGE_UP;
621 if (i < 4)
622 return 24 + i;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000623 if (input[2] == REAL_KEY_HOME_XTERM)
624 return KEY_HOME;
625 if (input[2] == REAL_KEY_END_XTERM)
626 return KEY_END;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000627 return 0;
628 }
629 /* Reject almost all control chars */
630 i = input[0];
631 if (i < ' ' && i != 0x0d && i != 8) goto again;
632 set_tty_cooked();
633 return i;
634}
635
636static char* less_gets(int sz)
637{
638 char c;
639 int i = 0;
640 char *result = xzalloc(1);
641 while (1) {
642 fflush(stdout);
643
644 /* I be damned if I know why is it needed *repeatedly*,
645 * but it is needed. Is it because of stdio? */
646 tcsetattr(kbd_fd, TCSANOW, &term_vi);
647
648 read(kbd_fd, &c, 1);
649 if (c == 0x0d)
650 return result;
651 if (c == 0x7f)
652 c = 8;
653 if (c == 8 && i) {
654 printf("\x8 \x8");
655 i--;
656 }
657 if (c < ' ')
658 continue;
659 if (i >= width - sz - 1)
660 continue; /* len limit */
661 putchar(c);
662 result[i++] = c;
663 result = xrealloc(result, i+1);
664 result[i] = '\0';
665 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000666}
667
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000668static void examine_file(void)
669{
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000670 print_statusline("Examine: ");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000671 free(filename);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000672 filename = less_gets(sizeof("Examine: ")-1);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000673 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000674 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000675 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000676 num_files++; */
677 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000678 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000679 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000680}
681
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000682/* This function changes the file currently being paged. direction can be one of the following:
683 * -1: go back one file
684 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000685 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000686static void change_file(int direction)
687{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000688 if (current_file != ((direction > 0) ? num_files : 1)) {
689 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000690 free(filename);
691 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000692 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000693 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000694 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000695 }
696}
697
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000698static void remove_current_file(void)
699{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000700 int i;
701
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000702 if (num_files < 2)
703 return;
704
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000705 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000706 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000707 for (i = 3; i <= num_files; i++)
708 files[i - 2] = files[i - 1];
709 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000710 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000711 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000712 for (i = 2; i <= num_files; i++)
713 files[i - 2] = files[i - 1];
714 num_files--;
715 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000716 }
717}
718
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000719static void colon_process(void)
720{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000721 int keypress;
722
723 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000724 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000725
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000726 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000727 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000728 case 'd':
729 remove_current_file();
730 break;
731 case 'e':
732 examine_file();
733 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000734#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000735 case 'f':
736 m_status_print();
737 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000738#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000739 case 'n':
740 change_file(1);
741 break;
742 case 'p':
743 change_file(-1);
744 break;
745 case 'q':
746 less_exit(0);
747 break;
748 case 'x':
749 change_file(0);
750 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000751 }
752}
753
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000754#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000755static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000756{
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000757 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000758 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000759 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000760 match = 0;
761 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000762}
763
Rob Landleya2e98042006-04-18 01:53:41 +0000764static void goto_match(int match)
765{
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000766 if (!pattern_valid)
767 return;
768 if (match < 0)
769 match = 0;
770 /* Try to find next match if eof isn't reached yet */
771 if (match >= num_matches && eof_error > 0) {
772 cur_fline = MAXLINES; /* look as far as needed */
773 read_lines();
Denis Vlasenko5c1de362007-03-08 16:44:32 +0000774 cap_cur_fline(cur_fline);
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000775 }
776 if (num_matches) {
777 normalize_match_pos(match);
778 buffer_line(match_lines[match_pos]);
779 }
Rob Landleya2e98042006-04-18 01:53:41 +0000780}
781
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000782static void fill_match_lines(unsigned pos)
783{
784 if (!pattern_valid)
785 return;
786 /* Run the regex on each line of the current file */
787 while (pos <= max_fline) {
788 /* If this line matches */
789 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
790 /* and we didn't match it last time */
791 && !(num_matches && match_lines[num_matches-1] == pos)
792 ) {
793 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
794 match_lines[num_matches++] = pos;
795 }
796 pos++;
797 }
798}
799
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000800static void regex_process(void)
801{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000802 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000803
804 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000805 free(match_lines);
806 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000807 match_pos = 0;
808 num_matches = 0;
809 if (pattern_valid) {
810 regfree(&pattern);
811 pattern_valid = 0;
812 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000813
Rob Landleya2e98042006-04-18 01:53:41 +0000814 /* Get the uncompiled regular expression from the user */
815 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000816 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000817 uncomp_regex = less_gets(1);
818 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000819 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000820 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000821 return;
822 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000823
Rob Landleya2e98042006-04-18 01:53:41 +0000824 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000825 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000826 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000827 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000828 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000829 free(err);
830 return;
831 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000832 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000833 match_pos = 0;
Rob Landleya2e98042006-04-18 01:53:41 +0000834
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000835 fill_match_lines(0);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000836
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000837 if (num_matches == 0 || max_fline <= max_displayed_line) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000838 buffer_print();
839 return;
840 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000841 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000842 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000843 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000844 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000845 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000846 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
847 match_pos--;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000848 normalize_match_pos(match_pos);
849 buffer_line(match_lines[match_pos]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000850}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000851#endif
852
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000853static void number_process(int first_digit)
854{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000855 int i = 1;
856 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000857 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000858 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000859
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000860 num_input[0] = first_digit;
861
862 /* Clear the current line, print a prompt, and then print the digit */
863 clear_line();
864 printf(":%c", first_digit);
865
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000866 /* Receive input until a letter is given */
867 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000868 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000869 if (!num_input[i] || !isdigit(num_input[i]))
870 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000871 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000872 i++;
873 }
874
875 /* Take the final letter out of the digits string */
876 keypress = num_input[i];
877 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000878 num = bb_strtou(num_input, NULL, 10);
879 /* on format error, num == -1 */
880 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000881 buffer_print();
882 return;
883 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000884
885 /* We now know the number and the letter entered, so we process them */
886 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000887 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
888 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000889 break;
890 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
891 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000892 break;
893 case 'g': case '<': case 'G': case '>':
894 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000895 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000896 buffer_line(num - 1);
897 break;
898 case 'p': case '%':
899 num = num * (max_fline / 100); /* + max_fline / 2; */
900 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000901 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000902 buffer_line(num);
903 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000904#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000905 case 'n':
906 goto_match(match_pos + num);
907 break;
908 case '/':
909 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
910 regex_process();
911 break;
912 case '?':
913 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
914 regex_process();
915 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000916#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000917 }
918}
919
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000920#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000921static void flag_change(void)
922{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000923 int keypress;
924
925 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000926 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000927 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000928
929 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000930 case 'M':
931 option_mask32 ^= FLAG_M;
932 break;
933 case 'm':
934 option_mask32 ^= FLAG_m;
935 break;
936 case 'E':
937 option_mask32 ^= FLAG_E;
938 break;
939 case '~':
940 option_mask32 ^= FLAG_TILDE;
941 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000942 }
943}
944
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000945static void show_flag_status(void)
946{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000947 int keypress;
948 int flag_val;
949
950 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000951 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000952 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000953
954 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000955 case 'M':
956 flag_val = option_mask32 & FLAG_M;
957 break;
958 case 'm':
959 flag_val = option_mask32 & FLAG_m;
960 break;
961 case '~':
962 flag_val = option_mask32 & FLAG_TILDE;
963 break;
964 case 'N':
965 flag_val = option_mask32 & FLAG_N;
966 break;
967 case 'E':
968 flag_val = option_mask32 & FLAG_E;
969 break;
970 default:
971 flag_val = 0;
972 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000973 }
974
975 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000976 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000977}
978#endif
979
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000980static void save_input_to_file(void)
981{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000982 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000983 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000984 int i;
985 FILE *fp;
986
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000987 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000988 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000989 if (strlen(current_line) > 0) {
990 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000991 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000992 msg = "Error opening log file";
993 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000994 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000995 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000996 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000997 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000998 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000999 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +00001000 ret:
1001 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001002 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001003}
1004
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001005#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001006static void add_mark(void)
1007{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001008 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001009
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001010 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001011 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001012
1013 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001014 /* If we exceed 15 marks, start overwriting previous ones */
1015 if (num_marks == 14)
1016 num_marks = 0;
1017
1018 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001019 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001020 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001021 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001022 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001023 }
1024}
1025
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001026static void goto_mark(void)
1027{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001028 int letter;
1029 int i;
1030
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001031 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001032 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001033 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001034
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001035 if (isalpha(letter)) {
1036 for (i = 0; i <= num_marks; i++)
1037 if (letter == mark_lines[i][0]) {
1038 buffer_line(mark_lines[i][1]);
1039 break;
1040 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001041 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001042 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001043 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001044 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001045}
1046#endif
1047
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001048#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001049static char opp_bracket(char bracket)
1050{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001051 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001052 case '{': case '[':
1053 return bracket + 2;
1054 case '(':
1055 return ')';
1056 case '}': case ']':
1057 return bracket - 2;
1058 case ')':
1059 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001060 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001061 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001062}
1063
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001064static void match_right_bracket(char bracket)
1065{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001066 int bracket_line = -1;
1067 int i;
1068
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001069 if (strchr(flines[cur_fline], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001070 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001071 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001072 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001073 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001074 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1075 bracket_line = i;
1076 break;
1077 }
1078 }
1079 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001080 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001081 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001082}
1083
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001084static void match_left_bracket(char bracket)
1085{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001086 int bracket_line = -1;
1087 int i;
1088
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001089 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001090 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001091 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001092 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001093
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001094 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001095 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1096 bracket_line = i;
1097 break;
1098 }
1099 }
1100 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001101 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001102 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001103}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001104#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001105
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001106static void keypress_process(int keypress)
1107{
Rob Landley9200e792005-09-15 19:26:59 +00001108 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001109 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1110 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001111 break;
1112 case KEY_UP: case 'y': case 'k':
1113 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001114 break;
1115 case PAGE_DOWN: case ' ': case 'z':
1116 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001117 break;
1118 case PAGE_UP: case 'w': case 'b':
1119 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001120 break;
1121 case 'd':
1122 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001123 break;
1124 case 'u':
1125 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001126 break;
1127 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1128 buffer_line(0);
1129 break;
1130 case KEY_END: case 'G': case '>':
1131 cur_fline = MAXLINES;
1132 read_lines();
1133 buffer_line(cur_fline);
1134 break;
1135 case 'q': case 'Q':
1136 less_exit(0);
1137 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001138#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001139 case 'm':
1140 add_mark();
1141 buffer_print();
1142 break;
1143 case '\'':
1144 goto_mark();
1145 buffer_print();
1146 break;
Rob Landley9200e792005-09-15 19:26:59 +00001147#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001148 case 'r': case 'R':
1149 buffer_print();
1150 break;
1151 /*case 'R':
1152 full_repaint();
1153 break;*/
1154 case 's':
1155 save_input_to_file();
1156 break;
1157 case 'E':
1158 examine_file();
1159 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001160#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001161 case '=':
1162 m_status_print();
1163 break;
Rob Landley9200e792005-09-15 19:26:59 +00001164#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001165#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001166 case '/':
1167 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1168 regex_process();
1169 break;
1170 case 'n':
1171 goto_match(match_pos + 1);
1172 break;
1173 case 'N':
1174 goto_match(match_pos - 1);
1175 break;
1176 case '?':
1177 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1178 regex_process();
1179 break;
Rob Landley9200e792005-09-15 19:26:59 +00001180#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001181#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001182 case '-':
1183 flag_change();
1184 buffer_print();
1185 break;
1186 case '_':
1187 show_flag_status();
1188 break;
Rob Landley9200e792005-09-15 19:26:59 +00001189#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001190#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001191 case '{': case '(': case '[':
1192 match_right_bracket(keypress);
1193 break;
1194 case '}': case ')': case ']':
1195 match_left_bracket(keypress);
1196 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001197#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001198 case ':':
1199 colon_process();
1200 break;
Rob Landley9200e792005-09-15 19:26:59 +00001201 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001202
Rob Landley9200e792005-09-15 19:26:59 +00001203 if (isdigit(keypress))
1204 number_process(keypress);
1205}
1206
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001207static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1208{
1209 set_tty_cooked();
1210 exit(1);
1211}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001212
Denis Vlasenko06af2162007-02-03 17:28:39 +00001213int less_main(int argc, char **argv);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001214int less_main(int argc, char **argv)
1215{
Rob Landley9200e792005-09-15 19:26:59 +00001216 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001217
Denis Vlasenko95b30712006-12-31 19:23:31 +00001218 /* TODO: -x: do not interpret backspace, -xx: tab also */
1219 /* -xxx: newline also */
1220 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001221 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001222 argc -= optind;
1223 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001224 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001225 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001226
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001227 /* Another popular pager, most, detects when stdout
1228 * is not a tty and turns into cat. This makes sense. */
1229 if (!isatty(STDOUT_FILENO))
1230 return bb_cat(argv);
1231
Rob Landley9200e792005-09-15 19:26:59 +00001232 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001233 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001234 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001235 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001236 bb_show_usage();
1237 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001238 } else
1239 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001240
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001241 kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001242
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001243 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1244 /* 20: two tabstops + 4 */
1245 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001246 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001247 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001248
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001249 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001250 if (option_mask32 & FLAG_TILDE)
1251 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001252
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001253 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001254 signal(SIGTERM, sig_catcher);
1255 signal(SIGINT, sig_catcher);
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001256 term_vi = term_orig;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001257 term_vi.c_lflag &= ~(ICANON | ECHO);
1258 term_vi.c_iflag &= ~(IXON | ICRNL);
1259 /*term_vi.c_oflag &= ~ONLCR;*/
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001260 term_vi.c_cc[VMIN] = 1;
1261 term_vi.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001262
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001263 /* Want to do it just once, but it doesn't work, */
1264 /* so we are redoing it (see code above). Mystery... */
1265 /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001266
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001267 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001268 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001269 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001270 keypress_process(keypress);
1271 }
1272}