Mercurial > repos > blastem
comparison config.c @ 430:7f84090ab1cd
Add config file parser and default config file
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 10 Jul 2013 09:38:05 -0700 |
parents | |
children | 440efd7d27a9 |
comparison
equal
deleted
inserted
replaced
429:f6fdde540791 | 430:7f84090ab1cd |
---|---|
1 #include "tern.h" | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 #include <stdarg.h> | |
6 #include <ctype.h> | |
7 | |
8 #include <sys/types.h> | |
9 #include <sys/stat.h> | |
10 #include <unistd.h> | |
11 | |
12 char * alloc_concat(char * first, char * second) | |
13 { | |
14 int flen = strlen(first); | |
15 int slen = strlen(second); | |
16 char * ret = malloc(flen + slen + 1); | |
17 memcpy(ret, first, flen); | |
18 memcpy(ret+flen, second, slen+1); | |
19 return ret; | |
20 } | |
21 | |
22 char * alloc_concat_m(int num_parts, char ** parts) | |
23 { | |
24 int total = 0; | |
25 for (int i = 0; i < num_parts; i++) { | |
26 total += strlen(parts[i]); | |
27 } | |
28 char * ret = malloc(total + 1); | |
29 *ret = 0; | |
30 for (int i = 0; i < num_parts; i++) { | |
31 strcat(ret, parts[i]); | |
32 } | |
33 return ret; | |
34 } | |
35 | |
36 long file_size(FILE * f) | |
37 { | |
38 fseek(f, 0, SEEK_END); | |
39 long fsize = ftell(f); | |
40 fseek(f, 0, SEEK_SET); | |
41 return fsize; | |
42 } | |
43 | |
44 char * strip_ws(char * text) | |
45 { | |
46 while (*text && (!isprint(*text) || isblank(*text))) | |
47 { | |
48 text++; | |
49 } | |
50 char * ret = text; | |
51 text = ret + strlen(ret) - 1; | |
52 while (text > ret && (!isprint(*text) || isblank(*text))) | |
53 { | |
54 *text = 0; | |
55 text--; | |
56 } | |
57 return ret; | |
58 } | |
59 | |
60 char * split_keyval(char * text) | |
61 { | |
62 while (*text && !isblank(*text)) | |
63 { | |
64 text++; | |
65 } | |
66 if (!*text) { | |
67 return text; | |
68 } | |
69 *text = 0; | |
70 return text+1; | |
71 } | |
72 | |
73 #define MAX_NEST 30 //way more than I'll ever need | |
74 | |
75 tern_node * parse_config(char * config_data) | |
76 { | |
77 char *state, *curline; | |
78 char *prefix = NULL; | |
79 int nest_level = 0; | |
80 char * prefix_parts[MAX_NEST]; | |
81 int line = 1; | |
82 tern_node * head = NULL; | |
83 while ((curline = strtok_r(config_data, "\n", &state))) | |
84 { | |
85 config_data = NULL; | |
86 curline = strip_ws(curline); | |
87 int len = strlen(curline); | |
88 if (!len) { | |
89 continue; | |
90 } | |
91 if (curline[0] == '#') { | |
92 continue; | |
93 } | |
94 if (curline[0] == '}') { | |
95 if (!nest_level) { | |
96 fprintf(stderr, "unexpected } on line %d\n", line); | |
97 exit(1); | |
98 } | |
99 if (prefix) { | |
100 free(prefix); | |
101 prefix = NULL; | |
102 } | |
103 nest_level--; | |
104 curline = strip_ws(curline+1); | |
105 } | |
106 char * end = curline + len - 1; | |
107 if (*end == '{') { | |
108 *end = 0; | |
109 curline = strip_ws(curline); | |
110 prefix_parts[nest_level++] = curline; | |
111 if (prefix) { | |
112 free(prefix); | |
113 prefix = NULL; | |
114 } | |
115 } else { | |
116 if (nest_level && !prefix) { | |
117 prefix = alloc_concat_m(nest_level, prefix_parts); | |
118 } | |
119 char * val = strip_ws(split_keyval(curline)); | |
120 char * key = curline; | |
121 if (*key) { | |
122 if (prefix) { | |
123 key = alloc_concat(prefix, key); | |
124 } | |
125 head = tern_insert_ptr(head, key, val); | |
126 if (prefix) { | |
127 free(key); | |
128 } | |
129 } | |
130 } | |
131 } | |
132 if (prefix) { | |
133 free(prefix); | |
134 } | |
135 return head; | |
136 } | |
137 | |
138 tern_node * parse_config_file(char * config_path) | |
139 { | |
140 tern_node * ret = NULL; | |
141 FILE * config_file = fopen(config_path, "r"); | |
142 if (!config_file) { | |
143 goto open_fail; | |
144 } | |
145 long config_size = file_size(config_file); | |
146 if (!config_size) { | |
147 goto config_empty; | |
148 } | |
149 char * config_data = malloc(config_size); | |
150 if (fread(config_data, 1, config_size, config_file) != config_size) { | |
151 goto config_read_fail; | |
152 } | |
153 ret = parse_config(config_data); | |
154 config_read_fail: | |
155 free(config_data); | |
156 config_empty: | |
157 fclose(config_file); | |
158 open_fail: | |
159 return ret; | |
160 } | |
161 | |
162 char * readlink_alloc(char * path) | |
163 { | |
164 char * linktext = NULL; | |
165 ssize_t linksize = 512; | |
166 ssize_t cursize = 0; | |
167 do { | |
168 if (linksize > cursize) { | |
169 cursize = linksize; | |
170 if (linktext) { | |
171 free(linktext); | |
172 } | |
173 } | |
174 linktext = malloc(cursize); | |
175 linksize = readlink(path, linktext, cursize-1); | |
176 if (linksize == -1) { | |
177 perror("readlink"); | |
178 free(linktext); | |
179 linktext = NULL; | |
180 } | |
181 } while (linksize > cursize); | |
182 return linktext; | |
183 } | |
184 | |
185 tern_node * load_config(char * expath) | |
186 { | |
187 char * linktext; | |
188 char * home = getenv("HOME"); | |
189 if (!home) { | |
190 goto load_in_app_dir; | |
191 } | |
192 char * path = alloc_concat(home, "/.config/blastem/blastem.cfg"); | |
193 tern_node * ret = parse_config_file(path); | |
194 if (ret) { | |
195 goto success; | |
196 } | |
197 free(path); | |
198 load_in_app_dir: | |
199 | |
200 linktext = readlink_alloc("/proc/self/exe"); | |
201 if (!linktext) { | |
202 goto link_prob; | |
203 } | |
204 char * cur; | |
205 int linksize = strlen(linktext); | |
206 for(cur = linktext + linksize - 1; cur != linktext; cur--) | |
207 { | |
208 if (*cur == '/') { | |
209 *cur = 0; | |
210 break; | |
211 } | |
212 } | |
213 if (cur == linktext) { | |
214 goto link_prob; | |
215 } | |
216 path = alloc_concat(linktext, "/default.cfg"); | |
217 ret = parse_config_file(path); | |
218 success: | |
219 return ret; | |
220 link_prob: | |
221 if (linktext) { | |
222 free(linktext); | |
223 } | |
224 no_proc: | |
225 //TODO: Fall back to using expath if /proc is not available | |
226 fputs("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n", stderr); | |
227 exit(1); | |
228 } | |
229 |