| 1 | /* |
| 2 | * Copyright (c) 2015 Moritz Grimm <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | # include "config.h" |
| 19 | #endif /* HAVE_CONFIG_H */ |
| 20 | |
| 21 | #include <sys/queue.h> |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "cfg_private.h" |
| 26 | #include "cfg_decoder.h" |
| 27 | #include "log.h" |
| 28 | #include "xalloc.h" |
| 29 | |
| 30 | struct file_ext { |
| 31 | TAILQ_ENTRY(file_ext) entry; |
| 32 | char *ext; |
| 33 | }; |
| 34 | TAILQ_HEAD(file_ext_list, file_ext); |
| 35 | |
| 36 | struct cfg_decoder { |
| 37 | TAILQ_ENTRY(cfg_decoder) entry; |
| 38 | char *name; |
| 39 | char *program; |
| 40 | struct file_ext_list exts; |
| 41 | }; |
| 42 | |
| 43 | TAILQ_HEAD(cfg_decoder_list, cfg_decoder); |
| 44 | |
| 45 | struct cfg_decoder_list * |
| 46 | cfg_decoder_list_create(void) |
| 47 | { |
| 48 | struct cfg_decoder_list *dl; |
| 49 | |
| 50 | dl = xcalloc(1UL, sizeof(*dl)); |
| 51 | TAILQ_INIT(dl); |
| 52 | |
| 53 | return (dl); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | cfg_decoder_list_destroy(struct cfg_decoder_list **dl_p) |
| 58 | { |
| 59 | struct cfg_decoder_list *dl = *dl_p; |
| 60 | struct cfg_decoder *d; |
| 61 | |
| 62 | if (!dl) |
| 63 | return; |
| 64 | |
| 65 | while (NULL != (d = TAILQ_FIRST(dl))) |
| 66 | cfg_decoder_list_remove(dl, &d); |
| 67 | |
| 68 | xfree(dl); |
| 69 | *dl_p = NULL; |
| 70 | } |
| 71 | |
| 72 | unsigned int |
| 73 | cfg_decoder_list_nentries(struct cfg_decoder_list *dl) |
| 74 | { |
| 75 | struct cfg_decoder *d; |
| 76 | unsigned int n = 0; |
| 77 | |
| 78 | TAILQ_FOREACH(d, dl, entry) { |
| 79 | n++; |
| 80 | } |
| 81 | |
| 82 | return (n); |
| 83 | } |
| 84 | |
| 85 | struct cfg_decoder * |
| 86 | cfg_decoder_list_find(struct cfg_decoder_list *dl, const char *name) |
| 87 | { |
| 88 | struct cfg_decoder *d; |
| 89 | |
| 90 | TAILQ_FOREACH(d, dl, entry) { |
| 91 | if (0 == strcasecmp(d->name, name)) |
| 92 | return (d); |
| 93 | } |
| 94 | |
| 95 | return (NULL); |
| 96 | } |
| 97 | |
| 98 | struct cfg_decoder * |
| 99 | cfg_decoder_list_findext(struct cfg_decoder_list *dl, const char *ext) |
| 100 | { |
| 101 | struct cfg_decoder *d; |
| 102 | |
| 103 | TAILQ_FOREACH(d, dl, entry) { |
| 104 | if (cfg_decoder_extsupport(d, ext)) |
| 105 | return (d); |
| 106 | } |
| 107 | |
| 108 | return (NULL); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | struct cfg_decoder * |
| 113 | cfg_decoder_list_get(struct cfg_decoder_list *dl, const char *name) |
| 114 | { |
| 115 | struct cfg_decoder *d; |
| 116 | |
| 117 | d = cfg_decoder_list_find(dl, name); |
| 118 | if (d) |
| 119 | return (d); |
| 120 | d = cfg_decoder_create(name); |
| 121 | if (!d) |
| 122 | return (NULL); |
| 123 | |
| 124 | TAILQ_INSERT_TAIL(dl, d, entry); |
| 125 | |
| 126 | return (d); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | cfg_decoder_list_remove(struct cfg_decoder_list *dl, struct cfg_decoder **d_p) |
| 131 | { |
| 132 | TAILQ_REMOVE(dl, *d_p, entry); |
| 133 | cfg_decoder_destroy(d_p); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | cfg_decoder_list_foreach(struct cfg_decoder_list *dl, |
| 138 | void (*cb)(cfg_decoder_t, void *), void *cb_arg) |
| 139 | { |
| 140 | struct cfg_decoder *d; |
| 141 | |
| 142 | TAILQ_FOREACH(d, dl, entry) { |
| 143 | cb(d, cb_arg); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | struct cfg_decoder * |
| 148 | cfg_decoder_create(const char *name) |
| 149 | { |
| 150 | struct cfg_decoder *d; |
| 151 | |
| 152 | if (!name || !name[0]) |
| 153 | return (NULL); |
| 154 | |
| 155 | d = xcalloc(1UL, sizeof(*d)); |
| 156 | d->name = xstrdup(name); |
| 157 | TAILQ_INIT(&d->exts); |
| 158 | |
| 159 | return (d); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | cfg_decoder_destroy(struct cfg_decoder **d_p) |
| 164 | { |
| 165 | struct cfg_decoder *d = *d_p; |
| 166 | struct file_ext *e; |
| 167 | |
| 168 | xfree(d->name); |
| 169 | xfree(d->program); |
| 170 | while (NULL != (e = TAILQ_FIRST(&d->exts))) { |
| 171 | TAILQ_REMOVE(&d->exts, e, entry); |
| 172 | xfree(e->ext); |
| 173 | xfree(e); |
| 174 | } |
| 175 | xfree(d); |
| 176 | *d_p = NULL; |
| 177 | } |
| 178 | |
| 179 | int |
| 180 | cfg_decoder_set_name(struct cfg_decoder *d, struct cfg_decoder_list *dl, |
| 181 | const char *name, const char **errstrp) |
| 182 | { |
| 183 | struct cfg_decoder *d2; |
| 184 | |
| 185 | if (!name || !name[0]) { |
| 186 | if (errstrp) |
| 187 | *errstrp = "empty"; |
| 188 | return (-1); |
| 189 | } |
| 190 | |
| 191 | d2 = cfg_decoder_list_find(dl, name); |
| 192 | if (d2 && d2 != d) { |
| 193 | if (errstrp) |
| 194 | *errstrp = "already exists"; |
| 195 | return (-1); |
| 196 | } |
| 197 | |
| 198 | SET_XSTRDUP(d->name, name, errstrp); |
| 199 | return (0); |
| 200 | } |
| 201 | |
| 202 | int |
| 203 | cfg_decoder_set_program(struct cfg_decoder *d, |
| 204 | struct cfg_decoder_list *not_used, const char *program, |
| 205 | const char **errstrp) |
| 206 | { |
| 207 | (void)not_used; |
| 208 | |
| 209 | if (!program || !program[0]) { |
| 210 | if (errstrp) |
| 211 | *errstrp = "empty"; |
| 212 | return (-1); |
| 213 | } |
| 214 | |
| 215 | xfree(d->program); |
| 216 | d->program = xstrdup(program); |
| 217 | |
| 218 | return (0); |
| 219 | } |
| 220 | |
| 221 | int |
| 222 | cfg_decoder_add_match(struct cfg_decoder *d, struct cfg_decoder_list *dl, |
| 223 | const char *ext, const char **errstrp) |
| 224 | { |
| 225 | struct cfg_decoder *d2; |
| 226 | struct file_ext *e, *e2; |
| 227 | |
| 228 | if (!ext || !ext[0]) { |
| 229 | if (errstrp) |
| 230 | *errstrp = "empty"; |
| 231 | return (-1); |
| 232 | } |
| 233 | |
| 234 | d2 = cfg_decoder_list_findext(dl, ext); |
| 235 | e = NULL; |
| 236 | if (d2) { |
| 237 | e2 = TAILQ_FIRST(&d2->exts); |
| 238 | while (e2) { |
| 239 | if (0 == strcasecmp(e2->ext, ext)) { |
| 240 | log_notice("%s: relocating match from %s to %s", |
| 241 | ext, d2->name, d->name); |
| 242 | TAILQ_REMOVE(&d2->exts, e2, entry); |
| 243 | e = e2; |
| 244 | break; |
| 245 | } |
| 246 | e2 = TAILQ_NEXT(e2, entry); |
| 247 | } |
| 248 | } |
| 249 | if (!e) { |
| 250 | e = xcalloc(1UL, sizeof(*e)); |
| 251 | e->ext = xstrdup(ext); |
| 252 | } |
| 253 | TAILQ_INSERT_TAIL(&d->exts, e, entry); |
| 254 | |
| 255 | return (0); |
| 256 | } |
| 257 | |
| 258 | int |
| 259 | cfg_decoder_validate(struct cfg_decoder *d, const char **errstrp) |
| 260 | { |
| 261 | struct file_ext *e; |
| 262 | unsigned int num_exts; |
| 263 | |
| 264 | if (!d->program) { |
| 265 | if (errstrp) |
| 266 | *errstrp = "program not set"; |
| 267 | return (-1); |
| 268 | } |
| 269 | |
| 270 | num_exts = 0; |
| 271 | TAILQ_FOREACH(e, &d->exts, entry) { |
| 272 | num_exts++; |
| 273 | } |
| 274 | if (!num_exts) { |
| 275 | if (errstrp) |
| 276 | *errstrp = "no file extensions registered"; |
| 277 | return (-1); |
| 278 | } |
| 279 | |
| 280 | CHECKPH_PROHIBITED(d->program, PLACEHOLDER_STRING); |
| 281 | CHECKPH_DUPLICATE(d->program, PLACEHOLDER_TRACK); |
| 282 | CHECKPH_DUPLICATE(d->program, PLACEHOLDER_METADATA); |
| 283 | CHECKPH_DUPLICATE(d->program, PLACEHOLDER_ARTIST); |
| 284 | CHECKPH_DUPLICATE(d->program, PLACEHOLDER_TITLE); |
| 285 | CHECKPH_REQUIRED(d->program, PLACEHOLDER_TRACK); |
| 286 | |
| 287 | return (0); |
| 288 | } |
| 289 | |
| 290 | int |
| 291 | cfg_decoder_extsupport(struct cfg_decoder *d, const char *ext) |
| 292 | { |
| 293 | struct file_ext *e; |
| 294 | |
| 295 | TAILQ_FOREACH(e, &d->exts, entry) { |
| 296 | if (0 == strcasecmp(e->ext, ext)) |
| 297 | return (1); |
| 298 | } |
| 299 | |
| 300 | return (0); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | cfg_decoder_ext_foreach(struct cfg_decoder *d, |
| 305 | void (*cb)(const char *, void *), void *cb_arg) |
| 306 | { |
| 307 | struct file_ext *e; |
| 308 | |
| 309 | TAILQ_FOREACH(e, &d->exts, entry) { |
| 310 | cb(e->ext, cb_arg); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | const char * |
| 315 | cfg_decoder_get_name(struct cfg_decoder *d) |
| 316 | { |
| 317 | return (d->name); |
| 318 | } |
| 319 | |
| 320 | const char * |
| 321 | cfg_decoder_get_program(struct cfg_decoder *d) |
| 322 | { |
| 323 | return (d->program); |
| 324 | } |