i-bash/builtins/echo.def
2009-09-12 16:46:49 +00:00

157 lines
3.8 KiB
Modula-2

This file is echo.def, from which is created echo.c.
It implements the builtin "echo" in Bash.
Copyright (C) 1987, 1989, 1991 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 1, 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; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
$PRODUCES echo.c
#include <config.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <stdio.h>
#include "../shell.h"
$BUILTIN echo
$FUNCTION echo_builtin
$DEPENDS_ON V9_ECHO
$SHORT_DOC echo [-neE] [arg ...]
Output the ARGs. If -n is specified, the trailing newline is
suppressed. If the -e option is given, interpretation of the
following backslash-escaped characters is turned on:
\a alert (bell)
\b backspace
\c suppress trailing newline
\E escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\num the character whose ASCII code is NUM (octal).
You can explicitly turn off the interpretation of the above characters
with the -E option.
$END
$BUILTIN echo
$FUNCTION echo_builtin
$DEPENDS_ON !V9_ECHO
$SHORT_DOC echo [-n] [arg ...]
Output the ARGs. If -n is specified, the trailing newline is suppressed.
$END
#if defined (V9_ECHO)
# define VALID_ECHO_OPTIONS "neE"
#else /* !V9_ECHO */
# define VALID_ECHO_OPTIONS "n"
#endif /* !V9_ECHO */
/* 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;
char *temp;
#if defined (DEFAULT_ECHO_TO_USG)
/* 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. */
do_v9 = 1;
#else
do_v9 = 0;
#endif /* DEFAULT_ECHO_TO_USG */
display_return = 1;
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:
while (list)
{
i = 0;
temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), &i)
: list->word->word;
if (temp)
{
printf ("%s", temp);
fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
}
if (do_v9 && temp)
free (temp);
list = list->next;
if (i)
{
display_return = 0;
break;
}
if (list)
putchar(' ');
}
if (display_return)
putchar ('\n');
fflush (stdout);
return (EXECUTION_SUCCESS);
}