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"
__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
@ -871,7 +871,7 @@ expand(
/* $(<...) failed */
subst_exstat = 1;
/* fake EOF */
c = EOF;
c = -1;
} else if (newlines) {
/* spit out saved NLs */
c = '\n';
@ -881,13 +881,13 @@ expand(
if (c == '\n')
/* save newlines */
newlines++;
if (newlines && c != EOF) {
if (newlines && c != -1) {
shf_ungetc(c, x.u.shf);
c = '\n';
--newlines;
}
}
if (c == EOF) {
if (c == -1) {
newlines = 0;
if (x.u.shf)
shf_close(x.u.shf);

29
exec.c
View File

@ -23,7 +23,7 @@
#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
#define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -507,16 +507,17 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
int type_flags;
bool resetspec;
int fcflags = FC_BI|FC_FUNC|FC_PATH;
bool bourne_function_call = false;
struct block *l_expand, *l_assign;
int optc;
/*
* 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()?.
*/
/* snag the last argument for $_ */
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)
;
/* setstr() can't fail here */
@ -556,7 +557,6 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
ap++;
flags |= XEXEC;
} else if (tp->val.f == c_command) {
int optc;
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);
while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p')
saw_p = true;
if (optc != EOF)
if (optc != -1)
/* command -vV or something */
break;
/* don't look for functions */
@ -627,10 +627,9 @@ comexec(struct op *t, struct tbl * volatile tp, const char **ap,
newblock();
/* ksh functions don't keep assignments, POSIX functions do. */
if (!resetspec && tp && tp->type == CFUNC &&
!(tp->flag & FKSH)) {
bourne_function_call = true;
!(tp->flag & FKSH))
type_flags = EXPORT;
} else
else
type_flags = LOCAL|LOCAL_COPY|EXPORT;
}
l_assign = e->loc;
@ -1491,7 +1490,7 @@ herein(struct ioword *iop, char **resbuf)
struct temp *h;
int i;
/* ksh -c 'cat << EOF' can cause this... */
/* ksh -c 'cat <<EOF' can cause this... */
if (iop->heredoc == NULL) {
warningf(true, "%s missing", "here document");
/* special to iosetup(): don't print error */
@ -1526,7 +1525,7 @@ herein(struct ioword *iop, char **resbuf)
return (-2);
}
if (shf_close(shf) == EOF) {
if (shf_close(shf) == -1) {
i = errno;
close(fd);
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,
* 2011, 2012, 2014
* 2011, 2012, 2014, 2015
* Thorsten Glaser <tg@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
@ -27,7 +27,7 @@
#include <sys/file.h>
#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];
static struct sigaction Sigact_ign;
@ -303,7 +303,7 @@ c_fc(const char **wp)
for (hp = rflag ? hlast : hfirst;
hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
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",
"write", tf->tffn, cstrerror(errno));
return (1);

92
shf.c
View File

@ -2,7 +2,7 @@
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
* 2012, 2013
* 2012, 2013, 2015
* Thorsten Glaser <tg@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
@ -25,7 +25,7 @@
#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() */
#define EB_READSW 0x01 /* about to switch to reading */
@ -232,7 +232,7 @@ shf_close(struct shf *shf)
if (shf->fd >= 0) {
ret = shf_flush(shf);
if (close(shf->fd) < 0)
ret = EOF;
ret = -1;
}
if (shf->flags & SHF_ALLOCS)
afree(shf, shf->areap);
@ -251,7 +251,7 @@ shf_fdclose(struct shf *shf)
if (shf->fd >= 0) {
ret = shf_flush(shf);
if (close(shf->fd) < 0)
ret = EOF;
ret = -1;
shf->rnleft = 0;
shf->rp = shf->buf;
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
* buffered. Returns 0 for success, EOF for (write) error.
* buffered. Returns 0 for success, -1 for (write) error.
*/
int
shf_flush(struct shf *shf)
{
if (shf->flags & SHF_STRING)
return ((shf->flags & SHF_WR) ? EOF : 0);
return ((shf->flags & SHF_WR) ? -1 : 0);
if (shf->fd < 0)
internal_errorf("%s: %s", "shf_flush", "no fd");
if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
return (EOF);
return (-1);
}
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
* buffer. Returns 0 for success, EOF for (write) error.
* buffer. Returns 0 for success, -1 for (write) error.
*/
static int
shf_emptybuf(struct shf *shf, int flags)
@ -327,7 +327,7 @@ shf_emptybuf(struct shf *shf, int flags)
if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
return (EOF);
return (-1);
}
if (shf->flags & SHF_READING) {
@ -347,7 +347,7 @@ shf_emptybuf(struct shf *shf, int flags)
*/
if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
!(shf->flags & SHF_ALLOCB))
return (EOF);
return (-1);
/* allocate more space for buffer */
nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
shf->rp = nbuf + (shf->rp - shf->buf);
@ -379,7 +379,7 @@ shf_emptybuf(struct shf *shf, int flags)
ntowrite);
shf->wp = shf->buf + ntowrite;
}
return (EOF);
return (-1);
}
buf += n;
ntowrite -= n;
@ -399,7 +399,7 @@ shf_emptybuf(struct shf *shf, int flags)
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
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_ERROR)
errno = shf->errnosv;
return (EOF);
return (-1);
}
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
return (EOF);
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (-1);
shf->flags |= SHF_READING;
@ -434,7 +434,7 @@ shf_fillbuf(struct shf *shf)
shf->errnosv = errno;
shf->rnleft = 0;
shf->rp = shf->buf;
return (EOF);
return (-1);
}
if ((shf->rnleft = n) == 0)
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
* 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.
*/
ssize_t
@ -459,7 +459,7 @@ shf_read(char *buf, ssize_t bsize, struct shf *shf)
while (bsize > 0) {
if (shf->rnleft == 0 &&
(shf_fillbuf(shf) == EOF || shf->rnleft == 0))
(shf_fillbuf(shf) == -1 || shf->rnleft == 0))
break;
ncopy = shf->rnleft;
if (ncopy > bsize)
@ -471,12 +471,12 @@ shf_read(char *buf, ssize_t bsize, struct shf *shf)
shf->rnleft -= ncopy;
}
/* 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);
}
/*
* 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
* before end of file, returns a pointer to the NUL byte in buf
* otherwise.
@ -498,7 +498,7 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
--bsize;
do {
if (shf->rnleft == 0) {
if (shf_fillbuf(shf) == EOF)
if (shf_fillbuf(shf) == -1)
return (NULL);
if (shf->rnleft == 0) {
*buf = '\0';
@ -520,22 +520,22 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
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
shf_getchar(struct shf *shf)
{
if (!(shf->flags & SHF_RD))
internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
return (EOF);
if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
return (-1);
--shf->rnleft;
return (*shf->rp++);
}
/*
* 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
shf_ungetc(int c, struct shf *shf)
@ -543,12 +543,12 @@ shf_ungetc(int c, struct shf *shf)
if (!(shf->flags & SHF_RD))
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))
return (EOF);
return (-1);
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
return (EOF);
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (-1);
if (shf->rp == shf->buf)
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.
*/
if ((int)(shf->rp[-1]) != c)
return (EOF);
return (-1);
shf->flags &= ~SHF_EOF;
shf->rp--;
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.
*/
int
@ -580,8 +580,8 @@ shf_putchar(int c, struct shf *shf)
if (!(shf->flags & SHF_WR))
internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
if (c == EOF)
return (EOF);
if (c == -1)
return (-1);
if (shf->flags & SHF_UNBUF) {
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");
if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
return (EOF);
return (-1);
}
while ((n = write(shf->fd, &cc, 1)) != 1)
if (n < 0) {
@ -600,12 +600,12 @@ shf_putchar(int c, struct shf *shf)
continue;
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
return (EOF);
return (-1);
}
} else {
/* Flush deals with strings and sticky errors */
if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
return (EOF);
if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
return (-1);
shf->wnleft--;
*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.
*/
ssize_t
shf_puts(const char *s, struct shf *shf)
{
if (!s)
return (EOF);
return (-1);
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
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) {
/* resize buffer until there's enough space left */
while (nbytes > shf->wnleft)
if (shf_emptybuf(shf, EB_GROW) == EOF)
return (EOF);
if (shf_emptybuf(shf, EB_GROW) == -1)
return (-1);
/* then write everything into the buffer */
} else {
/* flush deals with sticky errors */
if (shf_emptybuf(shf, EB_GROW) == EOF)
return (EOF);
if (shf_emptybuf(shf, EB_GROW) == -1)
return (-1);
/* write chunks larger than window size directly */
if (nbytes > shf->wbsize) {
ncopy = nbytes;
@ -679,7 +679,7 @@ shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
* Note: fwrite(3) returns 0
* for errors - this doesn't
*/
return (EOF);
return (-1);
}
buf += 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)