Line data Source code
1 : #include "burp.h"
2 : #include "alloc.h"
3 : #include "log.h"
4 : #include "pathcmp.h"
5 : #include "regexp.h"
6 : #include "strlist.h"
7 :
8 : // Maybe rename this stuff to 'struct pathlist'.
9 :
10 366 : static void strlist_free(struct strlist *strlist)
11 : {
12 732 : if(!strlist) return;
13 366 : regex_free(&strlist->re);
14 366 : free_w(&strlist->path);
15 366 : free_v((void **)&strlist);
16 : }
17 :
18 1991 : void strlists_free(struct strlist **strlist)
19 : {
20 : struct strlist *s;
21 1991 : struct strlist *shead=*strlist;
22 4348 : while(shead)
23 : {
24 366 : s=shead;
25 366 : shead=shead->next;
26 366 : strlist_free(s);
27 : }
28 1991 : *strlist=NULL;
29 1991 : }
30 :
31 366 : static struct strlist *strlist_alloc(const char *path, long flag)
32 : {
33 366 : struct strlist *slnew=NULL;
34 366 : if(!path)
35 : {
36 0 : logp("%s called with NULL path!\n", __func__);
37 0 : return NULL;
38 : }
39 366 : if(!(slnew=(struct strlist *)
40 : calloc_w(1, sizeof(struct strlist), __func__))
41 366 : || !(slnew->path=strdup_w(path, __func__)))
42 0 : return NULL;
43 366 : slnew->flag=flag;
44 366 : return slnew;
45 : }
46 :
47 366 : static int do_strlist_add(struct strlist **strlist,
48 : const char *path, long flag, int sorted)
49 : {
50 366 : struct strlist *s=NULL;
51 366 : struct strlist *slast=NULL;
52 366 : struct strlist *slnew=NULL;
53 :
54 366 : if(!(slnew=strlist_alloc(path, flag))) return -1;
55 :
56 : // Insert into a sorted position in the list, or if the sorted flag
57 : // was zero, add to the end of the list.
58 : // FIX THIS: Unsorted means that it goes through the whole list to
59 : // find the last entry. Can this be made better?
60 683 : for(s=*strlist; s; s=s->next)
61 : {
62 331 : if(sorted && pathcmp(path, s->path)<0) break;
63 317 : slast=s;
64 : }
65 366 : if(slast)
66 : {
67 164 : slnew->next=slast->next;
68 164 : slast->next=slnew;
69 : }
70 : else
71 : {
72 202 : *strlist=slnew;
73 202 : slnew->next=s;
74 : }
75 :
76 366 : return 0;
77 : }
78 :
79 254 : int strlist_add(struct strlist **strlist,
80 : const char *path, long flag)
81 : {
82 254 : return do_strlist_add(strlist, path, flag, 0 /* unsorted */);
83 : }
84 :
85 112 : int strlist_add_sorted(struct strlist **strlist,
86 : const char *path, long flag)
87 : {
88 112 : return do_strlist_add(strlist, path, flag, 1 /* sorted */);
89 : }
90 :
91 124 : int strlist_compile_regexes(struct strlist *strlist)
92 : {
93 : struct strlist *l;
94 : // FIX THIS: when the regex does not compile, should remove the
95 : // strlist entry completely.
96 126 : for(l=strlist; l; l=l->next)
97 2 : if(!(l->re=regex_compile(l->path)))
98 0 : logp("unable to compile regex: %s\n", l->path);
99 124 : return 0;
100 : }
101 :
102 6 : int strlist_find(struct strlist *strlist, const char *path, long flag)
103 : {
104 : struct strlist *s;
105 12 : for(s=strlist; s; s=s->next)
106 6 : if(!strcmp(path, s->path) && flag==s->flag)
107 0 : return 1;
108 6 : return 0;
109 : }
|