19
19
#include "postgres_fe.h"
20
20
21
21
#include "psqlscanslash.h"
22
+ #include "settings.h"
23
+
22
24
#include "common/logging.h"
23
25
#include "fe_utils/conditional.h"
24
26
@@ -772,6 +774,7 @@ evaluate_backtick(PsqlScanState state)
772
774
PQExpBufferData cmd_output;
773
775
FILE *fd;
774
776
bool error = false ;
777
+ int exit_code = 0 ;
775
778
char buf[512 ];
776
779
size_t result;
777
780
@@ -783,6 +786,7 @@ evaluate_backtick(PsqlScanState state)
783
786
{
784
787
pg_log_error (" %s: %m" , cmd);
785
788
error = true ;
789
+ exit_code = -1 ;
786
790
}
787
791
788
792
if (!error)
@@ -800,10 +804,19 @@ evaluate_backtick(PsqlScanState state)
800
804
} while (!feof (fd));
801
805
}
802
806
803
- if (fd && pclose (fd) == - 1 )
807
+ if (fd)
804
808
{
805
- pg_log_error (" %s: %m" , cmd);
806
- error = true ;
809
+ /*
810
+ * Although pclose's result always sets SHELL_EXIT_CODE, we
811
+ * historically have abandoned the backtick substitution only if it
812
+ * returns -1.
813
+ */
814
+ exit_code = pclose (fd);
815
+ if (exit_code == -1 )
816
+ {
817
+ pg_log_error (" %s: %m" , cmd);
818
+ error = true ;
819
+ }
807
820
}
808
821
809
822
if (PQExpBufferDataBroken (cmd_output))
@@ -826,5 +839,10 @@ evaluate_backtick(PsqlScanState state)
826
839
appendBinaryPQExpBuffer (output_buf, cmd_output.data , cmd_output.len );
827
840
}
828
841
842
+ /* And finally, set the shell error variables */
843
+ snprintf (buf, sizeof (buf), " %d" , wait_result_to_exit_code (exit_code));
844
+ SetVariable (pset.vars , " SHELL_EXIT_CODE" , buf);
845
+ SetVariable (pset.vars , " SHELL_ERROR" , (exit_code == 0 ) ? " false" : " true" );
846
+
829
847
termPQExpBuffer (&cmd_output);
830
848
}
0 commit comments