1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/* GDB parameters implemented in Guile.
Copyright (C) 2008-2024 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "value.h"
#include "charset.h"
#include "cli/cli-decode.h"
#include "completer.h"
#include "language.h"
#include "arch-utils.h"
#include "guile-internal.h"
/* A GDB color. */
struct color_smob
{
/* This always appears first. */
gdb_smob base;
/* Underlying value. */
ui_file_style::color color;
};
static const char color_smob_name[] = "gdb:color";
/* The tag Guile knows the color smob by. */
static scm_t_bits color_smob_tag;
/* Keywords used by make-color. */
static SCM colorspace_keyword;
static const char *coscm_colorspace_name (color_space colorspace);
/* Administrivia for color smobs. */
static int
coscm_print_color_smob (SCM self, SCM port, scm_print_state *pstate)
{
const ui_file_style::color &color = coscm_get_color (self);
gdbscm_printf (port, "#<%s", color_smob_name);
gdbscm_printf (port, " %s", color.to_string ().c_str ());
gdbscm_printf (port, " %s", coscm_colorspace_name (color.colorspace ()));
scm_puts (">", port);
scm_remember_upto_here_1 (self);
/* Non-zero means success. */
return 1;
}
/* Create an empty (uninitialized) color. */
static SCM
coscm_make_color_smob (void)
{
color_smob *c_smob = (color_smob *)
scm_gc_calloc (sizeof (color_smob), color_smob_name);
SCM c_scm;
c_smob->color = ui_file_style::color (ui_file_style::NONE);
c_scm = scm_new_smob (color_smob_tag, (scm_t_bits) c_smob);
gdbscm_init_gsmob (&c_smob->base);
return c_scm;
}
/* Return the <gdb:color> object that encapsulates COLOR. */
SCM
coscm_scm_from_color (const ui_file_style::color &color)
{
SCM c_scm = coscm_make_color_smob ();
color_smob *c_smob = (color_smob *) SCM_SMOB_DATA (c_scm);
c_smob->color = color;
return c_scm;
}
/* Return the color field of color_smob. */
const ui_file_style::color &
coscm_get_color (SCM color_scm)
{
SCM_ASSERT_TYPE (coscm_is_color (color_scm), color_scm, SCM_ARG1, FUNC_NAME,
_("<gdb:color>"));
color_smob *c_smob = (color_smob *) SCM_SMOB_DATA (color_scm);
return c_smob->color;
}
/* Returns non-zero if SCM is a <gdb:color> object. */
int
coscm_is_color (SCM scm)
{
return SCM_SMOB_PREDICATE (color_smob_tag, scm);
}
/* (gdb:color? scm) -> boolean */
static SCM
gdbscm_color_p (SCM scm)
{
return scm_from_bool (coscm_is_color (scm));
}
static const scheme_integer_constant colorspaces[] =
{
{ "COLORSPACE_MONOCHROME", (int) color_space::MONOCHROME },
{ "COLORSPACE_ANSI_8COLOR", (int) color_space::ANSI_8COLOR },
{ "COLORSPACE_AIXTERM_16COLOR", (int) color_space::AIXTERM_16COLOR },
{ "COLORSPACE_XTERM_256COLOR", (int) color_space::XTERM_256COLOR },
{ "COLORSPACE_RGB_24BIT", (int) color_space::RGB_24BIT },
END_INTEGER_CONSTANTS
};
/* Return COLORSPACE as a string. */
static const char *
coscm_colorspace_name (color_space colorspace)
{
for (int i = 0; colorspaces[i].name != nullptr; ++i)
{
if (colorspaces[i].value == static_cast<int> (colorspace))
return colorspaces[i].name;
}
gdb_assert_not_reached ("bad color space");
}
/* Free function for a color_smob. */
static size_t
coscm_free_color_smob (SCM self)
{
(void) self;
return 0;
}
/* Color Scheme functions. */
/* (make-color [value
[#:color-space colorspace]]) -> <gdb:color>
VALUE is the value of the color. It may be SCM_UNDEFINED, string, number
or list.
COLORSPACE is the color space of the VALUE. It should be one of the
COLORSPACE_* constants defined in the gdb module.
The result is the <gdb:color> Scheme object. */
static SCM
gdbscm_make_color (SCM value_scm, SCM rest)
{
SCM colorspace_arg = SCM_UNDEFINED;
color_space colorspace = color_space::MONOCHROME;
scm_c_bind_keyword_arguments (FUNC_NAME, rest,
static_cast<scm_t_keyword_arguments_flags> (0),
colorspace_keyword, &colorspace_arg,
SCM_UNDEFINED);
if (!SCM_UNBNDP (colorspace_arg))
{
SCM_ASSERT_TYPE (scm_is_integer (colorspace_arg), colorspace_arg,
SCM_ARG2, FUNC_NAME, _("int"));
int colorspace_int = scm_to_int (colorspace_arg);
if (!color_space_safe_cast (&colorspace, colorspace_int))
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2,
scm_from_int (colorspace_int),
_("invalid colorspace argument"));
}
ui_file_style::color color = ui_file_style::NONE;
gdbscm_gdb_exception exc {};
try
{
if (SCM_UNBNDP (value_scm) || scm_is_integer (value_scm))
{
int i = -1;
if (scm_is_integer (value_scm))
{
i = scm_to_int (value_scm);
if (i < 0)
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, value_scm,
_("negative color index"));
}
if (SCM_UNBNDP (colorspace_arg))
color = ui_file_style::color (i);
else
color = ui_file_style::color (colorspace, i);
}
else if (gdbscm_is_true (scm_list_p (value_scm)))
{
if (SCM_UNBNDP (colorspace_arg)
|| colorspace != color_space::RGB_24BIT)
error (_("colorspace must be COLORSPACE_RGB_24BIT with "
"value of list type."));
if (scm_ilength (value_scm) != 3)
error (_("List value with RGB must be of size 3."));
uint8_t rgb[3] = {};
int i = 0;
for (; i < 3 && !scm_is_eq (value_scm, SCM_EOL); ++i)
{
SCM item = scm_car (value_scm);
SCM_ASSERT_TYPE (scm_is_integer (item), item, SCM_ARG1, FUNC_NAME,
_("int"));
int component = scm_to_int (item);
if (component < 0 || component > UINT8_MAX)
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, item,
_("invalid rgb component"));
rgb[i] = static_cast<uint8_t> (component);
value_scm = scm_cdr (value_scm);
}
gdb_assert (i == 3);
color = ui_file_style::color (rgb[0], rgb[1], rgb[2]);
}
else if (scm_is_string (value_scm))
{
SCM exception;
gdb::unique_xmalloc_ptr<char> string
= gdbscm_scm_to_host_string (value_scm, nullptr, &exception);
if (string == nullptr)
gdbscm_throw (exception);
color = parse_var_color (string.get ());
if (!SCM_UNBNDP (colorspace_arg) && colorspace != color.colorspace ())
error (_("colorspace doesn't match to the value."));
}
else
scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, value_scm,
"integer, string or list");
}
catch (const gdb_exception &except)
{
exc = unpack (except);
}
GDBSCM_HANDLE_GDB_EXCEPTION (exc);
return coscm_scm_from_color (color);
}
/* (color-string <gdb:color>) -> value */
static SCM
gdbscm_color_string (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
std::string s = color.to_string ();
return gdbscm_scm_from_host_string (s.c_str (), s.size ());
}
/* (color-colorspace <gdb:color>) -> value */
static SCM
gdbscm_color_colorspace (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
return scm_from_int (static_cast<int> (color.colorspace ()));
}
/* (color-none? scm) -> boolean */
static SCM
gdbscm_color_none_p (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
return scm_from_bool (color.is_none ());
}
/* (color-indexed? scm) -> boolean */
static SCM
gdbscm_color_indexed_p (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
return scm_from_bool (color.is_indexed ());
}
/* (color-direct? scm) -> boolean */
static SCM
gdbscm_color_direct_p (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
return scm_from_bool (color.is_direct ());
}
/* (color-index <gdb:color>) -> value */
static SCM
gdbscm_color_index (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
if (!color.is_indexed ())
gdbscm_misc_error (FUNC_NAME, SCM_ARG1, self, "color is not indexed");
return scm_from_int (color.get_value ());
}
/* (color-components <gdb:color>) -> value */
static SCM
gdbscm_color_components (SCM self)
{
const ui_file_style::color &color = coscm_get_color (self);
if (!color.is_direct ())
gdbscm_misc_error (FUNC_NAME, SCM_ARG1, self, "color is not direct");
uint8_t rgb[3] = {};
color.get_rgb (rgb);
SCM red = scm_from_uint8 (rgb[0]);
SCM green = scm_from_uint8 (rgb[1]);
SCM blue = scm_from_uint8 (rgb[2]);
return scm_list_3 (red, green, blue);
}
/* (color-escape-sequence <gdb:color> is_fg) -> value */
static SCM
gdbscm_color_escape_sequence (SCM self, SCM is_fg_scm)
{
const ui_file_style::color &color = coscm_get_color (self);
SCM_ASSERT_TYPE (gdbscm_is_bool (is_fg_scm), is_fg_scm, SCM_ARG2, FUNC_NAME,
_("boolean"));
bool is_fg = gdbscm_is_true (is_fg_scm);
std::string s = color.to_ansi (is_fg);
return gdbscm_scm_from_host_string (s.c_str (), s.size ());
}
/* Initialize the Scheme color support. */
static const scheme_function color_functions[] =
{
{ "make-color", 0, 1, 1, as_a_scm_t_subr (gdbscm_make_color),
"\
Make a GDB color object.\n\
\n\
Arguments: [value\n\
[#:color-space <colorspace>]]\n\
value: The name of the color. It may be string, number with color index\n\
or list with RGB components.\n\
colorspace: The color space of the color, one of COLORSPACE_*." },
{ "color?", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_p),
"\
Return #t if the object is a <gdb:color> object." },
{ "color-none?", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_none_p),
"\
Return #t if the <gdb:color> object has default color." },
{ "color-indexed?", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_indexed_p),
"\
Return #t if the <gdb:color> object is from indexed color space." },
{ "color-direct?", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_direct_p),
"\
Return #t if the <gdb:color> object has direct color (e.g. RGB, CMY, CMYK)." },
{ "color-string", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_string),
"\
Return the textual representation of a <gdb:color> object." },
{ "color-colorspace", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_colorspace),
"\
Return the color space of a <gdb:color> object." },
{ "color-index", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_index),
"\
Return index of the color of a <gdb:color> object in a palette." },
{ "color-components", 1, 0, 0, as_a_scm_t_subr (gdbscm_color_components),
"\
Return components of the direct <gdb:color> object." },
{ "color-escape-sequence", 2, 0, 0,
as_a_scm_t_subr (gdbscm_color_escape_sequence),
"\
Return string to change terminal's color to this." },
END_FUNCTIONS
};
void
gdbscm_initialize_colors (void)
{
color_smob_tag = gdbscm_make_smob_type (color_smob_name, sizeof (color_smob));
scm_set_smob_free (color_smob_tag, coscm_free_color_smob);
scm_set_smob_print (color_smob_tag, coscm_print_color_smob);
gdbscm_define_integer_constants (colorspaces, 1);
gdbscm_define_functions (color_functions, 1);
colorspace_keyword = scm_from_latin1_keyword ("color-space");
}
|