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

Re: [tyndur-devel] [PATCH] losh, shell.c



Alexander Hartmut Kluth schrieb:
! Leerzeichen am Ende werden nun entfernt. Nicht sehr schön, tutet aber

Signed-off-by: Alexander Hartmut Kluth <hartmut@xxxxxxxxxx>
---
 src/modules/c/shell/shell.c |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c
index 9d5583a..3560aea 100644
--- a/src/modules/c/shell/shell.c
+++ b/src/modules/c/shell/shell.c
@@ -134,7 +134,7 @@ int main(int argc, char* argv[])
         strcpy(shell_command_buffer, argv[2]);
         shell_do_cmd();
     }
- +
     return 0;
 }
@@ -144,7 +144,8 @@ void shell_read_command(void)
     char* cwd = getcwd(NULL, 0);
     char* prompt;
     char* input;
- + int i;
+
     asprintf(&prompt, "%s # ", cwd);
input = readline(prompt);
@@ -153,6 +154,19 @@ void shell_read_command(void)
             add_history(input);
         }
+ if ((strlen(input)) > 2) {
+            for (i = strlen(input) - 1; i >= 0; i--) {
+                if (input[i] != ' ') {
+                    input[i+1] = 0;
+                    break;
+                }
+            }
+
+            if (i == 0) {
+                input[0] = 0;
+            }
+        }
+
Wie in #niwohlos erklärt, ist das nicht ganz richtig und wird bei einem String wie "x " fehlschlagen. Die "strlen() > 2"-Prüfung kann weg, dafür muss das zweite if-Statement so aussehen:

if ((i == 0) && (input[0] == ' ')) {
  input[0] = 0;
}
         strncpy(shell_command_buffer, input, COMMAND_BUFFER_SIZE);
         free(input);
     } else {
@@ -200,7 +214,7 @@ bool shell_match_command(const char* cmd, const char* cmdline)
         cmd++;
         cmdline++;
     }
- +
     // Kommt nie vor.
     return FALSE;
 }
@@ -263,6 +277,7 @@ int shell_do_cmd(void)
         return 0;
     }
+
     // Die Liste mit den Befehlen durchsuchen. Das wird solange gemacht, bis
     // der NULL eintrag am Ende erreicht wird.
     for (i = 0; (command = &shell_commands[i]) && (command->name); i++)
@@ -280,7 +295,7 @@ int shell_do_cmd(void)
                     argc++;
                 }
             }
- +
             char* argv[argc];
             argv[0] = strtok(args, " ");
             for(pos = 1; pos < argc; pos++)
@@ -291,13 +306,12 @@ int shell_do_cmd(void)
             while (argv[argc - 1] == NULL) {
                 argc--;
             }
- +
             return command->handler(argc, argv, cmdstring);
         }
     }
     return shell_command_default(0, 0, cmdstring);
 }
-
 /**
  * Shell-Skript ausfuehren
  *
--
Max Reitz
<max@xxxxxxxxxx>