Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / amd / mk-amd-map / mk-amd-map.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1990 Jan-Simon Pendry
4  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: mk-amd-map.c,v 1.4 1999/02/04 07:24:50 ezk Exp $
42  * $FreeBSD: src/contrib/amd/mk-amd-map/mk-amd-map.c,v 1.7 1999/09/15 05:45:16 obrien Exp $
43  * $DragonFly: src/contrib/amd/mk-amd-map/mk-amd-map.c,v 1.2 2003/06/17 04:23:58 dillon Exp $
44  */
45
46 /*
47  * Convert a file map into an ndbm map
48  */
49
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54
55 /* (libdb version 2) uses .db extensions but an old dbm API */
56 /* check for libgdbm to distinguish it from linux systems */
57 #if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
58 # define HAVE_DB_SUFFIX
59 #endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
60
61 #ifdef HAVE_MAP_NDBM
62
63 static int
64 store_data(voidp db, char *k, char *v)
65 {
66   datum key, val;
67
68   key.dptr = k;
69   val.dptr = v;
70   key.dsize = strlen(k) + 1;
71   val.dsize = strlen(v) + 1;
72   return dbm_store((DBM *) db, key, val, DBM_INSERT);
73 }
74
75
76 /*
77  * Read one line from file.
78  */
79 static int
80 read_line(char *buf, int size, FILE *fp)
81 {
82   int done = 0;
83
84   do {
85     while (fgets(buf, size, fp)) {
86       int len = strlen(buf);
87
88       done += len;
89       if (len > 1 && buf[len - 2] == '\\' && buf[len - 1] == '\n') {
90         int ch;
91         buf += len - 2;
92         size -= len - 2;
93         *buf = '\n';
94         buf[1] = '\0';
95
96         /*
97          * Skip leading white space on next line
98          */
99         while ((ch = getc(fp)) != EOF && isascii(ch) && isspace(ch)) ;
100         (void) ungetc(ch, fp);
101       } else {
102         return done;
103       }
104     }
105   } while (size > 0 && !feof(fp));
106
107   return done;
108 }
109
110
111 /*
112  * Read through a map.
113  */
114 static int
115 read_file(FILE *fp, char *map, voidp db)
116 {
117   char key_val[2048];
118   int chuck = 0;
119   int line_no = 0;
120   int errs = 0;
121
122   while (read_line(key_val, 2048, fp)) {
123     char *kp;
124     char *cp;
125     char *hash;
126     int len = strlen(key_val);
127
128     line_no++;
129
130     /*
131      * Make sure we got the whole line
132      */
133     if (key_val[len - 1] != '\n') {
134       fprintf(stderr, "line %d in \"%s\" is too long", line_no, map);
135       chuck = 1;
136     } else {
137       key_val[len - 1] = '\0';
138     }
139
140     /*
141      * Strip comments
142      */
143     hash = strchr(key_val, '#');
144     if (hash)
145       *hash = '\0';
146
147     /*
148      * Find start of key
149      */
150     for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ;
151
152     /*
153      * Ignore blank lines
154      */
155     if (!*kp)
156       goto again;
157
158     /*
159      * Find end of key
160      */
161     for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ;
162
163     /*
164      * Check whether key matches, or whether
165      * the entry is a wildcard entry.
166      */
167     if (*cp)
168       *cp++ = '\0';
169     while (*cp && isascii(*cp) && isspace((int)*cp))
170       cp++;
171     if (*kp == '+') {
172       fprintf(stderr, "Can't interpolate %s\n", kp);
173       errs++;
174     } else if (*cp) {
175       if (db) {
176         if (store_data(db, kp, cp) < 0) {
177           fprintf(stderr, "Could store %s -> %s\n", kp, cp);
178           errs++;
179         }
180       } else {
181         printf("%s\t%s\n", kp, cp);
182       }
183     } else {
184       fprintf(stderr, "%s: line %d has no value field", map, line_no);
185       errs++;
186     }
187
188   again:
189     /*
190      * If the last read didn't get a whole line then
191      * throw away the remainder before continuing...
192      */
193     if (chuck) {
194       while (fgets(key_val, sizeof(key_val), fp) &&
195              !strchr(key_val, '\n')) ;
196       chuck = 0;
197     }
198   }
199   return errs;
200 }
201
202
203 static int
204 remove_file(char *f)
205 {
206   if (unlink(f) < 0 && errno != ENOENT)
207     return -1;
208
209   return 0;
210 }
211
212
213 int
214 main(int argc, char *argv[])
215 {
216   FILE *mapf;                   /* the input file to read from */
217   int error;
218   char *mapsrc;
219   DBM *db = NULL;
220   static char maptmp[] = "dbmXXXXXX";
221 #ifdef HAVE_DB_SUFFIX
222   char maptdb[16];
223   char *map_name_db = (char *) NULL;
224 #else /* not HAVE_DB_SUFFIX */
225   char maptpag[16], maptdir[16];
226   char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL;
227 #endif /* not HAVE_DB_SUFFIX */
228   int len;
229   char *sl;
230   int printit = 0;
231   int usage = 0;
232   int ch;
233   extern int optind;
234
235   /* test options */
236   while ((ch = getopt(argc, argv, "p")) != -1)
237     switch (ch) {
238     case 'p':
239       printit = 1;
240       break;
241     default:
242       usage++;
243       break;
244     }
245
246   if (usage || optind != (argc - 1)) {
247     fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
248     exit(1);
249   }
250   mapsrc = argv[optind];
251
252   /* test if can get to the map directory */
253   sl = strrchr(mapsrc, '/');
254   if (sl) {
255     *sl = '\0';
256     if (chdir(mapsrc) < 0) {
257       fputs("Can't chdir to ", stderr);
258       perror(mapsrc);
259       exit(1);
260     }
261     mapsrc = sl + 1;
262   }
263
264   /* open source file */
265   mapf = fopen(mapsrc, "r");
266   if (!mapf) {
267     fprintf(stderr, "cannot open source file ");
268     perror(mapsrc);
269     exit(1);
270   }
271
272 #ifndef DEBUG
273   signal(SIGINT, SIG_IGN);
274 #endif /* DEBUG */
275
276   if (!printit) {
277     len = strlen(mapsrc);
278 #ifdef HAVE_DB_SUFFIX
279     map_name_db = (char *) malloc(len + 4);
280     error = (map_name_db == NULL);
281 #else /* not HAVE_DB_SUFFIX */
282     map_name_pag = (char *) malloc(len + 5);
283     map_name_dir = (char *) malloc(len + 5);
284     error = (map_name_pag == NULL || map_name_dir == NULL);
285 #endif /* not HAVE_DB_SUFFIX */
286     if (error) {
287       perror("mk-amd-map: malloc");
288       exit(1);
289     }
290
291     mktemp(maptmp);
292
293     /* remove existing temps (if any) */
294 #ifdef HAVE_DB_SUFFIX
295     sprintf(maptdb, "%s.db", maptmp);
296     if (remove_file(maptdb) < 0) {
297       fprintf(stderr, "Can't remove existing temporary file; ");
298       perror(maptdb);
299       exit(1);
300     }
301 #else /* not HAVE_DB_SUFFIX */
302     sprintf(maptpag, "%s.pag", maptmp);
303     sprintf(maptdir, "%s.dir", maptmp);
304     if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) {
305       fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag);
306       perror(maptdir);
307       exit(1);
308     }
309 #endif /* not HAVE_DB_SUFFIX */
310
311     db = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
312     if (!db) {
313       fprintf(stderr, "cannot initialize temporary database: %s", maptmp);
314       exit(1);
315     }
316   }
317
318   /* print db to stdout or to temp database */
319   error = read_file(mapf, mapsrc, db);
320   fclose(mapf);
321   if (error) {
322     if (printit)
323       fprintf(stderr, "Error reading source file  %s\n", mapsrc);
324     else
325       fprintf(stderr, "Error creating database map for %s\n", mapsrc);
326     exit(1);
327   }
328
329   if (printit)
330     exit(0);                    /* nothing more to do */
331
332   /* if gets here, we wrote to a database */
333
334   dbm_close(db);
335   /* all went well */
336
337 #ifdef HAVE_DB_SUFFIX
338   sprintf(map_name_db, "%s.db", mapsrc);
339   if (rename(maptdb, map_name_db) < 0) {
340     fprintf(stderr, "Couldn't rename %s to ", maptdb);
341     perror(map_name_db);
342     /* Throw away the temporary map */
343     unlink(maptdb);
344     exit(1);
345   }
346 #else /* not HAVE_DB_SUFFIX */
347   sprintf(map_name_pag, "%s.pag", mapsrc);
348   sprintf(map_name_dir, "%s.dir", mapsrc);
349   if (rename(maptpag, map_name_pag) < 0) {
350     fprintf(stderr, "Couldn't rename %s to ", maptpag);
351     perror(map_name_pag);
352     /* Throw away the temporary map */
353     unlink(maptpag);
354     unlink(maptdir);
355     exit(1);
356   }
357   if (rename(maptdir, map_name_dir) < 0) {
358     fprintf(stderr, "Couldn't rename %s to ", maptdir);
359     perror(map_name_dir);
360     /* remove the (presumably bad) .pag file */
361     unlink(map_name_pag);
362     /* throw away remaining part of original map */
363     unlink(map_name_dir);
364     /* throw away the temporary map */
365     unlink(maptdir);
366     fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
367             mapsrc);
368     exit(1);
369   }
370 #endif /* not HAVE_DB_SUFFIX */
371
372   exit(0);
373 }
374
375 #else /* not HAVE_MAP_NDBM */
376
377 int
378 main()
379 {
380   fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
381   exit(1);
382 }
383
384 #endif /* not HAVE_MAP_NDBM */