less.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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 GPLv2 or later, see file LICENSE in this source tree.
  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. //config:config LESS
  23. //config: bool "less"
  24. //config: default y
  25. //config: help
  26. //config: 'less' is a pager, meaning that it displays text files. It possesses
  27. //config: a wide array of features, and is an improvement over 'more'.
  28. //config:
  29. //config:config FEATURE_LESS_MAXLINES
  30. //config: int "Max number of input lines less will try to eat"
  31. //config: default 9999999
  32. //config: depends on LESS
  33. //config:
  34. //config:config FEATURE_LESS_BRACKETS
  35. //config: bool "Enable bracket searching"
  36. //config: default y
  37. //config: depends on LESS
  38. //config: help
  39. //config: This option adds the capability to search for matching left and right
  40. //config: brackets, facilitating programming.
  41. //config:
  42. //config:config FEATURE_LESS_FLAGS
  43. //config: bool "Enable -m/-M"
  44. //config: default y
  45. //config: depends on LESS
  46. //config: help
  47. //config: The -M/-m flag enables a more sophisticated status line.
  48. //config:
  49. //config:config FEATURE_LESS_MARKS
  50. //config: bool "Enable marks"
  51. //config: default y
  52. //config: depends on LESS
  53. //config: help
  54. //config: Marks enable positions in a file to be stored for easy reference.
  55. //config:
  56. //config:config FEATURE_LESS_REGEXP
  57. //config: bool "Enable regular expressions"
  58. //config: default y
  59. //config: depends on LESS
  60. //config: help
  61. //config: Enable regular expressions, allowing complex file searches.
  62. //config:
  63. //config:config FEATURE_LESS_WINCH
  64. //config: bool "Enable automatic resizing on window size changes"
  65. //config: default y
  66. //config: depends on LESS
  67. //config: help
  68. //config: Makes less track window size changes.
  69. //config:
  70. //config:config FEATURE_LESS_ASK_TERMINAL
  71. //config: bool "Use 'tell me cursor position' ESC sequence to measure window"
  72. //config: default y
  73. //config: depends on FEATURE_LESS_WINCH
  74. //config: help
  75. //config: Makes less track window size changes.
  76. //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
  77. //config: this option makes less perform a last-ditch effort to find it:
  78. //config: position cursor to 999,999 and ask terminal to report real
  79. //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
  80. //config:
  81. //config: This is not clean but helps a lot on serial lines and such.
  82. //config:
  83. //config:config FEATURE_LESS_DASHCMD
  84. //config: bool "Enable flag changes ('-' command)"
  85. //config: default y
  86. //config: depends on LESS
  87. //config: help
  88. //config: This enables the ability to change command-line flags within
  89. //config: less itself ('-' keyboard command).
  90. //config:
  91. //config:config FEATURE_LESS_LINENUMS
  92. //config: bool "Enable dynamic switching of line numbers"
  93. //config: default y
  94. //config: depends on FEATURE_LESS_DASHCMD
  95. //config: help
  96. //config: Enables "-N" command.
  97. //usage:#define less_trivial_usage
  98. //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") "Nh~] [FILE]..."
  99. //usage:#define less_full_usage "\n\n"
  100. //usage: "View FILE (or stdin) one screenful at a time\n"
  101. //usage: "\n -E Quit once the end of a file is reached"
  102. //usage: IF_FEATURE_LESS_REGEXP(
  103. //usage: "\n -I Ignore case in all searches"
  104. //usage: )
  105. //usage: IF_FEATURE_LESS_FLAGS(
  106. //usage: "\n -M,-m Display status line with line numbers"
  107. //usage: "\n and percentage through the file"
  108. //usage: )
  109. //usage: "\n -N Prefix line number to each line"
  110. //usage: "\n -~ Suppress ~s displayed past EOF"
  111. #include <sched.h> /* sched_yield() */
  112. #include "libbb.h"
  113. #if ENABLE_FEATURE_LESS_REGEXP
  114. #include "xregex.h"
  115. #endif
  116. #define ESC "\033"
  117. /* The escape codes for highlighted and normal text */
  118. #define HIGHLIGHT ESC"[7m"
  119. #define NORMAL ESC"[0m"
  120. /* The escape code to home and clear to the end of screen */
  121. #define CLEAR ESC"[H\033[J"
  122. /* The escape code to clear to the end of line */
  123. #define CLEAR_2_EOL ESC"[K"
  124. enum {
  125. /* Absolute max of lines eaten */
  126. MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
  127. /* This many "after the end" lines we will show (at max) */
  128. TILDES = 1,
  129. };
  130. /* Command line options */
  131. enum {
  132. FLAG_E = 1 << 0,
  133. FLAG_M = 1 << 1,
  134. FLAG_m = 1 << 2,
  135. FLAG_N = 1 << 3,
  136. FLAG_TILDE = 1 << 4,
  137. FLAG_I = 1 << 5,
  138. FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_DASHCMD,
  139. /* hijack command line options variable for internal state vars */
  140. LESS_STATE_MATCH_BACKWARDS = 1 << 15,
  141. };
  142. #if !ENABLE_FEATURE_LESS_REGEXP
  143. enum { pattern_valid = 0 };
  144. #endif
  145. struct globals {
  146. int cur_fline; /* signed */
  147. int kbd_fd; /* fd to get input from */
  148. int less_gets_pos;
  149. /* last position in last line, taking into account tabs */
  150. size_t last_line_pos;
  151. unsigned max_fline;
  152. unsigned max_lineno; /* this one tracks linewrap */
  153. unsigned max_displayed_line;
  154. unsigned width;
  155. #if ENABLE_FEATURE_LESS_WINCH
  156. unsigned winch_counter;
  157. #endif
  158. ssize_t eof_error; /* eof if 0, error if < 0 */
  159. ssize_t readpos;
  160. ssize_t readeof; /* must be signed */
  161. const char **buffer;
  162. const char **flines;
  163. const char *empty_line_marker;
  164. unsigned num_files;
  165. unsigned current_file;
  166. char *filename;
  167. char **files;
  168. #if ENABLE_FEATURE_LESS_MARKS
  169. unsigned num_marks;
  170. unsigned mark_lines[15][2];
  171. #endif
  172. #if ENABLE_FEATURE_LESS_REGEXP
  173. unsigned *match_lines;
  174. int match_pos; /* signed! */
  175. int wanted_match; /* signed! */
  176. int num_matches;
  177. regex_t pattern;
  178. smallint pattern_valid;
  179. #endif
  180. #if ENABLE_FEATURE_LESS_ASK_TERMINAL
  181. smallint winsize_err;
  182. #endif
  183. smallint terminated;
  184. struct termios term_orig, term_less;
  185. char kbd_input[KEYCODE_BUFFER_SIZE];
  186. };
  187. #define G (*ptr_to_globals)
  188. #define cur_fline (G.cur_fline )
  189. #define kbd_fd (G.kbd_fd )
  190. #define less_gets_pos (G.less_gets_pos )
  191. #define last_line_pos (G.last_line_pos )
  192. #define max_fline (G.max_fline )
  193. #define max_lineno (G.max_lineno )
  194. #define max_displayed_line (G.max_displayed_line)
  195. #define width (G.width )
  196. #define winch_counter (G.winch_counter )
  197. /* This one is 100% not cached by compiler on read access */
  198. #define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
  199. #define eof_error (G.eof_error )
  200. #define readpos (G.readpos )
  201. #define readeof (G.readeof )
  202. #define buffer (G.buffer )
  203. #define flines (G.flines )
  204. #define empty_line_marker (G.empty_line_marker )
  205. #define num_files (G.num_files )
  206. #define current_file (G.current_file )
  207. #define filename (G.filename )
  208. #define files (G.files )
  209. #define num_marks (G.num_marks )
  210. #define mark_lines (G.mark_lines )
  211. #if ENABLE_FEATURE_LESS_REGEXP
  212. #define match_lines (G.match_lines )
  213. #define match_pos (G.match_pos )
  214. #define num_matches (G.num_matches )
  215. #define wanted_match (G.wanted_match )
  216. #define pattern (G.pattern )
  217. #define pattern_valid (G.pattern_valid )
  218. #endif
  219. #define terminated (G.terminated )
  220. #define term_orig (G.term_orig )
  221. #define term_less (G.term_less )
  222. #define kbd_input (G.kbd_input )
  223. #define INIT_G() do { \
  224. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  225. less_gets_pos = -1; \
  226. empty_line_marker = "~"; \
  227. num_files = 1; \
  228. current_file = 1; \
  229. eof_error = 1; \
  230. terminated = 1; \
  231. IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
  232. } while (0)
  233. /* flines[] are lines read from stdin, each in malloc'ed buffer.
  234. * Line numbers are stored as uint32_t prepended to each line.
  235. * Pointer is adjusted so that flines[i] points directly past
  236. * line number. Accesor: */
  237. #define MEMPTR(p) ((char*)(p) - 4)
  238. #define LINENO(p) (*(uint32_t*)((p) - 4))
  239. /* Reset terminal input to normal */
  240. static void set_tty_cooked(void)
  241. {
  242. fflush_all();
  243. tcsetattr(kbd_fd, TCSANOW, &term_orig);
  244. }
  245. /* Move the cursor to a position (x,y), where (0,0) is the
  246. top-left corner of the console */
  247. static void move_cursor(int line, int row)
  248. {
  249. printf(ESC"[%u;%uH", line, row);
  250. }
  251. static void clear_line(void)
  252. {
  253. printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2);
  254. }
  255. static void print_hilite(const char *str)
  256. {
  257. printf(HIGHLIGHT"%s"NORMAL, str);
  258. }
  259. static void print_statusline(const char *str)
  260. {
  261. clear_line();
  262. printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str);
  263. }
  264. /* Exit the program gracefully */
  265. static void less_exit(int code)
  266. {
  267. set_tty_cooked();
  268. clear_line();
  269. if (code < 0)
  270. kill_myself_with_sig(- code); /* does not return */
  271. exit(code);
  272. }
  273. #if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \
  274. || ENABLE_FEATURE_LESS_WINCH
  275. static void re_wrap(void)
  276. {
  277. int w = width;
  278. int new_line_pos;
  279. int src_idx;
  280. int dst_idx;
  281. int new_cur_fline = 0;
  282. uint32_t lineno;
  283. char linebuf[w + 1];
  284. const char **old_flines = flines;
  285. const char *s;
  286. char **new_flines = NULL;
  287. char *d;
  288. if (option_mask32 & FLAG_N)
  289. w -= 8;
  290. src_idx = 0;
  291. dst_idx = 0;
  292. s = old_flines[0];
  293. lineno = LINENO(s);
  294. d = linebuf;
  295. new_line_pos = 0;
  296. while (1) {
  297. *d = *s;
  298. if (*d != '\0') {
  299. new_line_pos++;
  300. if (*d == '\t') /* tab */
  301. new_line_pos += 7;
  302. s++;
  303. d++;
  304. if (new_line_pos >= w) {
  305. int sz;
  306. /* new line is full, create next one */
  307. *d = '\0';
  308. next_new:
  309. sz = (d - linebuf) + 1; /* + 1: NUL */
  310. d = ((char*)xmalloc(sz + 4)) + 4;
  311. LINENO(d) = lineno;
  312. memcpy(d, linebuf, sz);
  313. new_flines = xrealloc_vector(new_flines, 8, dst_idx);
  314. new_flines[dst_idx] = d;
  315. dst_idx++;
  316. if (new_line_pos < w) {
  317. /* if we came here thru "goto next_new" */
  318. if (src_idx > max_fline)
  319. break;
  320. lineno = LINENO(s);
  321. }
  322. d = linebuf;
  323. new_line_pos = 0;
  324. }
  325. continue;
  326. }
  327. /* *d == NUL: old line ended, go to next old one */
  328. free(MEMPTR(old_flines[src_idx]));
  329. /* btw, convert cur_fline... */
  330. if (cur_fline == src_idx)
  331. new_cur_fline = dst_idx;
  332. src_idx++;
  333. /* no more lines? finish last new line (and exit the loop) */
  334. if (src_idx > max_fline)
  335. goto next_new;
  336. s = old_flines[src_idx];
  337. if (lineno != LINENO(s)) {
  338. /* this is not a continuation line!
  339. * create next _new_ line too */
  340. goto next_new;
  341. }
  342. }
  343. free(old_flines);
  344. flines = (const char **)new_flines;
  345. max_fline = dst_idx - 1;
  346. last_line_pos = new_line_pos;
  347. cur_fline = new_cur_fline;
  348. /* max_lineno is screen-size independent */
  349. #if ENABLE_FEATURE_LESS_REGEXP
  350. pattern_valid = 0;
  351. #endif
  352. }
  353. #endif
  354. #if ENABLE_FEATURE_LESS_REGEXP
  355. static void fill_match_lines(unsigned pos);
  356. #else
  357. #define fill_match_lines(pos) ((void)0)
  358. #endif
  359. /* Devilishly complex routine.
  360. *
  361. * Has to deal with EOF and EPIPE on input,
  362. * with line wrapping, with last line not ending in '\n'
  363. * (possibly not ending YET!), with backspace and tabs.
  364. * It reads input again if last time we got an EOF (thus supporting
  365. * growing files) or EPIPE (watching output of slow process like make).
  366. *
  367. * Variables used:
  368. * flines[] - array of lines already read. Linewrap may cause
  369. * one source file line to occupy several flines[n].
  370. * flines[max_fline] - last line, possibly incomplete.
  371. * terminated - 1 if flines[max_fline] is 'terminated'
  372. * (if there was '\n' [which isn't stored itself, we just remember
  373. * that it was seen])
  374. * max_lineno - last line's number, this one doesn't increment
  375. * on line wrap, only on "real" new lines.
  376. * readbuf[0..readeof-1] - small preliminary buffer.
  377. * readbuf[readpos] - next character to add to current line.
  378. * last_line_pos - screen line position of next char to be read
  379. * (takes into account tabs and backspaces)
  380. * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error
  381. *
  382. * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs,
  383. * "/search on very long input" and "reaching max line count" corner cases.
  384. */
  385. static void read_lines(void)
  386. {
  387. #define readbuf bb_common_bufsiz1
  388. char *current_line, *p;
  389. int w = width;
  390. char last_terminated = terminated;
  391. time_t last_time = 0;
  392. int retry_EAGAIN = 2;
  393. #if ENABLE_FEATURE_LESS_REGEXP
  394. unsigned old_max_fline = max_fline;
  395. #endif
  396. /* (careful: max_fline can be -1) */
  397. if (max_fline + 1 > MAXLINES)
  398. return;
  399. if (option_mask32 & FLAG_N)
  400. w -= 8;
  401. p = current_line = ((char*)xmalloc(w + 4)) + 4;
  402. if (!last_terminated) {
  403. const char *cp = flines[max_fline];
  404. p = stpcpy(p, cp);
  405. free(MEMPTR(cp));
  406. /* last_line_pos is still valid from previous read_lines() */
  407. } else {
  408. max_fline++;
  409. last_line_pos = 0;
  410. }
  411. while (1) { /* read lines until we reach cur_fline or wanted_match */
  412. *p = '\0';
  413. terminated = 0;
  414. while (1) { /* read chars until we have a line */
  415. char c;
  416. /* if no unprocessed chars left, eat more */
  417. if (readpos >= readeof) {
  418. int flags = ndelay_on(0);
  419. while (1) {
  420. time_t t;
  421. errno = 0;
  422. eof_error = safe_read(STDIN_FILENO, readbuf, sizeof(readbuf));
  423. if (errno != EAGAIN)
  424. break;
  425. t = time(NULL);
  426. if (t != last_time) {
  427. last_time = t;
  428. if (--retry_EAGAIN < 0)
  429. break;
  430. }
  431. sched_yield();
  432. }
  433. fcntl(0, F_SETFL, flags); /* ndelay_off(0) */
  434. readpos = 0;
  435. readeof = eof_error;
  436. if (eof_error <= 0)
  437. goto reached_eof;
  438. retry_EAGAIN = 1;
  439. }
  440. c = readbuf[readpos];
  441. /* backspace? [needed for manpages] */
  442. /* <tab><bs> is (a) insane and */
  443. /* (b) harder to do correctly, so we refuse to do it */
  444. if (c == '\x8' && last_line_pos && p[-1] != '\t') {
  445. readpos++; /* eat it */
  446. last_line_pos--;
  447. /* was buggy (p could end up <= current_line)... */
  448. *--p = '\0';
  449. continue;
  450. }
  451. {
  452. size_t new_last_line_pos = last_line_pos + 1;
  453. if (c == '\t') {
  454. new_last_line_pos += 7;
  455. new_last_line_pos &= (~7);
  456. }
  457. if ((int)new_last_line_pos >= w)
  458. break;
  459. last_line_pos = new_last_line_pos;
  460. }
  461. /* ok, we will eat this char */
  462. readpos++;
  463. if (c == '\n') {
  464. terminated = 1;
  465. last_line_pos = 0;
  466. break;
  467. }
  468. /* NUL is substituted by '\n'! */
  469. if (c == '\0') c = '\n';
  470. *p++ = c;
  471. *p = '\0';
  472. } /* end of "read chars until we have a line" loop */
  473. #if 0
  474. //BUG: also triggers on this:
  475. // { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less
  476. // (resulting in lost empty line between "foo" and "bar" lines)
  477. // the "terminated" logic needs fixing (or explaining)
  478. /* Corner case: linewrap with only "" wrapping to next line */
  479. /* Looks ugly on screen, so we do not store this empty line */
  480. if (!last_terminated && !current_line[0]) {
  481. last_terminated = 1;
  482. max_lineno++;
  483. continue;
  484. }
  485. #endif
  486. reached_eof:
  487. last_terminated = terminated;
  488. flines = xrealloc_vector(flines, 8, max_fline);
  489. flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4;
  490. LINENO(flines[max_fline]) = max_lineno;
  491. if (terminated)
  492. max_lineno++;
  493. if (max_fline >= MAXLINES) {
  494. eof_error = 0; /* Pretend we saw EOF */
  495. break;
  496. }
  497. if (!(option_mask32 & FLAG_S)
  498. ? (max_fline > cur_fline + max_displayed_line)
  499. : (max_fline >= cur_fline
  500. && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
  501. ) {
  502. #if !ENABLE_FEATURE_LESS_REGEXP
  503. break;
  504. #else
  505. if (wanted_match >= num_matches) { /* goto_match called us */
  506. fill_match_lines(old_max_fline);
  507. old_max_fline = max_fline;
  508. }
  509. if (wanted_match < num_matches)
  510. break;
  511. #endif
  512. }
  513. if (eof_error <= 0) {
  514. break;
  515. }
  516. max_fline++;
  517. current_line = ((char*)xmalloc(w + 4)) + 4;
  518. p = current_line;
  519. last_line_pos = 0;
  520. } /* end of "read lines until we reach cur_fline" loop */
  521. if (eof_error < 0) {
  522. if (errno == EAGAIN) {
  523. eof_error = 1;
  524. } else {
  525. print_statusline(bb_msg_read_error);
  526. }
  527. }
  528. fill_match_lines(old_max_fline);
  529. #if ENABLE_FEATURE_LESS_REGEXP
  530. /* prevent us from being stuck in search for a match */
  531. wanted_match = -1;
  532. #endif
  533. #undef readbuf
  534. }
  535. #if ENABLE_FEATURE_LESS_FLAGS
  536. /* Interestingly, writing calc_percent as a function saves around 32 bytes
  537. * on my build. */
  538. static int calc_percent(void)
  539. {
  540. unsigned p = (100 * (cur_fline+max_displayed_line+1) + max_fline/2) / (max_fline+1);
  541. return p <= 100 ? p : 100;
  542. }
  543. /* Print a status line if -M was specified */
  544. static void m_status_print(void)
  545. {
  546. int percentage;
  547. if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
  548. return;
  549. clear_line();
  550. printf(HIGHLIGHT"%s", filename);
  551. if (num_files > 1)
  552. printf(" (file %i of %i)", current_file, num_files);
  553. printf(" lines %i-%i/%i ",
  554. cur_fline + 1, cur_fline + max_displayed_line + 1,
  555. max_fline + 1);
  556. if (cur_fline >= (int)(max_fline - max_displayed_line)) {
  557. printf("(END)"NORMAL);
  558. if (num_files > 1 && current_file != num_files)
  559. printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]);
  560. return;
  561. }
  562. percentage = calc_percent();
  563. printf("%i%%"NORMAL, percentage);
  564. }
  565. #endif
  566. /* Print the status line */
  567. static void status_print(void)
  568. {
  569. const char *p;
  570. if (less_gets_pos >= 0) /* don't touch statusline while input is done! */
  571. return;
  572. /* Change the status if flags have been set */
  573. #if ENABLE_FEATURE_LESS_FLAGS
  574. if (option_mask32 & (FLAG_M|FLAG_m)) {
  575. m_status_print();
  576. return;
  577. }
  578. /* No flags set */
  579. #endif
  580. clear_line();
  581. if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
  582. bb_putchar(':');
  583. return;
  584. }
  585. p = "(END)";
  586. if (!cur_fline)
  587. p = filename;
  588. if (num_files > 1) {
  589. printf(HIGHLIGHT"%s (file %i of %i)"NORMAL,
  590. p, current_file, num_files);
  591. return;
  592. }
  593. print_hilite(p);
  594. }
  595. static void cap_cur_fline(int nlines)
  596. {
  597. int diff;
  598. if (cur_fline < 0)
  599. cur_fline = 0;
  600. if (cur_fline + max_displayed_line > max_fline + TILDES) {
  601. cur_fline -= nlines;
  602. if (cur_fline < 0)
  603. cur_fline = 0;
  604. diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
  605. /* As the number of lines requested was too large, we just move
  606. * to the end of the file */
  607. if (diff > 0)
  608. cur_fline += diff;
  609. }
  610. }
  611. static const char controls[] ALIGN1 =
  612. /* NUL: never encountered; TAB: not converted */
  613. /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f"
  614. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  615. "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */
  616. static const char ctrlconv[] ALIGN1 =
  617. /* why 40 instead of 4a below? - it is a replacement for '\n'.
  618. * '\n' is a former NUL - we subst it with @, not J */
  619. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f"
  620. "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f";
  621. static void lineno_str(char *nbuf9, const char *line)
  622. {
  623. nbuf9[0] = '\0';
  624. if (option_mask32 & FLAG_N) {
  625. const char *fmt;
  626. unsigned n;
  627. if (line == empty_line_marker) {
  628. memset(nbuf9, ' ', 8);
  629. nbuf9[8] = '\0';
  630. return;
  631. }
  632. /* Width of 7 preserves tab spacing in the text */
  633. fmt = "%7u ";
  634. n = LINENO(line) + 1;
  635. if (n > 9999999) {
  636. n %= 10000000;
  637. fmt = "%07u ";
  638. }
  639. sprintf(nbuf9, fmt, n);
  640. }
  641. }
  642. #if ENABLE_FEATURE_LESS_REGEXP
  643. static void print_found(const char *line)
  644. {
  645. int match_status;
  646. int eflags;
  647. char *growline;
  648. regmatch_t match_structs;
  649. char buf[width];
  650. char nbuf9[9];
  651. const char *str = line;
  652. char *p = buf;
  653. size_t n;
  654. while (*str) {
  655. n = strcspn(str, controls);
  656. if (n) {
  657. if (!str[n]) break;
  658. memcpy(p, str, n);
  659. p += n;
  660. str += n;
  661. }
  662. n = strspn(str, controls);
  663. memset(p, '.', n);
  664. p += n;
  665. str += n;
  666. }
  667. strcpy(p, str);
  668. /* buf[] holds quarantined version of str */
  669. /* Each part of the line that matches has the HIGHLIGHT
  670. * and NORMAL escape sequences placed around it.
  671. * NB: we regex against line, but insert text
  672. * from quarantined copy (buf[]) */
  673. str = buf;
  674. growline = NULL;
  675. eflags = 0;
  676. goto start;
  677. while (match_status == 0) {
  678. char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
  679. growline ? growline : "",
  680. (int)match_structs.rm_so, str,
  681. (int)(match_structs.rm_eo - match_structs.rm_so),
  682. str + match_structs.rm_so);
  683. free(growline);
  684. growline = new;
  685. str += match_structs.rm_eo;
  686. line += match_structs.rm_eo;
  687. eflags = REG_NOTBOL;
  688. start:
  689. /* Most of the time doesn't find the regex, optimize for that */
  690. match_status = regexec(&pattern, line, 1, &match_structs, eflags);
  691. /* if even "" matches, treat it as "not a match" */
  692. if (match_structs.rm_so >= match_structs.rm_eo)
  693. match_status = 1;
  694. }
  695. lineno_str(nbuf9, line);
  696. if (!growline) {
  697. printf(CLEAR_2_EOL"%s%s\n", nbuf9, str);
  698. return;
  699. }
  700. printf(CLEAR_2_EOL"%s%s%s\n", nbuf9, growline, str);
  701. free(growline);
  702. }
  703. #else
  704. void print_found(const char *line);
  705. #endif
  706. static void print_ascii(const char *str)
  707. {
  708. char buf[width];
  709. char nbuf9[9];
  710. char *p;
  711. size_t n;
  712. lineno_str(nbuf9, str);
  713. printf(CLEAR_2_EOL"%s", nbuf9);
  714. while (*str) {
  715. n = strcspn(str, controls);
  716. if (n) {
  717. if (!str[n]) break;
  718. printf("%.*s", (int) n, str);
  719. str += n;
  720. }
  721. n = strspn(str, controls);
  722. p = buf;
  723. do {
  724. if (*str == 0x7f)
  725. *p++ = '?';
  726. else if (*str == (char)0x9b)
  727. /* VT100's CSI, aka Meta-ESC. Who's inventor? */
  728. /* I want to know who committed this sin */
  729. *p++ = '{';
  730. else
  731. *p++ = ctrlconv[(unsigned char)*str];
  732. str++;
  733. } while (--n);
  734. *p = '\0';
  735. print_hilite(buf);
  736. }
  737. puts(str);
  738. }
  739. /* Print the buffer */
  740. static void buffer_print(void)
  741. {
  742. unsigned i;
  743. move_cursor(0, 0);
  744. for (i = 0; i <= max_displayed_line; i++) {
  745. if (pattern_valid)
  746. print_found(buffer[i]);
  747. else
  748. print_ascii(buffer[i]);
  749. }
  750. if ((option_mask32 & FLAG_E)
  751. && eof_error <= 0
  752. && (max_fline - cur_fline) <= max_displayed_line
  753. ) {
  754. less_exit(EXIT_SUCCESS);
  755. }
  756. status_print();
  757. }
  758. static void buffer_fill_and_print(void)
  759. {
  760. unsigned i;
  761. #if ENABLE_FEATURE_LESS_DASHCMD
  762. int fpos = cur_fline;
  763. if (option_mask32 & FLAG_S) {
  764. /* Go back to the beginning of this line */
  765. while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1]))
  766. fpos--;
  767. }
  768. i = 0;
  769. while (i <= max_displayed_line && fpos <= max_fline) {
  770. int lineno = LINENO(flines[fpos]);
  771. buffer[i] = flines[fpos];
  772. i++;
  773. do {
  774. fpos++;
  775. } while ((fpos <= max_fline)
  776. && (option_mask32 & FLAG_S)
  777. && lineno == LINENO(flines[fpos])
  778. );
  779. }
  780. #else
  781. for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) {
  782. buffer[i] = flines[cur_fline + i];
  783. }
  784. #endif
  785. for (; i <= max_displayed_line; i++) {
  786. buffer[i] = empty_line_marker;
  787. }
  788. buffer_print();
  789. }
  790. /* Move the buffer up and down in the file in order to scroll */
  791. static void buffer_down(int nlines)
  792. {
  793. cur_fline += nlines;
  794. read_lines();
  795. cap_cur_fline(nlines);
  796. buffer_fill_and_print();
  797. }
  798. static void buffer_up(int nlines)
  799. {
  800. cur_fline -= nlines;
  801. if (cur_fline < 0) cur_fline = 0;
  802. read_lines();
  803. buffer_fill_and_print();
  804. }
  805. static void buffer_line(int linenum)
  806. {
  807. if (linenum < 0)
  808. linenum = 0;
  809. cur_fline = linenum;
  810. read_lines();
  811. if (linenum + max_displayed_line > max_fline)
  812. linenum = max_fline - max_displayed_line + TILDES;
  813. if (linenum < 0)
  814. linenum = 0;
  815. cur_fline = linenum;
  816. buffer_fill_and_print();
  817. }
  818. static void open_file_and_read_lines(void)
  819. {
  820. if (filename) {
  821. xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
  822. } else {
  823. /* "less" with no arguments in argv[] */
  824. /* For status line only */
  825. filename = xstrdup(bb_msg_standard_input);
  826. }
  827. readpos = 0;
  828. readeof = 0;
  829. last_line_pos = 0;
  830. terminated = 1;
  831. read_lines();
  832. }
  833. /* Reinitialize everything for a new file - free the memory and start over */
  834. static void reinitialize(void)
  835. {
  836. unsigned i;
  837. if (flines) {
  838. for (i = 0; i <= max_fline; i++)
  839. free(MEMPTR(flines[i]));
  840. free(flines);
  841. flines = NULL;
  842. }
  843. max_fline = -1;
  844. cur_fline = 0;
  845. max_lineno = 0;
  846. open_file_and_read_lines();
  847. #if ENABLE_FEATURE_LESS_ASK_TERMINAL
  848. if (G.winsize_err)
  849. printf("\033[999;999H" "\033[6n");
  850. #endif
  851. buffer_fill_and_print();
  852. }
  853. static int64_t getch_nowait(void)
  854. {
  855. int rd;
  856. int64_t key64;
  857. struct pollfd pfd[2];
  858. pfd[0].fd = STDIN_FILENO;
  859. pfd[0].events = POLLIN;
  860. pfd[1].fd = kbd_fd;
  861. pfd[1].events = POLLIN;
  862. again:
  863. tcsetattr(kbd_fd, TCSANOW, &term_less);
  864. /* NB: select/poll returns whenever read will not block. Therefore:
  865. * if eof is reached, select/poll will return immediately
  866. * because read will immediately return 0 bytes.
  867. * Even if select/poll says that input is available, read CAN block
  868. * (switch fd into O_NONBLOCK'ed mode to avoid it)
  869. */
  870. rd = 1;
  871. /* Are we interested in stdin? */
  872. //TODO: reuse code for determining this
  873. if (!(option_mask32 & FLAG_S)
  874. ? !(max_fline > cur_fline + max_displayed_line)
  875. : !(max_fline >= cur_fline
  876. && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
  877. ) {
  878. if (eof_error > 0) /* did NOT reach eof yet */
  879. rd = 0; /* yes, we are interested in stdin */
  880. }
  881. /* Position cursor if line input is done */
  882. if (less_gets_pos >= 0)
  883. move_cursor(max_displayed_line + 2, less_gets_pos + 1);
  884. fflush_all();
  885. if (kbd_input[0] == 0) { /* if nothing is buffered */
  886. #if ENABLE_FEATURE_LESS_WINCH
  887. while (1) {
  888. int r;
  889. /* NB: SIGWINCH interrupts poll() */
  890. r = poll(pfd + rd, 2 - rd, -1);
  891. if (/*r < 0 && errno == EINTR &&*/ winch_counter)
  892. return '\\'; /* anything which has no defined function */
  893. if (r) break;
  894. }
  895. #else
  896. safe_poll(pfd + rd, 2 - rd, -1);
  897. #endif
  898. }
  899. /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
  900. * would not block even if there is no input available */
  901. key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
  902. if ((int)key64 == -1) {
  903. if (errno == EAGAIN) {
  904. /* No keyboard input available. Since poll() did return,
  905. * we should have input on stdin */
  906. read_lines();
  907. buffer_fill_and_print();
  908. goto again;
  909. }
  910. /* EOF/error (ssh session got killed etc) */
  911. less_exit(0);
  912. }
  913. set_tty_cooked();
  914. return key64;
  915. }
  916. /* Grab a character from input without requiring the return key.
  917. * May return KEYCODE_xxx values.
  918. * Note that this function works best with raw input. */
  919. static int64_t less_getch(int pos)
  920. {
  921. int64_t key64;
  922. int key;
  923. again:
  924. less_gets_pos = pos;
  925. key = key64 = getch_nowait();
  926. less_gets_pos = -1;
  927. /* Discard Ctrl-something chars.
  928. * (checking only lower 32 bits is a size optimization:
  929. * upper 32 bits are used only by KEYCODE_CURSOR_POS)
  930. */
  931. if (key >= 0 && key < ' ' && key != 0x0d && key != 8)
  932. goto again;
  933. return key64;
  934. }
  935. static char* less_gets(int sz)
  936. {
  937. int c;
  938. unsigned i = 0;
  939. char *result = xzalloc(1);
  940. while (1) {
  941. c = '\0';
  942. less_gets_pos = sz + i;
  943. c = getch_nowait();
  944. if (c == 0x0d) {
  945. result[i] = '\0';
  946. less_gets_pos = -1;
  947. return result;
  948. }
  949. if (c == 0x7f)
  950. c = 8;
  951. if (c == 8 && i) {
  952. printf("\x8 \x8");
  953. i--;
  954. }
  955. if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
  956. continue;
  957. if (i >= width - sz - 1)
  958. continue; /* len limit */
  959. bb_putchar(c);
  960. result[i++] = c;
  961. result = xrealloc(result, i+1);
  962. }
  963. }
  964. static void examine_file(void)
  965. {
  966. char *new_fname;
  967. print_statusline("Examine: ");
  968. new_fname = less_gets(sizeof("Examine: ") - 1);
  969. if (!new_fname[0]) {
  970. status_print();
  971. err:
  972. free(new_fname);
  973. return;
  974. }
  975. if (access(new_fname, R_OK) != 0) {
  976. print_statusline("Cannot read this file");
  977. goto err;
  978. }
  979. free(filename);
  980. filename = new_fname;
  981. /* files start by = argv. why we assume that argv is infinitely long??
  982. files[num_files] = filename;
  983. current_file = num_files + 1;
  984. num_files++; */
  985. files[0] = filename;
  986. num_files = current_file = 1;
  987. reinitialize();
  988. }
  989. /* This function changes the file currently being paged. direction can be one of the following:
  990. * -1: go back one file
  991. * 0: go to the first file
  992. * 1: go forward one file */
  993. static void change_file(int direction)
  994. {
  995. if (current_file != ((direction > 0) ? num_files : 1)) {
  996. current_file = direction ? current_file + direction : 1;
  997. free(filename);
  998. filename = xstrdup(files[current_file - 1]);
  999. reinitialize();
  1000. } else {
  1001. print_statusline(direction > 0 ? "No next file" : "No previous file");
  1002. }
  1003. }
  1004. static void remove_current_file(void)
  1005. {
  1006. unsigned i;
  1007. if (num_files < 2)
  1008. return;
  1009. if (current_file != 1) {
  1010. change_file(-1);
  1011. for (i = 3; i <= num_files; i++)
  1012. files[i - 2] = files[i - 1];
  1013. num_files--;
  1014. } else {
  1015. change_file(1);
  1016. for (i = 2; i <= num_files; i++)
  1017. files[i - 2] = files[i - 1];
  1018. num_files--;
  1019. current_file--;
  1020. }
  1021. }
  1022. static void colon_process(void)
  1023. {
  1024. int keypress;
  1025. /* Clear the current line and print a prompt */
  1026. print_statusline(" :");
  1027. keypress = less_getch(2);
  1028. switch (keypress) {
  1029. case 'd':
  1030. remove_current_file();
  1031. break;
  1032. case 'e':
  1033. examine_file();
  1034. break;
  1035. #if ENABLE_FEATURE_LESS_FLAGS
  1036. case 'f':
  1037. m_status_print();
  1038. break;
  1039. #endif
  1040. case 'n':
  1041. change_file(1);
  1042. break;
  1043. case 'p':
  1044. change_file(-1);
  1045. break;
  1046. case 'q':
  1047. less_exit(EXIT_SUCCESS);
  1048. break;
  1049. case 'x':
  1050. change_file(0);
  1051. break;
  1052. }
  1053. }
  1054. #if ENABLE_FEATURE_LESS_REGEXP
  1055. static void normalize_match_pos(int match)
  1056. {
  1057. if (match >= num_matches)
  1058. match = num_matches - 1;
  1059. if (match < 0)
  1060. match = 0;
  1061. match_pos = match;
  1062. }
  1063. static void goto_match(int match)
  1064. {
  1065. if (!pattern_valid)
  1066. return;
  1067. if (match < 0)
  1068. match = 0;
  1069. /* Try to find next match if eof isn't reached yet */
  1070. if (match >= num_matches && eof_error > 0) {
  1071. wanted_match = match; /* "I want to read until I see N'th match" */
  1072. read_lines();
  1073. }
  1074. if (num_matches) {
  1075. normalize_match_pos(match);
  1076. buffer_line(match_lines[match_pos]);
  1077. } else {
  1078. print_statusline("No matches found");
  1079. }
  1080. }
  1081. static void fill_match_lines(unsigned pos)
  1082. {
  1083. if (!pattern_valid)
  1084. return;
  1085. /* Run the regex on each line of the current file */
  1086. while (pos <= max_fline) {
  1087. /* If this line matches */
  1088. if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0
  1089. /* and we didn't match it last time */
  1090. && !(num_matches && match_lines[num_matches-1] == pos)
  1091. ) {
  1092. match_lines = xrealloc_vector(match_lines, 4, num_matches);
  1093. match_lines[num_matches++] = pos;
  1094. }
  1095. pos++;
  1096. }
  1097. }
  1098. static void regex_process(void)
  1099. {
  1100. char *uncomp_regex, *err;
  1101. /* Reset variables */
  1102. free(match_lines);
  1103. match_lines = NULL;
  1104. match_pos = 0;
  1105. num_matches = 0;
  1106. if (pattern_valid) {
  1107. regfree(&pattern);
  1108. pattern_valid = 0;
  1109. }
  1110. /* Get the uncompiled regular expression from the user */
  1111. clear_line();
  1112. bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
  1113. uncomp_regex = less_gets(1);
  1114. if (!uncomp_regex[0]) {
  1115. free(uncomp_regex);
  1116. buffer_print();
  1117. return;
  1118. }
  1119. /* Compile the regex and check for errors */
  1120. err = regcomp_or_errmsg(&pattern, uncomp_regex,
  1121. (option_mask32 & FLAG_I) ? REG_ICASE : 0);
  1122. free(uncomp_regex);
  1123. if (err) {
  1124. print_statusline(err);
  1125. free(err);
  1126. return;
  1127. }
  1128. pattern_valid = 1;
  1129. match_pos = 0;
  1130. fill_match_lines(0);
  1131. while (match_pos < num_matches) {
  1132. if ((int)match_lines[match_pos] > cur_fline)
  1133. break;
  1134. match_pos++;
  1135. }
  1136. if (option_mask32 & LESS_STATE_MATCH_BACKWARDS)
  1137. match_pos--;
  1138. /* It's possible that no matches are found yet.
  1139. * goto_match() will read input looking for match,
  1140. * if needed */
  1141. goto_match(match_pos);
  1142. }
  1143. #endif
  1144. static void number_process(int first_digit)
  1145. {
  1146. unsigned i;
  1147. int num;
  1148. int keypress;
  1149. char num_input[sizeof(int)*4]; /* more than enough */
  1150. num_input[0] = first_digit;
  1151. /* Clear the current line, print a prompt, and then print the digit */
  1152. clear_line();
  1153. printf(":%c", first_digit);
  1154. /* Receive input until a letter is given */
  1155. i = 1;
  1156. while (i < sizeof(num_input)-1) {
  1157. keypress = less_getch(i + 1);
  1158. if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
  1159. break;
  1160. num_input[i] = keypress;
  1161. bb_putchar(keypress);
  1162. i++;
  1163. }
  1164. num_input[i] = '\0';
  1165. num = bb_strtou(num_input, NULL, 10);
  1166. /* on format error, num == -1 */
  1167. if (num < 1 || num > MAXLINES) {
  1168. buffer_print();
  1169. return;
  1170. }
  1171. /* We now know the number and the letter entered, so we process them */
  1172. switch (keypress) {
  1173. case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
  1174. buffer_down(num);
  1175. break;
  1176. case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
  1177. buffer_up(num);
  1178. break;
  1179. case 'g': case '<': case 'G': case '>':
  1180. cur_fline = num + max_displayed_line;
  1181. read_lines();
  1182. buffer_line(num - 1);
  1183. break;
  1184. case 'p': case '%':
  1185. num = num * (max_fline / 100); /* + max_fline / 2; */
  1186. cur_fline = num + max_displayed_line;
  1187. read_lines();
  1188. buffer_line(num);
  1189. break;
  1190. #if ENABLE_FEATURE_LESS_REGEXP
  1191. case 'n':
  1192. goto_match(match_pos + num);
  1193. break;
  1194. case '/':
  1195. option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
  1196. regex_process();
  1197. break;
  1198. case '?':
  1199. option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
  1200. regex_process();
  1201. break;
  1202. #endif
  1203. }
  1204. }
  1205. #if ENABLE_FEATURE_LESS_DASHCMD
  1206. static void flag_change(void)
  1207. {
  1208. int keypress;
  1209. clear_line();
  1210. bb_putchar('-');
  1211. keypress = less_getch(1);
  1212. switch (keypress) {
  1213. case 'M':
  1214. option_mask32 ^= FLAG_M;
  1215. break;
  1216. case 'm':
  1217. option_mask32 ^= FLAG_m;
  1218. break;
  1219. case 'E':
  1220. option_mask32 ^= FLAG_E;
  1221. break;
  1222. case '~':
  1223. option_mask32 ^= FLAG_TILDE;
  1224. break;
  1225. case 'S':
  1226. option_mask32 ^= FLAG_S;
  1227. buffer_fill_and_print();
  1228. break;
  1229. #if ENABLE_FEATURE_LESS_LINENUMS
  1230. case 'N':
  1231. option_mask32 ^= FLAG_N;
  1232. re_wrap();
  1233. buffer_fill_and_print();
  1234. break;
  1235. #endif
  1236. }
  1237. }
  1238. #ifdef BLOAT
  1239. static void show_flag_status(void)
  1240. {
  1241. int keypress;
  1242. int flag_val;
  1243. clear_line();
  1244. bb_putchar('_');
  1245. keypress = less_getch(1);
  1246. switch (keypress) {
  1247. case 'M':
  1248. flag_val = option_mask32 & FLAG_M;
  1249. break;
  1250. case 'm':
  1251. flag_val = option_mask32 & FLAG_m;
  1252. break;
  1253. case '~':
  1254. flag_val = option_mask32 & FLAG_TILDE;
  1255. break;
  1256. case 'N':
  1257. flag_val = option_mask32 & FLAG_N;
  1258. break;
  1259. case 'E':
  1260. flag_val = option_mask32 & FLAG_E;
  1261. break;
  1262. default:
  1263. flag_val = 0;
  1264. break;
  1265. }
  1266. clear_line();
  1267. printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0);
  1268. }
  1269. #endif
  1270. #endif /* ENABLE_FEATURE_LESS_DASHCMD */
  1271. static void save_input_to_file(void)
  1272. {
  1273. const char *msg = "";
  1274. char *current_line;
  1275. unsigned i;
  1276. FILE *fp;
  1277. print_statusline("Log file: ");
  1278. current_line = less_gets(sizeof("Log file: ")-1);
  1279. if (current_line[0]) {
  1280. fp = fopen_for_write(current_line);
  1281. if (!fp) {
  1282. msg = "Error opening log file";
  1283. goto ret;
  1284. }
  1285. for (i = 0; i <= max_fline; i++)
  1286. fprintf(fp, "%s\n", flines[i]);
  1287. fclose(fp);
  1288. msg = "Done";
  1289. }
  1290. ret:
  1291. print_statusline(msg);
  1292. free(current_line);
  1293. }
  1294. #if ENABLE_FEATURE_LESS_MARKS
  1295. static void add_mark(void)
  1296. {
  1297. int letter;
  1298. print_statusline("Mark: ");
  1299. letter = less_getch(sizeof("Mark: ") - 1);
  1300. if (isalpha(letter)) {
  1301. /* If we exceed 15 marks, start overwriting previous ones */
  1302. if (num_marks == 14)
  1303. num_marks = 0;
  1304. mark_lines[num_marks][0] = letter;
  1305. mark_lines[num_marks][1] = cur_fline;
  1306. num_marks++;
  1307. } else {
  1308. print_statusline("Invalid mark letter");
  1309. }
  1310. }
  1311. static void goto_mark(void)
  1312. {
  1313. int letter;
  1314. int i;
  1315. print_statusline("Go to mark: ");
  1316. letter = less_getch(sizeof("Go to mark: ") - 1);
  1317. clear_line();
  1318. if (isalpha(letter)) {
  1319. for (i = 0; i <= num_marks; i++)
  1320. if (letter == mark_lines[i][0]) {
  1321. buffer_line(mark_lines[i][1]);
  1322. break;
  1323. }
  1324. if (num_marks == 14 && letter != mark_lines[14][0])
  1325. print_statusline("Mark not set");
  1326. } else
  1327. print_statusline("Invalid mark letter");
  1328. }
  1329. #endif
  1330. #if ENABLE_FEATURE_LESS_BRACKETS
  1331. static char opp_bracket(char bracket)
  1332. {
  1333. switch (bracket) {
  1334. case '{': case '[': /* '}' == '{' + 2. Same for '[' */
  1335. bracket++;
  1336. case '(': /* ')' == '(' + 1 */
  1337. bracket++;
  1338. break;
  1339. case '}': case ']':
  1340. bracket--;
  1341. case ')':
  1342. bracket--;
  1343. break;
  1344. };
  1345. return bracket;
  1346. }
  1347. static void match_right_bracket(char bracket)
  1348. {
  1349. unsigned i;
  1350. if (strchr(flines[cur_fline], bracket) == NULL) {
  1351. print_statusline("No bracket in top line");
  1352. return;
  1353. }
  1354. bracket = opp_bracket(bracket);
  1355. for (i = cur_fline + 1; i < max_fline; i++) {
  1356. if (strchr(flines[i], bracket) != NULL) {
  1357. buffer_line(i);
  1358. return;
  1359. }
  1360. }
  1361. print_statusline("No matching bracket found");
  1362. }
  1363. static void match_left_bracket(char bracket)
  1364. {
  1365. int i;
  1366. if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
  1367. print_statusline("No bracket in bottom line");
  1368. return;
  1369. }
  1370. bracket = opp_bracket(bracket);
  1371. for (i = cur_fline + max_displayed_line; i >= 0; i--) {
  1372. if (strchr(flines[i], bracket) != NULL) {
  1373. buffer_line(i);
  1374. return;
  1375. }
  1376. }
  1377. print_statusline("No matching bracket found");
  1378. }
  1379. #endif /* FEATURE_LESS_BRACKETS */
  1380. static void keypress_process(int keypress)
  1381. {
  1382. switch (keypress) {
  1383. case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
  1384. buffer_down(1);
  1385. break;
  1386. case KEYCODE_UP: case 'y': case 'k':
  1387. buffer_up(1);
  1388. break;
  1389. case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
  1390. buffer_down(max_displayed_line + 1);
  1391. break;
  1392. case KEYCODE_PAGEUP: case 'w': case 'b':
  1393. buffer_up(max_displayed_line + 1);
  1394. break;
  1395. case 'd':
  1396. buffer_down((max_displayed_line + 1) / 2);
  1397. break;
  1398. case 'u':
  1399. buffer_up((max_displayed_line + 1) / 2);
  1400. break;
  1401. case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
  1402. buffer_line(0);
  1403. break;
  1404. case KEYCODE_END: case 'G': case '>':
  1405. cur_fline = MAXLINES;
  1406. read_lines();
  1407. buffer_line(cur_fline);
  1408. break;
  1409. case 'q': case 'Q':
  1410. less_exit(EXIT_SUCCESS);
  1411. break;
  1412. #if ENABLE_FEATURE_LESS_MARKS
  1413. case 'm':
  1414. add_mark();
  1415. buffer_print();
  1416. break;
  1417. case '\'':
  1418. goto_mark();
  1419. buffer_print();
  1420. break;
  1421. #endif
  1422. case 'r': case 'R':
  1423. /* TODO: (1) also bind ^R, ^L to this?
  1424. * (2) re-measure window size?
  1425. */
  1426. buffer_print();
  1427. break;
  1428. /*case 'R':
  1429. full_repaint();
  1430. break;*/
  1431. case 's':
  1432. save_input_to_file();
  1433. break;
  1434. case 'E':
  1435. examine_file();
  1436. break;
  1437. #if ENABLE_FEATURE_LESS_FLAGS
  1438. case '=':
  1439. m_status_print();
  1440. break;
  1441. #endif
  1442. #if ENABLE_FEATURE_LESS_REGEXP
  1443. case '/':
  1444. option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS;
  1445. regex_process();
  1446. break;
  1447. case 'n':
  1448. goto_match(match_pos + 1);
  1449. break;
  1450. case 'N':
  1451. goto_match(match_pos - 1);
  1452. break;
  1453. case '?':
  1454. option_mask32 |= LESS_STATE_MATCH_BACKWARDS;
  1455. regex_process();
  1456. break;
  1457. #endif
  1458. #if ENABLE_FEATURE_LESS_DASHCMD
  1459. case '-':
  1460. flag_change();
  1461. buffer_print();
  1462. break;
  1463. #ifdef BLOAT
  1464. case '_':
  1465. show_flag_status();
  1466. break;
  1467. #endif
  1468. #endif
  1469. #if ENABLE_FEATURE_LESS_BRACKETS
  1470. case '{': case '(': case '[':
  1471. match_right_bracket(keypress);
  1472. break;
  1473. case '}': case ')': case ']':
  1474. match_left_bracket(keypress);
  1475. break;
  1476. #endif
  1477. case ':':
  1478. colon_process();
  1479. break;
  1480. }
  1481. if (isdigit(keypress))
  1482. number_process(keypress);
  1483. }
  1484. static void sig_catcher(int sig)
  1485. {
  1486. less_exit(- sig);
  1487. }
  1488. #if ENABLE_FEATURE_LESS_WINCH
  1489. static void sigwinch_handler(int sig UNUSED_PARAM)
  1490. {
  1491. winch_counter++;
  1492. }
  1493. #endif
  1494. int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1495. int less_main(int argc, char **argv)
  1496. {
  1497. char *tty_name;
  1498. int tty_fd;
  1499. INIT_G();
  1500. /* TODO: -x: do not interpret backspace, -xx: tab also
  1501. * -xxx: newline also
  1502. * -w N: assume width N (-xxx -w 32: hex viewer of sorts)
  1503. * -s: condense many empty lines to one
  1504. * (used by some setups for manpage display)
  1505. */
  1506. getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S") /*ignored:*/"s");
  1507. argc -= optind;
  1508. argv += optind;
  1509. num_files = argc;
  1510. files = argv;
  1511. /* Another popular pager, most, detects when stdout
  1512. * is not a tty and turns into cat. This makes sense. */
  1513. if (!isatty(STDOUT_FILENO))
  1514. return bb_cat(argv);
  1515. if (!num_files) {
  1516. if (isatty(STDIN_FILENO)) {
  1517. /* Just "less"? No args and no redirection? */
  1518. bb_error_msg("missing filename");
  1519. bb_show_usage();
  1520. }
  1521. } else {
  1522. filename = xstrdup(files[0]);
  1523. }
  1524. if (option_mask32 & FLAG_TILDE)
  1525. empty_line_marker = "";
  1526. /* Some versions of less can survive w/o controlling tty,
  1527. * try to do the same. This also allows to specify an alternative
  1528. * tty via "less 1<>TTY".
  1529. * We don't try to use STDOUT_FILENO directly,
  1530. * since we want to set this fd to non-blocking mode,
  1531. * and not bother with restoring it on exit.
  1532. */
  1533. tty_name = xmalloc_ttyname(STDOUT_FILENO);
  1534. if (tty_name) {
  1535. tty_fd = open(tty_name, O_RDONLY);
  1536. free(tty_name);
  1537. if (tty_fd < 0)
  1538. goto try_ctty;
  1539. } else {
  1540. /* Try controlling tty */
  1541. try_ctty:
  1542. tty_fd = open(CURRENT_TTY, O_RDONLY);
  1543. if (tty_fd < 0)
  1544. return bb_cat(argv);
  1545. }
  1546. ndelay_on(tty_fd);
  1547. kbd_fd = tty_fd; /* save in a global */
  1548. tcgetattr(kbd_fd, &term_orig);
  1549. term_less = term_orig;
  1550. term_less.c_lflag &= ~(ICANON | ECHO);
  1551. term_less.c_iflag &= ~(IXON | ICRNL);
  1552. /*term_less.c_oflag &= ~ONLCR;*/
  1553. term_less.c_cc[VMIN] = 1;
  1554. term_less.c_cc[VTIME] = 0;
  1555. IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
  1556. /* 20: two tabstops + 4 */
  1557. if (width < 20 || max_displayed_line < 3)
  1558. return bb_cat(argv);
  1559. max_displayed_line -= 2;
  1560. /* We want to restore term_orig on exit */
  1561. bb_signals(BB_FATAL_SIGS, sig_catcher);
  1562. #if ENABLE_FEATURE_LESS_WINCH
  1563. signal(SIGWINCH, sigwinch_handler);
  1564. #endif
  1565. buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
  1566. reinitialize();
  1567. while (1) {
  1568. int64_t keypress;
  1569. #if ENABLE_FEATURE_LESS_WINCH
  1570. while (WINCH_COUNTER) {
  1571. again:
  1572. winch_counter--;
  1573. IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
  1574. IF_FEATURE_LESS_ASK_TERMINAL(got_size:)
  1575. /* 20: two tabstops + 4 */
  1576. if (width < 20)
  1577. width = 20;
  1578. if (max_displayed_line < 3)
  1579. max_displayed_line = 3;
  1580. max_displayed_line -= 2;
  1581. free(buffer);
  1582. buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
  1583. /* Avoid re-wrap and/or redraw if we already know
  1584. * we need to do it again. These ops are expensive */
  1585. if (WINCH_COUNTER)
  1586. goto again;
  1587. re_wrap();
  1588. if (WINCH_COUNTER)
  1589. goto again;
  1590. buffer_fill_and_print();
  1591. /* This took some time. Loop back and check,
  1592. * were there another SIGWINCH? */
  1593. }
  1594. keypress = less_getch(-1); /* -1: do not position cursor */
  1595. # if ENABLE_FEATURE_LESS_ASK_TERMINAL
  1596. if ((int32_t)keypress == KEYCODE_CURSOR_POS) {
  1597. uint32_t rc = (keypress >> 32);
  1598. width = (rc & 0x7fff);
  1599. max_displayed_line = ((rc >> 16) & 0x7fff);
  1600. goto got_size;
  1601. }
  1602. # endif
  1603. #else
  1604. keypress = less_getch(-1); /* -1: do not position cursor */
  1605. #endif
  1606. keypress_process(keypress);
  1607. }
  1608. }
  1609. /*
  1610. Help text of less version 418 is below.
  1611. If you are implementing something, keeping
  1612. key and/or command line switch compatibility is a good idea:
  1613. SUMMARY OF LESS COMMANDS
  1614. Commands marked with * may be preceded by a number, N.
  1615. Notes in parentheses indicate the behavior if N is given.
  1616. h H Display this help.
  1617. q :q Q :Q ZZ Exit.
  1618. ---------------------------------------------------------------------------
  1619. MOVING
  1620. e ^E j ^N CR * Forward one line (or N lines).
  1621. y ^Y k ^K ^P * Backward one line (or N lines).
  1622. f ^F ^V SPACE * Forward one window (or N lines).
  1623. b ^B ESC-v * Backward one window (or N lines).
  1624. z * Forward one window (and set window to N).
  1625. w * Backward one window (and set window to N).
  1626. ESC-SPACE * Forward one window, but don't stop at end-of-file.
  1627. d ^D * Forward one half-window (and set half-window to N).
  1628. u ^U * Backward one half-window (and set half-window to N).
  1629. ESC-) RightArrow * Left one half screen width (or N positions).
  1630. ESC-( LeftArrow * Right one half screen width (or N positions).
  1631. F Forward forever; like "tail -f".
  1632. r ^R ^L Repaint screen.
  1633. R Repaint screen, discarding buffered input.
  1634. ---------------------------------------------------
  1635. Default "window" is the screen height.
  1636. Default "half-window" is half of the screen height.
  1637. ---------------------------------------------------------------------------
  1638. SEARCHING
  1639. /pattern * Search forward for (N-th) matching line.
  1640. ?pattern * Search backward for (N-th) matching line.
  1641. n * Repeat previous search (for N-th occurrence).
  1642. N * Repeat previous search in reverse direction.
  1643. ESC-n * Repeat previous search, spanning files.
  1644. ESC-N * Repeat previous search, reverse dir. & spanning files.
  1645. ESC-u Undo (toggle) search highlighting.
  1646. ---------------------------------------------------
  1647. Search patterns may be modified by one or more of:
  1648. ^N or ! Search for NON-matching lines.
  1649. ^E or * Search multiple files (pass thru END OF FILE).
  1650. ^F or @ Start search at FIRST file (for /) or last file (for ?).
  1651. ^K Highlight matches, but don't move (KEEP position).
  1652. ^R Don't use REGULAR EXPRESSIONS.
  1653. ---------------------------------------------------------------------------
  1654. JUMPING
  1655. g < ESC-< * Go to first line in file (or line N).
  1656. G > ESC-> * Go to last line in file (or line N).
  1657. p % * Go to beginning of file (or N percent into file).
  1658. t * Go to the (N-th) next tag.
  1659. T * Go to the (N-th) previous tag.
  1660. { ( [ * Find close bracket } ) ].
  1661. } ) ] * Find open bracket { ( [.
  1662. ESC-^F <c1> <c2> * Find close bracket <c2>.
  1663. ESC-^B <c1> <c2> * Find open bracket <c1>
  1664. ---------------------------------------------------
  1665. Each "find close bracket" command goes forward to the close bracket
  1666. matching the (N-th) open bracket in the top line.
  1667. Each "find open bracket" command goes backward to the open bracket
  1668. matching the (N-th) close bracket in the bottom line.
  1669. m<letter> Mark the current position with <letter>.
  1670. '<letter> Go to a previously marked position.
  1671. '' Go to the previous position.
  1672. ^X^X Same as '.
  1673. ---------------------------------------------------
  1674. A mark is any upper-case or lower-case letter.
  1675. Certain marks are predefined:
  1676. ^ means beginning of the file
  1677. $ means end of the file
  1678. ---------------------------------------------------------------------------
  1679. CHANGING FILES
  1680. :e [file] Examine a new file.
  1681. ^X^V Same as :e.
  1682. :n * Examine the (N-th) next file from the command line.
  1683. :p * Examine the (N-th) previous file from the command line.
  1684. :x * Examine the first (or N-th) file from the command line.
  1685. :d Delete the current file from the command line list.
  1686. = ^G :f Print current file name.
  1687. ---------------------------------------------------------------------------
  1688. MISCELLANEOUS COMMANDS
  1689. -<flag> Toggle a command line option [see OPTIONS below].
  1690. --<name> Toggle a command line option, by name.
  1691. _<flag> Display the setting of a command line option.
  1692. __<name> Display the setting of an option, by name.
  1693. +cmd Execute the less cmd each time a new file is examined.
  1694. !command Execute the shell command with $SHELL.
  1695. |Xcommand Pipe file between current pos & mark X to shell command.
  1696. v Edit the current file with $VISUAL or $EDITOR.
  1697. V Print version number of "less".
  1698. ---------------------------------------------------------------------------
  1699. OPTIONS
  1700. Most options may be changed either on the command line,
  1701. or from within less by using the - or -- command.
  1702. Options may be given in one of two forms: either a single
  1703. character preceded by a -, or a name preceeded by --.
  1704. -? ........ --help
  1705. Display help (from command line).
  1706. -a ........ --search-skip-screen
  1707. Forward search skips current screen.
  1708. -b [N] .... --buffers=[N]
  1709. Number of buffers.
  1710. -B ........ --auto-buffers
  1711. Don't automatically allocate buffers for pipes.
  1712. -c ........ --clear-screen
  1713. Repaint by clearing rather than scrolling.
  1714. -d ........ --dumb
  1715. Dumb terminal.
  1716. -D [xn.n] . --color=xn.n
  1717. Set screen colors. (MS-DOS only)
  1718. -e -E .... --quit-at-eof --QUIT-AT-EOF
  1719. Quit at end of file.
  1720. -f ........ --force
  1721. Force open non-regular files.
  1722. -F ........ --quit-if-one-screen
  1723. Quit if entire file fits on first screen.
  1724. -g ........ --hilite-search
  1725. Highlight only last match for searches.
  1726. -G ........ --HILITE-SEARCH
  1727. Don't highlight any matches for searches.
  1728. -h [N] .... --max-back-scroll=[N]
  1729. Backward scroll limit.
  1730. -i ........ --ignore-case
  1731. Ignore case in searches that do not contain uppercase.
  1732. -I ........ --IGNORE-CASE
  1733. Ignore case in all searches.
  1734. -j [N] .... --jump-target=[N]
  1735. Screen position of target lines.
  1736. -J ........ --status-column
  1737. Display a status column at left edge of screen.
  1738. -k [file] . --lesskey-file=[file]
  1739. Use a lesskey file.
  1740. -L ........ --no-lessopen
  1741. Ignore the LESSOPEN environment variable.
  1742. -m -M .... --long-prompt --LONG-PROMPT
  1743. Set prompt style.
  1744. -n -N .... --line-numbers --LINE-NUMBERS
  1745. Don't use line numbers.
  1746. -o [file] . --log-file=[file]
  1747. Copy to log file (standard input only).
  1748. -O [file] . --LOG-FILE=[file]
  1749. Copy to log file (unconditionally overwrite).
  1750. -p [pattern] --pattern=[pattern]
  1751. Start at pattern (from command line).
  1752. -P [prompt] --prompt=[prompt]
  1753. Define new prompt.
  1754. -q -Q .... --quiet --QUIET --silent --SILENT
  1755. Quiet the terminal bell.
  1756. -r -R .... --raw-control-chars --RAW-CONTROL-CHARS
  1757. Output "raw" control characters.
  1758. -s ........ --squeeze-blank-lines
  1759. Squeeze multiple blank lines.
  1760. -S ........ --chop-long-lines
  1761. Chop long lines.
  1762. -t [tag] .. --tag=[tag]
  1763. Find a tag.
  1764. -T [tagsfile] --tag-file=[tagsfile]
  1765. Use an alternate tags file.
  1766. -u -U .... --underline-special --UNDERLINE-SPECIAL
  1767. Change handling of backspaces.
  1768. -V ........ --version
  1769. Display the version number of "less".
  1770. -w ........ --hilite-unread
  1771. Highlight first new line after forward-screen.
  1772. -W ........ --HILITE-UNREAD
  1773. Highlight first new line after any forward movement.
  1774. -x [N[,...]] --tabs=[N[,...]]
  1775. Set tab stops.
  1776. -X ........ --no-init
  1777. Don't use termcap init/deinit strings.
  1778. --no-keypad
  1779. Don't use termcap keypad init/deinit strings.
  1780. -y [N] .... --max-forw-scroll=[N]
  1781. Forward scroll limit.
  1782. -z [N] .... --window=[N]
  1783. Set size of window.
  1784. -" [c[c]] . --quotes=[c[c]]
  1785. Set shell quote characters.
  1786. -~ ........ --tilde
  1787. Don't display tildes after end of file.
  1788. -# [N] .... --shift=[N]
  1789. Horizontal scroll amount (0 = one half screen width)
  1790. ---------------------------------------------------------------------------
  1791. LINE EDITING
  1792. These keys can be used to edit text being entered
  1793. on the "command line" at the bottom of the screen.
  1794. RightArrow ESC-l Move cursor right one character.
  1795. LeftArrow ESC-h Move cursor left one character.
  1796. CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word.
  1797. CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word.
  1798. HOME ESC-0 Move cursor to start of line.
  1799. END ESC-$ Move cursor to end of line.
  1800. BACKSPACE Delete char to left of cursor.
  1801. DELETE ESC-x Delete char under cursor.
  1802. CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor.
  1803. CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor.
  1804. CNTL-U ESC (MS-DOS only) Delete entire line.
  1805. UpArrow ESC-k Retrieve previous command line.
  1806. DownArrow ESC-j Retrieve next command line.
  1807. TAB Complete filename & cycle.
  1808. SHIFT-TAB ESC-TAB Complete filename & reverse cycle.
  1809. CNTL-L Complete filename, list all.
  1810. */