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