shell.c

Go to the documentation of this file.
00001 
00013  /*
00014  * Copyright (c) 2003, Adam Dunkels.
00015  * All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions
00019  * are met:
00020  * 1. Redistributions of source code must retain the above copyright
00021  *    notice, this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright
00023  *    notice, this list of conditions and the following disclaimer in the
00024  *    documentation and/or other materials provided with the distribution.
00025  * 3. The name of the author may not be used to endorse or promote
00026  *    products derived from this software without specific prior
00027  *    written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00030  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00031  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00032  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00033  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00034  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00035  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00036  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00037  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00038  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00039  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00040  *
00041  * This file is part of the uIP TCP/IP stack.
00042  *
00043  * $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
00044  *
00045  */
00046 
00047 #include "shell.h"
00048 #include "../cli/api.h"
00049 
00050 #include <string.h>
00051 
00052 struct ptentry {
00053   char *commandstr;
00054   void (* pfunc)(char *str);
00055 };
00056 
00057 #define SHELL_PROMPT "uIP 1.0> "
00058 
00059 /*---------------------------------------------------------------------------*/
00060 static void
00061 parse(register char *str, struct ptentry *t)
00062 {
00063   struct ptentry *p;
00064   for(p = t; p->commandstr != NULL; ++p) {
00065     if(strncmp(p->commandstr, str, strlen(p->commandstr)) == 0) {
00066       break;
00067     }
00068   }
00069 
00070   p->pfunc(str);
00071 }
00072 /*---------------------------------------------------------------------------*/
00073 static void
00074 inttostr(register char *str, unsigned int i)
00075 {
00076   str[0] = '0' + i / 100;
00077   if(str[0] == '0') {
00078     str[0] = ' ';
00079   }
00080   str[1] = '0' + (i / 10) % 10;
00081   if(str[0] == ' ' && str[1] == '0') {
00082     str[1] = ' ';
00083   }
00084   str[2] = '0' + i % 10;
00085   str[3] = ' ';
00086   str[4] = 0;
00087 }
00088 /*---------------------------------------------------------------------------*/
00089 static void
00090 help(char *str)
00091 {
00092   shell_output("Available commands:", "");
00093   shell_output("stats   - show network statistics", "");
00094   shell_output("conn    - show TCP connections", "");
00095   shell_output("help, ? - show help", "");
00096   shell_output("exit    - exit shell", "");
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 static void
00100 unknown(char *str)
00101 {
00102   if(strlen(str) > 0) {
00103     shell_output("Unknown command: ", str);
00104   }
00105 }
00106 /*---------------------------------------------------------------------------*/
00107 static struct ptentry parsetab[] =
00108   {{"stats", help},
00109    {"conn", help},
00110    {"help", help},
00111    {"exit", shell_quit},
00112    {"?", help},
00113 
00114    /* Default action */
00115    {NULL, unknown}};
00116 /*---------------------------------------------------------------------------*/
00117 void
00118 shell_init(void)
00119 {
00120         str_t *tp;
00121         do_init(tp);
00122         shell_output(tp->resp,"");
00123 }
00124 /*---------------------------------------------------------------------------*/
00125 void
00126 shell_start(void)
00127 {
00128         str_t *tp;
00129         do_init(tp);
00130         shell_output(tp->resp,"");
00131   shell_output("uIP command shell", "");
00132   shell_output("command 'quit' to return.\n","");
00133   shell_prompt(CLI_PROMPT);
00134 }
00135 /*---------------------------------------------------------------------------*/
00136 void
00137 shell_input(char *cmd)
00138 {
00139         char *buff;
00140   //parse(cmd, parsetab);
00141         if(!strcmp(cmd,"quit")) shell_quit(cmd);
00142         else {
00143           CLI_Builder(cmd, buff);
00144           shell_output(buff,"\n");
00145           shell_prompt(CLI_PROMPT);
00146         }
00147 }
00148 /*---------------------------------------------------------------------------*/
00149