xref: /titanic_53/usr/src/cmd/genmsg/genmsg.l (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate %{
2*7c478bd9Sstevel@tonic-gate /*
3*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate  * with the License.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate  *
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <limits.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <libintl.h>
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include "genmsg.h"
35*7c478bd9Sstevel@tonic-gate #include "y.tab.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate extern int is_cat_found;	/* from main.c */
38*7c478bd9Sstevel@tonic-gate extern void add_comment(Mode, char *);/* from util.c */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate int lineno = 1;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * msg_line stores the line number where a msgid is to be replaced.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate int msg_line = 0;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate int end_of_cat = TRUE;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * In preprocessor mode, genmsg has to parse both the original
51*7c478bd9Sstevel@tonic-gate  * soruce code and the code which a preprocessor generates.
52*7c478bd9Sstevel@tonic-gate  * While genmsg is parsing the original source code,  'pound_is_mine'
53*7c478bd9Sstevel@tonic-gate  * is set to TRUE.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate int pound_is_mine = FALSE;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate void warning(char *);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #define NOLINEMSG	-2
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate void set_linemsgid(int, int);
62*7c478bd9Sstevel@tonic-gate int get_linemsgid(int);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * cat_field indicates which token is currently parsed by lex.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate #define	CatdField	0
68*7c478bd9Sstevel@tonic-gate #define	SetidField	1
69*7c478bd9Sstevel@tonic-gate #define	MsgidField	2
70*7c478bd9Sstevel@tonic-gate #define StrField	3
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static int cat_field;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * This will be turned on when '-' is found in the catgets message
76*7c478bd9Sstevel@tonic-gate  * number field.
77*7c478bd9Sstevel@tonic-gate  */
78*7c478bd9Sstevel@tonic-gate static int save_minus = FALSE;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static char *skip_quoted(int skip_ch);
81*7c478bd9Sstevel@tonic-gate static char *skip_comment(void);
82*7c478bd9Sstevel@tonic-gate static void parse_cppline(char *);
83*7c478bd9Sstevel@tonic-gate %}
84*7c478bd9Sstevel@tonic-gate %s CAT
85*7c478bd9Sstevel@tonic-gate %%
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate [0-9a-zA-Z\_\.]catgets	{
88*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
89*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
90*7c478bd9Sstevel@tonic-gate 			}
91*7c478bd9Sstevel@tonic-gate 		}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate catgets[0-9a-zA-Z\_\.]	{
94*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
95*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate catgets		{
100*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) { /* If the previous catgets
101*7c478bd9Sstevel@tonic-gate 					   * state is on, turn it off
102*7c478bd9Sstevel@tonic-gate 					   * first.
103*7c478bd9Sstevel@tonic-gate 					   */
104*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
105*7c478bd9Sstevel@tonic-gate 			}
106*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
107*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
108*7c478bd9Sstevel@tonic-gate 			}
109*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode) &&
110*7c478bd9Sstevel@tonic-gate 				IsActiveMode(PreProcessMode)) {
111*7c478bd9Sstevel@tonic-gate 				; /* Okay, do nothing in this mode. */
112*7c478bd9Sstevel@tonic-gate 			} else {
113*7c478bd9Sstevel@tonic-gate 				BEGIN CAT;
114*7c478bd9Sstevel@tonic-gate 				end_of_cat = FALSE;
115*7c478bd9Sstevel@tonic-gate 				cat_field = CatdField;
116*7c478bd9Sstevel@tonic-gate 				return CATGETS;
117*7c478bd9Sstevel@tonic-gate 			}
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate <CAT>\,		{	/* punctuation */
121*7c478bd9Sstevel@tonic-gate 			cat_field++;
122*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
123*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%c", yytext[0]);
124*7c478bd9Sstevel@tonic-gate 			}
125*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
126*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
127*7c478bd9Sstevel@tonic-gate 			} else {
128*7c478bd9Sstevel@tonic-gate 				return yytext[0];
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate <CAT>[+*/();>]	{	/* punctuation */
133*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
134*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%c", yytext[0]);
135*7c478bd9Sstevel@tonic-gate 			}
136*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
137*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
138*7c478bd9Sstevel@tonic-gate 			} else {
139*7c478bd9Sstevel@tonic-gate 				return yytext[0];
140*7c478bd9Sstevel@tonic-gate 			}
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate <CAT>const	{
144*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
145*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
146*7c478bd9Sstevel@tonic-gate 			}
147*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
148*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
149*7c478bd9Sstevel@tonic-gate 			} else {
150*7c478bd9Sstevel@tonic-gate 				return CONST;
151*7c478bd9Sstevel@tonic-gate 			}
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate <CAT>nl_catd	{
155*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
156*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
157*7c478bd9Sstevel@tonic-gate 			}
158*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
159*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
160*7c478bd9Sstevel@tonic-gate 			} else {
161*7c478bd9Sstevel@tonic-gate 				return CATD;
162*7c478bd9Sstevel@tonic-gate 			}
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate <CAT>char	{
166*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
167*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
170*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
171*7c478bd9Sstevel@tonic-gate 			} else {
172*7c478bd9Sstevel@tonic-gate 				return CHAR;
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 		}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate <CAT>int	{
177*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
178*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
179*7c478bd9Sstevel@tonic-gate 			}
180*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
181*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
182*7c478bd9Sstevel@tonic-gate 			} else {
183*7c478bd9Sstevel@tonic-gate 				return INT;
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate <CAT>\+\+	{
188*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
189*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
190*7c478bd9Sstevel@tonic-gate 			}
191*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
192*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
193*7c478bd9Sstevel@tonic-gate 			} else {
194*7c478bd9Sstevel@tonic-gate 				return INC;
195*7c478bd9Sstevel@tonic-gate 			}
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate <CAT>\-\-	{
199*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
200*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
201*7c478bd9Sstevel@tonic-gate 			}
202*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
203*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
204*7c478bd9Sstevel@tonic-gate 			} else {
205*7c478bd9Sstevel@tonic-gate 				return INC;
206*7c478bd9Sstevel@tonic-gate 			}
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate <CAT>\"		{	/* extract quoted string */
210*7c478bd9Sstevel@tonic-gate 			yylval.str = skip_quoted('"');
211*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
212*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "\"%s\"", yylval.str);
213*7c478bd9Sstevel@tonic-gate 			}
214*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) { /* just in case */
215*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
216*7c478bd9Sstevel@tonic-gate 				free(yylval.str);
217*7c478bd9Sstevel@tonic-gate 			} else {
218*7c478bd9Sstevel@tonic-gate 				return QSTR;
219*7c478bd9Sstevel@tonic-gate 			}
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate <CAT>-		{	/* punctuation */
223*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
224*7c478bd9Sstevel@tonic-gate 				if (cat_field == MsgidField &&
225*7c478bd9Sstevel@tonic-gate 					get_linemsgid(lineno) != NOLINEMSG) {
226*7c478bd9Sstevel@tonic-gate 					save_minus = TRUE; /*  be replaced. */
227*7c478bd9Sstevel@tonic-gate 				} else {
228*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%c", yytext[0]);
229*7c478bd9Sstevel@tonic-gate 				}
230*7c478bd9Sstevel@tonic-gate                         }
231*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) { /* just in case */
232*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
233*7c478bd9Sstevel@tonic-gate 			} else {
234*7c478bd9Sstevel@tonic-gate 				return yytext[0];
235*7c478bd9Sstevel@tonic-gate 			}
236*7c478bd9Sstevel@tonic-gate 		}
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate <CAT>[0-9]+	{	/* numbers */
239*7c478bd9Sstevel@tonic-gate 			switch (cat_field) {
240*7c478bd9Sstevel@tonic-gate 			case SetidField:
241*7c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
242*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
243*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%s", yytext);
244*7c478bd9Sstevel@tonic-gate 				}
245*7c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
246*7c478bd9Sstevel@tonic-gate 					BEGIN 0;
247*7c478bd9Sstevel@tonic-gate 				} else {
248*7c478bd9Sstevel@tonic-gate 					return SETID;
249*7c478bd9Sstevel@tonic-gate 				}
250*7c478bd9Sstevel@tonic-gate 			case MsgidField:
251*7c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
252*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
253*7c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
254*7c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
255*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%s", yytext);
256*7c478bd9Sstevel@tonic-gate 					} else if (id == NOMSGID &&
257*7c478bd9Sstevel@tonic-gate 						IsActiveMode(ReverseMode)) {
258*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%d", NOMSGID);
259*7c478bd9Sstevel@tonic-gate 					} else if (save_minus == TRUE &&
260*7c478bd9Sstevel@tonic-gate 						yylval.id == 1) {
261*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%d",id);
262*7c478bd9Sstevel@tonic-gate 					} else { /* just in case */
263*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%s", yytext);
264*7c478bd9Sstevel@tonic-gate 					}
265*7c478bd9Sstevel@tonic-gate 					save_minus = FALSE;
266*7c478bd9Sstevel@tonic-gate 				} else {
267*7c478bd9Sstevel@tonic-gate 					msg_line = lineno;
268*7c478bd9Sstevel@tonic-gate 				}
269*7c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
270*7c478bd9Sstevel@tonic-gate 					BEGIN 0;
271*7c478bd9Sstevel@tonic-gate 				} else {
272*7c478bd9Sstevel@tonic-gate 					return MSGID;
273*7c478bd9Sstevel@tonic-gate 				}
274*7c478bd9Sstevel@tonic-gate 			default:
275*7c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
276*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
277*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%s", yytext);
278*7c478bd9Sstevel@tonic-gate 				}
279*7c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
280*7c478bd9Sstevel@tonic-gate 					BEGIN 0;
281*7c478bd9Sstevel@tonic-gate 				} else {
282*7c478bd9Sstevel@tonic-gate 					return DIGIT;
283*7c478bd9Sstevel@tonic-gate 				}
284*7c478bd9Sstevel@tonic-gate 			}
285*7c478bd9Sstevel@tonic-gate 		}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate <CAT>[a-zA-Z0-9_\&][a-zA-Z0-9_\>\&\.]*	{
288*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
289*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
290*7c478bd9Sstevel@tonic-gate 			}
291*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
292*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
293*7c478bd9Sstevel@tonic-gate 			} else {
294*7c478bd9Sstevel@tonic-gate 				return STR;
295*7c478bd9Sstevel@tonic-gate 			}
296*7c478bd9Sstevel@tonic-gate 		}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate <CAT>\n		{
299*7c478bd9Sstevel@tonic-gate 			lineno++;
300*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
301*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "\n");
302*7c478bd9Sstevel@tonic-gate 			}
303*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
304*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
305*7c478bd9Sstevel@tonic-gate 			}
306*7c478bd9Sstevel@tonic-gate 		}
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate <CAT>.		{	/* not interested */
309*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
310*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%c", yytext[0]);
311*7c478bd9Sstevel@tonic-gate 			}
312*7c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
313*7c478bd9Sstevel@tonic-gate 				BEGIN 0;
314*7c478bd9Sstevel@tonic-gate 			}
315*7c478bd9Sstevel@tonic-gate 		}
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate -((([ \t]+)1)|1) {	/* -1 */
318*7c478bd9Sstevel@tonic-gate 			if (end_of_cat == FALSE) {
319*7c478bd9Sstevel@tonic-gate 				REJECT;
320*7c478bd9Sstevel@tonic-gate 			} else if (IsActiveMode(ReplaceMode)) {
321*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(PreProcessMode)) {
322*7c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
323*7c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
324*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%s", yytext);
325*7c478bd9Sstevel@tonic-gate 					} else { /* could be -1. */
326*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%d", id);
327*7c478bd9Sstevel@tonic-gate 					}
328*7c478bd9Sstevel@tonic-gate 				} else {
329*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%s", yytext);
330*7c478bd9Sstevel@tonic-gate 				}
331*7c478bd9Sstevel@tonic-gate 			}
332*7c478bd9Sstevel@tonic-gate 		}
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate [0-9]+		{
335*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
336*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(PreProcessMode) &&
337*7c478bd9Sstevel@tonic-gate 					IsActiveMode(ReverseMode)) {
338*7c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
339*7c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
340*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%s", yytext);
341*7c478bd9Sstevel@tonic-gate 					} else if (id == NOMSGID) {
342*7c478bd9Sstevel@tonic-gate 						fprintf(newfp, "%d", id);
343*7c478bd9Sstevel@tonic-gate 					}
344*7c478bd9Sstevel@tonic-gate 				} else {
345*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%s", yytext);
346*7c478bd9Sstevel@tonic-gate 				}
347*7c478bd9Sstevel@tonic-gate 			}
348*7c478bd9Sstevel@tonic-gate 		}
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate ^#[ \t]*[0-9]+.*\n	{	/* pound for c-preprocessor */
351*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(PreProcessMode)) {
352*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
353*7c478bd9Sstevel@tonic-gate 					fprintf(newfp, "%s", yytext);
354*7c478bd9Sstevel@tonic-gate 				} else {
355*7c478bd9Sstevel@tonic-gate 					parse_cppline(yytext);
356*7c478bd9Sstevel@tonic-gate 				}
357*7c478bd9Sstevel@tonic-gate 			} else if (IsActiveMode(ReplaceMode)) {
358*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
359*7c478bd9Sstevel@tonic-gate 			}
360*7c478bd9Sstevel@tonic-gate 			lineno++;
361*7c478bd9Sstevel@tonic-gate 		}
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate "/*"		{	/* skip a comment block */
364*7c478bd9Sstevel@tonic-gate 			char *comment = skip_comment();
365*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
366*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", comment);
367*7c478bd9Sstevel@tonic-gate 			} else {
368*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(MsgCommentMode)) {
369*7c478bd9Sstevel@tonic-gate 					add_comment(MsgCommentMode, comment);
370*7c478bd9Sstevel@tonic-gate 				}
371*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(SetCommentMode)) {
372*7c478bd9Sstevel@tonic-gate 					add_comment(SetCommentMode, comment);
373*7c478bd9Sstevel@tonic-gate 				}
374*7c478bd9Sstevel@tonic-gate 			}
375*7c478bd9Sstevel@tonic-gate 			free(comment);
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate "//".*\n	{	/* skip a c++ comment */
379*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
380*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%s", yytext);
381*7c478bd9Sstevel@tonic-gate 			} else {
382*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(MsgCommentMode)) {
383*7c478bd9Sstevel@tonic-gate 					add_comment(MsgCommentMode, yytext);
384*7c478bd9Sstevel@tonic-gate 				}
385*7c478bd9Sstevel@tonic-gate 				if (IsActiveMode(SetCommentMode)) {
386*7c478bd9Sstevel@tonic-gate 					add_comment(SetCommentMode, yytext);
387*7c478bd9Sstevel@tonic-gate 				}
388*7c478bd9Sstevel@tonic-gate 			}
389*7c478bd9Sstevel@tonic-gate 			lineno++;
390*7c478bd9Sstevel@tonic-gate 		}
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate \"		{	/* skip quoted string */
393*7c478bd9Sstevel@tonic-gate 			char *qstr = skip_quoted('"');
394*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
395*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "\"%s\"", qstr);
396*7c478bd9Sstevel@tonic-gate 			}
397*7c478bd9Sstevel@tonic-gate 			free(qstr);
398*7c478bd9Sstevel@tonic-gate 		}
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate \'		{	/* skip single-quoted character */
401*7c478bd9Sstevel@tonic-gate 			char *qchr = skip_quoted('\'');
402*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
403*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "\'%s\'", qchr);
404*7c478bd9Sstevel@tonic-gate 			}
405*7c478bd9Sstevel@tonic-gate 			free(qchr);
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate \n		{
409*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
410*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "\n");
411*7c478bd9Sstevel@tonic-gate 			}
412*7c478bd9Sstevel@tonic-gate 			lineno++;
413*7c478bd9Sstevel@tonic-gate 		}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate .		{
416*7c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
417*7c478bd9Sstevel@tonic-gate 				fprintf(newfp, "%c", yytext[0]);
418*7c478bd9Sstevel@tonic-gate 			}
419*7c478bd9Sstevel@tonic-gate 		}
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate %%
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate static char *
424*7c478bd9Sstevel@tonic-gate skip_quoted(int skip_ch)
425*7c478bd9Sstevel@tonic-gate {
426*7c478bd9Sstevel@tonic-gate 	char *buf, *ptr;	/* saved buffer and its pointer */
427*7c478bd9Sstevel@tonic-gate 	int bsize = BUFSIZ;	/* growing buffer size */
428*7c478bd9Sstevel@tonic-gate 	int i = 0;		/* counter */
429*7c478bd9Sstevel@tonic-gate 	int c, old = 0;		/* input character */
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 	if ((buf = ptr = (char *) malloc(bsize)) == NULL) {
432*7c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
433*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 	for (;; i++) {
436*7c478bd9Sstevel@tonic-gate 		if (i == bsize) {
437*7c478bd9Sstevel@tonic-gate 			bsize += BUFSIZ;
438*7c478bd9Sstevel@tonic-gate 			if ((buf = (char *) realloc((void *) buf,
439*7c478bd9Sstevel@tonic-gate 				bsize)) == NULL) {
440*7c478bd9Sstevel@tonic-gate 				prg_err(gettext("fatal: out of memory"));
441*7c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
442*7c478bd9Sstevel@tonic-gate 			}
443*7c478bd9Sstevel@tonic-gate 			ptr = buf + i;
444*7c478bd9Sstevel@tonic-gate 		}
445*7c478bd9Sstevel@tonic-gate 		c = input();
446*7c478bd9Sstevel@tonic-gate 		if (c == skip_ch && old != '\\') {
447*7c478bd9Sstevel@tonic-gate 			break;
448*7c478bd9Sstevel@tonic-gate 		} else if (c == '\n') {
449*7c478bd9Sstevel@tonic-gate 			lineno++;
450*7c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
451*7c478bd9Sstevel@tonic-gate 			if (skip_ch == '"') {
452*7c478bd9Sstevel@tonic-gate 				warning(gettext("warning: unmatched \""));
453*7c478bd9Sstevel@tonic-gate 			} else if (skip_ch == '\'') {
454*7c478bd9Sstevel@tonic-gate 				warning(gettext("warning: unmatched '"));
455*7c478bd9Sstevel@tonic-gate 			} else {
456*7c478bd9Sstevel@tonic-gate 				/* Should not happen */
457*7c478bd9Sstevel@tonic-gate 				warning(gettext("warning: unmatched \
458*7c478bd9Sstevel@tonic-gate character"));
459*7c478bd9Sstevel@tonic-gate 			}
460*7c478bd9Sstevel@tonic-gate 			break;
461*7c478bd9Sstevel@tonic-gate 		}
462*7c478bd9Sstevel@tonic-gate 		*ptr++ = c;
463*7c478bd9Sstevel@tonic-gate 		if (old == '\\') {
464*7c478bd9Sstevel@tonic-gate 			old = '\0';
465*7c478bd9Sstevel@tonic-gate 		} else {
466*7c478bd9Sstevel@tonic-gate 			old = c;
467*7c478bd9Sstevel@tonic-gate 		}
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 	*ptr = '\0';
470*7c478bd9Sstevel@tonic-gate 	return buf;
471*7c478bd9Sstevel@tonic-gate }
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate static char *
474*7c478bd9Sstevel@tonic-gate skip_comment(void)
475*7c478bd9Sstevel@tonic-gate {
476*7c478bd9Sstevel@tonic-gate 	char *buf, *ptr;	/* saved buffer and its pointer */
477*7c478bd9Sstevel@tonic-gate 	int bsize = BUFSIZ;	/* growing buffer size */
478*7c478bd9Sstevel@tonic-gate 	int i = 0;		/* counter */
479*7c478bd9Sstevel@tonic-gate 	int c, old = 0;		/* input character */
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	if ((buf = ptr = (char *) malloc(bsize)) == NULL) {
482*7c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
483*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
484*7c478bd9Sstevel@tonic-gate 	}
485*7c478bd9Sstevel@tonic-gate 	*ptr++ = '/';	i++;
486*7c478bd9Sstevel@tonic-gate 	*ptr++ = '*';	i++;
487*7c478bd9Sstevel@tonic-gate 	for (;; i++) {
488*7c478bd9Sstevel@tonic-gate 		if (i == bsize) {
489*7c478bd9Sstevel@tonic-gate 			bsize += BUFSIZ;
490*7c478bd9Sstevel@tonic-gate 			if ((buf = (char *) realloc((void *) buf,
491*7c478bd9Sstevel@tonic-gate 				bsize)) == NULL) {
492*7c478bd9Sstevel@tonic-gate 				prg_err(gettext("fatal: out of memory"));
493*7c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
494*7c478bd9Sstevel@tonic-gate 			}
495*7c478bd9Sstevel@tonic-gate 			ptr = buf + i;
496*7c478bd9Sstevel@tonic-gate 		}
497*7c478bd9Sstevel@tonic-gate 		c = input();
498*7c478bd9Sstevel@tonic-gate 		if (c == '/' && old == '*') {
499*7c478bd9Sstevel@tonic-gate 			*ptr++ = c;
500*7c478bd9Sstevel@tonic-gate 			break;
501*7c478bd9Sstevel@tonic-gate 		} else if (c == '\n') {
502*7c478bd9Sstevel@tonic-gate 			lineno++;
503*7c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
504*7c478bd9Sstevel@tonic-gate 			warning(gettext("warning: unmatched /*"));
505*7c478bd9Sstevel@tonic-gate 			break;
506*7c478bd9Sstevel@tonic-gate 		}
507*7c478bd9Sstevel@tonic-gate 		*ptr++ = old = c;
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate 	*ptr = '\0';
510*7c478bd9Sstevel@tonic-gate 	return buf;
511*7c478bd9Sstevel@tonic-gate }
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate /*
514*7c478bd9Sstevel@tonic-gate  * parse_cppline() parses the line control information that a C
515*7c478bd9Sstevel@tonic-gate  * preprocessor generates to indicate the location in the original
516*7c478bd9Sstevel@tonic-gate  * file.  See the cpp man in the details.
517*7c478bd9Sstevel@tonic-gate  */
518*7c478bd9Sstevel@tonic-gate static void
519*7c478bd9Sstevel@tonic-gate parse_cppline(char *str)
520*7c478bd9Sstevel@tonic-gate {
521*7c478bd9Sstevel@tonic-gate 	int n, line, len;
522*7c478bd9Sstevel@tonic-gate 	char ch;
523*7c478bd9Sstevel@tonic-gate 	char file[LINE_MAX];
524*7c478bd9Sstevel@tonic-gate 	char *pfile = &file[0];
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	n = sscanf(str, "%c%d%s", &ch, &line, file);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	/* 'file' is a quoted string but 'srcfile' is not. */
529*7c478bd9Sstevel@tonic-gate 	len = strlen(file) - 2;
530*7c478bd9Sstevel@tonic-gate 	pfile++;
531*7c478bd9Sstevel@tonic-gate 	if (n == 3 && (strncmp(pfile, srcfile, len) == 0)) {
532*7c478bd9Sstevel@tonic-gate 		pound_is_mine = TRUE;
533*7c478bd9Sstevel@tonic-gate 		lineno = line - 1;
534*7c478bd9Sstevel@tonic-gate 	} else if (n == 2 && (pound_is_mine == TRUE)) {
535*7c478bd9Sstevel@tonic-gate 		lineno = line - 1;
536*7c478bd9Sstevel@tonic-gate 	} else {
537*7c478bd9Sstevel@tonic-gate 		pound_is_mine = FALSE;
538*7c478bd9Sstevel@tonic-gate 	}
539*7c478bd9Sstevel@tonic-gate }
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate typedef struct {
542*7c478bd9Sstevel@tonic-gate 	int line;
543*7c478bd9Sstevel@tonic-gate 	int msgid;
544*7c478bd9Sstevel@tonic-gate } LineMsgID;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate static LineMsgID line_msgid[NL_MSGMAX];
547*7c478bd9Sstevel@tonic-gate static int line_msgcnt;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate void
550*7c478bd9Sstevel@tonic-gate init_lex(void)
551*7c478bd9Sstevel@tonic-gate {
552*7c478bd9Sstevel@tonic-gate 	lineno = 1;
553*7c478bd9Sstevel@tonic-gate 	end_of_cat = TRUE;
554*7c478bd9Sstevel@tonic-gate 	pound_is_mine = FALSE;
555*7c478bd9Sstevel@tonic-gate }
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate void
558*7c478bd9Sstevel@tonic-gate init_linemsgid(void)
559*7c478bd9Sstevel@tonic-gate {
560*7c478bd9Sstevel@tonic-gate 	line_msgcnt = 0;
561*7c478bd9Sstevel@tonic-gate 	memset(line_msgid, 0, sizeof(LineMsgID) * NL_MSGMAX);
562*7c478bd9Sstevel@tonic-gate }
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate void
565*7c478bd9Sstevel@tonic-gate set_linemsgid(int line, int msgid)
566*7c478bd9Sstevel@tonic-gate {
567*7c478bd9Sstevel@tonic-gate 	if (line_msgcnt >= NL_MSGMAX) {
568*7c478bd9Sstevel@tonic-gate 		return; /* oops */
569*7c478bd9Sstevel@tonic-gate 	}
570*7c478bd9Sstevel@tonic-gate 	line_msgid[line_msgcnt].line = line;
571*7c478bd9Sstevel@tonic-gate 	line_msgid[line_msgcnt].msgid = msgid;
572*7c478bd9Sstevel@tonic-gate 	line_msgcnt++;
573*7c478bd9Sstevel@tonic-gate }
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate int
576*7c478bd9Sstevel@tonic-gate get_linemsgid(int line)
577*7c478bd9Sstevel@tonic-gate {
578*7c478bd9Sstevel@tonic-gate 	register int i, left, right;
579*7c478bd9Sstevel@tonic-gate 	left = 0;
580*7c478bd9Sstevel@tonic-gate 	right = line_msgcnt - 1;
581*7c478bd9Sstevel@tonic-gate 	while (left <= right) {
582*7c478bd9Sstevel@tonic-gate 		i = (left + right) >> 1;
583*7c478bd9Sstevel@tonic-gate 		if ( line < line_msgid[i].line) {
584*7c478bd9Sstevel@tonic-gate 			right = i - 1;
585*7c478bd9Sstevel@tonic-gate 		} else if (line > line_msgid[i].line) {
586*7c478bd9Sstevel@tonic-gate 			left = i + 1;
587*7c478bd9Sstevel@tonic-gate 		} else {
588*7c478bd9Sstevel@tonic-gate 			return line_msgid[i].msgid;
589*7c478bd9Sstevel@tonic-gate 		}
590*7c478bd9Sstevel@tonic-gate 	}
591*7c478bd9Sstevel@tonic-gate 	return NOLINEMSG;
592*7c478bd9Sstevel@tonic-gate }
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate void
595*7c478bd9Sstevel@tonic-gate yyerror(char *s)
596*7c478bd9Sstevel@tonic-gate {
597*7c478bd9Sstevel@tonic-gate 	if ((IsActiveMode(PreProcessMode) && pound_is_mine == FALSE) ||
598*7c478bd9Sstevel@tonic-gate 		IsActiveMode(ReplaceMode)) {
599*7c478bd9Sstevel@tonic-gate 		return;
600*7c478bd9Sstevel@tonic-gate 	}
601*7c478bd9Sstevel@tonic-gate 	src_err(srcfile, lineno, gettext("%s before or at: %s"), s, yytext);
602*7c478bd9Sstevel@tonic-gate }
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate void
605*7c478bd9Sstevel@tonic-gate warning(char *s)
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	if ((IsActiveMode(PreProcessMode) && pound_is_mine == FALSE) ||
608*7c478bd9Sstevel@tonic-gate 		IsActiveMode(ReplaceMode)) {
609*7c478bd9Sstevel@tonic-gate 		return;
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate 	src_err(srcfile, lineno, "%s", s);
612*7c478bd9Sstevel@tonic-gate }
613