2008-02-05 00:42:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2008-02-05 00:42:17 +01:00
|
|
|
* 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. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2008-02-05 00:42:17 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2008-02-05 00:42:17 +01:00
|
|
|
* This file is part of the Contiki operating system.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2008-02-05 00:42:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2009-03-02 21:44:15 +01:00
|
|
|
* The shell application
|
2008-02-05 00:42:17 +01:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
2006-06-18 00:41:10 +02:00
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup shell
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-lib.h"
|
|
|
|
|
2013-12-12 20:50:07 +01:00
|
|
|
#include "net/rime/rime.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
#include <ctype.h>
|
2006-06-18 00:41:10 +02:00
|
|
|
#include <string.h>
|
2008-02-05 00:42:17 +01:00
|
|
|
#include <stdio.h>
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2011-05-21 11:48:50 +02:00
|
|
|
#ifdef SHELL_CONF_PROMPT
|
2011-05-21 20:52:00 +02:00
|
|
|
extern char shell_prompt_text[];
|
2011-05-21 11:48:50 +02:00
|
|
|
#else
|
2011-05-21 20:52:00 +02:00
|
|
|
static char shell_prompt_text[] = "Contiki> ";
|
2011-04-16 21:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
2011-05-21 11:48:50 +02:00
|
|
|
#ifdef SHELL_CONF_BANNER
|
2011-05-21 20:52:00 +02:00
|
|
|
extern char shell_banner_text[];
|
2011-05-21 11:48:50 +02:00
|
|
|
#else
|
2011-05-21 20:52:00 +02:00
|
|
|
static char shell_banner_text[] = "Contiki command shell";
|
2011-04-16 21:10:22 +02:00
|
|
|
#endif
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
LIST(commands);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
int shell_event_input;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
static struct process *front_process;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
static unsigned long time_offset;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS(shell_process, "Shell");
|
|
|
|
PROCESS(shell_server_process, "Shell server");
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(help_command_process, "help");
|
|
|
|
SHELL_COMMAND(help_command, "help", "help: shows this help",
|
|
|
|
&help_command_process);
|
|
|
|
SHELL_COMMAND(question_command, "?", "?: shows this help",
|
|
|
|
&help_command_process);
|
|
|
|
PROCESS(shell_killall_process, "killall");
|
|
|
|
SHELL_COMMAND(killall_command, "killall", "killall: stop all running commands",
|
|
|
|
&shell_killall_process);
|
|
|
|
PROCESS(shell_kill_process, "kill");
|
|
|
|
SHELL_COMMAND(kill_command, "kill", "kill <command>: stop a specific command",
|
|
|
|
&shell_kill_process);
|
|
|
|
PROCESS(shell_null_process, "null");
|
2011-04-16 21:10:22 +02:00
|
|
|
SHELL_COMMAND(null_command, "null", "null: discard input",
|
2008-02-05 00:42:17 +01:00
|
|
|
&shell_null_process);
|
2011-04-16 21:10:22 +02:00
|
|
|
PROCESS(shell_exit_process, "exit");
|
|
|
|
SHELL_COMMAND(exit_command, "exit", "exit: exit shell",
|
|
|
|
&shell_exit_process);
|
|
|
|
SHELL_COMMAND(quit_command, "quit", "quit: exit shell",
|
|
|
|
&shell_exit_process);
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_null_process, ev, data)
|
|
|
|
{
|
|
|
|
struct shell_input *input;
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
|
|
|
|
input = data;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
if(input->len1 + input->len2 == 0) {
|
|
|
|
PROCESS_EXIT();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_END();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2008-02-05 00:42:17 +01:00
|
|
|
command_kill(struct shell_command *c)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
if(c != NULL) {
|
|
|
|
shell_output_str(&killall_command, "Stopping command ", c->command);
|
|
|
|
process_exit(c->process);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-29 00:29:07 +01:00
|
|
|
static void
|
|
|
|
killall(void)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_command *c;
|
|
|
|
for(c = list_head(commands);
|
|
|
|
c != NULL;
|
|
|
|
c = c->next) {
|
|
|
|
if(c != &killall_command && process_is_running(c->process)) {
|
|
|
|
command_kill(c);
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-29 00:29:07 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_killall_process, ev, data)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
killall();
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
PROCESS_END();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_kill_process, ev, data)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_command *c;
|
|
|
|
char *name;
|
|
|
|
PROCESS_BEGIN();
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
name = data;
|
|
|
|
if(name == NULL || strlen(name) == 0) {
|
|
|
|
shell_output_str(&kill_command,
|
|
|
|
"kill <command>: command name must be given", "");
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
for(c = list_head(commands);
|
|
|
|
c != NULL;
|
|
|
|
c = c->next) {
|
|
|
|
if(strcmp(name, c->command) == 0 &&
|
|
|
|
c != &kill_command &&
|
|
|
|
process_is_running(c->process)) {
|
|
|
|
command_kill(c);
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
shell_output_str(&kill_command, "Command not found: ", name);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_END();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(help_command_process, ev, data)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_command *c;
|
|
|
|
PROCESS_BEGIN();
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_output_str(&help_command, "Available commands:", "");
|
|
|
|
for(c = list_head(commands);
|
|
|
|
c != NULL;
|
|
|
|
c = c->next) {
|
|
|
|
shell_output_str(&help_command, c->description, "");
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2011-04-16 21:10:22 +02:00
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_exit_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
shell_exit();
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_END();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2008-02-05 00:42:17 +01:00
|
|
|
replace_braces(char *commandline)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
char *ptr;
|
|
|
|
int level = 0;
|
|
|
|
|
|
|
|
for(ptr = commandline; *ptr != 0; ++ptr) {
|
|
|
|
if(*ptr == '{') {
|
|
|
|
if(level == 0) {
|
|
|
|
*ptr = ' ';
|
|
|
|
}
|
|
|
|
++level;
|
|
|
|
} else if(*ptr == '}') {
|
|
|
|
--level;
|
|
|
|
if(level == 0) {
|
|
|
|
*ptr = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static char *
|
|
|
|
find_pipe(char *commandline)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
char *ptr;
|
|
|
|
int level = 0;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
for(ptr = commandline; *ptr != 0; ++ptr) {
|
|
|
|
if(*ptr == '{') {
|
|
|
|
++level;
|
|
|
|
} else if(*ptr == '}') {
|
|
|
|
--level;
|
|
|
|
} else if(*ptr == '|') {
|
|
|
|
if(level == 0) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static struct shell_command *
|
|
|
|
start_command(char *commandline, struct shell_command *child)
|
|
|
|
{
|
|
|
|
char *next, *args;
|
|
|
|
int command_len;
|
|
|
|
struct shell_command *c;
|
|
|
|
|
2008-07-03 23:15:12 +02:00
|
|
|
/* Shave off any leading spaces. */
|
2008-02-05 00:42:17 +01:00
|
|
|
while(*commandline == ' ') {
|
|
|
|
commandline++;
|
|
|
|
}
|
2008-07-03 23:15:12 +02:00
|
|
|
|
|
|
|
/* Find the next command in a pipeline and start it. */
|
2008-02-05 00:42:17 +01:00
|
|
|
next = find_pipe(commandline);
|
|
|
|
if(next != NULL) {
|
|
|
|
*next = 0;
|
|
|
|
child = start_command(next + 1, child);
|
|
|
|
}
|
|
|
|
|
2008-07-03 23:15:12 +02:00
|
|
|
/* Separate the command arguments, and remove braces. */
|
2008-02-05 00:42:17 +01:00
|
|
|
replace_braces(commandline);
|
|
|
|
args = strchr(commandline, ' ');
|
|
|
|
if(args != NULL) {
|
|
|
|
args++;
|
|
|
|
}
|
|
|
|
|
2008-07-03 23:15:12 +02:00
|
|
|
/* Shave off any trailing spaces. */
|
|
|
|
command_len = (int)strlen(commandline);
|
|
|
|
while(command_len > 0 && commandline[command_len - 1] == ' ') {
|
|
|
|
commandline[command_len - 1] = 0;
|
|
|
|
command_len--;
|
|
|
|
}
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
if(args == NULL) {
|
2008-02-10 13:24:43 +01:00
|
|
|
command_len = (int)strlen(commandline);
|
2008-02-05 00:42:17 +01:00
|
|
|
args = &commandline[command_len];
|
|
|
|
} else {
|
2008-02-10 13:24:43 +01:00
|
|
|
command_len = (int)(args - commandline - 1);
|
2008-02-05 00:42:17 +01:00
|
|
|
}
|
|
|
|
|
2006-08-31 00:06:18 +02:00
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
/* Go through list of commands to find a match for the first word in
|
|
|
|
the command line. */
|
|
|
|
for(c = list_head(commands);
|
|
|
|
c != NULL &&
|
|
|
|
!(strncmp(c->command, commandline, command_len) == 0 &&
|
|
|
|
c->command[command_len] == 0);
|
|
|
|
c = c->next);
|
|
|
|
|
|
|
|
if(c == NULL) {
|
|
|
|
shell_output_str(NULL, commandline, ": command not found (try 'help')");
|
|
|
|
command_kill(child);
|
|
|
|
c = NULL;
|
|
|
|
} else if(process_is_running(c->process) || child == c) {
|
|
|
|
shell_output_str(NULL, commandline, ": command already running");
|
|
|
|
c->child = NULL;
|
|
|
|
c = NULL;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else {
|
2008-02-05 00:42:17 +01:00
|
|
|
c->child = child;
|
|
|
|
/* printf("shell: start_command starting '%s'\n", c->process->name);*/
|
|
|
|
/* Start a new process for the command. */
|
2014-06-12 13:40:28 +02:00
|
|
|
process_start(c->process, (void *)args);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
return c;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
shell_start_command(char *commandline, int commandline_len,
|
|
|
|
struct shell_command *child,
|
|
|
|
struct process **started_process)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_command *c;
|
|
|
|
int background = 0;
|
|
|
|
|
|
|
|
if(commandline_len == 0) {
|
|
|
|
if(started_process != NULL) {
|
|
|
|
*started_process = NULL;
|
|
|
|
}
|
|
|
|
return SHELL_NOTHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(commandline[commandline_len - 1] == '&') {
|
|
|
|
commandline[commandline_len - 1] = 0;
|
|
|
|
background = 1;
|
|
|
|
commandline_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = start_command(commandline, child);
|
|
|
|
|
|
|
|
/* Return a pointer to the started process, so that the caller can
|
|
|
|
wait for the process to complete. */
|
|
|
|
if(c != NULL && started_process != NULL) {
|
|
|
|
*started_process = c->process;
|
|
|
|
if(background) {
|
|
|
|
return SHELL_BACKGROUND;
|
|
|
|
} else {
|
|
|
|
return SHELL_FOREGROUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SHELL_NOTHING;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2008-02-05 00:42:17 +01:00
|
|
|
input_to_child_command(struct shell_command *c,
|
|
|
|
char *data1, int len1,
|
|
|
|
const char *data2, int len2)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_input input;
|
|
|
|
if(process_is_running(c->process)) {
|
|
|
|
input.data1 = data1;
|
|
|
|
input.len1 = len1;
|
|
|
|
input.data2 = data2;
|
|
|
|
input.len2 = len2;
|
|
|
|
process_post_synch(c->process, shell_event_input, &input);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_input(char *commandline, int commandline_len)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_input input;
|
|
|
|
|
|
|
|
/* printf("shell_input front_process '%s'\n", front_process->name);*/
|
|
|
|
|
|
|
|
if(commandline[0] == '~' &&
|
|
|
|
commandline[1] == 'K') {
|
2014-06-12 13:40:28 +02:00
|
|
|
/* process_start(&shell_killall_process, (void *)commandline);*/
|
2008-02-05 00:42:17 +01:00
|
|
|
if(front_process != &shell_process) {
|
|
|
|
process_exit(front_process);
|
|
|
|
}
|
2008-02-24 21:35:03 +01:00
|
|
|
} else {
|
2008-02-05 00:42:17 +01:00
|
|
|
if(process_is_running(front_process)) {
|
|
|
|
input.data1 = commandline;
|
|
|
|
input.len1 = commandline_len;
|
|
|
|
input.data2 = "";
|
|
|
|
input.len2 = 0;
|
|
|
|
process_post_synch(front_process, shell_event_input, &input);
|
|
|
|
}
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_output_str(struct shell_command *c, char *text1, const char *text2)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
if(c != NULL && c->child != NULL) {
|
2008-02-10 13:24:43 +01:00
|
|
|
input_to_child_command(c->child, text1, (int)strlen(text1),
|
|
|
|
text2, (int)strlen(text2));
|
2008-02-05 00:42:17 +01:00
|
|
|
} else {
|
2008-02-10 13:24:43 +01:00
|
|
|
shell_default_output(text1, (int)strlen(text1),
|
|
|
|
text2, (int)strlen(text2));
|
2008-02-05 00:42:17 +01:00
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_output(struct shell_command *c,
|
|
|
|
void *data1, int len1,
|
|
|
|
const void *data2, int len2)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
if(c != NULL && c->child != NULL) {
|
|
|
|
input_to_child_command(c->child, data1, len1, data2, len2);
|
|
|
|
} else {
|
|
|
|
shell_default_output(data1, len1, data2, len2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_unregister_command(struct shell_command *c)
|
|
|
|
{
|
|
|
|
list_remove(commands, c);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_register_command(struct shell_command *c)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
struct shell_command *i, *p;
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
for(i = list_head(commands);
|
|
|
|
i != NULL &&
|
|
|
|
strcmp(i->command, c->command) < 0;
|
|
|
|
i = i->next) {
|
|
|
|
p = i;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
if(p == NULL) {
|
|
|
|
list_push(commands, c);
|
|
|
|
} else if(i == NULL) {
|
|
|
|
list_add(commands, c);
|
|
|
|
} else {
|
|
|
|
list_insert(commands, p, c);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_process, ev, data)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-05 00:42:17 +01:00
|
|
|
static struct process *started_process;
|
2011-04-12 14:12:09 +02:00
|
|
|
struct shell_input *input;
|
|
|
|
int ret;
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
/* Let the system start up before showing the prompt. */
|
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
|
|
|
while(1) {
|
2011-05-21 20:52:00 +02:00
|
|
|
shell_prompt(shell_prompt_text);
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
|
|
|
|
{
|
2011-04-12 14:12:09 +02:00
|
|
|
input = data;
|
2008-02-05 00:42:17 +01:00
|
|
|
ret = shell_start_command(input->data1, input->len1, NULL,
|
|
|
|
&started_process);
|
|
|
|
|
|
|
|
if(started_process != NULL &&
|
|
|
|
ret == SHELL_FOREGROUND &&
|
|
|
|
process_is_running(started_process)) {
|
|
|
|
front_process = started_process;
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_EXITED &&
|
|
|
|
data == started_process);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
front_process = &shell_process;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_server_process, ev, data)
|
|
|
|
{
|
|
|
|
struct process *p;
|
|
|
|
struct shell_command *c;
|
|
|
|
static struct etimer etimer;
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
etimer_set(&etimer, CLOCK_SECOND * 10);
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
if(ev == PROCESS_EVENT_EXITED) {
|
|
|
|
p = data;
|
|
|
|
/* printf("process exited '%s' (front '%s')\n", p->name,
|
|
|
|
front_process->name);*/
|
|
|
|
for(c = list_head(commands);
|
|
|
|
c != NULL && c->process != p;
|
|
|
|
c = c->next);
|
|
|
|
while(c != NULL) {
|
|
|
|
if(c->child != NULL && c->child->process != NULL) {
|
|
|
|
/* printf("Killing '%s'\n", c->process->name);*/
|
|
|
|
input_to_child_command(c->child, "", 0, "", 0);
|
|
|
|
/* process_exit(c->process);*/
|
|
|
|
}
|
|
|
|
c = c->child;
|
|
|
|
}
|
|
|
|
} else if(ev == PROCESS_EVENT_TIMER) {
|
|
|
|
etimer_reset(&etimer);
|
|
|
|
shell_set_time(shell_time());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_init(void)
|
|
|
|
{
|
|
|
|
list_init(commands);
|
|
|
|
shell_register_command(&help_command);
|
|
|
|
shell_register_command(&question_command);
|
|
|
|
shell_register_command(&killall_command);
|
|
|
|
shell_register_command(&kill_command);
|
|
|
|
shell_register_command(&null_command);
|
2011-04-16 21:10:22 +02:00
|
|
|
shell_register_command(&exit_command);
|
|
|
|
shell_register_command(&quit_command);
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
shell_event_input = process_alloc_event();
|
|
|
|
|
|
|
|
process_start(&shell_process, NULL);
|
|
|
|
process_start(&shell_server_process, NULL);
|
|
|
|
|
|
|
|
front_process = &shell_process;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
unsigned long
|
|
|
|
shell_strtolong(const char *str, const char **retstr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long num = 0;
|
|
|
|
const char *strptr = str;
|
|
|
|
|
|
|
|
if(str == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*strptr == ' ') {
|
|
|
|
++strptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < 10 && isdigit(strptr[i]); ++i) {
|
|
|
|
num = num * 10 + strptr[i] - '0';
|
|
|
|
}
|
|
|
|
if(retstr != NULL) {
|
|
|
|
if(i == 0) {
|
|
|
|
*retstr = str;
|
|
|
|
} else {
|
|
|
|
*retstr = strptr + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
unsigned long
|
|
|
|
shell_time(void)
|
|
|
|
{
|
2008-07-08 01:46:12 +02:00
|
|
|
return clock_seconds() + time_offset;
|
2008-02-05 00:42:17 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_set_time(unsigned long seconds)
|
|
|
|
{
|
2008-07-08 01:46:12 +02:00
|
|
|
time_offset = seconds - clock_seconds();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 21:35:03 +01:00
|
|
|
void
|
|
|
|
shell_start(void)
|
|
|
|
{
|
2011-05-21 20:52:00 +02:00
|
|
|
shell_output_str(NULL, shell_banner_text, "");
|
2008-02-24 21:35:03 +01:00
|
|
|
shell_output_str(NULL, "Type '?' and return for help", "");
|
2011-05-21 20:52:00 +02:00
|
|
|
shell_prompt(shell_prompt_text);
|
2008-02-24 21:35:03 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-29 00:29:07 +01:00
|
|
|
void
|
2011-04-16 21:10:22 +02:00
|
|
|
shell_stop(void)
|
2008-02-29 00:29:07 +01:00
|
|
|
{
|
|
|
|
killall();
|
2011-04-16 21:10:22 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_quit(void)
|
|
|
|
{
|
|
|
|
shell_stop();
|
2008-02-29 00:29:07 +01:00
|
|
|
process_exit(&shell_process);
|
|
|
|
process_exit(&shell_server_process);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-03-02 21:44:15 +01:00
|
|
|
|
|
|
|
/** @} */
|