diff options
Diffstat (limited to 'Src/input.c')
-rw-r--r-- | Src/input.c | 89 |
1 files changed, 69 insertions, 20 deletions
diff --git a/Src/input.c b/Src/input.c index caeaff0e3..d8ac2c0e7 100644 --- a/Src/input.c +++ b/Src/input.c @@ -217,20 +217,49 @@ shinbufrestore(void) static int shingetchar(void) { - int nread; + int nread, rsize = isset(SHINSTDIN) ? 1 : SHINBUFSIZE; if (shinbufptr < shinbufendptr) - return STOUC(*shinbufptr++); + return (unsigned char) *shinbufptr++; shinbufreset(); - do { - errno = 0; - nread = read(SHIN, shinbuffer, SHINBUFSIZE); - } while (nread < 0 && errno == EINTR); - if (nread <= 0) - return -1; - shinbufendptr = shinbuffer + nread; - return STOUC(*shinbufptr++); +#ifdef USE_LSEEK + if (rsize == 1 && lseek(SHIN, 0, SEEK_CUR) != (off_t)-1) + rsize = SHINBUFSIZE; + if (rsize > 1) { + do { + errno = 0; + nread = read(SHIN, shinbuffer, rsize); + } while (nread < 0 && errno == EINTR); + if (nread <= 0) + return -1; + if (isset(SHINSTDIN) && + (shinbufendptr = memchr(shinbuffer, '\n', nread))) { + shinbufendptr++; + rsize = (shinbufendptr - shinbuffer); + if (nread > rsize && + lseek(SHIN, -(nread - rsize), SEEK_CUR) < 0) + zerr("lseek(%d, %d): %e", SHIN, -(nread - rsize), errno); + } else + shinbufendptr = shinbuffer + nread; + return (unsigned char) *shinbufptr++; + } +#endif + for (;;) { + errno = 0; + nread = read(SHIN, shinbufendptr, 1); + if (nread > 0) { + /* Use line buffering (POSIX requirement) */ + if (*shinbufendptr++ == '\n') + break; + if (shinbufendptr == shinbuffer + SHINBUFSIZE) + break; + } else if (nread == 0 || errno != EINTR) + break; + } + if (shinbufendptr == shinbuffer) + return -1; + return (unsigned char) *shinbufptr++; } /* Read a line from SHIN. Convert tokens and * @@ -299,7 +328,7 @@ ingetc(void) if (inbufleft) { inbufleft--; inbufct--; - if (itok(lastc = STOUC(*inbufptr++))) + if (itok(lastc = (unsigned char) *inbufptr++)) continue; if (((inbufflags & INP_LINENO) || !strin) && lastc == '\n') lineno++; @@ -373,7 +402,7 @@ inputline(void) char *pptbuf; int pptlen; pptbuf = unmetafy(promptexpand(ingetcpmptl ? *ingetcpmptl : NULL, - 0, NULL, NULL, NULL), &pptlen); + 0, NULL, NULL), &pptlen); write_loop(2, pptbuf, pptlen); free(pptbuf); } @@ -581,11 +610,11 @@ inungetc(int c) } } -/* stuff a whole file into the input queue and print it */ +/* stuff a whole file into memory and return it */ /**/ -int -stuff(char *fn) +off_t +zstuff(char **out, const char *fn) { FILE *in; char *buf; @@ -593,20 +622,39 @@ stuff(char *fn) if (!(in = fopen(unmeta(fn), "r"))) { zerr("can't open %s", fn); - return 1; + return -1; } + queue_signals(); fseek(in, 0, SEEK_END); len = ftell(in); fseek(in, 0, SEEK_SET); buf = (char *)zalloc(len + 1); - if (!(fread(buf, len, 1, in))) { + if (len && !(fread(buf, len, 1, in))) { zerr("read error on %s", fn); fclose(in); zfree(buf, len + 1); - return 1; + unqueue_signals(); + return -1; } fclose(in); buf[len] = '\0'; + *out = buf; + unqueue_signals(); + return len; +} + +/* stuff a whole file into the input queue and print it */ + +/**/ +int +stuff(char *fn) +{ + char *buf; + off_t len = zstuff(&buf, fn); + + if (len < 0) + return 1; + fwrite(buf, len, 1, stderr); fflush(stderr); inputsetline(metafy(buf, len, META_REALLOC), INP_FREE); @@ -787,6 +835,7 @@ char *input_hasalias(void) { int flags = inbufflags; struct instacks *instackptr = instacktop; + char *alias_nam = NULL; for (;;) { @@ -795,9 +844,9 @@ char *input_hasalias(void) DPUTS(instackptr == instack, "BUG: continuation at bottom of instack"); instackptr--; if (instackptr->alias) - return instackptr->alias->node.nam; + alias_nam = instackptr->alias->node.nam; flags = instackptr->flags; } - return NULL; + return alias_nam; } |