3
0

less.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. /*
  10. * This program needs a lot of development, so consider it in a beta stage
  11. * at best.
  12. *
  13. * TODO:
  14. * - Add more regular expression support - search modifiers, certain matches, etc.
  15. * - Add more complex bracket searching - currently, nested brackets are
  16. * not considered.
  17. * - Add support for "F" as an input. This causes less to act in
  18. * a similar way to tail -f.
  19. * - Check for binary files, and prompt the user if a binary file
  20. * is detected.
  21. * - Allow horizontal scrolling. Currently, lines simply continue onto
  22. * the next line, per the terminal's discretion
  23. *
  24. * Notes:
  25. * - filename is an array and not a pointer because that avoids all sorts
  26. * of complications involving the fact that something that is pointed to
  27. * will be changed if the pointer is changed.
  28. * - the inp file pointer is used so that keyboard input works after
  29. * redirected input has been read from stdin
  30. */
  31. #include "busybox.h"
  32. #ifdef CONFIG_FEATURE_LESS_REGEXP
  33. #include "xregex.h"
  34. #endif
  35. /* These are the escape sequences corresponding to special keys */
  36. #define REAL_KEY_UP 'A'
  37. #define REAL_KEY_DOWN 'B'
  38. #define REAL_KEY_RIGHT 'C'
  39. #define REAL_KEY_LEFT 'D'
  40. #define REAL_PAGE_UP '5'
  41. #define REAL_PAGE_DOWN '6'
  42. #define REAL_KEY_HOME '7'
  43. #define REAL_KEY_END '8'
  44. /* These are the special codes assigned by this program to the special keys */
  45. #define KEY_UP 20
  46. #define KEY_DOWN 21
  47. #define KEY_RIGHT 22
  48. #define KEY_LEFT 23
  49. #define PAGE_UP 24
  50. #define PAGE_DOWN 25
  51. #define KEY_HOME 26
  52. #define KEY_END 27
  53. /* The escape codes for highlighted and normal text */
  54. #define HIGHLIGHT "\033[7m"
  55. #define NORMAL "\033[0m"
  56. /* The escape code to clear the screen */
  57. #define CLEAR "\033[H\033[J"
  58. /* Maximum number of lines in a file */
  59. #define MAXLINES 10000
  60. static int height;
  61. static int width;
  62. static char **files;
  63. static char filename[256];
  64. static char **buffer;
  65. static char **flines;
  66. static int current_file = 1;
  67. static int line_pos;
  68. static int num_flines;
  69. static int num_files = 1;
  70. /* Command line options */
  71. static unsigned flags;
  72. #define FLAG_E 1
  73. #define FLAG_M (1<<1)
  74. #define FLAG_m (1<<2)
  75. #define FLAG_N (1<<3)
  76. #define FLAG_TILDE (1<<4)
  77. /* hijack command line options variable for internal state vars */
  78. #define LESS_STATE_INP_STDIN (1<<5)
  79. #define LESS_STATE_PAST_EOF (1<<6)
  80. #define LESS_STATE_MATCH_BACKWARDS (1<<7)
  81. /* INP_STDIN is used to change behaviour when input comes from stdin */
  82. #ifdef CONFIG_FEATURE_LESS_MARKS
  83. static int mark_lines[15][2];
  84. static int num_marks;
  85. #endif
  86. #ifdef CONFIG_FEATURE_LESS_REGEXP
  87. static int match_found;
  88. static int *match_lines;
  89. static int match_pos;
  90. static int num_matches;
  91. static regex_t old_pattern;
  92. #endif
  93. /* Needed termios structures */
  94. static struct termios term_orig, term_vi;
  95. /* File pointer to get input from */
  96. static FILE *inp;
  97. /* Reset terminal input to normal */
  98. static void set_tty_cooked(void)
  99. {
  100. fflush(stdout);
  101. tcsetattr(fileno(inp), TCSANOW, &term_orig);
  102. }
  103. /* Exit the program gracefully */
  104. static void tless_exit(int code)
  105. {
  106. /* TODO: We really should save the terminal state when we start,
  107. and restore it when we exit. Less does this with the
  108. "ti" and "te" termcap commands; can this be done with
  109. only termios.h? */
  110. putchar('\n');
  111. fflush_stdout_and_exit(code);
  112. }
  113. /* Grab a character from input without requiring the return key. If the
  114. character is ASCII \033, get more characters and assign certain sequences
  115. special return codes. Note that this function works best with raw input. */
  116. static int tless_getch(void)
  117. {
  118. int input;
  119. /* Set terminal input to raw mode (taken from vi.c) */
  120. tcsetattr(fileno(inp), TCSANOW, &term_vi);
  121. input = getc(inp);
  122. /* Detect escape sequences (i.e. arrow keys) and handle
  123. them accordingly */
  124. if (input == '\033' && getc(inp) == '[') {
  125. unsigned int i;
  126. input = getc(inp);
  127. set_tty_cooked();
  128. i = input - REAL_KEY_UP;
  129. if (i < 4)
  130. return 20 + i;
  131. else if ((i = input - REAL_PAGE_UP) < 4)
  132. return 24 + i;
  133. }
  134. /* The input is a normal ASCII value */
  135. else {
  136. set_tty_cooked();
  137. return input;
  138. }
  139. return 0;
  140. }
  141. /* Move the cursor to a position (x,y), where (0,0) is the
  142. top-left corner of the console */
  143. static void move_cursor(int x, int y)
  144. {
  145. printf("\033[%i;%iH", x, y);
  146. }
  147. static void clear_line(void)
  148. {
  149. move_cursor(height, 0);
  150. printf("\033[K");
  151. }
  152. /* This adds line numbers to every line, as the -N flag necessitates */
  153. static void add_linenumbers(void)
  154. {
  155. int i;
  156. for (i = 0; i <= num_flines; i++) {
  157. char *new = xasprintf("%5d %s", i + 1, flines[i]);
  158. free(flines[i]);
  159. flines[i] = new;
  160. }
  161. }
  162. static void data_readlines(void)
  163. {
  164. int i;
  165. char current_line[256];
  166. FILE *fp;
  167. fp = (flags & LESS_STATE_INP_STDIN) ? stdin : xfopen(filename, "r");
  168. flines = NULL;
  169. for (i = 0; (feof(fp)==0) && (i <= MAXLINES); i++) {
  170. strcpy(current_line, "");
  171. fgets(current_line, 256, fp);
  172. if (fp != stdin)
  173. die_if_ferror(fp, filename);
  174. flines = xrealloc(flines, (i+1) * sizeof(char *));
  175. flines[i] = xstrdup(current_line);
  176. }
  177. num_flines = i - 2;
  178. /* Reset variables for a new file */
  179. line_pos = 0;
  180. flags &= ~LESS_STATE_PAST_EOF;
  181. fclose(fp);
  182. if (inp == NULL)
  183. inp = (flags & LESS_STATE_INP_STDIN) ? xfopen(CURRENT_TTY, "r") : stdin;
  184. if (flags & FLAG_N)
  185. add_linenumbers();
  186. }
  187. #ifdef CONFIG_FEATURE_LESS_FLAGS
  188. /* Interestingly, writing calc_percent as a function and not a prototype saves around 32 bytes
  189. * on my build. */
  190. static int calc_percent(void)
  191. {
  192. return ((100 * (line_pos + height - 2) / num_flines) + 1);
  193. }
  194. /* Print a status line if -M was specified */
  195. static void m_status_print(void)
  196. {
  197. int percentage;
  198. if (!(flags & LESS_STATE_PAST_EOF)) {
  199. if (!line_pos) {
  200. if (num_files > 1)
  201. printf("%s%s %s%i%s%i%s%i-%i/%i ", HIGHLIGHT,
  202. filename, "(file ", current_file, " of ", num_files, ") lines ",
  203. line_pos + 1, line_pos + height - 1, num_flines + 1);
  204. else {
  205. printf("%s%s lines %i-%i/%i ", HIGHLIGHT,
  206. filename, line_pos + 1, line_pos + height - 1,
  207. num_flines + 1);
  208. }
  209. }
  210. else {
  211. printf("%s %s lines %i-%i/%i ", HIGHLIGHT, filename,
  212. line_pos + 1, line_pos + height - 1, num_flines + 1);
  213. }
  214. if (line_pos == num_flines - height + 2) {
  215. printf("(END) %s", NORMAL);
  216. if ((num_files > 1) && (current_file != num_files))
  217. printf("%s- Next: %s%s", HIGHLIGHT, files[current_file], NORMAL);
  218. }
  219. else {
  220. percentage = calc_percent();
  221. printf("%i%% %s", percentage, NORMAL);
  222. }
  223. }
  224. else {
  225. printf("%s%s lines %i-%i/%i (END) ", HIGHLIGHT, filename,
  226. line_pos + 1, num_flines + 1, num_flines + 1);
  227. if ((num_files > 1) && (current_file != num_files))
  228. printf("- Next: %s", files[current_file]);
  229. printf("%s", NORMAL);
  230. }
  231. }
  232. /* Print a status line if -m was specified */
  233. static void medium_status_print(void)
  234. {
  235. int percentage;
  236. percentage = calc_percent();
  237. if (!line_pos)
  238. printf("%s%s %i%%%s", HIGHLIGHT, filename, percentage, NORMAL);
  239. else if (line_pos == num_flines - height + 2)
  240. printf("%s(END)%s", HIGHLIGHT, NORMAL);
  241. else
  242. printf("%s%i%%%s", HIGHLIGHT, percentage, NORMAL);
  243. }
  244. #endif
  245. /* Print the status line */
  246. static void status_print(void)
  247. {
  248. /* Change the status if flags have been set */
  249. #ifdef CONFIG_FEATURE_LESS_FLAGS
  250. if (flags & FLAG_M)
  251. m_status_print();
  252. else if (flags & FLAG_m)
  253. medium_status_print();
  254. /* No flags set */
  255. else {
  256. #endif
  257. if (!line_pos) {
  258. printf("%s%s %s", HIGHLIGHT, filename, NORMAL);
  259. if (num_files > 1)
  260. printf("%s%s%i%s%i%s%s", HIGHLIGHT, "(file ",
  261. current_file, " of ", num_files, ")", NORMAL);
  262. }
  263. else if (line_pos == num_flines - height + 2) {
  264. printf("%s%s %s", HIGHLIGHT, "(END)", NORMAL);
  265. if ((num_files > 1) && (current_file != num_files))
  266. printf("%s%s%s%s", HIGHLIGHT, "- Next: ", files[current_file], NORMAL);
  267. }
  268. else {
  269. putchar(':');
  270. }
  271. #ifdef CONFIG_FEATURE_LESS_FLAGS
  272. }
  273. #endif
  274. }
  275. /* Print the buffer */
  276. static void buffer_print(void)
  277. {
  278. int i;
  279. printf("%s", CLEAR);
  280. if (num_flines >= height - 2) {
  281. for (i = 0; i < height - 1; i++)
  282. printf("%s", buffer[i]);
  283. }
  284. else {
  285. for (i = 1; i < (height - 1 - num_flines); i++)
  286. putchar('\n');
  287. for (i = 0; i < height - 1; i++)
  288. printf("%s", buffer[i]);
  289. }
  290. status_print();
  291. }
  292. /* Initialise the buffer */
  293. static void buffer_init(void)
  294. {
  295. int i;
  296. if (buffer == NULL) {
  297. /* malloc the number of lines needed for the buffer */
  298. buffer = xrealloc(buffer, height * sizeof(char *));
  299. } else {
  300. for (i = 0; i < (height - 1); i++)
  301. free(buffer[i]);
  302. }
  303. /* Fill the buffer until the end of the file or the
  304. end of the buffer is reached */
  305. for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) {
  306. buffer[i] = xstrdup(flines[i]);
  307. }
  308. /* If the buffer still isn't full, fill it with blank lines */
  309. for (; i < (height - 1); i++) {
  310. buffer[i] = xstrdup("");
  311. }
  312. }
  313. /* Move the buffer up and down in the file in order to scroll */
  314. static void buffer_down(int nlines)
  315. {
  316. int i;
  317. if (!(flags & LESS_STATE_PAST_EOF)) {
  318. if (line_pos + (height - 3) + nlines < num_flines) {
  319. line_pos += nlines;
  320. for (i = 0; i < (height - 1); i++) {
  321. free(buffer[i]);
  322. buffer[i] = xstrdup(flines[line_pos + i]);
  323. }
  324. }
  325. else {
  326. /* As the number of lines requested was too large, we just move
  327. to the end of the file */
  328. while (line_pos + (height - 3) + 1 < num_flines) {
  329. line_pos += 1;
  330. for (i = 0; i < (height - 1); i++) {
  331. free(buffer[i]);
  332. buffer[i] = xstrdup(flines[line_pos + i]);
  333. }
  334. }
  335. }
  336. /* We exit if the -E flag has been set */
  337. if ((flags & FLAG_E) && (line_pos + (height - 2) == num_flines))
  338. tless_exit(0);
  339. }
  340. }
  341. static void buffer_up(int nlines)
  342. {
  343. int i;
  344. int tilde_line;
  345. if (!(flags & LESS_STATE_PAST_EOF)) {
  346. if (line_pos - nlines >= 0) {
  347. line_pos -= nlines;
  348. for (i = 0; i < (height - 1); i++) {
  349. free(buffer[i]);
  350. buffer[i] = xstrdup(flines[line_pos + i]);
  351. }
  352. }
  353. else {
  354. /* As the requested number of lines to move was too large, we
  355. move one line up at a time until we can't. */
  356. while (line_pos != 0) {
  357. line_pos -= 1;
  358. for (i = 0; i < (height - 1); i++) {
  359. free(buffer[i]);
  360. buffer[i] = xstrdup(flines[line_pos + i]);
  361. }
  362. }
  363. }
  364. }
  365. else {
  366. /* Work out where the tildes start */
  367. tilde_line = num_flines - line_pos + 3;
  368. line_pos -= nlines;
  369. /* Going backwards nlines lines has taken us to a point where
  370. nothing is past the EOF, so we revert to normal. */
  371. if (line_pos < num_flines - height + 3) {
  372. flags &= ~LESS_STATE_PAST_EOF;
  373. buffer_up(nlines);
  374. }
  375. else {
  376. /* We only move part of the buffer, as the rest
  377. is past the EOF */
  378. for (i = 0; i < (height - 1); i++) {
  379. free(buffer[i]);
  380. if (i < tilde_line - nlines + 1)
  381. buffer[i] = xstrdup(flines[line_pos + i]);
  382. else {
  383. if (line_pos >= num_flines - height + 2)
  384. buffer[i] = xstrdup("~\n");
  385. }
  386. }
  387. }
  388. }
  389. }
  390. static void buffer_line(int linenum)
  391. {
  392. int i;
  393. flags &= ~LESS_STATE_PAST_EOF;
  394. if (linenum < 0 || linenum > num_flines) {
  395. clear_line();
  396. printf("%s%s%i%s", HIGHLIGHT, "Cannot seek to line number ", linenum + 1, NORMAL);
  397. }
  398. else if (linenum < (num_flines - height - 2)) {
  399. for (i = 0; i < (height - 1); i++) {
  400. free(buffer[i]);
  401. buffer[i] = xstrdup(flines[linenum + i]);
  402. }
  403. line_pos = linenum;
  404. buffer_print();
  405. }
  406. else {
  407. for (i = 0; i < (height - 1); i++) {
  408. free(buffer[i]);
  409. if (linenum + i < num_flines + 2)
  410. buffer[i] = xstrdup(flines[linenum + i]);
  411. else
  412. buffer[i] = xstrdup((flags & FLAG_TILDE) ? "\n" : "~\n");
  413. }
  414. line_pos = linenum;
  415. /* Set past_eof so buffer_down and buffer_up act differently */
  416. flags |= LESS_STATE_PAST_EOF;
  417. buffer_print();
  418. }
  419. }
  420. /* Reinitialise everything for a new file - free the memory and start over */
  421. static void reinitialise(void)
  422. {
  423. int i;
  424. for (i = 0; i <= num_flines; i++)
  425. free(flines[i]);
  426. free(flines);
  427. data_readlines();
  428. buffer_init();
  429. buffer_print();
  430. }
  431. static void examine_file(void)
  432. {
  433. int newline_offset;
  434. clear_line();
  435. printf("Examine: ");
  436. fgets(filename, 256, inp);
  437. /* As fgets adds a newline to the end of an input string, we
  438. need to remove it */
  439. newline_offset = strlen(filename) - 1;
  440. filename[newline_offset] = '\0';
  441. files[num_files] = xstrdup(filename);
  442. current_file = num_files + 1;
  443. num_files++;
  444. flags &= ~LESS_STATE_INP_STDIN;
  445. reinitialise();
  446. }
  447. /* This function changes the file currently being paged. direction can be one of the following:
  448. * -1: go back one file
  449. * 0: go to the first file
  450. * 1: go forward one file
  451. */
  452. static void change_file(int direction)
  453. {
  454. if (current_file != ((direction > 0) ? num_files : 1)) {
  455. current_file = direction ? current_file + direction : 1;
  456. strcpy(filename, files[current_file - 1]);
  457. reinitialise();
  458. }
  459. else {
  460. clear_line();
  461. printf("%s%s%s", HIGHLIGHT, (direction > 0) ? "No next file" : "No previous file", NORMAL);
  462. }
  463. }
  464. static void remove_current_file(void)
  465. {
  466. int i;
  467. if (current_file != 1) {
  468. change_file(-1);
  469. for (i = 3; i <= num_files; i++)
  470. files[i - 2] = files[i - 1];
  471. num_files--;
  472. buffer_print();
  473. }
  474. else {
  475. change_file(1);
  476. for (i = 2; i <= num_files; i++)
  477. files[i - 2] = files[i - 1];
  478. num_files--;
  479. current_file--;
  480. buffer_print();
  481. }
  482. }
  483. static void colon_process(void)
  484. {
  485. int keypress;
  486. /* Clear the current line and print a prompt */
  487. clear_line();
  488. printf(" :");
  489. keypress = tless_getch();
  490. switch (keypress) {
  491. case 'd':
  492. remove_current_file();
  493. break;
  494. case 'e':
  495. examine_file();
  496. break;
  497. #ifdef CONFIG_FEATURE_LESS_FLAGS
  498. case 'f':
  499. clear_line();
  500. m_status_print();
  501. break;
  502. #endif
  503. case 'n':
  504. change_file(1);
  505. break;
  506. case 'p':
  507. change_file(-1);
  508. break;
  509. case 'q':
  510. tless_exit(0);
  511. break;
  512. case 'x':
  513. change_file(0);
  514. break;
  515. default:
  516. break;
  517. }
  518. }
  519. #ifdef CONFIG_FEATURE_LESS_REGEXP
  520. /* The below two regular expression handler functions NEED development. */
  521. /* Get a regular expression from the user, and then go through the current
  522. file line by line, running a processing regex function on each one. */
  523. static char *process_regex_on_line(char *line, regex_t *pattern, int action)
  524. {
  525. /* This function takes the regex and applies it to the line.
  526. Each part of the line that matches has the HIGHLIGHT
  527. and NORMAL escape sequences placed around it by
  528. insert_highlights if action = 1, or has the escape sequences
  529. removed if action = 0, and then the line is returned. */
  530. int match_status;
  531. char *line2 = xmalloc((sizeof(char) * (strlen(line) + 1)) + 64);
  532. char *growline = "";
  533. regmatch_t match_structs;
  534. line2 = xstrdup(line);
  535. match_found = 0;
  536. match_status = regexec(pattern, line2, 1, &match_structs, 0);
  537. while (match_status == 0) {
  538. if (match_found == 0)
  539. match_found = 1;
  540. if (action) {
  541. growline = xasprintf("%s%.*s%s%.*s%s", growline,
  542. match_structs.rm_so, line2, HIGHLIGHT,
  543. match_structs.rm_eo - match_structs.rm_so,
  544. line2 + match_structs.rm_so, NORMAL);
  545. }
  546. else {
  547. growline = xasprintf("%s%.*s%.*s", growline,
  548. match_structs.rm_so - 4, line2,
  549. match_structs.rm_eo - match_structs.rm_so,
  550. line2 + match_structs.rm_so);
  551. }
  552. line2 += match_structs.rm_eo;
  553. match_status = regexec(pattern, line2, 1, &match_structs, REG_NOTBOL);
  554. }
  555. growline = xasprintf("%s%s", growline, line2);
  556. return (match_found ? growline : line);
  557. free(growline);
  558. free(line2);
  559. }
  560. static void goto_match(int match)
  561. {
  562. /* This goes to a specific match - all line positions of matches are
  563. stored within the match_lines[] array. */
  564. if ((match < num_matches) && (match >= 0)) {
  565. buffer_line(match_lines[match]);
  566. match_pos = match;
  567. }
  568. }
  569. static void regex_process(void)
  570. {
  571. char uncomp_regex[100];
  572. char *current_line;
  573. int i;
  574. int j = 0;
  575. regex_t pattern;
  576. /* Get the uncompiled regular expression from the user */
  577. clear_line();
  578. putchar((flags & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/');
  579. uncomp_regex[0] = 0;
  580. fgets(uncomp_regex, sizeof(uncomp_regex), inp);
  581. if (strlen(uncomp_regex) == 1) {
  582. if (num_matches)
  583. goto_match((flags & LESS_STATE_MATCH_BACKWARDS)
  584. ? match_pos - 1 : match_pos + 1);
  585. else
  586. buffer_print();
  587. return;
  588. }
  589. uncomp_regex[strlen(uncomp_regex) - 1] = '\0';
  590. /* Compile the regex and check for errors */
  591. xregcomp(&pattern, uncomp_regex, 0);
  592. if (num_matches) {
  593. /* Get rid of all the highlights we added previously */
  594. for (i = 0; i <= num_flines; i++) {
  595. current_line = process_regex_on_line(flines[i], &old_pattern, 0);
  596. flines[i] = xstrdup(current_line);
  597. }
  598. }
  599. old_pattern = pattern;
  600. /* Reset variables */
  601. match_lines = xrealloc(match_lines, sizeof(int));
  602. match_lines[0] = -1;
  603. match_pos = 0;
  604. num_matches = 0;
  605. match_found = 0;
  606. /* Run the regex on each line of the current file here */
  607. for (i = 0; i <= num_flines; i++) {
  608. current_line = process_regex_on_line(flines[i], &pattern, 1);
  609. flines[i] = xstrdup(current_line);
  610. if (match_found) {
  611. match_lines = xrealloc(match_lines, (j + 1) * sizeof(int));
  612. match_lines[j] = i;
  613. j++;
  614. }
  615. }
  616. num_matches = j;
  617. if ((match_lines[0] != -1) && (num_flines > height - 2)) {
  618. if (flags & LESS_STATE_MATCH_BACKWARDS) {
  619. for (i = 0; i < num_matches; i++) {
  620. if (match_lines[i] > line_pos) {
  621. match_pos = i - 1;
  622. buffer_line(match_lines[match_pos]);
  623. break;
  624. }
  625. }
  626. }
  627. else
  628. buffer_line(match_lines[0]);
  629. }
  630. else
  631. buffer_init();
  632. }
  633. #endif
  634. static void number_process(int first_digit)
  635. {
  636. int i = 1;
  637. int num;
  638. char num_input[80];
  639. char keypress;
  640. char *endptr;
  641. num_input[0] = first_digit;
  642. /* Clear the current line, print a prompt, and then print the digit */
  643. clear_line();
  644. printf(":%c", first_digit);
  645. /* Receive input until a letter is given (max 80 chars)*/
  646. while((i < 80) && (num_input[i] = tless_getch()) && isdigit(num_input[i])) {
  647. putchar(num_input[i]);
  648. i++;
  649. }
  650. /* Take the final letter out of the digits string */
  651. keypress = num_input[i];
  652. num_input[i] = '\0';
  653. num = strtol(num_input, &endptr, 10);
  654. if (endptr==num_input || *endptr!='\0' || num < 1 || num > MAXLINES) {
  655. buffer_print();
  656. return;
  657. }
  658. /* We now know the number and the letter entered, so we process them */
  659. switch (keypress) {
  660. case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
  661. buffer_down(num);
  662. break;
  663. case KEY_UP: case 'b': case 'w': case 'y': case 'u':
  664. buffer_up(num);
  665. break;
  666. case 'g': case '<': case 'G': case '>':
  667. if (num_flines >= height - 2)
  668. buffer_line(num - 1);
  669. break;
  670. case 'p': case '%':
  671. buffer_line(((num / 100) * num_flines) - 1);
  672. break;
  673. #ifdef CONFIG_FEATURE_LESS_REGEXP
  674. case 'n':
  675. goto_match(match_pos + num);
  676. break;
  677. case '/':
  678. flags &= ~LESS_STATE_MATCH_BACKWARDS;
  679. regex_process();
  680. break;
  681. case '?':
  682. flags |= LESS_STATE_MATCH_BACKWARDS;
  683. regex_process();
  684. break;
  685. #endif
  686. default:
  687. break;
  688. }
  689. }
  690. #ifdef CONFIG_FEATURE_LESS_FLAGCS
  691. static void flag_change(void)
  692. {
  693. int keypress;
  694. clear_line();
  695. putchar('-');
  696. keypress = tless_getch();
  697. switch (keypress) {
  698. case 'M':
  699. flags ^= FLAG_M;
  700. break;
  701. case 'm':
  702. flags ^= FLAG_m;
  703. break;
  704. case 'E':
  705. flags ^= FLAG_E;
  706. break;
  707. case '~':
  708. flags ^= FLAG_TILDE;
  709. break;
  710. default:
  711. break;
  712. }
  713. }
  714. static void show_flag_status(void)
  715. {
  716. int keypress;
  717. int flag_val;
  718. clear_line();
  719. putchar('_');
  720. keypress = tless_getch();
  721. switch (keypress) {
  722. case 'M':
  723. flag_val = flags & FLAG_M;
  724. break;
  725. case 'm':
  726. flag_val = flags & FLAG_m;
  727. break;
  728. case '~':
  729. flag_val = flags & FLAG_TILDE;
  730. break;
  731. case 'N':
  732. flag_val = flags & FLAG_N;
  733. break;
  734. case 'E':
  735. flag_val = flags & FLAG_E;
  736. break;
  737. default:
  738. flag_val = 0;
  739. break;
  740. }
  741. clear_line();
  742. printf("%s%s%i%s", HIGHLIGHT, "The status of the flag is: ", flag_val != 0, NORMAL);
  743. }
  744. #endif
  745. static void full_repaint(void)
  746. {
  747. int temp_line_pos = line_pos;
  748. data_readlines();
  749. buffer_init();
  750. buffer_line(temp_line_pos);
  751. }
  752. static void save_input_to_file(void)
  753. {
  754. char current_line[256];
  755. int i;
  756. FILE *fp;
  757. clear_line();
  758. printf("Log file: ");
  759. fgets(current_line, 256, inp);
  760. current_line[strlen(current_line) - 1] = '\0';
  761. if (strlen(current_line) > 1) {
  762. fp = xfopen(current_line, "w");
  763. for (i = 0; i < num_flines; i++)
  764. fprintf(fp, "%s", flines[i]);
  765. fclose(fp);
  766. buffer_print();
  767. }
  768. else
  769. printf("%s%s%s", HIGHLIGHT, "No log file", NORMAL);
  770. }
  771. #ifdef CONFIG_FEATURE_LESS_MARKS
  772. static void add_mark(void)
  773. {
  774. int letter;
  775. clear_line();
  776. printf("Mark: ");
  777. letter = tless_getch();
  778. if (isalpha(letter)) {
  779. /* If we exceed 15 marks, start overwriting previous ones */
  780. if (num_marks == 14)
  781. num_marks = 0;
  782. mark_lines[num_marks][0] = letter;
  783. mark_lines[num_marks][1] = line_pos;
  784. num_marks++;
  785. }
  786. else {
  787. clear_line();
  788. printf("%s%s%s", HIGHLIGHT, "Invalid mark letter", NORMAL);
  789. }
  790. }
  791. static void goto_mark(void)
  792. {
  793. int letter;
  794. int i;
  795. clear_line();
  796. printf("Go to mark: ");
  797. letter = tless_getch();
  798. clear_line();
  799. if (isalpha(letter)) {
  800. for (i = 0; i <= num_marks; i++)
  801. if (letter == mark_lines[i][0]) {
  802. buffer_line(mark_lines[i][1]);
  803. break;
  804. }
  805. if ((num_marks == 14) && (letter != mark_lines[14][0]))
  806. printf("%s%s%s", HIGHLIGHT, "Mark not set", NORMAL);
  807. }
  808. else
  809. printf("%s%s%s", HIGHLIGHT, "Invalid mark letter", NORMAL);
  810. }
  811. #endif
  812. #ifdef CONFIG_FEATURE_LESS_BRACKETS
  813. static char opp_bracket(char bracket)
  814. {
  815. switch (bracket) {
  816. case '{': case '[':
  817. return bracket + 2;
  818. case '(':
  819. return ')';
  820. case '}': case ']':
  821. return bracket - 2;
  822. case ')':
  823. return '(';
  824. default:
  825. return 0;
  826. }
  827. }
  828. static void match_right_bracket(char bracket)
  829. {
  830. int bracket_line = -1;
  831. int i;
  832. clear_line();
  833. if (strchr(flines[line_pos], bracket) == NULL)
  834. printf("%s%s%s", HIGHLIGHT, "No bracket in top line", NORMAL);
  835. else {
  836. for (i = line_pos + 1; i < num_flines; i++) {
  837. if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
  838. bracket_line = i;
  839. break;
  840. }
  841. }
  842. if (bracket_line == -1)
  843. printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL);
  844. buffer_line(bracket_line - height + 2);
  845. }
  846. }
  847. static void match_left_bracket(char bracket)
  848. {
  849. int bracket_line = -1;
  850. int i;
  851. clear_line();
  852. if (strchr(flines[line_pos + height - 2], bracket) == NULL) {
  853. printf("%s%s%s", HIGHLIGHT, "No bracket in bottom line", NORMAL);
  854. printf("%s", flines[line_pos + height]);
  855. sleep(4);
  856. }
  857. else {
  858. for (i = line_pos + height - 2; i >= 0; i--) {
  859. if (strchr(flines[i], opp_bracket(bracket)) != NULL) {
  860. bracket_line = i;
  861. break;
  862. }
  863. }
  864. if (bracket_line == -1)
  865. printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL);
  866. buffer_line(bracket_line);
  867. }
  868. }
  869. #endif /* CONFIG_FEATURE_LESS_BRACKETS */
  870. static void keypress_process(int keypress)
  871. {
  872. switch (keypress) {
  873. case KEY_DOWN: case 'e': case 'j': case '\015':
  874. buffer_down(1);
  875. buffer_print();
  876. break;
  877. case KEY_UP: case 'y': case 'k':
  878. buffer_up(1);
  879. buffer_print();
  880. break;
  881. case PAGE_DOWN: case ' ': case 'z':
  882. buffer_down(height - 1);
  883. buffer_print();
  884. break;
  885. case PAGE_UP: case 'w': case 'b':
  886. buffer_up(height - 1);
  887. buffer_print();
  888. break;
  889. case 'd':
  890. buffer_down((height - 1) / 2);
  891. buffer_print();
  892. break;
  893. case 'u':
  894. buffer_up((height - 1) / 2);
  895. buffer_print();
  896. break;
  897. case KEY_HOME: case 'g': case 'p': case '<': case '%':
  898. buffer_line(0);
  899. break;
  900. case KEY_END: case 'G': case '>':
  901. buffer_line(num_flines - height + 2);
  902. break;
  903. case 'q': case 'Q':
  904. tless_exit(0);
  905. break;
  906. #ifdef CONFIG_FEATURE_LESS_MARKS
  907. case 'm':
  908. add_mark();
  909. buffer_print();
  910. break;
  911. case '\'':
  912. goto_mark();
  913. buffer_print();
  914. break;
  915. #endif
  916. case 'r':
  917. buffer_print();
  918. break;
  919. case 'R':
  920. full_repaint();
  921. break;
  922. case 's':
  923. if (flags & LESS_STATE_INP_STDIN)
  924. save_input_to_file();
  925. break;
  926. case 'E':
  927. examine_file();
  928. break;
  929. #ifdef CONFIG_FEATURE_LESS_FLAGS
  930. case '=':
  931. clear_line();
  932. m_status_print();
  933. break;
  934. #endif
  935. #ifdef CONFIG_FEATURE_LESS_REGEXP
  936. case '/':
  937. flags &= ~LESS_STATE_MATCH_BACKWARDS;
  938. regex_process();
  939. break;
  940. case 'n':
  941. goto_match(match_pos + 1);
  942. break;
  943. case 'N':
  944. goto_match(match_pos - 1);
  945. break;
  946. case '?':
  947. flags |= LESS_STATE_MATCH_BACKWARDS;
  948. regex_process();
  949. break;
  950. #endif
  951. #ifdef CONFIG_FEATURE_LESS_FLAGCS
  952. case '-':
  953. flag_change();
  954. buffer_print();
  955. break;
  956. case '_':
  957. show_flag_status();
  958. break;
  959. #endif
  960. #ifdef CONFIG_FEATURE_LESS_BRACKETS
  961. case '{': case '(': case '[':
  962. match_right_bracket(keypress);
  963. break;
  964. case '}': case ')': case ']':
  965. match_left_bracket(keypress);
  966. break;
  967. #endif
  968. case ':':
  969. colon_process();
  970. break;
  971. default:
  972. break;
  973. }
  974. if (isdigit(keypress))
  975. number_process(keypress);
  976. }
  977. int less_main(int argc, char **argv) {
  978. int keypress;
  979. flags = getopt32(argc, argv, "EMmN~");
  980. argc -= optind;
  981. argv += optind;
  982. files = argv;
  983. num_files = argc;
  984. if (!num_files) {
  985. if (ttyname(STDIN_FILENO) == NULL)
  986. flags |= LESS_STATE_INP_STDIN;
  987. else {
  988. bb_error_msg("missing filename");
  989. bb_show_usage();
  990. }
  991. }
  992. strcpy(filename, (flags & LESS_STATE_INP_STDIN) ? bb_msg_standard_input : files[0]);
  993. get_terminal_width_height(0, &width, &height);
  994. data_readlines();
  995. tcgetattr(fileno(inp), &term_orig);
  996. term_vi = term_orig;
  997. term_vi.c_lflag &= (~ICANON & ~ECHO);
  998. term_vi.c_iflag &= (~IXON & ~ICRNL);
  999. term_vi.c_oflag &= (~ONLCR);
  1000. term_vi.c_cc[VMIN] = 1;
  1001. term_vi.c_cc[VTIME] = 0;
  1002. buffer_init();
  1003. buffer_print();
  1004. while (1) {
  1005. keypress = tless_getch();
  1006. keypress_process(keypress);
  1007. }
  1008. }