Remove make_cleanup_value_free_to_mark
[deliverable/binutils-gdb.git] / gdb / guile / scm-string.c
CommitLineData
ed3ef339
DE
1/* GDB/Scheme charset interface.
2
e2882c85 3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
ed3ef339
DE
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"
ed3ef339
DE
24#include "charset.h"
25#include "guile-internal.h"
26
d2929fdc
DE
27/* Convert STRING to an int.
28 STRING must be a valid integer. */
29
30int
31gdbscm_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
ed3ef339
DE
40/* Convert a C (latin1) string to an SCM string.
41 "latin1" is chosen because Guile won't throw an exception. */
42
43SCM
44gdbscm_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 Space for the result is allocated with malloc, caller must free.
52 It is an error to call this if STRING is not a string. */
53
54char *
55gdbscm_scm_to_c_string (SCM string)
56{
57 return scm_to_latin1_string (string);
58}
59
60/* Use printf to construct a Scheme string. */
61
62SCM
63gdbscm_scm_from_printf (const char *format, ...)
64{
65 va_list args;
66 char *string;
67 SCM result;
68
69 va_start (args, format);
70 string = xstrvprintf (format, args);
71 va_end (args);
72 result = scm_from_latin1_string (string);
73 xfree (string);
74
75 return result;
76}
77
78/* Struct to pass data from gdbscm_scm_to_string to
79 gdbscm_call_scm_to_stringn. */
80
81struct scm_to_stringn_data
82{
83 SCM string;
84 size_t *lenp;
85 const char *charset;
f486487f 86 scm_t_string_failed_conversion_handler conversion_kind;
ed3ef339
DE
87 char *result;
88};
89
90/* Helper for gdbscm_scm_to_string to call scm_to_stringn
91 from within scm_c_catch. */
92
93static SCM
94gdbscm_call_scm_to_stringn (void *datap)
95{
9a3c8263 96 struct scm_to_stringn_data *data = (struct scm_to_stringn_data *) datap;
ed3ef339
DE
97
98 data->result = scm_to_stringn (data->string, data->lenp, data->charset,
99 data->conversion_kind);
100 return SCM_BOOL_F;
101}
102
103/* Convert an SCM string to a string in charset CHARSET.
104 This function is guaranteed to not throw an exception.
06eb1586
DE
105
106 If LENP is NULL then the returned string is NUL-terminated,
107 and an exception is thrown if the string contains embedded NULs.
108 Otherwise the string is not guaranteed to be NUL-terminated, but worse
109 there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
110
ed3ef339
DE
111 If STRICT is non-zero, and there's a conversion error, then a
112 <gdb:exception> object is stored in *EXCEPT_SCMP, and NULL is returned.
113 If STRICT is zero, then escape sequences are used for characters that
114 can't be converted, and EXCEPT_SCMP may be passed as NULL.
06eb1586 115
ed3ef339
DE
116 It is an error to call this if STRING is not a string. */
117
c6c6149a 118gdb::unique_xmalloc_ptr<char>
ed3ef339
DE
119gdbscm_scm_to_string (SCM string, size_t *lenp,
120 const char *charset, int strict, SCM *except_scmp)
121{
122 struct scm_to_stringn_data data;
123 SCM scm_result;
124
125 data.string = string;
126 data.lenp = lenp;
127 data.charset = charset;
128 data.conversion_kind = (strict
129 ? SCM_FAILED_CONVERSION_ERROR
130 : SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
131 data.result = NULL;
132
133 scm_result = gdbscm_call_guile (gdbscm_call_scm_to_stringn, &data, NULL);
134
135 if (gdbscm_is_false (scm_result))
136 {
137 gdb_assert (data.result != NULL);
c6c6149a 138 return gdb::unique_xmalloc_ptr<char> (data.result);
ed3ef339
DE
139 }
140 gdb_assert (gdbscm_is_exception (scm_result));
141 *except_scmp = scm_result;
142 return NULL;
143}
144
145/* Struct to pass data from gdbscm_scm_from_string to
146 gdbscm_call_scm_from_stringn. */
147
148struct scm_from_stringn_data
149{
150 const char *string;
151 size_t len;
152 const char *charset;
f486487f 153 scm_t_string_failed_conversion_handler conversion_kind;
ed3ef339
DE
154 SCM result;
155};
156
157/* Helper for gdbscm_scm_from_string to call scm_from_stringn
158 from within scm_c_catch. */
159
160static SCM
161gdbscm_call_scm_from_stringn (void *datap)
162{
9a3c8263 163 struct scm_from_stringn_data *data = (struct scm_from_stringn_data *) datap;
ed3ef339
DE
164
165 data->result = scm_from_stringn (data->string, data->len, data->charset,
166 data->conversion_kind);
167 return SCM_BOOL_F;
168}
169
170/* Convert STRING to a Scheme string in charset CHARSET.
171 This function is guaranteed to not throw an exception.
06eb1586 172
ed3ef339
DE
173 If STRICT is non-zero, and there's a conversion error, then a
174 <gdb:exception> object is returned.
175 If STRICT is zero, then question marks are used for characters that
176 can't be converted (limitation of underlying Guile conversion support). */
177
178SCM
179gdbscm_scm_from_string (const char *string, size_t len,
180 const char *charset, int strict)
181{
182 struct scm_from_stringn_data data;
183 SCM scm_result;
184
185 data.string = string;
186 data.len = len;
187 data.charset = charset;
188 /* The use of SCM_FAILED_CONVERSION_QUESTION_MARK is specified by Guile. */
189 data.conversion_kind = (strict
190 ? SCM_FAILED_CONVERSION_ERROR
191 : SCM_FAILED_CONVERSION_QUESTION_MARK);
192 data.result = SCM_UNDEFINED;
193
194 scm_result = gdbscm_call_guile (gdbscm_call_scm_from_stringn, &data, NULL);
195
196 if (gdbscm_is_false (scm_result))
197 {
198 gdb_assert (!SCM_UNBNDP (data.result));
199 return data.result;
200 }
201 gdb_assert (gdbscm_is_exception (scm_result));
202 return scm_result;
203}
204
06eb1586
DE
205/* Convert an SCM string to a host string.
206 This function is guaranteed to not throw an exception.
207
208 If LENP is NULL then the returned string is NUL-terminated,
209 and if the string contains embedded NULs then NULL is returned with
210 an exception object stored in *EXCEPT_SCMP.
211 Otherwise the string is not guaranteed to be NUL-terminated, but worse
212 there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
213
214 Returns NULL if there is a conversion error, with the exception object
215 stored in *EXCEPT_SCMP.
06eb1586
DE
216 It is an error to call this if STRING is not a string. */
217
c6c6149a 218gdb::unique_xmalloc_ptr<char>
06eb1586
DE
219gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp)
220{
221 return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp);
222}
223
224/* Convert a host string to an SCM string.
225 This function is guaranteed to not throw an exception.
226 Returns a <gdb:exception> object if there's a conversion error. */
227
228SCM
229gdbscm_scm_from_host_string (const char *string, size_t len)
230{
231 return gdbscm_scm_from_string (string, len, host_charset (), 1);
232}
233
ed3ef339
DE
234/* (string->argv string) -> list
235 Return list of strings split up according to GDB's argv parsing rules.
236 This is useful when writing GDB commands in Scheme. */
237
238static SCM
239gdbscm_string_to_argv (SCM string_scm)
240{
241 char *string;
ed3ef339
DE
242 SCM result = SCM_EOL;
243
244 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
245 string_scm, &string);
246
247 if (string == NULL || *string == '\0')
248 {
249 xfree (string);
250 return SCM_EOL;
251 }
252
773a1edc
TT
253 gdb_argv c_argv (string);
254 for (char *arg : c_argv)
255 result = scm_cons (gdbscm_scm_from_c_string (arg), result);
ed3ef339 256
ed3ef339
DE
257 xfree (string);
258
259 return scm_reverse_x (result, SCM_EOL);
260}
261\f
262/* Initialize the Scheme charset interface to GDB. */
263
264static const scheme_function string_functions[] =
265{
72e02483 266 { "string->argv", 1, 0, 0, as_a_scm_t_subr (gdbscm_string_to_argv),
ed3ef339
DE
267 "\
268Convert a string to a list of strings split up according to\n\
269gdb's argv parsing rules." },
270
271 END_FUNCTIONS
272};
273
274void
275gdbscm_initialize_strings (void)
276{
277 gdbscm_define_functions (string_functions, 1);
278}
This page took 0.370412 seconds and 4 git commands to generate.