zsh-workers
 help / color / mirror / code / Atom feed
* whence following symlinks
@ 1997-04-10 21:04 Juergen A. Erhard
  1997-04-11 14:10 ` Zefram
  0 siblings, 1 reply; 3+ messages in thread
From: Juergen A. Erhard @ 1997-04-10 21:04 UTC (permalink / raw)
  To: zsh-workers

-----BEGIN PGP SIGNED MESSAGE-----

My first zsh hack.

Ever did a 'which sh', and wondered whether '/bin/sh' was REALLY just
'/bin/sh' or not, rather, '/bin/bash'... honest, how many systems do
YOU know that have a '/bin/sh'? Is your's NOT linked to '/bin/zsh'?
Shame on you ;-)

Okay. Now with this patch, 'which -s' follows symlinks and prints
them, separated by ' -> ', after the first path found.

Unfortunately, 'whence -ms' doesn't work as expected, because '-m'
jumps to a different code section, and that code doesn't care for
'-s'.

To get 'whence -ms', do something like this

  whence -m '*sh' | sed 's/.*/whence -s 

This at least works for me.

Another thing missing is checking for hard links... but that is a lot
more complicated, next to impossible. At least we might check whether
the path found has more than one name (hard links > 1), to give the
user an idea that this might not be what it seems to be.

And now, here's the patch, done with 'diff -cp', relative to zsh 3.1.0.

Enjoy -- JAE

*** hashtable.h	1997/03/19 09:45:12	1.1
- --- hashtable.h	1997/03/19 10:07:07
*************** struct builtin builtins[] =
*** 338,346 ****
      {NULL, "unset", BINF_PSPECIAL, bin_unset, 1, -1, 0, "fm", NULL},
      {NULL, "unsetopt", 0, bin_setopt, 0, -1, BIN_UNSETOPT, NULL, NULL},
      {NULL, "wait", 0, bin_fg, 0, -1, BIN_WAIT, NULL, NULL},
!     {NULL, "whence", 0, bin_whence, 0, -1, 0, "acmpvf", NULL},
      {NULL, "where", 0, bin_whence, 0, -1, 0, "pm", "ca"},
!     {NULL, "which", 0, bin_whence, 0, -1, 0, "amp", "c"},
      {NULL, NULL}
  };
  #else
- --- 338,346 ----
      {NULL, "unset", BINF_PSPECIAL, bin_unset, 1, -1, 0, "fm", NULL},
      {NULL, "unsetopt", 0, bin_setopt, 0, -1, BIN_UNSETOPT, NULL, NULL},
      {NULL, "wait", 0, bin_fg, 0, -1, BIN_WAIT, NULL, NULL},
!     {NULL, "whence", 0, bin_whence, 0, -1, 0, "acmpvfs", NULL},
      {NULL, "where", 0, bin_whence, 0, -1, 0, "pm", "ca"},
!     {NULL, "which", 0, bin_whence, 0, -1, 0, "amps", "c"},
      {NULL, NULL}
  };
  #else
*** builtin.c	1997/03/19 07:48:38	1.1
- --- builtin.c	1997/04/09 15:09:10
***************
*** 2244,2252 ****
  	} else if ((cnam = findcmd(*argv))) {
  	    /* Found external command. */
  	    if (v && !csh)
! 		printf("%s is %s\n", *argv, cnam);
  	    else
! 		puts(cnam);
  	    zsfree(cnam);
  	} else {
  	    /* Not found at all. */
- --- 2244,2278 ----
  	} else if ((cnam = findcmd(*argv))) {
  	    /* Found external command. */
  	    if (v && !csh)
! 		printf("%s is %s", *argv, cnam);
  	    else
! 		fputs(cnam,stdout);
! 
! 	    /* now check symlinks if -s given */
! 	    if (ops['s']) {
! 		/* temporary vars for this if */
! 		char *try_link;
! 		int chase;
! 		
! 		/* temporarily set CHASELINKS */
! 		chase = isset(CHASELINKS);
! 		dosetopt(CHASELINKS, 1, 0);
! 		
! 		/* get possible link */
! 		try_link = xsymlink(cnam);
! 
! 		/* if it is one (different from cnam), print it */
! 		if (strcmp(cnam,try_link)!=0)
! 		    printf(" -> %s",try_link);
! 		
! 		/* revert setting of CHASELINKS */
! 		dosetopt(CHASELINKS, chase, 0);
! 
! 		/* throw away try_link... hope this is the right fn */
! 		zsfree(try_link);
! 	    }
! 
! 	    fputc('\n',stdout);
  	    zsfree(cnam);
  	} else {
  	    /* Not found at all. */

- -- 
Juergen A. Erhard (that ue is actually \"u (TeX) or ü (HTML/SGML))
eMail: jae@laden.ilk.de                             |
snailMail: Belfortstr. 6; 76133 Karlsruhe; GERMANY  |  pros do it for money,
phone: +49 721 27326                                |  amateurs out of love.
WWW: http://members.tripod.com/~Juergen_Erhard/  (under construction!)

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: noconv
Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface

iQEVAwUBM0u7dgIG66LugGzRAQE6uAf6AkR+eDriEOB9/zWFKwL+d2nsq/V772pL
nh2XaeqesKGIzvFN106uHIXDVj01JtbZjWQKVtPn9Alb1tkIpBGDxU0cHQ+qo1eR
uNpdeoWxwl9kbYNgZar38YI0TXm7+RvoEnIR27ADp1cplBYunOcReezCEAB7RhME
K5cMQBTP8swNrY8WCfxt/cAyKXL1PkkjLSbbn/8hEIVnrJMKcr30tWtLzgpD8SF+
veW6oOTCmmybFljODWAOS9/BRfmkqH8XV5dKdMJupvWYJDw8K3FVV7ewDUwamJfb
vxufZ6k77VOFySWCcEid3bnUsZ8/tPOkXF4M6aP7FKp0EWJZFi6AjQ==
=SHwK
-----END PGP SIGNATURE-----


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

* Re: whence following symlinks
  1997-04-10 21:04 whence following symlinks Juergen A. Erhard
@ 1997-04-11 14:10 ` Zefram
  1997-04-21 14:34   ` Juergen A. Erhard
  0 siblings, 1 reply; 3+ messages in thread
From: Zefram @ 1997-04-11 14:10 UTC (permalink / raw)
  To: Juergen A. Erhard; +Cc: zsh-workers

Juergen A. Erhard wrote:
>Unfortunately, 'whence -ms' doesn't work as expected, because '-m'
>jumps to a different code section, and that code doesn't care for
>'-s'.

You could duplicate the new code, or use the hashtable printnode() method.

>! 		/* temporarily set CHASELINKS */
>! 		chase = isset(CHASELINKS);
>! 		dosetopt(CHASELINKS, 1, 0);
...
>! 		/* revert setting of CHASELINKS */
>! 		dosetopt(CHASELINKS, chase, 0);

Do

	chase = opts[CHASELINKS];
	opts[CHASELINKS] = 1;
	...
	opts[CHASELINKS] = chase;

>! 		/* throw away try_link... hope this is the right fn */
>! 		zsfree(try_link);

It is the right function.

-zefram


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

* Re: whence following symlinks
  1997-04-11 14:10 ` Zefram
@ 1997-04-21 14:34   ` Juergen A. Erhard
  0 siblings, 0 replies; 3+ messages in thread
From: Juergen A. Erhard @ 1997-04-21 14:34 UTC (permalink / raw)
  To: zefram; +Cc: zsh-workers

-----BEGIN PGP SIGNED MESSAGE-----

>>>>> "Zefram" == Zefram  <zefram@dcs.warwick.ac.uk> writes:

    Zefram> Juergen A. Erhard wrote:
    >> Unfortunately, 'whence -ms' doesn't work as expected, because '-m'
    >> jumps to a different code section, and that code doesn't care for
    >> '-s'.

    Zefram> You could duplicate the new code, or use the hashtable printnode() method.

Okay, I'll check this printnode() out...

    >> ! 		/* temporarily set CHASELINKS */
    >> ! 		chase = isset(CHASELINKS);
    >> ! 		dosetopt(CHASELINKS, 1, 0);
    Zefram> ...
    >> ! 		/* revert setting of CHASELINKS */
    >> ! 		dosetopt(CHASELINKS, chase, 0);

    Zefram> Do

    Zefram> 	chase = opts[CHASELINKS];
    Zefram> 	opts[CHASELINKS] = 1;
    Zefram> 	...
    Zefram> 	opts[CHASELINKS] = chase;

Well, there's a reason to my madness ;-)...

I simply don't like directly editing some data structures that might
or might not transmogrify into something completely different...

On the other hand, you're right in your (implied) statement that
dosetopt might carry too much overhead...

So, if TPTB gives me the ok to use your suggestion (that is,
they/he/she/it guarantees opts to stay a simple int[]), I will.

    >> ! 		/* throw away try_link... hope this is the right fn */
    >> ! 		zsfree(try_link);

    Zefram> It is the right function.

Thanks. I'd really love to see a couple more comments on this
beast. Especially on fns like 'xsymlink'... I had to RTFS to find out
I had to free(3) the result... and had to guess (more or less) on
zsfree (the doc on the mem-mgt could be better... or maybe I could be
smarter ;-)

    Zefram> -zefram

Bye, J

- -- 
Juergen A. Erhard (that ue is actually \"u (TeX) or &uuml; (HTML/SGML))
eMail: jae@laden.ilk.de                             |
snailMail: Belfortstr. 6; 76133 Karlsruhe; GERMANY  |  pros do it for money,
phone: +49 721 27326                                |  amateurs out of love.
WWW: http://members.tripod.com/~Juergen_Erhard/     |

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: noconv
Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface

iQEVAwUBM0/sAwIG66LugGzRAQH+AwgAn3nUBG6elVvRrbKBGPv9xfYzReQxT8k0
hdvJhzPwJR6gXGREjcwEs1+agl/tvE/FENt+7eG3QtKCPo/1jlZTWX1mkkLFUsox
qabkAB42xn0eLHy9MFVpA4sKA5uh1XeOEZ1rRSU/QsN8RRGcWqzc3PiWgDt+dukq
3AZQ32vPYMMersdy9VA5kGCXjDDv5t0w6XoaIZKxBlBjGcYJxRYHrwtZpi0QhQUH
2Mm9+VTLux0ZcqklZ3NO5D7le+W6N0vkchjN7hH2pF1MADcXoxaKSShFgBmHy8XK
k2KbA6iuYBO/1ksgrrfHnkuaoKwNVH6Miyk9WsE3f3GBXkLO+ydUbw==
=ocZm
-----END PGP SIGNATURE-----


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

end of thread, other threads:[~1997-04-21 14:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-10 21:04 whence following symlinks Juergen A. Erhard
1997-04-11 14:10 ` Zefram
1997-04-21 14:34   ` Juergen A. Erhard

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).