diff options
-rw-r--r-- | color.c | 39 | ||||
-rw-r--r-- | color.h | 28 | ||||
-rw-r--r-- | ctx.c | 2 | ||||
-rw-r--r-- | parse.c | 45 | ||||
-rwxr-xr-x | tests.sh | 29 | ||||
-rw-r--r-- | tests/test_fprint_append.out | 38 | ||||
-rw-r--r-- | tests/test_fprint_truncate.out | 1 | ||||
-rw-r--r-- | util.c | 1 |
8 files changed, 115 insertions, 68 deletions
@@ -501,7 +501,7 @@ void free_colors(struct colors *colors) { } } -static CFILE *cfalloc(void) { +CFILE *cfwrap(FILE *file, const struct colors *colors, bool close) { CFILE *cfile = malloc(sizeof(*cfile)); if (!cfile) { return NULL; @@ -513,43 +513,8 @@ static CFILE *cfalloc(void) { return NULL; } - cfile->file = NULL; - cfile->colors = NULL; - cfile->close = false; - - return cfile; -} - -CFILE *cfopen(const char *path, const struct colors *colors) { - CFILE *cfile = cfalloc(); - if (!cfile) { - return NULL; - } - - cfile->file = xfopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC); - if (!cfile->file) { - cfclose(cfile); - return NULL; - } - cfile->close = true; - - if (isatty(fileno(cfile->file))) { - cfile->colors = colors; - } else { - cfile->colors = NULL; - } - - return cfile; -} - -CFILE *cfdup(FILE *file, const struct colors *colors) { - CFILE *cfile = cfalloc(); - if (!cfile) { - return NULL; - } - - cfile->close = false; cfile->file = file; + cfile->close = close; if (isatty(fileno(file))) { cfile->colors = colors; @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2015-2020 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2015-2021 Tavian Barnes <tavianator@tavianator.com> * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -63,33 +63,26 @@ typedef struct CFILE { } CFILE; /** - * Open a file for colored output. - * - * @param path - * The path to the file to open. - * @param colors - * The color table to use if file is a TTY. - * @return A colored file stream. - */ -CFILE *cfopen(const char *path, const struct colors *colors); - -/** - * Make a colored copy of an open file. + * Wrap an existing file into a colored stream. * * @param file * The underlying file. * @param colors * The color table to use if file is a TTY. - * @return A colored wrapper around file. + * @param close + * Whether to close the underlying stream when this stream is closed. + * @return + * A colored wrapper around file. */ -CFILE *cfdup(FILE *file, const struct colors *colors); +CFILE *cfwrap(FILE *file, const struct colors *colors, bool close); /** * Close a colored file. * * @param cfile * The colored file to close. - * @return 0 on success, -1 on failure. + * @return + * 0 on success, -1 on failure. */ int cfclose(CFILE *cfile); @@ -114,7 +107,8 @@ int cfclose(CFILE *cfile); * %%: A literal '%' * ${cc}: Change the color to 'cc' * $$: A literal '$' - * @return 0 on success, -1 on failure. + * @return + * 0 on success, -1 on failure. */ BFS_FORMATTER(2, 3) int cfprintf(CFILE *cfile, const char *format, ...); @@ -161,7 +161,7 @@ struct bfs_ctx_file { const char *path; }; -struct CFILE *bfs_ctx_dedup(struct bfs_ctx *ctx, CFILE *cfile, const char *path) { +CFILE *bfs_ctx_dedup(struct bfs_ctx *ctx, CFILE *cfile, const char *path) { struct bfs_stat sb; if (bfs_stat(fileno(cfile->file), NULL, 0, &sb) != 0) { return NULL; @@ -334,25 +334,45 @@ static void init_print_expr(struct parser_state *state, struct expr *expr) { static int expr_open(struct parser_state *state, struct expr *expr, const char *path) { struct bfs_ctx *ctx = state->ctx; - CFILE *cfile = cfopen(path, state->use_color ? ctx->colors : NULL); + FILE *file = NULL; + CFILE *cfile = NULL; + + file = xfopen(path, O_WRONLY | O_CREAT | O_CLOEXEC); + if (!file) { + goto fail; + } + + cfile = cfwrap(file, state->use_color ? ctx->colors : NULL, true); if (!cfile) { - parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %m.\n", expr->argv[0], path); - return -1; + goto fail; } CFILE *dedup = bfs_ctx_dedup(ctx, cfile, path); if (!dedup) { - parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %m.\n", expr->argv[0], path); + goto fail; + } + + if (dedup == cfile) { + // O_TRUNC was omitted above to avoid repeatedly truncating the same file, so do it + // manually here + if (ftruncate(fileno(file), 0) != 0) { + goto fail; + } + } else { cfclose(cfile); - return -1; } expr->cfile = dedup; + return 0; - if (dedup != cfile) { +fail: + parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %m.\n", expr->argv[0], path); + if (cfile) { cfclose(cfile); + } else if (file) { + fclose(file); } - return 0; + return -1; } /** @@ -2675,12 +2695,11 @@ static CFILE *launch_pager(pid_t *pid, CFILE *cout) { } pipefd[1] = -1; - CFILE *ret = cfdup(file, NULL); + CFILE *ret = cfwrap(file, NULL, true); if (!ret) { goto fail_file; } file = NULL; - ret->close = true; struct bfs_spawn ctx; if (bfs_spawn_init(&ctx) != 0) { @@ -3689,15 +3708,15 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { ctx->colors_error = errno; } - ctx->cerr = cfdup(stderr, use_color ? ctx->colors : NULL); + ctx->cerr = cfwrap(stderr, use_color ? ctx->colors : NULL, false); if (!ctx->cerr) { - perror("cfdup()"); + perror("cfwrap()"); goto fail; } - ctx->cout = cfdup(stdout, use_color ? ctx->colors : NULL); + ctx->cout = cfwrap(stdout, use_color ? ctx->colors : NULL, false); if (!ctx->cout) { - bfs_perror(ctx, "cfdup()"); + bfs_perror(ctx, "cfwrap()"); goto fail; } @@ -518,6 +518,7 @@ gnu_tests=( test_fprint test_fprint_duplicate test_fprint_error + test_fprint_truncate test_fprint0 @@ -723,6 +724,7 @@ bfs_tests=( test_execdir_plus + test_fprint_append test_fprint_duplicate_stdout test_fprint_error_stdout test_fprint_error_stderr @@ -1725,6 +1727,33 @@ function test_fprint_duplicate_stdout() { fi } +function test_fprint_truncate() { + printf "basic\nbasic\n" >scratch/test_fprint_truncate.out + + invoke_bfs basic -maxdepth 0 -fprint scratch/test_fprint_truncate.out + sort -o scratch/test_fprint_truncate.out scratch/test_fprint_truncate.out + + if [ "$UPDATE" ]; then + cp {scratch,"$TESTS"}/test_fprint_truncate.out + else + diff -u {"$TESTS",scratch}/test_fprint_truncate.out + fi +} + +function test_fprint_append() { + rm -f scratch/test_fprint_append.out + + invoke_bfs basic -fprint scratch/test_fprint_append.out >>scratch/test_fprint_append.out + invoke_bfs basic -fprint scratch/test_fprint_append.out >>scratch/test_fprint_append.out + sort -o scratch/test_fprint_append.out scratch/test_fprint_append.out + + if [ "$UPDATE" ]; then + cp {scratch,"$TESTS"}/test_fprint_append.out + else + diff -u {"$TESTS",scratch}/test_fprint_append.out + fi +} + function test_double_dash() { cd basic bfs_diff -- . -type f diff --git a/tests/test_fprint_append.out b/tests/test_fprint_append.out new file mode 100644 index 0000000..6c21751 --- /dev/null +++ b/tests/test_fprint_append.out @@ -0,0 +1,38 @@ +basic +basic +basic/a +basic/a +basic/b +basic/b +basic/c +basic/c +basic/c/d +basic/c/d +basic/e +basic/e +basic/e/f +basic/e/f +basic/g +basic/g +basic/g/h +basic/g/h +basic/i +basic/i +basic/j +basic/j +basic/j/foo +basic/j/foo +basic/k +basic/k +basic/k/foo +basic/k/foo +basic/k/foo/bar +basic/k/foo/bar +basic/l +basic/l +basic/l/foo +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar +basic/l/foo/bar/baz +basic/l/foo/bar/baz diff --git a/tests/test_fprint_truncate.out b/tests/test_fprint_truncate.out new file mode 100644 index 0000000..15a13db --- /dev/null +++ b/tests/test_fprint_truncate.out @@ -0,0 +1 @@ +basic @@ -443,6 +443,7 @@ FILE *xfopen(const char *path, int flags) { break; default: assert(!"Invalid access mode"); + errno = EINVAL; return NULL; } |