summaryrefslogtreecommitdiffstats
path: root/parse.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-03-16 23:43:08 -0400
committerTavian Barnes <tavianator@tavianator.com>2017-03-16 23:43:08 -0400
commit139405503359f97867cbcb3cd580b4efde60a02d (patch)
treeef6ced0b6674e45e3ba2cade9daa65cfe7ef7b84 /parse.c
parent4da8ba603b588bef38f772c18d883fbf5ddc9e65 (diff)
downloadbfs-139405503359f97867cbcb3cd580b4efde60a02d.tar.xz
Respect -nocolor for -fprint /dev/stdout
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/parse.c b/parse.c
index 5cc286c..31ee7fe 100644
--- a/parse.c
+++ b/parse.c
@@ -204,6 +204,15 @@ void free_cmdline(struct cmdline *cmdline) {
}
/**
+ * Color use flags.
+ */
+enum use_color {
+ COLOR_NEVER,
+ COLOR_AUTO,
+ COLOR_ALWAYS,
+};
+
+/**
* Ephemeral state for parsing the command line.
*/
struct parser_state {
@@ -219,6 +228,8 @@ struct parser_state {
/** The current regex flags to use. */
int regex_flags;
+ /** Whether -color or -nocolor has been passed. */
+ enum use_color use_color;
/** Whether a -print action is implied. */
bool implicit_print;
/** Whether warnings are enabled (see -warn, -nowarn). */
@@ -781,9 +792,11 @@ static struct expr *parse_color(struct parser_state *state, int color, int arg2)
struct cmdline *cmdline = state->cmdline;
struct colors *colors = cmdline->colors;
if (color) {
+ state->use_color = COLOR_ALWAYS;
cmdline->cout->colors = colors;
cmdline->cerr->colors = colors;
} else {
+ state->use_color = COLOR_NEVER;
cmdline->cout->colors = NULL;
cmdline->cerr->colors = NULL;
}
@@ -938,7 +951,7 @@ static struct expr *parse_f(struct parser_state *state, int arg1, int arg2) {
* Open a file for an expression.
*/
static int expr_open(struct parser_state *state, struct expr *expr, const char *path) {
- expr->cfile = cfopen(path, state->cmdline->colors);
+ expr->cfile = cfopen(path, state->use_color ? state->cmdline->colors : NULL);
if (!expr->cfile) {
cfprintf(state->cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", path, strerror(errno));
return -1;
@@ -2641,6 +2654,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) {
.command = argv[0],
.roots_tail = &cmdline->roots,
.regex_flags = 0,
+ .use_color = COLOR_AUTO,
.implicit_print = true,
.warn = isatty(STDIN_FILENO),
.non_option_seen = false,
@@ -2667,7 +2681,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) {
}
if (cmdline->debug & DEBUG_TREE) {
- dump_cmdline(cmdline, 0);
+ dump_cmdline(cmdline, false);
}
done: