Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1 | This file is caller.def, from which is created caller.c. It implements the |
| 2 | builtin "caller" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 2002-2008 Rocky Bernstein for Free Software Foundation, Inc. |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 5 | Copyright (C) 2008-2013 Free Software Foundation, Inc. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 6 | |
| 7 | This file is part of GNU Bash, the Bourne Again SHell. |
| 8 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 13 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 18 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 19 | You should have received a copy of the GNU General Public License |
| 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 21 | |
| 22 | $PRODUCES caller.c |
| 23 | |
| 24 | $BUILTIN caller |
| 25 | $FUNCTION caller_builtin |
| 26 | $DEPENDS_ON DEBUGGER |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 27 | $SHORT_DOC caller [expr] |
| 28 | Return the context of the current subroutine call. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 29 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 30 | Without EXPR, returns "$line $filename". With EXPR, returns |
| 31 | "$line $subroutine $filename"; this extra information can be used to |
| 32 | provide a stack trace. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 33 | |
| 34 | The value of EXPR indicates how many call frames to go back before the |
| 35 | current one; the top frame is frame 0. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 36 | |
| 37 | Exit Status: |
| 38 | Returns 0 unless the shell is not executing a shell function or EXPR |
| 39 | is invalid. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 40 | $END |
| 41 | |
| 42 | #include <config.h> |
| 43 | #include <stdio.h> |
| 44 | #include "chartypes.h" |
| 45 | #include "bashtypes.h" |
| 46 | |
| 47 | #if defined (HAVE_UNISTD_H) |
| 48 | # ifdef _MINIX |
| 49 | # include <sys/types.h> |
| 50 | # endif |
| 51 | # include <unistd.h> |
| 52 | #endif |
| 53 | |
| 54 | #include <errno.h> |
| 55 | |
| 56 | #include "../bashintl.h" |
| 57 | |
| 58 | #include "../shell.h" |
| 59 | #include "common.h" |
| 60 | #include "builtext.h" |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 61 | #include "bashgetopt.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 62 | |
| 63 | #ifdef LOADABLE_BUILTIN |
| 64 | # include "builtins.h" |
| 65 | #endif |
| 66 | |
| 67 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 68 | #include <errno.h> |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 69 | #endif /* !errno */ |
| 70 | |
| 71 | int |
| 72 | caller_builtin (list) |
| 73 | WORD_LIST *list; |
| 74 | { |
| 75 | #if !defined (ARRAY_VARS) |
| 76 | printf ("1 NULL\n"); |
| 77 | return (EXECUTION_FAILURE); |
| 78 | #else |
| 79 | SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v; |
| 80 | ARRAY *funcname_a, *bash_source_a, *bash_lineno_a; |
| 81 | char *funcname_s, *source_s, *lineno_s; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 82 | intmax_t num; |
| 83 | |
| 84 | GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); |
| 85 | GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); |
| 86 | GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a); |
| 87 | |
| 88 | if (bash_lineno_a == 0 || array_empty (bash_lineno_a)) |
| 89 | return (EXECUTION_FAILURE); |
| 90 | |
| 91 | if (bash_source_a == 0 || array_empty (bash_source_a)) |
| 92 | return (EXECUTION_FAILURE); |
| 93 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 94 | if (no_options (list)) |
| 95 | return (EX_USAGE); |
| 96 | list = loptend; /* skip over possible `--' */ |
| 97 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 98 | /* If there is no argument list, then give short form: line filename. */ |
| 99 | if (list == 0) |
| 100 | { |
| 101 | lineno_s = array_reference (bash_lineno_a, 0); |
| 102 | source_s = array_reference (bash_source_a, 1); |
| 103 | printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL"); |
| 104 | return (EXECUTION_SUCCESS); |
| 105 | } |
| 106 | |
| 107 | if (funcname_a == 0 || array_empty (funcname_a)) |
| 108 | return (EXECUTION_FAILURE); |
| 109 | |
| 110 | if (legal_number (list->word->word, &num)) |
| 111 | { |
| 112 | lineno_s = array_reference (bash_lineno_a, num); |
| 113 | source_s = array_reference (bash_source_a, num+1); |
| 114 | funcname_s = array_reference (funcname_a, num+1); |
| 115 | |
| 116 | if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL) |
| 117 | return (EXECUTION_FAILURE); |
| 118 | |
| 119 | printf("%s %s %s\n", lineno_s, funcname_s, source_s); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | sh_invalidnum (list->word->word); |
| 124 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 125 | return (EX_USAGE); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return (EXECUTION_SUCCESS); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | #ifdef LOADABLE_BUILTIN |
| 133 | static char *caller_doc[] = { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 134 | N_("Returns the context of the current subroutine call.\n\ |
| 135 | \n\ |
| 136 | Without EXPR, returns "$line $filename". With EXPR, returns\n\ |
| 137 | "$line $subroutine $filename"; this extra information can be used to\n\ |
| 138 | provide a stack trace.\n\ |
| 139 | \n\ |
| 140 | The value of EXPR indicates how many call frames to go back before the\n\ |
| 141 | current one; the top frame is frame 0."), |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 142 | (char *)NULL |
| 143 | }; |
| 144 | |
| 145 | struct builtin caller_struct = { |
| 146 | "caller", |
| 147 | caller_builtin, |
| 148 | BUILTIN_ENABLED, |
| 149 | caller_doc, |
| 150 | "caller [EXPR]", |
| 151 | 0 |
| 152 | }; |
| 153 | |
| 154 | #endif /* LOADABLE_BUILTIN */ |