[Asterisk-code-review] func_env: Add DIRNAME and BASENAME functions (asterisk[19])
N A
asteriskteam at digium.com
Thu Sep 9 17:51:51 CDT 2021
N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16462 )
Change subject: func_env: Add DIRNAME and BASENAME functions
......................................................................
func_env: Add DIRNAME and BASENAME functions
Adds the DIRNAME and BASENAME functions, which are
wrappers around the corresponding C library functions.
These can be used to safely and conveniently work with
file paths and names in the dialplan.
ASTERISK-29628 #close
Change-Id: Id3aeb907f65c0ff96b6e57751ff0cb49d61db7f3
---
A doc/CHANGES-staging/func_env.txt
M funcs/func_env.c
2 files changed, 92 insertions(+), 0 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/62/16462/1
diff --git a/doc/CHANGES-staging/func_env.txt b/doc/CHANGES-staging/func_env.txt
new file mode 100644
index 0000000..af03d5f
--- /dev/null
+++ b/doc/CHANGES-staging/func_env.txt
@@ -0,0 +1,5 @@
+Subject: func_env.c
+
+Two new functions, DIRNAME and BASENAME, are now
+included which allow users to obtain the directory
+or the base filename of any file.
diff --git a/funcs/func_env.c b/funcs/func_env.c
index e625853..e5e3e70 100644
--- a/funcs/func_env.c
+++ b/funcs/func_env.c
@@ -28,6 +28,7 @@
#include "asterisk.h"
#include <sys/stat.h> /* stat(2) */
+#include <libgen.h> /* dirname and basename */
#include "asterisk/module.h"
#include "asterisk/channel.h"
@@ -240,6 +241,42 @@
<ref type="function">FILE_COUNT_LINE</ref>
</see-also>
</function>
+ <function name="BASENAME" language="en_US">
+ <synopsis>
+ Return the name of a file.
+ </synopsis>
+ <syntax>
+ <parameter name="filename" required="true" />
+ </syntax>
+ <description>
+ <para>Return the base file name, given a full file path.</para>
+ <example title="Directory name">
+ same => n,Set(basename=${BASENAME(/etc/asterisk/extensions.conf)})
+ same => n,NoOp(${basename}) ; outputs extensions.conf
+ </example>
+ </description>
+ <see-also>
+ <ref type="function">DIRNAME</ref>
+ </see-also>
+ </function>
+ <function name="DIRNAME" language="en_US">
+ <synopsis>
+ Return the directory of a file.
+ </synopsis>
+ <syntax>
+ <parameter name="filename" required="true" />
+ </syntax>
+ <description>
+ <para>Return the directory of a file, given a full file path.</para>
+ <example title="Directory name">
+ same => n,Set(dirname=${DIRNAME(/etc/asterisk/extensions.conf)})
+ same => n,NoOp(${dirname}) ; outputs /etc/asterisk
+ </example>
+ </description>
+ <see-also>
+ <ref type="function">BASENAME</ref>
+ </see-also>
+ </function>
***/
static int env_read(struct ast_channel *chan, const char *cmd, char *data,
@@ -483,6 +520,40 @@
return 0;
}
+static int file_dirname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+ char *ret = NULL;
+
+ *buf = '\0';
+
+ if (data) {
+ ret = dirname(data);
+ }
+
+ if (ret) {
+ ast_copy_string(buf, ret, len);
+ }
+
+ return 0;
+}
+
+static int file_basename(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+ char *ret = NULL;
+
+ *buf = '\0';
+
+ if (data) {
+ ret = basename(data);
+ }
+
+ if (ret) {
+ ast_copy_string(buf, ret, len);
+ }
+
+ return 0;
+}
+
static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
FILE *ff;
@@ -1260,6 +1331,18 @@
.read_max = 2,
};
+static struct ast_custom_function file_dirname_function = {
+ .name = "DIRNAME",
+ .read = file_dirname,
+ .read_max = 12,
+};
+
+static struct ast_custom_function file_basename_function = {
+ .name = "BASENAME",
+ .read = file_basename,
+ .read_max = 12,
+};
+
static int unload_module(void)
{
int res = 0;
@@ -1269,6 +1352,8 @@
res |= ast_custom_function_unregister(&file_function);
res |= ast_custom_function_unregister(&file_count_line_function);
res |= ast_custom_function_unregister(&file_format_function);
+ res |= ast_custom_function_unregister(&file_dirname_function);
+ res |= ast_custom_function_unregister(&file_basename_function);
return res;
}
@@ -1282,6 +1367,8 @@
res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH);
res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ);
res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ);
+ res |= ast_custom_function_register(&file_dirname_function);
+ res |= ast_custom_function_register(&file_basename_function);
return res;
}
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16462
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 19
Gerrit-Change-Id: Id3aeb907f65c0ff96b6e57751ff0cb49d61db7f3
Gerrit-Change-Number: 16462
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210909/85708dee/attachment.html>
More information about the asterisk-code-review
mailing list