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