1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-12-23 10:04:36 +02:00

eeshow/: use dedicated functions for diagnostics, instead of fprintf and exit

This commit is contained in:
Werner Almesberger 2016-08-11 17:19:32 -03:00
parent 3e236d0456
commit 62ca12c2da
13 changed files with 111 additions and 102 deletions

View File

@ -22,6 +22,38 @@
unsigned verbose = 0; 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, ...) void progress(unsigned level, const char *fmt, ...)
{ {
va_list ap; va_list ap;

View File

@ -23,6 +23,28 @@
extern unsigned verbose; 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 * Progress message, used mainly for debugging. "level" is the minimum
* verbosity level required. * verbosity level required.

View File

@ -20,6 +20,7 @@
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "main.h" #include "main.h"
#include "cro.h" #include "cro.h"
#include "file.h" #include "file.h"
@ -280,11 +281,8 @@ static void diff_end(void *ctx)
int w, h, stride; int w, h, stride;
old_img = cro_img_end(diff->cr_ctx, &w, &h, &stride); old_img = cro_img_end(diff->cr_ctx, &w, &h, &stride);
if (diff->w != w || diff->h != h) { if (diff->w != w || diff->h != h)
fprintf(stderr, "%d x %d vs. %d x %d image\n", fatal("%d x %d vs. %d x %d image\n", w, h, diff->w, diff->h);
w, h, diff->w, diff->h);
exit(1);
}
differences(diff, old_img, diff->new_img); differences(diff, old_img, diff->new_img);
show_areas(diff, old_img); show_areas(diff, old_img);

View File

@ -194,7 +194,7 @@ bool file_open(struct file *file, const char *name, const struct file *related)
if (file->vcs) if (file->vcs)
return 1; return 1;
fprintf(stderr, "could not open %s\n", name); error("could not open %s\n", name);
fail: fail:
free((char *) file->name); free((char *) file->name);
return 0; return 0;

View File

@ -19,6 +19,7 @@
#include <alloca.h> #include <alloca.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "fmt-pango.h" #include "fmt-pango.h"
@ -107,8 +108,7 @@ unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap)
res++; res++;
break; break;
default: default:
fprintf(stderr, "unrecognized format '%%%c'\n", *q); fatal("unrecognized format '%%%c'\n", *q);
exit(1);
} }
p = q; p = q;
} }

View File

@ -99,22 +99,17 @@ static git_tree *pick_revision(git_repository *repo, const char *revision)
if (git_revparse_single(&obj, repo, revision)) { if (git_revparse_single(&obj, repo, revision)) {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
fprintf(stderr, "%s: %s\n", fatal("%s: %s\n", git_repository_path(repo), e->message);
git_repository_path(repo), e->message);
exit(1);
} }
if (git_object_type(obj) != GIT_OBJ_COMMIT) { if (git_object_type(obj) != GIT_OBJ_COMMIT)
fprintf(stderr, "%s: not a commit\n", revision); fatal("%s: not a commit\n", revision);
exit(1);
}
commit = (git_commit *) obj; commit = (git_commit *) obj;
if (git_commit_tree(&tree, commit)) { if (git_commit_tree(&tree, commit)) {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
fprintf(stderr, "%s: %s\n", revision, e->message); fatal("%s: %s\n", revision, e->message);
exit(1);
} }
return tree; return tree;
@ -134,10 +129,8 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path)
perror(repo_dir); perror(repo_dir);
exit(1); exit(1);
} }
if (!S_ISDIR(repo_st.st_mode)) { if (!S_ISDIR(repo_st.st_mode))
fprintf(stderr, "%s: not a directory\n", repo_dir); fatal("%s: not a directory\n", repo_dir);
exit(1);
}
/* convert relative paths to absolute */ /* 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); progress(3, "probing \"%s\" tail \"%s\"\n", tmp, tail);
if (stat(tmp, &path_st) == 0) if (stat(tmp, &path_st) == 0)
break; break;
if (!tmp[1]) { if (!tmp[1])
fprintf(stderr, "%s: cannot resolve\n", path); fatal("%s: cannot resolve\n", path);
exit(1);
}
slash = strrchr(tmp, '/'); slash = strrchr(tmp, '/');
if (tail != end) if (tail != end)
tail[-1] = '/'; tail[-1] = '/';
@ -198,15 +189,13 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path)
if (!*from) if (!*from)
break; break;
} }
if (to == tail) {
/* /*
* We have something like this: * We have something like this:
* /home/repo/dead/../../foo * /home/repo/dead/../../foo
*/ */
fprintf(stderr, "%s: can't climb out of dead path\n", if (to == tail)
path); fatal("%s: can't climb out of dead path\n", path);
exit(1);
}
/* /*
* We have something like * We have something like
@ -255,12 +244,9 @@ static char *canonical_path_into_repo(const char *repo_dir, const char *path)
slash = strrchr(tmp, '/'); slash = strrchr(tmp, '/');
/* "this cannot happen" */ /* "this cannot happen" */
if (tail == tmp || !slash) { if (tail == tmp || !slash)
fprintf(stderr, fatal("divergent paths:\nrepo \"%s\"\nobject \"%s\"\n",
"divergent paths:\nrepo \"%s\"\nobject \"%s\"\n",
repo_dir, tail); repo_dir, tail);
exit(1);
}
if (tail != end) if (tail != end)
tail[-1] = '/'; 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)) { if (git_tree_entry_bypath(&entry, tree, canon_path)) {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
fprintf(stderr, "%s: %s\n", path, e->message); error("%s: %s\n", path, e->message);
free(canon_path); free(canon_path);
return NULL; return NULL;
} }
@ -318,15 +304,12 @@ static const void *get_data(struct vcs_git *vcs_git, git_tree_entry *entry,
git_object *obj; git_object *obj;
git_blob *blob; git_blob *blob;
if (git_tree_entry_type(entry) != GIT_OBJ_BLOB) { if (git_tree_entry_type(entry) != GIT_OBJ_BLOB)
fprintf(stderr, "entry is not a blob\n"); fatal("entry is not a blob\n");
exit(1);
}
if (git_tree_entry_to_object(&obj, repo, entry)) { if (git_tree_entry_to_object(&obj, repo, entry)) {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
fprintf(stderr, "%s\n", e->message); fatal("%s\n", e->message);
exit(1);
} }
vcs_git->obj = obj; 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)) { if (git_object_short_id(&buf, obj)) {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
fprintf(stderr, "%s\n", e->message); fatal("%s\n", e->message);
exit(1);
} }
progress(3, "object %s\n", buf.ptr); progress(3, "object %s\n", buf.ptr);
git_buf_free(&buf); 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 */ /* @@@ find revision <= date of revision in related */
if (!shown) if (!shown)
fprintf(stderr, warning("related_other_repo is not yet implemented\n");
"warning: related_other_repo is not yet implemented\n");
shown = 1; shown = 1;
return 0; return 0;
} }
@ -463,7 +444,7 @@ struct vcs_git *vcs_git_open(const char *revision, const char *name,
vcs_git->repo = select_repo(name); vcs_git->repo = select_repo(name);
if (!vcs_git->repo) { if (!vcs_git->repo) {
fprintf(stderr, "%s: not found\n", name); error("%s: not found\n", name);
goto fail; goto fail;
} }
progress(2, "using repository %s\n", progress(2, "using repository %s\n",

View File

@ -88,8 +88,7 @@ static void recurse(struct hist *h,
n = git_commit_parentcount(h->commit); n = git_commit_parentcount(h->commit);
if (verbose > 2) if (verbose > 2)
fprintf(stderr, "commit %p: %u + %u\n", progress(3, "commit %p: %u + %u\n", h->commit, n_branches, n);
h->commit, n_branches, n);
b = alloca(sizeof(struct hist) * (n_branches - 1 + n)); b = alloca(sizeof(struct hist) * (n_branches - 1 + n));
n_branches--; n_branches--;
@ -104,8 +103,7 @@ static void recurse(struct hist *h,
if (git_commit_parent(&commit, h->commit, i)) { if (git_commit_parent(&commit, h->commit, i)) {
e = giterr_last(); e = giterr_last();
fprintf(stderr, "git_commit_parent: %s\n", e->message); fatal("git_commit_parent: %s\n", e->message);
exit(1);
} }
for (j = 0; j != n_branches; j++) { for (j = 0; j != n_branches; j++) {
found = find_commit(b[j], commit); 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, if (git_repository_open_ext(&repo, path,
GIT_REPOSITORY_OPEN_CROSS_FS, NULL)) { GIT_REPOSITORY_OPEN_CROSS_FS, NULL)) {
e = giterr_last(); e = giterr_last();
fprintf(stderr, "%s: %s\n", path, e->message); fatal("%s: %s\n", path, e->message);
exit(1);
} }
if (git_reference_name_to_id(&oid, repo, "HEAD")) { if (git_reference_name_to_id(&oid, repo, "HEAD")) {
e = giterr_last(); e = giterr_last();
fprintf(stderr, "%s: %s\n", fatal("%s: %s\n", git_repository_path(repo), e->message);
git_repository_path(repo), e->message);
exit(1);
} }
if (git_commit_lookup(&head->commit, repo, &oid)) { if (git_commit_lookup(&head->commit, repo, &oid)) {
e = giterr_last(); e = giterr_last();
fprintf(stderr, "%s: %s\n", fatal("%s: %s\n", git_repository_path(repo), e->message);
git_repository_path(repo), e->message);
exit(1);
} }
recurse(head, 1, &head); recurse(head, 1, &head);
@ -210,8 +203,7 @@ const char *vcs_git_summary(struct hist *h)
return summary; return summary;
e = giterr_last(); e = giterr_last();
fprintf(stderr, "git_commit_summary: %s\n", e->message); fatal("git_commit_summary: %s\n", e->message);
exit(1);
} }
@ -243,8 +235,7 @@ char *vcs_git_long_for_pango(struct hist *h)
fail: fail:
e = giterr_last(); e = giterr_last();
fprintf(stderr, "vcs_git_long_for_pango: %s\n", e->message); fatal("vcs_git_long_for_pango: %s\n", e->message);
exit(1);
} }
@ -269,9 +260,7 @@ void dump_hist(struct hist *h)
if (h->commit) { if (h->commit) {
if (git_object_short_id(&buf, (git_object *) h->commit)) { if (git_object_short_id(&buf, (git_object *) h->commit)) {
e = giterr_last(); e = giterr_last();
fprintf(stderr, "git_object_short_id: %s\n", fatal("git_object_short_id: %s\n", e->message);
e->message);
exit(1);
} }
printf("%*s%s %s\n", printf("%*s%s %s\n",
2 * h->branch, "", buf.ptr, vcs_git_summary(h)); 2 * h->branch, "", buf.ptr, vcs_git_summary(h));

View File

@ -29,6 +29,7 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "style.h" #include "style.h"
#include "cro.h" #include "cro.h"
#include "gfx.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); get_revisions(&ctx, n_args, args, recurse, limit);
for (ctx.new_hist = ctx.hist; ctx.new_hist && !ctx.new_hist->sheets; for (ctx.new_hist = ctx.hist; ctx.new_hist && !ctx.new_hist->sheets;
ctx.new_hist = ctx.new_hist->next); ctx.new_hist = ctx.new_hist->next);
if (!ctx.new_hist) { if (!ctx.new_hist)
fprintf(stderr, "no valid sheets\n"); fatal("no valid sheets\n");
return 1;
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

View File

@ -16,6 +16,7 @@
#include <assert.h> #include <assert.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "text.h" #include "text.h"
#include "file.h" #include "file.h"
#include "lib.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 ? * zero2 seems to be the font style: 0 = normal, 1 = bold ?
*/ */
if (n == 12) { if (n == 12) {
if (zero1) { if (zero1)
fprintf(stderr, "%u: only understand 0 x x\n" fatal("%u: only understand 0 x x\n"
"\"%s\"\n", file->lineno, line); "\"%s\"\n", file->lineno, line);
exit(1);
}
obj->u.text.style = decode_style(style); obj->u.text.style = decode_style(style);
obj->type = lib_obj_text; obj->type = lib_obj_text;
return 1; return 1;
@ -272,8 +271,7 @@ static bool lib_parse_line(const struct file *file,
default: default:
abort(); abort();
} }
fprintf(stderr, "%u: cannot parse\n\"%s\"\n", file->lineno, line); fatal("%u: cannot parse\n\"%s\"\n", file->lineno, line);
exit(1);
} }

View File

@ -16,6 +16,7 @@
#include <assert.h> #include <assert.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "misc.h" #include "misc.h"
#include "style.h" #include "style.h"
#include "gfx.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)) if (!strcmp(alias->name, name))
return comp; return comp;
} }
fprintf(stderr, "\"%s\" not found\n", name); fatal("\"%s\" not found\n", name);
exit(1);
} }

View File

@ -204,9 +204,7 @@ int main(int argc, char **argv)
for (ops = ops_list; ops != ARRAY_END(ops_list); ops++) for (ops = ops_list; ops != ARRAY_END(ops_list); ops++)
if (!strcmp((*ops)->name, *gfx_argv)) if (!strcmp((*ops)->name, *gfx_argv))
goto found; goto found;
fprintf(stderr, "graphics backend \"%s\" not found\n", fatal("graphics backend \"%s\" not found\n", *gfx_argv);
*gfx_argv);
exit(1);
found: found:
; ;
} }
@ -221,11 +219,8 @@ found:
if (recurse) { if (recurse) {
const struct sheet *sheet; const struct sheet *sheet;
if (!gfx_multi_sheet()) { if (!gfx_multi_sheet())
fprintf(stderr, fatal("graphics backend only supports single sheet\n");
"graphics backend only supports single sheet\n");
exit(1);
}
for (sheet = sch_ctx.sheets; sheet; sheet = sheet->next) { for (sheet = sch_ctx.sheets; sheet; sheet = sheet->next) {
sch_render(sheet); sch_render(sheet);
if (sheet->next) if (sheet->next)

View File

@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include "diag.h"
#include "misc.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 */ if (eq(m, 0, -1, 1, 0)) /* x-flipped, 270 deg */
return 270; return 270;
fprintf(stderr, "unrecognized matrix %d %d %d %d\n", fatal("unrecognized matrix %d %d %d %d\n", m[1], m[2], m[4], m[5]);
m[1], m[2], m[4], m[5]);
exit(1);
} }

View File

@ -20,6 +20,7 @@
#include <assert.h> #include <assert.h>
#include "util.h" #include "util.h"
#include "diag.h"
#include "dwg.h" #include "dwg.h"
#include "file.h" #include "file.h"
#include "lib.h" #include "lib.h"
@ -41,8 +42,7 @@ static enum dwg_shape do_decode_shape(const char *s)
return dwg_tri; return dwg_tri;
if (!strcmp(s, "BiDi")) if (!strcmp(s, "BiDi"))
return dwg_bidir; return dwg_bidir;
fprintf(stderr, "unknown shape: \"%s\"\n", s); fatal("unknown shape: \"%s\"\n", s);
exit(1);
} }
@ -176,8 +176,7 @@ static enum dwg_shape decode_form(char form)
case 'U': case 'U':
return dwg_unspec; return dwg_unspec;
default: default:
fprintf(stderr, "unknown form: \"%c\"\n", form); fatal("unknown form: \"%c\"\n", form);
exit(1);
} }
} }
@ -194,8 +193,7 @@ static int decode_side(char side)
case 'T': case 'T':
return 3; /* down */ return 3; /* down */
default: default:
fprintf(stderr, "unknown side: \"%c\"\n", side); fatal("unknown side: \"%c\"\n", side);
exit(1);
} }
} }
@ -569,9 +567,7 @@ static bool parse_line(const struct file *file, void *user, const char *line)
default: default:
abort(); abort();
} }
fprintf(stderr, "%s:%u: cannot parse\n\"%s\"\n", fatal("%s:%u: cannot parse\n\"%s\"\n", file->name, file->lineno, line);
file->name, file->lineno, line);
exit(1);
} }