e8b6edb2afa544f0d24d93968c84126d832261a2
[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 <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 /* Print the words in LIST to standard output.  If the first word is
71    `-n', then don't print a trailing newline.  We also support the
72    echo syntax from Version 9 Unix systems. */
73 int
74 echo_builtin (list)
75      WORD_LIST *list;
76 {
77   int display_return, do_v9, i, len;
78   char *temp, *s;
79
80 #if defined (DEFAULT_ECHO_TO_USG)
81 /* System V machines already have a /bin/sh with a v9 behaviour.  We
82    give Bash the identical behaviour for these machines so that the
83    existing system shells won't barf. */
84   do_v9 = 1;
85 #else
86   do_v9 = 0;
87 #endif /* DEFAULT_ECHO_TO_USG */
88
89   display_return = 1;
90
91   for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
92     {
93       /* If it appears that we are handling options, then make sure that
94          all of the options specified are actually valid.  Otherwise, the
95          string should just be echoed. */
96       temp++;
97
98       for (i = 0; temp[i]; i++)
99         {
100           if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
101             break;
102         }
103
104       /* echo - and echo -<nonopt> both mean to just echo the arguments. */
105       if (*temp == 0 || temp[i])
106         break;
107
108       /* All of the options in TEMP are valid options to ECHO.
109          Handle them. */
110       while (i = *temp++)
111         {
112           switch (i)
113             {
114             case 'n':
115               display_return = 0;
116               break;
117 #if defined (V9_ECHO)
118             case 'e':
119               do_v9 = 1;
120               break;
121             case 'E':
122               do_v9 = 0;
123               break;
124 #endif /* V9_ECHO */
125             default:
126               goto just_echo;   /* XXX */
127             }
128         }
129     }
130
131 just_echo:
132
133   while (list)
134     {
135       i = len = 0;
136       temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), &i, &len)
137                    : list->word->word;
138       if (temp)
139         {
140           if (do_v9)
141             {
142               for (s = temp; len > 0; len--)
143                 putchar (*s++);
144             }
145           else      
146             printf ("%s", temp);
147 #if defined (SunOS5)
148           fflush (stdout);      /* Fix for bug in SunOS 5.5 printf(3) */
149 #endif
150         }
151       if (do_v9 && temp)
152         free (temp);
153       list = list->next;
154       if (i)
155         {
156           display_return = 0;
157           break;
158         }
159       if (list)
160         putchar(' ');
161     }
162
163   if (display_return)
164     putchar ('\n');
165   fflush (stdout);
166   return (EXECUTION_SUCCESS);
167 }