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