1 This file is caller.def, from which is created caller.c. It implements the
2 builtin "caller" in Bash.
4 Copyright (C) 2002-2008 Rocky Bernstein for Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION caller_builtin
26 $SHORT_DOC caller [expr]
27 Return the context of the current subroutine call.
29 Without EXPR, returns "$line $filename". With EXPR, returns
30 "$line $subroutine $filename"; this extra information can be used to
31 provide a stack trace.
33 The value of EXPR indicates how many call frames to go back before the
34 current one; the top frame is frame 0.
37 Returns 0 unless the shell is not executing a shell function or EXPR
43 #include "chartypes.h"
44 #include "bashtypes.h"
46 #if defined (HAVE_UNISTD_H)
48 # include <sys/types.h>
55 #include "../bashintl.h"
60 #include "bashgetopt.h"
62 #ifdef LOADABLE_BUILTIN
63 # include "builtins.h"
74 #if !defined (ARRAY_VARS)
76 return (EXECUTION_FAILURE);
78 SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
79 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
80 char *funcname_s, *source_s, *lineno_s;
83 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
84 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
85 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
87 if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
88 return (EXECUTION_FAILURE);
90 if (bash_source_a == 0 || array_empty (bash_source_a))
91 return (EXECUTION_FAILURE);
93 if (no_options (list))
95 list = loptend; /* skip over possible `--' */
97 /* If there is no argument list, then give short form: line filename. */
100 lineno_s = array_reference (bash_lineno_a, 0);
101 source_s = array_reference (bash_source_a, 1);
102 printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
103 return (EXECUTION_SUCCESS);
106 if (funcname_a == 0 || array_empty (funcname_a))
107 return (EXECUTION_FAILURE);
109 if (legal_number (list->word->word, &num))
111 lineno_s = array_reference (bash_lineno_a, num);
112 source_s = array_reference (bash_source_a, num+1);
113 funcname_s = array_reference (funcname_a, num+1);
115 if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
116 return (EXECUTION_FAILURE);
118 printf("%s %s %s\n", lineno_s, funcname_s, source_s);
122 sh_invalidnum (list->word->word);
124 return (EXECUTION_FAILURE);
127 return (EXECUTION_SUCCESS);
131 #ifdef LOADABLE_BUILTIN
132 static char *caller_doc[] = {
133 N_("Returns the context of the current subroutine call.\n\
135 Without EXPR, returns "$line $filename". With EXPR, returns\n\
136 "$line $subroutine $filename"; this extra information can be used to\n\
137 provide a stack trace.\n\
139 The value of EXPR indicates how many call frames to go back before the\n\
140 current one; the top frame is frame 0."),
144 struct builtin caller_struct = {
153 #endif /* LOADABLE_BUILTIN */