156811Seric /* 256811Seric * Copyright (c) 1992 Eric P. Allman. 3*62514Sbostic * Copyright (c) 1992, 1993 4*62514Sbostic * The Regents of the University of California. All rights reserved. 556811Seric * 656811Seric * %sccs.include.redist.c% 756811Seric */ 856811Seric 956811Seric #ifndef lint 10*62514Sbostic static char sccsid[] = "@(#)makemap.c 8.1 (Berkeley) 06/07/93"; 1156811Seric #endif /* not lint */ 1256811Seric 1356811Seric #include <stdio.h> 1456811Seric #include <sysexits.h> 1556811Seric #include <sys/file.h> 1656811Seric #include <ctype.h> 1756811Seric #include <string.h> 1856811Seric #include "useful.h" 1956811Seric #include "conf.h" 2056811Seric 2160558Seric #ifdef NDBM 2256811Seric #include <ndbm.h> 2356811Seric #endif 2456811Seric 2560558Seric #ifdef NEWDB 2656811Seric #include <db.h> 2756811Seric #endif 2856811Seric 2956811Seric enum type { T_DBM, T_BTREE, T_HASH, T_ERR, T_UNKNOWN }; 3056811Seric 3156811Seric union dbent 3256811Seric { 3360558Seric #ifdef NDBM 3456811Seric datum dbm; 3556811Seric #endif 3660558Seric #ifdef NEWDB 3756811Seric DBT db; 3856811Seric #endif 3956811Seric struct 4056811Seric { 4156811Seric char *data; 4256811Seric int size; 4356811Seric } xx; 4456811Seric }; 4556811Seric 4656811Seric #define BUFSIZE 1024 4756811Seric 4856811Seric main(argc, argv) 4956811Seric int argc; 5056811Seric char **argv; 5156811Seric { 5256811Seric char *progname; 5356811Seric bool inclnull = FALSE; 5456811Seric bool notrunc = FALSE; 5556811Seric bool allowreplace = FALSE; 5656811Seric bool verbose = FALSE; 5757078Seric bool foldcase = FALSE; 5856811Seric int exitstat; 5956811Seric int opt; 6056811Seric char *typename; 6156811Seric char *mapname; 6256811Seric int lineno; 6356811Seric int st; 6456811Seric int mode; 6556811Seric enum type type; 6656811Seric union 6756811Seric { 6860558Seric #ifdef NDBM 6956811Seric DBM *dbm; 7056811Seric #endif 7160558Seric #ifdef NEWDB 7256811Seric DB *db; 7356811Seric #endif 7456811Seric void *dbx; 7556811Seric } dbp; 7656811Seric union dbent key, val; 7756811Seric char ibuf[BUFSIZE]; 7856811Seric extern char *optarg; 7956811Seric extern int optind; 8056811Seric 8156811Seric progname = argv[0]; 8256811Seric 8357078Seric while ((opt = getopt(argc, argv, "Nforv")) != EOF) 8456811Seric { 8556811Seric switch (opt) 8656811Seric { 8756811Seric case 'N': 8856811Seric inclnull = TRUE; 8956811Seric break; 9056811Seric 9157078Seric case 'f': 9257078Seric foldcase = TRUE; 9357078Seric break; 9457078Seric 9556811Seric case 'o': 9656811Seric notrunc = TRUE; 9756811Seric break; 9856811Seric 9956811Seric case 'r': 10056811Seric allowreplace = TRUE; 10156811Seric break; 10256811Seric 10356811Seric case 'v': 10456811Seric verbose = TRUE; 10556811Seric break; 10656811Seric 10756811Seric default: 10856811Seric type = T_ERR; 10956811Seric break; 11056811Seric } 11156811Seric } 11256811Seric 11356811Seric argc -= optind; 11456811Seric argv += optind; 11556811Seric if (argc != 2) 11656811Seric type = T_ERR; 11756811Seric else 11856811Seric { 11956811Seric typename = argv[0]; 12056811Seric mapname = argv[1]; 12156811Seric 12256811Seric if (strcmp(typename, "dbm") == 0) 12356811Seric type = T_DBM; 12456811Seric else if (strcmp(typename, "btree") == 0) 12556811Seric type = T_BTREE; 12656811Seric else if (strcmp(typename, "hash") == 0) 12756811Seric type = T_HASH; 12856811Seric else 12956811Seric type = T_UNKNOWN; 13056811Seric } 13156811Seric 13256811Seric switch (type) 13356811Seric { 13456811Seric case T_ERR: 13556811Seric fprintf(stderr, "Usage: %s [-N] [-o] [-v] type mapname\n", progname); 13656811Seric exit(EX_USAGE); 13756811Seric 13856811Seric case T_UNKNOWN: 13956811Seric fprintf(stderr, "%s: Unknown database type %s\n", 14056811Seric progname, typename); 14156811Seric exit(EX_USAGE); 14256811Seric 14360558Seric #ifndef NDBM 14456811Seric case T_DBM: 14556811Seric #endif 14660558Seric #ifndef NEWDB 14756811Seric case T_BTREE: 14856811Seric case T_HASH: 14956811Seric #endif 15056811Seric fprintf(stderr, "%s: Type %s not supported in this version\n", 15156811Seric progname, typename); 15256811Seric exit(EX_UNAVAILABLE); 15356811Seric } 15456811Seric 15556811Seric /* 15656811Seric ** Create the database. 15756811Seric */ 15856811Seric 15956811Seric mode = O_RDWR; 16056811Seric if (!notrunc) 16156811Seric mode |= O_CREAT|O_TRUNC; 16256811Seric switch (type) 16356811Seric { 16460558Seric #ifdef NDBM 16556811Seric case T_DBM: 16656811Seric dbp.dbm = dbm_open(mapname, mode, 0644); 16756811Seric break; 16856811Seric #endif 16956811Seric 17060558Seric #ifdef NEWDB 17156811Seric case T_HASH: 17256811Seric dbp.db = dbopen(mapname, mode, 0644, DB_HASH, NULL); 17356811Seric break; 17456811Seric 17556811Seric case T_BTREE: 17656811Seric dbp.db = dbopen(mapname, mode, 0644, DB_BTREE, NULL); 17756811Seric break; 17856811Seric #endif 17956811Seric 18056811Seric default: 18156811Seric fprintf(stderr, "%s: internal error: type %d\n", progname, type); 18256811Seric exit(EX_SOFTWARE); 18356811Seric } 18456811Seric 18556811Seric if (dbp.dbx == NULL) 18656811Seric { 18756811Seric fprintf(stderr, "%s: cannot create type %s map %s\n", 18856811Seric progname, typename, mapname); 18956811Seric exit(EX_CANTCREAT); 19056811Seric } 19156811Seric 19256811Seric /* 19356811Seric ** Copy the data 19456811Seric */ 19556811Seric 19656811Seric lineno = 0; 19756811Seric exitstat = EX_OK; 19856811Seric while (fgets(ibuf, sizeof ibuf, stdin) != NULL) 19956811Seric { 20056811Seric register char *p; 20156811Seric 20256811Seric lineno++; 20356811Seric 20456811Seric /* 20556811Seric ** Parse the line. 20656811Seric */ 20756811Seric 20856811Seric p = strchr(ibuf, '\n'); 20956811Seric if (*p != '\0') 21056811Seric *p = '\0'; 21156811Seric if (ibuf[0] == '\0' || ibuf[0] == '#') 21256811Seric continue; 21356811Seric if (isspace(ibuf[0])) 21456811Seric { 21556811Seric fprintf(stderr, "%s: %s: line %d: syntax error (leading space)\n", 21656811Seric progname, mapname, lineno); 21756811Seric continue; 21856811Seric } 21956811Seric key.xx.data = ibuf; 22056811Seric for (p = ibuf; *p != '\0' && !isspace(*p); p++) 22157078Seric { 22257078Seric if (foldcase && isupper(*p)) 22357078Seric *p = tolower(*p); 22457078Seric } 22556811Seric key.xx.size = p - key.xx.data; 22656811Seric if (inclnull) 22756811Seric key.xx.size++; 22856811Seric if (*p != '\0') 22956811Seric *p++ = '\0'; 23056811Seric while (isspace(*p)) 23156811Seric p++; 23256811Seric if (*p == '\0') 23356811Seric { 23456811Seric fprintf(stderr, "%s: %s: line %d: no RHS for LHS %s\n", 23556811Seric progname, mapname, lineno, key.xx.data); 23656811Seric continue; 23756811Seric } 23856811Seric val.xx.data = p; 23956811Seric val.xx.size = strlen(p); 24056811Seric if (inclnull) 24156811Seric val.xx.size++; 24256811Seric 24356811Seric /* 24456811Seric ** Do the database insert. 24556811Seric */ 24656811Seric 24756811Seric if (verbose) 24856811Seric { 24956811Seric printf("key=`%s', val=`%s'\n", key.xx.data, val.xx.data); 25056811Seric } 25156811Seric 25256811Seric switch (type) 25356811Seric { 25460558Seric #ifdef NDBM 25556811Seric case T_DBM: 25656811Seric st = dbm_store(dbp.dbm, key.dbm, val.dbm, 25756811Seric allowreplace ? DBM_REPLACE : DBM_INSERT); 25856811Seric break; 25956811Seric #endif 26056811Seric 26160558Seric #ifdef NEWDB 26256811Seric case T_BTREE: 26356811Seric case T_HASH: 26456811Seric st = (*dbp.db->put)(dbp.db, &key.db, &val.db, 26556811Seric allowreplace ? 0 : R_NOOVERWRITE); 26656811Seric break; 26756811Seric #endif 26856811Seric } 26956811Seric 27056811Seric if (st < 0) 27156811Seric { 27256811Seric fprintf(stderr, "%s: %s: line %d: key %s: put error\n", 27356811Seric progname, mapname, lineno, key.xx.data); 27456811Seric perror(mapname); 27556811Seric exitstat = EX_IOERR; 27656811Seric } 27756811Seric else if (st > 0) 27856811Seric { 27956811Seric fprintf(stderr, "%s: %s: line %d: key %s: duplicate key\n", 28056811Seric progname, mapname, lineno, key.xx.data); 28156811Seric } 28256811Seric } 28356811Seric 28456811Seric /* 28556811Seric ** Now close the database. 28656811Seric */ 28756811Seric 28856811Seric switch (type) 28956811Seric { 29060558Seric #ifdef NDBM 29156811Seric case T_DBM: 29256811Seric dbm_close(dbp.dbm); 29356811Seric break; 29456811Seric #endif 29556811Seric 29660558Seric #ifdef NEWDB 29756811Seric case T_HASH: 29856811Seric case T_BTREE: 29956811Seric if ((*dbp.db->close)(dbp.db) < 0) 30056811Seric { 30156811Seric fprintf(stderr, "%s: %s: error on close\n", 30256811Seric progname, mapname); 30356811Seric perror(mapname); 30456811Seric exitstat = EX_IOERR; 30556811Seric } 30656811Seric #endif 30756811Seric } 30856811Seric 30956811Seric exit (exitstat); 31056811Seric } 311