139 lines
2.5 KiB
C
139 lines
2.5 KiB
C
#include "pp_ifdef.h"
|
|
#include "pp_defines.h"
|
|
#include "expand_line.h"
|
|
#include "utils.h"
|
|
#include "pp_line_id.h"
|
|
#include "line_handler.h"
|
|
#include <stdlib.h>
|
|
|
|
int _is_sym_defined(void *def_map, char *ifdef_line);
|
|
|
|
int expand_ifdef(void *def_map, const struct str_dyn_arr *inc_dirs, FILE *source, FILE *out)
|
|
{
|
|
char line[PP_LINE_LEN + 1];
|
|
char *exp_line;
|
|
|
|
int ret;
|
|
int line_len;
|
|
int if_en;
|
|
int else_en;
|
|
|
|
if_en = 0;
|
|
else_en = 0;
|
|
|
|
while (fgets(line, PP_LINE_LEN, source)) {
|
|
if (IS_ENDIF_LINE(line))
|
|
break;
|
|
|
|
if (if_en && IS_ELSE_LINE(line)) {
|
|
ret = skip_until(source, SKIP_UNTIL_ENDIF_LINE);
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
if_en = 0;
|
|
|
|
continue;
|
|
}
|
|
|
|
if (if_en || else_en) {
|
|
ret = line_handler(line, def_map, inc_dirs, source, out);
|
|
if (ret == PP_FAILED)
|
|
return ret;
|
|
|
|
continue;
|
|
}
|
|
|
|
if (IS_IFDEF_LINE(line)) {
|
|
exp_line = get_expanded_line(def_map, line);
|
|
if (!exp_line)
|
|
return PP_FAILED;
|
|
|
|
if (!_is_sym_defined(def_map, line)) {
|
|
ret = skip_until(source, SKIP_UNTIL_CURR_BRANCH_END);
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
|
|
fgets(line, PP_LINE_LEN, source);
|
|
if (IS_ELSE_LINE(line)) {
|
|
else_en = 1;
|
|
} else {
|
|
line_len = strlen(line);
|
|
ret = fseek(source, -line_len, SEEK_CUR);
|
|
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
}
|
|
} else {
|
|
if_en = 1;
|
|
}
|
|
|
|
free(exp_line);
|
|
|
|
continue;
|
|
}
|
|
|
|
if (IS_IFNDEF_LINE(line)) {
|
|
exp_line = get_expanded_line(def_map, line);
|
|
|
|
if (!exp_line)
|
|
return PP_FAILED;
|
|
|
|
if (_is_sym_defined(def_map, line)) {
|
|
ret = skip_until(source, SKIP_UNTIL_CURR_BRANCH_END);
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
|
|
fgets(line, PP_LINE_LEN, source);
|
|
|
|
if (IS_ELSE_LINE(line)) {
|
|
else_en = 1;
|
|
} else {
|
|
line_len = strlen(line);
|
|
ret = fseek(source, -line_len, SEEK_CUR);
|
|
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
}
|
|
} else {
|
|
if_en = 1;
|
|
}
|
|
|
|
free(exp_line);
|
|
|
|
continue;
|
|
}
|
|
|
|
}
|
|
|
|
return PP_OK;
|
|
}
|
|
|
|
int _is_sym_defined(void *def_map, char *ifdef_line)
|
|
{
|
|
char *sym;
|
|
char *mapp;
|
|
|
|
int sym_len;
|
|
int ret;
|
|
|
|
if (!strncmp(ifdef_line, IFDEF_LINE_START, IFDEF_LINE_START_SZ))
|
|
sym = ifdef_line + IFDEF_LINE_START_SZ;
|
|
else if (!strncmp(ifdef_line, IFNDEF_LINE_START, IFNDEF_LINE_START_SZ))
|
|
sym = ifdef_line + IFNDEF_LINE_START_SZ;
|
|
|
|
sym_len = strlen(sym);
|
|
sym[sym_len - 1] = 0x00;
|
|
|
|
ret = hashmap_get(def_map, sym, &mapp);
|
|
if (ret == MAP_BAD_ARGS) {
|
|
ret = -1;
|
|
goto rettore_line_and_exit;
|
|
}
|
|
|
|
ret = ret == MAP_OK && mapp;
|
|
|
|
rettore_line_and_exit:
|
|
sym[sym_len - 1] = '\n';
|
|
return ret;
|
|
}
|
|
|