Пример #1
0
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;
}
Пример #2
0
bool Shell::mainLoop(std::istream &in, Environment &env) const
{
	InputParser iParser;
	ShellCommand::Args args;
	
	const Shell *oldShell = env.shell();
	env.shell() = this;
	
	bool success = true;
	do {
		if(env.interactive()) cout << endl << "dbxml> ";
		env.lineNo() += iParser.parse(in, args);
		string command = args.empty() ? string("") : args.front();
		try {
			ShellCommand *cmd = findCommand(command);
			if (cmd)
				cmd->execute(args, env);
			else if(!args.empty()) {
				throw CommandException("Unknown command");
			}
		}
		//catches XmlException
		catch(exception &e) {
			cerr << env.streamName() << ":" << env.lineNo() <<
				": " << command << " failed, ";
			cerr << e.what() << endl;
			success = false;
			if(!env.interactive() && !env.ignoreErrors())
				env.quit() = true;
		}
		catch(...) {
			cerr << env.streamName() << ":" << env.lineNo() <<
				": " << command << " failed" << endl;
			success = false;
			if(!env.interactive() && !env.ignoreErrors())
				env.quit() = true;
		}
	} while(!env.quit() && !in.eof() && !env.sigBlock().isInterrupted());
	
	env.shell() = oldShell;
	return success;
}