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