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-2003 Rocky Bernstein for Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This file is part of GNU Bash, the Bourne Again SHell.
|
---|
7 |
|
---|
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
|
---|
11 | version.
|
---|
12 |
|
---|
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
|
---|
16 | for more details.
|
---|
17 |
|
---|
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.
|
---|
21 |
|
---|
22 | $PRODUCES caller.c
|
---|
23 |
|
---|
24 | $BUILTIN caller
|
---|
25 | $FUNCTION caller_builtin
|
---|
26 | $DEPENDS_ON DEBUGGER
|
---|
27 | $SHORT_DOC caller [EXPR]
|
---|
28 |
|
---|
29 | Returns the context of the current subroutine call.
|
---|
30 |
|
---|
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.
|
---|
34 |
|
---|
35 | The value of EXPR indicates how many call frames to go back before the
|
---|
36 | current one; the top frame is frame 0.
|
---|
37 | $END
|
---|
38 |
|
---|
39 | #include <config.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include "chartypes.h"
|
---|
42 | #include "bashtypes.h"
|
---|
43 |
|
---|
44 | #if defined (HAVE_UNISTD_H)
|
---|
45 | # ifdef _MINIX
|
---|
46 | # include <sys/types.h>
|
---|
47 | # endif
|
---|
48 | # include <unistd.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #include <errno.h>
|
---|
52 |
|
---|
53 | #include "../bashintl.h"
|
---|
54 |
|
---|
55 | #include "../shell.h"
|
---|
56 | #include "common.h"
|
---|
57 | #include "builtext.h"
|
---|
58 | #include "bashgetopt.h"
|
---|
59 |
|
---|
60 | #ifdef LOADABLE_BUILTIN
|
---|
61 | # include "builtins.h"
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #if !defined (errno)
|
---|
65 | extern int errno;
|
---|
66 | #endif /* !errno */
|
---|
67 |
|
---|
68 | int
|
---|
69 | caller_builtin (list)
|
---|
70 | WORD_LIST *list;
|
---|
71 | {
|
---|
72 | #if !defined (ARRAY_VARS)
|
---|
73 | printf ("1 NULL\n");
|
---|
74 | return (EXECUTION_FAILURE);
|
---|
75 | #else
|
---|
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;
|
---|
79 | ARRAY_ELEMENT *ae;
|
---|
80 | intmax_t num;
|
---|
81 |
|
---|
82 | GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
|
---|
83 | GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
|
---|
84 | GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
|
---|
85 |
|
---|
86 | if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
|
---|
87 | return (EXECUTION_FAILURE);
|
---|
88 |
|
---|
89 | if (bash_source_a == 0 || array_empty (bash_source_a))
|
---|
90 | return (EXECUTION_FAILURE);
|
---|
91 |
|
---|
92 | if (no_options (list))
|
---|
93 | return (EX_USAGE);
|
---|
94 | list = loptend; /* skip over possible `--' */
|
---|
95 |
|
---|
96 | /* If there is no argument list, then give short form: line filename. */
|
---|
97 | if (list == 0)
|
---|
98 | {
|
---|
99 | lineno_s = array_reference (bash_lineno_a, 0);
|
---|
100 | source_s = array_reference (bash_source_a, 1);
|
---|
101 | printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
|
---|
102 | return (EXECUTION_SUCCESS);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (funcname_a == 0 || array_empty (funcname_a))
|
---|
106 | return (EXECUTION_FAILURE);
|
---|
107 |
|
---|
108 | if (legal_number (list->word->word, &num))
|
---|
109 | {
|
---|
110 | lineno_s = array_reference (bash_lineno_a, num);
|
---|
111 | source_s = array_reference (bash_source_a, num+1);
|
---|
112 | funcname_s = array_reference (funcname_a, num+1);
|
---|
113 |
|
---|
114 | if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
|
---|
115 | return (EXECUTION_FAILURE);
|
---|
116 |
|
---|
117 | printf("%s %s %s\n", lineno_s, funcname_s, source_s);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | sh_invalidnum (list->word->word);
|
---|
122 | builtin_usage ();
|
---|
123 | return (EXECUTION_FAILURE);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return (EXECUTION_SUCCESS);
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 | #ifdef LOADABLE_BUILTIN
|
---|
131 | static char *caller_doc[] = {
|
---|
132 | N_("Returns the context of the current subroutine call."),
|
---|
133 | N_(" "),
|
---|
134 | N_("Without EXPR, returns returns \"$line $filename\". With EXPR,"),
|
---|
135 | N_("returns \"$line $subroutine $filename\"; this extra information"),
|
---|
136 | N_("can be used used to provide a stack trace."),
|
---|
137 | N_(" "),
|
---|
138 | N_("The value of EXPR indicates how many call frames to go back before the"),
|
---|
139 | N_("current one; the top frame is frame 0."),
|
---|
140 | (char *)NULL
|
---|
141 | };
|
---|
142 |
|
---|
143 | struct builtin caller_struct = {
|
---|
144 | "caller",
|
---|
145 | caller_builtin,
|
---|
146 | BUILTIN_ENABLED,
|
---|
147 | caller_doc,
|
---|
148 | "caller [EXPR]",
|
---|
149 | 0
|
---|
150 | };
|
---|
151 |
|
---|
152 | #endif /* LOADABLE_BUILTIN */
|
---|