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