blob: 3db7c9cdb86df44dae8ab334011cbc476c6c14d7 [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 Vlasenko3f3190e2006-12-21 00:22:03 +0000345static char controls[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000346 /* NUL: never encountered; TAB: not converted */
347 /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000348 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
349 "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
350static char ctrlconv[] =
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000351 /* '\n': it's a former NUL - subst with '@', not 'J' */
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000352 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
353 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
354
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000355#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000356static void print_found(const char *line)
357{
358 int match_status;
359 int eflags;
360 char *growline;
361 regmatch_t match_structs;
362
363 char buf[width];
364 const char *str = line;
365 char *p = buf;
366 size_t n;
367
368 while (*str) {
369 n = strcspn(str, controls);
370 if (n) {
371 if (!str[n]) break;
372 memcpy(p, str, n);
373 p += n;
374 str += n;
375 }
376 n = strspn(str, controls);
377 memset(p, '.', n);
378 p += n;
379 str += n;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000380 }
381 strcpy(p, str);
382
383 /* buf[] holds quarantined version of str */
384
385 /* Each part of the line that matches has the HIGHLIGHT
386 and NORMAL escape sequences placed around it.
387 NB: we regex against line, but insert text
388 from quarantined copy (buf[]) */
389 str = buf;
390 growline = NULL;
391 eflags = 0;
392 goto start;
393
394 while (match_status == 0) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000395 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000396 growline ? : "",
397 match_structs.rm_so, str,
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000398 match_structs.rm_eo - match_structs.rm_so,
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000399 str + match_structs.rm_so);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000400 free(growline); growline = new;
401 str += match_structs.rm_eo;
402 line += match_structs.rm_eo;
403 eflags = REG_NOTBOL;
404 start:
405 /* Most of the time doesn't find the regex, optimize for that */
406 match_status = regexec(&pattern, line, 1, &match_structs, eflags);
407 }
408
409 if (!growline) {
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000410 printf(CLEAR_2_EOL"%s\n", str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000411 return;
412 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000413 printf(CLEAR_2_EOL"%s%s\n", growline, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000414 free(growline);
415}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000416#else
417void print_found(const char *line);
418#endif
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000419
420static void print_ascii(const char *str)
421{
422 char buf[width];
423 char *p;
424 size_t n;
425
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000426 printf(CLEAR_2_EOL);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000427 while (*str) {
428 n = strcspn(str, controls);
429 if (n) {
430 if (!str[n]) break;
Denis Vlasenko806116b2006-12-31 12:14:16 +0000431 printf("%.*s", (int) n, str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000432 str += n;
433 }
434 n = strspn(str, controls);
435 p = buf;
436 do {
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000437 if (*str == 0x7f)
438 *p++ = '?';
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000439 else if (*str == (char)0x9b)
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000440 /* VT100's CSI, aka Meta-ESC. Who's inventor? */
441 /* I want to know who committed this sin */
442 *p++ = '{';
443 else
444 *p++ = ctrlconv[(unsigned char)*str];
445 str++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000446 } while (--n);
447 *p = '\0';
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000448 print_hilite(buf);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000449 }
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000450 puts(str);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000451}
452
Rob Landley9200e792005-09-15 19:26:59 +0000453/* Print the buffer */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000454static void buffer_print(void)
455{
Rob Landley9200e792005-09-15 19:26:59 +0000456 int i;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000457
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000458 move_cursor(0, 0);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000459 for (i = 0; i <= max_displayed_line; i++)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000460 if (pattern_valid)
461 print_found(buffer[i]);
462 else
463 print_ascii(buffer[i]);
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000464 status_print();
Rob Landley9200e792005-09-15 19:26:59 +0000465}
466
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000467static void buffer_fill_and_print(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000468{
Rob Landley9200e792005-09-15 19:26:59 +0000469 int i;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000470 for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
471 buffer[i] = flines[cur_fline + i];
Rob Landley9200e792005-09-15 19:26:59 +0000472 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000473 for (; i <= max_displayed_line; i++) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000474 buffer[i] = empty_line_marker;
Rob Landley9200e792005-09-15 19:26:59 +0000475 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000476 buffer_print();
Rob Landley9200e792005-09-15 19:26:59 +0000477}
478
479/* Move the buffer up and down in the file in order to scroll */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000480static void buffer_down(int nlines)
481{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000482 int diff;
483 cur_fline += nlines;
484 read_lines();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000485
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000486 if (cur_fline + max_displayed_line > max_fline + TILDES) {
487 cur_fline -= nlines;
488 diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000489 /* As the number of lines requested was too large, we just move
490 to the end of the file */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000491 if (diff > 0)
492 cur_fline += diff;
Rob Landley9200e792005-09-15 19:26:59 +0000493 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000494 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000495}
496
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000497static void buffer_up(int nlines)
498{
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000499 cur_fline -= nlines;
500 if (cur_fline < 0) cur_fline = 0;
501 read_lines();
502 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000503}
504
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000505static void buffer_line(int linenum)
506{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000507 if (linenum < 0)
508 linenum = 0;
509 cur_fline = linenum;
510 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000511 if (linenum + max_displayed_line > max_fline)
512 linenum = max_fline - max_displayed_line + TILDES;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000513 if (linenum < 0)
514 linenum = 0;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000515 cur_fline = linenum;
516 buffer_fill_and_print();
Rob Landley9200e792005-09-15 19:26:59 +0000517}
518
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000519static void open_file_and_read_lines(void)
520{
521 if (filename) {
522 int fd = xopen(filename, O_RDONLY);
523 dup2(fd, 0);
524 if (fd) close(fd);
525 } else {
526 /* "less" with no arguments in argv[] */
527 /* For status line only */
528 filename = xstrdup(bb_msg_standard_input);
529 }
530 readpos = 0;
531 readeof = 0;
532 linepos = 0;
533 terminated = 1;
534 read_lines();
535}
536
537/* Reinitialize everything for a new file - free the memory and start over */
538static void reinitialize(void)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000539{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000540 int i;
541
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000542 if (flines) {
543 for (i = 0; i <= max_fline; i++)
544 free((void*)(flines[i]));
545 free(flines);
546 flines = NULL;
547 }
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +0000548
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000549 max_fline = -1;
550 cur_fline = 0;
551 max_lineno = 0;
552 open_file_and_read_lines();
553 buffer_fill_and_print();
554}
555
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000556static void getch_nowait(char* input, int sz)
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000557{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000558 ssize_t rd;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000559 fd_set readfds;
560 again:
561 fflush(stdout);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000562
563 /* NB: select returns whenever read will not block. Therefore:
564 * (a) with O_NONBLOCK'ed fds select will return immediately
565 * (b) if eof is reached, select will also return
566 * because read will immediately return 0 bytes.
567 * Even if select says that input is available, read CAN block
568 * (switch fd into O_NONBLOCK'ed mode to avoid it)
569 */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000570 FD_ZERO(&readfds);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000571 if (max_fline <= cur_fline + max_displayed_line
572 && eof_error > 0 /* did NOT reach eof yet */
573 ) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000574 /* We are interested in stdin */
575 FD_SET(0, &readfds);
576 }
577 FD_SET(kbd_fd, &readfds);
578 tcsetattr(kbd_fd, TCSANOW, &term_vi);
579 select(kbd_fd + 1, &readfds, NULL, NULL, NULL);
580
581 input[0] = '\0';
582 ndelay_on(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000583 rd = read(kbd_fd, input, sz);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000584 ndelay_off(kbd_fd);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000585 if (rd < 0) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000586 /* No keyboard input, but we have input on stdin! */
587 if (errno != EAGAIN) /* Huh?? */
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000588 return;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000589 read_lines();
590 buffer_fill_and_print();
591 goto again;
592 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000593}
594
595/* Grab a character from input without requiring the return key. If the
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000596 * character is ASCII \033, get more characters and assign certain sequences
597 * special return codes. Note that this function works best with raw input. */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000598static int less_getch(void)
599{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000600 char input[16];
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000601 unsigned i;
602 again:
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000603 getch_nowait(input, sizeof(input));
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000604 /* Detect escape sequences (i.e. arrow keys) and handle
605 * them accordingly */
606
607 if (input[0] == '\033' && input[1] == '[') {
608 set_tty_cooked();
609 i = input[2] - REAL_KEY_UP;
610 if (i < 4)
611 return 20 + i;
612 i = input[2] - REAL_PAGE_UP;
613 if (i < 4)
614 return 24 + i;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000615 if (input[2] == REAL_KEY_HOME_XTERM)
616 return KEY_HOME;
617 if (input[2] == REAL_KEY_END_XTERM)
618 return KEY_END;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000619 return 0;
620 }
621 /* Reject almost all control chars */
622 i = input[0];
623 if (i < ' ' && i != 0x0d && i != 8) goto again;
624 set_tty_cooked();
625 return i;
626}
627
628static char* less_gets(int sz)
629{
630 char c;
631 int i = 0;
632 char *result = xzalloc(1);
633 while (1) {
634 fflush(stdout);
635
636 /* I be damned if I know why is it needed *repeatedly*,
637 * but it is needed. Is it because of stdio? */
638 tcsetattr(kbd_fd, TCSANOW, &term_vi);
639
640 read(kbd_fd, &c, 1);
641 if (c == 0x0d)
642 return result;
643 if (c == 0x7f)
644 c = 8;
645 if (c == 8 && i) {
646 printf("\x8 \x8");
647 i--;
648 }
649 if (c < ' ')
650 continue;
651 if (i >= width - sz - 1)
652 continue; /* len limit */
653 putchar(c);
654 result[i++] = c;
655 result = xrealloc(result, i+1);
656 result[i] = '\0';
657 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000658}
659
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000660static void examine_file(void)
661{
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000662 print_statusline("Examine: ");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000663 free(filename);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000664 filename = less_gets(sizeof("Examine: ")-1);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000665 /* files start by = argv. why we assume that argv is infinitely long??
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000666 files[num_files] = filename;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000667 current_file = num_files + 1;
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000668 num_files++; */
669 files[0] = filename;
Denis Vlasenkoe147a722006-12-21 13:26:54 +0000670 num_files = current_file = 1;
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000671 reinitialize();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000672}
673
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000674/* This function changes the file currently being paged. direction can be one of the following:
675 * -1: go back one file
676 * 0: go to the first file
Denis Vlasenko7cea2622006-12-24 07:30:09 +0000677 * 1: go forward one file */
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000678static void change_file(int direction)
679{
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000680 if (current_file != ((direction > 0) ? num_files : 1)) {
681 current_file = direction ? current_file + direction : 1;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000682 free(filename);
683 filename = xstrdup(files[current_file - 1]);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000684 reinitialize();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000685 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000686 print_statusline(direction > 0 ? "No next file" : "No previous file");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000687 }
688}
689
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000690static void remove_current_file(void)
691{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000692 int i;
693
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000694 if (num_files < 2)
695 return;
696
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000697 if (current_file != 1) {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000698 change_file(-1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000699 for (i = 3; i <= num_files; i++)
700 files[i - 2] = files[i - 1];
701 num_files--;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000702 } else {
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000703 change_file(1);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000704 for (i = 2; i <= num_files; i++)
705 files[i - 2] = files[i - 1];
706 num_files--;
707 current_file--;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000708 }
709}
710
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000711static void colon_process(void)
712{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000713 int keypress;
714
715 /* Clear the current line and print a prompt */
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000716 print_statusline(" :");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000717
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000718 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000719 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000720 case 'd':
721 remove_current_file();
722 break;
723 case 'e':
724 examine_file();
725 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000726#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000727 case 'f':
728 m_status_print();
729 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000730#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000731 case 'n':
732 change_file(1);
733 break;
734 case 'p':
735 change_file(-1);
736 break;
737 case 'q':
738 less_exit(0);
739 break;
740 case 'x':
741 change_file(0);
742 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000743 }
744}
745
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000746#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000747static void normalize_match_pos(int match)
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000748{
Denis Vlasenkof65d1332006-12-21 15:23:45 +0000749 if (match >= num_matches)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000750 match = num_matches - 1;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000751 if (match < 0)
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000752 match = 0;
753 match_pos = match;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000754}
755
Rob Landleya2e98042006-04-18 01:53:41 +0000756static void goto_match(int match)
757{
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000758 if (!pattern_valid)
759 return;
760 if (match < 0)
761 match = 0;
762 /* Try to find next match if eof isn't reached yet */
763 if (match >= num_matches && eof_error > 0) {
764 cur_fline = MAXLINES; /* look as far as needed */
765 read_lines();
766 }
767 if (num_matches) {
768 normalize_match_pos(match);
769 buffer_line(match_lines[match_pos]);
770 }
Rob Landleya2e98042006-04-18 01:53:41 +0000771}
772
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000773static void fill_match_lines(unsigned pos)
774{
775 if (!pattern_valid)
776 return;
777 /* Run the regex on each line of the current file */
778 while (pos <= max_fline) {
779 /* If this line matches */
780 if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
781 /* and we didn't match it last time */
782 && !(num_matches && match_lines[num_matches-1] == pos)
783 ) {
784 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
785 match_lines[num_matches++] = pos;
786 }
787 pos++;
788 }
789}
790
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000791static void regex_process(void)
792{
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000793 char *uncomp_regex, *err;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000794
795 /* Reset variables */
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000796 free(match_lines);
797 match_lines = NULL;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000798 match_pos = 0;
799 num_matches = 0;
800 if (pattern_valid) {
801 regfree(&pattern);
802 pattern_valid = 0;
803 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000804
Rob Landleya2e98042006-04-18 01:53:41 +0000805 /* Get the uncompiled regular expression from the user */
806 clear_line();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000807 putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000808 uncomp_regex = less_gets(1);
809 if (!uncomp_regex[0]) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000810 free(uncomp_regex);
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000811 buffer_print();
Rob Landleya2e98042006-04-18 01:53:41 +0000812 return;
813 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000814
Rob Landleya2e98042006-04-18 01:53:41 +0000815 /* Compile the regex and check for errors */
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000816 err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000817 free(uncomp_regex);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000818 if (err) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000819 print_statusline(err);
Denis Vlasenkoe865e812006-12-21 13:24:58 +0000820 free(err);
821 return;
822 }
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000823 pattern_valid = 1;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000824 match_pos = 0;
Rob Landleya2e98042006-04-18 01:53:41 +0000825
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000826 fill_match_lines(0);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000827
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000828 if (num_matches == 0 || max_fline <= max_displayed_line) {
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000829 buffer_print();
830 return;
831 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000832 while (match_pos < num_matches) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000833 if (match_lines[match_pos] > cur_fline)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000834 break;
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000835 match_pos++;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +0000836 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000837 if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
838 match_pos--;
Denis Vlasenkob30418a2007-02-14 20:49:14 +0000839 normalize_match_pos(match_pos);
840 buffer_line(match_lines[match_pos]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000841}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000842#endif
843
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000844static void number_process(int first_digit)
845{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000846 int i = 1;
847 int num;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000848 char num_input[sizeof(int)*4]; /* more than enough */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000849 char keypress;
"Vladimir N. Oleynik"bc374802005-09-19 14:23:46 +0000850
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000851 num_input[0] = first_digit;
852
853 /* Clear the current line, print a prompt, and then print the digit */
854 clear_line();
855 printf(":%c", first_digit);
856
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000857 /* Receive input until a letter is given */
858 while (i < sizeof(num_input)-1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000859 num_input[i] = less_getch();
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000860 if (!num_input[i] || !isdigit(num_input[i]))
861 break;
"Vladimir N. Oleynik"b8fa7e82005-09-22 14:19:34 +0000862 putchar(num_input[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000863 i++;
864 }
865
866 /* Take the final letter out of the digits string */
867 keypress = num_input[i];
868 num_input[i] = '\0';
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000869 num = bb_strtou(num_input, NULL, 10);
870 /* on format error, num == -1 */
871 if (num < 1 || num > MAXLINES) {
Rob Landleya2e98042006-04-18 01:53:41 +0000872 buffer_print();
873 return;
874 }
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000875
876 /* We now know the number and the letter entered, so we process them */
877 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000878 case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
879 buffer_down(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000880 break;
881 case KEY_UP: case 'b': case 'w': case 'y': case 'u':
882 buffer_up(num);
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000883 break;
884 case 'g': case '<': case 'G': case '>':
885 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000886 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000887 buffer_line(num - 1);
888 break;
889 case 'p': case '%':
890 num = num * (max_fline / 100); /* + max_fline / 2; */
891 cur_fline = num + max_displayed_line;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000892 read_lines();
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000893 buffer_line(num);
894 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000895#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000896 case 'n':
897 goto_match(match_pos + num);
898 break;
899 case '/':
900 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
901 regex_process();
902 break;
903 case '?':
904 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
905 regex_process();
906 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000907#endif
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000908 }
909}
910
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000911#if ENABLE_FEATURE_LESS_FLAGCS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000912static void flag_change(void)
913{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000914 int keypress;
915
916 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000917 putchar('-');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000918 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000919
920 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000921 case 'M':
922 option_mask32 ^= FLAG_M;
923 break;
924 case 'm':
925 option_mask32 ^= FLAG_m;
926 break;
927 case 'E':
928 option_mask32 ^= FLAG_E;
929 break;
930 case '~':
931 option_mask32 ^= FLAG_TILDE;
932 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000933 }
934}
935
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000936static void show_flag_status(void)
937{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000938 int keypress;
939 int flag_val;
940
941 clear_line();
Rob Landleyd57ae8b2005-09-18 00:58:49 +0000942 putchar('_');
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000943 keypress = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000944
945 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000946 case 'M':
947 flag_val = option_mask32 & FLAG_M;
948 break;
949 case 'm':
950 flag_val = option_mask32 & FLAG_m;
951 break;
952 case '~':
953 flag_val = option_mask32 & FLAG_TILDE;
954 break;
955 case 'N':
956 flag_val = option_mask32 & FLAG_N;
957 break;
958 case 'E':
959 flag_val = option_mask32 & FLAG_E;
960 break;
961 default:
962 flag_val = 0;
963 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000964 }
965
966 clear_line();
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000967 printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000968}
969#endif
970
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000971static void save_input_to_file(void)
972{
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000973 const char *msg = "";
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000974 char *current_line;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000975 int i;
976 FILE *fp;
977
Denis Vlasenkof1282a82006-12-21 17:03:20 +0000978 print_statusline("Log file: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +0000979 current_line = less_gets(sizeof("Log file: ")-1);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000980 if (strlen(current_line) > 0) {
981 fp = fopen(current_line, "w");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000982 if (!fp) {
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000983 msg = "Error opening log file";
984 goto ret;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000985 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000986 for (i = 0; i <= max_fline; i++)
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000987 fprintf(fp, "%s\n", flines[i]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000988 fclose(fp);
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000989 msg = "Done";
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000990 }
Denis Vlasenko5a4f0992006-12-25 01:23:02 +0000991 ret:
992 print_statusline(msg);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000993 free(current_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000994}
995
Denis Vlasenko9a7cef92006-12-20 02:46:48 +0000996#if ENABLE_FEATURE_LESS_MARKS
Mike Frysinger3a2b1032006-04-16 20:34:26 +0000997static void add_mark(void)
998{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +0000999 int letter;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001000
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001001 print_statusline("Mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001002 letter = less_getch();
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001003
1004 if (isalpha(letter)) {
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001005 /* If we exceed 15 marks, start overwriting previous ones */
1006 if (num_marks == 14)
1007 num_marks = 0;
1008
1009 mark_lines[num_marks][0] = letter;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001010 mark_lines[num_marks][1] = cur_fline;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001011 num_marks++;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001012 } else {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001013 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001014 }
1015}
1016
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001017static void goto_mark(void)
1018{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001019 int letter;
1020 int i;
1021
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001022 print_statusline("Go to mark: ");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001023 letter = less_getch();
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001024 clear_line();
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001025
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001026 if (isalpha(letter)) {
1027 for (i = 0; i <= num_marks; i++)
1028 if (letter == mark_lines[i][0]) {
1029 buffer_line(mark_lines[i][1]);
1030 break;
1031 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001032 if (num_marks == 14 && letter != mark_lines[14][0])
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001033 print_statusline("Mark not set");
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001034 } else
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001035 print_statusline("Invalid mark letter");
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001036}
1037#endif
1038
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001039#if ENABLE_FEATURE_LESS_BRACKETS
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001040static char opp_bracket(char bracket)
1041{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001042 switch (bracket) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001043 case '{': case '[':
1044 return bracket + 2;
1045 case '(':
1046 return ')';
1047 case '}': case ']':
1048 return bracket - 2;
1049 case ')':
1050 return '(';
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001051 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001052 return 0;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001053}
1054
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001055static void match_right_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], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001061 print_statusline("No bracket in top line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001062 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001063 }
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001064 for (i = cur_fline + 1; i < max_fline; i++) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001065 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1066 bracket_line = i;
1067 break;
1068 }
1069 }
1070 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001071 print_statusline("No matching bracket found");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001072 buffer_line(bracket_line - max_displayed_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001073}
1074
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001075static void match_left_bracket(char bracket)
1076{
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001077 int bracket_line = -1;
1078 int i;
1079
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001080 if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001081 print_statusline("No bracket in bottom line");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001082 return;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001083 }
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001084
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001085 for (i = cur_fline + max_displayed_line; i >= 0; i--) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001086 if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
1087 bracket_line = i;
1088 break;
1089 }
1090 }
1091 if (bracket_line == -1)
Denis Vlasenkof1282a82006-12-21 17:03:20 +00001092 print_statusline("No matching bracket found");
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001093 buffer_line(bracket_line);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001094}
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001095#endif /* FEATURE_LESS_BRACKETS */
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001096
Mike Frysinger3a2b1032006-04-16 20:34:26 +00001097static void keypress_process(int keypress)
1098{
Rob Landley9200e792005-09-15 19:26:59 +00001099 switch (keypress) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001100 case KEY_DOWN: case 'e': case 'j': case 0x0d:
1101 buffer_down(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001102 break;
1103 case KEY_UP: case 'y': case 'k':
1104 buffer_up(1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001105 break;
1106 case PAGE_DOWN: case ' ': case 'z':
1107 buffer_down(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001108 break;
1109 case PAGE_UP: case 'w': case 'b':
1110 buffer_up(max_displayed_line + 1);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001111 break;
1112 case 'd':
1113 buffer_down((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001114 break;
1115 case 'u':
1116 buffer_up((max_displayed_line + 1) / 2);
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001117 break;
1118 case KEY_HOME: case 'g': case 'p': case '<': case '%':
1119 buffer_line(0);
1120 break;
1121 case KEY_END: case 'G': case '>':
1122 cur_fline = MAXLINES;
1123 read_lines();
1124 buffer_line(cur_fline);
1125 break;
1126 case 'q': case 'Q':
1127 less_exit(0);
1128 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001129#if ENABLE_FEATURE_LESS_MARKS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001130 case 'm':
1131 add_mark();
1132 buffer_print();
1133 break;
1134 case '\'':
1135 goto_mark();
1136 buffer_print();
1137 break;
Rob Landley9200e792005-09-15 19:26:59 +00001138#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001139 case 'r': case 'R':
1140 buffer_print();
1141 break;
1142 /*case 'R':
1143 full_repaint();
1144 break;*/
1145 case 's':
1146 save_input_to_file();
1147 break;
1148 case 'E':
1149 examine_file();
1150 break;
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001151#if ENABLE_FEATURE_LESS_FLAGS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001152 case '=':
1153 m_status_print();
1154 break;
Rob Landley9200e792005-09-15 19:26:59 +00001155#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001156#if ENABLE_FEATURE_LESS_REGEXP
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001157 case '/':
1158 option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
1159 regex_process();
1160 break;
1161 case 'n':
1162 goto_match(match_pos + 1);
1163 break;
1164 case 'N':
1165 goto_match(match_pos - 1);
1166 break;
1167 case '?':
1168 option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
1169 regex_process();
1170 break;
Rob Landley9200e792005-09-15 19:26:59 +00001171#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001172#if ENABLE_FEATURE_LESS_FLAGCS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001173 case '-':
1174 flag_change();
1175 buffer_print();
1176 break;
1177 case '_':
1178 show_flag_status();
1179 break;
Rob Landley9200e792005-09-15 19:26:59 +00001180#endif
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001181#if ENABLE_FEATURE_LESS_BRACKETS
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001182 case '{': case '(': case '[':
1183 match_right_bracket(keypress);
1184 break;
1185 case '}': case ')': case ']':
1186 match_left_bracket(keypress);
1187 break;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001188#endif
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001189 case ':':
1190 colon_process();
1191 break;
Rob Landley9200e792005-09-15 19:26:59 +00001192 }
Rob Landleyd57ae8b2005-09-18 00:58:49 +00001193
Rob Landley9200e792005-09-15 19:26:59 +00001194 if (isdigit(keypress))
1195 number_process(keypress);
1196}
1197
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001198static void sig_catcher(int sig ATTRIBUTE_UNUSED)
1199{
1200 set_tty_cooked();
1201 exit(1);
1202}
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001203
Denis Vlasenko06af2162007-02-03 17:28:39 +00001204int less_main(int argc, char **argv);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001205int less_main(int argc, char **argv)
1206{
Rob Landley9200e792005-09-15 19:26:59 +00001207 int keypress;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001208
Denis Vlasenko95b30712006-12-31 19:23:31 +00001209 /* TODO: -x: do not interpret backspace, -xx: tab also */
1210 /* -xxx: newline also */
1211 /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001212 getopt32(argc, argv, "EMmN~");
Rob Landley9200e792005-09-15 19:26:59 +00001213 argc -= optind;
1214 argv += optind;
Rob Landley9200e792005-09-15 19:26:59 +00001215 num_files = argc;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001216 files = argv;
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001217
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001218 /* Another popular pager, most, detects when stdout
1219 * is not a tty and turns into cat. This makes sense. */
1220 if (!isatty(STDOUT_FILENO))
1221 return bb_cat(argv);
1222
Rob Landley9200e792005-09-15 19:26:59 +00001223 if (!num_files) {
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001224 if (isatty(STDIN_FILENO)) {
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001225 /* Just "less"? No args and no redirection? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001226 bb_error_msg("missing filename");
Rob Landley9200e792005-09-15 19:26:59 +00001227 bb_show_usage();
1228 }
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001229 } else
1230 filename = xstrdup(files[0]);
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001231
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001232 kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001233
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001234 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
1235 /* 20: two tabstops + 4 */
1236 if (width < 20 || max_displayed_line < 3)
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001237 bb_error_msg_and_die("too narrow here");
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001238 max_displayed_line -= 2;
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001239
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001240 buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001241 if (option_mask32 & FLAG_TILDE)
1242 empty_line_marker = "";
Denis Vlasenko3f3190e2006-12-21 00:22:03 +00001243
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001244 tcgetattr(kbd_fd, &term_orig);
Denis Vlasenko9a7cef92006-12-20 02:46:48 +00001245 signal(SIGTERM, sig_catcher);
1246 signal(SIGINT, sig_catcher);
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001247 term_vi = term_orig;
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001248 term_vi.c_lflag &= ~(ICANON | ECHO);
1249 term_vi.c_iflag &= ~(IXON | ICRNL);
1250 /*term_vi.c_oflag &= ~ONLCR;*/
"Vladimir N. Oleynik"a0ae6de2005-09-19 10:28:43 +00001251 term_vi.c_cc[VMIN] = 1;
1252 term_vi.c_cc[VTIME] = 0;
Denis Vlasenkoe865e812006-12-21 13:24:58 +00001253
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001254 /* Want to do it just once, but it doesn't work, */
1255 /* so we are redoing it (see code above). Mystery... */
1256 /*tcsetattr(kbd_fd, TCSANOW, &term_vi);*/
"Vladimir N. Oleynik"2b306e92005-09-16 12:32:22 +00001257
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001258 reinitialize();
Rob Landley9200e792005-09-15 19:26:59 +00001259 while (1) {
Denis Vlasenkof4dff772006-12-24 07:14:17 +00001260 keypress = less_getch();
Rob Landley9200e792005-09-15 19:26:59 +00001261 keypress_process(keypress);
1262 }
1263}