Fix "make doc"
[nasm/avx512.git] / rdoff / rdlib.c
blobf511c1c77167b0a0e928ef0f57e6cff659e4e3ab
1 /*
2 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "rdoff.h"
10 #include "rdlib.h"
11 #include "rdlar.h"
13 /* See Texinfo documentation about new RDOFF libraries format */
15 int rdl_error = 0;
17 char *rdl_errors[5] = {
18 "no error","could not open file", "invalid file structure",
19 "file contains modules of an unsupported RDOFF version",
20 "module not found"
23 int rdl_verify(const char * filename)
25 FILE * fp = fopen(filename, "rb");
26 char buf[257];
27 int i;
28 long length;
29 static char lastverified[256];
30 static int lastresult = -1;
32 if (lastresult != -1 && !strcmp(filename, lastverified))
33 return lastresult;
35 strcpy(lastverified, filename);
37 if (!fp)
38 return (rdl_error = lastresult = 1);
40 while (!feof(fp))
42 i = 0;
44 while (fread(buf + i,1,1,fp) == 1 && buf[i] && i < 257)
45 i++;
46 if (feof(fp)) break;
48 if (buf[0] == '.') {
50 * A special module, eg a signature block or a directory.
51 * Format of such a module is defined to be:
52 * six char type identifier
53 * long count bytes content
54 * content
55 * so we can handle it uniformaly with RDOFF2 modules.
57 fread(buf, 6, 1, fp);
58 buf[6] = 0;
59 /* Currently, nothing useful to do with signature block.. */
60 } else {
61 fread(buf, 6, 1, fp);
62 buf[6] = 0;
63 if (strncmp(buf, "RDOFF", 5)) {
64 return rdl_error = lastresult = 2;
65 } else if (buf[5] != '2') {
66 return rdl_error = lastresult = 3;
69 fread(&length, 4, 1, fp);
70 fseek(fp, length, SEEK_CUR); /* skip over the module */
72 fclose(fp);
73 return lastresult = 0; /* library in correct format */
76 int rdl_open (struct librarynode * lib, const char * name)
78 int i = rdl_verify(name);
79 if (i) return i;
81 lib->fp = NULL;
82 lib->name = strdup(name);
83 lib->referenced = 0;
84 lib->next = NULL;
85 return 0;
88 void rdl_close (struct librarynode * lib)
90 if (lib->fp)
91 fclose(lib->fp);
92 free(lib->name);
95 int rdl_searchlib (struct librarynode * lib,
96 const char * label, rdffile * f)
98 char buf[512];
99 int i, t;
100 void * hdr;
101 rdfheaderrec * r;
102 long l;
104 rdl_error = 0;
105 lib->referenced ++;
107 if (! lib->fp)
109 lib->fp = fopen(lib->name,"rb");
111 if (! lib->fp) {
112 rdl_error = 1;
113 return 0;
116 else
117 rewind(lib->fp);
119 while (! feof(lib->fp) )
122 * read the module name from the file, and prepend
123 * the library name and '.' to it.
125 strcpy(buf, lib->name);
127 i = strlen(lib->name);
128 buf[i++] = '.'; t = i;
129 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
130 i++;
132 buf[i] = 0;
134 if (feof(lib->fp)) break;
135 if (!strcmp(buf + t, ".dir")) /* skip over directory */
137 fread (&l, 4, 1, lib->fp);
138 fseek (lib->fp, l, SEEK_CUR);
139 continue;
142 * open the RDOFF module
144 if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) {
145 rdl_error = 16 * rdf_errno;
146 return 0;
149 * read in the header, and scan for exported symbols
151 hdr = malloc(f->header_len);
152 rdfloadseg(f,RDOFF_HEADER,hdr);
154 while ((r = rdfgetheaderrec(f)))
156 if (r->type != 3) /* not an export */
157 continue;
159 if (! strcmp(r->e.label, label) ) /* match! */
161 free(hdr); /* reset to 'just open' */
162 f->header_loc = NULL; /* state... */
163 f->header_fp = 0;
164 return 1;
168 /* find start of next module... */
169 i = f->eof_offset;
170 rdfclose(f);
171 fseek(lib->fp,i,SEEK_SET);
175 * close the file if nobody else is using it
177 lib->referenced --;
178 if (! lib->referenced)
180 fclose(lib->fp);
181 lib->fp = NULL;
183 return 0;
186 int rdl_openmodule (struct librarynode * lib, int moduleno, rdffile * f)
188 char buf[512];
189 int i, cmod, t;
190 long length;
192 lib->referenced++;
194 if (!lib->fp)
196 lib->fp = fopen(lib->name, "rb");
197 if (!lib->fp) {
198 lib->referenced--;
199 return (rdl_error = 1);
202 else
203 rewind(lib->fp);
205 cmod = -1;
206 while (!feof(lib->fp))
208 strcpy(buf, lib->name);
209 i = strlen(buf);
210 buf[i++] = '.'; t = i;
211 while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512)
212 i++;
213 buf[i] = 0;
214 if (feof(lib->fp)) break;
216 if (buf[t] != '.') /* special module - not counted in the numbering */
217 cmod++; /* of RDOFF modules - must be referred to by name */
219 if (cmod == moduleno) {
220 rdl_error = 16 *
221 rdfopenhere(f, lib->fp, &lib->referenced, buf);
222 lib->referenced--;
223 if (!lib->referenced) {
224 fclose(lib->fp);
225 lib->fp = NULL;
227 return rdl_error;
230 fread(buf, 6, 1, lib->fp);
231 buf[6] = 0;
232 if (buf[t] == '.') {
233 /* do nothing */
235 else if (strncmp(buf, "RDOFF", 5)) {
236 if (! --lib->referenced) {
237 fclose(lib->fp);
238 lib->fp = NULL;
240 return rdl_error = 2;
242 else if (buf[5] != '2') {
243 if (! --lib->referenced) {
244 fclose(lib->fp);
245 lib->fp = NULL;
247 return rdl_error = 3;
250 fread(&length, 4, 1, lib->fp);
251 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
253 if (! --lib->referenced) {
254 fclose(lib->fp);
255 lib->fp = NULL;
257 return rdl_error = 4; /* module not found */
260 void rdl_perror(const char *apname, const char *filename)
262 if (rdl_error >= 16)
263 rdfperror(apname, filename);
264 else
265 fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]);