shell.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2015-2018 Richard Braun.
  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 3 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. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. *
  20. *
  21. * Minimalist shell for embedded systems.
  22. */
  23. #ifndef KERN_SHELL_H
  24. #define KERN_SHELL_H
  25. #include <stdarg.h>
  26. #include <stddef.h>
  27. #include <kern/error.h>
  28. #include <kern/init.h>
  29. #include <kern/macros.h>
  30. /*
  31. * Types for I/O functions.
  32. */
  33. typedef int (*shell_getc_fn_t)(void *io_object);
  34. typedef void (*shell_vfprintf_fn_t)(void *io_object,
  35. const char *format, va_list ap);
  36. /*
  37. * Shell structure, statically allocatable.
  38. */
  39. struct shell;
  40. /*
  41. * Shell command structure.
  42. */
  43. struct shell_cmd;
  44. /*
  45. * Command container, shareable across multiple shell instances.
  46. */
  47. struct shell_cmd_set;
  48. /*
  49. * Type for command implementation callbacks.
  50. */
  51. typedef void (*shell_fn_t)(struct shell *shell, int argc, char **argv);
  52. #include <kern/shell_i.h>
  53. #define SHELL_REGISTER_CMDS(cmds, cmd_set) \
  54. MACRO_BEGIN \
  55. size_t i_; \
  56. int error_; \
  57. \
  58. for (i_ = 0; i_ < ARRAY_SIZE(cmds); i_++) { \
  59. error_ = shell_cmd_set_register(cmd_set, &(cmds)[i_]); \
  60. error_check(error_, __func__); \
  61. } \
  62. MACRO_END
  63. /*
  64. * Static shell command initializers.
  65. */
  66. #define SHELL_CMD_INITIALIZER(name, fn, usage, short_desc) \
  67. { NULL, NULL, name, fn, usage, short_desc, NULL }
  68. #define SHELL_CMD_INITIALIZER2(name, fn, usage, short_desc, long_desc) \
  69. { NULL, NULL, name, fn, usage, short_desc, long_desc }
  70. /*
  71. * Initialize a shell command structure.
  72. */
  73. void shell_cmd_init(struct shell_cmd *cmd, const char *name,
  74. shell_fn_t fn, const char *usage,
  75. const char *short_desc, const char *long_desc);
  76. /*
  77. * Initialize a command set.
  78. */
  79. void shell_cmd_set_init(struct shell_cmd_set *cmd_set);
  80. /*
  81. * Register a shell command.
  82. *
  83. * The command name must be unique. It must not include characters outside
  84. * the [a-zA-Z0-9-_] class.
  85. *
  86. * Commands may safely be registered while the command set is used.
  87. *
  88. * The command structure must persist in memory as long as the command set
  89. * is used.
  90. */
  91. int shell_cmd_set_register(struct shell_cmd_set *cmd_set,
  92. struct shell_cmd *cmd);
  93. /*
  94. * Initialize a shell instance.
  95. *
  96. * On return, shell commands can be registered.
  97. */
  98. void shell_init(struct shell *shell, struct shell_cmd_set *cmd_set,
  99. shell_getc_fn_t getc_fn, shell_vfprintf_fn_t vfprintf_fn,
  100. void *io_object);
  101. /*
  102. * Obtain the command set associated with a shell.
  103. */
  104. struct shell_cmd_set * shell_get_cmd_set(struct shell *shell);
  105. /*
  106. * Printf-like functions specific to the given shell instance.
  107. */
  108. void shell_printf(struct shell *shell, const char *format, ...)
  109. __attribute__((format(printf, 2, 3)));
  110. void shell_vprintf(struct shell *shell, const char *format, va_list ap)
  111. __attribute__((format(printf, 2, 0)));
  112. /*
  113. * This init operation provides :
  114. * - main shell command registration
  115. */
  116. INIT_OP_DECLARE(shell_setup);
  117. struct shell_cmd_set * shell_get_main_cmd_set(void);
  118. #endif /* KERN_SHELL_H */