aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src/util.cpp
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-06-05 23:33:44 +0200
committerjaseg <git@jaseg.de>2021-06-05 23:33:44 +0200
commit4ffb4c65825ab0a5ded00ff799b494097e7bd143 (patch)
tree68791213543bd330a094c67469a1de745c36867a /svg-flatten/src/util.cpp
parentf2f0ac241672bbaac4ccb9eaa369c501951de949 (diff)
downloadgerbolyze-4ffb4c65825ab0a5ded00ff799b494097e7bd143.tar.gz
gerbolyze-4ffb4c65825ab0a5ded00ff799b494097e7bd143.tar.bz2
gerbolyze-4ffb4c65825ab0a5ded00ff799b494097e7bd143.zip
Make svg-flatten auto-search for cargo dependencies in all call sites
Diffstat (limited to 'svg-flatten/src/util.cpp')
-rw-r--r--svg-flatten/src/util.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/svg-flatten/src/util.cpp b/svg-flatten/src/util.cpp
new file mode 100644
index 0000000..e88f4e9
--- /dev/null
+++ b/svg-flatten/src/util.cpp
@@ -0,0 +1,84 @@
+
+#include <pwd.h>
+#include <sys/types.h>
+#include <string>
+#include <iostream>
+
+#include <subprocess.h>
+#include <filesystem>
+
+#include "util.h"
+
+using namespace std;
+
+int gerbolyze::run_cargo_command(const char *cmd_name, const char *cmdline[], const char *envvar) {
+ const char *homedir;
+ if ((homedir = getenv("HOME")) == NULL) {
+ homedir = getpwuid(getuid())->pw_dir;
+ }
+ string homedir_s(homedir);
+ string cargo_bin_dir = homedir_s + "/.cargo/bin/" + cmd_name;
+
+ bool found = false;
+ int proc_rc = -1;
+ for (int i=0; i<3; i++) {
+ const char *envvar_val;
+ switch (i) {
+ case 0:
+ if ((envvar_val = getenv(envvar)) == NULL) {
+ continue;
+ } else {
+ cmdline[0] = envvar_val;
+ }
+ break;
+
+ case 1:
+ cmdline[0] = cmd_name;
+ break;
+
+ case 2:
+ cmdline[0] = cargo_bin_dir.c_str();
+ break;
+ }
+
+ struct subprocess_s subprocess;
+ int rc = subprocess_create(cmdline, subprocess_option_inherit_environment, &subprocess);
+ if (rc) {
+ cerr << "Error calling " << cmd_name << endl;
+ return EXIT_FAILURE;
+ }
+
+ proc_rc = -1;
+ rc = subprocess_join(&subprocess, &proc_rc);
+ if (rc) {
+ cerr << "Error calling " << cmd_name << endl;
+ return EXIT_FAILURE;
+ }
+
+ rc = subprocess_destroy(&subprocess);
+ if (rc) {
+ cerr << "Error calling " << cmd_name << endl;
+ return EXIT_FAILURE;
+ }
+
+ /* Fail if the command given in the environment variable could not be found. */
+ if (i > 0 && proc_rc == 255) {
+ continue;
+ }
+ found = true;
+ break;
+ }
+
+ if (!found) {
+ cerr << "Error: Cannot find " << cmd_name << ". Is it installed and in $PATH?" << endl;
+ return EXIT_FAILURE;
+ }
+
+ if (proc_rc) {
+ cerr << cmd_name << " returned an error code: " << proc_rc << endl;
+ return EXIT_FAILURE;
+ }
+
+ return 0;
+}
+