]> Git Repo - binutils.git/blob - gdb/python/lib/gdb/prompt.py
9b6c322e61e0ded5e808b366af807359eae1e75e
[binutils.git] / gdb / python / lib / gdb / prompt.py
1 # Extended prompt utilities.
2 # Copyright (C) 2011-2021 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 """ Extended prompt library functions."""
18
19 import gdb
20 import os
21
22
23 def _prompt_pwd(ignore):
24     "The current working directory."
25     return os.getcwd()
26
27
28 def _prompt_object_attr(func, what, attr, nattr):
29     """Internal worker for fetching GDB attributes."""
30     if attr is None:
31         attr = nattr
32     try:
33         obj = func()
34     except gdb.error:
35         return "<no %s>" % what
36     if hasattr(obj, attr):
37         result = getattr(obj, attr)
38         if callable(result):
39             result = result()
40         return result
41     else:
42         return "<no attribute %s on current %s>" % (attr, what)
43
44
45 def _prompt_frame(attr):
46     "The selected frame; an argument names a frame parameter."
47     return _prompt_object_attr(gdb.selected_frame, "frame", attr, "name")
48
49
50 def _prompt_thread(attr):
51     "The selected thread; an argument names a thread parameter."
52     return _prompt_object_attr(gdb.selected_thread, "thread", attr, "num")
53
54
55 def _prompt_version(attr):
56     "The version of GDB."
57     return gdb.VERSION
58
59
60 def _prompt_esc(attr):
61     "The ESC character."
62     return "\033"
63
64
65 def _prompt_bs(attr):
66     "A backslash."
67     return "\\"
68
69
70 def _prompt_n(attr):
71     "A newline."
72     return "\n"
73
74
75 def _prompt_r(attr):
76     "A carriage return."
77     return "\r"
78
79
80 def _prompt_param(attr):
81     "A parameter's value; the argument names the parameter."
82     return gdb.parameter(attr)
83
84
85 def _prompt_noprint_begin(attr):
86     "Begins a sequence of non-printing characters."
87     return "\001"
88
89
90 def _prompt_noprint_end(attr):
91     "Ends a sequence of non-printing characters."
92     return "\002"
93
94
95 prompt_substitutions = {
96     "e": _prompt_esc,
97     "\\": _prompt_bs,
98     "n": _prompt_n,
99     "r": _prompt_r,
100     "v": _prompt_version,
101     "w": _prompt_pwd,
102     "f": _prompt_frame,
103     "t": _prompt_thread,
104     "p": _prompt_param,
105     "[": _prompt_noprint_begin,
106     "]": _prompt_noprint_end,
107 }
108
109
110 def prompt_help():
111     """Generate help dynamically from the __doc__ strings of attribute
112     functions."""
113
114     result = ""
115     keys = sorted(prompt_substitutions.keys())
116     for key in keys:
117         result += "  \\%s\t%s\n" % (key, prompt_substitutions[key].__doc__)
118     result += """
119 A substitution can be used in a simple form, like "\\f".
120 An argument can also be passed to it, like "\\f{name}".
121 The meaning of the argument depends on the particular substitution."""
122     return result
123
124
125 def substitute_prompt(prompt):
126     "Perform substitutions on PROMPT."
127
128     result = ""
129     plen = len(prompt)
130     i = 0
131     while i < plen:
132         if prompt[i] == "\\":
133             i = i + 1
134             if i >= plen:
135                 break
136             cmdch = prompt[i]
137
138             if cmdch in prompt_substitutions:
139                 cmd = prompt_substitutions[cmdch]
140
141                 if i + 1 < plen and prompt[i + 1] == "{":
142                     j = i + 1
143                     while j < plen and prompt[j] != "}":
144                         j = j + 1
145                     # Just ignore formatting errors.
146                     if j >= plen or prompt[j] != "}":
147                         arg = None
148                     else:
149                         arg = prompt[i + 2 : j]
150                         i = j
151                 else:
152                     arg = None
153                 result += str(cmd(arg))
154             else:
155                 # Unrecognized escapes are turned into the escaped
156                 # character itself.
157                 result += prompt[i]
158         else:
159             result += prompt[i]
160
161         i = i + 1
162
163     return result
This page took 0.02437 seconds and 2 git commands to generate.