@@ -64,6 +64,8 @@ static wchar_t *expand_ps1_posix(wchar_t *s)
64
64
__attribute__((nonnull ,malloc ,warn_unused_result ));
65
65
static inline wchar_t get_euid_marker (void )
66
66
__attribute__((pure ));
67
+ static wchar_t * post_prompt_command (wchar_t * line )
68
+ __attribute__((nonnull ,malloc ,warn_unused_result ));
67
69
68
70
/* An input function that inputs from a wide string.
69
71
* `inputinfo' must be a pointer to a `struct input_wcs_info_T'.
@@ -288,6 +290,8 @@ inputresult_T input_interactive(struct xwcsbuf_T *buf, void *inputinfo)
288
290
#endif
289
291
if (info -> prompttype == 1 )
290
292
info -> prompttype = 2 ;
293
+ if (!posixly_correct )
294
+ line = post_prompt_command (line );
291
295
#if YASH_ENABLE_HISTORY
292
296
add_history (line );
293
297
#endif
@@ -481,6 +485,39 @@ wchar_t get_euid_marker(void)
481
485
return geteuid () == 0 ? L'#' : L'$' ;
482
486
}
483
487
488
+ /* Executes $POST_PROMPT_COMMAND, if any.
489
+ * `line' is the just input command line, which will be assigned to $COMMAND
490
+ * during the execution. The post-prompt command may modify or unset the
491
+ * variable. The final value of the variable is returned as a newly malloced
492
+ * string. `line' is freed in this function. */
493
+ wchar_t * post_prompt_command (wchar_t * line )
494
+ {
495
+ // If `line` ends with a newline, trim it here and append it back later.
496
+ size_t linelen = wcslen (line );
497
+ bool newline = linelen > 0 && line [linelen - 1 ] == L'\n' ;
498
+ if (newline )
499
+ line [linelen - 1 ] = L'\0' ;
500
+
501
+ open_new_environment (false);
502
+ set_positional_parameters ((void * []) { NULL });
503
+ set_variable (L VAR_COMMAND , line , SCOPE_LOCAL , false );
504
+
505
+ exec_variable_as_auxiliary_ (VAR_POST_PROMPT_COMMAND );
506
+ const wchar_t * c = getvar (L VAR_COMMAND );
507
+ if (c == NULL )
508
+ c = L"" ;
509
+
510
+ xwcsbuf_T linebuf ;
511
+ wb_init (& linebuf );
512
+ wb_cat (& linebuf , c );
513
+ if (newline )
514
+ wb_wccat (& linebuf , L'\n' );
515
+
516
+ close_current_environment ();
517
+
518
+ return wb_towcs (& linebuf );
519
+ }
520
+
484
521
/* Unsets O_NONBLOCK flag of the specified file descriptor.
485
522
* If `fd' is negative, does nothing.
486
523
* Returns true if successful. On error, `errno' is set and false is returned.*/
0 commit comments