-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
172 lines (147 loc) · 3.89 KB
/
utils.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
165
166
167
168
169
170
171
172
#include "inc.h"
static void msg(char* kind, char* fmt, va_list ap) {
fprintf(stderr, "%s: ", kind);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
static void msgat(char* kind, Token tok, char* fmt, va_list ap) {
fprintf(stderr, "%s:%d:%d: %s: ", options.input_filename, tok->line_no,
tok->char_no + 1, kind);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
fprintf(stderr, " ");
for (const char* p = tok->line; *p && *p != '\n'; ++p)
fputc(*p, stderr);
fprintf(stderr, "\n");
fprintf(stderr, " %*s%s\n", tok->char_no, "", "^");
}
void error(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msg("error", fmt, ap);
va_end(ap);
*((int*)0) = 1;
}
void warn(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msg("warn", fmt, ap);
va_end(ap);
}
void info(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msg("info", fmt, ap);
va_end(ap);
}
void errorat(Token tok, char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msgat("error", tok, fmt, ap);
va_end(ap);
*((int*)0) = 1;
}
void warnat(Token tok, char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msgat("warn", tok, fmt, ap);
va_end(ap);
}
void infoat(Token tok, char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
msgat("info", tok, fmt, ap);
va_end(ap);
}
static unsigned long hash(unsigned char* str, unsigned n) {
unsigned long hash = 5381;
for (int i = 0; i < n; i++)
hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */
return hash;
}
#define STSIZE 128
static struct string {
char* s;
struct string* next;
} * string_table[STSIZE];
const char* stringn(const char* s, int n) {
unsigned h = hash((unsigned char*)s, n) % STSIZE;
for (struct string* i = string_table[h]; i; i = i->next) {
if (strlen(i->s) == n && strncmp(i->s, s, n) == 0)
return i->s;
}
struct string* new = malloc(sizeof(struct string));
new->s = calloc(n + 1, sizeof(char));
strncpy(new->s, s, n);
new->next = string_table[h];
string_table[h] = new;
return new->s;
}
const char* string(const char* s) {
return stringn(s, strlen(s));
}
int read_source(char** dst) {
int length;
FILE* f;
if (!(f = fopen(options.input_filename, "rb")))
error("can't open input file");
if (fseek(f, 0, SEEK_END))
error("can't seek input file");
if ((length = ftell(f)) == -1)
error("can't tell input file");
if (fseek(f, 0, SEEK_SET))
error("can't seek input file");
if (!(*dst = calloc(1 + length, 1)))
error("can't allocate memory");
if (fread(*dst, 1, length, f) != length)
error("can't read intput file");
fclose(f);
return length;
}
void output(const char* fmt, ...) {
static FILE* f = NULL;
if (!f && !(f = fopen(options.output_filename, "wb")))
error("can't open output file");
va_list ap;
va_start(ap, fmt);
vfprintf(f, fmt, ap);
}
static const char* basename(const char* path) {
char* s = strrchr(path, '/');
return s ? s + 1 : path;
}
static const char* extension(const char* path) {
char* s = strrchr(basename(path), '.');
return s ? s + 1 : s + strlen(s);
}
static char* strdup(const char* name) {
int n = strlen(name);
char* s = malloc(n + 1);
strcpy(s, name);
s[n] = 0;
return s;
}
struct options options;
void parse_arguments(int argc, char* argv[]) {
int idx = 1;
while (idx < argc) {
if (strcmp(argv[idx], "-o") == 0) {
if (++idx == argc)
error("missing file name after -o");
options.output_filename = argv[idx++];
} else {
if (options.input_filename)
error("more than one input file");
options.input_filename = argv[idx++];
}
}
if (!options.input_filename)
error("no input file");
if (strcmp(extension(options.input_filename), "c") != 0)
error("input file should have exension \".c\"");
if (!options.output_filename) {
char* name = strdup(basename(options.input_filename));
name[strlen(name) - 1] = 's';
options.output_filename = name;
}
}