zsh-workers
 help / color / mirror / code / Atom feed
* Re: zsh portable script
       [not found] ` <20100712170648.706cb975@csr.com>
@ 2010-07-13 20:01   ` Peter Stephenson
  2010-07-13 21:08     ` Benjamin R. Haskell
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2010-07-13 20:01 UTC (permalink / raw)
  To: Zsh Hackers' List

On Mon, 12 Jul 2010 17:06:48 +0100
Peter Stephenson <Peter.Stephenson@csr.com> wrote:
> Looking at the responses, on which I can't improve, one might speculate it
> was time for someone to implement path searching down in the #! handler for
> relative paths.
>...
> This doesn't really touch zsh except that we could do it in our own
> #! handler (see zexecve() in Src/exec.c).w

Straightforward, not sure if anyone's interested since this only
addresses a fairly tiny part of the problem...

Note if zsh is in the current directory it tries that first.  That maintains compatibility
with how it used to work (whether intentionally or not).

% cat execme 
#!zsh -f
print I am running under $ZSH_VERSION
% PATH=/bin ./execme
I am running under 4.3.10
% PATH=/usr/local/bin ./execme
I am running under 4.3.10-dev-1

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.180
diff -p -u -r1.180 exec.c
--- Src/exec.c	14 Jun 2010 11:57:08 -0000	1.180
+++ Src/exec.c	13 Jul 2010 19:54:41 -0000
@@ -461,8 +461,15 @@ zexecve(char *pth, char **argv, char **n
 			for (ptr = execvebuf + 2; *ptr && *ptr == ' '; ptr++);
 			for (ptr2 = ptr; *ptr && *ptr != ' '; ptr++);
 			if (eno == ENOENT) {
+			    char *pprog;
 			    if (*ptr)
 				*ptr = '\0';
+			    if (*ptr2 != '/' &&
+				(pprog = pathprog(ptr2, NULL))) {
+				argv[-2] = ptr2;
+				argv[-1] = ptr + 1;
+				execve(pprog, argv - 2, newenvp);
+			    }
 			    zerr("%s: bad interpreter: %s: %e", pth, ptr2,
 				 eno);
 			} else if (*ptr) {
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.115
diff -p -u -r1.115 init.c
--- Src/init.c	8 Jun 2010 08:51:04 -0000	1.115
+++ Src/init.c	13 Jul 2010 19:54:41 -0000
@@ -949,25 +949,7 @@ setupshin(char *runscript)
 	     * With the PATHSCRIPT option, search the path if no
 	     * path was given in the script name.
 	     */
-	    char **pp, ppmaxlen = 0, *buf;
-	    for (pp = path; *pp; pp++)
-	    {
-		int len = strlen(*pp);
-		if (len > ppmaxlen)
-		    ppmaxlen = len;
-	    }
-	    buf = zhalloc(ppmaxlen + strlen(runscript) + 2);
-	    for (pp = path; *pp; pp++) {
-		sprintf(buf, "%s/%s", *pp, runscript);
-		/* careful, static buffer used in open() later */
-		funmeta = unmeta(buf);
-		if (access(funmeta, F_OK) == 0 &&
-		    stat(funmeta, &st) >= 0 &&
-		    !S_ISDIR(st.st_mode)) {
-		    sfname = buf;
-		    break;
-		}
-	    }
+	    funmeta = pathprog(runscript, &sfname);
 	}
 	if (!sfname ||
 	    (SHIN = movefd(open(funmeta, O_RDONLY | O_NOCTTY)))
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.244
diff -p -u -r1.244 utils.c
--- Src/utils.c	11 Jun 2010 20:08:03 -0000	1.244
+++ Src/utils.c	13 Jul 2010 19:54:42 -0000
@@ -606,6 +606,44 @@ zwcwidth(wint_t wc)
 /**/
 #endif /* MULTIBYTE_SUPPORT */
 
+/*
+ * Search the path for prog and return the file name.
+ * The returned value is unmetafied and in the unmeta storage
+ * area (N.B. should be duplicated if not used immediately and not
+ * equal to *namep).
+ *
+ * If namep is not NULL, *namep is set to the metafied programme
+ * name, which is in heap storage.
+ */
+/**/
+char *
+pathprog(char *prog, char **namep)
+{
+    char **pp, ppmaxlen = 0, *buf, *funmeta;
+    struct stat st;
+
+    for (pp = path; *pp; pp++)
+    {
+	int len = strlen(*pp);
+	if (len > ppmaxlen)
+	    ppmaxlen = len;
+    }
+    buf = zhalloc(ppmaxlen + strlen(prog) + 2);
+    for (pp = path; *pp; pp++) {
+	sprintf(buf, "%s/%s", *pp, prog);
+	funmeta = unmeta(buf);
+	if (access(funmeta, F_OK) == 0 &&
+	    stat(funmeta, &st) >= 0 &&
+	    !S_ISDIR(st.st_mode)) {
+	    if (namep)
+		*namep = buf;
+	    return funmeta;
+	}
+    }
+
+    return NULL;
+}
+
 /* get a symlink-free pathname for s relative to PWD */
 
 /**/

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: zsh portable script
  2010-07-13 20:01   ` zsh portable script Peter Stephenson
@ 2010-07-13 21:08     ` Benjamin R. Haskell
  2010-07-14 10:33       ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin R. Haskell @ 2010-07-13 21:08 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Hackers' List

On Tue, 13 Jul 2010, Peter Stephenson wrote:

> On Mon, 12 Jul 2010 17:06:48 +0100 Peter Stephenson wrote:
> > Looking at the responses, on which I can't improve, one might 
> > speculate it was time for someone to implement path searching down 
> > in the #! handler for relative paths.
> >...
> > This doesn't really touch zsh except that we could do it in our own 
> > #! handler (see zexecve() in Src/exec.c).w
> 
> Straightforward, not sure if anyone's interested since this only 
> addresses a fairly tiny part of the problem...
> 
> Note if zsh is in the current directory it tries that first.  That 
> maintains compatibility with how it used to work (whether 
> intentionally or not).
> 
> % cat execme 
> #!zsh -f
> print I am running under $ZSH_VERSION
> % PATH=/bin ./execme
> I am running under 4.3.10
> % PATH=/usr/local/bin ./execme
> I am running under 4.3.10-dev-1

I don't quite understand... what does this improve?  Doesn't the script 
already have to be running under Zsh for this to have any effect?

How do most systems handle basenames in shebangs?

Using the same set of shells that I was using to test the 
weird-numbers-of-backslashes thing that came up not too long ago, only 
the decidedly non-standard psh, sash, and shish don't completely barf on 
it.  And even those three don't use it:

zsh$ cat test.script
#!zsh -f
print ZSH_VERSION $ZSH_VERSION
zsh$ for l in {ba,z,c,tc,,k,fi,p,da,mk,sa,shi,po}sh ; $l -c ./test.script && echo ok $l || echo nok $l
nok bash
nok zsh
nok csh
nok tcsh
nok sh
nok ksh
nok fish
ok psh
nok dash
nok mksh
ok sash
ok shish
nok posh
zsh$ for l in {p,sa,shi}sh ; $l -c ./test.script
Error - Could not exec /home/bhaskell/tmp/testshebang/test.script.
[1] Error 20533 ./test.script
./test.script: No such file or directory
shish: shish: ./test.script: Success


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: zsh portable script
  2010-07-13 21:08     ` Benjamin R. Haskell
@ 2010-07-14 10:33       ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2010-07-14 10:33 UTC (permalink / raw)
  To: Zsh Hackers' List

On Tue, 13 Jul 2010 17:08:42 -0400 (EDT)
"Benjamin R. Haskell" <zsh@benizi.com> wrote:
> > % cat execme 
> > #!zsh -f
> > print I am running under $ZSH_VERSION
> > % PATH=/bin ./execme
> > I am running under 4.3.10
> > % PATH=/usr/local/bin ./execme
> > I am running under 4.3.10-dev-1
> 
> I don't quite understand... what does this improve?  Doesn't the
> script already have to be running under Zsh for this to have any
> effect?

Yes, the script has to be started from an instance of zsh, that's exactly
why this has a limited effect.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-07-14 10:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1007130241570.5546@smasher>
     [not found] ` <20100712170648.706cb975@csr.com>
2010-07-13 20:01   ` zsh portable script Peter Stephenson
2010-07-13 21:08     ` Benjamin R. Haskell
2010-07-14 10:33       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).