[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 1/5] shell: Programme starten mit init_execv()



* shell: Mit der Benutzung von init_execv() wird der libc direkt ein
  argv-Array übergeben statt der kompletten Kommandozeile. Sobald die
  Shell mal anfängt, die Kommandozeile anständig zu parsen, ist das das
  viel sinnvollere Format.

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/modules/c/shell/commands.c | 38 ++++++++++----------------------
 src/modules/c/shell/shell.c    | 50 ++++++++++++++++++++++--------------------
 2 files changed, 38 insertions(+), 50 deletions(-)

diff --git a/src/modules/c/shell/commands.c b/src/modules/c/shell/commands.c
index 53a8cfe..c36f577 100644
--- a/src/modules/c/shell/commands.c
+++ b/src/modules/c/shell/commands.c
@@ -74,17 +74,12 @@
 #endif
 
 
-int shell_start_path_app(const char* cmd, bool wait)
+static int shell_start_path_app(char* argv[], bool wait)
 {
-    DEBUG_MSG1("Starte das Programm %s", cmd);
+    DEBUG_MSG1("Starte das Programm %s", argv[0]);
     pid_t pid;
 
-    // Fuehrende Leerzeichen im Befehl grosszuegig ueberspringen
-    while (*cmd == ' ') {
-        cmd++;
-    }
-
-    pid = init_execute(cmd);
+    pid = init_execv(argv[0], (const char const**) argv);
 
     // Fehler ist aufgetreten
     if (pid == 0) {
@@ -110,7 +105,7 @@ int shell_command_default(int argc, char* argv[], const char* cmd)
         return 0;
     }
 
-    if (shell_start_path_app(cmd, true) == 0) {
+    if (shell_start_path_app(argv, true) == 0) {
         return 0;
     }
 
@@ -123,7 +118,6 @@ int shell_command_default(int argc, char* argv[], const char* cmd)
  */
 int shell_command_start(int argc, char* argv[], const char* cmd)
 {
-    const char* program;
     DEBUG_MSG("start");
 
     if (argc < 2) {
@@ -131,12 +125,7 @@ int shell_command_start(int argc, char* argv[], const char* cmd)
         return -1;
     }
 
-    program = strchr(cmd, ' ') + 1;
-    if (program == NULL) {
-        return -1;
-    }
-
-    if (shell_start_path_app(program, false) == 0) {
+    if (shell_start_path_app(&argv[1], false) == 0) {
         return 0;
     }
 
@@ -150,19 +139,16 @@ int shell_command_start(int argc, char* argv[], const char* cmd)
  */
 int shell_command_help(int argc, char* argv[], const char* args)
 {
-    char* help_args = strchr(args, ' ');
-    char* cmd;
+    char** thelp_args;
     int ret;
 
     // Erstmal versuchen, ob thelp installiert ist
-    if (help_args == NULL) {
-        cmd = strdup("thelp tyndur");
-    } else {
-        asprintf(&cmd, "thelp %s", help_args + 1);
-    }
-
-    ret = shell_start_path_app(cmd, true);
-    free(cmd);
+    thelp_args = (char*[]) {
+        "thelp",
+        argc > 1 ? argv[1] : "tyndur",
+        NULL,
+    };
+    ret = shell_start_path_app(thelp_args, true);
     if (ret == 0) {
         return 0;
     }
diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c
index cdca2f1..b7ed005 100644
--- a/src/modules/c/shell/shell.c
+++ b/src/modules/c/shell/shell.c
@@ -259,6 +259,7 @@ int shell_do_cmd(void)
     shell_command_t* command;
     uint32_t i;
     char* cmdstring = shell_command_buffer;
+    int argc, pos;
 
     substitute_envvars();
 
@@ -272,6 +273,30 @@ int shell_do_cmd(void)
         return 0;
     }
 
+    // argc und argv befüllen
+    char args[strlen(cmdstring) + 1];
+    memcpy(args, cmdstring, strlen(cmdstring) + 1);
+
+    argc = 1;
+    //Jetzt werden die Argumente gezaehlt.
+    for (pos = 0; pos < strlen(args); pos++) {
+        if (args[pos] == ' ') {
+            argc++;
+        }
+    }
+
+    char* argv[argc + 1];
+    argv[0] = strtok(args, " ");
+    for(pos = 1; pos < argc; pos++)
+    {
+        argv[pos] = strtok(NULL, " ");
+    }
+    argv[pos] = NULL;
+
+    while (argv[argc - 1] == NULL) {
+        argc--;
+    }
+
 
     // Die Liste mit den Befehlen durchsuchen. Das wird solange gemacht, bis
     // der NULL eintrag am Ende erreicht wird.
@@ -279,33 +304,10 @@ int shell_do_cmd(void)
     {
         command = &shell_commands[i];
         if (shell_match_command(command->name, cmdstring) == true) {
-            char args[strlen(cmdstring) + 1];
-            memcpy(args, cmdstring, strlen(cmdstring) + 1);
-
-            int argc, pos;
-            argc = 1;
-            //Jetzt werden die Argumente gezaehlt.
-            for (pos = 0; pos < strlen(args); pos++) {
-                if (args[pos] == ' ') {
-                    argc++;
-                }
-            }
-
-            char* argv[argc];
-            argv[0] = strtok(args, " ");
-            for(pos = 1; pos < argc; pos++)
-            {
-                argv[pos] = strtok(NULL, " ");
-            }
-
-            while (argv[argc - 1] == NULL) {
-                argc--;
-            }
-
             return command->handler(argc, argv, cmdstring);
         }
     }
-    return shell_command_default(0, 0, cmdstring);
+    return shell_command_default(argc, argv, cmdstring);
 }
 /**
  * Shell-Skript ausfuehren
-- 
2.1.4