156811Seric /* 256811Seric * Copyright (c) 1992 Eric P. Allman. 362514Sbostic * Copyright (c) 1992, 1993 462514Sbostic * The Regents of the University of California. All rights reserved. 556811Seric * 656811Seric * %sccs.include.redist.c% 756811Seric */ 856811Seric 956811Seric #ifndef lint 10*64152Seric static char sccsid[] = "@(#)makemap.c 8.3 (Berkeley) 08/08/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; 62*64152Seric char *ext; 6356811Seric int lineno; 6456811Seric int st; 6556811Seric int mode; 6656811Seric enum type type; 6756811Seric union 6856811Seric { 6960558Seric #ifdef NDBM 7056811Seric DBM *dbm; 7156811Seric #endif 7260558Seric #ifdef NEWDB 7356811Seric DB *db; 7456811Seric #endif 7556811Seric void *dbx; 7656811Seric } dbp; 7756811Seric union dbent key, val; 7856811Seric char ibuf[BUFSIZE]; 7964151Seric char fbuf[MAXNAME]; 8056811Seric extern char *optarg; 8156811Seric extern int optind; 8256811Seric 8356811Seric progname = argv[0]; 8456811Seric 8557078Seric while ((opt = getopt(argc, argv, "Nforv")) != EOF) 8656811Seric { 8756811Seric switch (opt) 8856811Seric { 8956811Seric case 'N': 9056811Seric inclnull = TRUE; 9156811Seric break; 9256811Seric 9357078Seric case 'f': 9457078Seric foldcase = TRUE; 9557078Seric break; 9657078Seric 9756811Seric case 'o': 9856811Seric notrunc = TRUE; 9956811Seric break; 10056811Seric 10156811Seric case 'r': 10256811Seric allowreplace = TRUE; 10356811Seric break; 10456811Seric 10556811Seric case 'v': 10656811Seric verbose = TRUE; 10756811Seric break; 10856811Seric 10956811Seric default: 11056811Seric type = T_ERR; 11156811Seric break; 11256811Seric } 11356811Seric } 11456811Seric 11556811Seric argc -= optind; 11656811Seric argv += optind; 11756811Seric if (argc != 2) 11856811Seric type = T_ERR; 11956811Seric else 12056811Seric { 12156811Seric typename = argv[0]; 12256811Seric mapname = argv[1]; 12364151Seric ext = NULL; 12456811Seric 12556811Seric if (strcmp(typename, "dbm") == 0) 12664151Seric { 12756811Seric type = T_DBM; 12864151Seric } 12956811Seric else if (strcmp(typename, "btree") == 0) 13064151Seric { 13156811Seric type = T_BTREE; 13264151Seric ext = ".db"; 13364151Seric } 13456811Seric else if (strcmp(typename, "hash") == 0) 13564151Seric { 13656811Seric type = T_HASH; 13764151Seric ext = ".db"; 13864151Seric } 13956811Seric else 14056811Seric type = T_UNKNOWN; 14156811Seric } 14256811Seric 14356811Seric switch (type) 14456811Seric { 14556811Seric case T_ERR: 14656811Seric fprintf(stderr, "Usage: %s [-N] [-o] [-v] type mapname\n", progname); 14756811Seric exit(EX_USAGE); 14856811Seric 14956811Seric case T_UNKNOWN: 15056811Seric fprintf(stderr, "%s: Unknown database type %s\n", 15156811Seric progname, typename); 15256811Seric exit(EX_USAGE); 15356811Seric 15460558Seric #ifndef NDBM 15556811Seric case T_DBM: 15656811Seric #endif 15760558Seric #ifndef NEWDB 15856811Seric case T_BTREE: 15956811Seric case T_HASH: 16056811Seric #endif 16156811Seric fprintf(stderr, "%s: Type %s not supported in this version\n", 16256811Seric progname, typename); 16356811Seric exit(EX_UNAVAILABLE); 16456811Seric } 16556811Seric 16656811Seric /* 16764151Seric ** Adjust file names. 16864151Seric */ 16964151Seric 17064151Seric if (ext != NULL) 17164151Seric { 17264151Seric int el, fl; 17364151Seric 17464151Seric el = strlen(ext); 17564151Seric fl = strlen(mapname); 17664151Seric if (fl < el || strcmp(&mapname[fl - el], ext) != 0) 17764151Seric { 17864151Seric strcpy(fbuf, mapname); 17964151Seric strcat(fbuf, ext); 18064151Seric mapname = fbuf; 18164151Seric } 18264151Seric } 18364151Seric 18464151Seric /* 18556811Seric ** Create the database. 18656811Seric */ 18756811Seric 18856811Seric mode = O_RDWR; 18956811Seric if (!notrunc) 19056811Seric mode |= O_CREAT|O_TRUNC; 19156811Seric switch (type) 19256811Seric { 19360558Seric #ifdef NDBM 19456811Seric case T_DBM: 19556811Seric dbp.dbm = dbm_open(mapname, mode, 0644); 19656811Seric break; 19756811Seric #endif 19856811Seric 19960558Seric #ifdef NEWDB 20056811Seric case T_HASH: 20156811Seric dbp.db = dbopen(mapname, mode, 0644, DB_HASH, NULL); 20256811Seric break; 20356811Seric 20456811Seric case T_BTREE: 20556811Seric dbp.db = dbopen(mapname, mode, 0644, DB_BTREE, NULL); 20656811Seric break; 20756811Seric #endif 20856811Seric 20956811Seric default: 21056811Seric fprintf(stderr, "%s: internal error: type %d\n", progname, type); 21156811Seric exit(EX_SOFTWARE); 21256811Seric } 21356811Seric 21456811Seric if (dbp.dbx == NULL) 21556811Seric { 21656811Seric fprintf(stderr, "%s: cannot create type %s map %s\n", 21756811Seric progname, typename, mapname); 21856811Seric exit(EX_CANTCREAT); 21956811Seric } 22056811Seric 22156811Seric /* 22256811Seric ** Copy the data 22356811Seric */ 22456811Seric 22556811Seric lineno = 0; 22656811Seric exitstat = EX_OK; 22756811Seric while (fgets(ibuf, sizeof ibuf, stdin) != NULL) 22856811Seric { 22956811Seric register char *p; 23056811Seric 23156811Seric lineno++; 23256811Seric 23356811Seric /* 23456811Seric ** Parse the line. 23556811Seric */ 23656811Seric 23756811Seric p = strchr(ibuf, '\n'); 23856811Seric if (*p != '\0') 23956811Seric *p = '\0'; 24056811Seric if (ibuf[0] == '\0' || ibuf[0] == '#') 24156811Seric continue; 24256811Seric if (isspace(ibuf[0])) 24356811Seric { 24456811Seric fprintf(stderr, "%s: %s: line %d: syntax error (leading space)\n", 24556811Seric progname, mapname, lineno); 24656811Seric continue; 24756811Seric } 24856811Seric key.xx.data = ibuf; 24956811Seric for (p = ibuf; *p != '\0' && !isspace(*p); p++) 25057078Seric { 25157078Seric if (foldcase && isupper(*p)) 25257078Seric *p = tolower(*p); 25357078Seric } 25456811Seric key.xx.size = p - key.xx.data; 25556811Seric if (inclnull) 25656811Seric key.xx.size++; 25756811Seric if (*p != '\0') 25856811Seric *p++ = '\0'; 25956811Seric while (isspace(*p)) 26056811Seric p++; 26156811Seric if (*p == '\0') 26256811Seric { 26356811Seric fprintf(stderr, "%s: %s: line %d: no RHS for LHS %s\n", 26456811Seric progname, mapname, lineno, key.xx.data); 26556811Seric continue; 26656811Seric } 26756811Seric val.xx.data = p; 26856811Seric val.xx.size = strlen(p); 26956811Seric if (inclnull) 27056811Seric val.xx.size++; 27156811Seric 27256811Seric /* 27356811Seric ** Do the database insert. 27456811Seric */ 27556811Seric 27656811Seric if (verbose) 27756811Seric { 27856811Seric printf("key=`%s', val=`%s'\n", key.xx.data, val.xx.data); 27956811Seric } 28056811Seric 28156811Seric switch (type) 28256811Seric { 28360558Seric #ifdef NDBM 28456811Seric case T_DBM: 28556811Seric st = dbm_store(dbp.dbm, key.dbm, val.dbm, 28656811Seric allowreplace ? DBM_REPLACE : DBM_INSERT); 28756811Seric break; 28856811Seric #endif 28956811Seric 29060558Seric #ifdef NEWDB 29156811Seric case T_BTREE: 29256811Seric case T_HASH: 29356811Seric st = (*dbp.db->put)(dbp.db, &key.db, &val.db, 29456811Seric allowreplace ? 0 : R_NOOVERWRITE); 29556811Seric break; 29656811Seric #endif 29756811Seric } 29856811Seric 29956811Seric if (st < 0) 30056811Seric { 30156811Seric fprintf(stderr, "%s: %s: line %d: key %s: put error\n", 30256811Seric progname, mapname, lineno, key.xx.data); 30356811Seric perror(mapname); 30456811Seric exitstat = EX_IOERR; 30556811Seric } 30656811Seric else if (st > 0) 30756811Seric { 30856811Seric fprintf(stderr, "%s: %s: line %d: key %s: duplicate key\n", 30956811Seric progname, mapname, lineno, key.xx.data); 31056811Seric } 31156811Seric } 31256811Seric 31356811Seric /* 31456811Seric ** Now close the database. 31556811Seric */ 31656811Seric 31756811Seric switch (type) 31856811Seric { 31960558Seric #ifdef NDBM 32056811Seric case T_DBM: 32156811Seric dbm_close(dbp.dbm); 32256811Seric break; 32356811Seric #endif 32456811Seric 32560558Seric #ifdef NEWDB 32656811Seric case T_HASH: 32756811Seric case T_BTREE: 32856811Seric if ((*dbp.db->close)(dbp.db) < 0) 32956811Seric { 33056811Seric fprintf(stderr, "%s: %s: error on close\n", 33156811Seric progname, mapname); 33256811Seric perror(mapname); 33356811Seric exitstat = EX_IOERR; 33456811Seric } 33556811Seric #endif 33656811Seric } 33756811Seric 33856811Seric exit (exitstat); 33956811Seric } 340