forked from rui314/8cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
164 lines (147 loc) · 3.5 KB
/
file.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2014 Rui Ueyama. Released under the MIT license.
/*
* This file provides character input stream for C source code.
* An input stream is either backed by stdio's FILE * or
* backed by a string.
* The following input processing is done at this stage.
*
* - C11 5.1.1.2p1: "\r\n" or "\r" are canonicalized to "\n".
* - C11 5.1.1.2p2: A sequence of backslash and newline is removed.
* - EOF not immediately following a newline is converted to
* a sequence of newline and EOF. (The C spec requires source
* files end in a newline character (5.1.1.2p2). Thus, if all
* source files are comforming, this step wouldn't be needed.)
*
* Trigraphs are not supported by design.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "8cc.h"
static Vector *files = &EMPTY_VECTOR;
static Vector *stashed = &EMPTY_VECTOR;
File *make_file(FILE *file, char *name) {
File *r = calloc(1, sizeof(File));
r->file = file;
r->name = name;
r->line = 1;
r->column = 1;
struct stat st;
if (fstat(fileno(file), &st) == -1)
error("fstat failed: %s", strerror(errno));
r->mtime = st.st_mtime;
return r;
}
File *make_file_string(char *s) {
File *r = calloc(1, sizeof(File));
r->line = 1;
r->column = 1;
r->p = s;
return r;
}
static void close_file(File *f) {
if (f->file)
fclose(f->file);
}
static int readc_file(File *f) {
int c = getc(f->file);
if (c == EOF) {
c = (f->last == '\n' || f->last == EOF) ? EOF : '\n';
} else if (c == '\r') {
int c2 = getc(f->file);
if (c2 != '\n')
ungetc(c2, f->file);
c = '\n';
}
f->last = c;
return c;
}
static int readc_string(File *f) {
int c;
if (*f->p == '\0') {
c = (f->last == '\n' || f->last == EOF) ? EOF : '\n';
} else if (*f->p == '\r') {
f->p++;
if (*f->p == '\n')
f->p++;
c = '\n';
} else {
c = *f->p++;
}
f->last = c;
return c;
}
static int get() {
File *f = vec_tail(files);
int c;
if (f->buflen > 0) {
c = f->buf[--f->buflen];
} else if (f->file) {
c = readc_file(f);
} else {
c = readc_string(f);
}
if (c == '\n') {
f->line++;
f->column = 1;
} else if (c != EOF) {
f->column++;
}
return c;
}
int readc() {
for (;;) {
int c = get();
if (c == EOF) {
if (vec_len(files) == 1)
return c;
close_file(vec_pop(files));
continue;
}
if (c != '\\')
return c;
int c2 = get();
if (c2 == '\n')
continue;
unreadc(c2);
return c;
}
}
void unreadc(int c) {
if (c == EOF)
return;
File *f = vec_tail(files);
assert(f->buflen < sizeof(f->buf) / sizeof(f->buf[0]));
f->buf[f->buflen++] = c;
if (c == '\n') {
f->column = 1;
f->line--;
} else {
f->column--;
}
}
File *current_file() {
return vec_tail(files);
}
void stream_push(File *f) {
vec_push(files, f);
}
int stream_depth() {
return vec_len(files);
}
char *input_position() {
if (vec_len(files) == 0)
return "(unknown)";
File *f = vec_tail(files);
return format("%s:%d:%d", f->name, f->line, f->column);
}
void stream_stash(File *f) {
vec_push(stashed, files);
files = make_vector1(f);
}
void stream_unstash() {
files = vec_pop(stashed);
}