less.c 55 KB

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