46 #include "contiki-lib.h"
56 #ifdef SHELL_CONF_PROMPT
57 extern char shell_prompt_text[];
59 static char shell_prompt_text[] =
"Contiki> ";
62 #ifdef SHELL_CONF_BANNER
63 extern char shell_banner_text[];
65 static char shell_banner_text[] =
"Contiki command shell";
72 static struct process *front_process;
74 static unsigned long time_offset;
76 PROCESS(shell_process,
"Shell");
77 PROCESS(shell_server_process,
"Shell server");
79 PROCESS(help_command_process,
"help");
81 &help_command_process);
83 &help_command_process);
84 PROCESS(shell_killall_process,
"killall");
85 SHELL_COMMAND(killall_command,
"killall",
"killall: stop all running commands",
86 &shell_killall_process);
87 PROCESS(shell_kill_process,
"kill");
88 SHELL_COMMAND(kill_command,
"kill",
"kill <command>: stop a specific command",
90 PROCESS(shell_null_process,
"null");
93 PROCESS(shell_exit_process,
"exit");
107 if(input->len1 + input->len2 == 0) {
153 if(name ==
NULL || strlen(name) == 0) {
155 "kill <command>: command name must be given",
"");
161 if(strcmp(name, c->command) == 0 &&
162 c != &kill_command &&
199 replace_braces(
char *commandline)
204 for(ptr = commandline; *ptr != 0; ++ptr) {
210 }
else if(*ptr ==
'}') {
220 find_pipe(
char *commandline)
225 for(ptr = commandline; *ptr != 0; ++ptr) {
228 }
else if(*ptr ==
'}') {
230 }
else if(*ptr ==
'|') {
240 start_command(
char *commandline,
struct shell_command *child)
247 while(*commandline ==
' ') {
252 next = find_pipe(commandline);
255 child = start_command(next + 1, child);
259 replace_braces(commandline);
260 args = strchr(commandline,
' ');
266 command_len = (int)strlen(commandline);
267 while(command_len > 0 && commandline[command_len - 1] ==
' ') {
268 commandline[command_len - 1] = 0;
273 command_len = (int)strlen(commandline);
274 args = &commandline[command_len];
276 command_len = (int)(args - commandline - 1);
285 !(strncmp(c->command, commandline, command_len) == 0 &&
286 c->command[command_len] == 0);
310 struct process **started_process)
315 if(commandline_len == 0) {
316 if(started_process !=
NULL) {
317 *started_process =
NULL;
319 return SHELL_NOTHING;
322 if(commandline[commandline_len - 1] ==
'&') {
323 commandline[commandline_len - 1] = 0;
328 c = start_command(commandline, child);
332 if(c !=
NULL && started_process !=
NULL) {
333 *started_process = c->process;
335 return SHELL_BACKGROUND;
337 return SHELL_FOREGROUND;
340 return SHELL_NOTHING;
345 char *data1,
int len1,
346 const char *data2,
int len2)
365 if(commandline[0] ==
'~' &&
366 commandline[1] ==
'K') {
368 if(front_process != &shell_process) {
373 input.data1 = commandline;
374 input.len1 = commandline_len;
386 input_to_child_command(c->child, text1, (
int)strlen(text1),
387 text2, (
int)strlen(text2));
390 text2, (
int)strlen(text2));
396 void *data1,
int len1,
397 const void *data2,
int len2)
400 input_to_child_command(c->child, data1, len1, data2, len2);
420 strcmp(i->command, c->command) < 0;
426 }
else if(i ==
NULL) {
435 static struct process *started_process;
452 if(started_process !=
NULL &&
453 ret == SHELL_FOREGROUND &&
455 front_process = started_process;
457 data == started_process);
459 front_process = &shell_process;
476 if(ev == PROCESS_EVENT_EXITED) {
481 c !=
NULL && c->process != p;
484 if(c->child !=
NULL && c->child->process !=
NULL) {
486 input_to_child_command(c->child,
"", 0,
"", 0);
491 }
else if(ev == PROCESS_EVENT_TIMER) {
493 shell_set_time(shell_time());
517 front_process = &shell_process;
524 unsigned long num = 0;
525 const char *strptr = str;
531 while(*strptr ==
' ') {
535 for(i = 0; i < 10 && isdigit(strptr[i]); ++i) {
536 num = num * 10 + strptr[i] -
'0';
542 *retstr = strptr + i;
556 shell_set_time(
unsigned long seconds)
void shell_prompt(char *str)
Print a prompt.
int process_is_running(struct process *p)
Check if a process is running.
void shell_stop(void)
Stop the shell.
void shell_init(void)
Initialize the shell.
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
int shell_start_command(char *commandline, int commandline_len, struct shell_command *child, struct process **started_process)
Start a shell command from another shell command.
void list_push(list_t list, void *item)
Add an item to the start of the list.
#define PROCESS_EXIT()
Exit the currently running process.
#define PROCESS_BEGIN()
Define the beginning of a process.
void process_post_synch(struct process *p, process_event_t ev, process_data_t data)
Post a synchronous event to a process.
Main header file for the Contiki shell
#define NULL
The null pointer.
void shell_input(char *commandline, int commandline_len)
Send a line of input to the shell.
void shell_quit(void)
Quit the shell.
process_event_t process_alloc_event(void)
Allocate a global event number.
void shell_start(void)
Start the shell.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Header file for the Rime stack
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
void list_insert(list_t list, void *previtem, void *newitem)
Insert an item after a specified item on the list.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
void list_init(list_t list)
Initialize a list.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Structure for shell input data.
void shell_register_command(struct shell_command *c)
Register a command with the shell.
#define PROCESS_PAUSE()
Yield the process for a short while.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
void list_add(list_t list, void *item)
Add an item at the end of a list.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS(name, strname)
Declare a process.
#define LIST(name)
Declare a linked list.
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
int shell_event_input
The event number for shell input data.
void shell_unregister_command(struct shell_command *c)
Unregister a previously registered shell command.
void process_start(struct process *p, process_data_t data)
Start a process.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Holds a information about a shell command.
void shell_default_output(const char *text1, int len1, const char *text2, int len2)
Print a line of output from the shell.
void shell_exit(void)
Request shell exit.
void process_exit(struct process *p)
Cause a process to exit.
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
#define CLOCK_SECOND
A second, measured in system clock time.