From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20837 invoked from network); 24 Jan 2009 15:59:56 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00 autolearn=ham version=3.2.5 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 24 Jan 2009 15:59:56 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 77148 invoked from network); 24 Jan 2009 15:59:50 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 24 Jan 2009 15:59:50 -0000 Received: (qmail 29385 invoked by alias); 24 Jan 2009 15:59:44 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 26414 Received: (qmail 29371 invoked from network); 24 Jan 2009 15:59:44 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 24 Jan 2009 15:59:44 -0000 Received: from mail-fx0-f31.google.com (mail-fx0-f31.google.com [209.85.220.31]) by bifrost.dotsrc.org (Postfix) with ESMTP id 6966080271F0 for ; Sat, 24 Jan 2009 16:59:36 +0100 (CET) Received: by fxm12 with SMTP id 12so1482825fxm.21 for ; Sat, 24 Jan 2009 07:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:subject:date :user-agent:references:in-reply-to:mime-version:content-type :content-transfer-encoding:message-id; bh=/JYwS0CrvbGuZzGqyQSDeNSWS1ktZ4h/L/1OV8QJy8E=; b=dIgxEvVZOyb4XmV/BCJ+vqhlUv7AXTWjMSClGykTBSijK+K+6Njz9MVk1U7HKK3oky UrxDwN3QnrfJroYNBRx9G6DBG4S5KS6bG8uU3dTX/4OpYLqVtzekZ1exJaz8o3vufySq JvGiJHHqMynCfJePspOvqZae0a2I15BtkoMrE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:subject:date:user-agent:references:in-reply-to:mime-version :content-type:content-transfer-encoding:message-id; b=wl2vnKpUc7I2V3m1sxOSDBCf8WRKlfJCCbqJvTaD0PV+vRDnWm3uC+OKWVEYdEVVdP efx3cDwD2QXOUCuoH7RDZu+aoxhkFL0uZE4J4ROc1I44LF6IO/G4WQz/83iDKfWmPzW2 eKSrwh68Maf4gSRJ7M4RV+OTYk/AfQWK9Yzuw= Received: by 10.86.96.18 with SMTP id t18mr649571fgb.56.1232812775839; Sat, 24 Jan 2009 07:59:35 -0800 (PST) Received: from cooker.localnet (ppp91-77-251-164.pppoe.mtu-net.ru [91.77.251.164]) by mx.google.com with ESMTPS id 12sm10820889fgg.46.2009.01.24.07.59.34 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 24 Jan 2009 07:59:35 -0800 (PST) From: Andrey Borzenkov To: zsh-workers@sunsite.dk Subject: Re: sourcing a sh file in zsh Date: Sat, 24 Jan 2009 18:59:27 +0300 User-Agent: KMail/1.11.0 (Linux/2.6.29-rc2-1avb; KDE/4.1.96; i686; ; ) References: <200901161939.54651.arvidjaar@newmail.ru> <090116102934.ZM22119@torch.brasslantern.com> In-Reply-To: <090116102934.ZM22119@torch.brasslantern.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart28043924.n9KzYjTc5k"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200901241859.30029.arvidjaar@gmail.com> X-Virus-Scanned: ClamAV 0.92.1/8899/Sat Jan 24 14:06:33 2009 on bifrost X-Virus-Status: Clean --nextPart28043924.n9KzYjTc5k Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 16 =D1=8F=D0=BD=D0=B2=D0=B0=D1=80=D1=8F 2009 21:29:34 Bart Schaefer wrot= e: > Another possibility would be to extend the "emulate" command into a > precommand modifier sort of thing, where arguments following the name > of the emulation are treated as a command to execute. Perhaps > require another option to make this active, to avoid any compatibily > issue. > > emulate -LRE sh source file.sh > What about the following patch. It simply eval's any code after setting=20 requested emulation. As a bonus it prints current emulation if no argument= =20 is specified. Neither should be of compatibility issues as emulate always=20 allowed exactly one argument. Comments? Index: Doc/Zsh/builtins.yo =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v retrieving revision 1.114 diff -u -p -r1.114 builtins.yo =2D-- Doc/Zsh/builtins.yo 18 Dec 2008 09:49:04 -0000 1.114 +++ Doc/Zsh/builtins.yo 24 Jan 2009 15:40:16 -0000 @@ -338,8 +338,11 @@ cindex(compatibility, csh) cindex(sh, compatibility) cindex(ksh, compatibility) cindex(csh, compatibility) =2Ditem(tt(emulate) [ tt(-LR) ] {tt(zsh)|tt(sh)|tt(ksh)|tt(csh)})( =2DSet up zsh options to emulate the specified shell as much as possible. +item(tt(emulate) [ tt(-ELR) ] [ {tt(zsh)|tt(sh)|tt(ksh)|tt(csh)} [ tt(arg)= =20 =2E.. ] ])( +Without any argument print current emulation mode. + +With single argument set up zsh options to emulate the specified shell +as much as possible. bf(csh) will never be fully emulated. If the argument is not one of the shells listed above, tt(zsh) will be used as a default; more precisely, the tests performed on the @@ -351,12 +354,19 @@ the section `Compatibility' in zmanref(z ifnzman(\ noderef(Compatibility) )\ =2D. If the tt(-R) option is given, all options +. + +If the tt(-E) option is given, arguments that follow +emulation mode are evaluated using tt(eval) after setting requested +emulation.=20 +If the tt(-R) option is given, all options are reset to their default value corresponding to the specified emulation mode, except for certain options describing the interactive environment; otherwise, only those options likely to cause portability =2Dproblems in scripts and functions are altered. If the tt(-L) option =2Dis given, the options tt(LOCAL_OPTIONS) and tt(LOCAL_TRAPS) will be set = as +problems in scripts and functions are altered. The tt(-L) option together +with tt(-E) causes emulation mode to be restored after evaluating the rest +of arguments; without tt(-E), the options tt(LOCAL_OPTIONS) and=20 tt(LOCAL_TRAPS) +will be set as well, causing the effects of the tt(emulate) command and any tt(setopt) and tt(trap) commands to be local to the immediately surrounding shell function, if any; normally these options are turned off in all emulation Index: Src/builtin.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v retrieving revision 1.218 diff -u -p -r1.218 builtin.c =2D-- Src/builtin.c 12 Nov 2008 12:57:26 -0000 1.218 +++ Src/builtin.c 24 Jan 2009 15:40:17 -0000 @@ -58,7 +58,7 @@ static struct builtin builtins[] =3D BUILTIN("disable", 0, bin_enable, 0, -1, BIN_DISABLE, "afmrs", NULL), BUILTIN("disown", 0, bin_fg, 0, -1, BIN_DISOWN, NULL, NULL), BUILTIN("echo", BINF_SKIPINVALID, bin_print, 0, -1, BIN_ECHO, "neE",=20 "-"), =2D BUILTIN("emulate", 0, bin_emulate, 1, 1, 0, "LR", NULL), + BUILTIN("emulate", 0, bin_emulate, 0, -1, 0, "ELR", NULL), BUILTIN("enable", 0, bin_enable, 0, -1, BIN_ENABLE, "afmrs", NULL), BUILTIN("eval", BINF_PSPECIAL, bin_eval, 0, -1, BIN_EVAL, NULL, NULL), BUILTIN("exit", BINF_PSPECIAL, bin_break, 0, 1, BIN_EXIT, NULL, NULL), @@ -4744,24 +4744,12 @@ bin_dot(char *name, char **argv, UNUSED( return ret ? ret : lastval; } =20 =2D/**/ =2Dint =2Dbin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func= )) =2D{ =2D emulate(*argv, OPT_ISSET(ops,'R')); =2D if (OPT_ISSET(ops,'L')) =2D opts[LOCALOPTIONS] =3D opts[LOCALTRAPS] =3D 1; =2D return 0; =2D} =2D =2D/* eval: simple evaluation */ =2D =2D/**/ =2Dmod_export int ineval; +/* + * common for bin_emulate and bin_eval + */ =20 =2D/**/ =2Dint =2Dbin_eval(UNUSED(char *nam), char **argv, UNUSED(Options ops), UNUSED(int= =20 func)) +static int +eval(char **argv) { Eprog prog; char *oscriptname =3D scriptname; @@ -4838,6 +4826,83 @@ bin_eval(UNUSED(char *nam), char **argv, return lastval; } =20 +/* emulate: set emulation mode and optionally evaluate shell code */ + +/**/ +int +bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func)) +{ + int opt_E =3D OPT_ISSET(ops, 'E'); + int opt_L =3D OPT_ISSET(ops, 'L'); + int opt_R =3D OPT_ISSET(ops, 'R'); + int saveemulation ; + int ret; + char saveopts[OPT_SIZE]; + + /* without arguments just print current emulation */ + if (!*argv) { + if (opt_E || opt_L || opt_R) { + zwarnnam("emulate", "not enough arguments"); + return 1; + } + + printf("%s\n", emulation =3D=3D EMULATE_CSH ? "csh" : + emulation =3D=3D EMULATE_KSH ? "ksh" : + emulation =3D=3D EMULATE_SH ? "sh" : + "zsh"); + return 0; + } + + /* with single argument set current emulation */ + if (!*(argv+1)) { + if (opt_E) { + zwarnnam("emulate", "not enough arguments"); + return 1; + } + + emulate(*argv, OPT_ISSET(ops,'R')); + if (OPT_ISSET(ops,'L')) + opts[LOCALOPTIONS] =3D opts[LOCALTRAPS] =3D 1; + return 0; + } + + /* If more than one argument is given, first one is emulation + * and subsequent are eval'ed using specified emulation mode. + * If option -L is given, emulation restored after evaluation. + */ + if (!opt_E) { + zwarnnam("emulate", "too many arguments"); + return 1; + } + + if (opt_L) { + memcpy(saveopts, opts, sizeof(opts)); + saveemulation =3D emulation; + } + emulate(*argv, OPT_ISSET(ops,'R')); + ret =3D eval(argv+1); + if (opt_L) { + /* restore all shell options except PRIVILEGED and RESTRICTED */ + saveopts[PRIVILEGED] =3D opts[PRIVILEGED]; + saveopts[RESTRICTED] =3D opts[RESTRICTED]; + memcpy(opts, saveopts, sizeof(opts)); + emulation =3D saveemulation; + } + return ret; +} + +/* eval: simple evaluation */ + +/**/ +mod_export int ineval; + +/**/ +int +bin_eval(UNUSED(char *nam), char **argv, UNUSED(Options ops), UNUSED(int=20 func)) +{ + return eval(argv); +} + static char *zbuf; static int readfd; =20 --nextPart28043924.n9KzYjTc5k Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEABECAAYFAkl7OuEACgkQR6LMutpd94w5QQCfSilGUJ7G+1dBVBJxazbBUR1r 7EUAnAifIMgnFZMfMi952F5kBLQ2L/0u =C8bs -----END PGP SIGNATURE----- --nextPart28043924.n9KzYjTc5k--