diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-21 20:31:50 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-21 20:31:50 +0000 |
commit | 3387538bbeab0540783c356b298ae45dfb3684a3 (patch) | |
tree | f43aed466314e0ce5062e25ec37b512ecd10b97c | |
parent | b175946ba4426c4bb529f9b98f75105e21208c83 (diff) | |
download | busybox-w32-3387538bbeab0540783c356b298ae45dfb3684a3.tar.gz busybox-w32-3387538bbeab0540783c356b298ae45dfb3684a3.tar.bz2 busybox-w32-3387538bbeab0540783c356b298ae45dfb3684a3.zip |
vi: small code shrink
function old new delta
file_insert 328 329 +1
colon 3067 3064 -3
string_insert 93 89 -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 1/-7) Total: -6 bytes
-rw-r--r-- | editors/vi.c | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/editors/vi.c b/editors/vi.c index 17ce88b93..86d418025 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -1124,7 +1124,7 @@ static void colon(char *buf) | |||
1124 | } | 1124 | } |
1125 | #endif /* FEATURE_VI_SEARCH */ | 1125 | #endif /* FEATURE_VI_SEARCH */ |
1126 | } else if (strncasecmp(cmd, "version", i) == 0) { // show software version | 1126 | } else if (strncasecmp(cmd, "version", i) == 0) { // show software version |
1127 | status_line("%s", BB_VER " " BB_BT); | 1127 | status_line(BB_VER " " BB_BT); |
1128 | } else if (strncasecmp(cmd, "write", i) == 0 // write text to file | 1128 | } else if (strncasecmp(cmd, "write", i) == 0 // write text to file |
1129 | || strncasecmp(cmd, "wq", i) == 0 | 1129 | || strncasecmp(cmd, "wq", i) == 0 |
1130 | || strncasecmp(cmd, "wn", i) == 0 | 1130 | || strncasecmp(cmd, "wn", i) == 0 |
@@ -1540,7 +1540,7 @@ static char *new_screen(int ro, int co) | |||
1540 | } | 1540 | } |
1541 | 1541 | ||
1542 | #if ENABLE_FEATURE_VI_SEARCH | 1542 | #if ENABLE_FEATURE_VI_SEARCH |
1543 | static int mycmp(const char * s1, const char * s2, int len) | 1543 | static int mycmp(const char *s1, const char *s2, int len) |
1544 | { | 1544 | { |
1545 | int i; | 1545 | int i; |
1546 | 1546 | ||
@@ -1552,7 +1552,7 @@ static int mycmp(const char * s1, const char * s2, int len) | |||
1552 | } | 1552 | } |
1553 | 1553 | ||
1554 | // search for pattern starting at p | 1554 | // search for pattern starting at p |
1555 | static char *char_search(char * p, const char * pat, int dir, int range) | 1555 | static char *char_search(char *p, const char *pat, int dir, int range) |
1556 | { | 1556 | { |
1557 | #ifndef REGEX_SEARCH | 1557 | #ifndef REGEX_SEARCH |
1558 | char *start, *stop; | 1558 | char *start, *stop; |
@@ -1645,7 +1645,7 @@ static char *char_search(char * p, const char * pat, int dir, int range) | |||
1645 | } | 1645 | } |
1646 | #endif /* FEATURE_VI_SEARCH */ | 1646 | #endif /* FEATURE_VI_SEARCH */ |
1647 | 1647 | ||
1648 | static char *char_insert(char * p, char c) // insert the char c at 'p' | 1648 | static char *char_insert(char *p, char c) // insert the char c at 'p' |
1649 | { | 1649 | { |
1650 | if (c == 22) { // Is this an ctrl-V? | 1650 | if (c == 22) { // Is this an ctrl-V? |
1651 | p = stupid_insert(p, '^'); // use ^ to indicate literal next | 1651 | p = stupid_insert(p, '^'); // use ^ to indicate literal next |
@@ -1694,7 +1694,7 @@ static char *char_insert(char * p, char c) // insert the char c at 'p' | |||
1694 | return p; | 1694 | return p; |
1695 | } | 1695 | } |
1696 | 1696 | ||
1697 | static char *stupid_insert(char * p, char c) // stupidly insert the char c at 'p' | 1697 | static char *stupid_insert(char *p, char c) // stupidly insert the char c at 'p' |
1698 | { | 1698 | { |
1699 | p = text_hole_make(p, 1); | 1699 | p = text_hole_make(p, 1); |
1700 | *p = c; | 1700 | *p = c; |
@@ -1702,7 +1702,7 @@ static char *stupid_insert(char * p, char c) // stupidly insert the char c at 'p | |||
1702 | return p + 1; | 1702 | return p + 1; |
1703 | } | 1703 | } |
1704 | 1704 | ||
1705 | static int find_range(char ** start, char ** stop, char c) | 1705 | static int find_range(char **start, char **stop, char c) |
1706 | { | 1706 | { |
1707 | char *save_dot, *p, *q, *t; | 1707 | char *save_dot, *p, *q, *t; |
1708 | int cnt, multiline = 0; | 1708 | int cnt, multiline = 0; |
@@ -1775,7 +1775,7 @@ static int find_range(char ** start, char ** stop, char c) | |||
1775 | return multiline; | 1775 | return multiline; |
1776 | } | 1776 | } |
1777 | 1777 | ||
1778 | static int st_test(char * p, int type, int dir, char * tested) | 1778 | static int st_test(char *p, int type, int dir, char *tested) |
1779 | { | 1779 | { |
1780 | char c, c0, ci; | 1780 | char c, c0, ci; |
1781 | int test, inc; | 1781 | int test, inc; |
@@ -1809,7 +1809,7 @@ static int st_test(char * p, int type, int dir, char * tested) | |||
1809 | return test; | 1809 | return test; |
1810 | } | 1810 | } |
1811 | 1811 | ||
1812 | static char *skip_thing(char * p, int linecnt, int dir, int type) | 1812 | static char *skip_thing(char *p, int linecnt, int dir, int type) |
1813 | { | 1813 | { |
1814 | char c; | 1814 | char c; |
1815 | 1815 | ||
@@ -1827,7 +1827,7 @@ static char *skip_thing(char * p, int linecnt, int dir, int type) | |||
1827 | } | 1827 | } |
1828 | 1828 | ||
1829 | // find matching char of pair () [] {} | 1829 | // find matching char of pair () [] {} |
1830 | static char *find_pair(char * p, const char c) | 1830 | static char *find_pair(char *p, const char c) |
1831 | { | 1831 | { |
1832 | char match, *q; | 1832 | char match, *q; |
1833 | int dir, level; | 1833 | int dir, level; |
@@ -1880,7 +1880,7 @@ static void showmatching(char *p) | |||
1880 | #endif /* FEATURE_VI_SETOPTS */ | 1880 | #endif /* FEATURE_VI_SETOPTS */ |
1881 | 1881 | ||
1882 | // open a hole in text[] | 1882 | // open a hole in text[] |
1883 | static char *text_hole_make(char * p, int size) // at "p", make a 'size' byte hole | 1883 | static char *text_hole_make(char *p, int size) // at "p", make a 'size' byte hole |
1884 | { | 1884 | { |
1885 | if (size <= 0) | 1885 | if (size <= 0) |
1886 | return p; | 1886 | return p; |
@@ -1901,7 +1901,7 @@ static char *text_hole_make(char * p, int size) // at "p", make a 'size' byte ho | |||
1901 | } | 1901 | } |
1902 | 1902 | ||
1903 | // close a hole in text[] | 1903 | // close a hole in text[] |
1904 | static char *text_hole_delete(char * p, char * q) // delete "p" through "q", inclusive | 1904 | static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive |
1905 | { | 1905 | { |
1906 | char *src, *dest; | 1906 | char *src, *dest; |
1907 | int cnt, hole_size; | 1907 | int cnt, hole_size; |
@@ -1937,7 +1937,7 @@ static char *text_hole_delete(char * p, char * q) // delete "p" through "q", inc | |||
1937 | // copy text into register, then delete text. | 1937 | // copy text into register, then delete text. |
1938 | // if dist <= 0, do not include, or go past, a NewLine | 1938 | // if dist <= 0, do not include, or go past, a NewLine |
1939 | // | 1939 | // |
1940 | static char *yank_delete(char * start, char * stop, int dist, int yf) | 1940 | static char *yank_delete(char *start, char *stop, int dist, int yf) |
1941 | { | 1941 | { |
1942 | char *p; | 1942 | char *p; |
1943 | 1943 | ||
@@ -2030,27 +2030,26 @@ static void end_cmd_q(void) | |||
2030 | #if ENABLE_FEATURE_VI_YANKMARK \ | 2030 | #if ENABLE_FEATURE_VI_YANKMARK \ |
2031 | || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \ | 2031 | || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \ |
2032 | || ENABLE_FEATURE_VI_CRASHME | 2032 | || ENABLE_FEATURE_VI_CRASHME |
2033 | static char *string_insert(char * p, char * s) // insert the string at 'p' | 2033 | static char *string_insert(char *p, char *s) // insert the string at 'p' |
2034 | { | 2034 | { |
2035 | int cnt, i; | 2035 | int cnt, i; |
2036 | 2036 | ||
2037 | i = strlen(s); | 2037 | i = strlen(s); |
2038 | if (text_hole_make(p, i)) { | 2038 | text_hole_make(p, i); |
2039 | strncpy(p, s, i); | 2039 | strncpy(p, s, i); |
2040 | for (cnt = 0; *s != '\0'; s++) { | 2040 | for (cnt = 0; *s != '\0'; s++) { |
2041 | if (*s == '\n') | 2041 | if (*s == '\n') |
2042 | cnt++; | 2042 | cnt++; |
2043 | } | 2043 | } |
2044 | #if ENABLE_FEATURE_VI_YANKMARK | 2044 | #if ENABLE_FEATURE_VI_YANKMARK |
2045 | status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg()); | 2045 | status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg()); |
2046 | #endif | 2046 | #endif |
2047 | } | ||
2048 | return p; | 2047 | return p; |
2049 | } | 2048 | } |
2050 | #endif | 2049 | #endif |
2051 | 2050 | ||
2052 | #if ENABLE_FEATURE_VI_YANKMARK | 2051 | #if ENABLE_FEATURE_VI_YANKMARK |
2053 | static char *text_yank(char * p, char * q, int dest) // copy text into a register | 2052 | static char *text_yank(char *p, char *q, int dest) // copy text into a register |
2054 | { | 2053 | { |
2055 | char *t; | 2054 | char *t; |
2056 | int cnt; | 2055 | int cnt; |
@@ -2392,7 +2391,7 @@ static int file_size(const char *fn) // what is the byte size of "fn" | |||
2392 | return cnt; | 2391 | return cnt; |
2393 | } | 2392 | } |
2394 | 2393 | ||
2395 | static int file_insert(const char * fn, char *p | 2394 | static int file_insert(const char *fn, char *p |
2396 | USE_FEATURE_VI_READONLY(, int update_ro_status)) | 2395 | USE_FEATURE_VI_READONLY(, int update_ro_status)) |
2397 | { | 2396 | { |
2398 | int cnt = -1; | 2397 | int cnt = -1; |
@@ -2404,18 +2403,11 @@ static int file_insert(const char * fn, char *p | |||
2404 | status_line_bold("\"%s\" %s", fn, strerror(errno)); | 2403 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
2405 | goto fi0; | 2404 | goto fi0; |
2406 | } | 2405 | } |
2407 | if ((statbuf.st_mode & S_IFREG) == 0) { | 2406 | if (!S_ISREG(statbuf.st_mode)) { |
2408 | // This is not a regular file | 2407 | // This is not a regular file |
2409 | status_line_bold("\"%s\" Not a regular file", fn); | 2408 | status_line_bold("\"%s\" Not a regular file", fn); |
2410 | goto fi0; | 2409 | goto fi0; |
2411 | } | 2410 | } |
2412 | /* // this check is done by open() | ||
2413 | if ((statbuf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) { | ||
2414 | // dont have any read permissions | ||
2415 | status_line_bold("\"%s\" Not readable", fn); | ||
2416 | goto fi0; | ||
2417 | } | ||
2418 | */ | ||
2419 | if (p < text || p > end) { | 2411 | if (p < text || p > end) { |
2420 | status_line_bold("Trying to insert file outside of memory"); | 2412 | status_line_bold("Trying to insert file outside of memory"); |
2421 | goto fi0; | 2413 | goto fi0; |
@@ -2429,8 +2421,6 @@ static int file_insert(const char * fn, char *p | |||
2429 | } | 2421 | } |
2430 | size = statbuf.st_size; | 2422 | size = statbuf.st_size; |
2431 | p = text_hole_make(p, size); | 2423 | p = text_hole_make(p, size); |
2432 | if (p == NULL) | ||
2433 | goto fi0; | ||
2434 | cnt = safe_read(fd, p, size); | 2424 | cnt = safe_read(fd, p, size); |
2435 | if (cnt < 0) { | 2425 | if (cnt < 0) { |
2436 | status_line_bold("\"%s\" %s", fn, strerror(errno)); | 2426 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
@@ -2458,8 +2448,7 @@ static int file_insert(const char * fn, char *p | |||
2458 | return cnt; | 2448 | return cnt; |
2459 | } | 2449 | } |
2460 | 2450 | ||
2461 | 2451 | static int file_write(char *fn, char *first, char *last) | |
2462 | static int file_write(char * fn, char * first, char * last) | ||
2463 | { | 2452 | { |
2464 | int fd, cnt, charcnt; | 2453 | int fd, cnt, charcnt; |
2465 | 2454 | ||