1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/* input.c: i/o routines for files and pseudo-files (strings) */
#include "rc.h"
#include <errno.h>
#include "jbwrap.h"
/*
NB: character unget is supported for up to two characters, but NOT
in the case of EOF. Since EOF does not fit in a char, it is easiest
to support only one unget of EOF.
*/
typedef struct Input {
inputtype t;
char *ibuf;
int fd, index, read, lineno, last;
bool saved, eofread;
} 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;
static bool eofread = FALSE, save_lineno = TRUE;
static Input *istack, *itop;
static int (*realgchar)(void);
static void (*realugchar)(int);
int lastchar;
#if EDITLINE || READLINE
static char *rlinebuf, *prompt;
#endif
extern int gchar() {
int c;
if (eofread) {
eofread = FALSE;
return lastchar = EOF;
}
while ((c = (*realgchar)()) == '\0')
pr_error("warning: null character ignored", 0);
return c;
}
extern void ugchar(int c) {
(*realugchar)(c);
}
static int dead() {
return lastchar = EOF;
}
static void ugdead(int ignore) {
return;
}
static void ugalive(int c) {
if (c == EOF)
eofread = TRUE;
else
inbuf[--chars_out] = c;
}
/* get the next character from a string. */
static int stringgchar() {
return lastchar = (inbuf[chars_out] == '\0' ? EOF : inbuf[chars_out++]);
}
/*
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
buffer.
*/
static int fdgchar() {
if (chars_out >= chars_in + 2) { /* has the buffer been exhausted? if so, replenish it */
while (1) {
#if EDITLINE || READLINE
if (interactive && istack->t == iFd && isatty(istack->fd)) {
/* The readline library doesn't handle
* read() returning EAGAIN or EIO. */
makeblocking(istack->fd);
makesamepgrp(istack->fd);
rlinebuf = rc_readline(prompt);
if (rlinebuf == NULL) {
chars_in = 0;
} else {
if (*rlinebuf != '\0')
add_history(rlinebuf);
chars_in = strlen(rlinebuf) + 1;
efree(inbuf);
inbuf = ealloc(chars_in + 3);
strcpy(inbuf+2, rlinebuf);
strcat(inbuf+2, "\n");
efree(rlinebuf);
}
} else
#endif
{
ssize_t r;
do {
r = rc_read(istack->fd, inbuf + 2, BUFSIZE);
sigchk();
switch (errno) {
case EAGAIN:
if (!makeblocking(istack->fd))
panic("not O_NONBLOCK");
errno = EINTR;
break;
case EIO:
if (makesamepgrp(istack->fd))
errno = EINTR;
else
errno = EIO;
break;
}
} while (r < 0 && errno == EINTR);
if (r < 0) {
uerror("read");
rc_raise(eError);
}
chars_in = (size_t) r;
}
break;
}
if (chars_in == 0)
return lastchar = EOF;
chars_out = 2;
if (dashvee)
writeall(2, inbuf + 2, chars_in);
history();
}
return lastchar = inbuf[chars_out++];
}
/* set up the input stack, and put a "dead" input at the bottom, so that yyparse will always read eof */
extern void initinput() {
istack = itop = ealloc(istacksize = 256 * sizeof (Input));
istack->t = iFd;
istack->fd = -1;
realugchar = ugalive;
}
/* push an input source onto the stack. set up a new input buffer, and set gchar() */
static void pushcommon() {
size_t idiff;
istack->index = chars_out;
istack->read = chars_in;
istack->ibuf = inbuf;
istack->lineno = lineno;
istack->saved = save_lineno;
istack->last = lastchar;
istack->eofread = eofread;
istack++;
idiff = istack - itop;
if (idiff >= istacksize / sizeof (Input)) {
itop = erealloc(itop, istacksize *= 2);
istack = itop + idiff;
}
realugchar = ugalive;
chars_out = 2;
chars_in = 0;
}
extern void pushfd(int fd) {
pushcommon();
istack->t = iFd;
save_lineno = TRUE;
istack->fd = fd;
realgchar = fdgchar;
inbuf = ealloc(BUFSIZE + 2);
lineno = 1;
}
extern void pushstring(char **a, bool save) {
pushcommon();
istack->t = iString;
save_lineno = save;
inbuf = mprint("..%A", a);
realgchar = stringgchar;
if (save_lineno)
lineno = 1;
else
--lineno;
}
/* remove an input source from the stack. restore the right kind of getchar (string,fd) etc. */
extern void popinput() {
if (istack->t == iFd)
close(istack->fd);
efree(inbuf);
--istack;
realgchar = (istack->t == iString ? stringgchar : fdgchar);
if (istack->t == iFd && istack->fd == -1) { /* top of input stack */
realgchar = dead;
realugchar = ugdead;
}
lastchar = istack->last;
eofread = istack->eofread;
inbuf = istack->ibuf;
chars_out = istack->index;
chars_in = istack->read;
if (save_lineno)
lineno = istack->lineno;
else
lineno++;
save_lineno = istack->saved;
}
/* flush input characters upto newline. Used by scanerror() */
extern void flushu() {
int c;
if (lastchar == '\n' || lastchar == EOF)
return;
while ((c = gchar()) != '\n' && c != EOF)
; /* skip to newline */
if (c == EOF)
ugchar(c);
}
/* the wrapper loop in rc: prompt for commands until EOF, calling yyparse and walk() */
extern Node *doit(bool clobberexecit) {
bool eof;
bool execit;
Jbwrap j;
Estack e1;
Edata jerror;
if (dashen)
clobberexecit = FALSE;
execit = clobberexecit;
sigsetjmp(j.j, 1);
jerror.jb = &j;
except(eError, jerror, &e1);
for (eof = FALSE; !eof;) {
Edata block;
Estack e2;
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) {
static bool died = FALSE;
static char *arglist[] = { "prompt", NULL };
if (!died) {
died = TRUE;
funcall(arglist);
}
died = FALSE;
}
if ((s = varlookup("prompt")) != NULL) {
#if EDITLINE || READLINE
if (istack->t == iFd && isatty(istack->fd))
prompt = s->w;
else
#endif
fprint(2, "%s", s->w);
prompt2 = (s->n == NULL ? "" : s->n->w);
}
}
inityy();
if (yyparse() == 1 && execit)
rc_raise(eError);
eof = (lastchar == EOF); /* "lastchar" can be clobbered during a walk() */
if (parsetree != NULL) {
if (execit)
walk(parsetree, TRUE);
else if (dashex && dashen)
fprint(2, "%T\n", parsetree);
}
unexcept(); /* eArena */
}
popinput();
unexcept(); /* eError */
return parsetree;
}
/* parse a function imported from the environment */
extern Node *parseline(char *extdef) {
int i = interactive;
char *in[2];
Node *fun;
in[0] = extdef;
in[1] = NULL;
interactive = FALSE;
pushstring(in, TRUE);
fun = doit(FALSE);
interactive = i;
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() {
Input *i;
for (i = istack; i != itop; --i) /* close open scripts */
if (i->t == iFd && i->fd > 2) {
close(i->fd);
i->fd = -1;
}
}
extern void print_prompt2() {
lineno++;
if (interactive) {
#if EDITLINE || READLINE
if (istack->t == iFd && isatty(istack->fd))
prompt = prompt2;
else
#endif
fprint(2, "%s", prompt2);
}
}
|