Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Feature request: Provide name of file being sourced, to the script.



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.



Messages sorted by: Reverse Date, Date, Thread, Author