86 lines
1.4 KiB
C
86 lines
1.4 KiB
C
#include "pp_if.h"
|
|
#include "pp_line_id.h"
|
|
#include "line_handler.h"
|
|
#include "utils.h"
|
|
#include "expand_line.h"
|
|
#include <stdlib.h>
|
|
|
|
int expand_if(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) || IS_ELIF_LINE(line))) {
|
|
ret = skip_until(source, SKIP_UNTIL_ENDIF_LINE);
|
|
if (ret == -1)
|
|
return PP_FAILED;
|
|
if_en = 0;
|
|
}
|
|
|
|
if (if_en || else_en) {
|
|
ret = line_handler(
|
|
line,
|
|
def_map,
|
|
inc_dirs,
|
|
source,
|
|
out
|
|
);
|
|
|
|
if (ret == PP_FAILED)
|
|
return ret;
|
|
}
|
|
|
|
if (IS_IF_LINE(line) || IS_ELIF_LINE(line)) {
|
|
exp_line = get_expanded_line(def_map, line);
|
|
|
|
if (!exp_line)
|
|
return PP_FAILED;
|
|
|
|
if (is_cond_null(exp_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);
|
|
}
|
|
|
|
}
|
|
|
|
return PP_OK;
|
|
}
|