xref: /freebsd/crypto/krb5/src/wconfig.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* wconfig.c */
2 /*
3  * Copyright 1995,1996,1997,1998 by the Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 /*
27  * Program to take the place of the configure shell script under DOS.
28  * The makefile.in files are constructed in such a way that all this
29  * program needs to do is uncomment lines beginning ##DOS by removing the
30  * first 5 characters of the line.  This will allow lines like:
31  * ##DOS!include win-pre.in to become: !include win-pre.in
32  *
33  * We also turn any line beginning with '@' into a blank line.
34  *
35  * If a config directory is specified, then the output will be start with
36  * config\pre.in, then the filtered stdin text, and will end with
37  * config\post.in.
38  *
39  * Syntax: wconfig [options] [config_directory] <input_file >output_file
40  *
41  */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 
47 static int copy_file (char *path, char *fname);
48 void add_ignore_list(char *str);
49 
50 int mit_specific = 0;
51 
52 char *win16_flag = "WIN16##";
53 char *win32_flag = "WIN32##";
54 
55 
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58 	char *ignore_str = "--ignore=";
59 	int ignore_len;
60 	char *cp, *tmp;
61 	char *win_flag;
62 	char wflags[1024];
63 	size_t wlen, alen;
64 
65 #ifdef _WIN32
66 	win_flag = win32_flag;
67 #else
68 	win_flag = "UNIX##";
69 #endif
70 
71 	wlen = 0;
72 
73 	ignore_len = strlen(ignore_str);
74 	argc--; argv++;
75 	while (*argv && *argv[0] == '-') {
76 		alen = strlen(*argv);
77 		if (wlen + 1 + alen > sizeof (wflags) - 1) {
78 			fprintf (stderr,
79 				 "wconfig: argument list too long (internal limit %lu)",
80 				 (unsigned long) sizeof (wflags));
81 			exit (1);
82 		}
83 		if (wlen > 0)
84 			wflags[wlen++] = ' ';
85 		memcpy(&wflags[wlen], *argv, alen);
86 		wlen += alen;
87 
88 		if (!strcmp(*argv, "--mit")) {
89 			mit_specific = 1;
90 			argc--; argv++;
91 			continue;
92 		}
93 		if (!strcmp(*argv, "--win16")) {
94 			win_flag = win16_flag;
95 			argc--; argv++;
96 			continue;
97 		}
98 		if (!strcmp(*argv, "--win32")) {
99 			win_flag = win32_flag;
100 			argc--; argv++;
101 			continue;
102 		}
103 		if (!strncmp(*argv, "--enable-", 9)) {
104 			tmp = malloc(alen - ignore_len + 3);
105 			if (!tmp) {
106 				fprintf(stderr,
107 					"wconfig: malloc failed!\n");
108 				exit(1);
109 			}
110 			memcpy(tmp, *argv + ignore_len, alen - ignore_len);
111 			memcpy(tmp + alen - ignore_len, "##", 3);
112 			for (cp = tmp; *cp; cp++) {
113 				if (islower(*cp))
114 					*cp = toupper(*cp);
115 			}
116 			add_ignore_list(tmp);
117 			argc--; argv++;
118 			continue;
119 		}
120 		if (!strncmp(*argv, ignore_str, ignore_len)) {
121 			add_ignore_list((*argv)+ignore_len);
122 			argc--; argv++;
123 			continue;
124 		}
125 		fprintf(stderr, "Invalid option: %s\n", *argv);
126 		exit(1);
127 	}
128 	wflags[wlen] = '\0';
129 
130 	if (win_flag)
131 		add_ignore_list(win_flag);
132 
133 	if (mit_specific)
134 		add_ignore_list("MIT##");
135 
136 	if (wflags[0] && (argc > 0))
137 		printf("WCONFIG_FLAGS=%s\n", wflags);
138 
139 	if (argc > 0)
140 		copy_file (*argv, "win-pre.in");
141 
142 	copy_file("", "-");
143 
144 	if (argc > 0)
145 		copy_file (*argv, "win-post.in");
146 
147 	return 0;
148 }
149 
150 char *ignore_list[64] = {
151 	"DOS##",
152 	"DOS",
153 	};
154 
155 /*
156  * Add a new item to the ignore list
157  */
add_ignore_list(char * str)158 void add_ignore_list(char *str)
159 {
160 	char **cpp;
161 
162 	for (cpp = ignore_list; *cpp; cpp++)
163 		;
164 	*cpp = str;
165 }
166 
167 
168 /*
169  *
170  * Copy_file
171  *
172  * Copies file 'path\fname' to stdout.
173  *
174  */
175 static int
copy_file(char * path,char * fname)176 copy_file (char *path, char *fname)
177 {
178     FILE *fin;
179     char buf[1024];
180     char **cpp, *ptr;
181     size_t len, plen, flen;
182 
183     if (strcmp(fname, "-") == 0) {
184 	    fin = stdin;
185     } else {
186 	    plen = strlen(path);
187 	    flen = strlen(fname);
188 	    if (plen + 1 + flen > sizeof(buf) - 1) {
189 		    fprintf(stderr, "Name %s or %s too long", path, fname);
190 		    return 1;
191 	    }
192 	    memcpy(buf, path, plen);
193 #ifdef _WIN32
194 	    buf[plen] = '\\';
195 #else
196 	    buf[plen] = '/';
197 #endif
198 	    memcpy(buf + plen + 1, fname, flen);
199 	    buf[plen + 1 + flen] = '\0';
200 	    fin = fopen (buf, "r");                     /* File to read */
201 	    if (fin == NULL) {
202 		    fprintf(stderr, "wconfig: Can't open file %s\n", buf);
203 		    return 1;
204 	    }
205     }
206 
207 
208     while (fgets (buf, sizeof(buf), fin) != NULL) { /* Copy file over */
209 	    if (buf[0] == '@') {
210 		    fputs("\n", stdout);
211 		    continue;
212 	    }
213 	    if (buf[0] != '#' || buf[1] != '#') {
214 		    fputs(buf, stdout);
215 		    continue;
216 	    }
217 	    ptr = buf;
218 	    for (cpp = ignore_list; *cpp; cpp++) {
219 		    len = strlen(*cpp);
220 		    if (memcmp (*cpp, buf+2, len) == 0) {
221 			    ptr += 2+len;
222 			    break;
223 		    }
224 	    }
225 	    fputs(ptr, stdout);
226     }
227 
228     fclose (fin);
229 
230     return 0;
231 }
232