less.c 55 KB

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