Eliminate remaining gdb/guile cleanups
[deliverable/binutils-gdb.git] / gdb / guile / scm-string.c
1 /* GDB/Scheme charset interface.
2
3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
22
23 #include "defs.h"
24 #include "charset.h"
25 #include "guile-internal.h"
26
27 /* Convert STRING to an int.
28 STRING must be a valid integer. */
29
30 int
31 gdbscm_scm_string_to_int (SCM string)
32 {
33 char *s = scm_to_latin1_string (string);
34 int r = atoi (s);
35
36 free (s);
37 return r;
38 }
39
40 /* Convert a C (latin1) string to an SCM string.
41 "latin1" is chosen because Guile won't throw an exception. */
42
43 SCM
44 gdbscm_scm_from_c_string (const char *string)
45 {
46 return scm_from_latin1_string (string);
47 }
48
49 /* Convert an SCM string to a C (latin1) string.
50 "latin1" is chosen because Guile won't throw an exception.
51 It is an error to call this if STRING is not a string. */
52
53 gdb::unique_xmalloc_ptr<char>
54 gdbscm_scm_to_c_string (SCM string)
55 {
56 return gdb::unique_xmalloc_ptr<char> (scm_to_latin1_string (string));
57 }
58
59 /* Use printf to construct a Scheme string. */
60
61 SCM
62 gdbscm_scm_from_printf (const char *format, ...)
63 {
64 va_list args;
65 char *string;
66 SCM result;
67
68 va_start (args, format);
69 string = xstrvprintf (format, args);
70 va_end (args);
71 result = scm_from_latin1_string (string);
72 xfree (string);
73
74 return result;
75 }
76
77 /* Struct to pass data from gdbscm_scm_to_string to
78 gdbscm_call_scm_to_stringn. */
79
80 struct scm_to_stringn_data
81 {
82 SCM string;
83 size_t *lenp;
84 const char *charset;
85 scm_t_string_failed_conversion_handler conversion_kind;
86 char *result;
87 };
88
89 /* Helper for gdbscm_scm_to_string to call scm_to_stringn
90 from within scm_c_catch. */
91
92 static SCM
93 gdbscm_call_scm_to_stringn (void *datap)
94 {
95 struct scm_to_stringn_data *data = (struct scm_to_stringn_data *) datap;
96
97 data->result = scm_to_stringn (data->string, data->lenp, data->charset,
98 data->conversion_kind);
99 return SCM_BOOL_F;
100 }
101
102 /* Convert an SCM string to a string in charset CHARSET.
103 This function is guaranteed to not throw an exception.
104
105 If LENP is NULL then the returned string is NUL-terminated,
106 and an exception is thrown if the string contains embedded NULs.
107 Otherwise the string is not guaranteed to be NUL-terminated, but worse
108 there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
109
110 If STRICT is non-zero, and there's a conversion error, then a
111 <gdb:exception> object is stored in *EXCEPT_SCMP, and NULL is returned.
112 If STRICT is zero, then escape sequences are used for characters that
113 can't be converted, and EXCEPT_SCMP may be passed as NULL.
114
115 It is an error to call this if STRING is not a string. */
116
117 gdb::unique_xmalloc_ptr<char>
118 gdbscm_scm_to_string (SCM string, size_t *lenp,
119 const char *charset, int strict, SCM *except_scmp)
120 {
121 struct scm_to_stringn_data data;
122 SCM scm_result;
123
124 data.string = string;
125 data.lenp = lenp;
126 data.charset = charset;
127 data.conversion_kind = (strict
128 ? SCM_FAILED_CONVERSION_ERROR
129 : SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
130 data.result = NULL;
131
132 scm_result = gdbscm_call_guile (gdbscm_call_scm_to_stringn, &data, NULL);
133
134 if (gdbscm_is_false (scm_result))
135 {
136 gdb_assert (data.result != NULL);
137 return gdb::unique_xmalloc_ptr<char> (data.result);
138 }
139 gdb_assert (gdbscm_is_exception (scm_result));
140 *except_scmp = scm_result;
141 return NULL;
142 }
143
144 /* Struct to pass data from gdbscm_scm_from_string to
145 gdbscm_call_scm_from_stringn. */
146
147 struct scm_from_stringn_data
148 {
149 const char *string;
150 size_t len;
151 const char *charset;
152 scm_t_string_failed_conversion_handler conversion_kind;
153 SCM result;
154 };
155
156 /* Helper for gdbscm_scm_from_string to call scm_from_stringn
157 from within scm_c_catch. */
158
159 static SCM
160 gdbscm_call_scm_from_stringn (void *datap)
161 {
162 struct scm_from_stringn_data *data = (struct scm_from_stringn_data *) datap;
163
164 data->result = scm_from_stringn (data->string, data->len, data->charset,
165 data->conversion_kind);
166 return SCM_BOOL_F;
167 }
168
169 /* Convert STRING to a Scheme string in charset CHARSET.
170 This function is guaranteed to not throw an exception.
171
172 If STRICT is non-zero, and there's a conversion error, then a
173 <gdb:exception> object is returned.
174 If STRICT is zero, then question marks are used for characters that
175 can't be converted (limitation of underlying Guile conversion support). */
176
177 SCM
178 gdbscm_scm_from_string (const char *string, size_t len,
179 const char *charset, int strict)
180 {
181 struct scm_from_stringn_data data;
182 SCM scm_result;
183
184 data.string = string;
185 data.len = len;
186 data.charset = charset;
187 /* The use of SCM_FAILED_CONVERSION_QUESTION_MARK is specified by Guile. */
188 data.conversion_kind = (strict
189 ? SCM_FAILED_CONVERSION_ERROR
190 : SCM_FAILED_CONVERSION_QUESTION_MARK);
191 data.result = SCM_UNDEFINED;
192
193 scm_result = gdbscm_call_guile (gdbscm_call_scm_from_stringn, &data, NULL);
194
195 if (gdbscm_is_false (scm_result))
196 {
197 gdb_assert (!SCM_UNBNDP (data.result));
198 return data.result;
199 }
200 gdb_assert (gdbscm_is_exception (scm_result));
201 return scm_result;
202 }
203
204 /* Convert an SCM string to a host string.
205 This function is guaranteed to not throw an exception.
206
207 If LENP is NULL then the returned string is NUL-terminated,
208 and if the string contains embedded NULs then NULL is returned with
209 an exception object stored in *EXCEPT_SCMP.
210 Otherwise the string is not guaranteed to be NUL-terminated, but worse
211 there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
212
213 Returns NULL if there is a conversion error, with the exception object
214 stored in *EXCEPT_SCMP.
215 It is an error to call this if STRING is not a string. */
216
217 gdb::unique_xmalloc_ptr<char>
218 gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp)
219 {
220 return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp);
221 }
222
223 /* Convert a host string to an SCM string.
224 This function is guaranteed to not throw an exception.
225 Returns a <gdb:exception> object if there's a conversion error. */
226
227 SCM
228 gdbscm_scm_from_host_string (const char *string, size_t len)
229 {
230 return gdbscm_scm_from_string (string, len, host_charset (), 1);
231 }
232
233 /* (string->argv string) -> list
234 Return list of strings split up according to GDB's argv parsing rules.
235 This is useful when writing GDB commands in Scheme. */
236
237 static SCM
238 gdbscm_string_to_argv (SCM string_scm)
239 {
240 char *string;
241 SCM result = SCM_EOL;
242
243 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
244 string_scm, &string);
245
246 if (string == NULL || *string == '\0')
247 {
248 xfree (string);
249 return SCM_EOL;
250 }
251
252 gdb_argv c_argv (string);
253 for (char *arg : c_argv)
254 result = scm_cons (gdbscm_scm_from_c_string (arg), result);
255
256 xfree (string);
257
258 return scm_reverse_x (result, SCM_EOL);
259 }
260 \f
261 /* Initialize the Scheme charset interface to GDB. */
262
263 static const scheme_function string_functions[] =
264 {
265 { "string->argv", 1, 0, 0, as_a_scm_t_subr (gdbscm_string_to_argv),
266 "\
267 Convert a string to a list of strings split up according to\n\
268 gdb's argv parsing rules." },
269
270 END_FUNCTIONS
271 };
272
273 void
274 gdbscm_initialize_strings (void)
275 {
276 gdbscm_define_functions (string_functions, 1);
277 }
This page took 0.03986 seconds and 5 git commands to generate.