chiark / gitweb /
Fix mis-merged trackdb_open().
[disorder] / lib / macros-builtin.c
index d8551fbd1cc78b644545dd80dcf07e07ac5aa28d..f314bd9c7ae27f7369416e82a3888e3d2a341dfb 100644 (file)
@@ -2,40 +2,41 @@
  * This file is part of DisOrder
  * Copyright (C) 2008 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 /** @file lib/macros-builtin.c
  * @brief Built-in expansions
  *
- * This is a grab-bag of non-domain-specific expansions.  Documentation will be
- * generated from the comments at the head of each function.
+ * This is a grab-bag of non-domain-specific expansions
+ *
+ * Documentation is generated from the comments at the head of each function.
+ * The comment should have a '$' and the expansion name on the first line and
+ * should have a blank line between each paragraph.
+ *
+ * To make a bulleted list, put a '-' at the start of each line.
+ *
+ * You can currently get away with troff markup but this is horribly ugly and
+ * might be changed.
  */
 
-#include <config.h>
-#include "types.h"
+#include "common.h"
 
-#include <stdio.h>
-#include <string.h>
 #include <errno.h>
-#include <assert.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
-#include <sys/wait.h>
 
 #include "hash.h"
 #include "mem.h"
@@ -48,6 +49,7 @@
 #include "split.h"
 #include "printf.h"
 #include "vector.h"
+#include "filepart.h"
 
 static struct vector include_path;
 
@@ -70,13 +72,14 @@ int mx_bool_result(struct sink *output, int result) {
 }
 
 /** @brief Search the include path */
-char *mx_find(const char *name) {
+char *mx_find(const char *name, int report) {
   char *path;
   int n;
   
   if(name[0] == '/') {
     if(access(name, O_RDONLY) < 0) {
-      error(errno, "cannot read %s", name);
+      if(report)
+        error(errno, "cannot read %s", name);
       return 0;
     }
     path = xstrdup(name);
@@ -88,14 +91,15 @@ char *mx_find(const char *name) {
         break;
     }
     if(n >= include_path.nvec) {
-      error(0, "cannot find '%s' in search path", name);
+      if(report)
+        error(0, "cannot find '%s' in search path", name);
       return 0;
     }
   }
   return path;
 }
 
-/* @include{TEMPLATE}@
+/*$ @include{TEMPLATE}
  *
  * Includes TEMPLATE.
  *
@@ -122,7 +126,7 @@ static int exp_include(int attribute((unused)) nargs,
   char buffer[4096];
   struct stat sb;
 
-  if(!(path = mx_find(args[0]))) {
+  if(!(path = mx_find(args[0], 1/*report*/))) {
     if(sink_printf(output, "[[cannot find '%s']]", args[0]) < 0)
       return 0;
     return 0;
@@ -150,9 +154,9 @@ static int exp_include(int attribute((unused)) nargs,
   return 0;
 }
 
-/* @include{COMMAND}@
+/*$ @include{COMMAND}
  *
- * Executes COMMAND via the shell (using "sh -c") and copies its
+ * Executes COMMAND via the shell (using "sh \-c") and copies its
  * standard output to the template output.  The shell command output
  * is not expanded or modified in any other way.
  *
@@ -199,7 +203,7 @@ static int exp_shell(int attribute((unused)) nargs,
   return 0;
 }
 
-/* @if{CONDITION}{IF-TRUE}{IF-FALSE}@
+/*$ @if{CONDITION}{IF-TRUE}{IF-FALSE}
  *
  * If CONDITION is "true" then evaluates to IF-TRUE.  Otherwise
  * evaluates to IF-FALSE.  The IF-FALSE part is optional.
@@ -221,7 +225,7 @@ static int exp_if(int nargs,
     return 0;
 }
 
-/* @and{BRANCH}{BRANCH}...@
+/*$ @and{BRANCH}{BRANCH}...
  *
  * Expands to "true" if all the branches are "true" otherwise to "false".  If
  * there are no brances then the result is "true".  Only as many branches as
@@ -247,7 +251,7 @@ static int exp_and(int nargs,
   return mx_bool_result(output, result);
 }
 
-/* @or{BRANCH}{BRANCH}...@
+/*$ @or{BRANCH}{BRANCH}...
  *
  * Expands to "true" if any of the branches are "true" otherwise to "false".
  * If there are no brances then the result is "false".  Only as many branches
@@ -273,7 +277,7 @@ static int exp_or(int nargs,
   return mx_bool_result(output, result);
 }
 
-/* @not{CONDITION}@
+/*$ @not{CONDITION}
  *
  * Expands to "true" unless CONDITION is "true" in which case "false".
  */
@@ -284,7 +288,7 @@ static int exp_not(int attribute((unused)) nargs,
   return mx_bool_result(output, !mx_str2bool(args[0]));
 }
 
-/* @#{...}@
+/*$ @#{...}
  *
  * Expands to nothing.  The argument(s) are not fully evaluated, and no side
  * effects occur.
@@ -296,7 +300,7 @@ static int exp_comment(int attribute((unused)) nargs,
   return 0;
 }
 
-/* @urlquote{STRING}@
+/*$ @urlquote{STRING}
  *
  * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
  * in a URL with %-encoded form.
@@ -311,7 +315,7 @@ static int exp_urlquote(int attribute((unused)) nargs,
     return 0;
 }
 
-/* @eq{S1}{S2}...@
+/*$ @eq{S1}{S2}...
  *
  * Expands to "true" if all the arguments are identical, otherwise to "false"
  * (i.e. if any pair of arguments differs).
@@ -335,7 +339,7 @@ static int exp_eq(int nargs,
   return mx_bool_result(output, result);
 }
 
-/* @ne{S1}{S2}...@
+/*$ @ne{S1}{S2}...
  *
  * Expands to "true" if all of the arguments differ from one another, otherwise
  * to "false" (i.e. if any value appears more than once).
@@ -359,7 +363,7 @@ static int exp_ne(int nargs,
   return mx_bool_result(output, result);
 }
 
-/* @discard{...}@
+/*$ @discard{...}
  *
  * Expands to nothing.  Unlike the comment expansion @#{...}, side effects of
  * arguments are not suppressed.  So this can be used to surround a collection
@@ -372,7 +376,7 @@ static int exp_discard(int attribute((unused)) nargs,
   return 0;
 }
 
-/* @define{NAME}{ARG1 ARG2...}{DEFINITION}@
+/*$ @define{NAME}{ARG1 ARG2...}{DEFINITION}
  *
  * Define a macro.  The macro will be called NAME and will act like an
  * expansion.  When it is expanded, the expansion is replaced by DEFINITION,
@@ -395,13 +399,43 @@ static int exp_define(int attribute((unused)) nargs,
   return 0;
 }
 
+/*$ @basename{PATH}
+ *
+ * Expands to the UNQUOTED basename of PATH.
+ */
+static int exp_basename(int attribute((unused)) nargs,
+                        char **args,
+                        struct sink attribute((unused)) *output,
+                        void attribute((unused)) *u) {
+  return sink_writes(output, d_basename(args[0])) < 0 ? -1 : 0;
+}
+
+/*$ @dirname{PATH}
+ *
+ * Expands to the UNQUOTED directory name of PATH.
+ */
+static int exp_dirname(int attribute((unused)) nargs,
+                       char **args,
+                       struct sink attribute((unused)) *output,
+                       void attribute((unused)) *u) {
+  return sink_writes(output, d_dirname(args[0])) < 0 ? -1 : 0;
+}
+
+/*$ @q{STRING}
+ *
+ * Expands to STRING.
+ */
+static int exp_q(int attribute((unused)) nargs,
+                 char **args,
+                 struct sink attribute((unused)) *output,
+                 void attribute((unused)) *u) {
+  return sink_writes(output, args[0]) < 0 ? -1 : 0;
+}
+
 /** @brief Register built-in expansions */
 void mx_register_builtin(void) {
-  mx_register_magic("#", 0, INT_MAX, exp_comment);
-  mx_register_magic("and", 0, INT_MAX, exp_and);
-  mx_register_magic("define", 3, 3, exp_define);
-  mx_register_magic("if", 2, 3, exp_if);
-  mx_register_magic("or", 0, INT_MAX, exp_or);
+  mx_register("basename", 1, 1, exp_basename);
+  mx_register("dirname", 1, 1, exp_dirname);
   mx_register("discard", 0, INT_MAX, exp_discard);
   mx_register("eq", 0, INT_MAX, exp_eq);
   mx_register("include", 1, 1, exp_include);
@@ -409,6 +443,12 @@ void mx_register_builtin(void) {
   mx_register("not", 1, 1, exp_not);
   mx_register("shell", 1, 1, exp_shell);
   mx_register("urlquote", 1, 1, exp_urlquote);
+  mx_register("q", 1, 1, exp_q);
+  mx_register_magic("#", 0, INT_MAX, exp_comment);
+  mx_register_magic("and", 0, INT_MAX, exp_and);
+  mx_register_magic("define", 3, 3, exp_define);
+  mx_register_magic("if", 2, 3, exp_if);
+  mx_register_magic("or", 0, INT_MAX, exp_or);
 }
 
 /** @brief Add a directory to the search path