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