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