You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
2.9 KiB

  1. /** @page primercommand Command Development Primer
  2. This page provides a primer for writing commands by introducing @c hello
  3. module. The full source code used in this example can be found in
  4. hello.c, and the @ref primercmdcode section shows how to use it.
  5. A summary of this information can be found in @ref helpercommand .
  6. @section primercmdhandler Command Handlers
  7. Defining new commands and their helpers is easy. The following code
  8. defines a simple command handler that delegates its argument parsing:
  9. @code
  10. COMMAND_HANDLER(handle_hello_command)
  11. {
  12. const char *sep, *name;
  13. int retval = CALL_COMMAND_HANDLER(handle_hello_args);
  14. if (ERROR_OK == retval)
  15. command_print(cmd_ctx, "Greetings%s%s!", sep, name);
  16. return retval;
  17. }
  18. @endcode
  19. Here, the @c COMMAND_HANDLER macro establishes the function signature,
  20. see in command.h by the @c __COMMAND_HANDLER macro.
  21. The COMMAND_HELPER macro function allows defining functions with an
  22. extended version of the base signature. These helper functions can be
  23. called (with the appropriate parameters), the @c CALL_COMMAND_HANDLER
  24. macro to pass any e as parameters to the following helper function:
  25. The subsequent blocks of code are a normal C function that can do
  26. anything, so only complex commands deserve should use comamnd helper
  27. functions. In this respect, this example uses one to demonstrate how --
  28. not when -- they should be used.
  29. @code
  30. static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
  31. {
  32. if (argc > 1)
  33. {
  34. LOG_ERROR("%s: too many arguments", COMMAND_NAME);
  35. return ERROR_COMMAND_SYNTAX_ERROR;
  36. }
  37. if (1 == argc)
  38. {
  39. *sep = ", ";
  40. *name = args[0];
  41. }
  42. else
  43. *sep = *name = "";
  44. return ERROR_OK;
  45. }
  46. @endcode
  47. Of course, you may also call other macros or functions, but that extends
  48. beyond the scope of this tutorial on writing commands.
  49. @section primercmdreg Command Registration
  50. Before this new function can be used, it must be registered somehow.
  51. For a new module, registering should be done in a new function for
  52. the purpose, which must be called from @c openocd.c:
  53. @code
  54. int hello_register_commands(struct command_context_s *cmd_ctx)
  55. {
  56. struct command_s *cmd = register_command(cmd_ctx, NULL, "hello",
  57. NULL, COMMAND_ANY, "print greetings");
  58. return cmd ? ERROR_OK : -ENOMEM;
  59. }
  60. @endcode
  61. That's it! The command should now be registered and avaiable to scripts.
  62. @section primercmdcode Trying These Example Commands
  63. The commands may be enabled by editing src/openocd.c and uncommenting
  64. the call to @c hello_register_commands and rebuilding the source tree.
  65. Once OpenOCD has been built with this example code, the following script
  66. demonstrate the abilities that the @c hello module provides:
  67. @code
  68. hello
  69. hello World
  70. hello {John Doe}
  71. hello John Doe # error: too many arguments
  72. @endcode
  73. If saved in @c hello.cfg, then running <code>openocd -f hello.cfg</code>
  74. should produce the following output before exiting:
  75. @code
  76. Greetings!
  77. Greetings, World!
  78. Greetings, John Doe!
  79. Error: ocd_hello: too many arguments
  80. @endcode
  81. */