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 returns "$line $filename". With EXPR,
32 returns "$line $subroutine $filename"; this extra information
33 can be used 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"
59 #ifdef LOADABLE_BUILTIN
60 # include "builtins.h"
71 #if !defined (ARRAY_VARS)
73 return (EXECUTION_FAILURE);
75 SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
76 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
77 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 there is no argument list, then give short form: line filename. */
94 lineno_s = array_reference (bash_lineno_a, 0);
95 source_s = array_reference (bash_source_a, 1);
96 printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
97 return (EXECUTION_SUCCESS);
100 if (funcname_a == 0 || array_empty (funcname_a))
101 return (EXECUTION_FAILURE);
103 if (legal_number (list->word->word, &num))
105 lineno_s = array_reference (bash_lineno_a, num);
106 source_s = array_reference (bash_source_a, num+1);
107 funcname_s = array_reference (funcname_a, num+1);
109 if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
110 return (EXECUTION_FAILURE);
112 printf("%s %s %s\n", lineno_s, funcname_s, source_s);
116 sh_invalidnum (list->word->word);
118 return (EXECUTION_FAILURE);
121 return (EXECUTION_SUCCESS);
125 #ifdef LOADABLE_BUILTIN
126 static char *caller_doc[] = {
127 N_("Returns the context of the current subroutine call."),
129 N_("Without EXPR, returns returns \"$line $filename\". With EXPR,"),
130 N_("returns \"$line $subroutine $filename\"; this extra information"),
131 N_("can be used used to provide a stack trace."),
133 N_("The value of EXPR indicates how many call frames to go back before the"),
134 N_("current one; the top frame is frame 0."),
138 struct builtin caller_struct = {
147 #endif /* LOADABLE_BUILTIN */