#include "split.h"
#include "printf.h"
#include "vector.h"
+#include "filepart.h"
static struct vector include_path;
}
/** @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);
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.
*
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;
return 0;
}
-/* @include{COMMAND}@
+/*! @include{COMMAND}@
*
* Executes COMMAND via the shell (using "sh -c") and copies its
* standard output to the template output. The shell command output
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.
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
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
return mx_bool_result(output, result);
}
-/* @not{CONDITION}@
+/*! @not{CONDITION}@
*
* Expands to "true" unless CONDITION is "true" in which case "false".
*/
return mx_bool_result(output, !mx_str2bool(args[0]));
}
-/* @#{...}@
+/*! @#{...}@
*
* Expands to nothing. The argument(s) are not fully evaluated, and no side
* effects occur.
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.
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).
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).
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
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,
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);
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