Replace use of magic number with named constant.
[deliverable/binutils-gdb.git] / gdb / python / py-cmd.c
1 /* gdb commands implemented in Python
2
3 Copyright (C) 2008-2014 Free Software Foundation, Inc.
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 "arch-utils.h"
23 #include "value.h"
24 #include "exceptions.h"
25 #include "python-internal.h"
26 #include "charset.h"
27 #include "gdbcmd.h"
28 #include "cli/cli-decode.h"
29 #include "completer.h"
30 #include "language.h"
31
32 /* Struct representing built-in completion types. */
33 struct cmdpy_completer
34 {
35 /* Python symbol name.
36 This isn't a const char * for Python 2.4's sake.
37 PyModule_AddIntConstant only takes a char *, sigh. */
38 char *name;
39 /* Completion function. */
40 completer_ftype *completer;
41 };
42
43 static const struct cmdpy_completer completers[] =
44 {
45 { "COMPLETE_NONE", noop_completer },
46 { "COMPLETE_FILENAME", filename_completer },
47 { "COMPLETE_LOCATION", location_completer },
48 { "COMPLETE_COMMAND", command_completer },
49 { "COMPLETE_SYMBOL", make_symbol_completion_list_fn },
50 { "COMPLETE_EXPRESSION", expression_completer },
51 };
52
53 #define N_COMPLETERS (sizeof (completers) / sizeof (completers[0]))
54
55 /* A gdb command. For the time being only ordinary commands (not
56 set/show commands) are allowed. */
57 struct cmdpy_object
58 {
59 PyObject_HEAD
60
61 /* The corresponding gdb command object, or NULL if the command is
62 no longer installed. */
63 struct cmd_list_element *command;
64
65 /* A prefix command requires storage for a list of its sub-commands.
66 A pointer to this is passed to add_prefix_command, and to add_cmd
67 for sub-commands of that prefix. If this Command is not a prefix
68 command, then this field is unused. */
69 struct cmd_list_element *sub_list;
70 };
71
72 typedef struct cmdpy_object cmdpy_object;
73
74 static PyTypeObject cmdpy_object_type
75 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("cmdpy_object");
76
77 /* Constants used by this module. */
78 static PyObject *invoke_cst;
79 static PyObject *complete_cst;
80
81 \f
82
83 /* Python function which wraps dont_repeat. */
84 static PyObject *
85 cmdpy_dont_repeat (PyObject *self, PyObject *args)
86 {
87 dont_repeat ();
88 Py_RETURN_NONE;
89 }
90
91 \f
92
93 /* Called if the gdb cmd_list_element is destroyed. */
94
95 static void
96 cmdpy_destroyer (struct cmd_list_element *self, void *context)
97 {
98 cmdpy_object *cmd;
99 struct cleanup *cleanup;
100
101 cleanup = ensure_python_env (get_current_arch (), current_language);
102
103 /* Release our hold on the command object. */
104 cmd = (cmdpy_object *) context;
105 cmd->command = NULL;
106 Py_DECREF (cmd);
107
108 /* We allocated the name, doc string, and perhaps the prefix
109 name. */
110 xfree ((char *) self->name);
111 xfree ((char *) self->doc);
112 xfree ((char *) self->prefixname);
113
114 do_cleanups (cleanup);
115 }
116
117 /* Called by gdb to invoke the command. */
118
119 static void
120 cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
121 {
122 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
123 PyObject *argobj, *ttyobj, *result;
124 struct cleanup *cleanup;
125
126 cleanup = ensure_python_env (get_current_arch (), current_language);
127
128 if (! obj)
129 error (_("Invalid invocation of Python command object."));
130 if (! PyObject_HasAttr ((PyObject *) obj, invoke_cst))
131 {
132 if (obj->command->prefixname)
133 {
134 /* A prefix command does not need an invoke method. */
135 do_cleanups (cleanup);
136 return;
137 }
138 error (_("Python command object missing 'invoke' method."));
139 }
140
141 if (! args)
142 args = "";
143 argobj = PyUnicode_Decode (args, strlen (args), host_charset (), NULL);
144 if (! argobj)
145 {
146 gdbpy_print_stack ();
147 error (_("Could not convert arguments to Python string."));
148 }
149
150 ttyobj = from_tty ? Py_True : Py_False;
151 Py_INCREF (ttyobj);
152 result = PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst, argobj,
153 ttyobj, NULL);
154 Py_DECREF (argobj);
155 Py_DECREF (ttyobj);
156
157 if (! result)
158 {
159 PyObject *ptype, *pvalue, *ptraceback;
160 char *msg;
161
162 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
163
164 /* Try to fetch an error message contained within ptype, pvalue.
165 When fetching the error message we need to make our own copy,
166 we no longer own ptype, pvalue after the call to PyErr_Restore. */
167
168 msg = gdbpy_exception_to_string (ptype, pvalue);
169 make_cleanup (xfree, msg);
170
171 if (msg == NULL)
172 {
173 /* An error occurred computing the string representation of the
174 error message. This is rare, but we should inform the user. */
175 printf_filtered (_("An error occurred in a Python command\n"
176 "and then another occurred computing the "
177 "error message.\n"));
178 gdbpy_print_stack ();
179 }
180
181 /* Don't print the stack for gdb.GdbError exceptions.
182 It is generally used to flag user errors.
183
184 We also don't want to print "Error occurred in Python command"
185 for user errors. However, a missing message for gdb.GdbError
186 exceptions is arguably a bug, so we flag it as such. */
187
188 if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
189 || msg == NULL || *msg == '\0')
190 {
191 PyErr_Restore (ptype, pvalue, ptraceback);
192 gdbpy_print_stack ();
193 if (msg != NULL && *msg != '\0')
194 error (_("Error occurred in Python command: %s"), msg);
195 else
196 error (_("Error occurred in Python command."));
197 }
198 else
199 {
200 Py_XDECREF (ptype);
201 Py_XDECREF (pvalue);
202 Py_XDECREF (ptraceback);
203 error ("%s", msg);
204 }
205 }
206
207 Py_DECREF (result);
208 do_cleanups (cleanup);
209 }
210
211 /* Helper function for the Python command completers (both "pure"
212 completer and brkchar handler). This function takes COMMAND, TEXT
213 and WORD and tries to call the Python method for completion with
214 these arguments. It also takes HANDLE_BRKCHARS_P, an argument to
215 identify whether it is being called from the brkchar handler or
216 from the "pure" completer. In the first case, it effectively calls
217 the Python method for completion, and records the PyObject in a
218 static variable (used as a "cache"). In the second case, it just
219 returns that variable, without actually calling the Python method
220 again. This saves us one Python method call.
221
222 The reason for this two step dance is that we need to know the set
223 of "brkchars" to use early on, before we actually try to perform
224 the completion. But if a Python command supplies a "complete"
225 method then we have to call that method first: it may return as its
226 result the kind of completion to perform and that will in turn
227 specify which brkchars to use. IOW, we need the result of the
228 "complete" method before we actually perform the completion.
229
230 It is important to mention that this function is built on the
231 assumption that the calls to cmdpy_completer_handle_brkchars and
232 cmdpy_completer will be subsequent with nothing intervening. This
233 is true for our completer mechanism.
234
235 This function returns the PyObject representing the Python method
236 call. */
237
238 static PyObject *
239 cmdpy_completer_helper (struct cmd_list_element *command,
240 const char *text, const char *word,
241 int handle_brkchars_p)
242 {
243 cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
244 PyObject *textobj, *wordobj;
245 /* This static variable will server as a "cache" for us, in order to
246 store the PyObject that results from calling the Python
247 function. */
248 static PyObject *resultobj = NULL;
249
250 if (handle_brkchars_p)
251 {
252 /* If we were called to handle brkchars, it means this is the
253 first function call of two that will happen in a row.
254 Therefore, we need to call the completer ourselves, and cache
255 the return value in the static variable RESULTOBJ. Then, in
256 the second call, we can just use the value of RESULTOBJ to do
257 our job. */
258 if (resultobj != NULL)
259 Py_DECREF (resultobj);
260
261 resultobj = NULL;
262 if (obj == NULL)
263 error (_("Invalid invocation of Python command object."));
264 if (!PyObject_HasAttr ((PyObject *) obj, complete_cst))
265 {
266 /* If there is no complete method, don't error. */
267 return NULL;
268 }
269
270 textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
271 if (textobj == NULL)
272 error (_("Could not convert argument to Python string."));
273 wordobj = PyUnicode_Decode (word, sizeof (word), host_charset (), NULL);
274 if (wordobj == NULL)
275 {
276 Py_DECREF (textobj);
277 error (_("Could not convert argument to Python string."));
278 }
279
280 resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
281 textobj, wordobj, NULL);
282 Py_DECREF (textobj);
283 Py_DECREF (wordobj);
284 if (!resultobj)
285 {
286 /* Just swallow errors here. */
287 PyErr_Clear ();
288 }
289
290 Py_XINCREF (resultobj);
291 }
292
293 return resultobj;
294 }
295
296 /* Python function called to determine the break characters of a
297 certain completer. We are only interested in knowing if the
298 completer registered by the user will return one of the integer
299 codes (see COMPLETER_* symbols). */
300
301 static void
302 cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
303 const char *text, const char *word)
304 {
305 PyObject *resultobj = NULL;
306 struct cleanup *cleanup;
307
308 cleanup = ensure_python_env (get_current_arch (), current_language);
309
310 /* Calling our helper to obtain the PyObject of the Python
311 function. */
312 resultobj = cmdpy_completer_helper (command, text, word, 1);
313
314 /* Check if there was an error. */
315 if (resultobj == NULL)
316 goto done;
317
318 if (PyInt_Check (resultobj))
319 {
320 /* User code may also return one of the completion constants,
321 thus requesting that sort of completion. We are only
322 interested in this kind of return. */
323 long value;
324
325 if (!gdb_py_int_as_long (resultobj, &value))
326 {
327 /* Ignore. */
328 PyErr_Clear ();
329 }
330 else if (value >= 0 && value < (long) N_COMPLETERS)
331 {
332 /* This is the core of this function. Depending on which
333 completer type the Python function returns, we have to
334 adjust the break characters accordingly. */
335 set_gdb_completion_word_break_characters
336 (completers[value].completer);
337 }
338 }
339
340 done:
341
342 /* We do not call Py_XDECREF here because RESULTOBJ will be used in
343 the subsequent call to cmdpy_completer function. */
344 do_cleanups (cleanup);
345 }
346
347 /* Called by gdb for command completion. */
348
349 static VEC (char_ptr) *
350 cmdpy_completer (struct cmd_list_element *command,
351 const char *text, const char *word)
352 {
353 PyObject *resultobj = NULL;
354 VEC (char_ptr) *result = NULL;
355 struct cleanup *cleanup;
356
357 cleanup = ensure_python_env (get_current_arch (), current_language);
358
359 /* Calling our helper to obtain the PyObject of the Python
360 function. */
361 resultobj = cmdpy_completer_helper (command, text, word, 0);
362
363 /* If the result object of calling the Python function is NULL, it
364 means that there was an error. In this case, just give up and
365 return NULL. */
366 if (resultobj == NULL)
367 goto done;
368
369 result = NULL;
370 if (PyInt_Check (resultobj))
371 {
372 /* User code may also return one of the completion constants,
373 thus requesting that sort of completion. */
374 long value;
375
376 if (! gdb_py_int_as_long (resultobj, &value))
377 {
378 /* Ignore. */
379 PyErr_Clear ();
380 }
381 else if (value >= 0 && value < (long) N_COMPLETERS)
382 result = completers[value].completer (command, text, word);
383 }
384 else
385 {
386 PyObject *iter = PyObject_GetIter (resultobj);
387 PyObject *elt;
388
389 if (iter == NULL)
390 goto done;
391
392 while ((elt = PyIter_Next (iter)) != NULL)
393 {
394 char *item;
395
396 if (! gdbpy_is_string (elt))
397 {
398 /* Skip problem elements. */
399 Py_DECREF (elt);
400 continue;
401 }
402 item = python_string_to_host_string (elt);
403 Py_DECREF (elt);
404 if (item == NULL)
405 {
406 /* Skip problem elements. */
407 PyErr_Clear ();
408 continue;
409 }
410 VEC_safe_push (char_ptr, result, item);
411 }
412
413 Py_DECREF (iter);
414
415 /* If we got some results, ignore problems. Otherwise, report
416 the problem. */
417 if (result != NULL && PyErr_Occurred ())
418 PyErr_Clear ();
419 }
420
421 done:
422
423 do_cleanups (cleanup);
424
425 return result;
426 }
427
428 /* Helper for cmdpy_init which locates the command list to use and
429 pulls out the command name.
430
431 NAME is the command name list. The final word in the list is the
432 name of the new command. All earlier words must be existing prefix
433 commands.
434
435 *BASE_LIST is set to the final prefix command's list of
436 *sub-commands.
437
438 START_LIST is the list in which the search starts.
439
440 This function returns the xmalloc()d name of the new command. On
441 error sets the Python error and returns NULL. */
442
443 char *
444 gdbpy_parse_command_name (const char *name,
445 struct cmd_list_element ***base_list,
446 struct cmd_list_element **start_list)
447 {
448 struct cmd_list_element *elt;
449 int len = strlen (name);
450 int i, lastchar;
451 char *prefix_text;
452 const char *prefix_text2;
453 char *result;
454
455 /* Skip trailing whitespace. */
456 for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
457 ;
458 if (i < 0)
459 {
460 PyErr_SetString (PyExc_RuntimeError, _("No command name found."));
461 return NULL;
462 }
463 lastchar = i;
464
465 /* Find first character of the final word. */
466 for (; i > 0 && (isalnum (name[i - 1])
467 || name[i - 1] == '-'
468 || name[i - 1] == '_');
469 --i)
470 ;
471 result = xmalloc (lastchar - i + 2);
472 memcpy (result, &name[i], lastchar - i + 1);
473 result[lastchar - i + 1] = '\0';
474
475 /* Skip whitespace again. */
476 for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
477 ;
478 if (i < 0)
479 {
480 *base_list = start_list;
481 return result;
482 }
483
484 prefix_text = xmalloc (i + 2);
485 memcpy (prefix_text, name, i + 1);
486 prefix_text[i + 1] = '\0';
487
488 prefix_text2 = prefix_text;
489 elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
490 if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
491 {
492 PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
493 prefix_text);
494 xfree (prefix_text);
495 xfree (result);
496 return NULL;
497 }
498
499 if (elt->prefixlist)
500 {
501 xfree (prefix_text);
502 *base_list = elt->prefixlist;
503 return result;
504 }
505
506 PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
507 prefix_text);
508 xfree (prefix_text);
509 xfree (result);
510 return NULL;
511 }
512
513 /* Object initializer; sets up gdb-side structures for command.
514
515 Use: __init__(NAME, COMMAND_CLASS [, COMPLETER_CLASS][, PREFIX]]).
516
517 NAME is the name of the command. It may consist of multiple words,
518 in which case the final word is the name of the new command, and
519 earlier words must be prefix commands.
520
521 COMMAND_CLASS is the kind of command. It should be one of the COMMAND_*
522 constants defined in the gdb module.
523
524 COMPLETER_CLASS is the kind of completer. If not given, the
525 "complete" method will be used. Otherwise, it should be one of the
526 COMPLETE_* constants defined in the gdb module.
527
528 If PREFIX is True, then this command is a prefix command.
529
530 The documentation for the command is taken from the doc string for
531 the python class. */
532
533 static int
534 cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
535 {
536 cmdpy_object *obj = (cmdpy_object *) self;
537 const char *name;
538 int cmdtype;
539 int completetype = -1;
540 char *docstring = NULL;
541 volatile struct gdb_exception except;
542 struct cmd_list_element **cmd_list;
543 char *cmd_name, *pfx_name;
544 static char *keywords[] = { "name", "command_class", "completer_class",
545 "prefix", NULL };
546 PyObject *is_prefix = NULL;
547 int cmp;
548
549 if (obj->command)
550 {
551 /* Note: this is apparently not documented in Python. We return
552 0 for success, -1 for failure. */
553 PyErr_Format (PyExc_RuntimeError,
554 _("Command object already initialized."));
555 return -1;
556 }
557
558 if (! PyArg_ParseTupleAndKeywords (args, kw, "si|iO",
559 keywords, &name, &cmdtype,
560 &completetype, &is_prefix))
561 return -1;
562
563 if (cmdtype != no_class && cmdtype != class_run
564 && cmdtype != class_vars && cmdtype != class_stack
565 && cmdtype != class_files && cmdtype != class_support
566 && cmdtype != class_info && cmdtype != class_breakpoint
567 && cmdtype != class_trace && cmdtype != class_obscure
568 && cmdtype != class_maintenance && cmdtype != class_user)
569 {
570 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
571 return -1;
572 }
573
574 if (completetype < -1 || completetype >= (int) N_COMPLETERS)
575 {
576 PyErr_Format (PyExc_RuntimeError,
577 _("Invalid completion type argument."));
578 return -1;
579 }
580
581 cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
582 if (! cmd_name)
583 return -1;
584
585 pfx_name = NULL;
586 if (is_prefix != NULL)
587 {
588 cmp = PyObject_IsTrue (is_prefix);
589 if (cmp == 1)
590 {
591 int i, out;
592
593 /* Make a normalized form of the command name. */
594 pfx_name = xmalloc (strlen (name) + 2);
595
596 i = 0;
597 out = 0;
598 while (name[i])
599 {
600 /* Skip whitespace. */
601 while (name[i] == ' ' || name[i] == '\t')
602 ++i;
603 /* Copy non-whitespace characters. */
604 while (name[i] && name[i] != ' ' && name[i] != '\t')
605 pfx_name[out++] = name[i++];
606 /* Add a single space after each word -- including the final
607 word. */
608 pfx_name[out++] = ' ';
609 }
610 pfx_name[out] = '\0';
611 }
612 else if (cmp < 0)
613 {
614 xfree (cmd_name);
615 return -1;
616 }
617 }
618 if (PyObject_HasAttr (self, gdbpy_doc_cst))
619 {
620 PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
621
622 if (ds_obj && gdbpy_is_string (ds_obj))
623 {
624 docstring = python_string_to_host_string (ds_obj);
625 if (docstring == NULL)
626 {
627 xfree (cmd_name);
628 xfree (pfx_name);
629 Py_DECREF (ds_obj);
630 return -1;
631 }
632 }
633
634 Py_XDECREF (ds_obj);
635 }
636 if (! docstring)
637 docstring = xstrdup (_("This command is not documented."));
638
639 Py_INCREF (self);
640
641 TRY_CATCH (except, RETURN_MASK_ALL)
642 {
643 struct cmd_list_element *cmd;
644
645 if (pfx_name)
646 {
647 int allow_unknown;
648
649 /* If we have our own "invoke" method, then allow unknown
650 sub-commands. */
651 allow_unknown = PyObject_HasAttr (self, invoke_cst);
652 cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
653 NULL, docstring, &obj->sub_list,
654 pfx_name, allow_unknown, cmd_list);
655 }
656 else
657 cmd = add_cmd (cmd_name, (enum command_class) cmdtype, NULL,
658 docstring, cmd_list);
659
660 /* There appears to be no API to set this. */
661 cmd->func = cmdpy_function;
662 cmd->destroyer = cmdpy_destroyer;
663
664 obj->command = cmd;
665 set_cmd_context (cmd, self);
666 set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
667 : completers[completetype].completer));
668 if (completetype == -1)
669 set_cmd_completer_handle_brkchars (cmd,
670 cmdpy_completer_handle_brkchars);
671 }
672 if (except.reason < 0)
673 {
674 xfree (cmd_name);
675 xfree (docstring);
676 xfree (pfx_name);
677 Py_DECREF (self);
678 PyErr_Format (except.reason == RETURN_QUIT
679 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
680 "%s", except.message);
681 return -1;
682 }
683 return 0;
684 }
685
686 \f
687
688 /* Initialize the 'commands' code. */
689
690 int
691 gdbpy_initialize_commands (void)
692 {
693 int i;
694
695 cmdpy_object_type.tp_new = PyType_GenericNew;
696 if (PyType_Ready (&cmdpy_object_type) < 0)
697 return -1;
698
699 /* Note: alias and user are special; pseudo appears to be unused,
700 and there is no reason to expose tui or xdb, I think. */
701 if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
702 || PyModule_AddIntConstant (gdb_module, "COMMAND_RUNNING", class_run) < 0
703 || PyModule_AddIntConstant (gdb_module, "COMMAND_DATA", class_vars) < 0
704 || PyModule_AddIntConstant (gdb_module, "COMMAND_STACK", class_stack) < 0
705 || PyModule_AddIntConstant (gdb_module, "COMMAND_FILES", class_files) < 0
706 || PyModule_AddIntConstant (gdb_module, "COMMAND_SUPPORT",
707 class_support) < 0
708 || PyModule_AddIntConstant (gdb_module, "COMMAND_STATUS", class_info) < 0
709 || PyModule_AddIntConstant (gdb_module, "COMMAND_BREAKPOINTS",
710 class_breakpoint) < 0
711 || PyModule_AddIntConstant (gdb_module, "COMMAND_TRACEPOINTS",
712 class_trace) < 0
713 || PyModule_AddIntConstant (gdb_module, "COMMAND_OBSCURE",
714 class_obscure) < 0
715 || PyModule_AddIntConstant (gdb_module, "COMMAND_MAINTENANCE",
716 class_maintenance) < 0
717 || PyModule_AddIntConstant (gdb_module, "COMMAND_USER", class_user) < 0)
718 return -1;
719
720 for (i = 0; i < N_COMPLETERS; ++i)
721 {
722 if (PyModule_AddIntConstant (gdb_module, completers[i].name, i) < 0)
723 return -1;
724 }
725
726 if (gdb_pymodule_addobject (gdb_module, "Command",
727 (PyObject *) &cmdpy_object_type) < 0)
728 return -1;
729
730 invoke_cst = PyString_FromString ("invoke");
731 if (invoke_cst == NULL)
732 return -1;
733 complete_cst = PyString_FromString ("complete");
734 if (complete_cst == NULL)
735 return -1;
736
737 return 0;
738 }
739
740 \f
741
742 static PyMethodDef cmdpy_object_methods[] =
743 {
744 { "dont_repeat", cmdpy_dont_repeat, METH_NOARGS,
745 "Prevent command repetition when user enters empty line." },
746
747 { 0 }
748 };
749
750 static PyTypeObject cmdpy_object_type =
751 {
752 PyVarObject_HEAD_INIT (NULL, 0)
753 "gdb.Command", /*tp_name*/
754 sizeof (cmdpy_object), /*tp_basicsize*/
755 0, /*tp_itemsize*/
756 0, /*tp_dealloc*/
757 0, /*tp_print*/
758 0, /*tp_getattr*/
759 0, /*tp_setattr*/
760 0, /*tp_compare*/
761 0, /*tp_repr*/
762 0, /*tp_as_number*/
763 0, /*tp_as_sequence*/
764 0, /*tp_as_mapping*/
765 0, /*tp_hash */
766 0, /*tp_call*/
767 0, /*tp_str*/
768 0, /*tp_getattro*/
769 0, /*tp_setattro*/
770 0, /*tp_as_buffer*/
771 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
772 "GDB command object", /* tp_doc */
773 0, /* tp_traverse */
774 0, /* tp_clear */
775 0, /* tp_richcompare */
776 0, /* tp_weaklistoffset */
777 0, /* tp_iter */
778 0, /* tp_iternext */
779 cmdpy_object_methods, /* tp_methods */
780 0, /* tp_members */
781 0, /* tp_getset */
782 0, /* tp_base */
783 0, /* tp_dict */
784 0, /* tp_descr_get */
785 0, /* tp_descr_set */
786 0, /* tp_dictoffset */
787 cmdpy_init, /* tp_init */
788 0, /* tp_alloc */
789 };
790
791 \f
792
793 /* Utility to build a buildargv-like result from ARGS.
794 This intentionally parses arguments the way libiberty/argv.c:buildargv
795 does. It splits up arguments in a reasonable way, and we want a standard
796 way of parsing arguments. Several gdb commands use buildargv to parse their
797 arguments. Plus we want to be able to write compatible python
798 implementations of gdb commands. */
799
800 PyObject *
801 gdbpy_string_to_argv (PyObject *self, PyObject *args)
802 {
803 PyObject *py_argv;
804 const char *input;
805
806 if (!PyArg_ParseTuple (args, "s", &input))
807 return NULL;
808
809 py_argv = PyList_New (0);
810 if (py_argv == NULL)
811 return NULL;
812
813 /* buildargv uses NULL to represent an empty argument list, but we can't use
814 that in Python. Instead, if ARGS is "" then return an empty list.
815 This undoes the NULL -> "" conversion that cmdpy_function does. */
816
817 if (*input != '\0')
818 {
819 char **c_argv = gdb_buildargv (input);
820 int i;
821
822 for (i = 0; c_argv[i] != NULL; ++i)
823 {
824 PyObject *argp = PyString_FromString (c_argv[i]);
825
826 if (argp == NULL
827 || PyList_Append (py_argv, argp) < 0)
828 {
829 Py_XDECREF (argp);
830 Py_DECREF (py_argv);
831 freeargv (c_argv);
832 return NULL;
833 }
834 Py_DECREF (argp);
835 }
836
837 freeargv (c_argv);
838 }
839
840 return py_argv;
841 }
This page took 0.063556 seconds and 4 git commands to generate.