diff options
-rw-r--r-- | src/color.c | 57 | ||||
-rw-r--r-- | src/color.h | 2 | ||||
-rw-r--r-- | src/eval.c | 4 | ||||
-rw-r--r-- | src/parse.c | 39 |
4 files changed, 66 insertions, 36 deletions
diff --git a/src/color.c b/src/color.c index 0cc950b..588dbac 100644 --- a/src/color.c +++ b/src/color.c @@ -1240,6 +1240,33 @@ static int print_link_target(CFILE *cfile, const struct BFTW *ftwbuf) { _printf(2, 3) static int cbuff(CFILE *cfile, const char *format, ...); +/** Print an expression's name, for diagnostics. */ +static int print_expr_name(CFILE *cfile, const struct bfs_expr *expr) { + switch (expr->kind) { + case BFS_FLAG: + return cbuff(cfile, "${cyn}%pq${rs}", expr->argv[0]); + case BFS_OPERATOR: + return cbuff(cfile, "${red}%pq${rs}", expr->argv[0]); + default: + return cbuff(cfile, "${blu}%pq${rs}", expr->argv[0]); + } +} + +/** Print an expression's args, for diagnostics. */ +static int print_expr_args(CFILE *cfile, const struct bfs_expr *expr) { + if (print_expr_name(cfile, expr) != 0) { + return -1; + } + + for (size_t i = 1; i < expr->argc; ++i) { + if (cbuff(cfile, " ${bld}%pq${rs}", expr->argv[i]) < 0) { + return -1; + } + } + + return 0; +} + /** Dump a parsed expression tree, for debugging. */ static int print_expr(CFILE *cfile, const struct bfs_expr *expr, bool verbose, int depth) { if (depth >= 2) { @@ -1254,28 +1281,10 @@ static int print_expr(CFILE *cfile, const struct bfs_expr *expr, bool verbose, i return -1; } - int ret; - switch (expr->kind) { - case BFS_FLAG: - ret = cbuff(cfile, "${cyn}%pq${rs}", expr->argv[0]); - break; - case BFS_OPERATOR: - ret = cbuff(cfile, "${red}%pq${rs}", expr->argv[0]); - break; - default: - ret = cbuff(cfile, "${blu}%pq${rs}", expr->argv[0]); - break; - } - if (ret < 0) { + if (print_expr_args(cfile, expr) != 0) { return -1; } - for (size_t i = 1; i < expr->argc; ++i) { - if (cbuff(cfile, " ${bld}%pq${rs}", expr->argv[i]) < 0) { - return -1; - } - } - if (verbose) { double rate = 0.0, time = 0.0; if (expr->evaluations) { @@ -1413,6 +1422,16 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { return -1; } break; + case 'x': + if (print_expr_args(cfile, va_arg(args, const struct bfs_expr *)) != 0) { + return -1; + } + break; + case 'X': + if (print_expr_name(cfile, va_arg(args, const struct bfs_expr *)) != 0) { + return -1; + } + break; default: goto invalid; diff --git a/src/color.h b/src/color.h index 2394af2..aac8b33 100644 --- a/src/color.h +++ b/src/color.h @@ -95,6 +95,8 @@ int cfclose(CFILE *cfile); * %pL: A colored link target, from a const struct BFTW * argument * %pe: Dump a const struct bfs_expr *, for debugging. * %pE: Dump a const struct bfs_expr * in verbose form, for debugging. + * %px: Print a const struct bfs_expr * with syntax highlighting. + * %pX: Print the name of a const struct bfs_expr *, without arguments. * %%: A literal '%' * ${cc}: Change the color to 'cc' * $$: A literal '$' @@ -408,7 +408,7 @@ static int eval_exec_finish(const struct bfs_expr *expr, const struct bfs_ctx *c if (expr->eval_fn == eval_exec) { if (bfs_exec_finish(expr->exec) != 0) { if (errno != 0) { - bfs_error(ctx, "%s %s: %s.\n", expr->argv[0], expr->argv[1], errstr()); + bfs_error(ctx, "${blu}%pq${rs} ${bld}%pq${rs}: %s.\n", expr->argv[0], expr->argv[1], errstr()); } ret = -1; } @@ -429,7 +429,7 @@ static int eval_exec_finish(const struct bfs_expr *expr, const struct bfs_ctx *c bool eval_exec(const struct bfs_expr *expr, struct bfs_eval *state) { bool ret = bfs_exec(expr->exec, state->ftwbuf) == 0; if (errno != 0) { - eval_error(state, "%s %s: %s.\n", expr->argv[0], expr->argv[1], errstr()); + eval_error(state, "${blu}%pq${rs} ${bld}%pq${rs}: %s.\n", expr->argv[0], expr->argv[1], errstr()); } return ret; } diff --git a/src/parse.c b/src/parse.c index b968be5..9631b91 100644 --- a/src/parse.c +++ b/src/parse.c @@ -272,7 +272,8 @@ static bool parse_expr_warning(const struct bfs_parser *parser, const struct bfs static bool consume_stdin(struct bfs_parser *parser, const struct bfs_expr *expr) { if (parser->stdin_expr) { parse_conflict_error(parser, parser->stdin_expr, expr, - "Both expressions attempted to consume standard input.\n"); + "%pX and %pX can't both use standard input.\n", + parser->stdin_expr, expr); return false; } @@ -1186,8 +1187,10 @@ static struct bfs_expr *parse_delete(struct bfs_parser *parser, int arg1, int ar /** * Parse -d. */ -static struct bfs_expr *parse_depth(struct bfs_parser *parser, int arg1, int arg2) { - struct bfs_expr *expr = parse_nullary_flag(parser); +static struct bfs_expr *parse_depth(struct bfs_parser *parser, int flag, int arg2) { + struct bfs_expr *expr = flag + ? parse_nullary_flag(parser) + : parse_nullary_option(parser); if (!expr) { return NULL; } @@ -1662,7 +1665,7 @@ static struct bfs_expr *parse_limit(struct bfs_parser *parser, int arg1, int arg } if (expr->num <= 0) { - parse_expr_error(parser, expr, "The ${blu}%s${rs} must be at least ${bld}1${rs}.\n", expr->argv[0]); + parse_expr_error(parser, expr, "The %pX must be at least ${bld}1${rs}.\n", expr); return NULL; } @@ -1879,9 +1882,15 @@ static struct bfs_expr *parse_nohidden(struct bfs_parser *parser, int arg1, int * Parse -noleaf. */ static struct bfs_expr *parse_noleaf(struct bfs_parser *parser, int arg1, int arg2) { - parse_warning(parser, "${ex}%s${rs} does not apply the optimization that ${blu}%s${rs} inhibits.\n\n", - BFS_COMMAND, parser->argv[0]); - return parse_nullary_option(parser); + struct bfs_expr *expr = parse_nullary_option(parser); + if (!expr) { + return NULL; + } + + parse_expr_warning(parser, expr, + "${ex}%s${rs} does not apply the optimization that %px inhibits.\n\n", + BFS_COMMAND, expr); + return expr; } /** @@ -3082,10 +3091,10 @@ static const struct table_entry parse_table[] = { {"-context", BFS_TEST, parse_context, true}, {"-csince", BFS_TEST, parse_since, BFS_STAT_CTIME}, {"-ctime", BFS_TEST, parse_time, BFS_STAT_CTIME}, - {"-d", BFS_FLAG, parse_depth}, + {"-d", BFS_FLAG, parse_depth, true}, {"-daystart", BFS_OPTION, parse_daystart}, {"-delete", BFS_ACTION, parse_delete}, - {"-depth", BFS_OPTION, parse_depth_n}, + {"-depth", BFS_OPTION, parse_depth_n, false}, {"-empty", BFS_TEST, parse_empty}, {"-exclude", BFS_OPERATOR}, {"-exec", BFS_ACTION, parse_exec, 0}, @@ -3566,8 +3575,8 @@ static struct bfs_expr *parse_whole_expr(struct bfs_parser *parser) { const struct bfs_expr *limit = parser->limit_expr; if (limit) { parse_expr_error(parser, limit, - "With ${blu}%s${rs}, you must specify an action explicitly; for example, ${blu}-print${rs} ${blu}%s${rs} ${bld}%s${rs}.\n", - limit->argv[0], limit->argv[0], limit->argv[1]); + "With %pX, you must specify an action explicitly; for example, ${blu}-print${rs} %px.\n", + limit, limit); return NULL; } @@ -3585,14 +3594,14 @@ static struct bfs_expr *parse_whole_expr(struct bfs_parser *parser) { if (parser->mount_expr && parser->xdev_expr) { parse_conflict_warning(parser, parser->mount_expr, parser->xdev_expr, - "${blu}%s${rs} is redundant in the presence of ${blu}%s${rs}.\n\n", - parser->xdev_expr->argv[0], parser->mount_expr->argv[0]); + "%px is redundant in the presence of %px.\n\n", + parser->xdev_expr, parser->mount_expr); } if (ctx->warn && parser->depth_expr && parser->prune_expr) { parse_conflict_warning(parser, parser->depth_expr, parser->prune_expr, - "${blu}%s${rs} does not work in the presence of ${blu}%s${rs}.\n", - parser->prune_expr->argv[0], parser->depth_expr->argv[0]); + "%px does not work in the presence of %px.\n", + parser->prune_expr, parser->depth_expr); if (ctx->interactive) { bfs_warning(ctx, "Do you want to continue? "); |