blob: e40b10788c8f1c073550add849d9c5127f2dc4f3 [file] [log] [blame]
Dan Pasanenc6e37862014-10-02 14:08:59 -05001/* caller.c, created from caller.def. */
2#line 23 "./caller.def"
3
4#line 41 "./caller.def"
5
6#include <config.h>
7#include <stdio.h>
8#include "chartypes.h"
9#include "bashtypes.h"
10
11#if defined (HAVE_UNISTD_H)
12# ifdef _MINIX
13# include <sys/types.h>
14# endif
15# include <unistd.h>
16#endif
17
18#include <errno.h>
19
20#include "../bashintl.h"
21
22#include "../shell.h"
23#include "common.h"
24#include "builtext.h"
25#include "bashgetopt.h"
26
27#ifdef LOADABLE_BUILTIN
28# include "builtins.h"
29#endif
30
31#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010032#include <errno.h>
Dan Pasanenc6e37862014-10-02 14:08:59 -050033#endif /* !errno */
34
35int
36caller_builtin (list)
37 WORD_LIST *list;
38{
39#if !defined (ARRAY_VARS)
40 printf ("1 NULL\n");
41 return (EXECUTION_FAILURE);
42#else
43 SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
44 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
45 char *funcname_s, *source_s, *lineno_s;
46 intmax_t num;
47
48 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
49 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
50 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
51
52 if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
53 return (EXECUTION_FAILURE);
54
55 if (bash_source_a == 0 || array_empty (bash_source_a))
56 return (EXECUTION_FAILURE);
57
58 if (no_options (list))
59 return (EX_USAGE);
60 list = loptend; /* skip over possible `--' */
61
62 /* If there is no argument list, then give short form: line filename. */
63 if (list == 0)
64 {
65 lineno_s = array_reference (bash_lineno_a, 0);
66 source_s = array_reference (bash_source_a, 1);
67 printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
68 return (EXECUTION_SUCCESS);
69 }
70
71 if (funcname_a == 0 || array_empty (funcname_a))
72 return (EXECUTION_FAILURE);
73
74 if (legal_number (list->word->word, &num))
75 {
76 lineno_s = array_reference (bash_lineno_a, num);
77 source_s = array_reference (bash_source_a, num+1);
78 funcname_s = array_reference (funcname_a, num+1);
79
80 if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
81 return (EXECUTION_FAILURE);
82
83 printf("%s %s %s\n", lineno_s, funcname_s, source_s);
84 }
85 else
86 {
87 sh_invalidnum (list->word->word);
88 builtin_usage ();
89 return (EX_USAGE);
90 }
91
92 return (EXECUTION_SUCCESS);
93#endif
94}
95
96#ifdef LOADABLE_BUILTIN
97static char *caller_doc[] = {
98N_("Returns the context of the current subroutine call.\n\
99 \n\
100 Without EXPR, returns "$line $filename". With EXPR, returns\n\
101 "$line $subroutine $filename"; this extra information can be used to\n\
102 provide a stack trace.\n\
103 \n\
104 The value of EXPR indicates how many call frames to go back before the\n\
105 current one; the top frame is frame 0."),
106 (char *)NULL
107};
108
109struct builtin caller_struct = {
110 "caller",
111 caller_builtin,
112 BUILTIN_ENABLED,
113 caller_doc,
114 "caller [EXPR]",
115 0
116};
117
118#endif /* LOADABLE_BUILTIN */