Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
$PRODUCES echo.c
+#include <config.h>
+
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
+#endif
+
#include <stdio.h>
#include "../shell.h"
\a alert (bell)
\b backspace
\c suppress trailing newline
+ \E escape character
\f form feed
\n new line
\r carriage return
/* Print the words in LIST to standard output. If the first word is
`-n', then don't print a trailing newline. We also support the
- echo syntax from Version 9 unix systems. */
+ echo syntax from Version 9 Unix systems. */
+int
echo_builtin (list)
WORD_LIST *list;
{
- int display_return = 1, do_v9 = 0;
+ int display_return, do_v9, i;
+ char *temp;
#if defined (DEFAULT_ECHO_TO_USG)
/* System V machines already have a /bin/sh with a v9 behaviour. We
give Bash the identical behaviour for these machines so that the
existing system shells won't barf. */
do_v9 = 1;
+#else
+ do_v9 = 0;
#endif /* DEFAULT_ECHO_TO_USG */
- while (list && list->word->word[0] == '-')
- {
- register char *temp;
- register int i;
+ display_return = 1;
+ for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
+ {
/* If it appears that we are handling options, then make sure that
all of the options specified are actually valid. Otherwise, the
string should just be echoed. */
- temp = &(list->word->word[1]);
+ temp++;
for (i = 0; temp[i]; i++)
{
if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
- goto just_echo;
+ break;
}
- if (!*temp)
- goto just_echo;
+ /* echo - and echo -<nonopt> both mean to just echo the arguments. */
+ if (*temp == 0 || temp[i])
+ break;
/* All of the options in TEMP are valid options to ECHO.
Handle them. */
- while (*temp)
+ while (i = *temp++)
{
- if (*temp == 'n')
- display_return = 0;
+ switch (i)
+ {
+ case 'n':
+ display_return = 0;
+ break;
#if defined (V9_ECHO)
- else if (*temp == 'e')
- do_v9 = 1;
- else if (*temp == 'E')
- do_v9 = 0;
+ case 'e':
+ do_v9 = 1;
+ break;
+ case 'E':
+ do_v9 = 0;
+ break;
#endif /* V9_ECHO */
- else
- goto just_echo;
-
- temp++;
+ default:
+ goto just_echo; /* XXX */
+ }
}
- list = list->next;
}
just_echo:
- if (list)
+ while (list)
{
-#if defined (V9_ECHO)
- if (do_v9)
+ i = 0;
+ temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), &i)
+ : list->word->word;
+ if (temp)
{
- while (list)
- {
- register char *s = list->word->word;
- register int c;
-
- while (c = *s++)
- {
- if (c == '\\' && *s)
- {
- switch (c = *s++)
- {
- case 'a': c = '\007'; break;
- case 'b': c = '\b'; break;
- case 'c': display_return = 0; continue;
- case 'f': c = '\f'; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case 'v': c = (int) 0x0B; break;
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- c -= '0';
- if (*s >= '0' && *s <= '7')
- c = c * 8 + (*s++ - '0');
- if (*s >= '0' && *s <= '7')
- c = c * 8 + (*s++ - '0');
- break;
- case '\\': break;
- default: putchar ('\\'); break;
- }
- }
- putchar(c);
- }
- list = list->next;
- if (list)
- putchar(' ');
- }
+ printf ("%s", temp);
+ fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
}
- else
-#endif /* V9_ECHO */
- print_word_list (list, " ");
+ if (do_v9 && temp)
+ free (temp);
+ list = list->next;
+ if (i)
+ {
+ display_return = 0;
+ break;
+ }
+ if (list)
+ putchar(' ');
}
+
if (display_return)
- printf ("\n");
+ putchar ('\n');
fflush (stdout);
return (EXECUTION_SUCCESS);
}