From 53bd3f325f898b6892742ce928da3daa02ba0edd Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Fri, 8 Oct 1999 22:57:20 +0000 Subject: [PATCH] gen-unix.h, gen-win32.h: platform-specific handling of option parsing and directory iteration. gen.c: use gen-unix.h/gen-win32.h --- lib/dns/gen-unix.h | 63 ++++++++++ lib/dns/gen-win32.h | 282 ++++++++++++++++++++++++++++++++++++++++++++ lib/dns/gen.c | 49 ++++---- 3 files changed, 371 insertions(+), 23 deletions(-) create mode 100644 lib/dns/gen-unix.h create mode 100644 lib/dns/gen-win32.h diff --git a/lib/dns/gen-unix.h b/lib/dns/gen-unix.h new file mode 100644 index 0000000000..74386d036d --- /dev/null +++ b/lib/dns/gen-unix.h @@ -0,0 +1,63 @@ + +/* + * This file is responsible for defining two operations that are not + * directly portable between Unix-like systems and Windows NT, option + * parsing and directory scanning. It is here because it was decided + * that the "gen" build utility was not to depend on libisc.a, so + * the functions delcared in isc/commandline.h and isc/dir.h could not + * be used. + * + * The commandline stuff is really just a wrapper around getopt(). + * The dir stuff was shrunk to fit the needs of gen.c. + */ + +#include +#include + +#include + +#define isc_commandline_parse getopt +#define isc_commandline_argument optarg + +typedef struct { + DIR *handle; + char *filename; +} isc_dir_t; + +static isc_boolean_t +start_directory(const char *path, isc_dir_t *dir) { + dir->handle = opendir(path); + + if (dir->handle != NULL) + return (ISC_TRUE); + else + return (ISC_FALSE); + +} + +static isc_boolean_t +next_file(isc_dir_t *dir) { + struct dirent *dirent; + + dir->filename = NULL; + + if (dir->handle != NULL) { + dirent = readdir(dir->handle); + if (dirent != NULL) + dir->filename = dirent->d_name; + } + + if (dir->filename != NULL) + return (ISC_TRUE); + else + return (ISC_FALSE); +} + +static void +end_directory(isc_dir_t *dir) { + if (dir->handle != NULL) + closedir(dir->handle); + + dir->handle = NULL; +} + diff --git a/lib/dns/gen-win32.h b/lib/dns/gen-win32.h new file mode 100644 index 0000000000..8665a69db1 --- /dev/null +++ b/lib/dns/gen-win32.h @@ -0,0 +1,282 @@ +/* + * Copyright (C) 1999 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Copyright (c) 1987, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* $Id: gen-win32.h,v 1.1 1999/10/08 22:57:19 tale Exp $ */ + +/* + * Principal Authors: Computer Systems Research Group at UC Berkeley + * Principal ISC caretaker: DCL + */ + +/* + * This file was adapted from the NetBSD project's source tree, RCS ID: + * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp + * + * The primary change has been to rename items to the ISC namespace + * and format in the ISC coding style. + * + * This file is responsible for defining two operations that are not + * directly portable between Unix-like systems and Windows NT, option + * parsing and directory scanning. It is here because it was decided + * that the "gen" build utility was not to depend on libisc.a, so + * the functions delcared in isc/commandline.h and isc/dir.h could not + * be used. + * + * The commandline stuff is pretty much a straight copy from the initial + * isc/commandline.c. The dir stuff was shrunk to fit the needs of gen.c. + */ + +#include +#include +#include + +#include +#include +#include + +int isc_commandline_index = 1; /* Index into parent argv vector. */ +int isc_commandline_option; /* Character checked for validity. */ + +char *isc_commandline_argument; /* Argument associated with option. */ +char *isc_commandline_progname; /* For printing error messages. */ + +isc_boolean_t isc_commandline_errprint = 1; /* Print error messages. */ +isc_boolean_t isc_commandline_reset = ISC_TRUE; /* Reset processing. */ + +#define BADOPT '?' +#define BADARG ':' +#define ENDOPT "" + +/* + * getopt -- + * Parse argc/argv argument vector. + */ +int +isc_commandline_parse(int argc, char * const *argv, const char *options) { + static char *place = ENDOPT; + char *option; /* Index into *options of option. */ + + /* + * Update scanning pointer, either because a reset was requested or + * the previous argv was finished. + */ + if (isc_commandline_reset || *place == '\0') { + isc_commandline_reset = ISC_FALSE; + + if (isc_commandline_progname == NULL) + isc_commandline_progname = argv[0]; + + if (isc_commandline_index >= argc || + *(place = argv[isc_commandline_index]) != '-') { + /* + * Index out of range or points to non-option. + */ + place = ENDOPT; + return (-1); + } + + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + /* + * Found '--' to signal end of options. Advance + * index to next argv, the first non-option. + */ + isc_commandline_index++; + place = ENDOPT; + return (-1); + } + } + + isc_commandline_option = *place++; + option = strchr(options, isc_commandline_option); + + /* + * Ensure valid option has been passed as specified by options string. + * '-:' is never a valid command line option because it could not + * distinguish ':' from the argument specifier in the options string. + */ + if (isc_commandline_option == ':' || option == NULL) { + if (*place == '\0') + isc_commandline_index++; + + if (isc_commandline_errprint && *options != ':') + fprintf(stderr, "%s: illegal option -- %c\n", + isc_commandline_progname, + isc_commandline_option); + + return (BADOPT); + } + + if (*++option != ':') { + /* + * Option does not take an argument. + */ + isc_commandline_argument = NULL; + + /* + * Skip to next argv if at the end of the current argv. + */ + if (*place == '\0') + ++isc_commandline_index; + + } else { + /* + * Option needs an argument. + */ + if (*place != '\0') + /* + * Option is in this argv, -D1 style. + */ + isc_commandline_argument = place; + + else if (argc > ++isc_commandline_index) + /* + * Option is next argv, -D 1 style. + */ + isc_commandline_argument = argv[isc_commandline_index]; + + else { + /* + * Argument needed, but no more argv. + */ + place = ENDOPT; + + /* + * Silent failure with "missing argument" return + * when ':' starts options string, per historical spec. + */ + if (*options == ':') + return (BADARG); + + if (isc_commandline_errprint) + fprintf(stderr, + "%s: option requires an argument -- %c\n", + isc_commandline_progname, + isc_commandline_option); + + return (BADOPT); + } + + place = ENDOPT; + + /* + * Point to argv that follows argument. + */ + isc_commandline_index++; + } + + return (isc_commandline_option); +} + +typedef struct { + HANDLE handle; + WIN32_FIND_DATA find_data; + isc_boolean_t first_file; + char *filename; +} isc_dir_t; + +isc_boolean_t +start_directory(char *path, isc_dir_t *dir) { + char pattern[_MAX_PATH], *p; + + /* + * Need space for slash-splat and final NUL. + */ + if (strlen(path) + 3 > sizeof(pattern)) + return (ISC_FALSE); + + strcpy(pattern, path); + + /* + * Append slash (if needed) and splat. + */ + p = pattern + strlen(pattern); + if (p != pattern && p[-1] != '\\' && p[-1] != ':') + *p++ = '\\'; + *p++ = '*'; + *p++ = '\0'; + + dir->first_file = ISC_TRUE; + + dir->handle = FindFirstFile(pattern, &dir->find_data); + + if (dir->handle == INVALID_HANDLE_VALUE) { + dir->filename = NULL; + return (ISC_FALSE); + } else { + dir->filename = dir->find_data.cFileName; + return (ISC_TRUE); + } +} + +isc_boolean_t +next_file(isc_dir_t *dir) { + if (dir->first_file) + dir->first_file = ISC_FALSE; + + else if (dir->handle != INVALID_HANDLE_VALUE) { + if (FindNextFile(dir->handle, &dir->find_data) == TRUE) + dir->filename = dir->find_data.cFileName; + else + dir->filename = NULL; + + } else + dir->filename = NULL; + + if (dir->filename != NULL) + return (ISC_TRUE); + else + return (ISC_FALSE); +} + +void +end_directory(isc_dir_t *dir) { + if (dir->handle != INVALID_HANDLE_VALUE) + FindClose(dir->handle); +} diff --git a/lib/dns/gen.c b/lib/dns/gen.c index cabc86aaa9..bb4b8f00a4 100644 --- a/lib/dns/gen.c +++ b/lib/dns/gen.c @@ -15,21 +15,24 @@ * SOFTWARE. */ - /* $Id: gen.c,v 1.27 1999/10/07 02:55:00 halley Exp $ */ + /* $Id: gen.c,v 1.28 1999/10/08 22:57:20 tale Exp $ */ #include #include #include -#include #include #include #include #include #include -#include +#ifdef WIN32 +#include "gen-win32.h" +#else +#include "gen-unix.h" +#endif #define FROMTEXTDECL "dns_rdataclass_t rdclass, dns_rdatatype_t type, isc_lex_t *lexer, dns_name_t *origin, isc_boolean_t downcase, isc_buffer_t *target" #define FROMTEXTARGS "rdclass, type, lexer, origin, downcase, target" @@ -323,40 +326,38 @@ add(int rdclass, char *classname, int type, char *typename, char *dirname) { } void -sd(int rdclass, char *classname, char *dir, char filetype) { +sd(int rdclass, char *classname, char *dirname, char filetype) { char buf[sizeof "0123456789_65535.h"]; char fmt[sizeof "%10[-0-9a-z]_%d.h"]; - DIR *d; int type; char typename[11]; - struct dirent *dp; + isc_dir_t dir; - if ((d = opendir(dir)) == NULL) + if (!start_directory(dirname, &dir)) return; sprintf(fmt,"%s%c", "%10[-0-9a-z]_%d.", filetype); - while ((dp = readdir(d)) != NULL) { - if (sscanf(dp->d_name, fmt, typename, &type) != 2) + while (next_file(&dir)) { + if (sscanf(dir.filename, fmt, typename, &type) != 2) continue; if ((type > 65535) || (type < 0)) continue; sprintf(buf, "%s_%d.%c", typename, type, filetype); - if (strcmp(buf, dp->d_name) != 0) + if (strcmp(buf, dir.filename) != 0) continue; - add(rdclass, classname, type, typename, dir); + add(rdclass, classname, type, typename, dirname); } - closedir(d); + + end_directory(&dir); } int main(int argc, char **argv) { - DIR *d; char buf[256]; /* XXX Should be max path length */ char srcdir[256]; /* XXX Should be max path length */ int rdclass; char classname[11]; - struct dirent *dp; struct tt *tt; struct cc *cc; struct tm *tm; @@ -373,9 +374,10 @@ main(int argc, char **argv) { FILE *fd; char *prefix = NULL; char *suffix = NULL; + isc_dir_t dir; strcpy(srcdir, ""); - while ((c = getopt(argc, argv, "cits:P:S:")) != -1) + while ((c = isc_commandline_parse(argc, argv, "cits:P:S:")) != -1) switch (c) { case 'c': code = 0; @@ -399,35 +401,36 @@ main(int argc, char **argv) { filetype = 'h'; break; case 's': - sprintf(srcdir, "%s/", optarg); + sprintf(srcdir, "%s/", isc_commandline_argument); break; case 'P': - prefix = optarg; + prefix = isc_commandline_argument; break; case 'S': - suffix = optarg; + suffix = isc_commandline_argument; break; case '?': exit(1); } sprintf(buf, "%srdata", srcdir); - if ((d = opendir(buf)) == NULL) + + if (!start_directory(buf, &dir)) exit(1); - while ((dp = readdir(d)) != NULL) { - if (sscanf(dp->d_name, "%10[0-9a-z]_%d", + while (next_file(&dir)) { + if (sscanf(dir.filename, "%10[0-9a-z]_%d", classname, &rdclass) != 2) continue; if ((rdclass > 65535) || (rdclass < 0)) continue; sprintf(buf, "%srdata/%s_%d", srcdir, classname, rdclass); - if (strcmp(buf + 6 + strlen(srcdir), dp->d_name) != 0) + if (strcmp(buf + 6 + strlen(srcdir), dir.filename) != 0) continue; sd(rdclass, classname, buf, filetype); } - closedir(d); + end_directory(&dir); sprintf(buf, "%srdata/generic", srcdir); sd(0, "", buf, filetype);