xref: /nrf52832-nimble/rt-thread/components/net/uip/apps/telnetd/uip_shell.c (revision 104654410c56c573564690304ae786df310c91fc)
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 
35 #include "uip_shell.h"
36 
37 #include <string.h>
38 
39 struct ptentry {
40   char *commandstr;
41   void (* pfunc)(char *str);
42 };
43 
44 #define SHELL_PROMPT "uIP 1.0> "
45 
46 /*---------------------------------------------------------------------------*/
47 static void
parse(register char * str,struct ptentry * t)48 parse(register char *str, struct ptentry *t)
49 {
50   struct ptentry *p;
51   for(p = t; p->commandstr != NULL; ++p) {
52     if(strncmp(p->commandstr, str, strlen(p->commandstr)) == 0) {
53       break;
54     }
55   }
56 
57   p->pfunc(str);
58 }
59 /*---------------------------------------------------------------------------*/
60 static void
inttostr(register char * str,unsigned int i)61 inttostr(register char *str, unsigned int i)
62 {
63   str[0] = '0' + i / 100;
64   if(str[0] == '0') {
65     str[0] = ' ';
66   }
67   str[1] = '0' + (i / 10) % 10;
68   if(str[0] == ' ' && str[1] == '0') {
69     str[1] = ' ';
70   }
71   str[2] = '0' + i % 10;
72   str[3] = ' ';
73   str[4] = 0;
74 }
75 /*---------------------------------------------------------------------------*/
76 static void
help(char * str)77 help(char *str)
78 {
79   shell_output("Available commands:", "");
80   shell_output("stats   - show network statistics", "");
81   shell_output("conn    - show TCP connections", "");
82   shell_output("help, ? - show help", "");
83   shell_output("exit    - exit shell", "");
84 }
85 /*---------------------------------------------------------------------------*/
86 static void
unknown(char * str)87 unknown(char *str)
88 {
89   if(strlen(str) > 0) {
90     shell_output("Unknown command: ", str);
91   }
92 }
93 /*---------------------------------------------------------------------------*/
94 static struct ptentry parsetab[] =
95   {{"stats", help},
96    {"conn", help},
97    {"help", help},
98    {"exit", shell_quit},
99    {"?", help},
100 
101    /* Default action */
102    {NULL, unknown}};
103 /*---------------------------------------------------------------------------*/
104 void
shell_init(void)105 shell_init(void)
106 {
107 }
108 /*---------------------------------------------------------------------------*/
109 void
shell_start(void)110 shell_start(void)
111 {
112   shell_output("uIP command shell", "");
113   shell_output("Type '?' and return for help", "");
114   shell_prompt(SHELL_PROMPT);
115 }
116 /*---------------------------------------------------------------------------*/
117 void
shell_input(char * cmd)118 shell_input(char *cmd)
119 {
120   parse(cmd, parsetab);
121   shell_prompt(SHELL_PROMPT);
122 }
123 /*---------------------------------------------------------------------------*/
124