/* Change PWD (Present Working Directory) * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_cd(const char *cmdstr, const char *ipwdstr) { int rv; char filestr[ROSH_PATH_SZ]; int cmdpos; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; rv = 0; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) != 0) rv = chdir(filestr); else rv = chdir(ipwdstr); if (rv != 0) { rosh_error(errno, "cd", filestr); errno = 0; } else { #ifdef DO_DEBUG if (getcwd(filestr, ROSH_PATH_SZ)) ROSH_DEBUG(" %s\n", filestr); #endif /* DO_DEBUG */ } } /* rosh_cd */
/* Concatenate multiple files to stdout * cmdstr command string to process */ void rosh_cat(const char *cmdstr) { FILE *f; char filestr[ROSH_PATH_SZ]; char buf[ROSH_BUF_SZ]; int numrd; int cmdpos; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); f = fopen(filestr, "r"); if (f != NULL) { numrd = fread(buf, 1, ROSH_BUF_SZ, f); while (numrd > 0) { fwrite(buf, 1, numrd, stdout); numrd = fread(buf, 1, ROSH_BUF_SZ, f); } fclose(f); } else { rosh_error(errno, "cat", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } } /* rosh_cat */
/* Page through a file like the more command * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_more(const char *cmdstr) { int fd; char filestr[ROSH_PATH_SZ]; int cmdpos; int rows, cols; char *scrbuf; int ret; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; ret = getscreensize(1, &rows, &cols); if (ret) { ROSH_DEBUG("getscreensize() fail(%d); fall back\n", ret); ROSH_DEBUG("\tROWS='%d'\tCOLS='%d'\n", rows, cols); /* If either fail, go under normal size, just in case */ if (!rows) rows = 20; if (!cols) cols = 75; } ROSH_DEBUG("\tUSE ROWS='%d'\tCOLS='%d'\n", rows, cols); /* 32 bit align beginning of row and over allocate */ scrbuf = malloc(rows * ((cols+3)&(INT_MAX - 3))); if (!scrbuf) return; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) > 0) { /* There is no need to mess up the console if we don't have a file */ rosh_console_raw(); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); fd = open(filestr, O_RDONLY); if (fd != -1) { rosh_more_fd(fd, rows, cols, scrbuf); close(fd); } else { rosh_error(errno, "more", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } rosh_console_std(); } free(scrbuf); } /* rosh_more */
/* List Directory based on cmdstr and pwdstr * cmdstr command string to process * pwdstr Present Working Directory string */ void rosh_ls(const char *cmdstr) { char filestr[ROSH_PATH_SZ]; char optstr[ROSH_OPT_SZ]; /* Options string */ int cmdpos, tpos; /* Position within cmdstr, temp position */ int numargs; /* number of non-option arguments */ int argpos; /* number of non-option arguments processed */ int optarr[3]; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; optstr[0] = 0; cmdpos = 0; numargs = 0; argpos = 0; /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); tpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); /* If there are no real arguments, substitute PWD */ if (strlen(filestr) == 0) { strcpy(filestr, "."); cmdpos = tpos; } else { /* Parse for command line options */ while (strlen(filestr) > 0) { numargs += rosh_ls_parse_opt(filestr, optstr); tpos = rosh_parse_sp_1(filestr, cmdstr, tpos); } if (numargs == 0) { strcpy(filestr, "."); cmdpos = tpos; } else { cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } } #ifdef DO_DEBUG if (!strchr(optstr, 'l')) strcat(optstr, "l"); #endif /* DO_DEBUG */ rosh_ls_arg_opt(optstr, optarr); ROSH_DEBUG("\tfopt: '%s'\n", optstr); while (strlen(filestr) > 0) { if (rosh_ls_parse_opt(filestr, NULL)) { rosh_ls_arg(filestr, optarr); argpos++; } if (argpos < numargs) cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); else break; } } /* rosh_ls */
/* Page through a file like the more command * cmdstr command string to process * ipwdstr Initial PWD */ void rosh_more(const char *cmdstr) { int fd; char filestr[ROSH_PATH_SZ]; int cmdpos; int rows, cols; ROSH_DEBUG("CMD: '%s'\n", cmdstr); /* Initialization */ filestr[0] = 0; cmdpos = 0; if (getscreensize(1, &rows, &cols)) { ROSH_DEBUG("getscreensize() fail; fall back\n"); ROSH_DEBUG("\tROWS='%d'\tCOLS='%d'\n", rows, cols); /* If either fail, go under normal size, just in case */ if (!rows) rows = 20; if (!cols) cols = 75; } ROSH_DEBUG("\tUSE ROWS='%d'\tCOLS='%d'\n", rows, cols); /* skip the first word */ cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); if (strlen(filestr) > 0) { /* There is no need to mess up the console if we don't have a file */ rosh_console_raw(); while (strlen(filestr) > 0) { printf("--File = '%s'\n", filestr); fd = open(filestr, O_RDONLY); if (fd != -1) { rosh_more_fd(fd, rows, cols); close(fd); } else { rosh_error(errno, "more", filestr); errno = 0; } cmdpos = rosh_parse_sp_1(filestr, cmdstr, cmdpos); } rosh_console_std(); } } /* rosh_more */
/* Process a single command string and call handling function * cmdstr command string to process * ipwdstr Initial Present Working Directory string * returns Whether to exit prompt */ char rosh_command(const char *cmdstr, const char *ipwdstr) { char do_exit; char tstr[ROSH_CMD_SZ]; int tlen; do_exit = false; ROSH_DEBUG("--cmd:'%s'\n", cmdstr); tlen = rosh_parse_sp_1(tstr, cmdstr, 0); switch (cmdstr[0]) { case 'e': case 'E': case 'q': case 'Q': if ((strncasecmp("exit", tstr, tlen) == 0) || (strncasecmp("quit", tstr, tlen) == 0)) do_exit = true; else rosh_help(1, NULL); break; case 'c': case 'C': /* run 'cd' 'cat' 'cfg' */ switch (cmdstr[1]) { case 'a': case 'A': if (strncasecmp("cat", tstr, tlen) == 0) rosh_cat(cmdstr); else rosh_help(1, NULL); break; case 'd': case 'D': if (strncasecmp("cd", tstr, tlen) == 0) rosh_cd(cmdstr, ipwdstr); else rosh_help(1, NULL); break; case 'f': case 'F': if (strncasecmp("cfg", tstr, tlen) == 0) rosh_cfg(); else rosh_help(1, NULL); break; default: rosh_help(1, NULL); } break; case 'd': case 'D': /* run 'dir' */ if (strncasecmp("dir", tstr, tlen) == 0) rosh_dir(cmdstr); else rosh_help(1, NULL); break; case 'h': case 'H': case '?': if ((strncasecmp("help", tstr, tlen) == 0) || (tlen == 1)) rosh_help(2, cmdstr); else rosh_help(1, NULL); break; case 'l': case 'L': /* run 'ls' 'less' */ switch (cmdstr[1]) { case 0: case ' ': case 's': case 'S': if (strncasecmp("ls", tstr, tlen) == 0) rosh_ls(cmdstr); else rosh_help(1, NULL); break; case 'e': case 'E': if (strncasecmp("less", tstr, tlen) == 0) rosh_less(cmdstr); else rosh_help(1, NULL); break; default: rosh_help(1, NULL); } break; case 'm': case 'M': switch (cmdstr[1]) { case 'a': case 'A': if (strncasecmp("man", tstr, tlen) == 0) rosh_help(2, cmdstr); else rosh_help(1, NULL); break; case 'o': case 'O': if (strncasecmp("more", tstr, tlen) == 0) rosh_more(cmdstr); else rosh_help(1, NULL); break; default: rosh_help(1, NULL); } break; case 'p': case 'P': /* run 'pwd' */ if (strncasecmp("pwd", tstr, tlen) == 0) rosh_pwd(cmdstr); else rosh_help(1, NULL); break; case 'r': case 'R': /* run 'run' */ switch (cmdstr[1]) { case 0: case ' ': case 'e': case 'E': if (strncasecmp("reboot", tstr, tlen) == 0) rosh_reboot(); else rosh_help(1, NULL); break; case 'u': case 'U': if (strncasecmp("run", tstr, tlen) == 0) rosh_run(cmdstr); else rosh_help(1, NULL); break; } break; case 'v': case 'V': if (strncasecmp("version", tstr, tlen) == 0) rosh_version(1); else rosh_help(1, NULL); break; case 0: case '\n': break; default: rosh_help(1, NULL); } /* switch(cmdstr[0]) */ return do_exit; } /* rosh_command */