tests: more automated quote adjustment
[coreutils/ericb.git] / src / echo.c
blobb909656778dfc6292deba9a60533a544320d4742
1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 1987, 1989, 1991-1997, 1999-2005, 2007-2012 Free Software
3 Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include "system.h"
23 /* The official name of this program (e.g., no `g' prefix). */
24 #define PROGRAM_NAME "echo"
26 #define AUTHORS \
27 proper_name ("Brian Fox"), \
28 proper_name ("Chet Ramey")
30 /* If true, interpret backslash escapes by default. */
31 #ifndef DEFAULT_ECHO_TO_XPG
32 enum { DEFAULT_ECHO_TO_XPG = false };
33 #endif
35 void
36 usage (int status)
38 if (status != EXIT_SUCCESS)
39 emit_try_help ();
40 else
42 printf (_("\
43 Usage: %s [SHORT-OPTION]... [STRING]...\n\
44 or: %s LONG-OPTION\n\
45 "), program_name, program_name);
46 fputs (_("\
47 Echo the STRING(s) to standard output.\n\
48 \n\
49 -n do not output the trailing newline\n\
50 "), stdout);
51 fputs (_(DEFAULT_ECHO_TO_XPG
52 ? N_("\
53 -e enable interpretation of backslash escapes (default)\n\
54 -E disable interpretation of backslash escapes\n")
55 : N_("\
56 -e enable interpretation of backslash escapes\n\
57 -E disable interpretation of backslash escapes (default)\n")),
58 stdout);
59 fputs (HELP_OPTION_DESCRIPTION, stdout);
60 fputs (VERSION_OPTION_DESCRIPTION, stdout);
61 fputs (_("\
62 \n\
63 If -e is in effect, the following sequences are recognized:\n\
64 \n\
65 "), stdout);
66 fputs (_("\
67 \\\\ backslash\n\
68 \\a alert (BEL)\n\
69 \\b backspace\n\
70 \\c produce no further output\n\
71 \\e escape\n\
72 \\f form feed\n\
73 \\n new line\n\
74 \\r carriage return\n\
75 \\t horizontal tab\n\
76 \\v vertical tab\n\
77 "), stdout);
78 fputs (_("\
79 \\0NNN byte with octal value NNN (1 to 3 digits)\n\
80 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
81 "), stdout);
82 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
83 emit_ancillary_info ();
85 exit (status);
88 /* Convert C from hexadecimal character to integer. */
89 static int
90 hextobin (unsigned char c)
92 switch (c)
94 default: return c - '0';
95 case 'a': case 'A': return 10;
96 case 'b': case 'B': return 11;
97 case 'c': case 'C': return 12;
98 case 'd': case 'D': return 13;
99 case 'e': case 'E': return 14;
100 case 'f': case 'F': return 15;
104 /* Print the words in LIST to standard output. If the first word is
105 `-n', then don't print a trailing newline. We also support the
106 echo syntax from Version 9 unix systems. */
109 main (int argc, char **argv)
111 bool display_return = true;
112 bool allow_options =
113 (! getenv ("POSIXLY_CORRECT")
114 || (! DEFAULT_ECHO_TO_XPG && 1 < argc && STREQ (argv[1], "-n")));
116 /* System V machines already have a /bin/sh with a v9 behavior.
117 Use the identical behavior for these machines so that the
118 existing system shell scripts won't barf. */
119 bool do_v9 = DEFAULT_ECHO_TO_XPG;
121 initialize_main (&argc, &argv);
122 set_program_name (argv[0]);
123 setlocale (LC_ALL, "");
124 bindtextdomain (PACKAGE, LOCALEDIR);
125 textdomain (PACKAGE);
127 atexit (close_stdout);
129 /* We directly parse options, rather than use parse_long_options, in
130 order to avoid accepting abbreviations. */
131 if (allow_options && argc == 2)
133 if (STREQ (argv[1], "--help"))
134 usage (EXIT_SUCCESS);
136 if (STREQ (argv[1], "--version"))
138 version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
139 (char *) NULL);
140 exit (EXIT_SUCCESS);
144 --argc;
145 ++argv;
147 if (allow_options)
148 while (argc > 0 && *argv[0] == '-')
150 char const *temp = argv[0] + 1;
151 size_t i;
153 /* If it appears that we are handling options, then make sure that
154 all of the options specified are actually valid. Otherwise, the
155 string should just be echoed. */
157 for (i = 0; temp[i]; i++)
158 switch (temp[i])
160 case 'e': case 'E': case 'n':
161 break;
162 default:
163 goto just_echo;
166 if (i == 0)
167 goto just_echo;
169 /* All of the options in TEMP are valid options to ECHO.
170 Handle them. */
171 while (*temp)
172 switch (*temp++)
174 case 'e':
175 do_v9 = true;
176 break;
178 case 'E':
179 do_v9 = false;
180 break;
182 case 'n':
183 display_return = false;
184 break;
187 argc--;
188 argv++;
191 just_echo:
193 if (do_v9)
195 while (argc > 0)
197 char const *s = argv[0];
198 unsigned char c;
200 while ((c = *s++))
202 if (c == '\\' && *s)
204 switch (c = *s++)
206 case 'a': c = '\a'; break;
207 case 'b': c = '\b'; break;
208 case 'c': exit (EXIT_SUCCESS);
209 case 'e': c = '\x1B'; break;
210 case 'f': c = '\f'; break;
211 case 'n': c = '\n'; break;
212 case 'r': c = '\r'; break;
213 case 't': c = '\t'; break;
214 case 'v': c = '\v'; break;
215 case 'x':
217 unsigned char ch = *s;
218 if (! isxdigit (ch))
219 goto not_an_escape;
220 s++;
221 c = hextobin (ch);
222 ch = *s;
223 if (isxdigit (ch))
225 s++;
226 c = c * 16 + hextobin (ch);
229 break;
230 case '0':
231 c = 0;
232 if (! ('0' <= *s && *s <= '7'))
233 break;
234 c = *s++;
235 /* Fall through. */
236 case '1': case '2': case '3':
237 case '4': case '5': case '6': case '7':
238 c -= '0';
239 if ('0' <= *s && *s <= '7')
240 c = c * 8 + (*s++ - '0');
241 if ('0' <= *s && *s <= '7')
242 c = c * 8 + (*s++ - '0');
243 break;
244 case '\\': break;
246 not_an_escape:
247 default: putchar ('\\'); break;
250 putchar (c);
252 argc--;
253 argv++;
254 if (argc > 0)
255 putchar (' ');
258 else
260 while (argc > 0)
262 fputs (argv[0], stdout);
263 argc--;
264 argv++;
265 if (argc > 0)
266 putchar (' ');
270 if (display_return)
271 putchar ('\n');
272 exit (EXIT_SUCCESS);