summaryrefslogtreecommitdiffstats
path: root/scratch/bash-3.1-postpatch/builtins/caller.def
blob: 5142cab9a68e5e7dd4618b3eb5f8d724070f467f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
This file is callerdef, from which is created callerc  It implements the
builtin "caller" in Bash

Copyright (C) 2002-2003 Rocky Bernstein for Free Software Foundation, Inc

This file is part of GNU Bash, the Bourne Again SHell

Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version

Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE  See the GNU General Public License
for more details

You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING  If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA

$PRODUCES callerc

$BUILTIN caller
$FUNCTION caller_builtin
$DEPENDS_ON DEBUGGER
$SHORT_DOC caller [EXPR]

Returns the context of the current subroutine call  

Without EXPR, returns returns "$line $filename".  With EXPR,
returns "$line $subroutine $filename"; this extra information
can be used used to provide a stack trace

The value of EXPR indicates how many call frames to go back before the
current one; the top frame is frame 0
$END

#include <configh
#include <stdioh
#include "chartypesh"
#include "bashtypesh"

#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#    include <sys/typesh
#  endif
#  include <unistdh
#endif

#include <errnoh

#include "../bashintlh"

#include "../shellh"
#include "commonh"
#include "builtexth"
#include "bashgetopth"

#ifdef LOADABLE_BUILTIN
#  include "builtinsh"
#endif

#if !defined (errno)
extern int errno;
#endif /* !errno */

int
caller_builtin (list)
     WORD_LIST *list;
{
#if !defined (ARRAY_VARS)
  printf ("1 NULL\n");
  return (EXECUTION_FAILURE);
#else
  SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
  ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
  char *funcname_s, *source_s, *lineno_s;
  ARRAY_ELEMENT *ae;
  intmax_t num;

  GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
  GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
  GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);

  if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
    return (EXECUTION_FAILURE);

  if (bash_source_a == 0 || array_empty (bash_source_a))
    return (EXECUTION_FAILURE);

 if (no_options (list))
    return (EX_USAGE);
  list = loptend;       /* skip over possible `--' */

  /* If there is no argument list, then give short form: line filename */
  if (list == 0)
    {
      lineno_s = array_reference (bash_lineno_a, 0);
      source_s = array_reference (bash_source_a, 1);
      printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
      return (EXECUTION_SUCCESS);
    }
  
  if (funcname_a == 0 || array_empty (funcname_a))
    return (EXECUTION_FAILURE);

  if (legal_number (list->word->word, &num))
    {
      lineno_s = array_reference (bash_lineno_a, num);
      source_s = array_reference (bash_source_a, num+1);
      funcname_s = array_reference (funcname_a, num+1);

      if (lineno_s == NULL| source_s == NULL || funcname_s == NULL)
	return (EXECUTION_FAILURE);

      printf("%s %s %s\n", lineno_s, funcname_s, source_s);
    }
  else
    {
      sh_invalidnum (list->word->word);
      builtin_usage ();
      return (EXECUTION_FAILURE);
    }

  return (EXECUTION_SUCCESS);
#endif
}

#ifdef LOADABLE_BUILTIN
static char *caller_doc[] = {
  N_("Returns the context of the current subroutine call"),
  N_(" "),
  N_("Without EXPR, returns returns \"$line $filename\".  With EXPR,"),
  N_("returns \"$line $subroutine $filename\"; this extra information"),
  N_("can be used used to provide a stack trace"),
  N_(" "),
  N_("The value of EXPR indicates how many call frames to go back before the"),
  N_("current one; the top frame is frame 0"),
  (char *)NULL
};

struct builtin caller_struct = {
	"caller",
	caller_builtin,
	BUILTIN_ENABLED,
	caller_doc,
	"caller [EXPR]",
	0
};

#endif /* LOADABLE_BUILTIN */