chiark / gitweb /
banish fd passing. currently a bit ugly but seems to work
[disorder] / server / dcgi.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
78efa64e 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 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
21#include <config.h>
22#include "types.h"
23
24#include <stdio.h>
25#include <errno.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <stddef.h>
29#include <stdlib.h>
30#include <time.h>
31#include <unistd.h>
32#include <string.h>
33#include <sys/wait.h>
34#include <pcre.h>
35#include <assert.h>
36
37#include "client.h"
38#include "mem.h"
39#include "vector.h"
40#include "sink.h"
41#include "cgi.h"
42#include "dcgi.h"
43#include "log.h"
44#include "configuration.h"
45#include "table.h"
46#include "queue.h"
47#include "plugin.h"
48#include "split.h"
49#include "words.h"
50#include "wstat.h"
51#include "kvp.h"
52#include "syscalls.h"
53#include "printf.h"
54#include "regsub.h"
55#include "defs.h"
56#include "trackname.h"
57
58static void expand(cgi_sink *output,
59 const char *template,
60 dcgi_state *ds);
61static void expandstring(cgi_sink *output,
62 const char *string,
63 dcgi_state *ds);
64
65struct entry {
66 const char *path;
67 const char *sort;
68 const char *display;
69};
70
71static const char *nonce(void) {
72 static unsigned long count;
73 char *s;
74
75 byte_xasprintf(&s, "%lx%lx%lx",
76 (unsigned long)time(0),
77 (unsigned long)getpid(),
78 count++);
79 return s;
80}
81
82static int compare_entry(const void *a, const void *b) {
83 const struct entry *ea = a, *eb = b;
84
85 return compare_tracks(ea->sort, eb->sort,
86 ea->display, eb->display,
87 ea->path, eb->path);
88}
89
90static const char *front_url(void) {
91 char *url;
92 const char *mgmt;
93
94 /* preserve management interface visibility */
95 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
96 byte_xasprintf(&url, "%s?mgmt=true", config->url);
97 return url;
98 }
99 return config->url;
100}
101
102static void redirect(struct sink *output) {
103 const char *back;
104
105 cgi_header(output, "Location",
106 (back = cgi_get("back")) ? back : front_url());
107 cgi_body(output);
108}
109
110static void lookups(dcgi_state *ds, unsigned want) {
111 unsigned need;
112 struct queue_entry *r, *rnext;
113 const char *dir, *re;
114
115 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
116 if(need & DC_QUEUE)
117 disorder_queue(ds->g->client, &ds->g->queue);
118 if(need & DC_PLAYING)
119 disorder_playing(ds->g->client, &ds->g->playing);
78efa64e
RK
120 if(need & DC_NEW)
121 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
460b9539 122 if(need & DC_RECENT) {
123 /* we need to reverse the order of the list */
124 disorder_recent(ds->g->client, &r);
125 while(r) {
126 rnext = r->next;
127 r->next = ds->g->recent;
128 ds->g->recent = r;
129 r = rnext;
130 }
131 }
132 if(need & DC_VOLUME)
133 disorder_get_volume(ds->g->client,
134 &ds->g->volume_left, &ds->g->volume_right);
135 if(need & (DC_FILES|DC_DIRS)) {
136 if(!(dir = cgi_get("directory")))
137 dir = "";
138 re = cgi_get("regexp");
139 if(need & DC_DIRS)
140 if(disorder_directories(ds->g->client, dir, re,
141 &ds->g->dirs, &ds->g->ndirs))
142 ds->g->ndirs = 0;
143 if(need & DC_FILES)
144 if(disorder_files(ds->g->client, dir, re,
145 &ds->g->files, &ds->g->nfiles))
146 ds->g->nfiles = 0;
147 }
148 ds->g->flags |= need;
149 }
150}
151
152/* actions ********************************************************************/
153
154static void act_disable(cgi_sink *output,
155 dcgi_state *ds) {
156 if(ds->g->client)
157 disorder_disable(ds->g->client);
158 redirect(output->sink);
159}
160
161static void act_enable(cgi_sink *output,
162 dcgi_state *ds) {
163 if(ds->g->client)
164 disorder_enable(ds->g->client);
165 redirect(output->sink);
166}
167
168static void act_random_disable(cgi_sink *output,
169 dcgi_state *ds) {
170 if(ds->g->client)
171 disorder_random_disable(ds->g->client);
172 redirect(output->sink);
173}
174
175static void act_random_enable(cgi_sink *output,
176 dcgi_state *ds) {
177 if(ds->g->client)
178 disorder_random_enable(ds->g->client);
179 redirect(output->sink);
180}
181
182static void act_remove(cgi_sink *output,
183 dcgi_state *ds) {
184 const char *id;
185
186 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
187 if(ds->g->client)
188 disorder_remove(ds->g->client, id);
189 redirect(output->sink);
190}
191
192static void act_move(cgi_sink *output,
193 dcgi_state *ds) {
194 const char *id, *delta;
195
196 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
197 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
198 if(ds->g->client)
199 disorder_move(ds->g->client, id, atoi(delta));
200 redirect(output->sink);
201}
202
203static void act_scratch(cgi_sink *output,
204 dcgi_state *ds) {
205 if(ds->g->client)
206 disorder_scratch(ds->g->client, cgi_get("id"));
207 redirect(output->sink);
208}
209
210static void act_playing(cgi_sink *output, dcgi_state *ds) {
211 char r[1024];
212 long refresh = config->refresh, length;
213 time_t now, fin;
214 int random_enabled = 0;
215 int enabled = 0;
216
217 lookups(ds, DC_PLAYING|DC_QUEUE);
218 cgi_header(output->sink, "Content-Type", "text/html");
219 disorder_random_enabled(ds->g->client, &random_enabled);
220 disorder_enabled(ds->g->client, &enabled);
221 if(ds->g->playing
222 && ds->g->playing->state == playing_started /* i.e. not paused */
223 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
224 && length
225 && ds->g->playing->sofar >= 0) {
226 /* Try to put the next refresh at the start of the next track. */
227 time(&now);
228 fin = now + length - ds->g->playing->sofar + config->gap;
229 if(now + refresh > fin)
230 refresh = fin - now;
231 }
232 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
233 /* next track is a scratch, don't leave more than the inter-track gap */
234 if(refresh > config->gap)
235 refresh = config->gap;
236 }
237 if(!ds->g->playing && ((ds->g->queue
238 && ds->g->queue->state != playing_random)
239 || random_enabled) && enabled) {
240 /* no track playing but playing is enabled and there is something coming
241 * up, must be in a gap */
242 if(refresh > config->gap)
243 refresh = config->gap;
244 }
245 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
246 front_url());
247 cgi_header(output->sink, "Refresh", r);
248 cgi_body(output->sink);
249 expand(output, "playing", ds);
250}
251
252static void act_play(cgi_sink *output,
253 dcgi_state *ds) {
254 const char *track, *dir;
255 char **tracks;
256 int ntracks, n;
257 struct entry *e;
258
259 if((track = cgi_get("file"))) {
260 disorder_play(ds->g->client, track);
261 } else if((dir = cgi_get("directory"))) {
262 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
263 if(ntracks) {
264 e = xmalloc(ntracks * sizeof (struct entry));
265 for(n = 0; n < ntracks; ++n) {
266 e[n].path = tracks[n];
267 e[n].sort = trackname_transform("track", tracks[n], "sort");
268 e[n].display = trackname_transform("track", tracks[n], "display");
269 }
270 qsort(e, ntracks, sizeof (struct entry), compare_entry);
271 for(n = 0; n < ntracks; ++n)
272 disorder_play(ds->g->client, e[n].path);
273 }
274 }
275 /* XXX error handling */
276 redirect(output->sink);
277}
278
279static int clamp(int n, int min, int max) {
280 if(n < min)
281 return min;
282 if(n > max)
283 return max;
284 return n;
285}
286
287static const char *volume_url(void) {
288 char *url;
289
290 byte_xasprintf(&url, "%s?action=volume", config->url);
291 return url;
292}
293
294static void act_volume(cgi_sink *output, dcgi_state *ds) {
295 const char *l, *r, *d, *back;
296 int nd, changed = 0;;
297
298 if((d = cgi_get("delta"))) {
299 lookups(ds, DC_VOLUME);
300 nd = clamp(atoi(d), -255, 255);
301 disorder_set_volume(ds->g->client,
302 clamp(ds->g->volume_left + nd, 0, 255),
303 clamp(ds->g->volume_right + nd, 0, 255));
304 changed = 1;
305 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
306 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
307 changed = 1;
308 }
309 if(changed) {
310 /* redirect back to ourselves (but without the volume-changing bits in the
311 * URL) */
312 cgi_header(output->sink, "Location",
313 (back = cgi_get("back")) ? back : volume_url());
314 cgi_body(output->sink);
315 } else {
316 cgi_header(output->sink, "Content-Type", "text/html");
317 cgi_body(output->sink);
318 expand(output, "volume", ds);
319 }
320}
321
322static void act_prefs_errors(const char *msg,
323 void attribute((unused)) *u) {
324 fatal(0, "error splitting parts list: %s", msg);
325}
326
327static const char *numbered_arg(const char *argname, int numfile) {
328 char *fullname;
329
330 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
331 return cgi_get(fullname);
332}
333
334static void process_prefs(dcgi_state *ds, int numfile) {
335 const char *file, *name, *value, *part, *parts, *current, *context;
336 char **partslist;
337
338 if(!(file = numbered_arg("file", numfile)))
339 /* The first file doesn't need numbering. */
340 if(numfile > 0 || !(file = cgi_get("file")))
341 return;
342 if((parts = numbered_arg("parts", numfile))
343 || (parts = cgi_get("parts"))) {
344 /* Default context is display. Other contexts not actually tested. */
345 if(!(context = numbered_arg("context", numfile))) context = "display";
346 partslist = split(parts, 0, 0, act_prefs_errors, 0);
347 while((part = *partslist++)) {
348 if(!(value = numbered_arg(part, numfile)))
349 continue;
350 /* If it's already right (whether regexps or db) don't change anything,
351 * so we don't fill the database up with rubbish. */
352 if(disorder_part(ds->g->client, (char **)&current,
353 file, context, part))
354 fatal(0, "disorder_part() failed");
355 if(!strcmp(current, value))
356 continue;
357 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
358 disorder_set(ds->g->client, file, name, value);
359 }
360 if((value = numbered_arg("random", numfile)))
361 disorder_unset(ds->g->client, file, "pick_at_random");
362 else
363 disorder_set(ds->g->client, file, "pick_at_random", "0");
364 if((value = numbered_arg("tags", numfile)))
365 disorder_set(ds->g->client, file, "tags", value);
366 } else if((name = cgi_get("name"))) {
367 /* Raw preferences. Not well supported in the templates at the moment. */
368 value = cgi_get("value");
369 if(value)
370 disorder_set(ds->g->client, file, name, value);
371 else
372 disorder_unset(ds->g->client, file, name);
373 }
374}
375
376static void act_prefs(cgi_sink *output, dcgi_state *ds) {
377 const char *files;
378 int nfiles, numfile;
379
380 if((files = cgi_get("files"))) nfiles = atoi(files);
381 else nfiles = 1;
382 for(numfile = 0; numfile < nfiles; ++numfile)
383 process_prefs(ds, numfile);
384 cgi_header(output->sink, "Content-Type", "text/html");
385 cgi_body(output->sink);
386 expand(output, "prefs", ds);
387}
388
389static void act_pause(cgi_sink *output,
390 dcgi_state *ds) {
391 if(ds->g->client)
392 disorder_pause(ds->g->client);
393 redirect(output->sink);
394}
395
396static void act_resume(cgi_sink *output,
397 dcgi_state *ds) {
398 if(ds->g->client)
399 disorder_resume(ds->g->client);
400 redirect(output->sink);
401}
402
403static const struct action {
404 const char *name;
405 void (*handler)(cgi_sink *output, dcgi_state *ds);
406} actions[] = {
407 { "disable", act_disable },
408 { "enable", act_enable },
409 { "move", act_move },
410 { "pause", act_pause },
411 { "play", act_play },
412 { "playing", act_playing },
413 { "prefs", act_prefs },
414 { "random-disable", act_random_disable },
415 { "random-enable", act_random_enable },
416 { "remove", act_remove },
417 { "resume", act_resume },
418 { "scratch", act_scratch },
419 { "volume", act_volume },
420};
421
422/* expansions *****************************************************************/
423
424static void exp_include(int attribute((unused)) nargs,
425 char **args,
426 cgi_sink *output,
427 void *u) {
428 expand(output, args[0], u);
429}
430
431static void exp_server_version(int attribute((unused)) nargs,
432 char attribute((unused)) **args,
433 cgi_sink *output,
434 void *u) {
435 dcgi_state *ds = u;
436 const char *v;
437
438 if(ds->g->client) {
439 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
440 } else
441 v = "(server not running)";
442 cgi_output(output, "%s", v);
443}
444
445static void exp_version(int attribute((unused)) nargs,
446 char attribute((unused)) **args,
447 cgi_sink *output,
448 void attribute((unused)) *u) {
449 cgi_output(output, "%s", disorder_version_string);
450}
451
452static void exp_nonce(int attribute((unused)) nargs,
453 char attribute((unused)) **args,
454 cgi_sink *output,
455 void attribute((unused)) *u) {
456 cgi_output(output, "%s", nonce());
457}
458
459static void exp_label(int attribute((unused)) nargs,
460 char **args,
461 cgi_sink *output,
462 void attribute((unused)) *u) {
463 cgi_output(output, "%s", cgi_label(args[0]));
464}
465
466struct trackinfo_state {
467 dcgi_state *ds;
468 const struct queue_entry *q;
469 long length;
470 time_t when;
471};
472
473static void exp_who(int attribute((unused)) nargs,
474 char attribute((unused)) **args,
475 cgi_sink *output,
476 void *u) {
477 dcgi_state *ds = u;
478
479 if(ds->track && ds->track->submitter)
480 cgi_output(output, "%s", ds->track->submitter);
481}
482
483static void exp_length(int attribute((unused)) nargs,
484 char attribute((unused)) **args,
485 cgi_sink *output,
486 void *u) {
487 dcgi_state *ds = u;
78efa64e 488 long length = 0;
460b9539 489
490 if(ds->track
491 && (ds->track->state == playing_started
492 || ds->track->state == playing_paused)
493 && ds->track->sofar >= 0)
494 cgi_output(output, "%ld:%02ld/",
495 ds->track->sofar / 60, ds->track->sofar % 60);
78efa64e
RK
496 length = 0;
497 if(ds->track)
498 disorder_length(ds->g->client, ds->track->track, &length);
499 else if(ds->tracks)
500 disorder_length(ds->g->client, ds->tracks[0], &length);
460b9539 501 if(length)
502 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
503 else
504 sink_printf(output->sink, "%s", "&nbsp;");
505}
506
507static void exp_when(int attribute((unused)) nargs,
508 char attribute((unused)) **args,
509 cgi_sink *output,
510 void *u) {
511 dcgi_state *ds = u;
512 const struct tm *w = 0;
513
514 if(ds->track)
515 switch(ds->track->state) {
516 case playing_isscratch:
517 case playing_unplayed:
518 case playing_random:
519 if(ds->track->expected)
520 w = localtime(&ds->track->expected);
521 break;
522 case playing_failed:
523 case playing_no_player:
524 case playing_ok:
525 case playing_scratched:
526 case playing_started:
527 case playing_paused:
528 case playing_quitting:
529 if(ds->track->played)
530 w = localtime(&ds->track->played);
531 break;
532 }
533 if(w)
534 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
535 else
536 sink_printf(output->sink, "&nbsp;");
537}
538
539static void exp_part(int nargs,
540 char **args,
541 cgi_sink *output,
542 void *u) {
543 dcgi_state *ds = u;
544 const char *s, *track, *part, *context;
545
546 if(nargs == 3)
547 track = args[2];
548 else {
549 if(ds->track)
550 track = ds->track->track;
551 else if(ds->tracks)
552 track = ds->tracks[0];
553 else
554 track = 0;
555 }
556 if(track) {
557 switch(nargs) {
558 case 1:
559 context = "display";
560 part = args[0];
561 break;
562 case 2:
563 case 3:
564 context = args[0];
565 part = args[1];
566 break;
567 default:
568 abort();
569 }
570 if(disorder_part(ds->g->client, (char **)&s, track, context, part))
571 fatal(0, "disorder_part() failed");
572 cgi_output(output, "%s", s);
573 } else
574 sink_printf(output->sink, "&nbsp;");
575}
576
577static void exp_playing(int attribute((unused)) nargs,
578 char **args,
579 cgi_sink *output,
580 void *u) {
581 dcgi_state *ds = u;
582 dcgi_state s;
583
584 lookups(ds, DC_PLAYING);
585 memset(&s, 0, sizeof s);
586 s.g = ds->g;
587 if(ds->g->playing) {
588 s.track = ds->g->playing;
589 expandstring(output, args[0], &s);
590 }
591}
592
593static void exp_queue(int attribute((unused)) nargs,
594 char **args,
595 cgi_sink *output,
596 void *u) {
597 dcgi_state *ds = u;
598 dcgi_state s;
599 struct queue_entry *q;
600
601 lookups(ds, DC_QUEUE);
602 memset(&s, 0, sizeof s);
603 s.g = ds->g;
604 s.first = 1;
605 for(q = ds->g->queue; q; q = q->next) {
606 s.last = !q->next;
607 s.track = q;
608 expandstring(output, args[0], &s);
609 s.index++;
610 s.first = 0;
611 }
612}
613
614static void exp_recent(int attribute((unused)) nargs,
615 char **args,
616 cgi_sink *output,
617 void *u) {
618 dcgi_state *ds = u;
619 dcgi_state s;
620 struct queue_entry *q;
621
622 lookups(ds, DC_RECENT);
623 memset(&s, 0, sizeof s);
624 s.g = ds->g;
625 s.first = 1;
626 for(q = ds->g->recent; q; q = q->next) {
627 s.last = !q;
628 s.track = q;
629 expandstring(output, args[0], &s);
630 s.index++;
631 s.first = 0;
632 }
633}
634
78efa64e
RK
635static void exp_new(int attribute((unused)) nargs,
636 char **args,
637 cgi_sink *output,
638 void *u) {
639 dcgi_state *ds = u;
640 dcgi_state s;
641
642 lookups(ds, DC_NEW);
643 memset(&s, 0, sizeof s);
644 s.g = ds->g;
645 s.first = 1;
646 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
647 s.last = s.index + 1 < ds->g->nnew;
648 s.tracks = &ds->g->new[s.index];
649 expandstring(output, args[0], &s);
650 s.first = 0;
651 }
652}
653
460b9539 654static void exp_url(int attribute((unused)) nargs,
655 char attribute((unused)) **args,
656 cgi_sink *output,
657 void attribute((unused)) *u) {
658 cgi_output(output, "%s", config->url);
659}
660
661struct result {
662 char *track;
663 const char *sort;
664};
665
666static int compare_result(const void *a, const void *b) {
667 const struct result *ra = a, *rb = b;
668 int c;
669
670 if(!(c = strcmp(ra->sort, rb->sort)))
671 c = strcmp(ra->track, rb->track);
672 return c;
673}
674
675static void exp_search(int nargs,
676 char **args,
677 cgi_sink *output,
678 void *u) {
679 dcgi_state *ds = u, substate;
680 char **tracks;
681 const char *q, *context, *part, *template;
682 int ntracks, n, m;
683 struct result *r;
684
685 switch(nargs) {
686 case 2:
687 part = args[0];
688 context = "sort";
689 template = args[1];
690 break;
691 case 3:
692 part = args[0];
693 context = args[1];
694 template = args[2];
695 break;
696 default:
697 assert(!"should never happen");
698 part = context = template = 0; /* quieten compiler */
699 }
700 if(ds->tracks == 0) {
701 /* we are the top level, let's get some search results */
702 if(!(q = cgi_get("query"))) return; /* no results yet */
703 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
704 if(!ntracks) return;
705 } else {
706 tracks = ds->tracks;
707 ntracks = ds->ntracks;
708 }
709 assert(ntracks != 0);
710 /* sort tracks by the appropriate part */
711 r = xmalloc(ntracks * sizeof *r);
712 for(n = 0; n < ntracks; ++n) {
713 r[n].track = tracks[n];
714 if(disorder_part(ds->g->client, (char **)&r[n].sort,
715 tracks[n], context, part))
716 fatal(0, "disorder_part() failed");
717 }
718 qsort(r, ntracks, sizeof (struct result), compare_result);
719 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
720 * array as we know it's guaranteed to be big enough and isn't going to be
721 * used for anything else any more. */
722 memset(&substate, 0, sizeof substate);
723 substate.g = ds->g;
724 substate.first = 1;
725 n = 0;
726 while(n < ntracks) {
727 substate.tracks = tracks;
728 substate.ntracks = 0;
729 m = n;
730 while(m < ntracks
731 && !strcmp(r[m].sort, r[n].sort))
732 tracks[substate.ntracks++] = r[m++].track;
733 substate.last = (m == ntracks);
734 expandstring(output, template, &substate);
735 substate.index++;
736 substate.first = 0;
737 n = m;
738 }
739 assert(substate.last != 0);
740}
741
742static void exp_arg(int attribute((unused)) nargs,
743 char **args,
744 cgi_sink *output,
745 void attribute((unused)) *u) {
746 const char *v;
747
748 if((v = cgi_get(args[0])))
749 cgi_output(output, "%s", v);
750}
751
752static void exp_stats(int attribute((unused)) nargs,
753 char attribute((unused)) **args,
754 cgi_sink *output,
755 void *u) {
756 dcgi_state *ds = u;
757 char **v;
758
759 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
760 if(!disorder_stats(ds->g->client, &v, 0)) {
761 while(*v)
762 cgi_output(output, "%s\n", *v++);
763 }
764 cgi_closetag(output->sink, "pre");
765}
766
767static void exp_volume(int attribute((unused)) nargs,
768 char **args,
769 cgi_sink *output,
770 void *u) {
771 dcgi_state *ds = u;
772
773 lookups(ds, DC_VOLUME);
774 if(!strcmp(args[0], "left"))
775 cgi_output(output, "%d", ds->g->volume_left);
776 else
777 cgi_output(output, "%d", ds->g->volume_right);
778}
779
780static void exp_shell(int attribute((unused)) nargs,
781 char **args,
782 cgi_sink *output,
783 void attribute((unused)) *u) {
784 int w, p[2], n;
785 char buffer[4096];
786 pid_t pid;
787
788 xpipe(p);
789 if(!(pid = xfork())) {
790 exitfn = _exit;
791 xclose(p[0]);
792 xdup2(p[1], 1);
793 xclose(p[1]);
794 execlp("sh", "sh", "-c", args[0], (char *)0);
795 fatal(errno, "error executing sh");
796 }
797 xclose(p[1]);
798 while((n = read(p[0], buffer, sizeof buffer))) {
799 if(n < 0) {
800 if(errno == EINTR) continue;
801 else fatal(errno, "error reading from pipe");
802 }
803 output->sink->write(output->sink, buffer, n);
804 }
805 xclose(p[0]);
806 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
807 ;
808 if(n < 0) fatal(errno, "error calling waitpid");
809 if(w)
810 error(0, "shell command '%s' %s", args[0], wstat(w));
811}
812
813static inline int str2bool(const char *s) {
814 return !strcmp(s, "true");
815}
816
817static inline const char *bool2str(int n) {
818 return n ? "true" : "false";
819}
820
821static char *expandarg(const char *arg, dcgi_state *ds) {
822 struct dynstr d;
823 cgi_sink output;
824
825 dynstr_init(&d);
826 output.quote = 0;
827 output.sink = sink_dynstr(&d);
828 expandstring(&output, arg, ds);
829 dynstr_terminate(&d);
830 return d.vec;
831}
832
833static void exp_prefs(int attribute((unused)) nargs,
834 char **args,
835 cgi_sink *output,
836 void *u) {
837 dcgi_state *ds = u;
838 dcgi_state substate;
839 struct kvp *k;
840 const char *file = expandarg(args[0], ds);
841
842 memset(&substate, 0, sizeof substate);
843 substate.g = ds->g;
844 substate.first = 1;
845 if(disorder_prefs(ds->g->client, file, &k)) return;
846 while(k) {
847 substate.last = !k->next;
848 substate.pref = k;
849 expandstring(output, args[1], &substate);
850 ++substate.index;
851 k = k->next;
852 substate.first = 0;
853 }
854}
855
856static void exp_pref(int attribute((unused)) nargs,
857 char **args,
858 cgi_sink *output,
859 void *u) {
860 char *value;
861 dcgi_state *ds = u;
862
863 if(!disorder_get(ds->g->client, args[0], args[1], &value))
864 cgi_output(output, "%s", value);
865}
866
867static void exp_if(int nargs,
868 char **args,
869 cgi_sink *output,
870 void *u) {
871 dcgi_state *ds = u;
872 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
873
874 if(n < nargs)
875 expandstring(output, args[n], ds);
876}
877
878static void exp_and(int nargs,
879 char **args,
880 cgi_sink *output,
881 void *u) {
882 dcgi_state *ds = u;
883 int n, result = 1;
884
885 for(n = 0; n < nargs; ++n)
886 if(!str2bool(expandarg(args[n], ds))) {
887 result = 0;
888 break;
889 }
890 sink_printf(output->sink, "%s", bool2str(result));
891}
892
893static void exp_or(int nargs,
894 char **args,
895 cgi_sink *output,
896 void *u) {
897 dcgi_state *ds = u;
898 int n, result = 0;
899
900 for(n = 0; n < nargs; ++n)
901 if(str2bool(expandarg(args[n], ds))) {
902 result = 1;
903 break;
904 }
905 sink_printf(output->sink, "%s", bool2str(result));
906}
907
908static void exp_not(int attribute((unused)) nargs,
909 char **args,
910 cgi_sink *output,
911 void attribute((unused)) *u) {
912 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
913}
914
915static void exp_isplaying(int attribute((unused)) nargs,
916 char attribute((unused)) **args,
917 cgi_sink *output,
918 void *u) {
919 dcgi_state *ds = u;
920
921 lookups(ds, DC_PLAYING);
922 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
923}
924
925static void exp_isqueue(int attribute((unused)) nargs,
926 char attribute((unused)) **args,
927 cgi_sink *output,
928 void *u) {
929 dcgi_state *ds = u;
930
931 lookups(ds, DC_QUEUE);
932 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
933}
934
935static void exp_isrecent(int attribute((unused)) nargs,
936 char attribute((unused)) **args,
937 cgi_sink *output,
938 void *u) {
939 dcgi_state *ds = u;
940
941 lookups(ds, DC_RECENT);
942 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
943}
944
78efa64e
RK
945static void exp_isnew(int attribute((unused)) nargs,
946 char attribute((unused)) **args,
947 cgi_sink *output,
948 void *u) {
949 dcgi_state *ds = u;
950
951 lookups(ds, DC_NEW);
952 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
953}
954
460b9539 955static void exp_id(int attribute((unused)) nargs,
956 char attribute((unused)) **args,
957 cgi_sink *output,
958 void *u) {
959 dcgi_state *ds = u;
960
961 if(ds->track)
962 cgi_output(output, "%s", ds->track->id);
963}
964
965static void exp_track(int attribute((unused)) nargs,
966 char attribute((unused)) **args,
967 cgi_sink *output,
968 void *u) {
969 dcgi_state *ds = u;
970
971 if(ds->track)
972 cgi_output(output, "%s", ds->track->track);
973}
974
975static void exp_parity(int attribute((unused)) nargs,
976 char attribute((unused)) **args,
977 cgi_sink *output,
978 void *u) {
979 dcgi_state *ds = u;
980
981 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
982}
983
984static void exp_comment(int attribute((unused)) nargs,
985 char attribute((unused)) **args,
986 cgi_sink attribute((unused)) *output,
987 void attribute((unused)) *u) {
988 /* do nothing */
989}
990
991static void exp_prefname(int attribute((unused)) nargs,
992 char attribute((unused)) **args,
993 cgi_sink *output,
994 void *u) {
995 dcgi_state *ds = u;
996
997 if(ds->pref && ds->pref->name)
998 cgi_output(output, "%s", ds->pref->name);
999}
1000
1001static void exp_prefvalue(int attribute((unused)) nargs,
1002 char attribute((unused)) **args,
1003 cgi_sink *output,
1004 void *u) {
1005 dcgi_state *ds = u;
1006
1007 if(ds->pref && ds->pref->value)
1008 cgi_output(output, "%s", ds->pref->value);
1009}
1010
1011static void exp_isfiles(int attribute((unused)) nargs,
1012 char attribute((unused)) **args,
1013 cgi_sink *output,
1014 void *u) {
1015 dcgi_state *ds = u;
1016
1017 lookups(ds, DC_FILES);
1018 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1019}
1020
1021static void exp_isdirectories(int attribute((unused)) nargs,
1022 char attribute((unused)) **args,
1023 cgi_sink *output,
1024 void *u) {
1025 dcgi_state *ds = u;
1026
1027 lookups(ds, DC_DIRS);
1028 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1029}
1030
1031static void exp_choose(int attribute((unused)) nargs,
1032 char **args,
1033 cgi_sink *output,
1034 void *u) {
1035 dcgi_state *ds = u;
1036 dcgi_state substate;
1037 int nfiles, n;
1038 char **files;
1039 struct entry *e;
1040 const char *type, *what = expandarg(args[0], ds);
1041
1042 if(!strcmp(what, "files")) {
1043 lookups(ds, DC_FILES);
1044 files = ds->g->files;
1045 nfiles = ds->g->nfiles;
1046 type = "track";
1047 } else if(!strcmp(what, "directories")) {
1048 lookups(ds, DC_DIRS);
1049 files = ds->g->dirs;
1050 nfiles = ds->g->ndirs;
1051 type = "dir";
1052 } else {
1053 error(0, "unknown @choose@ argument '%s'", what);
1054 return;
1055 }
1056 e = xmalloc(nfiles * sizeof (struct entry));
1057 for(n = 0; n < nfiles; ++n) {
1058 e[n].path = files[n];
1059 e[n].sort = trackname_transform(type, files[n], "sort");
1060 e[n].display = trackname_transform(type, files[n], "display");
1061 }
1062 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1063 memset(&substate, 0, sizeof substate);
1064 substate.g = ds->g;
1065 substate.first = 1;
1066 for(n = 0; n < nfiles; ++n) {
1067 substate.last = (n == nfiles - 1);
1068 substate.index = n;
1069 substate.entry = &e[n];
1070 expandstring(output, args[1], &substate);
1071 substate.first = 0;
1072 }
1073}
1074
1075static void exp_file(int attribute((unused)) nargs,
1076 char attribute((unused)) **args,
1077 cgi_sink *output,
1078 void *u) {
1079 dcgi_state *ds = u;
1080
1081 if(ds->entry)
1082 cgi_output(output, "%s", ds->entry->path);
1083 else if(ds->track)
1084 cgi_output(output, "%s", ds->track->track);
1085 else if(ds->tracks)
1086 cgi_output(output, "%s", ds->tracks[0]);
1087}
1088
1089static void exp_transform(int nargs,
1090 char **args,
1091 cgi_sink *output,
1092 void attribute((unused)) *u) {
1093 const char *context = nargs > 2 ? args[2] : "display";
1094
1095 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1096}
1097
1098static void exp_urlquote(int attribute((unused)) nargs,
1099 char **args,
1100 cgi_sink *output,
1101 void attribute((unused)) *u) {
1102 cgi_output(output, "%s", urlencodestring(args[0]));
1103}
1104
1105static void exp_scratchable(int attribute((unused)) nargs,
1106 char attribute((unused)) **args,
1107 cgi_sink *output,
1108 void attribute((unused)) *u) {
1109 dcgi_state *ds = u;
1110 int result;
1111
1112 if(config->restrictions & RESTRICT_SCRATCH) {
1113 lookups(ds, DC_PLAYING);
1114 result = (ds->g->playing
1115 && (!ds->g->playing->submitter
1116 || !strcmp(ds->g->playing->submitter,
1117 disorder_user(ds->g->client))));
1118 } else
1119 result = 1;
1120 sink_printf(output->sink, "%s", bool2str(result));
1121}
1122
1123static void exp_removable(int attribute((unused)) nargs,
1124 char attribute((unused)) **args,
1125 cgi_sink *output,
1126 void attribute((unused)) *u) {
1127 dcgi_state *ds = u;
1128 int result;
1129
1130 if(config->restrictions & RESTRICT_REMOVE)
1131 result = (ds->track
1132 && ds->track->submitter
1133 && !strcmp(ds->track->submitter,
1134 disorder_user(ds->g->client)));
1135 else
1136 result = 1;
1137 sink_printf(output->sink, "%s", bool2str(result));
1138}
1139
1140static void exp_navigate(int attribute((unused)) nargs,
1141 char **args,
1142 cgi_sink *output,
1143 void *u) {
1144 dcgi_state *ds = u;
1145 dcgi_state substate;
1146 const char *path = expandarg(args[0], ds);
1147 const char *ptr;
1148 int dirlen;
1149
1150 if(*path) {
1151 memset(&substate, 0, sizeof substate);
1152 substate.g = ds->g;
1153 ptr = path + 1; /* skip root */
1154 dirlen = 0;
1155 substate.nav_path = path;
1156 substate.first = 1;
1157 while(*ptr) {
1158 while(*ptr && *ptr != '/')
1159 ++ptr;
1160 substate.last = !*ptr;
1161 substate.nav_len = ptr - path;
1162 substate.nav_dirlen = dirlen;
1163 expandstring(output, args[1], &substate);
1164 dirlen = substate.nav_len;
1165 if(*ptr) ++ptr;
1166 substate.first = 0;
1167 }
1168 }
1169}
1170
1171static void exp_fullname(int attribute((unused)) nargs,
1172 char attribute((unused)) **args,
1173 cgi_sink *output,
1174 void *u) {
1175 dcgi_state *ds = u;
1176 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1177}
1178
1179static void exp_basename(int nargs,
1180 char **args,
1181 cgi_sink *output,
1182 void *u) {
1183 dcgi_state *ds = u;
1184 const char *s;
1185
1186 if(nargs) {
1187 if((s = strrchr(args[0], '/'))) ++s;
1188 else s = args[0];
1189 cgi_output(output, "%s", s);
1190 } else
1191 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1192 ds->nav_path + ds->nav_dirlen + 1);
1193}
1194
1195static void exp_dirname(int nargs,
1196 char **args,
1197 cgi_sink *output,
1198 void *u) {
1199 dcgi_state *ds = u;
1200 const char *s;
1201
1202 if(nargs) {
1203 if((s = strrchr(args[0], '/')))
1204 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1205 } else
1206 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1207}
1208
1209static void exp_eq(int attribute((unused)) nargs,
1210 char **args,
1211 cgi_sink *output,
1212 void attribute((unused)) *u) {
1213 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1214}
1215
1216static void exp_ne(int attribute((unused)) nargs,
1217 char **args,
1218 cgi_sink *output,
1219 void attribute((unused)) *u) {
1220 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1221}
1222
1223static void exp_enabled(int attribute((unused)) nargs,
1224 char attribute((unused)) **args,
1225 cgi_sink *output,
1226 void *u) {
1227 dcgi_state *ds = u;
1228 int enabled = 0;
1229
1230 if(ds->g->client)
1231 disorder_enabled(ds->g->client, &enabled);
1232 cgi_output(output, "%s", bool2str(enabled));
1233}
1234
1235static void exp_random_enabled(int attribute((unused)) nargs,
1236 char attribute((unused)) **args,
1237 cgi_sink *output,
1238 void *u) {
1239 dcgi_state *ds = u;
1240 int enabled = 0;
1241
1242 if(ds->g->client)
1243 disorder_random_enabled(ds->g->client, &enabled);
1244 cgi_output(output, "%s", bool2str(enabled));
1245}
1246
1247static void exp_trackstate(int attribute((unused)) nargs,
1248 char **args,
1249 cgi_sink *output,
1250 void *u) {
1251 dcgi_state *ds = u;
1252 struct queue_entry *q;
1253 char *track;
1254
1255 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1256 lookups(ds, DC_QUEUE|DC_PLAYING);
1257 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1258 cgi_output(output, "playing");
1259 else {
1260 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1261 ;
1262 if(q)
1263 cgi_output(output, "queued");
1264 }
1265}
1266
1267static void exp_thisurl(int attribute((unused)) nargs,
1268 char attribute((unused)) **args,
1269 cgi_sink *output,
1270 void attribute((unused)) *u) {
1271 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1272 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1273}
1274
1275static void exp_isfirst(int attribute((unused)) nargs,
1276 char attribute((unused)) **args,
1277 cgi_sink *output,
1278 void *u) {
1279 dcgi_state *ds = u;
1280
1281 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1282}
1283
1284static void exp_islast(int attribute((unused)) nargs,
1285 char attribute((unused)) **args,
1286 cgi_sink *output,
1287 void *u) {
1288 dcgi_state *ds = u;
1289
1290 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1291}
1292
1293static void exp_action(int attribute((unused)) nargs,
1294 char attribute((unused)) **args,
1295 cgi_sink *output,
1296 void attribute((unused)) *u) {
1297 const char *action = cgi_get("action"), *mgmt;
1298
1299 if(!action) action = "playing";
1300 if(!strcmp(action, "playing")
1301 && (mgmt = cgi_get("mgmt"))
1302 && !strcmp(mgmt, "true"))
1303 action = "manage";
1304 sink_printf(output->sink, "%s", action);
1305}
1306
1307static void exp_resolve(int attribute((unused)) nargs,
1308 char **args,
1309 cgi_sink *output,
1310 void attribute((unused)) *u) {
1311 dcgi_state *ds = u;
1312 char *track;
1313
1314 if(!disorder_resolve(ds->g->client, &track, args[0]))
1315 sink_printf(output->sink, "%s", track);
1316}
1317
1318static void exp_paused(int attribute((unused)) nargs,
1319 char attribute((unused)) **args,
1320 cgi_sink *output,
1321 void *u) {
1322 dcgi_state *ds = u;
1323 int paused = 0;
1324
1325 lookups(ds, DC_PLAYING);
1326 if(ds->g->playing && ds->g->playing->state == playing_paused)
1327 paused = 1;
1328 cgi_output(output, "%s", bool2str(paused));
1329}
1330
1331static void exp_state(int attribute((unused)) nargs,
1332 char attribute((unused)) **args,
1333 cgi_sink *output,
1334 void *u) {
1335 dcgi_state *ds = u;
1336
1337 if(ds->track)
1338 cgi_output(output, "%s", playing_states[ds->track->state]);
1339}
1340
1341static void exp_files(int attribute((unused)) nargs,
1342 char **args,
1343 cgi_sink *output,
1344 void *u) {
1345 dcgi_state *ds = u;
1346 dcgi_state substate;
1347 const char *nfiles_arg, *directory;
1348 int nfiles, numfile;
1349 struct kvp *k;
1350
1351 memset(&substate, 0, sizeof substate);
1352 substate.g = ds->g;
1353 if((directory = cgi_get("directory"))) {
1354 /* Prefs for whole directory. */
1355 lookups(ds, DC_FILES);
1356 /* Synthesize args for the file list. */
1357 nfiles = ds->g->nfiles;
1358 for(numfile = 0; numfile < nfiles; ++numfile) {
1359 k = xmalloc(sizeof *k);
1360 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1361 k->value = ds->g->files[numfile];
1362 k->next = cgi_args;
1363 cgi_args = k;
1364 }
1365 } else {
1366 /* Args already present. */
1367 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1368 else nfiles = 1;
1369 }
1370 for(numfile = 0; numfile < nfiles; ++numfile) {
1371 substate.index = numfile;
1372 expandstring(output, args[0], &substate);
1373 }
1374}
1375
1376static void exp_index(int attribute((unused)) nargs,
1377 char attribute((unused)) **args,
1378 cgi_sink *output,
1379 void *u) {
1380 dcgi_state *ds = u;
1381
1382 cgi_output(output, "%d", ds->index);
1383}
1384
1385static void exp_nfiles(int attribute((unused)) nargs,
1386 char attribute((unused)) **args,
1387 cgi_sink *output,
1388 void *u) {
1389 dcgi_state *ds = u;
1390 const char *files_arg;
1391
1392 if(cgi_get("directory")) {
1393 lookups(ds, DC_FILES);
1394 cgi_output(output, "%d", ds->g->nfiles);
1395 } else if((files_arg = cgi_get("files")))
1396 cgi_output(output, "%s", files_arg);
1397 else
1398 cgi_output(output, "1");
1399}
1400
1401static const struct cgi_expansion expansions[] = {
1402 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1403 { "action", 0, 0, 0, exp_action },
1404 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1405 { "arg", 1, 1, 0, exp_arg },
1406 { "basename", 0, 1, 0, exp_basename },
1407 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1408 { "dirname", 0, 1, 0, exp_dirname },
1409 { "enabled", 0, 0, 0, exp_enabled },
1410 { "eq", 2, 2, 0, exp_eq },
1411 { "file", 0, 0, 0, exp_file },
1412 { "files", 1, 1, EXP_MAGIC, exp_files },
1413 { "fullname", 0, 0, 0, exp_fullname },
1414 { "id", 0, 0, 0, exp_id },
1415 { "if", 2, 3, EXP_MAGIC, exp_if },
1416 { "include", 1, 1, 0, exp_include },
1417 { "index", 0, 0, 0, exp_index },
1418 { "isdirectories", 0, 0, 0, exp_isdirectories },
1419 { "isfiles", 0, 0, 0, exp_isfiles },
1420 { "isfirst", 0, 0, 0, exp_isfirst },
1421 { "islast", 0, 0, 0, exp_islast },
78efa64e 1422 { "isnew", 0, 0, 0, exp_isnew },
460b9539 1423 { "isplaying", 0, 0, 0, exp_isplaying },
1424 { "isqueue", 0, 0, 0, exp_isqueue },
1425 { "isrecent", 0, 0, 0, exp_isrecent },
1426 { "label", 1, 1, 0, exp_label },
1427 { "length", 0, 0, 0, exp_length },
1428 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1429 { "ne", 2, 2, 0, exp_ne },
78efa64e 1430 { "new", 1, 1, EXP_MAGIC, exp_new },
460b9539 1431 { "nfiles", 0, 0, 0, exp_nfiles },
1432 { "nonce", 0, 0, 0, exp_nonce },
1433 { "not", 1, 1, 0, exp_not },
1434 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1435 { "parity", 0, 0, 0, exp_parity },
1436 { "part", 1, 3, 0, exp_part },
1437 { "paused", 0, 0, 0, exp_paused },
1438 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1439 { "pref", 2, 2, 0, exp_pref },
1440 { "prefname", 0, 0, 0, exp_prefname },
1441 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1442 { "prefvalue", 0, 0, 0, exp_prefvalue },
1443 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1444 { "random-enabled", 0, 0, 0, exp_random_enabled },
1445 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1446 { "removable", 0, 0, 0, exp_removable },
1447 { "resolve", 1, 1, 0, exp_resolve },
1448 { "scratchable", 0, 0, 0, exp_scratchable },
1449 { "search", 2, 3, EXP_MAGIC, exp_search },
1450 { "server-version", 0, 0, 0, exp_server_version },
1451 { "shell", 1, 1, 0, exp_shell },
1452 { "state", 0, 0, 0, exp_state },
1453 { "stats", 0, 0, 0, exp_stats },
1454 { "thisurl", 0, 0, 0, exp_thisurl },
1455 { "track", 0, 0, 0, exp_track },
1456 { "trackstate", 1, 1, 0, exp_trackstate },
1457 { "transform", 2, 3, 0, exp_transform },
1458 { "url", 0, 0, 0, exp_url },
1459 { "urlquote", 1, 1, 0, exp_urlquote },
1460 { "version", 0, 0, 0, exp_version },
1461 { "volume", 1, 1, 0, exp_volume },
1462 { "when", 0, 0, 0, exp_when },
1463 { "who", 0, 0, 0, exp_who }
1464};
1465
1466static void expand(cgi_sink *output,
1467 const char *template,
1468 dcgi_state *ds) {
1469 cgi_expand(template,
1470 expansions, sizeof expansions / sizeof *expansions,
1471 output,
1472 ds);
1473}
1474
1475static void expandstring(cgi_sink *output,
1476 const char *string,
1477 dcgi_state *ds) {
1478 cgi_expand_string("",
1479 string,
1480 expansions, sizeof expansions / sizeof *expansions,
1481 output,
1482 ds);
1483}
1484
1485static void perform_action(cgi_sink *output, dcgi_state *ds,
1486 const char *action) {
1487 int n;
1488
1489 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1490 actions[n].handler(output, ds);
1491 else {
1492 cgi_header(output->sink, "Content-Type", "text/html");
1493 cgi_body(output->sink);
1494 expand(output, action, ds);
1495 }
1496}
1497
1498void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1499 const char *action = cgi_get("action");
1500
1501 if(!action) action = "playing";
1502 perform_action(output, ds, action);
1503}
1504
1505void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1506 const char *msg) {
1507 cgi_set_option("error", msg);
1508 perform_action(output, ds, "error");
1509}
1510
1511/*
1512Local Variables:
1513c-basic-offset:2
1514comment-column:40
1515fill-column:79
1516End:
1517*/