Exemplo n.º 1
0
void
buffer_stream_initialize(stream_buffer_t* stream, void* buffer, unsigned int mode, size_t size,
                         size_t capacity, bool adopt, bool grow) {
	memset(stream, 0, sizeof(stream_buffer_t));
	stream_initialize((stream_t*)stream, system_byteorder());

	if (!adopt && grow) {
		log_warn(0, WARNING_INVALID_VALUE, STRING_CONST("Cannot grow buffer streams that are not adopted"));
		grow = false;
	}

	if (!buffer) {
		size = 0;
		capacity = 0;
	}
	if (size > capacity)
		size = capacity;

	stream->type = STREAMTYPE_MEMORY;
	stream->path = string_allocate_format(STRING_CONST("buffer://0x%" PRIfixPTR), (uintptr_t)stream);
	stream->mode = mode & (STREAM_OUT | STREAM_IN | STREAM_BINARY);
	stream->buffer = buffer;
	stream->size = size;
	stream->capacity = capacity;
	stream->own = adopt;
	stream->grow = (adopt && grow);
	stream->lastmod = time_current();

	if ((mode & STREAM_OUT) && (mode & STREAM_TRUNCATE))
		stream->size = 0;
	if (mode & STREAM_ATEND)
		stream->current = stream->size;

	stream->vtable = &_buffer_stream_vtable;
}
Exemplo n.º 2
0
/*! Normal entry point for all platforms, including Windows console applications */
int
main(int argc, char** argv)
#endif
{
	int ret;

#if !FOUNDATION_PLATFORM_ANDROID && !FOUNDATION_PLATFORM_PNACL
	_environment_main_args(argc, (const char* const*)argv);
#elif FOUNDATION_PLATFORM_PNACL
	FOUNDATION_UNUSED(instance);
#endif

	ret = main_initialize();
	if (ret < 0)
		return ret;

#if FOUNDATION_PLATFORM_POSIX

	//Set signal handlers
	{
		struct sigaction action;
		memset(&action, 0, sizeof(action));

#if FOUNDATION_COMPILER_CLANG
#  pragma clang diagnostic push
#  pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
#endif

		//Signals we process globally
		action.sa_handler = sighandler;
		sigaction(SIGKILL, &action, 0);
		sigaction(SIGTERM, &action, 0);
		sigaction(SIGQUIT, &action, 0);
		sigaction(SIGINT,  &action, 0);
		sigaction(SIGABRT, &action, 0);

		//Ignore sigpipe
		action.sa_handler = SIG_IGN;
		sigaction(SIGPIPE, &action, 0);

#if FOUNDATION_COMPILER_CLANG
#  pragma clang diagnostic pop
#endif
	}

#endif

#if FOUNDATION_PLATFORM_ANDROID
	if ((ret = android_initialize()) < 0)
		return ret;
#endif

#if FOUNDATION_PLATFORM_TIZEN
	if ((ret = tizen_initialize()) < 0)
		return ret;
#endif

#if FOUNDATION_PLATFORM_WINDOWS

	SetConsoleCtrlHandler(_main_console_handler, TRUE);

#endif

	thread_set_main();

	foundation_startup();

#if FOUNDATION_PLATFORM_WINDOWS || FOUNDATION_PLATFORM_LINUX || FOUNDATION_PLATFORM_PNACL
	system_post_event(FOUNDATIONEVENT_START);
#endif

#if FOUNDATION_PLATFORM_APPLE
#  if FOUNDATION_PLATFORM_MACOSX
	if (!(environment_application()->flags & APPLICATION_UTILITY)) {
		delegate_start_main_ns_thread();

		extern int NSApplicationMain(int argc, char* argv[]);
		ret = NSApplicationMain(argc, argv);

#  elif FOUNDATION_PLATFORM_IOS
	{
		delegate_start_main_ns_thread();

		extern int UIApplicationMain(int argc, char* argv[], void* principalClassName,
		                             void* delegateClassName);
		ret = UIApplicationMain(argc, (char**)argv, 0, 0);

#  endif
		//NSApplicationMain and UIApplicationMain never returns though
		return ret;
	}
#endif

#if !FOUNDATION_PLATFORM_IOS

#  if FOUNDATION_PLATFORM_TIZEN
	tizen_start_main_thread();
	ret = tizen_app_main(argc, argv);
#  else
	{
		string_t name;
		const application_t* app = environment_application();
		{
			string_const_t vstr = string_from_version_static(app->version);
			string_const_t aname = app->short_name;
			if (!aname.length)
				aname = string_const(STRING_CONST("unknown"));
			name = string_allocate_format(STRING_CONST("%.*s-%.*s"), (int)aname.length, aname.str,
			                              (int)vstr.length, vstr.str);
		}

		if (app->dump_callback)
			crash_guard_set(app->dump_callback, name.str, name.length);

		if (system_debugger_attached())
			ret = main_run(0);
		else
			ret = crash_guard(main_run, 0, app->dump_callback, name.str, name.length);

		string_deallocate(name.str);
	}
#  endif

	main_finalize();

#if FOUNDATION_PLATFORM_ANDROID
	android_finalize();
#endif

#if FOUNDATION_PLATFORM_TIZEN
	tizen_finalize();
#endif

	return ret;
#endif
}

#if FOUNDATION_PLATFORM_ANDROID

/*! Android native glue entry point */
void
android_main(struct android_app * app) {
	if (!app)
		return;
	android_entry(app);
	real_main();
}

#endif

#if FOUNDATION_PLATFORM_PNACL

/*! PNaCl glue entry points */
PP_EXPORT int32_t
PPP_InitializeModule(PP_Module module_id, PPB_GetInterface get_browser) {
	return pnacl_module_initialize(module_id, get_browser);
}

PP_EXPORT const void*
PPP_GetInterface(const char* interface_name) {
	return pnacl_module_interface(interface_name, string_length(interface_name));
}

PP_EXPORT void
PPP_ShutdownModule() {
	pnacl_module_finalize();
}
Exemplo n.º 3
0
int
hashify_process_files(string_t* files, bool check_only) {
	int result = HASHIFY_RESULT_OK;
	hashify_string_t* history = 0;
	size_t ifile, files_size;
	for (ifile = 0, files_size = array_size(files); (result == HASHIFY_RESULT_OK) &&
	        (ifile < files_size); ++ifile) {
		string_t input_filename;
		string_t output_filename;
		string_const_t base_filename;

		stream_t* input_file;
		stream_t* output_file;

		input_filename = string_allocate(0, BUILD_MAX_PATHLEN);
		input_filename = string_copy(input_filename.str, BUILD_MAX_PATHLEN, STRING_ARGS(files[ifile]));
		input_filename = path_clean(STRING_ARGS(input_filename), BUILD_MAX_PATHLEN);
		error_context_push(STRING_CONST("parsing file"), STRING_ARGS(input_filename));

		base_filename = path_base_file_name_with_directory(STRING_ARGS(input_filename));
		output_filename = string_allocate_format(STRING_CONST("%.*s.h"), STRING_FORMAT(base_filename));

		log_infof(0, STRING_CONST("Hashifying %.*s -> %.*s"), STRING_FORMAT(input_filename),
		          STRING_FORMAT(output_filename));

		input_file  = stream_open(STRING_ARGS(input_filename), STREAM_IN);

		//If only validating, open the final output file. If generating, make a memory buffer as intermediate storage
		if (check_only)
			output_file = stream_open(STRING_ARGS(output_filename), STREAM_IN);
		else
			output_file = buffer_stream_allocate(memory_allocate(0, 65536, 0, MEMORY_PERSISTENT),
			                                     STREAM_IN | STREAM_OUT, 0, 65536, true, true);

		if (!input_file) {
			log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open input file: %.*s"),
			          STRING_FORMAT(input_filename));
			result = HASHIFY_RESULT_MISSING_INPUT_FILE;
		}
		else if (!output_file) {
			log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open output file: %.*s"),
			          STRING_FORMAT(output_filename));
			result = HASHIFY_RESULT_MISSING_OUTPUT_FILE;
		}

		if (input_file && output_file) {
			result = hashify_process_file(input_file, output_file, output_filename, check_only, &history);
			if ((result == HASHIFY_RESULT_OK) && !check_only)
				result = hashify_write_file(output_file, output_filename);
		}

		stream_deallocate(input_file);
		stream_deallocate(output_file);

		error_context_pop();

		string_deallocate(input_filename.str);
		string_deallocate(output_filename.str);
	}

	if ((result == HASHIFY_RESULT_OK) && (files_size > 0)) {
		if (check_only)
			log_info(0, STRING_CONST("All hashes validated"));
		else
			log_info(0, STRING_CONST("All hashes generated"));
	}

	array_deallocate(history);

	return result;
}