More updated translations
[binutils-gdb.git] / gdb / guile / scm-cmd.c
blob8255529a2fe83413cb96f9db55248324c897cd50
1 /* GDB commands implemented in Scheme.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
23 #include <ctype.h>
24 #include "charset.h"
25 #include "cli/cli-cmds.h"
26 #include "cli/cli-decode.h"
27 #include "completer.h"
28 #include "guile-internal.h"
30 /* The <gdb:command> smob.
32 Note: Commands are added to gdb using a two step process:
33 1) Call make-command to create a <gdb:command> object.
34 2) Call register-command! to add the command to gdb.
35 It is done this way so that the constructor, make-command, doesn't have
36 any side-effects. This means that the smob needs to store everything
37 that was passed to make-command. */
39 struct command_smob
41 /* This always appears first. */
42 gdb_smob base;
44 /* The name of the command, as passed to make-command. */
45 char *name;
47 /* The last word of the command.
48 This is needed because add_cmd requires us to allocate space
49 for it. :-( */
50 char *cmd_name;
52 /* Non-zero if this is a prefix command. */
53 int is_prefix;
55 /* One of the COMMAND_* constants. */
56 enum command_class cmd_class;
58 /* The documentation for the command. */
59 char *doc;
61 /* The corresponding gdb command object.
62 This is NULL if the command has not been registered yet, or
63 is no longer registered. */
64 struct cmd_list_element *command;
66 /* A prefix command requires storage for a list of its sub-commands.
67 A pointer to this is passed to add_prefix_command, and to add_cmd
68 for sub-commands of that prefix.
69 This is NULL if the command has not been registered yet, or
70 is no longer registered. If this command is not a prefix
71 command, then this field is unused. */
72 struct cmd_list_element *sub_list;
74 /* The procedure to call to invoke the command.
75 (lambda (self arg from-tty) ...).
76 Its result is unspecified. */
77 SCM invoke;
79 /* Either #f, one of the COMPLETE_* constants, or a procedure to call to
80 perform command completion. Called as (lambda (self text word) ...). */
81 SCM complete;
83 /* The <gdb:command> object we are contained in, needed to protect/unprotect
84 the object since a reference to it comes from non-gc-managed space
85 (the command context pointer). */
86 SCM containing_scm;
89 static const char command_smob_name[] = "gdb:command";
91 /* The tag Guile knows the objfile smob by. */
92 static scm_t_bits command_smob_tag;
94 /* Keywords used by make-command. */
95 static SCM invoke_keyword;
96 static SCM command_class_keyword;
97 static SCM completer_class_keyword;
98 static SCM prefix_p_keyword;
99 static SCM doc_keyword;
101 /* Struct representing built-in completion types. */
102 struct cmdscm_completer
104 /* Scheme symbol name. */
105 const char *name;
106 /* Completion function. */
107 completer_ftype *completer;
110 static const struct cmdscm_completer cmdscm_completers[] =
112 { "COMPLETE_NONE", noop_completer },
113 { "COMPLETE_FILENAME", filename_maybe_quoted_completer },
114 { "COMPLETE_LOCATION", location_completer },
115 { "COMPLETE_COMMAND", command_completer },
116 { "COMPLETE_SYMBOL", symbol_completer },
117 { "COMPLETE_EXPRESSION", expression_completer },
120 #define N_COMPLETERS (sizeof (cmdscm_completers) \
121 / sizeof (cmdscm_completers[0]))
123 static int cmdscm_is_valid (command_smob *);
125 /* Administrivia for command smobs. */
127 /* The smob "print" function for <gdb:command>. */
129 static int
130 cmdscm_print_command_smob (SCM self, SCM port, scm_print_state *pstate)
132 command_smob *c_smob = (command_smob *) SCM_SMOB_DATA (self);
134 gdbscm_printf (port, "#<%s", command_smob_name);
136 gdbscm_printf (port, " %s",
137 c_smob->name != NULL ? c_smob->name : "{unnamed}");
139 if (! cmdscm_is_valid (c_smob))
140 scm_puts (" {invalid}", port);
142 scm_puts (">", port);
144 scm_remember_upto_here_1 (self);
146 /* Non-zero means success. */
147 return 1;
150 /* Low level routine to create a <gdb:command> object.
151 It's empty in the sense that a command still needs to be associated
152 with it. */
154 static SCM
155 cmdscm_make_command_smob (void)
157 command_smob *c_smob = (command_smob *)
158 scm_gc_malloc (sizeof (command_smob), command_smob_name);
159 SCM c_scm;
161 memset (c_smob, 0, sizeof (*c_smob));
162 c_smob->cmd_class = no_class;
163 c_smob->invoke = SCM_BOOL_F;
164 c_smob->complete = SCM_BOOL_F;
165 c_scm = scm_new_smob (command_smob_tag, (scm_t_bits) c_smob);
166 c_smob->containing_scm = c_scm;
167 gdbscm_init_gsmob (&c_smob->base);
169 return c_scm;
172 /* Clear the COMMAND pointer in C_SMOB and unprotect the object from GC. */
174 static void
175 cmdscm_release_command (command_smob *c_smob)
177 c_smob->command = NULL;
178 scm_gc_unprotect_object (c_smob->containing_scm);
181 /* Return non-zero if SCM is a command smob. */
183 static int
184 cmdscm_is_command (SCM scm)
186 return SCM_SMOB_PREDICATE (command_smob_tag, scm);
189 /* (command? scm) -> boolean */
191 static SCM
192 gdbscm_command_p (SCM scm)
194 return scm_from_bool (cmdscm_is_command (scm));
197 /* Returns the <gdb:command> object in SELF.
198 Throws an exception if SELF is not a <gdb:command> object. */
200 static SCM
201 cmdscm_get_command_arg_unsafe (SCM self, int arg_pos, const char *func_name)
203 SCM_ASSERT_TYPE (cmdscm_is_command (self), self, arg_pos, func_name,
204 command_smob_name);
206 return self;
209 /* Returns a pointer to the command smob of SELF.
210 Throws an exception if SELF is not a <gdb:command> object. */
212 static command_smob *
213 cmdscm_get_command_smob_arg_unsafe (SCM self, int arg_pos,
214 const char *func_name)
216 SCM c_scm = cmdscm_get_command_arg_unsafe (self, arg_pos, func_name);
217 command_smob *c_smob = (command_smob *) SCM_SMOB_DATA (c_scm);
219 return c_smob;
222 /* Return non-zero if command C_SMOB is valid. */
224 static int
225 cmdscm_is_valid (command_smob *c_smob)
227 return c_smob->command != NULL;
230 /* Returns a pointer to the command smob of SELF.
231 Throws an exception if SELF is not a valid <gdb:command> object. */
233 static command_smob *
234 cmdscm_get_valid_command_smob_arg_unsafe (SCM self, int arg_pos,
235 const char *func_name)
237 command_smob *c_smob
238 = cmdscm_get_command_smob_arg_unsafe (self, arg_pos, func_name);
240 if (!cmdscm_is_valid (c_smob))
242 gdbscm_invalid_object_error (func_name, arg_pos, self,
243 _("<gdb:command>"));
246 return c_smob;
249 /* Scheme functions for GDB commands. */
251 /* (command-valid? <gdb:command>) -> boolean
252 Returns #t if SELF is still valid. */
254 static SCM
255 gdbscm_command_valid_p (SCM self)
257 command_smob *c_smob
258 = cmdscm_get_command_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
260 return scm_from_bool (cmdscm_is_valid (c_smob));
263 /* (dont-repeat cmd) -> unspecified
264 Scheme function which wraps dont_repeat. */
266 static SCM
267 gdbscm_dont_repeat (SCM self)
269 /* We currently don't need anything from SELF, but still verify it.
270 Call for side effects. */
271 cmdscm_get_valid_command_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
273 dont_repeat ();
275 return SCM_UNSPECIFIED;
278 /* The make-command function. */
280 /* Called if the gdb cmd_list_element is destroyed. */
282 static void
283 cmdscm_destroyer (struct cmd_list_element *self, void *context)
285 command_smob *c_smob = (command_smob *) context;
287 cmdscm_release_command (c_smob);
290 /* Called by gdb to invoke the command. */
292 static void
293 cmdscm_function (const char *args, int from_tty, cmd_list_element *command)
295 command_smob *c_smob/*obj*/ = (command_smob *) command->context ();
296 SCM arg_scm, tty_scm, result;
298 gdb_assert (c_smob != NULL);
300 if (args == NULL)
301 args = "";
302 arg_scm = gdbscm_scm_from_string (args, strlen (args), host_charset (), 1);
303 if (gdbscm_is_exception (arg_scm))
304 error (_("Could not convert arguments to Scheme string."));
306 tty_scm = scm_from_bool (from_tty);
308 result = gdbscm_safe_call_3 (c_smob->invoke, c_smob->containing_scm,
309 arg_scm, tty_scm, gdbscm_user_error_p);
311 if (gdbscm_is_exception (result))
313 /* Don't print the stack if this was an error signalled by the command
314 itself. */
315 if (gdbscm_user_error_p (gdbscm_exception_key (result)))
317 gdb::unique_xmalloc_ptr<char> msg
318 = gdbscm_exception_message_to_string (result);
320 error ("%s", msg.get ());
322 else
324 gdbscm_print_gdb_exception (SCM_BOOL_F, result);
325 error (_("Error occurred in Scheme-implemented GDB command."));
330 /* Subroutine of cmdscm_completer to simplify it.
331 Print an error message indicating that COMPLETION is a bad completion
332 result. */
334 static void
335 cmdscm_bad_completion_result (const char *msg, SCM completion)
337 SCM port = scm_current_error_port ();
339 scm_puts (msg, port);
340 scm_display (completion, port);
341 scm_newline (port);
344 /* Subroutine of cmdscm_completer to simplify it.
345 Validate COMPLETION and add to RESULT.
346 If an error occurs print an error message.
347 The result is a boolean indicating success. */
349 static int
350 cmdscm_add_completion (SCM completion, completion_tracker &tracker)
352 SCM except_scm;
354 if (!scm_is_string (completion))
356 /* Inform the user, but otherwise ignore the entire result. */
357 cmdscm_bad_completion_result (_("Bad text from completer: "),
358 completion);
359 return 0;
362 gdb::unique_xmalloc_ptr<char> item
363 = gdbscm_scm_to_string (completion, NULL, host_charset (), 1,
364 &except_scm);
365 if (item == NULL)
367 /* Inform the user, but otherwise ignore the entire result. */
368 gdbscm_print_gdb_exception (SCM_BOOL_F, except_scm);
369 return 0;
372 tracker.add_completion (std::move (item));
374 return 1;
377 /* Called by gdb for command completion. */
379 static void
380 cmdscm_completer (struct cmd_list_element *command,
381 completion_tracker &tracker,
382 const char *text, const char *word)
384 command_smob *c_smob/*obj*/ = (command_smob *) command->context ();
385 SCM completer_result_scm;
386 SCM text_scm, word_scm;
388 gdb_assert (c_smob != NULL);
389 gdb_assert (gdbscm_is_procedure (c_smob->complete));
391 text_scm = gdbscm_scm_from_string (text, strlen (text), host_charset (),
393 if (gdbscm_is_exception (text_scm))
394 error (_("Could not convert \"text\" argument to Scheme string."));
395 word_scm = gdbscm_scm_from_string (word, strlen (word), host_charset (),
397 if (gdbscm_is_exception (word_scm))
398 error (_("Could not convert \"word\" argument to Scheme string."));
400 completer_result_scm
401 = gdbscm_safe_call_3 (c_smob->complete, c_smob->containing_scm,
402 text_scm, word_scm, NULL);
404 if (gdbscm_is_exception (completer_result_scm))
406 /* Inform the user, but otherwise ignore. */
407 gdbscm_print_gdb_exception (SCM_BOOL_F, completer_result_scm);
408 return;
411 if (gdbscm_is_true (scm_list_p (completer_result_scm)))
413 SCM list = completer_result_scm;
415 while (!scm_is_eq (list, SCM_EOL))
417 SCM next = scm_car (list);
419 if (!cmdscm_add_completion (next, tracker))
420 break;
422 list = scm_cdr (list);
425 else if (itscm_is_iterator (completer_result_scm))
427 SCM iter = completer_result_scm;
428 SCM next = itscm_safe_call_next_x (iter, NULL);
430 while (gdbscm_is_true (next))
432 if (gdbscm_is_exception (next))
434 /* Inform the user. */
435 gdbscm_print_gdb_exception (SCM_BOOL_F, completer_result_scm);
436 break;
439 if (cmdscm_add_completion (next, tracker))
440 break;
442 next = itscm_safe_call_next_x (iter, NULL);
445 else
447 /* Inform the user, but otherwise ignore. */
448 cmdscm_bad_completion_result (_("Bad completer result: "),
449 completer_result_scm);
453 /* Helper for gdbscm_make_command which locates the command list to use and
454 pulls out the command name.
456 NAME is the command name list. The final word in the list is the
457 name of the new command. All earlier words must be existing prefix
458 commands.
460 *BASE_LIST is set to the final prefix command's list of
461 *sub-commands.
463 START_LIST is the list in which the search starts.
465 This function returns the xmalloc()d name of the new command.
466 On error a Scheme exception is thrown. */
468 char *
469 gdbscm_parse_command_name (const char *name,
470 const char *func_name, int arg_pos,
471 struct cmd_list_element ***base_list,
472 struct cmd_list_element **start_list)
474 struct cmd_list_element *elt;
475 int len = strlen (name);
476 int i, lastchar;
477 char *msg;
479 /* Skip trailing whitespace. */
480 for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
482 if (i < 0)
484 gdbscm_out_of_range_error (func_name, arg_pos,
485 gdbscm_scm_from_c_string (name),
486 _("no command name found"));
488 lastchar = i;
490 /* Find first character of the final word. */
491 for (; i > 0 && valid_cmd_char_p (name[i - 1]); --i)
493 gdb::unique_xmalloc_ptr<char> result ((char *) xmalloc (lastchar - i + 2));
494 memcpy (result.get (), &name[i], lastchar - i + 1);
495 result.get ()[lastchar - i + 1] = '\0';
497 /* Skip whitespace again. */
498 for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
500 if (i < 0)
502 *base_list = start_list;
503 return result.release ();
506 gdb::unique_xmalloc_ptr<char> prefix_text ((char *) xmalloc (i + 2));
507 memcpy (prefix_text.get (), name, i + 1);
508 prefix_text.get ()[i + 1] = '\0';
510 const char *prefix_text2 = prefix_text.get ();
511 elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1);
512 if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
514 msg = xstrprintf (_("could not find command prefix '%s'"),
515 prefix_text.get ()).release ();
516 scm_dynwind_begin ((scm_t_dynwind_flags) 0);
517 gdbscm_dynwind_xfree (msg);
518 gdbscm_out_of_range_error (func_name, arg_pos,
519 gdbscm_scm_from_c_string (name), msg);
522 if (elt->is_prefix ())
524 *base_list = elt->subcommands;
525 return result.release ();
528 msg = xstrprintf (_("'%s' is not a prefix command"),
529 prefix_text.get ()).release ();
530 scm_dynwind_begin ((scm_t_dynwind_flags) 0);
531 gdbscm_dynwind_xfree (msg);
532 gdbscm_out_of_range_error (func_name, arg_pos,
533 gdbscm_scm_from_c_string (name), msg);
534 /* NOTREACHED */
537 static const scheme_integer_constant command_classes[] =
539 /* Note: alias and user are special; pseudo appears to be unused,
540 and there is no reason to expose tui, I think. */
541 { "COMMAND_NONE", no_class },
542 { "COMMAND_RUNNING", class_run },
543 { "COMMAND_DATA", class_vars },
544 { "COMMAND_STACK", class_stack },
545 { "COMMAND_FILES", class_files },
546 { "COMMAND_SUPPORT", class_support },
547 { "COMMAND_STATUS", class_info },
548 { "COMMAND_BREAKPOINTS", class_breakpoint },
549 { "COMMAND_TRACEPOINTS", class_trace },
550 { "COMMAND_OBSCURE", class_obscure },
551 { "COMMAND_MAINTENANCE", class_maintenance },
552 { "COMMAND_USER", class_user },
554 END_INTEGER_CONSTANTS
557 /* Return non-zero if command_class is a valid command class. */
560 gdbscm_valid_command_class_p (int command_class)
562 int i;
564 for (i = 0; command_classes[i].name != NULL; ++i)
566 if (command_classes[i].value == command_class)
567 return 1;
570 return 0;
573 /* Return a normalized form of command NAME.
574 That is tabs are replaced with spaces and multiple spaces are replaced
575 with a single space.
576 If WANT_TRAILING_SPACE is non-zero, add one space at the end. This is for
577 prefix commands.
578 but that is the caller's responsibility.
579 Space for the result is allocated on the GC heap. */
581 char *
582 gdbscm_canonicalize_command_name (const char *name, int want_trailing_space)
584 int i, out, seen_word;
585 char *result
586 = (char *) scm_gc_malloc_pointerless (strlen (name) + 2, FUNC_NAME);
588 i = out = seen_word = 0;
589 while (name[i])
591 /* Skip whitespace. */
592 while (name[i] == ' ' || name[i] == '\t')
593 ++i;
594 /* Copy non-whitespace characters. */
595 if (name[i])
597 if (seen_word)
598 result[out++] = ' ';
599 while (name[i] && name[i] != ' ' && name[i] != '\t')
600 result[out++] = name[i++];
601 seen_word = 1;
604 if (want_trailing_space)
605 result[out++] = ' ';
606 result[out] = '\0';
608 return result;
611 /* (make-command name [#:invoke lambda]
612 [#:command-class class] [#:completer-class completer]
613 [#:prefix? <bool>] [#:doc <string>]) -> <gdb:command>
615 NAME is the name of the command. It may consist of multiple words,
616 in which case the final word is the name of the new command, and
617 earlier words must be prefix commands.
619 INVOKE is a procedure of three arguments that performs the command when
620 invoked: (lambda (self arg from-tty) ...).
621 Its result is unspecified.
623 CLASS is the kind of command. It must be one of the COMMAND_*
624 constants defined in the gdb module. If not specified, "no_class" is used.
626 COMPLETER is the kind of completer. It must be either:
627 #f - completion is not supported for this command.
628 One of the COMPLETE_* constants defined in the gdb module.
629 A procedure of three arguments: (lambda (self text word) ...).
630 Its result is one of:
631 A list of strings.
632 A <gdb:iterator> object that returns the set of possible completions,
633 ending with #f.
634 TODO(dje): Once PR 16699 is fixed, add support for returning
635 a COMPLETE_* constant.
636 If not specified, then completion is not supported for this command.
638 If PREFIX is #t, then this command is a prefix command.
640 DOC is the doc string for the command.
642 The result is the <gdb:command> Scheme object.
643 The command is not available to be used yet, however.
644 It must still be added to gdb with register-command!. */
646 static SCM
647 gdbscm_make_command (SCM name_scm, SCM rest)
649 const SCM keywords[] = {
650 invoke_keyword, command_class_keyword, completer_class_keyword,
651 prefix_p_keyword, doc_keyword, SCM_BOOL_F
653 int invoke_arg_pos = -1, command_class_arg_pos = 1;
654 int completer_class_arg_pos = -1, is_prefix_arg_pos = -1;
655 int doc_arg_pos = -1;
656 char *s;
657 char *name;
658 enum command_class command_class = no_class;
659 SCM completer_class = SCM_BOOL_F;
660 int is_prefix = 0;
661 char *doc = NULL;
662 SCM invoke = SCM_BOOL_F;
663 SCM c_scm;
664 command_smob *c_smob;
666 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#OiOts",
667 name_scm, &name, rest,
668 &invoke_arg_pos, &invoke,
669 &command_class_arg_pos, &command_class,
670 &completer_class_arg_pos, &completer_class,
671 &is_prefix_arg_pos, &is_prefix,
672 &doc_arg_pos, &doc);
674 if (doc == NULL)
675 doc = xstrdup (_("This command is not documented."));
677 s = name;
678 name = gdbscm_canonicalize_command_name (s, is_prefix);
679 xfree (s);
680 s = doc;
681 doc = gdbscm_gc_xstrdup (s);
682 xfree (s);
684 if (is_prefix
685 ? name[0] == ' '
686 : name[0] == '\0')
688 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, name_scm,
689 _("no command name found"));
692 if (gdbscm_is_true (invoke))
694 SCM_ASSERT_TYPE (gdbscm_is_procedure (invoke), invoke,
695 invoke_arg_pos, FUNC_NAME, _("procedure"));
698 if (!gdbscm_valid_command_class_p (command_class))
700 gdbscm_out_of_range_error (FUNC_NAME, command_class_arg_pos,
701 scm_from_int (command_class),
702 _("invalid command class argument"));
705 SCM_ASSERT_TYPE (gdbscm_is_false (completer_class)
706 || scm_is_integer (completer_class)
707 || gdbscm_is_procedure (completer_class),
708 completer_class, completer_class_arg_pos, FUNC_NAME,
709 _("integer or procedure"));
710 if (scm_is_integer (completer_class)
711 && !scm_is_signed_integer (completer_class, 0, N_COMPLETERS - 1))
713 gdbscm_out_of_range_error (FUNC_NAME, completer_class_arg_pos,
714 completer_class,
715 _("invalid completion type argument"));
718 c_scm = cmdscm_make_command_smob ();
719 c_smob = (command_smob *) SCM_SMOB_DATA (c_scm);
720 c_smob->name = name;
721 c_smob->is_prefix = is_prefix;
722 c_smob->cmd_class = command_class;
723 c_smob->doc = doc;
724 c_smob->invoke = invoke;
725 c_smob->complete = completer_class;
727 return c_scm;
730 /* (register-command! <gdb:command>) -> unspecified
732 It is an error to register a command more than once. */
734 static SCM
735 gdbscm_register_command_x (SCM self)
737 command_smob *c_smob
738 = cmdscm_get_command_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
739 char *cmd_name;
740 struct cmd_list_element **cmd_list;
741 struct cmd_list_element *cmd = NULL;
743 if (cmdscm_is_valid (c_smob))
744 scm_misc_error (FUNC_NAME, _("command is already registered"), SCM_EOL);
746 cmd_name = gdbscm_parse_command_name (c_smob->name, FUNC_NAME, SCM_ARG1,
747 &cmd_list, &cmdlist);
748 c_smob->cmd_name = gdbscm_gc_xstrdup (cmd_name);
749 xfree (cmd_name);
751 gdbscm_gdb_exception exc {};
754 if (c_smob->is_prefix)
756 /* If we have our own "invoke" method, then allow unknown
757 sub-commands. */
758 int allow_unknown = gdbscm_is_true (c_smob->invoke);
760 cmd = add_prefix_cmd (c_smob->cmd_name, c_smob->cmd_class,
761 NULL, c_smob->doc, &c_smob->sub_list,
762 allow_unknown, cmd_list);
764 else
766 cmd = add_cmd (c_smob->cmd_name, c_smob->cmd_class,
767 c_smob->doc, cmd_list);
770 catch (const gdb_exception &except)
772 exc = unpack (except);
774 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
776 /* Note: At this point the command exists in gdb.
777 So no more errors after this point. */
779 /* There appears to be no API to set this. */
780 cmd->func = cmdscm_function;
781 cmd->destroyer = cmdscm_destroyer;
783 c_smob->command = cmd;
784 cmd->set_context (c_smob);
786 if (gdbscm_is_true (c_smob->complete))
788 set_cmd_completer (cmd,
789 scm_is_integer (c_smob->complete)
790 ? cmdscm_completers[scm_to_int (c_smob->complete)].completer
791 : cmdscm_completer);
794 /* The owner of this command is not in GC-controlled memory, so we need
795 to protect it from GC until the command is deleted. */
796 scm_gc_protect_object (c_smob->containing_scm);
798 return SCM_UNSPECIFIED;
801 /* Initialize the Scheme command support. */
803 static const scheme_function command_functions[] =
805 { "make-command", 1, 0, 1, as_a_scm_t_subr (gdbscm_make_command),
807 Make a GDB command object.\n\
809 Arguments: name [#:invoke lambda]\n\
810 [#:command-class <class>] [#:completer-class <completer>]\n\
811 [#:prefix? <bool>] [#:doc string]\n\
812 name: The name of the command. It may consist of multiple words,\n\
813 in which case the final word is the name of the new command, and\n\
814 earlier words must be prefix commands.\n\
815 invoke: A procedure of three arguments to perform the command.\n\
816 (lambda (self arg from-tty) ...)\n\
817 Its result is unspecified.\n\
818 class: The class of the command, one of COMMAND_*.\n\
819 The default is COMMAND_NONE.\n\
820 completer: The kind of completer, #f, one of COMPLETE_*, or a procedure\n\
821 to perform the completion: (lambda (self text word) ...).\n\
822 prefix?: If true then the command is a prefix command.\n\
823 doc: The \"doc string\" of the command.\n\
824 Returns: <gdb:command> object" },
826 { "register-command!", 1, 0, 0, as_a_scm_t_subr (gdbscm_register_command_x),
828 Register a <gdb:command> object with GDB." },
830 { "command?", 1, 0, 0, as_a_scm_t_subr (gdbscm_command_p),
832 Return #t if the object is a <gdb:command> object." },
834 { "command-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_command_valid_p),
836 Return #t if the <gdb:command> object is valid." },
838 { "dont-repeat", 1, 0, 0, as_a_scm_t_subr (gdbscm_dont_repeat),
840 Prevent command repetition when user enters an empty line.\n\
842 Arguments: <gdb:command>\n\
843 Returns: unspecified" },
845 END_FUNCTIONS
848 /* Initialize the 'commands' code. */
850 void
851 gdbscm_initialize_commands (void)
853 int i;
855 command_smob_tag
856 = gdbscm_make_smob_type (command_smob_name, sizeof (command_smob));
857 scm_set_smob_print (command_smob_tag, cmdscm_print_command_smob);
859 gdbscm_define_integer_constants (command_classes, 1);
860 gdbscm_define_functions (command_functions, 1);
862 for (i = 0; i < N_COMPLETERS; ++i)
864 scm_c_define (cmdscm_completers[i].name, scm_from_int (i));
865 scm_c_export (cmdscm_completers[i].name, NULL);
868 invoke_keyword = scm_from_latin1_keyword ("invoke");
869 command_class_keyword = scm_from_latin1_keyword ("command-class");
870 completer_class_keyword = scm_from_latin1_keyword ("completer-class");
871 prefix_p_keyword = scm_from_latin1_keyword ("prefix?");
872 doc_keyword = scm_from_latin1_keyword ("doc");