summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-05 21:02:57 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-05 21:02:57 -0300
commit12164098658bd4bb80b107d748342f223e62b2a0 (patch)
tree2954ff0d07f96eea0b6db5862791299c4797ee7e
parentcb7002960e47960468153249c5d4d6c7a2886981 (diff)
downloadeeshow-12164098658bd4bb80b107d748342f223e62b2a0.tar.gz
eeshow-12164098658bd4bb80b107d748342f223e62b2a0.tar.bz2
eeshow-12164098658bd4bb80b107d748342f223e62b2a0.zip
eeshow/ (file_open, *_parse): return 0 on error (may still exit)
-rw-r--r--diff.c17
-rw-r--r--file.c21
-rw-r--r--file.h2
-rw-r--r--gui.c17
-rw-r--r--lib-parse.c9
-rw-r--r--lib.h2
-rw-r--r--main.c15
-rw-r--r--sch-parse.c25
-rw-r--r--sch.h2
9 files changed, 76 insertions, 34 deletions
diff --git a/diff.c b/diff.c
index f6e1a23..8f48f4d 100644
--- a/diff.c
+++ b/diff.c
@@ -138,10 +138,13 @@ static void *diff_init(int argc, char *const *argv)
if (argc - optind < 1)
usage(*argv);
- file_open(&sch_file, argv[argc - 1], NULL);
+ if (!file_open(&sch_file, argv[argc - 1], NULL))
+ goto fail_open;
for (arg = optind; arg != argc - 1; arg++)
- lib_parse(&new_lib, argv[arg], &sch_file);
- sch_parse(&new_sch, &sch_file, &new_lib);
+ if (!lib_parse(&new_lib, argv[arg], &sch_file))
+ goto fail_parse;
+ if (!sch_parse(&new_sch, &sch_file, &new_lib))
+ goto fail_parse;
file_close(&sch_file);
optind = 0;
@@ -155,6 +158,14 @@ static void *diff_init(int argc, char *const *argv)
diff->cr_ctx = cro_img_ops.init(argc, argv);
return diff;
+
+fail_parse:
+ file_close(&sch_file);
+fail_open:
+ sch_free(&new_sch);
+ lib_free(&new_lib);
+ free(diff);
+ return NULL;
}
diff --git a/file.c b/file.c
index 2d6aa17..428f211 100644
--- a/file.c
+++ b/file.c
@@ -137,7 +137,7 @@ static void *open_vcs(struct file *file)
}
-void file_open(struct file *file, const char *name, const struct file *related)
+bool file_open(struct file *file, const char *name, const struct file *related)
{
file->name = stralloc(name);
file->lineno = 0;
@@ -148,18 +148,18 @@ void file_open(struct file *file, const char *name, const struct file *related)
if (related && related->vcs) {
file->vcs = open_vcs(file);
if (file->vcs)
- return;
+ return 1;
}
file->file = fopen(name, "r");
if (file->file) {
if (verbose)
fprintf(stderr, "reading %s\n", name);
- return;
+ return 1;
}
if (try_related(file))
- return;
+ return 1;
if (verbose)
perror(name);
@@ -167,14 +167,17 @@ void file_open(struct file *file, const char *name, const struct file *related)
if (!strchr(name, ':')) {
if (!verbose)
perror(name);
- exit(1);
+ goto fail;
}
file->vcs = open_vcs(file);
- if (!file->vcs) {
- fprintf(stderr, "could not open %s\n", name);
- exit(1);
- }
+ if (file->vcs)
+ return 1;
+
+ fprintf(stderr, "could not open %s\n", name);
+fail:
+ free((char *) file->name);
+ return 0;
}
diff --git a/file.h b/file.h
index b77e99c..6a0e9ae 100644
--- a/file.h
+++ b/file.h
@@ -30,7 +30,7 @@ bool file_cat(const struct file *file, void *user, const char *line);
char *file_graft_relative(const char *base, const char *name);
-void file_open(struct file *file, const char *name,
+bool file_open(struct file *file, const char *name,
const struct file *related);
bool file_read(struct file *file,
bool (*parse)(const struct file *file, void *user, const char *line),
diff --git a/gui.c b/gui.c
index bcdff7f..0840731 100644
--- a/gui.c
+++ b/gui.c
@@ -607,15 +607,26 @@ static struct sheet *parse_sheets(int n_args, char **args, bool recurse)
int i;
sch_init(&sch_ctx, recurse);
- file_open(&sch_file, args[n_args - 1], NULL);
+ if (!file_open(&sch_file, args[n_args - 1], NULL)) {
+ sch_free(&sch_ctx);
+ return NULL;
+ }
lib_init(&lib);
for (i = 0; i != n_args - 1; i++)
- lib_parse(&lib, args[i], &sch_file);
- sch_parse(&sch_ctx, &sch_file, &lib);
+ if (!lib_parse(&lib, args[i], &sch_file))
+ goto fail;
+ if (!sch_parse(&sch_ctx, &sch_file, &lib))
+ goto fail;
file_close(&sch_file);
return sch_ctx.sheets;
+
+fail:
+ sch_free(&sch_ctx);
+ lib_free(&lib);
+ file_close(&sch_file);
+ return NULL;
}
diff --git a/lib-parse.c b/lib-parse.c
index 93ef2d7..00a8854 100644
--- a/lib-parse.c
+++ b/lib-parse.c
@@ -252,14 +252,17 @@ static bool lib_parse_line(const struct file *file,
}
-void lib_parse(struct lib *lib, const char *name, const struct file *related)
+bool lib_parse(struct lib *lib, const char *name, const struct file *related)
{
struct file file;
+ bool res;
lib->state = lib_skip;
- file_open(&file, name, related);
- file_read(&file, lib_parse_line, lib);
+ if (!file_open(&file, name, related))
+ return 0;
+ res = file_read(&file, lib_parse_line, lib);
file_close(&file);
+ return res;
}
diff --git a/lib.h b/lib.h
index 22f8720..115a516 100644
--- a/lib.h
+++ b/lib.h
@@ -119,7 +119,7 @@ const struct comp *lib_find(const struct lib *lib, const char *name);
bool lib_field_visible(const struct comp *comp, int n);
void lib_render(const struct comp *comp, unsigned unit, const int m[6]);
-void lib_parse(struct lib *lib, const char *name, const struct file *related);
+bool lib_parse(struct lib *lib, const char *name, const struct file *related);
void lib_init(struct lib *lib);
void lib_free(struct lib *lib);
diff --git a/main.c b/main.c
index a29d97e..091bba8 100644
--- a/main.c
+++ b/main.c
@@ -129,8 +129,10 @@ int main(int argc, char **argv)
if (argc != optind)
usage(*argv);
- file_open(&file, cat, NULL);
- file_read(&file, file_cat, NULL);
+ if (!file_open(&file, cat, NULL))
+ return 1;
+ if (!file_read(&file, file_cat, NULL))
+ return 1;
file_close(&file);
return 0;
}
@@ -158,11 +160,13 @@ int main(int argc, char **argv)
}
sch_init(&sch_ctx, recurse);
- file_open(&sch_file, argv[dashdash - 1], NULL);
+ if (!file_open(&sch_file, argv[dashdash - 1], NULL))
+ return 1;
lib_init(&lib);
for (arg = optind; arg != dashdash - 1; arg++)
- lib_parse(&lib, argv[arg], &sch_file);
+ if (!lib_parse(&lib, argv[arg], &sch_file))
+ return 1;
if (dashdash == argc) {
gfx_argc = 1;
@@ -189,7 +193,8 @@ found:
optind = 0; /* reset getopt */
- sch_parse(&sch_ctx, &sch_file, &lib);
+ if (!sch_parse(&sch_ctx, &sch_file, &lib))
+ return 1;
file_close(&sch_file);
gfx_init(*ops, gfx_argc, gfx_argv);
diff --git a/sch-parse.c b/sch-parse.c
index eb3955f..49ddeae 100644
--- a/sch-parse.c
+++ b/sch-parse.c
@@ -314,12 +314,16 @@ static const struct sheet *recurse_sheet(struct sch_ctx *ctx,
const char *name = ctx->obj.u.sheet.file;
const struct sheet *sheet;
struct file file;
+ bool res;
+ if (!file_open(&file, name, related))
+ return NULL;
sheet = new_sheet(ctx);
ctx->state = sch_descr;
- file_open(&file, name, related);
- file_read(&file, parse_line, ctx);
+ res = file_read(&file, parse_line, ctx);
file_close(&file);
+ if (!res)
+ return NULL; /* caller MUST clean up */
end_sheet(ctx);
return sheet;
@@ -487,11 +491,16 @@ static bool parse_line(const struct file *file, void *user, const char *line)
struct sch_obj *sheet_obj;
sheet_obj = submit_obj(ctx, sch_obj_sheet);
- if (ctx->recurse)
- sheet_obj->u.sheet.sheet =
- recurse_sheet(ctx, file);
- else
+ if (ctx->recurse) {
+ const struct sheet *sheet;
+
+ sheet = recurse_sheet(ctx, file);
+ if (!sheet)
+ return 0;
+ sheet_obj->u.sheet.sheet = sheet;
+ } else {
sheet_obj->u.sheet.sheet = NULL;
+ }
ctx->state = sch_basic;
return 1;
}
@@ -543,10 +552,10 @@ static bool parse_line(const struct file *file, void *user, const char *line)
}
-void sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib)
+bool sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib)
{
ctx->lib = lib;
- file_read(file, parse_line, ctx);
+ return file_read(file, parse_line, ctx);
}
diff --git a/sch.h b/sch.h
index 61106e4..1aa31c7 100644
--- a/sch.h
+++ b/sch.h
@@ -118,7 +118,7 @@ struct sch_ctx {
void decode_alignment(struct text *txt, char hor, char vert);
void sch_render(const struct sheet *sheet);
-void sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib);
+bool sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib);
void sch_init(struct sch_ctx *ctx, bool recurse);
void sch_free(struct sch_ctx *ctx);