7ddbdad491711bbe2cae75c0ffd6ebb94c220a71
[platform/upstream/bash.git] / builtins / caller.def
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
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
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.
12
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.
17
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/>.
20
21 $PRODUCES caller.c
22
23 $BUILTIN caller
24 $FUNCTION caller_builtin
25 $DEPENDS_ON DEBUGGER
26 $SHORT_DOC caller [expr]
27 Return the context of the current subroutine call.
28
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.
32
33 The value of EXPR indicates how many call frames to go back before the
34 current one; the top frame is frame 0.
35
36 Exit Status:
37 Returns 0 unless the shell is not executing a shell function or EXPR
38 is invalid.
39 $END
40
41 #include <config.h>
42 #include <stdio.h>
43 #include "chartypes.h"
44 #include "bashtypes.h"
45
46 #if defined (HAVE_UNISTD_H)
47 #  ifdef _MINIX
48 #    include <sys/types.h>
49 #  endif
50 #  include <unistd.h>
51 #endif
52
53 #include <errno.h>
54
55 #include "../bashintl.h"
56
57 #include "../shell.h"
58 #include "common.h"
59 #include "builtext.h"
60 #include "bashgetopt.h"
61
62 #ifdef LOADABLE_BUILTIN
63 #  include "builtins.h"
64 #endif
65
66 #if !defined (errno)
67 extern int errno;
68 #endif /* !errno */
69
70 int
71 caller_builtin (list)
72      WORD_LIST *list;
73 {
74 #if !defined (ARRAY_VARS)
75   printf ("1 NULL\n");
76   return (EXECUTION_FAILURE);
77 #else
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;
81   intmax_t num;
82
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);
86
87   if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
88     return (EXECUTION_FAILURE);
89
90   if (bash_source_a == 0 || array_empty (bash_source_a))
91     return (EXECUTION_FAILURE);
92
93  if (no_options (list))
94     return (EX_USAGE);
95   list = loptend;       /* skip over possible `--' */
96
97   /* If there is no argument list, then give short form: line filename. */
98   if (list == 0)
99     {
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);
104     }
105   
106   if (funcname_a == 0 || array_empty (funcname_a))
107     return (EXECUTION_FAILURE);
108
109   if (legal_number (list->word->word, &num))
110     {
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);
114
115       if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
116         return (EXECUTION_FAILURE);
117
118       printf("%s %s %s\n", lineno_s, funcname_s, source_s);
119     }
120   else
121     {
122       sh_invalidnum (list->word->word);
123       builtin_usage ();
124       return (EXECUTION_FAILURE);
125     }
126
127   return (EXECUTION_SUCCESS);
128 #endif
129 }
130
131 #ifdef LOADABLE_BUILTIN
132 static char *caller_doc[] = {
133 N_("Returns the context of the current subroutine call.\n\
134     \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\
138     \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."),
141   (char *)NULL
142 };
143
144 struct builtin caller_struct = {
145         "caller",
146         caller_builtin,
147         BUILTIN_ENABLED,
148         caller_doc,
149         "caller [EXPR]",
150         0
151 };
152
153 #endif /* LOADABLE_BUILTIN */