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