From 0cdc4d3e93d24edebf360b6820f8aca55bfd204f Mon Sep 17 00:00:00 2001 From: "Rustem Gimadutdinov (rgimad)" Date: Sat, 18 Apr 2020 14:11:23 +0000 Subject: [PATCH] SHELL 0.8.1 changelog - added output redirection to file (> >>) for echo command e.g. echo "">123.txt ; echo a b c >> y.txt git-svn-id: svn://kolibrios.org@7808 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/system/shell/cmd/cmd_echo.c | 172 ++++++++++++++++++++++++++- programs/system/shell/globals.h | 2 +- 2 files changed, 170 insertions(+), 4 deletions(-) diff --git a/programs/system/shell/cmd/cmd_echo.c b/programs/system/shell/cmd/cmd_echo.c index ec102ad7d1..2ca0a41456 100644 --- a/programs/system/shell/cmd/cmd_echo.c +++ b/programs/system/shell/cmd/cmd_echo.c @@ -1,7 +1,173 @@ -int cmd_echo(char text[]) + +int newline_to_file(char *fn, unsigned long long pos) { -printf("%s\n\r", text); -return TRUE; + char *newline = "\n\r"; + + kol_struct70 k70_out; + + k70_out.p00 = 3; + k70_out.p04 = pos; // offset + //k70_out.p08 = 0; + k70_out.p12 = 2; + k70_out.p16 = (unsigned)newline; + k70_out.p20 = 0; + k70_out.p21 = fn; + + return kol_file_70(&k70_out); // write +} + + +int cmd_echo(char text[]) +{ // added output redirection by rgimad 2020. + + int text_len = strlen(text); + int out_len = text_len; + int i, redirect = 0, redirect_mode = 0; // if redirect = 0 echo to screen, if 1 to file. If redirect_mode = 0 rewrite the file, if 1 append to the file + char *filename; // pointer to name of the file redirect to + int ignore_redir_char = 0; + + for (i = 0; i < text_len; i++) + { + if (text[i] == '"') + { + ignore_redir_char = !ignore_redir_char; + continue; + } + if (!ignore_redir_char && text[i] == '>') + { + if (i + 1 < text_len && text[i + 1] == '>') + { + redirect = 1; + redirect_mode = 1; + filename = text + i + 2; + out_len = i; + break; + } else + { + redirect = 1; + redirect_mode = 0; + filename = text + i + 1; + out_len = i; + break; + } + } + } + + // remove leading spaces in filename + while (*filename == ' ') { filename++; } + + // remove spaces at the end of out_len + while (out_len > 0 && text[out_len - 1] == ' ') { out_len--; } + + // delete quotes if has + if (text[out_len - 1] == '"') { out_len--; } + if (text[0] == '"') { text++; out_len--; } + + + if (redirect == 0) // echo to screen + { + text[out_len] = '\0'; + printf("%s\n\r", text); + } else + { + char *filename_out = (char*) malloc(FILENAME_MAX); // abs path + + if (filename[0] != '/') + { + strcpy(filename_out, cur_dir); + if (filename_out[strlen(filename_out)-1] != '/') + { + strcat(filename_out, "/"); // add slash + } + strcat(filename_out, filename); + } else + { + strcpy(filename_out, filename); + } + + kol_struct70 k70_out, k70_in; + int result; + + if (redirect_mode == 0) // rewrite the output file + { + + k70_out.p00 = 2; + k70_out.p04 = 0; // offset + //k70_out.p08 = 0; + k70_out.p12 = out_len; + k70_out.p16 = (unsigned)text; + k70_out.p20 = 0; + k70_out.p21 = filename_out; + + result = kol_file_70(&k70_out); // write + + if (result != 0) // unable to write + { + free(filename_out); + return FALSE; + } + newline_to_file(filename_out, out_len); + + free(filename_out); + return TRUE; + } else // append to the output file + { + kol_struct_BDVK bdvk; + + k70_in.p00 = 5; + k70_in.p04 = 0LL; + k70_in.p12 = 0; + k70_in.p16 = (unsigned) &bdvk; + k70_in.p20 = 0; + k70_in.p21 = filename_out; + + result = kol_file_70(&k70_in); // get information about file + if ( 0 != result ) // file doesnt exist, then rewrite + { + k70_out.p00 = 2; + k70_out.p04 = 0; // offset + //k70_out.p08 = 0; + k70_out.p12 = out_len; + k70_out.p16 = (unsigned)text; + k70_out.p20 = 0; + k70_out.p21 = filename_out; + + result = kol_file_70(&k70_out); // write + if (result != 0) // unable to write + { + free(filename_out); + return FALSE; + } + newline_to_file(filename_out, out_len); + + free(filename_out); + return TRUE; + } + // if exists, append + unsigned long long filesize = bdvk.p32; + + k70_out.p00 = 3; + k70_out.p04 = filesize; // offset + //k70_out.p08 = 0; + k70_out.p12 = out_len; + k70_out.p16 = (unsigned)text; + k70_out.p20 = 0; + k70_out.p21 = filename_out; + + result = kol_file_70(&k70_out); // write + if (result != 0) // unable to write + { + free(filename_out); + return FALSE; + } + + newline_to_file(filename_out, filesize + out_len); + free(filename_out); + return TRUE; + } + } + //free(filename_out); + return TRUE; } diff --git a/programs/system/shell/globals.h b/programs/system/shell/globals.h index 35c7f23dae..dd19b85cb6 100644 --- a/programs/system/shell/globals.h +++ b/programs/system/shell/globals.h @@ -1,5 +1,5 @@ -#define SHELL_VERSION "0.8a" +#define SHELL_VERSION "0.8.1" extern char PATH[256]; extern char PARAM[256];