1 /*
2  * Copyright (c) 1992 Eric P. Allman.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)makemap.c	8.11 (Berkeley) 03/06/95";
11 #endif /* not lint */
12 
13 #include <stdio.h>
14 #include <sysexits.h>
15 #include <sys/types.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <sys/errno.h>
19 #ifndef ISC_UNIX
20 # include <sys/file.h>
21 #endif
22 #include "useful.h"
23 #include "conf.h"
24 
25 #ifdef NDBM
26 #include <ndbm.h>
27 #endif
28 
29 #ifdef NEWDB
30 #include <db.h>
31 #endif
32 
33 enum type { T_DBM, T_BTREE, T_HASH, T_ERR, T_UNKNOWN };
34 
35 union dbent
36 {
37 #ifdef NDBM
38 	datum	dbm;
39 #endif
40 #ifdef NEWDB
41 	DBT	db;
42 #endif
43 	struct
44 	{
45 		char	*data;
46 		size_t	size;
47 	} xx;
48 };
49 
50 #define BUFSIZE		1024
51 
52 main(argc, argv)
53 	int argc;
54 	char **argv;
55 {
56 	char *progname;
57 	bool inclnull = FALSE;
58 	bool notrunc = FALSE;
59 	bool allowreplace = FALSE;
60 	bool allowdups = FALSE;
61 	bool verbose = FALSE;
62 	bool foldcase = TRUE;
63 	int exitstat;
64 	int opt;
65 	char *typename;
66 	char *mapname;
67 	char *ext;
68 	int lineno;
69 	int st;
70 	int mode;
71 	enum type type;
72 	int fd;
73 	union
74 	{
75 #ifdef NDBM
76 		DBM	*dbm;
77 #endif
78 #ifdef NEWDB
79 		DB	*db;
80 #endif
81 		void	*dbx;
82 	} dbp;
83 	union dbent key, val;
84 #ifdef NEWDB
85 	BTREEINFO bti;
86 #endif
87 	char ibuf[BUFSIZE];
88 	char fbuf[MAXNAME];
89 	extern char *optarg;
90 	extern int optind;
91 	extern bool lockfile();
92 
93 	progname = argv[0];
94 
95 	while ((opt = getopt(argc, argv, "Ndforv")) != EOF)
96 	{
97 		switch (opt)
98 		{
99 		  case 'N':
100 			inclnull = TRUE;
101 			break;
102 
103 		  case 'd':
104 			allowdups = TRUE;
105 			break;
106 
107 		  case 'f':
108 			foldcase = FALSE;
109 			break;
110 
111 		  case 'o':
112 			notrunc = TRUE;
113 			break;
114 
115 		  case 'r':
116 			allowreplace = TRUE;
117 			break;
118 
119 		  case 'v':
120 			verbose = TRUE;
121 			break;
122 
123 		  default:
124 			type = T_ERR;
125 			break;
126 		}
127 	}
128 
129 	argc -= optind;
130 	argv += optind;
131 	if (argc != 2)
132 		type = T_ERR;
133 	else
134 	{
135 		typename = argv[0];
136 		mapname = argv[1];
137 		ext = NULL;
138 
139 		if (strcmp(typename, "dbm") == 0)
140 		{
141 			type = T_DBM;
142 		}
143 		else if (strcmp(typename, "btree") == 0)
144 		{
145 			type = T_BTREE;
146 			ext = ".db";
147 		}
148 		else if (strcmp(typename, "hash") == 0)
149 		{
150 			type = T_HASH;
151 			ext = ".db";
152 		}
153 		else
154 			type = T_UNKNOWN;
155 	}
156 
157 	switch (type)
158 	{
159 	  case T_ERR:
160 		fprintf(stderr, "Usage: %s [-N] [-d] [-f] [-o] [-r] [-v] type mapname\n", progname);
161 		exit(EX_USAGE);
162 
163 	  case T_UNKNOWN:
164 		fprintf(stderr, "%s: Unknown database type %s\n",
165 			progname, typename);
166 		exit(EX_USAGE);
167 
168 #ifndef NDBM
169 	  case T_DBM:
170 #endif
171 #ifndef NEWDB
172 	  case T_BTREE:
173 	  case T_HASH:
174 #endif
175 		fprintf(stderr, "%s: Type %s not supported in this version\n",
176 			progname, typename);
177 		exit(EX_UNAVAILABLE);
178 
179 #ifdef NEWDB
180 	  case T_BTREE:
181 		bzero(&bti, sizeof bti);
182 		if (allowdups)
183 			bti.flags |= R_DUP;
184 		break;
185 
186 	  case T_HASH:
187 #endif
188 #ifdef NDBM
189 	  case T_DBM:
190 #endif
191 		if (allowdups)
192 		{
193 			fprintf(stderr, "%s: Type %s does not support -d (allow dups)\n",
194 				progname, typename);
195 			exit(EX_UNAVAILABLE);
196 		}
197 		break;
198 	}
199 
200 	/*
201 	**  Adjust file names.
202 	*/
203 
204 	if (ext != NULL)
205 	{
206 		int el, fl;
207 
208 		el = strlen(ext);
209 		fl = strlen(mapname);
210 		if (fl < el || strcmp(&mapname[fl - el], ext) != 0)
211 		{
212 			strcpy(fbuf, mapname);
213 			strcat(fbuf, ext);
214 			mapname = fbuf;
215 		}
216 	}
217 
218 	/*
219 	**  Create the database.
220 	*/
221 
222 	mode = O_RDWR;
223 #ifdef O_EXLOCK
224 	mode |= O_EXLOCK;
225 #endif
226 	if (!notrunc)
227 		mode |= O_CREAT|O_TRUNC;
228 	switch (type)
229 	{
230 #ifdef NDBM
231 	  case T_DBM:
232 		dbp.dbm = dbm_open(mapname, mode, 0644);
233 		break;
234 #endif
235 
236 #ifdef NEWDB
237 	  case T_HASH:
238 		dbp.db = dbopen(mapname, mode, 0644, DB_HASH, NULL);
239 		if (dbp.db != NULL)
240 			(void) (*dbp.db->sync)(dbp.db, 0);
241 		break;
242 
243 	  case T_BTREE:
244 		dbp.db = dbopen(mapname, mode, 0644, DB_BTREE, &bti);
245 		if (dbp.db != NULL)
246 			(void) (*dbp.db->sync)(dbp.db, 0);
247 		break;
248 #endif
249 
250 	  default:
251 		fprintf(stderr, "%s: internal error: type %d\n", progname, type);
252 		exit(EX_SOFTWARE);
253 	}
254 
255 	if (dbp.dbx == NULL)
256 	{
257 		fprintf(stderr, "%s: cannot create type %s map %s\n",
258 			progname, typename, mapname);
259 		exit(EX_CANTCREAT);
260 	}
261 
262 #ifndef O_EXLOCK
263 	switch (type)
264 	{
265 # ifdef NDBM
266 	  case T_DBM:
267 		fd = dbm_dirfno(dbp.dbm);
268 		if (fd >= 0)
269 			lockfile(fd);
270 		break;
271 # endif
272 # ifdef NEWDB
273 	  case T_HASH:
274 	  case T_BTREE:
275 		fd = dbp.db->fd(dbp.db);
276 		if (fd >= 0)
277 			lockfile(fd);
278 		break;
279 # endif
280 	}
281 #endif
282 
283 	/*
284 	**  Copy the data
285 	*/
286 
287 	lineno = 0;
288 	exitstat = EX_OK;
289 	while (fgets(ibuf, sizeof ibuf, stdin) != NULL)
290 	{
291 		register char *p;
292 
293 		lineno++;
294 
295 		/*
296 		**  Parse the line.
297 		*/
298 
299 		p = strchr(ibuf, '\n');
300 		if (p != NULL)
301 			*p = '\0';
302 		else if (!feof(stdin))
303 		{
304 			fprintf(stderr, "%s: %s: line %d: line too long (%d bytes max)\n",
305 				progname, mapname, lineno, sizeof ibuf);
306 			continue;
307 		}
308 
309 		if (ibuf[0] == '\0' || ibuf[0] == '#')
310 			continue;
311 		if (isspace(ibuf[0]))
312 		{
313 			fprintf(stderr, "%s: %s: line %d: syntax error (leading space)\n",
314 				progname, mapname, lineno);
315 			continue;
316 		}
317 		key.xx.data = ibuf;
318 		for (p = ibuf; *p != '\0' && !isspace(*p); p++)
319 		{
320 			if (foldcase && isupper(*p))
321 				*p = tolower(*p);
322 		}
323 		key.xx.size = p - key.xx.data;
324 		if (inclnull)
325 			key.xx.size++;
326 		if (*p != '\0')
327 			*p++ = '\0';
328 		while (isspace(*p))
329 			p++;
330 		if (*p == '\0')
331 		{
332 			fprintf(stderr, "%s: %s: line %d: no RHS for LHS %s\n",
333 				progname, mapname, lineno, key.xx.data);
334 			continue;
335 		}
336 		val.xx.data = p;
337 		val.xx.size = strlen(p);
338 		if (inclnull)
339 			val.xx.size++;
340 
341 		/*
342 		**  Do the database insert.
343 		*/
344 
345 		if (verbose)
346 		{
347 			printf("key=`%s', val=`%s'\n", key.xx.data, val.xx.data);
348 		}
349 
350 		switch (type)
351 		{
352 #ifdef NDBM
353 		  case T_DBM:
354 			st = dbm_store(dbp.dbm, key.dbm, val.dbm,
355 					allowreplace ? DBM_REPLACE : DBM_INSERT);
356 			break;
357 #endif
358 
359 #ifdef NEWDB
360 		  case T_BTREE:
361 		  case T_HASH:
362 			st = (*dbp.db->put)(dbp.db, &key.db, &val.db,
363 					allowreplace ? 0 : R_NOOVERWRITE);
364 			break;
365 #endif
366 		}
367 
368 		if (st < 0)
369 		{
370 			fprintf(stderr, "%s: %s: line %d: key %s: put error\n",
371 				progname, mapname, lineno, key.xx.data);
372 			perror(mapname);
373 			exitstat = EX_IOERR;
374 		}
375 		else if (st > 0)
376 		{
377 			fprintf(stderr, "%s: %s: line %d: key %s: duplicate key\n",
378 				progname, mapname, lineno, key.xx.data);
379 		}
380 	}
381 
382 	/*
383 	**  Now close the database.
384 	*/
385 
386 	switch (type)
387 	{
388 #ifdef NDBM
389 	  case T_DBM:
390 		dbm_close(dbp.dbm);
391 		break;
392 #endif
393 
394 #ifdef NEWDB
395 	  case T_HASH:
396 	  case T_BTREE:
397 		if ((*dbp.db->close)(dbp.db) < 0)
398 		{
399 			fprintf(stderr, "%s: %s: error on close\n",
400 				progname, mapname);
401 			perror(mapname);
402 			exitstat = EX_IOERR;
403 		}
404 #endif
405 	}
406 
407 	exit (exitstat);
408 }
409 /*
410 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
411 **
412 **	Parameters:
413 **		fd -- the file descriptor of the file.
414 **
415 **	Returns:
416 **		TRUE if the lock was acquired.
417 **		FALSE otherwise.
418 */
419 
420 bool
421 lockfile(fd)
422 	int fd;
423 {
424 # if !HASFLOCK
425 	int action;
426 	struct flock lfd;
427 	extern int errno;
428 
429 	bzero(&lfd, sizeof lfd);
430 	lfd.l_type = F_WRLCK;
431 	action = F_SETLKW;
432 
433 	if (fcntl(fd, action, &lfd) >= 0)
434 		return TRUE;
435 
436 	/*
437 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
438 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
439 	**  as type "tmp" (that is, served from swap space), the
440 	**  previous fcntl will fail with "Invalid argument" errors.
441 	**  Since this is fairly common during testing, we will assume
442 	**  that this indicates that the lock is successfully grabbed.
443 	*/
444 
445 	if (errno == EINVAL)
446 		return TRUE;
447 
448 # else	/* HASFLOCK */
449 
450 	if (flock(fd, LOCK_EX) >= 0)
451 		return TRUE;
452 
453 # endif
454 
455 	return FALSE;
456 }
457