Blame shf.c

Packit 991819
/*	$OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $	*/
Packit 991819
Packit 991819
/*-
Packit 991819
 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
Packit 991819
 *		 2012, 2013, 2015, 2016, 2017, 2018
Packit 991819
 *	mirabilos <m@mirbsd.org>
Packit 991819
 * Copyright (c) 2015
Packit 991819
 *	Daniel Richard G. <skunk@iSKUNK.ORG>
Packit 991819
 *
Packit 991819
 * Provided that these terms and disclaimer and all copyright notices
Packit 991819
 * are retained or reproduced in an accompanying document, permission
Packit 991819
 * is granted to deal in this work without restriction, including un-
Packit 991819
 * limited rights to use, publicly perform, distribute, sell, modify,
Packit 991819
 * merge, give away, or sublicence.
Packit 991819
 *
Packit 991819
 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
Packit 991819
 * the utmost extent permitted by applicable law, neither express nor
Packit 991819
 * implied; without malicious intent or gross negligence. In no event
Packit 991819
 * may a licensor, author or contributor be held liable for indirect,
Packit 991819
 * direct, other damage, loss, or other issues arising in any way out
Packit 991819
 * of dealing in the work, even if advised of the possibility of such
Packit 991819
 * damage or existence of a defect, except proven that it results out
Packit 991819
 * of said person's immediate fault when using the work as intended.
Packit 991819
 *-
Packit 991819
 * Use %zX instead of %p and floating point isn't supported at all.
Packit 991819
 */
Packit 991819
Packit 991819
#include "sh.h"
Packit 991819
Packit 991819
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.97 2018/01/14 01:28:16 tg Exp $");
Packit 991819
Packit 991819
/* flags to shf_emptybuf() */
Packit 991819
#define EB_READSW	0x01	/* about to switch to reading */
Packit 991819
#define EB_GROW		0x02	/* grow buffer if necessary (STRING+DYNAMIC) */
Packit 991819
Packit 991819
/*
Packit 991819
 * Replacement stdio routines. Stdio is too flakey on too many machines
Packit 991819
 * to be useful when you have multiple processes using the same underlying
Packit 991819
 * file descriptors.
Packit 991819
 */
Packit 991819
Packit 991819
static int shf_fillbuf(struct shf *);
Packit 991819
static int shf_emptybuf(struct shf *, int);
Packit 991819
Packit 991819
/*
Packit 991819
 * Open a file. First three args are for open(), last arg is flags for
Packit 991819
 * this package. Returns NULL if file could not be opened, or if a dup
Packit 991819
 * fails.
Packit 991819
 */
Packit 991819
struct shf *
Packit 991819
shf_open(const char *name, int oflags, int mode, int sflags)
Packit 991819
{
Packit 991819
	struct shf *shf;
Packit 991819
	ssize_t bsize =
Packit 991819
	    /* at most 512 */
Packit 991819
	    sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
Packit 991819
	int fd, eno;
Packit 991819
Packit 991819
	/* Done before open so if alloca fails, fd won't be lost. */
Packit 991819
	shf = alloc(sizeof(struct shf) + bsize, ATEMP);
Packit 991819
	shf->areap = ATEMP;
Packit 991819
	shf->buf = (unsigned char *)&shf[1];
Packit 991819
	shf->bsize = bsize;
Packit 991819
	shf->flags = SHF_ALLOCS;
Packit 991819
	/* Rest filled in by reopen. */
Packit 991819
Packit 991819
	fd = binopen3(name, oflags, mode);
Packit 991819
	if (fd < 0) {
Packit 991819
		eno = errno;
Packit 991819
		afree(shf, shf->areap);
Packit 991819
		errno = eno;
Packit 991819
		return (NULL);
Packit 991819
	}
Packit 991819
	if ((sflags & SHF_MAPHI) && fd < FDBASE) {
Packit 991819
		int nfd;
Packit 991819
Packit 991819
		nfd = fcntl(fd, F_DUPFD, FDBASE);
Packit 991819
		eno = errno;
Packit 991819
		close(fd);
Packit 991819
		if (nfd < 0) {
Packit 991819
			afree(shf, shf->areap);
Packit 991819
			errno = eno;
Packit 991819
			return (NULL);
Packit 991819
		}
Packit 991819
		fd = nfd;
Packit 991819
	}
Packit 991819
	sflags &= ~SHF_ACCMODE;
Packit 991819
	sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
Packit 991819
	    ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
Packit 991819
Packit 991819
	return (shf_reopen(fd, sflags, shf));
Packit 991819
}
Packit 991819
Packit 991819
/* helper function for shf_fdopen and shf_reopen */
Packit 991819
static void
Packit 991819
shf_open_hlp(int fd, int *sflagsp, const char *where)
Packit 991819
{
Packit 991819
	int sflags = *sflagsp;
Packit 991819
Packit 991819
	/* use fcntl() to figure out correct read/write flags */
Packit 991819
	if (sflags & SHF_GETFL) {
Packit 991819
		int flags = fcntl(fd, F_GETFL, 0);
Packit 991819
Packit 991819
		if (flags < 0)
Packit 991819
			/* will get an error on first read/write */
Packit 991819
			sflags |= SHF_RDWR;
Packit 991819
		else {
Packit 991819
			switch (flags & O_ACCMODE) {
Packit 991819
			case O_RDONLY:
Packit 991819
				sflags |= SHF_RD;
Packit 991819
				break;
Packit 991819
			case O_WRONLY:
Packit 991819
				sflags |= SHF_WR;
Packit 991819
				break;
Packit 991819
			case O_RDWR:
Packit 991819
				sflags |= SHF_RDWR;
Packit 991819
				break;
Packit 991819
			}
Packit 991819
		}
Packit 991819
		*sflagsp = sflags;
Packit 991819
	}
Packit 991819
Packit 991819
	if (!(sflags & (SHF_RD | SHF_WR)))
Packit 991819
		internal_errorf(Tf_sD_s, where, "missing read/write");
Packit 991819
}
Packit 991819
Packit 991819
/* Set up the shf structure for a file descriptor. Doesn't fail. */
Packit 991819
struct shf *
Packit 991819
shf_fdopen(int fd, int sflags, struct shf *shf)
Packit 991819
{
Packit 991819
	ssize_t bsize =
Packit 991819
	    /* at most 512 */
Packit 991819
	    sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
Packit 991819
Packit 991819
	shf_open_hlp(fd, &sflags, "shf_fdopen");
Packit 991819
	if (shf) {
Packit 991819
		if (bsize) {
Packit 991819
			shf->buf = alloc(bsize, ATEMP);
Packit 991819
			sflags |= SHF_ALLOCB;
Packit 991819
		} else
Packit 991819
			shf->buf = NULL;
Packit 991819
	} else {
Packit 991819
		shf = alloc(sizeof(struct shf) + bsize, ATEMP);
Packit 991819
		shf->buf = (unsigned char *)&shf[1];
Packit 991819
		sflags |= SHF_ALLOCS;
Packit 991819
	}
Packit 991819
	shf->areap = ATEMP;
Packit 991819
	shf->fd = fd;
Packit 991819
	shf->rp = shf->wp = shf->buf;
Packit 991819
	shf->rnleft = 0;
Packit 991819
	shf->rbsize = bsize;
Packit 991819
	shf->wnleft = 0; /* force call to shf_emptybuf() */
Packit 991819
	shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
Packit 991819
	shf->flags = sflags;
Packit 991819
	shf->errnosv = 0;
Packit 991819
	shf->bsize = bsize;
Packit 991819
	if (sflags & SHF_CLEXEC)
Packit 991819
		fcntl(fd, F_SETFD, FD_CLOEXEC);
Packit 991819
	return (shf);
Packit 991819
}
Packit 991819
Packit 991819
/* Set up an existing shf (and buffer) to use the given fd */
Packit 991819
struct shf *
Packit 991819
shf_reopen(int fd, int sflags, struct shf *shf)
Packit 991819
{
Packit 991819
	ssize_t bsize =
Packit 991819
	    /* at most 512 */
Packit 991819
	    sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
Packit 991819
Packit 991819
	shf_open_hlp(fd, &sflags, "shf_reopen");
Packit 991819
	if (!shf || !shf->buf || shf->bsize < bsize)
Packit 991819
		internal_errorf(Tf_sD_s, "shf_reopen", Tbad_bsize);
Packit 991819
Packit 991819
	/* assumes shf->buf and shf->bsize already set up */
Packit 991819
	shf->fd = fd;
Packit 991819
	shf->rp = shf->wp = shf->buf;
Packit 991819
	shf->rnleft = 0;
Packit 991819
	shf->rbsize = bsize;
Packit 991819
	shf->wnleft = 0; /* force call to shf_emptybuf() */
Packit 991819
	shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
Packit 991819
	shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
Packit 991819
	shf->errnosv = 0;
Packit 991819
	if (sflags & SHF_CLEXEC)
Packit 991819
		fcntl(fd, F_SETFD, FD_CLOEXEC);
Packit 991819
	return (shf);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Open a string for reading or writing. If reading, bsize is the number
Packit 991819
 * of bytes that can be read. If writing, bsize is the maximum number of
Packit 991819
 * bytes that can be written. If shf is not NULL, it is filled in and
Packit 991819
 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
Packit 991819
 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
Packit 991819
 * used for the initial size). Doesn't fail.
Packit 991819
 * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
Packit 991819
 */
Packit 991819
struct shf *
Packit 991819
shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
Packit 991819
{
Packit 991819
	/* can't have a read+write string */
Packit 991819
	if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
Packit 991819
		internal_errorf(Tf_flags, "shf_sopen",
Packit 991819
		    (unsigned int)sflags);
Packit 991819
Packit 991819
	if (!shf) {
Packit 991819
		shf = alloc(sizeof(struct shf), ATEMP);
Packit 991819
		sflags |= SHF_ALLOCS;
Packit 991819
	}
Packit 991819
	shf->areap = ATEMP;
Packit 991819
	if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
Packit 991819
		if (bsize <= 0)
Packit 991819
			bsize = 64;
Packit 991819
		sflags |= SHF_ALLOCB;
Packit 991819
		buf = alloc(bsize, shf->areap);
Packit 991819
	}
Packit 991819
	shf->fd = -1;
Packit 991819
	shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
Packit 991819
	shf->rnleft = bsize;
Packit 991819
	shf->rbsize = bsize;
Packit 991819
	shf->wnleft = bsize - 1;	/* space for a '\0' */
Packit 991819
	shf->wbsize = bsize;
Packit 991819
	shf->flags = sflags | SHF_STRING;
Packit 991819
	shf->errnosv = 0;
Packit 991819
	shf->bsize = bsize;
Packit 991819
Packit 991819
	return (shf);
Packit 991819
}
Packit 991819
Packit 991819
/* Flush and close file descriptor, free the shf structure */
Packit 991819
int
Packit 991819
shf_close(struct shf *shf)
Packit 991819
{
Packit 991819
	int ret = 0;
Packit 991819
Packit 991819
	if (shf->fd >= 0) {
Packit 991819
		ret = shf_flush(shf);
Packit 991819
		if (close(shf->fd) < 0)
Packit 991819
			ret = -1;
Packit 991819
	}
Packit 991819
	if (shf->flags & SHF_ALLOCS)
Packit 991819
		afree(shf, shf->areap);
Packit 991819
	else if (shf->flags & SHF_ALLOCB)
Packit 991819
		afree(shf->buf, shf->areap);
Packit 991819
Packit 991819
	return (ret);
Packit 991819
}
Packit 991819
Packit 991819
/* Flush and close file descriptor, don't free file structure */
Packit 991819
int
Packit 991819
shf_fdclose(struct shf *shf)
Packit 991819
{
Packit 991819
	int ret = 0;
Packit 991819
Packit 991819
	if (shf->fd >= 0) {
Packit 991819
		ret = shf_flush(shf);
Packit 991819
		if (close(shf->fd) < 0)
Packit 991819
			ret = -1;
Packit 991819
		shf->rnleft = 0;
Packit 991819
		shf->rp = shf->buf;
Packit 991819
		shf->wnleft = 0;
Packit 991819
		shf->fd = -1;
Packit 991819
	}
Packit 991819
Packit 991819
	return (ret);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Close a string - if it was opened for writing, it is NUL terminated;
Packit 991819
 * returns a pointer to the string and frees shf if it was allocated
Packit 991819
 * (does not free string if it was allocated).
Packit 991819
 */
Packit 991819
char *
Packit 991819
shf_sclose(struct shf *shf)
Packit 991819
{
Packit 991819
	unsigned char *s = shf->buf;
Packit 991819
Packit 991819
	/* NUL terminate */
Packit 991819
	if (shf->flags & SHF_WR) {
Packit 991819
		shf->wnleft++;
Packit 991819
		shf_putc('\0', shf);
Packit 991819
	}
Packit 991819
	if (shf->flags & SHF_ALLOCS)
Packit 991819
		afree(shf, shf->areap);
Packit 991819
	return ((char *)s);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Un-read what has been read but not examined, or write what has been
Packit 991819
 * buffered. Returns 0 for success, -1 for (write) error.
Packit 991819
 */
Packit 991819
int
Packit 991819
shf_flush(struct shf *shf)
Packit 991819
{
Packit 991819
	int rv = 0;
Packit 991819
Packit 991819
	if (shf->flags & SHF_STRING)
Packit 991819
		rv = (shf->flags & SHF_WR) ? -1 : 0;
Packit 991819
	else if (shf->fd < 0)
Packit 991819
		internal_errorf(Tf_sD_s, "shf_flush", "no fd");
Packit 991819
	else if (shf->flags & SHF_ERROR) {
Packit 991819
		errno = shf->errnosv;
Packit 991819
		rv = -1;
Packit 991819
	} else if (shf->flags & SHF_READING) {
Packit 991819
		shf->flags &= ~(SHF_EOF | SHF_READING);
Packit 991819
		if (shf->rnleft > 0) {
Packit 991819
			if (lseek(shf->fd, (off_t)-shf->rnleft,
Packit 991819
			    SEEK_CUR) == -1) {
Packit 991819
				shf->flags |= SHF_ERROR;
Packit 991819
				shf->errnosv = errno;
Packit 991819
				rv = -1;
Packit 991819
			}
Packit 991819
			shf->rnleft = 0;
Packit 991819
			shf->rp = shf->buf;
Packit 991819
		}
Packit 991819
	} else if (shf->flags & SHF_WRITING)
Packit 991819
		rv = shf_emptybuf(shf, 0);
Packit 991819
Packit 991819
	return (rv);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Write out any buffered data. If currently reading, flushes the read
Packit 991819
 * buffer. Returns 0 for success, -1 for (write) error.
Packit 991819
 */
Packit 991819
static int
Packit 991819
shf_emptybuf(struct shf *shf, int flags)
Packit 991819
{
Packit 991819
	int ret = 0;
Packit 991819
Packit 991819
	if (!(shf->flags & SHF_STRING) && shf->fd < 0)
Packit 991819
		internal_errorf(Tf_sD_s, "shf_emptybuf", "no fd");
Packit 991819
Packit 991819
	if (shf->flags & SHF_ERROR) {
Packit 991819
		errno = shf->errnosv;
Packit 991819
		return (-1);
Packit 991819
	}
Packit 991819
Packit 991819
	if (shf->flags & SHF_READING) {
Packit 991819
		if (flags & EB_READSW)
Packit 991819
			/* doesn't happen */
Packit 991819
			return (0);
Packit 991819
		ret = shf_flush(shf);
Packit 991819
		shf->flags &= ~SHF_READING;
Packit 991819
	}
Packit 991819
	if (shf->flags & SHF_STRING) {
Packit 991819
		unsigned char *nbuf;
Packit 991819
Packit 991819
		/*
Packit 991819
		 * Note that we assume SHF_ALLOCS is not set if
Packit 991819
		 * SHF_ALLOCB is set... (changing the shf pointer could
Packit 991819
		 * cause problems)
Packit 991819
		 */
Packit 991819
		if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
Packit 991819
		    !(shf->flags & SHF_ALLOCB))
Packit 991819
			return (-1);
Packit 991819
		/* allocate more space for buffer */
Packit 991819
		nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
Packit 991819
		shf->rp = nbuf + (shf->rp - shf->buf);
Packit 991819
		shf->wp = nbuf + (shf->wp - shf->buf);
Packit 991819
		shf->rbsize += shf->wbsize;
Packit 991819
		shf->wnleft += shf->wbsize;
Packit 991819
		shf->wbsize <<= 1;
Packit 991819
		shf->buf = nbuf;
Packit 991819
	} else {
Packit 991819
		if (shf->flags & SHF_WRITING) {
Packit 991819
			ssize_t n, ntowrite = shf->wp - shf->buf;
Packit 991819
			unsigned char *buf = shf->buf;
Packit 991819
Packit 991819
			while (ntowrite > 0) {
Packit 991819
				n = write(shf->fd, buf, ntowrite);
Packit 991819
				if (n < 0) {
Packit 991819
					if (errno == EINTR &&
Packit 991819
					    !(shf->flags & SHF_INTERRUPT))
Packit 991819
						continue;
Packit 991819
					shf->flags |= SHF_ERROR;
Packit 991819
					shf->errnosv = errno;
Packit 991819
					shf->wnleft = 0;
Packit 991819
					if (buf != shf->buf) {
Packit 991819
						/*
Packit 991819
						 * allow a second flush
Packit 991819
						 * to work
Packit 991819
						 */
Packit 991819
						memmove(shf->buf, buf,
Packit 991819
						    ntowrite);
Packit 991819
						shf->wp = shf->buf + ntowrite;
Packit 991819
					}
Packit 991819
					return (-1);
Packit 991819
				}
Packit 991819
				buf += n;
Packit 991819
				ntowrite -= n;
Packit 991819
			}
Packit 991819
			if (flags & EB_READSW) {
Packit 991819
				shf->wp = shf->buf;
Packit 991819
				shf->wnleft = 0;
Packit 991819
				shf->flags &= ~SHF_WRITING;
Packit 991819
				return (0);
Packit 991819
			}
Packit 991819
		}
Packit 991819
		shf->wp = shf->buf;
Packit 991819
		shf->wnleft = shf->wbsize;
Packit 991819
	}
Packit 991819
	shf->flags |= SHF_WRITING;
Packit 991819
Packit 991819
	return (ret);
Packit 991819
}
Packit 991819
Packit 991819
/* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
Packit 991819
static int
Packit 991819
shf_fillbuf(struct shf *shf)
Packit 991819
{
Packit 991819
	ssize_t n;
Packit 991819
Packit 991819
	if (shf->flags & SHF_STRING)
Packit 991819
		return (0);
Packit 991819
Packit 991819
	if (shf->fd < 0)
Packit 991819
		internal_errorf(Tf_sD_s, "shf_fillbuf", "no fd");
Packit 991819
Packit 991819
	if (shf->flags & (SHF_EOF | SHF_ERROR)) {
Packit 991819
		if (shf->flags & SHF_ERROR)
Packit 991819
			errno = shf->errnosv;
Packit 991819
		return (-1);
Packit 991819
	}
Packit 991819
Packit 991819
	if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
Packit 991819
		return (-1);
Packit 991819
Packit 991819
	shf->flags |= SHF_READING;
Packit 991819
Packit 991819
	shf->rp = shf->buf;
Packit 991819
	while (/* CONSTCOND */ 1) {
Packit 991819
		n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
Packit 991819
		if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
Packit 991819
			continue;
Packit 991819
		break;
Packit 991819
	}
Packit 991819
	if (n < 0) {
Packit 991819
		shf->flags |= SHF_ERROR;
Packit 991819
		shf->errnosv = errno;
Packit 991819
		shf->rnleft = 0;
Packit 991819
		shf->rp = shf->buf;
Packit 991819
		return (-1);
Packit 991819
	}
Packit 991819
	if ((shf->rnleft = n) == 0)
Packit 991819
		shf->flags |= SHF_EOF;
Packit 991819
	return (0);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Read a buffer from shf. Returns the number of bytes read into buf, if
Packit 991819
 * no bytes were read, returns 0 if end of file was seen, -1 if a read
Packit 991819
 * error occurred.
Packit 991819
 */
Packit 991819
ssize_t
Packit 991819
shf_read(char *buf, ssize_t bsize, struct shf *shf)
Packit 991819
{
Packit 991819
	ssize_t ncopy, orig_bsize = bsize;
Packit 991819
Packit 991819
	if (!(shf->flags & SHF_RD))
Packit 991819
		internal_errorf(Tf_flags, Tshf_read,
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if (bsize <= 0)
Packit 991819
		internal_errorf(Tf_szs, Tshf_read, bsize, Tbsize);
Packit 991819
Packit 991819
	while (bsize > 0) {
Packit 991819
		if (shf->rnleft == 0 &&
Packit 991819
		    (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
Packit 991819
			break;
Packit 991819
		ncopy = shf->rnleft;
Packit 991819
		if (ncopy > bsize)
Packit 991819
			ncopy = bsize;
Packit 991819
		memcpy(buf, shf->rp, ncopy);
Packit 991819
		buf += ncopy;
Packit 991819
		bsize -= ncopy;
Packit 991819
		shf->rp += ncopy;
Packit 991819
		shf->rnleft -= ncopy;
Packit 991819
	}
Packit 991819
	/* Note: fread(3S) returns 0 for errors - this doesn't */
Packit 991819
	return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
Packit 991819
	    orig_bsize - bsize);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Read up to a newline or -1. The newline is put in buf; buf is always
Packit 991819
 * NUL terminated. Returns NULL on read error or if nothing was read
Packit 991819
 * before end of file, returns a pointer to the NUL byte in buf
Packit 991819
 * otherwise.
Packit 991819
 */
Packit 991819
char *
Packit 991819
shf_getse(char *buf, ssize_t bsize, struct shf *shf)
Packit 991819
{
Packit 991819
	unsigned char *end;
Packit 991819
	ssize_t ncopy;
Packit 991819
	char *orig_buf = buf;
Packit 991819
Packit 991819
	if (!(shf->flags & SHF_RD))
Packit 991819
		internal_errorf(Tf_flags, "shf_getse",
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if (bsize <= 0)
Packit 991819
		return (NULL);
Packit 991819
Packit 991819
	/* save room for NUL */
Packit 991819
	--bsize;
Packit 991819
	do {
Packit 991819
		if (shf->rnleft == 0) {
Packit 991819
			if (shf_fillbuf(shf) == -1)
Packit 991819
				return (NULL);
Packit 991819
			if (shf->rnleft == 0) {
Packit 991819
				*buf = '\0';
Packit 991819
				return (buf == orig_buf ? NULL : buf);
Packit 991819
			}
Packit 991819
		}
Packit 991819
		end = (unsigned char *)memchr((char *)shf->rp, '\n',
Packit 991819
		    shf->rnleft);
Packit 991819
		ncopy = end ? end - shf->rp + 1 : shf->rnleft;
Packit 991819
		if (ncopy > bsize)
Packit 991819
			ncopy = bsize;
Packit 991819
		memcpy(buf, (char *) shf->rp, ncopy);
Packit 991819
		shf->rp += ncopy;
Packit 991819
		shf->rnleft -= ncopy;
Packit 991819
		buf += ncopy;
Packit 991819
		bsize -= ncopy;
Packit 991819
#ifdef MKSH_WITH_TEXTMODE
Packit 991819
		if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
Packit 991819
			buf--;
Packit 991819
			bsize++;
Packit 991819
			buf[-1] = '\n';
Packit 991819
		}
Packit 991819
#endif
Packit 991819
	} while (!end && bsize);
Packit 991819
#ifdef MKSH_WITH_TEXTMODE
Packit 991819
	if (!bsize && buf[-1] == '\r') {
Packit 991819
		int c = shf_getc(shf);
Packit 991819
		if (c == '\n')
Packit 991819
			buf[-1] = '\n';
Packit 991819
		else if (c != -1)
Packit 991819
			shf_ungetc(c, shf);
Packit 991819
	}
Packit 991819
#endif
Packit 991819
	*buf = '\0';
Packit 991819
	return (buf);
Packit 991819
}
Packit 991819
Packit 991819
/* Returns the char read. Returns -1 for error and end of file. */
Packit 991819
int
Packit 991819
shf_getchar(struct shf *shf)
Packit 991819
{
Packit 991819
	if (!(shf->flags & SHF_RD))
Packit 991819
		internal_errorf(Tf_flags, "shf_getchar",
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
Packit 991819
		return (-1);
Packit 991819
	--shf->rnleft;
Packit 991819
	return (ord(*shf->rp++));
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Put a character back in the input stream. Returns the character if
Packit 991819
 * successful, -1 if there is no room.
Packit 991819
 */
Packit 991819
int
Packit 991819
shf_ungetc(int c, struct shf *shf)
Packit 991819
{
Packit 991819
	if (!(shf->flags & SHF_RD))
Packit 991819
		internal_errorf(Tf_flags, "shf_ungetc",
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if ((shf->flags & SHF_ERROR) || c == -1 ||
Packit 991819
	    (shf->rp == shf->buf && shf->rnleft))
Packit 991819
		return (-1);
Packit 991819
Packit 991819
	if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
Packit 991819
		return (-1);
Packit 991819
Packit 991819
	if (shf->rp == shf->buf)
Packit 991819
		shf->rp = shf->buf + shf->rbsize;
Packit 991819
	if (shf->flags & SHF_STRING) {
Packit 991819
		/*
Packit 991819
		 * Can unget what was read, but not something different;
Packit 991819
		 * we don't want to modify a string.
Packit 991819
		 */
Packit 991819
		if ((int)(shf->rp[-1]) != c)
Packit 991819
			return (-1);
Packit 991819
		shf->flags &= ~SHF_EOF;
Packit 991819
		shf->rp--;
Packit 991819
		shf->rnleft++;
Packit 991819
		return (c);
Packit 991819
	}
Packit 991819
	shf->flags &= ~SHF_EOF;
Packit 991819
	*--(shf->rp) = c;
Packit 991819
	shf->rnleft++;
Packit 991819
	return (c);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Write a character. Returns the character if successful, -1 if the
Packit 991819
 * char could not be written.
Packit 991819
 */
Packit 991819
int
Packit 991819
shf_putchar(int c, struct shf *shf)
Packit 991819
{
Packit 991819
	if (!(shf->flags & SHF_WR))
Packit 991819
		internal_errorf(Tf_flags, "shf_putchar",
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if (c == -1)
Packit 991819
		return (-1);
Packit 991819
Packit 991819
	if (shf->flags & SHF_UNBUF) {
Packit 991819
		unsigned char cc = (unsigned char)c;
Packit 991819
		ssize_t n;
Packit 991819
Packit 991819
		if (shf->fd < 0)
Packit 991819
			internal_errorf(Tf_sD_s, "shf_putchar", "no fd");
Packit 991819
		if (shf->flags & SHF_ERROR) {
Packit 991819
			errno = shf->errnosv;
Packit 991819
			return (-1);
Packit 991819
		}
Packit 991819
		while ((n = write(shf->fd, &cc, 1)) != 1)
Packit 991819
			if (n < 0) {
Packit 991819
				if (errno == EINTR &&
Packit 991819
				    !(shf->flags & SHF_INTERRUPT))
Packit 991819
					continue;
Packit 991819
				shf->flags |= SHF_ERROR;
Packit 991819
				shf->errnosv = errno;
Packit 991819
				return (-1);
Packit 991819
			}
Packit 991819
	} else {
Packit 991819
		/* Flush deals with strings and sticky errors */
Packit 991819
		if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
Packit 991819
			return (-1);
Packit 991819
		shf->wnleft--;
Packit 991819
		*shf->wp++ = c;
Packit 991819
	}
Packit 991819
Packit 991819
	return (c);
Packit 991819
}
Packit 991819
Packit 991819
/*
Packit 991819
 * Write a string. Returns the length of the string if successful, -1
Packit 991819
 * if the string could not be written.
Packit 991819
 */
Packit 991819
ssize_t
Packit 991819
shf_puts(const char *s, struct shf *shf)
Packit 991819
{
Packit 991819
	if (!s)
Packit 991819
		return (-1);
Packit 991819
Packit 991819
	return (shf_write(s, strlen(s), shf));
Packit 991819
}
Packit 991819
Packit 991819
/* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
Packit 991819
ssize_t
Packit 991819
shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
Packit 991819
{
Packit 991819
	ssize_t n, ncopy, orig_nbytes = nbytes;
Packit 991819
Packit 991819
	if (!(shf->flags & SHF_WR))
Packit 991819
		internal_errorf(Tf_flags, Tshf_write,
Packit 991819
		    (unsigned int)shf->flags);
Packit 991819
Packit 991819
	if (nbytes < 0)
Packit 991819
		internal_errorf(Tf_szs, Tshf_write, nbytes, Tbytes);
Packit 991819
Packit 991819
	/* Don't buffer if buffer is empty and we're writting a large amount. */
Packit 991819
	if ((ncopy = shf->wnleft) &&
Packit 991819
	    (shf->wp != shf->buf || nbytes < shf->wnleft)) {
Packit 991819
		if (ncopy > nbytes)
Packit 991819
			ncopy = nbytes;
Packit 991819
		memcpy(shf->wp, buf, ncopy);
Packit 991819
		nbytes -= ncopy;
Packit 991819
		buf += ncopy;
Packit 991819
		shf->wp += ncopy;
Packit 991819
		shf->wnleft -= ncopy;
Packit 991819
	}
Packit 991819
	if (nbytes > 0) {
Packit 991819
		if (shf->flags & SHF_STRING) {
Packit 991819
			/* resize buffer until there's enough space left */
Packit 991819
			while (nbytes > shf->wnleft)
Packit 991819
				if (shf_emptybuf(shf, EB_GROW) == -1)
Packit 991819
					return (-1);
Packit 991819
			/* then write everything into the buffer */
Packit 991819
		} else {
Packit 991819
			/* flush deals with sticky errors */
Packit 991819
			if (shf_emptybuf(shf, EB_GROW) == -1)
Packit 991819
				return (-1);
Packit 991819
			/* write chunks larger than window size directly */
Packit 991819
			if (nbytes > shf->wbsize) {
Packit 991819
				ncopy = nbytes;
Packit 991819
				if (shf->wbsize)
Packit 991819
					ncopy -= nbytes % shf->wbsize;
Packit 991819
				nbytes -= ncopy;
Packit 991819
				while (ncopy > 0) {
Packit 991819
					n = write(shf->fd, buf, ncopy);
Packit 991819
					if (n < 0) {
Packit 991819
						if (errno == EINTR &&
Packit 991819
						    !(shf->flags & SHF_INTERRUPT))
Packit 991819
							continue;
Packit 991819
						shf->flags |= SHF_ERROR;
Packit 991819
						shf->errnosv = errno;
Packit 991819
						shf->wnleft = 0;
Packit 991819
						/*
Packit 991819
						 * Note: fwrite(3) returns 0
Packit 991819
						 * for errors - this doesn't
Packit 991819
						 */
Packit 991819
						return (-1);
Packit 991819
					}
Packit 991819
					buf += n;
Packit 991819
					ncopy -= n;
Packit 991819
				}
Packit 991819
			}
Packit 991819
			/* ... and buffer the rest */
Packit 991819
		}
Packit 991819
		if (nbytes > 0) {
Packit 991819
			/* write remaining bytes to buffer */
Packit 991819
			memcpy(shf->wp, buf, nbytes);
Packit 991819
			shf->wp += nbytes;
Packit 991819
			shf->wnleft -= nbytes;
Packit 991819
		}
Packit 991819
	}
Packit 991819
Packit 991819
	return (orig_nbytes);
Packit 991819
}
Packit 991819
Packit 991819
ssize_t
Packit 991819
shf_fprintf(struct shf *shf, const char *fmt, ...)
Packit 991819
{
Packit 991819
	va_list args;
Packit 991819
	ssize_t n;
Packit 991819
Packit 991819
	va_start(args, fmt);
Packit 991819
	n = shf_vfprintf(shf, fmt, args);
Packit 991819
	va_end(args);
Packit 991819
Packit 991819
	return (n);
Packit 991819
}
Packit 991819
Packit 991819
ssize_t
Packit 991819
shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
Packit 991819
{
Packit 991819
	struct shf shf;
Packit 991819
	va_list args;
Packit 991819
	ssize_t n;
Packit 991819
Packit 991819
	if (!buf || bsize <= 0)
Packit 991819
		internal_errorf("shf_snprintf: buf %zX, bsize %zd",
Packit 991819
		    (size_t)buf, bsize);
Packit 991819
Packit 991819
	shf_sopen(buf, bsize, SHF_WR, &shf;;
Packit 991819
	va_start(args, fmt);
Packit 991819
	n = shf_vfprintf(&shf, fmt, args);
Packit 991819
	va_end(args);
Packit 991819
	/* NUL terminates */
Packit 991819
	shf_sclose(&shf;;
Packit 991819
	return (n);
Packit 991819
}
Packit 991819
Packit 991819
char *
Packit 991819
shf_smprintf(const char *fmt, ...)
Packit 991819
{
Packit 991819
	struct shf shf;
Packit 991819
	va_list args;
Packit 991819
Packit 991819
	shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf;;
Packit 991819
	va_start(args, fmt);
Packit 991819
	shf_vfprintf(&shf, fmt, args);
Packit 991819
	va_end(args);
Packit 991819
	/* NUL terminates */
Packit 991819
	return (shf_sclose(&shf));
Packit 991819
}
Packit 991819
Packit 991819
#define	FL_HASH		0x001	/* '#' seen */
Packit 991819
#define FL_PLUS		0x002	/* '+' seen */
Packit 991819
#define FL_RIGHT	0x004	/* '-' seen */
Packit 991819
#define FL_BLANK	0x008	/* ' ' seen */
Packit 991819
#define FL_SHORT	0x010	/* 'h' seen */
Packit 991819
#define FL_LONG		0x020	/* 'l' seen */
Packit 991819
#define FL_ZERO		0x040	/* '0' seen */
Packit 991819
#define FL_DOT		0x080	/* '.' seen */
Packit 991819
#define FL_UPPER	0x100	/* format character was uppercase */
Packit 991819
#define FL_NUMBER	0x200	/* a number was formated %[douxefg] */
Packit 991819
#define FL_SIZET	0x400	/* 'z' seen */
Packit 991819
#define FM_SIZES	0x430	/* h/l/z mask */
Packit 991819
Packit 991819
ssize_t
Packit 991819
shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
Packit 991819
{
Packit 991819
	const char *s;
Packit 991819
	char c, *cp;
Packit 991819
	int tmp = 0, flags;
Packit 991819
	size_t field, precision, len;
Packit 991819
	unsigned long lnum;
Packit 991819
	/* %#o produces the longest output */
Packit 991819
	char numbuf[(8 * sizeof(long) + 2) / 3 + 1 + /* NUL */ 1];
Packit 991819
	/* this stuff for dealing with the buffer */
Packit 991819
	ssize_t nwritten = 0;
Packit 991819
Packit 991819
#define VA(type) va_arg(args, type)
Packit 991819
Packit 991819
	if (!fmt)
Packit 991819
		return (0);
Packit 991819
Packit 991819
	while ((c = *fmt++)) {
Packit 991819
		if (c != '%') {
Packit 991819
			shf_putc(c, shf);
Packit 991819
			nwritten++;
Packit 991819
			continue;
Packit 991819
		}
Packit 991819
		/*
Packit 991819
		 * This will accept flags/fields in any order - not just
Packit 991819
		 * the order specified in printf(3), but this is the way
Packit 991819
		 * _doprnt() seems to work (on BSD and SYSV). The only
Packit 991819
		 * restriction is that the format character must come
Packit 991819
		 * last :-).
Packit 991819
		 */
Packit 991819
		flags = 0;
Packit 991819
		field = precision = 0;
Packit 991819
		while ((c = *fmt++)) {
Packit 991819
			switch (c) {
Packit 991819
			case '#':
Packit 991819
				flags |= FL_HASH;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case '+':
Packit 991819
				flags |= FL_PLUS;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case '-':
Packit 991819
				flags |= FL_RIGHT;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case ' ':
Packit 991819
				flags |= FL_BLANK;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case '0':
Packit 991819
				if (!(flags & FL_DOT))
Packit 991819
					flags |= FL_ZERO;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case '.':
Packit 991819
				flags |= FL_DOT;
Packit 991819
				precision = 0;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case '*':
Packit 991819
				tmp = VA(int);
Packit 991819
				if (tmp < 0) {
Packit 991819
					if (flags & FL_DOT)
Packit 991819
						precision = 0;
Packit 991819
					else {
Packit 991819
						field = (unsigned int)-tmp;
Packit 991819
						flags |= FL_RIGHT;
Packit 991819
					}
Packit 991819
				} else if (flags & FL_DOT)
Packit 991819
					precision = (unsigned int)tmp;
Packit 991819
				else
Packit 991819
					field = (unsigned int)tmp;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case 'l':
Packit 991819
				flags &= ~FM_SIZES;
Packit 991819
				flags |= FL_LONG;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case 'h':
Packit 991819
				flags &= ~FM_SIZES;
Packit 991819
				flags |= FL_SHORT;
Packit 991819
				continue;
Packit 991819
Packit 991819
			case 'z':
Packit 991819
				flags &= ~FM_SIZES;
Packit 991819
				flags |= FL_SIZET;
Packit 991819
				continue;
Packit 991819
			}
Packit 991819
			if (ctype(c, C_DIGIT)) {
Packit 991819
				bool overflowed = false;
Packit 991819
Packit 991819
				tmp = ksh_numdig(c);
Packit 991819
				while (ctype((c = *fmt++), C_DIGIT))
Packit 991819
					if (notok2mul(2147483647, tmp, 10))
Packit 991819
						overflowed = true;
Packit 991819
					else
Packit 991819
						tmp = tmp * 10 + ksh_numdig(c);
Packit 991819
				--fmt;
Packit 991819
				if (overflowed)
Packit 991819
					tmp = 0;
Packit 991819
				if (flags & FL_DOT)
Packit 991819
					precision = (unsigned int)tmp;
Packit 991819
				else
Packit 991819
					field = (unsigned int)tmp;
Packit 991819
				continue;
Packit 991819
			}
Packit 991819
			break;
Packit 991819
		}
Packit 991819
Packit 991819
		if (!c)
Packit 991819
			/* nasty format */
Packit 991819
			break;
Packit 991819
Packit 991819
		if (ctype(c, C_UPPER)) {
Packit 991819
			flags |= FL_UPPER;
Packit 991819
			c = ksh_tolower(c);
Packit 991819
		}
Packit 991819
Packit 991819
		switch (c) {
Packit 991819
		case 'd':
Packit 991819
		case 'i':
Packit 991819
			if (flags & FL_SIZET)
Packit 991819
				lnum = (long)VA(ssize_t);
Packit 991819
			else if (flags & FL_LONG)
Packit 991819
				lnum = VA(long);
Packit 991819
			else if (flags & FL_SHORT)
Packit 991819
				lnum = (long)(short)VA(int);
Packit 991819
			else
Packit 991819
				lnum = (long)VA(int);
Packit 991819
			goto integral;
Packit 991819
Packit 991819
		case 'o':
Packit 991819
		case 'u':
Packit 991819
		case 'x':
Packit 991819
			if (flags & FL_SIZET)
Packit 991819
				lnum = VA(size_t);
Packit 991819
			else if (flags & FL_LONG)
Packit 991819
				lnum = VA(unsigned long);
Packit 991819
			else if (flags & FL_SHORT)
Packit 991819
				lnum = (unsigned long)(unsigned short)VA(int);
Packit 991819
			else
Packit 991819
				lnum = (unsigned long)VA(unsigned int);
Packit 991819
Packit 991819
 integral:
Packit 991819
			flags |= FL_NUMBER;
Packit 991819
			cp = numbuf + sizeof(numbuf);
Packit 991819
			*--cp = '\0';
Packit 991819
Packit 991819
			switch (c) {
Packit 991819
			case 'd':
Packit 991819
			case 'i':
Packit 991819
				if (0 > (long)lnum) {
Packit 991819
					lnum = -(long)lnum;
Packit 991819
					tmp = 1;
Packit 991819
				} else
Packit 991819
					tmp = 0;
Packit 991819
				/* FALLTHROUGH */
Packit 991819
			case 'u':
Packit 991819
				do {
Packit 991819
					*--cp = digits_lc[lnum % 10];
Packit 991819
					lnum /= 10;
Packit 991819
				} while (lnum);
Packit 991819
Packit 991819
				if (c != 'u') {
Packit 991819
					if (tmp)
Packit 991819
						*--cp = '-';
Packit 991819
					else if (flags & FL_PLUS)
Packit 991819
						*--cp = '+';
Packit 991819
					else if (flags & FL_BLANK)
Packit 991819
						*--cp = ' ';
Packit 991819
				}
Packit 991819
				break;
Packit 991819
Packit 991819
			case 'o':
Packit 991819
				do {
Packit 991819
					*--cp = digits_lc[lnum & 0x7];
Packit 991819
					lnum >>= 3;
Packit 991819
				} while (lnum);
Packit 991819
Packit 991819
				if ((flags & FL_HASH) && *cp != '0')
Packit 991819
					*--cp = '0';
Packit 991819
				break;
Packit 991819
Packit 991819
			case 'x': {
Packit 991819
				const char *digits = (flags & FL_UPPER) ?
Packit 991819
				    digits_uc : digits_lc;
Packit 991819
				do {
Packit 991819
					*--cp = digits[lnum & 0xF];
Packit 991819
					lnum >>= 4;
Packit 991819
				} while (lnum);
Packit 991819
Packit 991819
				if (flags & FL_HASH) {
Packit 991819
					*--cp = (flags & FL_UPPER) ? 'X' : 'x';
Packit 991819
					*--cp = '0';
Packit 991819
				}
Packit 991819
			    }
Packit 991819
			}
Packit 991819
			len = numbuf + sizeof(numbuf) - 1 - (s = cp);
Packit 991819
			if (flags & FL_DOT) {
Packit 991819
				if (precision > len) {
Packit 991819
					field = precision;
Packit 991819
					flags |= FL_ZERO;
Packit 991819
				} else
Packit 991819
					/* no loss */
Packit 991819
					precision = len;
Packit 991819
			}
Packit 991819
			break;
Packit 991819
Packit 991819
		case 's':
Packit 991819
			if ((s = VA(const char *)) == NULL)
Packit 991819
				s = "(null)";
Packit 991819
			else if (flags & FL_HASH) {
Packit 991819
				print_value_quoted(shf, s);
Packit 991819
				continue;
Packit 991819
			}
Packit 991819
			len = utf_mbswidth(s);
Packit 991819
			break;
Packit 991819
Packit 991819
		case 'c':
Packit 991819
			flags &= ~FL_DOT;
Packit 991819
			c = (char)(VA(int));
Packit 991819
			/* FALLTHROUGH */
Packit 991819
Packit 991819
		case '%':
Packit 991819
		default:
Packit 991819
			numbuf[0] = c;
Packit 991819
			numbuf[1] = 0;
Packit 991819
			s = numbuf;
Packit 991819
			len = 1;
Packit 991819
			break;
Packit 991819
		}
Packit 991819
Packit 991819
		/*
Packit 991819
		 * At this point s should point to a string that is to be
Packit 991819
		 * formatted, and len should be the length of the string.
Packit 991819
		 */
Packit 991819
		if (!(flags & FL_DOT) || len < precision)
Packit 991819
			precision = len;
Packit 991819
		if (field > precision) {
Packit 991819
			field -= precision;
Packit 991819
			if (!(flags & FL_RIGHT)) {
Packit 991819
				/* skip past sign or 0x when padding with 0 */
Packit 991819
				if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
Packit 991819
					if (ctype(*s, C_SPC | C_PLUS | C_MINUS)) {
Packit 991819
						shf_putc(*s, shf);
Packit 991819
						s++;
Packit 991819
						precision--;
Packit 991819
						nwritten++;
Packit 991819
					} else if (*s == '0') {
Packit 991819
						shf_putc(*s, shf);
Packit 991819
						s++;
Packit 991819
						nwritten++;
Packit 991819
						if (--precision &&
Packit 991819
						    ksh_eq(*s, 'X', 'x')) {
Packit 991819
							shf_putc(*s, shf);
Packit 991819
							s++;
Packit 991819
							precision--;
Packit 991819
							nwritten++;
Packit 991819
						}
Packit 991819
					}
Packit 991819
					c = '0';
Packit 991819
				} else
Packit 991819
					c = flags & FL_ZERO ? '0' : ' ';
Packit 991819
				nwritten += field;
Packit 991819
				while (field--)
Packit 991819
					shf_putc(c, shf);
Packit 991819
				field = 0;
Packit 991819
			} else
Packit 991819
				c = ' ';
Packit 991819
		} else
Packit 991819
			field = 0;
Packit 991819
Packit 991819
		nwritten += precision;
Packit 991819
		precision = utf_skipcols(s, precision, &tmp) - s;
Packit 991819
		while (precision--)
Packit 991819
			shf_putc(*s++, shf);
Packit 991819
Packit 991819
		nwritten += field;
Packit 991819
		while (field--)
Packit 991819
			shf_putc(c, shf);
Packit 991819
	}
Packit 991819
Packit 991819
	return (shf_error(shf) ? -1 : nwritten);
Packit 991819
}
Packit 991819
Packit 991819
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
Packit 991819
int
Packit 991819
shf_getc(struct shf *shf)
Packit 991819
{
Packit 991819
	return (shf_getc_i(shf));
Packit 991819
}
Packit 991819
Packit 991819
int
Packit 991819
shf_putc(int c, struct shf *shf)
Packit 991819
{
Packit 991819
	return (shf_putc_i(c, shf));
Packit 991819
}
Packit 991819
#endif
Packit 991819
Packit 991819
#ifdef DEBUG
Packit 991819
const char *
Packit 991819
cstrerror(int errnum)
Packit 991819
{
Packit 991819
#undef strerror
Packit 991819
	return (strerror(errnum));
Packit 991819
#define strerror dontuse_strerror /* poisoned */
Packit 991819
}
Packit 991819
#elif !HAVE_STRERROR
Packit 991819
Packit 991819
#if HAVE_SYS_ERRLIST
Packit 991819
#if !HAVE_SYS_ERRLIST_DECL
Packit 991819
extern const int sys_nerr;
Packit 991819
extern const char * const sys_errlist[];
Packit 991819
#endif
Packit 991819
#endif
Packit 991819
Packit 991819
const char *
Packit 991819
cstrerror(int errnum)
Packit 991819
{
Packit 991819
	/* "Unknown error: " + sign + rough estimate + NUL */
Packit 991819
	static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
Packit 991819
Packit 991819
#if HAVE_SYS_ERRLIST
Packit 991819
	if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
Packit 991819
		return (sys_errlist[errnum]);
Packit 991819
#endif
Packit 991819
Packit 991819
	switch (errnum) {
Packit 991819
	case 0:
Packit 991819
		return ("Undefined error: 0");
Packit 991819
	case EPERM:
Packit 991819
		return ("Operation not permitted");
Packit 991819
	case ENOENT:
Packit 991819
		return ("No such file or directory");
Packit 991819
#ifdef ESRCH
Packit 991819
	case ESRCH:
Packit 991819
		return ("No such process");
Packit 991819
#endif
Packit 991819
#ifdef E2BIG
Packit 991819
	case E2BIG:
Packit 991819
		return ("Argument list too long");
Packit 991819
#endif
Packit 991819
	case ENOEXEC:
Packit 991819
		return ("Exec format error");
Packit 991819
	case EBADF:
Packit 991819
		return ("Bad file descriptor");
Packit 991819
#ifdef ENOMEM
Packit 991819
	case ENOMEM:
Packit 991819
		return ("Cannot allocate memory");
Packit 991819
#endif
Packit 991819
	case EACCES:
Packit 991819
		return ("Permission denied");
Packit 991819
	case EEXIST:
Packit 991819
		return ("File exists");
Packit 991819
	case ENOTDIR:
Packit 991819
		return ("Not a directory");
Packit 991819
#ifdef EINVAL
Packit 991819
	case EINVAL:
Packit 991819
		return ("Invalid argument");
Packit 991819
#endif
Packit 991819
#ifdef ELOOP
Packit 991819
	case ELOOP:
Packit 991819
		return ("Too many levels of symbolic links");
Packit 991819
#endif
Packit 991819
	default:
Packit 991819
		shf_snprintf(errbuf, sizeof(errbuf),
Packit 991819
		    "Unknown error: %d", errnum);
Packit 991819
		return (errbuf);
Packit 991819
	}
Packit 991819
}
Packit 991819
#endif
Packit 991819
Packit 991819
/* fast character classes */
Packit 991819
const uint32_t tpl_ctypes[128] = {
Packit 991819
	/* 0x00 */
Packit 991819
	CiNUL,		CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	CiCNTRL,	CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	CiCNTRL,	CiTAB,		CiNL,		CiSPX,
Packit 991819
	CiSPX,		CiCR,		CiCNTRL,	CiCNTRL,
Packit 991819
	/* 0x10 */
Packit 991819
	CiCNTRL,	CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	CiCNTRL,	CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	CiCNTRL,	CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	CiCNTRL,	CiCNTRL,	CiCNTRL,	CiCNTRL,
Packit 991819
	/* 0x20 */
Packit 991819
	CiSP,		CiALIAS | CiVAR1,	CiQC,	CiHASH,
Packit 991819
	CiSS,		CiPERCT,	CiQCL,		CiQC,
Packit 991819
	CiQCL,		CiQCL,		CiQCX | CiVAR1,	CiPLUS,
Packit 991819
	CiALIAS,	CiMINUS,	CiALIAS,	CiQCM,
Packit 991819
	/* 0x30 */
Packit 991819
	CiOCTAL,	CiOCTAL,	CiOCTAL,	CiOCTAL,
Packit 991819
	CiOCTAL,	CiOCTAL,	CiOCTAL,	CiOCTAL,
Packit 991819
	CiDIGIT,	CiDIGIT,	CiCOLON,	CiQCL,
Packit 991819
	CiANGLE,	CiEQUAL,	CiANGLE,	CiQUEST,
Packit 991819
	/* 0x40 */
Packit 991819
	CiALIAS | CiVAR1,	CiUPPER | CiHEXLT,
Packit 991819
	CiUPPER | CiHEXLT,	CiUPPER | CiHEXLT,
Packit 991819
	CiUPPER | CiHEXLT,	CiUPPER | CiHEXLT,
Packit 991819
	CiUPPER | CiHEXLT,	CiUPPER,
Packit 991819
	CiUPPER,	CiUPPER,	CiUPPER,	CiUPPER,
Packit 991819
	CiUPPER,	CiUPPER,	CiUPPER,	CiUPPER,
Packit 991819
	/* 0x50 */
Packit 991819
	CiUPPER,	CiUPPER,	CiUPPER,	CiUPPER,
Packit 991819
	CiUPPER,	CiUPPER,	CiUPPER,	CiUPPER,
Packit 991819
	CiUPPER,	CiUPPER,	CiUPPER,	CiQCX | CiBRACK,
Packit 991819
	CiQCX,		CiBRACK,	CiQCM,		CiUNDER,
Packit 991819
	/* 0x60 */
Packit 991819
	CiGRAVE,		CiLOWER | CiHEXLT,
Packit 991819
	CiLOWER | CiHEXLT,	CiLOWER | CiHEXLT,
Packit 991819
	CiLOWER | CiHEXLT,	CiLOWER | CiHEXLT,
Packit 991819
	CiLOWER | CiHEXLT,	CiLOWER,
Packit 991819
	CiLOWER,	CiLOWER,	CiLOWER,	CiLOWER,
Packit 991819
	CiLOWER,	CiLOWER,	CiLOWER,	CiLOWER,
Packit 991819
	/* 0x70 */
Packit 991819
	CiLOWER,	CiLOWER,	CiLOWER,	CiLOWER,
Packit 991819
	CiLOWER,	CiLOWER,	CiLOWER,	CiLOWER,
Packit 991819
	CiLOWER,	CiLOWER,	CiLOWER,	CiCURLY,
Packit 991819
	CiQCL,		CiCURLY,	CiQCM,		CiCNTRL
Packit 991819
};
Packit 991819
Packit 991819
void
Packit 991819
set_ifs(const char *s)
Packit 991819
{
Packit 991819
#if defined(MKSH_EBCDIC) || defined(MKSH_FAUX_EBCDIC)
Packit 991819
	int i = 256;
Packit 991819
Packit 991819
	memset(ksh_ctypes, 0, sizeof(ksh_ctypes));
Packit 991819
	while (i--)
Packit 991819
		if (ebcdic_map[i] < 0x80U)
Packit 991819
			ksh_ctypes[i] = tpl_ctypes[ebcdic_map[i]];
Packit 991819
#else
Packit 991819
	memcpy(ksh_ctypes, tpl_ctypes, sizeof(tpl_ctypes));
Packit 991819
	memset((char *)ksh_ctypes + sizeof(tpl_ctypes), '\0',
Packit 991819
	    sizeof(ksh_ctypes) - sizeof(tpl_ctypes));
Packit 991819
#endif
Packit 991819
	ifs0 = *s;
Packit 991819
	while (*s)
Packit 991819
		ksh_ctypes[ord(*s++)] |= CiIFS;
Packit 991819
}
Packit 991819
Packit 991819
#if defined(MKSH_EBCDIC) || defined(MKSH_FAUX_EBCDIC)
Packit 991819
#include <locale.h>
Packit 991819
Packit 991819
/*
Packit 991819
 * Many headaches with EBCDIC:
Packit 991819
 * 1. There are numerous EBCDIC variants, and it is not feasible for us
Packit 991819
 *    to support them all. But we can support the EBCDIC code pages that
Packit 991819
 *    contain all (most?) of the characters in ASCII, and these
Packit 991819
 *    usually tend to agree on the code points assigned to the ASCII
Packit 991819
 *    subset. If you need a representative example, look at EBCDIC 1047,
Packit 991819
 *    which is first among equals in the IBM MVS development
Packit 991819
 *    environment: https://en.wikipedia.org/wiki/EBCDIC_1047
Packit 991819
 *    Unfortunately, the square brackets are not consistently mapped,
Packit 991819
 *    and for certain reasons, we need an unambiguous bijective
Packit 991819
 *    mapping between EBCDIC and "extended ASCII".
Packit 991819
 * 2. Character ranges that are contiguous in ASCII, like the letters
Packit 991819
 *    in [A-Z], are broken up into segments (i.e. [A-IJ-RS-Z]), so we
Packit 991819
 *    can't implement e.g. islower() as { return c >= 'a' && c <= 'z'; }
Packit 991819
 *    because it will also return true for a handful of extraneous
Packit 991819
 *    characters (like the plus-minus sign at 0x8F in EBCDIC 1047, a
Packit 991819
 *    little after 'i'). But at least '_' is not one of these.
Packit 991819
 * 3. The normal [0-9A-Za-z] characters are at codepoints beyond 0x80.
Packit 991819
 *    Not only do they require all 8 bits instead of 7, if chars are
Packit 991819
 *    signed, they will have negative integer values! Something like
Packit 991819
 *    (c - 'A') could actually become (c + 63)! Use the ord() macro to
Packit 991819
 *    ensure you're getting a value in [0, 255] (ORD for constants).
Packit 991819
 * 4. '\n' is actually NL (0x15, U+0085) instead of LF (0x25, U+000A).
Packit 991819
 *    EBCDIC has a proper newline character instead of "emulating" one
Packit 991819
 *    with line feeds, although this is mapped to LF for our purposes.
Packit 991819
 * 5. Note that it is possible to compile programs in ASCII mode on IBM
Packit 991819
 *    mainframe systems, using the -qascii option to the XL C compiler.
Packit 991819
 *    We can determine the build mode by looking at __CHARSET_LIB:
Packit 991819
 *    0 == EBCDIC, 1 == ASCII
Packit 991819
 */
Packit 991819
Packit 991819
void
Packit 991819
ebcdic_init(void)
Packit 991819
{
Packit 991819
	int i = 256;
Packit 991819
	unsigned char t;
Packit 991819
	bool mapcache[256];
Packit 991819
Packit 991819
	while (i--)
Packit 991819
		ebcdic_rtt_toascii[i] = i;
Packit 991819
	memset(ebcdic_rtt_fromascii, 0xFF, sizeof(ebcdic_rtt_fromascii));
Packit 991819
	setlocale(LC_ALL, "");
Packit 991819
#ifdef MKSH_EBCDIC
Packit 991819
	if (__etoa_l(ebcdic_rtt_toascii, 256) != 256) {
Packit 991819
		write(2, "mksh: could not map EBCDIC to ASCII\n", 36);
Packit 991819
		exit(255);
Packit 991819
	}
Packit 991819
#endif
Packit 991819
Packit 991819
	memset(mapcache, 0, sizeof(mapcache));
Packit 991819
	i = 256;
Packit 991819
	while (i--) {
Packit 991819
		t = ebcdic_rtt_toascii[i];
Packit 991819
		/* ensure unique round-trip capable mapping */
Packit 991819
		if (mapcache[t]) {
Packit 991819
			write(2, "mksh: duplicate EBCDIC to ASCII mapping\n", 40);
Packit 991819
			exit(255);
Packit 991819
		}
Packit 991819
		/*
Packit 991819
		 * since there are 256 input octets, this also ensures
Packit 991819
		 * the other mapping direction is completely filled
Packit 991819
		 */
Packit 991819
		mapcache[t] = true;
Packit 991819
		/* fill the complete round-trip map */
Packit 991819
		ebcdic_rtt_fromascii[t] = i;
Packit 991819
		/*
Packit 991819
		 * Only use the converted value if it's in the range
Packit 991819
		 * [0x00; 0x7F], which I checked; the "extended ASCII"
Packit 991819
		 * characters can be any encoding, not just Latin1,
Packit 991819
		 * and the C1 control characters other than NEL are
Packit 991819
		 * hopeless, but we map EBCDIC NEL to ASCII LF so we
Packit 991819
		 * cannot even use C1 NEL.
Packit 991819
		 * If ever we map to Unicode, bump the table width to
Packit 991819
		 * an unsigned int, and or the raw unconverted EBCDIC
Packit 991819
		 * values with 0x01000000 instead.
Packit 991819
		 */
Packit 991819
		if (t < 0x80U)
Packit 991819
			ebcdic_map[i] = (unsigned short)ord(t);
Packit 991819
		else
Packit 991819
			ebcdic_map[i] = (unsigned short)(0x100U | ord(i));
Packit 991819
	}
Packit 991819
	if (ebcdic_rtt_toascii[0] || ebcdic_rtt_fromascii[0] || ebcdic_map[0]) {
Packit 991819
		write(2, "mksh: NUL not at position 0\n", 28);
Packit 991819
		exit(255);
Packit 991819
	}
Packit 991819
}
Packit 991819
#endif