]>
Commit | Line | Data |
---|---|---|
fa3a4f15 | 1 | # Extended prompt utilities. |
1d506c26 | 2 | # Copyright (C) 2011-2024 Free Software Foundation, Inc. |
fa3a4f15 PM |
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 | ||
13123da8 | 22 | |
fa3a4f15 PM |
23 | def _prompt_pwd(ignore): |
24 | "The current working directory." | |
89ed8ea1 | 25 | return os.getcwd() |
fa3a4f15 | 26 | |
13123da8 | 27 | |
fa3a4f15 PM |
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: | |
13123da8 | 35 | return "<no %s>" % what |
fa3a4f15 PM |
36 | if hasattr(obj, attr): |
37 | result = getattr(obj, attr) | |
38 | if callable(result): | |
39 | result = result() | |
40 | return result | |
41 | else: | |
13123da8 SM |
42 | return "<no attribute %s on current %s>" % (attr, what) |
43 | ||
fa3a4f15 PM |
44 | |
45 | def _prompt_frame(attr): | |
46 | "The selected frame; an argument names a frame parameter." | |
13123da8 SM |
47 | return _prompt_object_attr(gdb.selected_frame, "frame", attr, "name") |
48 | ||
fa3a4f15 PM |
49 | |
50 | def _prompt_thread(attr): | |
51 | "The selected thread; an argument names a thread parameter." | |
13123da8 SM |
52 | return _prompt_object_attr(gdb.selected_thread, "thread", attr, "num") |
53 | ||
fa3a4f15 PM |
54 | |
55 | def _prompt_version(attr): | |
56 | "The version of GDB." | |
57 | return gdb.VERSION | |
58 | ||
13123da8 | 59 | |
fa3a4f15 PM |
60 | def _prompt_esc(attr): |
61 | "The ESC character." | |
13123da8 SM |
62 | return "\033" |
63 | ||
fa3a4f15 PM |
64 | |
65 | def _prompt_bs(attr): | |
66 | "A backslash." | |
13123da8 SM |
67 | return "\\" |
68 | ||
fa3a4f15 PM |
69 | |
70 | def _prompt_n(attr): | |
71 | "A newline." | |
13123da8 SM |
72 | return "\n" |
73 | ||
fa3a4f15 PM |
74 | |
75 | def _prompt_r(attr): | |
76 | "A carriage return." | |
13123da8 SM |
77 | return "\r" |
78 | ||
fa3a4f15 PM |
79 | |
80 | def _prompt_param(attr): | |
81 | "A parameter's value; the argument names the parameter." | |
82 | return gdb.parameter(attr) | |
83 | ||
13123da8 | 84 | |
fa3a4f15 PM |
85 | def _prompt_noprint_begin(attr): |
86 | "Begins a sequence of non-printing characters." | |
13123da8 SM |
87 | return "\001" |
88 | ||
fa3a4f15 PM |
89 | |
90 | def _prompt_noprint_end(attr): | |
13123da8 SM |
91 | "Ends a sequence of non-printing characters." |
92 | return "\002" | |
93 | ||
fa3a4f15 PM |
94 | |
95 | prompt_substitutions = { | |
13123da8 SM |
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, | |
fa3a4f15 PM |
107 | } |
108 | ||
13123da8 | 109 | |
fa3a4f15 PM |
110 | def prompt_help(): |
111 | """Generate help dynamically from the __doc__ strings of attribute | |
112 | functions.""" | |
113 | ||
13123da8 SM |
114 | result = "" |
115 | keys = sorted(prompt_substitutions.keys()) | |
fa3a4f15 | 116 | for key in keys: |
13123da8 | 117 | result += " \\%s\t%s\n" % (key, prompt_substitutions[key].__doc__) |
fa3a4f15 PM |
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 | ||
13123da8 | 124 | |
fa3a4f15 PM |
125 | def substitute_prompt(prompt): |
126 | "Perform substitutions on PROMPT." | |
127 | ||
13123da8 | 128 | result = "" |
fa3a4f15 PM |
129 | plen = len(prompt) |
130 | i = 0 | |
131 | while i < plen: | |
13123da8 | 132 | if prompt[i] == "\\": |
fa3a4f15 PM |
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 | ||
13123da8 | 141 | if i + 1 < plen and prompt[i + 1] == "{": |
fa3a4f15 | 142 | j = i + 1 |
13123da8 | 143 | while j < plen and prompt[j] != "}": |
fa3a4f15 PM |
144 | j = j + 1 |
145 | # Just ignore formatting errors. | |
13123da8 | 146 | if j >= plen or prompt[j] != "}": |
fa3a4f15 PM |
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 |