Line data Source code
1 : This file is caller.def, from which is created caller.c. It implements the
2 : builtin "caller" in Bash.
3 :
4 : Copyright (C) 2002-2008 Rocky Bernstein for Free Software Foundation, Inc.
5 : Copyright (C) 2008-2013 Free Software Foundation, Inc.
6 :
7 : This file is part of GNU Bash, the Bourne Again SHell.
8 :
9 : Bash is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : Bash is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 :
22 : $PRODUCES caller.c
23 :
24 : $BUILTIN caller
25 : $FUNCTION caller_builtin
26 : $DEPENDS_ON DEBUGGER
27 : $SHORT_DOC caller [expr]
28 : Return the context of the current subroutine call.
29 :
30 : Without EXPR, returns "$line $filename". With EXPR, returns
31 : "$line $subroutine $filename"; this extra information can be used to
32 : provide a stack trace.
33 :
34 : The value of EXPR indicates how many call frames to go back before the
35 : current one; the top frame is frame 0.
36 :
37 : Exit Status:
38 : Returns 0 unless the shell is not executing a shell function or EXPR
39 : is invalid.
40 : $END
41 :
42 : #include <config.h>
43 : #include <stdio.h>
44 : #include "chartypes.h"
45 : #include "bashtypes.h"
46 :
47 : #if defined (HAVE_UNISTD_H)
48 : # ifdef _MINIX
49 : # include <sys/types.h>
50 : # endif
51 : # include <unistd.h>
52 : #endif
53 :
54 : #include <errno.h>
55 :
56 : #include "../bashintl.h"
57 :
58 : #include "../shell.h"
59 : #include "common.h"
60 : #include "builtext.h"
61 : #include "bashgetopt.h"
62 :
63 : #ifdef LOADABLE_BUILTIN
64 : # include "builtins.h"
65 : #endif
66 :
67 : #if !defined (errno)
68 : extern int errno;
69 : #endif /* !errno */
70 :
71 : int
72 74 : caller_builtin (list)
73 : WORD_LIST *list;
74 : {
75 : #if !defined (ARRAY_VARS)
76 : printf ("1 NULL\n");
77 : return (EXECUTION_FAILURE);
78 : #else
79 74 : SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
80 74 : ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
81 74 : char *funcname_s, *source_s, *lineno_s;
82 74 : intmax_t num;
83 :
84 74 : CHECK_HELPOPT (list);
85 :
86 74 : GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
87 74 : GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
88 74 : GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
89 :
90 74 : if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
91 : return (EXECUTION_FAILURE);
92 :
93 0 : if (bash_source_a == 0 || array_empty (bash_source_a))
94 : return (EXECUTION_FAILURE);
95 :
96 0 : if (no_options (list))
97 : return (EX_USAGE);
98 0 : list = loptend; /* skip over possible `--' */
99 :
100 : /* If there is no argument list, then give short form: line filename. */
101 0 : if (list == 0)
102 : {
103 0 : lineno_s = array_reference (bash_lineno_a, 0);
104 0 : source_s = array_reference (bash_source_a, 1);
105 0 : printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
106 0 : return (EXECUTION_SUCCESS);
107 : }
108 :
109 0 : if (funcname_a == 0 || array_empty (funcname_a))
110 : return (EXECUTION_FAILURE);
111 :
112 0 : if (legal_number (list->word->word, &num))
113 : {
114 0 : lineno_s = array_reference (bash_lineno_a, num);
115 0 : source_s = array_reference (bash_source_a, num+1);
116 0 : funcname_s = array_reference (funcname_a, num+1);
117 :
118 0 : if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
119 : return (EXECUTION_FAILURE);
120 :
121 0 : printf("%s %s %s\n", lineno_s, funcname_s, source_s);
122 : }
123 : else
124 : {
125 0 : sh_invalidnum (list->word->word);
126 0 : builtin_usage ();
127 0 : return (EX_USAGE);
128 : }
129 :
130 0 : return (EXECUTION_SUCCESS);
131 : #endif
132 : }
133 :
134 : #ifdef LOADABLE_BUILTIN
135 : static char *caller_doc[] = {
136 : N_("Returns the context of the current subroutine call.\n\
137 : \n\
138 : Without EXPR, returns "$line $filename". With EXPR, returns\n\
139 : "$line $subroutine $filename"; this extra information can be used to\n\
140 : provide a stack trace.\n\
141 : \n\
142 : The value of EXPR indicates how many call frames to go back before the\n\
143 : current one; the top frame is frame 0."),
144 : (char *)NULL
145 : };
146 :
147 : struct builtin caller_struct = {
148 : "caller",
149 : caller_builtin,
150 : BUILTIN_ENABLED,
151 : caller_doc,
152 : "caller [EXPR]",
153 : 0
154 : };
155 :
156 : #endif /* LOADABLE_BUILTIN */
|