1 |
commit: 55e64136dc0bf63a563da1faff1e7a4fb1ce6a9c |
2 |
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Dec 31 09:03:16 2019 +0000 |
4 |
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Dec 31 09:03:16 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=55e64136 |
7 |
|
8 |
main: replace iniparser by a small bit of custom code |
9 |
|
10 |
This make portage-utils dep-free (when qmanifest/qtegrity are not |
11 |
compiled in) again, thus easier for the static binary case. |
12 |
|
13 |
Small bonus is that the repo order is now deterministic (as found in the |
14 |
config file(s)), which may help reduce test differences. |
15 |
|
16 |
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> |
17 |
|
18 |
TODO.md | 3 +- |
19 |
main.c | 89 ++++++++++++++++++++++++++++++----------- |
20 |
tests/qmanifest/manifest00.good | 2 +- |
21 |
3 files changed, 69 insertions(+), 25 deletions(-) |
22 |
|
23 |
diff --git a/TODO.md b/TODO.md |
24 |
index 00ccdea..dffaa91 100644 |
25 |
--- a/TODO.md |
26 |
+++ b/TODO.md |
27 |
@@ -19,7 +19,8 @@ |
28 |
to use foreach\_pkg and get\_atom -- set is ready for storing objects |
29 |
now |
30 |
|
31 |
-- implement our own iniparser so we *can* be dep-free |
32 |
+- replace all strtok by strtok\_r, because the latter is already used, |
33 |
+ so we can |
34 |
|
35 |
# Atoms |
36 |
|
37 |
|
38 |
diff --git a/main.c b/main.c |
39 |
index f0a8841..c6ef973 100644 |
40 |
--- a/main.c |
41 |
+++ b/main.c |
42 |
@@ -10,7 +10,6 @@ |
43 |
#include "main.h" |
44 |
#include "applets.h" |
45 |
|
46 |
-#include <iniparser.h> |
47 |
#include <xalloc.h> |
48 |
#include <assert.h> |
49 |
#include <ctype.h> |
50 |
@@ -540,7 +539,7 @@ read_portage_profile(const char *profile, env_vars vars[], set *masks) |
51 |
* treat parent profiles as defaults, that can be overridden by |
52 |
* *this* profile. */ |
53 |
strcpy(profile_file + profile_len, "parent"); |
54 |
- if (eat_file(profile_file, &buf, &buf_len) == 0) { |
55 |
+ if (!eat_file(profile_file, &buf, &buf_len)) { |
56 |
if (buf != NULL) |
57 |
free(buf); |
58 |
return; |
59 |
@@ -637,37 +636,82 @@ set *package_masks = NULL; |
60 |
static void |
61 |
read_one_repos_conf(const char *repos_conf, char **primary) |
62 |
{ |
63 |
- int nsec; |
64 |
- char *conf; |
65 |
char rrepo[_Q_PATH_MAX]; |
66 |
- const char *main_repo; |
67 |
- const char *repo; |
68 |
- const char *path; |
69 |
- dictionary *dict; |
70 |
+ char *main_repo; |
71 |
+ char *repo; |
72 |
+ char *buf = NULL; |
73 |
+ size_t buf_len = 0; |
74 |
+ char *s; |
75 |
+ char *p; |
76 |
+ char *q; |
77 |
+ char *r; |
78 |
+ char *e; |
79 |
+ bool do_trim; |
80 |
+ bool is_default; |
81 |
|
82 |
if (getenv("DEBUG")) |
83 |
fprintf(stderr, " parse %s\n", repos_conf); |
84 |
|
85 |
- dict = iniparser_load(repos_conf); |
86 |
+ if (!eat_file(repos_conf, &buf, &buf_len)) { |
87 |
+ if (buf != NULL) |
88 |
+ free(buf); |
89 |
+ return; |
90 |
+ } |
91 |
|
92 |
- main_repo = iniparser_getstring(dict, "DEFAULT:main-repo", NULL); |
93 |
+ main_repo = NULL; |
94 |
+ repo = NULL; |
95 |
+ for (p = strtok_r(buf, "\n", &s); p != NULL; p = strtok_r(NULL, "\n", &s)) |
96 |
+ { |
97 |
+ /* trim trailing whitespace, remove comments, locate = */ |
98 |
+ do_trim = true; |
99 |
+ e = NULL; |
100 |
+ for (r = q = s - 2; q >= p; q--) { |
101 |
+ if (do_trim && isspace((int)*q)) { |
102 |
+ *q = '\0'; |
103 |
+ r = q - 1; |
104 |
+ } else if (*q == '#') { |
105 |
+ do_trim = true; |
106 |
+ *q = '\0'; |
107 |
+ r = q - 1; |
108 |
+ } else { |
109 |
+ if (*q == '=') |
110 |
+ e = q; |
111 |
+ do_trim = false; |
112 |
+ } |
113 |
+ } |
114 |
+ /* make q point to the last char */ |
115 |
+ q = r; |
116 |
|
117 |
- nsec = iniparser_getnsec(dict); |
118 |
- while (nsec-- > 0) { |
119 |
- repo = iniparser_getsecname(dict, nsec); |
120 |
- if (strcmp(repo, "DEFAULT") == 0) /* already handled above */ |
121 |
+ if (*p == '[' && *q == ']') { /* section header */ |
122 |
+ repo = p + 1; |
123 |
+ *q = '\0'; |
124 |
+ is_default = strcmp(repo, "DEFAULT") == 0; |
125 |
+ continue; |
126 |
+ } else if (*p == '\0') { /* empty line */ |
127 |
+ continue; |
128 |
+ } else if (e == NULL) { /* missing = */ |
129 |
continue; |
130 |
+ } else if (repo == NULL) { /* not in a section */ |
131 |
+ continue; |
132 |
+ } |
133 |
|
134 |
- xasprintf(&conf, "%s:location", repo); |
135 |
- path = iniparser_getstring(dict, conf, NULL); |
136 |
- if (path) { |
137 |
+ /* trim off whitespace before = */ |
138 |
+ for (r = e - 1; r >= p && isspace((int)*r); r--) |
139 |
+ *r = '\0'; |
140 |
+ /* and after the = */ |
141 |
+ for (e++; e < q && isspace((int)*e); e++) |
142 |
+ ; |
143 |
+ |
144 |
+ if (is_default && strcmp(p, "main-repo") == 0) { |
145 |
+ main_repo = e; |
146 |
+ } else if (!is_default && strcmp(p, "location") == 0) { |
147 |
void *ele; |
148 |
size_t n; |
149 |
char *overlay; |
150 |
|
151 |
/* try not to get confused by symlinks etc. */ |
152 |
- if (realpath(path, rrepo) != NULL) |
153 |
- path = rrepo; |
154 |
+ if (realpath(e, rrepo) != NULL) |
155 |
+ e = rrepo; |
156 |
|
157 |
array_for_each(overlay_names, n, overlay) { |
158 |
if (strcmp(overlay, repo) == 0) |
159 |
@@ -681,19 +725,18 @@ read_one_repos_conf(const char *repos_conf, char **primary) |
160 |
array_get_elem(overlay_src, n) = xstrdup(repos_conf); |
161 |
ele = array_get_elem(overlays, n); |
162 |
free(ele); |
163 |
- ele = array_get_elem(overlays, n) = xstrdup(path); |
164 |
+ ele = array_get_elem(overlays, n) = xstrdup(e); |
165 |
} else { |
166 |
- ele = xarraypush_str(overlays, path); |
167 |
+ ele = xarraypush_str(overlays, e); |
168 |
overlay = xarraypush_str(overlay_names, repo); |
169 |
xarraypush_str(overlay_src, repos_conf); |
170 |
} |
171 |
if (main_repo && strcmp(repo, main_repo) == 0) |
172 |
*primary = overlay; |
173 |
} |
174 |
- free(conf); |
175 |
} |
176 |
|
177 |
- iniparser_freedict(dict); |
178 |
+ free(buf); |
179 |
} |
180 |
|
181 |
/* Handle a possible directory of files. */ |
182 |
|
183 |
diff --git a/tests/qmanifest/manifest00.good b/tests/qmanifest/manifest00.good |
184 |
index 31ab76f..0e89a93 100644 |
185 |
--- a/tests/qmanifest/manifest00.good |
186 |
+++ b/tests/qmanifest/manifest00.good |
187 |
@@ -1,2 +1,2 @@ |
188 |
-not_a_tree: /notatree |
189 |
bad_tree: /simpletree (main) |
190 |
+not_a_tree: /notatree |