summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-10-04 13:46:35 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-10-05 15:39:22 -0400
commit98a3dd364739136329fe2b8f9cdeecf776fb17c9 (patch)
treed571ed17227849679b397e0cfd6c95a51c41c524
parentb3312002a5d96cd486156a711d3c1861c150b7c6 (diff)
downloadbfs-98a3dd364739136329fe2b8f9cdeecf776fb17c9.tar.xz
diag: New bfs_perror() function
-rw-r--r--diag.c4
-rw-r--r--diag.h5
-rw-r--r--eval.c12
-rw-r--r--exec.c4
-rw-r--r--parse.c39
-rw-r--r--printf.c26
6 files changed, 53 insertions, 37 deletions
diff --git a/diag.c b/diag.c
index 308b05d..66609e1 100644
--- a/diag.c
+++ b/diag.c
@@ -24,6 +24,10 @@
#include <stdlib.h>
#include <string.h>
+void bfs_perror(const struct bfs_ctx *ctx, const char *str) {
+ bfs_error(ctx, "%s: %m.\n", str);
+}
+
void bfs_error(const struct bfs_ctx *ctx, const char *format, ...) {
va_list args;
va_start(args, format);
diff --git a/diag.h b/diag.h
index 8faef07..aa5e1c7 100644
--- a/diag.h
+++ b/diag.h
@@ -27,6 +27,11 @@
#include <stdbool.h>
/**
+ * Like perror(), but decorated like bfs_error().
+ */
+void bfs_perror(const struct bfs_ctx *ctx, const char *str);
+
+/**
* Shorthand for printing error messages.
*/
BFS_FORMATTER(2, 3)
diff --git a/eval.c b/eval.c
index 551f069..246a60c 100644
--- a/eval.c
+++ b/eval.c
@@ -799,7 +799,7 @@ bool eval_regex(const struct expr *expr, struct eval_state *state) {
eval_error(state, "%s.\n", str);
free(str);
} else {
- perror("xregerror()");
+ eval_error(state, "xregerror(): %m.\n");
}
*state->ret = EXIT_FAILURE;
@@ -915,11 +915,11 @@ bool eval_xtype(const struct expr *expr, struct eval_state *state) {
/**
* Call clock_gettime(), if available.
*/
-static int eval_gettime(struct timespec *ts) {
+static int eval_gettime(struct eval_state *state, struct timespec *ts) {
#ifdef BFS_CLOCK
int ret = clock_gettime(BFS_CLOCK, ts);
if (ret != 0) {
- perror("clock_gettime()");
+ bfs_warning(state->ctx, "%pP: clock_gettime(): %m.\n", state->ftwbuf);
}
return ret;
#else
@@ -949,7 +949,7 @@ static bool eval_expr(struct expr *expr, struct eval_state *state) {
struct timespec start, end;
bool time = state->ctx->debug & DEBUG_RATES;
if (time) {
- if (eval_gettime(&start) != 0) {
+ if (eval_gettime(state, &start) != 0) {
time = false;
}
}
@@ -959,7 +959,7 @@ static bool eval_expr(struct expr *expr, struct eval_state *state) {
bool ret = expr->eval(expr, state);
if (time) {
- if (eval_gettime(&end) == 0) {
+ if (eval_gettime(state, &end) == 0) {
add_elapsed(expr, &start, &end);
}
}
@@ -1381,7 +1381,7 @@ int bfs_eval(const struct bfs_ctx *ctx) {
if (bftw(&bftw_args) != 0) {
args.ret = EXIT_FAILURE;
- perror("bftw()");
+ bfs_perror(ctx, "bftw()");
}
if (eval_exec_finish(ctx->expr, ctx) != 0) {
diff --git a/exec.c b/exec.c
index 723f1dc..3c9c6a5 100644
--- a/exec.c
+++ b/exec.c
@@ -120,7 +120,7 @@ static size_t bfs_exec_arg_max(const struct bfs_exec *execbuf) {
struct bfs_exec *parse_bfs_exec(char **argv, enum bfs_exec_flags flags, const struct bfs_ctx *ctx) {
struct bfs_exec *execbuf = malloc(sizeof(*execbuf));
if (!execbuf) {
- perror("malloc()");
+ bfs_perror(ctx, "malloc()");
goto fail;
}
@@ -167,7 +167,7 @@ struct bfs_exec *parse_bfs_exec(char **argv, enum bfs_exec_flags flags, const st
execbuf->argv_cap = execbuf->tmpl_argc + 1;
execbuf->argv = malloc(execbuf->argv_cap*sizeof(*execbuf->argv));
if (!execbuf->argv) {
- perror("malloc()");
+ bfs_perror(ctx, "malloc()");
goto fail;
}
diff --git a/parse.c b/parse.c
index c0c345c..5ce78c3 100644
--- a/parse.c
+++ b/parse.c
@@ -295,6 +295,13 @@ static void parse_error(const struct parser_state *state, const char *format, ..
}
/**
+ * Print a low-level error message during parsing.
+ */
+static void parse_perror(const struct parser_state *state, const char *str) {
+ bfs_perror(state->ctx, str);
+}
+
+/**
* Print a warning message during parsing.
*/
BFS_FORMATTER(2, 3)
@@ -379,7 +386,7 @@ static int parse_root(struct parser_state *state, const char *path) {
struct bfs_ctx *ctx = state->ctx;
int ret = DARRAY_PUSH(&ctx->paths, &path);
if (ret != 0) {
- perror("DARRAY_PUSH()");
+ parse_perror(state, "DARRAY_PUSH()");
}
return ret;
}
@@ -995,7 +1002,7 @@ static struct expr *parse_const(struct parser_state *state, int value, int arg2)
static struct expr *parse_daystart(struct parser_state *state, int arg1, int arg2) {
struct tm tm;
if (xlocaltime(&state->now.tv_sec, &tm) != 0) {
- perror("xlocaltime()");
+ parse_perror(state, "xlocaltime()");
return NULL;
}
@@ -1008,7 +1015,7 @@ static struct expr *parse_daystart(struct parser_state *state, int arg1, int arg
time_t time;
if (xmktime(&tm, &time) != 0) {
- perror("xmktime()");
+ parse_perror(state, "xmktime()");
return NULL;
}
@@ -1552,7 +1559,7 @@ static int parse_reftime(const struct parser_state *state, struct expr *expr) {
struct tm tm;
if (xlocaltime(&state->now.tv_sec, &tm) != 0) {
- perror("xlocaltime()");
+ parse_perror(state, "xlocaltime()");
return -1;
}
@@ -1572,7 +1579,7 @@ static int parse_reftime(const struct parser_state *state, struct expr *expr) {
year, month, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tz_hour, tz_min);
if (xgmtime(&state->now.tv_sec, &tm) != 0) {
- perror("xgmtime()");
+ parse_perror(state, "xgmtime()");
return -1;
}
@@ -2058,7 +2065,7 @@ static struct expr *parse_regex(struct parser_state *state, int flags, int arg2)
expr->regex = malloc(sizeof(regex_t));
if (!expr->regex) {
- perror("malloc()");
+ parse_perror(state, "malloc()");
goto fail;
}
@@ -2069,7 +2076,7 @@ static struct expr *parse_regex(struct parser_state *state, int flags, int arg2)
parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %s.\n", expr->argv[0], expr->argv[1], str);
free(str);
} else {
- perror("xregerror()");
+ parse_perror(state, "xregerror()");
}
goto fail_regex;
}
@@ -3437,11 +3444,11 @@ static void dump_costs(const struct bfs_ctx *ctx) {
/**
* Get the current time.
*/
-static int parse_gettime(struct timespec *ts) {
+static int parse_gettime(const struct bfs_ctx *ctx, struct timespec *ts) {
#if _POSIX_TIMERS > 0
int ret = clock_gettime(CLOCK_REALTIME, ts);
if (ret != 0) {
- perror("clock_gettime()");
+ bfs_perror(ctx, "clock_gettime()");
}
return ret;
#else
@@ -3451,7 +3458,7 @@ static int parse_gettime(struct timespec *ts) {
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000L;
} else {
- perror("gettimeofday()");
+ bfs_perror(ctx, "gettimeofday()");
}
return ret;
#endif
@@ -3490,15 +3497,15 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) {
ctx->colors_error = errno;
}
- ctx->cout = cfdup(stdout, use_color ? ctx->colors : NULL);
- if (!ctx->cout) {
+ ctx->cerr = cfdup(stderr, use_color ? ctx->colors : NULL);
+ if (!ctx->cerr) {
perror("cfdup()");
goto fail;
}
- ctx->cerr = cfdup(stderr, use_color ? ctx->colors : NULL);
- if (!ctx->cerr) {
- perror("cfdup()");
+ ctx->cout = cfdup(stdout, use_color ? ctx->colors : NULL);
+ if (!ctx->cout) {
+ bfs_perror(ctx, "cfdup()");
goto fail;
}
@@ -3534,7 +3541,7 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) {
ctx->strategy = BFTW_DFS;
}
- if (parse_gettime(&state.now) != 0) {
+ if (parse_gettime(ctx, &state.now) != 0) {
goto fail;
}
diff --git a/printf.c b/printf.c
index c30868d..0d13fe6 100644
--- a/printf.c
+++ b/printf.c
@@ -504,17 +504,17 @@ static void free_directive(struct bfs_printf *directive) {
/**
* Create a new printf directive.
*/
-static struct bfs_printf *new_directive(bfs_printf_fn *fn) {
+static struct bfs_printf *new_directive(const struct bfs_ctx *ctx, bfs_printf_fn *fn) {
struct bfs_printf *directive = malloc(sizeof(*directive));
if (!directive) {
- perror("malloc()");
+ bfs_perror(ctx, "malloc()");
goto error;
}
directive->fn = fn;
directive->str = dstralloc(2);
if (!directive->str) {
- perror("dstralloc()");
+ bfs_perror(ctx, "dstralloc()");
goto error;
}
directive->stat_field = 0;
@@ -554,7 +554,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
struct bfs_printf *head = NULL;
struct bfs_printf **tail = &head;
- struct bfs_printf *literal = new_directive(bfs_printf_literal);
+ struct bfs_printf *literal = new_directive(ctx, bfs_printf_literal);
if (!literal) {
goto error;
}
@@ -587,7 +587,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
case 'c':
tail = append_literal(tail, &literal);
- struct bfs_printf *directive = new_directive(bfs_printf_flush);
+ struct bfs_printf *directive = new_directive(ctx, bfs_printf_flush);
if (!directive) {
goto error;
}
@@ -608,12 +608,12 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
goto one_char;
}
- struct bfs_printf *directive = new_directive(NULL);
+ struct bfs_printf *directive = new_directive(ctx, NULL);
if (!directive) {
goto directive_error;
}
if (dstrapp(&directive->str, c) != 0) {
- perror("dstrapp()");
+ bfs_perror(ctx, "dstrapp()");
goto directive_error;
}
@@ -637,7 +637,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
goto directive_error;
}
if (dstrapp(&directive->str, c) != 0) {
- perror("dstrapp()");
+ bfs_perror(ctx, "dstrapp()");
goto directive_error;
}
continue;
@@ -649,7 +649,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
// Parse the field width
while (c >= '0' && c <= '9') {
if (dstrapp(&directive->str, c) != 0) {
- perror("dstrapp()");
+ bfs_perror(ctx, "dstrapp()");
goto directive_error;
}
c = *++i;
@@ -659,7 +659,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
if (c == '.') {
do {
if (dstrapp(&directive->str, c) != 0) {
- perror("dstrapp()");
+ bfs_perror(ctx, "dstrapp()");
goto directive_error;
}
c = *++i;
@@ -814,7 +814,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
}
if (dstrcat(&directive->str, specifier) != 0) {
- perror("dstrcat()");
+ bfs_perror(ctx, "dstrcat()");
goto directive_error;
}
@@ -822,7 +822,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
tail = append_directive(tail, directive);
if (!literal) {
- literal = new_directive(bfs_printf_literal);
+ literal = new_directive(ctx, bfs_printf_literal);
if (!literal) {
goto error;
}
@@ -837,7 +837,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct bfs_ctx *ctx) {
one_char:
if (dstrapp(&literal->str, c) != 0) {
- perror("dstrapp()");
+ bfs_perror(ctx, "dstrapp()");
goto error;
}
}