Exemplo n.º 1
0
Arquivo: main.c Projeto: malind/bam
/* ********* */
int main(int argc, char **argv)
{
	int i, error;

	/* set exe */
	session.exe = argv[0];

	/* fetch program name, if we can */
	for(i = 0; argv[0][i]; ++i)
	{
		if(argv[0][i] == '/' || argv[0][i] == '\\')
			session.name = &argv[0][i+1];
	}

	/* parse environment parameters */
	if(getenv("BAM_OPTIONS"))
	{
		if(parse_parameters_str(getenv("BAM_OPTIONS")))
			return -1;
	}
	
	/* parse commandline parameters */
	if(parse_parameters(argc-1, argv+1))
		return -1;

	/* set eventlog */
	if(option_win_msvcmode)
		session.win_msvcmode = option_win_msvcmode;

	/* init platform */
	platform_init();

	/* set eventlog */
	if(option_debug_eventlog)
	{
		session.eventlog = fopen(option_debug_eventlog, "w");
		session.eventlogflush = option_debug_eventlogflush;
		if(!session.eventlog)
		{
			printf("%s: error opening '%s' for output\n", session.name, option_debug_eventlog);
			return 1;
		}
	}

	/* parse the report str */
	for(i = 0; option_report_str[i]; i++)
	{
		if(option_report_str[i] == 'c')
			session.report_color = 1;
		else if(option_report_str[i] == 'b')
			session.report_bar = 1;
		else if(option_report_str[i] == 's')
			session.report_steps = 1;
	}
	
	/* convert the threads string */
	if(option_threads_str)
	{
		session.threads = atoi(option_threads_str);
		if(session.threads < 0)
		{
			printf("%s: invalid number of threads supplied\n", session.name);
			return 1;
		}
	}
	else
	{
		session.threads = threads_corecount();
		if(session.verbose)
			printf("%s: detected %d cores\n", session.name, session.threads);
	}
	
	/* check for help argument */
	if(option_print_debughelp)
	{
		print_help(OF_PRINT|OF_DEBUG);
		return 0;
	}
	else if(option_print_help)
	{
		print_help(OF_PRINT);
		return 0;
	}

	/* */
	if(option_debug_dumpinternal)
	{
		int f;
		for(f = 0; internal_files[f].filename; f++)
		{
			printf("%s:\n", internal_files[f].filename);
			puts(internal_files[f].content);
		}
				
		return 0;
	}
	
	if(option_lua_execute)
	{
		struct lua_State* lua = lua_newstate(lua_alloctor_malloc, 0x0);
		lua_atpanic(lua, lf_panicfunc);

		/* register all functions */
		if(register_lua_globals(lua, "script_dir", option_lua_execute) != 0)
		{
			printf("%s: error: registering of lua functions failed\n", session.name);
			return -1;
		}

		lua_getglobal(lua, "errorfunc");
		switch(luaL_loadfile(lua, option_lua_execute))
		{
			case 0: break;
			case LUA_ERRSYNTAX:
					lf_errorfunc(lua);
					return -1;
			case LUA_ERRMEM: 
					printf("%s: memory allocation error\n", session.name);
					return -1;
			case LUA_ERRFILE:
					printf("%s: error opening '%s'\n", session.name, option_lua_execute);
					return -1;
			default:
					printf("%s: unknown error\n", session.name);
					return -1;
		}

		/* call the code chunk */	
		if(lua_pcall(lua, 0, LUA_MULTRET, -2) != 0)
		{
			printf("%s: script error (-t for more detail)\n", session.name);
			return -1;
		}

		lua_close(lua);
		error = 0;
	}
	else
	{
		/* init the context */
		error = bam(option_script, option_targets, option_num_targets);
	}
	
	platform_shutdown();


	/* error could be some high value like 256 seams like this could */
	/* be clamped down to a unsigned char and not be an error anymore */
	if(error)
		return 1;

	return 0;
}
Exemplo n.º 2
0
/* ********* */
int main(int argc, char **argv)
{
	int i, error;

	/* init platform */
	platform_init();

	/* set exe */
	session.exe = argv[0];

	/* fetch program name, if we can */
	for(i = 0; argv[0][i]; ++i)
	{
		if(argv[0][i] == '/' || argv[0][i] == '\\')
			session.name = &argv[0][i+1];
	}

	/* parse environment parameters */
	if(getenv("BAM_OPTIONS"))
	{
		if(parse_parameters_str(getenv("BAM_OPTIONS")))
			return -1;
	}
	
	/* parse commandline parameters */
	if(parse_parameters(argc-1, argv+1))
		return -1;
		
	/* parse the report str */
	for(i = 0; option_report_str[i]; i++)
	{
		if(option_report_str[i] == 'c')
			session.report_color = 1;
		else if(option_report_str[i] == 'b')
			session.report_bar = 1;
		else if(option_report_str[i] == 's')
			session.report_steps = 1;
	}
	
	/* convert the threads string */
	session.threads = atoi(option_threads_str);
	if(session.threads < 0)
	{
		printf("%s: invalid number of threads supplied\n", session.name);
		return 1;
	}
	
	/* check for help argument */
	if(option_print_help)
	{
		print_help();
		return 0;
	}

	/* */
	if(option_debug_dumpinternal)
	{
		int f;
		for(f = 0; internal_files[f].filename; f++)
		{
			printf("%s:\n", internal_files[f].filename);
			puts(internal_files[f].content);
		}
				
		return 0;
	}
	
	/* init the context */
	error = bam(option_script, option_targets, option_num_targets);
	
	platform_shutdown();

	/* error could be some high value like 256 seams like this could */
	/* be clamped down to a unsigned char and not be an error anymore */
	if(error)
		return 1;
	return 0;
}