~sircmpwn/koio

1061c3e564684cf0fc329d3e8fda11d52ef78a07 — Drew DeVault 4 years ago 0f26a32
Add sample program, fix minor bugs
5 files changed, 76 insertions(+), 24 deletions(-)

A cat/README
A cat/main.c
A koio.syms
M meson.build
M tool/main.c
A cat/README => cat/README +3 -0
@@ 0,0 1,3 @@
Example program using koio.

It's compiled with its own main.c embedded at the path ./main.c.

A cat/main.c => cat/main.c +22 -0
@@ 0,0 1,22 @@
#include <koio.h>

void koio_load_assets(void);

int main(int argc, char **argv) {
	// This source file is embedded in the executable as //main.c
	koio_load_assets();

	for (int i = 1; i < argc; ++i) {
		FILE *f = ko_fopen(argv[i], "r");
		if (!f) {
			fprintf(stderr, "Failed to open '%s' for reading\n", argv[i]);
			return 1;
		}
		int c;
		while ((c = fgetc(f)) != EOF) {
			putchar(c);
		}
		fclose(f);
	}
	return 0;
}

A koio.syms => koio.syms +6 -0
@@ 0,0 1,6 @@
KOIO_0_0_0 {
	global:
		ko_add_file;
		ko_del_file;
		ko_fopen;
};

M meson.build => meson.build +25 -7
@@ 20,8 20,9 @@ add_project_arguments('-Wno-unused-parameter', language: 'c')

koio_inc = include_directories('include')

#symbols_file = 'wlroots.syms'
#symbols_flag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), symbols_file)
symbols_file = 'koio.syms'
symbols_flag = '-Wl,--version-script,@0@/@1@'.format(
	meson.current_source_dir(), symbols_file)
lib_koio = library(meson.project_name(),
	[
		'lib/ko_add_file.c',


@@ 32,11 33,28 @@ lib_koio = library(meson.project_name(),
	version: '.'.join(so_version),
	include_directories: koio_inc,
	install: true,
	#link_args: symbols_flag,
	link_args: symbols_flag,
)

koio_tool = executable(meson.project_name(),
	[
		'tool/main.c'
koio = declare_dependency(
	link_with: lib_koio,
	include_directories: koio_inc,
)

koio_tool = executable(meson.project_name(), [
	'tool/main.c'
], install: true)

cat_gen = custom_target('cat_gen.c',
	build_by_default: true,
	input: ['cat/main.c'],
	output: 'cat_gen.c',
	command: [koio_tool, '-o', '@OUTPUT@', '@INPUT@://main.c']
)

cat = executable('cat', [
		'cat/main.c',
		cat_gen
	],
	install: true)
	dependencies: [koio]
)

M tool/main.c => tool/main.c +20 -17
@@ 87,23 87,26 @@ int main(int argc, char **argv) {
			if (cols == 1) {
				fprintf(out, "\n\"");
			}
			if (isprint(c) && c != '\"') {
				cols += fprintf(out, "%c", c);
			} else {
				switch (c) {
				case '\"':
					cols += fprintf(out, "\\\"");
					break;
				case '\n':
					cols += fprintf(out, "\\n");
					break;
				case '\t':
					cols += fprintf(out, "\\t");
					break;
				default:
			switch (c) {
			case '\\':
				cols += fprintf(out, "\\\\");
				break;
			case '\"':
				cols += fprintf(out, "\\\"");
				break;
			case '\n':
				cols += fprintf(out, "\\n");
				break;
			case '\t':
				cols += fprintf(out, "\\t");
				break;
			default:
				if (isprint(c)) {
					cols += fprintf(out, "%c", c);
				} else {
					cols += fprintf(out, "\\x%02X", c);
					break;
				}
				break;
			}
			if (cols >= 79) {
				cols = 1;


@@ 119,14 122,14 @@ int main(int argc, char **argv) {
	}
	fprintf(out, "};\n\n");

	fprintf(out, "void %s() {\n", init);
	fprintf(out, "void %s(void) {\n", init);
	for (int i = 0; i < argc - optind; ++i) {
		fprintf(out, "	ko_add_file(files[%d].path, "
				"files[%d].data, files[%d].len);\n", i, i, i);
	}
	fprintf(out, "}\n\n");

	fprintf(out, "void %s() {\n", deinit);
	fprintf(out, "void %s(void) {\n", deinit);
	for (int i = 0; i < argc - optind; ++i) {
		fprintf(out, "	ko_del_file(files[%d].path);\n", i);
	}