+ * Expands to the latest status string.
+ */
+static int exp_status(int attribute((unused)) nargs,
+ char attribute((unused)) **args,
+ struct sink *output,
+ void attribute((unused)) *u) {
+ return sink_writes(output, dcgi_status_string ? dcgi_status_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 Compare two @ref entry objects */
+int dcgi_compare_entry(const void *a, const void *b) {
+ const struct dcgi_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 dcgi_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 dcgi_entry), dcgi_compare_entry);
+ /* Expand the subsiduary templates. We chuck in @sort and @display because
+ * it is particularly easy to do so. */
+ 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",
+ "sort", e[n].sort,
+ "display", e[n].display,
+ (char *)0),
+ output, u)))
+ return rc;
+ return 0;
+
+}
+
+/*! @tracks{DIR}{RE}{TEMPLATE}
+ *
+ * For each track below DIR, expands TEMPLATE with the
+ * following expansions:
+ * - @track: the UNQUOTED track name
+ * - @index: the track number from 0
+ * - @parity: "even" or "odd" alternately
+ * - @first: "true" on the first track and "false" otherwise
+ * - @last: "true" on the last track and "false" otherwise
+ * - @sort: the sort key for this track
+ * - @display: the UNQUOTED display string for this track
+ *
+ * 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: the UNQUOTED directory name
+ * - @index: the directory number from 0
+ * - @parity: "even" or "odd" alternately
+ * - @first: "true" on the first directory and "false" otherwise
+ * - @last: "true" on the last directory and "false" otherwise
+ * - @sort: the sort key for this directory
+ * - @display: the UNQUOTED display string for this directory
+ *
+ * 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);
+}
+
+static int exp__search_shim(disorder_client *c, const char *terms,
+ const char attribute((unused)) *re,
+ char ***vecp, int *nvecp) {
+ return disorder_search(c, terms, vecp, nvecp);
+}
+
+/*! @search{KEYWORDS}{TEMPLATE}
+ *
+ * For each track matching KEYWORDS, expands TEMPLATE with the
+ * following expansions:
+ * - @track: the UNQUOTED directory name
+ * - @index: the directory number from 0
+ * - @parity: "even" or "odd" alternately
+ * - @first: "true" on the first directory and "false" otherwise
+ * - @last: "true" on the last directory and "false" otherwise
+ * - @sort: the sort key for this track
+ * - @display: the UNQUOTED display string for this track
+ */
+static int exp_search(int nargs,
+ const struct mx_node **args,
+ struct sink *output,
+ void *u) {
+ return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
+}
+
+/*! @label{NAME}
+ *
+ * Expands to label NAME from options.labels. Undefined lables expand to the
+ * last dot-separated component, e.g. X.Y.Z to Z.
+ */
+static int exp_label(int attribute((unused)) nargs,
+ char **args,
+ struct sink *output,
+ void attribute((unused)) *u) {
+ return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0;
+}
+
+/*! @breadcrumbs{DIR}{TEMPLATE}
+ *
+ * Expands TEMPLATE for each directory in the path up to DIR, excluding the root
+ * but including DIR itself, with the following expansions:
+ * - @dir: the directory
+ * - @last: "true" if this is the last directory, otherwise "false"
+ *
+ * DIR must be an absolute path.
+ */
+static int exp_breadcrumbs(int attribute((unused)) nargs,
+ const struct mx_node **args,
+ struct sink *output,
+ void attribute((unused)) *u) {
+ int rc;
+ char *dir, *parent, *ptr;
+
+ if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
+ return rc;
+ /* Reject relative paths */
+ if(dir[0] != '/') {
+ error(0, "breadcrumbs: '%s' is a relative path", dir);
+ return 0;
+ }
+ /* Skip the root */
+ ptr = dir + 1;
+ while(*ptr) {
+ /* Find the end of this directory */
+ while(*ptr && *ptr != '/')
+ ++ptr;
+ parent = xstrndup(dir, ptr - dir);
+ if((rc = mx_expand(mx_rewritel(args[1],
+ "dir", parent,
+ "last", *ptr ? "false" : "true",
+ (char *)0),
+ output, u)))
+ return rc;
+ if(*ptr)
+ ++ptr;
+ }
+ return 0;
+}
+