Deleted Added
full compact
output.c (275346) output.c (275766)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/output.c 275346 2014-11-30 20:12:47Z jilles $");
39__FBSDID("$FreeBSD: head/bin/sh/output.c 275766 2014-12-14 16:26:19Z jilles $");
40
41/*
42 * Shell output routines. We use our own output routines because:
43 * When a builtin command is interrupted we have to discard
44 * any pending output.
45 * When a builtin command appears in back quotes, we want to
46 * save the output of the command in a region obtained
47 * via malloc, rather than doing a fork and reading the
48 * output of the command via a pipe.
49 */
50
51#include <stdio.h> /* defines BUFSIZ */
52#include <string.h>
53#include <stdarg.h>
54#include <errno.h>
55#include <unistd.h>
56#include <stdlib.h>
40
41/*
42 * Shell output routines. We use our own output routines because:
43 * When a builtin command is interrupted we have to discard
44 * any pending output.
45 * When a builtin command appears in back quotes, we want to
46 * save the output of the command in a region obtained
47 * via malloc, rather than doing a fork and reading the
48 * output of the command via a pipe.
49 */
50
51#include <stdio.h> /* defines BUFSIZ */
52#include <string.h>
53#include <stdarg.h>
54#include <errno.h>
55#include <unistd.h>
56#include <stdlib.h>
57#include <wchar.h>
58#include <wctype.h>
57
58#include "shell.h"
59#include "syntax.h"
60#include "output.h"
61#include "memalloc.h"
62#include "error.h"
63#include "var.h"
64

--- 41 unchanged lines hidden (view full) ---

106}
107
108void
109outstr(const char *p, struct output *file)
110{
111 outbin(p, strlen(p), file);
112}
113
59
60#include "shell.h"
61#include "syntax.h"
62#include "output.h"
63#include "memalloc.h"
64#include "error.h"
65#include "var.h"
66

--- 41 unchanged lines hidden (view full) ---

108}
109
110void
111outstr(const char *p, struct output *file)
112{
113 outbin(p, strlen(p), file);
114}
115
116static void
117byteseq(int ch, struct output *file)
118{
119 char seq[4];
120
121 seq[0] = '\\';
122 seq[1] = (ch >> 6 & 0x3) + '0';
123 seq[2] = (ch >> 3 & 0x7) + '0';
124 seq[3] = (ch & 0x7) + '0';
125 outbin(seq, 4, file);
126}
127
128static void
129outdqstr(const char *p, struct output *file)
130{
131 const char *end;
132 mbstate_t mbs;
133 size_t clen;
134 wchar_t wc;
135
136 memset(&mbs, '\0', sizeof(mbs));
137 end = p + strlen(p);
138 outstr("$'", file);
139 while ((clen = mbrtowc(&wc, p, end - p + 1, &mbs)) != 0) {
140 if (clen == (size_t)-2) {
141 while (p < end)
142 byteseq(*p++, file);
143 break;
144 }
145 if (clen == (size_t)-1) {
146 memset(&mbs, '\0', sizeof(mbs));
147 byteseq(*p++, file);
148 continue;
149 }
150 if (wc == L'\n')
151 outcslow('\n', file), p++;
152 else if (wc == L'\r')
153 outstr("\\r", file), p++;
154 else if (wc == L'\t')
155 outstr("\\t", file), p++;
156 else if (!iswprint(wc)) {
157 for (; clen > 0; clen--)
158 byteseq(*p++, file);
159 } else {
160 if (wc == L'\'' || wc == L'\\')
161 outcslow('\\', file);
162 outbin(p, clen, file);
163 p += clen;
164 }
165 }
166 outcslow('\'', file);
167}
168
114/* Like outstr(), but quote for re-input into the shell. */
115void
116outqstr(const char *p, struct output *file)
117{
169/* Like outstr(), but quote for re-input into the shell. */
170void
171outqstr(const char *p, struct output *file)
172{
118 char ch;
119 int inquotes;
173 int i;
120
121 if (p[0] == '\0') {
122 outstr("''", file);
123 return;
124 }
174
175 if (p[0] == '\0') {
176 outstr("''", file);
177 return;
178 }
125 if (p[strcspn(p, "|&;<>()$`\\\"' \t\n*?[~#=")] == '\0' ||
179 for (i = 0; p[i] != '\0'; i++) {
180 if ((p[i] > '\0' && p[i] < ' ' && p[i] != '\n') ||
181 (p[i] & 0x80) != 0 || p[i] == '\'') {
182 outdqstr(p, file);
183 return;
184 }
185 }
186
187 if (p[strcspn(p, "|&;<>()$`\\\" \n*?[~#=")] == '\0' ||
126 strcmp(p, "[") == 0) {
127 outstr(p, file);
128 return;
129 }
130
188 strcmp(p, "[") == 0) {
189 outstr(p, file);
190 return;
191 }
192
131 inquotes = 0;
132 while ((ch = *p++) != '\0') {
133 switch (ch) {
134 case '\'':
135 /* Can't quote single quotes inside single quotes. */
136 if (inquotes)
137 outcslow('\'', file);
138 inquotes = 0;
139 outstr("\\'", file);
140 break;
141 default:
142 if (!inquotes)
143 outcslow('\'', file);
144 inquotes = 1;
145 outc(ch, file);
146 }
147 }
148 if (inquotes)
149 outcslow('\'', file);
193 outcslow('\'', file);
194 outstr(p, file);
195 outcslow('\'', file);
150}
151
152void
153outbin(const void *data, size_t len, struct output *file)
154{
155 const char *p;
156
157 p = data;

--- 172 unchanged lines hidden ---
196}
197
198void
199outbin(const void *data, size_t len, struct output *file)
200{
201 const char *p;
202
203 p = data;

--- 172 unchanged lines hidden ---