]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/echo.def
Imported from ../bash-4.0-rc1.tar.gz.
[thirdparty/bash.git] / builtins / echo.def
1 This file is echo.def, from which is created echo.c.
2 It implements the builtin "echo" in Bash.
3
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20
21 $PRODUCES echo.c
22 #include <config.h>
23
24 #if defined (HAVE_UNISTD_H)
25 # include <unistd.h>
26 #endif
27
28 #include "../bashansi.h"
29
30 #include <stdio.h>
31 #include "../shell.h"
32
33 #include "common.h"
34
35 $BUILTIN echo
36 $FUNCTION echo_builtin
37 $DEPENDS_ON V9_ECHO
38 $SHORT_DOC echo [-neE] [arg ...]
39 Write arguments to the standard output.
40
41 Display the ARGs on the standard output followed by a newline.
42
43 Options:
44 -n do not append a newline
45 -e enable interpretation of the following backslash escapes
46 -E explicitly suppress interpretation of backslash escapes
47
48 `echo' interprets the following backslash-escaped characters:
49 \a alert (bell)
50 \b backspace
51 \c suppress further output
52 \e escape character
53 \f form feed
54 \n new line
55 \r carriage return
56 \t horizontal tab
57 \v vertical tab
58 \\ backslash
59 \0nnn the character whose ASCII code is NNN (octal). NNN can be
60 0 to 3 octal digits
61 \xHH the eight-bit character whose value is HH (hexadecimal). HH
62 can be one or two hex digits
63
64 Exit Status:
65 Returns success unless a write error occurs.
66 $END
67
68 $BUILTIN echo
69 $FUNCTION echo_builtin
70 $DEPENDS_ON !V9_ECHO
71 $SHORT_DOC echo [-n] [arg ...]
72 Write arguments to the standard output.
73
74 Display the ARGs on the standard output followed by a newline.
75
76 Options:
77 -n do not append a newline
78
79 Exit Status:
80 Returns success unless a write error occurs.
81 $END
82
83 #if defined (V9_ECHO)
84 # define VALID_ECHO_OPTIONS "neE"
85 #else /* !V9_ECHO */
86 # define VALID_ECHO_OPTIONS "n"
87 #endif /* !V9_ECHO */
88
89 /* System V machines already have a /bin/sh with a v9 behaviour. We
90 give Bash the identical behaviour for these machines so that the
91 existing system shells won't barf. Regrettably, the SUS v2 has
92 standardized the Sys V echo behavior. This variable is external
93 so that we can have a `shopt' variable to control it at runtime. */
94 #if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
95 int xpg_echo = 1;
96 #else
97 int xpg_echo = 0;
98 #endif /* DEFAULT_ECHO_TO_XPG */
99
100 extern int posixly_correct;
101
102 /* Print the words in LIST to standard output. If the first word is
103 `-n', then don't print a trailing newline. We also support the
104 echo syntax from Version 9 Unix systems. */
105 int
106 echo_builtin (list)
107 WORD_LIST *list;
108 {
109 int display_return, do_v9, i, len;
110 char *temp, *s;
111
112 do_v9 = xpg_echo;
113 display_return = 1;
114
115 if (posixly_correct && xpg_echo)
116 goto just_echo;
117
118 for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
119 {
120 /* If it appears that we are handling options, then make sure that
121 all of the options specified are actually valid. Otherwise, the
122 string should just be echoed. */
123 temp++;
124
125 for (i = 0; temp[i]; i++)
126 {
127 if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
128 break;
129 }
130
131 /* echo - and echo -<nonopt> both mean to just echo the arguments. */
132 if (*temp == 0 || temp[i])
133 break;
134
135 /* All of the options in TEMP are valid options to ECHO.
136 Handle them. */
137 while (i = *temp++)
138 {
139 switch (i)
140 {
141 case 'n':
142 display_return = 0;
143 break;
144 #if defined (V9_ECHO)
145 case 'e':
146 do_v9 = 1;
147 break;
148 case 'E':
149 do_v9 = 0;
150 break;
151 #endif /* V9_ECHO */
152 default:
153 goto just_echo; /* XXX */
154 }
155 }
156 }
157
158 just_echo:
159
160 clearerr (stdout); /* clear error before writing and testing success */
161
162 terminate_immediately++;
163 while (list)
164 {
165 i = len = 0;
166 temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
167 : list->word->word;
168 if (temp)
169 {
170 if (do_v9)
171 {
172 for (s = temp; len > 0; len--)
173 putchar (*s++);
174 }
175 else
176 printf ("%s", temp);
177 #if defined (SunOS5)
178 fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
179 #endif
180 }
181 if (do_v9 && temp)
182 free (temp);
183 list = list->next;
184 if (i)
185 {
186 display_return = 0;
187 break;
188 }
189 if (list)
190 putchar(' ');
191 }
192
193 if (display_return)
194 putchar ('\n');
195
196 terminate_immediately--;
197 return (sh_chkwrite (EXECUTION_SUCCESS));
198 }