diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-03-29 13:09:05 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-03-29 13:10:23 +0100 |
commit | 1d14569a66c741361afe0ebc6e2ef8ba9a6aeb71 (patch) | |
tree | e023ab33524a779ed23aa262759ce7c44935c868 /libbb | |
parent | 8f2cb7ab26b9c720c24cdeffb624bfe0c2352353 (diff) | |
download | busybox-w32-1d14569a66c741361afe0ebc6e2ef8ba9a6aeb71.tar.gz busybox-w32-1d14569a66c741361afe0ebc6e2ef8ba9a6aeb71.tar.bz2 busybox-w32-1d14569a66c741361afe0ebc6e2ef8ba9a6aeb71.zip |
lineedit: implement \T \t \A \@ prompts escapes, fix \W escape, drop \!
function old new delta
parse_and_put_prompt 742 793 +51
read_line_input 3836 3826 -10
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/lineedit.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 591bb6d5e..45448dfab 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -133,9 +133,6 @@ struct lineedit_statics { | |||
133 | CHAR_T *command_ps; | 133 | CHAR_T *command_ps; |
134 | 134 | ||
135 | const char *cmdedit_prompt; | 135 | const char *cmdedit_prompt; |
136 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT | ||
137 | int num_ok_lines; /* = 1; */ | ||
138 | #endif | ||
139 | 136 | ||
140 | #if ENABLE_USERNAME_OR_HOMEDIR | 137 | #if ENABLE_USERNAME_OR_HOMEDIR |
141 | char *user_buf; | 138 | char *user_buf; |
@@ -172,7 +169,6 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics; | |||
172 | #define command_len (S.command_len ) | 169 | #define command_len (S.command_len ) |
173 | #define command_ps (S.command_ps ) | 170 | #define command_ps (S.command_ps ) |
174 | #define cmdedit_prompt (S.cmdedit_prompt ) | 171 | #define cmdedit_prompt (S.cmdedit_prompt ) |
175 | #define num_ok_lines (S.num_ok_lines ) | ||
176 | #define user_buf (S.user_buf ) | 172 | #define user_buf (S.user_buf ) |
177 | #define home_pwd_buf (S.home_pwd_buf ) | 173 | #define home_pwd_buf (S.home_pwd_buf ) |
178 | #define matches (S.matches ) | 174 | #define matches (S.matches ) |
@@ -185,7 +181,6 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics; | |||
185 | (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \ | 181 | (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \ |
186 | barrier(); \ | 182 | barrier(); \ |
187 | cmdedit_termw = 80; \ | 183 | cmdedit_termw = 80; \ |
188 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \ | ||
189 | IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \ | 184 | IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \ |
190 | IF_FEATURE_EDITING_VI(delptr = delbuf;) \ | 185 | IF_FEATURE_EDITING_VI(delptr = delbuf;) \ |
191 | } while (0) | 186 | } while (0) |
@@ -1532,7 +1527,6 @@ static void remember_in_history(char *str) | |||
1532 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT | 1527 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
1533 | save_history(str); | 1528 | save_history(str); |
1534 | # endif | 1529 | # endif |
1535 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;) | ||
1536 | } | 1530 | } |
1537 | 1531 | ||
1538 | #else /* MAX_HISTORY == 0 */ | 1532 | #else /* MAX_HISTORY == 0 */ |
@@ -1768,17 +1762,16 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1768 | size_t cur_prmt_len = 0; | 1762 | size_t cur_prmt_len = 0; |
1769 | char flg_not_length = '['; | 1763 | char flg_not_length = '['; |
1770 | char *prmt_mem_ptr = xzalloc(1); | 1764 | char *prmt_mem_ptr = xzalloc(1); |
1771 | char *cwd_buf = xrealloc_getcwd_or_warn(NULL); | 1765 | # if ENABLE_USERNAME_OR_HOMEDIR |
1766 | char *cwd_buf = NULL; | ||
1767 | # endif | ||
1768 | char timebuf[sizeof("HH:MM:SS")]; | ||
1772 | char cbuf[2]; | 1769 | char cbuf[2]; |
1773 | char c; | 1770 | char c; |
1774 | char *pbuf; | 1771 | char *pbuf; |
1775 | 1772 | ||
1776 | cmdedit_prmt_len = 0; | 1773 | cmdedit_prmt_len = 0; |
1777 | 1774 | ||
1778 | if (!cwd_buf) { | ||
1779 | cwd_buf = (char *)bb_msg_unknown; | ||
1780 | } | ||
1781 | |||
1782 | cbuf[1] = '\0'; /* never changes */ | 1775 | cbuf[1] = '\0'; /* never changes */ |
1783 | 1776 | ||
1784 | while (*prmt_ptr) { | 1777 | while (*prmt_ptr) { |
@@ -1787,7 +1780,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1787 | pbuf = cbuf; | 1780 | pbuf = cbuf; |
1788 | c = *prmt_ptr++; | 1781 | c = *prmt_ptr++; |
1789 | if (c == '\\') { | 1782 | if (c == '\\') { |
1790 | const char *cp = prmt_ptr; | 1783 | const char *cp; |
1791 | int l; | 1784 | int l; |
1792 | /* | 1785 | /* |
1793 | * Supported via bb_process_escape_sequence: | 1786 | * Supported via bb_process_escape_sequence: |
@@ -1799,35 +1792,38 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1799 | * \nnn char with octal code nnn | 1792 | * \nnn char with octal code nnn |
1800 | * Supported: | 1793 | * Supported: |
1801 | * \$ if the effective UID is 0, a #, otherwise a $ | 1794 | * \$ if the effective UID is 0, a #, otherwise a $ |
1802 | * \! history number of this command | ||
1803 | * (buggy?) | ||
1804 | * \w current working directory, with $HOME abbreviated with a tilde | 1795 | * \w current working directory, with $HOME abbreviated with a tilde |
1805 | * Note: we do not support $PROMPT_DIRTRIM=n feature | 1796 | * Note: we do not support $PROMPT_DIRTRIM=n feature |
1797 | * \W basename of the current working directory, with $HOME abbreviated with a tilde | ||
1806 | * \h hostname up to the first '.' | 1798 | * \h hostname up to the first '.' |
1807 | * \H hostname | 1799 | * \H hostname |
1808 | * \u username | 1800 | * \u username |
1809 | * \[ begin a sequence of non-printing characters | 1801 | * \[ begin a sequence of non-printing characters |
1810 | * \] end a sequence of non-printing characters | 1802 | * \] end a sequence of non-printing characters |
1803 | * \T current time in 12-hour HH:MM:SS format | ||
1804 | * \@ current time in 12-hour am/pm format | ||
1805 | * \A current time in 24-hour HH:MM format | ||
1806 | * \t current time in 24-hour HH:MM:SS format | ||
1807 | * (all of the above work as \A) | ||
1811 | * Not supported: | 1808 | * Not supported: |
1809 | * \! history number of this command | ||
1812 | * \# command number of this command | 1810 | * \# command number of this command |
1813 | * \j number of jobs currently managed by the shell | 1811 | * \j number of jobs currently managed by the shell |
1814 | * \l basename of the shell's terminal device name | 1812 | * \l basename of the shell's terminal device name |
1815 | * \s name of the shell, the basename of $0 (the portion following the final slash) | 1813 | * \s name of the shell, the basename of $0 (the portion following the final slash) |
1816 | * \V release of bash, version + patch level (e.g., 2.00.0) | 1814 | * \V release of bash, version + patch level (e.g., 2.00.0) |
1817 | * \W basename of the current working directory, with $HOME abbreviated with a tilde | ||
1818 | * \d date in "Weekday Month Date" format (e.g., "Tue May 26") | 1815 | * \d date in "Weekday Month Date" format (e.g., "Tue May 26") |
1819 | * \D{format} | 1816 | * \D{format} |
1820 | * format is passed to strftime(3). | 1817 | * format is passed to strftime(3). |
1821 | * An empty format results in a locale-specific time representation. | 1818 | * An empty format results in a locale-specific time representation. |
1822 | * The braces are required. | 1819 | * The braces are required. |
1823 | * \T current time in 12-hour HH:MM:SS format | ||
1824 | * \@ current time in 12-hour am/pm format | ||
1825 | * \A current time in 24-hour HH:MM format | ||
1826 | * Mishandled by bb_process_escape_sequence: | 1820 | * Mishandled by bb_process_escape_sequence: |
1827 | * \t current time in 24-hour HH:MM:SS format | ||
1828 | * \v version of bash (e.g., 2.00) | 1821 | * \v version of bash (e.g., 2.00) |
1829 | */ | 1822 | */ |
1830 | c = bb_process_escape_sequence(&prmt_ptr); | 1823 | cp = prmt_ptr; |
1824 | c = *cp; | ||
1825 | if (c != 't') /* don't treat \t as tab */ | ||
1826 | c = bb_process_escape_sequence(&prmt_ptr); | ||
1831 | if (prmt_ptr == cp) { | 1827 | if (prmt_ptr == cp) { |
1832 | if (*cp == '\0') | 1828 | if (*cp == '\0') |
1833 | break; | 1829 | break; |
@@ -1848,29 +1844,41 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1848 | case '$': | 1844 | case '$': |
1849 | c = (geteuid() == 0 ? '#' : '$'); | 1845 | c = (geteuid() == 0 ? '#' : '$'); |
1850 | break; | 1846 | break; |
1847 | case 'T': /* 12-hour HH:MM:SS format */ | ||
1848 | case '@': /* 12-hour am/pm format */ | ||
1849 | case 'A': /* 24-hour HH:MM format */ | ||
1850 | case 't': /* 24-hour HH:MM:SS format */ | ||
1851 | /* We show all of them as 24-hour HH:MM */ | ||
1852 | strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0'; | ||
1853 | pbuf = timebuf; | ||
1854 | break; | ||
1851 | # if ENABLE_USERNAME_OR_HOMEDIR | 1855 | # if ENABLE_USERNAME_OR_HOMEDIR |
1852 | case 'w': | 1856 | case 'w': /* current dir */ |
1853 | /* /home/user[/something] -> ~[/something] */ | 1857 | case 'W': /* basename of cur dir */ |
1854 | pbuf = cwd_buf; | 1858 | if (!cwd_buf) { |
1855 | l = strlen(home_pwd_buf); | 1859 | cwd_buf = xrealloc_getcwd_or_warn(NULL); |
1856 | if (l != 0 | 1860 | if (!cwd_buf) |
1857 | && strncmp(home_pwd_buf, cwd_buf, l) == 0 | 1861 | cwd_buf = (char *)bb_msg_unknown; |
1858 | && (cwd_buf[l]=='/' || cwd_buf[l]=='\0') | 1862 | else { |
1859 | && strlen(cwd_buf + l) < PATH_MAX | 1863 | /* /home/user[/something] -> ~[/something] */ |
1860 | ) { | 1864 | l = strlen(home_pwd_buf); |
1861 | pbuf = free_me = xasprintf("~%s", cwd_buf + l); | 1865 | if (l != 0 |
1866 | && strncmp(home_pwd_buf, cwd_buf, l) == 0 | ||
1867 | && (cwd_buf[l] == '/' || cwd_buf[l] == '\0') | ||
1868 | ) { | ||
1869 | cwd_buf[0] = '~'; | ||
1870 | overlapping_strcpy(cwd_buf + 1, cwd_buf + l); | ||
1871 | } | ||
1872 | } | ||
1862 | } | 1873 | } |
1863 | break; | ||
1864 | # endif | ||
1865 | case 'W': | ||
1866 | pbuf = cwd_buf; | 1874 | pbuf = cwd_buf; |
1875 | if (c == 'w') | ||
1876 | break; | ||
1867 | cp = strrchr(pbuf, '/'); | 1877 | cp = strrchr(pbuf, '/'); |
1868 | if (cp != NULL && cp != pbuf) | 1878 | if (cp != NULL && cp != pbuf) |
1869 | pbuf += (cp-pbuf) + 1; | 1879 | pbuf = (char*)cp + 1; |
1870 | break; | ||
1871 | case '!': | ||
1872 | pbuf = free_me = xasprintf("%d", num_ok_lines); | ||
1873 | break; | 1880 | break; |
1881 | # endif | ||
1874 | // bb_process_escape_sequence does this now: | 1882 | // bb_process_escape_sequence does this now: |
1875 | // case 'e': case 'E': /* \e \E = \033 */ | 1883 | // case 'e': case 'E': /* \e \E = \033 */ |
1876 | // c = '\033'; | 1884 | // c = '\033'; |
@@ -1912,8 +1920,10 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1912 | free(free_me); | 1920 | free(free_me); |
1913 | } /* while */ | 1921 | } /* while */ |
1914 | 1922 | ||
1923 | # if ENABLE_USERNAME_OR_HOMEDIR | ||
1915 | if (cwd_buf != (char *)bb_msg_unknown) | 1924 | if (cwd_buf != (char *)bb_msg_unknown) |
1916 | free(cwd_buf); | 1925 | free(cwd_buf); |
1926 | # endif | ||
1917 | cmdedit_prompt = prmt_mem_ptr; | 1927 | cmdedit_prompt = prmt_mem_ptr; |
1918 | put_prompt(); | 1928 | put_prompt(); |
1919 | } | 1929 | } |