X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/04024c2cafab56ea76a9a2b35097584d6db98c06..9bf61b29f946707fc9d331bb1ee860dac8327739:/server/macros-disorder.c diff --git a/server/macros-disorder.c b/server/macros-disorder.c index cc79657..92fcb75 100644 --- a/server/macros-disorder.c +++ b/server/macros-disorder.c @@ -21,34 +21,10 @@ * @brief DisOrder-specific expansions */ -#include -#include "types.h" - -#include "sink.h" -#include "client.h" -#include "cgi.h" -#include "macros-disorder.h" -#include "lookups.h" +#include "disorder-cgi.h" /** @brief For error template */ -char *error_string; - -/** @brief Locate a track by ID */ -static struct queue_entry *findtrack(const char *id) { - struct queue_entry *q; - - lookups(DC_PLAYING); - if(playing && !strcmp(playing->id, id)) - return playing; - lookups(DC_QUEUE); - for(q = queue; q; q = q->next) - if(!strcmp(q->id, id)) - return q; - lookups(DC_RECENT); - for(q = recent; q; q = q->next) - if(!strcmp(q->id, id)) - return q; -} +char *dcgi_error_string; /** @brief Return @p i as a string */ static const char *make_index(int i) { @@ -69,12 +45,12 @@ static int exp_server_version(int attribute((unused)) nargs, void attribute((unused)) *u) { const char *v; - if(client) { - if(disorder_version(client, (char **)&v)) + if(dcgi_client) { + if(disorder_version(dcgi_client, (char **)&v)) v = "(cannot get version)"; } else v = "(server not running)"; - return sink_write(output, cgi_sgmlquote(v)) < 0 ? -1 : 0; + return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0; } /* @version @@ -85,8 +61,8 @@ static int exp_version(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - return sink_write(output, - cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0; + return sink_writes(output, + cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0; } /* @url @@ -97,8 +73,8 @@ static int exp_url(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - return sink_write(output, - cgi_sgmlquote(config->url)) < 0 ? -1 : 0; + return sink_writes(output, + cgi_sgmlquote(config->url)) < 0 ? -1 : 0; } /* @arg{NAME} @@ -111,9 +87,10 @@ static int exp_arg(int attribute((unused)) nargs, struct sink *output, void attribute((unused)) *u) { const char *s = cgi_get(args[0]); + if(s) - return sink_write(output, - cgi_sgmlquote(s)) < 0 ? -1 : 0; + return sink_writes(output, + cgi_sgmlquote(s)) < 0 ? -1 : 0; else return 0; } @@ -127,10 +104,10 @@ static int exp_user(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - const char *u; + const char *user; - if(client && (u = disorder_user(client))) - return sink_write(output, cgi_sgmlquote(u)) < 0 ? -1 : 0; + if(dcgi_client && (user = disorder_user(dcgi_client))) + return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0; return 0; } @@ -155,18 +132,19 @@ static int exp_part(int nargs, char *s; if(track[0] != '/') { - struct queue_entry *q = findtrack(track); + struct queue_entry *q = dcgi_findtrack(track); if(q) track = q->track; else return 0; } - if(client - && !disorder_get(client, &s, track, - !strcmp(context, "short") ? "display" : context, - part)) - return sink_write(output, cgi_sgmlquote(s)) < 0 ? -1 : 0; + if(dcgi_client + && !disorder_part(dcgi_client, &s, + track, + !strcmp(context, "short") ? "display" : context, + part)) + return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0; return 0; } @@ -179,7 +157,7 @@ static int exp_quote(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - return sink_write(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0; + return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0; } /* @who{ID} @@ -191,10 +169,10 @@ static int exp_who(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - struct queue_entry *q = findtrack(args[0]); + struct queue_entry *q = dcgi_findtrack(args[0]); if(q && q->submitter) - return sink_write(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0; + return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0; return 0; } @@ -207,7 +185,7 @@ static int exp_when(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - struct queue_entry *q = findtrack(args[0]); + struct queue_entry *q = dcgi_findtrack(args[0]); const struct tm *w = 0; if(q) { @@ -232,7 +210,7 @@ static int exp_when(int attribute((unused)) nargs, if(w) return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0; } - return sink_write(output, " ") < 0 ? -1 : 0; + return sink_writes(output, " ") < 0 ? -1 : 0; } /* @length{ID|TRACK} @@ -254,17 +232,17 @@ static int exp_length(int attribute((unused)) nargs, name = args[0]; else { /* Track identified by queue ID */ - if(!(q = findtrack(args[0]))) + if(!(q = dcgi_findtrack(args[0]))) return 0; - if(q->state == play_start || q->state == playing_paused) + if(q->state == playing_started || q->state == playing_paused) if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0) return -1; name = q->track; } - if(client && disorder_length(client, name, &length)) + if(dcgi_client && disorder_length(dcgi_client, name, &length)) return sink_printf(output, "%ld:%02ld", length / 60, length % 60) < 0 ? -1 : 0; - return sink_write(output, " ") < 0 ? -1 : 0; + return sink_writes(output, " ") < 0 ? -1 : 0; } /* @removable{ID} @@ -276,40 +254,55 @@ static int exp_removable(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - struct queue_entry *q = findtrack(args[0]); + struct queue_entry *q = dcgi_findtrack(args[0]); /* TODO would be better to reject recent */ - if(!q || !client) + if(!q || !dcgi_client) return mx_bool_result(output, 0); - lookup(DC_RIGHTS); + dcgi_lookup(DCGI_RIGHTS); return mx_bool_result(output, - (q == playing ? right_scratchable : right_removable) - (rights, disorder_user(client), q)); + (q == dcgi_playing ? right_scratchable : right_removable) + (dcgi_rights, disorder_user(dcgi_client), q)); } -/* @movable{ID} +/* @movable{ID}{DIR} * * Expands to "true" if track ID is movable and "false" otherwise. + * + * DIR (which is optional) should be a non-zero integer. If it is negative + * then the intended move is down (later in the queue), if it is positive then + * the intended move is up (earlier in the queue). The first track is not + * movable up and the last track not movable down. */ -static int exp_movable(int attribute((unused)) nargs, +static int exp_movable(int nargs, char **args, struct sink *output, void attribute((unused)) *u) { - struct queue_entry *q = findtrack(args[0]); + struct queue_entry *q = dcgi_findtrack(args[0]); /* TODO would be better to recent playing/recent */ - if(!q || !client) + if(!q || !dcgi_client) return mx_bool_result(output, 0); - lookup(DC_RIGHTS); + if(nargs > 1) { + const long dir = atoi(args[1]); + + if(dir > 0 && q == dcgi_queue) + return mx_bool_result(output, 0); + if(dir < 0 && q->next == 0) + return mx_bool_result(output, 0); + } + dcgi_lookup(DCGI_RIGHTS); return mx_bool_result(output, - right_movable(rights, disorder_user(client), q)); + right_movable(dcgi_rights, + disorder_user(dcgi_client), + q)); } /* @playing{TEMPLATE} * * Expands to TEMPLATE, with: - * - @id@ expanded to the queue ID of the playing track - * - @track@ expanded to its UNQUOTED name + * - @id expanded to the queue ID of the playing track + * - @track expanded to its UNQUOTED name * * If no track is playing expands to nothing. * @@ -319,45 +312,47 @@ static int exp_movable(int attribute((unused)) nargs, static int exp_playing(int nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { - lookup(DC_PLAYING); - if(!playing) + void *u) { + dcgi_lookup(DCGI_PLAYING); + if(!dcgi_playing) return 0; if(!nargs) - return sink_write(output, playing->id) < 0 ? -1 : 0; - return mx_rewritel(args[0], - "id", playing->id, - "track", playing->track, - (char *)0); + return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0; + return mx_expand(mx_rewritel(args[0], + "id", dcgi_playing->id, + "track", dcgi_playing->track, + (char *)0), + output, u); } /* @queue{TEMPLATE} * * For each track in the queue, expands TEMPLATE with the following expansions: - * - @id@ to the queue ID of the track - * - @track@ to the UNQUOTED track name - * - @index@ to the track number from 0 - * - @parity@ to "even" or "odd" alternately - * - @first@ to "true" on the first track and "false" otherwise - * - @last@ to "true" on the last track and "false" otherwise + * - @id to the queue ID of the track + * - @track to the UNQUOTED track name + * - @index to the track number from 0 + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first track and "false" otherwise + * - @last to "true" on the last track and "false" otherwise */ static int exp_queue(int attribute((unused)) nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { + void *u) { struct queue_entry *q; int rc, i; - lookup(DC_QUEUE); - for(q = queue, i = 0; q; q = q->next, ++i) - if((rc = mx_rewritel(args[0], - "id", q->id, - "track", q->track, - "index", make_index(i), - "parity", i % 2 ? "odd" : "even", - "first", q == queue ? "true" : "false", - "last", q->next ? "false" : "true", - (char *)0))) + dcgi_lookup(DCGI_QUEUE); + for(q = dcgi_queue, i = 0; q; q = q->next, ++i) + if((rc = mx_expand(mx_rewritel(args[0], + "id", q->id, + "track", q->track, + "index", make_index(i), + "parity", i % 2 ? "odd" : "even", + "first", q == dcgi_queue ? "true" : "false", + "last", q->next ? "false" : "true", + (char *)0), + output, u))) return rc; return 0; } @@ -366,30 +361,31 @@ static int exp_queue(int attribute((unused)) nargs, * * For each track in the recently played list, expands TEMPLATE with the * following expansions: - * - @id@ to the queue ID of the track - * - @track@ to the UNQUOTED track name - * - @index@ to the track number from 0 - * - @parity@ to "even" or "odd" alternately - * - @first@ to "true" on the first track and "false" otherwise - * - @last@ to "true" on the last track and "false" otherwise + * - @id to the queue ID of the track + * - @track to the UNQUOTED track name + * - @index to the track number from 0 + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first track and "false" otherwise + * - @last to "true" on the last track and "false" otherwise */ static int exp_recent(int attribute((unused)) nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { + void *u) { struct queue_entry *q; int rc, i; - lookup(DC_RECENT); - for(q = recent, i = 0; q; q = q->next, ++i) - if((rc = mx_rewritel(args[0], - "id", q->id, - "track", q->track, - "index", make_index(i), - "parity", i % 2 ? "odd" : "even", - "first", q == recent ? "true" : "false", - "last", q->next ? "false" : "true", - (char *)0))) + dcgi_lookup(DCGI_RECENT); + for(q = dcgi_recent, i = 0; q; q = q->next, ++i) + if((rc = mx_expand(mx_rewritel(args[0], + "id", q->id, + "track", q->track, + "index", make_index(i), + "parity", i % 2 ? "odd" : "even", + "first", q == dcgi_recent ? "true" : "false", + "last", q->next ? "false" : "true", + (char *)0), + output, u))) return rc; return 0; } @@ -398,32 +394,32 @@ static int exp_recent(int attribute((unused)) nargs, * * For each track in the newly added list, expands TEMPLATE wit the following * expansions: - * - @track@ to the UNQUOTED track name - * - @index@ to the track number from 0 - * - @parity@ to "even" or "odd" alternately - * - @first@ to "true" on the first track and "false" otherwise - * - @last@ to "true" on the last track and "false" otherwise + * - @track to the UNQUOTED track name + * - @index to the track number from 0 + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first track and "false" otherwise + * - @last to "true" on the last track and "false" otherwise * - * Note that unlike @playing@, @queue@ and @recent@ which are otherwise - * superficially similar, there is no @id@ sub-expansion here. + * Note that unlike @playing, @queue and @recent which are otherwise + * superficially similar, there is no @id sub-expansion here. */ static int exp_new(int attribute((unused)) nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { - struct queue_entry *q; + void *u) { int rc, i; - lookup(DC_NEW); + dcgi_lookup(DCGI_NEW); /* TODO perhaps we should generate an ID value for tracks in the new list */ - for(i = 0; i < nnew; ++i) - if((rc = mx_rewritel(args[0], - "track", new[i], - "index", make_index(i), - "parity", i % 2 ? "odd" : "even", - "first", i == 0 ? "true" : "false", - "last", i == nnew - 1 ? "false" : "true", - (char *)0))) + for(i = 0; i < dcgi_nnew; ++i) + if((rc = mx_expand(mx_rewritel(args[0], + "track", dcgi_new[i], + "index", make_index(i), + "parity", i % 2 ? "odd" : "even", + "first", i == 0 ? "true" : "false", + "last", i == dcgi_nnew - 1 ? "false" : "true", + (char *)0), + output, u))) return rc; return 0; } @@ -437,10 +433,10 @@ static int exp_volume(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_VOLUME); - return sink_write(output, "%d", - !strcmp(args[0], "left") - ? volume_left : volume_right) < 0 ? -1 : 0; + dcgi_lookup(DCGI_VOLUME); + return sink_printf(output, "%d", + !strcmp(args[0], "left") + ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0; } /* @isplaying @@ -451,8 +447,8 @@ static int exp_isplaying(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_PLAYING); - return mx_bool_result(output, !!playing); + dcgi_lookup(DCGI_PLAYING); + return mx_bool_result(output, !!dcgi_playing); } /* @isqueue @@ -463,8 +459,8 @@ static int exp_isqueue(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_QUEUE); - return mx_bool_result(output, !!queue); + dcgi_lookup(DCGI_QUEUE); + return mx_bool_result(output, !!dcgi_queue); } /* @isrecent@ @@ -476,8 +472,8 @@ static int exp_isrecent(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_RECENT); - return mx_bool_result(output, !!recent); + dcgi_lookup(DCGI_RECENT); + return mx_bool_result(output, !!dcgi_recent); } /* @isnew @@ -489,8 +485,8 @@ static int exp_isnew(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_NEW); - return mx_bool_result(output, !!nnew); + dcgi_lookup(DCGI_NEW); + return mx_bool_result(output, !!dcgi_nnew); } /* @pref{TRACK}{KEY} @@ -503,44 +499,46 @@ static int exp_pref(int attribute((unused)) nargs, void attribute((unused)) *u) { char *value; - if(client && !disorder_get(client, args[0], args[1], &value)) - return sink_write(output, cgi_sgmlquote(value)) < 0 ? -1 : 0; + if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value)) + return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0; + return 0; } /* @prefs{TRACK}{TEMPLATE} * * For each track preference of track TRACK, expands TEMPLATE with the * following expansions: - * - @name@ to the UNQUOTED preference name - * - @index@ to the preference number from 0 - * - @value@ to the UNQUOTED preference value - * - @parity@ to "even" or "odd" alternately - * - @first@ to "true" on the first preference and "false" otherwise - * - @last@ to "true" on the last preference and "false" otherwise + * - @name to the UNQUOTED preference name + * - @index to the preference number from 0 + * - @value to the UNQUOTED preference value + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first preference and "false" otherwise + * - @last to "true" on the last preference and "false" otherwise * - * Use @quote@ to quote preference names and values where necessary; see below. + * Use @quote to quote preference names and values where necessary; see below. */ static int exp_prefs(int attribute((unused)) nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { + void *u) { int rc, i; struct kvp *k, *head; char *track; if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)"))) return rc; - if(!client || disorder_prefs(client, track, &head)) + if(!dcgi_client || disorder_prefs(dcgi_client, track, &head)) return 0; for(k = head, i = 0; k; k = k->next, ++i) - if((rc = mx_rewritel(args[1], - "index", make_index(i), - "parity", i % 2 ? "odd" : "even", - "name", k->name, - "value", k->value, - "first", k == head ? "true" : "false", - "last", k->next ? "false" : "true", - (char *)0))) + if((rc = mx_expand(mx_rewritel(args[1], + "index", make_index(i), + "parity", i % 2 ? "odd" : "even", + "name", k->name, + "value", k->value, + "first", k == head ? "true" : "false", + "last", k->next ? "false" : "true", + (char *)0), + output, u))) return rc; return 0; } @@ -556,8 +554,8 @@ static int exp_transform(int nargs, struct sink *output, void attribute((unused)) *u) { const char *t = trackname_transform(args[1], args[0], - (nargs > 2 ? args[2] : "display"))); - return sink_write(output, cgi_sgmlquote(t)) < 0 ? -1 : 0; + (nargs > 2 ? args[2] : "display")); + return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0; } /* @enabled@ @@ -568,26 +566,26 @@ static int exp_enabled(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - int enabled = 0; + int e = 0; - if(client) - disorder_enabled(client, &enabled); - return mx_bool_result(output, enabled); + if(dcgi_client) + disorder_enabled(dcgi_client, &e); + return mx_bool_result(output, e); } /* @random-enabled * * Expands to "true" if random play is enabled, otherwise "false". */ -static int exp_enabled(int attribute((unused)) nargs, - char attribute((unused)) **args, - struct sink *output, - void attribute((unused)) *u) { - int enabled = 0; +static int exp_random_enabled(int attribute((unused)) nargs, + char attribute((unused)) **args, + struct sink *output, + void attribute((unused)) *u) { + int e = 0; - if(client) - disorder_random_enabled(client, &enabled); - return mx_bool_result(output, enabled); + if(dcgi_client) + disorder_random_enabled(dcgi_client, &e); + return mx_bool_result(output, e); } /* @trackstate{TRACK} @@ -602,17 +600,17 @@ static int exp_trackstate(int attribute((unused)) nargs, char *track; struct queue_entry *q; - if(!client) + if(!dcgi_client) return 0; - if(disorder_resolve(client, &track, args[0])) + if(disorder_resolve(dcgi_client, &track, args[0])) return 0; - lookup(DC_PLAYING); - if(playing && !strcmp(track, playing->track)) - return sink_write(output, "playing") < 0 ? -1 : 0; - lookup(DC_QUEUE); - for(q = queue; q; q = q->next) + dcgi_lookup(DCGI_PLAYING); + if(dcgi_playing && !strcmp(track, dcgi_playing->track)) + return sink_writes(output, "playing") < 0 ? -1 : 0; + dcgi_lookup(DCGI_QUEUE); + for(q = dcgi_queue; q; q = q->next) if(!strcmp(track, q->track)) - return sink_write(output, "queued") < 0 ? -1 : 0; + return sink_writes(output, "queued") < 0 ? -1 : 0; return 0; } @@ -626,7 +624,7 @@ static int exp_thisurl(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - return cgi_thisurl(config->url); + return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0; } /* @resolve{TRACK} @@ -640,8 +638,8 @@ static int exp_resolve(int attribute((unused)) nargs, void attribute((unused)) *u) { char *r; - if(client && !disorder_resolve(client, &r, args[0])) - return sink_write(output, r) < 0 ? -1 : 0; + if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0])) + return sink_writes(output, r) < 0 ? -1 : 0; return 0; } @@ -654,8 +652,9 @@ static int exp_paused(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_PLAYING); - return mx_bool_result(output, playing && playing->state == playing_paused); + dcgi_lookup(DCGI_PLAYING); + return mx_bool_result(output, (dcgi_playing + && dcgi_playing->state == playing_paused)); } /* @state{ID}@ @@ -666,10 +665,10 @@ static int exp_state(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - struct queue_entry *q = findtrack(args[0]); + struct queue_entry *q = dcgi_findtrack(args[0]); if(q) - return sink_write(output, playing_states[q->state]) < 0 ? -1 : 0; + return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0; return 0; } @@ -687,25 +686,26 @@ static int exp_state(int attribute((unused)) nargs, static int exp_right(int nargs, const struct mx_node **args, struct sink *output, - void attribute((unused)) *u) { + void *u) { char *right; rights_type r; + int rc; - if(!client) + if(!dcgi_client) return 0; - lookup(DC_RIGHTS); - if((rc = mx_expandstr(args[0], &rightname, u, "argument #0 (RIGHT)"))) + dcgi_lookup(DCGI_RIGHTS); + if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)"))) return rc; if(parse_rights(right, &r, 1/*report*/)) return 0; /* Single-argument form */ if(nargs == 1) - return mx_bool_result(output, !!(r & rights)); + return mx_bool_result(output, !!(r & dcgi_rights)); /* Multiple argument form */ - if(r & rights) - return mx_expandl(args[1], (char *)0); + if(r & dcgi_rights) + return mx_expand(args[1], output, u); if(nargs == 3) - return mx_expandl(args[2], (char *)0); + return mx_expand(args[2], output, u); return 0; } @@ -719,8 +719,10 @@ static int exp_userinfo(int attribute((unused)) nargs, void attribute((unused)) *u) { char *v; - if(client && !disorder_userinfo(client, disorder_user(client), args[0], &v)) - return sink_write(output, v) < 0 ? -1 : 0; + if(dcgi_client + && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client), + args[0], &v)) + return sink_writes(output, v) < 0 ? -1 : 0; return 0; } @@ -732,56 +734,200 @@ static int exp_error(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - return sink_write(output, cgi_sgmlquote(error_string)) < 0 ? -1 : 0; + return sink_writes(output, dcgi_error_string) < 0 ? -1 : 0; +} + +/* @image{NAME} + * + * Expands to the URL of the image called NAME. + * + * Firstly if there is a label called images.NAME then the image stem will be + * the value of that label. Otherwise the stem will be NAME.png. + * + * If the label url.static exists then it will give the base URL for images. + * Otherwise the base url will be /disorder/. + */ +static int exp_image(int attribute((unused)) nargs, + char **args, + struct sink *output, + void attribute((unused)) *u) { + const char *url, *stem; + char *labelname; + + /* Compute the stem */ + byte_xasprintf(&labelname, "images.%s", args[0]); + if(option_label_exists(labelname)) + stem = option_label(labelname); + else + byte_xasprintf((char **)&stem, "%s.png", args[0]); + /* If the stem looks like it's reasonalby complete, use that */ + if(stem[0] == '/' + || !strncmp(stem, "http:", 5) + || !strncmp(stem, "https:", 6)) + url = stem; + else { + /* Compute the URL */ + if(option_label_exists("url.static")) + byte_xasprintf((char **)&url, "%s/%s", + option_label("url.static"), stem); + else + /* Default base is /disorder */ + byte_xasprintf((char **)&url, "/disorder/%s", stem); + } + return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0; +} + +/** @brief Entry in a list of tracks or directories */ +struct entry { + /** @brief Track name */ + const char *track; + /** @brief Sort key */ + const char *sort; + /** @brief Display key */ + const char *display; +}; + +/** @brief Compare two @ref entry objects */ +static int compare_entry(const void *a, const void *b) { + const struct entry *ea = a, *eb = b; + + return compare_tracks(ea->sort, eb->sort, + ea->display, eb->display, + ea->track, eb->track); +} + +/** @brief Implementation of exp_tracks() and exp_dirs() */ +static int exp__files_dirs(int nargs, + const struct mx_node **args, + struct sink *output, + void *u, + const char *type, + int (*fn)(disorder_client *client, + const char *name, + const char *re, + char ***vecp, + int *nvecp)) { + char **tracks, *dir, *re; + int n, ntracks, rc; + const struct mx_node *m; + struct entry *e; + + if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)"))) + return rc; + if(nargs == 3) { + if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)"))) + return rc; + m = args[2]; + } else { + re = 0; + m = args[1]; + } + if(!dcgi_client) + return 0; + /* Get the list */ + if(fn(dcgi_client, dir, re, &tracks, &ntracks)) + return 0; + /* Sort it. NB trackname_transform() does not go to the server. */ + e = xcalloc(ntracks, sizeof *e); + for(n = 0; n < ntracks; ++n) { + e->track = tracks[n]; + e[n].track = tracks[n]; + e[n].sort = trackname_transform(type, tracks[n], "sort"); + e[n].display = trackname_transform(type, tracks[n], "display"); + } + qsort(e, ntracks, sizeof (struct entry), compare_entry); + /* Expand the subsiduary templates */ + for(n = 0; n < ntracks; ++n) + if((rc = mx_expand(mx_rewritel(m, + "index", make_index(n), + "parity", n % 2 ? "odd" : "even", + "track", tracks[n], + "first", n == 0 ? "true" : "false", + "last", n + 1 == ntracks ? "false" : "true", + (char *)0), + output, u))) + return rc; + return 0; + } -/* @userinfo{PROPERTY}@ +/** @tracks{DIR}{RE}{TEMPLATE} * + * For each track below DIR, expands TEMPLATE with the + * following expansions: + * - @track to the UNQUOTED track name + * - @index to the track number from 0 + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first track and "false" otherwise + * - @last to "true" on the last track and "false" otherwise + * + * RE is optional and if present is the regexp to match against. + */ +static int exp_tracks(int nargs, + const struct mx_node **args, + struct sink *output, + void *u) { + return exp__files_dirs(nargs, args, output, u, "track", disorder_files); +} + +/** @dirs{DIR}{RE}{TEMPLATE} + * + * For each directory below DIR, expands TEMPLATE with the + * following expansions: + * - @track to the UNQUOTED directory name + * - @index to the directory number from 0 + * - @parity to "even" or "odd" alternately + * - @first to "true" on the first directory and "false" otherwise + * - @last to "true" on the last directory and "false" otherwise + * + * RE is optional and if present is the regexp to match against. + */ +static int exp_dirs(int nargs, + const struct mx_node **args, + struct sink *output, + void *u) { + return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories); +} + /** @brief Register DisOrder-specific expansions */ -void register_disorder_expansions(void) { - mx_register(exp_arg, 1, 1, "arg"); - mx_register(exp_enabled, 0, 0, "enabled"); - mx_register(exp_error, 0, 0, "error"); - mx_register(exp_isnew, 0, 0, "isnew"); - mx_register(exp_isplaying, 0, 0, "isplaying"); - mx_register(exp_isqueue, 0, 0, "isplaying"); - mx_register(exp_isrecent, 0, 0, "isrecent"); - mx_register(exp_length, 1, 1, "length"); - mx_register(exp_movable, 1, 1, "movable"); - mx_register(exp_part, 2, 3, "part"); - mx_register(exp_pref, 2, 2, "pref"); - mx_register(exp_quote, 1, 1, "quote"); - mx_register(exp_random_enabled, 0, 0, "random-enabled"); - mx_register(exp_removable, 1, 1, "removable"); - mx_register(exp_resolve, 1, 1, "resolve"); - mx_register(exp_right, 1, 3, "right"); - mx_register(exp_server_version, 0, 0, "server-version"); - mx_register(exp_state, 1, 1, "state"); - mx_register(exp_thisurl, 0, 0, "thisurl"); - mx_register(exp_trackstate, 1, 1, "trackstate"); - mx_register(exp_transform, 2, 3, "transform"); - mx_register(exp_url, 0, 0, "url"); - mx_register(exp_user, 0, 0, "user"); - mx_register(exp_userinfo, 1, 1, "userinfo"); - mx_register(exp_version, 0, 0, "version"); - mx_register(exp_volume, 1, 1, "volume"); - mx_register(exp_when, 1, 1, "when"); - mx_register(exp_who, 1, 1, "who"); - mx_register_magic(exp_new, 1, 1, "new"); - mx_register_magic(exp_playing, 0, 1, "playing"); - mx_register_magic(exp_prefs, 2, 2, "prefs"); - mx_register_magic(exp_queue, 1, 1, "queue"); - mx_register_magic(exp_recent, 1, 1, "recent"); -} - -void disorder_macros_reset(void) { - /* Junk the old connection if there is one */ - if(client) - disorder_close(client); - /* Create a new connection */ - client = disorder_new(0); - /* Forget everything we knew */ - flags = 0; +void dcgi_expansions(void) { + mx_register("arg", 1, 1, exp_arg); + mx_register("enabled", 0, 0, exp_enabled); + mx_register("error", 0, 0, exp_error); + mx_register("image", 1, 1, exp_image); + mx_register("isnew", 0, 0, exp_isnew); + mx_register("isplaying", 0, 0, exp_isplaying); + mx_register("isplaying", 0, 0, exp_isqueue); + mx_register("isrecent", 0, 0, exp_isrecent); + mx_register("length", 1, 1, exp_length); + mx_register("movable", 1, 1, exp_movable); + mx_register("part", 2, 3, exp_part); + mx_register("paused", 0, 0, exp_paused); + mx_register("pref", 2, 2, exp_pref); + mx_register("quote", 1, 1, exp_quote); + mx_register("random-enabled", 0, 0, exp_random_enabled); + mx_register("removable", 1, 1, exp_removable); + mx_register("resolve", 1, 1, exp_resolve); + mx_register("server-version", 0, 0, exp_server_version); + mx_register("state", 1, 1, exp_state); + mx_register("thisurl", 0, 0, exp_thisurl); + mx_register("trackstate", 1, 1, exp_trackstate); + mx_register("transform", 2, 3, exp_transform); + mx_register("url", 0, 0, exp_url); + mx_register("user", 0, 0, exp_user); + mx_register("userinfo", 1, 1, exp_userinfo); + mx_register("version", 0, 0, exp_version); + mx_register("volume", 1, 1, exp_volume); + mx_register("when", 1, 1, exp_when); + mx_register("who", 1, 1, exp_who); + mx_register_magic("dirs", 2, 3, exp_dirs); + mx_register_magic("new", 1, 1, exp_new); + mx_register_magic("playing", 0, 1, exp_playing); + mx_register_magic("prefs", 2, 2, exp_prefs); + mx_register_magic("queue", 1, 1, exp_queue); + mx_register_magic("recent", 1, 1, exp_recent); + mx_register_magic("right", 1, 3, exp_right); + mx_register_magic("tracks", 2, 3, exp_tracks); } /*