[Asterisk-Users] How do i make best use of Macro?

Steven Critchfield critch at basesys.com
Mon Jun 16 14:13:46 MST 2003


To comply with Marks request, here is a new patch against app_meetme.c
that copies data to a localbuffer. While I feel moderately comfortable
with this patch, please review my usage of strsep, malloc, and free.
When I run make from the apps/ directory I get a error message about
strsep passing incompatible argument, and under the root level make file
I get several messages that are similar to the 1 from the apps/
directory. As for the malloc and free, I would just feel better if
someone checks my sanity.
 

On Fri, 2003-06-13 at 11:38, Mark Spencer wrote:
> data should be considered a "const void *" in all honesty (maybe i should
> enforce it).   Always copy to a local buffer before strseping it.
> 
> mark
> 
> On Wed, 11 Jun 2003, Steven Critchfield wrote:
> 
> > Since there was some interest in this, here is the diff against current
> > cvs. Someone that is better at C should look into my use of strsep
> > because there is a couple of warnings. Also there is a warning on my use
> > of pbx_builtin_setvar_helper, but I can't see whats wrong here.
> >
> > BTW, SayNumber doesn't seem to say '0'.
> >
> > Usage is like this.
> >
> > exten => 1234,1,MeetMeCount(1234|var)
> > exten => 1234,2,SayNumber(${var})
> > exten => 1234,3,MeetMe(1234)
> >
> > ---------------------------------------------------------------------------------
> >
> >
> > diff -U3 -r asterisk-orig/apps/app_meetme.c asterisk/apps/app_meetme.c
> > --- asterisk-orig/apps/app_meetme.c     2003-06-11 23:14:38.000000000 -0500
> > +++ asterisk/apps/app_meetme.c  2003-06-11 22:58:32.000000000 -0500
> > @@ -54,9 +54,10 @@
> >  "      'q' -- quiet mode (don't play enter/leave sounds)\n";
> >
> >  static char *descrip2 =
> > -"  MeetMeCount(confno): Plays back the number of users in the specified MeetMe\n"
> > -"conference.  Returns 0 on success or -1 on a hangup.  A ZAPTEL INTERFACE\n"
> > -"MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
> > +"  MeetMeCount(confno[|var]): Plays back the number of users in the specifiedi\n"
> > +"MeetMe conference. If var is specified, playback will be skipped and the value\n"
> > +"will be returned in the variable. Returns 0 on success or -1 on a hangup.\n"
> > +"A ZAPTEL INTERFACE MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
> >
> >  STANDARD_LOCAL_USER;
> >
> > @@ -465,19 +466,29 @@
> >         int res = 0;
> >         struct conf *conf;
> >         int cnt;
> > +       char* confnum;
> > +       char val[5] = "0"; /* I don't think we will ever get 99,999 callers into a single meetme */
> > +
> >         if (!data || !strlen(data)) {
> >                 ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
> >                 return -1;
> >         }
> >         LOCAL_USER_ADD(u);
> > -       conf = find_conf(data, 0);
> > +       confnum = strsep((char*) &data,"|");
> > +       conf = find_conf(confnum, 0);
> >         if (conf)
> >                 cnt = conf->users;
> >         else
> >                 cnt = 0;
> > -       if (chan->_state != AST_STATE_UP)
> > -               ast_answer(chan);
> > -       res = ast_say_number(chan, cnt, "", chan->language);
> > +       if(strlen(data)){
> > +               /* have var so load it and exit */
> > +               sprintf(val,"%i",cnt);
> > +               pbx_builtin_setvar_helper(chan,(char*) data,&val);
> > +       }else{
> > +               if (chan->_state != AST_STATE_UP)
> > +                       ast_answer(chan);
> > +               res = ast_say_number(chan, cnt, "", chan->language);
> > +       }
> >         LOCAL_USER_REMOVE(u);
> >         return res;
> >  }
> >
> >
> > --
> > Steven Critchfield <critch at basesys.com>
> >
> > _______________________________________________
> > Asterisk-Users mailing list
> > Asterisk-Users at lists.digium.com
> > http://lists.digium.com/mailman/listinfo/asterisk-users
> >
> 
> _______________________________________________
> Asterisk-Users mailing list
> Asterisk-Users at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-users
-- 
Steven Critchfield  <critch at basesys.com>
-------------- next part --------------
--- apps/app_meetme.c	2003-06-16 16:11:53.000000000 -0500
+++ /home/critch/app_meetme.c	2003-06-16 16:11:18.000000000 -0500
@@ -54,9 +54,10 @@
 "      'q' -- quiet mode (don't play enter/leave sounds)\n";
 
 static char *descrip2 =
-"  MeetMeCount(confno): Plays back the number of users in the specified MeetMe\n"
-"conference.  Returns 0 on success or -1 on a hangup.  A ZAPTEL INTERFACE\n"
-"MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
+"  MeetMeCount(confno[|var]): Plays back the number of users in the specifiedi\n"
+"MeetMe conference. If var is specified, playback will be skipped and the value\n"
+"will be returned in the variable. Returns 0 on success or -1 on a hangup.\n"
+"A ZAPTEL INTERFACE MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
 
 STANDARD_LOCAL_USER;
 
@@ -465,19 +466,37 @@
 	int res = 0;
 	struct conf *conf;
 	int cnt;
+	char *confnum,*localdata,*mhandle;
+	char val[5] = "0"; /* I don't think we will ever get 99,999 callers into a single meetme */
+	
 	if (!data || !strlen(data)) {
 		ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
 		return -1;
 	}
+	mhandle = malloc(strlen(data)); 
+	localdata = mhandle; /* this is to make sure I have the original pointer for the free below */
+	if(!mhandle){
+		ast_log(LOG_WARNING, "MeetMeCount couldn't malloc data space\n");
+		return -1;
+	}
 	LOCAL_USER_ADD(u);
-	conf = find_conf(data, 0);
+	strcpy(localdata,data);
+	confnum = strsep((char*) &localdata,"|");	
+	conf = find_conf(confnum, 0);
 	if (conf)
 		cnt = conf->users;
 	else
 		cnt = 0;
-	if (chan->_state != AST_STATE_UP)
-		ast_answer(chan);
-	res = ast_say_number(chan, cnt, "", chan->language);
+	if(strlen(localdata)){
+		/* have var so load it and exit */
+		sprintf(val,"%i",cnt);
+		pbx_builtin_setvar_helper(chan, localdata,(void*) &val);
+	}else{
+		if (chan->_state != AST_STATE_UP)
+			ast_answer(chan);
+		res = ast_say_number(chan, cnt, "", chan->language);
+	}
+	free(mhandle);
 	LOCAL_USER_REMOVE(u);
 	return res;
 }


More information about the asterisk-users mailing list