3 * This file is part of DisOrder.
4 * Copyright (C) 2004-2008 Richard Kettlewell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file server/macros-disorder.c
22 * @brief DisOrder-specific expansions
25 #include "disorder-cgi.h"
27 /** @brief For error template */
28 const char *dcgi_error_string;
30 /** @brief For login template */
31 const char *dcgi_status_string;
33 /** @brief Return @p i as a string */
34 static const char *make_index(int i) {
37 byte_xasprintf(&s, "%d", i);
43 * Expands to the server's version string, or a (safe to use) error
44 * value if the server is unavailable or broken.
46 static int exp_server_version(int attribute((unused)) nargs,
47 char attribute((unused)) **args,
49 void attribute((unused)) *u) {
53 if(disorder_version(dcgi_client, (char **)&v))
54 v = "(cannot get version)";
56 v = "(server not running)";
57 return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
62 * Expands to the local version string.
64 static int exp_version(int attribute((unused)) nargs,
65 char attribute((unused)) **args,
67 void attribute((unused)) *u) {
68 return sink_writes(output,
69 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
74 * Expands to the base URL of the web interface.
76 static int exp_url(int attribute((unused)) nargs,
77 char attribute((unused)) **args,
79 void attribute((unused)) *u) {
80 return sink_writes(output,
81 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
86 * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if
87 * there is no such argument. Use @argq for a quick way to quote the argument.
89 static int exp_arg(int attribute((unused)) nargs,
92 void attribute((unused)) *u) {
93 const char *s = cgi_get(args[0]);
96 return sink_writes(output, s) < 0 ? -1 : 0;
103 * Expands to the (quoted) form of CGI argument NAME, or the empty string if
104 * there is no such argument. Use @arg for the unquoted argument.
106 static int exp_argq(int attribute((unused)) nargs,
109 void attribute((unused)) *u) {
110 const char *s = cgi_get(args[0]);
113 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
120 * Expands to the logged-in username (which might be "guest"), or to
121 * the empty string if not connected.
123 static int exp_user(int attribute((unused)) nargs,
124 char attribute((unused)) **args,
126 void attribute((unused)) *u) {
129 if(dcgi_client && (user = disorder_user(dcgi_client)))
130 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
134 /*! @part{TRACK|ID}{PART}{CONTEXT}
136 * Expands to a track name part.
138 * A track may be identified by name or by queue ID.
140 * CONTEXT may be omitted. If it is then 'display' is assumed.
142 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
143 * result truncated according to the length defined by the short_display
144 * configuration directive.
146 static int exp_part(int nargs,
149 void attribute((unused)) *u) {
150 const char *track = args[0], *part = args[1];
151 const char *context = nargs > 2 ? args[2] : "display";
154 if(track[0] != '/') {
155 struct queue_entry *q = dcgi_findtrack(track);
163 && !disorder_part(dcgi_client, (char **)&s,
165 !strcmp(context, "short") ? "display" : context,
167 if(!strcmp(context, "short"))
168 s = truncate_for_display(s, config->short_display);
169 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
176 * SGML-quotes STRING. Note that most expansion results are already suitable
177 * quoted, so this expansion is usually not required.
179 static int exp_quote(int attribute((unused)) nargs,
182 void attribute((unused)) *u) {
183 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
188 * Expands to the name of the submitter of track ID, which must be a playing
189 * track, in the queue, or in the recent list.
191 static int exp_who(int attribute((unused)) nargs,
194 void attribute((unused)) *u) {
195 struct queue_entry *q = dcgi_findtrack(args[0]);
197 if(q && q->submitter)
198 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
204 * Expands to the time a track started or is expected to start. The track must
205 * be a playing track, in the queue, or in the recent list.
207 static int exp_when(int attribute((unused)) nargs,
210 void attribute((unused)) *u) {
211 struct queue_entry *q = dcgi_findtrack(args[0]);
212 const struct tm *w = 0;
216 case playing_isscratch:
217 case playing_unplayed:
220 w = localtime(&q->expected);
223 case playing_no_player:
225 case playing_scratched:
226 case playing_started:
228 case playing_quitting:
230 w = localtime(&q->played);
234 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
236 return sink_writes(output, " ") < 0 ? -1 : 0;
239 /*! @length{ID|TRACK}
241 * Expands to the length of a track, identified by its queue ID or its name.
242 * If it is the playing track (identified by ID) then the amount played so far
245 static int exp_length(int attribute((unused)) nargs,
248 void attribute((unused)) *u) {
249 struct queue_entry *q;
253 if(args[0][0] == '/')
254 /* Track identified by name */
257 /* Track identified by queue ID */
258 if(!(q = dcgi_findtrack(args[0])))
260 if(q->state == playing_started || q->state == playing_paused)
261 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
265 if(dcgi_client && !disorder_length(dcgi_client, name, &length))
266 return sink_printf(output, "%ld:%02ld",
267 length / 60, length % 60) < 0 ? -1 : 0;
268 return sink_writes(output, " ") < 0 ? -1 : 0;
273 * Expands to "true" if track ID is removable (or scratchable, if it is the
274 * playing track) and "false" otherwise.
276 static int exp_removable(int attribute((unused)) nargs,
279 void attribute((unused)) *u) {
280 struct queue_entry *q = dcgi_findtrack(args[0]);
281 /* TODO would be better to reject recent */
283 if(!q || !dcgi_client)
284 return mx_bool_result(output, 0);
285 dcgi_lookup(DCGI_RIGHTS);
286 return mx_bool_result(output,
287 (q == dcgi_playing ? right_scratchable : right_removable)
288 (dcgi_rights, disorder_user(dcgi_client), q));
291 /*! @movable{ID}{DIR}
293 * Expands to "true" if track ID is movable and "false" otherwise.
295 * DIR (which is optional) should be a non-zero integer. If it is negative
296 * then the intended move is down (later in the queue), if it is positive then
297 * the intended move is up (earlier in the queue). The first track is not
298 * movable up and the last track not movable down.
300 static int exp_movable(int nargs,
303 void attribute((unused)) *u) {
304 struct queue_entry *q = dcgi_findtrack(args[0]);
305 /* TODO would be better to recent playing/recent */
307 if(!q || !dcgi_client)
308 return mx_bool_result(output, 0);
310 const long dir = atoi(args[1]);
312 if(dir > 0 && q == dcgi_queue)
313 return mx_bool_result(output, 0);
314 if(dir < 0 && q->next == 0)
315 return mx_bool_result(output, 0);
317 dcgi_lookup(DCGI_RIGHTS);
318 return mx_bool_result(output,
319 right_movable(dcgi_rights,
320 disorder_user(dcgi_client),
324 /*! @playing{TEMPLATE}
326 * Expands to TEMPLATE, with the following expansions:
327 * - @id: the queue ID of the playing track
328 * - @track: the playing track's
331 * If no track is playing expands to nothing.
333 * TEMPLATE is optional. If it is left out then instead expands to the queue
334 * ID of the playing track.
336 static int exp_playing(int nargs,
337 const struct mx_node **args,
340 dcgi_lookup(DCGI_PLAYING);
344 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
345 return mx_expand(mx_rewritel(args[0],
346 "id", dcgi_playing->id,
347 "track", dcgi_playing->track,
354 * For each track in the queue, expands TEMPLATE with the following expansions:
355 * - @id: the queue ID of the track
356 * - @track: the UNQUOTED track name
357 * - @index: the track number from 0
358 * - @parity: "even" or "odd" alternately
359 * - @first: "true" on the first track and "false" otherwise
360 * - @last: "true" on the last track and "false" otherwise
362 static int exp_queue(int attribute((unused)) nargs,
363 const struct mx_node **args,
366 struct queue_entry *q;
369 dcgi_lookup(DCGI_QUEUE);
370 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
371 if((rc = mx_expand(mx_rewritel(args[0],
374 "index", make_index(i),
375 "parity", i % 2 ? "odd" : "even",
376 "first", q == dcgi_queue ? "true" : "false",
377 "last", q->next ? "false" : "true",
384 /*! @recent{TEMPLATE}
386 * For each track in the recently played list, expands TEMPLATE with the
387 * following expansions:
388 * - @id: the queue ID of the track
389 * - @track: the UNQUOTED track name
390 * - @index: the track number from 0
391 * - @parity: "even" or "odd" alternately
392 * - @first: "true" on the first track and "false" otherwise
393 * - @last: "true" on the last track and "false" otherwise
395 static int exp_recent(int attribute((unused)) nargs,
396 const struct mx_node **args,
399 struct queue_entry *q;
402 dcgi_lookup(DCGI_RECENT);
403 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
404 if((rc = mx_expand(mx_rewritel(args[0],
407 "index", make_index(i),
408 "parity", i % 2 ? "odd" : "even",
409 "first", q == dcgi_recent ? "true" : "false",
410 "last", q->next ? "false" : "true",
419 * For each track in the newly added list, expands TEMPLATE wit the following
421 * - @track: the UNQUOTED track name
422 * - @index: the track number from 0
423 * - @parity: "even" or "odd" alternately
424 * - @first: "true" on the first track and "false" otherwise
425 * - @last: "true" on the last track and "false" otherwise
427 * Note that unlike @playing, @queue and @recent which are otherwise
428 * superficially similar, there is no @id sub-expansion here.
430 static int exp_new(int attribute((unused)) nargs,
431 const struct mx_node **args,
436 dcgi_lookup(DCGI_NEW);
437 /* TODO perhaps we should generate an ID value for tracks in the new list */
438 for(i = 0; i < dcgi_nnew; ++i)
439 if((rc = mx_expand(mx_rewritel(args[0],
440 "track", dcgi_new[i],
441 "index", make_index(i),
442 "parity", i % 2 ? "odd" : "even",
443 "first", i == 0 ? "true" : "false",
444 "last", i == dcgi_nnew - 1 ? "false" : "true",
453 * Expands to the volume in a given channel. CHANNEL must be "left" or
456 static int exp_volume(int attribute((unused)) nargs,
459 void attribute((unused)) *u) {
460 dcgi_lookup(DCGI_VOLUME);
461 return sink_printf(output, "%d",
462 !strcmp(args[0], "left")
463 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
468 * Expands to "true" if there is a playing track, otherwise "false".
470 static int exp_isplaying(int attribute((unused)) nargs,
471 char attribute((unused)) **args,
473 void attribute((unused)) *u) {
474 dcgi_lookup(DCGI_PLAYING);
475 return mx_bool_result(output, !!dcgi_playing);
480 * Expands to "true" if there the queue is nonempty, otherwise "false".
482 static int exp_isqueue(int attribute((unused)) nargs,
483 char attribute((unused)) **args,
485 void attribute((unused)) *u) {
486 dcgi_lookup(DCGI_QUEUE);
487 return mx_bool_result(output, !!dcgi_queue);
492 * Expands to "true" if there the recently played list is nonempty, otherwise
495 static int exp_isrecent(int attribute((unused)) nargs,
496 char attribute((unused)) **args,
498 void attribute((unused)) *u) {
499 dcgi_lookup(DCGI_RECENT);
500 return mx_bool_result(output, !!dcgi_recent);
505 * Expands to "true" if there the newly added track list is nonempty, otherwise
508 static int exp_isnew(int attribute((unused)) nargs,
509 char attribute((unused)) **args,
511 void attribute((unused)) *u) {
512 dcgi_lookup(DCGI_NEW);
513 return mx_bool_result(output, !!dcgi_nnew);
516 /*! @pref{TRACK}{KEY}
518 * Expands to a track preference.
520 static int exp_pref(int attribute((unused)) nargs,
523 void attribute((unused)) *u) {
526 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
527 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
531 /*! @prefs{TRACK}{TEMPLATE}
533 * For each track preference of track TRACK, expands TEMPLATE with the
534 * following expansions:
535 * - @name: the UNQUOTED preference name
536 * - @index: the preference number from 0
537 * - @value: the UNQUOTED preference value
538 * - @parity: "even" or "odd" alternately
539 * - @first: "true" on the first preference and "false" otherwise
540 * - @last: "true" on the last preference and "false" otherwise
542 * Use @quote to quote preference names and values where necessary; see below.
544 static int exp_prefs(int attribute((unused)) nargs,
545 const struct mx_node **args,
549 struct kvp *k, *head;
552 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
554 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
556 for(k = head, i = 0; k; k = k->next, ++i)
557 if((rc = mx_expand(mx_rewritel(args[1],
558 "index", make_index(i),
559 "parity", i % 2 ? "odd" : "even",
562 "first", k == head ? "true" : "false",
563 "last", k->next ? "false" : "true",
570 /*! @transform{TRACK}{TYPE}{CONTEXT}
572 * Transforms a track name (if TYPE is "track") or directory name (if type is
573 * "dir"). CONTEXT should be the context, if it is left out then "display" is
576 static int exp_transform(int nargs,
579 void attribute((unused)) *u) {
580 const char *t = trackname_transform(args[1], args[0],
581 (nargs > 2 ? args[2] : "display"));
582 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
587 * Expands to "true" if playing is enabled, otherwise "false".
589 static int exp_enabled(int attribute((unused)) nargs,
590 char attribute((unused)) **args,
592 void attribute((unused)) *u) {
596 disorder_enabled(dcgi_client, &e);
597 return mx_bool_result(output, e);
602 * Expands to "true" if random play is enabled, otherwise "false".
604 static int exp_random_enabled(int attribute((unused)) nargs,
605 char attribute((unused)) **args,
607 void attribute((unused)) *u) {
611 disorder_random_enabled(dcgi_client, &e);
612 return mx_bool_result(output, e);
615 /*! @trackstate{TRACK}
617 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
618 * the queue, otherwise to nothing.
620 static int exp_trackstate(int attribute((unused)) nargs,
623 void attribute((unused)) *u) {
625 struct queue_entry *q;
629 if(disorder_resolve(dcgi_client, &track, args[0]))
631 dcgi_lookup(DCGI_PLAYING);
632 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
633 return sink_writes(output, "playing") < 0 ? -1 : 0;
634 dcgi_lookup(DCGI_QUEUE);
635 for(q = dcgi_queue; q; q = q->next)
636 if(!strcmp(track, q->track))
637 return sink_writes(output, "queued") < 0 ? -1 : 0;
643 * Expands to an UNQUOTED URL which points back to the current page. (NB it
644 * might not be byte for byte identical - for instance, CGI arguments might be
647 static int exp_thisurl(int attribute((unused)) nargs,
648 char attribute((unused)) **args,
650 void attribute((unused)) *u) {
651 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
656 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
657 * nothing if it is not a valid track.
659 static int exp_resolve(int attribute((unused)) nargs,
662 void attribute((unused)) *u) {
665 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
666 return sink_writes(output, r) < 0 ? -1 : 0;
672 * Expands to "true" if the playing track is paused, to "false" if it is
673 * playing (or if there is no playing track at all).
675 static int exp_paused(int attribute((unused)) nargs,
676 char attribute((unused)) **args,
678 void attribute((unused)) *u) {
679 dcgi_lookup(DCGI_PLAYING);
680 return mx_bool_result(output, (dcgi_playing
681 && dcgi_playing->state == playing_paused));
686 * Expands to the current state of track ID.
688 static int exp_state(int attribute((unused)) nargs,
691 void attribute((unused)) *u) {
692 struct queue_entry *q = dcgi_findtrack(args[0]);
695 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
699 /*! @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
701 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
702 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
704 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
705 * the user has the right and "false" otherwise.
707 * If there is no connection to the server then expands to nothing (in all
710 static int exp_right(int nargs,
711 const struct mx_node **args,
720 dcgi_lookup(DCGI_RIGHTS);
721 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
723 if(parse_rights(right, &r, 1/*report*/))
725 /* Single-argument form */
727 return mx_bool_result(output, !!(r & dcgi_rights));
728 /* Multiple argument form */
730 return mx_expand(args[1], output, u);
732 return mx_expand(args[2], output, u);
736 /*! @userinfo{PROPERTY}
738 * Expands to the named property of the current user.
740 static int exp_userinfo(int attribute((unused)) nargs,
743 void attribute((unused)) *u) {
747 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
749 return sink_writes(output, v) < 0 ? -1 : 0;
755 * Expands to the latest error string.
757 static int exp_error(int attribute((unused)) nargs,
758 char attribute((unused)) **args,
760 void attribute((unused)) *u) {
761 return sink_writes(output, dcgi_error_string ? dcgi_error_string : "")
767 * Expands to the latest status string.
769 static int exp_status(int attribute((unused)) nargs,
770 char attribute((unused)) **args,
772 void attribute((unused)) *u) {
773 return sink_writes(output, dcgi_status_string ? dcgi_status_string : "")
779 * Expands to the URL of the image called NAME.
781 * Firstly if there is a label called images.NAME then the image stem will be
782 * the value of that label. Otherwise the stem will be NAME.png.
784 * If the label url.static exists then it will give the base URL for images.
785 * Otherwise the base url will be /disorder/.
787 static int exp_image(int attribute((unused)) nargs,
790 void attribute((unused)) *u) {
791 const char *url, *stem;
794 /* Compute the stem */
795 byte_xasprintf(&labelname, "images.%s", args[0]);
796 if(option_label_exists(labelname))
797 stem = option_label(labelname);
799 byte_xasprintf((char **)&stem, "%s.png", args[0]);
800 /* If the stem looks like it's reasonalby complete, use that */
802 || !strncmp(stem, "http:", 5)
803 || !strncmp(stem, "https:", 6))
806 /* Compute the URL */
807 if(option_label_exists("url.static"))
808 byte_xasprintf((char **)&url, "%s/%s",
809 option_label("url.static"), stem);
811 /* Default base is /disorder */
812 byte_xasprintf((char **)&url, "/disorder/%s", stem);
814 return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
817 /** @brief Compare two @ref entry objects */
818 int dcgi_compare_entry(const void *a, const void *b) {
819 const struct dcgi_entry *ea = a, *eb = b;
821 return compare_tracks(ea->sort, eb->sort,
822 ea->display, eb->display,
823 ea->track, eb->track);
826 /** @brief Implementation of exp_tracks() and exp_dirs() */
827 static int exp__files_dirs(int nargs,
828 const struct mx_node **args,
832 int (*fn)(disorder_client *client,
837 char **tracks, *dir, *re;
839 const struct mx_node *m;
840 struct dcgi_entry *e;
842 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
845 if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
855 if(fn(dcgi_client, dir, re, &tracks, &ntracks))
857 /* Sort it. NB trackname_transform() does not go to the server. */
858 e = xcalloc(ntracks, sizeof *e);
859 for(n = 0; n < ntracks; ++n) {
860 e[n].track = tracks[n];
861 e[n].sort = trackname_transform(type, tracks[n], "sort");
862 e[n].display = trackname_transform(type, tracks[n], "display");
864 qsort(e, ntracks, sizeof (struct dcgi_entry), dcgi_compare_entry);
865 /* Expand the subsiduary templates. We chuck in @sort and @display because
866 * it is particularly easy to do so. */
867 for(n = 0; n < ntracks; ++n)
868 if((rc = mx_expand(mx_rewritel(m,
869 "index", make_index(n),
870 "parity", n % 2 ? "odd" : "even",
872 "first", n == 0 ? "true" : "false",
873 "last", n + 1 == ntracks ? "false" : "true",
875 "display", e[n].display,
883 /*! @tracks{DIR}{RE}{TEMPLATE}
885 * For each track below DIR, expands TEMPLATE with the
886 * following expansions:
887 * - @track: the UNQUOTED track name
888 * - @index: the track number from 0
889 * - @parity: "even" or "odd" alternately
890 * - @first: "true" on the first track and "false" otherwise
891 * - @last: "true" on the last track and "false" otherwise
892 * - @sort: the sort key for this track
893 * - @display: the UNQUOTED display string for this track
895 * RE is optional and if present is the regexp to match against.
897 static int exp_tracks(int nargs,
898 const struct mx_node **args,
901 return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
904 /*! @dirs{DIR}{RE}{TEMPLATE}
906 * For each directory below DIR, expands TEMPLATE with the
907 * following expansions:
908 * - @track: the UNQUOTED directory name
909 * - @index: the directory number from 0
910 * - @parity: "even" or "odd" alternately
911 * - @first: "true" on the first directory and "false" otherwise
912 * - @last: "true" on the last directory and "false" otherwise
913 * - @sort: the sort key for this directory
914 * - @display: the UNQUOTED display string for this directory
916 * RE is optional and if present is the regexp to match against.
918 static int exp_dirs(int nargs,
919 const struct mx_node **args,
922 return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
925 static int exp__search_shim(disorder_client *c, const char *terms,
926 const char attribute((unused)) *re,
927 char ***vecp, int *nvecp) {
928 return disorder_search(c, terms, vecp, nvecp);
931 /*! @search{KEYWORDS}{TEMPLATE}
933 * For each track matching KEYWORDS, expands TEMPLATE with the
934 * following expansions:
935 * - @track: the UNQUOTED directory name
936 * - @index: the directory number from 0
937 * - @parity: "even" or "odd" alternately
938 * - @first: "true" on the first directory and "false" otherwise
939 * - @last: "true" on the last directory and "false" otherwise
940 * - @sort: the sort key for this track
941 * - @display: the UNQUOTED display string for this track
943 static int exp_search(int nargs,
944 const struct mx_node **args,
947 return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
952 * Expands to label NAME from options.labels. Undefined lables expand to the
953 * last dot-separated component, e.g. X.Y.Z to Z.
955 static int exp_label(int attribute((unused)) nargs,
958 void attribute((unused)) *u) {
959 return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0;
962 /*! @breadcrumbs{DIR}{TEMPLATE}
964 * Expands TEMPLATE for each directory in the path up to DIR, excluding the root
965 * but including DIR itself, with the following expansions:
966 * - @dir: the directory
967 * - @last: "true" if this is the last directory, otherwise "false"
969 * DIR must be an absolute path.
971 static int exp_breadcrumbs(int attribute((unused)) nargs,
972 const struct mx_node **args,
974 void attribute((unused)) *u) {
976 char *dir, *parent, *ptr;
978 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
980 /* Reject relative paths */
982 error(0, "breadcrumbs: '%s' is a relative path", dir);
988 /* Find the end of this directory */
989 while(*ptr && *ptr != '/')
991 parent = xstrndup(dir, ptr - dir);
992 if((rc = mx_expand(mx_rewritel(args[1],
994 "last", *ptr ? "false" : "true",
1004 /** @brief Register DisOrder-specific expansions */
1005 void dcgi_expansions(void) {
1006 mx_register("arg", 1, 1, exp_arg);
1007 mx_register("argq", 1, 1, exp_argq);
1008 mx_register("enabled", 0, 0, exp_enabled);
1009 mx_register("error", 0, 0, exp_error);
1010 mx_register("image", 1, 1, exp_image);
1011 mx_register("isnew", 0, 0, exp_isnew);
1012 mx_register("isplaying", 0, 0, exp_isplaying);
1013 mx_register("isqueue", 0, 0, exp_isqueue);
1014 mx_register("isrecent", 0, 0, exp_isrecent);
1015 mx_register("label", 1, 1, exp_label);
1016 mx_register("length", 1, 1, exp_length);
1017 mx_register("movable", 1, 2, exp_movable);
1018 mx_register("part", 2, 3, exp_part);
1019 mx_register("paused", 0, 0, exp_paused);
1020 mx_register("pref", 2, 2, exp_pref);
1021 mx_register("quote", 1, 1, exp_quote);
1022 mx_register("random-enabled", 0, 0, exp_random_enabled);
1023 mx_register("removable", 1, 1, exp_removable);
1024 mx_register("resolve", 1, 1, exp_resolve);
1025 mx_register("server-version", 0, 0, exp_server_version);
1026 mx_register("state", 1, 1, exp_state);
1027 mx_register("status", 0, 0, exp_status);
1028 mx_register("thisurl", 0, 0, exp_thisurl);
1029 mx_register("trackstate", 1, 1, exp_trackstate);
1030 mx_register("transform", 2, 3, exp_transform);
1031 mx_register("url", 0, 0, exp_url);
1032 mx_register("user", 0, 0, exp_user);
1033 mx_register("userinfo", 1, 1, exp_userinfo);
1034 mx_register("version", 0, 0, exp_version);
1035 mx_register("volume", 1, 1, exp_volume);
1036 mx_register("when", 1, 1, exp_when);
1037 mx_register("who", 1, 1, exp_who);
1038 mx_register_magic("breadcrumbs", 2, 2, exp_breadcrumbs);
1039 mx_register_magic("dirs", 2, 3, exp_dirs);
1040 mx_register_magic("new", 1, 1, exp_new);
1041 mx_register_magic("playing", 0, 1, exp_playing);
1042 mx_register_magic("prefs", 2, 2, exp_prefs);
1043 mx_register_magic("queue", 1, 1, exp_queue);
1044 mx_register_magic("recent", 1, 1, exp_recent);
1045 mx_register_magic("right", 1, 3, exp_right);
1046 mx_register_magic("search", 2, 2, exp_search);
1047 mx_register_magic("tracks", 2, 3, exp_tracks);
1055 indent-tabs-mode:nil