From f85551ea172e9a1ec70422b3623b28e9f74d8bc0 Mon Sep 17 00:00:00 2001 From: arno Date: Fri, 7 Jul 2000 14:09:06 +0000 Subject: [PATCH] * frontend/Makefile.am: Updates for new files. * frontend/breakp2.c, frontend/newcoms.c, frontend/postcoms.c, frontend/resource.c, frontend/terminal.h, frontend/variable.c, frontend/variable.h, frontend/com_compose.c, frontend/com_display.c, frontend/com_setscale.c, frontend/com_strcmp.c: Include files update. * parser/var2.c, parser/var2.h: Empty files, removed. * parser/Makefile.am: Updates for removed files. * parser/lexical.c: Small adjustments * parser/input.c, parser/input.h: Input, output and error streams handled in the frontend. Moved to the frontend directory. * frontend/streams.c: Its new home. --- src/frontend/Makefile.am | 7 ++ src/frontend/breakp2.c | 2 + src/frontend/com_compose.c | 1 + src/frontend/com_display.c | 1 + src/frontend/com_setscale.c | 10 +- src/frontend/com_strcmp.c | 4 + src/frontend/newcoms.c | 3 +- src/frontend/postcoms.c | 3 +- src/frontend/resource.c | 1 + src/frontend/streams.c | 211 ++++++++++++++++++++++++++++++++++++ src/frontend/terminal.h | 2 + src/frontend/variable.c | 5 +- src/frontend/variable.h | 4 + src/parser/Makefile.am | 4 - src/parser/input.c | 61 ++++++++--- src/parser/input.h | 3 +- src/parser/lexical.c | 45 -------- src/parser/var2.c | 13 --- src/parser/var2.h | 21 ---- 19 files changed, 292 insertions(+), 109 deletions(-) create mode 100644 src/frontend/streams.c delete mode 100644 src/parser/var2.c delete mode 100644 src/parser/var2.h diff --git a/src/frontend/Makefile.am b/src/frontend/Makefile.am index fc18840e9..53b2ba2be 100644 --- a/src/frontend/Makefile.am +++ b/src/frontend/Makefile.am @@ -15,10 +15,12 @@ libfte_a_SOURCES = \ com_asciiplot.h \ com_cdump.c \ com_cdump.h \ + com_chdir.c \ com_compose.c \ com_compose.h \ com_display.c \ com_display.h \ + com_echo.c \ com_ghelp.c \ com_ghelp.h \ com_hardcopy.c \ @@ -31,9 +33,11 @@ libfte_a_SOURCES = \ com_let.h \ com_plot.c \ com_plot.h \ + com_rehash.c \ com_set.c \ com_setscale.c \ com_setscale.h \ + com_shell.c \ com_shift.c \ com_strcmp.c \ com_strcmp.h \ @@ -48,7 +52,10 @@ libfte_a_SOURCES = \ hcomp.h \ init.c \ init.h \ + quote.c \ + quote.h \ streams.h \ + streams.c \ terminal.c \ terminal.h \ variable.c \ diff --git a/src/frontend/breakp2.c b/src/frontend/breakp2.c index e06839dbc..6764dcb11 100644 --- a/src/frontend/breakp2.c +++ b/src/frontend/breakp2.c @@ -12,6 +12,8 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include "ftedefs.h" #include "dvec.h" #include "ftedebug.h" + +#include "quote.h" #include "breakp2.h" diff --git a/src/frontend/com_compose.c b/src/frontend/com_compose.c index 467afa156..a29a3a8e4 100644 --- a/src/frontend/com_compose.c +++ b/src/frontend/com_compose.c @@ -9,6 +9,7 @@ #include #include +#include "quote.h" #include "com_compose.h" #include "completion.h" diff --git a/src/frontend/com_display.c b/src/frontend/com_display.c index 0c81b5054..c61dd2da1 100644 --- a/src/frontend/com_display.c +++ b/src/frontend/com_display.c @@ -5,6 +5,7 @@ #include #include "com_display.h" +#include "quote.h" #include "variable.h" #include "plotting/plotting.h" #include "plotting/pvec.h" diff --git a/src/frontend/com_setscale.c b/src/frontend/com_setscale.c index 312ddf3e1..dc76a7687 100644 --- a/src/frontend/com_setscale.c +++ b/src/frontend/com_setscale.c @@ -1,15 +1,17 @@ #include +#include #include -#include -#include "vectors.h" #include "com_setscale.h" +#include "quote.h" +#include "streams.h" +#include "vectors.h" #include "plotting/plotting.h" #include "plotting/pvec.h" + /* Set the default scale to the named vector. If no vector named, - * find and print the default scale. - */ + * find and print the default scale. */ void com_setscale(wordlist *wl) { diff --git a/src/frontend/com_strcmp.c b/src/frontend/com_strcmp.c index dcb9e36e0..2475985c5 100644 --- a/src/frontend/com_strcmp.c +++ b/src/frontend/com_strcmp.c @@ -1,7 +1,11 @@ +#include +#include + #include #include #include "com_strcmp.h" +#include "quote.h" #include "variable.h" diff --git a/src/frontend/newcoms.c b/src/frontend/newcoms.c index 99e921867..3bd5bd7f0 100644 --- a/src/frontend/newcoms.c +++ b/src/frontend/newcoms.c @@ -11,8 +11,9 @@ Copyright 1992 Regents of the University of California. All rights reserved. #include "ftedefs.h" #include "fteparse.h" #include "dvec.h" -#include "newcoms.h" +#include "newcoms.h" +#include "quote.h" /* * reshape v(1) vxx#branch [10] diff --git a/src/frontend/postcoms.c b/src/frontend/postcoms.c index fc4f9768f..802f82acc 100644 --- a/src/frontend/postcoms.c +++ b/src/frontend/postcoms.c @@ -14,8 +14,9 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include #include -#include "postcoms.h" #include "completion.h" +#include "postcoms.h" +#include "quote.h" #include "variable.h" /* static declarations */ diff --git a/src/frontend/resource.c b/src/frontend/resource.c index cbf3bd6b7..a9d82630b 100644 --- a/src/frontend/resource.c +++ b/src/frontend/resource.c @@ -13,6 +13,7 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include "ftedefs.h" #include "circuits.h" +#include "quote.h" #include "resource.h" #include "variable.h" diff --git a/src/frontend/streams.c b/src/frontend/streams.c new file mode 100644 index 000000000..bb51c2aff --- /dev/null +++ b/src/frontend/streams.c @@ -0,0 +1,211 @@ +#include +#include +#include +#include + +#include "variable.h" +#include "terminal.h" +#include "quote.h" +#include "streams.h" + + +bool cp_debug = FALSE; +char cp_gt = '>'; +char cp_lt = '<'; +char cp_amp = '&'; + +FILE *cp_in; +FILE *cp_out; +FILE *cp_err; + +/* These are the fps that cp_ioreset resets the cp_* to. They are + * changed by the source routines. */ + +FILE *cp_curin = NULL; +FILE *cp_curout = NULL; +FILE *cp_curerr = NULL; + + +static bool +fileexists(char *name) +{ +#ifdef HAVE_ACCESS + if (access(name, 0) == 0) + return (TRUE); +#endif + return (FALSE); +} + + +/* This routine sets the cp_{in,out,err} pointers and takes the io + * directions out of the command line. */ +wordlist * +cp_redirect(wordlist *wl) +{ + bool gotinput = FALSE, gotoutput = FALSE, goterror = FALSE; + bool app = FALSE, erralso = FALSE; + wordlist *w, *bt, *nw; + char *s; + FILE *tmpfp; + + w = wl->wl_next; /* Don't consider empty commands. */ + while (w) { + if (*w->wl_word == cp_lt) { + bt = w; + if (gotinput) { + fprintf(cp_err, + "Error: ambiguous input redirect.\n"); + goto error; + } + gotinput = TRUE; + w = w->wl_next; + if (w == NULL) { + fprintf(cp_err, + "Error: missing name for input.\n"); + return (NULL); + } + if (*w->wl_word == cp_lt) { + /* Do reasonable stuff here... */ + } else { + tmpfp = fopen(cp_unquote(w->wl_word), "r"); + if (!tmpfp) { + perror(w->wl_word); + goto error; + } else + cp_in = tmpfp; + } +#ifdef CPDEBUG + if (cp_debug) + fprintf(cp_err, "Input file is %s...\n", + w->wl_word); +#endif + bt->wl_prev->wl_next = w->wl_next; + if (w->wl_next) + w->wl_next->wl_prev = bt->wl_prev; + nw = w->wl_next; + w->wl_next = NULL; + w = nw; + wl_free(bt); + } else if (*w->wl_word == cp_gt) { + bt = w; + if (gotoutput) { + fprintf(cp_err, + "Error: ambiguous output redirect.\n"); + goto error; + } + gotoutput = TRUE; + w = w->wl_next; + if (w == NULL) { + fprintf(cp_err, + "Error: missing name for output.\n"); + return (NULL); + } + if (*w->wl_word == cp_gt) { + app = TRUE; + w = w->wl_next; + if (w == NULL) { + fprintf(cp_err, + "Error: missing name for output.\n"); + return (NULL); + } + } + if (*w->wl_word == cp_amp) { + erralso = TRUE; + if (goterror) { + fprintf(cp_err, + "Error: ambiguous error redirect.\n"); + return (NULL); + } + goterror = TRUE; + w = w->wl_next; + if (w == NULL) { + fprintf(cp_err, + "Error: missing name for output.\n"); + return (NULL); + } + } + s = cp_unquote(w->wl_word); + if (cp_noclobber && fileexists(s)) { + fprintf(stderr, "Error: %s: file exists\n", s); + goto error; + } + if (app) + tmpfp = fopen(s, "a"); + else + tmpfp = fopen(s, "w+"); + if (!tmpfp) { + perror(w->wl_word); + goto error; + } else { + cp_out = tmpfp; + out_isatty = FALSE; + } +#ifdef CPDEBUG + if (cp_debug) + fprintf(cp_err, "Output file is %s... %s\n", + w->wl_word, app ? "(append)" : ""); +#endif + bt->wl_prev->wl_next = w->wl_next; + if (w->wl_next) + w->wl_next->wl_prev = bt->wl_prev; + w = w->wl_next; + if (w) + w->wl_prev->wl_next = NULL; + wl_free(bt); + if (erralso) + cp_err = cp_out; + } else + w = w->wl_next; + } + return (wl); + +error: wl_free(wl); + return (NULL); +} + +/* Reset the cp_* FILE pointers to the standard ones. This is tricky, + * since if we are sourcing a command file, and io has been redirected + * from inside the file, we have to reset it back to what it was for + * the source, not for the top level. That way if you type "foo > + * bar" where foo is a script, and it has redirections of its own + * inside of it, none of the output from foo will get sent to + * stdout... */ + +void +cp_ioreset(void) +{ + if (cp_in != cp_curin) { + if (cp_in) + fclose(cp_in); + cp_in = cp_curin; + } + if (cp_out != cp_curout) { + if (cp_out) + fclose(cp_out); + cp_out = cp_curout; + } + if (cp_err != cp_curerr) { + if (cp_err) + fclose(cp_err); + cp_err = cp_curerr; + } + + /*** Minor bug here... */ + out_isatty = TRUE; + return; +} + + +/* Do this only right before an exec, since we lose the old std*'s. */ + +void +fixdescriptors(void) +{ + if (cp_in != stdin) + dup2(fileno(cp_in), fileno(stdin)); + if (cp_out != stdout) + dup2(fileno(cp_out), fileno(stdout)); + if (cp_err != stderr) + dup2(fileno(cp_err), fileno(stderr)); + return; +} diff --git a/src/frontend/terminal.h b/src/frontend/terminal.h index a3890750c..bf045a59f 100644 --- a/src/frontend/terminal.h +++ b/src/frontend/terminal.h @@ -1,6 +1,8 @@ #ifndef _TERMINAL_H #define _TERMINAL_H +extern bool out_isatty; + void out_init(void); void outbufputc(void); void promptreturn(void); diff --git a/src/frontend/variable.c b/src/frontend/variable.c index 2dd995043..473af5677 100644 --- a/src/frontend/variable.c +++ b/src/frontend/variable.c @@ -17,9 +17,10 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include #include "circuits.h" -#include "variable.h" -#include "streams.h" #include "com_history.h" +#include "quote.h" +#include "streams.h" +#include "variable.h" bool cp_noglob = TRUE; diff --git a/src/frontend/variable.h b/src/frontend/variable.h index 3c14dc1b8..98706bda2 100644 --- a/src/frontend/variable.h +++ b/src/frontend/variable.h @@ -38,6 +38,10 @@ struct xxx { extern struct variable *variables; +extern bool cp_noglob; +extern bool cp_nonomatch; +extern bool cp_noclobber; +extern bool cp_ignoreeof; // extern struct variable *variables; wordlist * cp_varwl(struct variable *var); diff --git a/src/parser/Makefile.am b/src/parser/Makefile.am index 2a39cc898..b0df1d149 100644 --- a/src/parser/Makefile.am +++ b/src/parser/Makefile.am @@ -17,13 +17,9 @@ libparser_a_SOURCES = \ lexical.h \ numparse.c \ numparse.h \ - quote.c \ - quote.h \ std.c \ unixcom.c \ unixcom.h \ - var2.c \ - var2.h \ wlist.c \ wlist.h diff --git a/src/parser/input.c b/src/parser/input.c index 71ff3032e..284e9c0e5 100644 --- a/src/parser/input.c +++ b/src/parser/input.c @@ -6,28 +6,55 @@ Author: 1988 Jeffrey M. Hsu /* * Stand-alone input routine. */ +#include +#include + +#include -#include "ngspice.h" #include "fteinput.h" #include "input.h" +#include "cpextern.h" - -extern int inchar(FILE *fp); - -void -Input(REQUEST *request, RESPONSE *response) +/* A special 'getc' so that we can deal with ^D properly. There is no way for + * stdio to know if we have typed a ^D after some other characters, so + * don't use buffering at all + */ +int +inchar(FILE *fp) { - switch (request->option) { - case char_option: - response->reply.ch = inchar(request->fp); - response->option = request->option; - break; - default: - /* just ignore, since we don't want a million error messages */ - response->option = error_option; - break; - } - return; + char c; + int i; + + if (cp_interactive && !cp_nocc) { + do { + i = read((int) fileno(fp), &c, 1); + } while (i == -1 && errno == EINTR); + if (i == 0 || c == '\004') + return (EOF); + else if (i == -1) { + perror("read"); + return (EOF); + } else + return ((int) c); + } else + c = getc(fp); + return ((int) c); +} + + +extern void Input(REQUEST *req, RESPONSE *resp); + +int +input(FILE *fp) +{ + + REQUEST request; + RESPONSE response; + + request.option = char_option; + request.fp = fp; + Input(&request, &response); + return(response.reply.ch); } diff --git a/src/parser/input.h b/src/parser/input.h index 25c9f88f3..c5e9df378 100644 --- a/src/parser/input.h +++ b/src/parser/input.h @@ -7,7 +7,8 @@ #define INPUT_H_INCLUDED -void Input(REQUEST *request, RESPONSE *response); +int inchar(FILE *fp); +int input(FILE *fp); #endif diff --git a/src/parser/lexical.c b/src/parser/lexical.c index 3a0689375..64e0c9e0b 100644 --- a/src/parser/lexical.c +++ b/src/parser/lexical.c @@ -57,8 +57,6 @@ char cp_hash = '#'; static int numeofs = 0; -extern void Input(); - #define ESCAPE '\033' /* Return a list of words, with backslash quoting and '' quoting done. @@ -314,46 +312,3 @@ prompt(void) (void) fflush(cp_out); return; } - - -/* A special 'getc' so that we can deal with ^D properly. There is no way for - * stdio to know if we have typed a ^D after some other characters, so - * don't use buffering at all - */ -int -inchar(FILE *fp) -{ - - char c; - int i; - - if (cp_interactive && !cp_nocc) { - do { - i = read((int) fileno(fp), &c, 1); - } while (i == -1 && errno == EINTR); - if (i == 0 || c == '\004') - return (EOF); - else if (i == -1) { - perror("read"); - return (EOF); - } else - return ((int) c); - } else - c = getc(fp); - return ((int) c); -} - -int -input(FILE *fp) -{ - - REQUEST request; - RESPONSE response; - - request.option = char_option; - request.fp = fp; - Input(&request, &response); - return(response.reply.ch); - -} - diff --git a/src/parser/var2.c b/src/parser/var2.c deleted file mode 100644 index 03df7a93f..000000000 --- a/src/parser/var2.c +++ /dev/null @@ -1,13 +0,0 @@ -/********** -Copyright 1990 Regents of the University of California. All rights reserved. -Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group -**********/ - -/* - * Do variable substitution. - */ - -#include "ngspice.h" -#include "cpdefs.h" -#include "var2.h" - diff --git a/src/parser/var2.h b/src/parser/var2.h deleted file mode 100644 index 5b0613cbd..000000000 --- a/src/parser/var2.h +++ /dev/null @@ -1,21 +0,0 @@ -/************* - * Header file for var2.c - * 1999 E. Rouat - ************/ - -#ifndef VAR2_H_INCLUDED -#define VAR2_H_INCLUDED - - -wordlist * cp_variablesubst(wordlist *wlist); -wordlist * vareval(char *string); -void cp_vprint(void); -void com_set(wordlist *wl); -void com_unset(wordlist *wl); -void com_shift(wordlist *wl); -bool cp_getvar(char *name, int type, char *retval); - - - - -#endif