xref: /titanic_44/usr/src/cmd/genmsg/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1995 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <limits.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <libgen.h>
34*7c478bd9Sstevel@tonic-gate #include <libintl.h>
35*7c478bd9Sstevel@tonic-gate #include <locale.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "genmsg.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	MSG_SUFFIX	".msg"
42*7c478bd9Sstevel@tonic-gate #define	NEW_SUFFIX	".new"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
45*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"genmsg"
46*7c478bd9Sstevel@tonic-gate #endif
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * External functions.
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate extern void write_msgfile(char *);	/* from util.c */
52*7c478bd9Sstevel@tonic-gate extern int read_projfile(char *);	/* from util.c */
53*7c478bd9Sstevel@tonic-gate extern void write_projfile(char *);	/* from util.c */
54*7c478bd9Sstevel@tonic-gate extern void read_msgfile(char *);	/* from util.c */
55*7c478bd9Sstevel@tonic-gate extern int is_writable(char *);		/* from util.c */
56*7c478bd9Sstevel@tonic-gate extern int file_copy(char *, char *);	/* from util.c */
57*7c478bd9Sstevel@tonic-gate extern void init_lex(void);		/* from genmsg.l */
58*7c478bd9Sstevel@tonic-gate extern void init_linemsgid(void);	/* from genmsg.l */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /* Program name. */
61*7c478bd9Sstevel@tonic-gate char *program;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /* File pointer for auto-message-numbering. */
64*7c478bd9Sstevel@tonic-gate FILE *newfp = NULL;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /* Input source file. */
67*7c478bd9Sstevel@tonic-gate char *srcfile;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /* Tag for message comments. */
70*7c478bd9Sstevel@tonic-gate char *mctag = NULL;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /* Tag for set number comments. */
73*7c478bd9Sstevel@tonic-gate char *sctag = NULL;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /* Mode mask to define the genmsg tasks. */
76*7c478bd9Sstevel@tonic-gate Mode active_mode = NoMode;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * This flag will be TRUE if a catgets() call is found
80*7c478bd9Sstevel@tonic-gate  * in the input file.
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate int is_cat_found = FALSE;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /* Suppress an error message if this flag is TRUE. */
85*7c478bd9Sstevel@tonic-gate int suppress_error = FALSE;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /* Prefix and suffix of messages for testing. */
88*7c478bd9Sstevel@tonic-gate char *premsg = NULL;
89*7c478bd9Sstevel@tonic-gate char *sufmsg = NULL;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate static void usage(void);
92*7c478bd9Sstevel@tonic-gate static void validate_options(void);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate int
95*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	int c;
98*7c478bd9Sstevel@tonic-gate 	extern char *optarg;	/* from getopt */
99*7c478bd9Sstevel@tonic-gate 	extern int optind;	/* from getopt */
100*7c478bd9Sstevel@tonic-gate 	extern FILE *yyin;	/* from lex */
101*7c478bd9Sstevel@tonic-gate 	extern int yyparse(void);
102*7c478bd9Sstevel@tonic-gate 	char *msgfile = NULL;
103*7c478bd9Sstevel@tonic-gate 	char *projfile = NULL;
104*7c478bd9Sstevel@tonic-gate 	char *newprojfile = NULL;
105*7c478bd9Sstevel@tonic-gate 	char *cpppath = NULL;
106*7c478bd9Sstevel@tonic-gate 	int do_msgfile = FALSE;
107*7c478bd9Sstevel@tonic-gate 	int tmpfd = -1;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	program = basename(argv[0]);
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
112*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "arndfg:o:l:p:c:s:m:M:txvb")) != EOF) {
115*7c478bd9Sstevel@tonic-gate 		switch (c) {
116*7c478bd9Sstevel@tonic-gate 		case 'o':
117*7c478bd9Sstevel@tonic-gate 			SetActiveMode(MessageMode);
118*7c478bd9Sstevel@tonic-gate 			msgfile = optarg;
119*7c478bd9Sstevel@tonic-gate 			break;
120*7c478bd9Sstevel@tonic-gate 		case 'a':
121*7c478bd9Sstevel@tonic-gate 			SetActiveMode(AppendMode);
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		case 'l':
124*7c478bd9Sstevel@tonic-gate 			projfile = optarg;
125*7c478bd9Sstevel@tonic-gate 			SetActiveMode(AutoNumMode);
126*7c478bd9Sstevel@tonic-gate 			break;
127*7c478bd9Sstevel@tonic-gate 		case 'r':
128*7c478bd9Sstevel@tonic-gate 			SetActiveMode(ReverseMode);
129*7c478bd9Sstevel@tonic-gate 			break;
130*7c478bd9Sstevel@tonic-gate 		case 'p':
131*7c478bd9Sstevel@tonic-gate 			cpppath = optarg;
132*7c478bd9Sstevel@tonic-gate 			SetActiveMode(PreProcessMode);
133*7c478bd9Sstevel@tonic-gate 			break;
134*7c478bd9Sstevel@tonic-gate 		case 'g':
135*7c478bd9Sstevel@tonic-gate 			newprojfile = optarg;
136*7c478bd9Sstevel@tonic-gate 			suppress_error = TRUE;
137*7c478bd9Sstevel@tonic-gate 			SetActiveMode(ProjectMode);
138*7c478bd9Sstevel@tonic-gate 			break;
139*7c478bd9Sstevel@tonic-gate 		case 'c':
140*7c478bd9Sstevel@tonic-gate 			mctag = optarg;
141*7c478bd9Sstevel@tonic-gate 			SetActiveMode(MsgCommentMode);
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		case 's':
144*7c478bd9Sstevel@tonic-gate 			sctag = optarg;
145*7c478bd9Sstevel@tonic-gate 			SetActiveMode(SetCommentMode);
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		case 'b':
148*7c478bd9Sstevel@tonic-gate 			SetActiveMode(BackCommentMode);
149*7c478bd9Sstevel@tonic-gate 			break;
150*7c478bd9Sstevel@tonic-gate 		case 'n':
151*7c478bd9Sstevel@tonic-gate 			SetActiveMode(LineInfoMode);
152*7c478bd9Sstevel@tonic-gate 			break;
153*7c478bd9Sstevel@tonic-gate 		case 'm':
154*7c478bd9Sstevel@tonic-gate 			premsg = optarg;
155*7c478bd9Sstevel@tonic-gate 			SetActiveMode(PrefixMode);
156*7c478bd9Sstevel@tonic-gate 			break;
157*7c478bd9Sstevel@tonic-gate 		case 'M':
158*7c478bd9Sstevel@tonic-gate 			sufmsg = optarg;
159*7c478bd9Sstevel@tonic-gate 			SetActiveMode(SuffixMode);
160*7c478bd9Sstevel@tonic-gate 			break;
161*7c478bd9Sstevel@tonic-gate 		case 't':
162*7c478bd9Sstevel@tonic-gate 			SetActiveMode(TripleMode);
163*7c478bd9Sstevel@tonic-gate 			break;
164*7c478bd9Sstevel@tonic-gate 		case 'd':
165*7c478bd9Sstevel@tonic-gate 			SetActiveMode(DoubleLineMode);
166*7c478bd9Sstevel@tonic-gate 			break;
167*7c478bd9Sstevel@tonic-gate 		case 'f':
168*7c478bd9Sstevel@tonic-gate 			SetActiveMode(OverwriteMode);
169*7c478bd9Sstevel@tonic-gate 			break;
170*7c478bd9Sstevel@tonic-gate 		case 'x':
171*7c478bd9Sstevel@tonic-gate 			suppress_error = TRUE;
172*7c478bd9Sstevel@tonic-gate 			SetActiveMode(NoErrorMode);
173*7c478bd9Sstevel@tonic-gate 			break;
174*7c478bd9Sstevel@tonic-gate 		case 'v':
175*7c478bd9Sstevel@tonic-gate 			(void) printf("genmsg version %s\n", "%I%");
176*7c478bd9Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 		default:
179*7c478bd9Sstevel@tonic-gate 			usage();
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (optind >= argc) {
185*7c478bd9Sstevel@tonic-gate 		usage();
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	validate_options();
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(AutoNumMode)) {
191*7c478bd9Sstevel@tonic-gate 		int len;
192*7c478bd9Sstevel@tonic-gate 		char *tmp;
193*7c478bd9Sstevel@tonic-gate 		if (read_projfile(projfile)) {
194*7c478bd9Sstevel@tonic-gate 			tmp = basename(projfile);
195*7c478bd9Sstevel@tonic-gate 			len = strlen(tmp);
196*7c478bd9Sstevel@tonic-gate 			if ((newprojfile = malloc(len + sizeof (NEW_SUFFIX)))
197*7c478bd9Sstevel@tonic-gate 				== NULL) {
198*7c478bd9Sstevel@tonic-gate 				prg_err(gettext("fatal: out of memory"));
199*7c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
200*7c478bd9Sstevel@tonic-gate 			}
201*7c478bd9Sstevel@tonic-gate 			(void) sprintf(newprojfile, "%s%s", tmp, NEW_SUFFIX);
202*7c478bd9Sstevel@tonic-gate 		} else {
203*7c478bd9Sstevel@tonic-gate 			newprojfile = basename(projfile);
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if ((IsActiveMode(AutoNumMode) || IsActiveMode(ProjectMode)) &&
208*7c478bd9Sstevel@tonic-gate 		(is_writable(IsActiveMode(OverwriteMode) ?
209*7c478bd9Sstevel@tonic-gate 				projfile : newprojfile) == FALSE)) {
210*7c478bd9Sstevel@tonic-gate 		prg_err(gettext("cannot write \"%s\": permission denied"),
211*7c478bd9Sstevel@tonic-gate 			IsActiveMode(OverwriteMode) ? projfile : newprojfile);
212*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(AppendMode) && msgfile) {
216*7c478bd9Sstevel@tonic-gate 		read_msgfile(msgfile);
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (!msgfile) {
220*7c478bd9Sstevel@tonic-gate 		char *tmp = basename(argv[optind]);
221*7c478bd9Sstevel@tonic-gate 		int len = strlen(tmp);
222*7c478bd9Sstevel@tonic-gate 		if ((msgfile = malloc(len + sizeof (MSG_SUFFIX)))
223*7c478bd9Sstevel@tonic-gate 			== NULL) {
224*7c478bd9Sstevel@tonic-gate 			prg_err(gettext("fatal: out of memory"));
225*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
226*7c478bd9Sstevel@tonic-gate 		}
227*7c478bd9Sstevel@tonic-gate 		(void) sprintf(msgfile, "%s%s", tmp, MSG_SUFFIX);
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	while (optind < argc) {
231*7c478bd9Sstevel@tonic-gate 		is_cat_found = FALSE;
232*7c478bd9Sstevel@tonic-gate 		srcfile = argv[optind];
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		if (IsActiveMode(AutoNumMode) || IsActiveMode(ReverseMode)) {
235*7c478bd9Sstevel@tonic-gate 			init_linemsgid();
236*7c478bd9Sstevel@tonic-gate 		}
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 		if (IsActiveMode(PreProcessMode)) {
239*7c478bd9Sstevel@tonic-gate 			char cmd[MAXPATHLEN];
240*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cmd, "%s %s", cpppath, srcfile);
241*7c478bd9Sstevel@tonic-gate 			if ((yyin = (FILE *) popen(cmd, "r")) == NULL) {
242*7c478bd9Sstevel@tonic-gate 				prg_err(
243*7c478bd9Sstevel@tonic-gate 				gettext("fatal: cannot execute \"%s\""),
244*7c478bd9Sstevel@tonic-gate 				cpppath);
245*7c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
246*7c478bd9Sstevel@tonic-gate 			}
247*7c478bd9Sstevel@tonic-gate 		} else {
248*7c478bd9Sstevel@tonic-gate 			if ((yyin = fopen(srcfile, "r")) == NULL) {
249*7c478bd9Sstevel@tonic-gate 				prg_err(/* stupid cstyle */
250*7c478bd9Sstevel@tonic-gate 				gettext("cannot open \"%s\""), srcfile);
251*7c478bd9Sstevel@tonic-gate 				goto end;
252*7c478bd9Sstevel@tonic-gate 			}
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		init_lex();
256*7c478bd9Sstevel@tonic-gate 		yyparse();
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		if (IsActiveMode(PreProcessMode)) {
259*7c478bd9Sstevel@tonic-gate 			if (pclose(yyin) != 0) {
260*7c478bd9Sstevel@tonic-gate 				prg_err(gettext("\"%s\" failed for \"%s\""),
261*7c478bd9Sstevel@tonic-gate 					cpppath, srcfile);
262*7c478bd9Sstevel@tonic-gate 				goto end;
263*7c478bd9Sstevel@tonic-gate 			}
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		if (is_cat_found == FALSE) {
267*7c478bd9Sstevel@tonic-gate 			if (!IsActiveMode(PreProcessMode)) {
268*7c478bd9Sstevel@tonic-gate 				(void) fclose(yyin);
269*7c478bd9Sstevel@tonic-gate 			}
270*7c478bd9Sstevel@tonic-gate 			goto end;
271*7c478bd9Sstevel@tonic-gate 		}
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 		if (!do_msgfile) {
274*7c478bd9Sstevel@tonic-gate 			do_msgfile = TRUE;
275*7c478bd9Sstevel@tonic-gate 		}
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 		if (IsActiveMode(AutoNumMode) || IsActiveMode(ReverseMode)) {
278*7c478bd9Sstevel@tonic-gate 			char *tmp = basename(srcfile);
279*7c478bd9Sstevel@tonic-gate 			char newfile[MAXPATHLEN];
280*7c478bd9Sstevel@tonic-gate 			char tmpfile[32];
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(OverwriteMode)) {
283*7c478bd9Sstevel@tonic-gate 				(void) sprintf(newfile, "%s", srcfile);
284*7c478bd9Sstevel@tonic-gate 			} else {
285*7c478bd9Sstevel@tonic-gate 				(void) sprintf(newfile, "%s%s",
286*7c478bd9Sstevel@tonic-gate 						tmp, NEW_SUFFIX);
287*7c478bd9Sstevel@tonic-gate 			}
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 			if (is_writable(newfile) == FALSE) {
290*7c478bd9Sstevel@tonic-gate 				prg_err(
291*7c478bd9Sstevel@tonic-gate 			gettext("cannot create \"%s\": permission denied"),
292*7c478bd9Sstevel@tonic-gate 					newfile);
293*7c478bd9Sstevel@tonic-gate 				goto end;
294*7c478bd9Sstevel@tonic-gate 			}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 			(void) sprintf(tmpfile, "%s", "/tmp/gensmg.XXXXXX");
297*7c478bd9Sstevel@tonic-gate 			if ((tmpfd = mkstemp(tmpfile)) == -1) {
298*7c478bd9Sstevel@tonic-gate 				prg_err(/* stupid cstyle */
299*7c478bd9Sstevel@tonic-gate 				gettext("cannot create \"%s\""), tmpfile);
300*7c478bd9Sstevel@tonic-gate 				if (!IsActiveMode(PreProcessMode)) {
301*7c478bd9Sstevel@tonic-gate 					(void) fclose(yyin);
302*7c478bd9Sstevel@tonic-gate 				}
303*7c478bd9Sstevel@tonic-gate 					goto end;
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
306*7c478bd9Sstevel@tonic-gate 			if ((newfp = fopen(tmpfile, "w")) == NULL) {
307*7c478bd9Sstevel@tonic-gate 				prg_err(/* stupid cstyle */
308*7c478bd9Sstevel@tonic-gate 				gettext("cannot create \"%s\""), tmpfile);
309*7c478bd9Sstevel@tonic-gate 				if (!IsActiveMode(PreProcessMode)) {
310*7c478bd9Sstevel@tonic-gate 					(void) fclose(yyin);
311*7c478bd9Sstevel@tonic-gate 				}
312*7c478bd9Sstevel@tonic-gate 				(void) unlink(tmpfile);
313*7c478bd9Sstevel@tonic-gate 				goto end;
314*7c478bd9Sstevel@tonic-gate 			}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(PreProcessMode)) {
317*7c478bd9Sstevel@tonic-gate 				if ((yyin = fopen(srcfile, "r")) == NULL) {
318*7c478bd9Sstevel@tonic-gate 					prg_err(/* stupid cstyle */
319*7c478bd9Sstevel@tonic-gate 				gettext("cannot open \"%s\""), srcfile);
320*7c478bd9Sstevel@tonic-gate 					(void) unlink(tmpfile);
321*7c478bd9Sstevel@tonic-gate 					goto end;
322*7c478bd9Sstevel@tonic-gate 				}
323*7c478bd9Sstevel@tonic-gate 			} else {
324*7c478bd9Sstevel@tonic-gate 				rewind(yyin);
325*7c478bd9Sstevel@tonic-gate 			}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 			SetActiveMode(ReplaceMode);
328*7c478bd9Sstevel@tonic-gate 			init_lex();
329*7c478bd9Sstevel@tonic-gate 			yyparse();
330*7c478bd9Sstevel@tonic-gate 			ResetActiveMode(ReplaceMode);
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 			(void) fclose(newfp);
333*7c478bd9Sstevel@tonic-gate 			newfp = NULL;
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 			(void) fclose(yyin);
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 			(void) file_copy(tmpfile, newfile);
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 			goto end;
342*7c478bd9Sstevel@tonic-gate 		}
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		if (!IsActiveMode(PreProcessMode)) {
345*7c478bd9Sstevel@tonic-gate 			(void) fclose(yyin);
346*7c478bd9Sstevel@tonic-gate 		}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate end:
349*7c478bd9Sstevel@tonic-gate 		optind++;
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	if (!do_msgfile) { /* no more business. */
353*7c478bd9Sstevel@tonic-gate 		return (EXIT_SUCCESS);
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if (!IsActiveMode(ReverseMode) && !IsActiveMode(ProjectMode)) {
357*7c478bd9Sstevel@tonic-gate 		write_msgfile(msgfile);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(AutoNumMode) || IsActiveMode(ProjectMode)) {
361*7c478bd9Sstevel@tonic-gate 		write_projfile(IsActiveMode(OverwriteMode) ?
362*7c478bd9Sstevel@tonic-gate 				projfile : newprojfile);
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate 	return (EXIT_SUCCESS);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate static void
368*7c478bd9Sstevel@tonic-gate validate_options(void)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	/* -r doesn't work with either -a or -l. */
371*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ReverseMode) &&
372*7c478bd9Sstevel@tonic-gate 		(IsActiveMode(AutoNumMode) || IsActiveMode(AppendMode))) {
373*7c478bd9Sstevel@tonic-gate 		usage();
374*7c478bd9Sstevel@tonic-gate 	}
375*7c478bd9Sstevel@tonic-gate 	/* -b should be accompanied with -c, -s, -d, and -n. */
376*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(BackCommentMode) &&
377*7c478bd9Sstevel@tonic-gate 		(!IsActiveMode(MsgCommentMode) &&
378*7c478bd9Sstevel@tonic-gate 		!IsActiveMode(SetCommentMode) &&
379*7c478bd9Sstevel@tonic-gate 		!IsActiveMode(DoubleLineMode) &&
380*7c478bd9Sstevel@tonic-gate 		!IsActiveMode(LineInfoMode))) {
381*7c478bd9Sstevel@tonic-gate 		usage();
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ProjectMode) &&
384*7c478bd9Sstevel@tonic-gate 		(IsActiveMode(AutoNumMode) || IsActiveMode(ReverseMode) ||
385*7c478bd9Sstevel@tonic-gate 		IsActiveMode(AppendMode) || IsActiveMode(MsgCommentMode) ||
386*7c478bd9Sstevel@tonic-gate 		IsActiveMode(LineInfoMode) || IsActiveMode(OverwriteMode) ||
387*7c478bd9Sstevel@tonic-gate 		IsActiveMode(PrefixMode) || IsActiveMode(SuffixMode) ||
388*7c478bd9Sstevel@tonic-gate 		IsActiveMode(TripleMode) || IsActiveMode(DoubleLineMode) ||
389*7c478bd9Sstevel@tonic-gate 		IsActiveMode(MessageMode) || IsActiveMode(NoErrorMode))) {
390*7c478bd9Sstevel@tonic-gate 		usage();
391*7c478bd9Sstevel@tonic-gate 	}
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate static void
395*7c478bd9Sstevel@tonic-gate usage(void)
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
398*7c478bd9Sstevel@tonic-gate 	gettext("Usage: %s [-o message-file] [-a] [-d] [-p preprocessor]\n"
399*7c478bd9Sstevel@tonic-gate 		"          [-s set-tag] [-c message-tag] [-b] [-n]\n"
400*7c478bd9Sstevel@tonic-gate 		"          [-l project-file] [-r] [-f] [-g project-file]\n"
401*7c478bd9Sstevel@tonic-gate 		"          [-m prefix] [-M suffix] [-t] [-x] files ...\n"),
402*7c478bd9Sstevel@tonic-gate 		program);
403*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
404*7c478bd9Sstevel@tonic-gate }
405