NASM 0.98.08
[nasm.git] / rdoff / rdflib.c
blob7a00fc6dd63bce3d71d29b0cdfd13768ae878ee2
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 * There may be an optional
9 * directory placed on the end of the file. The format of the
10 * directory will be 'RDLDD' followed by a version number, followed by
11 * the length of the directory, and then the directory, the format of
12 * which has not yet been designed. The module name of the directory
13 * must be '.dir'.
15 * All module names beginning with '.' are reserved
16 * for possible future extensions. The linker ignores all such modules,
17 * assuming they have the format of a six byte type & version identifier
18 * followed by long content size, followed by data.
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h>
25 /* functions supported:
26 create a library (no extra operands required)
27 add a module from a library (requires filename and name to give mod.)
28 remove a module from a library (requires given name) (not implemented)
29 extract a module from the library (requires given name and filename)
30 list modules */
32 const char *usage =
33 "usage:\n"
34 " rdflib x libname [extra operands]\n\n"
35 " where x is one of:\n"
36 " c - create library\n"
37 " a - add module (operands = filename module-name)\n"
38 " r - remove (module-name) [not implemented]\n"
39 " x - extract (module-name filename)\n"
40 " t - list\n";
42 char **_argv;
44 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
46 static void longtolocal(long * l)
48 #if _ENDIANNESS
49 unsigned char t;
50 unsigned char * p = (unsigned char *) l;
52 t = p[0];
53 p[0] = p[3];
54 p[3] = t;
55 t = p[1];
56 p[1] = p[2];
57 p[2] = p[1];
58 #endif
61 char copybytes(FILE *fp, FILE *fp2, int n)
63 int i, t = 0;
65 for (i = 0 ; i < n; i++ )
67 t = fgetc(fp);
68 if (t == EOF)
70 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
71 _argv[2]);
72 exit(1);
74 if (fp2)
75 if (fputc(t, fp2) == EOF)
77 fprintf(stderr,"rdflib: write error\n");
78 exit(1);
81 return (char) t; /* return last char read */
84 long copylong(FILE *fp, FILE *fp2)
86 long l;
87 int i,t;
88 unsigned char * p = (unsigned char *) &l;
91 for (i = 0 ; i < 4; i++ ) /* skip magic no */
93 t = fgetc(fp);
94 if (t == EOF)
96 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
97 _argv[2]);
98 exit(1);
100 if (fp2)
101 if (fputc(t, fp2) == EOF)
103 fprintf(stderr,"rdflib: write error\n");
104 exit(1);
106 *p++ = t;
108 longtolocal (&l);
109 return l;
112 int main(int argc, char **argv)
114 FILE *fp, *fp2;
115 char *p, buf[256], c;
116 int i;
117 long l;
119 _argv = argv;
121 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
123 printf(usage);
124 exit(1);
127 switch(argv[1][0])
129 case 'c': /* create library */
130 fp = fopen(argv[2],"wb");
131 if (! fp) {
132 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
133 perror("rdflib");
134 exit(1);
136 fclose(fp);
137 break;
139 case 'a': /* add module */
140 if (argc < 5) {
141 fprintf(stderr,"rdflib: required parameter missing\n");
142 exit(1);
144 fp = fopen(argv[2],"ab");
145 if (! fp)
147 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
148 perror("rdflib");
149 exit(1);
152 fp2 = fopen(argv[3],"rb");
153 if (! fp)
155 fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
156 perror("rdflib");
157 exit(1);
160 p = argv[4];
161 do {
162 if ( fputc(*p,fp) == EOF ) {
163 fprintf(stderr,"rdflib: write error\n");
164 exit(1);
166 } while (*p++);
168 while (! feof (fp2) ) {
169 i = fgetc (fp2);
170 if (i == EOF) {
171 break;
174 if ( fputc(i, fp) == EOF ) {
175 fprintf(stderr,"rdflib: write error\n");
176 exit(1);
179 fclose(fp2);
180 fclose(fp);
181 break;
183 case 'x':
184 if (argc < 5) {
185 fprintf(stderr,"rdflib: required parameter missing\n");
186 exit(1);
188 case 't':
189 if (argc < 3) {
190 fprintf(stderr, "rdflib: required paramenter missing\n");
191 exit(1);
193 fp = fopen(argv[2],"rb");
194 if (! fp)
196 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
197 perror("rdflib");
198 exit(1);
201 fp2 = NULL;
202 while (! feof(fp) ) {
203 /* read name */
204 p = buf;
205 while( ( *(p++) = (char) fgetc(fp) ) )
206 if (feof(fp)) break;
208 if (feof(fp)) break;
210 fp2 = NULL;
211 if (argv[1][0] == 'x') {
212 /* check against desired name */
213 if (! strcmp(buf,argv[3]) )
215 fp2 = fopen(argv[4],"wb");
216 if (! fp2)
218 fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]);
219 perror("rdflib");
220 exit(1);
224 else
225 printf("%-40s ", buf);
227 /* step over the RDOFF file, extracting type information for
228 * the listing, and copying it if fp2 != NULL */
230 if (buf[0] == '.') {
232 if (argv[1][0] == 't')
233 for (i = 0; i < 6; i++)
234 printf("%c", copybytes(fp,fp2,1));
235 else
236 copybytes(fp,fp2,6);
238 l = copylong(fp,fp2);
240 if (argv[1][0] == 't') printf(" %ld bytes content\n", l);
242 copybytes(fp,fp2,l);
244 else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */
246 l = copylong(fp,fp2);
248 if (argv[1][0] == 't')
249 printf("RDOFF%c %ld bytes content\n", c, l);
250 copybytes(fp,fp2, l); /* entire object */
252 else
254 if (argv[1][0] == 't')
255 printf("RDOFF1\n");
257 * version 1 object, so we don't have an object content
258 * length field.
260 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
261 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
262 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
265 if (fp2)
266 break;
268 fclose(fp);
269 if (fp2)
270 fclose(fp2);
271 else if (argv[1][0] == 'x')
273 fprintf(stderr,"rdflib: module '%s' not found in '%s'\n",
274 argv[3],argv[2]);
275 exit(1);
277 break;
279 default:
280 fprintf(stderr,"rdflib: command '%c' not recognised\n",
281 argv[1][0]);
282 exit(1);
284 return 0;