1 This file is caller.def, from which is created caller.c. It implements the
2 builtin "caller" in Bash.
4 Copyright (C) 2002-2003 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION caller_builtin
27 $SHORT_DOC caller [EXPR]
29 Returns the context of the current subroutine call.
31 Without EXPR, returns "$line $filename". With EXPR,
32 returns "$line $subroutine $filename"; this extra information
33 can be used to provide a stack trace.
35 The value of EXPR indicates how many call frames to go back before the
36 current one; the top frame is frame 0.
41 #include "chartypes.h"
42 #include "bashtypes.h"
44 #if defined (HAVE_UNISTD_H)
46 # include <sys/types.h>
53 #include "../bashintl.h"
58 #include "bashgetopt.h"
60 #ifdef LOADABLE_BUILTIN
61 # include "builtins.h"
72 #if !defined (ARRAY_VARS)
74 return (EXECUTION_FAILURE);
76 SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
77 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
78 char *funcname_s, *source_s, *lineno_s;
81 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
82 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
83 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
85 if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
86 return (EXECUTION_FAILURE);
88 if (bash_source_a == 0 || array_empty (bash_source_a))
89 return (EXECUTION_FAILURE);
91 if (no_options (list))
93 list = loptend; /* skip over possible `--' */
95 /* If there is no argument list, then give short form: line filename. */
98 lineno_s = array_reference (bash_lineno_a, 0);
99 source_s = array_reference (bash_source_a, 1);
100 printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
101 return (EXECUTION_SUCCESS);
104 if (funcname_a == 0 || array_empty (funcname_a))
105 return (EXECUTION_FAILURE);
107 if (legal_number (list->word->word, &num))
109 lineno_s = array_reference (bash_lineno_a, num);
110 source_s = array_reference (bash_source_a, num+1);
111 funcname_s = array_reference (funcname_a, num+1);
113 if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
114 return (EXECUTION_FAILURE);
116 printf("%s %s %s\n", lineno_s, funcname_s, source_s);
120 sh_invalidnum (list->word->word);
122 return (EXECUTION_FAILURE);
125 return (EXECUTION_SUCCESS);
129 #ifdef LOADABLE_BUILTIN
130 static char *caller_doc[] = {
131 N_("Returns the context of the current subroutine call."),
133 N_("Without EXPR, returns returns \"$line $filename\". With EXPR,"),
134 N_("returns \"$line $subroutine $filename\"; this extra information"),
135 N_("can be used used to provide a stack trace."),
137 N_("The value of EXPR indicates how many call frames to go back before the"),
138 N_("current one; the top frame is frame 0."),
142 struct builtin caller_struct = {
151 #endif /* LOADABLE_BUILTIN */