void msh_loop(t_config *conf) { int status; char *cmd; t_list **lcmd; t_list *tmp; status = 1; while (1) { ft_putstr("$>"); cmd = msh_read_cmd(); if (!(lcmd = ft_memalloc(sizeof(t_list*)))) exit(msh_error(NULL, NULL, MSH_ERR_MEM)); lcmd = msh_parse(cmd, conf, lcmd); tmp = *lcmd; while (tmp) { status = msh_exec(tmp->content, conf); tmp = tmp->next; } ft_lstdel(lcmd, &msh_lstarray_free); free(lcmd); } }
void process_entry(t_sh_token *token, t_envp *envp) { t_int32 ret; t_mysh_er er; t_struct_linker job_list; init_linker(&job_list); while (token) { er = 0; if (token->up && token->flag & P_SEPARATOR_L) { if ((ret = check_if_builtin(token->up->str)) >= 0) { if (test_job(&job_list, token->flag)) er |= msh_builtin(&job_list, ret, token, envp); } else if ((ret = get_exec_path(envp, token))) er |= ret; else if (token->flag & P_SCOL_F || test_job(&job_list, token->flag)) er |= msh_exec(&job_list, token, envp); } msh_error(er, token->up); token = token->next; } free_jobs((t_job *)job_list.first); }
int system(const char *command) { int ret = -RT_ENOMEM; char *cmd = rt_strdup(command); if (cmd) { ret = msh_exec(cmd, rt_strlen(cmd)); rt_free(cmd); } return ret; }
void finsh_thread_entry(void* parameter) { char ch; /* normal is echo mode */ shell->echo_mode = 1; finsh_init(&shell->parser); rt_kprintf(FINSH_PROMPT); /* set console device as shell device */ shell->device = rt_console_get_device(); if (shell->device != RT_NULL) { rt_device_open(shell->device, RT_DEVICE_OFLAG_RDWR); rt_device_set_rx_indicate(shell->device, finsh_rx_ind); } while (1) { /* wait receive */ if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue; /* read one character from device */ while (rt_device_read(shell->device, 0, &ch, 1) == 1) { /* * handle control key * up key : 0x1b 0x5b 0x41 * down key: 0x1b 0x5b 0x42 * right key:0x1b 0x5b 0x43 * left key: 0x1b 0x5b 0x44 */ if (ch == 0x1b) { shell->stat = WAIT_SPEC_KEY; continue; } else if (shell->stat == WAIT_SPEC_KEY) { if (ch == 0x5b) { shell->stat = WAIT_FUNC_KEY; continue; } shell->stat = WAIT_NORMAL; } else if (shell->stat == WAIT_FUNC_KEY) { shell->stat = WAIT_NORMAL; if (ch == 0x41) /* up key */ { #ifdef FINSH_USING_HISTORY /* prev history */ if (shell->current_history > 0) shell->current_history --; else { shell->current_history = 0; continue; } /* copy the history command */ memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); finsh_handle_history(shell); #endif continue; } else if (ch == 0x42) /* down key */ { #ifdef FINSH_USING_HISTORY /* next history */ if (shell->current_history < shell->history_count - 1) shell->current_history ++; else { /* set to the end of history */ if (shell->history_count != 0) shell->current_history = shell->history_count - 1; else continue; } memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); finsh_handle_history(shell); #endif continue; } else if (ch == 0x44) /* left key */ { if (shell->line_curpos) { rt_kprintf("\b"); shell->line_curpos --; } continue; } else if (ch == 0x43) /* right key */ { if (shell->line_curpos < shell->line_position) { rt_kprintf("%c", shell->line[shell->line_curpos]); shell->line_curpos ++; } continue; } } /* handle CR key */ if (ch == '\r') { char next; if (rt_device_read(shell->device, 0, &next, 1) == 1) ch = next; else ch = '\r'; } /* handle tab key */ else if (ch == '\t') { int i; /* move the cursor to the beginning of line */ for (i = 0; i < shell->line_curpos; i++) rt_kprintf("\b"); /* auto complete */ finsh_auto_complete(&shell->line[0]); /* re-calculate position */ shell->line_curpos = shell->line_position = strlen(shell->line); continue; } /* handle backspace key */ else if (ch == 0x7f || ch == 0x08) { /* note that shell->line_curpos >= 0 */ if (shell->line_curpos == 0) continue; shell->line_position--; shell->line_curpos--; if (shell->line_position > shell->line_curpos) { int i; rt_memmove(&shell->line[shell->line_curpos], &shell->line[shell->line_curpos + 1], shell->line_position - shell->line_curpos); shell->line[shell->line_position] = 0; rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]); /* move the cursor to the origin position */ for (i = shell->line_curpos; i <= shell->line_position; i++) rt_kprintf("\b"); } else { rt_kprintf("\b \b"); shell->line[shell->line_position] = 0; } continue; } /* handle end of line, break */ if (ch == '\r' || ch == '\n') { #ifdef FINSH_USING_MSH if (msh_is_used() == RT_TRUE && shell->line_position != 0) { rt_kprintf("\n"); msh_exec(shell->line, shell->line_position); #ifdef FINSH_USING_HISTORY finsh_push_history(shell); #endif } else #endif { /* add ';' and run the command line */ shell->line[shell->line_position] = ';'; #ifdef FINSH_USING_HISTORY finsh_push_history(shell); #endif if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line); else rt_kprintf("\n"); } rt_kprintf(FINSH_PROMPT); memset(shell->line, 0, sizeof(shell->line)); shell->line_curpos = shell->line_position = 0; break; } /* it's a large line, discard it */ if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0; /* normal character */ if (shell->line_curpos < shell->line_position) { int i; rt_memmove(&shell->line[shell->line_curpos + 1], &shell->line[shell->line_curpos], shell->line_position - shell->line_curpos); shell->line[shell->line_curpos] = ch; if (shell->echo_mode) rt_kprintf("%s", &shell->line[shell->line_curpos]); /* move the cursor to new position */ for (i = shell->line_curpos; i < shell->line_position; i++) rt_kprintf("\b"); } else { shell->line[shell->line_position] = ch; rt_kprintf("%c", ch); } ch = 0; shell->line_position ++; shell->line_curpos++; } /* end of device read */ } }
int msh_exec_script(const char *cmd_line, int size) { int ret; int fd = -1; char *pg_name; int length, cmd_length = 0; if (size == 0) return -RT_ERROR; /* get the length of command0 */ while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size) cmd_length ++; /* get name length */ length = cmd_length + 32; /* allocate program name memory */ pg_name = (char *) rt_malloc(length); if (pg_name == RT_NULL) return -RT_ENOMEM; /* copy command0 */ memcpy(pg_name, cmd_line, cmd_length); pg_name[cmd_length] = '\0'; if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL) { /* try to open program */ fd = open(pg_name, O_RDONLY, 0); /* search in /bin path */ if (fd < 0) { rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line); fd = open(pg_name, O_RDONLY, 0); } } rt_free(pg_name); if (fd >= 0) { /* found script */ char *line_buf; int length; line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE); /* read line by line and then exec it */ do { length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE); if (length > 0) { char ch = '\0'; int index; for (index = 0; index < length; index ++) { ch = line_buf[index]; if (ch == ' ' || ch == '\t') continue; else break; } if (ch != '#') /* not a comment */ msh_exec(line_buf, length); } } while (length > 0); close(fd); rt_free(line_buf); ret = 0; } else { ret = -1; } return ret; }
void finsh_thread_entry(void *parameter) { char ch; /* normal is echo mode */ shell->echo_mode = 1; #ifndef FINSH_USING_MSH_ONLY finsh_init(&shell->parser); #endif #ifndef RT_USING_POSIX /* set console device as shell device */ if (shell->device == RT_NULL) { rt_device_t console = rt_console_get_device(); if (console) { finsh_set_device(console->parent.name); } } #endif #ifdef FINSH_USING_AUTH /* set the default password when the password isn't setting */ if (rt_strlen(finsh_get_password()) == 0) { if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK) { rt_kprintf("Finsh password set failed.\n"); } } /* waiting authenticate success */ finsh_wait_auth(); #endif rt_kprintf(FINSH_PROMPT); while (1) { ch = finsh_getchar(); /* * handle control key * up key : 0x1b 0x5b 0x41 * down key: 0x1b 0x5b 0x42 * right key:0x1b 0x5b 0x43 * left key: 0x1b 0x5b 0x44 */ if (ch == 0x1b) { shell->stat = WAIT_SPEC_KEY; continue; } else if (shell->stat == WAIT_SPEC_KEY) { if (ch == 0x5b) { shell->stat = WAIT_FUNC_KEY; continue; } shell->stat = WAIT_NORMAL; } else if (shell->stat == WAIT_FUNC_KEY) { shell->stat = WAIT_NORMAL; if (ch == 0x41) /* up key */ { #ifdef FINSH_USING_HISTORY /* prev history */ if (shell->current_history > 0) shell->current_history --; else { shell->current_history = 0; continue; } /* copy the history command */ memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); shell_handle_history(shell); #endif continue; } else if (ch == 0x42) /* down key */ { #ifdef FINSH_USING_HISTORY /* next history */ if (shell->current_history < shell->history_count - 1) shell->current_history ++; else { /* set to the end of history */ if (shell->history_count != 0) shell->current_history = shell->history_count - 1; else continue; } memcpy(shell->line, &shell->cmd_history[shell->current_history][0], FINSH_CMD_SIZE); shell->line_curpos = shell->line_position = strlen(shell->line); shell_handle_history(shell); #endif continue; } else if (ch == 0x44) /* left key */ { if (shell->line_curpos) { rt_kprintf("\b"); shell->line_curpos --; } continue; } else if (ch == 0x43) /* right key */ { if (shell->line_curpos < shell->line_position) { rt_kprintf("%c", shell->line[shell->line_curpos]); shell->line_curpos ++; } continue; } } /* handle CR key */ if (ch == '\0') continue; /* handle tab key */ else if (ch == '\t') { int i; /* move the cursor to the beginning of line */ for (i = 0; i < shell->line_curpos; i++) rt_kprintf("\b"); /* auto complete */ shell_auto_complete(&shell->line[0]); /* re-calculate position */ shell->line_curpos = shell->line_position = strlen(shell->line); continue; } /* handle backspace key */ else if (ch == 0x7f || ch == 0x08) { /* note that shell->line_curpos >= 0 */ if (shell->line_curpos == 0) continue; shell->line_position--; shell->line_curpos--; if (shell->line_position > shell->line_curpos) { int i; rt_memmove(&shell->line[shell->line_curpos], &shell->line[shell->line_curpos + 1], shell->line_position - shell->line_curpos); shell->line[shell->line_position] = 0; rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]); /* move the cursor to the origin position */ for (i = shell->line_curpos; i <= shell->line_position; i++) rt_kprintf("\b"); } else { rt_kprintf("\b \b"); shell->line[shell->line_position] = 0; } continue; } /* handle end of line, break */ if (ch == '\r' || ch == '\n') { #ifdef FINSH_USING_HISTORY shell_push_history(shell); #endif #ifdef FINSH_USING_MSH if (msh_is_used() == RT_TRUE) { if (shell->echo_mode) rt_kprintf("\n"); msh_exec(shell->line, shell->line_position); } else #endif { #ifndef FINSH_USING_MSH_ONLY /* add ';' and run the command line */ shell->line[shell->line_position] = ';'; if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line); else if (shell->echo_mode) rt_kprintf("\n"); #endif } rt_kprintf(FINSH_PROMPT); memset(shell->line, 0, sizeof(shell->line)); shell->line_curpos = shell->line_position = 0; continue; } /* it's a large line, discard it */ if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0; /* normal character */ if (shell->line_curpos < shell->line_position) { int i; rt_memmove(&shell->line[shell->line_curpos + 1], &shell->line[shell->line_curpos], shell->line_position - shell->line_curpos); shell->line[shell->line_curpos] = ch; if (shell->echo_mode) rt_kprintf("%s", &shell->line[shell->line_curpos]); /* move the cursor to new position */ for (i = shell->line_curpos; i < shell->line_position; i++) rt_kprintf("\b"); } else { shell->line[shell->line_position] = ch; if (shell->echo_mode) rt_kprintf("%c", ch); } ch = 0; shell->line_position ++; shell->line_curpos++; if (shell->line_position >= FINSH_CMD_SIZE) { /* clear command line */ shell->line_position = 0; shell->line_curpos = 0; } } /* end of device read */ }