2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file cgi/macros-disorder.c
19 * @brief DisOrder-specific expansions
21 * See @ref lib/macros-builtin.c for docstring syntax.
24 #include "disorder-cgi.h"
26 /** @brief For error template */
27 const char *dcgi_error_string;
29 /** @brief For login template */
30 const char *dcgi_status_string;
32 /** @brief Return @p i as a string */
33 static const char *make_index(int i) {
36 byte_xasprintf(&s, "%d", i);
42 * Expands to the server's version string, or a (safe to use) error
43 * value if the server is unavailable or broken.
45 static int exp_server_version(int attribute((unused)) nargs,
46 char attribute((unused)) **args,
48 void attribute((unused)) *u) {
52 if(disorder_version(dcgi_client, (char **)&v))
53 v = "(cannot get version)";
55 v = "(server not running)";
56 return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
61 * Expands to the local version string.
63 static int exp_version(int attribute((unused)) nargs,
64 char attribute((unused)) **args,
66 void attribute((unused)) *u) {
67 return sink_writes(output,
68 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
73 * Expands to the base URL of the web interface.
75 static int exp_url(int attribute((unused)) nargs,
76 char attribute((unused)) **args,
78 void attribute((unused)) *u) {
79 return sink_writes(output,
80 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
85 * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if
86 * there is no such argument. Use @argq for a quick way to quote the argument.
88 static int exp_arg(int attribute((unused)) nargs,
91 void attribute((unused)) *u) {
92 const char *s = cgi_get(args[0]);
95 return sink_writes(output, s) < 0 ? -1 : 0;
102 * Expands to the (quoted) form of CGI argument NAME, or the empty string if
103 * there is no such argument. Use @arg for the unquoted argument.
105 static int exp_argq(int attribute((unused)) nargs,
108 void attribute((unused)) *u) {
109 const char *s = cgi_get(args[0]);
112 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
119 * Expands to the logged-in username (which might be "guest"), or to
120 * the empty string if not connected.
122 static int exp_user(int attribute((unused)) nargs,
123 char attribute((unused)) **args,
125 void attribute((unused)) *u) {
128 if(dcgi_client && (user = disorder_user(dcgi_client)))
129 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
133 /*$ @part{TRACK|ID}{PART}{CONTEXT}
135 * Expands to a track name part.
137 * A track may be identified by name or by queue ID.
139 * CONTEXT may be omitted. If it is then 'display' is assumed.
141 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
142 * result truncated according to the length defined by the short_display
143 * configuration directive.
145 static int exp_part(int nargs,
148 void attribute((unused)) *u) {
149 const char *track = args[0], *part = args[1];
150 const char *context = nargs > 2 ? args[2] : "display";
153 if(track[0] != '/') {
154 struct queue_entry *q = dcgi_findtrack(track);
162 && !disorder_part(dcgi_client, (char **)&s,
164 !strcmp(context, "short") ? "display" : context,
166 if(!strcmp(context, "short"))
167 s = truncate_for_display(s, config->short_display);
168 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
175 * SGML-quotes STRING. Note that most expansion results are already suitable
176 * quoted, so this expansion is usually not required.
178 static int exp_quote(int attribute((unused)) nargs,
181 void attribute((unused)) *u) {
182 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
187 * Expands to the name of the submitter of track ID, which must be a playing
188 * track, in the queue, or in the recent list.
190 static int exp_who(int attribute((unused)) nargs,
193 void attribute((unused)) *u) {
194 struct queue_entry *q = dcgi_findtrack(args[0]);
196 if(q && q->submitter)
197 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
203 * Expands to the time a track started or is expected to start. The track must
204 * be a playing track, in the queue, or in the recent list.
206 static int exp_when(int attribute((unused)) nargs,
209 void attribute((unused)) *u) {
210 struct queue_entry *q = dcgi_findtrack(args[0]);
211 const struct tm *w = 0;
215 case playing_isscratch:
216 case playing_unplayed:
219 w = localtime(&q->expected);
222 case playing_no_player:
224 case playing_scratched:
225 case playing_started:
227 case playing_quitting:
229 w = localtime(&q->played);
233 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
235 return sink_writes(output, " ") < 0 ? -1 : 0;
238 /*$ @length{ID|TRACK}
240 * Expands to the length of a track, identified by its queue ID or its name.
241 * If it is the playing track (identified by ID) then the amount played so far
244 static int exp_length(int attribute((unused)) nargs,
247 void attribute((unused)) *u) {
248 struct queue_entry *q;
252 if(args[0][0] == '/')
253 /* Track identified by name */
256 /* Track identified by queue ID */
257 if(!(q = dcgi_findtrack(args[0])))
259 if(q->state == playing_started || q->state == playing_paused)
260 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
264 if(dcgi_client && !disorder_length(dcgi_client, name, &length))
265 return sink_printf(output, "%ld:%02ld",
266 length / 60, length % 60) < 0 ? -1 : 0;
267 return sink_writes(output, " ") < 0 ? -1 : 0;
272 * Expands to "true" if track ID is removable (or scratchable, if it is the
273 * playing track) and "false" otherwise.
275 static int exp_removable(int attribute((unused)) nargs,
278 void attribute((unused)) *u) {
279 struct queue_entry *q = dcgi_findtrack(args[0]);
280 /* TODO would be better to reject recent */
282 if(!q || !dcgi_client)
283 return mx_bool_result(output, 0);
284 dcgi_lookup(DCGI_RIGHTS);
285 return mx_bool_result(output,
286 (q == dcgi_playing ? right_scratchable : right_removable)
287 (dcgi_rights, disorder_user(dcgi_client), q));
290 /*$ @movable{ID}{DIR}
292 * Expands to "true" if track ID is movable and "false" otherwise.
294 * DIR (which is optional) should be a non-zero integer. If it is negative
295 * then the intended move is down (later in the queue), if it is positive then
296 * the intended move is up (earlier in the queue). The first track is not
297 * movable up and the last track not movable down.
299 static int exp_movable(int nargs,
302 void attribute((unused)) *u) {
303 struct queue_entry *q = dcgi_findtrack(args[0]);
304 /* TODO would be better to recent playing/recent */
306 if(!q || !dcgi_client)
307 return mx_bool_result(output, 0);
309 const long dir = atoi(args[1]);
311 if(dir > 0 && q == dcgi_queue)
312 return mx_bool_result(output, 0);
313 if(dir < 0 && q->next == 0)
314 return mx_bool_result(output, 0);
316 dcgi_lookup(DCGI_RIGHTS);
317 return mx_bool_result(output,
318 right_movable(dcgi_rights,
319 disorder_user(dcgi_client),
323 /*$ @playing{TEMPLATE}
325 * Expands to TEMPLATE, with the following expansions:
326 * - @id: the queue ID of the playing track
327 * - @track: the playing track's
330 * If no track is playing expands to nothing.
332 * TEMPLATE is optional. If it is left out then instead expands to the queue
333 * ID of the playing track.
335 static int exp_playing(int nargs,
336 const struct mx_node **args,
339 dcgi_lookup(DCGI_PLAYING);
343 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
344 return mx_expand(mx_rewritel(args[0],
345 "id", dcgi_playing->id,
346 "track", dcgi_playing->track,
353 * For each track in the queue, expands TEMPLATE with the following expansions:
354 * - @id: the queue ID of the track
355 * - @track: the UNQUOTED track name
356 * - @index: the track number from 0
357 * - @parity: "even" or "odd" alternately
358 * - @first: "true" on the first track and "false" otherwise
359 * - @last: "true" on the last track and "false" otherwise
361 static int exp_queue(int attribute((unused)) nargs,
362 const struct mx_node **args,
365 struct queue_entry *q;
368 dcgi_lookup(DCGI_QUEUE);
369 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
370 if((rc = mx_expand(mx_rewritel(args[0],
373 "index", make_index(i),
374 "parity", i % 2 ? "odd" : "even",
375 "first", q == dcgi_queue ? "true" : "false",
376 "last", q->next ? "false" : "true",
383 /*$ @recent{TEMPLATE}
385 * For each track in the recently played list, expands TEMPLATE with the
386 * following expansions:
387 * - @id: the queue ID of the track
388 * - @track: the UNQUOTED track name
389 * - @index: the track number from 0
390 * - @parity: "even" or "odd" alternately
391 * - @first: "true" on the first track and "false" otherwise
392 * - @last: "true" on the last track and "false" otherwise
394 static int exp_recent(int attribute((unused)) nargs,
395 const struct mx_node **args,
398 struct queue_entry *q;
401 dcgi_lookup(DCGI_RECENT);
402 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
403 if((rc = mx_expand(mx_rewritel(args[0],
406 "index", make_index(i),
407 "parity", i % 2 ? "odd" : "even",
408 "first", q == dcgi_recent ? "true" : "false",
409 "last", q->next ? "false" : "true",
418 * For each track in the newly added list, expands TEMPLATE wit the following
420 * - @track: the UNQUOTED track name
421 * - @index: the track number from 0
422 * - @parity: "even" or "odd" alternately
423 * - @first: "true" on the first track and "false" otherwise
424 * - @last: "true" on the last track and "false" otherwise
426 * Note that unlike @playing, @queue and @recent which are otherwise
427 * superficially similar, there is no @id sub-expansion here.
429 static int exp_new(int attribute((unused)) nargs,
430 const struct mx_node **args,
435 dcgi_lookup(DCGI_NEW);
436 /* TODO perhaps we should generate an ID value for tracks in the new list */
437 for(i = 0; i < dcgi_nnew; ++i)
438 if((rc = mx_expand(mx_rewritel(args[0],
439 "track", dcgi_new[i],
440 "index", make_index(i),
441 "parity", i % 2 ? "odd" : "even",
442 "first", i == 0 ? "true" : "false",
443 "last", i == dcgi_nnew - 1 ? "false" : "true",
452 * Expands to the volume in a given channel. CHANNEL must be "left" or
455 static int exp_volume(int attribute((unused)) nargs,
458 void attribute((unused)) *u) {
459 dcgi_lookup(DCGI_VOLUME);
460 return sink_printf(output, "%d",
461 !strcmp(args[0], "left")
462 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
467 * Expands to "true" if there is a playing track, otherwise "false".
469 static int exp_isplaying(int attribute((unused)) nargs,
470 char attribute((unused)) **args,
472 void attribute((unused)) *u) {
473 dcgi_lookup(DCGI_PLAYING);
474 return mx_bool_result(output, !!dcgi_playing);
479 * Expands to "true" if there the queue is nonempty, otherwise "false".
481 static int exp_isqueue(int attribute((unused)) nargs,
482 char attribute((unused)) **args,
484 void attribute((unused)) *u) {
485 dcgi_lookup(DCGI_QUEUE);
486 return mx_bool_result(output, !!dcgi_queue);
491 * Expands to "true" if there the recently played list is nonempty, otherwise
494 static int exp_isrecent(int attribute((unused)) nargs,
495 char attribute((unused)) **args,
497 void attribute((unused)) *u) {
498 dcgi_lookup(DCGI_RECENT);
499 return mx_bool_result(output, !!dcgi_recent);
504 * Expands to "true" if there the newly added track list is nonempty, otherwise
507 static int exp_isnew(int attribute((unused)) nargs,
508 char attribute((unused)) **args,
510 void attribute((unused)) *u) {
511 dcgi_lookup(DCGI_NEW);
512 return mx_bool_result(output, !!dcgi_nnew);
515 /*$ @pref{TRACK}{KEY}
517 * Expands to a track preference.
519 static int exp_pref(int attribute((unused)) nargs,
522 void attribute((unused)) *u) {
525 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
526 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
530 /*$ @prefs{TRACK}{TEMPLATE}
532 * For each track preference of track TRACK, expands TEMPLATE with the
533 * following expansions:
534 * - @name: the UNQUOTED preference name
535 * - @index: the preference number from 0
536 * - @value: the UNQUOTED preference value
537 * - @parity: "even" or "odd" alternately
538 * - @first: "true" on the first preference and "false" otherwise
539 * - @last: "true" on the last preference and "false" otherwise
541 * Use @quote to quote preference names and values where necessary; see below.
543 static int exp_prefs(int attribute((unused)) nargs,
544 const struct mx_node **args,
548 struct kvp *k, *head;
551 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
553 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
555 for(k = head, i = 0; k; k = k->next, ++i)
556 if((rc = mx_expand(mx_rewritel(args[1],
557 "index", make_index(i),
558 "parity", i % 2 ? "odd" : "even",
561 "first", k == head ? "true" : "false",
562 "last", k->next ? "false" : "true",
569 /*$ @transform{TRACK}{TYPE}{CONTEXT}
571 * Transforms a track name (if TYPE is "track") or directory name (if type is
572 * "dir"). CONTEXT should be the context, if it is left out then "display" is
575 static int exp_transform(int nargs,
578 void attribute((unused)) *u) {
579 const char *t = trackname_transform(args[1], args[0],
580 (nargs > 2 ? args[2] : "display"));
581 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
586 * Expands to "true" if playing is enabled, otherwise "false".
588 static int exp_enabled(int attribute((unused)) nargs,
589 char attribute((unused)) **args,
591 void attribute((unused)) *u) {
595 disorder_enabled(dcgi_client, &e);
596 return mx_bool_result(output, e);
601 * Expands to "true" if random play is enabled, otherwise "false".
603 static int exp_random_enabled(int attribute((unused)) nargs,
604 char attribute((unused)) **args,
606 void attribute((unused)) *u) {
610 disorder_random_enabled(dcgi_client, &e);
611 return mx_bool_result(output, e);
614 /*$ @trackstate{TRACK}
616 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
617 * the queue, otherwise to nothing.
619 static int exp_trackstate(int attribute((unused)) nargs,
622 void attribute((unused)) *u) {
624 struct queue_entry *q;
628 if(disorder_resolve(dcgi_client, &track, args[0]))
630 dcgi_lookup(DCGI_PLAYING);
631 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
632 return sink_writes(output, "playing") < 0 ? -1 : 0;
633 dcgi_lookup(DCGI_QUEUE);
634 for(q = dcgi_queue; q; q = q->next)
635 if(!strcmp(track, q->track))
636 return sink_writes(output, "queued") < 0 ? -1 : 0;
642 * Expands to an UNQUOTED URL which points back to the current page. (NB it
643 * might not be byte for byte identical - for instance, CGI arguments might be
646 static int exp_thisurl(int attribute((unused)) nargs,
647 char attribute((unused)) **args,
649 void attribute((unused)) *u) {
650 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
655 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
656 * nothing if it is not a valid track.
658 static int exp_resolve(int attribute((unused)) nargs,
661 void attribute((unused)) *u) {
664 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
665 return sink_writes(output, r) < 0 ? -1 : 0;
671 * Expands to "true" if the playing track is paused, to "false" if it is
672 * playing (or if there is no playing track at all).
674 static int exp_paused(int attribute((unused)) nargs,
675 char attribute((unused)) **args,
677 void attribute((unused)) *u) {
678 dcgi_lookup(DCGI_PLAYING);
679 return mx_bool_result(output, (dcgi_playing
680 && dcgi_playing->state == playing_paused));
685 * Expands to the current state of track ID.
687 static int exp_state(int attribute((unused)) nargs,
690 void attribute((unused)) *u) {
691 struct queue_entry *q = dcgi_findtrack(args[0]);
694 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
698 /*$ @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
700 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
701 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
703 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
704 * the user has the right and "false" otherwise.
706 * If there is no connection to the server then expands to nothing (in all
709 static int exp_right(int nargs,
710 const struct mx_node **args,
719 dcgi_lookup(DCGI_RIGHTS);
720 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
722 if(parse_rights(right, &r, 1/*report*/))
724 /* Single-argument form */
726 return mx_bool_result(output, !!(r & dcgi_rights));
727 /* Multiple argument form */
729 return mx_expand(args[1], output, u);
731 return mx_expand(args[2], output, u);
735 /*$ @userinfo{PROPERTY}
737 * Expands to the named property of the current user.
739 static int exp_userinfo(int attribute((unused)) nargs,
742 void attribute((unused)) *u) {
746 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
748 return sink_writes(output, v) < 0 ? -1 : 0;
754 * Expands to the latest error string.
756 static int exp_error(int attribute((unused)) nargs,
757 char attribute((unused)) **args,
759 void attribute((unused)) *u) {
760 return sink_writes(output, dcgi_error_string ? dcgi_error_string : "")
766 * Expands to the latest status string.
768 static int exp_status(int attribute((unused)) nargs,
769 char attribute((unused)) **args,
771 void attribute((unused)) *u) {
772 return sink_writes(output, dcgi_status_string ? dcgi_status_string : "")
778 * Expands to the URL of the image called NAME.
780 * Firstly if there is a label called images.NAME then the image stem will be
781 * the value of that label. Otherwise the stem will be NAME.png.
783 * If the label url.static exists then it will give the base URL for images.
784 * Otherwise the base url will be /disorder/.
786 static int exp_image(int attribute((unused)) nargs,
789 void attribute((unused)) *u) {
790 const char *url, *stem;
793 /* Compute the stem */
794 byte_xasprintf(&labelname, "images.%s", args[0]);
795 if(option_label_exists(labelname))
796 stem = option_label(labelname);
798 byte_xasprintf((char **)&stem, "%s.png", args[0]);
799 /* If the stem looks like it's reasonalby complete, use that */
801 || !strncmp(stem, "http:", 5)
802 || !strncmp(stem, "https:", 6))
805 /* Compute the URL */
806 if(option_label_exists("url.static"))
807 byte_xasprintf((char **)&url, "%s/%s",
808 option_label("url.static"), stem);
810 /* Default base is /disorder */
811 byte_xasprintf((char **)&url, "/disorder/%s", stem);
813 return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
816 /** @brief Implementation of exp_tracks() and exp_dirs() */
817 static int exp__files_dirs(int nargs,
818 const struct mx_node **args,
822 int (*fn)(disorder_client *client,
827 char **tracks, *dir, *re;
829 const struct mx_node *m;
830 struct tracksort_data *tsd;
832 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
835 if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
845 if(fn(dcgi_client, dir, re, &tracks, &ntracks))
847 /* Sort it. NB trackname_transform() does not go to the server. */
848 tsd = tracksort_init(ntracks, tracks, type);
849 /* Expand the subsiduary templates. We chuck in @sort and @display because
850 * it is particularly easy to do so. */
851 for(n = 0; n < ntracks; ++n)
852 if((rc = mx_expand(mx_rewritel(m,
853 "index", make_index(n),
854 "parity", n % 2 ? "odd" : "even",
855 "track", tsd[n].track,
856 "first", n == 0 ? "true" : "false",
857 "last", n + 1 == ntracks ? "false" : "true",
859 "display", tsd[n].display,
867 /*$ @tracks{DIR}{RE}{TEMPLATE}
869 * For each track below DIR, expands TEMPLATE with the
870 * following expansions:
871 * - @track: the UNQUOTED track name
872 * - @index: the track number from 0
873 * - @parity: "even" or "odd" alternately
874 * - @first: "true" on the first track and "false" otherwise
875 * - @last: "true" on the last track and "false" otherwise
876 * - @sort: the sort key for this track
877 * - @display: the UNQUOTED display string for this track
879 * RE is optional and if present is the regexp to match against.
881 static int exp_tracks(int nargs,
882 const struct mx_node **args,
885 return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
888 /*$ @dirs{DIR}{RE}{TEMPLATE}
890 * For each directory below DIR, expands TEMPLATE with the
891 * following expansions:
892 * - @track: the UNQUOTED directory name
893 * - @index: the directory number from 0
894 * - @parity: "even" or "odd" alternately
895 * - @first: "true" on the first directory and "false" otherwise
896 * - @last: "true" on the last directory and "false" otherwise
897 * - @sort: the sort key for this directory
898 * - @display: the UNQUOTED display string for this directory
900 * RE is optional and if present is the regexp to match against.
902 static int exp_dirs(int nargs,
903 const struct mx_node **args,
906 return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
909 static int exp__search_shim(disorder_client *c, const char *terms,
910 const char attribute((unused)) *re,
911 char ***vecp, int *nvecp) {
912 return disorder_search(c, terms, vecp, nvecp);
915 /*$ @search{KEYWORDS}{TEMPLATE}
917 * For each track matching KEYWORDS, expands TEMPLATE with the
918 * following expansions:
919 * - @track: the UNQUOTED directory name
920 * - @index: the directory number from 0
921 * - @parity: "even" or "odd" alternately
922 * - @first: "true" on the first directory and "false" otherwise
923 * - @last: "true" on the last directory and "false" otherwise
924 * - @sort: the sort key for this track
925 * - @display: the UNQUOTED display string for this track
927 static int exp_search(int nargs,
928 const struct mx_node **args,
931 return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
936 * Expands to label NAME from options.labels. Undefined lables expand to the
937 * last dot-separated component, e.g. X.Y.Z to Z.
939 static int exp_label(int attribute((unused)) nargs,
942 void attribute((unused)) *u) {
943 return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0;
946 /*$ @breadcrumbs{DIR}{TEMPLATE}
948 * Expands TEMPLATE for each directory in the path up to DIR, excluding the root
949 * but including DIR itself, with the following expansions:
950 * - @dir: the directory
951 * - @last: "true" if this is the last directory, otherwise "false"
953 * DIR must be an absolute path.
955 static int exp_breadcrumbs(int attribute((unused)) nargs,
956 const struct mx_node **args,
958 void attribute((unused)) *u) {
960 char *dir, *parent, *ptr;
962 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
964 /* Reject relative paths */
966 error(0, "breadcrumbs: '%s' is a relative path", dir);
972 /* Find the end of this directory */
973 while(*ptr && *ptr != '/')
975 parent = xstrndup(dir, ptr - dir);
976 if((rc = mx_expand(mx_rewritel(args[1],
978 "last", *ptr ? "false" : "true",
988 /** @brief Register DisOrder-specific expansions */
989 void dcgi_expansions(void) {
990 mx_register("arg", 1, 1, exp_arg);
991 mx_register("argq", 1, 1, exp_argq);
992 mx_register("enabled", 0, 0, exp_enabled);
993 mx_register("error", 0, 0, exp_error);
994 mx_register("image", 1, 1, exp_image);
995 mx_register("isnew", 0, 0, exp_isnew);
996 mx_register("isplaying", 0, 0, exp_isplaying);
997 mx_register("isqueue", 0, 0, exp_isqueue);
998 mx_register("isrecent", 0, 0, exp_isrecent);
999 mx_register("label", 1, 1, exp_label);
1000 mx_register("length", 1, 1, exp_length);
1001 mx_register("movable", 1, 2, exp_movable);
1002 mx_register("part", 2, 3, exp_part);
1003 mx_register("paused", 0, 0, exp_paused);
1004 mx_register("pref", 2, 2, exp_pref);
1005 mx_register("quote", 1, 1, exp_quote);
1006 mx_register("random-enabled", 0, 0, exp_random_enabled);
1007 mx_register("removable", 1, 1, exp_removable);
1008 mx_register("resolve", 1, 1, exp_resolve);
1009 mx_register("server-version", 0, 0, exp_server_version);
1010 mx_register("state", 1, 1, exp_state);
1011 mx_register("status", 0, 0, exp_status);
1012 mx_register("thisurl", 0, 0, exp_thisurl);
1013 mx_register("trackstate", 1, 1, exp_trackstate);
1014 mx_register("transform", 2, 3, exp_transform);
1015 mx_register("url", 0, 0, exp_url);
1016 mx_register("user", 0, 0, exp_user);
1017 mx_register("userinfo", 1, 1, exp_userinfo);
1018 mx_register("version", 0, 0, exp_version);
1019 mx_register("volume", 1, 1, exp_volume);
1020 mx_register("when", 1, 1, exp_when);
1021 mx_register("who", 1, 1, exp_who);
1022 mx_register_magic("breadcrumbs", 2, 2, exp_breadcrumbs);
1023 mx_register_magic("dirs", 2, 3, exp_dirs);
1024 mx_register_magic("new", 1, 1, exp_new);
1025 mx_register_magic("playing", 0, 1, exp_playing);
1026 mx_register_magic("prefs", 2, 2, exp_prefs);
1027 mx_register_magic("queue", 1, 1, exp_queue);
1028 mx_register_magic("recent", 1, 1, exp_recent);
1029 mx_register_magic("right", 1, 3, exp_right);
1030 mx_register_magic("search", 2, 2, exp_search);
1031 mx_register_magic("tracks", 2, 3, exp_tracks);
1039 indent-tabs-mode:nil