chiark / gitweb /
Return from login page to what you were trying to do
[disorder] / server / macros-disorder.c
index 92fcb754f785e1f39b260f74b96d6428eb2f25fd..807f398ac909dcd05de2403291e8c698185127b5 100644 (file)
@@ -1,3 +1,4 @@
+
 /*
  * This file is part of DisOrder.
  * Copyright (C) 2004-2008 Richard Kettlewell
 #include "disorder-cgi.h"
 
 /** @brief For error template */
-char *dcgi_error_string;
+const char *dcgi_error_string;
+
+/** @brief For login template */
+const char *dcgi_status_string;
 
 /** @brief Return @p i as a string */
 static const char *make_index(int i) {
@@ -79,8 +83,8 @@ static int exp_url(int attribute((unused)) nargs,
 
 /* @arg{NAME}
  *
- * Expands to the CGI argument NAME, or the empty string if there is
- * no such argument.
+ * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if
+ * there is no such argument.  Use @argq for a quick way to quote the argument.
  */
 static int exp_arg(int attribute((unused)) nargs,
                   char **args,
@@ -89,8 +93,24 @@ static int exp_arg(int attribute((unused)) nargs,
   const char *s = cgi_get(args[0]);
 
   if(s)
-    return sink_writes(output,
-                       cgi_sgmlquote(s)) < 0 ? -1 : 0;
+    return sink_writes(output, s) < 0 ? -1 : 0;
+  else
+    return 0;
+}
+
+/* @argq{NAME}
+ *
+ * Expands to the (quoted) form of CGI argument NAME, or the empty string if
+ * there is no such argument.  Use @arg for the unquoted argument.
+ */
+static int exp_argq(int attribute((unused)) nargs,
+                    char **args,
+                    struct sink *output,
+                    void attribute((unused)) *u) {
+  const char *s = cgi_get(args[0]);
+
+  if(s)
+    return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
   else
     return 0;
 }
@@ -300,9 +320,10 @@ static int exp_movable(int  nargs,
 
 /* @playing{TEMPLATE}
  *
- * Expands to TEMPLATE, with:
- * - @id expanded to the queue ID of the playing track
- * - @track expanded to its UNQUOTED name
+ * Expands to TEMPLATE, with the following expansions:
+ * - @id: the queue ID of the playing track
+ * - @track: the playing track's
+ UNQUOTED name
  *
  * If no track is playing expands to nothing.
  *
@@ -328,12 +349,12 @@ static int exp_playing(int nargs,
 /* @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: the queue ID of the track
+ * - @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
  */
 static int exp_queue(int attribute((unused)) nargs,
                      const struct mx_node **args,
@@ -361,12 +382,12 @@ 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: the queue ID of the track
+ * - @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
  */
 static int exp_recent(int attribute((unused)) nargs,
                       const struct mx_node **args,
@@ -394,11 +415,11 @@ 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: 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
  *
  * Note that unlike @playing, @queue and @recent which are otherwise
  * superficially similar, there is no @id sub-expansion here.
@@ -508,12 +529,12 @@ static int exp_pref(int attribute((unused)) nargs,
  *
  * 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: the UNQUOTED preference name
+ * - @index: the preference number from 0
+ * - @value: the UNQUOTED preference value
+ * - @parity: "even" or "odd" alternately
+ * - @first: "true" on the first preference and "false" otherwise
+ * - @last: "true" on the last preference and "false" otherwise
  *
  * Use @quote to quote preference names and values where necessary; see below.
  */
@@ -651,7 +672,7 @@ static int exp_resolve(int attribute((unused)) nargs,
 static int exp_paused(int attribute((unused)) nargs,
                       char attribute((unused)) **args,
                       struct sink *output,
-                      void attribute((unused)) *u) {
+                     void attribute((unused)) *u) {
   dcgi_lookup(DCGI_PLAYING);
   return mx_bool_result(output, (dcgi_playing
                                  && dcgi_playing->state == playing_paused));
@@ -734,7 +755,20 @@ static int exp_error(int attribute((unused)) nargs,
                      char attribute((unused)) **args,
                      struct sink *output,
                      void attribute((unused)) *u) {
-  return sink_writes(output, dcgi_error_string) < 0 ? -1 : 0;
+  return sink_writes(output, dcgi_error_string ? dcgi_error_string : "")
+              < 0 ? -1 : 0;
+}
+
+/* @status
+ *
+ * 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}
@@ -777,19 +811,9 @@ static int exp_image(int attribute((unused)) nargs,
   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;
+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,
@@ -810,7 +834,7 @@ static int exp__files_dirs(int nargs,
   char **tracks, *dir, *re;
   int n, ntracks, rc;
   const struct mx_node *m;
-  struct entry *e;
+  struct dcgi_entry *e;
 
   if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
     return rc;
@@ -835,8 +859,9 @@ static int exp__files_dirs(int nargs,
     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 */
+  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),
@@ -844,6 +869,8 @@ static int exp__files_dirs(int nargs,
                                    "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;
@@ -855,11 +882,13 @@ static int exp__files_dirs(int nargs,
  *
  * 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
+ * - @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.
  */
@@ -874,11 +903,13 @@ static int exp_tracks(int nargs,
  *
  * 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
+ * - @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.
  */
@@ -889,18 +920,52 @@ static int exp_dirs(int nargs,
   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);
+}
+
+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;
+}
+
 /** @brief Register DisOrder-specific expansions */
 void dcgi_expansions(void) {
   mx_register("arg", 1, 1, exp_arg);
+  mx_register("argq", 1, 1, exp_argq);
   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("isqueue", 0, 0, exp_isqueue);
   mx_register("isrecent", 0, 0, exp_isrecent);
+  mx_register("label",  1, 1, exp_label);
   mx_register("length", 1, 1, exp_length);
-  mx_register("movable", 1, 1, exp_movable);
+  mx_register("movable", 1, 2, exp_movable);
   mx_register("part", 2, 3, exp_part);
   mx_register("paused", 0, 0, exp_paused);
   mx_register("pref", 2, 2, exp_pref);
@@ -910,6 +975,7 @@ void dcgi_expansions(void) {
   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("status", 0, 0, exp_status);
   mx_register("thisurl", 0, 0, exp_thisurl);
   mx_register("trackstate", 1, 1, exp_trackstate);
   mx_register("transform", 2, 3, exp_transform);
@@ -927,6 +993,7 @@ void dcgi_expansions(void) {
   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("search", 2, 2, exp_search);
   mx_register_magic("tracks", 2, 3, exp_tracks);
 }