diff --git a/src/kernel/apps/shell/commands.c b/src/kernel/apps/shell/commands.c index 824187ab93..f41641cbf1 100644 --- a/src/kernel/apps/shell/commands.c +++ b/src/kernel/apps/shell/commands.c @@ -27,12 +27,16 @@ struct command cmds[] = { {NULL, NULL} }; -int cmd_exec(int argc, char *argv[]) + +int +cmd_exec(int argc, char *argv[]) { return cmd_create_proc(argc - 1,argv+1); } -int cmd_create_proc(int argc,char *argv[]) + +int +cmd_create_proc(int argc,char *argv[]) { bool must_wait=true; team_id pid; @@ -41,7 +45,7 @@ int cmd_create_proc(int argc,char *argv[]) char *tmp; char filename[SCAN_SIZE+1]; - if(argc <1){ + if (argc < 1) { printf("not enough args to exec\n"); return 0; } @@ -70,13 +74,10 @@ int cmd_create_proc(int argc,char *argv[]) } } - pid = sys_create_team(filename,filename, argv, argc, NULL, 0, 5); - if(pid >= 0) { - int retcode; - - if(must_wait) { - sys_wait_on_team(pid, &retcode); - } + pid = _kern_create_team(filename,filename, argv, argc, NULL, 0, 5); + if (pid >= 0) { + if (must_wait) + _kern_wait_for_team(pid, NULL); } else { printf("Error: cannot execute '%s'\n", filename); return 0; // should be -1, but the shell would exit @@ -85,7 +86,9 @@ int cmd_create_proc(int argc,char *argv[]) return 0; } -int cmd_mkdir(int argc, char *argv[]) + +int +cmd_mkdir(int argc, char *argv[]) { int rc; @@ -104,38 +107,42 @@ int cmd_mkdir(int argc, char *argv[]) return 0; } -int cmd_cat(int argc, char *argv[]) + +int +cmd_cat(int argc, char *argv[]) { int rc; int fd; char buf[257]; - if(argc < 2) { + if (argc < 2) { printf("not enough arguments to cat\n"); return 0; } - fd = sys_open(argv[1], 0); - if(fd < 0) { + fd = open(argv[1], 0); + if (fd < 0) { printf("cat: sys_open() returned error: %s!\n", strerror(fd)); goto done_cat; } for(;;) { - rc = sys_read(fd, -1, buf, sizeof(buf) -1); - if(rc <= 0) + rc = read_pos(fd, -1, buf, sizeof(buf) - 1); + if (rc <= 0) break; buf[rc] = '\0'; printf("%s", buf); } - sys_close(fd); + close(fd); done_cat: return 0; } -int cmd_cd(int argc, char *argv[]) + +int +cmd_cd(int argc, char *argv[]) { int rc; @@ -151,7 +158,9 @@ int cmd_cd(int argc, char *argv[]) return 0; } -int cmd_pwd(int argc, char *argv[]) + +int +cmd_pwd(int argc, char *argv[]) { char buffer[SYS_MAX_PATH_LEN]; @@ -165,7 +174,9 @@ int cmd_pwd(int argc, char *argv[]) return 0; } -int cmd_stat(int argc, char *argv[]) + +int +cmd_stat(int argc, char *argv[]) { int rc; struct stat st; @@ -187,7 +198,9 @@ int cmd_stat(int argc, char *argv[]) return 0; } -int cmd_kill(int argc, char *argv[]) + +int +cmd_kill(int argc, char *argv[]) { int rc; @@ -195,13 +208,15 @@ int cmd_kill(int argc, char *argv[]) printf("not enough arguments to kill\n"); return 0; } - rc = sys_send_signal(atoi(argv[2]), atoi(argv[1])); + rc = send_signal(atoi(argv[2]), atoi(argv[1])); if (rc) printf("kill failed\n"); return 0; } -int cmd_help(int argc, char *argv[]) + +int +cmd_help(int argc, char *argv[]) { printf("command list:\n\n"); printf("exit : quits this copy of the shell\n"); @@ -219,4 +234,3 @@ int cmd_help(int argc, char *argv[]) return 0; } - diff --git a/src/kernel/apps/shell/file_utils.c b/src/kernel/apps/shell/file_utils.c index 4de9546aad..65536c93d8 100644 --- a/src/kernel/apps/shell/file_utils.c +++ b/src/kernel/apps/shell/file_utils.c @@ -1,4 +1,3 @@ -//#include #include #include #include @@ -6,12 +5,15 @@ #include #include #include +#include #include "file_utils.h" #include "statements.h" #include "shell_defs.h" -bool combine_path(const char *path1,const char *path2,char *out,unsigned int max_len) + +bool +combine_path(const char *path1,const char *path2,char *out,unsigned int max_len) { unsigned int cur_len = 0; @@ -27,61 +29,74 @@ bool combine_path(const char *path1,const char *path2,char *out,unsigned int max cur_len++; } } - if(strlen(path2)+cur_len > max_len) return false; + if (strlen(path2)+cur_len > max_len) + return false; + strcat(out,path2); return true; } -bool exists_file(const char *file_name) + +bool +exists_file(const char *file_name) { - int handle = sys_open(file_name,0); + int handle = open(file_name, 0); bool exists; - exists =( handle >= 0); - if(exists) sys_close(handle); + exists = (handle >= 0); + if (exists) + close(handle); + return exists; } -bool find_file_in_path(const char *file_name,char *found_name,unsigned int max_size) -{ - char path[SCAN_SIZE+1]; - int cnt=0; - if(strchr(file_name,'/') != NULL){ +bool +find_file_in_path(const char *file_name, char *found_name, unsigned int max_size) +{ + char path[SCAN_SIZE + 1]; + int cnt = 0; + + if (strchr(file_name, '/') != NULL) { strncpy(found_name,file_name,max_size); found_name[max_size] = 0; - if(exists_file(found_name)) return true; + if (exists_file(found_name)) + return true; found_name[0] = 0; return false; } - while(get_path(cnt,path,SCAN_SIZE)){ - if(combine_path(path,file_name,found_name,max_size)){ - if(exists_file(found_name)) return true; + while (get_path(cnt, path, SCAN_SIZE)) { + if (combine_path(path, file_name, found_name, max_size)) { + if (exists_file(found_name)) + return true; } cnt++; } - found_name[0] =0; - return(false); + found_name[0] = 0; + return false; } -int exec_file(int argc,char *argv[],int *retcode) +int +exec_file(int argc, char *argv[], int *retcode) { char filename[255]; int pid; - if( !find_file_in_path(argv[0],filename,SCAN_SIZE)) return SHE_FILE_NOT_FOUND; + if (!find_file_in_path(argv[0], filename, SCAN_SIZE)) + return SHE_FILE_NOT_FOUND; - pid = sys_create_team(filename,filename, argv, argc, NULL, 0, 5); + pid = _kern_create_team(filename, filename, argv, argc, NULL, 0, 5); + if (pid < 0) + return SHE_CANT_EXECUTE; - if(pid < 0) return SHE_CANT_EXECUTE; - - sys_wait_on_team(pid, retcode); + _kern_wait_for_team(pid, retcode); return SHE_NO_ERROR; } -int read_file_in_buffer(const char *filename,char **buffer) +int +read_file_in_buffer(const char *filename,char **buffer) { struct stat stat; int file_no; @@ -102,9 +117,9 @@ int read_file_in_buffer(const char *filename,char **buffer) if (*buffer == NULL) return ENOMEM; - size = sys_read(file_no, 0, *buffer, stat.st_size); + size = read(file_no, *buffer, stat.st_size); - sys_close(file_no); + close(file_no); if (size < 0) free(*buffer); diff --git a/src/kernel/apps/shell/main.c b/src/kernel/apps/shell/main.c index da6a84eb3e..bbd85661b4 100644 --- a/src/kernel/apps/shell/main.c +++ b/src/kernel/apps/shell/main.c @@ -19,7 +19,8 @@ #include "shell_history.h" -static int readline(char *buf, int len) +static int +readline(char *buf, int len) { int i = 0; char ch; @@ -64,7 +65,8 @@ static int readline(char *buf, int len) } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { char buf[1024]; @@ -75,7 +77,7 @@ int main(int argc, char *argv[]) if (af_script_file_name) { run_script(af_script_file_name); if (af_exit_after_script) - sys_exit(0); + exit(0); } printf("Welcome to the OpenBeOS shell\n"); @@ -86,9 +88,8 @@ int main(int argc, char *argv[]) printf("$ "); chars_read = readline(buf, sizeof(buf)); - if (chars_read > 0) { + if (chars_read > 0) parse_string(buf); - } } printf("shell exiting\n"); diff --git a/src/kernel/apps/shell/statements.c b/src/kernel/apps/shell/statements.c index 9279a96a5b..0f541afb8f 100644 --- a/src/kernel/apps/shell/statements.c +++ b/src/kernel/apps/shell/statements.c @@ -24,109 +24,96 @@ typedef struct _oper_level oper_level; #define MAX_LEVEL 3 -oper_level oper_levels[]= -{{SVO_EQUAL,3} -,{SVO_LESS,3} -,{SVO_LESS_E,3} -,{SVO_BIGGER,3} -,{SVO_BIGGER_E,3} -,{SVO_NOT_EQUAL,3} -,{SVO_ADD,2} -,{SVO_SUB,2} -,{SVO_MUL,1} -,{SVO_DIV,1} -,{0,-1} +oper_level oper_levels[] = { + {SVO_EQUAL,3}, + {SVO_LESS,3}, + {SVO_LESS_E,3}, + {SVO_BIGGER,3}, + {SVO_BIGGER_E,3}, + {SVO_NOT_EQUAL,3}, + {SVO_ADD,2}, + {SVO_SUB,2}, + {SVO_MUL,1}, + {SVO_DIV,1}, + {0,-1} }; -static int get_oper_level(int oper) +static int +get_oper_level(int oper) { int cnt = 0; - while((oper_levels[cnt].level != 0) && (oper_levels[cnt].sym_code != oper)) cnt++; + while ((oper_levels[cnt].level != 0) && (oper_levels[cnt].sym_code != oper)) + cnt++; + return oper_levels[cnt].level; } -static int parse_neg(scan_info *info,shell_value **out) +static int +parse_neg(scan_info *info,shell_value **out) { int err; bool do_neg = false; - if(info->sym_code == SVO_SUB){ - + if (info->sym_code == SVO_SUB){ do_neg = true; - if(scan(info)) return SHE_SCAN_ERROR; - + if (scan(info)) + return SHE_SCAN_ERROR; } err = parse_value(info,out); - if(err != SHE_NO_ERROR) return err; - - if(do_neg){ + if (err != SHE_NO_ERROR) + return err; + if (do_neg) { err = shell_value_neg(*out); - if(err != SHE_NO_ERROR) { - + if (err != SHE_NO_ERROR) { shell_value_free(*out); *out = NULL; - } - } return err; } -static int parse_oper(scan_info *info,shell_value **out,int level) +static int +parse_oper(scan_info *info,shell_value **out,int level) { int err; int oper_code; shell_value *other; + if (level > 1) + err = parse_oper(info, out, level - 1); + else + err = parse_neg(info, out); - if(level > 1){ - - err = parse_oper(info,out,level - 1); - - } else { - - err = parse_neg(info,out); - - } - - - if(err != SHE_NO_ERROR) return err; - + if (err != SHE_NO_ERROR) + return err; oper_code = info->sym_code; - while(get_oper_level(info->sym_code) == level){ - - if(scan(info)){ - + while (get_oper_level(info->sym_code) == level) { + if (scan(info)) { err = SHE_SCAN_ERROR; goto err; - } - if(level > 1){ - + if (level > 1) err = parse_oper(info,&other,level - 1); - - } else { - + else err = parse_neg(info,&other); - } + if (err != SHE_NO_ERROR) + goto err; - if(err != SHE_NO_ERROR) goto err; - - err = shell_value_do_operation(*out,other,oper_code); - if(err != SHE_NO_ERROR) goto err_2; + err = shell_value_do_operation(*out,other,oper_code); + if (err != SHE_NO_ERROR) + goto err_2; shell_value_free(other); - } return SHE_NO_ERROR; @@ -142,56 +129,57 @@ err: } -static int parse_expr(scan_info *info,shell_value **out) +static int +parse_expr(scan_info *info,shell_value **out) { (*out) = NULL; return parse_oper(info,out,MAX_LEVEL); } -static int parse_cast(scan_info * info,shell_value **out) +static int +parse_cast(scan_info * info,shell_value **out) { int err; long int dummy; char *text; bool to_number = info->sym_code == SVO_NUMBER_CAST; - if(scan(info)) return SHE_SCAN_ERROR; - + if (scan(info)) + return SHE_SCAN_ERROR; err = parse_rvl_expr(info,out); - if(err != SHE_NO_ERROR) return err; + if (err != SHE_NO_ERROR) + return err; -//place cast in shell_vars=>change type direct - - if(to_number){ + // place cast in shell_vars=>change type direct + if (to_number) { err = shell_value_to_number(*out,&dummy); - if(err != SHE_NO_ERROR) goto err; + if (err != SHE_NO_ERROR) + goto err; err = shell_value_set_number(*out,dummy); - if(err != SHE_NO_ERROR) goto err; - + if (err != SHE_NO_ERROR) + goto err; } else { - text = shell_value_to_char(*out); - err = shell_value_set_text(*out,text); free(text); - } - return SHE_NO_ERROR; + return SHE_NO_ERROR; err: - shell_value_free(*out); *out = NULL; return err; } -static int parse_value(scan_info *info,shell_value **out) + +static int +parse_value(scan_info *info,shell_value **out) { char buf2[SCAN_SIZE+1]; int err = SHE_NO_ERROR; @@ -200,10 +188,8 @@ static int parse_value(scan_info *info,shell_value **out) *out = NULL; - switch(info->sym_code){ - + switch (info->sym_code){ case SVO_DQ_STRING: - parse_vars_in_string(info->token,buf2,SCAN_SIZE); *out = shell_value_init_text(buf2); break; @@ -214,7 +200,6 @@ static int parse_value(scan_info *info,shell_value **out) break; case SVO_SQ_STRING: - *out = shell_value_init_text(info->token); break; @@ -229,118 +214,110 @@ static int parse_value(scan_info *info,shell_value **out) case SVO_PARENL: err = parse_rvl_expr(info,out); - if(err) return err; - + if (err) + return err; break; - case SVO_DOLLAR: - if(scan(info)) return SHE_SCAN_ERROR; - - if(info->sym_code == SVO_IDENT){ - - value =get_value_by_name(info->token); - - if(value != NULL){ + if (scan(info)) + return SHE_SCAN_ERROR; + if (info->sym_code == SVO_IDENT) { + value = get_value_by_name(info->token); + if (value != NULL) *out = shell_value_clone(value); - - } else { - + else *out = shell_value_init_text(""); - - } - - } else { - + } else err = SVO_IDENT; - - } break; default: return SHE_PARSE_ERROR; } - - - if(err == SHE_NO_ERROR){ - - if(scan(info)){ - if(*out != NULL) shell_value_free(*out); - return SHE_SCAN_ERROR; + if (err == SHE_NO_ERROR) { + if (scan(info)) { + if (*out != NULL) + shell_value_free(*out); + return SHE_SCAN_ERROR; } - if(*out == NULL) err = SHE_NO_MEMORY; - + if (*out == NULL) + err = SHE_NO_MEMORY; } return err; } -static int handle_exit(scan_info *info) + +static int +handle_exit(scan_info *info) { long int exit_value = 0; int err = SHE_NO_ERROR; shell_value *expr; - if(info->sym_code == SVO_PARENL){ + if (info->sym_code == SVO_PARENL) { + if (scan(info)) + return SHE_SCAN_ERROR; - if(scan(info)) return SHE_SCAN_ERROR; + err = parse_expr(info, &expr); + if (err != SHE_NO_ERROR) + return err; - err = parse_expr(info,&expr); - if(err != SHE_NO_ERROR) return err; - - if(!(expr->isnumber)){ + if (!(expr->isnumber)) { err = SHE_INVALID_TYPE; goto err; } - err = shell_value_get_number(expr,&exit_value); - if(err != SHE_NO_ERROR) goto err; + err = shell_value_get_number(expr, &exit_value); + if (err != SHE_NO_ERROR) + goto err; - if(expect(info,SVO_PARENR)){ + if (expect(info, SVO_PARENR)) { err = SVO_PARENR; goto err; } - } - sys_exit(exit_value); - + exit(exit_value); err: shell_value_free(expr); return err; } -static int parse_rvl_expr(scan_info *info,shell_value **out) + +static int +parse_rvl_expr(scan_info *info,shell_value **out) { int err = SHE_NO_ERROR; *out = NULL; - if(expect(info,SVO_PARENL)) return SVO_PARENL; + if (expect(info, SVO_PARENL)) + return SVO_PARENL; - err = parse_expr(info,out); - - if(err != SHE_NO_ERROR) return err; - - if(expect(info,SVO_PARENR)) { + err = parse_expr(info, out); + if (err != SHE_NO_ERROR) + return err; + if (expect(info, SVO_PARENR)) { err = SVO_PARENR; goto err; - } return SHE_NO_ERROR; + err: shell_value_free(*out); - return err; } -static int handle_if(scan_info *info) + +static int +handle_if(scan_info *info) { int err = SHE_NO_ERROR; long int value; @@ -348,19 +325,20 @@ static int handle_if(scan_info *info) shell_value *expr; err = parse_rvl_expr(info,&expr); - if(err != SHE_NO_ERROR) return err; - - if(!expr->isnumber){ + if (err != SHE_NO_ERROR) + return err; + if (!expr->isnumber) { err = SHE_INVALID_TYPE; goto err; - } err = shell_value_get_number(expr,&value); - if(err != SHE_NO_ERROR) goto err; + if (err != SHE_NO_ERROR) + goto err; - if(value != 0) err = parse_info(info); + if (value != 0) + err = parse_info(info); err: shell_value_free(expr); @@ -372,7 +350,8 @@ err: // Parse exec function // -static int handle_exec(scan_info *info,shell_value **out) +static int +handle_exec(scan_info *info,shell_value **out) { shell_value *state; int argc; @@ -385,19 +364,22 @@ static int handle_exec(scan_info *info,shell_value **out) *out = NULL; - if(scan(info)) return SHE_SCAN_ERROR; + if (scan(info)) + return SHE_SCAN_ERROR; - err = parse_rvl_expr(info,&state); - if(err != SHE_NO_ERROR) return err; + err = parse_rvl_expr(info, &state); + if (err != SHE_NO_ERROR) + return err; - err = shell_value_get_text(state,&statement); - if(err != SHE_NO_ERROR) return err; + err = shell_value_get_text(state, &statement); + if (err != SHE_NO_ERROR) + return err; - argc = parse_line(statement,argv,64,redirect_in,redirect_out); + argc = parse_line(statement, argv, 64, redirect_in, redirect_out); + err = exec_file(argc, argv, &retcode); - err = exec_file(argc,argv,&retcode); - - if(err != SHE_NO_ERROR) goto err; + if (err != SHE_NO_ERROR) + goto err; *out = shell_value_init_number(retcode); @@ -406,11 +388,13 @@ err: return err; } + // // Parse load statement // -static int handle_load(scan_info *info) +static int +handle_load(scan_info *info) { char var_name[SCAN_SIZE+1]; shell_value *value;