3
0

less.c 54 KB

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