blob: 03936a7a92d08dcff9fb9baf0225fb105740b745 [file] [log] [blame]
swissChilic3829942020-09-06 19:36:04 -07001#include "as.h"
2#include "map.h"
3
4#include <errno.h>
5#include <string.h>
6#include <stdlib.h>
7
8enum /* State */
9{
10 NORMAL, /* Default State */
11 MACRO_DEF /* In Macro Definition */
12};
13
14enum /* Flags */
15{
16 PRESERVE_MACRO_ARGS,
17};
18
19int preproc(char *in, FILE *out, map_t *macros, int flags)
20{
21 int line_no = 1,
22 err = 0,
23 state = NORMAL;
24 char *line = strtok_fix(in, "\n"),
25 *macro_name = NULL;
26
27 while (line)
28 {
29 skip_ws(&line);
30
31 if (*line == '%')
32 {
33 // Line is preprocessor directive
34
35 char *directive = parse_label_name(&line);
36 if (!directive)
37 {
38 fprintf(stderr, ERR "Expected preprocessor directive on line %d\n" RESET,
39 line_no);
40 err = EINVAL;
41 goto cleanup;
42 }
43 else
44 {
45 if (!strcasecmp(directive, "macro"))
46 {
47 skip_ws(&line);
48 macro_name = parse_label_name(&line);
49 if (!macro_name)
50 {
51 fprintf(stderr, ERR "Expected name after %%macro on line %d\n" RESET,
52 line_no);
53 err = EINVAL;
54 goto cleanup;
55 }
56
57 if (!ws_end(&line))
58 {
59 fprintf(stderr, ERR "Expected new line after macro definition\n" RESET);
60 err = EINVAL;
61 goto cleanup;
62 }
63
64 state = MACRO_DEF;
65 }
66 }
67 }
68
69 fprintf(out, "%s\n", line);
70
71 line = strtok_fix(NULL, "\n");
72 line_no++;
73 }
74
75cleanup:
76 return err;
77}