diff options
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | match.c | 3 | ||||
-rw-r--r-- | parser.c | 65 | ||||
-rw-r--r-- | parser.h | 2 | ||||
-rw-r--r-- | txr.c | 5 |
5 files changed, 79 insertions, 11 deletions
@@ -1,5 +1,20 @@ 2015-06-10 Kaz Kylheku <kaz@kylheku.com> + 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 <kaz@kylheku.com> + Error handling improvement in read. * parser.y (spec): New grammar production to handle the cases @@ -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); @@ -33,6 +33,7 @@ #include <setjmp.h> #include <wchar.h> #include <signal.h> +#include <errno.h> #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; } } @@ -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); @@ -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"); } |