Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / builtins / echo.def
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, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES echo.c
23 #include <stdio.h>
24 #include "../shell.h"
25
26 $BUILTIN echo
27 $FUNCTION echo_builtin
28 $DEPENDS_ON V9_ECHO
29 $SHORT_DOC echo [-neE] [arg ...]
30 Output the ARGs.  If -n is specified, the trailing newline is
31 suppressed.  If the -e option is given, interpretation of the
32 following backslash-escaped characters is turned on:
33         \a      alert (bell)
34         \b      backspace
35         \c      suppress trailing newline
36         \f      form feed
37         \n      new line
38         \r      carriage return
39         \t      horizontal tab
40         \v      vertical tab
41         \\      backslash
42         \num    the character whose ASCII code is NUM (octal).
43
44 You can explicitly turn off the interpretation of the above characters
45 with the -E option.
46 $END
47
48 $BUILTIN echo
49 $FUNCTION echo_builtin
50 $DEPENDS_ON !V9_ECHO
51 $SHORT_DOC echo [-n] [arg ...]
52 Output the ARGs.  If -n is specified, the trailing newline is suppressed.
53 $END
54
55 #if defined (V9_ECHO)
56 #  define VALID_ECHO_OPTIONS "neE"
57 #else /* !V9_ECHO */
58 #  define VALID_ECHO_OPTIONS "n"
59 #endif /* !V9_ECHO */
60
61 /* Print the words in LIST to standard output.  If the first word is
62    `-n', then don't print a trailing newline.  We also support the
63    echo syntax from Version 9 unix systems. */
64 echo_builtin (list)
65      WORD_LIST *list;
66 {
67   int display_return = 1, do_v9 = 0;
68
69 #if defined (DEFAULT_ECHO_TO_USG)
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. */
73   do_v9 = 1;
74 #endif /* DEFAULT_ECHO_TO_USG */
75
76   while (list && list->word->word[0] == '-')
77     {
78       register char *temp;
79       register int i;
80
81       /* If it appears that we are handling options, then make sure that
82          all of the options specified are actually valid.  Otherwise, the
83          string should just be echoed. */
84       temp = &(list->word->word[1]);
85
86       for (i = 0; temp[i]; i++)
87         {
88           if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
89             goto just_echo;
90         }
91
92       if (!*temp)
93         goto just_echo;
94
95       /* All of the options in TEMP are valid options to ECHO.
96          Handle them. */
97       while (*temp)
98         {
99           if (*temp == 'n')
100             display_return = 0;
101 #if defined (V9_ECHO)
102           else if (*temp == 'e')
103             do_v9 = 1;
104           else if (*temp == 'E')
105             do_v9 = 0;
106 #endif /* V9_ECHO */
107           else
108             goto just_echo;
109
110           temp++;
111         }
112       list = list->next;
113     }
114
115 just_echo:
116
117   if (list)
118     {
119 #if defined (V9_ECHO)
120       if (do_v9)
121         {
122           while (list)
123             {
124               register char *s = list->word->word;
125               register int c;
126
127               while (c = *s++)
128                 {
129                   if (c == '\\' && *s)
130                     {
131                       switch (c = *s++)
132                         {
133                         case 'a': c = '\007'; break;
134                         case 'b': c = '\b'; break;
135                         case 'c': display_return = 0; continue;
136                         case 'f': c = '\f'; break;
137                         case 'n': c = '\n'; break;
138                         case 'r': c = '\r'; break;
139                         case 't': c = '\t'; break;
140                         case 'v': c = (int) 0x0B; break;
141                         case '0': case '1': case '2': case '3':
142                         case '4': case '5': case '6': case '7':
143                           c -= '0';
144                           if (*s >= '0' && *s <= '7')
145                             c = c * 8 + (*s++ - '0');
146                           if (*s >= '0' && *s <= '7')
147                             c = c * 8 + (*s++ - '0');
148                           break;
149                         case '\\': break;
150                         default:  putchar ('\\'); break;
151                         }
152                     }
153                   putchar(c);
154                 }
155               list = list->next;
156               if (list)
157                 putchar(' ');
158             }
159         }
160       else
161 #endif /* V9_ECHO */
162         print_word_list (list, " ");
163     }
164   if (display_return)
165     printf ("\n");
166   fflush (stdout);
167   return (EXECUTION_SUCCESS);
168 }