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