c-md/srcs/main.c.md
lohhiiccc 80f7a1b9b6 feat(build): add build instructions in README and convert sources to .c.md format
- Add detailed build and bootstrap instructions to README.md.
 - Convert all source and header files from .c/.h to .c.md/.h.md.
 - Add bootstrap.sh script for automated building across version history.
 - Update Makefile and sources.mk to reflect new markdown-based source organization.
2026-01-12 14:54:49 +01:00

1.3 KiB

#include <stdio.h>
#include <stdint.h>

#include "cli.h"
#include "validator.h"
#include "transpile.h"
#include "map.h"
#include "io.h"

static int8_t	run(t_args *args);
static void	print_error(int8_t code, const char *progname);

int
main(int argc, char **argv)
{
	t_args	args;
	int8_t	validation_code;

	if (0 != cli_parse(&args, (int32_t)argc, argv))
	{
		cli_print_help(argv[0]);
		return (1);
	}
	if (args.show_help)
	{
		cli_print_help(argv[0]);
		return (0);
	}
	validation_code = validator_validate_args(&args);
	if (0 != validation_code)
	{
		print_error(validation_code, argv[0]);
		return (1);
	}
	return (run(&args));
}

static void
print_error(int8_t code, const char *progname)
{
	if (1 == code)
		fprintf(stderr, "Error: -e required when reading from stdin\n");
	else if (2 == code)
		fprintf(stderr, "Error: -e required (cannot infer from filename)\n");
	cli_print_help(progname);
}

static int8_t
run(t_args *args)
{
	t_io	io;
	t_map	map;
	int8_t	ret;

	if (0 != io_open(&io, args->input, args->output))
		return (1);
	map_init(&map);
	ret = transpile(io.in, io.out, args->ext, &map);
	if (0 == ret && NULL != args->map_path)
	{
		ret = map_write(&map, args->map_path,
			(NULL != args->input) ? args->input : "stdin",
			(NULL != args->output) ? args->output : "stdout");
	}
	map_free(&map);
	io_close(&io);
	return (ret);
}