Commit | Line | Data |
---|---|---|
9faa7a88 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2004-2008 Richard Kettlewell | |
4 | * | |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
9faa7a88 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
9faa7a88 RK |
8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
9faa7a88 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
9faa7a88 | 17 | */ |
59cf25c4 | 18 | /** @file cgi/options.c |
9faa7a88 | 19 | * @brief CGI options |
1e97629d RK |
20 | * |
21 | * Options represent an additional configuration system private to the | |
22 | * CGI program. | |
9faa7a88 RK |
23 | */ |
24 | ||
1e97629d | 25 | #include "disorder-cgi.h" |
9faa7a88 RK |
26 | |
27 | struct column { | |
28 | int ncolumns; | |
29 | char **columns; | |
30 | }; | |
31 | ||
71634563 RK |
32 | struct read_options_state { |
33 | const char *name; | |
34 | int line; | |
35 | }; | |
36 | ||
9faa7a88 RK |
37 | static hash *labels; |
38 | static hash *columns; | |
39 | ||
71634563 RK |
40 | static void option__readfile(const char *name); |
41 | ||
9faa7a88 RK |
42 | static void option__label(int attribute((unused)) nvec, |
43 | char **vec) { | |
44 | option_set(vec[0], vec[1]); | |
45 | } | |
46 | ||
47 | static void option__include(int attribute((unused)) nvec, | |
48 | char **vec) { | |
49 | option__readfile(vec[0]); | |
50 | } | |
51 | ||
52 | static void option__columns(int nvec, | |
53 | char **vec) { | |
54 | struct column c; | |
55 | ||
56 | c.ncolumns = nvec - 1; | |
57 | c.columns = &vec[1]; | |
58 | hash_add(columns, vec[0], &c, HASH_INSERT_OR_REPLACE); | |
59 | } | |
60 | ||
598b07b7 | 61 | /** @brief Definition of an option command */ |
9faa7a88 | 62 | static struct option { |
598b07b7 | 63 | /** @brief Command name */ |
9faa7a88 | 64 | const char *name; |
598b07b7 RK |
65 | /** @brief Minimum number of arguments */ |
66 | int minargs; | |
67 | /** @brief Maximum number of arguments */ | |
68 | int maxargs; | |
69 | /** @brief Command handler */ | |
9faa7a88 RK |
70 | void (*handler)(int nvec, char **vec); |
71 | } options[] = { | |
71634563 RK |
72 | { "columns", 1, INT_MAX, option__columns }, |
73 | { "include", 1, 1, option__include }, | |
74 | { "label", 2, 2, option__label }, | |
9faa7a88 RK |
75 | }; |
76 | ||
77 | static void option__split_error(const char *msg, | |
78 | void *u) { | |
79 | struct read_options_state *cs = u; | |
80 | ||
2e9ba080 | 81 | disorder_error(0, "%s:%d: %s", cs->name, cs->line, msg); |
9faa7a88 RK |
82 | } |
83 | ||
84 | static void option__readfile(const char *name) { | |
85 | int n, i; | |
9faa7a88 RK |
86 | FILE *fp; |
87 | char **vec, *buffer; | |
88 | struct read_options_state cs; | |
9faa7a88 | 89 | |
f2d306b4 | 90 | if(!(cs.name = mx_find(name, 1/*report*/))) |
9faa7a88 RK |
91 | return; |
92 | if(!(fp = fopen(cs.name, "r"))) | |
2e9ba080 | 93 | disorder_fatal(errno, "error opening %s", cs.name); |
9faa7a88 RK |
94 | cs.line = 0; |
95 | while(!inputline(cs.name, fp, &buffer, '\n')) { | |
96 | ++cs.line; | |
97 | if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES, | |
98 | option__split_error, &cs))) | |
99 | continue; | |
100 | if(!n) | |
101 | continue; | |
ba937f01 | 102 | if((i = TABLE_FIND(options, name, vec[0])) == -1) { |
2e9ba080 | 103 | disorder_error(0, "%s:%d: unknown option '%s'", cs.name, cs.line, vec[0]); |
9faa7a88 RK |
104 | continue; |
105 | } | |
106 | ++vec; | |
107 | --n; | |
108 | if(n < options[i].minargs) { | |
2e9ba080 | 109 | disorder_error(0, "%s:%d: too few arguments to '%s'", cs.name, cs.line, vec[-1]); |
9faa7a88 RK |
110 | continue; |
111 | } | |
112 | if(n > options[i].maxargs) { | |
2e9ba080 | 113 | disorder_error(0, "%s:%d: too many arguments to '%s'", cs.name, cs.line, vec[-1]); |
9faa7a88 RK |
114 | continue; |
115 | } | |
116 | options[i].handler(n, vec); | |
117 | } | |
118 | fclose(fp); | |
119 | } | |
120 | ||
121 | static void option__init(void) { | |
122 | static int have_read_options; | |
123 | ||
124 | if(!have_read_options) { | |
125 | have_read_options = 1; | |
126 | labels = hash_new(sizeof (char *)); | |
127 | columns = hash_new(sizeof (struct column)); | |
128 | option__readfile("options"); | |
129 | } | |
130 | } | |
131 | ||
132 | /** @brief Set an option | |
133 | * @param name Option name | |
134 | * @param value Option value | |
135 | * | |
136 | * If the option was already set its value is replaced. | |
137 | * | |
138 | * @p name and @p value are copied. | |
139 | */ | |
140 | void option_set(const char *name, const char *value) { | |
141 | char *v = xstrdup(value); | |
142 | ||
143 | option__init(); | |
144 | hash_add(labels, name, &v, HASH_INSERT_OR_REPLACE); | |
145 | } | |
146 | ||
147 | /** @brief Get a label | |
148 | * @param key Name of label | |
149 | * @return Value of label (never NULL) | |
150 | * | |
151 | * If label images.X isn't found then the return value is | |
152 | * <url.static>X.png, allowing url.static to be used to provide a base | |
153 | * for all images with per-image overrides. | |
154 | * | |
155 | * Otherwise undefined labels expand to their last (dot-separated) | |
156 | * component. | |
157 | */ | |
158 | const char *option_label(const char *key) { | |
159 | const char *label; | |
99955407 | 160 | char **lptr; |
9faa7a88 RK |
161 | |
162 | option__init(); | |
99955407 RK |
163 | lptr = hash_find(labels, key); |
164 | if(lptr) | |
165 | return *lptr; | |
166 | /* No label found */ | |
167 | if(!strncmp(key, "images.", 7)) { | |
168 | static const char *url_static; | |
169 | /* images.X defaults to <url.static>X.png */ | |
170 | ||
171 | if(!url_static) | |
172 | url_static = option_label("url.static"); | |
173 | byte_xasprintf((char **)&label, "%s%s.png", url_static, key + 7); | |
174 | } else if((label = strrchr(key, '.'))) | |
175 | /* X.Y defaults to Y */ | |
176 | ++label; | |
177 | else | |
178 | /* otherwise default to label name */ | |
179 | label = key; | |
9faa7a88 RK |
180 | return label; |
181 | } | |
182 | ||
183 | /** @brief Test whether a label exists | |
184 | * @param key Name of label | |
185 | * @return 1 if label exists, otherwise 0 | |
186 | * | |
187 | * Labels that don't exist still have an expansion (per option_label() | |
188 | * documentation), and possibly not even a placeholder one. | |
189 | */ | |
190 | int option_label_exists(const char *key) { | |
191 | option__init(); | |
192 | return !!hash_find(labels, key); | |
193 | } | |
194 | ||
195 | /** @brief Return a column list | |
196 | * @param name Context (playing/recent/etc) | |
197 | * @param ncolumns Where to store column count or NULL | |
198 | * @return Pointer to column list | |
199 | */ | |
200 | char **option_columns(const char *name, int *ncolumns) { | |
201 | struct column *c; | |
202 | ||
203 | option__init(); | |
204 | c = hash_find(columns, name); | |
205 | if(c) { | |
206 | if(ncolumns) | |
207 | *ncolumns = c->ncolumns; | |
208 | return c->columns; | |
209 | } else { | |
210 | if(ncolumns) | |
211 | *ncolumns = 0; | |
212 | return 0; | |
213 | } | |
214 | } | |
215 | ||
216 | /* | |
217 | Local Variables: | |
218 | c-basic-offset:2 | |
219 | comment-column:40 | |
220 | End: | |
221 | */ |