[asterisk-dev] comments on this one ? (macros for parsing config file ?)

Luigi Rizzo rizzo at icir.org
Wed Nov 15 11:32:56 MST 2006


On Wed, Nov 15, 2006 at 06:24:28PM -0000, asterisk-commits at lists.digium.com wrote:
> Author: rizzo
> Date: Wed Nov 15 12:24:28 2006
> New Revision: 47681
> 
> URL: http://svn.digium.com/view/asterisk?view=rev&rev=47681
> Log:
> start using the macros already present in chan_oss.c to parse
> the configuration file.
> I think they make this part of the code a lot more readable and consistent
> and less error prone, especially because we can do the same parsing on
> fields of the same type.
> 
> 
> Modified:
>     team/rizzo/astobj2/channels/chan_sip.c
> 
> Modified: team/rizzo/astobj2/channels/chan_sip.c
> URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/channels/chan_sip.c?view=diff&rev=47681&r1=47680&r2=47681
> ==============================================================================
> --- team/rizzo/astobj2/channels/chan_sip.c (original)
> +++ team/rizzo/astobj2/channels/chan_sip.c Wed Nov 15 12:24:28 2006
> @@ -15631,6 +15631,34 @@
>  	return tmpc;
>  }
>  
> +/*
> + * Helper macros to parse config arguments. They will go in a common
> + * header file if their usage is globally accepted. In the meantime,
> + * we define them here. Typical usage is as below.
> + * Remember to open a block right before M_START (as it declares
> + * some variables) and use the M_* macros WITHOUT A SEMICOLON:
> + *
> + *      {
> + *              M_START(v->name, v->value)
> + *
> + *              M_BOOL("dothis", x->flag1)
> + *              M_STR("name", x->somestring)
> + *              M_F("bar", some_c_code)
> + *              M_END(some_final_statement)
> + *              ... other code in the block
> + *      }
> + *
> + * XXX NOTE these macros should NOT be replicated in other parts of asterisk.
> + * Likely we will come up with a better way of doing config file parsing.
> + */ 
> +#define M_START(var, val) \
> +        char *__s = var; char *__val = val;
> +#define M_END(x)   x;
> +#define M_F(tag, f)                     if (!strcasecmp((__s), tag)) { f; } else
> +#define M_BOOL(tag, dst)        M_F(tag, (dst) = ast_true(__val) )
> +#define M_UINT(tag, dst)        M_F(tag, (dst) = strtoul(__val, NULL, 0) )
> +#define M_STR(tag, dst)         M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
> +
>  /*!
>    \brief Handle flag-type options common to configuration of devices - users and peers
>    \param flags array of two struct ast_flags
> @@ -15963,77 +15991,57 @@
>  	strcpy(user->mohinterpret, default_mohinterpret);
>  	strcpy(user->mohsuggest, default_mohsuggest);
>  	for (; v; v = v->next) {
> +		M_START(v->name, v->value);
>  		if (handle_common_options(&userflags[0], &mask[0], v))
>  			continue;
>  
> -		if (!strcasecmp(v->name, "context")) {
> -			ast_copy_string(user->context, v->value, sizeof(user->context));
> -		} else if (!strcasecmp(v->name, "subscribecontext")) {
> -			ast_copy_string(user->subscribecontext, v->value, sizeof(user->subscribecontext));
> -		} else if (!strcasecmp(v->name, "setvar")) {
> -			user->chanvars = add_var(v->value, user->chanvars);
> -		} else if (!strcasecmp(v->name, "permit") ||
> -				   !strcasecmp(v->name, "deny")) {
> -			user->ha = ast_append_ha(v->name, v->value, user->ha);
> -		} else if (!strcasecmp(v->name, "allowtransfer")) {
> -			user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
> -		} else if (!strcasecmp(v->name, "secret")) {
> -			ast_copy_string(user->secret, v->value, sizeof(user->secret)); 
> -		} else if (!strcasecmp(v->name, "md5secret")) {
> -			ast_copy_string(user->md5secret, v->value, sizeof(user->md5secret));
> -		} else if (!strcasecmp(v->name, "callerid")) {
> -			ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
> -		} else if (!strcasecmp(v->name, "fullname")) {
> -			ast_copy_string(user->cid_name, v->value, sizeof(user->cid_name));
> -		} else if (!strcasecmp(v->name, "cid_number")) {
> -			ast_copy_string(user->cid_num, v->value, sizeof(user->cid_num));
> -		} else if (!strcasecmp(v->name, "callgroup")) {
> -			user->callgroup = ast_get_group(v->value);
> -		} else if (!strcasecmp(v->name, "pickupgroup")) {
> -			user->pickupgroup = ast_get_group(v->value);
> -		} else if (!strcasecmp(v->name, "language")) {
> -			ast_copy_string(user->language, v->value, sizeof(user->language));
> -		} else if (!strcasecmp(v->name, "mohinterpret") 
> -			|| !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
> -			ast_copy_string(user->mohinterpret, v->value, sizeof(user->mohinterpret));
> -		} else if (!strcasecmp(v->name, "mohsuggest")) {
> -			ast_copy_string(user->mohsuggest, v->value, sizeof(user->mohsuggest));
> -		} else if (!strcasecmp(v->name, "accountcode")) {
> -			ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
> -		} else if (!strcasecmp(v->name, "call-limit")) {
> +		M_STR("context", user->context)
> +		M_STR("subscribecontext", user->subscribecontext)
> +		M_F("setvar", user->chanvars = add_var(v->value, user->chanvars))
> +		M_F("permit", user->ha = ast_append_ha(v->name, v->value, user->ha))
> +		M_F("deny", user->ha = ast_append_ha(v->name, v->value, user->ha))
> +		M_F("allowtransfer", user->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED)
> +		M_STR("secret", user->secret)
> +		M_STR("md5secret", user->md5secret)
> +		M_F("callerid", ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num)))
> +		M_STR("fullname", user->cid_name)
> +		M_STR("cid_number", user->cid_num)
> +		M_F("callgroup", user->callgroup = ast_get_group(v->value))
> +		M_F("pickupgroup", user->pickupgroup = ast_get_group(v->value))
> +		M_STR("language", user->language)
> +		M_STR("mohinterpret", user->mohinterpret)
> +		M_STR("musicclass", user->mohinterpret)
> +		M_STR("musiconhold", user->mohinterpret)
> +		M_STR("mohsuggest", user->mohsuggest)
> +		M_STR("accountcode", user->accountcode)
> +		M_F("call-limit", {
>  			user->call_limit = atoi(v->value);
>  			if (user->call_limit < 0)
> -				user->call_limit = 0;
> -		} else if (!strcasecmp(v->name, "amaflags")) {
> +				user->call_limit = 0;} )
> +		M_F("amaflags", {
>  			format = ast_cdr_amaflags2int(v->value);
>  			if (format < 0) {
>  				ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
>  			} else {
>  				user->amaflags = format;
> -			}
> -		} else if (!strcasecmp(v->name, "allow")) {
> -			ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
> -		} else if (!strcasecmp(v->name, "disallow")) {
> -			ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
> -		} else if (!strcasecmp(v->name, "autoframing")) {
> -			user->autoframing = ast_true(v->value);
> -		} else if (!strcasecmp(v->name, "callingpres")) {
> +			} })
> +		M_F("allow", ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1))
> +		M_F("disallow", ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0))
> +		M_BOOL("autoframing", user->autoframing)
> +		M_F("callingpres", {
>  			user->callingpres = ast_parse_caller_presentation(v->value);
>  			if (user->callingpres == -1)
> -				user->callingpres = atoi(v->value);
> -		} else if (!strcasecmp(v->name, "maxcallbitrate")) {
> +				user->callingpres = atoi(v->value);} )
> +		M_F("maxcallbitrate", {
>  			user->maxcallbitrate = atoi(v->value);
>  			if (user->maxcallbitrate < 0)
> -				user->maxcallbitrate = default_maxcallbitrate;
> - 		} else if (!strcasecmp(v->name, "t38pt_udptl")) {
> -			ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);
> +				user->maxcallbitrate = default_maxcallbitrate;} )
> + 		M_F("t38pt_udptl", ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL))
>  #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
> -		} else if (!strcasecmp(v->name, "t38pt_rtp")) {
> -			ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);
> -		} else if (!strcasecmp(v->name, "t38pt_tcp")) {
> -			ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);
> +		M_F("t38pt_rtp", ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP))
> +		M_F("t38pt_tcp", ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP))
>  #endif
> -		}
> +		M_END(/* */)
>  	}
>  	ast_copy_flags(&user->flags[0], &userflags[0], mask[0].flags);
>  	ast_copy_flags(&user->flags[1], &userflags[1], mask[1].flags);
> 
> _______________________________________________
> --Bandwidth and Colocation provided by Easynews.com --
> 
> asterisk-commits mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-commits


More information about the asterisk-dev mailing list