1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-26 06:18:55 +00:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 06:18:55 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Shell output routines. We use our own output routines because:
|
|
|
|
* When a builtin command is interrupted we have to discard
|
|
|
|
* any pending output.
|
|
|
|
* When a builtin command appears in back quotes, we want to
|
|
|
|
* save the output of the command in a region obtained
|
|
|
|
* via malloc, rather than doing a fork and reading the
|
|
|
|
* output of the command via a pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h> /* defines BUFSIZ */
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <errno.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2014-12-14 16:26:19 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
2002-06-06 03:29:23 +00:00
|
|
|
#include "var.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define OUTBUFSIZ BUFSIZ
|
2010-12-11 17:47:27 +00:00
|
|
|
#define MEM_OUT -2 /* output to dynamically allocated memory */
|
1994-05-26 06:18:55 +00:00
|
|
|
#define OUTPUT_ERR 01 /* error occurred on output */
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int doformat_wr(void *, const char *, int);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2017-05-16 21:54:51 +00:00
|
|
|
struct output output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0};
|
|
|
|
struct output errout = {NULL, NULL, NULL, 256, 2, 0};
|
2017-05-18 21:44:14 +00:00
|
|
|
struct output memout = {NULL, NULL, NULL, 64, MEM_OUT, 0};
|
1994-05-26 06:18:55 +00:00
|
|
|
struct output *out1 = &output;
|
|
|
|
struct output *out2 = &errout;
|
|
|
|
|
2010-11-20 14:14:52 +00:00
|
|
|
void
|
|
|
|
outcslow(int c, struct output *file)
|
|
|
|
{
|
|
|
|
outc(c, file);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
out1str(const char *p)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
outstr(p, out1);
|
|
|
|
}
|
|
|
|
|
2002-06-04 12:59:12 +00:00
|
|
|
void
|
|
|
|
out1qstr(const char *p)
|
|
|
|
{
|
|
|
|
outqstr(p, out1);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
out2str(const char *p)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
outstr(p, out2);
|
|
|
|
}
|
|
|
|
|
2002-06-04 12:59:12 +00:00
|
|
|
void
|
|
|
|
out2qstr(const char *p)
|
|
|
|
{
|
|
|
|
outqstr(p, out2);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
outstr(const char *p, struct output *file)
|
|
|
|
{
|
2010-11-14 15:31:59 +00:00
|
|
|
outbin(p, strlen(p), file);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-14 16:26:19 +00:00
|
|
|
static void
|
|
|
|
byteseq(int ch, struct output *file)
|
|
|
|
{
|
|
|
|
char seq[4];
|
|
|
|
|
|
|
|
seq[0] = '\\';
|
|
|
|
seq[1] = (ch >> 6 & 0x3) + '0';
|
|
|
|
seq[2] = (ch >> 3 & 0x7) + '0';
|
|
|
|
seq[3] = (ch & 0x7) + '0';
|
|
|
|
outbin(seq, 4, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
outdqstr(const char *p, struct output *file)
|
|
|
|
{
|
|
|
|
const char *end;
|
|
|
|
mbstate_t mbs;
|
|
|
|
size_t clen;
|
|
|
|
wchar_t wc;
|
|
|
|
|
|
|
|
memset(&mbs, '\0', sizeof(mbs));
|
|
|
|
end = p + strlen(p);
|
|
|
|
outstr("$'", file);
|
|
|
|
while ((clen = mbrtowc(&wc, p, end - p + 1, &mbs)) != 0) {
|
|
|
|
if (clen == (size_t)-2) {
|
|
|
|
while (p < end)
|
|
|
|
byteseq(*p++, file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (clen == (size_t)-1) {
|
|
|
|
memset(&mbs, '\0', sizeof(mbs));
|
|
|
|
byteseq(*p++, file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (wc == L'\n')
|
|
|
|
outcslow('\n', file), p++;
|
|
|
|
else if (wc == L'\r')
|
|
|
|
outstr("\\r", file), p++;
|
|
|
|
else if (wc == L'\t')
|
|
|
|
outstr("\\t", file), p++;
|
|
|
|
else if (!iswprint(wc)) {
|
|
|
|
for (; clen > 0; clen--)
|
|
|
|
byteseq(*p++, file);
|
|
|
|
} else {
|
|
|
|
if (wc == L'\'' || wc == L'\\')
|
|
|
|
outcslow('\\', file);
|
|
|
|
outbin(p, clen, file);
|
|
|
|
p += clen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outcslow('\'', file);
|
|
|
|
}
|
|
|
|
|
2002-06-04 12:59:12 +00:00
|
|
|
/* Like outstr(), but quote for re-input into the shell. */
|
|
|
|
void
|
|
|
|
outqstr(const char *p, struct output *file)
|
|
|
|
{
|
2014-12-14 16:26:19 +00:00
|
|
|
int i;
|
2002-06-04 12:59:12 +00:00
|
|
|
|
2005-12-08 21:00:39 +00:00
|
|
|
if (p[0] == '\0') {
|
|
|
|
outstr("''", file);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-14 16:26:19 +00:00
|
|
|
for (i = 0; p[i] != '\0'; i++) {
|
|
|
|
if ((p[i] > '\0' && p[i] < ' ' && p[i] != '\n') ||
|
|
|
|
(p[i] & 0x80) != 0 || p[i] == '\'') {
|
|
|
|
outdqstr(p, file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p[strcspn(p, "|&;<>()$`\\\" \n*?[~#=")] == '\0' ||
|
2009-06-19 22:09:55 +00:00
|
|
|
strcmp(p, "[") == 0) {
|
2002-06-06 03:29:23 +00:00
|
|
|
outstr(p, file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-14 16:26:19 +00:00
|
|
|
outcslow('\'', file);
|
|
|
|
outstr(p, file);
|
|
|
|
outcslow('\'', file);
|
2002-06-04 12:59:12 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-11-14 15:31:59 +00:00
|
|
|
void
|
|
|
|
outbin(const void *data, size_t len, struct output *file)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
while (len-- > 0)
|
|
|
|
outc(*p++, file);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
emptyoutbuf(struct output *dest)
|
|
|
|
{
|
2017-05-18 21:44:14 +00:00
|
|
|
int offset, newsize;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-12-11 17:47:27 +00:00
|
|
|
if (dest->buf == NULL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
|
|
|
dest->buf = ckmalloc(dest->bufsize);
|
|
|
|
dest->nextc = dest->buf;
|
2017-05-16 21:54:51 +00:00
|
|
|
dest->bufend = dest->buf + dest->bufsize;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
} else if (dest->fd == MEM_OUT) {
|
2017-05-16 21:54:51 +00:00
|
|
|
offset = dest->nextc - dest->buf;
|
2017-05-18 21:44:14 +00:00
|
|
|
newsize = dest->bufsize << 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2017-05-18 21:44:14 +00:00
|
|
|
dest->buf = ckrealloc(dest->buf, newsize);
|
|
|
|
dest->bufsize = newsize;
|
|
|
|
dest->bufend = dest->buf + newsize;
|
1994-05-26 06:18:55 +00:00
|
|
|
dest->nextc = dest->buf + offset;
|
|
|
|
INTON;
|
|
|
|
} else {
|
|
|
|
flushout(dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
flushall(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
flushout(&output);
|
|
|
|
flushout(&errout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
flushout(struct output *dest)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
|
|
|
|
return;
|
|
|
|
if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
|
|
|
|
dest->flags |= OUTPUT_ERR;
|
|
|
|
dest->nextc = dest->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
freestdout(void)
|
|
|
|
{
|
2017-05-18 22:10:04 +00:00
|
|
|
output.nextc = output.buf;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-12 22:01:10 +00:00
|
|
|
int
|
|
|
|
outiserror(struct output *file)
|
|
|
|
{
|
|
|
|
return (file->flags & OUTPUT_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
outclearerror(struct output *file)
|
|
|
|
{
|
|
|
|
file->flags &= ~OUTPUT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
outfmt(struct output *file, const char *fmt, ...)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
doformat(file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
out1fmt(const char *fmt, ...)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
doformat(out1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-21 14:28:32 +00:00
|
|
|
out2fmt_flush(const char *fmt, ...)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
doformat(out2, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
flushout(out2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
fmtstr(char *outbuf, int length, const char *fmt, ...)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2010-12-11 17:47:27 +00:00
|
|
|
INTOFF;
|
1994-05-26 06:18:55 +00:00
|
|
|
va_start(ap, fmt);
|
2010-12-11 17:47:27 +00:00
|
|
|
vsnprintf(outbuf, length, fmt, ap);
|
2002-10-01 13:22:12 +00:00
|
|
|
va_end(ap);
|
2010-12-11 17:47:27 +00:00
|
|
|
INTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-10-01 13:22:12 +00:00
|
|
|
doformat_wr(void *cookie, const char *buf, int len)
|
|
|
|
{
|
|
|
|
struct output *o;
|
|
|
|
|
|
|
|
o = (struct output *)cookie;
|
2010-11-14 15:31:59 +00:00
|
|
|
outbin(buf, len, o);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-11-14 15:31:59 +00:00
|
|
|
return (len);
|
2002-10-01 13:22:12 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
doformat(struct output *dest, const char *f, va_list ap)
|
|
|
|
{
|
2002-10-01 13:22:12 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fwopen(dest, doformat_wr)) != NULL) {
|
|
|
|
vfprintf(fp, f, ap);
|
|
|
|
fclose(fp);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:27:30 +00:00
|
|
|
FILE *
|
|
|
|
out1fp(void)
|
|
|
|
{
|
|
|
|
return fwopen(out1, doformat_wr);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Version of write which resumes after a signal is caught.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
xwrite(int fd, const char *buf, int nbytes)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int ntry;
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = nbytes;
|
|
|
|
ntry = 0;
|
|
|
|
for (;;) {
|
|
|
|
i = write(fd, buf, n);
|
|
|
|
if (i > 0) {
|
|
|
|
if ((n -= i) <= 0)
|
|
|
|
return nbytes;
|
|
|
|
buf += i;
|
|
|
|
ntry = 0;
|
|
|
|
} else if (i == 0) {
|
|
|
|
if (++ntry > 10)
|
|
|
|
return nbytes - n;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|