diff --git a/eeshow/cro.c b/eeshow/cro.c index 6070287..84effae 100644 --- a/eeshow/cro.c +++ b/eeshow/cro.c @@ -23,6 +23,7 @@ #include #include "util.h" +#include "diag.h" #include "style.h" #include "text.h" #include "gfx.h" @@ -344,7 +345,7 @@ static cairo_status_t stream_to_stdout(void *closure, wrote = write(1, data, length); if (wrote == (ssize_t) length) return CAIRO_STATUS_SUCCESS; - perror("stdout"); + diag_perror("stdout"); return CAIRO_STATUS_WRITE_ERROR; } @@ -373,10 +374,8 @@ static void cr_pdf_new_sheet(void *ctx) cc->n_sheets++; cc->sheets = realloc(cc->sheets, sizeof(struct record) * cc->n_sheets); - if (!cc->sheets) { - perror("realloc"); - exit(1); - } + if (!cc->sheets) + diag_pfatal("realloc"); cc->sheets[cc->n_sheets - 1] = cc->record; record_wipe(&cc->record); } diff --git a/eeshow/diag.c b/eeshow/diag.c index d9bb5a5..46cc49f 100644 --- a/eeshow/diag.c +++ b/eeshow/diag.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include "diag.h" @@ -22,6 +24,23 @@ unsigned verbose = 0; +/* ----- Specialized diagnostic functions ---------------------------------- */ + + +void diag_pfatal(const char *s) +{ + fatal("%s: %s\n", s, strerror(errno)); +} + + +void diag_perror(const char *s) +{ + error("%s: %s\n", s, strerror(errno)); +} + + +/* ----- General diagnostic functions -------------------------------------- */ + void fatal(const char *fmt, ...) { va_list ap; diff --git a/eeshow/diag.h b/eeshow/diag.h index be152b1..bf6ee75 100644 --- a/eeshow/diag.h +++ b/eeshow/diag.h @@ -23,6 +23,18 @@ extern unsigned verbose; +/* ----- Specialized diagnostic functions ---------------------------------- */ + + +/* perror, based on "fatal" or "error" */ + +void diag_pfatal(const char *s); +void diag_perror(const char *s); + + +/* ----- General diagnostic functions -------------------------------------- */ + + /* * Terminate immediately. Further execution makes no sense. * E.g., out of memory. diff --git a/eeshow/fig.c b/eeshow/fig.c index d24f8e7..70313c7 100644 --- a/eeshow/fig.c +++ b/eeshow/fig.c @@ -20,6 +20,7 @@ #include #include "util.h" +#include "diag.h" #include "style.h" #include "text.h" #include "main.h" @@ -269,10 +270,8 @@ static void *fig_init(int argc, char *const *argv) } file = fopen(template, "r"); - if (!file) { - perror(template); - exit(1); - } + if (!file) + diag_pfatal(template); while (fgets(buf, sizeof(buf), file)) { while (apply_vars(buf, n_vars, vars)); printf("%s", buf); diff --git a/eeshow/file.c b/eeshow/file.c index d927725..a0a83db 100644 --- a/eeshow/file.c +++ b/eeshow/file.c @@ -182,11 +182,11 @@ bool file_open(struct file *file, const char *name, const struct file *related) return 1; if (verbose) - perror(name); /* @@@ may need changing later */ + diag_perror(name); if (!strchr(name, ':')) { if (!verbose) - perror(name); + diag_perror(name); goto fail; } diff --git a/eeshow/git-file.c b/eeshow/git-file.c index 3246fa9..4ddef3d 100644 --- a/eeshow/git-file.c +++ b/eeshow/git-file.c @@ -125,10 +125,8 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path) /* identify inode of repo root */ - if (stat(repo_dir, &repo_st) < 0) { - perror(repo_dir); - exit(1); - } + if (stat(repo_dir, &repo_st) < 0) + diag_pfatal(repo_dir); if (!S_ISDIR(repo_st.st_mode)) fatal("%s: not a directory\n", repo_dir); diff --git a/eeshow/git-hist.c b/eeshow/git-hist.c index 886b0a3..03d02d4 100644 --- a/eeshow/git-hist.c +++ b/eeshow/git-hist.c @@ -49,10 +49,8 @@ static void uplink(struct hist *down, struct hist *up) { down->newer = realloc(down->newer, sizeof(struct hist *) * (down->n_newer + 1)); - if (!down->newer) { - perror("realloc"); - exit(1); - } + if (!down->newer) + diag_pfatal("realloc"); down->newer[down->n_newer++] = up; } diff --git a/eeshow/sch-parse.c b/eeshow/sch-parse.c index 6d63309..ac15d8c 100644 --- a/eeshow/sch-parse.c +++ b/eeshow/sch-parse.c @@ -124,10 +124,8 @@ static bool parse_field(struct sch_ctx *ctx, const char *line) char *s; s = realloc((void *) txt->s, len + 3); - if (!s) { - perror("realloc"); - exit(1); - } + if (!s) + diag_pfatal("realloc"); if (comp->unit <= 26) sprintf(s + len, "%c", 'A' + comp->unit - 1); else