command.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2009 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /**
  16. * @defgroup command command
  17. * @{ @ingroup swanctl
  18. */
  19. #ifndef COMMAND_H_
  20. #define COMMAND_H_
  21. #include <libvici.h>
  22. #include <library.h>
  23. /**
  24. * Maximum number of commands (+1).
  25. */
  26. #define MAX_COMMANDS 26
  27. /**
  28. * Maximum number of options in a command (+3)
  29. */
  30. #define MAX_OPTIONS 34
  31. /**
  32. * Maximum number of usage summary lines (+1)
  33. */
  34. #define MAX_LINES 10
  35. typedef struct command_t command_t;
  36. typedef struct command_option_t command_option_t;
  37. typedef enum command_format_options_t command_format_options_t;
  38. /**
  39. * Option specification
  40. */
  41. struct command_option_t {
  42. /** long option string of the option */
  43. char *name;
  44. /** short option character of the option */
  45. char op;
  46. /** expected argument to option, no/req/opt_argument */
  47. int arg;
  48. /** description of the option */
  49. char *desc;
  50. };
  51. /**
  52. * Command specification.
  53. */
  54. struct command_t {
  55. /** Function implementing the command */
  56. int (*call)(vici_conn_t *conn);
  57. /** short option character */
  58. char op;
  59. /** long option string */
  60. char *cmd;
  61. /** description of the command */
  62. char *description;
  63. /** usage summary of the command */
  64. char *line[MAX_LINES];
  65. /** list of options the command accepts */
  66. command_option_t options[MAX_OPTIONS];
  67. };
  68. /**
  69. * Command format options
  70. */
  71. enum command_format_options_t {
  72. COMMAND_FORMAT_NONE = 0,
  73. COMMAND_FORMAT_RAW = (1<<0),
  74. COMMAND_FORMAT_PRETTY = (1<<1),
  75. COMMAND_FORMAT_PEM = (1<<2),
  76. };
  77. /**
  78. * Get the next option, as with getopt.
  79. */
  80. int command_getopt(char **arg);
  81. /**
  82. * Register a command.
  83. */
  84. void command_register(command_t command);
  85. /**
  86. * Dispatch commands.
  87. */
  88. int command_dispatch(int argc, char *argv[]);
  89. /**
  90. * Show usage information of active command.
  91. */
  92. int command_usage(char *error, ...);
  93. #endif /** COMMAND_H_ @}*/