Fix the MMXREG and XMMREG flags definitions.
[nasm/autotest.git] / rdoff / rdflib.c
blob6abd13cff9a3cf05c1a9925a7a883d487270ff10
1 /* rdflib - manipulate RDOFF library files (.rdl) */
3 /*
4 * an rdoff library is simply a sequence of RDOFF object files, each
5 * preceded by the name of the module, an ASCII string of up to 255
6 * characters, terminated by a zero.
8 * When a library is being created, special signature block is placed
9 * in the beginning of the file. It is a string 'RDLIB' followed by a
10 * version number, then int32_t content size and a int32_t time stamp.
11 * The module name of the signature block is '.sig'.
14 * There may be an optional directory placed on the end of the file.
15 * The format of the directory will be 'RDLDD' followed by a version
16 * number, followed by the length of the directory, and then the
17 * directory, the format of which has not yet been designed.
18 * The module name of the directory must be '.dir'.
20 * All module names beginning with '.' are reserved for possible future
21 * extensions. The linker ignores all such modules, assuming they have
22 * the format of a six uint8_t type & version identifier followed by int32_t
23 * content size, followed by data.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <time.h>
32 #include <inttypes.h>
34 /* functions supported:
35 * create a library (no extra operands required)
36 * add a module from a library (requires filename and name to give mod.)
37 * replace a module in a library (requires given name and filename)
38 * delete a module from a library (requires given name)
39 * extract a module from the library (requires given name and filename)
40 * list modules
43 const char *usage =
44 "usage:\n"
45 " rdflib x libname [extra operands]\n\n"
46 " where x is one of:\n"
47 " c - create library\n"
48 " a - add module (operands = filename module-name)\n"
49 " x - extract (module-name filename)\n"
50 " r - replace (module-name filename)\n"
51 " d - delete (module-name)\n" " t - list\n";
53 /* Library signature */
54 const char *rdl_signature = "RDLIB2", *sig_modname = ".sig";
56 char **_argv;
58 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
60 static void int32_ttolocal(int32_t *l)
62 #if _ENDIANNESS
63 uint8_t t;
64 uint8_t *p = (uint8_t *)l;
66 t = p[0];
67 p[0] = p[3];
68 p[3] = t;
69 t = p[1];
70 p[1] = p[2];
71 p[2] = p[1];
72 #else
73 (void)l; /* placate optimizers */
74 #endif
77 char copybytes(FILE * fp, FILE * fp2, int n)
79 int i, t = 0;
81 for (i = 0; i < n; i++) {
82 t = fgetc(fp);
83 if (t == EOF) {
84 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
85 _argv[2]);
86 exit(1);
88 if (fp2)
89 if (fputc(t, fp2) == EOF) {
90 fprintf(stderr, "rdflib: write error\n");
91 exit(1);
94 return (char)t; /* return last char read */
97 int32_t copyint32_t(FILE * fp, FILE * fp2)
99 int32_t l;
100 int i, t;
101 uint8_t *p = (uint8_t *)&l;
103 for (i = 0; i < 4; i++) { /* skip magic no */
104 t = fgetc(fp);
105 if (t == EOF) {
106 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
107 _argv[2]);
108 exit(1);
110 if (fp2)
111 if (fputc(t, fp2) == EOF) {
112 fprintf(stderr, "rdflib: write error\n");
113 exit(1);
115 *p++ = t;
117 int32_ttolocal(&l);
118 return l;
121 int main(int argc, char **argv)
123 FILE *fp, *fp2 = NULL, *fptmp;
124 char *p, buf[256], c;
125 int i;
126 int32_t l;
127 time_t t;
128 char rdbuf[10];
130 _argv = argv;
132 if (argc < 3 || !strncmp(argv[1], "-h", 2)
133 || !strncmp(argv[1], "--h", 3)) {
134 printf(usage);
135 exit(1);
138 switch (argv[1][0]) {
139 case 'c': /* create library */
140 fp = fopen(argv[2], "wb");
141 if (!fp) {
142 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
143 perror("rdflib");
144 exit(1);
146 fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp);
147 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
148 l = sizeof(t = time(NULL));
149 fwrite(&l, sizeof(l), 1, fp);
150 fwrite(&t, 1, l, fp);
151 fclose(fp);
152 break;
154 case 'a': /* add module */
155 if (argc < 5) {
156 fprintf(stderr, "rdflib: required parameter missing\n");
157 exit(1);
159 fp = fopen(argv[2], "ab");
160 if (!fp) {
161 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
162 perror("rdflib");
163 exit(1);
166 fp2 = fopen(argv[3], "rb");
167 if (!fp2) {
168 fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]);
169 perror("rdflib");
170 exit(1);
173 p = argv[4];
174 do {
175 if (fputc(*p, fp) == EOF) {
176 fprintf(stderr, "rdflib: write error\n");
177 exit(1);
179 } while (*p++);
181 while (!feof(fp2)) {
182 i = fgetc(fp2);
183 if (i == EOF) {
184 break;
187 if (fputc(i, fp) == EOF) {
188 fprintf(stderr, "rdflib: write error\n");
189 exit(1);
192 fclose(fp2);
193 fclose(fp);
194 break;
196 case 'x':
197 if (argc < 5) {
198 fprintf(stderr, "rdflib: required parameter missing\n");
199 exit(1);
201 case 't':
202 fp = fopen(argv[2], "rb");
203 if (!fp) {
204 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
205 perror("rdflib");
206 exit(1);
209 fp2 = NULL;
210 while (!feof(fp)) {
211 /* read name */
212 p = buf;
213 while ((*(p++) = (char)fgetc(fp)))
214 if (feof(fp))
215 break;
217 if (feof(fp))
218 break;
220 fp2 = NULL;
221 if (argv[1][0] == 'x') {
222 /* check against desired name */
223 if (!strcmp(buf, argv[3])) {
224 fp2 = fopen(argv[4], "wb");
225 if (!fp2) {
226 fprintf(stderr, "rdflib: could not open '%s'\n",
227 argv[4]);
228 perror("rdflib");
229 exit(1);
232 } else
233 printf("%-40s ", buf);
235 /* step over the RDOFF file, extracting type information for
236 * the listing, and copying it if fp2 != NULL */
238 if (buf[0] == '.') {
240 if (argv[1][0] == 't')
241 for (i = 0; i < 6; i++)
242 printf("%c", copybytes(fp, fp2, 1));
243 else
244 copybytes(fp, fp2, 6);
246 l = copyint32_t(fp, fp2);
248 if (argv[1][0] == 't')
249 printf(" %"PRId32" bytes content\n", l);
251 copybytes(fp, fp2, l);
252 } else if ((c = copybytes(fp, fp2, 6)) >= '2') { /* version 2 or above */
253 l = copyint32_t(fp, fp2);
255 if (argv[1][0] == 't')
256 printf("RDOFF%c %"PRId32" bytes content\n", c, l);
257 copybytes(fp, fp2, l); /* entire object */
258 } else {
259 if (argv[1][0] == 't')
260 printf("RDOFF1\n");
262 * version 1 object, so we don't have an object content
263 * length field.
265 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* header */
266 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* text */
267 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* data */
270 if (fp2)
271 break;
273 fclose(fp);
274 if (fp2)
275 fclose(fp2);
276 else if (argv[1][0] == 'x') {
277 fprintf(stderr, "rdflib: module '%s' not found in '%s'\n",
278 argv[3], argv[2]);
279 exit(1);
281 break;
283 case 'r': /* replace module */
284 argc--;
285 case 'd': /* delete module */
286 if (argc < 4) {
287 fprintf(stderr, "rdflib: required parameter missing\n");
288 exit(1);
291 fp = fopen(argv[2], "rb");
292 if (!fp) {
293 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
294 perror("rdflib");
295 exit(1);
298 if (argv[1][0] == 'r') {
299 fp2 = fopen(argv[4], "rb");
300 if (!fp2) {
301 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
302 perror("rdflib");
303 exit(1);
307 fptmp = tmpfile();
308 if (!fptmp) {
309 fprintf(stderr, "rdflib: could not open temporary file\n");
310 perror("rdflib");
311 exit(1);
314 /* copy library into temporary file */
315 fseek(fp, 0, SEEK_END); /* get file length */
316 l = ftell(fp);
317 fseek(fp, 0, SEEK_SET);
318 copybytes(fp, fptmp, l);
319 rewind(fptmp);
320 freopen(argv[2], "wb", fp);
322 while (!feof(fptmp)) {
323 /* read name */
324 p = buf;
325 while ((*(p++) = (char)fgetc(fptmp)))
326 if (feof(fptmp))
327 break;
329 if (feof(fptmp))
330 break;
332 /* check against desired name */
333 if (!strcmp(buf, argv[3])) {
334 fread(p = rdbuf, 1, sizeof(rdbuf), fptmp);
335 l = *(int32_t *)(p + 6);
336 fseek(fptmp, l, SEEK_CUR);
337 break;
338 } else {
339 fwrite(buf, 1, strlen(buf) + 1, fp); /* module name */
340 if ((c = copybytes(fptmp, fp, 6)) >= '2') {
341 l = copyint32_t(fptmp, fp); /* version 2 or above */
342 copybytes(fptmp, fp, l); /* entire object */
347 if (argv[1][0] == 'r') {
348 /* copy new module into library */
349 p = argv[3];
350 do {
351 if (fputc(*p, fp) == EOF) {
352 fprintf(stderr, "rdflib: write error\n");
353 exit(1);
355 } while (*p++);
357 while (!feof(fp2)) {
358 i = fgetc(fp2);
359 if (i == EOF) {
360 break;
362 if (fputc(i, fp) == EOF) {
363 fprintf(stderr, "rdflib: write error\n");
364 exit(1);
367 fclose(fp2);
370 /* copy rest of library if any */
371 while (!feof(fptmp)) {
372 i = fgetc(fptmp);
373 if (i == EOF) {
374 break;
377 if (fputc(i, fp) == EOF) {
378 fprintf(stderr, "rdflib: write error\n");
379 exit(1);
383 fclose(fp);
384 fclose(fptmp);
385 break;
387 default:
388 fprintf(stderr, "rdflib: command '%c' not recognized\n",
389 argv[1][0]);
390 exit(1);
392 return 0;