-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpreproc.c
61 lines (58 loc) · 1.79 KB
/
preproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "preproc.h"
#include "ObjectTree.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "errors.h"
#define LINESIZE 1024
int readFile(char name[], FILE * ofp, int *numline)
{
FILE *fp;
char line[LINESIZE], word[LINESIZE], word2[LINESIZE];
size_t len = strlen(name);
if (name[len - 1] == '"')
name[len - 1] = '\0';
if (name[0] == '"') {
name++;
}
if ((fp = fopen(name, "r")) == 0) {
compilerDebugPrintf("Cannot find import file %s in working directory. Trying RIX_HOME\n",
name);
const char *RIX_HOME = getenv("RIX_HOME");
char importPath[BUFFLEN];
if (RIX_HOME != 0) {
snprintf(importPath, BUFFLEN, "%s/%s", RIX_HOME, name);
if ((fp = fopen(importPath, "r")) == 0) {
printf
("Cannot find import file %s in working directory or RIX_HOME\n",
importPath);
perror("open");
return 1;
}
} else {
criticalError(0, "RIX_HOME not set.\n");
perror("open");
return 1;
}
}
while (fgets(line, LINESIZE, fp)) {
char * importPos = strstr(line, "import");
if(importPos != NULL) {
char * openParenPos = strchr(importPos, '(');
if (openParenPos!=NULL) {
char *word2 = malloc(BUFFLEN);
char *cursor = openParenPos + 1;
int i = 0;
while (*(cursor + i) != ')') {
word2[i] = *(cursor + i);
i++;
}
word[i] = '\0';
readFile(word2, ofp, numline);
continue;
}
}
(*numline)++;
fprintf(ofp, "%s", line);
}
}