1 // Implementation of some file loading functionality.
13 #include <sys/param.h>
19 static const int tabstop = 4;
22 char *resolve_path(const char *path, const char *relative_to, const char *system_path) {
23 if (!relative_to || streq(relative_to, "/dev/stdin")) relative_to = ".";
24 if (!path || strlen(path) == 0) return NULL;
26 // Resolve the path to an absolute path, assuming it's relative to the file
28 char buf[PATH_MAX] = {0};
29 if (streq(path, "~") || starts_with(path, "~/")) {
30 char *resolved = realpath(String(getenv("HOME"), path + 1), buf);
31 if (resolved) return GC_strdup(resolved);
32 } else if (streq(path, ".") || starts_with(path, "./") || starts_with(path, "../")) {
33 char *relative_dir = dirname(GC_strdup(relative_to));
34 char *resolved = realpath(String(relative_dir, "/", relative_dir, path), buf);
35 if (resolved) return GC_strdup(resolved);
36 } else if (path[0] == '/') {
38 char *resolved = realpath(path, buf);
39 if (resolved) return GC_strdup(resolved);
42 char *relative_dir = dirname(GC_strdup(relative_to));
43 if (!system_path) system_path = ".";
44 char *copy = GC_strdup(system_path);
45 for (char *dir, *pos = copy; (dir = strtok(pos, ":")); pos = NULL) {
47 char *resolved = realpath(String(dir, "/", path), buf);
48 if (resolved) return GC_strdup(resolved);
49 } else if (dir[0] == '~' && (dir[1] == '\0' || dir[1] == '/')) {
50 char *resolved = realpath(String(getenv("HOME"), dir + 1, "/", path), buf);
51 if (resolved) return GC_strdup(resolved);
52 } else if (streq(dir, ".") || strncmp(dir, "./", 2) == 0) {
53 char *resolved = realpath(String(relative_dir, "/", path), buf);
54 if (resolved) return GC_strdup(resolved);
55 } else if (streq(dir, ".") || streq(dir, "..") || strncmp(dir, "./", 2) == 0
56 || strncmp(dir, "../", 3) == 0) {
57 char *resolved = realpath(String(relative_dir, "/", dir, "/", path), buf);
58 if (resolved) return GC_strdup(resolved);
60 char *resolved = realpath(String(dir, "/", path), buf);
61 if (resolved) return GC_strdup(resolved);
69 char *file_base_name(const char *path) {
70 const char *slash = strrchr(path, '/');
71 if (slash) path = slash + 1;
72 assert(!isdigit(*path));
73 const char *end = path + strcspn(path, ".");
74 size_t len = (size_t)(end - path);
75 char *buf = GC_MALLOC_ATOMIC(len + 1);
76 strncpy(buf, path, len);
81 static file_t *_load_file(const char *filename, FILE *file) {
82 if (!file) return NULL;
84 file_t *ret = new (file_t, .filename = filename);
86 size_t file_size = 0, line_cap = 0;
87 char *file_buf = NULL, *line_buf = NULL;
88 FILE *mem = open_memstream(&file_buf, &file_size);
90 while ((line_len = getline(&line_buf, &line_cap, file)) >= 0) {
91 if (ret->line_capacity <= ret->num_lines)
92 ret->line_offsets = GC_REALLOC(ret->line_offsets, sizeof(int64_t[ret->line_capacity += 32]));
93 ret->line_offsets[ret->num_lines++] = (int64_t)file_size;
94 fwrite(line_buf, sizeof(char), (size_t)line_len, mem);
99 char *copy = GC_MALLOC_ATOMIC(file_size + 1);
100 memcpy(copy, file_buf, file_size);
101 copy[file_size] = '\0';
103 ret->len = (int64_t)file_size;
107 ret->relative_filename = filename;
108 if (filename && filename[0] != '<' && !streq(filename, "/dev/stdin")) {
109 filename = resolve_path(filename, ".", ".");
110 // Convert to relative path (if applicable)
112 char *cwd = getcwd(buf, sizeof(buf));
114 size_t cwd_len = strlen(cwd);
115 if (strncmp(cwd, filename, cwd_len) == 0 && filename[cwd_len] == '/')
116 ret->relative_filename = &filename[cwd_len + 1];
119 if (line_buf != NULL) free(line_buf);
124 // Read an entire file into memory.
127 file_t *load_file(const char *filename) {
128 FILE *file = filename[0] ? fopen(filename, "r") : stdin;
129 return _load_file(filename, file);
133 // Create a virtual file from a string.
136 file_t *spoof_file(const char *filename, const char *text) {
137 FILE *file = fmemopen((char *)text, strlen(text) + 1, "r");
138 return _load_file(filename, file);
142 // Given a pointer, determine which line number it points to (1-indexed)
145 int64_t get_line_number(file_t *f, const char *p) {
147 int64_t lo = 0, hi = (int64_t)f->num_lines - 1;
148 if (p < f->text) return 0;
149 int64_t offset = (int64_t)(p - f->text);
151 int64_t mid = (lo + hi) / 2;
152 int64_t line_offset = f->line_offsets[mid];
153 if (line_offset == offset) return mid + 1;
154 else if (line_offset < offset) lo = mid + 1;
155 else if (line_offset > offset) hi = mid - 1;
157 return lo; // Return the line number whose line starts closest before p
161 // Given a pointer, determine which line column it points to.
164 int64_t get_line_column(file_t *f, const char *p) {
165 int64_t line_no = get_line_number(f, p);
166 int64_t line_offset = f->line_offsets[line_no - 1];
167 return 1 + (int64_t)(p - (f->text + line_offset));
171 // Return a pointer to the line with the specified line number (1-indexed)
174 const char *get_line(file_t *f, int64_t line_number) {
175 if (line_number == 0 || line_number > (int64_t)f->num_lines) return NULL;
176 int64_t line_offset = f->line_offsets[line_number - 1];
177 return f->text + line_offset;
181 // Return a value like /foo:line.col
184 const char *get_file_pos(file_t *f, const char *p) {
185 return String(f->filename, ":", get_line_number(f, p), ".", get_line_column(f, p));
188 static int fputc_column(FILE *out, char c, char print_char, int *column) {
190 if (print_char == '\t') print_char = ' ';
192 for (int to_fill = tabstop - (*column % tabstop); to_fill > 0; --to_fill) {
193 printed += fputc(print_char, out);
197 printed += fputc(print_char, out);
204 // Print a span from a file
207 int highlight_error(file_t *file, const char *start, const char *end, const char *hl_color, int64_t context_lines,
211 // Handle spans that come from multiple files:
212 if (start < file->text || start > file->text + file->len) start = end;
213 if (end < file->text || end > file->text + file->len) end = start;
214 // Just in case neither end of the span came from this file:
215 if (end < file->text || end > file->text + file->len) start = end = file->text;
217 const char *lineno_prefix, *lineno_suffix, *normal_color, *empty_marker;
218 bool print_carets = false;
221 lineno_prefix = "\x1b[0;2m";
222 lineno_suffix = "\x1b(0\x78\x1b(B\x1b[m ";
223 normal_color = "\x1b[m";
224 empty_marker = "\x1b(0\x61\x1b(B";
225 printed += fprint(stderr, "\x1b[33;4;1m", file->relative_filename, "\x1b[m");
228 lineno_suffix = "| ";
233 printed += fprint(stderr, file->relative_filename);
236 if (context_lines == 0) return fprint(stderr, hl_color, string_slice(start, (size_t)(end - start)), normal_color);
238 int64_t start_line = get_line_number(file, start), end_line = get_line_number(file, end);
240 int64_t first_line = start_line - (context_lines - 1), last_line = end_line + (context_lines - 1);
242 if (first_line < 1) first_line = 1;
243 if (last_line > file->num_lines) last_line = file->num_lines;
246 for (int64_t i = last_line; i > 0; i /= 10)
249 for (int64_t line_no = first_line; line_no <= last_line; ++line_no) {
250 if (line_no > first_line + 5 && line_no < last_line - 5) {
252 printed += fprint(stderr, "\x1b[0;2;3;4m ... ", (last_line - first_line) - 11,
253 " lines omitted ... \x1b[m");
254 else printed += fprint(stderr, " ... ", (last_line - first_line) - 11, " lines omitted ...");
255 line_no = last_line - 6;
259 int needed_spaces = digits;
260 for (int64_t n = line_no; n > 0; n /= 10)
263 printed += fprint_inline(stderr, lineno_prefix, repeated_char(' ', needed_spaces), line_no, lineno_suffix);
264 const char *line = get_line(file, line_no);
268 const char *p = line;
270 for (; *p && *p != '\r' && *p != '\n' && p < start; ++p)
271 printed += fputc_column(stderr, *p, *p, &column);
273 // Zero-width matches
274 if (p == start && start == end) {
275 printed += fprint_inline(stderr, hl_color, empty_marker, normal_color);
280 if (start <= p && p < end) {
281 printed += fputs(hl_color, stderr);
282 for (; *p && *p != '\r' && *p != '\n' && p < end; ++p)
283 printed += fputc_column(stderr, *p, *p, &column);
284 printed += fputs(normal_color, stderr);
288 for (; *p && *p != '\r' && *p != '\n'; ++p)
289 printed += fputc_column(stderr, *p, *p, &column);
291 printed += fprint_inline(stderr, "\n");
293 const char *eol = line + strcspn(line, "\r\n");
294 if (print_carets && start >= line && start < eol && line <= start) {
295 for (int num = 0; num < digits; num++)
296 printed += fputc(' ', stderr);
297 printed += fputs(": ", stderr);
299 for (const char *sp = line; *sp && *sp != '\n'; ++sp) {
301 if (sp < start) print_char = ' ';
302 else if (sp == start && sp == end) print_char = '^';
303 else if (sp >= start && sp < end) print_char = '-';
304 else print_char = ' ';
305 printed += fputc_column(stderr, *sp, print_char, &col);
307 printed += fputs("\n", stderr);