*** empty log message ***
[coreutils.git] / src / echo.c
blobbbb37f4f12b2d20af8180dc9a56db39af3ff4ce7
1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 87,89, 1991-1997, 1999 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)
7 any later version.
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. */
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include "system.h"
22 #include "long-options.h"
24 /* The official name of this program (e.g., no `g' prefix). */
25 #define PROGRAM_NAME "echo"
27 #define AUTHORS "FIXME unknown"
29 /* echo [-neE] [arg ...]
30 Output the ARGs. If -n is specified, the trailing newline is
31 suppressed. If the -e option is given, interpretation of the
32 following backslash-escaped characters is turned on:
33 \a alert (bell)
34 \b backspace
35 \c suppress trailing newline
36 \f form feed
37 \n new line
38 \r carriage return
39 \t horizontal tab
40 \v vertical tab
41 \\ backslash
42 \num the character whose ASCII code is NUM (octal).
44 You can explicitly turn off the interpretation of the above characters
45 on System V systems with the -E option.
48 /* If defined, interpret backslash escapes if -e is given. */
49 #define V9_ECHO
51 /* If defined, interpret backslash escapes unless -E is given.
52 V9_ECHO must also be defined. */
53 /* #define V9_DEFAULT */
55 #if defined (V9_ECHO)
56 # if defined (V9_DEFAULT)
57 # define VALID_ECHO_OPTIONS "neE"
58 # else
59 # define VALID_ECHO_OPTIONS "ne"
60 # endif /* !V9_DEFAULT */
61 #else /* !V9_ECHO */
62 # define VALID_ECHO_OPTIONS "n"
63 #endif /* !V9_ECHO */
65 /* The name this program was run with. */
66 char *program_name;
68 void
69 usage (int status)
71 if (status != 0)
72 fprintf (stderr, _("Try `%s --help' for more information.\n"),
73 program_name);
74 else
76 printf (_("Usage: %s [OPTION]... [STRING]...\n"), program_name);
77 printf (_("\
78 Echo the STRING(s) to standard output.\n\
79 \n\
80 -n do not output the trailing newline\n\
81 -e enable interpretation of the backslash-escaped characters\n\
82 listed below\n\
83 -E disable interpretation of those sequences in STRINGs\n\
84 --help display this help and exit (should be alone)\n\
85 --version output version information and exit (should be alone)\n\
86 \n\
87 Without -E, the following sequences are recognized and interpolated:\n\
88 \n\
89 \\NNN the character whose ASCII code is NNN (octal)\n\
90 \\\\ backslash\n\
91 \\a alert (BEL)\n\
92 \\b backspace\n\
93 \\c suppress trailing newline\n\
94 \\f form feed\n\
95 \\n new line\n\
96 \\r carriage return\n\
97 \\t horizontal tab\n\
98 \\v vertical tab\n\
99 "));
100 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
102 exit (status);
105 /* Print the words in LIST to standard output. If the first word is
106 `-n', then don't print a trailing newline. We also support the
107 echo syntax from Version 9 unix systems. */
110 main (int argc, char **argv)
112 int display_return = 1, do_v9 = 0;
113 int allow_options = 1;
115 program_name = argv[0];
116 setlocale (LC_ALL, "");
117 bindtextdomain (PACKAGE, LOCALEDIR);
118 textdomain (PACKAGE);
120 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
121 if (getenv ("POSIXLY_CORRECT") == NULL)
122 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
123 AUTHORS, usage);
124 else
125 allow_options = 0;
127 /* System V machines already have a /bin/sh with a v9 behaviour. We
128 use the identical behaviour for these machines so that the
129 existing system shell scripts won't barf. */
130 #if defined (V9_ECHO) && defined (V9_DEFAULT)
131 do_v9 = allow_options;
132 #endif
134 --argc;
135 ++argv;
137 while (argc > 0 && *argv[0] == '-')
139 register char *temp;
140 register int i;
142 /* If it appears that we are handling options, then make sure that
143 all of the options specified are actually valid. Otherwise, the
144 string should just be echoed. */
145 temp = argv[0] + 1;
147 for (i = 0; temp[i]; i++)
149 if (strrchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
150 goto just_echo;
153 if (!*temp)
154 goto just_echo;
156 /* All of the options in TEMP are valid options to ECHO.
157 Handle them. */
158 while (*temp)
160 if (allow_options && *temp == 'n')
161 display_return = 0;
162 #if defined (V9_ECHO)
163 else if (allow_options && *temp == 'e')
164 do_v9 = 1;
165 # if defined (V9_DEFAULT)
166 else if (allow_options && *temp == 'E')
167 do_v9 = 0;
168 # endif /* V9_DEFAULT */
169 #endif /* V9_ECHO */
170 else
171 goto just_echo;
173 temp++;
175 argc--;
176 argv++;
179 just_echo:
181 if (argc > 0)
183 #if defined (V9_ECHO)
184 if (do_v9)
186 while (argc > 0)
188 register char *s = argv[0];
189 register int c;
191 while ((c = *s++))
193 if (c == '\\' && *s)
195 switch (c = *s++)
197 case 'a': c = '\007'; break;
198 case 'b': c = '\b'; break;
199 case 'c': display_return = 0; continue;
200 case 'f': c = '\f'; break;
201 case 'n': c = '\n'; break;
202 case 'r': c = '\r'; break;
203 case 't': c = '\t'; break;
204 case 'v': c = (int) 0x0B; break;
205 case '0': case '1': case '2': case '3':
206 case '4': case '5': case '6': case '7':
207 c -= '0';
208 if (*s >= '0' && *s <= '7')
209 c = c * 8 + (*s++ - '0');
210 if (*s >= '0' && *s <= '7')
211 c = c * 8 + (*s++ - '0');
212 break;
213 case '\\': break;
214 default: putchar ('\\'); break;
217 putchar(c);
219 argc--;
220 argv++;
221 if (argc > 0)
222 putchar(' ');
225 else
226 #endif /* V9_ECHO */
228 while (argc > 0)
230 fputs (argv[0], stdout);
231 argc--;
232 argv++;
233 if (argc > 0)
234 putchar (' ');
238 if (display_return)
239 putchar ('\n');
240 exit (0);