From 62ca12c2da460f297707c36ea8906969819d00ad Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 11 Aug 2016 17:19:32 -0300 Subject: [PATCH] eeshow/: use dedicated functions for diagnostics, instead of fprintf and exit --- eeshow/diag.c | 32 ++++++++++++++++++++++ eeshow/diag.h | 22 +++++++++++++++ eeshow/diff.c | 8 ++---- eeshow/file.c | 2 +- eeshow/fmt-pango.c | 4 +-- eeshow/git-file.c | 67 ++++++++++++++++----------------------------- eeshow/git-hist.c | 27 ++++++------------ eeshow/gui.c | 7 ++--- eeshow/lib-parse.c | 10 +++---- eeshow/lib-render.c | 4 +-- eeshow/main.c | 11 ++------ eeshow/misc.c | 5 ++-- eeshow/sch-parse.c | 14 ++++------ 13 files changed, 111 insertions(+), 102 deletions(-) diff --git a/eeshow/diag.c b/eeshow/diag.c index 1650f81..d9bb5a5 100644 --- a/eeshow/diag.c +++ b/eeshow/diag.c @@ -22,6 +22,38 @@ unsigned verbose = 0; +void fatal(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); /* @@@ for now ... */ +} + + +void error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + + +void warning(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fprintf(stderr, "warning: "); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + + void progress(unsigned level, const char *fmt, ...) { va_list ap; diff --git a/eeshow/diag.h b/eeshow/diag.h index 8abac6f..be152b1 100644 --- a/eeshow/diag.h +++ b/eeshow/diag.h @@ -23,6 +23,28 @@ extern unsigned verbose; +/* + * Terminate immediately. Further execution makes no sense. + * E.g., out of memory. + */ + +void __attribute__((noreturn)) fatal(const char *fmt, ...); + +/* + * Operation has failed, but the program as a whole may still be able to + * continue. E.g., a schematics component was not found. + */ + +void error(const char *fmt, ...); + +/* + * A minor operation has failed or some other issue was detected. This may + * be (or lead to) a more serious problem, but does not immediately affect + * operation. + */ + +void warning(const char *fmt, ...); + /* * Progress message, used mainly for debugging. "level" is the minimum * verbosity level required. diff --git a/eeshow/diff.c b/eeshow/diff.c index 4dca702..c79c316 100644 --- a/eeshow/diff.c +++ b/eeshow/diff.c @@ -20,6 +20,7 @@ #include #include "util.h" +#include "diag.h" #include "main.h" #include "cro.h" #include "file.h" @@ -280,11 +281,8 @@ static void diff_end(void *ctx) int w, h, stride; old_img = cro_img_end(diff->cr_ctx, &w, &h, &stride); - if (diff->w != w || diff->h != h) { - fprintf(stderr, "%d x %d vs. %d x %d image\n", - w, h, diff->w, diff->h); - exit(1); - } + if (diff->w != w || diff->h != h) + fatal("%d x %d vs. %d x %d image\n", w, h, diff->w, diff->h); differences(diff, old_img, diff->new_img); show_areas(diff, old_img); diff --git a/eeshow/file.c b/eeshow/file.c index fe687f5..d927725 100644 --- a/eeshow/file.c +++ b/eeshow/file.c @@ -194,7 +194,7 @@ bool file_open(struct file *file, const char *name, const struct file *related) if (file->vcs) return 1; - fprintf(stderr, "could not open %s\n", name); + error("could not open %s\n", name); fail: free((char *) file->name); return 0; diff --git a/eeshow/fmt-pango.c b/eeshow/fmt-pango.c index 9ae77f4..bdf1088 100644 --- a/eeshow/fmt-pango.c +++ b/eeshow/fmt-pango.c @@ -19,6 +19,7 @@ #include #include "util.h" +#include "diag.h" #include "fmt-pango.h" @@ -107,8 +108,7 @@ unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap) res++; break; default: - fprintf(stderr, "unrecognized format '%%%c'\n", *q); - exit(1); + fatal("unrecognized format '%%%c'\n", *q); } p = q; } diff --git a/eeshow/git-file.c b/eeshow/git-file.c index b450155..3246fa9 100644 --- a/eeshow/git-file.c +++ b/eeshow/git-file.c @@ -99,22 +99,17 @@ static git_tree *pick_revision(git_repository *repo, const char *revision) if (git_revparse_single(&obj, repo, revision)) { const git_error *e = giterr_last(); - fprintf(stderr, "%s: %s\n", - git_repository_path(repo), e->message); - exit(1); + fatal("%s: %s\n", git_repository_path(repo), e->message); } - if (git_object_type(obj) != GIT_OBJ_COMMIT) { - fprintf(stderr, "%s: not a commit\n", revision); - exit(1); - } + if (git_object_type(obj) != GIT_OBJ_COMMIT) + fatal("%s: not a commit\n", revision); commit = (git_commit *) obj; if (git_commit_tree(&tree, commit)) { const git_error *e = giterr_last(); - fprintf(stderr, "%s: %s\n", revision, e->message); - exit(1); + fatal("%s: %s\n", revision, e->message); } return tree; @@ -134,10 +129,8 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path) perror(repo_dir); exit(1); } - if (!S_ISDIR(repo_st.st_mode)) { - fprintf(stderr, "%s: not a directory\n", repo_dir); - exit(1); - } + if (!S_ISDIR(repo_st.st_mode)) + fatal("%s: not a directory\n", repo_dir); /* convert relative paths to absolute */ @@ -167,10 +160,8 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path) progress(3, "probing \"%s\" tail \"%s\"\n", tmp, tail); if (stat(tmp, &path_st) == 0) break; - if (!tmp[1]) { - fprintf(stderr, "%s: cannot resolve\n", path); - exit(1); - } + if (!tmp[1]) + fatal("%s: cannot resolve\n", path); slash = strrchr(tmp, '/'); if (tail != end) tail[-1] = '/'; @@ -198,15 +189,13 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path) if (!*from) break; } - if (to == tail) { - /* - * We have something like this: - * /home/repo/dead/../../foo - */ - fprintf(stderr, "%s: can't climb out of dead path\n", - path); - exit(1); - } + + /* + * We have something like this: + * /home/repo/dead/../../foo + */ + if (to == tail) + fatal("%s: can't climb out of dead path\n", path); /* * We have something like @@ -255,12 +244,9 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path) slash = strrchr(tmp, '/'); /* "this cannot happen" */ - if (tail == tmp || !slash) { - fprintf(stderr, - "divergent paths:\nrepo \"%s\"\nobject \"%s\"\n", + if (tail == tmp || !slash) + fatal("divergent paths:\nrepo \"%s\"\nobject \"%s\"\n", repo_dir, tail); - exit(1); - } if (tail != end) tail[-1] = '/'; @@ -301,7 +287,7 @@ static git_tree_entry *find_file(git_repository *repo, git_tree *tree, if (git_tree_entry_bypath(&entry, tree, canon_path)) { const git_error *e = giterr_last(); - fprintf(stderr, "%s: %s\n", path, e->message); + error("%s: %s\n", path, e->message); free(canon_path); return NULL; } @@ -318,15 +304,12 @@ static const void *get_data(struct vcs_git *vcs_git, git_tree_entry *entry, git_object *obj; git_blob *blob; - if (git_tree_entry_type(entry) != GIT_OBJ_BLOB) { - fprintf(stderr, "entry is not a blob\n"); - exit(1); - } + if (git_tree_entry_type(entry) != GIT_OBJ_BLOB) + fatal("entry is not a blob\n"); if (git_tree_entry_to_object(&obj, repo, entry)) { const git_error *e = giterr_last(); - fprintf(stderr, "%s\n", e->message); - exit(1); + fatal("%s\n", e->message); } vcs_git->obj = obj; @@ -336,8 +319,7 @@ static const void *get_data(struct vcs_git *vcs_git, git_tree_entry *entry, if (git_object_short_id(&buf, obj)) { const git_error *e = giterr_last(); - fprintf(stderr, "%s\n", e->message); - exit(1); + fatal("%s\n", e->message); } progress(3, "object %s\n", buf.ptr); git_buf_free(&buf); @@ -394,8 +376,7 @@ static bool related_other_repo(struct vcs_git *vcs_git) /* @@@ find revision <= date of revision in related */ if (!shown) - fprintf(stderr, - "warning: related_other_repo is not yet implemented\n"); + warning("related_other_repo is not yet implemented\n"); shown = 1; return 0; } @@ -463,7 +444,7 @@ struct vcs_git *vcs_git_open(const char *revision, const char *name, vcs_git->repo = select_repo(name); if (!vcs_git->repo) { - fprintf(stderr, "%s: not found\n", name); + error("%s: not found\n", name); goto fail; } progress(2, "using repository %s\n", diff --git a/eeshow/git-hist.c b/eeshow/git-hist.c index 6581542..886b0a3 100644 --- a/eeshow/git-hist.c +++ b/eeshow/git-hist.c @@ -88,8 +88,7 @@ static void recurse(struct hist *h, n = git_commit_parentcount(h->commit); if (verbose > 2) - fprintf(stderr, "commit %p: %u + %u\n", - h->commit, n_branches, n); + progress(3, "commit %p: %u + %u\n", h->commit, n_branches, n); b = alloca(sizeof(struct hist) * (n_branches - 1 + n)); n_branches--; @@ -104,8 +103,7 @@ static void recurse(struct hist *h, if (git_commit_parent(&commit, h->commit, i)) { e = giterr_last(); - fprintf(stderr, "git_commit_parent: %s\n", e->message); - exit(1); + fatal("git_commit_parent: %s\n", e->message); } for (j = 0; j != n_branches; j++) { found = find_commit(b[j], commit); @@ -156,22 +154,17 @@ struct hist *vcs_git_hist(const char *path) if (git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_CROSS_FS, NULL)) { e = giterr_last(); - fprintf(stderr, "%s: %s\n", path, e->message); - exit(1); + fatal("%s: %s\n", path, e->message); } if (git_reference_name_to_id(&oid, repo, "HEAD")) { e = giterr_last(); - fprintf(stderr, "%s: %s\n", - git_repository_path(repo), e->message); - exit(1); + fatal("%s: %s\n", git_repository_path(repo), e->message); } if (git_commit_lookup(&head->commit, repo, &oid)) { e = giterr_last(); - fprintf(stderr, "%s: %s\n", - git_repository_path(repo), e->message); - exit(1); + fatal("%s: %s\n", git_repository_path(repo), e->message); } recurse(head, 1, &head); @@ -210,8 +203,7 @@ const char *vcs_git_summary(struct hist *h) return summary; e = giterr_last(); - fprintf(stderr, "git_commit_summary: %s\n", e->message); - exit(1); + fatal("git_commit_summary: %s\n", e->message); } @@ -243,8 +235,7 @@ char *vcs_git_long_for_pango(struct hist *h) fail: e = giterr_last(); - fprintf(stderr, "vcs_git_long_for_pango: %s\n", e->message); - exit(1); + fatal("vcs_git_long_for_pango: %s\n", e->message); } @@ -269,9 +260,7 @@ void dump_hist(struct hist *h) if (h->commit) { if (git_object_short_id(&buf, (git_object *) h->commit)) { e = giterr_last(); - fprintf(stderr, "git_object_short_id: %s\n", - e->message); - exit(1); + fatal("git_object_short_id: %s\n", e->message); } printf("%*s%s %s\n", 2 * h->branch, "", buf.ptr, vcs_git_summary(h)); diff --git a/eeshow/gui.c b/eeshow/gui.c index 31a50bf..fe968c8 100644 --- a/eeshow/gui.c +++ b/eeshow/gui.c @@ -29,6 +29,7 @@ #include #include "util.h" +#include "diag.h" #include "style.h" #include "cro.h" #include "gfx.h" @@ -1294,10 +1295,8 @@ int gui(unsigned n_args, char **args, bool recurse, int limit) get_revisions(&ctx, n_args, args, recurse, limit); for (ctx.new_hist = ctx.hist; ctx.new_hist && !ctx.new_hist->sheets; ctx.new_hist = ctx.new_hist->next); - if (!ctx.new_hist) { - fprintf(stderr, "no valid sheets\n"); - return 1; - } + if (!ctx.new_hist) + fatal("no valid sheets\n"); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); diff --git a/eeshow/lib-parse.c b/eeshow/lib-parse.c index 955454a..015b031 100644 --- a/eeshow/lib-parse.c +++ b/eeshow/lib-parse.c @@ -16,6 +16,7 @@ #include #include "util.h" +#include "diag.h" #include "text.h" #include "file.h" #include "lib.h" @@ -250,11 +251,9 @@ static bool lib_parse_line(const struct file *file, * zero2 seems to be the font style: 0 = normal, 1 = bold ? */ if (n == 12) { - if (zero1) { - fprintf(stderr, "%u: only understand 0 x x\n" + if (zero1) + fatal("%u: only understand 0 x x\n" "\"%s\"\n", file->lineno, line); - exit(1); - } obj->u.text.style = decode_style(style); obj->type = lib_obj_text; return 1; @@ -272,8 +271,7 @@ static bool lib_parse_line(const struct file *file, default: abort(); } - fprintf(stderr, "%u: cannot parse\n\"%s\"\n", file->lineno, line); - exit(1); + fatal("%u: cannot parse\n\"%s\"\n", file->lineno, line); } diff --git a/eeshow/lib-render.c b/eeshow/lib-render.c index 098ad09..2e7407e 100644 --- a/eeshow/lib-render.c +++ b/eeshow/lib-render.c @@ -16,6 +16,7 @@ #include #include "util.h" +#include "diag.h" #include "misc.h" #include "style.h" #include "gfx.h" @@ -379,8 +380,7 @@ const struct comp *lib_find(const struct lib *lib, const char *name) if (!strcmp(alias->name, name)) return comp; } - fprintf(stderr, "\"%s\" not found\n", name); - exit(1); + fatal("\"%s\" not found\n", name); } diff --git a/eeshow/main.c b/eeshow/main.c index d104b14..d8313b9 100644 --- a/eeshow/main.c +++ b/eeshow/main.c @@ -204,9 +204,7 @@ int main(int argc, char **argv) for (ops = ops_list; ops != ARRAY_END(ops_list); ops++) if (!strcmp((*ops)->name, *gfx_argv)) goto found; - fprintf(stderr, "graphics backend \"%s\" not found\n", - *gfx_argv); - exit(1); + fatal("graphics backend \"%s\" not found\n", *gfx_argv); found: ; } @@ -221,11 +219,8 @@ found: if (recurse) { const struct sheet *sheet; - if (!gfx_multi_sheet()) { - fprintf(stderr, - "graphics backend only supports single sheet\n"); - exit(1); - } + if (!gfx_multi_sheet()) + fatal("graphics backend only supports single sheet\n"); for (sheet = sch_ctx.sheets; sheet; sheet = sheet->next) { sch_render(sheet); if (sheet->next) diff --git a/eeshow/misc.c b/eeshow/misc.c index d6ec408..687ce01 100644 --- a/eeshow/misc.c +++ b/eeshow/misc.c @@ -16,6 +16,7 @@ #include #include +#include "diag.h" #include "misc.h" @@ -45,9 +46,7 @@ unsigned matrix_to_angle(const int m[6]) if (eq(m, 0, -1, 1, 0)) /* x-flipped, 270 deg */ return 270; - fprintf(stderr, "unrecognized matrix %d %d %d %d\n", - m[1], m[2], m[4], m[5]); - exit(1); + fatal("unrecognized matrix %d %d %d %d\n", m[1], m[2], m[4], m[5]); } diff --git a/eeshow/sch-parse.c b/eeshow/sch-parse.c index 609b1d9..6d63309 100644 --- a/eeshow/sch-parse.c +++ b/eeshow/sch-parse.c @@ -20,6 +20,7 @@ #include #include "util.h" +#include "diag.h" #include "dwg.h" #include "file.h" #include "lib.h" @@ -41,8 +42,7 @@ static enum dwg_shape do_decode_shape(const char *s) return dwg_tri; if (!strcmp(s, "BiDi")) return dwg_bidir; - fprintf(stderr, "unknown shape: \"%s\"\n", s); - exit(1); + fatal("unknown shape: \"%s\"\n", s); } @@ -176,8 +176,7 @@ static enum dwg_shape decode_form(char form) case 'U': return dwg_unspec; default: - fprintf(stderr, "unknown form: \"%c\"\n", form); - exit(1); + fatal("unknown form: \"%c\"\n", form); } } @@ -194,8 +193,7 @@ static int decode_side(char side) case 'T': return 3; /* down */ default: - fprintf(stderr, "unknown side: \"%c\"\n", side); - exit(1); + fatal("unknown side: \"%c\"\n", side); } } @@ -569,9 +567,7 @@ static bool parse_line(const struct file *file, void *user, const char *line) default: abort(); } - fprintf(stderr, "%s:%u: cannot parse\n\"%s\"\n", - file->name, file->lineno, line); - exit(1); + fatal("%s:%u: cannot parse\n\"%s\"\n", file->name, file->lineno, line); }