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
, &s
,
165 !strcmp(context
, "short") ?
"display" : context
,
167 return sink_writes(output
, cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
173 * SGML-quotes STRING. Note that most expansion results are already suitable
174 * quoted, so this expansion is usually not required.
176 static int exp_quote(int attribute((unused
)) nargs
,
179 void attribute((unused
)) *u
) {
180 return sink_writes(output
, cgi_sgmlquote(args
[0])) < 0 ?
-1 : 0;
185 * Expands to the name of the submitter of track ID, which must be a playing
186 * track, in the queue, or in the recent list.
188 static int exp_who(int attribute((unused
)) nargs
,
191 void attribute((unused
)) *u
) {
192 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
194 if(q
&& q
->submitter
)
195 return sink_writes(output
, cgi_sgmlquote(q
->submitter
)) < 0 ?
-1 : 0;
201 * Expands to the time a track started or is expected to start. The track must
202 * be a playing track, in the queue, or in the recent list.
204 static int exp_when(int attribute((unused
)) nargs
,
207 void attribute((unused
)) *u
) {
208 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
209 const struct tm
*w
= 0;
213 case playing_isscratch
:
214 case playing_unplayed
:
217 w
= localtime(&q
->expected
);
220 case playing_no_player
:
222 case playing_scratched
:
223 case playing_started
:
225 case playing_quitting
:
227 w
= localtime(&q
->played
);
231 return sink_printf(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
) < 0 ?
-1 : 0;
233 return sink_writes(output
, " ") < 0 ?
-1 : 0;
236 /*! @length{ID|TRACK}
238 * Expands to the length of a track, identified by its queue ID or its name.
239 * If it is the playing track (identified by ID) then the amount played so far
242 static int exp_length(int attribute((unused
)) nargs
,
245 void attribute((unused
)) *u
) {
246 struct queue_entry
*q
;
250 if(args
[0][0] == '/')
251 /* Track identified by name */
254 /* Track identified by queue ID */
255 if(!(q
= dcgi_findtrack(args
[0])))
257 if(q
->state
== playing_started
|| q
->state
== playing_paused
)
258 if(sink_printf(output
, "%ld:%02ld/", q
->sofar
/ 60, q
->sofar
% 60) < 0)
262 if(dcgi_client
&& disorder_length(dcgi_client
, name
, &length
))
263 return sink_printf(output
, "%ld:%02ld",
264 length
/ 60, length
% 60) < 0 ?
-1 : 0;
265 return sink_writes(output
, " ") < 0 ?
-1 : 0;
270 * Expands to "true" if track ID is removable (or scratchable, if it is the
271 * playing track) and "false" otherwise.
273 static int exp_removable(int attribute((unused
)) nargs
,
276 void attribute((unused
)) *u
) {
277 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
278 /* TODO would be better to reject recent */
280 if(!q
|| !dcgi_client
)
281 return mx_bool_result(output
, 0);
282 dcgi_lookup(DCGI_RIGHTS
);
283 return mx_bool_result(output
,
284 (q
== dcgi_playing ? right_scratchable
: right_removable
)
285 (dcgi_rights
, disorder_user(dcgi_client
), q
));
288 /*! @movable{ID}{DIR}
290 * Expands to "true" if track ID is movable and "false" otherwise.
292 * DIR (which is optional) should be a non-zero integer. If it is negative
293 * then the intended move is down (later in the queue), if it is positive then
294 * the intended move is up (earlier in the queue). The first track is not
295 * movable up and the last track not movable down.
297 static int exp_movable(int nargs
,
300 void attribute((unused
)) *u
) {
301 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
302 /* TODO would be better to recent playing/recent */
304 if(!q
|| !dcgi_client
)
305 return mx_bool_result(output
, 0);
307 const long dir
= atoi(args
[1]);
309 if(dir
> 0 && q
== dcgi_queue
)
310 return mx_bool_result(output
, 0);
311 if(dir
< 0 && q
->next
== 0)
312 return mx_bool_result(output
, 0);
314 dcgi_lookup(DCGI_RIGHTS
);
315 return mx_bool_result(output
,
316 right_movable(dcgi_rights
,
317 disorder_user(dcgi_client
),
321 /*! @playing{TEMPLATE}
323 * Expands to TEMPLATE, with the following expansions:
324 * - @id: the queue ID of the playing track
325 * - @track: the playing track's
328 * If no track is playing expands to nothing.
330 * TEMPLATE is optional. If it is left out then instead expands to the queue
331 * ID of the playing track.
333 static int exp_playing(int nargs
,
334 const struct mx_node
**args
,
337 dcgi_lookup(DCGI_PLAYING
);
341 return sink_writes(output
, dcgi_playing
->id
) < 0 ?
-1 : 0;
342 return mx_expand(mx_rewritel(args
[0],
343 "id", dcgi_playing
->id
,
344 "track", dcgi_playing
->track
,
351 * For each track in the queue, expands TEMPLATE with the following expansions:
352 * - @id: the queue ID of the track
353 * - @track: the UNQUOTED track name
354 * - @index: the track number from 0
355 * - @parity: "even" or "odd" alternately
356 * - @first: "true" on the first track and "false" otherwise
357 * - @last: "true" on the last track and "false" otherwise
359 static int exp_queue(int attribute((unused
)) nargs
,
360 const struct mx_node
**args
,
363 struct queue_entry
*q
;
366 dcgi_lookup(DCGI_QUEUE
);
367 for(q
= dcgi_queue
, i
= 0; q
; q
= q
->next
, ++i
)
368 if((rc
= mx_expand(mx_rewritel(args
[0],
371 "index", make_index(i
),
372 "parity", i
% 2 ?
"odd" : "even",
373 "first", q
== dcgi_queue ?
"true" : "false",
374 "last", q
->next ?
"false" : "true",
381 /*! @recent{TEMPLATE}
383 * For each track in the recently played list, expands TEMPLATE with the
384 * following expansions:
385 * - @id: the queue ID of the track
386 * - @track: the UNQUOTED track name
387 * - @index: the track number from 0
388 * - @parity: "even" or "odd" alternately
389 * - @first: "true" on the first track and "false" otherwise
390 * - @last: "true" on the last track and "false" otherwise
392 static int exp_recent(int attribute((unused
)) nargs
,
393 const struct mx_node
**args
,
396 struct queue_entry
*q
;
399 dcgi_lookup(DCGI_RECENT
);
400 for(q
= dcgi_recent
, i
= 0; q
; q
= q
->next
, ++i
)
401 if((rc
= mx_expand(mx_rewritel(args
[0],
404 "index", make_index(i
),
405 "parity", i
% 2 ?
"odd" : "even",
406 "first", q
== dcgi_recent ?
"true" : "false",
407 "last", q
->next ?
"false" : "true",
416 * For each track in the newly added list, expands TEMPLATE wit the following
418 * - @track: the UNQUOTED track name
419 * - @index: the track number from 0
420 * - @parity: "even" or "odd" alternately
421 * - @first: "true" on the first track and "false" otherwise
422 * - @last: "true" on the last track and "false" otherwise
424 * Note that unlike @playing, @queue and @recent which are otherwise
425 * superficially similar, there is no @id sub-expansion here.
427 static int exp_new(int attribute((unused
)) nargs
,
428 const struct mx_node
**args
,
433 dcgi_lookup(DCGI_NEW
);
434 /* TODO perhaps we should generate an ID value for tracks in the new list */
435 for(i
= 0; i
< dcgi_nnew
; ++i
)
436 if((rc
= mx_expand(mx_rewritel(args
[0],
437 "track", dcgi_new
[i
],
438 "index", make_index(i
),
439 "parity", i
% 2 ?
"odd" : "even",
440 "first", i
== 0 ?
"true" : "false",
441 "last", i
== dcgi_nnew
- 1 ?
"false" : "true",
450 * Expands to the volume in a given channel. CHANNEL must be "left" or
453 static int exp_volume(int attribute((unused
)) nargs
,
456 void attribute((unused
)) *u
) {
457 dcgi_lookup(DCGI_VOLUME
);
458 return sink_printf(output
, "%d",
459 !strcmp(args
[0], "left")
460 ? dcgi_volume_left
: dcgi_volume_right
) < 0 ?
-1 : 0;
465 * Expands to "true" if there is a playing track, otherwise "false".
467 static int exp_isplaying(int attribute((unused
)) nargs
,
468 char attribute((unused
)) **args
,
470 void attribute((unused
)) *u
) {
471 dcgi_lookup(DCGI_PLAYING
);
472 return mx_bool_result(output
, !!dcgi_playing
);
477 * Expands to "true" if there the queue is nonempty, otherwise "false".
479 static int exp_isqueue(int attribute((unused
)) nargs
,
480 char attribute((unused
)) **args
,
482 void attribute((unused
)) *u
) {
483 dcgi_lookup(DCGI_QUEUE
);
484 return mx_bool_result(output
, !!dcgi_queue
);
489 * Expands to "true" if there the recently played list is nonempty, otherwise
492 static int exp_isrecent(int attribute((unused
)) nargs
,
493 char attribute((unused
)) **args
,
495 void attribute((unused
)) *u
) {
496 dcgi_lookup(DCGI_RECENT
);
497 return mx_bool_result(output
, !!dcgi_recent
);
502 * Expands to "true" if there the newly added track list is nonempty, otherwise
505 static int exp_isnew(int attribute((unused
)) nargs
,
506 char attribute((unused
)) **args
,
508 void attribute((unused
)) *u
) {
509 dcgi_lookup(DCGI_NEW
);
510 return mx_bool_result(output
, !!dcgi_nnew
);
513 /*! @pref{TRACK}{KEY}
515 * Expands to a track preference.
517 static int exp_pref(int attribute((unused
)) nargs
,
520 void attribute((unused
)) *u
) {
523 if(dcgi_client
&& !disorder_get(dcgi_client
, args
[0], args
[1], &value
))
524 return sink_writes(output
, cgi_sgmlquote(value
)) < 0 ?
-1 : 0;
528 /*! @prefs{TRACK}{TEMPLATE}
530 * For each track preference of track TRACK, expands TEMPLATE with the
531 * following expansions:
532 * - @name: the UNQUOTED preference name
533 * - @index: the preference number from 0
534 * - @value: the UNQUOTED preference value
535 * - @parity: "even" or "odd" alternately
536 * - @first: "true" on the first preference and "false" otherwise
537 * - @last: "true" on the last preference and "false" otherwise
539 * Use @quote to quote preference names and values where necessary; see below.
541 static int exp_prefs(int attribute((unused
)) nargs
,
542 const struct mx_node
**args
,
546 struct kvp
*k
, *head
;
549 if((rc
= mx_expandstr(args
[0], &track
, u
, "argument #0 (TRACK)")))
551 if(!dcgi_client
|| disorder_prefs(dcgi_client
, track
, &head
))
553 for(k
= head
, i
= 0; k
; k
= k
->next
, ++i
)
554 if((rc
= mx_expand(mx_rewritel(args
[1],
555 "index", make_index(i
),
556 "parity", i
% 2 ?
"odd" : "even",
559 "first", k
== head ?
"true" : "false",
560 "last", k
->next ?
"false" : "true",
567 /*! @transform{TRACK}{TYPE}{CONTEXT}
569 * Transforms a track name (if TYPE is "track") or directory name (if type is
570 * "dir"). CONTEXT should be the context, if it is left out then "display" is
573 static int exp_transform(int nargs
,
576 void attribute((unused
)) *u
) {
577 const char *t
= trackname_transform(args
[1], args
[0],
578 (nargs
> 2 ? args
[2] : "display"));
579 return sink_writes(output
, cgi_sgmlquote(t
)) < 0 ?
-1 : 0;
584 * Expands to "true" if playing is enabled, otherwise "false".
586 static int exp_enabled(int attribute((unused
)) nargs
,
587 char attribute((unused
)) **args
,
589 void attribute((unused
)) *u
) {
593 disorder_enabled(dcgi_client
, &e
);
594 return mx_bool_result(output
, e
);
599 * Expands to "true" if random play is enabled, otherwise "false".
601 static int exp_random_enabled(int attribute((unused
)) nargs
,
602 char attribute((unused
)) **args
,
604 void attribute((unused
)) *u
) {
608 disorder_random_enabled(dcgi_client
, &e
);
609 return mx_bool_result(output
, e
);
612 /*! @trackstate{TRACK}
614 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
615 * the queue, otherwise to nothing.
617 static int exp_trackstate(int attribute((unused
)) nargs
,
620 void attribute((unused
)) *u
) {
622 struct queue_entry
*q
;
626 if(disorder_resolve(dcgi_client
, &track
, args
[0]))
628 dcgi_lookup(DCGI_PLAYING
);
629 if(dcgi_playing
&& !strcmp(track
, dcgi_playing
->track
))
630 return sink_writes(output
, "playing") < 0 ?
-1 : 0;
631 dcgi_lookup(DCGI_QUEUE
);
632 for(q
= dcgi_queue
; q
; q
= q
->next
)
633 if(!strcmp(track
, q
->track
))
634 return sink_writes(output
, "queued") < 0 ?
-1 : 0;
640 * Expands to an UNQUOTED URL which points back to the current page. (NB it
641 * might not be byte for byte identical - for instance, CGI arguments might be
644 static int exp_thisurl(int attribute((unused
)) nargs
,
645 char attribute((unused
)) **args
,
647 void attribute((unused
)) *u
) {
648 return sink_writes(output
, cgi_thisurl(config
->url
)) < 0 ?
-1 : 0;
653 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
654 * nothing if it is not a valid track.
656 static int exp_resolve(int attribute((unused
)) nargs
,
659 void attribute((unused
)) *u
) {
662 if(dcgi_client
&& !disorder_resolve(dcgi_client
, &r
, args
[0]))
663 return sink_writes(output
, r
) < 0 ?
-1 : 0;
669 * Expands to "true" if the playing track is paused, to "false" if it is
670 * playing (or if there is no playing track at all).
672 static int exp_paused(int attribute((unused
)) nargs
,
673 char attribute((unused
)) **args
,
675 void attribute((unused
)) *u
) {
676 dcgi_lookup(DCGI_PLAYING
);
677 return mx_bool_result(output
, (dcgi_playing
678 && dcgi_playing
->state
== playing_paused
));
683 * Expands to the current state of track ID.
685 static int exp_state(int attribute((unused
)) nargs
,
688 void attribute((unused
)) *u
) {
689 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
692 return sink_writes(output
, playing_states
[q
->state
]) < 0 ?
-1 : 0;
696 /*! @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
698 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
699 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
701 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
702 * the user has the right and "false" otherwise.
704 * If there is no connection to the server then expands to nothing (in all
707 static int exp_right(int nargs
,
708 const struct mx_node
**args
,
717 dcgi_lookup(DCGI_RIGHTS
);
718 if((rc
= mx_expandstr(args
[0], &right
, u
, "argument #0 (RIGHT)")))
720 if(parse_rights(right
, &r
, 1/*report*/))
722 /* Single-argument form */
724 return mx_bool_result(output
, !!(r
& dcgi_rights
));
725 /* Multiple argument form */
727 return mx_expand(args
[1], output
, u
);
729 return mx_expand(args
[2], output
, u
);
733 /*! @userinfo{PROPERTY}
735 * Expands to the named property of the current user.
737 static int exp_userinfo(int attribute((unused
)) nargs
,
740 void attribute((unused
)) *u
) {
744 && !disorder_userinfo(dcgi_client
, disorder_user(dcgi_client
),
746 return sink_writes(output
, v
) < 0 ?
-1 : 0;
752 * Expands to the latest error string.
754 static int exp_error(int attribute((unused
)) nargs
,
755 char attribute((unused
)) **args
,
757 void attribute((unused
)) *u
) {
758 return sink_writes(output
, dcgi_error_string ? dcgi_error_string
: "")
764 * Expands to the latest status string.
766 static int exp_status(int attribute((unused
)) nargs
,
767 char attribute((unused
)) **args
,
769 void attribute((unused
)) *u
) {
770 return sink_writes(output
, dcgi_status_string ? dcgi_status_string
: "")
776 * Expands to the URL of the image called NAME.
778 * Firstly if there is a label called images.NAME then the image stem will be
779 * the value of that label. Otherwise the stem will be NAME.png.
781 * If the label url.static exists then it will give the base URL for images.
782 * Otherwise the base url will be /disorder/.
784 static int exp_image(int attribute((unused
)) nargs
,
787 void attribute((unused
)) *u
) {
788 const char *url
, *stem
;
791 /* Compute the stem */
792 byte_xasprintf(&labelname
, "images.%s", args
[0]);
793 if(option_label_exists(labelname
))
794 stem
= option_label(labelname
);
796 byte_xasprintf((char **)&stem
, "%s.png", args
[0]);
797 /* If the stem looks like it's reasonalby complete, use that */
799 || !strncmp(stem
, "http:", 5)
800 || !strncmp(stem
, "https:", 6))
803 /* Compute the URL */
804 if(option_label_exists("url.static"))
805 byte_xasprintf((char **)&url
, "%s/%s",
806 option_label("url.static"), stem
);
808 /* Default base is /disorder */
809 byte_xasprintf((char **)&url
, "/disorder/%s", stem
);
811 return sink_writes(output
, cgi_sgmlquote(url
)) < 0 ?
-1 : 0;
814 /** @brief Compare two @ref entry objects */
815 int dcgi_compare_entry(const void *a
, const void *b
) {
816 const struct dcgi_entry
*ea
= a
, *eb
= b
;
818 return compare_tracks(ea
->sort
, eb
->sort
,
819 ea
->display
, eb
->display
,
820 ea
->track
, eb
->track
);
823 /** @brief Implementation of exp_tracks() and exp_dirs() */
824 static int exp__files_dirs(int nargs
,
825 const struct mx_node
**args
,
829 int (*fn
)(disorder_client
*client
,
834 char **tracks
, *dir
, *re
;
836 const struct mx_node
*m
;
837 struct dcgi_entry
*e
;
839 if((rc
= mx_expandstr(args
[0], &dir
, u
, "argument #0 (DIR)")))
842 if((rc
= mx_expandstr(args
[1], &re
, u
, "argument #1 (RE)")))
852 if(fn(dcgi_client
, dir
, re
, &tracks
, &ntracks
))
854 /* Sort it. NB trackname_transform() does not go to the server. */
855 e
= xcalloc(ntracks
, sizeof *e
);
856 for(n
= 0; n
< ntracks
; ++n
) {
857 e
->track
= tracks
[n
];
858 e
[n
].track
= tracks
[n
];
859 e
[n
].sort
= trackname_transform(type
, tracks
[n
], "sort");
860 e
[n
].display
= trackname_transform(type
, tracks
[n
], "display");
862 qsort(e
, ntracks
, sizeof (struct dcgi_entry
), dcgi_compare_entry
);
863 /* Expand the subsiduary templates. We chuck in @sort and @display because
864 * it is particularly easy to do so. */
865 for(n
= 0; n
< ntracks
; ++n
)
866 if((rc
= mx_expand(mx_rewritel(m
,
867 "index", make_index(n
),
868 "parity", n
% 2 ?
"odd" : "even",
870 "first", n
== 0 ?
"true" : "false",
871 "last", n
+ 1 == ntracks ?
"false" : "true",
873 "display", e
[n
].display
,
881 /*! @tracks{DIR}{RE}{TEMPLATE}
883 * For each track below DIR, expands TEMPLATE with the
884 * following expansions:
885 * - @track: the UNQUOTED track name
886 * - @index: the track number from 0
887 * - @parity: "even" or "odd" alternately
888 * - @first: "true" on the first track and "false" otherwise
889 * - @last: "true" on the last track and "false" otherwise
890 * - @sort: the sort key for this track
891 * - @display: the UNQUOTED display string for this track
893 * RE is optional and if present is the regexp to match against.
895 static int exp_tracks(int nargs
,
896 const struct mx_node
**args
,
899 return exp__files_dirs(nargs
, args
, output
, u
, "track", disorder_files
);
902 /*! @dirs{DIR}{RE}{TEMPLATE}
904 * For each directory below DIR, expands TEMPLATE with the
905 * following expansions:
906 * - @track: the UNQUOTED directory name
907 * - @index: the directory number from 0
908 * - @parity: "even" or "odd" alternately
909 * - @first: "true" on the first directory and "false" otherwise
910 * - @last: "true" on the last directory and "false" otherwise
911 * - @sort: the sort key for this directory
912 * - @display: the UNQUOTED display string for this directory
914 * RE is optional and if present is the regexp to match against.
916 static int exp_dirs(int nargs
,
917 const struct mx_node
**args
,
920 return exp__files_dirs(nargs
, args
, output
, u
, "dir", disorder_directories
);
923 static int exp__search_shim(disorder_client
*c
, const char *terms
,
924 const char attribute((unused
)) *re
,
925 char ***vecp
, int *nvecp
) {
926 return disorder_search(c
, terms
, vecp
, nvecp
);
929 /*! @search{KEYWORDS}{TEMPLATE}
931 * For each track matching KEYWORDS, expands TEMPLATE with the
932 * following expansions:
933 * - @track: the UNQUOTED directory name
934 * - @index: the directory number from 0
935 * - @parity: "even" or "odd" alternately
936 * - @first: "true" on the first directory and "false" otherwise
937 * - @last: "true" on the last directory and "false" otherwise
938 * - @sort: the sort key for this track
939 * - @display: the UNQUOTED display string for this track
941 static int exp_search(int nargs
,
942 const struct mx_node
**args
,
945 return exp__files_dirs(nargs
, args
, output
, u
, "track", exp__search_shim
);
950 * Expands to label NAME from options.labels. Undefined lables expand to the
951 * last dot-separated component, e.g. X.Y.Z to Z.
953 static int exp_label(int attribute((unused
)) nargs
,
956 void attribute((unused
)) *u
) {
957 return sink_writes(output
, option_label(args
[0])) < 0 ?
-1 : 0;
960 /*! @breadcrumbs{DIR}{TEMPLATE}
962 * Expands TEMPLATE for each directory in the path up to DIR, excluding the root
963 * but including DIR itself, with the following expansions:
964 * - @dir: the directory
965 * - @last: "true" if this is the last directory, otherwise "false"
967 * DIR must be an absolute path.
969 static int exp_breadcrumbs(int attribute((unused
)) nargs
,
970 const struct mx_node
**args
,
972 void attribute((unused
)) *u
) {
974 char *dir
, *parent
, *ptr
;
976 if((rc
= mx_expandstr(args
[0], &dir
, u
, "argument #0 (DIR)")))
978 /* Reject relative paths */
980 error(0, "breadcrumbs: '%s' is a relative path", dir
);
986 /* Find the end of this directory */
987 while(*ptr
&& *ptr
!= '/')
989 parent
= xstrndup(dir
, ptr
- dir
);
990 if((rc
= mx_expand(mx_rewritel(args
[1],
992 "last", *ptr ?
"false" : "true",
1002 /** @brief Register DisOrder-specific expansions */
1003 void dcgi_expansions(void) {
1004 mx_register("arg", 1, 1, exp_arg
);
1005 mx_register("argq", 1, 1, exp_argq
);
1006 mx_register("enabled", 0, 0, exp_enabled
);
1007 mx_register("error", 0, 0, exp_error
);
1008 mx_register("image", 1, 1, exp_image
);
1009 mx_register("isnew", 0, 0, exp_isnew
);
1010 mx_register("isplaying", 0, 0, exp_isplaying
);
1011 mx_register("isqueue", 0, 0, exp_isqueue
);
1012 mx_register("isrecent", 0, 0, exp_isrecent
);
1013 mx_register("label", 1, 1, exp_label
);
1014 mx_register("length", 1, 1, exp_length
);
1015 mx_register("movable", 1, 2, exp_movable
);
1016 mx_register("part", 2, 3, exp_part
);
1017 mx_register("paused", 0, 0, exp_paused
);
1018 mx_register("pref", 2, 2, exp_pref
);
1019 mx_register("quote", 1, 1, exp_quote
);
1020 mx_register("random-enabled", 0, 0, exp_random_enabled
);
1021 mx_register("removable", 1, 1, exp_removable
);
1022 mx_register("resolve", 1, 1, exp_resolve
);
1023 mx_register("server-version", 0, 0, exp_server_version
);
1024 mx_register("state", 1, 1, exp_state
);
1025 mx_register("status", 0, 0, exp_status
);
1026 mx_register("thisurl", 0, 0, exp_thisurl
);
1027 mx_register("trackstate", 1, 1, exp_trackstate
);
1028 mx_register("transform", 2, 3, exp_transform
);
1029 mx_register("url", 0, 0, exp_url
);
1030 mx_register("user", 0, 0, exp_user
);
1031 mx_register("userinfo", 1, 1, exp_userinfo
);
1032 mx_register("version", 0, 0, exp_version
);
1033 mx_register("volume", 1, 1, exp_volume
);
1034 mx_register("when", 1, 1, exp_when
);
1035 mx_register("who", 1, 1, exp_who
);
1036 mx_register_magic("breadcrumbs", 2, 2, exp_breadcrumbs
);
1037 mx_register_magic("dirs", 2, 3, exp_dirs
);
1038 mx_register_magic("new", 1, 1, exp_new
);
1039 mx_register_magic("playing", 0, 1, exp_playing
);
1040 mx_register_magic("prefs", 2, 2, exp_prefs
);
1041 mx_register_magic("queue", 1, 1, exp_queue
);
1042 mx_register_magic("recent", 1, 1, exp_recent
);
1043 mx_register_magic("right", 1, 3, exp_right
);
1044 mx_register_magic("search", 2, 2, exp_search
);
1045 mx_register_magic("tracks", 2, 3, exp_tracks
);
1053 indent-tabs-mode:nil