Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 2 | /* |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 3 | * echo implementation for busybox |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 5 | * Copyright (c) 1991, 1993 |
| 6 | * The Regents of the University of California. All rights reserved. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 9 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 10 | * Original copyright notice is retained at the end of this file. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | */ |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 12 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 13 | * |
| 14 | * Because of behavioral differences, implemented configurable SUSv3 |
| 15 | * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs. |
| 16 | * 1) In handling '\c' escape, the previous version only suppressed the |
| 17 | * trailing newline. SUSv3 specifies _no_ output after '\c'. |
| 18 | * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}. |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 19 | * The previous version did not allow 4-digit octals. |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 20 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 21 | //config:config ECHO |
Denys Vlasenko | b86b39b | 2018-12-28 17:45:35 +0100 | [diff] [blame] | 22 | //config: bool "echo (1.8 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 23 | //config: default y |
| 24 | //config: help |
Denys Vlasenko | b86b39b | 2018-12-28 17:45:35 +0100 | [diff] [blame] | 25 | //config: echo prints a specified string to stdout. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 26 | //config: |
| 27 | //config:# this entry also appears in shell/Config.in, next to the echo builtin |
| 28 | //config:config FEATURE_FANCY_ECHO |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 29 | //config: bool "Enable -n and -e options" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 30 | //config: default y |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 31 | //config: depends on ECHO || ASH_ECHO || HUSH_ECHO |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 32 | |
| 33 | //applet:IF_ECHO(APPLET_NOFORK(echo, echo, BB_DIR_BIN, BB_SUID_DROP, echo)) |
| 34 | |
| 35 | //kbuild:lib-$(CONFIG_ECHO) += echo.o |
| 36 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 37 | //kbuild:lib-$(CONFIG_ASH_ECHO) += echo.o |
| 38 | //kbuild:lib-$(CONFIG_HUSH_ECHO) += echo.o |
| 39 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 40 | /* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */ |
| 41 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */ |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 42 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 43 | //usage:#define echo_trivial_usage |
| 44 | //usage: IF_FEATURE_FANCY_ECHO("[-neE] ") "[ARG]..." |
| 45 | //usage:#define echo_full_usage "\n\n" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 46 | //usage: "Print ARGs to stdout" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 47 | //usage: IF_FEATURE_FANCY_ECHO( "\n" |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame] | 48 | //usage: "\n -n No trailing newline" |
| 49 | //usage: "\n -e Interpret backslash escapes (\\t=tab etc)" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 50 | //usage: "\n -E Don't interpret backslash escapes (default)" |
| 51 | //usage: ) |
| 52 | //usage: |
| 53 | //usage:#define echo_example_usage |
| 54 | //usage: "$ echo \"Erik is cool\"\n" |
| 55 | //usage: "Erik is cool\n" |
| 56 | //usage: IF_FEATURE_FANCY_ECHO("$ echo -e \"Erik\\nis\\ncool\"\n" |
| 57 | //usage: "Erik\n" |
| 58 | //usage: "is\n" |
| 59 | //usage: "cool\n" |
| 60 | //usage: "$ echo \"Erik\\nis\\ncool\"\n" |
| 61 | //usage: "Erik\\nis\\ncool\n") |
| 62 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 63 | #include "libbb.h" |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | fe5e23b | 2007-11-24 02:23:51 +0000 | [diff] [blame] | 65 | /* This is a NOFORK applet. Be very careful! */ |
| 66 | |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 67 | /* NB: can be used by shell even if not enabled as applet */ |
Denis Vlasenko | fe5e23b | 2007-11-24 02:23:51 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 69 | /* |
| 70 | * NB2: we don't use stdio, we need better error handing. |
| 71 | * Examples include writing into non-opened stdout and error on write. |
| 72 | * |
| 73 | * With stdio, output gets shoveled into stdout buffer, and even |
| 74 | * fflush cannot clear it out. It seems that even if libc receives |
| 75 | * EBADF on write attempts, it feels determined to output data no matter what. |
| 76 | * If echo is called by shell, it will try writing again later, and possibly |
| 77 | * will clobber future output. Not good. |
| 78 | * |
| 79 | * Solaris has fpurge which discards buffered input. glibc has __fpurge. |
| 80 | * But this function is not standard. |
| 81 | */ |
| 82 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 83 | int echo_main(int argc UNUSED_PARAM, char **argv) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 84 | { |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 85 | char **pp; |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 86 | const char *arg; |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 87 | char *out; |
| 88 | char *buffer; |
| 89 | unsigned buflen; |
Denys Vlasenko | 7c813fb | 2021-04-14 18:55:30 +0200 | [diff] [blame] | 90 | int err; |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 91 | #if !ENABLE_FEATURE_FANCY_ECHO |
Denis Vlasenko | a9d7d24 | 2007-04-10 16:34:00 +0000 | [diff] [blame] | 92 | enum { |
Mike Frysinger | 670c3f7 | 2015-07-29 23:33:16 -0400 | [diff] [blame] | 93 | eflag = 0, /* 0 -- disable escape sequences */ |
Denis Vlasenko | a9d7d24 | 2007-04-10 16:34:00 +0000 | [diff] [blame] | 94 | nflag = 1, /* 1 -- print '\n' */ |
| 95 | }; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 96 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 97 | argv++; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 98 | #else |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 99 | char nflag = 1; |
| 100 | char eflag = 0; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 101 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 102 | while ((arg = *++argv) != NULL) { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 103 | char n, e; |
| 104 | |
| 105 | if (arg[0] != '-') |
| 106 | break; /* not an option arg, echo it */ |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 107 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 108 | /* If it appears that we are handling options, then make sure |
| 109 | * that all of the options specified are actually valid. |
| 110 | * Otherwise, the string should just be echoed. |
| 111 | */ |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 112 | arg++; |
| 113 | n = nflag; |
| 114 | e = eflag; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 115 | do { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 116 | if (*arg == 'n') |
| 117 | n = 0; |
| 118 | else if (*arg == 'e') |
| 119 | e = '\\'; |
| 120 | else if (*arg != 'E') { |
| 121 | /* "-ccc" arg with one of c's invalid, echo it */ |
| 122 | /* arg consisting from just "-" also handled here */ |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 123 | goto just_echo; |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 124 | } |
| 125 | } while (*++arg); |
| 126 | nflag = n; |
| 127 | eflag = e; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 128 | } |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 129 | just_echo: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 130 | #endif |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 131 | |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 132 | buflen = 0; |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 133 | pp = argv; |
| 134 | while ((arg = *pp) != NULL) { |
| 135 | buflen += strlen(arg) + 1; |
| 136 | pp++; |
| 137 | } |
Denys Vlasenko | 4fdb67c | 2011-02-15 18:35:54 +0100 | [diff] [blame] | 138 | out = buffer = xmalloc(buflen + 1); /* +1 is needed for "no args" case */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 139 | |
| 140 | while ((arg = *argv) != NULL) { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 141 | int c; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | a9d7d24 | 2007-04-10 16:34:00 +0000 | [diff] [blame] | 143 | if (!eflag) { |
| 144 | /* optimization for very common case */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 145 | out = stpcpy(out, arg); |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 146 | } else |
| 147 | while ((c = *arg++) != '\0') { |
| 148 | if (c == eflag) { |
| 149 | /* This is an "\x" sequence */ |
| 150 | |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 151 | if (*arg == 'c') { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 152 | /* "\c" means cancel newline and |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 153 | * ignore all subsequent chars. */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 154 | goto do_write; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 155 | } |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 156 | /* Since SUSv3 mandates a first digit of 0, 4-digit octals |
| 157 | * of the form \0### are accepted. */ |
| 158 | if (*arg == '0') { |
| 159 | if ((unsigned char)(arg[1] - '0') < 8) { |
| 160 | /* 2nd char is 0..7: skip leading '0' */ |
| 161 | arg++; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 162 | } |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 163 | } |
| 164 | /* bb_process_escape_sequence handles NUL correctly |
| 165 | * ("...\" case). */ |
| 166 | { |
| 167 | /* optimization: don't force arg to be on-stack, |
| 168 | * use another variable for that. ~30 bytes win */ |
| 169 | const char *z = arg; |
| 170 | c = bb_process_escape_sequence(&z); |
| 171 | arg = z; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 174 | *out++ = c; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 177 | if (!*++argv) |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 178 | break; |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 179 | *out++ = ' '; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 182 | if (nflag) { |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 183 | *out++ = '\n'; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 184 | } |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 185 | |
| 186 | do_write: |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 187 | /* Careful to error out on partial writes too (think ENOSPC!) */ |
| 188 | errno = 0; |
Natanael Copa | e880c9c | 2021-01-22 08:35:55 +0100 | [diff] [blame] | 189 | err = full_write(STDOUT_FILENO, buffer, out - buffer) != out - buffer; |
| 190 | if (err) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 191 | bb_simple_perror_msg(bb_msg_write_error); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 192 | } |
Natanael Copa | e880c9c | 2021-01-22 08:35:55 +0100 | [diff] [blame] | 193 | free(buffer); |
| 194 | return err; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 197 | /* |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 198 | * Copyright (c) 1991, 1993 |
| 199 | * The Regents of the University of California. All rights reserved. |
| 200 | * |
| 201 | * This code is derived from software contributed to Berkeley by |
| 202 | * Kenneth Almquist. |
| 203 | * |
| 204 | * Redistribution and use in source and binary forms, with or without |
| 205 | * modification, are permitted provided that the following conditions |
| 206 | * are met: |
| 207 | * 1. Redistributions of source code must retain the above copyright |
| 208 | * notice, this list of conditions and the following disclaimer. |
| 209 | * 2. Redistributions in binary form must reproduce the above copyright |
| 210 | * notice, this list of conditions and the following disclaimer in the |
| 211 | * documentation and/or other materials provided with the distribution. |
| 212 | * |
| 213 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 214 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 215 | * |
| 216 | * California, Berkeley and its contributors. |
| 217 | * 4. Neither the name of the University nor the names of its contributors |
| 218 | * may be used to endorse or promote products derived from this software |
| 219 | * without specific prior written permission. |
| 220 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 221 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 222 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 223 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 224 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 225 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 226 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 227 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 228 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 229 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 230 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 231 | * SUCH DAMAGE. |
| 232 | * |
| 233 | * @(#)echo.c 8.1 (Berkeley) 5/31/93 |
| 234 | */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 235 | |
| 236 | #ifdef VERSION_WITH_WRITEV |
| 237 | /* We can't use stdio. |
| 238 | * The reason for this is highly non-obvious. |
Denis Vlasenko | fe5e23b | 2007-11-24 02:23:51 +0000 | [diff] [blame] | 239 | * echo_main is used from shell. Shell must correctly handle "echo foo" |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 240 | * if stdout is closed. With stdio, output gets shoveled into |
| 241 | * stdout buffer, and even fflush cannot clear it out. It seems that |
| 242 | * even if libc receives EBADF on write attempts, it feels determined |
| 243 | * to output data no matter what. So it will try later, |
| 244 | * and possibly will clobber future output. Not good. |
| 245 | * |
| 246 | * Using writev instead, with 'direct' conversion of argv vector. |
| 247 | */ |
| 248 | |
Denis Vlasenko | fe5e23b | 2007-11-24 02:23:51 +0000 | [diff] [blame] | 249 | int echo_main(int argc, char **argv) |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 250 | { |
| 251 | struct iovec io[argc]; |
| 252 | struct iovec *cur_io = io; |
| 253 | char *arg; |
| 254 | char *p; |
| 255 | #if !ENABLE_FEATURE_FANCY_ECHO |
| 256 | enum { |
| 257 | eflag = '\\', |
| 258 | nflag = 1, /* 1 -- print '\n' */ |
| 259 | }; |
| 260 | arg = *++argv; |
| 261 | if (!arg) |
| 262 | goto newline_ret; |
| 263 | #else |
| 264 | char nflag = 1; |
| 265 | char eflag = 0; |
| 266 | |
| 267 | while (1) { |
| 268 | arg = *++argv; |
| 269 | if (!arg) |
| 270 | goto newline_ret; |
| 271 | if (*arg != '-') |
| 272 | break; |
| 273 | |
| 274 | /* If it appears that we are handling options, then make sure |
| 275 | * that all of the options specified are actually valid. |
| 276 | * Otherwise, the string should just be echoed. |
| 277 | */ |
| 278 | p = arg + 1; |
| 279 | if (!*p) /* A single '-', so echo it. */ |
| 280 | goto just_echo; |
| 281 | |
| 282 | do { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 283 | if (!strchr("neE", *p)) |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 284 | goto just_echo; |
| 285 | } while (*++p); |
| 286 | |
| 287 | /* All of the options in this arg are valid, so handle them. */ |
| 288 | p = arg + 1; |
| 289 | do { |
| 290 | if (*p == 'n') |
| 291 | nflag = 0; |
| 292 | if (*p == 'e') |
| 293 | eflag = '\\'; |
| 294 | } while (*++p); |
| 295 | } |
| 296 | just_echo: |
| 297 | #endif |
| 298 | |
| 299 | while (1) { |
| 300 | /* arg is already == *argv and isn't NULL */ |
| 301 | int c; |
| 302 | |
| 303 | cur_io->iov_base = p = arg; |
| 304 | |
| 305 | if (!eflag) { |
| 306 | /* optimization for very common case */ |
| 307 | p += strlen(arg); |
| 308 | } else while ((c = *arg++)) { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 309 | if (c == eflag) { |
| 310 | /* This is an "\x" sequence */ |
| 311 | |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 312 | if (*arg == 'c') { |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 313 | /* "\c" means cancel newline and |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 314 | * ignore all subsequent chars. */ |
| 315 | cur_io->iov_len = p - (char*)cur_io->iov_base; |
| 316 | cur_io++; |
| 317 | goto ret; |
| 318 | } |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 319 | /* Since SUSv3 mandates a first digit of 0, 4-digit octals |
| 320 | * of the form \0### are accepted. */ |
| 321 | if (*arg == '0' && (unsigned char)(arg[1] - '0') < 8) { |
| 322 | arg++; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 323 | } |
Denys Vlasenko | 0e2cb6d | 2021-12-29 06:41:05 +0100 | [diff] [blame^] | 324 | //FIXME? we also accept non-0 starting sequences (see echo-prints-slash_41 test) |
| 325 | // echo -ne '-\41-' prints "-!-". bash 5.0.17 does not (prints "-\41-"). |
Denys Vlasenko | b934844 | 2011-02-14 15:42:18 +0100 | [diff] [blame] | 326 | /* bb_process_escape_sequence can handle nul correctly */ |
| 327 | c = bb_process_escape_sequence( (void*) &arg); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 328 | } |
| 329 | *p++ = c; |
| 330 | } |
| 331 | |
| 332 | arg = *++argv; |
| 333 | if (arg) |
| 334 | *p++ = ' '; |
| 335 | cur_io->iov_len = p - (char*)cur_io->iov_base; |
| 336 | cur_io++; |
| 337 | if (!arg) |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | newline_ret: |
| 342 | if (nflag) { |
| 343 | cur_io->iov_base = (char*)"\n"; |
| 344 | cur_io->iov_len = 1; |
| 345 | cur_io++; |
| 346 | } |
| 347 | ret: |
| 348 | /* TODO: implement and use full_writev? */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 349 | return writev(1, io, (cur_io - io)) >= 0; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 350 | } |
| 351 | #endif |