1 /* GDB/Scheme charset interface.
3 Copyright (C) 2014 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. */
26 #include "guile-internal.h"
28 /* Convert a C (latin1) string to an SCM string.
29 "latin1" is chosen because Guile won't throw an exception. */
32 gdbscm_scm_from_c_string (const char *string)
34 return scm_from_latin1_string (string);
37 /* Convert an SCM string to a C (latin1) string.
38 "latin1" is chosen because Guile won't throw an exception.
39 Space for the result is allocated with malloc, caller must free.
40 It is an error to call this if STRING is not a string. */
43 gdbscm_scm_to_c_string (SCM string)
45 return scm_to_latin1_string (string);
48 /* Use printf to construct a Scheme string. */
51 gdbscm_scm_from_printf (const char *format, ...)
57 va_start (args, format);
58 string = xstrvprintf (format, args);
60 result = scm_from_latin1_string (string);
66 /* Struct to pass data from gdbscm_scm_to_string to
67 gdbscm_call_scm_to_stringn. */
69 struct scm_to_stringn_data
78 /* Helper for gdbscm_scm_to_string to call scm_to_stringn
79 from within scm_c_catch. */
82 gdbscm_call_scm_to_stringn (void *datap)
84 struct scm_to_stringn_data *data = datap;
86 data->result = scm_to_stringn (data->string, data->lenp, data->charset,
87 data->conversion_kind);
91 /* Convert an SCM string to a string in charset CHARSET.
92 This function is guaranteed to not throw an exception.
93 If STRICT is non-zero, and there's a conversion error, then a
94 <gdb:exception> object is stored in *EXCEPT_SCMP, and NULL is returned.
95 If STRICT is zero, then escape sequences are used for characters that
96 can't be converted, and EXCEPT_SCMP may be passed as NULL.
97 Space for the result is allocated with malloc, caller must free.
98 It is an error to call this if STRING is not a string. */
101 gdbscm_scm_to_string (SCM string, size_t *lenp,
102 const char *charset, int strict, SCM *except_scmp)
104 struct scm_to_stringn_data data;
107 data.string = string;
109 data.charset = charset;
110 data.conversion_kind = (strict
111 ? SCM_FAILED_CONVERSION_ERROR
112 : SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
115 scm_result = gdbscm_call_guile (gdbscm_call_scm_to_stringn, &data, NULL);
117 if (gdbscm_is_false (scm_result))
119 gdb_assert (data.result != NULL);
122 gdb_assert (gdbscm_is_exception (scm_result));
123 *except_scmp = scm_result;
127 /* Struct to pass data from gdbscm_scm_from_string to
128 gdbscm_call_scm_from_stringn. */
130 struct scm_from_stringn_data
139 /* Helper for gdbscm_scm_from_string to call scm_from_stringn
140 from within scm_c_catch. */
143 gdbscm_call_scm_from_stringn (void *datap)
145 struct scm_from_stringn_data *data = datap;
147 data->result = scm_from_stringn (data->string, data->len, data->charset,
148 data->conversion_kind);
152 /* Convert STRING to a Scheme string in charset CHARSET.
153 This function is guaranteed to not throw an exception.
154 If STRICT is non-zero, and there's a conversion error, then a
155 <gdb:exception> object is returned.
156 If STRICT is zero, then question marks are used for characters that
157 can't be converted (limitation of underlying Guile conversion support). */
160 gdbscm_scm_from_string (const char *string, size_t len,
161 const char *charset, int strict)
163 struct scm_from_stringn_data data;
166 data.string = string;
168 data.charset = charset;
169 /* The use of SCM_FAILED_CONVERSION_QUESTION_MARK is specified by Guile. */
170 data.conversion_kind = (strict
171 ? SCM_FAILED_CONVERSION_ERROR
172 : SCM_FAILED_CONVERSION_QUESTION_MARK);
173 data.result = SCM_UNDEFINED;
175 scm_result = gdbscm_call_guile (gdbscm_call_scm_from_stringn, &data, NULL);
177 if (gdbscm_is_false (scm_result))
179 gdb_assert (!SCM_UNBNDP (data.result));
182 gdb_assert (gdbscm_is_exception (scm_result));
186 /* Convert an SCM string to a target string.
187 This function will thrown a conversion error if there's a problem.
188 Space for the result is allocated with malloc, caller must free.
189 It is an error to call this if STRING is not a string. */
192 gdbscm_scm_to_target_string_unsafe (SCM string, size_t *lenp,
193 struct gdbarch *gdbarch)
195 return scm_to_stringn (string, lenp, target_charset (gdbarch),
196 SCM_FAILED_CONVERSION_ERROR);
199 /* (string->argv string) -> list
200 Return list of strings split up according to GDB's argv parsing rules.
201 This is useful when writing GDB commands in Scheme. */
204 gdbscm_string_to_argv (SCM string_scm)
209 SCM result = SCM_EOL;
211 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
212 string_scm, &string);
214 if (string == NULL || *string == '\0')
220 c_argv = gdb_buildargv (string);
221 for (i = 0; c_argv[i] != NULL; ++i)
222 result = scm_cons (gdbscm_scm_from_c_string (c_argv[i]), result);
227 return scm_reverse_x (result, SCM_EOL);
230 /* Initialize the Scheme charset interface to GDB. */
232 static const scheme_function string_functions[] =
234 { "string->argv", 1, 0, 0, gdbscm_string_to_argv,
236 Convert a string to a list of strings split up according to\n\
237 gdb's argv parsing rules." },
243 gdbscm_initialize_strings (void)
245 gdbscm_define_functions (string_functions, 1);