diff options
author | Walter Harms <wharms@bfs.de> | 2011-06-27 02:59:37 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-06-27 02:59:37 +0200 |
commit | b9ba580917b59e4770ba99db7c61288f2476eb61 (patch) | |
tree | cb8a6f71fa73527d6af24c049835927badc5581c | |
parent | 52f4fe9db6cedfba332ee0923182f5b2e9d9673b (diff) | |
download | busybox-w32-b9ba580917b59e4770ba99db7c61288f2476eb61.tar.gz busybox-w32-b9ba580917b59e4770ba99db7c61288f2476eb61.tar.bz2 busybox-w32-b9ba580917b59e4770ba99db7c61288f2476eb61.zip |
vi: fix regex search, make it selectable in config
function old new delta
char_search 134 214 +80
find_pair 187 169 -18
mycmp 37 - -37
Signed-off-by: Walter Harms <wharms@bfs.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/Config.src | 117 | ||||
-rw-r--r-- | editors/Kbuild.src | 1 | ||||
-rw-r--r-- | editors/vi.c | 221 | ||||
-rw-r--r-- | include/applets.src.h | 1 |
4 files changed, 181 insertions, 159 deletions
diff --git a/editors/Config.src b/editors/Config.src index 201ee6eb9..af1e1de5e 100644 --- a/editors/Config.src +++ b/editors/Config.src | |||
@@ -67,123 +67,6 @@ config SED | |||
67 | sed is used to perform text transformations on a file | 67 | sed is used to perform text transformations on a file |
68 | or input from a pipeline. | 68 | or input from a pipeline. |
69 | 69 | ||
70 | config VI | ||
71 | bool "vi" | ||
72 | default y | ||
73 | help | ||
74 | 'vi' is a text editor. More specifically, it is the One True | ||
75 | text editor <grin>. It does, however, have a rather steep | ||
76 | learning curve. If you are not already comfortable with 'vi' | ||
77 | you may wish to use something else. | ||
78 | |||
79 | config FEATURE_VI_MAX_LEN | ||
80 | int "Maximum screen width in vi" | ||
81 | range 256 16384 | ||
82 | default 4096 | ||
83 | depends on VI | ||
84 | help | ||
85 | Contrary to what you may think, this is not eating much. | ||
86 | Make it smaller than 4k only if you are very limited on memory. | ||
87 | |||
88 | config FEATURE_VI_8BIT | ||
89 | bool "Allow vi to display 8-bit chars (otherwise shows dots)" | ||
90 | default n | ||
91 | depends on VI | ||
92 | help | ||
93 | If your terminal can display characters with high bit set, | ||
94 | you may want to enable this. Note: vi is not Unicode-capable. | ||
95 | If your terminal combines several 8-bit bytes into one character | ||
96 | (as in Unicode mode), this will not work properly. | ||
97 | |||
98 | config FEATURE_VI_COLON | ||
99 | bool "Enable \":\" colon commands (no \"ex\" mode)" | ||
100 | default y | ||
101 | depends on VI | ||
102 | help | ||
103 | Enable a limited set of colon commands for vi. This does not | ||
104 | provide an "ex" mode. | ||
105 | |||
106 | config FEATURE_VI_YANKMARK | ||
107 | bool "Enable yank/put commands and mark cmds" | ||
108 | default y | ||
109 | depends on VI | ||
110 | help | ||
111 | This will enable you to use yank and put, as well as mark in | ||
112 | busybox vi. | ||
113 | |||
114 | config FEATURE_VI_SEARCH | ||
115 | bool "Enable search and replace cmds" | ||
116 | default y | ||
117 | depends on VI | ||
118 | help | ||
119 | Select this if you wish to be able to do search and replace in | ||
120 | busybox vi. | ||
121 | |||
122 | config FEATURE_VI_USE_SIGNALS | ||
123 | bool "Catch signals" | ||
124 | default y | ||
125 | depends on VI | ||
126 | help | ||
127 | Selecting this option will make busybox vi signal aware. This will | ||
128 | make busybox vi support SIGWINCH to deal with Window Changes, catch | ||
129 | Ctrl-Z and Ctrl-C and alarms. | ||
130 | |||
131 | config FEATURE_VI_DOT_CMD | ||
132 | bool "Remember previous cmd and \".\" cmd" | ||
133 | default y | ||
134 | depends on VI | ||
135 | help | ||
136 | Make busybox vi remember the last command and be able to repeat it. | ||
137 | |||
138 | config FEATURE_VI_READONLY | ||
139 | bool "Enable -R option and \"view\" mode" | ||
140 | default y | ||
141 | depends on VI | ||
142 | help | ||
143 | Enable the read-only command line option, which allows the user to | ||
144 | open a file in read-only mode. | ||
145 | |||
146 | config FEATURE_VI_SETOPTS | ||
147 | bool "Enable set-able options, ai ic showmatch" | ||
148 | default y | ||
149 | depends on VI | ||
150 | help | ||
151 | Enable the editor to set some (ai, ic, showmatch) options. | ||
152 | |||
153 | config FEATURE_VI_SET | ||
154 | bool "Support for :set" | ||
155 | default y | ||
156 | depends on VI | ||
157 | help | ||
158 | Support for ":set". | ||
159 | |||
160 | config FEATURE_VI_WIN_RESIZE | ||
161 | bool "Handle window resize" | ||
162 | default y | ||
163 | depends on VI | ||
164 | help | ||
165 | Make busybox vi behave nicely with terminals that get resized. | ||
166 | |||
167 | config FEATURE_VI_ASK_TERMINAL | ||
168 | bool "Use 'tell me cursor position' ESC sequence to measure window" | ||
169 | default y | ||
170 | depends on VI | ||
171 | help | ||
172 | If terminal size can't be retrieved and $LINES/$COLUMNS are not set, | ||
173 | this option makes vi perform a last-ditch effort to find it: | ||
174 | vi positions cursor to 999,999 and asks terminal to report real | ||
175 | cursor position using "ESC [ 6 n" escape sequence, then reads stdin. | ||
176 | |||
177 | This is not clean but helps a lot on serial lines and such. | ||
178 | |||
179 | config FEATURE_VI_OPTIMIZE_CURSOR | ||
180 | bool "Optimize cursor movement" | ||
181 | default y | ||
182 | depends on VI | ||
183 | help | ||
184 | This will make the cursor movement faster, but requires more memory | ||
185 | and it makes the applet a tiny bit larger. | ||
186 | |||
187 | config FEATURE_ALLOW_EXEC | 70 | config FEATURE_ALLOW_EXEC |
188 | bool "Allow vi and awk to execute shell commands" | 71 | bool "Allow vi and awk to execute shell commands" |
189 | default y | 72 | default y |
diff --git a/editors/Kbuild.src b/editors/Kbuild.src index 2f23ae12f..8888cba12 100644 --- a/editors/Kbuild.src +++ b/editors/Kbuild.src | |||
@@ -12,4 +12,3 @@ lib-$(CONFIG_CMP) += cmp.o | |||
12 | lib-$(CONFIG_DIFF) += diff.o | 12 | lib-$(CONFIG_DIFF) += diff.o |
13 | lib-$(CONFIG_ED) += ed.o | 13 | lib-$(CONFIG_ED) += ed.o |
14 | lib-$(CONFIG_SED) += sed.o | 14 | lib-$(CONFIG_SED) += sed.o |
15 | lib-$(CONFIG_VI) += vi.o | ||
diff --git a/editors/vi.c b/editors/vi.c index 3656fee95..3f4ccdcdd 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -21,6 +21,134 @@ | |||
21 | * An "ex" line oriented mode- maybe using "cmdedit" | 21 | * An "ex" line oriented mode- maybe using "cmdedit" |
22 | */ | 22 | */ |
23 | 23 | ||
24 | //config:config VI | ||
25 | //config: bool "vi" | ||
26 | //config: default y | ||
27 | //config: help | ||
28 | //config: 'vi' is a text editor. More specifically, it is the One True | ||
29 | //config: text editor <grin>. It does, however, have a rather steep | ||
30 | //config: learning curve. If you are not already comfortable with 'vi' | ||
31 | //config: you may wish to use something else. | ||
32 | //config: | ||
33 | //config:config FEATURE_VI_MAX_LEN | ||
34 | //config: int "Maximum screen width in vi" | ||
35 | //config: range 256 16384 | ||
36 | //config: default 4096 | ||
37 | //config: depends on VI | ||
38 | //config: help | ||
39 | //config: Contrary to what you may think, this is not eating much. | ||
40 | //config: Make it smaller than 4k only if you are very limited on memory. | ||
41 | //config: | ||
42 | //config:config FEATURE_VI_8BIT | ||
43 | //config: bool "Allow vi to display 8-bit chars (otherwise shows dots)" | ||
44 | //config: default n | ||
45 | //config: depends on VI | ||
46 | //config: help | ||
47 | //config: If your terminal can display characters with high bit set, | ||
48 | //config: you may want to enable this. Note: vi is not Unicode-capable. | ||
49 | //config: If your terminal combines several 8-bit bytes into one character | ||
50 | //config: (as in Unicode mode), this will not work properly. | ||
51 | //config: | ||
52 | //config:config FEATURE_VI_COLON | ||
53 | //config: bool "Enable \":\" colon commands (no \"ex\" mode)" | ||
54 | //config: default y | ||
55 | //config: depends on VI | ||
56 | //config: help | ||
57 | //config: Enable a limited set of colon commands for vi. This does not | ||
58 | //config: provide an "ex" mode. | ||
59 | //config: | ||
60 | //config:config FEATURE_VI_YANKMARK | ||
61 | //config: bool "Enable yank/put commands and mark cmds" | ||
62 | //config: default y | ||
63 | //config: depends on VI | ||
64 | //config: help | ||
65 | //config: This will enable you to use yank and put, as well as mark in | ||
66 | //config: busybox vi. | ||
67 | //config: | ||
68 | //config:config FEATURE_VI_SEARCH | ||
69 | //config: bool "Enable search and replace cmds" | ||
70 | //config: default y | ||
71 | //config: depends on VI | ||
72 | //config: help | ||
73 | //config: Select this if you wish to be able to do search and replace in | ||
74 | //config: busybox vi. | ||
75 | //config: | ||
76 | //config:config FEATURE_VI_REGEX_SEARCH | ||
77 | //config: bool "Enable regex in search and replace" | ||
78 | //config: default n # Uses GNU regex, which may be unavailable. FIXME | ||
79 | //config: depends on FEATURE_VI_SEARCH | ||
80 | //config: help | ||
81 | //config: Use extended regex search. | ||
82 | //config: | ||
83 | //config:config FEATURE_VI_USE_SIGNALS | ||
84 | //config: bool "Catch signals" | ||
85 | //config: default y | ||
86 | //config: depends on VI | ||
87 | //config: help | ||
88 | //config: Selecting this option will make busybox vi signal aware. This will | ||
89 | //config: make busybox vi support SIGWINCH to deal with Window Changes, catch | ||
90 | //config: Ctrl-Z and Ctrl-C and alarms. | ||
91 | //config: | ||
92 | //config:config FEATURE_VI_DOT_CMD | ||
93 | //config: bool "Remember previous cmd and \".\" cmd" | ||
94 | //config: default y | ||
95 | //config: depends on VI | ||
96 | //config: help | ||
97 | //config: Make busybox vi remember the last command and be able to repeat it. | ||
98 | //config: | ||
99 | //config:config FEATURE_VI_READONLY | ||
100 | //config: bool "Enable -R option and \"view\" mode" | ||
101 | //config: default y | ||
102 | //config: depends on VI | ||
103 | //config: help | ||
104 | //config: Enable the read-only command line option, which allows the user to | ||
105 | //config: open a file in read-only mode. | ||
106 | //config: | ||
107 | //config:config FEATURE_VI_SETOPTS | ||
108 | //config: bool "Enable set-able options, ai ic showmatch" | ||
109 | //config: default y | ||
110 | //config: depends on VI | ||
111 | //config: help | ||
112 | //config: Enable the editor to set some (ai, ic, showmatch) options. | ||
113 | //config: | ||
114 | //config:config FEATURE_VI_SET | ||
115 | //config: bool "Support for :set" | ||
116 | //config: default y | ||
117 | //config: depends on VI | ||
118 | //config: help | ||
119 | //config: Support for ":set". | ||
120 | //config: | ||
121 | //config:config FEATURE_VI_WIN_RESIZE | ||
122 | //config: bool "Handle window resize" | ||
123 | //config: default y | ||
124 | //config: depends on VI | ||
125 | //config: help | ||
126 | //config: Make busybox vi behave nicely with terminals that get resized. | ||
127 | //config: | ||
128 | //config:config FEATURE_VI_ASK_TERMINAL | ||
129 | //config: bool "Use 'tell me cursor position' ESC sequence to measure window" | ||
130 | //config: default y | ||
131 | //config: depends on VI | ||
132 | //config: help | ||
133 | //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set, | ||
134 | //config: this option makes vi perform a last-ditch effort to find it: | ||
135 | //config: vi positions cursor to 999,999 and asks terminal to report real | ||
136 | //config: cursor position using "ESC [ 6 n" escape sequence, then reads stdin. | ||
137 | //config: | ||
138 | //config: This is not clean but helps a lot on serial lines and such. | ||
139 | //config: | ||
140 | //config:config FEATURE_VI_OPTIMIZE_CURSOR | ||
141 | //config: bool "Optimize cursor movement" | ||
142 | //config: default y | ||
143 | //config: depends on VI | ||
144 | //config: help | ||
145 | //config: This will make the cursor movement faster, but requires more memory | ||
146 | //config: and it makes the applet a tiny bit larger. | ||
147 | |||
148 | //applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP)) | ||
149 | |||
150 | //kbuild:lib-$(CONFIG_VI) += vi.o | ||
151 | |||
24 | //usage:#define vi_trivial_usage | 152 | //usage:#define vi_trivial_usage |
25 | //usage: "[OPTIONS] [FILE]..." | 153 | //usage: "[OPTIONS] [FILE]..." |
26 | //usage:#define vi_full_usage "\n\n" | 154 | //usage:#define vi_full_usage "\n\n" |
@@ -33,6 +161,7 @@ | |||
33 | //usage: ) | 161 | //usage: ) |
34 | //usage: "\n -H Short help regarding available features" | 162 | //usage: "\n -H Short help regarding available features" |
35 | 163 | ||
164 | #include <regex.h> | ||
36 | #include "libbb.h" | 165 | #include "libbb.h" |
37 | 166 | ||
38 | /* the CRASHME code is unmaintained, and doesn't currently build */ | 167 | /* the CRASHME code is unmaintained, and doesn't currently build */ |
@@ -366,7 +495,6 @@ static void Hit_Return(void); | |||
366 | 495 | ||
367 | #if ENABLE_FEATURE_VI_SEARCH | 496 | #if ENABLE_FEATURE_VI_SEARCH |
368 | static char *char_search(char *, const char *, int, int); // search for pattern starting at p | 497 | static char *char_search(char *, const char *, int, int); // search for pattern starting at p |
369 | static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase" | ||
370 | #endif | 498 | #endif |
371 | #if ENABLE_FEATURE_VI_COLON | 499 | #if ENABLE_FEATURE_VI_COLON |
372 | static char *get_one_address(char *, int *); // get colon addr, if present | 500 | static char *get_one_address(char *, int *); // get colon addr, if present |
@@ -1561,48 +1689,16 @@ static char *new_screen(int ro, int co) | |||
1561 | } | 1689 | } |
1562 | 1690 | ||
1563 | #if ENABLE_FEATURE_VI_SEARCH | 1691 | #if ENABLE_FEATURE_VI_SEARCH |
1564 | static int mycmp(const char *s1, const char *s2, int len) | 1692 | |
1565 | { | 1693 | # if ENABLE_FEATURE_VI_REGEX_SEARCH |
1566 | if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) { | ||
1567 | return strncasecmp(s1, s2, len); | ||
1568 | } | ||
1569 | return strncmp(s1, s2, len); | ||
1570 | } | ||
1571 | 1694 | ||
1572 | // search for pattern starting at p | 1695 | // search for pattern starting at p |
1573 | static char *char_search(char *p, const char *pat, int dir, int range) | 1696 | static char *char_search(char *p, const char *pat, int dir, int range) |
1574 | { | 1697 | { |
1575 | #ifndef REGEX_SEARCH | ||
1576 | char *start, *stop; | ||
1577 | int len; | ||
1578 | |||
1579 | len = strlen(pat); | ||
1580 | if (dir == FORWARD) { | ||
1581 | stop = end - 1; // assume range is p - end-1 | ||
1582 | if (range == LIMITED) | ||
1583 | stop = next_line(p); // range is to next line | ||
1584 | for (start = p; start < stop; start++) { | ||
1585 | if (mycmp(start, pat, len) == 0) { | ||
1586 | return start; | ||
1587 | } | ||
1588 | } | ||
1589 | } else if (dir == BACK) { | ||
1590 | stop = text; // assume range is text - p | ||
1591 | if (range == LIMITED) | ||
1592 | stop = prev_line(p); // range is to prev line | ||
1593 | for (start = p - len; start >= stop; start--) { | ||
1594 | if (mycmp(start, pat, len) == 0) { | ||
1595 | return start; | ||
1596 | } | ||
1597 | } | ||
1598 | } | ||
1599 | // pattern not found | ||
1600 | return NULL; | ||
1601 | #else /* REGEX_SEARCH */ | ||
1602 | char *q; | 1698 | char *q; |
1603 | struct re_pattern_buffer preg; | 1699 | struct re_pattern_buffer preg; |
1604 | int i; | 1700 | int i; |
1605 | int size, range; | 1701 | int size; |
1606 | 1702 | ||
1607 | re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; | 1703 | re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; |
1608 | preg.translate = 0; | 1704 | preg.translate = 0; |
@@ -1625,7 +1721,7 @@ static char *char_search(char *p, const char *pat, int dir, int range) | |||
1625 | // RANGE could be negative if we are searching backwards | 1721 | // RANGE could be negative if we are searching backwards |
1626 | range = q - p; | 1722 | range = q - p; |
1627 | 1723 | ||
1628 | q = re_compile_pattern(pat, strlen(pat), &preg); | 1724 | q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg); |
1629 | if (q != 0) { | 1725 | if (q != 0) { |
1630 | // The pattern was not compiled | 1726 | // The pattern was not compiled |
1631 | status_line_bold("bad search pattern: \"%s\": %s", pat, q); | 1727 | status_line_bold("bad search pattern: \"%s\": %s", pat, q); |
@@ -1659,8 +1755,53 @@ static char *char_search(char *p, const char *pat, int dir, int range) | |||
1659 | p = p - i; | 1755 | p = p - i; |
1660 | } | 1756 | } |
1661 | return p; | 1757 | return p; |
1662 | #endif /* REGEX_SEARCH */ | ||
1663 | } | 1758 | } |
1759 | |||
1760 | # else | ||
1761 | |||
1762 | # if ENABLE_FEATURE_VI_SETOPTS | ||
1763 | static int mycmp(const char *s1, const char *s2, int len) | ||
1764 | { | ||
1765 | if (ignorecase) { | ||
1766 | return strncasecmp(s1, s2, len); | ||
1767 | } | ||
1768 | return strncmp(s1, s2, len); | ||
1769 | } | ||
1770 | # else | ||
1771 | # define mycmp strncmp | ||
1772 | # endif | ||
1773 | |||
1774 | static char *char_search(char *p, const char *pat, int dir, int range) | ||
1775 | { | ||
1776 | char *start, *stop; | ||
1777 | int len; | ||
1778 | |||
1779 | len = strlen(pat); | ||
1780 | if (dir == FORWARD) { | ||
1781 | stop = end - 1; // assume range is p - end-1 | ||
1782 | if (range == LIMITED) | ||
1783 | stop = next_line(p); // range is to next line | ||
1784 | for (start = p; start < stop; start++) { | ||
1785 | if (mycmp(start, pat, len) == 0) { | ||
1786 | return start; | ||
1787 | } | ||
1788 | } | ||
1789 | } else if (dir == BACK) { | ||
1790 | stop = text; // assume range is text - p | ||
1791 | if (range == LIMITED) | ||
1792 | stop = prev_line(p); // range is to prev line | ||
1793 | for (start = p - len; start >= stop; start--) { | ||
1794 | if (mycmp(start, pat, len) == 0) { | ||
1795 | return start; | ||
1796 | } | ||
1797 | } | ||
1798 | } | ||
1799 | // pattern not found | ||
1800 | return NULL; | ||
1801 | } | ||
1802 | |||
1803 | # endif | ||
1804 | |||
1664 | #endif /* FEATURE_VI_SEARCH */ | 1805 | #endif /* FEATURE_VI_SEARCH */ |
1665 | 1806 | ||
1666 | static char *char_insert(char *p, char c) // insert the char c at 'p' | 1807 | static char *char_insert(char *p, char c) // insert the char c at 'p' |
@@ -2022,8 +2163,8 @@ static void show_help(void) | |||
2022 | "\n\tNamed buffers with \"x" | 2163 | "\n\tNamed buffers with \"x" |
2023 | #endif | 2164 | #endif |
2024 | #if ENABLE_FEATURE_VI_READONLY | 2165 | #if ENABLE_FEATURE_VI_READONLY |
2025 | "\n\tReadonly if vi is called as \"view\"" | 2166 | //not implemented: "\n\tReadonly if vi is called as \"view\"" |
2026 | "\n\tReadonly with -R command line arg" | 2167 | //redundant: usage text says this too: "\n\tReadonly with -R command line arg" |
2027 | #endif | 2168 | #endif |
2028 | #if ENABLE_FEATURE_VI_SET | 2169 | #if ENABLE_FEATURE_VI_SET |
2029 | "\n\tSome colon mode commands with \':\'" | 2170 | "\n\tSome colon mode commands with \':\'" |
diff --git a/include/applets.src.h b/include/applets.src.h index c6d99fe8c..2d55ffec7 100644 --- a/include/applets.src.h +++ b/include/applets.src.h | |||
@@ -399,7 +399,6 @@ IF_USLEEP(APPLET_NOFORK(usleep, usleep, BB_DIR_BIN, BB_SUID_DROP, usleep)) | |||
399 | IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP)) | 399 | IF_UUDECODE(APPLET(uudecode, BB_DIR_USR_BIN, BB_SUID_DROP)) |
400 | IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP)) | 400 | IF_UUENCODE(APPLET(uuencode, BB_DIR_USR_BIN, BB_SUID_DROP)) |
401 | IF_VCONFIG(APPLET(vconfig, BB_DIR_SBIN, BB_SUID_DROP)) | 401 | IF_VCONFIG(APPLET(vconfig, BB_DIR_SBIN, BB_SUID_DROP)) |
402 | IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP)) | ||
403 | /* Needs to be run by root or be suid root - needs to change uid and gid: */ | 402 | /* Needs to be run by root or be suid root - needs to change uid and gid: */ |
404 | IF_VLOCK(APPLET(vlock, BB_DIR_USR_BIN, BB_SUID_REQUIRE)) | 403 | IF_VLOCK(APPLET(vlock, BB_DIR_USR_BIN, BB_SUID_REQUIRE)) |
405 | IF_VOLNAME(APPLET(volname, BB_DIR_USR_BIN, BB_SUID_DROP)) | 404 | IF_VOLNAME(APPLET(volname, BB_DIR_USR_BIN, BB_SUID_DROP)) |