Beispiel #1
0
int main(int argc, char* argv[])
{
    // Setup.
    AppLib::Logging::setApplicationName(std::string("apputil"));
    struct arg_file *script = arg_file1(NULL, NULL, "script", "the path of the script to execute");
    struct arg_end *end = arg_end(20);
    void *argtable[] = { script, end };
    script->filename[0] = NULL;

    // Check to see if the argument definitions were allocated
    // correctly.
    if (arg_nullcheck(argtable))
    {
        AppLib::Logging::showErrorW("Insufficient memory.");
        return 1;
    }

    // Now parse the arguments.
    int nerrors = arg_parse(argc, argv, argtable);

    // Check to see if there were errors.
    if (nerrors > 0 && script->filename[0] == NULL)
    {
        printf("Usage: apputil");
        arg_print_syntax(stdout, argtable, "\n");
        arg_print_errors(stdout, end, "apputil");
        
        printf("AppUtil - The application scripting utility.\n\n");
        arg_print_glossary(stdout, argtable, "    %-25s %s\n");
        return 1;
    }

    // Execute.
    return runPython(script->filename[0], argc, argv);
}
Beispiel #2
0
/*function checkBuiltinCmds
 *
 * checks for built-in commands
 *
 * returns 1 if found and run, break the loop and reprompt
 * returns -1 if in an incorrect format, break the loop and reprompt
 * returns 0 if not builtin or python, continue loop
 */
int checkBuiltinCmds(char* argTokens[256]) {
	// built-in commands
	if (strcmp("exit", argTokens[0]) == 0) {	// exit program
		if (argTokens[1] == NULL ) {
			exit(0);
			return (-2);
		}

		else {	//bad exit
			displayError();
			return (-1);
		}
	} else if (strcmp("pwd", argTokens[0]) == 0) {	// print working directory

		if (argTokens[1] == NULL ) {	// no other args
			char* currDir = NULL;
			currDir = getcwd(currDir, 0);
			if (currDir) {
				write(STDOUT_FILENO,currDir, strlen(currDir));
				write(STDOUT_FILENO,"\n", 1);
				return (1);
			}
			displayError();
			return (-1);

		} else {	//bad pwd
			displayError();
			return (-1);
		}
	} else if (strcmp("cd", argTokens[0]) == 0) {	// cd

		if (argTokens[1] == NULL ) {	// no args
			chdir(getenv("HOME"));
			return(1);
		}
		// make sure there is only one arg
		else if (argTokens[2] == NULL ) {	// one arg

			if (chdir(argTokens[1]) == -1) {	// check if directory is valid
				displayError();
				return(-1);
			} else {	// bad cd: bad arg
				chdir(argTokens[1]);
				return (1);
			}

		} else {	// bad cd2: extra args
			displayError();
			return (-1);
		}

	} else if (strcmp("wait", argTokens[0]) == 0) {	// wait for all children to exit

		if (argTokens[1] == NULL ) { 	// check for extra args
			int status, pid;
			while ((pid = wait(&status)) != -1) {}	// wait for stuff
			return(0);
		} else {	// bad wait
			displayError();
			return (-1);
		}

	} else if (isPythonFile(argTokens[0])) {

		// replace arg0 after shifting the argTokens to the right
		runPython(argTokens);
	}

	// not a built-in command run regularly
	return (0);

}