Dan Pasanen | c6e3786 | 2014-10-02 14:08:59 -0500 | [diff] [blame] | 1 | /* echo.c, created from echo.def. */ |
| 2 | #line 22 "./echo.def" |
| 3 | #include <config.h> |
| 4 | |
| 5 | #if defined (HAVE_UNISTD_H) |
| 6 | # include <unistd.h> |
| 7 | #endif |
| 8 | |
| 9 | #include "../bashansi.h" |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include "../shell.h" |
| 13 | |
| 14 | #include "common.h" |
| 15 | |
| 16 | #line 69 "./echo.def" |
| 17 | |
| 18 | #line 84 "./echo.def" |
| 19 | |
| 20 | #if defined (V9_ECHO) |
| 21 | # define VALID_ECHO_OPTIONS "neE" |
| 22 | #else /* !V9_ECHO */ |
| 23 | # define VALID_ECHO_OPTIONS "n" |
| 24 | #endif /* !V9_ECHO */ |
| 25 | |
| 26 | /* System V machines already have a /bin/sh with a v9 behaviour. We |
| 27 | give Bash the identical behaviour for these machines so that the |
| 28 | existing system shells won't barf. Regrettably, the SUS v2 has |
| 29 | standardized the Sys V echo behavior. This variable is external |
| 30 | so that we can have a `shopt' variable to control it at runtime. */ |
| 31 | #if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX) |
| 32 | int xpg_echo = 1; |
| 33 | #else |
| 34 | int xpg_echo = 0; |
| 35 | #endif /* DEFAULT_ECHO_TO_XPG */ |
| 36 | |
| 37 | extern int posixly_correct; |
| 38 | |
| 39 | /* Print the words in LIST to standard output. If the first word is |
| 40 | `-n', then don't print a trailing newline. We also support the |
| 41 | echo syntax from Version 9 Unix systems. */ |
| 42 | int |
| 43 | echo_builtin (list) |
| 44 | WORD_LIST *list; |
| 45 | { |
| 46 | int display_return, do_v9, i, len; |
| 47 | char *temp, *s; |
| 48 | |
| 49 | do_v9 = xpg_echo; |
| 50 | display_return = 1; |
| 51 | |
| 52 | if (posixly_correct && xpg_echo) |
| 53 | goto just_echo; |
| 54 | |
| 55 | for (; list && (temp = list->word->word) && *temp == '-'; list = list->next) |
| 56 | { |
| 57 | /* If it appears that we are handling options, then make sure that |
| 58 | all of the options specified are actually valid. Otherwise, the |
| 59 | string should just be echoed. */ |
| 60 | temp++; |
| 61 | |
| 62 | for (i = 0; temp[i]; i++) |
| 63 | { |
| 64 | if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0) |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | /* echo - and echo -<nonopt> both mean to just echo the arguments. */ |
| 69 | if (*temp == 0 || temp[i]) |
| 70 | break; |
| 71 | |
| 72 | /* All of the options in TEMP are valid options to ECHO. |
| 73 | Handle them. */ |
| 74 | while (i = *temp++) |
| 75 | { |
| 76 | switch (i) |
| 77 | { |
| 78 | case 'n': |
| 79 | display_return = 0; |
| 80 | break; |
| 81 | #if defined (V9_ECHO) |
| 82 | case 'e': |
| 83 | do_v9 = 1; |
| 84 | break; |
| 85 | case 'E': |
| 86 | do_v9 = 0; |
| 87 | break; |
| 88 | #endif /* V9_ECHO */ |
| 89 | default: |
| 90 | goto just_echo; /* XXX */ |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | just_echo: |
| 96 | |
| 97 | clearerr (stdout); /* clear error before writing and testing success */ |
| 98 | |
| 99 | terminate_immediately++; |
| 100 | while (list) |
| 101 | { |
| 102 | i = len = 0; |
| 103 | temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len) |
| 104 | : list->word->word; |
| 105 | if (temp) |
| 106 | { |
| 107 | if (do_v9) |
| 108 | { |
| 109 | for (s = temp; len > 0; len--) |
| 110 | putchar (*s++); |
| 111 | } |
| 112 | else |
| 113 | printf ("%s", temp); |
| 114 | #if defined (SunOS5) |
| 115 | fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */ |
| 116 | #endif |
| 117 | } |
| 118 | if (do_v9 && temp) |
| 119 | free (temp); |
| 120 | list = list->next; |
| 121 | if (i) |
| 122 | { |
| 123 | display_return = 0; |
| 124 | break; |
| 125 | } |
| 126 | if (list) |
| 127 | putchar(' '); |
| 128 | } |
| 129 | |
| 130 | if (display_return) |
| 131 | putchar ('\n'); |
| 132 | |
| 133 | terminate_immediately--; |
| 134 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
| 135 | } |