[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH] Verbesserte echo Funktion für LOSH ... mit bunt
From: Stefan Merettig <papierkorb@doseneintopf.(none)>
* echo kennt nun Flags (-n und -e) und kann mit Steuerzeichen umgehen (\\n, \\t, ..., \\0NNN, \\x42)
---
src/modules/c/shell/cmds/echo.c | 137 ++++++++++++++++++++++++++++++++++++++-
1 files changed, 134 insertions(+), 3 deletions(-)
diff --git a/src/modules/c/shell/cmds/echo.c b/src/modules/c/shell/cmds/echo.c
index fdb42b7..8d07893 100644
--- a/src/modules/c/shell/cmds/echo.c
+++ b/src/modules/c/shell/cmds/echo.c
@@ -36,20 +36,151 @@
#include "types.h"
#include "stdio.h"
#include "stdlib.h"
+#include "stdbool.h"
+#include "string.h"
#include <lost/config.h>
+#define is_numeric(c) ((c) >= '0' && (c) <= '9')
+
#ifdef CONFIG_SHELL_BUILTIN_ONLY
int shell_command_echo(int argc, char* argv[], const char* args)
#else
int main(int argc, char* argv[])
#endif
{
- int i;
+ int i, j, len;
+ bool trailing_newline = true;
+ bool backslash_escapes = false;
+ bool process_flags = true;
+ char* input_str = NULL;
+ char* output_str = NULL;
+ char num_tmp[4];
+ char append = 0x0;
+ int out_len = 0;
+ int pos = 0; // Position im Ausgabestring
for (i = 1; i < argc; i++) {
- printf("%s ", argv[i]);
+ if (process_flags && argv[i][0] == '-') {
+ input_str = argv[i];
+ for (j=1; input_str[j]; j++) {
+ switch (input_str[j]) {
+ case 'n':
+ trailing_newline = false;
+ break;
+ case 'e':
+ backslash_escapes = true;
+ break;
+ default:
+ j = 0xFFFFFFF; // weitere Flags überspringen
+ i++;
+ break;
+ }
+ }
+ } else {
+ process_flags = false;
+
+ input_str = argv[i];
+ len = strlen (input_str);
+ out_len += len+1;
+ output_str = realloc (output_str, out_len+2);
+
+ while (len) {
+ if (backslash_escapes && input_str[0] == '\\' &&
+ input_str[1] == '\\') {
+ len -= 2;
+ input_str += 2;
+ if (!input_str[0]) {
+ break;
+ }
+ switch (input_str[0]) {
+ case '0':
+ input_str++;
+ len--;
+ memset (num_tmp, 0x0, 4);
+ for (j = 0; input_str[j] // Zahl kopieren
+ && is_numeric (input_str[j]) && j < 3; j++) {
+ num_tmp[j] = input_str[j];
+ }
+ input_str += j - 1;
+ len -= j - 1;
+ append = (char)(strtol (num_tmp, NULL, 8));
+ break;
+ case 'x':
+ input_str++;
+ len--;
+ memset (num_tmp, 0x0, 4);
+ for (j = 0; input_str[j] // Zahl kopieren
+ && is_numeric (input_str[j]) && j < 2; j++) {
+ num_tmp[j] = input_str[j];
+ }
+ input_str += j - 1;
+ len -= j - 1;
+ append = (char)(strtol (num_tmp, NULL, 16));
+ break;
+ case 'a':
+ append = '\a';
+ break;
+ case 'b':
+ append = '\b';
+ break;
+ case 'c':
+ append = 0x0;
+ break;
+ case 'f':
+ append = '\f';
+ break;
+ case 'n':
+ append = '\n';
+ break;
+ case 'r':
+ append = '\r';
+ break;
+ case 't':
+ append = '\t';
+ break;
+ case 'v':
+ append = '\v';
+ break;
+ default:
+ append = '\\';
+ len++; // Nicht das nächste Zeichen überspringen
+ input_str--;
+ break;
+ }
+ input_str++;
+ len--;
+ } else {
+ append = input_str[0];
+ input_str++;
+ len--;
+ }
+
+ output_str[pos] = append;
+ pos++;
+ output_str[pos] = 0x0;
+
+ if (!append) {
+ goto output_string;
+ }
+ if (!input_str[0]) {
+ break;
+ }
+ }
+ if (i < argc-1) { // Kein Leerzeichen hinter dem letzten Parameter
+ output_str[pos] = ' ';
+ pos++;
+ output_str[pos] = 0x0;
+ }
+ }
+ }
+
+ output_string:
+ printf ("%s", output_str);
+ free (output_str);
+
+ if (trailing_newline) {
+ putchar('\n');
}
- putchar('\n');
return EXIT_SUCCESS;
}
--
1.7.0.4