aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Fox <pgf@brightstareng.com>2005-08-09 19:38:05 +0000
committerPaul Fox <pgf@brightstareng.com>2005-08-09 19:38:05 +0000
commit0b62158475ecbfce16fb857042ec7f402d7594ec (patch)
treed5f722c5d16a204996e46c84e8fe7923f8f40abf
parent3f11b1bf634536cb01d9913d1a3d10da5bf24541 (diff)
downloadbusybox-w32-0b62158475ecbfce16fb857042ec7f402d7594ec.tar.gz
busybox-w32-0b62158475ecbfce16fb857042ec7f402d7594ec.tar.bz2
busybox-w32-0b62158475ecbfce16fb857042ec7f402d7594ec.zip
implemented a builtin echo command in ash. moved the guts of the
echo applet into libbb, and now call bb_echo() from both echo.c and ash.c
-rw-r--r--coreutils/Config.in1
-rw-r--r--coreutils/echo.c140
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/Makefile.in3
-rw-r--r--libbb/bb_echo.c163
-rw-r--r--shell/Config.in15
-rw-r--r--shell/ash.c52
7 files changed, 204 insertions, 172 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in
index 49b884b70..9e06c0d80 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -156,6 +156,7 @@ config CONFIG_ECHO
156 help 156 help
157 echo is used to print a specified string to stdout. 157 echo is used to print a specified string to stdout.
158 158
159# this entry also appears in shell/Config.in, next to the echo builtin
159config CONFIG_FEATURE_FANCY_ECHO 160config CONFIG_FEATURE_FANCY_ECHO
160 bool " Enable echo options (-n and -e)" 161 bool " Enable echo options (-n and -e)"
161 default y 162 default y
diff --git a/coreutils/echo.c b/coreutils/echo.c
index 539640fb0..595e6defe 100644
--- a/coreutils/echo.c
+++ b/coreutils/echo.c
@@ -1,9 +1,5 @@
1/* vi: set sw=4 ts=4: */
2/* 1/*
3 * echo implementation for busybox 2 * echo applet implementation for busybox
4 *
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 * 3 *
8 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
@@ -19,20 +15,6 @@
19 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * 17 *
22 * Original copyright notice is retained at the end of this file.
23 */
24
25/* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
26/* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Because of behavioral differences, implemented configurable SUSv3
31 * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
32 * 1) In handling '\c' escape, the previous version only suppressed the
33 * trailing newline. SUSv3 specifies _no_ output after '\c'.
34 * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
35 * The previous version version did not allow 4-digit octals.
36 */ 18 */
37 19
38#include <stdio.h> 20#include <stdio.h>
@@ -42,124 +24,6 @@
42 24
43extern int echo_main(int argc, char** argv) 25extern int echo_main(int argc, char** argv)
44{ 26{
45#ifndef CONFIG_FEATURE_FANCY_ECHO 27 (void)bb_echo(argc, argv);
46#define eflag '\\'
47 ++argv;
48#else
49 const char *p;
50 int nflag = 1;
51 int eflag = 0;
52
53 while (*++argv && (**argv == '-')) {
54 /* If it appears that we are handling options, then make sure
55 * that all of the options specified are actually valid.
56 * Otherwise, the string should just be echoed.
57 */
58
59 if (!*(p = *argv + 1)) { /* A single '-', so echo it. */
60 goto just_echo;
61 }
62
63 do {
64 if (strrchr("neE", *p) == 0) {
65 goto just_echo;
66 }
67 } while (*++p);
68
69 /* All of the options in this arg are valid, so handle them. */
70 p = *argv + 1;
71 do {
72 if (*p == 'n') {
73 nflag = 0;
74 } else if (*p == 'e') {
75 eflag = '\\';
76 } else {
77 eflag = 0;
78 }
79 } while (*++p);
80 }
81
82just_echo:
83#endif
84 while (*argv) {
85 register int c;
86
87 while ((c = *(*argv)++)) {
88 if (c == eflag) { /* Check for escape seq. */
89 if (**argv == 'c') {
90 /* '\c' means cancel newline and
91 * ignore all subsequent chars. */
92 goto DONE;
93 }
94#ifndef CONFIG_FEATURE_FANCY_ECHO
95 /* SUSv3 specifies that octal escapes must begin with '0'. */
96 if (((unsigned int)(**argv - '1')) >= 7)
97#endif
98 {
99 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
100 * of the form \0### are accepted. */
101 if ((**argv == '0') && (((unsigned int)(argv[0][1] - '0')) < 8)) {
102 (*argv)++;
103 }
104 /* bb_process_escape_sequence can handle nul correctly */
105 c = bb_process_escape_sequence((const char **) argv);
106 }
107 }
108 putchar(c);
109 }
110
111 if (*++argv) {
112 putchar(' ');
113 }
114 }
115
116#ifdef CONFIG_FEATURE_FANCY_ECHO
117 if (nflag) {
118 putchar('\n');
119 }
120#else
121 putchar('\n');
122#endif
123
124DONE:
125 bb_fflush_stdout_and_exit(EXIT_SUCCESS); 28 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
126} 29}
127
128/*-
129 * Copyright (c) 1991, 1993
130 * The Regents of the University of California. All rights reserved.
131 *
132 * This code is derived from software contributed to Berkeley by
133 * Kenneth Almquist.
134 *
135 * Redistribution and use in source and binary forms, with or without
136 * modification, are permitted provided that the following conditions
137 * are met:
138 * 1. Redistributions of source code must retain the above copyright
139 * notice, this list of conditions and the following disclaimer.
140 * 2. Redistributions in binary form must reproduce the above copyright
141 * notice, this list of conditions and the following disclaimer in the
142 * documentation and/or other materials provided with the distribution.
143 *
144 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
145 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
146 *
147 * California, Berkeley and its contributors.
148 * 4. Neither the name of the University nor the names of its contributors
149 * may be used to endorse or promote products derived from this software
150 * without specific prior written permission.
151 *
152 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
153 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
154 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
155 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
156 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
157 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
158 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
159 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
160 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
161 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
162 * SUCH DAMAGE.
163 *
164 * @(#)echo.c 8.1 (Berkeley) 5/31/93
165 */
diff --git a/include/libbb.h b/include/libbb.h
index a544465fe..d12860ca9 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -105,6 +105,8 @@ extern void bb_perror_nomsg(void);
105extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0))); 105extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
106extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0))); 106extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
107 107
108extern int bb_echo(int argc, char** argv);
109
108extern const char *bb_mode_string(int mode); 110extern const char *bb_mode_string(int mode);
109extern int is_directory(const char *name, int followLinks, struct stat *statBuf); 111extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
110 112
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index 212ed70b7..6685305f4 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -46,7 +46,8 @@ LIBBB_SRC:= \
46 get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \ 46 get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \
47 getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \ 47 getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \
48 perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \ 48 perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \
49 warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c 49 warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c \
50 bb_echo.c
50 51
51LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) 52LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
52 53
diff --git a/libbb/bb_echo.c b/libbb/bb_echo.c
new file mode 100644
index 000000000..387ee9669
--- /dev/null
+++ b/libbb/bb_echo.c
@@ -0,0 +1,163 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * echo implementation for busybox
4 *
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Original copyright notice is retained at the end of this file.
23 */
24
25/* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
26/* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Because of behavioral differences, implemented configurable SUSv3
31 * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
32 * 1) In handling '\c' escape, the previous version only suppressed the
33 * trailing newline. SUSv3 specifies _no_ output after '\c'.
34 * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
35 * The previous version version did not allow 4-digit octals.
36 */
37
38
39#include <stdio.h>
40#include <string.h>
41#include "busybox.h"
42
43extern int bb_echo(int argc, char** argv)
44{
45#ifndef CONFIG_FEATURE_FANCY_ECHO
46#define eflag '\\'
47 ++argv;
48#else
49 const char *p;
50 int nflag = 1;
51 int eflag = 0;
52
53 while (*++argv && (**argv == '-')) {
54 /* If it appears that we are handling options, then make sure
55 * that all of the options specified are actually valid.
56 * Otherwise, the string should just be echoed.
57 */
58
59 if (!*(p = *argv + 1)) { /* A single '-', so echo it. */
60 goto just_echo;
61 }
62
63 do {
64 if (strrchr("neE", *p) == 0) {
65 goto just_echo;
66 }
67 } while (*++p);
68
69 /* All of the options in this arg are valid, so handle them. */
70 p = *argv + 1;
71 do {
72 if (*p == 'n') {
73 nflag = 0;
74 } else if (*p == 'e') {
75 eflag = '\\';
76 } else {
77 eflag = 0;
78 }
79 } while (*++p);
80 }
81
82just_echo:
83#endif
84 while (*argv) {
85 register int c;
86
87 while ((c = *(*argv)++)) {
88 if (c == eflag) { /* Check for escape seq. */
89 if (**argv == 'c') {
90 /* '\c' means cancel newline and
91 * ignore all subsequent chars. */
92 return 0;
93 }
94#ifndef CONFIG_FEATURE_FANCY_ECHO
95 /* SUSv3 specifies that octal escapes must begin with '0'. */
96 if (((unsigned int)(**argv - '1')) >= 7)
97#endif
98 {
99 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
100 * of the form \0### are accepted. */
101 if ((**argv == '0') && (((unsigned int)(argv[0][1] - '0')) < 8)) {
102 (*argv)++;
103 }
104 /* bb_process_escape_sequence can handle nul correctly */
105 c = bb_process_escape_sequence((const char **) argv);
106 }
107 }
108 putchar(c);
109 }
110
111 if (*++argv) {
112 putchar(' ');
113 }
114 }
115
116#ifdef CONFIG_FEATURE_FANCY_ECHO
117 if (nflag) {
118 putchar('\n');
119 }
120#else
121 putchar('\n');
122#endif
123 return 0;
124}
125
126/*-
127 * Copyright (c) 1991, 1993
128 * The Regents of the University of California. All rights reserved.
129 *
130 * This code is derived from software contributed to Berkeley by
131 * Kenneth Almquist.
132 *
133 * Redistribution and use in source and binary forms, with or without
134 * modification, are permitted provided that the following conditions
135 * are met:
136 * 1. Redistributions of source code must retain the above copyright
137 * notice, this list of conditions and the following disclaimer.
138 * 2. Redistributions in binary form must reproduce the above copyright
139 * notice, this list of conditions and the following disclaimer in the
140 * documentation and/or other materials provided with the distribution.
141 *
142 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
143 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
144 *
145 * California, Berkeley and its contributors.
146 * 4. Neither the name of the University nor the names of its contributors
147 * may be used to endorse or promote products derived from this software
148 * without specific prior written permission.
149 *
150 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
151 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
152 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
153 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
154 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
155 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
156 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
157 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
158 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
159 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
160 * SUCH DAMAGE.
161 *
162 * @(#)echo.c 8.1 (Berkeley) 5/31/93
163 */
diff --git a/shell/Config.in b/shell/Config.in
index 0d39e5bae..813044e2c 100644
--- a/shell/Config.in
+++ b/shell/Config.in
@@ -103,6 +103,21 @@ config CONFIG_ASH_CMDCMD
103 you to run the specified command with the specified arguments, 103 you to run the specified command with the specified arguments,
104 even when there is an ash builtin command with the same name. 104 even when there is an ash builtin command with the same name.
105 105
106config CONFIG_ASH_BUILTIN_ECHO
107 bool " Enable builtin version of 'echo'"
108 default n
109 depends on CONFIG_ASH
110 help
111 Enable support for echo, built in to ash.
112
113# this entry also appears in coreutils/Config.in, next to the echo applet
114config CONFIG_FEATURE_FANCY_ECHO
115 bool " Enable echo options (-n and -e)"
116 default y
117 depends on CONFIG_ASH_BUILTIN_ECHO
118 help
119 This adds options (-n and -e) to echo.
120
106config CONFIG_ASH_MAIL 121config CONFIG_ASH_MAIL
107 bool " Check for new mail on interactive shells" 122 bool " Check for new mail on interactive shells"
108 default y 123 default y
diff --git a/shell/ash.c b/shell/ash.c
index 7f77594a7..9660890f9 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -1249,6 +1249,9 @@ static int commandcmd(int, char **);
1249#endif 1249#endif
1250static int dotcmd(int, char **); 1250static int dotcmd(int, char **);
1251static int evalcmd(int, char **); 1251static int evalcmd(int, char **);
1252#ifdef CONFIG_ASH_BUILTIN_ECHO
1253static int echocmd(int, char **);
1254#endif
1252static int execcmd(int, char **); 1255static int execcmd(int, char **);
1253static int exitcmd(int, char **); 1256static int exitcmd(int, char **);
1254static int exportcmd(int, char **); 1257static int exportcmd(int, char **);
@@ -1308,39 +1311,12 @@ struct builtincmd {
1308 /* unsigned flags; */ 1311 /* unsigned flags; */
1309}; 1312};
1310 1313
1311#ifdef CONFIG_ASH_CMDCMD 1314
1312# ifdef JOBS 1315#define COMMANDCMD (builtincmd + 5 + \
1313# ifdef CONFIG_ASH_ALIAS 1316 ENABLE_ASH_ALIAS + ENABLE_ASH_JOB_CONTROL)
1314# define COMMANDCMD (builtincmd + 7) 1317#define EXECCMD (builtincmd + 7 + \
1315# define EXECCMD (builtincmd + 10) 1318 ENABLE_ASH_CMDCMD + ENABLE_ASH_ALIAS + \
1316# else 1319 ENABLE_ASH_BUILTIN_ECHO + ENABLE_ASH_JOB_CONTROL)
1317# define COMMANDCMD (builtincmd + 6)
1318# define EXECCMD (builtincmd + 9)
1319# endif
1320# else /* ! JOBS */
1321# ifdef CONFIG_ASH_ALIAS
1322# define COMMANDCMD (builtincmd + 6)
1323# define EXECCMD (builtincmd + 9)
1324# else
1325# define COMMANDCMD (builtincmd + 5)
1326# define EXECCMD (builtincmd + 8)
1327# endif
1328# endif /* JOBS */
1329#else /* ! CONFIG_ASH_CMDCMD */
1330# ifdef JOBS
1331# ifdef CONFIG_ASH_ALIAS
1332# define EXECCMD (builtincmd + 9)
1333# else
1334# define EXECCMD (builtincmd + 8)
1335# endif
1336# else /* ! JOBS */
1337# ifdef CONFIG_ASH_ALIAS
1338# define EXECCMD (builtincmd + 8)
1339# else
1340# define EXECCMD (builtincmd + 7)
1341# endif
1342# endif /* JOBS */
1343#endif /* CONFIG_ASH_CMDCMD */
1344 1320
1345#define BUILTIN_NOSPEC "0" 1321#define BUILTIN_NOSPEC "0"
1346#define BUILTIN_SPECIAL "1" 1322#define BUILTIN_SPECIAL "1"
@@ -1371,6 +1347,9 @@ static const struct builtincmd builtincmd[] = {
1371 { BUILTIN_REGULAR "command", commandcmd }, 1347 { BUILTIN_REGULAR "command", commandcmd },
1372#endif 1348#endif
1373 { BUILTIN_SPEC_REG "continue", breakcmd }, 1349 { BUILTIN_SPEC_REG "continue", breakcmd },
1350#ifdef CONFIG_ASH_BUILTIN_ECHO
1351 { BUILTIN_REGULAR "echo", echocmd },
1352#endif
1374 { BUILTIN_SPEC_REG "eval", evalcmd }, 1353 { BUILTIN_SPEC_REG "eval", evalcmd },
1375 { BUILTIN_SPEC_REG "exec", execcmd }, 1354 { BUILTIN_SPEC_REG "exec", execcmd },
1376 { BUILTIN_SPEC_REG "exit", exitcmd }, 1355 { BUILTIN_SPEC_REG "exit", exitcmd },
@@ -8200,6 +8179,13 @@ exitcmd(int argc, char **argv)
8200 /* NOTREACHED */ 8179 /* NOTREACHED */
8201} 8180}
8202 8181
8182#ifdef CONFIG_ASH_BUILTIN_ECHO
8183static int
8184echocmd(int argc, char **argv)
8185{
8186 return bb_echo(argc, argv);
8187}
8188#endif
8203/* $NetBSD: memalloc.c,v 1.27 2003/01/22 20:36:04 dsl Exp $ */ 8189/* $NetBSD: memalloc.c,v 1.27 2003/01/22 20:36:04 dsl Exp $ */
8204 8190
8205/* 8191/*