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
|
/*
Copyright 2013 Michael Pavone
This file is part of BlastEm.
BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include "tern.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
char * strtok_r(char * input, char * sep, char ** state)
{
if (input) {
*state = input;
}
char * ret = *state;
while (**state && **state != *sep)
{
++*state;
}
if (**state)
{
**state = 0;
++*state;
return ret;
}
return NULL;
}
#endif
tern_node * parse_config_int(char **state, int started, int *line)
{
char *config_data, *curline;
tern_node * head = NULL;
config_data = started ? NULL : *state;
while ((curline = strtok_r(config_data, "\n", state)))
{
config_data = NULL;
curline = strip_ws(curline);
int len = strlen(curline);
if (!len) {
*line++;
continue;
}
if (curline[0] == '#') {
*line++;
continue;
}
if (curline[0] == '}') {
if (started) {
return head;
}
fatal_error("unexpected } on line %d\n", *line);
}
char * end = curline + len - 1;
if (*end == '{') {
*end = 0;
curline = strip_ws(curline);
*line++;
head = tern_insert_node(head, curline, parse_config_int(state, 1, line));
} else {
char * val = strip_ws(split_keyval(curline));
char * key = curline;
if (*val) {
head = tern_insert_ptr(head, key, strdup(val));
} else {
fprintf(stderr, "Key %s is missing a value on line %d\n", key, *line);
}
*line++;
}
}
return head;
}
tern_node * parse_config(char * config_data)
{
int line = 1;
return parse_config_int(&config_data, 0, &line);
}
tern_node * parse_config_file(char * config_path)
{
tern_node * ret = NULL;
FILE * config_file = fopen(config_path, "r");
if (!config_file) {
goto open_fail;
}
long config_size = file_size(config_file);
if (!config_size) {
goto config_empty;
}
char * config_data = malloc(config_size);
if (fread(config_data, 1, config_size, config_file) != config_size) {
goto config_read_fail;
}
ret = parse_config(config_data);
config_read_fail:
free(config_data);
config_empty:
fclose(config_file);
open_fail:
return ret;
}
tern_node * load_config()
{
char * exe_dir;
char * home = get_home_dir();
if (!home) {
goto load_in_app_dir;
}
char * path = alloc_concat(home, "/.config/blastem/blastem.cfg");
tern_node * ret = parse_config_file(path);
if (ret) {
goto success;
}
free(path);
load_in_app_dir:
exe_dir = get_exe_dir();
if (!exe_dir) {
goto no_config;
}
path = alloc_concat(exe_dir, "/default.cfg");
ret = parse_config_file(path);
free(path);
success:
if (ret) {
return ret;
}
no_config:
fatal_error("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n");
//this will never get reached, but the compiler doesn't know that. Let's make it happy
return NULL;
}
|