diff options
author | Ron Yorston <rmy@pobox.com> | 2023-02-06 10:50:58 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-02-06 11:15:37 +0000 |
commit | c49b8b7052bb1df3ed5f9aeba61a7965d9c77f83 (patch) | |
tree | 4261e2a8be0c3fa8a2c0f493026cf27ff1038fed | |
parent | c5fe5ad712e11e4d06d5becab5f888531f55a863 (diff) | |
download | busybox-w32-c49b8b7052bb1df3ed5f9aeba61a7965d9c77f83.tar.gz busybox-w32-c49b8b7052bb1df3ed5f9aeba61a7965d9c77f83.tar.bz2 busybox-w32-c49b8b7052bb1df3ed5f9aeba61a7965d9c77f83.zip |
vi: introduce 'binary' option and '-b' flag
vim has the 'binary' option and corresponding '-b' command line
flag. These allow files to be opened in binary mode, ignoring
the 'fileformats' setting and treating all files as of type
'unix'.
Add these to busybox-w32 vi so it's possible to edit files which
have a mixture of different line endings.
Costs 80-112 bytes.
(GitHub issue #285)
-rw-r--r-- | editors/vi.c | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/editors/vi.c b/editors/vi.c index d6cfeae0e..13f0ae8ee 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -119,7 +119,11 @@ | |||
119 | //config: help | 119 | //config: help |
120 | //config: Enable the editor to detect the format of files it reads | 120 | //config: Enable the editor to detect the format of files it reads |
121 | //config: so they can be written out with the appropriate line | 121 | //config: so they can be written out with the appropriate line |
122 | //config: endings. Enable the 'fileformat' and 'fileformats' options. | 122 | //config: endings. Also allow files to be edited in binary mode. |
123 | //config: | ||
124 | //config: This enables the 'fileformat', 'fileformats' and 'binary' | ||
125 | //config: options and the '-b' command line flag. | ||
126 | //config: | ||
123 | //config: | 127 | //config: |
124 | //config:config FEATURE_VI_SET | 128 | //config:config FEATURE_VI_SET |
125 | //config: bool "Support :set" | 129 | //config: bool "Support :set" |
@@ -190,9 +194,12 @@ | |||
190 | //kbuild:lib-$(CONFIG_VI) += vi.o | 194 | //kbuild:lib-$(CONFIG_VI) += vi.o |
191 | 195 | ||
192 | //usage:#define vi_trivial_usage | 196 | //usage:#define vi_trivial_usage |
193 | //usage: IF_FEATURE_VI_COLON("[-c CMD] ")IF_FEATURE_VI_READONLY("[-R] ")"[-H] [FILE]..." | 197 | //usage: IF_FEATURE_VI_FILE_FORMAT("[-b] ")IF_FEATURE_VI_COLON("[-c CMD] ")IF_FEATURE_VI_READONLY("[-R] ")"[-H] [FILE]..." |
194 | //usage:#define vi_full_usage "\n\n" | 198 | //usage:#define vi_full_usage "\n\n" |
195 | //usage: "Edit FILE\n" | 199 | //usage: "Edit FILE\n" |
200 | //usage: IF_FEATURE_VI_FILE_FORMAT( | ||
201 | //usage: "\n -b Edit file in binary mode" | ||
202 | //usage: ) | ||
196 | //usage: IF_FEATURE_VI_COLON( | 203 | //usage: IF_FEATURE_VI_COLON( |
197 | //usage: "\n -c CMD Initial command to run ($EXINIT and ~/.exrc also available)" | 204 | //usage: "\n -c CMD Initial command to run ($EXINIT and ~/.exrc also available)" |
198 | //usage: ) | 205 | //usage: ) |
@@ -320,15 +327,17 @@ struct globals { | |||
320 | #define VI_SHOWMATCH (1 << 4) | 327 | #define VI_SHOWMATCH (1 << 4) |
321 | #define VI_TABSTOP (1 << 5) | 328 | #define VI_TABSTOP (1 << 5) |
322 | #else | 329 | #else |
323 | smalluint vi_setops; // set by setops() | 330 | int vi_setops; // set by setops() |
324 | #define VI_AUTOINDENT (1 << 0) | 331 | #define VI_AUTOINDENT (1 << 0) |
325 | #define VI_EXPANDTAB (1 << 1) | 332 | #define VI_BINARY (1 << 1) |
326 | #define VI_FILEFORMAT (1 << 2) | 333 | #define VI_EXPANDTAB (1 << 2) |
327 | #define VI_FILEFORMATS (1 << 3) | 334 | #define VI_FILEFORMAT (1 << 3) |
328 | #define VI_ERR_METHOD (1 << 4) | 335 | #define VI_FILEFORMATS (1 << 4) |
329 | #define VI_IGNORECASE (1 << 5) | 336 | #define VI_ERR_METHOD (1 << 5) |
330 | #define VI_SHOWMATCH (1 << 6) | 337 | #define VI_IGNORECASE (1 << 6) |
331 | #define VI_TABSTOP (1 << 7) | 338 | #define VI_SHOWMATCH (1 << 7) |
339 | #define VI_TABSTOP (1 << 8) | ||
340 | #define binary (vi_setops & VI_BINARY ) | ||
332 | #endif | 341 | #endif |
333 | #define autoindent (vi_setops & VI_AUTOINDENT) | 342 | #define autoindent (vi_setops & VI_AUTOINDENT) |
334 | #define expandtab (vi_setops & VI_EXPANDTAB ) | 343 | #define expandtab (vi_setops & VI_EXPANDTAB ) |
@@ -338,6 +347,7 @@ struct globals { | |||
338 | // order of constants and strings must match | 347 | // order of constants and strings must match |
339 | #define OPTS_STR \ | 348 | #define OPTS_STR \ |
340 | "ai\0""autoindent\0" \ | 349 | "ai\0""autoindent\0" \ |
350 | IF_FEATURE_VI_FILE_FORMAT("bin\0""binary\0") \ | ||
341 | "et\0""expandtab\0" \ | 351 | "et\0""expandtab\0" \ |
342 | IF_FEATURE_VI_FILE_FORMAT("ff\0""fileformat\0") \ | 352 | IF_FEATURE_VI_FILE_FORMAT("ff\0""fileformat\0") \ |
343 | IF_FEATURE_VI_FILE_FORMAT("ffs\0""fileformats\0") \ | 353 | IF_FEATURE_VI_FILE_FORMAT("ffs\0""fileformats\0") \ |
@@ -2073,6 +2083,8 @@ static int file_insert(const char *fn, char *p, int initial) | |||
2073 | 2083 | ||
2074 | #if !ENABLE_PLATFORM_MINGW32 | 2084 | #if !ENABLE_PLATFORM_MINGW32 |
2075 | fd = open(fn, O_RDONLY); | 2085 | fd = open(fn, O_RDONLY); |
2086 | #elif ENABLE_FEATURE_VI_FILE_FORMAT | ||
2087 | fd = open(fn, O_RDONLY | (!binary ? _O_TEXT : 0)); | ||
2076 | #else | 2088 | #else |
2077 | fd = open(fn, O_RDONLY | _O_TEXT); | 2089 | fd = open(fn, O_RDONLY | _O_TEXT); |
2078 | #endif | 2090 | #endif |
@@ -2458,7 +2470,7 @@ static int file_write(char *fn, char *first, char *last) | |||
2458 | int fd, cnt, charcnt; | 2470 | int fd, cnt, charcnt; |
2459 | #if ENABLE_PLATFORM_MINGW32 | 2471 | #if ENABLE_PLATFORM_MINGW32 |
2460 | # if ENABLE_FEATURE_VI_FILE_FORMAT | 2472 | # if ENABLE_FEATURE_VI_FILE_FORMAT |
2461 | # define dos (fileformat == FF_DOS) | 2473 | # define dos (!binary && fileformat == FF_DOS) |
2462 | # else | 2474 | # else |
2463 | # define dos (1) | 2475 | # define dos (1) |
2464 | # endif | 2476 | # endif |
@@ -2818,6 +2830,11 @@ static void setops(char *args, int flg_no) | |||
2818 | vi_setops &= ~index; | 2830 | vi_setops &= ~index; |
2819 | } else { | 2831 | } else { |
2820 | vi_setops |= index; | 2832 | vi_setops |= index; |
2833 | #if ENABLE_FEATURE_VI_FILE_FORMAT | ||
2834 | if (index == VI_BINARY) { | ||
2835 | vi_setops &= ~VI_EXPANDTAB; | ||
2836 | } | ||
2837 | #endif | ||
2821 | } | 2838 | } |
2822 | } | 2839 | } |
2823 | # endif | 2840 | # endif |
@@ -5100,17 +5117,20 @@ static void edit_file(char *fn) | |||
5100 | } | 5117 | } |
5101 | 5118 | ||
5102 | #define VI_OPTSTR \ | 5119 | #define VI_OPTSTR \ |
5120 | IF_FEATURE_VI_FILE_FORMAT("b") \ | ||
5103 | IF_FEATURE_VI_CRASHME("C") \ | 5121 | IF_FEATURE_VI_CRASHME("C") \ |
5104 | IF_FEATURE_VI_COLON("c:*") \ | 5122 | IF_FEATURE_VI_COLON("c:*") \ |
5105 | "Hh" \ | 5123 | "Hh" \ |
5106 | IF_FEATURE_VI_READONLY("R") | 5124 | IF_FEATURE_VI_READONLY("R") |
5107 | 5125 | ||
5108 | enum { | 5126 | enum { |
5127 | IF_FEATURE_VI_FILE_FORMAT(OPTBIT_b,) | ||
5109 | IF_FEATURE_VI_CRASHME(OPTBIT_C,) | 5128 | IF_FEATURE_VI_CRASHME(OPTBIT_C,) |
5110 | IF_FEATURE_VI_COLON(OPTBIT_c,) | 5129 | IF_FEATURE_VI_COLON(OPTBIT_c,) |
5111 | OPTBIT_H, | 5130 | OPTBIT_H, |
5112 | OPTBIT_h, | 5131 | OPTBIT_h, |
5113 | IF_FEATURE_VI_READONLY(OPTBIT_R,) | 5132 | IF_FEATURE_VI_READONLY(OPTBIT_R,) |
5133 | OPT_b = IF_FEATURE_VI_FILE_FORMAT( (1 << OPTBIT_b)) + 0, | ||
5114 | OPT_C = IF_FEATURE_VI_CRASHME( (1 << OPTBIT_C)) + 0, | 5134 | OPT_C = IF_FEATURE_VI_CRASHME( (1 << OPTBIT_C)) + 0, |
5115 | OPT_c = IF_FEATURE_VI_COLON( (1 << OPTBIT_c)) + 0, | 5135 | OPT_c = IF_FEATURE_VI_COLON( (1 << OPTBIT_c)) + 0, |
5116 | OPT_H = 1 << OPTBIT_H, | 5136 | OPT_H = 1 << OPTBIT_H, |
@@ -5147,6 +5167,10 @@ int vi_main(int argc, char **argv) | |||
5147 | //vi_setops = 0; | 5167 | //vi_setops = 0; |
5148 | opts = getopt32(argv, VI_OPTSTR IF_FEATURE_VI_COLON(, &initial_cmds)); | 5168 | opts = getopt32(argv, VI_OPTSTR IF_FEATURE_VI_COLON(, &initial_cmds)); |
5149 | 5169 | ||
5170 | #if ENABLE_FEATURE_VI_FILE_FORMAT | ||
5171 | if (opts & OPT_b) | ||
5172 | vi_setops |= VI_BINARY; | ||
5173 | #endif | ||
5150 | #if ENABLE_FEATURE_VI_CRASHME | 5174 | #if ENABLE_FEATURE_VI_CRASHME |
5151 | if (opts & OPT_C) | 5175 | if (opts & OPT_C) |
5152 | crashme = 1; | 5176 | crashme = 1; |