f5f2c0836649a704ed588e3e6afca3c18c3cbc9a
[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 "$line $filename".  With EXPR,
32 returns "$line $subroutine $filename"; this extra information
33 can be 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   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 (no_options (list))
92     return (EX_USAGE);
93   list = loptend;       /* skip over possible `--' */
94
95   /* If there is no argument list, then give short form: line filename. */
96   if (list == 0)
97     {
98       lineno_s = array_reference (bash_lineno_a, 0);
99       source_s = array_reference (bash_source_a, 1);
100       printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
101       return (EXECUTION_SUCCESS);
102     }
103   
104   if (funcname_a == 0 || array_empty (funcname_a))
105     return (EXECUTION_FAILURE);
106
107   if (legal_number (list->word->word, &num))
108     {
109       lineno_s = array_reference (bash_lineno_a, num);
110       source_s = array_reference (bash_source_a, num+1);
111       funcname_s = array_reference (funcname_a, num+1);
112
113       if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
114         return (EXECUTION_FAILURE);
115
116       printf("%s %s %s\n", lineno_s, funcname_s, source_s);
117     }
118   else
119     {
120       sh_invalidnum (list->word->word);
121       builtin_usage ();
122       return (EXECUTION_FAILURE);
123     }
124
125   return (EXECUTION_SUCCESS);
126 #endif
127 }
128
129 #ifdef LOADABLE_BUILTIN
130 static char *caller_doc[] = {
131   N_("Returns the context of the current subroutine call."),
132   N_(" "),
133   N_("Without EXPR, returns returns \"$line $filename\".  With EXPR,"),
134   N_("returns \"$line $subroutine $filename\"; this extra information"),
135   N_("can be used used to provide a stack trace."),
136   N_(" "),
137   N_("The value of EXPR indicates how many call frames to go back before the"),
138   N_("current one; the top frame is frame 0."),
139   (char *)NULL
140 };
141
142 struct builtin caller_struct = {
143         "caller",
144         caller_builtin,
145         BUILTIN_ENABLED,
146         caller_doc,
147         "caller [EXPR]",
148         0
149 };
150
151 #endif /* LOADABLE_BUILTIN */