From 939f824d96c928620eed221a9e767451ddb5175c Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Sun, 14 Dec 2008 11:55:46 +0000 Subject: [PATCH] Shell 0.4 from Albom git-svn-id: svn://kolibrios.org@959 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/system/shell/.shell | 5 + programs/system/shell/all.h | 39 +++ programs/system/shell/cmd_about.c | 9 + programs/system/shell/cmd_alias.c | 17 ++ programs/system/shell/cmd_cd.c | 47 +++ programs/system/shell/cmd_date.c | 22 ++ programs/system/shell/cmd_echo.c | 6 + programs/system/shell/cmd_exit.c | 7 + programs/system/shell/cmd_free.c | 12 + programs/system/shell/cmd_help.c | 31 ++ programs/system/shell/cmd_kill.c | 43 +++ programs/system/shell/cmd_ls.c | 41 +++ programs/system/shell/cmd_mkdir.c | 44 +++ programs/system/shell/cmd_more.c | 87 ++++++ programs/system/shell/cmd_ps.c | 32 +++ programs/system/shell/cmd_pwd.c | 5 + programs/system/shell/cmd_reboot.c | 5 + programs/system/shell/cmd_rm.c | 50 ++++ programs/system/shell/cmd_rmdir.c | 46 +++ programs/system/shell/cmd_touch.c | 54 ++++ programs/system/shell/cmd_ver.c | 5 + programs/system/shell/compile.bat | 7 + programs/system/shell/console.c | 82 ++++++ programs/system/shell/globals.h | 82 ++++++ programs/system/shell/kolibri.h | 90 ++++++ programs/system/shell/kolibri.ld | 20 ++ programs/system/shell/module_alias.c | 133 +++++++++ programs/system/shell/module_command.c | 336 ++++++++++++++++++++++ programs/system/shell/module_executable.c | 60 ++++ programs/system/shell/module_program.c | 16 ++ programs/system/shell/module_script.c | 102 +++++++ programs/system/shell/prototypes.h | 27 ++ programs/system/shell/readme.txt | 27 ++ programs/system/shell/shell.c | 134 +++++++++ programs/system/shell/stdlib.h | 14 + programs/system/shell/string.h | 12 + 36 files changed, 1749 insertions(+) create mode 100644 programs/system/shell/.shell create mode 100644 programs/system/shell/all.h create mode 100644 programs/system/shell/cmd_about.c create mode 100644 programs/system/shell/cmd_alias.c create mode 100644 programs/system/shell/cmd_cd.c create mode 100644 programs/system/shell/cmd_date.c create mode 100644 programs/system/shell/cmd_echo.c create mode 100644 programs/system/shell/cmd_exit.c create mode 100644 programs/system/shell/cmd_free.c create mode 100644 programs/system/shell/cmd_help.c create mode 100644 programs/system/shell/cmd_kill.c create mode 100644 programs/system/shell/cmd_ls.c create mode 100644 programs/system/shell/cmd_mkdir.c create mode 100644 programs/system/shell/cmd_more.c create mode 100644 programs/system/shell/cmd_ps.c create mode 100644 programs/system/shell/cmd_pwd.c create mode 100644 programs/system/shell/cmd_reboot.c create mode 100644 programs/system/shell/cmd_rm.c create mode 100644 programs/system/shell/cmd_rmdir.c create mode 100644 programs/system/shell/cmd_touch.c create mode 100644 programs/system/shell/cmd_ver.c create mode 100644 programs/system/shell/compile.bat create mode 100644 programs/system/shell/console.c create mode 100644 programs/system/shell/globals.h create mode 100644 programs/system/shell/kolibri.h create mode 100644 programs/system/shell/kolibri.ld create mode 100644 programs/system/shell/module_alias.c create mode 100644 programs/system/shell/module_command.c create mode 100644 programs/system/shell/module_executable.c create mode 100644 programs/system/shell/module_program.c create mode 100644 programs/system/shell/module_script.c create mode 100644 programs/system/shell/prototypes.h create mode 100644 programs/system/shell/readme.txt create mode 100644 programs/system/shell/shell.c create mode 100644 programs/system/shell/stdlib.h create mode 100644 programs/system/shell/string.h diff --git a/programs/system/shell/.shell b/programs/system/shell/.shell new file mode 100644 index 0000000000..c65fcb04e5 --- /dev/null +++ b/programs/system/shell/.shell @@ -0,0 +1,5 @@ +#SHS + +about +echo Type 'help' for help +echo diff --git a/programs/system/shell/all.h b/programs/system/shell/all.h new file mode 100644 index 0000000000..116da9c41c --- /dev/null +++ b/programs/system/shell/all.h @@ -0,0 +1,39 @@ + +/// =========================================================== + +#include "kolibri.h" +#include "stdlib.h" +#include "string.h" + +#include "globals.h" +#include "prototypes.h" + +#include "console.c" + +#include "cmd_about.c" +#include "cmd_help.c" +#include "cmd_ver.c" +#include "cmd_pwd.c" +#include "cmd_ls.c" +#include "cmd_ps.c" +#include "cmd_kill.c" +#include "cmd_echo.c" +#include "cmd_date.c" +#include "cmd_exit.c" +#include "cmd_cd.c" +#include "cmd_free.c" +#include "cmd_reboot.c" +#include "cmd_mkdir.c" +#include "cmd_rmdir.c" +#include "cmd_rm.c" +#include "cmd_touch.c" +#include "cmd_alias.c" +#include "cmd_more.c" + +#include "module_command.c" +#include "module_program.c" +#include "module_script.c" +#include "module_executable.c" +#include "module_alias.c" + +/// =========================================================== diff --git a/programs/system/shell/cmd_about.c b/programs/system/shell/cmd_about.c new file mode 100644 index 0000000000..a97850e701 --- /dev/null +++ b/programs/system/shell/cmd_about.c @@ -0,0 +1,9 @@ + +void cmd_about() +{ +printf("\n\rShell for KolibriOS\n\r"); +printf(" version %s\n\r", SHELL_VERSION); +printf("\n\r author: Oleksandr Bogomaz aka Albom"); +printf("\n\r e-mail: albom85@yandex.ru"); +printf("\n\r site: http://albom06.boom.ru/\n\r\n\r"); +} diff --git a/programs/system/shell/cmd_alias.c b/programs/system/shell/cmd_alias.c new file mode 100644 index 0000000000..c8fe128903 --- /dev/null +++ b/programs/system/shell/cmd_alias.c @@ -0,0 +1,17 @@ + +void cmd_alias(char arg[]) +{ + +int result; + +if (NULL == arg) + { + alias_list(); + return; + } + +result = alias_check(arg); +if ( ( 0 != result ) && ( -1 != result ) ) + alias_add(arg); + +} diff --git a/programs/system/shell/cmd_cd.c b/programs/system/shell/cmd_cd.c new file mode 100644 index 0000000000..8a2322f384 --- /dev/null +++ b/programs/system/shell/cmd_cd.c @@ -0,0 +1,47 @@ + +int cmd_cd(char dir[]) +{ + +char temp[256]; +unsigned result; + +if (NULL == dir) + { + printf(" cd directory\n\r"); + return FALSE; + } + +if ( 0 == strcmp(dir, ".") ) + return FALSE; + +if ( ( 0 == strcmp(dir, "..") ) && ( 0 != strcmp(cur_dir, "/")) ) + { + cur_dir[strlen(cur_dir)-1]='\0'; + dir_truncate(cur_dir); + return FALSE; + } + +if ( '/' == dir[0]) + { + if ( dir_check(dir) ) + { + strcpy(cur_dir, dir); + return TRUE; + } + return FALSE; + } +else + { + strcpy(temp, cur_dir); + strcat(temp, dir); + + if ( dir_check(temp) ) + { + strcpy(cur_dir, temp); + return TRUE; + } + + return FALSE; + } + +} diff --git a/programs/system/shell/cmd_date.c b/programs/system/shell/cmd_date.c new file mode 100644 index 0000000000..8ec8c37ab9 --- /dev/null +++ b/programs/system/shell/cmd_date.c @@ -0,0 +1,22 @@ + + +void cmd_date() +{ +unsigned date; +unsigned time; + +date = kol_system_date_get(); +printf(" date [dd.mm.yy]: %x%x.%x%x.%x%x", + (date&0xf00000)>>20, (date&0xf0000)>>16, // day + (date&0xf000)>>12, (date&0xf00)>>8, //month + (date&0xf0)>>4, (date&0xf) ); // year + + +time = kol_system_time_get(); + +printf("\n\r time [hh:mm:ss]: %x%x:%x%x:%x%x\n\r", + (time&0xf0)>>4, (time&0xf), // hours + (time&0xf000)>>12, (time&0xf00)>>8, // minutes + (time&0xf00000)>>20, (time&0xf0000)>>16 ); // seconds + +} diff --git a/programs/system/shell/cmd_echo.c b/programs/system/shell/cmd_echo.c new file mode 100644 index 0000000000..ae1a69e115 --- /dev/null +++ b/programs/system/shell/cmd_echo.c @@ -0,0 +1,6 @@ + + +void cmd_echo(char text[]) +{ +printf("%s\n\r", text); +} diff --git a/programs/system/shell/cmd_exit.c b/programs/system/shell/cmd_exit.c new file mode 100644 index 0000000000..4aa66da509 --- /dev/null +++ b/programs/system/shell/cmd_exit.c @@ -0,0 +1,7 @@ + +void cmd_exit() +{ +free(ALIASES); +_exit(1); +kol_exit(); +} diff --git a/programs/system/shell/cmd_free.c b/programs/system/shell/cmd_free.c new file mode 100644 index 0000000000..2811aca64a --- /dev/null +++ b/programs/system/shell/cmd_free.c @@ -0,0 +1,12 @@ + +void cmd_free() +{ +unsigned total, free, used; + +total = kol_system_mem(); +free = kol_system_memfree(); +used = total - free; + +printf (" total [kB / MB / %%]: %-7d / %-5d / 100\n\r free [kB / MB / %%]: %-7d / %-5d / %d\n\r used [kB / MB / %%]: %-7d / %-5d / %d\n\r", + total, total/1024, free, free/1024, (free*100)/total, used, total/1024-free/1024, 100-(free*100)/total ); +} diff --git a/programs/system/shell/cmd_help.c b/programs/system/shell/cmd_help.c new file mode 100644 index 0000000000..9972e90ae7 --- /dev/null +++ b/programs/system/shell/cmd_help.c @@ -0,0 +1,31 @@ + +int cmd_help(char cmd[]) +{ + +int i; + +char available[]={" %d commands available:\n\r"}; + +if (NULL == cmd) + { + printf (available, NUM_OF_CMD); + for (i = 0; i < NUM_OF_CMD; i++) + printf(" %s\n\r", HELP_COMMANDS[i]); + } +else + { + for (i=0; i'9')) + return 0; + else + n = 10 * n + s[i] - '0'; + +return n; +} + + + +int cmd_kill(char process[]) +{ + +unsigned proc; +int result; + +if (NULL == process) + { + printf(" kill PID\n\r"); + return FALSE; + } +else + { + proc = _atoi(process); + if ( 0 != proc ) + { + result = kol_process_kill_pid(proc); + if (result < 0) + return FALSE; + else + return TRUE; + } + } + +} diff --git a/programs/system/shell/cmd_ls.c b/programs/system/shell/cmd_ls.c new file mode 100644 index 0000000000..bc3c10abd3 --- /dev/null +++ b/programs/system/shell/cmd_ls.c @@ -0,0 +1,41 @@ + +int cmd_ls(char dir[]) +{ + +kol_struct70 k70; +unsigned *n; +unsigned num_of_file; +unsigned *t; +unsigned type_of_file; +int i; + +k70.p00 = 1; +k70.p04 = 0; +k70.p08 = 0; +k70.p12 = 2*1024*1024; // 2 MB +k70.p16 = malloc(2*1024*1024); +k70.p20 = 0; +k70.p21 = dir; + +if ( !kol_file_70(&k70) ) // проверяем существование каталога + { + free(k70.p16); + return FALSE; + } + +n = k70.p16+8; +num_of_file = *n; // число файлов в каталоге + +for (i = 0; i < num_of_file; i++) + { + printf (" %s", k70.p16+32+40+(264+40)*i); + t = k70.p16+32+(264+40)*i; + type_of_file = *t; + if ( (0x10 == (type_of_file&0x10)) || (8 == (type_of_file&8)) ) + printf ("/"); + printf ("\n\r"); + } + +free(k70.p16); +return TRUE; +} diff --git a/programs/system/shell/cmd_mkdir.c b/programs/system/shell/cmd_mkdir.c new file mode 100644 index 0000000000..b2d2533eee --- /dev/null +++ b/programs/system/shell/cmd_mkdir.c @@ -0,0 +1,44 @@ + +int cmd_mkdir(char dir[]) +{ + +char temp[256]; +kol_struct70 k70; +unsigned result; + +if (NULL == dir) + { + printf(" mkdir directory\n\r"); + return FALSE; + } + + +if ( 0 == strcmp(dir, ".") || ( 0 == strcmp(dir, "..") ) || ( 0 == strcmp(cur_dir, "/")) ) + { + return FALSE; + } + +k70.p00 = 9; +k70.p04 = 0; +k70.p08 = 0; +k70.p12 = 0; +k70.p16 = 0; +k70.p20 = 0; + +if ( '/' == dir[0]) + k70.p21 = dir; +else + { + strcpy(temp, cur_dir); + strcat(temp, dir); + k70.p21 = temp; + } + +result = kol_file_70(&k70); + +if (0 == result) + return TRUE; +else + return FALSE; + +} diff --git a/programs/system/shell/cmd_more.c b/programs/system/shell/cmd_more.c new file mode 100644 index 0000000000..8fdee45820 --- /dev/null +++ b/programs/system/shell/cmd_more.c @@ -0,0 +1,87 @@ + +int cmd_more(char file[]) +{ + +kol_struct70 k70; +kol_struct_BDVK bdvk; +unsigned result, filesize, pos, i; +char buf[81]; //буфер +char temp[256]; +unsigned flags; + +if (NULL == file) + { + printf (" less filename\n\r"); + return FALSE; + } + +if ( '/' == file[0]) + { + strcpy(temp, file); + + if ( !file_check(temp) ) + { + return FALSE; + } + } +else + { + strcpy(temp, cur_dir); + strcat(temp, file); + + if ( !file_check(temp) ) + { + return FALSE; + } + } + +k70.p00 = 5; +k70.p04 = k70.p08 = k70.p12 = 0; +k70.p16 = &bdvk; +k70.p20 = 0; +k70.p21 = temp; + +result = kol_file_70(&k70); // получаем информацию о файле +if ( 0 != result ) + return FALSE; + +filesize = bdvk.p32[0]; // получаем размер файла + +buf[80]=0; +flags = con_get_flags(); + +for (pos=0;pos 255) + return FALSE; + +if ( !alias_split (alias, buf1, buf2)) + return FALSE; + +strcpy (ALIASES+256*ALIAS_NUM, buf1); +strcpy (ALIASES+256*ALIAS_NUM+64*1024, buf2); +ALIAS_NUM++; + +return TRUE; +} + +/// =========================================================== + +void alias_list() +{ + +unsigned i; + +if ( 0 == ALIAS_NUM) + return; + +for (i = 0; i < ALIAS_NUM; i++) + printf (" %s=%s\n\r",ALIASES+256*i, ALIASES+256*i+64*1024); +} + +/// =========================================================== + +int alias_split (char alias[], char s1[], char s2[]) +{ + +unsigned i, j; + +if (NULL == strchr(alias, '=')) + return FALSE; + +for (i=0, j = 0;; i++) + { + if ('=' == alias[i]) + { + s1[i]='\0'; + break; + } + s1[i]=alias[i]; + } + +j = 0; + +for (;; i++, j++) + { + s2[j]=alias[i]; + if ('\0' == alias[i]) + break; + } + +trim(s1); + +for (i=0;;i++) + { + s2[i] = s2[i+1]; + if ('\0' == s2[i] ) + break; + } + +trim(s2); + +return TRUE; + +} + +/// =========================================================== diff --git a/programs/system/shell/module_command.c b/programs/system/shell/module_command.c new file mode 100644 index 0000000000..a8478b4b6e --- /dev/null +++ b/programs/system/shell/module_command.c @@ -0,0 +1,336 @@ + +/// =========================================================== + +void command_clear() +{ +for (;CMD_POS;CMD_POS--) + printf("%c %c", 8, 8); +CMD[0]='\0'; +} + +/// =========================================================== + +void command_history_add(char command[]) +{ + +if ( (0 != strcmp( CMD_HISTORY[0], CMD)) && + (0 != strcmp( CMD_HISTORY[1], CMD)) && + (0 != strcmp( CMD_HISTORY[2], CMD)) && + (0 != strcmp( CMD_HISTORY[3], CMD)) && + (0 != strcmp( CMD_HISTORY[4], CMD)) ) + + { + strcpy(CMD_HISTORY[4], CMD_HISTORY[3]); + strcpy(CMD_HISTORY[3], CMD_HISTORY[2]); + strcpy(CMD_HISTORY[2], CMD_HISTORY[1]); + strcpy(CMD_HISTORY[1], CMD_HISTORY[0]); + + strcpy(CMD_HISTORY[0], CMD); + } + +} + +/// =========================================================== + +void command_get() +{ +unsigned key; +//unsigned pos = 0; +int hist; + +CMD_POS = 0; +CMD_NUM = 0; + +for (;;) + { + key = getch(); + if ( 0 != (key & 0xff) ) + { + key &= 0xff; + switch (key) + { + case 27: // ESC + command_clear(); + break; + + case 13: // ENTER + CMD[CMD_POS] = '\0'; + printf("\n\r"); + + command_history_add(CMD); + return; + + case 8: // BACKSPACE + if (CMD_POS > 0) + { + printf ("%c %c", 8, 8); + CMD_POS--; + } + break; + + case 9: // TAB + break; + + default: + if (CMD_POS < 255) + { + CMD[CMD_POS] = key; + CMD_POS++; + printf("%c", key); + } + break; + }; + } + else // обработка расширенных клавиш + { + key = (key>>8)&0xff; +// printf ("%d\n\r", key); + + switch (key) + { + + case 72: // UP + for (hist = 0; hist < CMD_HISTORY_NUM; hist++) + { + command_clear(); + + if (CMD_NUM < CMD_HISTORY_NUM-1) + CMD_NUM++; + else + CMD_NUM = 0; + + printf( CMD_HISTORY[CMD_NUM] ); + strcpy(CMD, CMD_HISTORY[CMD_NUM]); + CMD_POS = strlen(CMD); + + if (0!=strlen(CMD)) + break; + } + + break; + + case 80: // DOWN + for (hist = 0; hist < CMD_HISTORY_NUM; hist++) + { + command_clear(); + + if (CMD_NUM > 0) + CMD_NUM--; + else + CMD_NUM = CMD_HISTORY_NUM-1; + + printf( CMD_HISTORY[CMD_NUM] ); + strcpy(CMD, CMD_HISTORY[CMD_NUM]); + CMD_POS = strlen(CMD); + + if (0!=strlen(CMD)) + break; + } + break; + + }; + } + + } +} + + +/// =========================================================== + +int command_get_cmd(char cmd[]) +{ +unsigned i; +for (i=0;;i++) + { + cmd[i] = CMD[i]; + if (0 == cmd[i]) + { + i = -2; + break; + } + if ( iswhite(cmd[i]) ) + { + cmd[i] = '\0'; + break; + } + } +return i+1; +} + +/// =========================================================== + +void command_execute() +{ +char cmd[256]; +char args[256]; +unsigned arg; + +trim(CMD); +arg = command_get_cmd(cmd); + +if ( !strlen(cmd) ) + return; + +strcpy(args, CMD+arg); +trim(args); + +if ( !strcmp(cmd, "help") ) + { + if (-1 == arg) + cmd_help(NULL); + else + cmd_help(args); + return; + } + +if ( !strcmp(cmd, "ver") ) + { + cmd_ver(); + return; + } + +if ( !strcmp(cmd, "cd") ) + { + if (-1 == arg) + cmd_cd(NULL); + else + cmd_cd(args); + + if ( '/' != cur_dir[strlen(cur_dir)-1] ) + strcat(cur_dir, "/"); + + return; + } + +if ( !strcmp(cmd, "ls") ) + { + if (-1 == arg) + cmd_ls(cur_dir); + else + cmd_ls(args); + return; + } + +if ( !strcmp(cmd, "ps") ) + { + cmd_ps(); + return; + } + +if ( !strcmp(cmd, "kill") ) + { + if (-1 == arg) + cmd_kill(NULL); + else + cmd_kill(args); + return; + } + +if ( !strcmp(cmd, "pwd") ) + { + cmd_pwd(); + return; + } + +if ( !strcmp(cmd, "mkdir") ) + { + if (-1 == arg) + cmd_mkdir(NULL); + else + cmd_mkdir(args); + return; + } + +if ( !strcmp(cmd, "rmdir") ) + { + if (-1 == arg) + cmd_rmdir(NULL); + else + cmd_rmdir(args); + return; + } + +if ( !strcmp(cmd, "rm") ) + { + if (-1 == arg) + cmd_rm(NULL); + else + cmd_rm(args); + return; + } + +if ( !strcmp(cmd, "more") ) + { + if (-1 == arg) + cmd_more(NULL); + else + cmd_more(args); + return; + } + +if ( !strcmp(cmd, "echo") ) + { + cmd_echo(args); + return; + } + +if ( !strcmp(cmd, "exit") ) + { + cmd_exit(); + return; + } + +if ( !strcmp(cmd, "date") ) + { + cmd_date(); + return; + } + +if ( !strcmp(cmd, "about") ) + { + cmd_about(); + return; + } + +if ( !strcmp(cmd, "free") ) + { + cmd_free(); + return; + } + +if ( !strcmp(cmd, "reboot") ) + { + cmd_reboot(); + return; + } + +if ( !strcmp(cmd, "touch") ) + { + if (-1 == arg) + cmd_touch(NULL); + else + cmd_touch(args); + return; + } + +if ( !strcmp(cmd, "alias") ) + { + if (-1 == arg) + cmd_alias(NULL); + else + cmd_alias(args); + return; + } + +if ( -1 != alias_search(CMD) ) + { + strcpy(CMD, ALIASES+64*1024+256*alias_search(CMD)); + command_execute(); + return; + } + +executable_run(cmd, args); + +} + +/// =========================================================== diff --git a/programs/system/shell/module_executable.c b/programs/system/shell/module_executable.c new file mode 100644 index 0000000000..78a9e80164 --- /dev/null +++ b/programs/system/shell/module_executable.c @@ -0,0 +1,60 @@ + +/// =========================================================== + +int executable_run(char cmd[], char args[]) +{ + +char exec[256]; +char error_starting[]={" No such command '%s'.\n\r"}; +int result; + +if ( '/' == cmd[0]) // если путь абсолбтный + { + strcpy(exec, cmd); + + if ( !file_check(exec) ) // проверяем существование файла + { + printf(error_starting, cmd); + return FALSE; + } + } + +else + { + strcpy(exec, cur_dir); // проверяем файл в текущем каталоге + strcat(exec, cmd); + + if ( !file_check(exec) ) // проверяем существование файла + { + strcpy(exec, "/rd/1/"); // проверяем файл на виртуальном диске + strcat(exec, cmd); + if ( !file_check(exec) ) // проверяем существование файла + { + printf(error_starting, cmd); + return FALSE; + } + } + } + + +if ( script_check(exec) ) + { + return script_run(exec, args); + } + +/* запуск программы */ +result = program_run(exec, args); +if (result > 0) + { + printf (" '%s' started. PID = %d\n\r", cmd, result); + return TRUE; + } +else + { + printf(error_starting, cmd); + return FALSE; + } + +} + +/// =========================================================== diff --git a/programs/system/shell/module_program.c b/programs/system/shell/module_program.c new file mode 100644 index 0000000000..6bbbe2685a --- /dev/null +++ b/programs/system/shell/module_program.c @@ -0,0 +1,16 @@ + +int program_run(char cmd[], char param[]) +{ + +kol_struct70 k70; + +k70.p00 = 7; +k70.p04 = 0; +k70.p08 = param; +k70.p12 = 0; +k70.p16 = 0; +k70.p20 = 0; +k70.p21 = cmd; + +return kol_file_70(&k70); +} diff --git a/programs/system/shell/module_script.c b/programs/system/shell/module_script.c new file mode 100644 index 0000000000..4abc0ee726 --- /dev/null +++ b/programs/system/shell/module_script.c @@ -0,0 +1,102 @@ + +/// =========================================================== + +int script_check(char file[]) +{ + +kol_struct70 k70; +char buf[4]; + +k70.p00 = 0; +k70.p04 = 0; +k70.p08 = 0; +k70.p12 = 4; // читать 4 байта +k70.p16 = buf; +k70.p20 = 0; +k70.p21 = file; + +kol_file_70(&k70); + +if ( !strcmp(buf, script_sign) ) + return TRUE; +else + return FALSE; +} + +/// =========================================================== + +int script_run(char exec[], char args[]) +{ + +kol_struct70 k70; +kol_struct_BDVK bdvk; +unsigned result, filesize, pos, i; +char *buf; //буфер, куда копируется скрипт + +k70.p00 = 5; +k70.p04 = k70.p08 = k70.p12 = 0; +k70.p16 = &bdvk; +k70.p20 = 0; +k70.p21 = exec; + +result = kol_file_70(&k70); // получаем информацию о файле +if ( 0 != result ) + return FALSE; + +filesize = bdvk.p32[0]; // получаем размер файла + +buf = malloc(filesize+256); +if (NULL == buf) + return FALSE; + +buf[filesize]=0; + +k70.p00 = 0; +k70.p04 = k70.p08 = 0; +k70.p12 = filesize; +k70.p16 = buf; +k70.p20 = 0; +k70.p21 = exec; + +result = kol_file_70(&k70); // считываем файл в буфер +if ( 0 != result ) + { + free(buf); + return FALSE; + } + +pos = 0; + +for (;;) // обработка скрипта + { + + if (pos > filesize) + break; + + for (i=0;;i++) // считывание строки + { + if ((0x0A == buf[pos])||(0x0D == buf[pos])||(0 == buf[pos])) + { + pos++; + CMD[i] = '\0'; + break; + } + CMD[i] = buf[pos]; + pos++; + } + + if ( 0 == strlen(CMD) ) // пустая строка + continue; + + if ('#' == CMD[0]) // комментарий + continue; + + command_execute(); + + } + +free(buf); +return TRUE; +} + +/// =========================================================== diff --git a/programs/system/shell/prototypes.h b/programs/system/shell/prototypes.h new file mode 100644 index 0000000000..00b2779450 --- /dev/null +++ b/programs/system/shell/prototypes.h @@ -0,0 +1,27 @@ + +/// =========================================================== + +int file_check(char file[]); +int dir_check(char dir[]); +void dir_truncate(char dir[]); +int iswhite(char c); +void trim(char string[]); +void execute(); +void kol_main(); + +int executable_run(char cmd[], char args[]); + +void command_execute(); +void command_get(); +int command_get_cmd(char cmd[]); + +int script_check(char file[]); +int script_run(char exec[], char args[]); + +int aliases_check(char alias[]); +int alias_search(char alias[]); +int alias_add(char alias[]); +int alias_split (char alias[], char s1[], char s2[]); +void alias_list(); + +/// =========================================================== diff --git a/programs/system/shell/readme.txt b/programs/system/shell/readme.txt new file mode 100644 index 0000000000..0b53955ea2 --- /dev/null +++ b/programs/system/shell/readme.txt @@ -0,0 +1,27 @@ +Shell for KolibriOS + version 0.4 + + author: Oleksandr Bogomaz aka Albom + e-mail: albom85@yandex.ru + site: http://albom06.boom.ru/ + +Available commands: + about Displays information about the program + alias Allows the user view the current aliases + cd Changes directories + date Returns the date and time + echo Echoes the data to the screen + exit Exits program + free Displays total, free and used memory + help Gives help + kill Stops a running process + ls Lists the files in a directory + mkdir Makes directory + more Displays a data file to the screen + ps Lists the current processes running + pwd Displays the name of the working directory + reboot Reboots the computer + rm Removes files + rmdir Removes directories + touch Creates an empty file or updates the time/date stamp on a file + ver Displays version diff --git a/programs/system/shell/shell.c b/programs/system/shell/shell.c new file mode 100644 index 0000000000..9a104f82a9 --- /dev/null +++ b/programs/system/shell/shell.c @@ -0,0 +1,134 @@ + +#include "all.h" + +/// =========================================================== + +int dir_check(char dir[]) +{ +kol_struct70 k70; +int result; + +k70.p00 = 1; +k70.p04 = 0; +k70.p08 = 0; +k70.p12 = 2*1024*1024; // 2 MB +k70.p16 = malloc(2*1024*1024); +k70.p20 = 0; +k70.p21 = dir; + +result = kol_file_70(&k70); + +free(k70.p16); + +if ( (0 == result)||(6 == result) ) + return TRUE; +else + return FALSE; + +} + +/// =========================================================== + +void dir_truncate(char dir[]) +{ +int i; +i = strlen(dir)-1; +for (;;i--) + if ('/' == dir[i]) + { + dir[i+1] = 0; + break; + } +} + +/// =========================================================== + +int file_check(char file[]) +{ +kol_struct70 k70; +int result; + +k70.p00 = 0; +k70.p04 = 0; +k70.p08 = 0; +k70.p12 = 0; +k70.p16 = 0; +k70.p20 = 0; +k70.p21 = file; + +result = kol_file_70(&k70); + +if (0 == result) + return TRUE; +else + return FALSE; +} + +/// =========================================================== + +int iswhite(char c) +{ +return ((' ' == c) || ('\t' == c) || (13 == c) || (10 == c)); +} + +/// =========================================================== + +void trim(char string[]) +{ +int i, j; + +for (i=0; ;i++) + if ( !iswhite(string[i]) ) + break; +j = 0; +for (;;i++, j++) + { + string[j] = string[i]; + if ('\0' == string[i] ) + break; + } + +for (i=0; ;i++) + if ('\0' == string[i]) + break; +i--; +for (;i>0;--i) + if ( iswhite(string[i]) ) + string[i] = '\0'; + else + break; +} + +/// =========================================================== + +void kol_main() +{ + +strcpy(title, "SHELL "); +strcat(title, SHELL_VERSION); +CONSOLE_INIT(title); + +strcpy(cur_dir, PATH); +dir_truncate(cur_dir); + +con_set_cursor_height(con_get_font_height()-1); + +if (strlen(PARAM) > 0) + strcpy(CMD, PARAM); +else + strcpy(CMD, ".shell"); + +command_execute(); + +for (;;) + { + printf ("# "); + command_get(); + command_execute(); + } + +_exit(0); +kol_exit(); +} + +/// =========================================================== diff --git a/programs/system/shell/stdlib.h b/programs/system/shell/stdlib.h new file mode 100644 index 0000000000..e984555604 --- /dev/null +++ b/programs/system/shell/stdlib.h @@ -0,0 +1,14 @@ + +#define RAND_MAX 0x7FFFU + +#define isspace(c) ((c)==' ') +#define abs(i) (((i)<0)?(-(i)):(i)) + +#define random(num) ((rand()*(num))/((RAND_MAX+1))) + +void* malloc(unsigned size); +void free(void *pointer); +void* realloc(void* pointer, unsigned size); + +void srand (unsigned seed); +int rand (void); diff --git a/programs/system/shell/string.h b/programs/system/shell/string.h new file mode 100644 index 0000000000..4bb934aa10 --- /dev/null +++ b/programs/system/shell/string.h @@ -0,0 +1,12 @@ + +#define NULL ((void*)0) + +void* memset(void *mem, int c, unsigned size); +void* memcpy(void *dst, const void *src, unsigned size); + +void strcat(char strDest[], char strSource[]); +int strcmp(const char* string1, const char* string2); +void strcpy(char strDest[], const char strSource[]); +char* strncpy(char *strDest, const char *strSource, unsigned n); +int strlen(const char* string); +char *strchr(const char* string, int c);