stop (ab)using the OS symbolic constant EOF, always use -1; plus misc fixes

This commit is contained in:
tg
2015-02-06 10:09:07 +00:00
parent f235766a3d
commit b94b2e363a
4 changed files with 67 additions and 68 deletions

8
eval.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.162 2015/02/06 09:33:41 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.163 2015/02/06 10:09:05 tg Exp $");
/* /*
* string expansion * string expansion
@ -871,7 +871,7 @@ expand(
/* $(<...) failed */ /* $(<...) failed */
subst_exstat = 1; subst_exstat = 1;
/* fake EOF */ /* fake EOF */
c = EOF; c = -1;
} else if (newlines) { } else if (newlines) {
/* spit out saved NLs */ /* spit out saved NLs */
c = '\n'; c = '\n';
@ -881,13 +881,13 @@ expand(
if (c == '\n') if (c == '\n')
/* save newlines */ /* save newlines */
newlines++; newlines++;
if (newlines && c != EOF) { if (newlines && c != -1) {
shf_ungetc(c, x.u.shf); shf_ungetc(c, x.u.shf);
c = '\n'; c = '\n';
--newlines; --newlines;
} }
} }
if (c == EOF) { if (c == -1) {
newlines = 0; newlines = 0;
if (x.u.shf) if (x.u.shf)
shf_close(x.u.shf); shf_close(x.u.shf);

29
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.141 2015/02/06 09:42:46 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.142 2015/02/06 10:09:06 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL #ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL "/bin/sh" #define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -507,16 +507,17 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
int type_flags; int type_flags;
bool resetspec; bool resetspec;
int fcflags = FC_BI|FC_FUNC|FC_PATH; int fcflags = FC_BI|FC_FUNC|FC_PATH;
bool bourne_function_call = false;
struct block *l_expand, *l_assign; struct block *l_expand, *l_assign;
int optc;
/* /* snag the last argument for $_ */
* snag the last argument for $_ XXX not the same as AT&T ksh,
* which only seems to set $_ after a newline (but not in
* functions/dot scripts, but in interactive and script) -
* perhaps save last arg here and set it in shell()?.
*/
if (Flag(FTALKING) && *(lastp = ap)) { if (Flag(FTALKING) && *(lastp = ap)) {
/*
* XXX not the same as AT&T ksh, which only seems to set $_
* after a newline (but not in functions/dot scripts, but in
* interactive and script) - perhaps save last arg here and
* set it in shell()?.
*/
while (*++lastp) while (*++lastp)
; ;
/* setstr() can't fail here */ /* setstr() can't fail here */
@ -556,7 +557,6 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
ap++; ap++;
flags |= XEXEC; flags |= XEXEC;
} else if (tp->val.f == c_command) { } else if (tp->val.f == c_command) {
int optc;
bool saw_p = false; bool saw_p = false;
/* /*
@ -566,7 +566,7 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
ksh_getopt_reset(&builtin_opt, 0); ksh_getopt_reset(&builtin_opt, 0);
while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p') while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p')
saw_p = true; saw_p = true;
if (optc != EOF) if (optc != -1)
/* command -vV or something */ /* command -vV or something */
break; break;
/* don't look for functions */ /* don't look for functions */
@ -627,10 +627,9 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
newblock(); newblock();
/* ksh functions don't keep assignments, POSIX functions do. */ /* ksh functions don't keep assignments, POSIX functions do. */
if (!resetspec && tp && tp->type == CFUNC && if (!resetspec && tp && tp->type == CFUNC &&
!(tp->flag & FKSH)) { !(tp->flag & FKSH))
bourne_function_call = true;
type_flags = EXPORT; type_flags = EXPORT;
} else else
type_flags = LOCAL|LOCAL_COPY|EXPORT; type_flags = LOCAL|LOCAL_COPY|EXPORT;
} }
l_assign = e->loc; l_assign = e->loc;
@ -1491,7 +1490,7 @@ herein(struct ioword *iop, char **resbuf)
struct temp *h; struct temp *h;
int i; int i;
/* ksh -c 'cat << EOF' can cause this... */ /* ksh -c 'cat <<EOF' can cause this... */
if (iop->heredoc == NULL) { if (iop->heredoc == NULL) {
warningf(true, "%s missing", "here document"); warningf(true, "%s missing", "here document");
/* special to iosetup(): don't print error */ /* special to iosetup(): don't print error */
@ -1526,7 +1525,7 @@ herein(struct ioword *iop, char **resbuf)
return (-2); return (-2);
} }
if (shf_close(shf) == EOF) { if (shf_close(shf) == -1) {
i = errno; i = errno;
close(fd); close(fd);
warningf(true, "can't %s temporary file %s: %s", warningf(true, "can't %s temporary file %s: %s",

View File

@ -3,7 +3,7 @@
/*- /*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
* 2011, 2012, 2014 * 2011, 2012, 2014, 2015
* Thorsten Glaser <tg@mirbsd.org> * Thorsten Glaser <tg@mirbsd.org>
* *
* Provided that these terms and disclaimer and all copyright notices * Provided that these terms and disclaimer and all copyright notices
@ -27,7 +27,7 @@
#include <sys/file.h> #include <sys/file.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.138 2015/01/22 16:54:29 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.139 2015/02/06 10:09:06 tg Exp $");
Trap sigtraps[NSIG + 1]; Trap sigtraps[NSIG + 1];
static struct sigaction Sigact_ign; static struct sigaction Sigact_ign;
@ -303,7 +303,7 @@ c_fc(const char **wp)
for (hp = rflag ? hlast : hfirst; for (hp = rflag ? hlast : hfirst;
hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1) hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
shf_fprintf(shf, "%s\n", *hp); shf_fprintf(shf, "%s\n", *hp);
if (shf_close(shf) == EOF) { if (shf_close(shf) == -1) {
bi_errorf("can't %s temporary file %s: %s", bi_errorf("can't %s temporary file %s: %s",
"write", tf->tffn, cstrerror(errno)); "write", tf->tffn, cstrerror(errno));
return (1); return (1);

92
shf.c
View File

@ -2,7 +2,7 @@
/*- /*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
* 2012, 2013 * 2012, 2013, 2015
* Thorsten Glaser <tg@mirbsd.org> * Thorsten Glaser <tg@mirbsd.org>
* *
* Provided that these terms and disclaimer and all copyright notices * Provided that these terms and disclaimer and all copyright notices
@ -25,7 +25,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.63 2014/11/25 21:01:14 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.64 2015/02/06 10:09:07 tg Exp $");
/* flags to shf_emptybuf() */ /* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */ #define EB_READSW 0x01 /* about to switch to reading */
@ -232,7 +232,7 @@ shf_close(struct shf *shf)
if (shf->fd >= 0) { if (shf->fd >= 0) {
ret = shf_flush(shf); ret = shf_flush(shf);
if (close(shf->fd) < 0) if (close(shf->fd) < 0)
ret = EOF; ret = -1;
} }
if (shf->flags & SHF_ALLOCS) if (shf->flags & SHF_ALLOCS)
afree(shf, shf->areap); afree(shf, shf->areap);
@ -251,7 +251,7 @@ shf_fdclose(struct shf *shf)
if (shf->fd >= 0) { if (shf->fd >= 0) {
ret = shf_flush(shf); ret = shf_flush(shf);
if (close(shf->fd) < 0) if (close(shf->fd) < 0)
ret = EOF; ret = -1;
shf->rnleft = 0; shf->rnleft = 0;
shf->rp = shf->buf; shf->rp = shf->buf;
shf->wnleft = 0; shf->wnleft = 0;
@ -283,20 +283,20 @@ shf_sclose(struct shf *shf)
/* /*
* Un-read what has been read but not examined, or write what has been * Un-read what has been read but not examined, or write what has been
* buffered. Returns 0 for success, EOF for (write) error. * buffered. Returns 0 for success, -1 for (write) error.
*/ */
int int
shf_flush(struct shf *shf) shf_flush(struct shf *shf)
{ {
if (shf->flags & SHF_STRING) if (shf->flags & SHF_STRING)
return ((shf->flags & SHF_WR) ? EOF : 0); return ((shf->flags & SHF_WR) ? -1 : 0);
if (shf->fd < 0) if (shf->fd < 0)
internal_errorf("%s: %s", "shf_flush", "no fd"); internal_errorf("%s: %s", "shf_flush", "no fd");
if (shf->flags & SHF_ERROR) { if (shf->flags & SHF_ERROR) {
errno = shf->errnosv; errno = shf->errnosv;
return (EOF); return (-1);
} }
if (shf->flags & SHF_READING) { if (shf->flags & SHF_READING) {
@ -315,7 +315,7 @@ shf_flush(struct shf *shf)
/* /*
* Write out any buffered data. If currently reading, flushes the read * Write out any buffered data. If currently reading, flushes the read
* buffer. Returns 0 for success, EOF for (write) error. * buffer. Returns 0 for success, -1 for (write) error.
*/ */
static int static int
shf_emptybuf(struct shf *shf, int flags) shf_emptybuf(struct shf *shf, int flags)
@ -327,7 +327,7 @@ shf_emptybuf(struct shf *shf, int flags)
if (shf->flags & SHF_ERROR) { if (shf->flags & SHF_ERROR) {
errno = shf->errnosv; errno = shf->errnosv;
return (EOF); return (-1);
} }
if (shf->flags & SHF_READING) { if (shf->flags & SHF_READING) {
@ -347,7 +347,7 @@ shf_emptybuf(struct shf *shf, int flags)
*/ */
if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) || if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
!(shf->flags & SHF_ALLOCB)) !(shf->flags & SHF_ALLOCB))
return (EOF); return (-1);
/* allocate more space for buffer */ /* allocate more space for buffer */
nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap); nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
shf->rp = nbuf + (shf->rp - shf->buf); shf->rp = nbuf + (shf->rp - shf->buf);
@ -379,7 +379,7 @@ shf_emptybuf(struct shf *shf, int flags)
ntowrite); ntowrite);
shf->wp = shf->buf + ntowrite; shf->wp = shf->buf + ntowrite;
} }
return (EOF); return (-1);
} }
buf += n; buf += n;
ntowrite -= n; ntowrite -= n;
@ -399,7 +399,7 @@ shf_emptybuf(struct shf *shf, int flags)
return (ret); return (ret);
} }
/* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */ /* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
static int static int
shf_fillbuf(struct shf *shf) shf_fillbuf(struct shf *shf)
{ {
@ -414,11 +414,11 @@ shf_fillbuf(struct shf *shf)
if (shf->flags & (SHF_EOF | SHF_ERROR)) { if (shf->flags & (SHF_EOF | SHF_ERROR)) {
if (shf->flags & SHF_ERROR) if (shf->flags & SHF_ERROR)
errno = shf->errnosv; errno = shf->errnosv;
return (EOF); return (-1);
} }
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF) if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (EOF); return (-1);
shf->flags |= SHF_READING; shf->flags |= SHF_READING;
@ -434,7 +434,7 @@ shf_fillbuf(struct shf *shf)
shf->errnosv = errno; shf->errnosv = errno;
shf->rnleft = 0; shf->rnleft = 0;
shf->rp = shf->buf; shf->rp = shf->buf;
return (EOF); return (-1);
} }
if ((shf->rnleft = n) == 0) if ((shf->rnleft = n) == 0)
shf->flags |= SHF_EOF; shf->flags |= SHF_EOF;
@ -443,7 +443,7 @@ shf_fillbuf(struct shf *shf)
/* /*
* Read a buffer from shf. Returns the number of bytes read into buf, if * Read a buffer from shf. Returns the number of bytes read into buf, if
* no bytes were read, returns 0 if end of file was seen, EOF if a read * no bytes were read, returns 0 if end of file was seen, -1 if a read
* error occurred. * error occurred.
*/ */
ssize_t ssize_t
@ -459,7 +459,7 @@ shf_read(char *buf, ssize_t bsize, struct shf *shf)
while (bsize > 0) { while (bsize > 0) {
if (shf->rnleft == 0 && if (shf->rnleft == 0 &&
(shf_fillbuf(shf) == EOF || shf->rnleft == 0)) (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
break; break;
ncopy = shf->rnleft; ncopy = shf->rnleft;
if (ncopy > bsize) if (ncopy > bsize)
@ -471,12 +471,12 @@ shf_read(char *buf, ssize_t bsize, struct shf *shf)
shf->rnleft -= ncopy; shf->rnleft -= ncopy;
} }
/* Note: fread(3S) returns 0 for errors - this doesn't */ /* Note: fread(3S) returns 0 for errors - this doesn't */
return (orig_bsize == bsize ? (shf_error(shf) ? EOF : 0) : return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
orig_bsize - bsize); orig_bsize - bsize);
} }
/* /*
* Read up to a newline or EOF. The newline is put in buf; buf is always * Read up to a newline or -1. The newline is put in buf; buf is always
* NUL terminated. Returns NULL on read error or if nothing was read * NUL terminated. Returns NULL on read error or if nothing was read
* before end of file, returns a pointer to the NUL byte in buf * before end of file, returns a pointer to the NUL byte in buf
* otherwise. * otherwise.
@ -498,7 +498,7 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
--bsize; --bsize;
do { do {
if (shf->rnleft == 0) { if (shf->rnleft == 0) {
if (shf_fillbuf(shf) == EOF) if (shf_fillbuf(shf) == -1)
return (NULL); return (NULL);
if (shf->rnleft == 0) { if (shf->rnleft == 0) {
*buf = '\0'; *buf = '\0';
@ -520,22 +520,22 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
return (buf); return (buf);
} }
/* Returns the char read. Returns EOF for error and end of file. */ /* Returns the char read. Returns -1 for error and end of file. */
int int
shf_getchar(struct shf *shf) shf_getchar(struct shf *shf)
{ {
if (!(shf->flags & SHF_RD)) if (!(shf->flags & SHF_RD))
internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags); internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0)) if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
return (EOF); return (-1);
--shf->rnleft; --shf->rnleft;
return (*shf->rp++); return (*shf->rp++);
} }
/* /*
* Put a character back in the input stream. Returns the character if * Put a character back in the input stream. Returns the character if
* successful, EOF if there is no room. * successful, -1 if there is no room.
*/ */
int int
shf_ungetc(int c, struct shf *shf) shf_ungetc(int c, struct shf *shf)
@ -543,12 +543,12 @@ shf_ungetc(int c, struct shf *shf)
if (!(shf->flags & SHF_RD)) if (!(shf->flags & SHF_RD))
internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags); internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
if ((shf->flags & SHF_ERROR) || c == EOF || if ((shf->flags & SHF_ERROR) || c == -1 ||
(shf->rp == shf->buf && shf->rnleft)) (shf->rp == shf->buf && shf->rnleft))
return (EOF); return (-1);
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF) if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (EOF); return (-1);
if (shf->rp == shf->buf) if (shf->rp == shf->buf)
shf->rp = shf->buf + shf->rbsize; shf->rp = shf->buf + shf->rbsize;
@ -558,7 +558,7 @@ shf_ungetc(int c, struct shf *shf)
* we don't want to modify a string. * we don't want to modify a string.
*/ */
if ((int)(shf->rp[-1]) != c) if ((int)(shf->rp[-1]) != c)
return (EOF); return (-1);
shf->flags &= ~SHF_EOF; shf->flags &= ~SHF_EOF;
shf->rp--; shf->rp--;
shf->rnleft++; shf->rnleft++;
@ -571,7 +571,7 @@ shf_ungetc(int c, struct shf *shf)
} }
/* /*
* Write a character. Returns the character if successful, EOF if the * Write a character. Returns the character if successful, -1 if the
* char could not be written. * char could not be written.
*/ */
int int
@ -580,8 +580,8 @@ shf_putchar(int c, struct shf *shf)
if (!(shf->flags & SHF_WR)) if (!(shf->flags & SHF_WR))
internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags); internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
if (c == EOF) if (c == -1)
return (EOF); return (-1);
if (shf->flags & SHF_UNBUF) { if (shf->flags & SHF_UNBUF) {
unsigned char cc = (unsigned char)c; unsigned char cc = (unsigned char)c;
@ -591,7 +591,7 @@ shf_putchar(int c, struct shf *shf)
internal_errorf("%s: %s", "shf_putchar", "no fd"); internal_errorf("%s: %s", "shf_putchar", "no fd");
if (shf->flags & SHF_ERROR) { if (shf->flags & SHF_ERROR) {
errno = shf->errnosv; errno = shf->errnosv;
return (EOF); return (-1);
} }
while ((n = write(shf->fd, &cc, 1)) != 1) while ((n = write(shf->fd, &cc, 1)) != 1)
if (n < 0) { if (n < 0) {
@ -600,12 +600,12 @@ shf_putchar(int c, struct shf *shf)
continue; continue;
shf->flags |= SHF_ERROR; shf->flags |= SHF_ERROR;
shf->errnosv = errno; shf->errnosv = errno;
return (EOF); return (-1);
} }
} else { } else {
/* Flush deals with strings and sticky errors */ /* Flush deals with strings and sticky errors */
if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF) if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
return (EOF); return (-1);
shf->wnleft--; shf->wnleft--;
*shf->wp++ = c; *shf->wp++ = c;
} }
@ -614,19 +614,19 @@ shf_putchar(int c, struct shf *shf)
} }
/* /*
* Write a string. Returns the length of the string if successful, EOF * Write a string. Returns the length of the string if successful, -1
* if the string could not be written. * if the string could not be written.
*/ */
ssize_t ssize_t
shf_puts(const char *s, struct shf *shf) shf_puts(const char *s, struct shf *shf)
{ {
if (!s) if (!s)
return (EOF); return (-1);
return (shf_write(s, strlen(s), shf)); return (shf_write(s, strlen(s), shf));
} }
/* Write a buffer. Returns nbytes if successful, EOF if there is an error. */ /* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
ssize_t ssize_t
shf_write(const char *buf, ssize_t nbytes, struct shf *shf) shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
{ {
@ -653,13 +653,13 @@ shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
if (shf->flags & SHF_STRING) { if (shf->flags & SHF_STRING) {
/* resize buffer until there's enough space left */ /* resize buffer until there's enough space left */
while (nbytes > shf->wnleft) while (nbytes > shf->wnleft)
if (shf_emptybuf(shf, EB_GROW) == EOF) if (shf_emptybuf(shf, EB_GROW) == -1)
return (EOF); return (-1);
/* then write everything into the buffer */ /* then write everything into the buffer */
} else { } else {
/* flush deals with sticky errors */ /* flush deals with sticky errors */
if (shf_emptybuf(shf, EB_GROW) == EOF) if (shf_emptybuf(shf, EB_GROW) == -1)
return (EOF); return (-1);
/* write chunks larger than window size directly */ /* write chunks larger than window size directly */
if (nbytes > shf->wbsize) { if (nbytes > shf->wbsize) {
ncopy = nbytes; ncopy = nbytes;
@ -679,7 +679,7 @@ shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
* Note: fwrite(3) returns 0 * Note: fwrite(3) returns 0
* for errors - this doesn't * for errors - this doesn't
*/ */
return (EOF); return (-1);
} }
buf += n; buf += n;
ncopy -= n; ncopy -= n;
@ -1049,7 +1049,7 @@ shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
} }
} }
return (shf_error(shf) ? EOF : nwritten); return (shf_error(shf) ? -1 : nwritten);
} }
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST) #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)