Summary:ASTERISK-00561: [patch] Named Arguments In Apps
Reporter:Anthony Minessale (anthm)Labels:
Date Opened:2003-11-21 20:50:35.000-0600Date Closed:2011-06-07 14:05:26
Versions:Frequency of
Environment:Attachments:( 0) asthash.c
( 1) asthash.h
( 2) Makefile.12.02.diff
Description:named_arg library

put named_args.h in /usr/src/asterisk/include
and named_args.c in /usr/src/asterisk

patch Makefile or just look for $OBJS and add in named_args.o

#include <named_arg.h> anywhere you want to use it.

This is a new set of functions to parse the app_* args as a hash.

#include <named_arg.h>

struct hash_table {
 char var[100];
 char val[100];
 struct hash_table *next;

char *hash_write(struct hash_table *hash,char *name,char *value);
char *hash_read(struct hash_table *hash,char *name);
char *hash_delete(struct hash_table *hash,char *name);
int named_args(struct hash_table *hash,char *data,char delim);
void print_hash(struct hash_table *hash);


;here is a normal app being called
exten=> 1,1,Whatever(this|that|morestuff);

/* let's say the var 'info' is the buffer of args to app_whatever */

/* declare and allocate a new hash  */
struct hash_table *arghash;
arghash = malloc(sizeof(struct hash_table));

/* named_args will chop up the string and fill up the hash parsed by the
  specified delim and return the total args parsed

arg_count = named_args(arghash,info,'|');

/* since there were no names specified the number element starting with 0
  is auto assigned

*/ use hash_read to get the value */

char *str;

str = hash_read(arghash,"0");

/* in this example str = "this"

  the above method makes for a good back compat mode to simulate the existing
  syntax. The cool part is detailed below.

  Using the new functionality you can name the args eliminating
  the age old issue we dont need to discuss about why unnamed args are a pita

; lets call an app that supports named args
exten=> 1,1,Whatever(chan=Zap/1|timeout=12|cool=yes|rtfm=no);

/* again assuming in our app the parameter buffer is called 'info'*/

arg_count = named_args(arghash,info,'|');

/* now a call to hash_read can get you the arg by name */

char *chan;
chan = hash_read(arghash,"chan");

of course chan = "Zap/1" */

/* debug /*



you can also declare/modify and use the hashes anywhere in the app


struct hash_table *myhash;
myhash = malloc(sizeof(struct hash_table));



put named_args.h in /usr/src/asterisk/include
and named_args.c in /usr/src/asterisk

#include <named_arg.h> anywhere you want to use it.

patch Makefile or just look for $OBJS and add in named_args.o
Comments:By: Anthony Minessale (anthm) 2003-11-22 10:27:32.000-0600

arg_count = named_args(arghash,info,'|');

       queuename = hash_read(arghash,"0");
       if(queuename) {
         options = hash_read(arghash,"1");
         skillmask = hash_read(arghash,"2");
         toolong = hash_read(arghash,"3");
         url = hash_read(arghash,"4");
         announceoverride = hash_read(arghash,"5");
       else { /* named args */
         queuename = hash_read(arghash,"queuename");
         options = hash_read(arghash,"options");
         skillmask = hash_read(arghash,"skillmask");
         toolong = hash_read(arghash,"toolong");
         url = hash_read(arghash,"url");
         announceoverride = hash_read(arghash,"announceoverride");

exten => 4007,1,Queue(queuename=ispsaver_setup|options=t|skillmask=1|toolong=0)

 -- Accepting AUTHENTICATED call from, requested format = 512, actual format = 512
   -- Executing Queue("IAX2[wsast1@tonysbox]/1", "queuename=ispsaver_setup|options=t|skillmask=1|toolong=0") in new stack
queue: ispsaver_setup, options: t, skillmask: 1 toolong: 0 url: (null), announce: (null)

does this rock or what ? fully back compat if you didnt name any args
it just takes em in order like the old way and names em 0,1,2,3,4,5

edited on: 11-22-03 10:21

By: Brian West (bkw918) 2003-11-22 14:30:28.000-0600


By: Anthony Minessale (anthm) 2003-11-25 16:58:21.000-0600

named_args.? became asthash.?

and functions hash_xyz became string_hash_xyz

and declaration is performed like this:
 struct string_hash_table *myhash = (struct string_hash_table *) init_hash();

instead of :

 struct hash_table *arghash;
 arghash = malloc(sizeof(struct hash_table));

By: Anthony Minessale (anthm) 2003-12-01 12:12:13.000-0600

New Syntax:

struct string_hash_table *arghash = (struct string_hash_table *) init_string_hash("args");

usage in an app: assuming info is the arg buffer from the app
 arg_count = named_args(arghash,info,'|');

       queuename = string_hash_read(arghash,"0");
       if(queuename) { /* unnamed args .. back compat */
         penalty = string_hash_read(arghash,"1");
         skillmask = string_hash_read(arghash,"2");
         interface = string_hash_read(arghash,"3");
       else { /* named args yay! */
         queuename = string_hash_read(arghash,"queuename");
         penalty = string_hash_read(arghash,"penalty");
         skillmask = string_hash_read(arghash,"skillmask");
         interface = string_hash_read(arghash,"interface");

edited on: 12-01-03 12:05

By: John Todd (jtodd) 2003-12-02 13:42:32.000-0600

OK, this is pretty cool.  However, does this require modifying every (current) application so that it will interpret named arguments, or does it "just work" without modification?

By: Anthony Minessale (anthm) 2003-12-02 18:51:10.000-0600

it requires modification =(

it also has bugs, here is revision on 12.02

By: Anthony Minessale (anthm) 2003-12-05 19:16:28.000-0600

small change use newest asthash.h

By: gunk (gunk) 2003-12-06 17:17:26.000-0600

Uhm... you need to change the include in asthash.c

#include "asthash.h"

If you use brackets, gcc can't find it.

By: Anthony Minessale (anthm) 2003-12-08 11:23:10.000-0600

asthash.h is meant to be put in /usr/src/asterisk/include

when you put it there the brackets will work.

By: jrollyson (jrollyson) 2004-01-07 21:20:19.000-0600

Any major problems preventing this from being included into CVS, its been here over a month?

By: Brian West (bkw918) 2004-01-26 01:03:49.000-0600

Tony I see no checks if malloc fails.  You should check to see if malloc fails and do the right thing(tm)!

By: Brian West (bkw918) 2004-01-26 18:56:03.000-0600

He has a newer version of this.