Commit 63fec213 63fec21357c175772a8ca153266919c12b6f8bd5 by Sergey Poznyakoff

Use wordsplit to expand command prompt in mu.

* mu/mu.h (mutool_prompt_vartab): Remove.
(mutool_prompt_env): New extern.
* mu/pop.c (pop_prompt_vartab): Rewrite as pop_prompt_env.
All callers changed.
* mu/shell.c (mutool_prompt_vartab): Remove variable.
(mutool_prompt_env): New variable.
(expand_prompt): Remove function.
(input_line_interactive): Use mu_wordsplit to expand command prompt.
1 parent 90ba2e7e
...@@ -40,7 +40,7 @@ int mutool_ldflags (int argc, char **argv); ...@@ -40,7 +40,7 @@ int mutool_ldflags (int argc, char **argv);
40 int mutool_cflags (int argc, char **argv); 40 int mutool_cflags (int argc, char **argv);
41 41
42 extern char *mutool_shell_prompt; 42 extern char *mutool_shell_prompt;
43 extern mu_vartab_t mutool_prompt_vartab; 43 extern char **mutool_prompt_env;
44 extern int mutool_shell_interactive; 44 extern int mutool_shell_interactive;
45 extern mu_stream_t mustrin, mustrout; 45 extern mu_stream_t mustrin, mustrout;
46 int mutool_shell (const char *name, struct mutool_command *cmd); 46 int mutool_shell (const char *name, struct mutool_command *cmd);
......
...@@ -152,26 +152,35 @@ pop_session_str (enum pop_session_status stat) ...@@ -152,26 +152,35 @@ pop_session_str (enum pop_session_status stat)
152 } 152 }
153 153
154 static void 154 static void
155 pop_prompt_vartab () 155 pop_prompt_env ()
156 { 156 {
157 mu_vartab_t vtab; 157 if (!mutool_prompt_env)
158 158 mutool_prompt_env = xcalloc (2*7 + 1, sizeof(mutool_prompt_env[0]));
159 if (mu_vartab_create (&vtab)) 159
160 return; 160 mutool_prompt_env[0] = "user";
161 mu_vartab_define (vtab, "user", 161 mutool_prompt_env[1] = (pop_session_status == pop_session_logged_in) ?
162 (pop_session_status == pop_session_logged_in) ? 162 username : "[nouser]";
163 username : "not logged in", 1); 163
164 mu_vartab_define (vtab, "host", 164 mutool_prompt_env[2] = "host";
165 (pop_session_status != pop_session_disconnected) ? 165 mutool_prompt_env[3] = (pop_session_status != pop_session_disconnected) ?
166 host : "not connected", 1); 166 host : "[nohost]";
167 mu_vartab_define (vtab, "program-name", mu_program_name, 1); 167
168 mu_vartab_define (vtab, "canonical-program-name", "mu", 1); 168 mutool_prompt_env[4] = "program-name";
169 mu_vartab_define (vtab, "package", PACKAGE, 1); 169 mutool_prompt_env[5] = (char*) mu_program_name;
170 mu_vartab_define (vtab, "version", PACKAGE_VERSION, 1); 170
171 mu_vartab_define (vtab, "status", pop_session_str (pop_session_status), 1); 171 mutool_prompt_env[6] = "canonical-program-name";
172 172 mutool_prompt_env[7] = "mu";
173 mu_vartab_destroy (&mutool_prompt_vartab); 173
174 mutool_prompt_vartab = vtab; 174 mutool_prompt_env[8] = "package";
175 mutool_prompt_env[9] = PACKAGE;
176
177 mutool_prompt_env[10] = "version";
178 mutool_prompt_env[11] = PACKAGE_VERSION;
179
180 mutool_prompt_env[12] = "status";
181 mutool_prompt_env[13] = (char*) pop_session_str (pop_session_status);
182
183 mutool_prompt_env[14] = NULL;
175 } 184 }
176 185
177 186
...@@ -295,7 +304,7 @@ com_user (int argc, char **argv) ...@@ -295,7 +304,7 @@ com_user (int argc, char **argv)
295 if (status == 0) 304 if (status == 0)
296 { 305 {
297 username = strdup (argv[1]); 306 username = strdup (argv[1]);
298 pop_prompt_vartab (); 307 pop_prompt_env ();
299 } 308 }
300 return status; 309 return status;
301 } 310 }
...@@ -495,7 +504,7 @@ com_pass (int argc, char **argv) ...@@ -495,7 +504,7 @@ com_pass (int argc, char **argv)
495 if (status == 0) 504 if (status == 0)
496 { 505 {
497 pop_session_status = pop_session_logged_in; 506 pop_session_status = pop_session_logged_in;
498 pop_prompt_vartab (); 507 pop_prompt_env ();
499 } 508 }
500 free (passbuf); 509 free (passbuf);
501 return status; 510 return status;
...@@ -593,7 +602,7 @@ com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED) ...@@ -593,7 +602,7 @@ com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
593 connect_argc = 0; 602 connect_argc = 0;
594 connect_argv = NULL; 603 connect_argv = NULL;
595 pop_session_status = pop_session_disconnected; 604 pop_session_status = pop_session_disconnected;
596 pop_prompt_vartab (); 605 pop_prompt_env ();
597 } 606 }
598 return 0; 607 return 0;
599 } 608 }
...@@ -689,7 +698,7 @@ com_connect (int argc, char **argv) ...@@ -689,7 +698,7 @@ com_connect (int argc, char **argv)
689 port = n; 698 port = n;
690 pop_session_status = pop_session_connected; 699 pop_session_status = pop_session_connected;
691 700
692 pop_prompt_vartab (); 701 pop_prompt_env ();
693 } 702 }
694 703
695 return status; 704 return status;
...@@ -794,7 +803,7 @@ mutool_pop (int argc, char **argv) ...@@ -794,7 +803,7 @@ mutool_pop (int argc, char **argv)
794 803
795 /* Command line prompt */ 804 /* Command line prompt */
796 mutool_shell_prompt = xstrdup ("pop> "); 805 mutool_shell_prompt = xstrdup ("pop> ");
797 pop_prompt_vartab (); 806 pop_prompt_env ();
798 mutool_shell ("pop", pop_comtab); 807 mutool_shell ("pop", pop_comtab);
799 return 0; 808 return 0;
800 } 809 }
......
...@@ -31,21 +31,10 @@ ...@@ -31,21 +31,10 @@
31 #endif 31 #endif
32 32
33 char *mutool_shell_prompt; 33 char *mutool_shell_prompt;
34 mu_vartab_t mutool_prompt_vartab; 34 char **mutool_prompt_env;
35 int mutool_shell_interactive; 35 int mutool_shell_interactive;
36 mu_stream_t mustrin, mustrout; 36 mu_stream_t mustrin, mustrout;
37 37
38 static char *
39 expand_prompt ()
40 {
41 char *str;
42
43 if (!mutool_prompt_vartab
44 || mu_vartab_expand (mutool_prompt_vartab, mutool_shell_prompt, &str))
45 str = strdup (mutool_shell_prompt);
46 return str;
47 }
48
49 38
50 static int shell_exit (int, char **); 39 static int shell_exit (int, char **);
51 static int shell_help (int, char **); 40 static int shell_help (int, char **);
...@@ -516,9 +505,22 @@ execute_line (char *line) ...@@ -516,9 +505,22 @@ execute_line (char *line)
516 static char * 505 static char *
517 input_line_interactive () 506 input_line_interactive ()
518 { 507 {
519 char *p = expand_prompt (); 508 char *line;
520 char *line = readline (p); 509 int wsflags = MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD;
521 free (p); 510 struct mu_wordsplit ws;
511
512 if (mutool_prompt_env)
513 {
514 ws.ws_env = (const char **)mutool_prompt_env;
515 wsflags |= MU_WRDSF_ENV | MU_WRDSF_ENV_KV;
516 }
517 if (mu_wordsplit (mutool_shell_prompt, &ws, wsflags))
518 line = readline (mutool_shell_prompt);
519 else
520 {
521 line = readline (ws.ws_wordv[0]);
522 mu_wordsplit_free (&ws);
523 }
522 return line; 524 return line;
523 } 525 }
524 526
......