Merged revisions 29196 via svnmerge from
[asterisk/asterisk.git] / res / res_indications.c
old mode 100755 (executable)
new mode 100644 (file)
index 18b486f..f4777e3
@@ -3,7 +3,6 @@
  *
  * Copyright (C) 2002, Pauline Middelink
  *
- * Pauline Middelink <middelink@polyware.nl>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
  * at the top of the source tree.
  */
 
-/** @file res_indications.c 
+/*! \file res_indications.c 
  *
- * Load the indications
+ * \brief Load the indications
  * 
+ * \author Pauline Middelink <middelink@polyware.nl>
+ *
  * Load the country specific dialtones into the asterisk PBX.
  */
  
@@ -27,6 +28,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -45,10 +47,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/indications.h"
-
+#include "asterisk/utils.h"
 
 /* Globals */
-static const char dtext[] = "Indications Configuration";
 static const char config[] = "indications.conf";
 
 /*
@@ -92,13 +93,10 @@ static int handle_add_indication(int fd, int argc, char *argv[])
        if (!tz) {
                /* country does not exist, create it */
                ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
-
-               tz = malloc(sizeof(struct tone_zone));
-               if (!tz) {
-                       ast_log(LOG_WARNING, "Out of memory\n");
+               
+               if (!(tz = ast_calloc(1, sizeof(*tz)))) {
                        return -1;
                }
-               memset(tz,0,sizeof(struct tone_zone));
                ast_copy_string(tz->country,argv[2],sizeof(tz->country));
                if (ast_register_indication_country(tz)) {
                        ast_log(LOG_WARNING, "Unable to register new country\n");
@@ -150,26 +148,20 @@ static int handle_remove_indication(int fd, int argc, char *argv[])
  */
 static int handle_show_indications(int fd, int argc, char *argv[])
 {
-       struct tone_zone *tz;
+       struct tone_zone *tz = NULL;
        char buf[256];
        int found_country = 0;
 
-       if (ast_mutex_lock(&tzlock)) {
-               ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
-               return 0;
-       }
        if (argc == 2) {
                /* no arguments, show a list of countries */
                ast_cli(fd,"Country Alias   Description\n"
                           "===========================\n");
-               for (tz=tone_zones; tz; tz=tz->next) {
+               while ( (tz = ast_walk_indications(tz) ) )
                        ast_cli(fd,"%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
-               }
-               ast_mutex_unlock(&tzlock);
                return 0;
        }
        /* there was a request for specific country(ies), lets humor them */
-       for (tz=tone_zones; tz; tz=tz->next) {
+       while ( (tz = ast_walk_indications(tz) ) ) {
                int i,j;
                for (i=2; i<argc; i++) {
                        if (strcasecmp(tz->country,argv[i])==0 &&
@@ -180,11 +172,12 @@ static int handle_show_indications(int fd, int argc, char *argv[])
                                        ast_cli(fd,"Country Indication      PlayList\n"
                                                   "=====================================\n");
                                }
-                               j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadance>");
-                               for (i=0; i<tz->nrringcadance; i++) {
-                                       j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadance[i]);
+                               j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadence>");
+                               for (i=0; i<tz->nrringcadence; i++) {
+                                       j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadence[i]);
                                }
-                               if (tz->nrringcadance) j--;
+                               if (tz->nrringcadence)
+                                       j--;
                                ast_copy_string(buf+j,"\n",sizeof(buf)-j);
                                ast_cli(fd,buf);
                                for (ts=tz->tones; ts; ts=ts->next)
@@ -195,7 +188,6 @@ static int handle_show_indications(int fd, int argc, char *argv[])
        }
        if (!found_country)
                ast_cli(fd,"No countries matched your criteria.\n");
-       ast_mutex_unlock(&tzlock);
        return -1;
 }
 
@@ -255,40 +247,35 @@ static int ind_load_module(void)
                if (!strcasecmp(cxt, "general")) {
                        cxt = ast_category_browse(cfg, cxt);
                        continue;
-               }
-               tones = malloc(sizeof(struct tone_zone));
-               if (!tones) {
-                       ast_log(LOG_WARNING,"Out of memory\n");
+               }               
+               if (!(tones = ast_calloc(1, sizeof(*tones)))) {
                        ast_config_destroy(cfg);
                        return -1;
                }
-               memset(tones,0,sizeof(struct tone_zone));
                ast_copy_string(tones->country,cxt,sizeof(tones->country));
 
                v = ast_variable_browse(cfg, cxt);
                while(v) {
                        if (!strcasecmp(v->name, "description")) {
                                ast_copy_string(tones->description, v->value, sizeof(tones->description));
-                       } else if (!strcasecmp(v->name,"ringcadance")) {
+                       } else if ((!strcasecmp(v->name,"ringcadence"))||(!strcasecmp(v->name,"ringcadance"))) {
                                char *ring,*rings = ast_strdupa(v->value);
                                c = rings;
                                ring = strsep(&c,",");
                                while (ring) {
                                        int *tmp, val;
                                        if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
-                                               ast_log(LOG_WARNING,"Invalid ringcadance given '%s' at line %d.\n",ring,v->lineno);
+                                               ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
                                                ring = strsep(&c,",");
                                                continue;
-                                       }
-                                       tmp = realloc(tones->ringcadance,(tones->nrringcadance+1)*sizeof(int));
-                                       if (!tmp) {
-                                               ast_log(LOG_WARNING, "Out of memory\n");
+                                       }                                       
+                                       if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
                                                ast_config_destroy(cfg);
                                                return -1;
                                        }
-                                       tones->ringcadance = tmp;
-                                       tmp[tones->nrringcadance] = val;
-                                       tones->nrringcadance++;
+                                       tones->ringcadence = tmp;
+                                       tmp[tones->nrringcadence] = val;
+                                       tones->nrringcadence++;
                                        /* next item */
                                        ring = strsep(&c,",");
                                }
@@ -297,13 +284,11 @@ static int ind_load_module(void)
                                c = countries;
                                country = strsep(&c,",");
                                while (country) {
-                                       struct tone_zone* azone = malloc(sizeof(struct tone_zone));
-                                       if (!azone) {
-                                               ast_log(LOG_WARNING,"Out of memory\n");
+                                       struct tone_zone* azone;
+                                       if (!(azone = ast_calloc(1, sizeof(*azone)))) {
                                                ast_config_destroy(cfg);
                                                return -1;
                                        }
-                                       memset(azone,0,sizeof(struct tone_zone));
                                        ast_copy_string(azone->country, country, sizeof(azone->country));
                                        ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
                                        if (ast_register_indication_country(azone)) {
@@ -323,10 +308,8 @@ static int ind_load_module(void)
                                                goto out;
                                        }
                                }
-                               /* not there, add it to the back */
-                               ts = malloc(sizeof(struct tone_zone_sound));
-                               if (!ts) {
-                                       ast_log(LOG_WARNING, "Out of memory\n");
+                               /* not there, add it to the back */                             
+                               if (!(ts = ast_malloc(sizeof(*ts)))) {
                                        ast_config_destroy(cfg);
                                        return -1;
                                }
@@ -380,7 +363,7 @@ static struct ast_cli_entry show_indications_cli =
 /*
  * Standard module functions ...
  */
-int unload_module(void)
+static int unload_module(void *mod)
 {
        /* remove the registed indications... */
        ast_unregister_indication_country(NULL);
@@ -395,9 +378,10 @@ int unload_module(void)
 }
 
 
-int load_module(void)
+static int load_module(void *mod)
 {
-       if (ind_load_module()) return -1;
+       if (ind_load_module())
+               return -1;
  
        ast_cli_register(&add_indication_cli);
        ast_cli_register(&remove_indication_cli);
@@ -408,7 +392,7 @@ int load_module(void)
        return 0;
 }
 
-int reload(void)
+static int reload(void *mod)
 {
        /* remove the registed indications... */
        ast_unregister_indication_country(NULL);
@@ -416,18 +400,14 @@ int reload(void)
        return ind_load_module();
 }
 
-char *description(void)
-{
-       /* that the following cast is needed, is yuk! */
-       return (char*)dtext;
-}
-
-int usecount(void)
+static const char *description(void)
 {
-       return 0;
+       return "Indications Configuration";
 }
 
-char *key()
+static const char *key(void)
 {
        return ASTERISK_GPL_KEY;
 }
+
+STD_MOD(MOD_0 | NO_USECOUNT, reload, NULL, NULL);