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.10 (Berkeley) 02/24/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 break; 240 241 case T_BTREE: 242 dbp.db = dbopen(mapname, mode, 0644, DB_BTREE, &bti); 243 break; 244 #endif 245 246 default: 247 fprintf(stderr, "%s: internal error: type %d\n", progname, type); 248 exit(EX_SOFTWARE); 249 } 250 251 if (dbp.dbx == NULL) 252 { 253 fprintf(stderr, "%s: cannot create type %s map %s\n", 254 progname, typename, mapname); 255 exit(EX_CANTCREAT); 256 } 257 258 #ifndef O_EXLOCK 259 switch (type) 260 { 261 # ifdef NDBM 262 case T_DBM: 263 fd = dbm_dirfno(dbp.dbm); 264 if (fd >= 0) 265 lockfile(fd); 266 break; 267 # endif 268 # ifdef NEWDB 269 case T_HASH: 270 case T_BTREE: 271 fd = dbp.db->fd(dbp.db); 272 if (fd >= 0) 273 lockfile(fd); 274 break; 275 # endif 276 } 277 #endif 278 279 /* 280 ** Copy the data 281 */ 282 283 lineno = 0; 284 exitstat = EX_OK; 285 while (fgets(ibuf, sizeof ibuf, stdin) != NULL) 286 { 287 register char *p; 288 289 lineno++; 290 291 /* 292 ** Parse the line. 293 */ 294 295 p = strchr(ibuf, '\n'); 296 if (p != NULL) 297 *p = '\0'; 298 else if (!feof(stdin)) 299 { 300 fprintf(stderr, "%s: %s: line %d: line too long (%d bytes max)\n", 301 progname, mapname, lineno, sizeof ibuf); 302 continue; 303 } 304 305 if (ibuf[0] == '\0' || ibuf[0] == '#') 306 continue; 307 if (isspace(ibuf[0])) 308 { 309 fprintf(stderr, "%s: %s: line %d: syntax error (leading space)\n", 310 progname, mapname, lineno); 311 continue; 312 } 313 key.xx.data = ibuf; 314 for (p = ibuf; *p != '\0' && !isspace(*p); p++) 315 { 316 if (foldcase && isupper(*p)) 317 *p = tolower(*p); 318 } 319 key.xx.size = p - key.xx.data; 320 if (inclnull) 321 key.xx.size++; 322 if (*p != '\0') 323 *p++ = '\0'; 324 while (isspace(*p)) 325 p++; 326 if (*p == '\0') 327 { 328 fprintf(stderr, "%s: %s: line %d: no RHS for LHS %s\n", 329 progname, mapname, lineno, key.xx.data); 330 continue; 331 } 332 val.xx.data = p; 333 val.xx.size = strlen(p); 334 if (inclnull) 335 val.xx.size++; 336 337 /* 338 ** Do the database insert. 339 */ 340 341 if (verbose) 342 { 343 printf("key=`%s', val=`%s'\n", key.xx.data, val.xx.data); 344 } 345 346 switch (type) 347 { 348 #ifdef NDBM 349 case T_DBM: 350 st = dbm_store(dbp.dbm, key.dbm, val.dbm, 351 allowreplace ? DBM_REPLACE : DBM_INSERT); 352 break; 353 #endif 354 355 #ifdef NEWDB 356 case T_BTREE: 357 case T_HASH: 358 st = (*dbp.db->put)(dbp.db, &key.db, &val.db, 359 allowreplace ? 0 : R_NOOVERWRITE); 360 break; 361 #endif 362 } 363 364 if (st < 0) 365 { 366 fprintf(stderr, "%s: %s: line %d: key %s: put error\n", 367 progname, mapname, lineno, key.xx.data); 368 perror(mapname); 369 exitstat = EX_IOERR; 370 } 371 else if (st > 0) 372 { 373 fprintf(stderr, "%s: %s: line %d: key %s: duplicate key\n", 374 progname, mapname, lineno, key.xx.data); 375 } 376 } 377 378 /* 379 ** Now close the database. 380 */ 381 382 switch (type) 383 { 384 #ifdef NDBM 385 case T_DBM: 386 dbm_close(dbp.dbm); 387 break; 388 #endif 389 390 #ifdef NEWDB 391 case T_HASH: 392 case T_BTREE: 393 if ((*dbp.db->close)(dbp.db) < 0) 394 { 395 fprintf(stderr, "%s: %s: error on close\n", 396 progname, mapname); 397 perror(mapname); 398 exitstat = EX_IOERR; 399 } 400 #endif 401 } 402 403 exit (exitstat); 404 } 405 /* 406 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 407 ** 408 ** Parameters: 409 ** fd -- the file descriptor of the file. 410 ** 411 ** Returns: 412 ** TRUE if the lock was acquired. 413 ** FALSE otherwise. 414 */ 415 416 bool 417 lockfile(fd) 418 int fd; 419 { 420 # if !HASFLOCK 421 int action; 422 struct flock lfd; 423 extern int errno; 424 425 bzero(&lfd, sizeof lfd); 426 lfd.l_type = F_WRLCK; 427 action = F_SETLKW; 428 429 if (fcntl(fd, action, &lfd) >= 0) 430 return TRUE; 431 432 /* 433 ** On SunOS, if you are testing using -oQ/tmp/mqueue or 434 ** -oA/tmp/aliases or anything like that, and /tmp is mounted 435 ** as type "tmp" (that is, served from swap space), the 436 ** previous fcntl will fail with "Invalid argument" errors. 437 ** Since this is fairly common during testing, we will assume 438 ** that this indicates that the lock is successfully grabbed. 439 */ 440 441 if (errno == EINVAL) 442 return TRUE; 443 444 # else /* HASFLOCK */ 445 446 if (flock(fd, LOCK_EX) >= 0) 447 return TRUE; 448 449 # endif 450 451 return FALSE; 452 } 453