diff options
Diffstat (limited to 'src/bin/psql/common.c')
-rw-r--r-- | src/bin/psql/common.c | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f907f5d4e8d..c0e6e8e6ed1 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -103,7 +103,7 @@ setQFout(const char *fname) if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr) { if (pset.queryFoutPipe) - pclose(pset.queryFout); + SetShellResultVariables(pclose(pset.queryFout)); else fclose(pset.queryFout); } @@ -450,6 +450,26 @@ SetResultVariables(PGresult *result, bool success) /* + * Set special variables from a shell command result + * - SHELL_ERROR: true/false, whether command returned exit code 0 + * - SHELL_EXIT_CODE: exit code according to shell conventions + * + * The argument is a wait status as returned by wait(2) or waitpid(2), + * which also applies to pclose(3) and system(3). + */ +void +SetShellResultVariables(int wait_result) +{ + char buf[32]; + + SetVariable(pset.vars, "SHELL_ERROR", + (wait_result == 0) ? "false" : "true"); + snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result)); + SetVariable(pset.vars, "SHELL_EXIT_CODE", buf); +} + + +/* * ClearOrSaveResult * * If the result represents an error, remember it for possible display by @@ -1652,7 +1672,7 @@ ExecQueryAndProcessResults(const char *query, { if (gfile_is_pipe) { - pclose(gfile_fout); + SetShellResultVariables(pclose(gfile_fout)); restore_sigpipe_trap(); } else @@ -1870,7 +1890,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) /* close \g argument file/pipe */ if (is_pipe) { - pclose(fout); + SetShellResultVariables(pclose(fout)); restore_sigpipe_trap(); } else |