summaryrefslogtreecommitdiff
path: root/builtins/echo.def
blob: 62c61994a2a85d942f2298817680bffa8bc40d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
This file is echo.def, from which is created echo.c.
It implements the builtin "echo" in Bash.

Copyright (C) 1987-2009 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bash.  If not, see <http://www.gnu.org/licenses/>.

$PRODUCES echo.c
#include <config.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "../bashansi.h"

#include <stdio.h>
#include "../shell.h"

#include "common.h"

$BUILTIN echo
$FUNCTION echo_builtin
$DEPENDS_ON V9_ECHO
$SHORT_DOC echo [-neE] [arg ...]
Write arguments to the standard output.

Display the ARGs on the standard output followed by a newline.

Options:
  -n	do not append a newline
  -e	enable interpretation of the following backslash escapes
  -E	explicitly suppress interpretation of backslash escapes

`echo' interprets the following backslash-escaped characters:
  \a	alert (bell)
  \b	backspace
  \c	suppress further output
  \e	escape character
  \f	form feed
  \n	new line
  \r	carriage return
  \t	horizontal tab
  \v	vertical tab
  \\	backslash
  \0nnn	the character whose ASCII code is NNN (octal).  NNN can be
	0 to 3 octal digits
  \xHH	the eight-bit character whose value is HH (hexadecimal).  HH
	can be one or two hex digits

Exit Status:
Returns success unless a write error occurs.
$END

$BUILTIN echo
$FUNCTION echo_builtin
$DEPENDS_ON !V9_ECHO
$SHORT_DOC echo [-n] [arg ...]
Write arguments to the standard output.

Display the ARGs on the standard output followed by a newline.

Options:
  -n	do not append a newline

Exit Status:
Returns success unless a write error occurs.
$END

#if defined (V9_ECHO)
#  define VALID_ECHO_OPTIONS "neE"
#else /* !V9_ECHO */
#  define VALID_ECHO_OPTIONS "n"
#endif /* !V9_ECHO */

/* System V machines already have a /bin/sh with a v9 behaviour.  We
   give Bash the identical behaviour for these machines so that the
   existing system shells won't barf.  Regrettably, the SUS v2 has
   standardized the Sys V echo behavior.  This variable is external
   so that we can have a `shopt' variable to control it at runtime. */
#if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
int xpg_echo = 1;
#else
int xpg_echo = 0;
#endif /* DEFAULT_ECHO_TO_XPG */

extern int posixly_correct;

/* Print the words in LIST to standard output.  If the first word is
   `-n', then don't print a trailing newline.  We also support the
   echo syntax from Version 9 Unix systems. */
int
echo_builtin (list)
     WORD_LIST *list;
{
  int display_return, do_v9, i, len;
  char *temp, *s;

  do_v9 = xpg_echo;
  display_return = 1;

  if (posixly_correct && xpg_echo)
    goto just_echo;

  for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
    {
      /* If it appears that we are handling options, then make sure that
	 all of the options specified are actually valid.  Otherwise, the
	 string should just be echoed. */
      temp++;

      for (i = 0; temp[i]; i++)
	{
	  if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
	    break;
	}

      /* echo - and echo -<nonopt> both mean to just echo the arguments. */
      if (*temp == 0 || temp[i])
	break;

      /* All of the options in TEMP are valid options to ECHO.
	 Handle them. */
      while (i = *temp++)
	{
	  switch (i)
	    {
	    case 'n':
	      display_return = 0;
	      break;
#if defined (V9_ECHO)
	    case 'e':
	      do_v9 = 1;
	      break;
	    case 'E':
	      do_v9 = 0;
	      break;
#endif /* V9_ECHO */
	    default:
	      goto just_echo;	/* XXX */
	    }
	}
    }

just_echo:

  clearerr (stdout);	/* clear error before writing and testing success */

  terminate_immediately++;
  while (list)
    {
      i = len = 0;
      temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
		   : list->word->word;
      if (temp)
	{
	  if (do_v9)
	    {
	      for (s = temp; len > 0; len--)
		putchar (*s++);
	    }
	  else	    
	    printf ("%s", temp);
#if defined (SunOS5)
	  fflush (stdout);	/* Fix for bug in SunOS 5.5 printf(3) */
#endif
	}
      if (do_v9 && temp)
	free (temp);
      list = list->next;
      if (i)
	{
	  display_return = 0;
	  break;
	}
      if (list)
	putchar(' ');
    }

  if (display_return)
    putchar ('\n');

  terminate_immediately--;
  return (sh_chkwrite (EXECUTION_SUCCESS));
}