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