| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | This file is echo.def, from which is created echo.c. | 
					
						
							|  |  |  | It implements the builtin "echo" in Bash. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | Copyright (C) 1987-2016 Free Software Foundation, Inc. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | along with Bash.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $PRODUCES echo.c | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <config.h> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H) | 
					
						
							|  |  |  | #  include <unistd.h> | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #include "../bashansi.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include <stdio.h> | 
					
						
							|  |  |  | #include "../shell.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | #include "common.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | $BUILTIN echo | 
					
						
							|  |  |  | $FUNCTION echo_builtin | 
					
						
							|  |  |  | $DEPENDS_ON V9_ECHO | 
					
						
							|  |  |  | $SHORT_DOC echo [-neE] [arg ...] | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | Write arguments to the standard output. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | Display the ARGs, separated by a single space character and followed by a | 
					
						
							|  |  |  | newline, on the standard output. | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |   \E	escape character | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   \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 | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | 		0 to 3 octal digits | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   \xHH	the eight-bit character whose value is HH (hexadecimal).  HH | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  | 		can be one or two hex digits | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Exit Status: | 
					
						
							|  |  |  | Returns success unless a write error occurs. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | $END | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $BUILTIN echo | 
					
						
							|  |  |  | $FUNCTION echo_builtin | 
					
						
							|  |  |  | $DEPENDS_ON !V9_ECHO | 
					
						
							|  |  |  | $SHORT_DOC echo [-n] [arg ...] | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | $END | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (V9_ECHO) | 
					
						
							|  |  |  | #  define VALID_ECHO_OPTIONS "neE" | 
					
						
							|  |  |  | #else /* !V9_ECHO */ | 
					
						
							|  |  |  | #  define VALID_ECHO_OPTIONS "n" | 
					
						
							|  |  |  | #endif /* !V9_ECHO */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | /* 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. */ | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | #if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | int xpg_echo = 1; | 
					
						
							|  |  |  | #else | 
					
						
							|  |  |  | int xpg_echo = 0; | 
					
						
							|  |  |  | #endif /* DEFAULT_ECHO_TO_XPG */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | extern int posixly_correct; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* 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 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |    echo syntax from Version 9 Unix systems. */ | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | echo_builtin (list) | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   int display_return, do_v9, i, len; | 
					
						
							|  |  |  |   char *temp, *s; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   do_v9 = xpg_echo; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   display_return = 1; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   if (posixly_correct && xpg_echo) | 
					
						
							|  |  |  |     goto just_echo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   for (; list && (temp = list->word->word) && *temp == '-'; list = list->next) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       /* 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. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       temp++; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       for (i = 0; temp[i]; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	    break; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       /* echo - and echo -<nonopt> both mean to just echo the arguments. */ | 
					
						
							|  |  |  |       if (*temp == 0 || temp[i]) | 
					
						
							|  |  |  | 	break; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /* All of the options in TEMP are valid options to ECHO. | 
					
						
							|  |  |  | 	 Handle them. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       while (i = *temp++) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	  switch (i) | 
					
						
							|  |  |  | 	    { | 
					
						
							|  |  |  | 	    case 'n': | 
					
						
							|  |  |  | 	      display_return = 0; | 
					
						
							|  |  |  | 	      break; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #if defined (V9_ECHO) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	    case 'e': | 
					
						
							|  |  |  | 	      do_v9 = 1; | 
					
						
							|  |  |  | 	      break; | 
					
						
							|  |  |  | 	    case 'E': | 
					
						
							|  |  |  | 	      do_v9 = 0; | 
					
						
							|  |  |  | 	      break; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #endif /* V9_ECHO */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	    default: | 
					
						
							|  |  |  | 	      goto just_echo;	/* XXX */ | 
					
						
							|  |  |  | 	    } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | just_echo: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   clearerr (stdout);	/* clear error before writing and testing success */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   while (list) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |       i = len = 0; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |       temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		   : list->word->word; | 
					
						
							|  |  |  |       if (temp) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	  if (do_v9) | 
					
						
							|  |  |  | 	    { | 
					
						
							|  |  |  | 	      for (s = temp; len > 0; len--) | 
					
						
							|  |  |  | 		putchar (*s++); | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	  else	     | 
					
						
							|  |  |  | 	    printf ("%s", temp); | 
					
						
							|  |  |  | #if defined (SunOS5) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	  fflush (stdout);	/* Fix for bug in SunOS 5.5 printf(3) */ | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #endif | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |       QUIT; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       if (do_v9 && temp) | 
					
						
							|  |  |  | 	free (temp); | 
					
						
							|  |  |  |       list = list->next; | 
					
						
							|  |  |  |       if (i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  display_return = 0; | 
					
						
							|  |  |  | 	  break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       if (list) | 
					
						
							|  |  |  | 	putchar(' '); | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |       QUIT; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   if (display_return) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     putchar ('\n'); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (sh_chkwrite (EXECUTION_SUCCESS)); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } |