#include #include #include #include "shelly.h" __attribute__((__used__)) static void parse_cmd_line(char * line, int * argc, char ** argv) { *argc = 0; char *token = strtok(line, " "); while (token != NULL) { argv[*argc] = token; token = strtok(NULL, " "); *argc += 1; } } static errval_t shelly_cmd_help(char * line) { errval_t err; err = shelly_write_str("commands implemented:\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- echo [string]\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- run [core] [binary]\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str(" run a module from bootinfo or filesystem\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- ps\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- ls\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- cat [path]\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str(" ...meow\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- mkdir [path]\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- rmdir [path]\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("- help\n\r"); if (err_is_fail(err)) return err; return SYS_ERR_OK; } static errval_t shelly_cmd_ls(char * line) { errval_t err; fs_dirhandle_t dh; err = opendir(line, &dh); if (err_is_fail(err)) { return err; } if (dh == NULL) { return SHELLY_ERR_FILE_NOT_FOUND; } char buf[256]; do { char *name; err = readdir(dh, &name); if (err_no(err) == FS_ERR_INDEX_BOUNDS) { break; } else if (err_is_fail(err)) { goto err_out; } snprintf(buf, 256, "%s\n\r", name); err = shelly_write_str(buf); if (err_is_fail(err)) goto err_out; } while(err_is_ok(err)); return closedir(dh); err_out: return err; } static errval_t shelly_cmd_cat(char * line) { errval_t err; err = shelly_write_str(" /\\_/\\ ___\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str(" = o_o =_______ \\ \\\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str(" __^ __( \\.__) )\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str("(@)<_____>__(_____)____/\n\r"); if (err_is_fail(err)) return err; int res = 0; FILE *f = fopen(line, "r"); if (f == NULL) { return FS_ERR_OPEN; } /* obtain the file size */ res = fseek(f , 0 , SEEK_END); if (res) { return FS_ERR_INVALID_FH; } size_t filesize = ftell(f); rewind(f); char str_buf[filesize + 10]; snprintf(str_buf, filesize + 10, "File size is %zu\n\r", filesize); err = shelly_write_str(str_buf); if (err_is_fail(err)) return err; char *file_content = calloc(filesize + 2, sizeof(char)); if (file_content == NULL) { return LIB_ERR_MALLOC_FAIL; } size_t read = fread(file_content, 1, filesize, f); snprintf(str_buf, filesize + 10, "%s\n\r", file_content); err = shelly_write_str(str_buf); if (err_is_fail(err)) return err; if (read != filesize) { return FS_ERR_READ; } rewind(f); size_t nchars = 0; int c; do { c = fgetc(f); nchars++; } while (c != EOF); if (nchars < filesize) { return FS_ERR_READ; } free(file_content); res = fclose(f); if (res) { return FS_ERR_CLOSE; } return SYS_ERR_OK; } static errval_t shelly_cmd_mkdir(char * line) { return mkdir(line); } static errval_t shelly_cmd_rmdir(char * line) { return rmdir(line); } static errval_t shelly_cmd_echo(char * line) { errval_t err; err = shelly_write_str(line); if (err_is_fail(err)) return err; return SYS_ERR_OK; } static errval_t shelly_cmd_run(char * line) { errval_t err; SHELLY_DEBUG("[shelly_cmd_run] line=%s\n", line); char * module_line; long core_id = strtol(line, &module_line, 10); module_line += 1; // start process domainid_t pid; struct aos_rpc * rpc = aos_rpc_get_process_channel(); err = aos_rpc_process_spawn(rpc, module_line, core_id, &pid); if (err == SPAWN_ERR_FIND_MODULE) { err = shelly_write_str("[run] couldn't find module:\n\r"); if (err_is_fail(err)) return err; err = shelly_write_str(line); if (err_is_fail(err)) return err; err = shelly_write_str("\n\r"); if (err_is_fail(err)) return err; } else if (err_is_fail(err)) return err_push(err, SHELLY_ERR_SPAWN_RPC); return SYS_ERR_OK; } static errval_t shelly_cmd_ps(char * line) { errval_t err; domainid_t *pids; size_t pid_count; struct aos_rpc * rpc = aos_rpc_get_process_channel(); err = aos_rpc_process_get_all_pids(rpc, &pids, &pid_count); if (err_is_fail(err)) return err_push(err, SHELLY_ERR_PS_RPC); shelly_write_str("PIDs:\n\r"); char buf[256]; for(size_t i = 0; i < pid_count; ++i) { // get process name char *name; err = aos_rpc_process_get_name(rpc, pids[i], &name); if (err_is_fail(err)) name = "unknown name"; snprintf(buf, 256, " %lu : %s\n\r", pids[i], name); shelly_write_str(buf); } return SYS_ERR_OK; } static errval_t handle_shelly_cmd(char* line, size_t len) { // SHELLY_DEBUG("[handle_shelly_cmd] len=%d\n", len); if (len <= 1) { return SYS_ERR_OK; } char * cmd = strtok(line, " "); char * remaining_line = line + strlen(cmd) + 1; // debug_printf("remaining_line = %s\n", remaining_line); if (strcmp(cmd, "echo") == 0) { return shelly_cmd_echo(remaining_line); } else if (strcmp(cmd, "run") == 0) { return shelly_cmd_run(remaining_line); } else if (strcmp(cmd, "ps") == 0) { return shelly_cmd_ps(remaining_line); } else if (strcmp(cmd, "help") == 0) { return shelly_cmd_help(remaining_line); } else if (strcmp(cmd, "ls") == 0) { init_fs_on_demand(); return shelly_cmd_ls(remaining_line); } else if (strcmp(cmd, "cat") == 0) { init_fs_on_demand(); return shelly_cmd_cat(remaining_line); } else if (strcmp(cmd, "mkdir") == 0) { init_fs_on_demand(); return shelly_cmd_mkdir(remaining_line); } else if (strcmp(cmd, "rmdir") == 0) { init_fs_on_demand(); return shelly_cmd_rmdir(remaining_line); } else { char buf[256]; snprintf(buf, 256, "unknown command \"%s\"\n\r", cmd); shelly_write_str(buf); } return SYS_ERR_OK; }