103 lines
1.9 KiB
Markdown
103 lines
1.9 KiB
Markdown
# main.c
|
|
|
|
Entry point of the c-md transpiler.
|
|
|
|
Parse command-line arguments, validate them, and run the transpilation process.
|
|
|
|
## Includes
|
|
```c
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
#include "cli.h"
|
|
#include "validator.h"
|
|
#include "transpile.h"
|
|
#include "map.h"
|
|
#include "io.h"
|
|
```
|
|
|
|
## Forward Declarations
|
|
|
|
```c
|
|
static int8_t run(t_args *args);
|
|
static void print_error(int8_t code, const char *progname);
|
|
```
|
|
|
|
## Main Function
|
|
|
|
Program entry point: parse arguments, validate, and run transpilation.
|
|
|
|
```c
|
|
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));
|
|
}
|
|
```
|
|
|
|
## Print Error Function
|
|
|
|
print_error: Print error messages based on validation code.
|
|
|
|
error codes:
|
|
1. Missing -e when reading from stdin
|
|
2. Missing -e when filename does not imply extension
|
|
|
|
```c
|
|
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);
|
|
}
|
|
```
|
|
|
|
## Run Function
|
|
|
|
Run the transpilation process with given arguments.
|
|
|
|
```c
|
|
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);
|
|
}
|
|
```
|