Imported from ../bash-3.0.tar.gz.
[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-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
59 #ifdef LOADABLE_BUILTIN
60 #  include "builtins.h"
61 #endif
62
63 #if !defined (errno)
64 extern int errno;
65 #endif /* !errno */
66
67 int
68 caller_builtin (list)
69      WORD_LIST *list;
70 {
71 #if !defined (ARRAY_VARS)
72   printf ("1 NULL\n");
73   return (EXECUTION_FAILURE);
74 #else
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;
78   ARRAY_ELEMENT *ae;
79   intmax_t num;
80
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);
84
85   if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
86     return (EXECUTION_FAILURE);
87
88   if (bash_source_a == 0 || array_empty (bash_source_a))
89     return (EXECUTION_FAILURE);
90
91   /* If there is no argument list, then give short form: line filename. */
92   if (list == 0)
93     {
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);
98     }
99   
100   if (funcname_a == 0 || array_empty (funcname_a))
101     return (EXECUTION_FAILURE);
102
103   if (legal_number (list->word->word, &num))
104     {
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);
108
109       if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
110         return (EXECUTION_FAILURE);
111
112       printf("%s %s %s\n", lineno_s, funcname_s, source_s);
113     }
114   else
115     {
116       sh_invalidnum (list->word->word);
117       builtin_usage ();
118       return (EXECUTION_FAILURE);
119     }
120
121   return (EXECUTION_SUCCESS);
122 #endif
123 }
124
125 #ifdef LOADABLE_BUILTIN
126 static char *caller_doc[] = {
127   N_("Returns the context of the current subroutine call."),
128   N_(""),
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."),
132   N_(""),
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."),
135   (char *)NULL
136 };
137
138 struct builtin caller_struct = {
139         "caller",
140         caller_builtin,
141         BUILTIN_ENABLED,
142         caller_doc,
143         "caller [EXPR]",
144         0
145 };
146
147 #endif /* LOADABLE_BUILTIN */