1 /** @page primercommand Command Development Primer
3 This page provides a primer for writing commands by introducing @c hello
4 module. The full source code used in this example can be found in
5 hello.c, and the @ref primercmdcode section shows how to use it.
7 A summary of this information can be found in @ref helpercommand .
9 @section primercmdhandler Command Handlers
11 Defining new commands and their helpers is easy. The following code
12 defines a simple command handler that delegates its argument parsing:
14 COMMAND_HANDLER(handle_hello_command)
16 const char *sep, *name;
17 int retval = CALL_COMMAND_HANDLER(handle_hello_args);
18 if (ERROR_OK == retval)
19 command_print(CMD_CTX, "Greetings%s%s!", sep, name);
24 Here, the @c COMMAND_HANDLER macro establishes the function signature,
25 see in command.h by the @c __COMMAND_HANDLER macro.
27 The COMMAND_HELPER macro function allows defining functions with an
28 extended version of the base signature. These helper functions can be
29 called (with the appropriate parameters), the @c CALL_COMMAND_HANDLER
30 macro to pass any e as parameters to the following helper function:
32 The subsequent blocks of code are a normal C function that can do
33 anything, so only complex commands deserve should use comamnd helper
34 functions. In this respect, this example uses one to demonstrate how --
35 not when -- they should be used.
38 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
42 LOG_ERROR("%s: too many arguments", CMD_NAME);
43 return ERROR_COMMAND_SYNTAX_ERROR;
57 Of course, you may also call other macros or functions, but that extends
58 beyond the scope of this tutorial on writing commands.
60 @section primercmdreg Command Registration
62 Before this new function can be used, it must be registered somehow.
63 For a new module, registering should be done in a new function for
64 the purpose, which must be called from @c openocd.c:
66 int hello_register_commands(struct command_context_s *cmd_ctx)
68 struct command_s *cmd = register_command(cmd_ctx, NULL, "hello",
69 NULL, COMMAND_ANY, "print greetings");
70 return cmd ? ERROR_OK : -ENOMEM;
74 That's it! The command should now be registered and avaiable to scripts.
76 @section primercmdcode Trying These Example Commands
78 The commands may be enabled by editing src/openocd.c and uncommenting
79 the call to @c hello_register_commands and rebuilding the source tree.
81 Once OpenOCD has been built with this example code, the following script
82 demonstrate the abilities that the @c hello module provides:
87 hello John Doe # error: too many arguments
90 If saved in @c hello.cfg, then running <code>openocd -f hello.cfg</code>
91 should produce the following output before exiting:
96 Error: ocd_hello: too many arguments
99 This difference between the registered and displayed command name comes from
100 the fact that the TCL scripts are provided with a stub that calls the munged
101 name. This stub wraps the internal <code>ocd_</code>-prefixed routine,
102 providing a measure of high-level error handling.