int Shell::execute(char *command) { char *argv[MAX_ARGV]; char tmp[128]; ShellCommand *cmd; Size argc; int pid, status; /* Valid argument? */ if (!strlen(command)) { return EXIT_SUCCESS; } /* Attempt to extract arguments. */ argc = parse(command, argv, MAX_ARGV); /* Ignore comments */ if (argv[0][0] == '#') return EXIT_SUCCESS; /* Do we have a matching ShellCommand? */ if (!(cmd = ShellCommand::byName(argv[0]))) { /* If not, try to execute it as a file directly. */ if ((pid = forkexec(argv[0], (const char **) argv)) != -1) { waitpid(pid, &status, 0); return status; } /* Try to find it on the livecd filesystem. (temporary hardcoded PATH) */ else if (snprintf(tmp, sizeof(tmp), "/bin/%s", argv[0]) && (pid = forkexec(tmp, (const char **) argv)) != -1) { waitpid(pid, &status, 0); return status; } else printf("forkexec '%s' failed: %s\r\n", argv[0], strerror(errno)); } /* Enough arguments given? */ else if (argc - 1 < cmd->getMinimumParams()) { printf("%s: not enough arguments (%u required)\r\n", cmd->getName(), cmd->getMinimumParams()); } /* Execute it. */ else { return cmd->execute(argc - 1, argv + 1); } /* Not successful. */ return EXIT_FAILURE; }
void Shell::start() { std::string commandWithArgs; out << PROMPT; while(std::getline(in, commandWithArgs, '\n')) { if (!commandWithArgs.empty() && !isSpaces(commandWithArgs)) { ShellCommand* cmd = commands[getCommandName(commandWithArgs)]; if (cmd == NULL) { out << "command not found" << std::endl; } else { cmd->run(commandWithArgs); if (cmd->getName() == "quit") { break; } } } out << PROMPT; } }