]>
Commit | Line | Data |
---|---|---|
25903407 ACM |
1 | /* |
2 | * (c) 2009 Arnaldo Carvalho de Melo <[email protected]> | |
3 | * | |
4 | * Licensed under the GPLv2. | |
5 | */ | |
6 | ||
7 | #include "strlist.h" | |
74cf249d | 8 | #include "util.h" |
25903407 ACM |
9 | #include <errno.h> |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
7a8ef4c4 | 13 | #include <unistd.h> |
25903407 | 14 | |
ee8dd3ca DA |
15 | static |
16 | struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry) | |
25903407 | 17 | { |
ee8dd3ca DA |
18 | const char *s = entry; |
19 | struct rb_node *rc = NULL; | |
20 | struct strlist *strlist = container_of(rblist, struct strlist, rblist); | |
21 | struct str_node *snode = malloc(sizeof(*snode)); | |
25903407 | 22 | |
ee8dd3ca DA |
23 | if (snode != NULL) { |
24 | if (strlist->dupstr) { | |
25903407 ACM |
25 | s = strdup(s); |
26 | if (s == NULL) | |
27 | goto out_delete; | |
28 | } | |
ee8dd3ca DA |
29 | snode->s = s; |
30 | rc = &snode->rb_node; | |
25903407 ACM |
31 | } |
32 | ||
ee8dd3ca | 33 | return rc; |
25903407 ACM |
34 | |
35 | out_delete: | |
ee8dd3ca | 36 | free(snode); |
25903407 ACM |
37 | return NULL; |
38 | } | |
39 | ||
d8639f06 | 40 | static void str_node__delete(struct str_node *snode, bool dupstr) |
25903407 ACM |
41 | { |
42 | if (dupstr) | |
7d16c634 | 43 | zfree((char **)&snode->s); |
d8639f06 | 44 | free(snode); |
25903407 ACM |
45 | } |
46 | ||
ee8dd3ca DA |
47 | static |
48 | void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node) | |
25903407 | 49 | { |
ee8dd3ca DA |
50 | struct strlist *slist = container_of(rblist, struct strlist, rblist); |
51 | struct str_node *snode = container_of(rb_node, struct str_node, rb_node); | |
25903407 | 52 | |
ee8dd3ca DA |
53 | str_node__delete(snode, slist->dupstr); |
54 | } | |
25903407 | 55 | |
ee8dd3ca DA |
56 | static int strlist__node_cmp(struct rb_node *rb_node, const void *entry) |
57 | { | |
58 | const char *str = entry; | |
59 | struct str_node *snode = container_of(rb_node, struct str_node, rb_node); | |
60 | ||
61 | return strcmp(snode->s, str); | |
62 | } | |
25903407 | 63 | |
d8639f06 | 64 | int strlist__add(struct strlist *slist, const char *new_entry) |
ee8dd3ca | 65 | { |
d8639f06 | 66 | return rblist__add_node(&slist->rblist, new_entry); |
25903407 ACM |
67 | } |
68 | ||
d8639f06 | 69 | int strlist__load(struct strlist *slist, const char *filename) |
25903407 ACM |
70 | { |
71 | char entry[1024]; | |
72 | int err; | |
73 | FILE *fp = fopen(filename, "r"); | |
74 | ||
75 | if (fp == NULL) | |
ab7322af | 76 | return -errno; |
25903407 ACM |
77 | |
78 | while (fgets(entry, sizeof(entry), fp) != NULL) { | |
79 | const size_t len = strlen(entry); | |
80 | ||
81 | if (len == 0) | |
82 | continue; | |
83 | entry[len - 1] = '\0'; | |
84 | ||
d8639f06 | 85 | err = strlist__add(slist, entry); |
25903407 ACM |
86 | if (err != 0) |
87 | goto out; | |
88 | } | |
89 | ||
90 | err = 0; | |
91 | out: | |
92 | fclose(fp); | |
93 | return err; | |
94 | } | |
95 | ||
ee8dd3ca | 96 | void strlist__remove(struct strlist *slist, struct str_node *snode) |
25903407 | 97 | { |
45922814 | 98 | rblist__remove_node(&slist->rblist, &snode->rb_node); |
25903407 ACM |
99 | } |
100 | ||
ee8dd3ca | 101 | struct str_node *strlist__find(struct strlist *slist, const char *entry) |
25903407 | 102 | { |
ee8dd3ca DA |
103 | struct str_node *snode = NULL; |
104 | struct rb_node *rb_node = rblist__find(&slist->rblist, entry); | |
25903407 | 105 | |
ee8dd3ca DA |
106 | if (rb_node) |
107 | snode = container_of(rb_node, struct str_node, rb_node); | |
108 | ||
109 | return snode; | |
25903407 ACM |
110 | } |
111 | ||
8ff9daf3 ACM |
112 | static int strlist__parse_list_entry(struct strlist *slist, const char *s, |
113 | const char *subst_dir) | |
25903407 | 114 | { |
8ff9daf3 ACM |
115 | int err; |
116 | char *subst = NULL; | |
117 | ||
25903407 | 118 | if (strncmp(s, "file://", 7) == 0) |
d8639f06 | 119 | return strlist__load(slist, s + 7); |
25903407 | 120 | |
8ff9daf3 ACM |
121 | if (subst_dir) { |
122 | err = -ENOMEM; | |
123 | if (asprintf(&subst, "%s/%s", subst_dir, s) < 0) | |
124 | goto out; | |
125 | ||
126 | if (access(subst, F_OK) == 0) { | |
127 | err = strlist__load(slist, subst); | |
128 | goto out; | |
129 | } | |
dd8232bc NK |
130 | |
131 | if (slist->file_only) { | |
132 | err = -ENOENT; | |
133 | goto out; | |
134 | } | |
8ff9daf3 ACM |
135 | } |
136 | ||
137 | err = strlist__add(slist, s); | |
138 | out: | |
139 | free(subst); | |
140 | return err; | |
25903407 ACM |
141 | } |
142 | ||
8816d38d | 143 | static int strlist__parse_list(struct strlist *slist, const char *s, const char *subst_dir) |
25903407 ACM |
144 | { |
145 | char *sep; | |
146 | int err; | |
147 | ||
148 | while ((sep = strchr(s, ',')) != NULL) { | |
149 | *sep = '\0'; | |
8ff9daf3 | 150 | err = strlist__parse_list_entry(slist, s, subst_dir); |
25903407 ACM |
151 | *sep = ','; |
152 | if (err != 0) | |
153 | return err; | |
154 | s = sep + 1; | |
155 | } | |
156 | ||
8ff9daf3 | 157 | return *s ? strlist__parse_list_entry(slist, s, subst_dir) : 0; |
25903407 ACM |
158 | } |
159 | ||
4a77e218 | 160 | struct strlist *strlist__new(const char *list, const struct strlist_config *config) |
25903407 | 161 | { |
d8639f06 | 162 | struct strlist *slist = malloc(sizeof(*slist)); |
25903407 | 163 | |
d8639f06 | 164 | if (slist != NULL) { |
8ff9daf3 | 165 | bool dupstr = true; |
dd8232bc | 166 | bool file_only = false; |
8ff9daf3 ACM |
167 | const char *dirname = NULL; |
168 | ||
169 | if (config) { | |
170 | dupstr = !config->dont_dupstr; | |
171 | dirname = config->dirname; | |
dd8232bc | 172 | file_only = config->file_only; |
8ff9daf3 ACM |
173 | } |
174 | ||
d8639f06 ACM |
175 | rblist__init(&slist->rblist); |
176 | slist->rblist.node_cmp = strlist__node_cmp; | |
177 | slist->rblist.node_new = strlist__node_new; | |
178 | slist->rblist.node_delete = strlist__node_delete; | |
ee8dd3ca | 179 | |
8ff9daf3 | 180 | slist->dupstr = dupstr; |
dd8232bc | 181 | slist->file_only = file_only; |
8ff9daf3 ACM |
182 | |
183 | if (list && strlist__parse_list(slist, list, dirname) != 0) | |
25903407 ACM |
184 | goto out_error; |
185 | } | |
186 | ||
d8639f06 | 187 | return slist; |
25903407 | 188 | out_error: |
d8639f06 | 189 | free(slist); |
25903407 ACM |
190 | return NULL; |
191 | } | |
192 | ||
d8639f06 | 193 | void strlist__delete(struct strlist *slist) |
25903407 | 194 | { |
d8639f06 ACM |
195 | if (slist != NULL) |
196 | rblist__delete(&slist->rblist); | |
25903407 | 197 | } |
27d0fd41 | 198 | |
ee8dd3ca | 199 | struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx) |
27d0fd41 | 200 | { |
ee8dd3ca DA |
201 | struct str_node *snode = NULL; |
202 | struct rb_node *rb_node; | |
27d0fd41 | 203 | |
ee8dd3ca DA |
204 | rb_node = rblist__entry(&slist->rblist, idx); |
205 | if (rb_node) | |
206 | snode = container_of(rb_node, struct str_node, rb_node); | |
27d0fd41 | 207 | |
ee8dd3ca | 208 | return snode; |
27d0fd41 | 209 | } |