shell_i.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 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. #ifndef KERN_SHELL_I_H
  21. #define KERN_SHELL_I_H
  22. #include <stddef.h>
  23. #include <kern/macros.h>
  24. #include <kern/mutex.h>
  25. struct shell_cmd {
  26. struct shell_cmd *ht_next;
  27. struct shell_cmd *ls_next;
  28. const char *name;
  29. shell_fn_t fn;
  30. const char *usage;
  31. const char *short_desc;
  32. const char *long_desc;
  33. };
  34. struct shell_bucket {
  35. struct shell_cmd *cmd;
  36. };
  37. /*
  38. * Binary exponent and size of the hash table used to store commands.
  39. */
  40. #define SHELL_HTABLE_BITS 6
  41. #define SHELL_HTABLE_SIZE (1 << SHELL_HTABLE_BITS)
  42. /*
  43. * The command list is sorted.
  44. */
  45. struct shell_cmd_set {
  46. struct mutex lock;
  47. struct shell_bucket htable[SHELL_HTABLE_SIZE];
  48. struct shell_cmd *cmd_list;
  49. };
  50. #define SHELL_LINE_MAX_SIZE 64
  51. /*
  52. * Line containing a shell entry.
  53. *
  54. * The string must be nul-terminated. The size doesn't include this
  55. * additional nul character, the same way strlen() doesn't account for it.
  56. */
  57. struct shell_line {
  58. char str[SHELL_LINE_MAX_SIZE];
  59. size_t size;
  60. };
  61. /*
  62. * Number of entries in the history.
  63. *
  64. * One of these entryes is used as the current line.
  65. */
  66. #define SHELL_HISTORY_SIZE 21
  67. #if SHELL_HISTORY_SIZE == 0
  68. #error "shell history size must be non-zero"
  69. #endif /* SHELL_HISTORY_SIZE == 0 */
  70. /*
  71. * Shell history.
  72. *
  73. * The history is never empty. There is always at least one entry, the
  74. * current line, referenced by the newest (most recent) index. The array
  75. * is used like a circular buffer, i.e. old entries are implicitely
  76. * erased by new ones. The index references the entry used as a template
  77. * for the current line.
  78. */
  79. struct shell_history {
  80. struct shell_line lines[SHELL_HISTORY_SIZE];
  81. size_t newest;
  82. size_t oldest;
  83. size_t index;
  84. };
  85. /*
  86. * This value changes depending on the standard used and was chosen arbitrarily.
  87. */
  88. #define SHELL_ESC_SEQ_MAX_SIZE 8
  89. #define SHELL_MAX_ARGS 16
  90. /*
  91. * Shell structure.
  92. *
  93. * A shell instance can include temporary variables to minimize stack usage.
  94. */
  95. struct shell {
  96. struct shell_cmd_set *cmd_set;
  97. shell_getc_fn_t getc_fn;
  98. shell_vfprintf_fn_t vfprintf_fn;
  99. void *io_object;
  100. struct shell_history history;
  101. /* Cursor within the current line */
  102. size_t cursor;
  103. /* Members used for escape sequence parsing */
  104. char esc_seq[SHELL_ESC_SEQ_MAX_SIZE];
  105. size_t esc_seq_index;
  106. /*
  107. * Buffer used to store the current line during argument processing.
  108. *
  109. * The pointers in the argv array point inside this buffer. The
  110. * separators immediately following the arguments are replaced with
  111. * null characters.
  112. */
  113. char tmp_line[SHELL_LINE_MAX_SIZE];
  114. int argc;
  115. char *argv[SHELL_MAX_ARGS];
  116. };
  117. #endif /* KERN_SHELL_I_H */