76601a92db6f84523766cae5e96787e107834ba1
[sip-router] / pvapi.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of SIP-Router, a free SIP server.
7  *
8  * SIP-Router is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * SIP-Router is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License 
19  * along with this program; if not, write to the Free Software 
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /*!
24  * \file
25  * \brief SIP-router core ::  PV API specification
26  * \ingroup core
27  * Module: \ref core
28  */
29
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "mem/mem.h"
37 #include "mem/shm_mem.h"
38 #include "ut.h"
39 #include "dprint.h"
40 #include "hashes.h"
41 #include "route.h"
42 #include "pvapi.h"
43 #include "pvar.h"
44
45 #define is_in_str(p, in) (p<in->s+in->len && *p)
46
47 #define PV_TABLE_SIZE   16
48 #define TR_TABLE_SIZE   4
49
50
51 void tr_destroy(trans_t *t);
52 void tr_free(trans_t *t);
53
54 typedef struct _pv_item
55 {
56         pv_export_t pve;
57         unsigned int pvid;
58         struct _pv_item *next;
59 } pv_item_t, *pv_item_p;
60
61 static pv_item_t* _pv_table[PV_TABLE_SIZE];
62 static int _pv_table_set = 0;
63
64
65 /**
66  *
67  */
68 void pv_init_table(void)
69 {
70         memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
71         _pv_table_set = 1;
72 }
73
74 /**
75  *
76  */
77 static int is_pv_valid_char(char c)
78 {
79         if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
80                         || (c=='_') || (c=='.') || (c=='?') /* ser $? */)
81                 return 1;
82         return 0;
83 }
84
85 /**
86  *
87  */
88 int pv_table_add(pv_export_t *e)
89 {
90         char *p;
91         str  *in;
92         pv_item_t *pvi = NULL;
93         pv_item_t *pvj = NULL;
94         pv_item_t *pvn = NULL;
95         int found;
96         unsigned int pvid;
97
98         if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
99         {
100                 LM_ERR("invalid parameters\n");
101                 return -1;
102         }
103         
104         if(_pv_table_set==0)
105         {
106                 LM_DBG("PV table not initialized, doing it now\n");
107                 pv_init_table();
108         }
109         in = &(e->name);
110         p = in->s;      
111         while(is_in_str(p,in) && is_pv_valid_char(*p))
112                 p++;
113         if(is_in_str(p,in))
114         {
115                 LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
116                 return -1;
117         }
118         found = 0;
119         //pvid = get_hash1_raw(in->s, in->len);
120         pvid = get_hash1_raw(in->s, in->len);
121
122         pvi = _pv_table[pvid%PV_TABLE_SIZE];
123         while(pvi)
124         {
125                 if(pvi->pvid > pvid)
126                         break;
127                 if(pvi->pve.name.len==in->len)
128                 {
129                         found = strncmp(pvi->pve.name.s, in->s, in->len);
130
131                         if(found==0)
132                         {
133                                 LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
134                                 return -1;
135                         }
136                 }
137                 pvj = pvi;
138                 pvi = pvi->next;
139         }
140
141         pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
142         if(pvn==0)
143         {
144                 LM_ERR("no more memory\n");
145                 return -1;
146         }
147         memset(pvn, 0, sizeof(pv_item_t));
148         memcpy(&(pvn->pve), e, sizeof(pv_export_t));
149         pvn->pvid = pvid;
150
151         if(pvj==0)
152         {
153                 pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
154                 _pv_table[pvid%PV_TABLE_SIZE] = pvn;
155                 goto done;
156         }
157         pvn->next = pvj->next;
158         pvj->next = pvn;
159
160 done:
161         return 0;
162 }
163
164 /**
165  *
166  */
167 int register_pvars_mod(char *mod_name, pv_export_t *items)
168 {
169         int ret;
170         int i;
171
172         if (items==0)
173                 return 0;
174
175         for ( i=0 ; items[i].name.s ; i++ ) {
176                 ret = pv_table_add(&items[i]);
177                 if (ret!=0) {
178                         LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
179                                         items[i].name.len, items[i].name.s, mod_name);
180                 }
181         }
182         return 0;
183 }
184
185 /**
186  *
187  */
188 int pv_table_free(void)
189 {
190         pv_item_p xe;
191         pv_item_p xe1;
192         int i;
193
194         for(i=0; i<PV_TABLE_SIZE; i++)
195         {
196                 xe = _pv_table[i];
197                 while(xe!=0)
198                 {
199                         xe1 = xe;
200                         xe = xe->next;
201                         pkg_free(xe1);
202                 }
203         }
204         memset(_pv_table, 0, sizeof(pv_item_t*)*PV_TABLE_SIZE);
205         _pv_table_set = 0;
206         
207         return 0;
208 }
209
210 /********** helper functions ********/
211 /**
212  * convert unsigned int to pv_value_t
213  */
214 int pv_get_uintval(struct sip_msg *msg, pv_param_t *param,
215                 pv_value_t *res, unsigned int uival)
216 {
217         int l = 0;
218         char *ch = NULL;
219
220         if(res==NULL)
221                 return -1;
222
223         ch = int2str(uival, &l);
224         res->rs.s = ch;
225         res->rs.len = l;
226
227         res->ri = (int)uival;
228         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
229         return 0;
230 }
231
232 /**
233  * convert signed int to pv_value_t
234  */
235 int pv_get_sintval(struct sip_msg *msg, pv_param_t *param,
236                 pv_value_t *res, int sival)
237 {
238         int l = 0;
239         char *ch = NULL;
240
241         if(res==NULL)
242                 return -1;
243
244         ch = sint2str(sival, &l);
245         res->rs.s = ch;
246         res->rs.len = l;
247
248         res->ri = sival;
249         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
250         return 0;
251 }
252
253 /**
254  * convert str to pv_value_t
255  */
256 int pv_get_strval(struct sip_msg *msg, pv_param_t *param,
257                 pv_value_t *res, str *sval)
258 {
259         if(res==NULL)
260                 return -1;
261
262         res->rs = *sval;
263         res->flags = PV_VAL_STR;
264         return 0;
265 }
266
267 /**
268  * convert str-int to pv_value_t (type is str)
269  */
270 int pv_get_strintval(struct sip_msg *msg, pv_param_t *param,
271                 pv_value_t *res, str *sval, int ival)
272 {
273         if(res==NULL)
274                 return -1;
275
276         res->rs = *sval;
277         res->ri = ival;
278         res->flags = PV_VAL_STR|PV_VAL_INT;
279         return 0;
280 }
281
282 /**
283  * convert int-str to pv_value_t (type is int)
284  */
285 int pv_get_intstrval(struct sip_msg *msg, pv_param_t *param,
286                 pv_value_t *res, int ival, str *sval)
287 {
288         if(res==NULL)
289                 return -1;
290
291         res->rs = *sval;
292         res->ri = ival;
293         res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
294         return 0;
295 }
296
297 /*** ============================= ***/
298 static str pv_str_marker = { PV_MARKER_STR, 1 };
299 static int pv_get_marker(struct sip_msg *msg, pv_param_t *param,
300                 pv_value_t *res)
301 {
302         return pv_get_strintval(msg, param, res, &pv_str_marker,
303                         (int)pv_str_marker.s[0]);
304 }
305
306 static str pv_str_empty  = { "", 0 };
307 static str pv_str_null   = { "<null>", 6 };
308 int pv_get_null(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
309 {
310         if(res==NULL)
311                 return -1;
312         
313         res->rs = pv_str_empty;
314         res->ri = 0;
315         res->flags = PV_VAL_NULL;
316         return 0;
317 }
318
319 /**
320  *
321  */
322 pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
323 {
324         pv_item_t *pvi;
325         unsigned int pvid;
326
327         if(pvname==0 || e==0)
328         {
329                 LM_ERR("bad parameters\n");
330                 return NULL;
331         }
332
333         /* search in PV table */
334         // pvid = get_hash1_raw(pvname->s, pvname->len);
335         pvid = get_hash1_raw(pvname->s, pvname->len);
336         pvi = _pv_table[pvid%PV_TABLE_SIZE];
337         while(pvi)
338         {
339                 if(pvi->pvid > pvid)
340                         break;
341
342                 if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
343                         && memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
344                 {
345                         /*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
346                                         _pv_names_table[i].type);*/
347                         /* copy data from table to spec */
348                         e->type = pvi->pve.type;
349                         e->getf = pvi->pve.getf;
350                         e->setf = pvi->pve.setf;
351                         return &(pvi->pve);
352                 }
353                 pvi = pvi->next;
354         }
355
356         return NULL;
357 }
358
359 /**
360  *
361  */
362 int pv_parse_index(pv_spec_p sp, str *in)
363 {
364         char *p;
365         char *s;
366         int sign;
367         pv_spec_p nsp = 0;
368
369         if(in==NULL || in->s==NULL || sp==NULL)
370                 return -1;
371         p = in->s;
372         if(*p==PV_MARKER)
373         {
374                 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
375                 if(nsp==NULL)
376                 {
377                         LM_ERR("no more memory\n");
378                         return -1;
379                 }
380                 s = pv_parse_spec(in, nsp);
381                 if(s==NULL)
382                 {
383                         LM_ERR("invalid index [%.*s]\n", in->len, in->s);
384                         pv_spec_free(nsp);
385                         return -1;
386                 }
387                 sp->pvp.pvi.type = PV_IDX_PVAR;
388                 sp->pvp.pvi.u.dval = (void*)nsp;
389                 return 0;
390         }
391         if(*p=='*' && in->len==1)
392         {
393                 sp->pvp.pvi.type = PV_IDX_ALL;
394                 return 0;
395         }
396         sign = 1;
397         if(*p=='-')
398         {
399                 sign = -1;
400                 p++;
401         }
402         sp->pvp.pvi.u.ival = 0;
403         while(p<in->s+in->len && *p>='0' && *p<='9')
404         {
405                 sp->pvp.pvi.u.ival = sp->pvp.pvi.u.ival * 10 + *p - '0';
406                 p++;
407         }
408         if(p!=in->s+in->len)
409         {
410                 LM_ERR("invalid index [%.*s]\n", in->len, in->s);
411                 return -1;
412         }
413         sp->pvp.pvi.u.ival *= sign;
414         sp->pvp.pvi.type = PV_IDX_INT;
415         return 0;
416 }
417
418 /**
419  *
420  */
421 int pv_init_iname(pv_spec_p sp, int param)
422 {
423         if(sp==NULL)
424                 return -1;
425         sp->pvp.pvn.type = PV_NAME_INTSTR;
426         sp->pvp.pvn.u.isname.name.n = param;
427         return 0;
428 }
429
430 /**
431  *
432  */
433 char* pv_parse_spec2(str *in, pv_spec_p e, int silent)
434 {
435         char *p;
436         str s;
437         str pvname;
438         int pvstate;
439         trans_t *tr = NULL;
440         pv_export_t *pte = NULL;
441         int n=0;
442
443         if(in==NULL || in->s==NULL || e==NULL || *in->s!=PV_MARKER)
444         {
445                 if (!silent) LM_ERR("bad parameters\n");
446                 return NULL;
447         }
448         
449         /* LM_DBG("***** input [%.*s] (%d)\n", in->len, in->s, in->len); */
450         tr = 0;
451         pvstate = 0;
452         memset(e, 0, sizeof(pv_spec_t));
453         p = in->s;
454         p++;
455         if(*p==PV_LNBRACKET)
456         {
457                 p++;
458                 pvstate = 1;
459         }
460         pvname.s = p;
461         if(*p == PV_MARKER) {
462                 p++;
463                 if(pvstate==1)
464                 {
465                         if(*p!=PV_RNBRACKET)
466                                 goto error;
467                         p++;
468                 }
469                 e->getf = pv_get_marker;
470                 e->type = PVT_MARKER;
471                 pvname.len = 1;
472                 goto done_all;
473         }
474         while(is_in_str(p,in) && is_pv_valid_char(*p))
475                 p++;
476         pvname.len = p - pvname.s;
477         if(pvstate==1)
478         {
479                 if(*p==PV_RNBRACKET)
480                 { /* full pv name ended here*/
481                         goto done_inm;
482                 } else if(*p==PV_LNBRACKET) {
483                         p++;
484                         pvstate = 2;
485                 } else if(*p==PV_LIBRACKET) {
486                         p++;
487                         pvstate = 3;
488                 } else if(*p==TR_LBRACKET) {
489                         p++;
490                         pvstate = 4;
491                 } else {
492                         if (!silent)
493                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
494                                                         *p, in->len, in->s, pvstate);
495                         goto error;
496                 }
497         } else { 
498                 if(!is_in_str(p, in)) {
499                         p--;
500                         goto done_inm;
501                 } else if(*p==PV_LNBRACKET) {
502                         p++;
503                         pvstate = 5;
504                 } else {
505                         /* still in input str, but end of PV */
506                         /* p is increased at the end, so decrement here */
507                         p--;
508                         goto done_inm;
509                 }
510         }
511
512 done_inm:
513         if((pte = pv_lookup_spec_name(&pvname, e))==NULL)
514         {
515                 if (!silent) 
516                         LM_ERR("error searching pvar \"%.*s\"\n", pvname.len, pvname.s);
517                 goto error;
518         }
519         if(pte->parse_name!=NULL && pvstate!=2 && pvstate!=5)
520         {
521                 if (!silent) 
522                         LM_ERR("pvar \"%.*s\" expects an inner name\n",
523                                                 pvname.len, pvname.s);
524                 goto error;
525         }
526         if(pvstate==2 || pvstate==5)
527         {
528                 if(pte->parse_name==NULL)
529                 {
530                         if (!silent)
531                                 LM_ERR("pvar \"%.*s\" does not get name param\n",
532                                                 pvname.len, pvname.s);
533                         goto error;
534                 }
535                 s.s = p;
536                 n = 0;
537                 while(is_in_str(p, in))
538                 {
539                         if(*p==PV_RNBRACKET)
540                         {
541                                 if(n==0)
542                                         break;
543                                 n--;
544                         }
545                         if(*p == PV_LNBRACKET)
546                                 n++;
547                         p++;
548                 }
549
550                 if(!is_in_str(p, in))
551                         goto error;
552
553                 if(p==s.s)
554                 {
555                         if (!silent)
556                                 LM_ERR("pvar \"%.*s\" does not get empty name param\n",
557                                                 pvname.len, pvname.s);
558                         goto error;
559                 }
560                 s.len = p - s.s;
561                 if(pte->parse_name(e, &s)!=0)
562                 {
563                         if (!silent)
564                                 LM_ERR("pvar \"%.*s\" has an invalid name param [%.*s]\n",
565                                                 pvname.len, pvname.s, s.len, s.s);
566                         goto error;
567                 }
568                 if(pvstate==2)
569                 {
570                         p++;
571                         if(*p==PV_RNBRACKET)
572                         { /* full pv name ended here*/
573                                 goto done_vnm;
574                         } else if(*p==PV_LIBRACKET) {
575                                 p++;
576                                 pvstate = 3;
577                         } else if(*p==TR_LBRACKET) {
578                                 p++;
579                                 pvstate = 4;
580                         } else {
581                                 if (!silent)
582                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
583                                                                 *p, in->len, in->s, pvstate);
584                                 goto error;
585                         }
586                 } else {
587                         if(*p==PV_RNBRACKET)
588                         { /* full pv name ended here*/
589                                 p++;
590                                 goto done_all;
591                         } else {
592                                 if (!silent)
593                                         LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
594                                                                 *p, in->len, in->s, pvstate);
595                                 goto error;
596                         }
597                 }
598         }
599 done_vnm:
600         if(pvstate==3)
601         {
602                 if(pte->parse_index==NULL)
603                 {
604                         if (!silent)
605                                 LM_ERR("pvar \"%.*s\" does not get index param\n",
606                                                 pvname.len, pvname.s);
607                         goto error;
608                 }
609                 s.s = p;
610                 n = 0;
611                 while(is_in_str(p, in))
612                 {
613                         if(*p==PV_RIBRACKET)
614                         {
615                                 if(n==0)
616                                         break;
617                                 n--;
618                         }
619                         if(*p == PV_LIBRACKET)
620                                 n++;
621                         p++;
622                 }
623                 if(!is_in_str(p, in))
624                         goto error;
625
626                 if(p==s.s)
627                 {
628                         if (!silent)
629                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
630                                                 pvname.len, pvname.s);
631                         goto error;
632                 }
633                 s.len = p - s.s;
634                 if(pte->parse_index(e, &s)!=0)
635                 {
636                         if (!silent)
637                                 LM_ERR("pvar \"%.*s\" has an invalid index param [%.*s]\n",
638                                                 pvname.len, pvname.s, s.len, s.s);
639                         goto error;
640                 }
641                 p++;
642                 if(*p==PV_RNBRACKET)
643                 { /* full pv name ended here*/
644                         goto done_idx;
645                 } else if(*p==TR_LBRACKET) {
646                         p++;
647                         pvstate = 4;
648                 } else {
649                         if (!silent)
650                                 LM_ERR("invalid char '%c' in [%.*s] (%d)\n",
651                                                         *p, in->len, in->s, pvstate);
652                         goto error;
653                 }
654         }
655 done_idx:
656         if(pvstate==4)
657         {
658                 s.s = p-1;
659                 n = 0;
660                 while(is_in_str(p, in))
661                 {
662                         if(*p==TR_RBRACKET)
663                         {
664                                 if(n==0)
665                                 {
666                                         /* yet another transformation */
667                                         p++;
668                                         while(is_in_str(p, in) && (*p==' ' || *p=='\t')) p++;
669
670                                         if(!is_in_str(p, in) || *p != TR_LBRACKET)
671                                         {
672                                                 p--;
673                                                 break;
674                                         }
675                                 }
676                                 n--;
677                         }
678                         if(*p == TR_LBRACKET)
679                                 n++;
680                         p++;
681                 }
682                 if(!is_in_str(p, in))
683                         goto error;
684
685                 if(p==s.s)
686                 {
687                         if (!silent)
688                                 LM_ERR("pvar \"%.*s\" does not get empty index param\n",
689                                                 pvname.len, pvname.s);
690                         goto error;
691                 }
692                 s.len = p - s.s + 1;
693
694                 p = tr_lookup(&s, &tr);
695                 if(p==NULL)
696                 {
697                         if (!silent)
698                                 LM_ERR("bad tr in pvar name \"%.*s\"\n", pvname.len, pvname.s);
699                         goto error;
700                 }
701                 if(*p!=PV_RNBRACKET)
702                 {
703                         if (!silent)
704                                 LM_ERR("bad pvar name \"%.*s\" (%c)!\n", in->len, in->s, *p);
705                         goto error;
706                 }
707                 e->trans = (void*)tr;
708         }
709         p++;
710
711 done_all:
712         if(pte!=NULL && pte->init_param)
713                 pte->init_param(e, pte->iparam);
714         return p;
715
716 error:
717         if(p!=NULL){
718                 if (!silent)
719                         LM_ERR("wrong char [%c/%d] in [%.*s] at [%d (%d)]\n", *p, (int)*p,
720                                         in->len, in->s, (int)(p-in->s), pvstate);
721         }else{
722                 if (!silent)
723                         LM_ERR("invalid parsing in [%.*s] at (%d)\n",
724                                                 in->len, in->s, pvstate);
725         }
726         return NULL;
727
728 } /* end: pv_parse_spec */
729
730 /**
731  *
732  */
733 int pv_parse_format(str *in, pv_elem_p *el)
734 {
735         char *p, *p0;
736         int n = 0;
737         pv_elem_p e, e0;
738         str s;
739
740         if(in==NULL || in->s==NULL || el==NULL)
741                 return -1;
742
743         /*LM_DBG("parsing [%.*s]\n", in->len, in->s);*/
744         
745         if(in->len == 0)
746         {
747                 *el = pkg_malloc(sizeof(pv_elem_t));
748                 if(*el == NULL)
749                         goto error;
750                 memset(*el, 0, sizeof(pv_elem_t));
751                 (*el)->text = *in;
752                 return 0;
753         }
754
755         p = in->s;
756         *el = NULL;
757         e = e0 = NULL;
758
759         while(is_in_str(p,in))
760         {
761                 e0 = e;
762                 e = pkg_malloc(sizeof(pv_elem_t));
763                 if(!e)
764                         goto error;
765                 memset(e, 0, sizeof(pv_elem_t));
766                 n++;
767                 if(*el == NULL)
768                         *el = e;
769                 if(e0)
770                         e0->next = e;
771         
772                 e->text.s = p;
773                 while(is_in_str(p,in) && *p!=PV_MARKER)
774                         p++;
775                 e->text.len = p - e->text.s;
776                 
777                 if(*p == '\0' || !is_in_str(p,in))
778                         break;
779                 s.s = p;
780                 s.len = in->s+in->len-p;
781                 p0 = pv_parse_spec(&s, &e->spec);
782                 
783                 if(p0==NULL)
784                         goto error;
785                 if(*p0 == '\0')
786                         break;
787                 p = p0;
788         }
789         /*LM_DBG("format parsed OK: [%d] items\n", n);*/
790
791         if(*el == NULL)
792                 return -1;
793
794         return 0;
795
796 error:
797         pv_elem_free_all(*el);
798         *el = NULL;
799         return -1;
800 }
801
802 /**
803  *
804  */
805 int pv_get_spec_name(struct sip_msg* msg, pv_param_p ip, pv_value_t *name)
806 {
807         if(msg==NULL || ip==NULL || name==NULL)
808                 return -1;
809         memset(name, 0, sizeof(pv_value_t));
810
811         if(ip->pvn.type==PV_NAME_INTSTR)
812         {
813                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
814                 {
815                         name->rs = ip->pvn.u.isname.name.s;
816                         name->flags = PV_VAL_STR;
817                 } else {
818                         name->ri = ip->pvn.u.isname.name.n;
819                         name->flags = PV_VAL_INT|PV_TYPE_INT;
820                 }
821                 return 0;
822         } else if(ip->pvn.type==PV_NAME_PVAR) {
823                 /* pvar */
824                 if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), name)!=0)
825                 {
826                         LM_ERR("cannot get name value\n");
827                         return -1;
828                 }
829                 if(name->flags&PV_VAL_NULL || name->flags&PV_VAL_EMPTY)
830                 {
831                         LM_ERR("null or empty name\n");
832                         return -1;
833                 }
834                 return 0;
835         }
836         LM_ERR("name type is PV_NAME_OTHER - cannot resolve\n");
837         return -1;
838 }
839
840 /**
841  * parse AVP name
842  * @return 0 on success, -1 on error
843  */
844 int pv_parse_avp_name(pv_spec_p sp, str *in)
845 {
846         char *p;
847         char *s;
848         pv_spec_p nsp = 0;
849
850         if(in==NULL || in->s==NULL || sp==NULL)
851                 return -1;
852         p = in->s;
853         if(*p==PV_MARKER)
854         {
855                 nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
856                 if(nsp==NULL)
857                 {
858                         LM_ERR("no more memory\n");
859                         return -1;
860                 }
861                 s = pv_parse_spec(in, nsp);
862                 if(s==NULL)
863                 {
864                         LM_ERR("invalid name [%.*s]\n", in->len, in->s);
865                         pv_spec_free(nsp);
866                         return -1;
867                 }
868                 //LM_ERR("dynamic name [%.*s]\n", in->len, in->s);
869                 //pv_print_spec(nsp);
870                 sp->pvp.pvn.type = PV_NAME_PVAR;
871                 sp->pvp.pvn.u.dname = (void*)nsp;
872                 return 0;
873         }
874         /*LM_DBG("static name [%.*s]\n", in->len, in->s);*/
875         if(km_parse_avp_spec(in, &sp->pvp.pvn.u.isname.type,
876                                           &sp->pvp.pvn.u.isname.name)!=0)
877         {
878                 LM_ERR("bad avp name [%.*s]\n", in->len, in->s);
879                 return -1;
880         }
881         sp->pvp.pvn.type = PV_NAME_INTSTR;
882         return 0;
883 }
884
885 /**
886  * fill avp name details (id and type)
887  * @return 0 on success, -1 on error
888  */
889 int pv_get_avp_name(struct sip_msg* msg, pv_param_p ip, int_str *avp_name,
890                 unsigned short *name_type)
891 {
892         pv_value_t tv;
893         if(ip==NULL || avp_name==NULL || name_type==NULL)
894                 return -1;
895         memset(avp_name, 0, sizeof(int_str));
896         *name_type = 0;
897
898         if(ip->pvn.type==PV_NAME_INTSTR)
899         {
900                 *name_type = ip->pvn.u.isname.type;
901                 if(ip->pvn.u.isname.type&AVP_NAME_STR)
902                 {
903                         avp_name->s = ip->pvn.u.isname.name.s;
904                         *name_type |= AVP_NAME_STR;
905                 } else {
906                         avp_name->n = ip->pvn.u.isname.name.n;
907                         /* *name_type &= AVP_SCRIPT_MASK; */
908                         *name_type = 0;
909                 }
910                 return 0;
911         }
912         /* pvar */
913         if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
914         {
915                 LM_ERR("cannot get avp value\n");
916                 return -1;
917         }
918         if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
919         {
920                 LM_ERR("null or empty name\n");
921                 return -1;
922         }
923                 
924         if((tv.flags&PV_TYPE_INT) && (tv.flags&PV_VAL_INT))
925         {
926                 avp_name->n = tv.ri;
927         } else {
928                 avp_name->s = tv.rs;
929                 *name_type = AVP_NAME_STR;
930         }
931         return 0;
932 }
933
934 /**
935  *
936  */
937 int pv_get_spec_index(struct sip_msg* msg, pv_param_p ip, int *idx, int *flags)
938 {
939         pv_value_t tv;
940         if(ip==NULL || idx==NULL || flags==NULL)
941                 return -1;
942
943         *idx = 0;
944         *flags = 0;
945
946         if(ip->pvi.type == PV_IDX_ALL) {
947                 *flags = PV_IDX_ALL;
948                 return 0;
949         }
950         
951         if(ip->pvi.type == PV_IDX_INT)
952         {
953                 *idx = ip->pvi.u.ival;
954                 return 0;
955         }
956
957         /* pvar */
958         if(pv_get_spec_value(msg, (pv_spec_p)ip->pvi.u.dval, &tv)!=0)
959         {
960                 LM_ERR("cannot get index value\n");
961                 return -1;
962         }
963         if(!(tv.flags&PV_VAL_INT))
964         {
965                 LM_ERR("invalid index value\n");
966                 return -1;
967         }
968         *idx = tv.ri;
969         return 0;
970 }
971
972 /**
973  *
974  */
975 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value)
976 {
977         int ret = 0;
978
979         if(msg==NULL || sp==NULL || sp->getf==NULL || value==NULL
980                         || sp->type==PVT_NONE)
981         {
982                 LM_ERR("bad parameters\n");
983                 return -1;
984         }
985         
986         memset(value, 0, sizeof(pv_value_t));
987
988         ret = (*sp->getf)(msg, &(sp->pvp), value);
989         if(ret!=0)
990                 return ret;
991                 
992         if(sp->trans)
993                 return tr_exec(msg, (trans_t*)sp->trans, value);
994         return ret;
995 }
996
997 /**
998  *
999  */
1000 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
1001                 pv_value_t *value)
1002 {
1003         if(sp==NULL || !pv_is_w(sp))
1004                 return 0; /* no op */
1005         if(pv_alter_context(sp) && is_route_type(LOCAL_ROUTE))
1006                 return 0; /* no op */
1007         return sp->setf(msg, &sp->pvp, op, value);
1008 }
1009
1010 /**
1011  *
1012  */
1013 int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1014 {
1015         int n;
1016         pv_value_t tok;
1017         pv_elem_p it;
1018         char *cur;
1019         
1020         if(msg==NULL || list==NULL || buf==NULL || len==NULL)
1021                 return -1;
1022
1023         if(*len <= 0)
1024                 return -1;
1025
1026         *buf = '\0';
1027         cur = buf;
1028         
1029         n = 0;
1030         for (it=list; it; it=it->next)
1031         {
1032                 /* put the text */
1033                 if(it->text.s && it->text.len>0)
1034                 {
1035                         if(n+it->text.len < *len)
1036                         {
1037                                 memcpy(cur, it->text.s, it->text.len);
1038                                 n += it->text.len;
1039                                 cur += it->text.len;
1040                         } else {
1041                                 LM_ERR("no more space for text [%d]\n", it->text.len);
1042                                 goto overflow;
1043                         }
1044                 }
1045                 /* put the value of the specifier */
1046                 if(it->spec.type!=PVT_NONE
1047                                 && pv_get_spec_value(msg, &(it->spec), &tok)==0)
1048                 {
1049                         if(tok.flags&PV_VAL_NULL)
1050                                 tok.rs = pv_str_null;
1051                         if(n+tok.rs.len < *len)
1052                         {
1053                                 if(tok.rs.len>0)
1054                                 {
1055                                         memcpy(cur, tok.rs.s, tok.rs.len);
1056                                         n += tok.rs.len;
1057                                         cur += tok.rs.len;
1058                                 }
1059                         } else {
1060                                 LM_ERR("no more space for spec value\n");
1061                                 goto overflow;
1062                         }
1063                 }
1064         }
1065
1066         goto done;
1067         
1068 overflow:
1069         LM_ERR("buffer overflow -- increase the buffer size...\n");
1070         return -1;
1071
1072 done:
1073 #ifdef EXTRA_DEBUG
1074         LM_DBG("final buffer length %d\n", n);
1075 #endif
1076         *cur = '\0';
1077         *len = n;
1078         return 0;
1079 }
1080
1081 /**
1082  *
1083  */
1084 pvname_list_t* parse_pvname_list(str *in, unsigned int type)
1085 {
1086         pvname_list_t* head = NULL;
1087         pvname_list_t* al = NULL;
1088         pvname_list_t* last = NULL;
1089         char *p;
1090         pv_spec_t spec;
1091         str s;
1092
1093         if(in==NULL || in->s==NULL)
1094         {
1095                 LM_ERR("bad parameters\n");
1096                 return NULL;
1097         }
1098
1099         p = in->s;
1100         while(is_in_str(p, in))
1101         {
1102                 while(is_in_str(p, in) && (*p==' '||*p=='\t'||*p==','||*p==';'))
1103                         p++;
1104                 if(!is_in_str(p, in))
1105                 {
1106                         if(head==NULL)
1107                                 LM_ERR("wrong item name list [%.*s]\n", in->len, in->s);
1108                         return head;
1109                 }
1110                 s.s=p;
1111                 s.len = in->s+in->len-p;
1112                 p = pv_parse_spec(&s, &spec);
1113                 if(p==NULL || (type && spec.type!=type))
1114                 {
1115                         LM_ERR("wrong item name list [%.*s]!\n", in->len, in->s);
1116                         goto error;
1117                 }
1118                 al = (pvname_list_t*)pkg_malloc(sizeof(pvname_list_t));
1119                 if(al==NULL)
1120                 {
1121                         LM_ERR("no more memory!\n");
1122                         goto error;
1123                 }
1124                 memset(al, 0, sizeof(pvname_list_t));
1125                 memcpy(&al->sname, &spec, sizeof(pv_spec_t));
1126
1127                 if(last==NULL)
1128                 {
1129                         head = al;
1130                         last = al;
1131                 } else {
1132                         last->next = al;
1133                         last = al;
1134                 }
1135         }
1136
1137         return head;
1138
1139 error:
1140         while(head)
1141         {
1142                 al = head;
1143                 head=head->next;
1144                 pkg_free(al);
1145         }
1146         return NULL;
1147 }
1148
1149
1150
1151 /** destroy the content of pv_spec_t structure.
1152  */
1153 void pv_spec_destroy(pv_spec_t *spec)
1154 {
1155         if(spec==0) return;
1156         /* free name if it is PV */
1157         if(spec->pvp.pvn.nfree)
1158                 spec->pvp.pvn.nfree((void*)(&spec->pvp.pvn));
1159         if(spec->trans)
1160                 tr_free((trans_t*)spec->trans);
1161 }
1162
1163 /** free the pv_spec_t structure.
1164  */
1165 void pv_spec_free(pv_spec_t *spec)
1166 {
1167         if(spec==0) return;
1168         pv_spec_destroy(spec);
1169         pkg_free(spec);
1170 }
1171
1172 /**
1173  *
1174  */
1175 int pv_elem_free_all(pv_elem_p log)
1176 {
1177         pv_elem_p t;
1178         while(log)
1179         {
1180                 t = log;
1181                 log = log->next;
1182                 pkg_free(t);
1183         }
1184         return 0;
1185 }
1186
1187 /**
1188  *
1189  */
1190 void pv_value_destroy(pv_value_t *val)
1191 {
1192         if(val==0) return;
1193         if(val->flags&PV_VAL_PKG) pkg_free(val->rs.s);
1194         if(val->flags&PV_VAL_SHM) shm_free(val->rs.s);
1195         memset(val, 0, sizeof(pv_value_t));
1196 }
1197
1198 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
1199 {
1200         s->s = pv_get_buffer();
1201         s->len = pv_get_buffer_size();
1202         return pv_printf( msg, list, s->s, &s->len);
1203 }
1204
1205 /********************************************************
1206  * Transformations API
1207  ********************************************************/
1208
1209 /**
1210  *
1211  */
1212 static inline char* tr_get_class(str *in, char *p, str *tclass)
1213 {
1214         tclass->s = p;
1215         while(is_in_str(p, in) && *p!=TR_CLASS_MARKER) p++;
1216         if(*p!=TR_CLASS_MARKER || tclass->s == p)
1217         {
1218                 LM_ERR("invalid transformation: %.*s (%c)!\n", in->len, in->s, *p);
1219                 return NULL;
1220         }
1221         tclass->len = p - tclass->s;
1222         p++;
1223
1224         return p;
1225 }
1226
1227 /**
1228  *
1229  */
1230 static inline trans_t* tr_new(void)
1231 {
1232         trans_t *t = NULL;
1233
1234         t = (trans_t*)pkg_malloc(sizeof(trans_t));
1235         if(t == NULL)
1236         {
1237                 LM_ERR("no more private memory\n");
1238                 return NULL;
1239         }
1240         memset(t, 0, sizeof(trans_t));
1241         return t;
1242 }
1243
1244 char* tr_lookup(str *in, trans_t **tr)
1245 {
1246         char *p;
1247         char *p0;
1248         str tclass;
1249         tr_export_t *te = NULL; 
1250         trans_t *t = NULL;
1251         trans_t *t0 = NULL;
1252         str s;
1253
1254         if(in==NULL || in->s==NULL || tr==NULL)
1255                 return NULL;
1256         
1257         p = in->s;
1258         do {
1259                 while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
1260                 if(*p != TR_LBRACKET)
1261                         break;
1262                 p++;
1263
1264                 if((t = tr_new())==NULL) return NULL;
1265
1266                 if(t0==NULL) *tr = t;
1267                 else t0->next = t;
1268                 t0 = t;
1269
1270                 /* find transformation class */
1271                 p = tr_get_class(in, p, &tclass);
1272                 if(p==NULL) goto error;
1273
1274                 /* locate transformation */
1275                 te = tr_lookup_class(&tclass);
1276                 if(te==NULL)
1277                 {
1278                         LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
1279                                 tclass.len, tclass.s, in->len, in->s);
1280                         goto error;
1281                 }
1282
1283                 s.s = p; s.len = in->s + in->len - p;
1284                 p0 = te->tparse(&s, t);
1285                 if(p0==NULL)
1286                         goto error;
1287                 p = p0;
1288
1289                 if(*p != TR_RBRACKET)
1290                 {
1291                         LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
1292                                         in->s, *p);
1293                         goto error;
1294                 }
1295
1296                 p++;
1297                 if(!is_in_str(p, in))
1298                         break;
1299         } while(1);
1300
1301         return p;
1302 error:
1303         LM_ERR("error parsing [%.*s]\n", in->len, in->s);
1304         t = *tr;
1305         while(t)
1306         {
1307                 t0 = t;
1308                 t = t->next;
1309                 tr_destroy(t0);
1310                 pkg_free(t0);
1311         }
1312         return NULL;
1313 }
1314
1315 /*!
1316  * \brief Destroy transformation including eventual parameter
1317  * \param t transformation
1318  */
1319 void tr_destroy(trans_t *t)
1320 {
1321         tr_param_t *tp;
1322         tr_param_t *tp0;
1323         if(t==NULL) return;
1324
1325         tp = t->params;
1326         while(tp)
1327         {
1328                 tp0 = tp;
1329                 tp = tp->next;
1330                 tr_param_free(tp0);
1331         }
1332         memset(t, 0, sizeof(trans_t));
1333 }
1334
1335 /*!
1336  * \brief Exec transformation on a pseudo-variable value
1337  * \param msg SIP message
1338  * \param tr one or more transformations
1339  * \param val pseudo-variable value
1340  * \return 0 on success, -1 on error
1341  */
1342
1343 int tr_exec(struct sip_msg *msg, trans_t *t, pv_value_t *v)
1344 {
1345         int r;
1346         trans_t *i;
1347
1348         if(t==NULL || v==NULL)
1349         {
1350                 LM_DBG("invalid parameters\n");
1351                 return -1;
1352         }
1353         
1354         for(i = t; i!=NULL; i=i->next)
1355         {
1356                 r = (*i->trf)(msg, i->params, i->subtype, v);
1357                 if(r!=0)
1358                         return r;
1359         }
1360         return 0;
1361 }
1362
1363 /*!
1364  * \brief Free allocated memory of transformation list
1365  * \param t transformation list
1366  */
1367 void tr_free(trans_t *t)
1368 {
1369         trans_t *t0;
1370
1371         while(t)
1372         {
1373                 t0 = t;
1374                 t = t->next;
1375                 tr_destroy(t0);
1376                 pkg_free(t0);
1377         }
1378 }
1379
1380
1381 /*!
1382  * \brief Free transformation parameter list
1383  * \param tp transformation list
1384  */
1385 void tr_param_free(tr_param_t *tp)
1386 {
1387         tr_param_t *tp0;
1388
1389         if(tp==NULL) return;
1390         while(tp)
1391         {
1392                 tp0 = tp;
1393                 tp = tp->next;
1394                 if(tp0->type==TR_PARAM_SPEC)
1395                         pv_spec_free((pv_spec_t*)tp0->v.data);
1396                 pkg_free(tp0);
1397         }
1398 }
1399
1400 typedef struct _tr_item
1401 {
1402         tr_export_t tre;
1403         unsigned int trid;
1404         struct _tr_item *next;
1405 } tr_item_t, *tr_item_p;
1406
1407 static tr_item_t* _tr_table[TR_TABLE_SIZE];
1408 static int _tr_table_set = 0;
1409
1410
1411 /**
1412  *
1413  */
1414 void tr_init_table(void)
1415 {
1416         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1417         _tr_table_set = 1;
1418 }
1419
1420 /**
1421  *
1422  */
1423 int tr_table_add(tr_export_t *e)
1424 {
1425         tr_item_t *tri = NULL;
1426         tr_item_t *trj = NULL;
1427         tr_item_t *trn = NULL;
1428         int found;
1429         unsigned int trid;
1430
1431         if(e==NULL || e->tclass.s==NULL)
1432         {
1433                 LM_ERR("invalid parameters\n");
1434                 return -1;
1435         }
1436         
1437         if(_tr_table_set==0)
1438         {
1439                 LM_DBG("TR table not initialized, doing it now\n");
1440                 tr_init_table();
1441         }
1442
1443         found = 0;
1444         // trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1445         trid = get_hash1_raw(e->tclass.s, e->tclass.len);
1446
1447         tri = _tr_table[trid%TR_TABLE_SIZE];
1448         while(tri)
1449         {
1450                 if(tri->tre.tclass.len==e->tclass.len)
1451                 {
1452                         found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
1453                         if(found==0)
1454                         {
1455                                 LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
1456                                                 e->tclass.s);
1457                                 return -1;
1458                         }
1459                 }
1460                 trj = tri;
1461                 tri = tri->next;
1462         }
1463
1464         trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
1465         if(trn==0)
1466         {
1467                 LM_ERR("no more memory\n");
1468                 return -1;
1469         }
1470         memset(trn, 0, sizeof(tr_item_t));
1471         memcpy(&(trn->tre), e, sizeof(tr_export_t));
1472         trn->trid = trid;
1473
1474         //LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
1475         //                                      e->tclass.s, trid%TR_TABLE_SIZE);
1476         if(trj==0)
1477         {
1478                 trn->next = _tr_table[trid%TR_TABLE_SIZE];
1479                 _tr_table[trid%TR_TABLE_SIZE] = trn;
1480                 goto done;
1481         }
1482         trn->next = trj->next;
1483         trj->next = trn;
1484
1485 done:
1486         return 0;
1487 }
1488
1489 /**
1490  *
1491  */
1492 int register_trans_mod(char *mod_name, tr_export_t *items)
1493 {
1494         int ret;
1495         int i;
1496
1497         if (items==0)
1498                 return 0;
1499
1500         for ( i=0 ; items[i].tclass.s ; i++ ) {
1501                 ret = tr_table_add(&items[i]);
1502                 if (ret!=0) {
1503                         LM_ERR("failed to register pseudo-variable <%.*s> for module %s\n",
1504                                         items[i].tclass.len, items[i].tclass.s, mod_name);
1505                 }
1506         }
1507         return 0;
1508 }
1509
1510 /**
1511  *
1512  */
1513 int tr_table_free(void)
1514 {
1515         tr_item_p te;
1516         tr_item_p te1;
1517         int i;
1518
1519         for(i=0; i<TR_TABLE_SIZE; i++)
1520         {
1521                 te = _tr_table[i];
1522                 while(te!=0)
1523                 {
1524                         te1 = te;
1525                         te = te->next;
1526                         pkg_free(te1);
1527                 }
1528         }
1529         memset(_tr_table, 0, sizeof(tr_item_t*)*TR_TABLE_SIZE);
1530         _tr_table_set = 0;
1531         
1532         return 0;
1533 }
1534
1535 /**
1536  *
1537  */
1538 tr_export_t* tr_lookup_class(str *tclass)
1539 {
1540         tr_item_t *tri;
1541         unsigned int trid;
1542
1543         if(tclass==0 || tclass->s==0)
1544         {
1545                 LM_ERR("bad parameters\n");
1546                 return NULL;
1547         }
1548
1549         /* search in TR table */
1550         // trid = get_hash1_raw(tclass->s, tclass->len);
1551         trid = get_hash1_raw(tclass->s, tclass->len);
1552         tri = _tr_table[trid%TR_TABLE_SIZE];
1553         while(tri)
1554         {
1555                 if(tri->trid==trid && tri->tre.tclass.len==tclass->len
1556                                 && memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
1557                         return &(tri->tre);
1558                 tri = tri->next;
1559         }
1560
1561         return NULL;
1562 }
1563
1564
1565 /********************************************************
1566  * core PVs, initialization and destroy APIs
1567  ********************************************************/
1568
1569 static pv_export_t _core_pvs[] = {
1570         {{"null", (sizeof("null")-1)}, /* */
1571                 PVT_NULL, pv_get_null, 0,
1572                 0, 0, 0, 0},
1573
1574         { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
1575 };
1576
1577 /** init pv api (optional).
1578  * @return 0 on success, -1 on error
1579  */
1580 int pv_init_api(void)
1581 {
1582         pv_init_table();
1583         tr_init_table();
1584         if(pv_init_buffer()<0)
1585                 return -1;
1586         if(register_pvars_mod("core", _core_pvs)<0)
1587                 return -1;
1588         return 0;
1589 }
1590
1591
1592 /** destroy pv api. */
1593 void pv_destroy_api(void)
1594 {
1595         /* free PV and TR hash tables */
1596         pv_table_free();
1597         tr_table_free();
1598         pv_destroy_buffer();
1599         return;
1600 }
1601
1602 /**
1603  * - buffer to print PVs
1604  */
1605 static char **_pv_print_buffer = NULL;
1606 #define PV_DEFAULT_PRINT_BUFFER_SIZE 1024
1607 static int _pv_print_buffer_size  = PV_DEFAULT_PRINT_BUFFER_SIZE;
1608 /* 6 mod params + 4 direct usage from mods */
1609 #define PV_DEFAULT_PRINT_BUFFER_SLOTS 10
1610 static int _pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
1611 static int _pv_print_buffer_index = 0;
1612
1613 /**
1614  *
1615  */
1616 int pv_init_buffer(void)
1617 {
1618         int i;
1619
1620         /* already initialized ?!? */
1621         if(_pv_print_buffer!=NULL)
1622                 return 0;
1623
1624         _pv_print_buffer =
1625                 (char**)pkg_malloc(_pv_print_buffer_slots*sizeof(char*));
1626         if(_pv_print_buffer==NULL)
1627         {
1628                 LM_ERR("cannot init PV print buffer slots\n");
1629                 return -1;
1630         }
1631         memset(_pv_print_buffer, 0, _pv_print_buffer_slots*sizeof(char*));
1632         for(i=0; i<_pv_print_buffer_slots; i++)
1633         {
1634                 _pv_print_buffer[i] =
1635                         (char*)pkg_malloc(_pv_print_buffer_size*sizeof(char));
1636                 if(_pv_print_buffer[i]==NULL)
1637                 {
1638                         LM_ERR("cannot init PV print buffer slot[%d]\n", i);
1639                         return -1;
1640                 }
1641         }
1642         LM_DBG("PV print buffer initialized to [%d][%d]\n",
1643                         _pv_print_buffer_slots, _pv_print_buffer_size);
1644         return 0;
1645 }
1646
1647 /**
1648  *
1649  */
1650 void pv_destroy_buffer(void)
1651 {
1652         int i;
1653
1654         if(_pv_print_buffer==NULL)
1655                 return;
1656         for(i=0; i<_pv_print_buffer_slots; i++)
1657         {
1658                 if(_pv_print_buffer[i]!=NULL)
1659                         pkg_free(_pv_print_buffer[i]);
1660         }
1661         pkg_free(_pv_print_buffer);
1662         _pv_print_buffer = NULL;
1663 }
1664
1665 /**
1666  *
1667  */
1668 int pv_reinit_buffer(void)
1669 {
1670         if(_pv_print_buffer_size==PV_DEFAULT_PRINT_BUFFER_SIZE
1671                         && _pv_print_buffer_slots==PV_DEFAULT_PRINT_BUFFER_SLOTS)
1672                 return 0;
1673         pv_destroy_buffer();
1674         return pv_init_buffer();
1675 }
1676
1677 /**
1678  *
1679  */
1680 char* pv_get_buffer(void)
1681 {
1682         char *p;
1683
1684         p = _pv_print_buffer[_pv_print_buffer_index];
1685         _pv_print_buffer_index = (_pv_print_buffer_index+1)%_pv_print_buffer_slots;
1686
1687         return p;
1688 }
1689
1690 /**
1691  *
1692  */
1693 int pv_get_buffer_size(void)
1694 {
1695         return _pv_print_buffer_size;
1696 }
1697
1698 /**
1699  *
1700  */
1701 int pv_get_buffer_slots(void)
1702 {
1703         return _pv_print_buffer_slots;
1704 }
1705
1706 /**
1707  *
1708  */
1709 void pv_set_buffer_size(int n)
1710 {
1711         _pv_print_buffer_size = n;
1712         if(_pv_print_buffer_size<=0)
1713                 _pv_print_buffer_size = PV_DEFAULT_PRINT_BUFFER_SIZE;
1714 }
1715
1716 /**
1717  *
1718  */
1719 void pv_set_buffer_slots(int n)
1720 {
1721         _pv_print_buffer_slots = n;
1722         if(_pv_print_buffer_slots<=0)
1723                 _pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
1724 }