1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 87, 89, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
22 #include "long-options.h"
24 /* echo [-neE] [arg ...]
25 Output the ARGs. If -n is specified, the trailing newline is
26 suppressed. If the -e option is given, interpretation of the
27 following backslash-escaped characters is turned on:
30 \c suppress trailing newline
37 \num the character whose ASCII code is NUM (octal).
39 You can explicitly turn off the interpretation of the above characters
40 on System V systems with the -E option.
43 /* If defined, interpret backslash escapes if -e is given. */
46 /* If defined, interpret backslash escapes unless -E is given.
47 V9_ECHO must also be defined. */
51 # if defined (V9_DEFAULT)
52 # define VALID_ECHO_OPTIONS "neE"
54 # define VALID_ECHO_OPTIONS "ne"
55 # endif /* !V9_DEFAULT */
57 # define VALID_ECHO_OPTIONS "n"
60 /* The name this program was run with. */
67 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
71 printf (_("Usage: %s [OPTION]... [STRING]...\n"), program_name
);
73 Echo the STRING(s) to standard output.\n\
75 -n do not output the trailing newline\n\
77 -E disable interpolation of some sequences in STRINGs\n\
78 --help display this help and exit (should be alone)\n\
79 --version output version information and exit (should be alone)\n\
81 Without -E, the following sequences are recognized and interpolated:\n\
83 \\NNN the character whose ASCII code is NNN (octal)\n\
87 \\c suppress trailing newline\n\
90 \\r carriage return\n\
94 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));
99 /* Print the words in LIST to standard output. If the first word is
100 `-n', then don't print a trailing newline. We also support the
101 echo syntax from Version 9 unix systems. */
104 main (int argc
, char **argv
)
106 int display_return
= 1, do_v9
= 0;
108 program_name
= argv
[0];
109 setlocale (LC_ALL
, "");
110 bindtextdomain (PACKAGE
, LOCALEDIR
);
111 textdomain (PACKAGE
);
113 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
114 if (getenv ("POSIXLY_CORRECT") == NULL
)
115 parse_long_options (argc
, argv
, "echo", GNU_PACKAGE
, VERSION
, usage
);
117 /* System V machines already have a /bin/sh with a v9 behaviour. We
118 use the identical behaviour for these machines so that the
119 existing system shell scripts won't barf. */
120 #if defined (V9_ECHO) && defined (V9_DEFAULT)
127 while (argc
> 0 && *argv
[0] == '-')
132 /* If it appears that we are handling options, then make sure that
133 all of the options specified are actually valid. Otherwise, the
134 string should just be echoed. */
137 for (i
= 0; temp
[i
]; i
++)
139 if (strrchr (VALID_ECHO_OPTIONS
, temp
[i
]) == 0)
146 /* All of the options in TEMP are valid options to ECHO.
152 #if defined (V9_ECHO)
153 else if (*temp
== 'e')
155 #if defined (V9_DEFAULT)
156 else if (*temp
== 'E')
158 #endif /* V9_DEFAULT */
173 #if defined (V9_ECHO)
178 register char *s
= argv
[0];
187 case 'a': c
= '\007'; break;
188 case 'b': c
= '\b'; break;
189 case 'c': display_return
= 0; continue;
190 case 'f': c
= '\f'; break;
191 case 'n': c
= '\n'; break;
192 case 'r': c
= '\r'; break;
193 case 't': c
= '\t'; break;
194 case 'v': c
= (int) 0x0B; break;
195 case '0': case '1': case '2': case '3':
196 case '4': case '5': case '6': case '7':
198 if (*s
>= '0' && *s
<= '7')
199 c
= c
* 8 + (*s
++ - '0');
200 if (*s
>= '0' && *s
<= '7')
201 c
= c
* 8 + (*s
++ - '0');
204 default: putchar ('\\'); break;
220 fputs (argv
[0], stdout
);