console.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <lib/console.h>
  29. static cmd_block *command_list = NULL;
  30. /* a linear array of statically defined command blocks,
  31. defined in the linker script.
  32. */
  33. extern cmd_block __commands_start[];
  34. extern cmd_block __commands_end[];
  35. static int cmd_help(int argc, const cmd_args *argv);
  36. static int cmd_test(int argc, const cmd_args *argv);
  37. STATIC_COMMAND_START
  38. { "help", "this list", &cmd_help },
  39. { "test", "test the command processor", &cmd_test },
  40. STATIC_COMMAND_END(help);
  41. int console_init(void)
  42. {
  43. printf("console_init: entry\n");
  44. /* add all the statically defined commands to the list */
  45. cmd_block *block;
  46. for (block = __commands_start; block != __commands_end; block++) {
  47. console_register_commands(block);
  48. }
  49. return 0;
  50. }
  51. static const cmd *match_command(const char *command)
  52. {
  53. cmd_block *block;
  54. size_t i;
  55. for (block = command_list; block != NULL; block = block->next) {
  56. const cmd *curr_cmd = block->list;
  57. for (i = 0; i < block->count; i++) {
  58. if (strcmp(command, curr_cmd[i].cmd_str) == 0) {
  59. return &curr_cmd[i];
  60. }
  61. }
  62. }
  63. return NULL;
  64. }
  65. static int read_line(char *buffer, int len)
  66. {
  67. int pos = 0;
  68. int escape_level = 0;
  69. for (;;) {
  70. char c;
  71. /* loop until we get a char */
  72. if (getc(&c) < 0)
  73. continue;
  74. // printf("c = 0x%hhx\n", c);
  75. if (escape_level == 0) {
  76. switch (c) {
  77. case '\r':
  78. case '\n':
  79. putc(c);
  80. goto done;
  81. case 0x7f: // backspace or delete
  82. case 0x8:
  83. if (pos > 0) {
  84. pos--;
  85. puts("\x1b[1D"); // move to the left one
  86. putc(' ');
  87. puts("\x1b[1D"); // move to the left one
  88. }
  89. break;
  90. case 0x1b: // escape
  91. escape_level++;
  92. break;
  93. default:
  94. buffer[pos++] = c;
  95. putc(c);
  96. }
  97. } else if (escape_level == 1) {
  98. // inside an escape, look for '['
  99. if (c == '[') {
  100. escape_level++;
  101. } else {
  102. // we didn't get it, abort
  103. escape_level = 0;
  104. }
  105. } else { // escape_level > 1
  106. switch (c) {
  107. case 67: // right arrow
  108. buffer[pos++] = ' ';
  109. putc(' ');
  110. break;
  111. case 68: // left arrow
  112. if (pos > 0) {
  113. pos--;
  114. puts("\x1b[1D"); // move to the left one
  115. putc(' ');
  116. puts("\x1b[1D"); // move to the left one
  117. }
  118. break;
  119. case 65: // up arrow
  120. case 66: // down arrow
  121. // XXX do history here
  122. break;
  123. default:
  124. break;
  125. }
  126. escape_level = 0;
  127. }
  128. /* end of line. */
  129. if (pos == (len - 1)) {
  130. puts("\nerror: line too long\n");
  131. pos = 0;
  132. goto done;
  133. }
  134. }
  135. done:
  136. // printf("returning pos %d\n", pos);
  137. buffer[pos] = 0;
  138. return pos;
  139. }
  140. static int tokenize_command(char *buffer, cmd_args *args, int arg_count)
  141. {
  142. int pos;
  143. int arg;
  144. bool finished;
  145. enum {
  146. INITIAL = 0,
  147. IN_SPACE,
  148. IN_TOKEN
  149. } state;
  150. pos = 0;
  151. arg = 0;
  152. state = INITIAL;
  153. finished = false;
  154. for (;;) {
  155. char c = buffer[pos];
  156. if (c == '\0')
  157. finished = true;
  158. // printf("c 0x%hhx state %d arg %d pos %d\n", c, state, arg, pos);
  159. switch (state) {
  160. case INITIAL:
  161. if (isspace(c)) {
  162. state = IN_SPACE;
  163. } else {
  164. state = IN_TOKEN;
  165. args[arg].str = &buffer[pos];
  166. }
  167. break;
  168. case IN_TOKEN:
  169. if (finished) {
  170. arg++;
  171. goto done;
  172. }
  173. if (isspace(c)) {
  174. arg++;
  175. buffer[pos] = 0;
  176. /* are we out of tokens? */
  177. if (arg == arg_count)
  178. goto done;
  179. state = IN_SPACE;
  180. }
  181. pos++;
  182. break;
  183. case IN_SPACE:
  184. if (finished)
  185. goto done;
  186. if (!isspace(c)) {
  187. state = IN_TOKEN;
  188. args[arg].str = &buffer[pos];
  189. }
  190. pos++;
  191. break;
  192. }
  193. }
  194. done:
  195. return arg;
  196. }
  197. static void convert_args(int argc, cmd_args *argv)
  198. {
  199. int i;
  200. for (i = 0; i < argc; i++) {
  201. argv[i].u = atoui(argv[i].str);
  202. argv[i].i = atoi(argv[i].str);
  203. }
  204. }
  205. static int console_loop(void)
  206. {
  207. cmd_args args[16];
  208. char buffer[256];
  209. printf("entering main console loop\n");
  210. for (;;) {
  211. puts("] ");
  212. int len = read_line(buffer, sizeof(buffer));
  213. if (len == 0)
  214. continue;
  215. // printf("line = '%s'\n", buffer);
  216. /* tokenize the line */
  217. int argc = tokenize_command(buffer, args, 16);
  218. if (argc < 0) {
  219. printf("syntax error\n");
  220. continue;
  221. } else if (argc == 0) {
  222. continue;
  223. }
  224. // printf("after tokenize: argc %d\n", argc);
  225. // for (int i = 0; i < argc; i++)
  226. // printf("%d: '%s'\n", i, args[i].str);
  227. /* convert the args */
  228. convert_args(argc, args);
  229. /* try to match the command */
  230. const cmd *command = match_command(args[0].str);
  231. if (!command) {
  232. printf("command not found\n");
  233. continue;
  234. }
  235. int result = command->cmd_callback(argc, args);
  236. printf("command found and result = %d\n",result);
  237. continue;
  238. //return result;
  239. // XXX do something with the result
  240. }
  241. }
  242. void console_start(void)
  243. {
  244. console_loop();
  245. }
  246. int console_run_command(const char *string)
  247. {
  248. const cmd *command;
  249. ASSERT(string != NULL);
  250. command = match_command(string);
  251. if (!command)
  252. return -1;
  253. int result = command->cmd_callback(0, NULL);
  254. return result;
  255. }
  256. void console_register_commands(cmd_block *block)
  257. {
  258. ASSERT(block);
  259. ASSERT(block->next == NULL);
  260. block->next = command_list;
  261. command_list = block;
  262. }
  263. static int cmd_help(int argc, const cmd_args *argv)
  264. {
  265. printf("command list:\n");
  266. cmd_block *block;
  267. size_t i;
  268. for (block = command_list; block != NULL; block = block->next) {
  269. const cmd *curr_cmd = block->list;
  270. for (i = 0; i < block->count; i++) {
  271. printf("\t%-16s: %s\n", curr_cmd[i].cmd_str, curr_cmd[i].help_str ? curr_cmd[i].help_str : "");
  272. }
  273. }
  274. return 0;
  275. }
  276. static int cmd_test(int argc, const cmd_args *argv)
  277. {
  278. int i;
  279. printf("argc %d, argv %p\n", argc, argv);
  280. for (i = 0; i < argc; i++)
  281. printf("\t%d: str '%s', i %d, u %#x\n", i, argv[i].str, argv[i].i, argv[i].u);
  282. return 0;
  283. }