uip_shell.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2003, Adam Dunkels.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote
  14. * products derived from this software without specific prior
  15. * written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  18. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  23. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * This file is part of the uIP TCP/IP stack.
  30. *
  31. * $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
  32. *
  33. */
  34. #include "uip_shell.h"
  35. #include <string.h>
  36. struct ptentry {
  37. char *commandstr;
  38. void (* pfunc)(char *str);
  39. };
  40. #define SHELL_PROMPT "uIP 1.0> "
  41. /*---------------------------------------------------------------------------*/
  42. static void
  43. parse(register char *str, struct ptentry *t)
  44. {
  45. struct ptentry *p;
  46. for(p = t; p->commandstr != NULL; ++p) {
  47. if(strncmp(p->commandstr, str, strlen(p->commandstr)) == 0) {
  48. break;
  49. }
  50. }
  51. p->pfunc(str);
  52. }
  53. /*---------------------------------------------------------------------------*/
  54. static void
  55. inttostr(register char *str, unsigned int i)
  56. {
  57. str[0] = '0' + i / 100;
  58. if(str[0] == '0') {
  59. str[0] = ' ';
  60. }
  61. str[1] = '0' + (i / 10) % 10;
  62. if(str[0] == ' ' && str[1] == '0') {
  63. str[1] = ' ';
  64. }
  65. str[2] = '0' + i % 10;
  66. str[3] = ' ';
  67. str[4] = 0;
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. static void
  71. help(char *str)
  72. {
  73. shell_output("Available commands:", "");
  74. shell_output("stats - show network statistics", "");
  75. shell_output("conn - show TCP connections", "");
  76. shell_output("help, ? - show help", "");
  77. shell_output("exit - exit shell", "");
  78. }
  79. /*---------------------------------------------------------------------------*/
  80. static void
  81. unknown(char *str)
  82. {
  83. if(strlen(str) > 0) {
  84. shell_output("Unknown command: ", str);
  85. }
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. static struct ptentry parsetab[] =
  89. {{"stats", help},
  90. {"conn", help},
  91. {"help", help},
  92. {"exit", shell_quit},
  93. {"?", help},
  94. /* Default action */
  95. {NULL, unknown}};
  96. /*---------------------------------------------------------------------------*/
  97. void
  98. shell_init(void)
  99. {
  100. }
  101. /*---------------------------------------------------------------------------*/
  102. void
  103. shell_start(void)
  104. {
  105. shell_output("uIP command shell", "");
  106. shell_output("Type '?' and return for help", "");
  107. shell_prompt(SHELL_PROMPT);
  108. }
  109. /*---------------------------------------------------------------------------*/
  110. void
  111. shell_input(char *cmd)
  112. {
  113. parse(cmd, parsetab);
  114. shell_prompt(SHELL_PROMPT);
  115. }
  116. /*---------------------------------------------------------------------------*/