• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision4140 (tree)
Time2020-11-05 20:57:52
Authormagicant

Log Message

Split exec_simple_command

Into the new two functions.

Change Summary

Incremental Difference

--- yash/trunk/exec.c (revision 4139)
+++ yash/trunk/exec.c (revision 4140)
@@ -159,6 +159,11 @@
159159 __attribute__((nonnull));
160160 static void exec_simple_command(const command_T *c, bool finally_exit)
161161 __attribute__((nonnull));
162+static bool exec_simple_command_without_words(const command_T *c)
163+ __attribute__((nonnull,warn_unused_result));
164+static bool exec_simple_command_with_words(
165+ const command_T *c, int argc, void **argv, bool finally_exit)
166+ __attribute__((nonnull,warn_unused_result));
162167 static void print_xtrace(void *const *argv);
163168 static void search_command(
164169 const char *restrict name, const wchar_t *restrict wname,
@@ -665,15 +670,69 @@
665670 if (is_interrupted())
666671 goto done1;
667672
668- char *argv0; // a multi-byte version of argv[0]
669- if (argc == 0) {
670- argv0 = NULL;
673+ /* execute the remaining part */
674+ if (argc == 0)
675+ finally_exit |= exec_simple_command_without_words(c);
676+ else
677+ finally_exit |=
678+ exec_simple_command_with_words(c, argc, argv, finally_exit);
679+
680+ /* cleanup */
681+done1:
682+ plfree(argv, free);
683+done:
684+ if (finally_exit)
685+ exit_shell();
686+}
687+
688+/* Executes the simple command that has no expanded words.
689+ * Returns true if the shell should exit. */
690+bool exec_simple_command_without_words(const command_T *c)
691+{
692+ bool finally_exit = false;
693+
694+ /* open redirections */
695+ savefd_T *savefd;
696+ if (!open_redirections(c->c_redirs, &savefd)) {
697+ laststatus = Exit_REDIRERR;
698+ apply_errexit_errreturn(NULL);
699+ goto done;
700+ }
701+
702+ /* perform assignments */
703+ last_assign = c->c_assigns;
704+ if (do_assignments(c->c_assigns, false, shopt_allexport)) {
705+ laststatus = lastcmdsubstatus;
671706 } else {
672- argv0 = malloc_wcstombs(argv[0]);
673- if (argv0 == NULL)
674- argv0 = xstrdup("");
707+ laststatus = Exit_ASSGNERR;
708+ if (!is_interactive_now)
709+ finally_exit = true;
675710 }
711+ print_xtrace(NULL);
676712
713+ /* cleanup */
714+done:
715+ undo_redirections(savefd);
716+
717+ return finally_exit;
718+}
719+
720+/* Executes the simple command that has one or more expanded words.
721+ * `argv' must be a NULL-terminated array of pointers to wide strings that are
722+ * the results of the word expansion on the simple command being executed.
723+ * `argc' must be the number of words in `argv', which must be at least 1.
724+ * If `finally_exit' is true, the shell process may be replaced by the command
725+ * process. However, this function still may return in some cases.
726+ * Returns true if the shell should exit. */
727+bool exec_simple_command_with_words(
728+ const command_T *c, int argc, void **argv, bool finally_exit)
729+{
730+ assert(argc > 0);
731+
732+ char *argv0 = malloc_wcstombs(argv[0]);
733+ if (argv0 == NULL)
734+ argv0 = xstrdup("");
735+
677736 /* open redirections */
678737 savefd_T *savefd;
679738 if (!open_redirections(c->c_redirs, &savefd)) {
@@ -680,25 +739,12 @@
680739 /* On redirection error, the command is not executed. */
681740 laststatus = Exit_REDIRERR;
682741 apply_errexit_errreturn(NULL);
683- if (posixly_correct && !is_interactive_now &&
684- argc > 0 && is_special_builtin(argv0))
742+ if (posixly_correct && !is_interactive_now && is_special_builtin(argv0))
685743 finally_exit = true;
686- goto done2;
744+ goto done;
687745 }
688746
689747 last_assign = c->c_assigns;
690- if (argc == 0) {
691- /* if there is no command word, just perform assignments */
692- if (do_assignments(c->c_assigns, false, shopt_allexport)) {
693- laststatus = lastcmdsubstatus;
694- } else {
695- laststatus = Exit_ASSGNERR;
696- if (!is_interactive_now)
697- finally_exit = true;
698- }
699- print_xtrace(NULL);
700- goto done2;
701- }
702748
703749 /* check if the command is a special built-in or function */
704750 commandinfo_T cmdinfo;
@@ -717,7 +763,7 @@
717763 laststatus = Exit_ASSGNERR;
718764 if (!is_interactive_now)
719765 finally_exit = true;
720- goto done3;
766+ goto done1;
721767 }
722768 print_xtrace(argv);
723769
@@ -727,7 +773,7 @@
727773 SCT_EXTERNAL | SCT_BUILTIN | SCT_CHECK);
728774 if (cmdinfo.type == CT_NONE) {
729775 if (!posixly_correct && command_not_found_handler(argv))
730- goto done3;
776+ goto done1;
731777 if (wcschr(argv[0], L'/') != NULL) {
732778 cmdinfo.type = CT_EXTERNALPROGRAM;
733779 cmdinfo.ci_path = argv0;
@@ -750,17 +796,14 @@
750796 exec_builtin_executed = false;
751797
752798 /* cleanup */
753-done3:
799+done1:
754800 if (temp)
755801 close_current_environment();
756-done2:
802+done:
757803 undo_redirections(savefd);
758804 free(argv0);
759-done1:
760- plfree(argv, free);
761-done:
762- if (finally_exit)
763- exit_shell();
805+
806+ return finally_exit;
764807 }
765808
766809 /* Returns a pointer to the xtrace buffer.
Show on old repository browser