chiark / gitweb /
split out dcgi_get_cookie
[disorder] / server / options.c
CommitLineData
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
1e97629d
RK
22 *
23 * Options represent an additional configuration system private to the
24 * CGI program.
9faa7a88
RK
25 */
26
1e97629d 27#include "disorder-cgi.h"
9faa7a88
RK
28
29struct column {
30 int ncolumns;
31 char **columns;
32};
33
71634563
RK
34struct read_options_state {
35 const char *name;
36 int line;
37};
38
9faa7a88
RK
39static hash *labels;
40static hash *columns;
41
71634563
RK
42static void option__readfile(const char *name);
43
9faa7a88
RK
44static void option__label(int attribute((unused)) nvec,
45 char **vec) {
46 option_set(vec[0], vec[1]);
47}
48
49static void option__include(int attribute((unused)) nvec,
50 char **vec) {
51 option__readfile(vec[0]);
52}
53
54static void option__columns(int nvec,
55 char **vec) {
56 struct column c;
57
58 c.ncolumns = nvec - 1;
59 c.columns = &vec[1];
60 hash_add(columns, vec[0], &c, HASH_INSERT_OR_REPLACE);
61}
62
63static struct option {
64 const char *name;
65 int minargs, maxargs;
66 void (*handler)(int nvec, char **vec);
67} options[] = {
71634563
RK
68 { "columns", 1, INT_MAX, option__columns },
69 { "include", 1, 1, option__include },
70 { "label", 2, 2, option__label },
9faa7a88
RK
71};
72
73static void option__split_error(const char *msg,
74 void *u) {
75 struct read_options_state *cs = u;
76
77 error(0, "%s:%d: %s", cs->name, cs->line, msg);
78}
79
80static void option__readfile(const char *name) {
81 int n, i;
9faa7a88
RK
82 FILE *fp;
83 char **vec, *buffer;
84 struct read_options_state cs;
9faa7a88
RK
85
86 if(!(cs.name = mx_find(name)))
87 return;
88 if(!(fp = fopen(cs.name, "r")))
89 fatal(errno, "error opening %s", cs.name);
90 cs.line = 0;
91 while(!inputline(cs.name, fp, &buffer, '\n')) {
92 ++cs.line;
93 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
94 option__split_error, &cs)))
95 continue;
96 if(!n)
97 continue;
98 if((i = TABLE_FIND(options, struct option, name, vec[0])) == -1) {
99 error(0, "%s:%d: unknown option '%s'", cs.name, cs.line, vec[0]);
100 continue;
101 }
102 ++vec;
103 --n;
104 if(n < options[i].minargs) {
105 error(0, "%s:%d: too few arguments to '%s'", cs.name, cs.line, vec[-1]);
106 continue;
107 }
108 if(n > options[i].maxargs) {
109 error(0, "%s:%d: too many arguments to '%s'", cs.name, cs.line, vec[-1]);
110 continue;
111 }
112 options[i].handler(n, vec);
113 }
114 fclose(fp);
115}
116
117static void option__init(void) {
118 static int have_read_options;
119
120 if(!have_read_options) {
121 have_read_options = 1;
122 labels = hash_new(sizeof (char *));
123 columns = hash_new(sizeof (struct column));
124 option__readfile("options");
125 }
126}
127
128/** @brief Set an option
129 * @param name Option name
130 * @param value Option value
131 *
132 * If the option was already set its value is replaced.
133 *
134 * @p name and @p value are copied.
135 */
136void option_set(const char *name, const char *value) {
137 char *v = xstrdup(value);
138
139 option__init();
140 hash_add(labels, name, &v, HASH_INSERT_OR_REPLACE);
141}
142
143/** @brief Get a label
144 * @param key Name of label
145 * @return Value of label (never NULL)
146 *
147 * If label images.X isn't found then the return value is
148 * <url.static>X.png, allowing url.static to be used to provide a base
149 * for all images with per-image overrides.
150 *
151 * Otherwise undefined labels expand to their last (dot-separated)
152 * component.
153 */
154const char *option_label(const char *key) {
155 const char *label;
156
157 option__init();
158 if(!(label = *(char **)hash_find(labels, key))) {
159 /* No label found */
160 if(!strncmp(key, "images.", 7)) {
161 static const char *url_static;
162 /* images.X defaults to <url.static>X.png */
163
164 if(!url_static)
165 url_static = option_label("url.static");
166 byte_xasprintf((char **)&label, "%s%s.png", url_static, key + 7);
167 } else if((label = strrchr(key, '.')))
168 /* X.Y defaults to Y */
169 ++label;
170 else
171 /* otherwise default to label name */
172 label = key;
173 }
174 return label;
175}
176
177/** @brief Test whether a label exists
178 * @param key Name of label
179 * @return 1 if label exists, otherwise 0
180 *
181 * Labels that don't exist still have an expansion (per option_label()
182 * documentation), and possibly not even a placeholder one.
183 */
184int option_label_exists(const char *key) {
185 option__init();
186 return !!hash_find(labels, key);
187}
188
189/** @brief Return a column list
190 * @param name Context (playing/recent/etc)
191 * @param ncolumns Where to store column count or NULL
192 * @return Pointer to column list
193 */
194char **option_columns(const char *name, int *ncolumns) {
195 struct column *c;
196
197 option__init();
198 c = hash_find(columns, name);
199 if(c) {
200 if(ncolumns)
201 *ncolumns = c->ncolumns;
202 return c->columns;
203 } else {
204 if(ncolumns)
205 *ncolumns = 0;
206 return 0;
207 }
208}
209
210/*
211Local Variables:
212c-basic-offset:2
213comment-column:40
214End:
215*/