less.c 54 KB

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