c-md/srcs/main.c
2026-01-12 00:48:09 +01:00

67 lines
1.3 KiB
C

#include <stdio.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);
}