xref: /netbsd/external/bsd/am-utils/dist/amd/sun2amd.c (revision 6550d01e)
1 /*	$NetBSD: sun2amd.c,v 1.1.1.2 2009/03/20 20:26:50 christos Exp $	*/
2 
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.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Jan-Simon Pendry at Imperial College, London.
13  *
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.
29  *
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.
41  *
42  *
43  * File: am-utils/amd/sun2amd.c
44  *
45  */
46 
47 /*
48  * Translate Sun-syntax maps to Amd maps
49  */
50 
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>
57 
58 
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
63 /*
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.
67  */
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) */
71 
72 
73 /*
74  * Parse the stream sun_in, convert the map information to amd, write
75  * the results to amd_out.
76  */
77 static int
78 sun2amd_convert(FILE *sun_in, FILE *amd_out)
79 {
80   char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry;
81   int pos, line = 0, retval = 1;
82 
83   /* just to be safe */
84   memset(line_buff, 0, sizeof(line_buff));
85 
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';
90 
91     /* remove comments */
92     if ((tmp = strchr(line_buff, '#')) != NULL) {
93       *tmp = '\0';
94     }
95 
96     /* find start of key */
97     key = line_buff;
98     while (*key != '\0' && isspace((unsigned char)*key)) {
99       key++;
100     }
101 
102     /* ignore blank lines */
103     if (*key == '\0') {
104       continue;
105     }
106 
107     /* find the end of the key and NULL terminate */
108     tmp = key;
109     while (*tmp != '\0' && isspace((unsigned char)*tmp) == 0) {
110       tmp++;
111     }
112     if (*tmp == '\0') {
113       plog(XLOG_ERROR, "map line %d has no entry", line);
114       goto err;
115     }
116     *tmp++ = '\0';
117     if (*tmp == '\0') {
118       plog(XLOG_ERROR, "map line %d has no entry", line);
119       goto err;
120     }
121     entry = tmp;
122 
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;
127     }
128 
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;
132     }
133 
134     /* just to be safe */
135     memset(line_buff, 0, sizeof(line_buff));
136   }
137 
138   /* success */
139   retval = 0;
140 
141  err:
142   return retval;
143 }
144 
145 
146 /*
147  * wrapper open function
148  */
149 static FILE *
150 sun2amd_open(const char *path, const char *mode)
151 {
152   FILE *retval = NULL;
153 
154   if ((retval = fopen(path,mode)) == NULL) {
155     plog(XLOG_ERROR,"could not open file %s",path);
156   }
157 
158   return retval;
159 }
160 
161 
162 /*
163  * echo the usage and exit
164  */
165 static void
166 sun2amd_usage(void)
167 {
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");
173 }
174 
175 
176 int
177 main(int argc, char **argv)
178 {
179   /* default in/out to stdin/stdout */
180   FILE *sun_in = stdin, *amd_out = stdout;
181   int opt, retval = 1;
182 
183   while ((opt = getopt(argc, argv , "i:o:hH")) != -1) {
184     switch (opt) {
185 
186     case 'i':
187       if ((sun_in = sun2amd_open(optarg,"r")) == NULL) {
188 	goto err;
189       }
190       break;
191 
192     case 'o':
193       if ((amd_out = sun2amd_open(optarg,"w")) == NULL) {
194 	goto err;
195       }
196       break;
197 
198     case 'h':
199     case 'H':
200       sun2amd_usage();
201       goto err;
202     }
203   }
204 
205   retval = sun2amd_convert(sun_in,amd_out);
206 
207  err:
208   exit(retval);
209 }
210