less.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini less implementation for busybox
  4. *
  5. * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. /*
  10. * TODO:
  11. * - Add more regular expression support - search modifiers, certain matches, etc.
  12. * - Add more complex bracket searching - currently, nested brackets are
  13. * not considered.
  14. * - Add support for "F" as an input. This causes less to act in
  15. * a similar way to tail -f.
  16. * - Allow horizontal scrolling.
  17. *
  18. * Notes:
  19. * - the inp file pointer is used so that keyboard input works after
  20. * redirected input has been read from stdin
  21. */
  22. #include <sched.h> /* sched_yield() */
  23. #include "libbb.h"
  24. #if ENABLE_FEATURE_LESS_REGEXP
  25. #include "xregex.h"
  26. #endif
  27. /* FIXME: currently doesn't work right */
  28. #undef ENABLE_FEATURE_LESS_FLAGCS
  29. #define ENABLE_FEATURE_LESS_FLAGCS 0
  30. /* The escape codes for highlighted and normal text */
  31. #define HIGHLIGHT "\033[7m"
  32. #define NORMAL "\033[0m"
  33. /* The escape code to clear the screen */
  34. #define CLEAR "\033[H\033[J"
  35. /* The escape code to clear to end of line */
  36. #define CLEAR_2_EOL "\033[K"
  37. /* These are the escape sequences corresponding to special keys */
  38. enum {
  39. REAL_KEY_UP = 'A',
  40. REAL_KEY_DOWN = 'B',
  41. REAL_KEY_RIGHT = 'C',
  42. REAL_KEY_LEFT = 'D',
  43. REAL_PAGE_UP = '5',
  44. REAL_PAGE_DOWN = '6',
  45. REAL_KEY_HOME = '7', // vt100? linux vt? or what?
  46. REAL_KEY_END = '8',
  47. REAL_KEY_HOME_ALT = '1', // ESC [1~ (vt100? linux vt? or what?)
  48. REAL_KEY_END_ALT = '4', // ESC [4~
  49. REAL_KEY_HOME_XTERM = 'H',
  50. REAL_KEY_END_XTERM = 'F',
  51. /* These are the special codes assigned by this program to the special keys */
  52. KEY_UP = 20,
  53. KEY_DOWN = 21,
  54. KEY_RIGHT = 22,
  55. KEY_LEFT = 23,
  56. PAGE_UP = 24,
  57. PAGE_DOWN = 25,
  58. KEY_HOME = 26,
  59. KEY_END = 27,
  60. /* Absolute max of lines eaten */
  61. MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
  62. /* This many "after the end" lines we will show (at max) */
  63. TILDES = 1,
  64. };
  65. /* Command line options */
  66. enum {
  67. FLAG_E = 1,
  68. FLAG_M = 1 << 1,
  69. FLAG_m = 1 << 2,
  70. FLAG_N = 1 << 3,
  71. FLAG_TILDE = 1 << 4,
  72. /* hijack command line options variable for internal state vars */
  73. LESS_STATE_MATCH_BACKWARDS = 1 << 15,
  74. };
  75. #if !ENABLE_FEATURE_LESS_REGEXP
  76. enum { pattern_valid = 0 };
  77. #endif
  78. struct globals {
  79. int cur_fline; /* signed */
  80. int kbd_fd; /* fd to get input from */
  81. int less_gets_pos;
  82. /* last position in last line, taking into account tabs */
  83. size_t linepos;
  84. unsigned max_displayed_line;
  85. unsigned max_fline;
  86. unsigned max_lineno; /* this one tracks linewrap */
  87. unsigned width;
  88. ssize_t eof_error; /* eof if 0, error if < 0 */
  89. ssize_t readpos;
  90. ssize_t readeof; /* must be signed */
  91. const char **buffer;
  92. const char **flines;
  93. const char *empty_line_marker;
  94. unsigned num_files;
  95. unsigned current_file;
  96. char *filename;
  97. char **files;
  98. #if ENABLE_FEATURE_LESS_MARKS
  99. unsigned num_marks;
  100. unsigned mark_lines[15][2];
  101. #endif
  102. #if ENABLE_FEATURE_LESS_REGEXP
  103. unsigned *match_lines;
  104. int match_pos; /* signed! */
  105. int wanted_match; /* signed! */
  106. int num_matches;
  107. regex_t pattern;
  108. smallint pattern_valid;
  109. #endif
  110. smallint terminated;
  111. struct termios term_orig, term_less;
  112. };
  113. #define G (*ptr_to_globals)
  114. #define cur_fline (G.cur_fline )
  115. #define kbd_fd (G.kbd_fd )
  116. #define less_gets_pos (G.less_gets_pos )
  117. #define linepos (G.linepos )
  118. #define max_displayed_line (G.max_displayed_line)
  119. #define max_fline (G.max_fline )
  120. #define max_lineno (G.max_lineno )
  121. #define width (G.width )
  122. #define eof_error (G.eof_error )
  123. #define readpos (G.readpos )
  124. #define readeof (G.readeof )
  125. #define buffer (G.buffer )
  126. #define flines (G.flines )
  127. #define empty_line_marker (G.empty_line_marker )
  128. #define num_files (G.num_files )
  129. #define current_file (G.current_file )
  130. #define filename (G.filename )
  131. #define files (G.files )
  132. #define num_marks (G.num_marks )
  133. #define mark_lines (G.mark_lines )
  134. #if ENABLE_FEATURE_LESS_REGEXP
  135. #define match_lines (G.match_lines )
  136. #define match_pos (G.match_pos )
  137. #define num_matches (G.num_matches )
  138. #define wanted_match (G.wanted_match )
  139. #define pattern (G.pattern )
  140. #define pattern_valid (G.pattern_valid )
  141. #endif
  142. #define terminated (G.terminated )
  143. #define term_orig (G.term_orig )
  144. #define term_less (G.term_less )
  145. #define INIT_G() do { \
  146. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  147. less_gets_pos = -1; \
  148. empty_line_marker = "~"; \
  149. num_files = 1; \
  150. current_file = 1; \
  151. eof_error = 1; \
  152. terminated = 1; \
  153. USE_FEATURE_LESS_REGEXP(wanted_match = -1;) \
  154. } while (0)
  155. /* Reset terminal input to normal */
  156. static void set_tty_cooked(void)
  157. {
  158. fflush(stdout);
  159. tcsetattr(kbd_fd, TCSANOW, &term_orig);
  160. }
  161. /* Move the cursor to a position (x,y), where (0,0) is the
  162. top-left corner of the console */
  163. static void move_cursor(int line, int row)
  164. {
  165. printf("\033[%u;%uH", line, row);
  166. }
  167. static void clear_line(void)
  168. {
  169. printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
  170. }
  171. static void print_hilite(const char *str)
  172. {
  173. printf(HIGHLIGHT"%s"NORMAL, str);
  174. }
  175. static void print_statusline(const char *str)
  176. {
  177. clear_line();
  178. printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
  179. }
  180. /* Exit the program gracefully */
  181. static void less_exit(int code)
  182. {
  183. set_tty_cooked();
  184. clear_line();
  185. if (code < 0)
  186. kill_myself_with_sig(- code); /* does not return */
  187. exit(code);
  188. }
  189. #if ENABLE_FEATURE_LESS_REGEXP
  190. static void fill_match_lines(unsigned pos);
  191. #else
  192. #define fill_match_lines(pos) ((void)0)
  193. #endif
  194. /* Devilishly complex routine.
  195. *
  196. * Has to deal with EOF and EPIPE on input,
  197. * with line wrapping, with last line not ending in '\n'
  198. * (possibly not ending YET!), with backspace and tabs.
  199. * It reads input again if last time we got an EOF (thus supporting
  200. * growing files) or EPIPE (watching output of slow process like make).
  201. *
  202. * Variables used:
  203. * flines[] - array of lines already read. Linewrap may cause
  204. * one source file line to occupy several flines[n].
  205. * flines[max_fline] - last line, possibly incomplete.
  206. * terminated - 1 if flines[max_fline] is 'terminated'
  207. * (if there was '\n' [which isn't stored itself, we just remember
  208. * that it was seen])
  209. * max_lineno - last line's number, this one doesn't increment
  210. * on line wrap, only on "real" new lines.
  211. * readbuf[0..readeof-1] - small preliminary buffer.
  212. * readbuf[readpos] - next character to add to current line.
  213. * linepos - screen line position of next char to be read
  214. * (takes into account tabs and backspaces)
  215. * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
  216. */
  217. static void read_lines(void)
  218. {
  219. #define readbuf bb_common_bufsiz1
  220. char *current_line, *p;
  221. int w = width;
  222. char last_terminated = terminated;
  223. #if ENABLE_FEATURE_LESS_REGEXP
  224. unsigned old_max_fline = max_fline;
  225. time_t last_time = 0;
  226. unsigned seconds_p1 = 3; /* seconds_to_loop + 1 */
  227. #endif
  228. if (option_mask32 & FLAG_N)
  229. w -= 8;
  230. USE_FEATURE_LESS_REGEXP(again0:)
  231. p = current_line = xmalloc(w);
  232. max_fline += last_terminated;
  233. if (!last_terminated) {
  234. const char *cp = flines[max_fline];
  235. if (option_mask32 & FLAG_N)
  236. cp += 8;
  237. strcpy(current_line, cp);
  238. p += strlen(current_line);
  239. free((char*)flines[max_fline]);
  240. /* linepos is still valid from previous read_lines() */
  241. } else {
  242. linepos = 0;
  243. }
  244. while (1) { /* read lines until we reach cur_fline or wanted_match */
  245. *p = '\0';
  246. terminated = 0;
  247. while (1) { /* read chars until we have a line */
  248. char c;
  249. /* if no unprocessed chars left, eat more */
  250. if (readpos >= readeof) {
  251. ndelay_on(0);
  252. eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
  253. ndelay_off(0);
  254. readpos = 0;
  255. readeof = eof_error;
  256. if (eof_error <= 0)
  257. goto reached_eof;
  258. }
  259. c = readbuf[readpos];
  260. /* backspace? [needed for manpages] */
  261. /* <tab><bs> is (a) insane and */
  262. /* (b) harder to do correctly, so we refuse to do it */
  263. if (c == '\x8' && linepos && p[-1] != '\t') {
  264. readpos++; /* eat it */
  265. linepos--;
  266. /* was buggy (p could end up <= current_line)... */
  267. *--p = '\0';
  268. continue;
  269. }
  270. {
  271. size_t new_linepos = linepos + 1;
  272. if (c == '\t') {
  273. new_linepos += 7;
  274. new_linepos &= (~7);
  275. }
  276. if ((int)new_linepos >= w)
  277. break;
  278. linepos = new_linepos;
  279. }
  280. /* ok, we will eat this char */
  281. readpos++;
  282. if (c == '\n') {
  283. terminated = 1;
  284. linepos = 0;
  285. break;
  286. }
  287. /* NUL is substituted by '\n'! */
  288. if (c == '\0') c = '\n';
  289. *p++ = c;
  290. *p = '\0';
  291. } /* end of "read chars until we have a line" loop */
  292. /* Corner case: linewrap with only "" wrapping to next line */
  293. /* Looks ugly on screen, so we do not store this empty line */
  294. if (!last_terminated && !current_line[0]) {
  295. last_terminated = 1;
  296. max_lineno++;
  297. continue;
  298. }
  299. reached_eof:
  300. last_terminated = terminated;
  301. flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
  302. if (option_mask32 & FLAG_N) {
  303. /* Width of 7 preserves tab spacing in the text */
  304. flines[max_fline] = xasprintf(
  305. (max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
  306. max_lineno % 10000000, current_line);
  307. free(current_line);
  308. if (terminated)
  309. max_lineno++;
  310. } else {
  311. flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
  312. }
  313. if (max_fline >= MAXLINES) {
  314. eof_error = 0; /* Pretend we saw EOF */
  315. break;
  316. }
  317. if (max_fline > cur_fline + max_displayed_line) {
  318. #if !ENABLE_FEATURE_LESS_REGEXP
  319. break;
  320. #else
  321. if (wanted_match >= num_matches) { /* goto_match called us */
  322. fill_match_lines(old_max_fline);
  323. old_max_fline = max_fline;
  324. }
  325. if (wanted_match < num_matches)
  326. break;
  327. #endif
  328. }
  329. if (eof_error <= 0) {
  330. if (eof_error < 0) {
  331. if (errno == EAGAIN) {
  332. /* not yet eof or error, reset flag (or else
  333. * we will hog CPU - select() will return
  334. * immediately */
  335. eof_error = 1;
  336. } else {
  337. print_statusline("read error");
  338. }
  339. }
  340. #if !ENABLE_FEATURE_LESS_REGEXP
  341. break;
  342. #else
  343. if (wanted_match < num_matches) {
  344. break;
  345. } else { /* goto_match called us */
  346. time_t t = time(NULL);
  347. if (t != last_time) {
  348. last_time = t;
  349. if (--seconds_p1 == 0)
  350. break;
  351. }
  352. sched_yield();
  353. goto again0; /* go loop again (max 2 seconds) */
  354. }
  355. #endif
  356. }
  357. max_fline++;
  358. current_line = xmalloc(w);
  359. p = current_line;
  360. linepos = 0;
  361. } /* end of "read lines until we reach cur_fline" loop */
  362. fill_match_lines(old_max_fline);
  363. #if ENABLE_FEATURE_LESS_REGEXP
  364. /* prevent us from being stuck in search for a match */
  365. wanted_match = -1;
  366. #endif
  367. #undef readbuf
  368. }
  369. #if ENABLE_FEATURE_LESS_FLAGS
  370. /* Interestingly, writing calc_percent as a function saves around 32 bytes
  371. * on my build. */
  372. static int calc_percent(void)
  373. {
  374. unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
  375. return p <= 100 ? p : 100;
  376. }
  377. /* Print a status line if -M was specified */
  378. static void m_status_print(void)
  379. {
  380. int percentage;
  381. if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
  382. return;
  383. clear_line();
  384. printf(HIGHLIGHT"%s", filename);
  385. if (num_files > 1)
  386. printf(" (file %i of %i)", current_file, num_files);
  387. printf(" lines %i-%i/%i ",
  388. cur_fline + 1, cur_fline + max_displayed_line + 1,
  389. max_fline + 1);
  390. if (cur_fline >= (int)(max_fline - max_displayed_line)) {
  391. printf("(END)"NORMAL);
  392. if (num_files > 1 && current_file != num_files)
  393. printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
  394. return;
  395. }
  396. percentage = calc_percent();
  397. printf("%i%%"NORMAL, percentage);
  398. }
  399. #endif
  400. /* Print the status line */
  401. static void status_print(void)
  402. {
  403. const char *p;
  404. if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
  405. return;
  406. /* Change the status if flags have been set */
  407. #if ENABLE_FEATURE_LESS_FLAGS
  408. if (option_mask32 & (FLAG_M|FLAG_m)) {
  409. m_status_print();
  410. return;
  411. }
  412. /* No flags set */
  413. #endif
  414. clear_line();
  415. if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
  416. bb_putchar(':');
  417. return;
  418. }
  419. p = "(END)";
  420. if (!cur_fline)
  421. p = filename;
  422. if (num_files > 1) {
  423. printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
  424. p, current_file, num_files);
  425. return;
  426. }
  427. print_hilite(p);
  428. }
  429. static void cap_cur_fline(int nlines)
  430. {
  431. int diff;
  432. if (cur_fline < 0)
  433. cur_fline = 0;
  434. if (cur_fline + max_displayed_line > max_fline + TILDES) {
  435. cur_fline -= nlines;
  436. if (cur_fline < 0)
  437. cur_fline = 0;
  438. diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
  439. /* As the number of lines requested was too large, we just move
  440. to the end of the file */
  441. if (diff > 0)
  442. cur_fline += diff;
  443. }
  444. }
  445. static const char controls[] ALIGN1 =
  446. /* NUL: never encountered; TAB: not converted */
  447. /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
  448. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  449. "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
  450. static const char ctrlconv[] ALIGN1 =
  451. /* '\n': it's a former NUL - subst with '@', not 'J' */
  452. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
  453. "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
  454. #if ENABLE_FEATURE_LESS_REGEXP
  455. static void print_found(const char *line)
  456. {
  457. int match_status;
  458. int eflags;
  459. char *growline;
  460. regmatch_t match_structs;
  461. char buf[width];
  462. const char *str = line;
  463. char *p = buf;
  464. size_t n;
  465. while (*str) {
  466. n = strcspn(str, controls);
  467. if (n) {
  468. if (!str[n]) break;
  469. memcpy(p, str, n);
  470. p += n;
  471. str += n;
  472. }
  473. n = strspn(str, controls);
  474. memset(p, '.', n);
  475. p += n;
  476. str += n;
  477. }
  478. strcpy(p, str);
  479. /* buf[] holds quarantined version of str */
  480. /* Each part of the line that matches has the HIGHLIGHT
  481. and NORMAL escape sequences placed around it.
  482. NB: we regex against line, but insert text
  483. from quarantined copy (buf[]) */
  484. str = buf;
  485. growline = NULL;
  486. eflags = 0;
  487. goto start;
  488. while (match_status == 0) {
  489. char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
  490. growline ? : "",
  491. match_structs.rm_so, str,
  492. match_structs.rm_eo - match_structs.rm_so,
  493. str + match_structs.rm_so);
  494. free(growline); growline = new;
  495. str += match_structs.rm_eo;
  496. line += match_structs.rm_eo;
  497. eflags = REG_NOTBOL;
  498. start:
  499. /* Most of the time doesn't find the regex, optimize for that */
  500. match_status = regexec(&pattern, line, 1, &match_structs, eflags);
  501. /* if even "" matches, treat it as "not a match" */
  502. if (match_structs.rm_so >= match_structs.rm_eo)
  503. match_status = 1;
  504. }
  505. if (!growline) {
  506. printf(CLEAR_2_EOL"%s\n", str);
  507. return;
  508. }
  509. printf(CLEAR_2_EOL"%s%s\n", growline, str);
  510. free(growline);
  511. }
  512. #else
  513. void print_found(const char *line);
  514. #endif
  515. static void print_ascii(const char *str)
  516. {
  517. char buf[width];
  518. char *p;
  519. size_t n;
  520. printf(CLEAR_2_EOL);
  521. while (*str) {
  522. n = strcspn(str, controls);
  523. if (n) {
  524. if (!str[n]) break;
  525. printf("%.*s", (int) n, str);
  526. str += n;
  527. }
  528. n = strspn(str, controls);
  529. p = buf;
  530. do {
  531. if (*str == 0x7f)
  532. *p++ = '?';
  533. else if (*str == (char)0x9b)
  534. /* VT100's CSI, aka Meta-ESC. Who's inventor? */
  535. /* I want to know who committed this sin */
  536. *p++ = '{';
  537. else
  538. *p++ = ctrlconv[(unsigned char)*str];
  539. str++;
  540. } while (--n);
  541. *p = '\0';
  542. print_hilite(buf);
  543. }
  544. puts(str);
  545. }
  546. /* Print the buffer */
  547. static void buffer_print(void)
  548. {
  549. unsigned i;
  550. move_cursor(0, 0);
  551. for (i = 0; i <= max_displayed_line; i++)
  552. if (pattern_valid)
  553. print_found(buffer[i]);
  554. else
  555. print_ascii(buffer[i]);
  556. status_print();
  557. }
  558. static void buffer_fill_and_print(void)
  559. {
  560. unsigned i;
  561. for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
  562. buffer[i] = flines[cur_fline + i];
  563. }
  564. for (; i <= max_displayed_line; i++) {
  565. buffer[i] = empty_line_marker;
  566. }
  567. buffer_print();
  568. }
  569. /* Move the buffer up and down in the file in order to scroll */
  570. static void buffer_down(int nlines)
  571. {
  572. cur_fline += nlines;
  573. read_lines();
  574. cap_cur_fline(nlines);
  575. buffer_fill_and_print();
  576. }
  577. static void buffer_up(int nlines)
  578. {
  579. cur_fline -= nlines;
  580. if (cur_fline < 0) cur_fline = 0;
  581. read_lines();
  582. buffer_fill_and_print();
  583. }
  584. static void buffer_line(int linenum)
  585. {
  586. if (linenum < 0)
  587. linenum = 0;
  588. cur_fline = linenum;
  589. read_lines();
  590. if (linenum + max_displayed_line > max_fline)
  591. linenum = max_fline - max_displayed_line + TILDES;
  592. if (linenum < 0)
  593. linenum = 0;
  594. cur_fline = linenum;
  595. buffer_fill_and_print();
  596. }
  597. static void open_file_and_read_lines(void)
  598. {
  599. if (filename) {
  600. int fd = xopen(filename, O_RDONLY);
  601. dup2(fd, 0);
  602. if (fd) close(fd);
  603. } else {
  604. /* "less" with no arguments in argv[] */
  605. /* For status line only */
  606. filename = xstrdup(bb_msg_standard_input);
  607. }
  608. readpos = 0;
  609. readeof = 0;
  610. linepos = 0;
  611. terminated = 1;
  612. read_lines();
  613. }
  614. /* Reinitialize everything for a new file - free the memory and start over */
  615. static void reinitialize(void)
  616. {
  617. unsigned i;
  618. if (flines) {
  619. for (i = 0; i <= max_fline; i++)
  620. free((void*)(flines[i]));
  621. free(flines);
  622. flines = NULL;
  623. }
  624. max_fline = -1;
  625. cur_fline = 0;
  626. max_lineno = 0;
  627. open_file_and_read_lines();
  628. buffer_fill_and_print();
  629. }
  630. static ssize_t getch_nowait(char* input, int sz)
  631. {
  632. ssize_t rd;
  633. struct pollfd pfd[2];
  634. pfd[0].fd = STDIN_FILENO;
  635. pfd[0].events = POLLIN;
  636. pfd[1].fd = kbd_fd;
  637. pfd[1].events = POLLIN;
  638. again:
  639. tcsetattr(kbd_fd, TCSANOW, &term_less);
  640. /* NB: select/poll returns whenever read will not block. Therefore:
  641. * if eof is reached, select/poll will return immediately
  642. * because read will immediately return 0 bytes.
  643. * Even if select/poll says that input is available, read CAN block
  644. * (switch fd into O_NONBLOCK'ed mode to avoid it)
  645. */
  646. rd = 1;
  647. if (max_fline <= cur_fline + max_displayed_line
  648. && eof_error > 0 /* did NOT reach eof yet */
  649. ) {
  650. /* We are interested in stdin */
  651. rd = 0;
  652. }
  653. /* position cursor if line input is done */
  654. if (less_gets_pos >= 0)
  655. move_cursor(max_displayed_line + 2, less_gets_pos + 1);
  656. fflush(stdout);
  657. safe_poll(pfd + rd, 2 - rd, -1);
  658. input[0] = '\0';
  659. rd = safe_read(kbd_fd, input, sz); /* NB: kbd_fd is in O_NONBLOCK mode */
  660. if (rd < 0 && errno == EAGAIN) {
  661. /* No keyboard input -> we have input on stdin! */
  662. read_lines();
  663. buffer_fill_and_print();
  664. goto again;
  665. }
  666. set_tty_cooked();
  667. return rd;
  668. }
  669. /* Grab a character from input without requiring the return key. If the
  670. * character is ASCII \033, get more characters and assign certain sequences
  671. * special return codes. Note that this function works best with raw input. */
  672. static int less_getch(int pos)
  673. {
  674. unsigned char input[16];
  675. unsigned i;
  676. again:
  677. less_gets_pos = pos;
  678. memset(input, 0, sizeof(input));
  679. getch_nowait((char *)input, sizeof(input));
  680. less_gets_pos = -1;
  681. /* Detect escape sequences (i.e. arrow keys) and handle
  682. * them accordingly */
  683. if (input[0] == '\033' && input[1] == '[') {
  684. i = input[2] - REAL_KEY_UP;
  685. if (i < 4)
  686. return 20 + i;
  687. i = input[2] - REAL_PAGE_UP;
  688. if (i < 4)
  689. return 24 + i;
  690. if (input[2] == REAL_KEY_HOME_XTERM)
  691. return KEY_HOME;
  692. if (input[2] == REAL_KEY_HOME_ALT)
  693. return KEY_HOME;
  694. if (input[2] == REAL_KEY_END_XTERM)
  695. return KEY_END;
  696. if (input[2] == REAL_KEY_END_ALT)
  697. return KEY_END;
  698. return 0;
  699. }
  700. /* Reject almost all control chars */
  701. i = input[0];
  702. if (i < ' ' && i != 0x0d && i != 8)
  703. goto again;
  704. return i;
  705. }
  706. static char* less_gets(int sz)
  707. {
  708. char c;
  709. unsigned i = 0;
  710. char *result = xzalloc(1);
  711. while (1) {
  712. c = '\0';
  713. less_gets_pos = sz + i;
  714. getch_nowait(&c, 1);
  715. if (c == 0x0d) {
  716. result[i] = '\0';
  717. less_gets_pos = -1;
  718. return result;
  719. }
  720. if (c == 0x7f)
  721. c = 8;
  722. if (c == 8 && i) {
  723. printf("\x8 \x8");
  724. i--;
  725. }
  726. if (c < ' ')
  727. continue;
  728. if (i >= width - sz - 1)
  729. continue; /* len limit */
  730. bb_putchar(c);
  731. result[i++] = c;
  732. result = xrealloc(result, i+1);
  733. }
  734. }
  735. static void examine_file(void)
  736. {
  737. char *new_fname;
  738. print_statusline("Examine: ");
  739. new_fname = less_gets(sizeof("Examine: ") - 1);
  740. if (!new_fname[0]) {
  741. status_print();
  742. err:
  743. free(new_fname);
  744. return;
  745. }
  746. if (access(new_fname, R_OK) != 0) {
  747. print_statusline("Cannot read this file");
  748. goto err;
  749. }
  750. free(filename);
  751. filename = new_fname;
  752. /* files start by = argv. why we assume that argv is infinitely long??
  753. files[num_files] = filename;
  754. current_file = num_files + 1;
  755. num_files++; */
  756. files[0] = filename;
  757. num_files = current_file = 1;
  758. reinitialize();
  759. }
  760. /* This function changes the file currently being paged. direction can be one of the following:
  761. * -1: go back one file
  762. * 0: go to the first file
  763. * 1: go forward one file */
  764. static void change_file(int direction)
  765. {
  766. if (current_file != ((direction > 0) ? num_files : 1)) {
  767. current_file = direction ? current_file + direction : 1;
  768. free(filename);
  769. filename = xstrdup(files[current_file - 1]);
  770. reinitialize();
  771. } else {
  772. print_statusline(direction > 0 ? "No next file" : "No previous file");
  773. }
  774. }
  775. static void remove_current_file(void)
  776. {
  777. unsigned i;
  778. if (num_files < 2)
  779. return;
  780. if (current_file != 1) {
  781. change_file(-1);
  782. for (i = 3; i <= num_files; i++)
  783. files[i - 2] = files[i - 1];
  784. num_files--;
  785. } else {
  786. change_file(1);
  787. for (i = 2; i <= num_files; i++)
  788. files[i - 2] = files[i - 1];
  789. num_files--;
  790. current_file--;
  791. }
  792. }
  793. static void colon_process(void)
  794. {
  795. int keypress;
  796. /* Clear the current line and print a prompt */
  797. print_statusline(" :");
  798. keypress = less_getch(2);
  799. switch (keypress) {
  800. case 'd':
  801. remove_current_file();
  802. break;
  803. case 'e':
  804. examine_file();
  805. break;
  806. #if ENABLE_FEATURE_LESS_FLAGS
  807. case 'f':
  808. m_status_print();
  809. break;
  810. #endif
  811. case 'n':
  812. change_file(1);
  813. break;
  814. case 'p':
  815. change_file(-1);
  816. break;
  817. case 'q':
  818. less_exit(EXIT_SUCCESS);
  819. break;
  820. case 'x':
  821. change_file(0);
  822. break;
  823. }
  824. }
  825. #if ENABLE_FEATURE_LESS_REGEXP
  826. static void normalize_match_pos(int match)
  827. {
  828. if (match >= num_matches)
  829. match = num_matches - 1;
  830. if (match < 0)
  831. match = 0;
  832. match_pos = match;
  833. }
  834. static void goto_match(int match)
  835. {
  836. if (!pattern_valid)
  837. return;
  838. if (match < 0)
  839. match = 0;
  840. /* Try to find next match if eof isn't reached yet */
  841. if (match >= num_matches && eof_error > 0) {
  842. wanted_match = match; /* "I want to read until I see N'th match" */
  843. read_lines();
  844. }
  845. if (num_matches) {
  846. normalize_match_pos(match);
  847. buffer_line(match_lines[match_pos]);
  848. } else {
  849. print_statusline("No matches found");
  850. }
  851. }
  852. static void fill_match_lines(unsigned pos)
  853. {
  854. if (!pattern_valid)
  855. return;
  856. /* Run the regex on each line of the current file */
  857. while (pos <= max_fline) {
  858. /* If this line matches */
  859. if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
  860. /* and we didn't match it last time */
  861. && !(num_matches && match_lines[num_matches-1] == pos)
  862. ) {
  863. match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int));
  864. match_lines[num_matches++] = pos;
  865. }
  866. pos++;
  867. }
  868. }
  869. static void regex_process(void)
  870. {
  871. char *uncomp_regex, *err;
  872. /* Reset variables */
  873. free(match_lines);
  874. match_lines = NULL;
  875. match_pos = 0;
  876. num_matches = 0;
  877. if (pattern_valid) {
  878. regfree(&pattern);
  879. pattern_valid = 0;
  880. }
  881. /* Get the uncompiled regular expression from the user */
  882. clear_line();
  883. bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
  884. uncomp_regex = less_gets(1);
  885. if (!uncomp_regex[0]) {
  886. free(uncomp_regex);
  887. buffer_print();
  888. return;
  889. }
  890. /* Compile the regex and check for errors */
  891. err = regcomp_or_errmsg(&pattern, uncomp_regex, 0);
  892. free(uncomp_regex);
  893. if (err) {
  894. print_statusline(err);
  895. free(err);
  896. return;
  897. }
  898. pattern_valid = 1;
  899. match_pos = 0;
  900. fill_match_lines(0);
  901. while (match_pos < num_matches) {
  902. if ((int)match_lines[match_pos] > cur_fline)
  903. break;
  904. match_pos++;
  905. }
  906. if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
  907. match_pos--;
  908. /* It's possible that no matches are found yet.
  909. * goto_match() will read input looking for match,
  910. * if needed */
  911. goto_match(match_pos);
  912. }
  913. #endif
  914. static void number_process(int first_digit)
  915. {
  916. unsigned i;
  917. int num;
  918. char num_input[sizeof(int)*4]; /* more than enough */
  919. char keypress;
  920. num_input[0] = first_digit;
  921. /* Clear the current line, print a prompt, and then print the digit */
  922. clear_line();
  923. printf(":%c", first_digit);
  924. /* Receive input until a letter is given */
  925. i = 1;
  926. while (i < sizeof(num_input)-1) {
  927. num_input[i] = less_getch(i + 1);
  928. if (!num_input[i] || !isdigit(num_input[i]))
  929. break;
  930. bb_putchar(num_input[i]);
  931. i++;
  932. }
  933. /* Take the final letter out of the digits string */
  934. keypress = num_input[i];
  935. num_input[i] = '\0';
  936. num = bb_strtou(num_input, NULL, 10);
  937. /* on format error, num == -1 */
  938. if (num < 1 || num > MAXLINES) {
  939. buffer_print();
  940. return;
  941. }
  942. /* We now know the number and the letter entered, so we process them */
  943. switch (keypress) {
  944. case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
  945. buffer_down(num);
  946. break;
  947. case KEY_UP: case 'b': case 'w': case 'y': case 'u':
  948. buffer_up(num);
  949. break;
  950. case 'g': case '<': case 'G': case '>':
  951. cur_fline = num + max_displayed_line;
  952. read_lines();
  953. buffer_line(num - 1);
  954. break;
  955. case 'p': case '%':
  956. num = num * (max_fline / 100); /* + max_fline / 2; */
  957. cur_fline = num + max_displayed_line;
  958. read_lines();
  959. buffer_line(num);
  960. break;
  961. #if ENABLE_FEATURE_LESS_REGEXP
  962. case 'n':
  963. goto_match(match_pos + num);
  964. break;
  965. case '/':
  966. option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
  967. regex_process();
  968. break;
  969. case '?':
  970. option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
  971. regex_process();
  972. break;
  973. #endif
  974. }
  975. }
  976. #if ENABLE_FEATURE_LESS_FLAGCS
  977. static void flag_change(void)
  978. {
  979. int keypress;
  980. clear_line();
  981. bb_putchar('-');
  982. keypress = less_getch(1);
  983. switch (keypress) {
  984. case 'M':
  985. option_mask32 ^= FLAG_M;
  986. break;
  987. case 'm':
  988. option_mask32 ^= FLAG_m;
  989. break;
  990. case 'E':
  991. option_mask32 ^= FLAG_E;
  992. break;
  993. case '~':
  994. option_mask32 ^= FLAG_TILDE;
  995. break;
  996. }
  997. }
  998. static void show_flag_status(void)
  999. {
  1000. int keypress;
  1001. int flag_val;
  1002. clear_line();
  1003. bb_putchar('_');
  1004. keypress = less_getch(1);
  1005. switch (keypress) {
  1006. case 'M':
  1007. flag_val = option_mask32 & FLAG_M;
  1008. break;
  1009. case 'm':
  1010. flag_val = option_mask32 & FLAG_m;
  1011. break;
  1012. case '~':
  1013. flag_val = option_mask32 & FLAG_TILDE;
  1014. break;
  1015. case 'N':
  1016. flag_val = option_mask32 & FLAG_N;
  1017. break;
  1018. case 'E':
  1019. flag_val = option_mask32 & FLAG_E;
  1020. break;
  1021. default:
  1022. flag_val = 0;
  1023. break;
  1024. }
  1025. clear_line();
  1026. printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
  1027. }
  1028. #endif
  1029. static void save_input_to_file(void)
  1030. {
  1031. const char *msg = "";
  1032. char *current_line;
  1033. unsigned i;
  1034. FILE *fp;
  1035. print_statusline("Log file: ");
  1036. current_line = less_gets(sizeof("Log file: ")-1);
  1037. if (current_line[0]) {
  1038. fp = fopen(current_line, "w");
  1039. if (!fp) {
  1040. msg = "Error opening log file";
  1041. goto ret;
  1042. }
  1043. for (i = 0; i <= max_fline; i++)
  1044. fprintf(fp, "%s\n", flines[i]);
  1045. fclose(fp);
  1046. msg = "Done";
  1047. }
  1048. ret:
  1049. print_statusline(msg);
  1050. free(current_line);
  1051. }
  1052. #if ENABLE_FEATURE_LESS_MARKS
  1053. static void add_mark(void)
  1054. {
  1055. int letter;
  1056. print_statusline("Mark: ");
  1057. letter = less_getch(sizeof("Mark: ") - 1);
  1058. if (isalpha(letter)) {
  1059. /* If we exceed 15 marks, start overwriting previous ones */
  1060. if (num_marks == 14)
  1061. num_marks = 0;
  1062. mark_lines[num_marks][0] = letter;
  1063. mark_lines[num_marks][1] = cur_fline;
  1064. num_marks++;
  1065. } else {
  1066. print_statusline("Invalid mark letter");
  1067. }
  1068. }
  1069. static void goto_mark(void)
  1070. {
  1071. int letter;
  1072. int i;
  1073. print_statusline("Go to mark: ");
  1074. letter = less_getch(sizeof("Go to mark: ") - 1);
  1075. clear_line();
  1076. if (isalpha(letter)) {
  1077. for (i = 0; i <= num_marks; i++)
  1078. if (letter == mark_lines[i][0]) {
  1079. buffer_line(mark_lines[i][1]);
  1080. break;
  1081. }
  1082. if (num_marks == 14 && letter != mark_lines[14][0])
  1083. print_statusline("Mark not set");
  1084. } else
  1085. print_statusline("Invalid mark letter");
  1086. }
  1087. #endif
  1088. #if ENABLE_FEATURE_LESS_BRACKETS
  1089. static char opp_bracket(char bracket)
  1090. {
  1091. switch (bracket) {
  1092. case '{': case '[': /* '}' == '{' + 2. Same for '[' */
  1093. bracket++;
  1094. case '(': /* ')' == '(' + 1 */
  1095. bracket++;
  1096. break;
  1097. case '}': case ']':
  1098. bracket--;
  1099. case ')':
  1100. bracket--;
  1101. break;
  1102. };
  1103. return bracket;
  1104. }
  1105. static void match_right_bracket(char bracket)
  1106. {
  1107. unsigned i;
  1108. if (strchr(flines[cur_fline], bracket) == NULL) {
  1109. print_statusline("No bracket in top line");
  1110. return;
  1111. }
  1112. bracket = opp_bracket(bracket);
  1113. for (i = cur_fline + 1; i < max_fline; i++) {
  1114. if (strchr(flines[i], bracket) != NULL) {
  1115. buffer_line(i);
  1116. return;
  1117. }
  1118. }
  1119. print_statusline("No matching bracket found");
  1120. }
  1121. static void match_left_bracket(char bracket)
  1122. {
  1123. int i;
  1124. if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
  1125. print_statusline("No bracket in bottom line");
  1126. return;
  1127. }
  1128. bracket = opp_bracket(bracket);
  1129. for (i = cur_fline + max_displayed_line; i >= 0; i--) {
  1130. if (strchr(flines[i], bracket) != NULL) {
  1131. buffer_line(i);
  1132. return;
  1133. }
  1134. }
  1135. print_statusline("No matching bracket found");
  1136. }
  1137. #endif /* FEATURE_LESS_BRACKETS */
  1138. static void keypress_process(int keypress)
  1139. {
  1140. switch (keypress) {
  1141. case KEY_DOWN: case 'e': case 'j': case 0x0d:
  1142. buffer_down(1);
  1143. break;
  1144. case KEY_UP: case 'y': case 'k':
  1145. buffer_up(1);
  1146. break;
  1147. case PAGE_DOWN: case ' ': case 'z': case 'f':
  1148. buffer_down(max_displayed_line + 1);
  1149. break;
  1150. case PAGE_UP: case 'w': case 'b':
  1151. buffer_up(max_displayed_line + 1);
  1152. break;
  1153. case 'd':
  1154. buffer_down((max_displayed_line + 1) / 2);
  1155. break;
  1156. case 'u':
  1157. buffer_up((max_displayed_line + 1) / 2);
  1158. break;
  1159. case KEY_HOME: case 'g': case 'p': case '<': case '%':
  1160. buffer_line(0);
  1161. break;
  1162. case KEY_END: case 'G': case '>':
  1163. cur_fline = MAXLINES;
  1164. read_lines();
  1165. buffer_line(cur_fline);
  1166. break;
  1167. case 'q': case 'Q':
  1168. less_exit(EXIT_SUCCESS);
  1169. break;
  1170. #if ENABLE_FEATURE_LESS_MARKS
  1171. case 'm':
  1172. add_mark();
  1173. buffer_print();
  1174. break;
  1175. case '\'':
  1176. goto_mark();
  1177. buffer_print();
  1178. break;
  1179. #endif
  1180. case 'r': case 'R':
  1181. buffer_print();
  1182. break;
  1183. /*case 'R':
  1184. full_repaint();
  1185. break;*/
  1186. case 's':
  1187. save_input_to_file();
  1188. break;
  1189. case 'E':
  1190. examine_file();
  1191. break;
  1192. #if ENABLE_FEATURE_LESS_FLAGS
  1193. case '=':
  1194. m_status_print();
  1195. break;
  1196. #endif
  1197. #if ENABLE_FEATURE_LESS_REGEXP
  1198. case '/':
  1199. option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
  1200. regex_process();
  1201. break;
  1202. case 'n':
  1203. goto_match(match_pos + 1);
  1204. break;
  1205. case 'N':
  1206. goto_match(match_pos - 1);
  1207. break;
  1208. case '?':
  1209. option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
  1210. regex_process();
  1211. break;
  1212. #endif
  1213. #if ENABLE_FEATURE_LESS_FLAGCS
  1214. case '-':
  1215. flag_change();
  1216. buffer_print();
  1217. break;
  1218. case '_':
  1219. show_flag_status();
  1220. break;
  1221. #endif
  1222. #if ENABLE_FEATURE_LESS_BRACKETS
  1223. case '{': case '(': case '[':
  1224. match_right_bracket(keypress);
  1225. break;
  1226. case '}': case ')': case ']':
  1227. match_left_bracket(keypress);
  1228. break;
  1229. #endif
  1230. case ':':
  1231. colon_process();
  1232. break;
  1233. }
  1234. if (isdigit(keypress))
  1235. number_process(keypress);
  1236. }
  1237. static void sig_catcher(int sig)
  1238. {
  1239. less_exit(- sig);
  1240. }
  1241. int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1242. int less_main(int argc, char **argv)
  1243. {
  1244. int keypress;
  1245. INIT_G();
  1246. /* TODO: -x: do not interpret backspace, -xx: tab also */
  1247. /* -xxx: newline also */
  1248. /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
  1249. getopt32(argv, "EMmN~");
  1250. argc -= optind;
  1251. argv += optind;
  1252. num_files = argc;
  1253. files = argv;
  1254. /* Another popular pager, most, detects when stdout
  1255. * is not a tty and turns into cat. This makes sense. */
  1256. if (!isatty(STDOUT_FILENO))
  1257. return bb_cat(argv);
  1258. kbd_fd = open(CURRENT_TTY, O_RDONLY);
  1259. if (kbd_fd < 0)
  1260. return bb_cat(argv);
  1261. ndelay_on(kbd_fd);
  1262. if (!num_files) {
  1263. if (isatty(STDIN_FILENO)) {
  1264. /* Just "less"? No args and no redirection? */
  1265. bb_error_msg("missing filename");
  1266. bb_show_usage();
  1267. }
  1268. } else
  1269. filename = xstrdup(files[0]);
  1270. get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
  1271. /* 20: two tabstops + 4 */
  1272. if (width < 20 || max_displayed_line < 3)
  1273. return bb_cat(argv);
  1274. max_displayed_line -= 2;
  1275. buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
  1276. if (option_mask32 & FLAG_TILDE)
  1277. empty_line_marker = "";
  1278. tcgetattr(kbd_fd, &term_orig);
  1279. term_less = term_orig;
  1280. term_less.c_lflag &= ~(ICANON | ECHO);
  1281. term_less.c_iflag &= ~(IXON | ICRNL);
  1282. /*term_less.c_oflag &= ~ONLCR;*/
  1283. term_less.c_cc[VMIN] = 1;
  1284. term_less.c_cc[VTIME] = 0;
  1285. /* We want to restore term_orig on exit */
  1286. bb_signals(BB_FATAL_SIGS, sig_catcher);
  1287. reinitialize();
  1288. while (1) {
  1289. keypress = less_getch(-1); /* -1: do not position cursor */
  1290. keypress_process(keypress);
  1291. }
  1292. }