diff options
author | Ron Yorston <rmy@pobox.com> | 2018-02-13 09:44:44 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-02-13 09:44:44 +0000 |
commit | dc19a361bd6c6df30338371532691bbc7f7126bb (patch) | |
tree | 1fb2cd646d54b5f8e425c4f11f3e09fc21d1966b /miscutils/less.c | |
parent | 096aee2bb468d1ab044de36e176ed1f6c7e3674d (diff) | |
parent | 3459024bf404af814cacfe90a0deb719e282ae62 (diff) | |
download | busybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.tar.gz busybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.tar.bz2 busybox-w32-dc19a361bd6c6df30338371532691bbc7f7126bb.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'miscutils/less.c')
-rw-r--r-- | miscutils/less.c | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 4ab0e17da..41e4989ad 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_RAW("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() */ |
@@ -161,6 +180,7 @@ enum { | |||
161 | FLAG_TILDE = 1 << 4, | 180 | FLAG_TILDE = 1 << 4, |
162 | FLAG_I = 1 << 5, | 181 | FLAG_I = 1 << 5, |
163 | FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, | 182 | FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, |
183 | FLAG_R = (1 << 7) * ENABLE_FEATURE_LESS_RAW, | ||
164 | /* hijack command line options variable for internal state vars */ | 184 | /* hijack command line options variable for internal state vars */ |
165 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, | 185 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, |
166 | }; | 186 | }; |
@@ -211,6 +231,9 @@ struct globals { | |||
211 | regex_t pattern; | 231 | regex_t pattern; |
212 | smallint pattern_valid; | 232 | smallint pattern_valid; |
213 | #endif | 233 | #endif |
234 | #if ENABLE_FEATURE_LESS_RAW | ||
235 | smallint in_escape; | ||
236 | #endif | ||
214 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL | 237 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL |
215 | smallint winsize_err; | 238 | smallint winsize_err; |
216 | #endif | 239 | #endif |
@@ -259,7 +282,6 @@ struct globals { | |||
259 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | 282 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
260 | less_gets_pos = -1; \ | 283 | less_gets_pos = -1; \ |
261 | empty_line_marker = "~"; \ | 284 | empty_line_marker = "~"; \ |
262 | num_files = 1; \ | ||
263 | current_file = 1; \ | 285 | current_file = 1; \ |
264 | eof_error = 1; \ | 286 | eof_error = 1; \ |
265 | terminated = 1; \ | 287 | terminated = 1; \ |
@@ -518,6 +540,26 @@ static void read_lines(void) | |||
518 | *--p = '\0'; | 540 | *--p = '\0'; |
519 | continue; | 541 | continue; |
520 | } | 542 | } |
543 | #if ENABLE_FEATURE_LESS_RAW | ||
544 | if (option_mask32 & FLAG_R) { | ||
545 | if (c == '\033') | ||
546 | goto discard; | ||
547 | if (G.in_escape) { | ||
548 | if (isdigit(c) | ||
549 | || c == '[' | ||
550 | || c == ';' | ||
551 | || c == 'm' | ||
552 | ) { | ||
553 | discard: | ||
554 | G.in_escape = (c != 'm'); | ||
555 | readpos++; | ||
556 | continue; | ||
557 | } | ||
558 | /* Hmm, unexpected end of "ESC [ N ; N m" sequence */ | ||
559 | G.in_escape = 0; | ||
560 | } | ||
561 | } | ||
562 | #endif | ||
521 | { | 563 | { |
522 | size_t new_last_line_pos = last_line_pos + 1; | 564 | size_t new_last_line_pos = last_line_pos + 1; |
523 | if (c == '\t') { | 565 | if (c == '\t') { |
@@ -1849,11 +1891,29 @@ int less_main(int argc, char **argv) | |||
1849 | * (used by some setups for manpage display) | 1891 | * (used by some setups for manpage display) |
1850 | */ | 1892 | */ |
1851 | getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s"); | 1893 | getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s"); |
1852 | argc -= optind; | ||
1853 | argv += optind; | 1894 | argv += optind; |
1854 | num_files = argc; | 1895 | num_files = argc - optind; |
1855 | files = argv; | 1896 | files = argv; |
1856 | 1897 | ||
1898 | /* Tools typically pass LESS="FRSXMK". | ||
1899 | * The options we don't understand are ignored. */ | ||
1900 | if (ENABLE_FEATURE_LESS_ENV) { | ||
1901 | char *c = getenv("LESS"); | ||
1902 | if (c) while (*c) switch (*c++) { | ||
1903 | case 'M': | ||
1904 | option_mask32 |= FLAG_M; | ||
1905 | break; | ||
1906 | case 'R': | ||
1907 | option_mask32 |= FLAG_R; | ||
1908 | break; | ||
1909 | case 'S': | ||
1910 | option_mask32 |= FLAG_S; | ||
1911 | break; | ||
1912 | default: | ||
1913 | break; | ||
1914 | } | ||
1915 | } | ||
1916 | |||
1857 | /* Another popular pager, most, detects when stdout | 1917 | /* Another popular pager, most, detects when stdout |
1858 | * is not a tty and turns into cat. This makes sense. */ | 1918 | * is not a tty and turns into cat. This makes sense. */ |
1859 | if (!isatty(STDOUT_FILENO)) | 1919 | if (!isatty(STDOUT_FILENO)) |