1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-07 23:51:14 +02:00

25448: add $funcfiletrace to show absolute line number for caller

This commit is contained in:
Peter Stephenson 2008-08-13 21:02:02 +00:00
parent 01f5d0a274
commit a5a1296fa0
7 changed files with 78 additions and 2 deletions

View file

@ -1,3 +1,10 @@
2008-08-13 Peter Stephenson <p.w.stephenson@ntlworld.com>
* 25448: Doc/Zsh/mod_parameter.yo, Src/exec.c, Src/init.c,
Src/Modules/parameter.c, Src/zsh.h, Src/Modules/parameter.c
Src/Modules/parameter.mdd: add $funcfiletrace to show
absolute line number for caller.
2008-08-12 Peter Stephenson <p.w.stephenson@ntlworld.com>
* 25443 (tweaked): Doc/Zsh/mod_parameter.yo,

View file

@ -164,6 +164,16 @@ item(tt(userdirs))(
This associative array maps user names to the pathnames of their home
directories.
)
vindex(funcfiletrace)
item(tt(funcfiletrace))(
This array contains the absolute line numbers and corresponding file
names for the point where the current function or sourced file was
called. The array is of the same length as tt(funcsourcetrace) and
tt(functrace), but differs from tt(funcsourcetrace) in that the line and
file are the point of call, not the point of definition, and differs
from tt(functrace) in that all values are absolute line numbers in
files, rather than relative to the start of a function, if any.
)
vindex(funcsourcetrace)
item(tt(funcsourcetrace))(
This array contains the file names and line numbers of the
@ -176,6 +186,9 @@ The format of each element is var(filename)tt(:)var(lineno).
For files that have been executed by the tt(source) or tt(.) builtins
(in which case there is no separate definition) the trace information is
shown as tt(source:0).
Most users will be interested in the information in the
tt(funcfiletrace) array instead.
)
vindex(funcstack)
item(tt(funcstack))(

View file

@ -566,6 +566,55 @@ funcsourcetracegetfn(UNUSED(Param pm))
return ret;
}
/* Functions for the funcfiletrace special parameter. */
/**/
static char **
funcfiletracegetfn(UNUSED(Param pm))
{
Funcstack f;
int num;
char **ret, **p;
for (f = funcstack, num = 0; f; f = f->prev, num++);
ret = (char **) zhalloc((num + 1) * sizeof(char *));
for (f = funcstack, p = ret; f; f = f->prev, p++) {
char *colonpair, *fname;
if (!f->prev || f->prev->sourced) {
/*
* Calling context is a file---either the parent
* script or interactive shell, or a sourced
* script. Just print the file information for the caller
* (same as $functrace)
*/
colonpair = zhalloc(strlen(f->caller) +
(f->lineno > 9999 ? 24 : 6));
sprintf(colonpair, "%s:%ld", f->caller, (long)f->lineno);
} else {
/*
* Calling context is a function; we need to find the line number
* in the file where that function was defined. For this we need
* the $funcsourcetrace information for the context above,
* together with the $functrace line number for the current
* context.
*/
long flineno = (long)(f->prev->flineno + f->lineno);
fname = f->prev->filename ? f->prev->filename : "";
colonpair = zhalloc(strlen(fname) + (flineno > 9999 ? 24 : 6));
sprintf(colonpair, "%s:%ld", fname, flineno);
}
*p = colonpair;
}
*p = NULL;
return ret;
}
/* Functions for the builtins special parameter. */
/**/
@ -1803,6 +1852,8 @@ static const struct gsu_array functrace_gsu =
{ functracegetfn, arrsetfn, stdunsetfn };
static const struct gsu_array funcsourcetrace_gsu =
{ funcsourcetracegetfn, arrsetfn, stdunsetfn };
static const struct gsu_array funcfiletrace_gsu =
{ funcfiletracegetfn, arrsetfn, stdunsetfn };
static const struct gsu_array reswords_gsu =
{ reswordsgetfn, arrsetfn, stdunsetfn };
static const struct gsu_array disreswords_gsu =
@ -1831,6 +1882,8 @@ static struct paramdef partab[] = {
&disreswords_gsu, NULL, NULL),
SPECIALPMDEF("dis_saliases", 0,
&pmdissaliases_gsu, getpmdissalias, scanpmdissaliases),
SPECIALPMDEF("funcfiletrace", PM_ARRAY|PM_READONLY,
&funcfiletrace_gsu, NULL, NULL),
SPECIALPMDEF("funcsourcetrace", PM_ARRAY|PM_READONLY,
&funcsourcetrace_gsu, NULL, NULL),
SPECIALPMDEF("funcstack", PM_ARRAY|PM_READONLY,

View file

@ -2,6 +2,6 @@ name=zsh/parameter
link=either
load=yes
autofeatures="p:parameters p:commands p:functions p:dis_functions p:funcsourcetrace p:funcstack p:functrace p:builtins p:dis_builtins p:reswords p:dis_reswords p:options p:modules p:dirstack p:history p:historywords p:jobtexts p:jobdirs p:jobstates p:nameddirs p:userdirs p:aliases p:dis_aliases p:galiases p:dis_galiases p:saliases p:dis_saliases"
autofeatures="p:parameters p:commands p:functions p:dis_functions p:funcfiletrace p:funcsourcetrace p:funcstack p:functrace p:builtins p:dis_builtins p:reswords p:dis_reswords p:options p:modules p:dirstack p:history p:historywords p:jobtexts p:jobdirs p:jobstates p:nameddirs p:userdirs p:aliases p:dis_aliases p:galiases p:dis_galiases p:saliases p:dis_saliases"
objects="parameter.o"

View file

@ -4258,6 +4258,7 @@ doshfunc(char *name, Eprog prog, LinkList doshargs, int flags, int noreturnval)
fstack.caller = dupstring(oargv0 ? oargv0 : argzero);
fstack.lineno = lineno;
fstack.prev = funcstack;
fstack.sourced = 0;
funcstack = &fstack;
if ((shf = (Shfunc) shfunctab->getnode(shfunctab, name))) {

View file

@ -1109,6 +1109,7 @@ source(char *s)
fstack.lineno = oldlineno;
fstack.filename = fstack.name;
fstack.prev = funcstack;
fstack.sourced = 1;
funcstack = &fstack;
}

View file

@ -1080,11 +1080,12 @@ struct shfunc {
struct funcstack {
Funcstack prev; /* previous in stack */
char *name; /* name of function called */
char *name; /* name of function/sourced file called */
char *filename; /* file function resides in */
char *caller; /* name of caller */
zlong flineno; /* line number in file */
zlong lineno; /* line offset from beginning of function */
int sourced; /* type of entry is a sourced file */
};
/* node in list of function call wrappers */