No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / gen-win32.h
blob4efe005a4d002c1bd4ba2bde2c7b72fc5908dc7e
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 1987, 1993, 1994
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
53 /* Id: gen-win32.h,v 1.25 2009/01/17 23:47:42 tbox Exp */
55 /*! \file
56 * \author Principal Authors: Computer Systems Research Group at UC Berkeley
57 * \author Principal ISC caretaker: DCL
61 * \note This file was adapted from the NetBSD project's source tree, RCS ID:
62 * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
64 * The primary change has been to rename items to the ISC namespace
65 * and format in the ISC coding style.
67 * This file is responsible for defining two operations that are not
68 * directly portable between Unix-like systems and Windows NT, option
69 * parsing and directory scanning. It is here because it was decided
70 * that the "gen" build utility was not to depend on libisc.a, so
71 * the functions declared in isc/commandline.h and isc/dir.h could not
72 * be used.
74 * The commandline stuff is pretty much a straight copy from the initial
75 * isc/commandline.c. The dir stuff was shrunk to fit the needs of gen.c.
78 #ifndef DNS_GEN_WIN32_H
79 #define DNS_GEN_WIN32_H 1
81 #include <stdio.h>
82 #include <string.h>
83 #include <windows.h>
85 #include <isc/boolean.h>
86 #include <isc/lang.h>
88 int isc_commandline_index = 1; /* Index into parent argv vector. */
89 int isc_commandline_option; /* Character checked for validity. */
91 char *isc_commandline_argument; /* Argument associated with option. */
92 char *isc_commandline_progname; /* For printing error messages. */
94 isc_boolean_t isc_commandline_errprint = ISC_TRUE; /* Print error messages. */
95 isc_boolean_t isc_commandline_reset = ISC_TRUE; /* Reset processing. */
97 #define BADOPT '?'
98 #define BADARG ':'
99 #define ENDOPT ""
101 ISC_LANG_BEGINDECLS
104 * getopt --
105 * Parse argc/argv argument vector.
108 isc_commandline_parse(int argc, char * const *argv, const char *options) {
109 static char *place = ENDOPT;
110 char *option; /* Index into *options of option. */
113 * Update scanning pointer, either because a reset was requested or
114 * the previous argv was finished.
116 if (isc_commandline_reset || *place == '\0') {
117 isc_commandline_reset = ISC_FALSE;
119 if (isc_commandline_progname == NULL)
120 isc_commandline_progname = argv[0];
122 if (isc_commandline_index >= argc ||
123 *(place = argv[isc_commandline_index]) != '-') {
125 * Index out of range or points to non-option.
127 place = ENDOPT;
128 return (-1);
131 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
133 * Found '--' to signal end of options. Advance
134 * index to next argv, the first non-option.
136 isc_commandline_index++;
137 place = ENDOPT;
138 return (-1);
142 isc_commandline_option = *place++;
143 option = strchr(options, isc_commandline_option);
146 * Ensure valid option has been passed as specified by options string.
147 * '-:' is never a valid command line option because it could not
148 * distinguish ':' from the argument specifier in the options string.
150 if (isc_commandline_option == ':' || option == NULL) {
151 if (*place == '\0')
152 isc_commandline_index++;
154 if (isc_commandline_errprint && *options != ':')
155 fprintf(stderr, "%s: illegal option -- %c\n",
156 isc_commandline_progname,
157 isc_commandline_option);
159 return (BADOPT);
162 if (*++option != ':') {
164 * Option does not take an argument.
166 isc_commandline_argument = NULL;
169 * Skip to next argv if at the end of the current argv.
171 if (*place == '\0')
172 ++isc_commandline_index;
174 } else {
176 * Option needs an argument.
178 if (*place != '\0')
180 * Option is in this argv, -D1 style.
182 isc_commandline_argument = place;
184 else if (argc > ++isc_commandline_index)
186 * Option is next argv, -D 1 style.
188 isc_commandline_argument = argv[isc_commandline_index];
190 else {
192 * Argument needed, but no more argv.
194 place = ENDOPT;
197 * Silent failure with "missing argument" return
198 * when ':' starts options string, per historical spec.
200 if (*options == ':')
201 return (BADARG);
203 if (isc_commandline_errprint)
204 fprintf(stderr,
205 "%s: option requires an argument -- %c\n",
206 isc_commandline_progname,
207 isc_commandline_option);
209 return (BADOPT);
212 place = ENDOPT;
215 * Point to argv that follows argument.
217 isc_commandline_index++;
220 return (isc_commandline_option);
223 typedef struct {
224 HANDLE handle;
225 WIN32_FIND_DATA find_data;
226 isc_boolean_t first_file;
227 char *filename;
228 } isc_dir_t;
230 isc_boolean_t
231 start_directory(const char *path, isc_dir_t *dir) {
232 char pattern[_MAX_PATH], *p;
235 * Need space for slash-splat and final NUL.
237 if (strlen(path) + 3 > sizeof(pattern))
238 return (ISC_FALSE);
240 strcpy(pattern, path);
243 * Append slash (if needed) and splat.
245 p = pattern + strlen(pattern);
246 if (p != pattern && p[-1] != '\\' && p[-1] != ':')
247 *p++ = '\\';
248 *p++ = '*';
249 *p++ = '\0';
251 dir->first_file = ISC_TRUE;
253 dir->handle = FindFirstFile(pattern, &dir->find_data);
255 if (dir->handle == INVALID_HANDLE_VALUE) {
256 dir->filename = NULL;
257 return (ISC_FALSE);
258 } else {
259 dir->filename = dir->find_data.cFileName;
260 return (ISC_TRUE);
264 isc_boolean_t
265 next_file(isc_dir_t *dir) {
266 if (dir->first_file)
267 dir->first_file = ISC_FALSE;
269 else if (dir->handle != INVALID_HANDLE_VALUE) {
270 if (FindNextFile(dir->handle, &dir->find_data) == TRUE)
271 dir->filename = dir->find_data.cFileName;
272 else
273 dir->filename = NULL;
275 } else
276 dir->filename = NULL;
278 if (dir->filename != NULL)
279 return (ISC_TRUE);
280 else
281 return (ISC_FALSE);
284 void
285 end_directory(isc_dir_t *dir) {
286 if (dir->handle != INVALID_HANDLE_VALUE)
287 FindClose(dir->handle);
290 ISC_LANG_ENDDECLS
292 #endif /* DNS_GEN_WIN32_H */