From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10794 invoked from network); 9 Apr 2023 13:30:54 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 9 Apr 2023 13:30:54 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1681047054; b=UIbBmoV4hK35dN6xAPmC+wEaqq9JFa4jnlwWKG0+J06tbj+cI9Hf/wj3f+/xBVamuhDTHahuak x/DqemQCWvpmhKbsP584ZAE3dzgjsrOLsO5I9Knr8lH3kjAaoyOujxmd/7GiSnwDl+mbTDHd9u a04sFGJKGYfGVcClefLhswMmNMmIQWKl26uAB5ytMGvaduHZ82zJWbzk36gAfznAFDdjyA1aHV riEQq+mebKiC8fnoqp0Gyr2UwWo78D/n9xE3w/O5TX2/yZl+a6mzfPBpfS1/MoH5xbZCr9Pg2y 66Hyube57MmnoVeNkISxiUJ67JyT8CFIMTEqTMuDpmq9qA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (snd01002-bg.im.kddi.ne.jp) smtp.remote-ip=27.86.113.18; dmarc=none header.from=kba.biglobe.ne.jp; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1681047054; bh=Gct40eUUqBDjeRfIvGoYXex4vul82Hrqmrvl+Ww/lw4=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Message-ID:In-Reply-To:To:References:Date:Subject: MIME-Version:Content-Transfer-Encoding:Content-Type:From:DKIM-Signature; b=Mr/8u2zNchr81hJJKGUdduIyuknhKiZz9q3+6rHJj8y35PobpmZTal5DI0+xyoldhDMz1LEaja XtTR/dDFMLuxJn6og7/f1cyFXHGEvAWZa2JuhagZ111dMO5G7dkD1wdAocBKaw1uDAfvtvbknt so2xnE4Z9I6klNOscJH64ZQlIKR/B9L0LAylBPEClK32qvZbcJluaJ6Jzgyx0kKjjD0XIOHivS ipesitJmZo2UqCNSxsFJtPHB7rew+LbNTmG6riykgrrjZHkO/JAO+JE1vJsytmjQzu7KYvimyK QM6YDH47aWZTcVW/ReIcZrfyHo89ztFUrHJpryl2GNKivQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-Id:In-Reply-To:To:References: Date:Subject:Mime-Version:Content-Transfer-Encoding:Content-Type:From: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=DON9XzN2AfYjg8Y4Y3I0gur8uCPdQD9dw3f+2shqXQw=; b=h2tmB/TMNF7UoSeQc1w9fznZFV pbJBc9acm2+oLYKsNecBGbHq+DAwX1myPKd3jzW9+dDGC1lug7jjGMHwXjed03cUZJDuEdxi4LJdr y6vL6LslasCoKZku+8I3wA0B9AY7dQY1FU7qTgUsHXFrdhAN47RoBCEqMV2g/FUOc/I+cU9y/2cVX MS+i+YlbgeFi2Ib5Z+0XTFPGDFpNLkjsexm0A2IDhYHazjIgPG51fR++Q4rV85l+Q5Du/XvHFXUzi 9hBPW3QMnRnNk487TOS7nhRbS5HZn7PsNYw9wQ2spjnpRWQ+6mQrwuHp/Y7EEMH/NJldRaxQFQKYy RmptkBtA==; Received: by zero.zsh.org with local id 1plV88-00095v-1y; Sun, 09 Apr 2023 13:30:52 +0000 Authentication-Results: zsh.org; iprev=pass (snd01002-bg.im.kddi.ne.jp) smtp.remote-ip=27.86.113.18; dmarc=none header.from=kba.biglobe.ne.jp; arc=none Received: from snd01002-bg.im.kddi.ne.jp ([27.86.113.18]:59617 helo=dfmta1028.biglobe.ne.jp) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1plV7W-0008mw-LW; Sun, 09 Apr 2023 13:30:17 +0000 Received: from mail.biglobe.ne.jp by omta1028.biglobe.ne.jp with ESMTP id <20230409133008379.FVQB.53494.mail.biglobe.ne.jp@biglobe.ne.jp> for ; Sun, 9 Apr 2023 22:30:08 +0900 From: "Jun. T" Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.2\)) Subject: Re: ksh compatibility: initial value of $_ Date: Sun, 9 Apr 2023 22:30:07 +0900 References: <2A0716F2-F80B-4357-87AA-0A9881A52CEC@kba.biglobe.ne.jp> <838816ad-0337-452b-94d1-e18a17c33c78@app.fastmail.com> <739C36BD-E3A5-40A4-B661-2AC54D32C2C1@kba.biglobe.ne.jp> <2e5f16a4-ba7c-4992-9d25-80214bd5d025@app.fastmail.com> <91076-1680970954.277199@scD9.nSRV.LVCF> To: zsh-workers@zsh.org In-Reply-To: <91076-1680970954.277199@scD9.nSRV.LVCF> Message-Id: <49E9BC96-C3F6-404B-8089-68FBD5960467@kba.biglobe.ne.jp> X-Mailer: Apple Mail (2.3696.120.41.1.2) X-Biglobe-Sender: takimoto-j@kba.biglobe.ne.jp X-Seq: 51639 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: > 2023/04/09 1:22=E3=80=81Oliver Kiddle =E3=81=AE=E3=83=A1=E3= =83=BC=E3=83=AB: >=20 > dana wrote: >> Maybe ZSH_EXECUTABLE? That kind of matches ZSH_SCRIPT too >=20 > That sounds good. Unless we want to take advantage of Bart's recent > changes and put it in a namespace. In the path below I used ZSH_EXEPATH to make it (somewhat) easier to guess that it is a full pathname, but ZSH_EXECUTABLE (or any other name) is OK for me. If it will be put in a namespace, is it available only in the namespace, or also in the toplevel namespace? diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo index 2db4210eb..08b3e79f6 100644 --- a/Doc/Zsh/params.yo +++ b/Doc/Zsh/params.yo @@ -1112,6 +1112,10 @@ item(tt(ZSH_EXECUTION_STRING))( If the shell was started with the option tt(-c), this contains the argument passed to the option. Otherwise it is not set. ) +vindex(ZSH_EXEPATH) +item(tt(ZSH_EXEPATH))( +Full pathname of the executable file of current zsh process. +) vindex(ZSH_NAME) item(tt(ZSH_NAME))( Expands to the basename of the command used to invoke this instance diff --git a/Src/init.c b/Src/init.c index 7e98af44c..f8acf2ff1 100644 --- a/Src/init.c +++ b/Src/init.c @@ -246,6 +246,9 @@ loop(int toplevel, int justonce) =20 static int restricted; =20 +/* original argv[0] for initialization of $_. this is already metafied = */ +static char *argv0; + /**/ static void parseargs(char *zsh_name, char **argv, char **runscript, char **cmdptr, @@ -257,7 +260,7 @@ parseargs(char *zsh_name, char **argv, char = **runscript, char **cmdptr, if (**argv =3D=3D '-') flags |=3D PARSEARGS_LOGIN; =20 - argzero =3D posixzero =3D *argv++; + argv0 =3D argzero =3D posixzero =3D *argv++; SHIN =3D 0; =20 /* @@ -893,6 +896,106 @@ init_term(void) return 1; } =20 +/* + * Get (or guess) the absolute pathname of the current zsh exeutable. + * Try OS-specific method, and if it fails, guess the absolute pathname + * from argv0, pwd, and PATH. 'name' and 'cwd' are unmetefied versions = of + * argv0 and pwd. + * Returns a zalloc()ed string (not metafied), or NULL if failed. + */ +#ifdef __APPLE__ +#include +#endif + +/**/ +static char * +getmypath(const char *name, const char *cwd) +{ + char *buf; + int namelen; + + if (!name) + return NULL; + if (*name =3D=3D '-') + ++name; + if ((namelen =3D strlen(name) =3D=3D 0)) + return NULL; +#if defined(__APPLE__) + { + uint32_t n =3D PATH_MAX; + int ret; + buf =3D (char *)zalloc(PATH_MAX); + if ((ret =3D _NSGetExecutablePath(buf, &n)) < 0) { + /* try again with increased buffer size */ + buf =3D (char *)zrealloc(buf, n); + ret =3D _NSGetExecutablePath(buf, &n); + } + if (ret =3D=3D 0 && strlen(buf) > 0) + return buf; + else + free(buf); + } +#elif defined(PROC_SELF_EXE) + { + ssize_t n; + buf =3D (char *)zalloc(PATH_MAX); + n =3D readlink(PROC_SELF_EXE, buf, PATH_MAX); + if (n > 0 && n < PATH_MAX) { + buf[n] =3D '\0'; + return buf; + } + else + free(buf); + } +#endif + /* guess the absolute pathname of 'name' */ + if (name[namelen-1] =3D=3D '/') /* name should not end with '/' = */ + return NULL; + else if (name[0] =3D=3D '/') { + /* name is already an absolute pathname */ + return ztrdup(name); + } + else if (strchr(name, '/')) { + /* relative path */ + if (!cwd) + return NULL; + buf =3D (char *)zalloc(strlen(cwd) + namelen + 2); + sprintf(buf, "%s/%s", cwd, name); + return buf; + } +#ifdef HAVE_REALPATH + else { + /* search each dir in PARH */ + const char *path, *sep; + char *real, *try; + int pathlen, dirlen; + + path =3D getenv("PATH"); + if (!path || (pathlen =3D strlen(path)) =3D=3D 0) + return NULL; + /* for simplicity, allocate buf even if REALPATH_ACCEPTS_NULL is = on */ + buf =3D (char *)zalloc(PATH_MAX); + try =3D (char *)zalloc(pathlen + namelen + 2); + do { + sep =3D strchr(path, ':'); + dirlen =3D sep ? sep - path : strlen(path); + strncpy(try, path, dirlen); + try[dirlen] =3D '/'; + try[dirlen+1] =3D '\0'; + strcat(try, name); + real =3D realpath(try, buf); + if (sep) + path =3D sep + 1; + } while (!real && sep); + free(try); + if (!real) + free(buf); + return real; /* this may be NULL */ + } +#endif + return NULL; +} + /* Initialize lots of global variables and hash tables */ =20 /**/ @@ -1195,6 +1298,18 @@ setupvals(char *cmd, char *runscript, char = *zsh_name) /* Colour sequences for outputting colours in prompts and zle */ set_default_colour_sequences(); =20 + /* ZSH_EXEPATH */ + { + char *mypath, *exename, *cwd; + exename =3D unmetafy(ztrdup(argv0), NULL); + cwd =3D pwd ? unmetafy(ztrdup(pwd), NULL) : NULL; + mypath =3D getmypath(exename, cwd); + free(exename); + free(cwd); + if (mypath) { + setsparam("ZSH_EXEPATH", metafy(mypath, -1, META_REALLOC)); + } + } if (cmd) setsparam("ZSH_EXECUTION_STRING", ztrdup(cmd)); if (runscript) diff --git a/configure.ac b/configure.ac index e6ced85d9..d8a17791a 100644 --- a/configure.ac +++ b/configure.ac @@ -2011,6 +2011,25 @@ if test x$zsh_cv_sys_path_dev_fd !=3D xno; then AC_DEFINE_UNQUOTED(PATH_DEV_FD, "$zsh_cv_sys_path_dev_fd") fi =20 +dnl ---------------------------------------------------- +dnl CHECK FOR SYMLINK TO THE CURRENT EXECUTABLE IN /proc +dnl ---------------------------------------------------- +dnl Linux: /proc/self/exe +dnl NetBSD: /proc/curproc/exe (or /proc/self/exe, but not = /proc/curproc/file) +dnl DragonFly: /proc/curproc/file +dnl Solaris: /proc/self/path/a.out +AH_TEMPLATE([PROC_SELF_EXE], +[Define to the path of the symlink to the current executable file.]) +AC_CACHE_CHECK(for symlink to the current executable in /proc, +zsh_cv_proc_self_exe, +[for zsh_cv_proc_self_exe in /proc/self/exe /proc/curproc/exe \ + /proc/curproc/file /proc/self/path/a.out = no; do + test -L $zsh_cv_proc_self_exe && break +done]) +if test x$zsh_cv_proc_self_exe !=3D xno; then + AC_DEFINE_UNQUOTED(PROC_SELF_EXE, "$zsh_cv_proc_self_exe") +fi + dnl --------------------------------- dnl CHECK FOR RFS SUPERROOT DIRECTORY dnl ---------------------------------