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.
 
 
 
 
 
 

113 lines
3.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  16. ***************************************************************************/
  17. #ifdef HAVE_CONFIG_H
  18. #include <config.h>
  19. #endif
  20. #include <helper/log.h>
  21. COMMAND_HANDLER(handle_foo_command)
  22. {
  23. if (CMD_ARGC < 1 || CMD_ARGC > 2)
  24. return ERROR_COMMAND_SYNTAX_ERROR;
  25. uint32_t address;
  26. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  27. const char *msg = "<unchanged>";
  28. if (CMD_ARGC == 2) {
  29. bool enable;
  30. COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
  31. msg = enable ? "enable" : "disable";
  32. }
  33. LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
  34. return ERROR_OK;
  35. }
  36. static bool foo_flag;
  37. COMMAND_HANDLER(handle_flag_command)
  38. {
  39. return CALL_COMMAND_HANDLER(handle_command_parse_bool,
  40. &foo_flag, "foo flag");
  41. }
  42. static const struct command_registration foo_command_handlers[] = {
  43. {
  44. .name = "bar",
  45. .handler = &handle_foo_command,
  46. .mode = COMMAND_ANY,
  47. .usage = "address ['enable'|'disable']",
  48. .help = "an example command",
  49. },
  50. {
  51. .name = "baz",
  52. .handler = &handle_foo_command,
  53. .mode = COMMAND_ANY,
  54. .usage = "address ['enable'|'disable']",
  55. .help = "a sample command",
  56. },
  57. {
  58. .name = "flag",
  59. .handler = &handle_flag_command,
  60. .mode = COMMAND_ANY,
  61. .usage = "[on|off]",
  62. .help = "set a flag",
  63. },
  64. COMMAND_REGISTRATION_DONE
  65. };
  66. static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
  67. {
  68. if (CMD_ARGC > 1)
  69. return ERROR_COMMAND_SYNTAX_ERROR;
  70. if (1 == CMD_ARGC) {
  71. *sep = " ";
  72. *name = CMD_ARGV[0];
  73. } else
  74. *sep = *name = "";
  75. return ERROR_OK;
  76. }
  77. COMMAND_HANDLER(handle_hello_command)
  78. {
  79. const char *sep, *name;
  80. int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
  81. if (retval == ERROR_OK)
  82. command_print(CMD, "Greetings%s%s!", sep, name);
  83. return retval;
  84. }
  85. const struct command_registration hello_command_handlers[] = {
  86. {
  87. .name = "hello",
  88. .handler = handle_hello_command,
  89. .mode = COMMAND_ANY,
  90. .help = "prints a warm welcome",
  91. .usage = "[name]",
  92. },
  93. {
  94. .name = "foo",
  95. .mode = COMMAND_ANY,
  96. .help = "example command handler skeleton",
  97. .chain = foo_command_handlers,
  98. .usage = "",
  99. },
  100. COMMAND_REGISTRATION_DONE
  101. };