ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
external_tool.h
Go to the documentation of this file.
1/**
2 * @file external_tool.h
3 * @brief Helpers for invoking external command-line tools.
4 *
5 * @c run_external_tool() wraps @c system() so that the @c provsql.tool_search_path
6 * GUC, if set, is prepended to @c $PATH for the duration of the call. The
7 * server's pre-existing @c PATH is restored afterwards. Used by the d-DNNF
8 * compilers (d4, c2d, minic2d, dsharp), the WeightMC model counter, and the
9 * graph-easy DOT renderer.
10 *
11 * @c find_external_tool() walks the same locations and reports whether a
12 * given binary is present and executable, so callers can fail with an
13 * actionable error before composing a command line.
14 *
15 * @c format_external_tool_status() decodes a @c system() return value
16 * into a human-readable message that distinguishes "tool not found"
17 * (shell exit 127), "tool not executable" (126), "killed by signal", and
18 * "ran and exited nonzero".
19 *
20 * In the standalone @c tdkc build (when @c TDKC is defined) the GUC layer is
21 * unavailable; the helpers degenerate to a plain @c std::system() call and
22 * a no-op @c find_external_tool.
23 */
24#ifndef PROVSQL_EXTERNAL_TOOL_H
25#define PROVSQL_EXTERNAL_TOOL_H
26
27#include <string>
28
29#ifdef TDKC
30#include <cstdlib>
31inline int run_external_tool(const std::string &cmdline) {
32 return std::system(cmdline.c_str());
33}
34inline std::string find_external_tool(const std::string &) {
35 // No GUC layer in tdkc; assume present and let system() handle missing.
36 return "/dev/null";
37}
38inline std::string format_external_tool_status(int, const std::string &tool) {
39 return "Error executing " + tool;
40}
41#else
42/**
43 * @brief Run a shell command line, optionally extending @c PATH.
44 *
45 * If @c provsql_tool_search_path is non-empty, it is prepended (with @c :)
46 * to @c $PATH before @c system() is called and restored afterwards.
47 *
48 * @param cmdline Shell command line, passed verbatim to @c /bin/sh -c.
49 * @return The raw return value of @c system().
50 */
51int run_external_tool(const std::string &cmdline);
52
53/**
54 * @brief Locate an external tool by name.
55 *
56 * Searches @c provsql_tool_search_path (colon-separated), then @c $PATH, for
57 * an executable file matching @p name (via @c access(X_OK)). Returns the
58 * full path on success, or the empty string if nothing matches.
59 *
60 * Names containing a slash are treated as paths and tested directly without
61 * any directory walk.
62 *
63 * @param name Bare executable name (e.g. @c "d4") or a path.
64 * @return Resolved path, or @c "" if @p name is not found.
65 */
66std::string find_external_tool(const std::string &name);
67
68/**
69 * @brief Decode a @c system() return value into a human-readable message.
70 *
71 * @param rv Return value from @c system() (or @c run_external_tool()).
72 * @param tool Tool name, used as the message subject.
73 * @return Empty string when @p rv indicates success; otherwise a
74 * message that distinguishes "not found at runtime"
75 * (shell exit 127), "not executable" (126), "killed by
76 * signal N", "exited with status N", or @c system()
77 * itself failing.
78 */
79std::string format_external_tool_status(int rv, const std::string &tool);
80#endif
81
82#endif
int run_external_tool(const std::string &cmdline)
Run a shell command line, optionally extending PATH.
std::string format_external_tool_status(int rv, const std::string &tool)
Decode a system() return value into a human-readable message.
std::string find_external_tool(const std::string &name)
Locate an external tool by name.