1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-10-01 14:33:14 +03:00

eeshow/ (file_open, *_parse): return 0 on error (may still exit)

This commit is contained in:
Werner Almesberger 2016-08-05 21:02:57 -03:00
parent dfebad4e6a
commit f4944a5da9
9 changed files with 76 additions and 34 deletions

View File

@ -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;
}

View File

@ -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) {
if (file->vcs)
return 1;
fprintf(stderr, "could not open %s\n", name);
exit(1);
}
fail:
free((char *) file->name);
return 0;
}

View File

@ -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),

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);