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. --- parser.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 7 deletions(-) (limited to 'parser.c') 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; } } -- cgit v1.2.3