cmdlinelib.c now calls system() rather than doing its own tokenisation
authorMichael Brown <mcb30@etherboot.org>
Fri, 8 Dec 2006 01:26:11 +0000 (01:26 +0000)
committerMichael Brown <mcb30@etherboot.org>
Fri, 8 Dec 2006 01:26:11 +0000 (01:26 +0000)
(which was extremely heavy on calls to malloc()).

Moved include/command.h to include/gpxe/command.h, since it's
gPXE-specific.

src/commandline/cmdlinelib.c
src/commandline/commands/help.c
src/commandline/commands/nvo_cmd.c
src/commandline/commands/test.c
src/commandline/commands/test2.c
src/include/command.h [deleted file]

index d49bad3..2e8e9c1 100644 (file)
@@ -1,5 +1,5 @@
 #include "cmdlinelib.h"
-#include "command.h"
+#include <gpxe/command.h>
 #include <gpxe/tables.h>
 #include <console.h>
 #include <malloc.h>
@@ -62,27 +62,6 @@ void cmdl_addstr(cmd_line* cmd, char* str)
        }
 }
 
-/*void cmdl_addoutput_str(cmd_line* cmd, char output[CMDL_OUTPUT_SIZE])
-{
-       if(cmdl_check(cmd) && output != NULL){
-               if(!cmd->has_output){
-                       cmdl_clearoutput(cmd);
-               }
-               strncat(cmd->output, output, CMDL_OUTPUT_SIZE);
-               cmd->has_output = 1;
-       }
-}*/
-
-/*char* cmdl_getoutput(cmd_line* cmd)
-{
-       if(cmdl_check(cmd) && cmd->has_output){
-               cmd->has_output = 0;
-               return cmd->output;
-       }else{
-               return "";
-       }
-}*/
-
 void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE])
 {
        if(cmdl_check(cmd) && prompt != NULL){
@@ -178,7 +157,8 @@ void cmdl_parsechar(cmd_line* cmd, char in)
 
                                case CMDLK_RETURN:
                                        cmd->putchar('\n');
-                                       cmdl_exec(cmd);
+                                       system ( cmd->buffer );
+                                       cmdl_clearbuffer(cmd);
                                        cmd->refresh = 1;
                                        break;
 
@@ -261,142 +241,6 @@ void cmdl_tabcomplete(cmd_line *cmd)
 
 }
 
-
-void cmdl_exec(cmd_line* cmd)
-{
-       cmdl_param_list* params;
-       int unknown=1;
-       struct command *ccmd;
-
-       params = cmdl_getparams(cmd->buffer);
-       
-       if(params == NULL){
-               cmdl_clearbuffer(cmd);
-               return;
-       }
-
-       if(params->argc > 0){
-               if(!strcmp(params->argv[0], "exit") || !strcmp(params->argv[0], "quit")){
-                       cmdl_setexit(cmd, 1);
-/*             }else if(!strcmp(params->argv[0], "help")){
-                       if(params->argc > 1){
-                               cmdl_builtin_help(cmd, params->argv[1]);
-                       }else{
-                               cmdl_builtin_help(cmd, "");
-                       }*/
-               }else{
-                       for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
-                               if(!strcmp(ccmd->name, params->argv[0])){
-                                       unknown = 0;
-                                       ccmd->exec(params->argc, params->argv);
-                                       break;
-                               }
-                       }
-                       if(unknown){
-                               cmd->printf("%s: unknown command\n", params->argv[0]);
-                       }
-               }
-       }
-
-       free(params);   
-       cmdl_clearbuffer(cmd);
-}
-
-/*void cmdl_builtin_help(cmd_line* cmd, char* command){
-       struct command *ccmd;
-       int unknown = 1;
-       if(strcmp(command, "") == 0){
-               cmd->printf("Built in commands:\n\n\thelp\t\tCommand usage help (\"help help\" for more info)\n\texit, quit\t\tExit the command line and boot\n\nCompiled in commands:\n\n");
-
-               for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
-                       cmd->printf ("\t%s\t\t%s\n", ccmd->name, ccmd->desc );
-               }
-       }else{
-               if(!strcmp(command, "help")){
-                       cmd->printf("help - The help command\n\nUsage: help <command>\n\n\tExample:\n\t\thelp help\n");
-               }else if(!strcmp(command, "exit") || !strcmp(command, "quit")){
-                       cmd->printf("exit, quit - The quit command\n\nUsage:\nquit or exit\n\n\tExample:\n\t\texit\n");
-               }else{
-                       for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
-                               if(!strcmp(ccmd->name, command)){
-                                       unknown = 0;
-                                       cmd->printf ("\t%s - %s\n\nUsage:\n%s\n", ccmd->name, ccmd->desc, ccmd->usage );
-                                       break;
-                               }
-                               if(unknown){
-                                       cmd->printf("\"%s\" isn't compiled in (does it exist?).\n", command);
-                               }
-                       }
-               }
-               
-       }
-}*/
-
-cmdl_param_list* cmdl_getparams(const char* command){
-       cmdl_param_list* this;
-       char *result = NULL;
-       int count=0;
-       char *command2;
-       
-       this = (cmdl_param_list*)malloc(sizeof(cmdl_param_list));
-       
-       if(this == NULL){
-               return NULL;
-       }
-
-       command2 = malloc(strlen(command) + 1);
-       
-       this->argc=0;
-
-       strcpy(command2, command);
-       result = strtok(command2, " ");
-       
-       while( result != NULL ) {
-               this->argc++;
-               result = strtok( NULL, " ");
-       }
-       
-       this->argv = (char**)malloc(sizeof(char*) * this->argc);
-       if(this->argv == NULL){
-               free(this);
-               return NULL;
-       }
-       
-       
-       strcpy(command2, command);
-       result = strtok(command2, " ");
-       
-       while( result != NULL && this->argc > count) {
-               this->argv[count] = (char*)malloc(sizeof(char) * (strlen(result) + 1));
-               if(this->argv[count] == NULL){
-                       free(this);
-                       return NULL;
-               }
-               strcpy(this->argv[count], result);
-               count++;
-               result = strtok( NULL, " ");
-       }   
-       free(command2); 
-       return this;
-}
-
-/*char* cmdl_parse_getcmd(cmd_line* cmd){
-       int i;
-       char* ret;
-       ret = (char*)malloc(1);
-       ret[0] = 0;
-
-       for(i=0; i < CMDL_BUFFER_SIZE - 1; i++){
-               if(cmd->buffer[i + 1] == ' ' || cmd->buffer[i + 1] == '\0'){
-                       free(ret);
-                       ret = (char*)malloc(i+1);
-                       strncat(ret, cmd->buffer, i+1);
-                       break;
-               }
-       }
-       return ret;
-}*/
-
 void cmdl_clearbuffer(cmd_line* cmd)
 {
        if(cmdl_check(cmd)){
@@ -408,16 +252,6 @@ void cmdl_clearbuffer(cmd_line* cmd)
        }
 }
 
-/*void cmdl_clearoutput(cmd_line* cmd)
-{
-       if(cmdl_check(cmd)){
-               int i;
-               for(i=0; i < CMDL_OUTPUT_SIZE; i++){
-                       cmd->output[i] = 0;
-               }
-       }
-}*/
-
 int cmdl_movecursor(cmd_line* cmd, int direction)
 {
        if(cmdl_check(cmd)){
index 6ba6175..3074f18 100644 (file)
@@ -1,7 +1,7 @@
-#include "command.h"
-#include "console.h"
 #include <string.h>
+#include <vsprintf.h>
 #include <gpxe/tables.h>
+#include <gpxe/command.h>
 
 static struct command cmd_start[0] __table_start ( commands );
 static struct command cmd_end[0] __table_end ( commands );
@@ -12,36 +12,19 @@ static int cmd_help_exec ( int argc, char **argv ) {
 
        struct command *ccmd;
        int unknown = 1;
-       if(argc == 1){
-               printf("Available commands:\n\n  exit - Exit the command line and boot\n");
-
-               for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
-                       printf ("  %s - %s\n", ccmd->name, ccmd->desc );
-               }
-       }else{
-               if(!strcmp(argv[1], "exit") || !strcmp(argv[1], "quit")){
-                       printf("exit - Exit the command line and boot\n\nUsage:\n  exit\n");
-               }else{
-                       for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
-                               if(!strcmp(ccmd->name, argv[1])){
-                                       unknown = 0;
-                                       printf ("%s - %s\n\nUsage:\n  %s\n", ccmd->name, ccmd->desc, ccmd->usage );
-                                       break;
-                               }
-                       }
-                       if(unknown){
-                               printf("\"%s\" isn't compiled in (does it exist?).\n", argv[1]);
-                       }
-               }
-               
+
+
+       printf("Available commands:\n\n  exit - Exit the command line and boot\n");
+
+       for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
+               printf ("  %s\n", ccmd->name );
        }
+
        return 0;
 }
 
 struct command help_command __command = {
        .name = "help",
-       .usage = "help <command>\n\nExample:\n  help help\n",
-       .desc = "The help command",
        .exec = cmd_help_exec,
 };
 
index 662ad7b..d859b2c 100644 (file)
@@ -3,10 +3,11 @@
 #include <string.h>
 #include <errno.h>
 #include <vsprintf.h>
-#include <command.h>
+#include <getopt.h>
 #include <gpxe/nvo.h>
 #include <gpxe/dhcp.h>
 #include <gpxe/settings.h>
+#include <gpxe/command.h>
 
 void nvo_cmd_req() {}
 
@@ -41,8 +42,6 @@ static int show_exec ( int argc, char **argv ) {
 
 struct command show_command __command = {
        .name = "show",
-       .usage = "show <identifier>\n",
-       .desc = "Show stored options",
        .exec = show_exec,
 };
 
@@ -78,7 +77,5 @@ static int set_exec ( int argc, char **argv ) {
 
 struct command set_command __command = {
        .name = "set",
-       .usage = "set <identifier> <value>\n",
-       .desc = "Set stored option",
        .exec = set_exec,
 };
index 1d37012..59b0b72 100644 (file)
@@ -1,5 +1,5 @@
-#include "command.h"
-#include "console.h"
+#include <vsprintf.h>
+#include <gpxe/command.h>
 
 void test_req(){}
 
@@ -15,8 +15,6 @@ static int cmd_test_exec ( int argc, char **argv ) {
 
 struct command test_command __command = {
        .name = "test",
-       .usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest",
-       .desc = "Does nothing",
        .exec = cmd_test_exec,
 };
 
index 077933d..6cb2ee7 100644 (file)
@@ -1,5 +1,5 @@
-#include "command.h"
-#include "console.h"
+#include <vsprintf.h>
+#include <gpxe/command.h>
 
 void test2_req(){}
 
@@ -15,8 +15,6 @@ static int cmd_test2_exec ( int argc, char **argv ) {
 
 struct command test2_command __command = {
        .name = "test2",
-       .usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest2",
-       .desc = "Does nothing",
        .exec = cmd_test2_exec,
 };
 
diff --git a/src/include/command.h b/src/include/command.h
deleted file mode 100644 (file)
index 113ca2f..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef COMMAND_H
-#define COMMAND_H
-
-#include <gpxe/tables.h>
-
-struct command {
-       const char *name;                                               // The name of the command
-       const char *usage;                                              // Description of how to use the command
-       const char *desc;                                               // Short description of the command
-       int ( *exec ) ( int argc, char **argv);                         // The command function to call
-};
-
-#define __command __table ( commands, 01 )
-#endif
-