1 /* GDB/Scheme charset interface.
3 Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
25 #include "guile-internal.h"
27 /* Convert STRING to an int.
28 STRING must be a valid integer. */
31 gdbscm_scm_string_to_int (SCM string)
33 char *s = scm_to_latin1_string (string);
40 /* Convert a C (latin1) string to an SCM string.
41 "latin1" is chosen because Guile won't throw an exception. */
44 gdbscm_scm_from_c_string (const char *string)
46 return scm_from_latin1_string (string);
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. */
55 gdbscm_scm_to_c_string (SCM string)
57 return scm_to_latin1_string (string);
60 /* Use printf to construct a Scheme string. */
63 gdbscm_scm_from_printf (const char *format, ...)
69 va_start (args, format);
70 string = xstrvprintf (format, args);
72 result = scm_from_latin1_string (string);
78 /* Struct to pass data from gdbscm_scm_to_string to
79 gdbscm_call_scm_to_stringn. */
81 struct scm_to_stringn_data
86 scm_t_string_failed_conversion_handler conversion_kind;
90 /* Helper for gdbscm_scm_to_string to call scm_to_stringn
91 from within scm_c_catch. */
94 gdbscm_call_scm_to_stringn (void *datap)
96 struct scm_to_stringn_data *data = (struct scm_to_stringn_data *) datap;
98 data->result = scm_to_stringn (data->string, data->lenp, data->charset,
99 data->conversion_kind);
103 /* Convert an SCM string to a string in charset CHARSET.
104 This function is guaranteed to not throw an exception.
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).
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.
116 Space for the result is allocated with malloc, caller must free.
117 It is an error to call this if STRING is not a string. */
120 gdbscm_scm_to_string (SCM string, size_t *lenp,
121 const char *charset, int strict, SCM *except_scmp)
123 struct scm_to_stringn_data data;
126 data.string = string;
128 data.charset = charset;
129 data.conversion_kind = (strict
130 ? SCM_FAILED_CONVERSION_ERROR
131 : SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
134 scm_result = gdbscm_call_guile (gdbscm_call_scm_to_stringn, &data, NULL);
136 if (gdbscm_is_false (scm_result))
138 gdb_assert (data.result != NULL);
141 gdb_assert (gdbscm_is_exception (scm_result));
142 *except_scmp = scm_result;
146 /* Struct to pass data from gdbscm_scm_from_string to
147 gdbscm_call_scm_from_stringn. */
149 struct scm_from_stringn_data
154 scm_t_string_failed_conversion_handler conversion_kind;
158 /* Helper for gdbscm_scm_from_string to call scm_from_stringn
159 from within scm_c_catch. */
162 gdbscm_call_scm_from_stringn (void *datap)
164 struct scm_from_stringn_data *data = (struct scm_from_stringn_data *) datap;
166 data->result = scm_from_stringn (data->string, data->len, data->charset,
167 data->conversion_kind);
171 /* Convert STRING to a Scheme string in charset CHARSET.
172 This function is guaranteed to not throw an exception.
174 If STRICT is non-zero, and there's a conversion error, then a
175 <gdb:exception> object is returned.
176 If STRICT is zero, then question marks are used for characters that
177 can't be converted (limitation of underlying Guile conversion support). */
180 gdbscm_scm_from_string (const char *string, size_t len,
181 const char *charset, int strict)
183 struct scm_from_stringn_data data;
186 data.string = string;
188 data.charset = charset;
189 /* The use of SCM_FAILED_CONVERSION_QUESTION_MARK is specified by Guile. */
190 data.conversion_kind = (strict
191 ? SCM_FAILED_CONVERSION_ERROR
192 : SCM_FAILED_CONVERSION_QUESTION_MARK);
193 data.result = SCM_UNDEFINED;
195 scm_result = gdbscm_call_guile (gdbscm_call_scm_from_stringn, &data, NULL);
197 if (gdbscm_is_false (scm_result))
199 gdb_assert (!SCM_UNBNDP (data.result));
202 gdb_assert (gdbscm_is_exception (scm_result));
206 /* Convert an SCM string to a host string.
207 This function is guaranteed to not throw an exception.
209 If LENP is NULL then the returned string is NUL-terminated,
210 and if the string contains embedded NULs then NULL is returned with
211 an exception object stored in *EXCEPT_SCMP.
212 Otherwise the string is not guaranteed to be NUL-terminated, but worse
213 there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
215 Returns NULL if there is a conversion error, with the exception object
216 stored in *EXCEPT_SCMP.
217 Space for the result is allocated with malloc, caller must free.
218 It is an error to call this if STRING is not a string. */
221 gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp)
223 return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp);
226 /* Convert a host string to an SCM string.
227 This function is guaranteed to not throw an exception.
228 Returns a <gdb:exception> object if there's a conversion error. */
231 gdbscm_scm_from_host_string (const char *string, size_t len)
233 return gdbscm_scm_from_string (string, len, host_charset (), 1);
236 /* (string->argv string) -> list
237 Return list of strings split up according to GDB's argv parsing rules.
238 This is useful when writing GDB commands in Scheme. */
241 gdbscm_string_to_argv (SCM string_scm)
246 SCM result = SCM_EOL;
248 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
249 string_scm, &string);
251 if (string == NULL || *string == '\0')
257 c_argv = gdb_buildargv (string);
258 for (i = 0; c_argv[i] != NULL; ++i)
259 result = scm_cons (gdbscm_scm_from_c_string (c_argv[i]), result);
264 return scm_reverse_x (result, SCM_EOL);
267 /* Initialize the Scheme charset interface to GDB. */
269 static const scheme_function string_functions[] =
271 { "string->argv", 1, 0, 0, as_a_scm_t_subr (gdbscm_string_to_argv),
273 Convert a string to a list of strings split up according to\n\
274 gdb's argv parsing rules." },
280 gdbscm_initialize_strings (void)
282 gdbscm_define_functions (string_functions, 1);