Browse code

- generic fix_param fixup function, the function produces fparam_t structure, which can contain parsed integer, str string, AVP, regex, or asciiz character. The function gets the original string parameter as input

Jan Janak authored on 01/03/2006 12:00:48
Showing 2 changed files
... ...
@@ -47,6 +47,7 @@
47 47
 #include "ut.h"
48 48
 #include "route_struct.h"
49 49
 #include "flags.h"
50
+#include "trim.h"
50 51
 
51 52
 #include <regex.h>
52 53
 #include <dlfcn.h>
... ...
@@ -722,3 +723,89 @@ int fix_flag( modparam_t type, void* val,
722 722
 	*flag=num;
723 723
 	return 0;
724 724
 }
725
+
726
+
727
+/*
728
+ * Generic parameter fixup function which creates
729
+ * fparam_t structure. type parameter contains allowed
730
+ * parameter types
731
+ */
732
+int fix_param(int type, void** param)
733
+{	
734
+	fparam_t* p;
735
+	str name;
736
+	unsigned long num;
737
+	int err;
738
+
739
+	p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
740
+	if (!p) {
741
+		ERR("fix_param: No memory left\n");
742
+		return E_OUT_OF_MEM;
743
+	}
744
+	memset(p, 0, sizeof(fparam_t));
745
+	p->orig = *param;
746
+
747
+	switch(type) {
748
+	case FPARAM_UNSPEC:
749
+		ERR("fix_param: Invalid type value\n");
750
+		goto error;
751
+
752
+	case FPARAM_ASCIIZ:
753
+		p->v.asciiz = *param;
754
+		break;
755
+
756
+	case FPARAM_STR:
757
+		p->v.str.s = (char*)*param;
758
+		p->v.str.len = strlen(p->v.str.s);
759
+		break;
760
+
761
+	case FPARAM_INT:
762
+		num = str2s(*param, strlen(*param), &err);
763
+		if (err == 0) {
764
+			p->v.i = num;
765
+		} else {
766
+			ERR("Bad number <%s>\n",
767
+			    (char*)(*param));
768
+			goto error;
769
+		}
770
+		break;
771
+
772
+	case FPARAM_REGEX:
773
+		if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
774
+			ERR("No memory left\n");
775
+			goto error;
776
+		}
777
+		if (regcomp(p->v.regex, *param, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
778
+			pkg_free(p->v.regex);
779
+			ERR("Bad regular expression '%s'\n", (char*)*param);
780
+		        goto error;
781
+		}
782
+		break;
783
+
784
+	case FPARAM_AVP:
785
+		name.s = (char*)*param;
786
+		name.len = strlen(name.s);
787
+		trim(&name);
788
+		if (!name.len || name.s[0] != '$') {
789
+			     /* Not an AVP identifier */
790
+			pkg_free(p);
791
+			return 1;
792
+		}
793
+		name.s++;
794
+		name.len--;
795
+		
796
+		if (parse_avp_ident(&name, &p->v.avp) < 0) {
797
+			ERR("Error while parsing attribute name\n");
798
+			goto error;
799
+		}
800
+		break;
801
+	}
802
+
803
+	p->type = type;
804
+	*param = (void*)p;
805
+	return 0;
806
+
807
+ error:
808
+	pkg_free(p);
809
+	return E_UNSPEC;
810
+}
... ...
@@ -110,6 +110,31 @@ struct param_export_ {
110 110
 };
111 111
 
112 112
 
113
+enum {
114
+	FPARAM_UNSPEC = 0,
115
+	FPARAM_ASCIIZ = (1 << 0),
116
+	FPARAM_STR    = (1 << 1),
117
+	FPARAM_INT    = (1 << 2),
118
+	FPARAM_REGEX  = (1 << 3),
119
+	FPARAM_AVP    = (1 << 5),
120
+};
121
+
122
+/*
123
+ * Function parameter
124
+ */
125
+typedef struct fparam {
126
+        char* orig;                /* The original value */
127
+        int type;                  /* Type of parameter */
128
+        union {
129
+		char* asciiz;      /* Zero terminated ASCII string */
130
+		str str;           /* pointer/len string */
131
+		int i;             /* Integer value */
132
+		regex_t* regex;    /* Compiled regular expression */
133
+		avp_ident_t avp;   /* AVP identifier */
134
+	} v;
135
+} fparam_t;
136
+
137
+
113 138
 typedef struct cmd_export_ cmd_export_t;
114 139
 typedef struct param_export_ param_export_t;
115 140
 
... ...
@@ -203,6 +228,13 @@ int fixup_regex_1(void** param, int param_no);
203 203
 /* Compile regular expression in second parameter */
204 204
 int fixup_regex_2(void** param, int param_no);
205 205
 
206
+/*
207
+ * Generic parameter fixup function which creates
208
+ * fparam_t structure. type parameter contains allowed
209
+ * parameter types
210
+ */
211
+int fix_param(int type, void** param);
212
+
206 213
 /* API function to get other parameters from fixup */
207 214
 action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no);
208 215
 int fixup_get_param_count(void **cur_param, int cur_param_no);