blob: 9663894ec824f5fc0478f67a418ddbd415d92461 [file] [log] [blame]
Paul Fox6ab03782006-06-08 21:37:26 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Paul Fox6ab03782006-06-08 21:37:26 +00003 * echo implementation for busybox
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Paul Fox6ab03782006-06-08 21:37:26 +00005 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
Erik Andersen13456d12000-03-16 08:09:57 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen13456d12000-03-16 08:09:57 +00009 *
Paul Fox6ab03782006-06-08 21:37:26 +000010 * Original copyright notice is retained at the end of this file.
Manuel Novoa III cad53642003-03-19 09:13:01 +000011 */
12
Paul Fox6ab03782006-06-08 21:37:26 +000013/* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
14/* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
15
16/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
17 *
18 * Because of behavioral differences, implemented configurable SUSv3
19 * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
20 * 1) In handling '\c' escape, the previous version only suppressed the
21 * trailing newline. SUSv3 specifies _no_ output after '\c'.
22 * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
Denis Vlasenko7e754f12007-04-09 13:04:50 +000023 * The previous version did not allow 4-digit octals.
Paul Fox6ab03782006-06-08 21:37:26 +000024 */
25
Pere Orga34425382011-03-31 14:43:25 +020026//usage:#define echo_trivial_usage
27//usage: IF_FEATURE_FANCY_ECHO("[-neE] ") "[ARG]..."
28//usage:#define echo_full_usage "\n\n"
29//usage: "Print the specified ARGs to stdout"
30//usage: IF_FEATURE_FANCY_ECHO( "\n"
Pere Orga34425382011-03-31 14:43:25 +020031//usage: "\n -n Suppress trailing newline"
32//usage: "\n -e Interpret backslash escapes (i.e., \\t=tab)"
33//usage: "\n -E Don't interpret backslash escapes (default)"
34//usage: )
35//usage:
36//usage:#define echo_example_usage
37//usage: "$ echo \"Erik is cool\"\n"
38//usage: "Erik is cool\n"
39//usage: IF_FEATURE_FANCY_ECHO("$ echo -e \"Erik\\nis\\ncool\"\n"
40//usage: "Erik\n"
41//usage: "is\n"
42//usage: "cool\n"
43//usage: "$ echo \"Erik\\nis\\ncool\"\n"
44//usage: "Erik\\nis\\ncool\n")
45
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000046#include "libbb.h"
Erik Andersen13456d12000-03-16 08:09:57 +000047
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +000048/* This is a NOFORK applet. Be very careful! */
49
Denis Vlasenko468aea22008-04-01 14:47:57 +000050/* NB: can be used by shell even if not enabled as applet */
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +000051
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +010052/*
53 * NB2: we don't use stdio, we need better error handing.
54 * Examples include writing into non-opened stdout and error on write.
55 *
56 * With stdio, output gets shoveled into stdout buffer, and even
57 * fflush cannot clear it out. It seems that even if libc receives
58 * EBADF on write attempts, it feels determined to output data no matter what.
59 * If echo is called by shell, it will try writing again later, and possibly
60 * will clobber future output. Not good.
61 *
62 * Solaris has fpurge which discards buffered input. glibc has __fpurge.
63 * But this function is not standard.
64 */
65
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000066int echo_main(int argc UNUSED_PARAM, char **argv)
Paul Fox6ab03782006-06-08 21:37:26 +000067{
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +010068 char **pp;
Denis Vlasenko7e754f12007-04-09 13:04:50 +000069 const char *arg;
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +010070 char *out;
71 char *buffer;
72 unsigned buflen;
Denis Vlasenko7e754f12007-04-09 13:04:50 +000073#if !ENABLE_FEATURE_FANCY_ECHO
Denis Vlasenkoa9d7d242007-04-10 16:34:00 +000074 enum {
75 eflag = '\\',
76 nflag = 1, /* 1 -- print '\n' */
77 };
Denis Vlasenko7d75a962007-11-22 08:16:57 +000078
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +010079 argv++;
Paul Fox6ab03782006-06-08 21:37:26 +000080#else
Denis Vlasenko7e754f12007-04-09 13:04:50 +000081 char nflag = 1;
82 char eflag = 0;
Paul Fox6ab03782006-06-08 21:37:26 +000083
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +010084 while ((arg = *++argv) != NULL) {
Denys Vlasenkob9348442011-02-14 15:42:18 +010085 char n, e;
86
87 if (arg[0] != '-')
88 break; /* not an option arg, echo it */
Denis Vlasenko7e754f12007-04-09 13:04:50 +000089
Paul Fox6ab03782006-06-08 21:37:26 +000090 /* If it appears that we are handling options, then make sure
91 * that all of the options specified are actually valid.
92 * Otherwise, the string should just be echoed.
93 */
Denys Vlasenkob9348442011-02-14 15:42:18 +010094 arg++;
95 n = nflag;
96 e = eflag;
Paul Fox6ab03782006-06-08 21:37:26 +000097 do {
Denys Vlasenkob9348442011-02-14 15:42:18 +010098 if (*arg == 'n')
99 n = 0;
100 else if (*arg == 'e')
101 e = '\\';
102 else if (*arg != 'E') {
103 /* "-ccc" arg with one of c's invalid, echo it */
104 /* arg consisting from just "-" also handled here */
Paul Fox6ab03782006-06-08 21:37:26 +0000105 goto just_echo;
Denys Vlasenkob9348442011-02-14 15:42:18 +0100106 }
107 } while (*++arg);
108 nflag = n;
109 eflag = e;
Paul Fox6ab03782006-06-08 21:37:26 +0000110 }
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000111 just_echo:
Paul Fox6ab03782006-06-08 21:37:26 +0000112#endif
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100113
Denys Vlasenkob9348442011-02-14 15:42:18 +0100114 buflen = 0;
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100115 pp = argv;
116 while ((arg = *pp) != NULL) {
117 buflen += strlen(arg) + 1;
118 pp++;
119 }
Denys Vlasenko4fdb67c2011-02-15 18:35:54 +0100120 out = buffer = xmalloc(buflen + 1); /* +1 is needed for "no args" case */
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100121
122 while ((arg = *argv) != NULL) {
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000123 int c;
Paul Fox6ab03782006-06-08 21:37:26 +0000124
Denis Vlasenkoa9d7d242007-04-10 16:34:00 +0000125 if (!eflag) {
126 /* optimization for very common case */
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100127 out = stpcpy(out, arg);
Denys Vlasenkob9348442011-02-14 15:42:18 +0100128 } else
129 while ((c = *arg++) != '\0') {
130 if (c == eflag) {
131 /* This is an "\x" sequence */
132
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000133 if (*arg == 'c') {
Denys Vlasenkob9348442011-02-14 15:42:18 +0100134 /* "\c" means cancel newline and
Paul Fox6ab03782006-06-08 21:37:26 +0000135 * ignore all subsequent chars. */
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100136 goto do_write;
Paul Fox6ab03782006-06-08 21:37:26 +0000137 }
Denys Vlasenkob9348442011-02-14 15:42:18 +0100138 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
139 * of the form \0### are accepted. */
140 if (*arg == '0') {
141 if ((unsigned char)(arg[1] - '0') < 8) {
142 /* 2nd char is 0..7: skip leading '0' */
143 arg++;
Paul Fox6ab03782006-06-08 21:37:26 +0000144 }
Denys Vlasenkob9348442011-02-14 15:42:18 +0100145 }
146 /* bb_process_escape_sequence handles NUL correctly
147 * ("...\" case). */
148 {
149 /* optimization: don't force arg to be on-stack,
150 * use another variable for that. ~30 bytes win */
151 const char *z = arg;
152 c = bb_process_escape_sequence(&z);
153 arg = z;
Paul Fox6ab03782006-06-08 21:37:26 +0000154 }
155 }
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100156 *out++ = c;
Paul Fox6ab03782006-06-08 21:37:26 +0000157 }
158
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100159 if (!*++argv)
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000160 break;
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100161 *out++ = ' ';
Paul Fox6ab03782006-06-08 21:37:26 +0000162 }
163
Paul Fox6ab03782006-06-08 21:37:26 +0000164 if (nflag) {
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100165 *out++ = '\n';
Paul Fox6ab03782006-06-08 21:37:26 +0000166 }
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100167
168 do_write:
Denys Vlasenkob9348442011-02-14 15:42:18 +0100169 /* Careful to error out on partial writes too (think ENOSPC!) */
170 errno = 0;
171 /*r =*/ full_write(STDOUT_FILENO, buffer, out - buffer);
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100172 free(buffer);
Denys Vlasenkob9348442011-02-14 15:42:18 +0100173 if (/*WRONG:r < 0*/ errno) {
Denys Vlasenko8ee2ada2011-02-07 02:03:51 +0100174 bb_perror_msg(bb_msg_write_error);
175 return 1;
176 }
177 return 0;
Paul Fox6ab03782006-06-08 21:37:26 +0000178}
179
Denys Vlasenkob9348442011-02-14 15:42:18 +0100180/*
Paul Fox6ab03782006-06-08 21:37:26 +0000181 * Copyright (c) 1991, 1993
182 * The Regents of the University of California. All rights reserved.
183 *
184 * This code is derived from software contributed to Berkeley by
185 * Kenneth Almquist.
186 *
187 * Redistribution and use in source and binary forms, with or without
188 * modification, are permitted provided that the following conditions
189 * are met:
190 * 1. Redistributions of source code must retain the above copyright
191 * notice, this list of conditions and the following disclaimer.
192 * 2. Redistributions in binary form must reproduce the above copyright
193 * notice, this list of conditions and the following disclaimer in the
194 * documentation and/or other materials provided with the distribution.
195 *
196 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
197 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
198 *
199 * California, Berkeley and its contributors.
200 * 4. Neither the name of the University nor the names of its contributors
201 * may be used to endorse or promote products derived from this software
202 * without specific prior written permission.
203 *
204 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
212 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
213 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
214 * SUCH DAMAGE.
215 *
216 * @(#)echo.c 8.1 (Berkeley) 5/31/93
217 */
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000218
219#ifdef VERSION_WITH_WRITEV
220/* We can't use stdio.
221 * The reason for this is highly non-obvious.
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000222 * echo_main is used from shell. Shell must correctly handle "echo foo"
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000223 * if stdout is closed. With stdio, output gets shoveled into
224 * stdout buffer, and even fflush cannot clear it out. It seems that
225 * even if libc receives EBADF on write attempts, it feels determined
226 * to output data no matter what. So it will try later,
227 * and possibly will clobber future output. Not good.
228 *
229 * Using writev instead, with 'direct' conversion of argv vector.
230 */
231
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000232int echo_main(int argc, char **argv)
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000233{
234 struct iovec io[argc];
235 struct iovec *cur_io = io;
236 char *arg;
237 char *p;
238#if !ENABLE_FEATURE_FANCY_ECHO
239 enum {
240 eflag = '\\',
241 nflag = 1, /* 1 -- print '\n' */
242 };
243 arg = *++argv;
244 if (!arg)
245 goto newline_ret;
246#else
247 char nflag = 1;
248 char eflag = 0;
249
250 while (1) {
251 arg = *++argv;
252 if (!arg)
253 goto newline_ret;
254 if (*arg != '-')
255 break;
256
257 /* If it appears that we are handling options, then make sure
258 * that all of the options specified are actually valid.
259 * Otherwise, the string should just be echoed.
260 */
261 p = arg + 1;
262 if (!*p) /* A single '-', so echo it. */
263 goto just_echo;
264
265 do {
Denys Vlasenkob9348442011-02-14 15:42:18 +0100266 if (!strchr("neE", *p))
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000267 goto just_echo;
268 } while (*++p);
269
270 /* All of the options in this arg are valid, so handle them. */
271 p = arg + 1;
272 do {
273 if (*p == 'n')
274 nflag = 0;
275 if (*p == 'e')
276 eflag = '\\';
277 } while (*++p);
278 }
279 just_echo:
280#endif
281
282 while (1) {
283 /* arg is already == *argv and isn't NULL */
284 int c;
285
286 cur_io->iov_base = p = arg;
287
288 if (!eflag) {
289 /* optimization for very common case */
290 p += strlen(arg);
291 } else while ((c = *arg++)) {
Denys Vlasenkob9348442011-02-14 15:42:18 +0100292 if (c == eflag) {
293 /* This is an "\x" sequence */
294
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000295 if (*arg == 'c') {
Denys Vlasenkob9348442011-02-14 15:42:18 +0100296 /* "\c" means cancel newline and
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000297 * ignore all subsequent chars. */
298 cur_io->iov_len = p - (char*)cur_io->iov_base;
299 cur_io++;
300 goto ret;
301 }
Denys Vlasenkob9348442011-02-14 15:42:18 +0100302 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
303 * of the form \0### are accepted. */
304 if (*arg == '0' && (unsigned char)(arg[1] - '0') < 8) {
305 arg++;
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000306 }
Denys Vlasenkob9348442011-02-14 15:42:18 +0100307 /* bb_process_escape_sequence can handle nul correctly */
308 c = bb_process_escape_sequence( (void*) &arg);
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000309 }
310 *p++ = c;
311 }
312
313 arg = *++argv;
314 if (arg)
315 *p++ = ' ';
316 cur_io->iov_len = p - (char*)cur_io->iov_base;
317 cur_io++;
318 if (!arg)
319 break;
320 }
321
322 newline_ret:
323 if (nflag) {
324 cur_io->iov_base = (char*)"\n";
325 cur_io->iov_len = 1;
326 cur_io++;
327 }
328 ret:
329 /* TODO: implement and use full_writev? */
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000330 return writev(1, io, (cur_io - io)) >= 0;
Denis Vlasenko7d75a962007-11-22 08:16:57 +0000331}
332#endif