]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/amd/amd/sun2amd.c
Upgrade Unbound to 1.6.4. More to follow.
[FreeBSD/FreeBSD.git] / contrib / amd / amd / sun2amd.c
1 /*
2  * Copyright (c) 1997-2014 Erez Zadok
3  * Copyright (c) 2005 Daniel P. Ottavio
4  * Copyright (c) 1989 Jan-Simon Pendry
5  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
6  * Copyright (c) 1989 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry at Imperial College, London.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *
37  * File: am-utils/amd/sun2amd.c
38  *
39  */
40
41 /*
42  * Translate Sun-syntax maps to Amd maps
43  */
44
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #endif /* HAVE_CONFIG_H */
48 #include <am_defs.h>
49 #include <amd.h>
50 #include <sun_map.h>
51
52
53 /* dummies to make the program compile and link */
54 struct amu_global_options gopt;
55 #if defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP)
56 # ifdef NEED_LIBWRAP_SEVERITY_VARIABLES
57 /*
58  * Some systems that define libwrap already define these two variables
59  * in libwrap, while others don't: so I need to know precisely iff
60  * to define these two severity variables.
61  */
62 int allow_severity=0, deny_severity=0, rfc931_timeout=0;
63 # endif /* NEED_LIBWRAP_SEVERITY_VARIABLES */
64 #endif /* defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) */
65
66
67 /*
68  * Parse the stream sun_in, convert the map information to amd, write
69  * the results to amd_out.
70  */
71 static int
72 sun2amd_convert(FILE *sun_in, FILE *amd_out)
73 {
74   char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry;
75   int pos, line = 0, retval = 1;
76
77   /* just to be safe */
78   memset(line_buff, 0, sizeof(line_buff));
79
80   /* Read the input line by line and do the conversion. */
81   while ((pos = file_read_line(line_buff, sizeof(line_buff), sun_in))) {
82     line++;
83     line_buff[pos - 1] = '\0';
84
85     /* remove comments */
86     if ((tmp = strchr(line_buff, '#')) != NULL) {
87       *tmp = '\0';
88     }
89
90     /* find start of key */
91     key = line_buff;
92     while (*key != '\0' && isspace((unsigned char)*key)) {
93       key++;
94     }
95
96     /* ignore blank lines */
97     if (*key == '\0') {
98       continue;
99     }
100
101     /* find the end of the key and NULL terminate */
102     tmp = key;
103     while (*tmp != '\0' && isspace((unsigned char)*tmp) == 0) {
104       tmp++;
105     }
106     if (*tmp == '\0') {
107       plog(XLOG_ERROR, "map line %d has no entry", line);
108       goto err;
109     }
110     *tmp++ = '\0';
111     if (*tmp == '\0') {
112       plog(XLOG_ERROR, "map line %d has no entry", line);
113       goto err;
114     }
115     entry = tmp;
116
117     /* convert the sun entry to an amd entry */
118     if ((tmp = sun_entry2amd(key, entry)) == NULL) {
119       plog(XLOG_ERROR, "parse error on line %d", line);
120       goto err;
121     }
122
123     if (fprintf(amd_out, "%s %s\n", key, tmp) < 0) {
124       plog(XLOG_ERROR, "can't write to output stream: %s", strerror(errno));
125       goto err;
126     }
127
128     /* just to be safe */
129     memset(line_buff, 0, sizeof(line_buff));
130   }
131
132   /* success */
133   retval = 0;
134
135  err:
136   return retval;
137 }
138
139
140 /*
141  * wrapper open function
142  */
143 static FILE *
144 sun2amd_open(const char *path, const char *mode)
145 {
146   FILE *retval = NULL;
147
148   if ((retval = fopen(path,mode)) == NULL) {
149     plog(XLOG_ERROR,"could not open file %s",path);
150   }
151
152   return retval;
153 }
154
155
156 /*
157  * echo the usage and exit
158  */
159 static void
160 sun2amd_usage(void)
161 {
162   fprintf(stderr,
163           "usage : sun2amd [-hH] [-i infile] [-o outfile]\n"
164           "-h\thelp\n"
165           "-i\tspecify an infile (defaults to stdin)\n"
166           "-o\tspecify an outfile (defaults to stdout)\n");
167 }
168
169
170 int
171 main(int argc, char **argv)
172 {
173   /* default in/out to stdin/stdout */
174   FILE *sun_in = stdin, *amd_out = stdout;
175   int opt, retval = 1;
176
177   while ((opt = getopt(argc, argv , "i:o:hH")) != -1) {
178     switch (opt) {
179
180     case 'i':
181       if ((sun_in = sun2amd_open(optarg,"r")) == NULL) {
182         goto err;
183       }
184       break;
185
186     case 'o':
187       if ((amd_out = sun2amd_open(optarg,"w")) == NULL) {
188         goto err;
189       }
190       break;
191
192     case 'h':
193     case 'H':
194       sun2amd_usage();
195       goto err;
196     }
197   }
198
199   retval = sun2amd_convert(sun_in,amd_out);
200
201  err:
202   exit(retval);
203 }