c-preprocessor/so-cpp.c

84 lines
1.2 KiB
C

/* SPDX-License-Identifier: BSD-2 */
#include "hashmap.h"
#include "pp.h"
#include "pp_defines.h"
#include "pp_args.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv)
{
void *def_map;
struct args *args;
FILE *source;
FILE *out;
int ret;
def_map = NULL;
args = NULL;
source = NULL;
out = NULL;
def_map = hashmap_new();
args = pp_parse_args(argc, argv, def_map);
if (!args) {
if (errno)
ret = errno;
else
ret = PP_FAILED;
goto free_and_exit;
}
if (args->in_file) {
source = fopen(args->in_file, "rb");
if (!source) {
ret = errno;
goto free_and_exit;
}
} else {
source = stdin;
}
if (args->out_file) {
out = fopen(args->out_file, "w");
if (!out) {
ret = errno;
goto free_and_exit;
}
} else {
out = stdout;
}
ret = pp_compute(def_map, args->inc_dirs, source, out);
if (ret == PP_FAILED && errno == ENOMEM) {
ret = ENOMEM;
goto free_and_exit;
}
if (ret == PP_OK && errno == ENOENT) {
ret = PP_OK;
goto free_and_exit;
}
if (ret == PP_FAILED)
goto free_and_exit;
ret = errno;
free_and_exit:
hashmap_free(def_map);
pp_free_args(args);
if (source)
fclose(source);
if (out)
fclose(out);
return ret;
}