-
Notifications
You must be signed in to change notification settings - Fork 994
/
shell.cpp
280 lines (254 loc) · 8.32 KB
/
shell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack.
*
* $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
*
*/
#include "stdlib.h"
#include "shell.h"
#include "uip.h"
#include <string.h>
#include "checksumm.h"
#include "utils.h"
#include "stdio.h"
#include "stdlib.h"
#include "telnetd.h"
#include "CallbackStream.h"
#include "Kernel.h"
//#define DEBUG_PRINTF(...)
#define DEBUG_PRINTF printf
struct ptentry {
uint16_t command_cs;
void (* pfunc)(char *str, Shell *sh);
};
#define SHELL_PROMPT "> "
/*---------------------------------------------------------------------------*/
bool Shell::parse(register char *str, struct ptentry *t)
{
struct ptentry *p;
for (p = t; p->command_cs != 0; ++p) {
if (get_checksum(str) == p->command_cs) {
break;
}
}
p->pfunc(str, this);
return p->command_cs != 0;
}
/*---------------------------------------------------------------------------*/
static void help(char *str, Shell *sh)
{
sh->output("Available commands: All others are passed on\n");
sh->output("netstat - show network info\n");
sh->output("? - show network help\n");
sh->output("help - show command help\n");
sh->output("exit, quit - exit shell\n");
}
/*---------------------------------------------------------------------------*/
static const char *states[] = {
"CLOSED",
"SYN_RCVD",
"SYN_SENT",
"ESTABLISHED",
"FIN_WAIT_1",
"FIN_WAIT_2",
"CLOSING",
"TIME_WAIT",
"LAST_ACK",
"NONE",
"RUNNING",
"CALLED"
};
static void connections(char *str, Shell *sh)
{
char istr[128];
struct uip_conn *connr;
snprintf(istr, sizeof(istr), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
sh->output(istr);
sh->output("Current connections: \n");
for (connr = &uip_conns[0]; connr <= &uip_conns[UIP_CONNS - 1]; ++connr) {
if(connr->tcpstateflags != UIP_CLOSED) {
snprintf(istr, sizeof(istr), "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c\n",
HTONS(connr->lport),
uip_ipaddr1(connr->ripaddr), uip_ipaddr2(connr->ripaddr), uip_ipaddr3(connr->ripaddr), uip_ipaddr4(connr->ripaddr),
HTONS(connr->rport),
states[connr->tcpstateflags & UIP_TS_MASK],
connr->nrtx,
connr->timer,
(uip_outstanding(connr)) ? '*' : ' ',
(uip_stopped(connr)) ? '!' : ' ');
sh->output(istr);
}
}
}
static void quit(char *str, Shell *sh)
{
sh->close();
}
//#include "clock.h"
static void test(char *str, Shell *sh)
{
printf("In Test\n");
// struct timer t;
// u16_t ticks= CLOCK_SECOND*5;
// timer_set(&t, ticks);
// printf("Wait....\n");
// while(!timer_expired(&t)) {
// }
// printf("Done\n");
/*
const char *fn= "/sd/test6.txt";
uint16_t *buf= (uint16_t *)malloc(200*2);
int cnt= 0;
FILE *fp;
for(int i=0;i<10;i++) {
fp= fopen(fn, i == 0 ? "w" : "a");
if(fp == NULL) {
printf("failed to open file\n");
return;
}
for (int x = 0; x < 200; ++x) {
buf[x]= x+cnt;
}
cnt+=200;
int n= fwrite(buf, 2, 200, fp);
printf("wrote %d, %d\n", i, n);
fclose(fp);
}
fp= fopen(fn, "r");
if(fp == NULL) {
printf("failed to open file for read\n");
return;
}
printf("Opened file %s for read\n", fn);
do {
int n= fread(buf, 2, 200, fp);
if(n <= 0) break;
for(int x=0;x<n;x++) {
printf("%04X, ", buf[x]);
}
}while(1);
fclose(fp);
free(buf);
*/
}
/*---------------------------------------------------------------------------*/
static void unknown(char *str, Shell *sh)
{
// its some other command, so queue it for mainloop to find
if (strlen(str) > 0) {
CommandQueue::getInstance()->add(str, sh->getStream());
}
}
/*---------------------------------------------------------------------------*/
static struct ptentry parsetab[] = {
{CHECKSUM("netstat"), connections},
{CHECKSUM("exit"), quit},
{CHECKSUM("quit"), quit},
{CHECKSUM("test"), test},
{CHECKSUM("?"), help},
/* Default action */
{0, unknown}
};
/*---------------------------------------------------------------------------*/
// this callback gets the results of a command, line by line
// NULL means command completed
// static
int Shell::command_result(const char *str, void *p)
{
// FIXME problem when shell is deleted and this gets called from slow command
// need a way to know this shell was closed or deleted
Shell *sh = (Shell *)p;
if (str == NULL) {
// indicates command is complete
// only prompt when command is completed
sh->telnet->output_prompt(SHELL_PROMPT);
return 0;
} else {
if (sh->telnet->can_output()) {
if (sh->telnet->output(str) == -1) return -1; // connection was closed
return 1;
}
// we are stalled
return 0;
}
}
/*---------------------------------------------------------------------------*/
void Shell::start()
{
telnet->output("Smoothie command shell\r\n> ");
}
int Shell::queue_size()
{
return CommandQueue::getInstance()->size();
}
/*---------------------------------------------------------------------------*/
void Shell::input(char *cmd)
{
if (parse(cmd, parsetab)) {
telnet->output_prompt(SHELL_PROMPT);
}
}
/*---------------------------------------------------------------------------*/
int Shell::output(const char *str)
{
return telnet->output(str);
}
void Shell::close()
{
telnet->close();
}
void Shell::setConsole()
{
// add it to the kernels output stream if we are a console
// TODO do we do this for all connections? so pronterface will get file done when playing from M24?
// then we need to turn it off for the streaming app
DEBUG_PRINTF("Shell: Adding stream to kernel streams\n");
THEKERNEL->streams->append_stream(pstream);
isConsole= true;
}
Shell::Shell(Telnetd *telnet)
{
DEBUG_PRINTF("Shell: ctor %p - %p\n", this, telnet);
this->telnet= telnet;
// create a callback StreamOutput for this connection
pstream = new CallbackStream(command_result, this);
isConsole= false;
}
Shell::~Shell()
{
if(isConsole) {
DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
THEKERNEL->streams->remove_stream(pstream);
}
// we cannot delete this stream until it is no longer in any command queue entries
// so mark it as closed, and allow it to delete itself when it is no longer being used
static_cast<CallbackStream*>(pstream)->mark_closed(); // mark the stream as closed so we do not get any callbacks
DEBUG_PRINTF("Shell: dtor %p\n", this);
}