11 char *checksum_field=NULL;
13 static void oom_die(void)
15 fputs("Out of memory!\n", stderr);
19 static char *xvasprintf(const char *fmt, va_list ap) {
22 if (vasprintf (&ret, fmt, ap) < 0) {
30 static char *xasprintf(const char *fmt, ...) {
35 ret = xvasprintf(fmt, ap);
40 static char *fieldcpy(char *dst, char *fld) {
41 while (*fld && *fld != ':')
45 while (isspace(*fld)) fld++;
46 return strcpy(dst, fld);
49 static void outputdeps(char *deps) {
53 while (isspace(*pch)) pch++;
56 while (*pch && *pch != '(' && *pch != '|' && *pch != ','
59 fputc(*pch++, stdout);
62 while (*pch && *pch++ != ',') (void)NULL;
66 static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
68 char cur_pkg[MAX_LINE];
69 char cur_deps[MAX_LINE];
70 char cur_predeps[MAX_LINE];
71 char prev_pkg[MAX_LINE];
78 cur_pkg[0] = cur_deps[0] = cur_predeps[0] = prev_pkg[0] = '\0';
80 for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
82 f = fopen(pkgsfile, "r");
89 while (fgets(buf, sizeof(buf), f)) {
90 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
91 if (strncasecmp(buf, "Package:", 8) == 0) {
94 fieldcpy(cur_pkg, buf);
95 if (strcmp(cur_pkg, prev_pkg) != 0) {
97 pkgs[output_pkg] = NULL;
101 outputdeps(cur_predeps);
102 strcpy(prev_pkg, cur_pkg);
104 cur_deps[0] = cur_predeps[0] = '\0';
106 for (i = 0; i < pkgc; i++) {
107 if (!pkgs[i]) continue;
109 if (strcmp(cur_pkg, pkgs[i]) == 0) {
116 } else if (!skip && strncasecmp(buf, "Depends:", 8) == 0)
117 fieldcpy(cur_deps, buf);
118 else if (!skip && strncasecmp(buf, "Pre-Depends:", 12) == 0)
119 fieldcpy(cur_predeps, buf);
122 outputdeps(cur_deps);
124 outputdeps(cur_predeps);
128 static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
129 char *fieldname, char **in_pkgs, int pkgc)
132 char cur_field[MAX_LINE];
133 char cur_pkg[MAX_LINE];
134 char cur_ver[MAX_LINE];
135 char cur_arch[MAX_LINE];
136 char cur_size[MAX_LINE];
137 char cur_checksum[MAX_LINE];
138 char cur_filename[MAX_LINE];
139 char prev_pkg[MAX_LINE];
140 char *pkgs[MAX_PKGS];
146 cur_field[0] = cur_pkg[0] = cur_ver[0] = cur_arch[0] = cur_filename[0] = prev_pkg[0] = '\0';
148 for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
150 f = fopen(pkgsfile, "r");
155 while (fgets(buf, sizeof(buf), f)) {
156 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
157 if (strncasecmp(buf, fieldname, strlen(fieldname)) == 0) {
158 fieldcpy(cur_field, buf);
160 if (strncasecmp(buf, "Package:", 8) == 0) {
161 fieldcpy(cur_pkg, buf);
162 if (strcmp(cur_pkg, prev_pkg) != 0) {
164 fputs(output, stdout);
165 if (uniq && output_pkg != -1)
166 pkgs[output_pkg] = NULL;
167 strcpy(prev_pkg, cur_pkg);
172 } else if (strncasecmp(buf, "Version:", 8) == 0) {
173 fieldcpy(cur_ver, buf);
174 } else if (strncasecmp(buf, "Architecture:", 13) == 0) {
175 fieldcpy(cur_arch, buf);
176 } else if (strncasecmp(buf, "Size:", 5) == 0) {
177 fieldcpy(cur_size, buf);
178 } else if (strncasecmp(buf, checksum_field, strlen(checksum_field)) == 0
179 && buf[strlen(checksum_field)] == ':') {
180 fieldcpy(cur_checksum, buf);
181 } else if (strncasecmp(buf, "Filename:", 9) == 0) {
182 fieldcpy(cur_filename, buf);
185 for (i = 0; i < pkgc; i++) {
186 if (!pkgs[i]) continue;
188 if (strcmp(cur_field, pkgs[i]) == 0) {
190 output = xasprintf("%s %s %s %s %s %s %s\n", cur_pkg, cur_ver, cur_arch, mirror, cur_filename, cur_checksum, cur_size);
200 fputs(output, stdout);
201 if (uniq && output_pkg != -1)
202 pkgs[output_pkg] = NULL;
205 /* any that weren't found are returned as "pkg -" */
207 for (i = 0; i < pkgc; i++) {
209 printf("%s -\n", pkgs[i]);
215 static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
219 size_t accum_size = 0, accum_alloc = MAX_LINE * 2;
220 char cur_pkg[MAX_LINE];
223 accum = malloc(accum_alloc);
227 f = fopen(pkgsfile, "r");
233 while (fgets(buf, sizeof(buf), f)) {
235 size_t len = strlen(buf);
236 if (accum_size + len + 1 > accum_alloc) {
237 accum_alloc = (accum_size + len + 1) * 2;
238 accum = realloc(accum, accum_alloc);
242 strcpy(accum + accum_size, buf);
245 if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
246 if (strncasecmp(buf, "Package:", 8) == 0) {
247 fieldcpy(cur_pkg, buf);
250 for (i = 0; i < pkgc; i++) {
251 if (!pkgs[i]) continue;
252 if (strcmp(cur_pkg, pkgs[i]) == 0) {
253 fputs(accum, stdout);
254 if (accum[accum_size - 1] != '\n')
255 fputs("\n\n", stdout);
256 else if (accum[accum_size - 2] != '\n')
270 static int dotranslatewgetpercent(int low, int high, int end, char *str) {
274 /* print out anything that looks like a % on its own line, appropriately
278 while ( (ch = getchar()) != EOF ) {
280 val *= 10; val += ch - '0';
281 } else if (ch == '%') {
282 float f = (float) val / 100.0 * (high - low) + low;
284 printf("P: %d %d %s\n", (int) f, end, str);
286 printf("P: %d %d\n", (int) f, end);
293 return lastval == 100;
296 int main(int argc, char *argv[]) {
297 checksum_field=getenv("DEBOOTSTRAP_CHECKSUM_FIELD");
298 if (checksum_field == NULL) {
299 checksum_field="MD5sum";
302 if ((argc == 6 || argc == 5) && strcmp(argv[1], "WGET%") == 0) {
303 if (dotranslatewgetpercent(atoi(argv[2]), atoi(argv[3]),
304 atoi(argv[4]), argc == 6 ? argv[5] : NULL))
310 } else if (argc >= 4 && strcmp(argv[1], "GETDEPS") == 0) {
312 for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
313 dogetdeps(argv[2], argv+i, MAX_PKGS);
315 dogetdeps(argv[2], argv+i, argc-i);
317 } else if (argc >= 5 && strcmp(argv[1], "PKGS") == 0) {
319 for (i = 4; argc - i > MAX_PKGS; i += MAX_PKGS) {
320 dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, MAX_PKGS);
322 dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, argc-i);
324 } else if (argc >= 6 && strcmp(argv[1], "FIELD") == 0) {
326 for (i = 5; argc - i > MAX_PKGS; i += MAX_PKGS) {
327 dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, MAX_PKGS);
329 dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, argc-i);
331 } else if (argc >= 4 && strcmp(argv[1], "STANZAS") == 0) {
333 for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
334 dopkgstanzas(argv[2], argv+i, MAX_PKGS);
336 dopkgstanzas(argv[2], argv+i, argc-i);
339 fprintf(stderr, "usage: %s PKGS mirror packagesfile pkgs..\n", argv[0]);
340 fprintf(stderr, " or: %s FIELD field mirror packagesfile pkgs..\n",
342 fprintf(stderr, " or: %s GETDEPS packagesfile pkgs..\n", argv[0]);
343 fprintf(stderr, " or: %s STANZAS packagesfile pkgs..\n", argv[0]);
344 fprintf(stderr, " or: %s WGET%% low high end reason\n", argv[0]);