diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-01-22 14:38:51 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-01-22 14:38:51 +0100 |
commit | 8528d3d4f8240ab4715f671aa819fe034f0fc285 (patch) | |
tree | 4505944ecf37bb576d77cfa6681e02205ec22bec | |
parent | e411cd071a2b6456c653f1a7179b83bef810bb72 (diff) | |
download | busybox-w32-8528d3d4f8240ab4715f671aa819fe034f0fc285.tar.gz busybox-w32-8528d3d4f8240ab4715f671aa819fe034f0fc285.tar.bz2 busybox-w32-8528d3d4f8240ab4715f671aa819fe034f0fc285.zip |
less: optional support of -R
Based on patches by Lubomir Rintel <lkundrak@v3.sk>
function old new delta
read_lines 653 722 +69
less_main 2464 2531 +67
packed_usage 32202 32239 +37
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 173/0) Total: 173 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/less.c | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index d60e6d920..c3221a49f 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
@@ -99,6 +99,22 @@ | |||
99 | //config: bool "Enable -N (dynamic switching of line numbers)" | 99 | //config: bool "Enable -N (dynamic switching of line numbers)" |
100 | //config: default y | 100 | //config: default y |
101 | //config: depends on FEATURE_LESS_DASHCMD | 101 | //config: depends on FEATURE_LESS_DASHCMD |
102 | //config: | ||
103 | //config:config FEATURE_LESS_RAW | ||
104 | //config: bool "Enable -R ('raw control characters')" | ||
105 | //config: default y | ||
106 | //config: depends on FEATURE_LESS_DASHCMD | ||
107 | //config: help | ||
108 | //config: This is essential for less applet to work with tools that use colors | ||
109 | //config: and paging, such as git, systemd tools or nmcli. | ||
110 | //config: | ||
111 | //config:config FEATURE_LESS_ENV | ||
112 | //config: bool "Take options from $LESS environment variable" | ||
113 | //config: default y | ||
114 | //config: depends on FEATURE_LESS_DASHCMD | ||
115 | //config: help | ||
116 | //config: This is essential for less applet to work with tools that use colors | ||
117 | //config: and paging, such as git, systemd tools or nmcli. | ||
102 | 118 | ||
103 | //applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP)) | 119 | //applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP)) |
104 | 120 | ||
@@ -106,7 +122,7 @@ | |||
106 | 122 | ||
107 | //usage:#define less_trivial_usage | 123 | //usage:#define less_trivial_usage |
108 | //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") | 124 | //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") |
109 | //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..." | 125 | //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") IF_FEATURE_LESS_TRUNCATE("R") "h~] [FILE]..." |
110 | //usage:#define less_full_usage "\n\n" | 126 | //usage:#define less_full_usage "\n\n" |
111 | //usage: "View FILE (or stdin) one screenful at a time\n" | 127 | //usage: "View FILE (or stdin) one screenful at a time\n" |
112 | //usage: "\n -E Quit once the end of a file is reached" | 128 | //usage: "\n -E Quit once the end of a file is reached" |
@@ -121,6 +137,9 @@ | |||
121 | //usage: IF_FEATURE_LESS_TRUNCATE( | 137 | //usage: IF_FEATURE_LESS_TRUNCATE( |
122 | //usage: "\n -S Truncate long lines" | 138 | //usage: "\n -S Truncate long lines" |
123 | //usage: ) | 139 | //usage: ) |
140 | //usage: IF_FEATURE_LESS_RAW( | ||
141 | //usage: "\n -R Remove color escape codes in input" | ||
142 | //usage: ) | ||
124 | //usage: "\n -~ Suppress ~s displayed past EOF" | 143 | //usage: "\n -~ Suppress ~s displayed past EOF" |
125 | 144 | ||
126 | #include <sched.h> /* sched_yield() */ | 145 | #include <sched.h> /* sched_yield() */ |
@@ -157,6 +176,7 @@ enum { | |||
157 | FLAG_TILDE = 1 << 4, | 176 | FLAG_TILDE = 1 << 4, |
158 | FLAG_I = 1 << 5, | 177 | FLAG_I = 1 << 5, |
159 | FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, | 178 | FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, |
179 | FLAG_R = (1 << 7) * ENABLE_FEATURE_LESS_RAW, | ||
160 | /* hijack command line options variable for internal state vars */ | 180 | /* hijack command line options variable for internal state vars */ |
161 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, | 181 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, |
162 | }; | 182 | }; |
@@ -207,6 +227,9 @@ struct globals { | |||
207 | regex_t pattern; | 227 | regex_t pattern; |
208 | smallint pattern_valid; | 228 | smallint pattern_valid; |
209 | #endif | 229 | #endif |
230 | #if ENABLE_FEATURE_LESS_RAW | ||
231 | smallint in_escape; | ||
232 | #endif | ||
210 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL | 233 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL |
211 | smallint winsize_err; | 234 | smallint winsize_err; |
212 | #endif | 235 | #endif |
@@ -513,6 +536,26 @@ static void read_lines(void) | |||
513 | *--p = '\0'; | 536 | *--p = '\0'; |
514 | continue; | 537 | continue; |
515 | } | 538 | } |
539 | #if ENABLE_FEATURE_LESS_RAW | ||
540 | if (option_mask32 & FLAG_R) { | ||
541 | if (c == '\033') | ||
542 | goto discard; | ||
543 | if (G.in_escape) { | ||
544 | if (isdigit(c) | ||
545 | || c == '[' | ||
546 | || c == ';' | ||
547 | || c == 'm' | ||
548 | ) { | ||
549 | discard: | ||
550 | G.in_escape = (c != 'm'); | ||
551 | readpos++; | ||
552 | continue; | ||
553 | } | ||
554 | /* Hmm, unexpected end of "ESC [ N ; N m" sequence */ | ||
555 | G.in_escape = 0; | ||
556 | } | ||
557 | } | ||
558 | #endif | ||
516 | { | 559 | { |
517 | size_t new_last_line_pos = last_line_pos + 1; | 560 | size_t new_last_line_pos = last_line_pos + 1; |
518 | if (c == '\t') { | 561 | if (c == '\t') { |
@@ -1776,6 +1819,25 @@ int less_main(int argc, char **argv) | |||
1776 | num_files = argc - optind; | 1819 | num_files = argc - optind; |
1777 | files = argv; | 1820 | files = argv; |
1778 | 1821 | ||
1822 | /* Tools typically pass LESS="FRSXMK". | ||
1823 | * The options we don't understand are ignored. */ | ||
1824 | if (ENABLE_FEATURE_LESS_ENV) { | ||
1825 | char *c = getenv("LESS"); | ||
1826 | if (c) while (*c) switch (*c++) { | ||
1827 | case 'M': | ||
1828 | option_mask32 |= FLAG_M; | ||
1829 | break; | ||
1830 | case 'R': | ||
1831 | option_mask32 |= FLAG_R; | ||
1832 | break; | ||
1833 | case 'S': | ||
1834 | option_mask32 |= FLAG_S; | ||
1835 | break; | ||
1836 | default: | ||
1837 | break; | ||
1838 | } | ||
1839 | } | ||
1840 | |||
1779 | /* Another popular pager, most, detects when stdout | 1841 | /* Another popular pager, most, detects when stdout |
1780 | * is not a tty and turns into cat. This makes sense. */ | 1842 | * is not a tty and turns into cat. This makes sense. */ |
1781 | if (!isatty(STDOUT_FILENO)) | 1843 | if (!isatty(STDOUT_FILENO)) |