From fbf525ae910ae48b1e6401fd2307772e214b1baa Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 10 Jun 2015 19:32:02 -0700 Subject: Preparing for lisp loading. * parser.c (open_txr_file): Rewritten to take new argument which indicates whether to treat an unsuffixed file as TXR or TXR Lisp, and is updated to indicate which is the case by looking at the suffix. * parser.h (open_txr_file): Declaration updated. * match.c (v_load): Follow change in open_txr_file. * txr.c (txr_main): Likewise. --- ChangeLog | 15 +++++++++++++++ match.c | 3 ++- parser.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- parser.h | 2 +- txr.c | 5 +++-- 5 files changed, 79 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 846a2fab..548a99df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2015-06-10 Kaz Kylheku + + Preparing for lisp loading. + + * parser.c (open_txr_file): Rewritten to take new argument + which indicates whether to treat an unsuffixed file as + TXR or TXR Lisp, and is updated to indicate which is the + case by looking at the suffix. + + * parser.h (open_txr_file): Declaration updated. + + * match.c (v_load): Follow change in open_txr_file. + + * txr.c (txr_main): Likewise. + 2015-06-10 Kaz Kylheku Error handling improvement in read. diff --git a/match.c b/match.c index b6d4163b..ccc1fa37 100644 --- a/match.c +++ b/match.c @@ -3723,8 +3723,9 @@ static val v_load(match_files_ctx *c) int gc = gc_state(0); val stream, name; parser_t parser; + val txr_lisp_p = nil; - open_txr_file(path, &name, &stream); + open_txr_file(path, &txr_lisp_p, &name, &stream); parse_once(stream, name, &parser); gc_state(gc); diff --git a/parser.c b/parser.c index 34903ce9..2cebed04 100644 --- a/parser.c +++ b/parser.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "config.h" #include "lib.h" #include "signal.h" @@ -120,17 +121,67 @@ val prime_parser(val parser) return parser; } -void open_txr_file(val spec_file, val *name, val *stream) +void open_txr_file(val spec_file, val *txr_lisp_p, val *name, val *stream) { + enum { none, tl, txr } suffix; + + if (match_str(spec_file, lit(".txr"), negone)) + suffix = txr; + else if (match_str(spec_file, lit(".tl"), negone)) + suffix = tl; + else + suffix = none; + + errno = 0; + { - FILE *in = w_fopen(c_str(spec_file), L"r"); + val spec_file_try = spec_file; + FILE *in = w_fopen(c_str(spec_file_try), L"r"); + + if (in != 0) { + switch (suffix) { + case tl: + *txr_lisp_p = t; + break; + case txr: + *txr_lisp_p = nil; + break; + default: + break; + } + } + +#ifdef ENOENT + if (in == 0 && errno != ENOENT) + goto except; + errno = 0; +#endif + + if (suffix == none && in == 0 && !*txr_lisp_p) { + spec_file_try = cat_str(list(spec_file, lit("txr"), nao), lit(".")); + in = w_fopen(c_str(spec_file_try), L"r"); +#ifdef ENOENT + if (in == 0 && errno != ENOENT) + goto except; + errno = 0; +#endif + } + + + if (suffix == none && in == 0) { + spec_file_try = cat_str(list(spec_file, lit("tl"), nao), lit(".")); + in = w_fopen(c_str(spec_file_try), L"r"); + *txr_lisp_p = t; + } + if (in == 0) { - spec_file = cat_str(list(spec_file, lit("txr"), nao), lit(".")); - in = w_fopen(c_str(spec_file), L"r"); - if (in == 0) - uw_throwf(file_error_s, lit("unable to open ~a"), spec_file, nao); +#ifdef ENOENT +except: +#endif + uw_throwf(file_error_s, lit("unable to open ~a"), spec_file_try, nao); } - *stream = make_stdio_stream(in, spec_file); + + *stream = make_stdio_stream(in, spec_file_try); *name = spec_file; } } diff --git a/parser.h b/parser.h index 46572b1f..bf1c1025 100644 --- a/parser.h +++ b/parser.h @@ -57,7 +57,7 @@ int yylex_destroy(yyscan_t scanner); parser_t *yyget_extra(yyscan_t scanner); void yyset_extra(parser_t *, yyscan_t); void parser_l_init(void); -void open_txr_file(val spec_file, val *name, val *stream); +void open_txr_file(val spec_file, val *txr_lisp_p, val *name, val *stream); val prime_parser(val parser); int parse_once(val stream, val name, parser_t *parser); int parse(parser_t *parser); diff --git a/txr.c b/txr.c index 23055734..af453af2 100644 --- a/txr.c +++ b/txr.c @@ -371,6 +371,7 @@ int txr_main(int argc, char **argv) int match_loglevel = opt_loglevel; val arg_undo = nil, arg; val parse_stream = std_input; + val txr_lisp_p = nil; list_collect_decl(arg_list, arg_tail); setvbuf(stderr, 0, _IOLBF, 0); @@ -631,7 +632,7 @@ int txr_main(int argc, char **argv) arg_list = arg_undo; } else if (spec_file) { if (wcscmp(c_str(spec_file), L"-") != 0) { - open_txr_file(spec_file, &spec_file_str, &parse_stream); + open_txr_file(spec_file, &txr_lisp_p, &spec_file_str, &parse_stream); } else { spec_file_str = lit("stdin"); } @@ -646,7 +647,7 @@ int txr_main(int argc, char **argv) } if (!equal(arg, lit("-"))) { - open_txr_file(arg, &spec_file_str, &parse_stream); + open_txr_file(arg, &txr_lisp_p, &spec_file_str, &parse_stream); } else { spec_file_str = lit("stdin"); } -- cgit v1.2.3