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