zsh-users
 help / color / mirror / code / Atom feed
* Re: Making a script 'sourceable'
@ 2004-09-04 11:07 DervishD
  2004-09-04 15:44 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: DervishD @ 2004-09-04 11:07 UTC (permalink / raw)
  To: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> >     That given, I'm trying to do two things. First one is detecting
> > the 'sourcing' non portably, in zsh.
> It can be done if setopt NO_FUNCTION_ARG_ZERO is in effect before the
> script is sourced by comparing $0 either to $SHELL or to the name of
> the script.
    Oh, I forgot FUNCTION_ARGZERO. For scripts not intended to be
'exported' outside my system this will do, thanks a lot :)

> > thinking about now is to check the value of the option 'interactive'.
> > If it is on, chances are that we have sourced the script. Otherwise,
> > we may not. This is not perfect, though, because you can run a script
> > (interactive=off) and then source the other script within...
> If all your scripts check both the option and the state of $0 and then 
> explicitly turn off FUNCTION_ARG_ZERO, you're pretty much guaranteed to 
> catch direct sourcing the with the options and indirect sourcing with $0.

    I'll give it a try.
 
> >     The second thing is derived from the above question: since
> > checking for 'sourcery' ;) is very difficult even non portably, I've
> > thought about making my zsh scripts sourceables.
> Lloyd Z. has the way of it.
    Well, an extra fork... I don't really like that method, but... I
was thinking about writing the scripts carefully so they don't mess
with the current environment. The main problem seems to be the
environment variables and, in the case of zsh, the options (a
portable shell script won't mess with the options). But Lloyd's
solution is more reliable and the price, in modern OS's, is almost
negligible.

    Thanks for your help.

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Making a script 'sourceable'
  2004-09-04 11:07 Making a script 'sourceable' DervishD
@ 2004-09-04 15:44 ` Bart Schaefer
  2004-09-05 13:33   ` DervishD
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2004-09-04 15:44 UTC (permalink / raw)
  To: Zsh Users

On Sat, 4 Sep 2004, DervishD wrote:

> > >     The second thing is derived from the above question: since
> > > checking for 'sourcery' ;) is very difficult even non portably, I've
> > > thought about making my zsh scripts sourceables.
> > Lloyd Z. has the way of it.
>     Well, an extra fork... I don't really like that method, but...

There's also this, wherein a function name unlikely to exist in the 
calling shell is invented and then that function destroys itself as soon 
as it is invoked:

--- 8< ---
#! bin/zsh
function __the_real_script_$$ {
  unfunction __the_real_script_$$
  emulate -LR zsh
  # body of script goes here, using "local" to control variables
}
__the_real_script_$$ "$@"
--- >8 ---

However, that pretty thoroughly demolishes the usefulness of $0, and any 
error messages that are printed will fail to show the name of the script 
and the line numbers will be "wrong".

On the other hand this (and the subshell wrapper variant, too) has the 
advantage that the entire script is parsed for syntax before any of it is 
executed, so if you make a mistake somewhere you don't have half-finished
script processing to clean up.

Back on the first hand again, though, you pay the memory cost of that 
parse on every call to the script.


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

* Re: Making a script 'sourceable'
  2004-09-04 15:44 ` Bart Schaefer
@ 2004-09-05 13:33   ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2004-09-05 13:33 UTC (permalink / raw)
  To: zsh-users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> > > >     The second thing is derived from the above question: since
> > > > checking for 'sourcery' ;) is very difficult even non portably, I've
> > > > thought about making my zsh scripts sourceables.
> > > Lloyd Z. has the way of it.
> >     Well, an extra fork... I don't really like that method, but...
> There's also this, wherein a function name unlikely to exist in the 
> calling shell is invented and then that function destroys itself as soon 
> as it is invoked:
> 
> --- 8< ---
> #! bin/zsh
> function __the_real_script_$$ {
>   unfunction __the_real_script_$$
>   emulate -LR zsh
>   # body of script goes here, using "local" to control variables
> }
> __the_real_script_$$ "$@"
> --- >8 ---
> 
> However, that pretty thoroughly demolishes the usefulness of $0, and any 
> error messages that are printed will fail to show the name of the script 
> and the line numbers will be "wrong".

    So the method pointed by Lloyd is more suitable...
 
> On the other hand this (and the subshell wrapper variant, too) has the 
> advantage that the entire script is parsed for syntax before any of it is 
> executed, so if you make a mistake somewhere you don't have half-finished
> script processing to clean up.

    Yes, I noticed that this morning, doing tests :)) For me that
pays for the extra fork.
 
> Back on the first hand again, though, you pay the memory cost of that 
> parse on every call to the script.

    Usually the scripts are quite short: in fact, that's the reason
that made me think about auditing the scripts to avoid side-effects
when the script is sourced instead of run in a subshell.

    Thanks a lot for your help. I probably end up doing the '()'
thing, because the extra fork really is not very expensive, the only
problem is 'ps' output cluttering ;)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Making a script 'sourceable'
  2004-09-03 17:01 ` Lloyd Zusman
@ 2004-09-03 22:16   ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2004-09-03 22:16 UTC (permalink / raw)
  To: Lloyd Zusman; +Cc: zsh-users, DervishD

    Hi Lloyd :)

 * Lloyd Zusman <ljz@asfast.com> dixit:
> >     So I must, by hand, restore any changed variables, options and,
> > what more? Must I restore any other thing? Is there any simple way of
> > making scripts source-safe or source-aware?
> ¡Hola Raúl!  Espero que te encuentres muy bien.

    Igualmente, Lloyd, cuanto tiempo :))) (list people, sorry for the
spanish bit, just a couple of compliments).

    I'm very happy of reading you again :)

> I'm not sure if this handles the case that you're concerned about, but
> when I want to make a script "sourceable" as well as executable, I do
> this:
> 
>   #!/bin/zsh
>   (
>     # all script stuff goes here
>   )

    That's what I've done a couple of times, but I wanted to avoid
the extra fork... For what Bart says in another message in this
thread, it seems the only way of doing it :(

    Thanks a lot Lloyd :) BTW, you can practice your spanish whenever
you want, just drop me an email :) Un abrazo.
 
    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Making a script 'sourceable'
  2004-09-03 16:35 DervishD
  2004-09-03 17:01 ` Lloyd Zusman
@ 2004-09-03 17:20 ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2004-09-03 17:20 UTC (permalink / raw)
  To: Zsh Users

On Fri, 3 Sep 2004, DervishD wrote:

>     That given, I'm trying to do two things. First one is detecting
> the 'sourcing' non portably, in zsh.

It can be done if setopt NO_FUNCTION_ARG_ZERO is in effect before the
script is sourced by comparing $0 either to $SHELL or to the name of
the script.

> thinking about now is to check the value of the option 'interactive'.
> If it is on, chances are that we have sourced the script. Otherwise,
> we may not. This is not perfect, though, because you can run a script
> (interactive=off) and then source the other script within...

If all your scripts check both the option and the state of $0 and then 
explicitly turn off FUNCTION_ARG_ZERO, you're pretty much guaranteed to 
catch direct sourcing the with the options and indirect sourcing with $0.

Other options you might check:

	[[ -o SH_IN_STDIN ]]
	[[ -o MONITOR ]]

>     The second thing is derived from the above question: since
> checking for 'sourcery' ;) is very difficult even non portably, I've
> thought about making my zsh scripts sourceables.

Lloyd Z. has the way of it.


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

* Re: Making a script 'sourceable'
  2004-09-03 16:35 DervishD
@ 2004-09-03 17:01 ` Lloyd Zusman
  2004-09-03 22:16   ` DervishD
  2004-09-03 17:20 ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Lloyd Zusman @ 2004-09-03 17:01 UTC (permalink / raw)
  To: zsh-users; +Cc: DervishD

DervishD <disposable1@telefonica.net> writes:

> [ ... ]
>
>     So I must, by hand, restore any changed variables, options and,
> what more? Must I restore any other thing? Is there any simple way of
> making scripts source-safe or source-aware?
>
>     Thanks a lot in advance :)
>
>     Raúl Núñez de Arenas Coronado

¡Hola Raúl!  Espero que te encuentres muy bien.

I'm not sure if this handles the case that you're concerned about, but
when I want to make a script "sourceable" as well as executable, I do
this:

  #!/bin/zsh
  (
    # all script stuff goes here
  )

Yes, it causes an extra fork, but that's the price I pay for this dual
sourceable/executable capability.


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

* Making a script 'sourceable'
@ 2004-09-03 16:35 DervishD
  2004-09-03 17:01 ` Lloyd Zusman
  2004-09-03 17:20 ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: DervishD @ 2004-09-03 16:35 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    Last month I asked here how to detect, portably, whether a script
had been sourced, and since then I've tried, with no success.

    That given, I'm trying to do two things. First one is detecting
the 'sourcing' non portably, in zsh. Checking for $SHLVL doesn't
work, because the user could have launched a new instance of zsh and
so SHLVL will appear increased even when sourceing. So what I'm
thinking about now is to check the value of the option 'interactive'.
If it is on, chances are that we have sourced the script. Otherwise,
we may not. This is not perfect, though, because you can run a script
(interactive=off) and then source the other script within...

    The second thing is derived from the above question: since
checking for 'sourcery' ;) is very difficult even non portably, I've
thought about making my zsh scripts sourceables. I thought that simply
doing 'emulate -L zsh' will do, since any change to options and
values will remain local to the script, but it won't work when
sourceing because the script is run in the current environment :(

    So I must, by hand, restore any changed variables, options and,
what more? Must I restore any other thing? Is there any simple way of
making scripts source-safe or source-aware?

    Thanks a lot in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

end of thread, other threads:[~2004-09-05 13:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-04 11:07 Making a script 'sourceable' DervishD
2004-09-04 15:44 ` Bart Schaefer
2004-09-05 13:33   ` DervishD
  -- strict thread matches above, loose matches on Subject: below --
2004-09-03 16:35 DervishD
2004-09-03 17:01 ` Lloyd Zusman
2004-09-03 22:16   ` DervishD
2004-09-03 17:20 ` Bart Schaefer

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