blob: 8869650819a6e5e15707a648c97e77f944b7950d [file] [log] [blame]
adamdunkelsd311bf12004-07-04 20:17:37 +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 2004/07/04 20:17:38 adamdunkels Exp $
32 *
33 */
34
35#include "program-handler.h"
36#include "loader.h"
37#include "uip.h"
38#include "uip_arp.h"
39#include "resolv.h"
40
41#include "shell.h"
42
43#include <string.h>
44
45
46struct ptentry {
47 char c;
48 void (* pfunc)(char *str);
49};
50
51extern char *shell_prompt_text;
52
53/*-----------------------------------------------------------------------------------*/
54static void
55parse(register char *str, struct ptentry *t)
56{
57 register struct ptentry *p;
58 char *sstr;
59
60 sstr = str;
61
62 /* Loop over the parse table entries in t in order to find one that
63 matches the first character in str. */
64 for(p = t; p->c != 0; ++p) {
65 if(*str == p->c) {
66 /* Skip rest of the characters up to the first space. */
67 while(*str != ' ') {
68 ++str;
69 }
70
71 /* Skip all spaces.*/
72 while(*str == ' ') {
73 ++str;
74 }
75
76 /* Call parse table entry function and return. */
77 p->pfunc(str);
78 return;
79 }
80 }
81
82 /* Did not find matching entry in parse table. We just call the
83 default handler supplied by the caller and return. */
84 p->pfunc(str);
85}
86/*-----------------------------------------------------------------------------------*/
87static void
88inttostr(register char *str, unsigned int i)
89{
90 str[0] = '0' + i / 100;
91 if(str[0] == '0') {
92 str[0] = ' ';
93 }
94 str[1] = '0' + (i / 10) % 10;
95 if(str[1] == '0') {
96 str[1] = ' ';
97 }
98 str[2] = '0' + i % 10;
99 str[3] = ' ';
100 str[4] = 0;
101}
102/*-----------------------------------------------------------------------------------*/
103static void
104processes(char *str)
105{
106 static char idstr[5];
107 struct ek_proc *p;
108
109 shell_output("Processes:", "");
110 /* Step through each possible process ID and see if there is a
111 matching process. */
112 for(p = EK_PROCS(); p != NULL; p = p->next) {
113 inttostr(idstr, p->id);
114 shell_output(idstr, p->name);
115 }
116}
117/*-----------------------------------------------------------------------------------*/
118static char *
119nullterminate(char *str)
120{
121 char *nt;
122
123 /* Nullterminate string. Start with finding newline character. */
124 for(nt = str; *nt != '\r' &&
125 *nt != '\n'; ++nt);
126
127 /* Replace newline with a null char. */
128 *nt = 0;
129
130 /* Remove trailing spaces. */
131 while(nt > str && *(nt - 1) == ' ') {
132 *(nt - 1) = 0;
133 --nt;
134 }
135
136 /* Return pointer to null char. */
137 return nt;
138}
139/*-----------------------------------------------------------------------------------*/
140static void
141killproc(char *str)
142{
143 char procnum, j, c;
144 char procstr[5];
145
146 nullterminate(str);
147
148 procnum = 0;
149
150 for(j = 0; j < 4; ++j) {
151 c = str[(int)j];
152 if(c >= '0' && c <= '9') {
153 procnum = procnum * 10 + (str[(int)j] - '0');
154 } else {
155 break;
156 }
157 }
158 if(procnum != 0) {
159 inttostr(procstr, procnum);
160 shell_output("Killing process ", procstr);
161 ek_post(procnum, EK_EVENT_REQUEST_EXIT, NULL);
162 } else {
163 shell_output("Could not parse process number", "");
164 }
165
166}
167/*-----------------------------------------------------------------------------------*/
168static void
169help(char *str)
170{
171 shell_output("Available commands:", "");
172 shell_output("ps - show processes", "");
173 shell_output("kill - kill process", "");
174 shell_output("quit - quit shell", "");
175 shell_output("? - show this help", "");
176}
177/*-----------------------------------------------------------------------------------*/
178static void
179none(char *str)
180{
181}
182/*-----------------------------------------------------------------------------------*/
183static struct ptentry configparsetab[] =
184 {{'k', killproc},
185 {'p', processes},
186 {'q', shell_quit},
187 {'?', help},
188
189 /* Default action */
190 {0, none}};
191/*-----------------------------------------------------------------------------------*/
192void
193shell_start(void)
194{
195 shell_output("Contiki command shell", "");
196 shell_output("Type '?' for help", "");
197 shell_prompt(shell_prompt_text);
198}
199/*-----------------------------------------------------------------------------------*/
200void
201shell_input(char *cmd)
202{
203 parse(cmd, configparsetab);
204 shell_prompt(shell_prompt_text);
205}
206/*-----------------------------------------------------------------------------------*/
207void
208shell_idle(void)
209{
210}
211/*-----------------------------------------------------------------------------------*/
212void
213shell_init(void)
214{
215}
216/*-----------------------------------------------------------------------------------*/