/* GDB/Scheme charset interface.
- Copyright (C) 2014 Free Software Foundation, Inc.
+ Copyright (C) 2014-2020 Free Software Foundation, Inc.
This file is part of GDB.
conventions, et.al. */
#include "defs.h"
-#include <stdarg.h>
#include "charset.h"
#include "guile-internal.h"
+/* Convert STRING to an int.
+ STRING must be a valid integer. */
+
+int
+gdbscm_scm_string_to_int (SCM string)
+{
+ char *s = scm_to_latin1_string (string);
+ int r = atoi (s);
+
+ free (s);
+ return r;
+}
+
/* Convert a C (latin1) string to an SCM string.
"latin1" is chosen because Guile won't throw an exception. */
/* Convert an SCM string to a C (latin1) string.
"latin1" is chosen because Guile won't throw an exception.
- Space for the result is allocated with malloc, caller must free.
It is an error to call this if STRING is not a string. */
-char *
+gdb::unique_xmalloc_ptr<char>
gdbscm_scm_to_c_string (SCM string)
{
- return scm_to_latin1_string (string);
+ return gdb::unique_xmalloc_ptr<char> (scm_to_latin1_string (string));
}
/* Use printf to construct a Scheme string. */
gdbscm_scm_from_printf (const char *format, ...)
{
va_list args;
- char *string;
SCM result;
va_start (args, format);
- string = xstrvprintf (format, args);
+ std::string string = string_vprintf (format, args);
va_end (args);
- result = scm_from_latin1_string (string);
- xfree (string);
+ result = scm_from_latin1_string (string.c_str ());
return result;
}
SCM string;
size_t *lenp;
const char *charset;
- int conversion_kind;
+ scm_t_string_failed_conversion_handler conversion_kind;
char *result;
};
static SCM
gdbscm_call_scm_to_stringn (void *datap)
{
- struct scm_to_stringn_data *data = datap;
+ struct scm_to_stringn_data *data = (struct scm_to_stringn_data *) datap;
data->result = scm_to_stringn (data->string, data->lenp, data->charset,
data->conversion_kind);
/* Convert an SCM string to a string in charset CHARSET.
This function is guaranteed to not throw an exception.
+
+ If LENP is NULL then the returned string is NUL-terminated,
+ and an exception is thrown if the string contains embedded NULs.
+ Otherwise the string is not guaranteed to be NUL-terminated, but worse
+ there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
+
If STRICT is non-zero, and there's a conversion error, then a
<gdb:exception> object is stored in *EXCEPT_SCMP, and NULL is returned.
If STRICT is zero, then escape sequences are used for characters that
can't be converted, and EXCEPT_SCMP may be passed as NULL.
- Space for the result is allocated with malloc, caller must free.
+
It is an error to call this if STRING is not a string. */
-char *
+gdb::unique_xmalloc_ptr<char>
gdbscm_scm_to_string (SCM string, size_t *lenp,
const char *charset, int strict, SCM *except_scmp)
{
if (gdbscm_is_false (scm_result))
{
gdb_assert (data.result != NULL);
- return data.result;
+ return gdb::unique_xmalloc_ptr<char> (data.result);
}
gdb_assert (gdbscm_is_exception (scm_result));
*except_scmp = scm_result;
const char *string;
size_t len;
const char *charset;
- int conversion_kind;
+ scm_t_string_failed_conversion_handler conversion_kind;
SCM result;
};
static SCM
gdbscm_call_scm_from_stringn (void *datap)
{
- struct scm_from_stringn_data *data = datap;
+ struct scm_from_stringn_data *data = (struct scm_from_stringn_data *) datap;
data->result = scm_from_stringn (data->string, data->len, data->charset,
data->conversion_kind);
/* Convert STRING to a Scheme string in charset CHARSET.
This function is guaranteed to not throw an exception.
+
If STRICT is non-zero, and there's a conversion error, then a
<gdb:exception> object is returned.
If STRICT is zero, then question marks are used for characters that
return scm_result;
}
+/* Convert an SCM string to a host string.
+ This function is guaranteed to not throw an exception.
+
+ If LENP is NULL then the returned string is NUL-terminated,
+ and if the string contains embedded NULs then NULL is returned with
+ an exception object stored in *EXCEPT_SCMP.
+ Otherwise the string is not guaranteed to be NUL-terminated, but worse
+ there's no space to put a NUL if we wanted to (scm_to_stringn limitation).
+
+ Returns NULL if there is a conversion error, with the exception object
+ stored in *EXCEPT_SCMP.
+ It is an error to call this if STRING is not a string. */
+
+gdb::unique_xmalloc_ptr<char>
+gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp)
+{
+ return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp);
+}
+
+/* Convert a host string to an SCM string.
+ This function is guaranteed to not throw an exception.
+ Returns a <gdb:exception> object if there's a conversion error. */
+
+SCM
+gdbscm_scm_from_host_string (const char *string, size_t len)
+{
+ return gdbscm_scm_from_string (string, len, host_charset (), 1);
+}
+
/* (string->argv string) -> list
Return list of strings split up according to GDB's argv parsing rules.
This is useful when writing GDB commands in Scheme. */
gdbscm_string_to_argv (SCM string_scm)
{
char *string;
- char **c_argv;
- int i;
SCM result = SCM_EOL;
gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
return SCM_EOL;
}
- c_argv = gdb_buildargv (string);
- for (i = 0; c_argv[i] != NULL; ++i)
- result = scm_cons (gdbscm_scm_from_c_string (c_argv[i]), result);
+ gdb_argv c_argv (string);
+ for (char *arg : c_argv)
+ result = scm_cons (gdbscm_scm_from_c_string (arg), result);
- freeargv (c_argv);
xfree (string);
return scm_reverse_x (result, SCM_EOL);
static const scheme_function string_functions[] =
{
- { "string->argv", 1, 0, 0, gdbscm_string_to_argv,
+ { "string->argv", 1, 0, 0, as_a_scm_t_subr (gdbscm_string_to_argv),
"\
Convert a string to a list of strings split up according to\n\
gdb's argv parsing rules." },