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.c156
1 files changed, 79 insertions, 77 deletions
diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c
index aaae523..5fa86f4 100644
--- a/src/blogc-make/exec.c
+++ b/src/blogc-make/exec.c
@@ -18,13 +18,15 @@
#include <sys/wait.h>
#include <errno.h>
#include <libgen.h>
-#include "../common/error.h"
-#include "../common/file.h"
-#include "../common/utils.h"
+#include <squareball.h>
+
#include "ctx.h"
+#include "error.h"
#include "exec.h"
#include "settings.h"
+#define BM_FILE_CHUNK_SIZE 1024
+
char*
bm_exec_find_binary(const char *argv0, const char *bin, const char *env)
@@ -34,14 +36,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
@@ -53,13 +55,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;
}
@@ -67,27 +69,27 @@ 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 3;
int fd_in[2];
if (-1 == pipe(fd_in)) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
"Failed to create stdin pipe: %s", strerror(errno));
return 3;
}
int fd_out[2];
if (-1 == pipe(fd_out)) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
"Failed to create stdout pipe: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
@@ -96,7 +98,7 @@ 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,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
"Failed to create stderr pipe: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
@@ -107,7 +109,7 @@ 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,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
"Failed to fork: %s", strerror(errno));
close(fd_in[0]);
close(fd_in[1]);
@@ -147,7 +149,7 @@ 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,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
"Failed to write to stdin pipe: %s", strerror(errno));
close(fd_in[1]);
close(fd_out[0]);
@@ -158,44 +160,44 @@ bm_exec_command(const char *cmd, const char *input, char **output,
close(fd_in[1]);
- char buffer[BC_FILE_CHUNK_SIZE];
+ char buffer[BM_FILE_CHUNK_SIZE];
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, BM_FILE_CHUNK_SIZE))) {
if (s == -1) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_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 3;
}
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, BM_FILE_CHUNK_SIZE))) {
if (s == -1) {
- *err = bc_error_new_printf(BLOGC_MAKE_ERROR_EXEC,
+ *err = sb_error_new_printf(BLOGC_MAKE_ERROR_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 3;
}
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]);
@@ -207,86 +209,86 @@ bm_exec_command(const char *cmd, const char *input, char **output,
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, bool listing,
+ sb_trie_t *global_variables, sb_trie_t *local_variables, bool listing,
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 (listing) {
- bc_string_append(rv, " -l");
+ sb_string_append(rv, " -l");
}
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,
- bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources,
+bm_exec_blogc(bm_ctx_t *ctx, sb_trie_t *global_variables, sb_trie_t *local_variables,
+ bool listing, bm_filectx_t *template, bm_filectx_t *output, sb_slist_t *sources,
bool only_first_source)
{
if (ctx == NULL)
return 3;
- 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;
}
@@ -303,7 +305,7 @@ 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);
@@ -312,12 +314,12 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia
}
if (error != NULL) {
- bc_error_print(error, "blogc-make");
+ bm_error_print(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 3;
}
@@ -332,27 +334,27 @@ 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));
}
}
else {
fprintf(stderr,
"blogc-make: error: Failed to execute command (%d)", rv);
if (err != NULL) {
- fprintf(stderr, ":\n%s", bc_str_strip(err));
+ fprintf(stderr, ":\n%s", sb_str_strip(err));
}
fprintf(stderr, "\n");
}
}
- bc_string_free(input, true);
+ sb_string_free(input, true);
free(cmd);
free(out);
free(err);
@@ -368,30 +370,30 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,
if (ctx == NULL)
return 3;
- 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)
@@ -403,7 +405,7 @@ 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 = WEXITSTATUS(status);
- bc_string_free(cmd, true);
+ sb_string_free(cmd, true);
if (rv != 0 && rv != 130) {
if (rv == 127) {