Date: Thu, 22 Aug 2002 04:56:03 -0700 From: Luigi Rizzo <rizzo@icir.org> To: "M. Warner Losh" <imp@bsdimp.com> Cc: nbm@mithrandr.moria.org, arch@FreeBSD.ORG Subject: Re: ugliness in rc.* scripts Message-ID: <20020822045603.A66453@iguana.icir.org> In-Reply-To: <20020821091942.B59085@iguana.icir.org>; from rizzo@icir.org on Wed, Aug 21, 2002 at 09:19:42AM -0700 References: <20020820232538.A53816@iguana.icir.org> <20020821074819.GA58163@mithrandr.moria.org> <20020821061822.A56560@iguana.icir.org> <20020821.095726.109035410.imp@bsdimp.com> <20020821091942.B59085@iguana.icir.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Aug 21, 2002 at 09:19:42AM -0700, Luigi Rizzo wrote:
> On Wed, Aug 21, 2002 at 09:57:26AM -0600, M. Warner Losh wrote:
> > In message: <20020821061822.A56560@iguana.icir.org>
> > Luigi Rizzo <rizzo@icir.org> writes:
> ...
> > : i think it is only one process -- echo is a builtin, tolower is a shell
> > : function. In any case, we never cared about performance in the rc scripts,
> > : not spawning processes (e.g. calling scripts with . instead of sh) has
> > : only to do with letting variable assignements be visible in the caller.
> >
> > Actually, we have cared about the performance of shell scripts in the
> > past. NetBSD got a nasty surprise when they went to their new rc
ok, I made a few tests with /bin/sh. Funny results.
Here, echo is a builtin command, /bin/echo an external one,
and "f() { echo $* ; }" and f2() { /bin/echo $*; }
are shell functions.
echo "foo" # does not fork
a=`echo "foo"` # does not fork
/bin/echo "foo" # forks once.
a=`/bin/echo "foo"` # forks once
f "foo" # does not fork
a=`f "foo"` # forks once. XXX
f2 "foo" # forks once.
a=`f2 "foo"` # forks twice. XXX
I guess the most surprising (to me) behaviour is the handling
of shell functions in backquotes -- but i suppose this is
because builtins do not have side effects on the environment
whereas shell functions can.
In any case -- to come back to the original problem of
folding strings into lowercase -- making "tr" a builtin
is trivial, see the attached patches (congratulations to
the designer of the builtin mechanism in /bin/sh).
It would be even more useful if "tr" could handle arguments
from the command line (or we hit again the problem of forking a
process to provide the data to translate on stdin)
cheers
luigi
Index: bin/sh/Makefile
===================================================================
RCS file: /home/ncvs/src/bin/sh/Makefile,v
retrieving revision 1.30.2.1
diff -u -r1.30.2.1 Makefile
--- bin/sh/Makefile 15 Dec 2001 10:05:18 -0000 1.30.2.1
+++ bin/sh/Makefile 22 Aug 2002 11:11:01 -0000
@@ -5,7 +5,7 @@
SHSRCS= alias.c arith.y arith_lex.l cd.c echo.c error.c eval.c exec.c expand.c \
histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c \
mystring.c options.c output.c parser.c printf.c redir.c show.c \
- test.c trap.c var.c
+ str.c test.c tr.c trap.c var.c
GENSRCS= builtins.c init.c nodes.c syntax.c
GENHDRS= builtins.h nodes.h syntax.h token.h y.tab.h
SRCS= ${SHSRCS} ${GENSRCS} ${GENHDRS} y.tab.h
@@ -24,7 +24,8 @@
.PATH: ${.CURDIR}/bltin \
${.CURDIR}/../../usr.bin/printf \
- ${.CURDIR}/../../bin/test
+ ${.CURDIR}/../../bin/test \
+ ${.CURDIR}/../../usr.bin/tr
CLEANFILES+= mkinit mkinit.o mknodes mknodes.o \
mksyntax mksyntax.o
Index: bin/sh/builtins.def
===================================================================
RCS file: /home/ncvs/src/bin/sh/builtins.def,v
retrieving revision 1.7.2.1
diff -u -r1.7.2.1 builtins.def
--- bin/sh/builtins.def 15 Dec 2001 10:05:18 -0000 1.7.2.1
+++ bin/sh/builtins.def 21 Aug 2002 19:50:15 -0000
@@ -91,3 +91,4 @@
aliascmd alias
ulimitcmd ulimit
testcmd test [
+trcmd tr
Index: usr.bin/tr/tr.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/tr/tr.c,v
retrieving revision 1.8
diff -u -r1.8 tr.c
--- usr.bin/tr/tr.c 28 Aug 1999 01:06:52 -0000 1.8
+++ usr.bin/tr/tr.c 21 Aug 2002 19:59:21 -0000
@@ -97,6 +97,11 @@
static void setup __P((int *, char *, STR *, int));
static void usage __P((void));
+#ifdef SHELL
+#define main trcmd
+#define exit return
+#include "bltin/bltin.h"
+#endif
int
main(argc, argv)
int argc;
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020822045603.A66453>
