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