zsh-workers
 help / color / mirror / code / Atom feed
* Feature request: Provide name of file being sourced, to the script.
@ 2015-04-13 14:51 Ronald Fischer
  2015-04-13 15:24 ` Eric Cook
  2015-04-13 15:39 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: Ronald Fischer @ 2015-04-13 14:51 UTC (permalink / raw)
  To: zsh-workers

While a zsh script can find out about itself (i.e. where it is located),
by looking at the variable $0, a piece of code executed by the 'source'
statement does not have this information. $0 always points to the script
which "belongs" to the zsh process.

Example: We have a script /home/usr/foo/x.zsh containing the lines

    #!/bin/zsh
    source /home/usr/bar/y.inc

we can have in /home/usr/foo/y.inc the line

    echo $0

which would output /home/usr/foo/x.zsh , but we have no way of
"knowing", that the echo command being executed is in
/home/usr/bar/y.inc

I suggest that zsh provides a shell variable, similar to $0, which
returns the path of the script being sourced. I suggest that this
variable is named __SCRIPT_PATH__ . Outside of a sourcing process, the
variable might return either the empty string, or $0; this is something
which would need yet to be decided.

Why would this be useful?

Consider a "library" of scripts, which are intended to be sourced (maybe
because the chdir to somewhere and the directory needs to be kept, or
maybe because they are supposed to set up an environment), and let's
assume that these scripts depend on each other, in that one of them
sources another one from this set. Since it is a library, we require
that all those scripts are installed in the same directory.

The problem is, that these scripts do not know the path of the
respective other scripts to be sourced, because by the time of writing
of these scripts, we don't know where they will be installed to.

If they were just "normal" scripts, not to be executed by a "source"
command, a script could simply use $0:A:h to find its own absolute path,
and located the "sibling" scripts in this directory. 

With a sourced script, this is not possible. The only solutions are:
Require the PATH variable to point to the directory containing the
scripts, and just use the script name without path component; or,
requireing the top level script (which initially sources one of the
scripts in the library), to pass as parameter its path, and have the
scripts pass on the path to each other.

Both solutions make our script library inconenient to use.


  


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

* Re: Feature request: Provide name of file being sourced, to the script.
  2015-04-13 14:51 Feature request: Provide name of file being sourced, to the script Ronald Fischer
@ 2015-04-13 15:24 ` Eric Cook
  2015-04-13 15:39 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Cook @ 2015-04-13 15:24 UTC (permalink / raw)
  To: zsh-workers

On 04/13/2015 10:51 AM, Ronald Fischer wrote:
> While a zsh script can find out about itself (i.e. where it is located),
> by looking at the variable $0, a piece of code executed by the 'source'
> statement does not have this information. $0 always points to the script
> which "belongs" to the zsh process.
There is a few of ways to do that currently.

print ". $PWD/bar" > foo; print 'print $funcstack[1]' > bar; zsh foo
print ". $PWD/bar" > foo; print 'print ${(%):-%x}' > bar; zsh foo


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

* Re: Feature request: Provide name of file being sourced, to the script.
  2015-04-13 14:51 Feature request: Provide name of file being sourced, to the script Ronald Fischer
  2015-04-13 15:24 ` Eric Cook
@ 2015-04-13 15:39 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2015-04-13 15:39 UTC (permalink / raw)
  To: zsh-workers

On Apr 13,  4:51pm, Ronald Fischer wrote:
} Subject: Feature request: Provide name of file being sourced, to the scrip
}
} While a zsh script can find out about itself (i.e. where it is located),
} by looking at the variable $0, a piece of code executed by the 'source'
} statement does not have this information. $0 always points to the script
} which "belongs" to the zsh process.
} 
} Example: We have a script /home/usr/foo/x.zsh containing the lines
} 
}     #!/bin/zsh
}     source /home/usr/bar/y.inc
} 
} we can have in /home/usr/foo/y.inc the line
} 
}     echo $0
} 
} which would output /home/usr/foo/x.zsh , but we have no way of
} "knowing", that the echo command being executed is in
} /home/usr/bar/y.inc

The value of $0 should give you what you want provided that you have left
the FUNCTION_ARGZERO and POSIX_ARGZERO options in their default state
(on and off, respectively).

If for some reason you can't leave these as they were (e.g. the user in
your example has changed them himself), the zsh/parameter module provides
several variables that have related information; I quote the doc:

funcfiletrace
     This array contains the absolute line numbers and corresponding
     file names for the point where the current function, sourced file,
     or (if EVAL_LINENO is set) eval command was called.  The array is
     of the same length as funcsourcetrace and functrace, but differs
     from funcsourcetrace in that the line and file are the point of
     call, not the point of definition, and differs from functrace in
     that all values are absolute line numbers in files, rather than
     relative to the start of a function, if any.

funcsourcetrace
     This array contains the file names and line numbers of the points
     where the functions, sourced files, and (if EVAL_LINENO is set)
     eval commands currently being executed were defined.  The line
     number is the line where the `function NAME' or `NAME ()' started.
     In the case of an autoloaded function  the line number is
     reported as zero.  The format of each element is FILENAME:LINENO.

funcstack
     This array contains the names of the functions, sourced files, and
     (if EVAL_LINENO is set) eval commands. currently being executed.
     The first element is the name of the function using the parameter.

functrace
     This array contains the names and line numbers of the callers
     corresponding to the functions currently being executed.  The
     format of each element is NAME:LINENO.  Callers are also shown for
     sourced files; the caller is the point where the source or `.'
     command was executed.


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

end of thread, other threads:[~2015-04-13 15:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-13 14:51 Feature request: Provide name of file being sourced, to the script Ronald Fischer
2015-04-13 15:24 ` Eric Cook
2015-04-13 15:39 ` 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).