Add Masquerade manager event which trips when a masquerade happens (issue #7840 repor...
[asterisk/asterisk.git] / funcs / func_cut.c
index 1df4703..a8b8c81 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
+ * Copyright (c) 2003-2006 Tilghman Lesher.  All rights reserved.
  *
  * Tilghman Lesher <app_cut__v003@the-tilghman.com>
  *
  * \ingroup functions
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/options.h"
@@ -44,12 +44,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /* Maximum length of any variable */
 #define MAXRESULT      1024
 
-static char *tdesc = "Cut out information from a string";
-
-STANDARD_LOCAL_USER;
-
-LOCAL_USER_DECL;
-
 struct sortable_keys {
        char *key;
        float value;
@@ -58,13 +52,12 @@ struct sortable_keys {
 static int sort_subroutine(const void *arg1, const void *arg2)
 {
        const struct sortable_keys *one=arg1, *two=arg2;
-       if (one->value < two->value) {
+       if (one->value < two->value)
                return -1;
-       } else if (one->value == two->value) {
+       else if (one->value == two->value)
                return 0;
-       } else {
+       else
                return 1;
-       }
 }
 
 #define ERROR_NOARG    (-1)
@@ -79,25 +72,17 @@ static int sort_internal(struct ast_channel *chan, char *data, char *buffer, siz
 
        memset(buffer, 0, buflen);
 
-       if (!data) {
+       if (!data)
                return ERROR_NOARG;
-       }
 
-       strings = ast_strdupa((char *)data);
-       if (!strings) {
-               return ERROR_NOMEM;
-       }
+       strings = ast_strdupa(data);
 
        for (ptrkey = strings; *ptrkey; ptrkey++) {
-               if (*ptrkey == '|') {
+               if (*ptrkey == '|')
                        count++;
-               }
        }
 
        sortable_keys = alloca(count * sizeof(struct sortable_keys));
-       if (!sortable_keys) {
-               return ERROR_NOMEM;
-       }
 
        memset(sortable_keys, 0, count * sizeof(struct sortable_keys));
 
@@ -109,8 +94,7 @@ static int sort_internal(struct ast_channel *chan, char *data, char *buffer, siz
                        count--;
                        continue;
                }
-               *ptrvalue = '\0';
-               ptrvalue++;
+               *ptrvalue++ = '\0';
                sortable_keys[count2].key = ptrkey;
                sscanf(ptrvalue, "%f", &sortable_keys[count2].value);
                count2++;
@@ -143,10 +127,6 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
        memset(buffer, 0, buflen); 
        
        parse = ast_strdupa(data);
-       if (!parse) {
-               ast_log(LOG_ERROR, "Out of memory!\n");
-               return ERROR_NOMEM;
-       }
 
        AST_STANDARD_APP_ARGS(args, parse);
 
@@ -165,10 +145,7 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
                        return ERROR_NOMEM;
                }
 
-               if (args.delimiter[0])
-                       d = args.delimiter[0];
-               else
-                       d = '-';
+               d = args.delimiter[0] ? args.delimiter[0] : '-';
 
                /* String form of the delimiter, for use with strsep(3) */
                snprintf(ds, sizeof(ds), "%c", d);
@@ -177,7 +154,7 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
 
                if (tmp2) {
                        int curfieldnum = 1;
-                       while ((tmp2 != NULL) && (args.field != NULL)) {
+                       while (tmp2 != NULL && args.field != NULL) {
                                char *nextgroup = strsep(&(args.field), "&");
                                int num1 = 0, num2 = MAXRESULT;
                                char trashchar;
@@ -199,31 +176,29 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
 
                                /* Get to start, if any */
                                if (num1 > 0) {
-                                       while ((tmp2 != (char *)NULL + 1) && (curfieldnum < num1)) {
+                                       while (tmp2 != (char *)NULL + 1 && curfieldnum < num1) {
                                                tmp2 = index(tmp2, d) + 1;
                                                curfieldnum++;
                                        }
                                }
 
                                /* Most frequent problem is the expectation of reordering fields */
-                               if ((num1 > 0) && (curfieldnum > num1)) {
+                               if ((num1 > 0) && (curfieldnum > num1))
                                        ast_log(LOG_WARNING, "We're already past the field you wanted?\n");
-                               }
 
                                /* Re-null tmp2 if we added 1 to NULL */
                                if (tmp2 == (char *)NULL + 1)
                                        tmp2 = NULL;
 
                                /* Output fields until we either run out of fields or num2 is reached */
-                               while ((tmp2 != NULL) && (curfieldnum <= num2)) {
+                               while (tmp2 != NULL && curfieldnum <= num2) {
                                        char *tmp3 = strsep(&tmp2, ds);
                                        int curlen = strlen(buffer);
 
-                                       if (curlen) {
+                                       if (curlen)
                                                snprintf(buffer + curlen, buflen - curlen, "%c%s", d, tmp3);
-                                       } else {
+                                       else
                                                snprintf(buffer, buflen, "%s", tmp3);
-                                       }
 
                                        curfieldnum++;
                                }
@@ -233,11 +208,12 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
        return 0;
 }
 
-static char *acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
+static int acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
-       struct localuser *u;
+       struct ast_module_user *u;
+       int ret = -1;
 
-       LOCAL_USER_ACF_ADD(u);
+       u = ast_module_user_add(chan);
 
        switch (sort_internal(chan, data, buf, len)) {
        case ERROR_NOARG:
@@ -247,19 +223,23 @@ static char *acf_sort_exec(struct ast_channel *chan, char *cmd, char *data, char
                ast_log(LOG_ERROR, "Out of memory\n");
                break;
        case 0:
+               ret = 0;
                break;
        default:
                ast_log(LOG_ERROR, "Unknown internal error\n");
        }
-       LOCAL_USER_REMOVE(u);
-       return buf;
+
+       ast_module_user_remove(u);
+
+       return ret;
 }
 
-static char *acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
+static int acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
-       struct localuser *u;
+       int ret = -1;
+       struct ast_module_user *u;
 
-       LOCAL_USER_ACF_ADD(u);
+       u = ast_module_user_add(chan);
 
        switch (cut_internal(chan, data, buf, len)) {
        case ERROR_NOARG:
@@ -272,12 +252,15 @@ static char *acf_cut_exec(struct ast_channel *chan, char *cmd, char *data, char
                ast_log(LOG_ERROR, "Usage: CUT(<varname>,<char-delim>,<range-spec>)\n");
                break;
        case 0:
+               ret = 0;
                break;
        default:
                ast_log(LOG_ERROR, "Unknown internal error\n");
        }
-       LOCAL_USER_REMOVE(u);
-       return buf;
+
+       ast_module_user_remove(u);
+
+       return ret;
 }
 
 struct ast_custom_function acf_sort = {
@@ -304,41 +287,26 @@ struct ast_custom_function acf_cut = {
        .read = acf_cut_exec,
 };
 
-int unload_module(void)
+static int unload_module(void)
 {
-       int res;
+       int res = 0;
 
-       res = ast_custom_function_unregister(&acf_cut);
+       res |= ast_custom_function_unregister(&acf_cut);
        res |= ast_custom_function_unregister(&acf_sort);
 
-       STANDARD_HANGUP_LOCALUSERS;
+       ast_module_user_hangup_all();
 
        return res;
 }
 
-int load_module(void)
+static int load_module(void)
 {
-       int res;
+       int res = 0;
 
-       res = ast_custom_function_register(&acf_cut);
+       res |= ast_custom_function_register(&acf_cut);
        res |= ast_custom_function_register(&acf_sort);
 
        return res;
 }
 
-char *description(void)
-{
-       return tdesc;
-}
-
-int usecount(void)
-{
-       int res;
-       STANDARD_USECOUNT(res);
-       return res;
-}
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cut out information from a string");