blob: 2b8adb44dd5352f3a7c06c1c7949bb10e4b9ff65 [file] [log] [blame]
oliverschmidta47cab62005-04-18 23:13:40 +00001/*
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 Contiki desktop OS.
30 *
oliverschmidt58d4eb22005-05-08 10:39:08 +000031 * $Id: shell.c,v 1.3 2005/05/08 10:39:08 oliverschmidt Exp $
oliverschmidta47cab62005-04-18 23:13:40 +000032 *
33 */
34
35#include "program-handler.h"
36#include "loader.h"
37#include "cfs.h"
38#include "uip.h"
39#include "uip_arp.h"
40#include "resolv.h"
41
42#include "shell.h"
43
44#include <string.h>
45
46static char showingdir = 0;
47static struct cfs_dir dir;
48static unsigned int totsize;
49
50struct ptentry {
oliverschmidt616fa122005-04-19 23:01:13 +000051 char c1;
52 char c2;
oliverschmidta47cab62005-04-18 23:13:40 +000053 void (* pfunc)(char *str);
54};
55
56/*-----------------------------------------------------------------------------------*/
57static void
58parse(register char *str, struct ptentry *t)
59{
60 register struct ptentry *p;
61 char *sstr;
62
63 sstr = str;
64
65 /* Loop over the parse table entries in t in order to find one that
66 matches the first character in str. */
oliverschmidt616fa122005-04-19 23:01:13 +000067 for(p = t; p->c1 != 0; ++p) {
68 if(*str == p->c1 || *str == p->c2) {
oliverschmidta47cab62005-04-18 23:13:40 +000069 /* Skip rest of the characters up to the first space. */
70 while(*str != ' ') {
71 ++str;
72 }
73
74 /* Skip all spaces.*/
75 while(*str == ' ') {
76 ++str;
77 }
78
79 /* Call parse table entry function and return. */
80 p->pfunc(str);
81 return;
82 }
83 }
84
85 /* Did not find matching entry in parse table. We just call the
86 default handler supplied by the caller and return. */
87 p->pfunc(str);
88}
89/*-----------------------------------------------------------------------------------*/
90static void
91inttostr(register char *str, unsigned int i)
92{
93 str[0] = '0' + i / 100;
94 if(str[0] == '0') {
95 str[0] = ' ';
96 }
97 str[1] = '0' + (i / 10) % 10;
98 if(str[0] == ' ' && str[1] == '0') {
99 str[1] = ' ';
100 }
101 str[2] = '0' + i % 10;
102 str[3] = ' ';
103 str[4] = 0;
104}
105/*-----------------------------------------------------------------------------------*/
106static void
107processes(char *str)
108{
109 static char idstr[5];
110 struct ek_proc *p;
111
112 shell_output("Processes:", "");
113 /* Step through each possible process ID and see if there is a
114 matching process. */
115 for(p = EK_PROCS(); p != NULL; p = p->next) {
116 inttostr(idstr, p->id);
117 shell_output(idstr, (char *)p->name);
118 }
119}
120/*-----------------------------------------------------------------------------------*/
121static char *
122nullterminate(char *str)
123{
124 char *nt;
125
126 /* Nullterminate string. Start with finding newline character. */
127 for(nt = str; *nt != '\r' &&
128 *nt != '\n'; ++nt);
129
130 /* Replace newline with a null char. */
131 *nt = 0;
132
133 /* Remove trailing spaces. */
134 while(nt > str && *(nt - 1) == ' ') {
135 *(nt - 1) = 0;
136 --nt;
137 }
138
139 /* Return pointer to null char. */
140 return nt;
141}
142/*-----------------------------------------------------------------------------------*/
143static void
144runfile(char *str)
145{
146 nullterminate(str);
147
148 if(strlen(str) > 0) {
149 /* Call loader function. */
150 program_handler_load(str, NULL);
151 shell_output("Starting program ", str);
152 } else {
153 shell_output("Must supply a program name", "");
154 }
155}
156/*-----------------------------------------------------------------------------------*/
157static void
158execfile(char *str)
159{
160 runfile(str);
161 shell_quit(NULL);
162}
163/*-----------------------------------------------------------------------------------*/
164static void
165killproc(char *str)
166{
167 char procnum, j, c;
168 char procstr[5];
169
170 nullterminate(str);
171
172 procnum = 0;
173
174 for(j = 0; j < 4; ++j) {
175 c = str[j];
176 if(c >= '0' && c <= '9') {
177 procnum = procnum * 10 + (str[j] - '0');
178 } else {
179 break;
180 }
181 }
182 if(procnum != 0) {
183 inttostr(procstr, procnum);
184 shell_output("Killing process ", procstr);
185 ek_post(procnum, EK_EVENT_REQUEST_EXIT, NULL);
186 } else {
187 shell_output("Invalid process number", "");
188 }
189
190}
191/*-----------------------------------------------------------------------------------*/
192static void
193help(char *str)
194{
195 shell_output("Available commands:", "");
196 shell_output("run - start program", "");
197 shell_output("exec - start program & exit shell", "");
198 shell_output("ps - show processes", "");
199 shell_output("kill - kill process", "");
200 shell_output("ls - display directory", "");
201 shell_output("quit - quit shell", "");
202 shell_output("? - show this help", "");
203}
204/*-----------------------------------------------------------------------------------*/
205static void
206directory(char *str)
207{
oliverschmidt58d4eb22005-05-08 10:39:08 +0000208 if(cfs_opendir(&dir, ".") != 0) {
oliverschmidta47cab62005-04-18 23:13:40 +0000209 shell_output("Cannot open directory", "");
210 showingdir = 0;
211 } else {
212 shell_output("Disk directory:", "");
213 showingdir = 1;
214 totsize = 0;
215 ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, NULL);
216 }
217
218}
219/*-----------------------------------------------------------------------------------*/
220static void
221none(char *str)
222{
223}
224/*-----------------------------------------------------------------------------------*/
225static struct ptentry configparsetab[] =
oliverschmidt616fa122005-04-19 23:01:13 +0000226 {{'e', 'E', execfile},
227 {'r', 'R', runfile},
228 {'k', 'K', killproc},
229 {'p', 'P', processes},
230 {'l', 'L', directory},
231 {'q', 'Q', shell_quit},
232 {'h', '?', help},
oliverschmidta47cab62005-04-18 23:13:40 +0000233
234 /* Default action */
oliverschmidt616fa122005-04-19 23:01:13 +0000235 {0, 0, none}};
oliverschmidta47cab62005-04-18 23:13:40 +0000236/*-----------------------------------------------------------------------------------*/
237void
238shell_init(void)
239{
240}
241/*-----------------------------------------------------------------------------------*/
242void
243shell_start(void)
244{
245 showingdir = 0;
246 shell_output("Contiki command shell", "");
247 shell_output("Type '?' and return for help", "");
248 shell_prompt("contiki> ");
249}
250/*-----------------------------------------------------------------------------------*/
251void
252shell_input(char *cmd)
253{
254 if(showingdir != 0) {
255 showingdir = 0;
256 shell_output("Directory stopped", "");
257 cfs_closedir(&dir);
258 }
259 parse(cmd, configparsetab);
260 if(showingdir == 0) {
261 shell_prompt("contiki> ");
262 }
263}
264/*-----------------------------------------------------------------------------------*/
265void
266shell_eventhandler(ek_event_t ev, ek_data_t data)
267{
268 static struct cfs_dirent dirent;
269 static char size[10];
270
271 if(ev == EK_EVENT_CONTINUE) {
272 if(showingdir != 0) {
273 if(cfs_readdir(&dir, &dirent) != 0) {
274 cfs_closedir(&dir);
275 showingdir = 0;
276 inttostr(size, totsize);
277 shell_output("Total number of blocks: ", size);
278 shell_prompt("contiki> ");
279 } else {
280 totsize += dirent.size;
281 inttostr(size, dirent.size);
282 shell_output(size, dirent.name);
283 ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, NULL);
284 }
285 }
286 }
287}
288/*-----------------------------------------------------------------------------------*/