aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/blogc-make/exec.c')
-rw-r--r--src/blogc-make/exec.c208
1 files changed, 103 insertions, 105 deletions
diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c
index 74af2ed..89d4b0a 100644
--- a/src/blogc-make/exec.c
+++ b/src/blogc-make/exec.c
@@ -19,13 +19,11 @@
#include <sysexits.h>
#include <errno.h>
#include <libgen.h>
-#include "../common/compat.h"
-#include "../common/error.h"
-#include "../common/file.h"
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "ctx.h"
-#include "exec.h"
#include "settings.h"
+#include "exec.h"
char*
@@ -36,14 +34,14 @@ bm_exec_find_binary(const char *argv0, const char *bin, const char *env)
// argv0, because the static binary may not be named `blogc`, and we
// prefer to use our own `blogc` instead of some other version around.
if (argv0 != NULL && bin != NULL && (0 == strcmp(bin, "blogc"))) {
- return bc_shell_quote(argv0);
+ return sb_shell_quote(argv0);
}
#endif
// first try: env var
const char *env_bin = getenv(env);
if (env_bin != NULL) {
- return bc_shell_quote(env_bin);
+ return sb_shell_quote(env_bin);
}
// second try: same dir as current exec
@@ -55,13 +53,13 @@ bm_exec_find_binary(const char *argv0, const char *bin, const char *env)
// that relative paths will work as expected.
// - windows path sep is not supported
if (argv0 != NULL && (NULL != strchr(argv0, '/'))) {
- char *path = bc_strdup(argv0);
- char *dir = bc_strdup(dirname(path));
+ char *path = sb_strdup(argv0);
+ char *dir = sb_strdup(dirname(path));
free(path);
- char *tmp = bc_strdup_printf("%s/%s", dir, bin);
+ char *tmp = sb_strdup_printf("%s/%s", dir, bin);
free(dir);
if (0 == access(tmp, X_OK)) {
- char *rv = bc_shell_quote(tmp);
+ char *rv = sb_shell_quote(tmp);
free(tmp);
return rv;
}
@@ -69,28 +67,28 @@ bm_exec_find_binary(const char *argv0, const char *bin, const char *env)
}
// last try: $PATH
- return bc_strdup(bin);
+ return sb_strdup(bin);
}
int
bm_exec_command(const char *cmd, const char *input, char **output,
- char **error, bc_error_t **err)
+ char **error, sb_error_t **err)
{
if (err == NULL || *err != NULL)
return 1;
int fd_in[2];
if (-1 == pipe(fd_in)) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to create stdin pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to create stdin pipe: %s", strerror(errno));
return 1;
}
int fd_out[2];
if (-1 == pipe(fd_out)) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to create stdout pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to create stdout pipe: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
return 1;
@@ -98,8 +96,8 @@ bm_exec_command(const char *cmd, const char *input, char **output,
int fd_err[2];
if (-1 == pipe(fd_err)) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to create stderr pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to create stderr pipe: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
close(fd_out[0]);
@@ -109,8 +107,8 @@ bm_exec_command(const char *cmd, const char *input, char **output,
pid_t pid = fork();
if (pid == -1) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to fork: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to fork: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
close(fd_out[0]);
@@ -149,8 +147,8 @@ bm_exec_command(const char *cmd, const char *input, char **output,
if (input != NULL) {
if (-1 == write(fd_in[1], input, strlen(input))) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to write to stdin pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to write to stdin pipe: %s", strerror(errno));
close(fd_in[1]);
close(fd_out[0]);
close(fd_err[0]);
@@ -160,145 +158,145 @@ bm_exec_command(const char *cmd, const char *input, char **output,
close(fd_in[1]);
- char buffer[BC_FILE_CHUNK_SIZE];
+ char buffer[128];
ssize_t s;
- bc_string_t *out = NULL;
- while(0 != (s = read(fd_out[0], buffer, BC_FILE_CHUNK_SIZE))) {
+ sb_string_t *out = NULL;
+ while(0 != (s = read(fd_out[0], buffer, 128))) {
if (s == -1) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to read from stdout pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to read from stdout pipe: %s", strerror(errno));
close(fd_out[0]);
close(fd_err[0]);
- bc_string_free(out, true);
+ sb_string_free(out, true);
return 1;
}
if (out == NULL) {
- out = bc_string_new();
+ out = sb_string_new();
}
- bc_string_append_len(out, buffer, s);
+ sb_string_append_len(out, buffer, s);
}
if (out != NULL) {
- *output = bc_string_free(out, false);
+ *output = sb_string_free(out, false);
}
close(fd_out[0]);
out = NULL;
- while(0 != (s = read(fd_err[0], buffer, BC_FILE_CHUNK_SIZE))) {
+ while(0 != (s = read(fd_err[0], buffer, 128))) {
if (s == -1) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
- "Failed to read from stderr pipe: %s", strerror(errno));
+ *err = sb_strerror_new_printf(
+ "exec: Failed to read from stderr pipe: %s", strerror(errno));
close(fd_err[0]);
- bc_string_free(out, true);
+ sb_string_free(out, true);
return 1;
}
if (out == NULL)
- out = bc_string_new();
- bc_string_append_len(out, buffer, s);
+ out = sb_string_new();
+ sb_string_append_len(out, buffer, s);
}
if (out != NULL) {
- *error = bc_string_free(out, false);
+ *error = sb_string_free(out, false);
}
close(fd_err[0]);
int status;
waitpid(pid, &status, 0);
- return bc_compat_status_code(status);
+ return sb_compat_status_code(status);
}
static void
-list_variables(const char *key, const char *value, bc_string_t *str)
+list_variables(const char *key, const char *value, sb_string_t *str)
{
- char *tmp = bc_shell_quote(value);
- bc_string_append_printf(str, " -D %s=%s", key, tmp);
+ char *tmp = sb_shell_quote(value);
+ sb_string_append_printf(str, " -D %s=%s", key, tmp);
free(tmp);
}
char*
bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings,
- bc_trie_t *global_variables, bc_trie_t *local_variables, const char *print,
+ sb_trie_t *global_variables, sb_trie_t *local_variables, const char *print,
bool listing, const char *listing_entry, const char *template,
const char *output, bool dev, bool sources_stdin)
{
- bc_string_t *rv = bc_string_new();
+ sb_string_t *rv = sb_string_new();
const char *locale = NULL;
if (settings != NULL) {
- locale = bc_trie_lookup(settings->settings, "locale");
+ locale = sb_trie_lookup(settings->settings, "locale");
}
if (locale != NULL) {
- char *tmp = bc_shell_quote(locale);
- bc_string_append_printf(rv, "LC_ALL=%s ", tmp);
+ char *tmp = sb_shell_quote(locale);
+ sb_string_append_printf(rv, "LC_ALL=%s ", tmp);
free(tmp);
}
- bc_string_append(rv, blogc_bin);
+ sb_string_append(rv, blogc_bin);
if (settings != NULL) {
if (settings->tags != NULL) {
- char *tags = bc_strv_join(settings->tags, " ");
- bc_string_append_printf(rv, " -D MAKE_TAGS='%s'", tags);
+ char *tags = sb_strv_join(settings->tags, " ");
+ sb_string_append_printf(rv, " -D MAKE_TAGS='%s'", tags);
free(tags);
}
- bc_trie_foreach(settings->global,
- (bc_trie_foreach_func_t) list_variables, rv);
+ sb_trie_foreach(settings->global,
+ (sb_trie_foreach_func_t) list_variables, rv);
}
- bc_trie_foreach(global_variables, (bc_trie_foreach_func_t) list_variables, rv);
- bc_trie_foreach(local_variables, (bc_trie_foreach_func_t) list_variables, rv);
+ sb_trie_foreach(global_variables, (sb_trie_foreach_func_t) list_variables, rv);
+ sb_trie_foreach(local_variables, (sb_trie_foreach_func_t) list_variables, rv);
if (dev) {
- bc_string_append(rv, " -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'");
+ sb_string_append(rv, " -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'");
}
if (print != NULL) {
- bc_string_append_printf(rv, " -p %s", print);
+ sb_string_append_printf(rv, " -p %s", print);
}
if (listing) {
- bc_string_append(rv, " -l");
+ sb_string_append(rv, " -l");
if (listing_entry != NULL) {
- char *tmp = bc_shell_quote(listing_entry);
- bc_string_append_printf(rv, " -e %s", tmp);
+ char *tmp = sb_shell_quote(listing_entry);
+ sb_string_append_printf(rv, " -e %s", tmp);
free(tmp);
}
}
if (template != NULL) {
- char *tmp = bc_shell_quote(template);
- bc_string_append_printf(rv, " -t %s", tmp);
+ char *tmp = sb_shell_quote(template);
+ sb_string_append_printf(rv, " -t %s", tmp);
free(tmp);
}
if (output != NULL) {
- char *tmp = bc_shell_quote(output);
- bc_string_append_printf(rv, " -o %s", tmp);
+ char *tmp = sb_shell_quote(output);
+ sb_string_append_printf(rv, " -o %s", tmp);
free(tmp);
}
if (sources_stdin) {
- bc_string_append(rv, " -i");
+ sb_string_append(rv, " -i");
}
- return bc_string_free(rv, false);
+ return sb_string_free(rv, false);
}
int
-bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_variables,
+bm_exec_blogc(bm_ctx_t *ctx, sb_trie_t *global_variables, sb_trie_t *local_variables,
bool listing, bm_filectx_t *listing_entry, bm_filectx_t *template,
- bm_filectx_t *output, bc_slist_t *sources, bool only_first_source)
+ bm_filectx_t *output, sb_slist_t *sources, bool only_first_source)
{
if (ctx == NULL)
return 1;
- bc_string_t *input = bc_string_new();
- for (bc_slist_t *l = sources; l != NULL; l = l->next) {
- bc_string_append_printf(input, "%s\n", ((bm_filectx_t*) l->data)->path);
+ sb_string_t *input = sb_string_new();
+ for (sb_slist_t *l = sources; l != NULL; l = l->next) {
+ sb_string_append_printf(input, "%s\n", ((bm_filectx_t*) l->data)->path);
if (only_first_source)
break;
}
@@ -315,17 +313,17 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia
char *out = NULL;
char *err = NULL;
- bc_error_t *error = NULL;
+ sb_error_t *error = NULL;
int rv = bm_exec_command(cmd, input->str, &out, &err, &error);
if (error != NULL) {
- bc_error_print(error, "blogc-make");
+ fprintf(stderr, "blogc-make: error: %s\n", sb_error_to_string(error));
free(cmd);
free(out);
free(err);
- bc_string_free(input, true);
- bc_error_free(error);
+ sb_string_free(input, true);
+ sb_error_free(error);
return 1;
}
@@ -339,21 +337,21 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia
"----------------------------->8-----------------------------\n"
"%s\n"
"----------------------------->8-----------------------------\n",
- bc_str_strip(input->str));
+ sb_str_strip(input->str));
}
if (out != NULL) {
fprintf(stderr, "\nSTDOUT:\n"
"----------------------------->8-----------------------------\n"
"%s\n"
"----------------------------->8-----------------------------\n",
- bc_str_strip(out));
+ sb_str_strip(out));
}
if (err != NULL) {
fprintf(stderr, "\nSTDERR:\n"
"----------------------------->8-----------------------------\n"
"%s\n"
"----------------------------->8-----------------------------\n",
- bc_str_strip(err));
+ sb_str_strip(err));
}
fprintf(stderr, "\n");
}
@@ -361,7 +359,7 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia
fprintf(stderr, "%s\n", err);
}
- bc_string_free(input, true);
+ sb_string_free(input, true);
free(cmd);
free(out);
free(err);
@@ -371,16 +369,16 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia
char*
-bm_exec_blogc_get_variable(bm_ctx_t *ctx, bc_trie_t *global_variables,
- bc_trie_t *local_variables, const char *variable, bool listing,
- bc_slist_t *sources, bool only_first_source)
+bm_exec_blogc_get_variable(bm_ctx_t *ctx, sb_trie_t *global_variables,
+ sb_trie_t *local_variables, const char *variable, bool listing,
+ sb_slist_t *sources, bool only_first_source)
{
if (ctx == NULL)
return NULL;
- bc_string_t *input = bc_string_new();
- for (bc_slist_t *l = sources; l != NULL; l = l->next) {
- bc_string_append_printf(input, "%s\n", ((bm_filectx_t*) l->data)->path);
+ sb_string_t *input = sb_string_new();
+ for (sb_slist_t *l = sources; l != NULL; l = l->next) {
+ sb_string_append_printf(input, "%s\n", ((bm_filectx_t*) l->data)->path);
if (only_first_source)
break;
}
@@ -394,14 +392,14 @@ bm_exec_blogc_get_variable(bm_ctx_t *ctx, bc_trie_t *global_variables,
char *out = NULL;
char *err = NULL;
- bc_error_t *error = NULL;
+ sb_error_t *error = NULL;
int rv = bm_exec_command(cmd, input->str, &out, &err, &error);
if (error != NULL) {
- bc_error_print(error, "blogc-make");
- bc_error_free(error);
- bc_string_free(input, true);
+ fprintf(stderr, "blogc-make: error: %s\n", sb_error_to_string(error));
+ sb_error_free(error);
+ sb_string_free(input, true);
free(cmd);
free(out);
free(err);
@@ -410,8 +408,8 @@ bm_exec_blogc_get_variable(bm_ctx_t *ctx, bc_trie_t *global_variables,
if (rv != 0) {
if (rv != EX_CONFIG)
- fprintf(stderr, "blogc-make: error: %s\n", bc_str_strip(err));
- bc_string_free(input, true);
+ fprintf(stderr, "blogc-make: error: %s\n", sb_str_strip(err));
+ sb_string_free(input, true);
free(cmd);
free(out);
free(err);
@@ -420,9 +418,9 @@ bm_exec_blogc_get_variable(bm_ctx_t *ctx, bc_trie_t *global_variables,
char *val = NULL;
if (out != NULL)
- val = bc_strndup(out, strlen(out) - 1);
+ val = sb_strndup(out, strlen(out) - 1);
- bc_string_free(input, true);
+ sb_string_free(input, true);
free(cmd);
free(out);
free(err);
@@ -438,30 +436,30 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,
if (ctx == NULL)
return 1;
- bc_string_t *cmd = bc_string_new();
+ sb_string_t *cmd = sb_string_new();
- bc_string_append(cmd, ctx->blogc_runserver);
+ sb_string_append(cmd, ctx->blogc_runserver);
if (host != NULL) {
- char *tmp = bc_shell_quote(host);
- bc_string_append_printf(cmd, " -t %s", tmp);
+ char *tmp = sb_shell_quote(host);
+ sb_string_append_printf(cmd, " -t %s", tmp);
free(tmp);
}
if (port != NULL) {
- char *tmp = bc_shell_quote(port);
- bc_string_append_printf(cmd, " -p %s", tmp);
+ char *tmp = sb_shell_quote(port);
+ sb_string_append_printf(cmd, " -p %s", tmp);
free(tmp);
}
if (threads != NULL) {
- char *tmp = bc_shell_quote(threads);
- bc_string_append_printf(cmd, " -m %s", tmp);
+ char *tmp = sb_shell_quote(threads);
+ sb_string_append_printf(cmd, " -m %s", tmp);
free(tmp);
}
- char *tmp = bc_shell_quote(ctx->output_dir);
- bc_string_append_printf(cmd, " %s", tmp);
+ char *tmp = sb_shell_quote(ctx->output_dir);
+ sb_string_append_printf(cmd, " %s", tmp);
free(tmp);
if (ctx->verbose)
@@ -472,8 +470,8 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,
// we don't need pipes to run blogc-runserver, because it is "interactive"
int status = system(cmd->str);
- int rv = bc_compat_status_code(status);
- bc_string_free(cmd, true);
+ int rv = sb_compat_status_code(status);
+ sb_string_free(cmd, true);
if (rv != 0 && rv != 130) {
if (rv == 127) {