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