1 /* echo.c, taken from Bash.
2 Copyright (C) 87, 89, 91, 92, 93, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Bash, the Bourne Again SHell.
6 Bash is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License along
17 with Bash; see the file COPYING. If not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
32 #include <sys/types.h>
35 #include "long-options.h"
37 /* echo [-neE] [arg ...]
38 Output the ARGs. If -n is specified, the trailing newline is
39 suppressed. If the -e option is given, interpretation of the
40 following backslash-escaped characters is turned on:
43 \c suppress trailing newline
50 \num the character whose ASCII code is NUM (octal).
52 You can explicitly turn off the interpretation of the above characters
53 on System V systems with the -E option.
56 /* If defined, interpret backslash escapes if -e is given. */
59 /* If defined, interpret backslash escapes unless -E is given.
60 V9_ECHO must also be defined. */
64 # if defined (V9_DEFAULT)
65 # define VALID_ECHO_OPTIONS "neE"
67 # define VALID_ECHO_OPTIONS "ne"
68 # endif /* !V9_DEFAULT */
70 # define VALID_ECHO_OPTIONS "n"
73 /* The name this program was run with. */
81 fprintf (stderr, "Try `%s --help' for more information.\n",
85 printf ("Usage: %s [OPTION]... [STRING]...\n", program_name);
88 -n do not output the trailing newline\n\
90 -E disable interpolation of some sequences in STRINGs\n\
91 --help display this help and exit (should be alone)\n\
92 --version output version information and exit (should be alone)\n\
94 Without -E, the following sequences are recognized and interpolated:\n\
96 \\NNN the character whose ASCII code is NNN (octal)\n\
100 \\c suppress trailing newline\n\
103 \\r carriage return\n\
104 \\t horizontal tab\n\
111 /* Print the words in LIST to standard output. If the first word is
112 `-n', then don't print a trailing newline. We also support the
113 echo syntax from Version 9 unix systems. */
119 int display_return = 1, do_v9 = 0;
121 program_name = argv[0];
123 parse_long_options (argc, argv, "echo", version_string, usage);
125 /* System V machines already have a /bin/sh with a v9 behaviour. We
126 use the identical behaviour for these machines so that the
127 existing system shell scripts won't barf. */
128 #if defined (V9_ECHO) && defined (V9_DEFAULT)
135 while (argc > 0 && *argv[0] == '-')
140 /* If it appears that we are handling options, then make sure that
141 all of the options specified are actually valid. Otherwise, the
142 string should just be echoed. */
145 for (i = 0; temp[i]; i++)
147 if (rindex (VALID_ECHO_OPTIONS, temp[i]) == 0)
154 /* All of the options in TEMP are valid options to ECHO.
160 #if defined (V9_ECHO)
161 else if (*temp == 'e')
163 #if defined (V9_DEFAULT)
164 else if (*temp == 'E')
166 #endif /* V9_DEFAULT */
181 #if defined (V9_ECHO)
186 register char *s = argv[0];
195 case 'a': c = '\007'; break;
196 case 'b': c = '\b'; break;
197 case 'c': display_return = 0; continue;
198 case 'f': c = '\f'; break;
199 case 'n': c = '\n'; break;
200 case 'r': c = '\r'; break;
201 case 't': c = '\t'; break;
202 case 'v': c = (int) 0x0B; break;
203 case '0': case '1': case '2': case '3':
204 case '4': case '5': case '6': case '7':
206 if (*s >= '0' && *s <= '7')
207 c = c * 8 + (*s++ - '0');
208 if (*s >= '0' && *s <= '7')
209 c = c * 8 + (*s++ - '0');
212 default: putchar ('\\'); break;
228 fputs (argv[0], stdout);