Commit | Line | Data |
---|---|---|
9faa7a88 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
7800abb3 | 3 | * Copyright (C) 2004-2008, 2011 Richard Kettlewell |
9faa7a88 | 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 | 26 | |
2c6ee627 | 27 | /** @brief State for parsing an options file */ |
71634563 | 28 | struct read_options_state { |
2c6ee627 | 29 | /** @brief Filename */ |
71634563 | 30 | const char *name; |
2c6ee627 RK |
31 | |
32 | /** @brief Line number */ | |
71634563 RK |
33 | int line; |
34 | }; | |
35 | ||
9faa7a88 | 36 | static hash *labels; |
9faa7a88 | 37 | |
71634563 RK |
38 | static void option__readfile(const char *name); |
39 | ||
9faa7a88 RK |
40 | static void option__label(int attribute((unused)) nvec, |
41 | char **vec) { | |
42 | option_set(vec[0], vec[1]); | |
43 | } | |
44 | ||
45 | static void option__include(int attribute((unused)) nvec, | |
46 | char **vec) { | |
47 | option__readfile(vec[0]); | |
48 | } | |
49 | ||
598b07b7 | 50 | /** @brief Definition of an option command */ |
9faa7a88 | 51 | static struct option { |
598b07b7 | 52 | /** @brief Command name */ |
9faa7a88 | 53 | const char *name; |
598b07b7 RK |
54 | /** @brief Minimum number of arguments */ |
55 | int minargs; | |
56 | /** @brief Maximum number of arguments */ | |
57 | int maxargs; | |
58 | /** @brief Command handler */ | |
9faa7a88 RK |
59 | void (*handler)(int nvec, char **vec); |
60 | } options[] = { | |
71634563 RK |
61 | { "include", 1, 1, option__include }, |
62 | { "label", 2, 2, option__label }, | |
9faa7a88 RK |
63 | }; |
64 | ||
65 | static void option__split_error(const char *msg, | |
66 | void *u) { | |
67 | struct read_options_state *cs = u; | |
68 | ||
2e9ba080 | 69 | disorder_error(0, "%s:%d: %s", cs->name, cs->line, msg); |
9faa7a88 RK |
70 | } |
71 | ||
72 | static void option__readfile(const char *name) { | |
73 | int n, i; | |
9faa7a88 RK |
74 | FILE *fp; |
75 | char **vec, *buffer; | |
76 | struct read_options_state cs; | |
9faa7a88 | 77 | |
f2d306b4 | 78 | if(!(cs.name = mx_find(name, 1/*report*/))) |
9faa7a88 RK |
79 | return; |
80 | if(!(fp = fopen(cs.name, "r"))) | |
2e9ba080 | 81 | disorder_fatal(errno, "error opening %s", cs.name); |
9faa7a88 RK |
82 | cs.line = 0; |
83 | while(!inputline(cs.name, fp, &buffer, '\n')) { | |
84 | ++cs.line; | |
85 | if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES, | |
86 | option__split_error, &cs))) | |
87 | continue; | |
88 | if(!n) | |
89 | continue; | |
ba937f01 | 90 | if((i = TABLE_FIND(options, name, vec[0])) == -1) { |
2e9ba080 | 91 | disorder_error(0, "%s:%d: unknown option '%s'", cs.name, cs.line, vec[0]); |
9faa7a88 RK |
92 | continue; |
93 | } | |
94 | ++vec; | |
95 | --n; | |
96 | if(n < options[i].minargs) { | |
2e9ba080 | 97 | disorder_error(0, "%s:%d: too few arguments to '%s'", cs.name, cs.line, vec[-1]); |
9faa7a88 RK |
98 | continue; |
99 | } | |
100 | if(n > options[i].maxargs) { | |
2e9ba080 | 101 | disorder_error(0, "%s:%d: too many arguments to '%s'", cs.name, cs.line, vec[-1]); |
9faa7a88 RK |
102 | continue; |
103 | } | |
104 | options[i].handler(n, vec); | |
105 | } | |
106 | fclose(fp); | |
107 | } | |
108 | ||
109 | static void option__init(void) { | |
110 | static int have_read_options; | |
111 | ||
112 | if(!have_read_options) { | |
113 | have_read_options = 1; | |
114 | labels = hash_new(sizeof (char *)); | |
9faa7a88 RK |
115 | option__readfile("options"); |
116 | } | |
117 | } | |
118 | ||
119 | /** @brief Set an option | |
120 | * @param name Option name | |
121 | * @param value Option value | |
122 | * | |
123 | * If the option was already set its value is replaced. | |
124 | * | |
125 | * @p name and @p value are copied. | |
126 | */ | |
127 | void option_set(const char *name, const char *value) { | |
128 | char *v = xstrdup(value); | |
129 | ||
130 | option__init(); | |
131 | hash_add(labels, name, &v, HASH_INSERT_OR_REPLACE); | |
132 | } | |
133 | ||
134 | /** @brief Get a label | |
135 | * @param key Name of label | |
136 | * @return Value of label (never NULL) | |
137 | * | |
138 | * If label images.X isn't found then the return value is | |
139 | * <url.static>X.png, allowing url.static to be used to provide a base | |
140 | * for all images with per-image overrides. | |
141 | * | |
142 | * Otherwise undefined labels expand to their last (dot-separated) | |
143 | * component. | |
144 | */ | |
145 | const char *option_label(const char *key) { | |
146 | const char *label; | |
99955407 | 147 | char **lptr; |
9faa7a88 RK |
148 | |
149 | option__init(); | |
99955407 RK |
150 | lptr = hash_find(labels, key); |
151 | if(lptr) | |
152 | return *lptr; | |
153 | /* No label found */ | |
154 | if(!strncmp(key, "images.", 7)) { | |
155 | static const char *url_static; | |
156 | /* images.X defaults to <url.static>X.png */ | |
157 | ||
158 | if(!url_static) | |
159 | url_static = option_label("url.static"); | |
160 | byte_xasprintf((char **)&label, "%s%s.png", url_static, key + 7); | |
161 | } else if((label = strrchr(key, '.'))) | |
162 | /* X.Y defaults to Y */ | |
163 | ++label; | |
164 | else | |
165 | /* otherwise default to label name */ | |
166 | label = key; | |
9faa7a88 RK |
167 | return label; |
168 | } | |
169 | ||
170 | /** @brief Test whether a label exists | |
171 | * @param key Name of label | |
172 | * @return 1 if label exists, otherwise 0 | |
173 | * | |
174 | * Labels that don't exist still have an expansion (per option_label() | |
175 | * documentation), and possibly not even a placeholder one. | |
176 | */ | |
177 | int option_label_exists(const char *key) { | |
178 | option__init(); | |
179 | return !!hash_find(labels, key); | |
180 | } | |
181 | ||
9faa7a88 RK |
182 | /* |
183 | Local Variables: | |
184 | c-basic-offset:2 | |
185 | comment-column:40 | |
186 | End: | |
187 | */ |