]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-param.c
gdb/doc: fix the example for get_set_string in Python API docs
[thirdparty/binutils-gdb.git] / gdb / python / py-param.c
CommitLineData
d7b32ed3
PM
1/* GDB parameters implemented in Python
2
3666a048 3 Copyright (C) 2008-2021 Free Software Foundation, Inc.
d7b32ed3
PM
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
21#include "defs.h"
22#include "value.h"
d7b32ed3
PM
23#include "python-internal.h"
24#include "charset.h"
25#include "gdbcmd.h"
26#include "cli/cli-decode.h"
27#include "completer.h"
ecec24e6
PM
28#include "language.h"
29#include "arch-utils.h"
d7b32ed3
PM
30
31/* Parameter constants and their values. */
6bd434d6 32static struct {
a121b7c1 33 const char *name;
d7b32ed3 34 int value;
6bd434d6 35} parm_constants[] =
d7b32ed3 36{
3c0ee1a4 37 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
d7b32ed3
PM
38 { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
39 { "PARAM_UINTEGER", var_uinteger },
40 { "PARAM_INTEGER", var_integer },
41 { "PARAM_STRING", var_string },
42 { "PARAM_STRING_NOESCAPE", var_string_noescape },
43 { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
44 { "PARAM_FILENAME", var_filename },
45 { "PARAM_ZINTEGER", var_zinteger },
0489430a
TT
46 { "PARAM_ZUINTEGER", var_zuinteger },
47 { "PARAM_ZUINTEGER_UNLIMITED", var_zuinteger_unlimited },
d7b32ed3
PM
48 { "PARAM_ENUM", var_enum },
49 { NULL, 0 }
50};
51
52/* A union that can hold anything described by enum var_types. */
53union parmpy_variable
54{
491144b5
CB
55 /* Hold a boolean value. */
56 bool boolval;
57
58 /* Hold an integer value. */
d7b32ed3
PM
59 int intval;
60
61 /* Hold an auto_boolean. */
62 enum auto_boolean autoboolval;
63
64 /* Hold an unsigned integer value, for uinteger. */
65 unsigned int uintval;
66
67 /* Hold a string, for the various string types. */
68 char *stringval;
69
70 /* Hold a string, for enums. */
71 const char *cstringval;
72};
73
74/* A GDB parameter. */
75struct parmpy_object
76{
77 PyObject_HEAD
78
79 /* The type of the parameter. */
80 enum var_types type;
81
82 /* The value of the parameter. */
83 union parmpy_variable value;
84
85 /* For an enum command, the possible values. The vector is
86 allocated with xmalloc, as is each element. It is
87 NULL-terminated. */
88 const char **enumeration;
89};
90
e36122e9 91extern PyTypeObject parmpy_object_type
62eec1a5 92 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
d7b32ed3
PM
93
94/* Some handy string constants. */
95static PyObject *set_doc_cst;
96static PyObject *show_doc_cst;
97
98\f
99
100/* Get an attribute. */
101static PyObject *
102get_attr (PyObject *obj, PyObject *attr_name)
103{
104 if (PyString_Check (attr_name)
9a27f2c6
PK
105#ifdef IS_PY3K
106 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
107#else
d7b32ed3 108 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 109#endif
d7b32ed3
PM
110 {
111 parmpy_object *self = (parmpy_object *) obj;
d59b6f6c 112
d7b32ed3
PM
113 return gdbpy_parameter_value (self->type, &self->value);
114 }
115
116 return PyObject_GenericGetAttr (obj, attr_name);
117}
118
8dc78533
JK
119/* Set a parameter value from a Python value. Return 0 on success. Returns
120 -1 on error, with a python exception set. */
d7b32ed3
PM
121static int
122set_parameter_value (parmpy_object *self, PyObject *value)
123{
124 int cmp;
125
126 switch (self->type)
127 {
128 case var_string:
129 case var_string_noescape:
130 case var_optional_filename:
131 case var_filename:
132 if (! gdbpy_is_string (value)
133 && (self->type == var_filename
134 || value != Py_None))
135 {
256458bc 136 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
137 _("String required for filename."));
138
139 return -1;
140 }
d7b32ed3
PM
141 if (value == Py_None)
142 {
8dc78533 143 xfree (self->value.stringval);
d7b32ed3
PM
144 if (self->type == var_optional_filename)
145 self->value.stringval = xstrdup ("");
146 else
147 self->value.stringval = NULL;
148 }
149 else
8dc78533 150 {
9b972014
TT
151 gdb::unique_xmalloc_ptr<char>
152 string (python_string_to_host_string (value));
8dc78533
JK
153 if (string == NULL)
154 return -1;
155
156 xfree (self->value.stringval);
9b972014 157 self->value.stringval = string.release ();
8dc78533 158 }
d7b32ed3
PM
159 break;
160
161 case var_enum:
162 {
163 int i;
d7b32ed3
PM
164
165 if (! gdbpy_is_string (value))
166 {
256458bc 167 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
168 _("ENUM arguments must be a string."));
169 return -1;
170 }
171
9b972014
TT
172 gdb::unique_xmalloc_ptr<char>
173 str (python_string_to_host_string (value));
8dc78533
JK
174 if (str == NULL)
175 return -1;
d7b32ed3 176 for (i = 0; self->enumeration[i]; ++i)
9b972014 177 if (! strcmp (self->enumeration[i], str.get ()))
d7b32ed3 178 break;
d7b32ed3
PM
179 if (! self->enumeration[i])
180 {
181 PyErr_SetString (PyExc_RuntimeError,
182 _("The value must be member of an enumeration."));
183 return -1;
184 }
185 self->value.cstringval = self->enumeration[i];
186 break;
187 }
188
189 case var_boolean:
190 if (! PyBool_Check (value))
191 {
256458bc 192 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
193 _("A boolean argument is required."));
194 return -1;
195 }
196 cmp = PyObject_IsTrue (value);
256458bc 197 if (cmp < 0)
d7b32ed3 198 return -1;
491144b5 199 self->value.boolval = cmp;
d7b32ed3
PM
200 break;
201
202 case var_auto_boolean:
203 if (! PyBool_Check (value) && value != Py_None)
204 {
205 PyErr_SetString (PyExc_RuntimeError,
206 _("A boolean or None is required"));
207 return -1;
208 }
209
210 if (value == Py_None)
211 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
212 else
213 {
214 cmp = PyObject_IsTrue (value);
215 if (cmp < 0 )
256458bc 216 return -1;
d7b32ed3
PM
217 if (cmp == 1)
218 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
256458bc 219 else
d7b32ed3 220 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
d7b32ed3 221 }
92e96192 222 break;
d7b32ed3
PM
223
224 case var_integer:
225 case var_zinteger:
226 case var_uinteger:
0489430a
TT
227 case var_zuinteger:
228 case var_zuinteger_unlimited:
d7b32ed3
PM
229 {
230 long l;
231 int ok;
232
233 if (! PyInt_Check (value))
234 {
256458bc 235 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
236 _("The value must be integer."));
237 return -1;
238 }
239
74aedc46
TT
240 if (! gdb_py_int_as_long (value, &l))
241 return -1;
242
0489430a 243 switch (self->type)
d7b32ed3 244 {
0489430a 245 case var_uinteger:
d7b32ed3
PM
246 if (l == 0)
247 l = UINT_MAX;
0489430a
TT
248 /* Fall through. */
249 case var_zuinteger:
250 ok = (l >= 0 && l <= UINT_MAX);
251 break;
252
253 case var_zuinteger_unlimited:
254 ok = (l >= -1 && l <= INT_MAX);
255 break;
256
257 case var_integer:
d7b32ed3
PM
258 ok = (l >= INT_MIN && l <= INT_MAX);
259 if (l == 0)
260 l = INT_MAX;
0489430a
TT
261 break;
262
263 case var_zinteger:
264 ok = (l >= INT_MIN && l <= INT_MAX);
265 break;
266
267 default:
268 gdb_assert_not_reached ("unknown var_ constant");
d7b32ed3 269 }
d7b32ed3
PM
270
271 if (! ok)
272 {
256458bc 273 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
274 _("Range exceeded."));
275 return -1;
276 }
277
0489430a
TT
278 if (self->type == var_uinteger || self->type == var_zuinteger)
279 self->value.uintval = (unsigned) l;
280 else
281 self->value.intval = (int) l;
d7b32ed3
PM
282 break;
283 }
284
285 default:
256458bc 286 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
287 _("Unhandled type in parameter value."));
288 return -1;
289 }
290
291 return 0;
292}
293
8dc78533 294/* Set an attribute. Returns -1 on error, with a python exception set. */
d7b32ed3
PM
295static int
296set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
297{
298 if (PyString_Check (attr_name)
9a27f2c6
PK
299#ifdef IS_PY3K
300 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
301#else
d7b32ed3 302 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 303#endif
d7b32ed3
PM
304 {
305 if (!val)
306 {
307 PyErr_SetString (PyExc_RuntimeError,
308 _("Cannot delete a parameter's value."));
309 return -1;
310 }
311 return set_parameter_value ((parmpy_object *) obj, val);
312 }
313
314 return PyObject_GenericSetAttr (obj, attr_name, val);
315}
316
ecec24e6
PM
317/* A helper function which returns a documentation string for an
318 object. */
319
9b972014 320static gdb::unique_xmalloc_ptr<char>
ecec24e6
PM
321get_doc_string (PyObject *object, PyObject *attr)
322{
9b972014 323 gdb::unique_xmalloc_ptr<char> result;
ecec24e6
PM
324
325 if (PyObject_HasAttr (object, attr))
326 {
7780f186 327 gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
ecec24e6 328
97d83487 329 if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
ecec24e6 330 {
97d83487 331 result = python_string_to_host_string (ds_obj.get ());
ecec24e6
PM
332 if (result == NULL)
333 gdbpy_print_stack ();
334 }
ecec24e6
PM
335 }
336 if (! result)
9b972014 337 result.reset (xstrdup (_("This command is not documented.")));
ecec24e6
PM
338 return result;
339}
340
341/* Helper function which will execute a METHOD in OBJ passing the
342 argument ARG. ARG can be NULL. METHOD should return a Python
343 string. If this function returns NULL, there has been an error and
344 the appropriate exception set. */
9b972014 345static gdb::unique_xmalloc_ptr<char>
ecec24e6
PM
346call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
347{
9b972014 348 gdb::unique_xmalloc_ptr<char> data;
7780f186 349 gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
ecec24e6 350
1bb44c9f 351 if (result == NULL)
ecec24e6
PM
352 return NULL;
353
1bb44c9f 354 if (gdbpy_is_string (result.get ()))
ecec24e6 355 {
1bb44c9f 356 data = python_string_to_host_string (result.get ());
ecec24e6
PM
357 if (! data)
358 return NULL;
359 }
360 else
361 {
362 PyErr_SetString (PyExc_RuntimeError,
363 _("Parameter must return a string value."));
364 return NULL;
365 }
366
367 return data;
368}
369
370/* A callback function that is registered against the respective
763b8efd
AB
371 add_setshow_* set_doc prototype. This function calls the Python function
372 "get_set_string" if it exists, which will return a string. That string
373 is then printed. If "get_set_string" does not exist, or returns an
374 empty string, then nothing is printed. */
ecec24e6 375static void
eb4c3f4a 376get_set_value (const char *args, int from_tty,
ecec24e6
PM
377 struct cmd_list_element *c)
378{
379 PyObject *obj = (PyObject *) get_cmd_context (c);
9b972014 380 gdb::unique_xmalloc_ptr<char> set_doc_string;
ecec24e6 381
2865bfce 382 gdbpy_enter enter_py (get_current_arch (), current_language);
7780f186 383 gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
ecec24e6 384
2865bfce 385 if (set_doc_func == NULL)
ecec24e6 386 {
2865bfce
TT
387 gdbpy_print_stack ();
388 return;
389 }
390
391 if (PyObject_HasAttr (obj, set_doc_func.get ()))
392 {
393 set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
ecec24e6 394 if (! set_doc_string)
ae778caf 395 gdbpy_handle_exception ();
ecec24e6 396 }
ecec24e6 397
984ee559
TT
398 const char *str = set_doc_string.get ();
399 if (str != nullptr && str[0] != '\0')
400 fprintf_filtered (gdb_stdout, "%s\n", str);
ecec24e6
PM
401}
402
403/* A callback function that is registered against the respective
404 add_setshow_* show_doc prototype. This function will either call
405 the Python function "get_show_string" or extract the Python
406 attribute "show_doc" and return the contents as a string. If
407 neither exist, insert a string indicating the Parameter is not
408 documented. */
409static void
410get_show_value (struct ui_file *file, int from_tty,
411 struct cmd_list_element *c,
412 const char *value)
413{
414 PyObject *obj = (PyObject *) get_cmd_context (c);
9b972014 415 gdb::unique_xmalloc_ptr<char> show_doc_string;
ecec24e6 416
2865bfce 417 gdbpy_enter enter_py (get_current_arch (), current_language);
7780f186 418 gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
2865bfce
TT
419
420 if (show_doc_func == NULL)
421 {
422 gdbpy_print_stack ();
423 return;
424 }
ecec24e6 425
2865bfce 426 if (PyObject_HasAttr (obj, show_doc_func.get ()))
ecec24e6 427 {
7780f186 428 gdbpy_ref<> val_obj (PyString_FromString (value));
ecec24e6 429
2865bfce
TT
430 if (val_obj == NULL)
431 {
432 gdbpy_print_stack ();
433 return;
434 }
ecec24e6 435
2865bfce
TT
436 show_doc_string = call_doc_function (obj, show_doc_func.get (),
437 val_obj.get ());
ecec24e6 438 if (! show_doc_string)
2865bfce
TT
439 {
440 gdbpy_print_stack ();
441 return;
442 }
ecec24e6 443
9b972014 444 fprintf_filtered (file, "%s\n", show_doc_string.get ());
ecec24e6
PM
445 }
446 else
447 {
448 /* We have to preserve the existing < GDB 7.3 API. If a
449 callback function does not exist, then attempt to read the
450 show_doc attribute. */
451 show_doc_string = get_doc_string (obj, show_doc_cst);
9b972014 452 fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
ecec24e6 453 }
ecec24e6 454}
d7b32ed3
PM
455\f
456
457/* A helper function that dispatches to the appropriate add_setshow
458 function. */
459static void
460add_setshow_generic (int parmclass, enum command_class cmdclass,
0d0b0ea2
TT
461 const char *cmd_name, parmpy_object *self,
462 const char *set_doc, const char *show_doc,
463 const char *help_doc,
d7b32ed3
PM
464 struct cmd_list_element **set_list,
465 struct cmd_list_element **show_list)
466{
ecec24e6 467 struct cmd_list_element *param = NULL;
6f937416 468 const char *tmp_name = NULL;
ecec24e6 469
d7b32ed3
PM
470 switch (parmclass)
471 {
472 case var_boolean:
ecec24e6
PM
473
474 add_setshow_boolean_cmd (cmd_name, cmdclass,
491144b5 475 &self->value.boolval, set_doc, show_doc,
ecec24e6
PM
476 help_doc, get_set_value, get_show_value,
477 set_list, show_list);
478
d7b32ed3
PM
479 break;
480
481 case var_auto_boolean:
482 add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
483 &self->value.autoboolval,
484 set_doc, show_doc, help_doc,
ecec24e6
PM
485 get_set_value, get_show_value,
486 set_list, show_list);
d7b32ed3
PM
487 break;
488
489 case var_uinteger:
ecec24e6
PM
490 add_setshow_uinteger_cmd (cmd_name, cmdclass,
491 &self->value.uintval, set_doc, show_doc,
492 help_doc, get_set_value, get_show_value,
493 set_list, show_list);
d7b32ed3
PM
494 break;
495
496 case var_integer:
ecec24e6
PM
497 add_setshow_integer_cmd (cmd_name, cmdclass,
498 &self->value.intval, set_doc, show_doc,
499 help_doc, get_set_value, get_show_value,
500 set_list, show_list); break;
d7b32ed3
PM
501
502 case var_string:
ecec24e6
PM
503 add_setshow_string_cmd (cmd_name, cmdclass,
504 &self->value.stringval, set_doc, show_doc,
505 help_doc, get_set_value, get_show_value,
506 set_list, show_list); break;
d7b32ed3
PM
507
508 case var_string_noescape:
509 add_setshow_string_noescape_cmd (cmd_name, cmdclass,
510 &self->value.stringval,
511 set_doc, show_doc, help_doc,
ecec24e6
PM
512 get_set_value, get_show_value,
513 set_list, show_list);
514
d7b32ed3
PM
515 break;
516
517 case var_optional_filename:
518 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
ecec24e6
PM
519 &self->value.stringval, set_doc,
520 show_doc, help_doc, get_set_value,
521 get_show_value, set_list,
522 show_list);
d7b32ed3
PM
523 break;
524
525 case var_filename:
ecec24e6
PM
526 add_setshow_filename_cmd (cmd_name, cmdclass,
527 &self->value.stringval, set_doc, show_doc,
528 help_doc, get_set_value, get_show_value,
529 set_list, show_list); break;
d7b32ed3
PM
530
531 case var_zinteger:
ecec24e6
PM
532 add_setshow_zinteger_cmd (cmd_name, cmdclass,
533 &self->value.intval, set_doc, show_doc,
534 help_doc, get_set_value, get_show_value,
535 set_list, show_list);
d7b32ed3
PM
536 break;
537
0489430a
TT
538 case var_zuinteger:
539 add_setshow_zuinteger_cmd (cmd_name, cmdclass,
540 &self->value.uintval, set_doc, show_doc,
541 help_doc, get_set_value, get_show_value,
542 set_list, show_list);
543 break;
544
545 case var_zuinteger_unlimited:
546 add_setshow_zuinteger_unlimited_cmd (cmd_name, cmdclass,
547 &self->value.intval, set_doc,
548 show_doc, help_doc, get_set_value,
549 get_show_value,
550 set_list, show_list);
551 break;
552
d7b32ed3
PM
553 case var_enum:
554 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
ecec24e6
PM
555 &self->value.cstringval, set_doc, show_doc,
556 help_doc, get_set_value, get_show_value,
557 set_list, show_list);
d7b32ed3
PM
558 /* Initialize the value, just in case. */
559 self->value.cstringval = self->enumeration[0];
560 break;
561 }
ecec24e6
PM
562
563 /* Lookup created parameter, and register Python object against the
564 parameter context. Perform this task against both lists. */
565 tmp_name = cmd_name;
cf00cd6f 566 param = lookup_cmd (&tmp_name, *show_list, "", NULL, 0, 1);
ecec24e6
PM
567 if (param)
568 set_cmd_context (param, self);
569
570 tmp_name = cmd_name;
cf00cd6f 571 param = lookup_cmd (&tmp_name, *set_list, "", NULL, 0, 1);
ecec24e6
PM
572 if (param)
573 set_cmd_context (param, self);
d7b32ed3
PM
574}
575
8dc78533
JK
576/* A helper which computes enum values. Returns 1 on success. Returns 0 on
577 error, with a python exception set. */
d7b32ed3
PM
578static int
579compute_enum_values (parmpy_object *self, PyObject *enum_values)
580{
581 Py_ssize_t size, i;
582
583 if (! enum_values)
584 {
585 PyErr_SetString (PyExc_RuntimeError,
586 _("An enumeration is required for PARAM_ENUM."));
587 return 0;
588 }
589
590 if (! PySequence_Check (enum_values))
591 {
256458bc 592 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
593 _("The enumeration is not a sequence."));
594 return 0;
595 }
596
597 size = PySequence_Size (enum_values);
598 if (size < 0)
599 return 0;
600 if (size == 0)
601 {
256458bc 602 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
603 _("The enumeration is empty."));
604 return 0;
605 }
606
1c034b67
TT
607 gdb_argv holder (XCNEWVEC (char *, size + 1));
608 char **enumeration = holder.get ();
d7b32ed3
PM
609
610 for (i = 0; i < size; ++i)
611 {
7780f186 612 gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
d59b6f6c 613
97d83487 614 if (item == NULL)
1c034b67 615 return 0;
97d83487 616 if (! gdbpy_is_string (item.get ()))
d7b32ed3 617 {
256458bc 618 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
619 _("The enumeration item not a string."));
620 return 0;
621 }
1c034b67
TT
622 enumeration[i] = python_string_to_host_string (item.get ()).release ();
623 if (enumeration[i] == NULL)
624 return 0;
d7b32ed3
PM
625 }
626
1c034b67 627 self->enumeration = const_cast<const char**> (holder.release ());
d7b32ed3
PM
628 return 1;
629}
630
d7b32ed3
PM
631/* Object initializer; sets up gdb-side structures for command.
632
633 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
634
635 NAME is the name of the parameter. It may consist of multiple
636 words, in which case the final word is the name of the new command,
637 and earlier words must be prefix commands.
638
639 CMDCLASS is the kind of command. It should be one of the COMMAND_*
640 constants defined in the gdb module.
641
642 PARMCLASS is the type of the parameter. It should be one of the
643 PARAM_* constants defined in the gdb module.
644
645 If PARMCLASS is PARAM_ENUM, then the final argument should be a
646 collection of strings. These strings are the valid values for this
647 parameter.
648
649 The documentation for the parameter is taken from the doc string
650 for the python class.
8dc78533
JK
651
652 Returns -1 on error, with a python exception set. */
653
d7b32ed3
PM
654static int
655parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
656{
657 parmpy_object *obj = (parmpy_object *) self;
ddd49eee 658 const char *name;
0d0b0ea2 659 gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
d7b32ed3
PM
660 char *cmd_name;
661 int parmclass, cmdtype;
662 PyObject *enum_values = NULL;
d7b32ed3 663 struct cmd_list_element **set_list, **show_list;
d7b32ed3
PM
664
665 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
666 &enum_values))
667 return -1;
668
669 if (cmdtype != no_class && cmdtype != class_run
670 && cmdtype != class_vars && cmdtype != class_stack
671 && cmdtype != class_files && cmdtype != class_support
672 && cmdtype != class_info && cmdtype != class_breakpoint
673 && cmdtype != class_trace && cmdtype != class_obscure
674 && cmdtype != class_maintenance)
675 {
676 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
677 return -1;
678 }
679
3c0ee1a4
PM
680 if (parmclass != var_boolean /* ARI: var_boolean */
681 && parmclass != var_auto_boolean
d7b32ed3
PM
682 && parmclass != var_uinteger && parmclass != var_integer
683 && parmclass != var_string && parmclass != var_string_noescape
684 && parmclass != var_optional_filename && parmclass != var_filename
0489430a
TT
685 && parmclass != var_zinteger && parmclass != var_zuinteger
686 && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
d7b32ed3 687 {
9a2b4c1b
MS
688 PyErr_SetString (PyExc_RuntimeError,
689 _("Invalid parameter class argument."));
d7b32ed3
PM
690 return -1;
691 }
692
693 if (enum_values && parmclass != var_enum)
694 {
695 PyErr_SetString (PyExc_RuntimeError,
696 _("Only PARAM_ENUM accepts a fourth argument."));
697 return -1;
698 }
699 if (parmclass == var_enum)
700 {
701 if (! compute_enum_values (obj, enum_values))
702 return -1;
703 }
704 else
705 obj->enumeration = NULL;
706 obj->type = (enum var_types) parmclass;
707 memset (&obj->value, 0, sizeof (obj->value));
708
63d97a20 709 cmd_name = gdbpy_parse_command_name (name, &set_list,
d7b32ed3
PM
710 &setlist);
711
712 if (! cmd_name)
63d97a20 713 return -1;
d7b32ed3 714 xfree (cmd_name);
63d97a20 715 cmd_name = gdbpy_parse_command_name (name, &show_list,
d7b32ed3
PM
716 &showlist);
717 if (! cmd_name)
718 return -1;
719
0d0b0ea2
TT
720 set_doc = get_doc_string (self, set_doc_cst);
721 show_doc = get_doc_string (self, show_doc_cst);
722 doc = get_doc_string (self, gdbpy_doc_cst);
d7b32ed3
PM
723
724 Py_INCREF (self);
725
a70b8144 726 try
d7b32ed3
PM
727 {
728 add_setshow_generic (parmclass, (enum command_class) cmdtype,
729 cmd_name, obj,
0d0b0ea2
TT
730 set_doc.get (), show_doc.get (),
731 doc.get (), set_list, show_list);
d7b32ed3 732 }
230d2906 733 catch (const gdb_exception &except)
d7b32ed3
PM
734 {
735 xfree (cmd_name);
d7b32ed3 736 Py_DECREF (self);
ec9c2750 737 gdbpy_convert_exception (except);
d7b32ed3
PM
738 return -1;
739 }
492d29ea 740
d7b32ed3
PM
741 return 0;
742}
743
744\f
745
746/* Initialize the 'parameters' module. */
999633ed 747int
d7b32ed3
PM
748gdbpy_initialize_parameters (void)
749{
750 int i;
751
6a1b1664 752 parmpy_object_type.tp_new = PyType_GenericNew;
d7b32ed3 753 if (PyType_Ready (&parmpy_object_type) < 0)
999633ed 754 return -1;
d7b32ed3
PM
755
756 set_doc_cst = PyString_FromString ("set_doc");
757 if (! set_doc_cst)
999633ed 758 return -1;
d7b32ed3
PM
759 show_doc_cst = PyString_FromString ("show_doc");
760 if (! show_doc_cst)
999633ed 761 return -1;
d7b32ed3
PM
762
763 for (i = 0; parm_constants[i].name; ++i)
764 {
765 if (PyModule_AddIntConstant (gdb_module,
766 parm_constants[i].name,
767 parm_constants[i].value) < 0)
999633ed 768 return -1;
d7b32ed3
PM
769 }
770
aa36459a
TT
771 return gdb_pymodule_addobject (gdb_module, "Parameter",
772 (PyObject *) &parmpy_object_type);
d7b32ed3
PM
773}
774
775\f
776
e36122e9 777PyTypeObject parmpy_object_type =
d7b32ed3 778{
9a27f2c6 779 PyVarObject_HEAD_INIT (NULL, 0)
d7b32ed3
PM
780 "gdb.Parameter", /*tp_name*/
781 sizeof (parmpy_object), /*tp_basicsize*/
782 0, /*tp_itemsize*/
783 0, /*tp_dealloc*/
784 0, /*tp_print*/
785 0, /*tp_getattr*/
786 0, /*tp_setattr*/
787 0, /*tp_compare*/
788 0, /*tp_repr*/
789 0, /*tp_as_number*/
790 0, /*tp_as_sequence*/
791 0, /*tp_as_mapping*/
792 0, /*tp_hash */
793 0, /*tp_call*/
794 0, /*tp_str*/
795 get_attr, /*tp_getattro*/
796 set_attr, /*tp_setattro*/
797 0, /*tp_as_buffer*/
798 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
799 "GDB parameter object", /* tp_doc */
800 0, /* tp_traverse */
801 0, /* tp_clear */
802 0, /* tp_richcompare */
803 0, /* tp_weaklistoffset */
804 0, /* tp_iter */
805 0, /* tp_iternext */
806 0, /* tp_methods */
807 0, /* tp_members */
808 0, /* tp_getset */
809 0, /* tp_base */
810 0, /* tp_dict */
811 0, /* tp_descr_get */
812 0, /* tp_descr_set */
813 0, /* tp_dictoffset */
814 parmpy_init, /* tp_init */
815 0, /* tp_alloc */
d7b32ed3 816};