upgrade debootstrap package
[openwrt/svn-archive/packages.git] / admin / debootstrap / files / pkgdetails.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #define MAX_LINE 1000
7 #define MAX_PKGS 100
8
9 char *checksum_field=NULL;
10
11 static char *fieldcpy(char *dst, char *fld) {
12 while (*fld && *fld != ':')
13 fld++;
14 if (!*(fld++))
15 return NULL;
16 while (isspace(*fld)) fld++;
17 return strcpy(dst, fld);
18 }
19
20 static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
21 char buf[MAX_LINE];
22 char cur_pkg[MAX_LINE];
23 char cur_deps[MAX_LINE];
24 char *pkgs[MAX_PKGS];
25 int i;
26 int skip;
27 FILE *f;
28
29 cur_pkg[0] = cur_deps[0] = '\0';
30
31 for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
32
33 f = fopen(pkgsfile, "r");
34 if (f == NULL) {
35 perror(pkgsfile);
36 exit(1);
37 }
38
39 skip = 1;
40 while (fgets(buf, sizeof(buf), f)) {
41 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
42 if (strncasecmp(buf, "Package:", 8) == 0) {
43 int any = 0;
44 skip = 1;
45 fieldcpy(cur_pkg, buf);
46 for (i = 0; i < pkgc; i++) {
47 if (!pkgs[i]) continue;
48 any = 1;
49 if (strcmp(cur_pkg, pkgs[i]) == 0) {
50 skip = 0;
51 pkgs[i] = NULL;
52 break;
53 }
54 }
55 if (!any) break;
56 } else if (!skip &&
57 (strncasecmp(buf, "Depends:", 8) == 0 ||
58 strncasecmp(buf, "Pre-Depends:", 12) == 0))
59 {
60 char *pch;
61 fieldcpy(cur_deps, buf);
62 pch = cur_deps;
63 while (1) {
64 while (isspace(*pch)) pch++;
65 if (!*pch) break;
66
67 while (*pch && *pch != '(' && *pch != '|' && *pch != ','
68 && !isspace(*pch))
69 {
70 fputc(*pch++, stdout);
71 }
72 fputc('\n', stdout);
73 while (*pch && *pch++ != ',') (void)NULL;
74 }
75 }
76 }
77 fclose(f);
78 }
79
80 static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
81 char *fieldname, char **in_pkgs, int pkgc)
82 {
83 char buf[MAX_LINE];
84 char cur_field[MAX_LINE];
85 char cur_pkg[MAX_LINE];
86 char cur_ver[MAX_LINE];
87 char cur_arch[MAX_LINE];
88 char cur_size[MAX_LINE];
89 char cur_checksum[MAX_LINE];
90 char cur_filename[MAX_LINE];
91 char *pkgs[MAX_PKGS];
92 int i;
93 FILE *f;
94
95 cur_pkg[0] = cur_ver[0] = cur_arch[0] = cur_filename[0] = '\0';
96
97 for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
98
99 f = fopen(pkgsfile, "r");
100 if (f == NULL) {
101 perror(pkgsfile);
102 exit(1);
103 }
104 while (fgets(buf, sizeof(buf), f)) {
105 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
106 if (strncasecmp(buf, fieldname, strlen(fieldname)) == 0) {
107 fieldcpy(cur_field, buf);
108 }
109 if (strncasecmp(buf, "Package:", 8) == 0) {
110 fieldcpy(cur_pkg, buf);
111 } else if (strncasecmp(buf, "Version:", 8) == 0) {
112 fieldcpy(cur_ver, buf);
113 } else if (strncasecmp(buf, "Architecture:", 13) == 0) {
114 fieldcpy(cur_arch, buf);
115 } else if (strncasecmp(buf, "Size:", 5) == 0) {
116 fieldcpy(cur_size, buf);
117 } else if (strncasecmp(buf, checksum_field, strlen(checksum_field)) == 0
118 && buf[strlen(checksum_field)] == ':') {
119 fieldcpy(cur_checksum, buf);
120 } else if (strncasecmp(buf, "Filename:", 9) == 0) {
121 fieldcpy(cur_filename, buf);
122 } else if (!*buf) {
123 int any = 0;
124 for (i = 0; i < pkgc; i++) {
125 if (!pkgs[i]) continue;
126 any = 1;
127 if (strcmp(cur_field, pkgs[i]) == 0) {
128 printf("%s %s %s %s %s %s %s\n", cur_pkg, cur_ver, cur_arch, mirror, cur_filename, cur_checksum, cur_size);
129 if (uniq) pkgs[i] = NULL;
130 break;
131 }
132 }
133 if (!any) break;
134 }
135 }
136 fclose(f);
137
138 /* any that weren't found are returned as "pkg -" */
139 if (uniq) {
140 for (i = 0; i < pkgc; i++) {
141 if (pkgs[i]) {
142 printf("%s -\n", pkgs[i]);
143 }
144 }
145 }
146 }
147
148 static void oom_die(void)
149 {
150 fprintf(stderr, "Out of memory!\n");
151 exit(1);
152 }
153
154 static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
155 {
156 char buf[MAX_LINE];
157 char *accum;
158 size_t accum_size = 0, accum_alloc = MAX_LINE * 2;
159 char cur_pkg[MAX_LINE];
160 FILE *f;
161
162 accum = malloc(accum_alloc);
163 if (!accum)
164 oom_die();
165
166 f = fopen(pkgsfile, "r");
167 if (f == NULL) {
168 perror(pkgsfile);
169 free(accum);
170 exit(1);
171 }
172 while (fgets(buf, sizeof(buf), f)) {
173 if (*buf) {
174 size_t len = strlen(buf);
175 if (accum_size + len + 1 > accum_alloc) {
176 accum_alloc = (accum_size + len + 1) * 2;
177 accum = realloc(accum, accum_alloc);
178 if (!accum)
179 oom_die();
180 }
181 strcpy(accum + accum_size, buf);
182 accum_size += len;
183 }
184 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
185 if (strncasecmp(buf, "Package:", 8) == 0) {
186 fieldcpy(cur_pkg, buf);
187 } else if (!*buf) {
188 int i;
189 for (i = 0; i < pkgc; i++) {
190 if (!pkgs[i]) continue;
191 if (strcmp(cur_pkg, pkgs[i]) == 0) {
192 fputs(accum, stdout);
193 if (accum[accum_size - 1] != '\n')
194 fputs("\n\n", stdout);
195 else if (accum[accum_size - 2] != '\n')
196 fputc('\n', stdout);
197 break;
198 }
199 }
200 *accum = '\0';
201 accum_size = 0;
202 }
203 }
204 fclose(f);
205
206 free(accum);
207 }
208
209 static int dotranslatewgetpercent(int low, int high, int end, char *str) {
210 int ch;
211 int val, lastval;
212
213 /* print out anything that looks like a % on its own line, appropriately
214 * scaled */
215
216 lastval = val = 0;
217 while ( (ch = getchar()) != EOF ) {
218 if (isdigit(ch)) {
219 val *= 10; val += ch - '0';
220 } else if (ch == '%') {
221 float f = (float) val / 100.0 * (high - low) + low;
222 if (str) {
223 printf("P: %d %d %s\n", (int) f, end, str);
224 } else {
225 printf("P: %d %d\n", (int) f, end);
226 }
227 lastval = val;
228 } else {
229 val = 0;
230 }
231 }
232 return lastval == 100;
233 }
234
235 int main(int argc, char *argv[]) {
236 checksum_field=getenv("DEBOOTSTRAP_CHECKSUM_FIELD");
237 if (checksum_field == NULL) {
238 checksum_field="MD5sum";
239 }
240
241 if ((argc == 6 || argc == 5) && strcmp(argv[1], "WGET%") == 0) {
242 if (dotranslatewgetpercent(atoi(argv[2]), atoi(argv[3]),
243 atoi(argv[4]), argc == 6 ? argv[5] : NULL))
244 {
245 exit(0);
246 } else {
247 exit(1);
248 }
249 } else if (argc >= 4 && strcmp(argv[1], "GETDEPS") == 0) {
250 int i;
251 for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
252 dogetdeps(argv[2], argv+i, MAX_PKGS);
253 }
254 dogetdeps(argv[2], argv+i, argc-i);
255 exit(0);
256 } else if (argc >= 5 && strcmp(argv[1], "PKGS") == 0) {
257 int i;
258 for (i = 4; argc - i > MAX_PKGS; i += MAX_PKGS) {
259 dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, MAX_PKGS);
260 }
261 dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, argc-i);
262 exit(0);
263 } else if (argc >= 6 && strcmp(argv[1], "FIELD") == 0) {
264 int i;
265 for (i = 5; argc - i > MAX_PKGS; i += MAX_PKGS) {
266 dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, MAX_PKGS);
267 }
268 dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, argc-i);
269 exit(0);
270 } else if (argc >= 4 && strcmp(argv[1], "STANZAS") == 0) {
271 int i;
272 for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
273 dopkgstanzas(argv[2], argv+i, MAX_PKGS);
274 }
275 dopkgstanzas(argv[2], argv+i, argc-i);
276 exit(0);
277 } else {
278 fprintf(stderr, "usage: %s PKGS mirror packagesfile pkgs..\n", argv[0]);
279 fprintf(stderr, " or: %s FIELD field mirror packagesfile pkgs..\n",
280 argv[0]);
281 fprintf(stderr, " or: %s GETDEPS packagesfile pkgs..\n", argv[0]);
282 fprintf(stderr, " or: %s STANZAS packagesfile pkgs..\n", argv[0]);
283 fprintf(stderr, " or: %s WGET%% low high end reason\n", argv[0]);
284 exit(1);
285 }
286 }