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