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.
 
 
 
 
 
 

115 lines
3.3 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, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <helper/log.h>
  23. COMMAND_HANDLER(handle_foo_command)
  24. {
  25. if (CMD_ARGC < 1 || CMD_ARGC > 2)
  26. return ERROR_COMMAND_SYNTAX_ERROR;
  27. uint32_t address;
  28. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  29. const char *msg = "<unchanged>";
  30. if (CMD_ARGC == 2) {
  31. bool enable;
  32. COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
  33. msg = enable ? "enable" : "disable";
  34. }
  35. LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
  36. return ERROR_OK;
  37. }
  38. static bool foo_flag;
  39. COMMAND_HANDLER(handle_flag_command)
  40. {
  41. return CALL_COMMAND_HANDLER(handle_command_parse_bool,
  42. &foo_flag, "foo flag");
  43. }
  44. static const struct command_registration foo_command_handlers[] = {
  45. {
  46. .name = "bar",
  47. .handler = &handle_foo_command,
  48. .mode = COMMAND_ANY,
  49. .usage = "address ['enable'|'disable']",
  50. .help = "an example command",
  51. },
  52. {
  53. .name = "baz",
  54. .handler = &handle_foo_command,
  55. .mode = COMMAND_ANY,
  56. .usage = "address ['enable'|'disable']",
  57. .help = "a sample command",
  58. },
  59. {
  60. .name = "flag",
  61. .handler = &handle_flag_command,
  62. .mode = COMMAND_ANY,
  63. .usage = "[on|off]",
  64. .help = "set a flag",
  65. },
  66. COMMAND_REGISTRATION_DONE
  67. };
  68. static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
  69. {
  70. if (CMD_ARGC > 1)
  71. return ERROR_COMMAND_SYNTAX_ERROR;
  72. if (1 == CMD_ARGC) {
  73. *sep = " ";
  74. *name = CMD_ARGV[0];
  75. } else
  76. *sep = *name = "";
  77. return ERROR_OK;
  78. }
  79. COMMAND_HANDLER(handle_hello_command)
  80. {
  81. const char *sep, *name;
  82. int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
  83. if (ERROR_OK == retval)
  84. command_print(CMD_CTX, "Greetings%s%s!", sep, name);
  85. return retval;
  86. }
  87. const struct command_registration hello_command_handlers[] = {
  88. {
  89. .name = "hello",
  90. .handler = handle_hello_command,
  91. .mode = COMMAND_ANY,
  92. .help = "prints a warm welcome",
  93. .usage = "[name]",
  94. },
  95. {
  96. .name = "foo",
  97. .mode = COMMAND_ANY,
  98. .help = "example command handler skeleton",
  99. .chain = foo_command_handlers,
  100. },
  101. COMMAND_REGISTRATION_DONE
  102. };