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 723
 	*flag=num;
723 724
 	return 0;
724 725
 }
726
+
727
+
728
+/*
729
+ * Generic parameter fixup function which creates
730
+ * fparam_t structure. type parameter contains allowed
731
+ * parameter types
732
+ */
733
+int fix_param(int type, void** param)
734
+{	
735
+	fparam_t* p;
736
+	str name;
737
+	unsigned long num;
738
+	int err;
739
+
740
+	p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
741
+	if (!p) {
742
+		ERR("fix_param: No memory left\n");
743
+		return E_OUT_OF_MEM;
744
+	}
745
+	memset(p, 0, sizeof(fparam_t));
746
+	p->orig = *param;
747
+
748
+	switch(type) {
749
+	case FPARAM_UNSPEC:
750
+		ERR("fix_param: Invalid type value\n");
751
+		goto error;
752
+
753
+	case FPARAM_ASCIIZ:
754
+		p->v.asciiz = *param;
755
+		break;
756
+
757
+	case FPARAM_STR:
758
+		p->v.str.s = (char*)*param;
759
+		p->v.str.len = strlen(p->v.str.s);
760
+		break;
761
+
762
+	case FPARAM_INT:
763
+		num = str2s(*param, strlen(*param), &err);
764
+		if (err == 0) {
765
+			p->v.i = num;
766
+		} else {
767
+			ERR("Bad number <%s>\n",
768
+			    (char*)(*param));
769
+			goto error;
770
+		}
771
+		break;
772
+
773
+	case FPARAM_REGEX:
774
+		if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
775
+			ERR("No memory left\n");
776
+			goto error;
777
+		}
778
+		if (regcomp(p->v.regex, *param, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
779
+			pkg_free(p->v.regex);
780
+			ERR("Bad regular expression '%s'\n", (char*)*param);
781
+		        goto error;
782
+		}
783
+		break;
784
+
785
+	case FPARAM_AVP:
786
+		name.s = (char*)*param;
787
+		name.len = strlen(name.s);
788
+		trim(&name);
789
+		if (!name.len || name.s[0] != '$') {
790
+			     /* Not an AVP identifier */
791
+			pkg_free(p);
792
+			return 1;
793
+		}
794
+		name.s++;
795
+		name.len--;
796
+		
797
+		if (parse_avp_ident(&name, &p->v.avp) < 0) {
798
+			ERR("Error while parsing attribute name\n");
799
+			goto error;
800
+		}
801
+		break;
802
+	}
803
+
804
+	p->type = type;
805
+	*param = (void*)p;
806
+	return 0;
807
+
808
+ error:
809
+	pkg_free(p);
810
+	return E_UNSPEC;
811
+}
... ...
@@ -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 228
 /* Compile regular expression in second parameter */
204 229
 int fixup_regex_2(void** param, int param_no);
205 230
 
231
+/*
232
+ * Generic parameter fixup function which creates
233
+ * fparam_t structure. type parameter contains allowed
234
+ * parameter types
235
+ */
236
+int fix_param(int type, void** param);
237
+
206 238
 /* API function to get other parameters from fixup */
207 239
 action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no);
208 240
 int fixup_get_param_count(void **cur_param, int cur_param_no);