rc

[fork] interactive rc shell
Log | Files | Refs | README | LICENSE

commit 70a21186f92ee19a995080744ce9a320345c05cb
parent 02c12b81c0b9b19a7b1372c8e6842337a9f00959
Author: tjg <tjg>
Date:   Wed, 24 Sep 2003 10:15:11 +0000

  Tidiness: minor improvements to input.c.

Diffstat:
MChangeLog | 4++++
Mbuiltins.c | 10+++-------
Mexcept.c | 1+
Mfn.c | 1+
Mfootobar.c | 2++
Mheredoc.c | 4+++-
Minput.c | 87++++++++++++++++++++++++++++++++++---------------------------------------------
Mlex.c | 4+++-
Mmain.c | 32+++++++++++++++++++++++++++++---
Mrc.h | 20+-------------------
10 files changed, 84 insertions(+), 81 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -830,3 +830,7 @@ Changes since rc-1.5b2 2003-07-22 Release: rc-1.7.1. + +2003-09-24 + + Tidiness: minor improvements to input.c. diff --git a/builtins.c b/builtins.c @@ -15,6 +15,7 @@ #include <errno.h> #include "addon.h" +#include "input.h" #include "jbwrap.h" #include "rlimit.h" #include "sigmsgs.h" @@ -389,15 +390,10 @@ extern void b_dot(char **av) { return; fd = rc_open(*av, rFrom); if (fd < 0) { - if (rcrc) /* on rc -l, don't flag nonexistence of .rcrc */ - rcrc = FALSE; - else { - uerror(*av); - set(FALSE); - } + uerror(*av); + set(FALSE); return; } - rcrc = FALSE; starassign(*av, av+1, TRUE); pushfd(fd); interactive = i; diff --git a/except.c b/except.c @@ -3,6 +3,7 @@ #include <setjmp.h> #include <signal.h> +#include "input.h" #include "jbwrap.h" /* diff --git a/fn.c b/fn.c @@ -8,6 +8,7 @@ #include <signal.h> #include <errno.h> +#include "input.h" #include "sigmsgs.h" static void fn_handler(int), dud_handler(int); diff --git a/footobar.c b/footobar.c @@ -5,6 +5,8 @@ #include "rc.h" +#include "input.h" + /* protect an exported name from brain-dead shells */ #if PROTECT_ENV diff --git a/heredoc.c b/heredoc.c @@ -2,6 +2,8 @@ #include "rc.h" +#include "input.h" + struct Hq { Node *doc; char *name; @@ -142,7 +144,7 @@ extern int qdoc(Node *name, Node *n) { Hq *new, **prev; if (name->type != nWord) { yyerror("eof-marker not a single literal word"); - flushu(); + skiptonl(); return FALSE; } for (prev = &hq; (new = *prev) != NULL; prev = &new->n) diff --git a/input.c b/input.c @@ -4,6 +4,7 @@ #include <errno.h> +#include "input.h" #include "jbwrap.h" /* @@ -22,14 +23,6 @@ typedef struct Input { #define BUFSIZE ((size_t) 256) static char *prompt2; -bool rcrc; - -static int dead(void); -static int fdgchar(void); -static int stringgchar(void); -static void history(void); -static void ugdead(int); -static void pushcommon(void); static char *inbuf; static size_t istacksize, chars_out, chars_in; @@ -84,6 +77,39 @@ static int stringgchar() { return lastchar = (inbuf[chars_out] == '\0' ? EOF : inbuf[chars_out++]); } + +/* write last command out to a file if interactive && $history is set */ + +static void history() { + List *hist; + size_t a; + + if (!interactive || (hist = varlookup("history")) == NULL) + return; + + for (a = 0; a < chars_in; a++) { + char c = inbuf[a+2]; + + /* skip empty lines and comments */ + if (c == '#' || c == '\n') + break; + + /* line matches [ \t]*[^#\n] so it's ok to write out */ + if (c != ' ' && c != '\t') { + char *name = hist->w; + int fd = rc_open(name, rAppend); + if (fd < 0) { + uerror(name); + varrm(name, TRUE); + } else { + writeall(fd, inbuf + 2, chars_in); + close(fd); + } + break; + } + } +} + /* read a character from a file-descriptor. If GNU readline is defined, add a newline and doctor the buffer to look like a regular fdgchar @@ -231,7 +257,7 @@ extern void popinput() { /* flush input characters upto newline. Used by scanerror() */ -extern void flushu() { +extern void skiptonl() { int c; if (lastchar == '\n' || lastchar == EOF) return; @@ -262,14 +288,7 @@ extern Node *doit(bool clobberexecit) { block.b = newblock(); except(eArena, block, &e2); sigchk(); - if (dashell) { - char *fname[3]; - fname[1] = concat(varlookup("home"), word("/.rcrc", NULL))->w; - fname[2] = NULL; - rcrc = TRUE; - dashell = FALSE; - b_dot(fname); - } + if (interactive) { List *s; if (!dashen && fnlookup("prompt") != NULL) { @@ -312,7 +331,7 @@ extern Node *doit(bool clobberexecit) { /* parse a function imported from the environment */ extern Node *parseline(char *extdef) { - int i = interactive; + bool i = interactive; char *in[2]; Node *fun; in[0] = extdef; @@ -324,38 +343,6 @@ extern Node *parseline(char *extdef) { return fun; } -/* write last command out to a file if interactive && $history is set */ - -static void history() { - List *hist; - size_t a; - - if (!interactive || (hist = varlookup("history")) == NULL) - return; - - for (a = 0; a < chars_in; a++) { - char c = inbuf[a+2]; - - /* skip empty lines and comments */ - if (c == '#' || c == '\n') - break; - - /* line matches [ \t]*[^#\n] so it's ok to write out */ - if (c != ' ' && c != '\t') { - char *name = hist->w; - int fd = rc_open(name, rAppend); - if (fd < 0) { - uerror(name); - varrm(name, TRUE); - } else { - writeall(fd, inbuf + 2, chars_in); - close(fd); - } - break; - } - } -} - /* close file descriptors after a fork() */ extern void closefds() { diff --git a/lex.c b/lex.c @@ -1,6 +1,8 @@ /* lex.c: rc's lexical analyzer */ #include "rc.h" + +#include "input.h" #include "parse.h" /* @@ -333,7 +335,7 @@ extern void yyerror(const char *s) { } extern void scanerror(char *s) { - flushu(); /* flush upto newline */ + skiptonl(); /* flush upto newline */ yyerror(s); errset = prerror = TRUE; } diff --git a/main.c b/main.c @@ -2,11 +2,15 @@ #include "rc.h" -bool dashdee, dashee, dashvee, dashex, dashell, dashEYE, dasheye, - dashen, dashpee, dashess, interactive; +#include <errno.h> + +#include "input.h" + +bool dashdee, dashee, dashvee, dashex, dasheye, + dashen, dashpee, interactive; pid_t rc_pid; -static bool dashoh; +static bool dashEYE, dashell, dashoh, dashess; static void assigndefault(char *,...); static void checkfd(int, enum redirtype); @@ -86,6 +90,28 @@ quitopts: null[0] = NULL; starassign(dollarzero, null, FALSE); /* assign $0 to $* */ inithandler(); + + if (dashell) { + char *rcrc; + int fd; + + rcrc = concat(varlookup("home"), word("/.rcrc", NULL))->w; + fd = rc_open(rcrc, rFrom); + if (fd == -1) { + if (errno != ENOENT) + uerror(rcrc); + } else { + bool push_interactive; + + pushfd(fd); + push_interactive = interactive; + interactive = FALSE; + doit(TRUE); + interactive = push_interactive; + close(fd); + } + } + if (dashsee[0] != NULL || dashess) { /* input from -c or -s? */ if (*argv != NULL) starassign(dollarzero, argv, FALSE); diff --git a/rc.h b/rc.h @@ -172,8 +172,7 @@ enum { /* main.c */ extern Rq *redirq; -extern bool dashdee, dashee, dashvee, dashex, dashell, - dasheye, dashen, dashpee, interactive; +extern bool dashdee, dashee, dashvee, dashex, dasheye, dashen, dashpee, interactive; extern pid_t rc_pid; extern int lineno; @@ -265,23 +264,6 @@ extern int heredoc(int); extern int qdoc(Node *, Node *); extern Hq *hq; - -/* input.c */ -extern void initinput(void); -extern Node *parseline(char *); -extern int gchar(void); -extern void ugchar(int); -extern Node *doit(bool); -extern void flushu(void); -extern void print_prompt2(void); -extern void pushfd(int); -extern void pushstring(char **, bool); -extern void popinput(void); -extern void closefds(void); -extern int lastchar; -extern bool rcrc; - - /* lex.c */ extern bool quotep(char *, bool); extern int yylex(void);