changes.src: remove double entry for %warning
[nasm.git] / rdoff / rdlib.c
blob885752d47f70e773987e4c3b4118157ea64dfc76
1 /*
2 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
3 */
5 #include "compiler.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #define RDOFF_UTILS
13 #include "rdoff.h"
14 #include "rdlib.h"
15 #include "rdlar.h"
17 /* See Texinfo documentation about new RDOFF libraries format */
19 int rdl_error = 0;
21 char *rdl_errors[5] = {
22 "no error", "could not open file", "invalid file structure",
23 "file contains modules of an unsupported RDOFF version",
24 "module not found"
27 int rdl_verify(const char *filename)
29 FILE *fp = fopen(filename, "rb");
30 char buf[257];
31 int i;
32 int32_t length;
33 static char lastverified[256];
34 static int lastresult = -1;
36 if (lastresult != -1 && !strcmp(filename, lastverified))
37 return lastresult;
39 strcpy(lastverified, filename);
41 if (!fp)
42 return (rdl_error = lastresult = 1);
44 while (!feof(fp)) {
45 i = 0;
47 while (fread(buf + i, 1, 1, fp) == 1 && buf[i] && i < 257)
48 i++;
49 if (feof(fp))
50 break;
52 if (buf[0] == '.') {
54 * A special module, eg a signature block or a directory.
55 * Format of such a module is defined to be:
56 * six char type identifier
57 * int32_t count bytes content
58 * content
59 * so we can handle it uniformaly with RDOFF2 modules.
61 fread(buf, 6, 1, fp);
62 buf[6] = 0;
63 /* Currently, nothing useful to do with signature block.. */
64 } else {
65 fread(buf, 6, 1, fp);
66 buf[6] = 0;
67 if (strncmp(buf, "RDOFF", 5)) {
68 return rdl_error = lastresult = 2;
69 } else if (buf[5] != '2') {
70 return rdl_error = lastresult = 3;
73 fread(&length, 4, 1, fp);
74 fseek(fp, length, SEEK_CUR); /* skip over the module */
76 fclose(fp);
77 return lastresult = 0; /* library in correct format */
80 int rdl_open(struct librarynode *lib, const char *name)
82 int i = rdl_verify(name);
83 if (i)
84 return i;
86 lib->fp = NULL;
87 lib->name = strdup(name);
88 lib->referenced = 0;
89 lib->next = NULL;
90 return 0;
93 void rdl_close(struct librarynode *lib)
95 if (lib->fp)
96 fclose(lib->fp);
97 free(lib->name);
100 int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f)
102 char buf[512];
103 int i, t;
104 void *hdr;
105 rdfheaderrec *r;
106 int32_t l;
108 rdl_error = 0;
109 lib->referenced++;
111 if (!lib->fp) {
112 lib->fp = fopen(lib->name, "rb");
114 if (!lib->fp) {
115 rdl_error = 1;
116 return 0;
118 } else
119 rewind(lib->fp);
121 while (!feof(lib->fp)) {
123 * read the module name from the file, and prepend
124 * the library name and '.' to it.
126 strcpy(buf, lib->name);
128 i = strlen(lib->name);
129 buf[i++] = '.';
130 t = i;
131 while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
132 i++;
134 buf[i] = 0;
136 if (feof(lib->fp))
137 break;
138 if (!strcmp(buf + t, ".dir")) { /* skip over directory */
139 fread(&l, 4, 1, lib->fp);
140 fseek(lib->fp, l, SEEK_CUR);
141 continue;
144 * open the RDOFF module
146 if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) {
147 rdl_error = 16 * rdf_errno;
148 return 0;
151 * read in the header, and scan for exported symbols
153 hdr = malloc(f->header_len);
154 rdfloadseg(f, RDOFF_HEADER, hdr);
156 while ((r = rdfgetheaderrec(f))) {
157 if (r->type != 3) /* not an export */
158 continue;
160 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) {
179 fclose(lib->fp);
180 lib->fp = NULL;
182 return 0;
185 int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f)
187 char buf[512];
188 int i, cmod, t;
189 int32_t length;
191 lib->referenced++;
193 if (!lib->fp) {
194 lib->fp = fopen(lib->name, "rb");
195 if (!lib->fp) {
196 lib->referenced--;
197 return (rdl_error = 1);
199 } else
200 rewind(lib->fp);
202 cmod = -1;
203 while (!feof(lib->fp)) {
204 strcpy(buf, lib->name);
205 i = strlen(buf);
206 buf[i++] = '.';
207 t = i;
208 while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512)
209 i++;
210 buf[i] = 0;
211 if (feof(lib->fp))
212 break;
214 if (buf[t] != '.') /* special module - not counted in the numbering */
215 cmod++; /* of RDOFF modules - must be referred to by name */
217 if (cmod == moduleno) {
218 rdl_error = 16 *
219 rdfopenhere(f, lib->fp, &lib->referenced, buf);
220 lib->referenced--;
221 if (!lib->referenced) {
222 fclose(lib->fp);
223 lib->fp = NULL;
225 return rdl_error;
228 fread(buf, 6, 1, lib->fp);
229 buf[6] = 0;
230 if (buf[t] == '.') {
231 /* do nothing */
232 } else if (strncmp(buf, "RDOFF", 5)) {
233 if (!--lib->referenced) {
234 fclose(lib->fp);
235 lib->fp = NULL;
237 return rdl_error = 2;
238 } else if (buf[5] != '2') {
239 if (!--lib->referenced) {
240 fclose(lib->fp);
241 lib->fp = NULL;
243 return rdl_error = 3;
246 fread(&length, 4, 1, lib->fp);
247 fseek(lib->fp, length, SEEK_CUR); /* skip over the module */
249 if (!--lib->referenced) {
250 fclose(lib->fp);
251 lib->fp = NULL;
253 return rdl_error = 4; /* module not found */
256 void rdl_perror(const char *apname, const char *filename)
258 if (rdl_error >= 16)
259 rdfperror(apname, filename);
260 else
261 fprintf(stderr, "%s:%s:%s\n", apname, filename,
262 rdl_errors[rdl_error]);