aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2017-03-03 01:10:35 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2017-03-03 01:10:35 +0100
commit0c290f72a9101c196031c3bcf5ee1340b4328b80 (patch)
tree4ac5e096ffc79afbd830b6c478646f355e1156b8 /src
parentbe39aad967f2243dd512ae9ab9510179d13abf16 (diff)
downloadblogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.tar.gz
blogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.tar.bz2
blogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.zip
make: reworked binary lookups, added them to ctx
Diffstat (limited to 'src')
-rw-r--r--src/blogc-make/ctx.c11
-rw-r--r--src/blogc-make/ctx.h5
-rw-r--r--src/blogc-make/exec.c20
-rw-r--r--src/blogc-make/exec.h7
-rw-r--r--src/blogc-make/main.c12
5 files changed, 26 insertions, 29 deletions
diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c
index 7d3489b..62b40b8 100644
--- a/src/blogc-make/ctx.c
+++ b/src/blogc-make/ctx.c
@@ -16,6 +16,7 @@
#include "../common/utils.h"
#include "atom.h"
#include "settings.h"
+#include "exec.h"
#include "ctx.h"
@@ -105,7 +106,8 @@ bm_filectx_free(bm_filectx_t *fctx)
bm_ctx_t*
-bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err)
+bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,
+ bc_error_t **err)
{
if (settings_file == NULL || err == NULL || *err != NULL)
return NULL;
@@ -138,6 +140,9 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err)
bm_ctx_t *rv = NULL;
if (base == NULL) {
rv = bc_malloc(sizeof(bm_ctx_t));
+ rv->blogc = bm_exec_find_binary(argv0, "blogc", "BLOGC");
+ rv->blogc_runserver = bm_exec_find_binary(argv0, "blogc-runserver",
+ "BLOGC_RUNSERVER");
rv->verbose = false;
}
else {
@@ -224,7 +229,7 @@ bm_ctx_reload(bm_ctx_t *ctx)
// needs to dup path, because it may be freed when reloading.
char *tmp = bc_strdup(ctx->settings_fctx->path);
bc_error_t *err = NULL;
- ctx = bm_ctx_new(ctx, tmp, &err);
+ ctx = bm_ctx_new(ctx, tmp, NULL, &err);
free(tmp);
if (err != NULL) { // failed to reload, keep old ctx
bc_error_print(err, "blogc-make");
@@ -283,5 +288,7 @@ void
bm_ctx_free(bm_ctx_t *ctx)
{
bm_ctx_free_internal(ctx);
+ free(ctx->blogc);
+ free(ctx->blogc_runserver);
free(ctx);
}
diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h
index eb25145..77e8088 100644
--- a/src/blogc-make/ctx.h
+++ b/src/blogc-make/ctx.h
@@ -29,6 +29,8 @@ typedef struct {
} bm_filectx_t;
typedef struct {
+ char *blogc;
+ char *blogc_runserver;
bool verbose;
bm_settings_t *settings;
@@ -49,7 +51,8 @@ bm_filectx_t* bm_filectx_new(bm_ctx_t *ctx, const char *filename);
bool bm_filectx_changed(bm_filectx_t *ctx, struct timespec *ts);
void bm_filectx_reload(bm_filectx_t *ctx);
void bm_filectx_free(bm_filectx_t *fctx);
-bm_ctx_t* bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err);
+bm_ctx_t* bm_ctx_new(bm_ctx_t *base, const char *settings_file,
+ const char *argv0, bc_error_t **err);
void bm_ctx_reload(bm_ctx_t *ctx);
void bm_ctx_free_internal(bm_ctx_t *ctx);
void bm_ctx_free(bm_ctx_t *ctx);
diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c
index 157254b..de5dd5c 100644
--- a/src/blogc-make/exec.c
+++ b/src/blogc-make/exec.c
@@ -21,11 +21,9 @@
#include "exec.h"
#include "settings.h"
-extern const char *argv0;
-
char*
-bm_exec_find_binary(const char *bin, const char *env)
+bm_exec_find_binary(const char *argv0, const char *bin, const char *env)
{
// first try: env var
const char *env_bin = getenv(env);
@@ -205,8 +203,9 @@ list_variables(const char *key, const char *value, bc_string_t *str)
char*
-bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables,
- bool listing, const char *template, const char *output, bool sources_stdin)
+bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings,
+ bc_trie_t *variables, bool listing, const char *template,
+ const char *output, bool sources_stdin)
{
bc_string_t *rv = bc_string_new();
@@ -220,9 +219,7 @@ bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables,
free(tmp);
}
- char *blogc_bin = bm_exec_find_binary("blogc", "BLOGC");
bc_string_append(rv, blogc_bin);
- free(blogc_bin);
if (settings != NULL) {
bc_trie_foreach(settings->env,
@@ -270,8 +267,8 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing,
break;
}
- char *cmd = bm_exec_build_blogc_cmd(ctx->settings, variables, listing,
- template->path, output->path, input->len > 0);
+ char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, variables,
+ listing, template->path, output->path, input->len > 0);
if (ctx->verbose)
printf("%s\n", cmd);
@@ -348,10 +345,7 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,
bc_string_t *cmd = bc_string_new();
- char *blogc_runserver = bm_exec_find_binary("blogc-runserver",
- "BLOGC_RUNSERVER");
- bc_string_append(cmd, blogc_runserver);
- free(blogc_runserver);
+ bc_string_append(cmd, ctx->blogc_runserver);
if (host != NULL) {
char *tmp = bc_shell_quote(host);
diff --git a/src/blogc-make/exec.h b/src/blogc-make/exec.h
index ec52eec..08cc608 100644
--- a/src/blogc-make/exec.h
+++ b/src/blogc-make/exec.h
@@ -15,11 +15,12 @@
#include "ctx.h"
#include "settings.h"
-char* bm_exec_find_binary(const char *bin, const char *env);
+char* bm_exec_find_binary(const char *argv0, const char *bin, const char *env);
int bm_exec_command(const char *cmd, const char *input, char **output,
char **error, bc_error_t **err);
-char* bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables,
- bool listing, const char *template, const char *output, bool sources_stdin);
+char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings,
+ bc_trie_t *variables, bool listing, const char *template,
+ const char *output, bool sources_stdin);
int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing,
bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources,
bool only_first_source);
diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c
index c27c302..96f55b9 100644
--- a/src/blogc-make/main.c
+++ b/src/blogc-make/main.c
@@ -19,10 +19,6 @@
#include "ctx.h"
#include "rules.h"
-// is this beautiful? no. but there's no point in passing something that is
-// essentially global to every function in exec.c
-const char *argv0 = NULL;
-
static void
print_help(void)
@@ -61,11 +57,6 @@ main(int argc, char **argv)
{
setlocale(LC_ALL, "");
- // i really hope that no operating system omits argv[0], but ...
- if (argc > 0) {
- argv0 = argv[0];
- }
-
int rv = 0;
bc_error_t *err = NULL;
@@ -114,7 +105,8 @@ main(int argc, char **argv)
rules = bc_slist_append(rules, bc_strdup("all"));
}
- ctx = bm_ctx_new(NULL, blogcfile ? blogcfile : "blogcfile", &err);
+ ctx = bm_ctx_new(NULL, blogcfile ? blogcfile : "blogcfile",
+ argc > 0 ? argv[0] : NULL, &err);
if (err != NULL) {
bc_error_print(err, "blogc-make");
rv = 3;