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.
 
 
 
 
 
 

129 lines
3.6 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. {
  27. LOG_ERROR("%s: incorrect number of arguments", CMD_NAME);
  28. return ERROR_COMMAND_SYNTAX_ERROR;
  29. }
  30. uint32_t address;
  31. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  32. const char *msg = "<unchanged>";
  33. if (CMD_ARGC == 2)
  34. {
  35. bool enable;
  36. COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
  37. msg = enable ? "enable" : "disable";
  38. }
  39. LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
  40. return ERROR_OK;
  41. }
  42. static bool foo_flag;
  43. COMMAND_HANDLER(handle_flag_command)
  44. {
  45. return CALL_COMMAND_HANDLER(handle_command_parse_bool,
  46. &foo_flag, "foo flag");
  47. }
  48. static const struct command_registration foo_command_handlers[] = {
  49. {
  50. .name = "bar",
  51. .handler = &handle_foo_command,
  52. .mode = COMMAND_ANY,
  53. .usage = "address ['enable'|'disable']",
  54. .help = "an example command",
  55. },
  56. {
  57. .name = "baz",
  58. .handler = &handle_foo_command,
  59. .mode = COMMAND_ANY,
  60. .usage = "address ['enable'|'disable']",
  61. .help = "a sample command",
  62. },
  63. {
  64. .name = "flag",
  65. .handler = &handle_flag_command,
  66. .mode = COMMAND_ANY,
  67. .usage = "[on|off]",
  68. .help = "set a flag",
  69. },
  70. COMMAND_REGISTRATION_DONE
  71. };
  72. static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
  73. {
  74. if (CMD_ARGC > 1)
  75. {
  76. LOG_ERROR("%s: too many arguments", CMD_NAME);
  77. return ERROR_COMMAND_SYNTAX_ERROR;
  78. }
  79. if (1 == CMD_ARGC)
  80. {
  81. *sep = " ";
  82. *name = CMD_ARGV[0];
  83. }
  84. else
  85. *sep = *name = "";
  86. return ERROR_OK;
  87. }
  88. COMMAND_HANDLER(handle_hello_command)
  89. {
  90. const char *sep, *name;
  91. int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
  92. if (ERROR_OK == retval)
  93. command_print(CMD_CTX, "Greetings%s%s!", sep, name);
  94. return retval;
  95. }
  96. const struct command_registration hello_command_handlers[] = {
  97. {
  98. .name = "hello",
  99. .handler = handle_hello_command,
  100. .mode = COMMAND_ANY,
  101. .help = "prints a warm welcome",
  102. .usage = "[name]",
  103. },
  104. {
  105. .name = "foo",
  106. .mode = COMMAND_ANY,
  107. .help = "example command handler skeleton",
  108. .chain = foo_command_handlers,
  109. },
  110. COMMAND_REGISTRATION_DONE
  111. };
  112. int hello_register_commands(struct command_context *cmd_ctx)
  113. {
  114. return register_commands(cmd_ctx, NULL, hello_command_handlers);
  115. }