xref: /titanic_53/usr/src/cmd/genmsg/genmsg.l (revision 6e54a631bc06b2a4f5919eb8d582d52780e17983)
17c478bd9Sstevel@tonic-gate %{
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * CDDL HEADER START
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
6*6e54a631Smuffin  * Common Development and Distribution License (the "License").
7*6e54a631Smuffin  * You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*6e54a631Smuffin  */
22*6e54a631Smuffin /*
23*6e54a631Smuffin  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*6e54a631Smuffin  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include "genmsg.h"
367c478bd9Sstevel@tonic-gate #include "y.tab.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate extern int is_cat_found;	/* from main.c */
397c478bd9Sstevel@tonic-gate extern void add_comment(Mode, char *);	/* from util.c */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate int lineno = 1;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * msg_line stores the line number where a msgid is to be replaced.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate int msg_line = 0;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int end_of_cat = TRUE;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * In preprocessor mode, genmsg has to parse both the original
527c478bd9Sstevel@tonic-gate  * soruce code and the code which a preprocessor generates.
537c478bd9Sstevel@tonic-gate  * While genmsg is parsing the original source code,  'pound_is_mine'
547c478bd9Sstevel@tonic-gate  * is set to TRUE.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate int pound_is_mine = FALSE;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void warning(char *);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	NOLINEMSG	-2
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void set_linemsgid(int, int);
637c478bd9Sstevel@tonic-gate int get_linemsgid(int);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * cat_field indicates which token is currently parsed by lex.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	CatdField	0
697c478bd9Sstevel@tonic-gate #define	SetidField	1
707c478bd9Sstevel@tonic-gate #define	MsgidField	2
717c478bd9Sstevel@tonic-gate #define	StrField	3
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static int cat_field;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * This will be turned on when '-' is found in the catgets message
777c478bd9Sstevel@tonic-gate  * number field.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate static int save_minus = FALSE;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static char *skip_quoted(int skip_ch);
827c478bd9Sstevel@tonic-gate static char *skip_comment(void);
837c478bd9Sstevel@tonic-gate static void parse_cppline(char *);
847c478bd9Sstevel@tonic-gate %}
857c478bd9Sstevel@tonic-gate %s CAT
867c478bd9Sstevel@tonic-gate %%
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate [0-9a-zA-Z\_\.]catgets	{
897c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
90*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
917c478bd9Sstevel@tonic-gate 			}
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate catgets[0-9a-zA-Z\_\.]	{
957c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
96*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate catgets		{
101*6e54a631Smuffin 			if (end_of_cat) {
102*6e54a631Smuffin 				/*
103*6e54a631Smuffin 				 * If the previous catgets
1047c478bd9Sstevel@tonic-gate 				 * state is on, turn it off
1057c478bd9Sstevel@tonic-gate 				 * first.
1067c478bd9Sstevel@tonic-gate 				 */
1077c478bd9Sstevel@tonic-gate 				BEGIN 0;
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
110*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1117c478bd9Sstevel@tonic-gate 			}
112*6e54a631Smuffin 			if (!IsActiveMode(ReplaceMode) ||
113*6e54a631Smuffin 			    !IsActiveMode(PreProcessMode)) {
1147c478bd9Sstevel@tonic-gate 				BEGIN CAT;
1157c478bd9Sstevel@tonic-gate 				end_of_cat = FALSE;
1167c478bd9Sstevel@tonic-gate 				cat_field = CatdField;
117*6e54a631Smuffin 				return (CATGETS);
1187c478bd9Sstevel@tonic-gate 			}
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate <CAT>\,		{	/* punctuation */
1227c478bd9Sstevel@tonic-gate 			cat_field++;
1237c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
124*6e54a631Smuffin 				(void) fprintf(newfp, "%c", yytext[0]);
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1277c478bd9Sstevel@tonic-gate 				BEGIN 0;
1287c478bd9Sstevel@tonic-gate 			} else {
129*6e54a631Smuffin 				return (yytext[0]);
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate <CAT>[+*/();>]	{	/* punctuation */
1347c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
135*6e54a631Smuffin 				(void) fprintf(newfp, "%c", yytext[0]);
1367c478bd9Sstevel@tonic-gate 			}
1377c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1387c478bd9Sstevel@tonic-gate 				BEGIN 0;
1397c478bd9Sstevel@tonic-gate 			} else {
140*6e54a631Smuffin 				return (yytext[0]);
1417c478bd9Sstevel@tonic-gate 			}
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate <CAT>const	{
1457c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
146*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1497c478bd9Sstevel@tonic-gate 				BEGIN 0;
1507c478bd9Sstevel@tonic-gate 			} else {
151*6e54a631Smuffin 				return (CONST);
1527c478bd9Sstevel@tonic-gate 			}
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate <CAT>nl_catd	{
1567c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
157*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1587c478bd9Sstevel@tonic-gate 			}
1597c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1607c478bd9Sstevel@tonic-gate 				BEGIN 0;
1617c478bd9Sstevel@tonic-gate 			} else {
162*6e54a631Smuffin 				return (CATD);
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate <CAT>char	{
1677c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
168*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1697c478bd9Sstevel@tonic-gate 			}
1707c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1717c478bd9Sstevel@tonic-gate 				BEGIN 0;
1727c478bd9Sstevel@tonic-gate 			} else {
173*6e54a631Smuffin 				return (CHAR);
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate <CAT>int	{
1787c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
179*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1827c478bd9Sstevel@tonic-gate 				BEGIN 0;
1837c478bd9Sstevel@tonic-gate 			} else {
184*6e54a631Smuffin 				return (INT);
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate <CAT>\+\+	{
1897c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
190*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
1937c478bd9Sstevel@tonic-gate 				BEGIN 0;
1947c478bd9Sstevel@tonic-gate 			} else {
195*6e54a631Smuffin 				return (INC);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate <CAT>\-\-	{
2007c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
201*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
2047c478bd9Sstevel@tonic-gate 				BEGIN 0;
2057c478bd9Sstevel@tonic-gate 			} else {
206*6e54a631Smuffin 				return (INC);
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate <CAT>\"		{	/* extract quoted string */
2117c478bd9Sstevel@tonic-gate 			yylval.str = skip_quoted('"');
2127c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
213*6e54a631Smuffin 				(void) fprintf(newfp, "\"%s\"", yylval.str);
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			if (end_of_cat) { /* just in case */
2167c478bd9Sstevel@tonic-gate 				BEGIN 0;
2177c478bd9Sstevel@tonic-gate 				free(yylval.str);
2187c478bd9Sstevel@tonic-gate 			} else {
219*6e54a631Smuffin 				return (QSTR);
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate <CAT>-		{	/* punctuation */
2247c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
2257c478bd9Sstevel@tonic-gate 				if (cat_field == MsgidField &&
2267c478bd9Sstevel@tonic-gate 					get_linemsgid(lineno) != NOLINEMSG) {
2277c478bd9Sstevel@tonic-gate 					save_minus = TRUE; /*  be replaced. */
2287c478bd9Sstevel@tonic-gate 				} else {
229*6e54a631Smuffin 					(void) fprintf(newfp, "%c", yytext[0]);
2307c478bd9Sstevel@tonic-gate 				}
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 			if (end_of_cat) { /* just in case */
2337c478bd9Sstevel@tonic-gate 				BEGIN 0;
2347c478bd9Sstevel@tonic-gate 			} else {
235*6e54a631Smuffin 				return (yytext[0]);
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate <CAT>[0-9]+	{	/* numbers */
2407c478bd9Sstevel@tonic-gate 			switch (cat_field) {
2417c478bd9Sstevel@tonic-gate 			case SetidField:
2427c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
2437c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
244*6e54a631Smuffin 					(void) fprintf(newfp, "%s", yytext);
2457c478bd9Sstevel@tonic-gate 				}
2467c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
2477c478bd9Sstevel@tonic-gate 					BEGIN 0;
2487c478bd9Sstevel@tonic-gate 				} else {
249*6e54a631Smuffin 					return (SETID);
2507c478bd9Sstevel@tonic-gate 				}
251*6e54a631Smuffin 				break;
2527c478bd9Sstevel@tonic-gate 			case MsgidField:
2537c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
2547c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
2557c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
2567c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
257*6e54a631Smuffin 						(void) fprintf(newfp, "%s",
258*6e54a631Smuffin 						    yytext);
2597c478bd9Sstevel@tonic-gate 					} else if (id == NOMSGID &&
2607c478bd9Sstevel@tonic-gate 						IsActiveMode(ReverseMode)) {
261*6e54a631Smuffin 						(void) fprintf(newfp, "%d",
262*6e54a631Smuffin 						    NOMSGID);
2637c478bd9Sstevel@tonic-gate 					} else if (save_minus == TRUE &&
2647c478bd9Sstevel@tonic-gate 						yylval.id == 1) {
265*6e54a631Smuffin 						(void) fprintf(newfp, "%d", id);
2667c478bd9Sstevel@tonic-gate 					} else { /* just in case */
267*6e54a631Smuffin 						(void) fprintf(newfp, "%s",
268*6e54a631Smuffin 						    yytext);
2697c478bd9Sstevel@tonic-gate 					}
2707c478bd9Sstevel@tonic-gate 					save_minus = FALSE;
2717c478bd9Sstevel@tonic-gate 				} else {
2727c478bd9Sstevel@tonic-gate 					msg_line = lineno;
2737c478bd9Sstevel@tonic-gate 				}
2747c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
2757c478bd9Sstevel@tonic-gate 					BEGIN 0;
2767c478bd9Sstevel@tonic-gate 				} else {
277*6e54a631Smuffin 					return (MSGID);
2787c478bd9Sstevel@tonic-gate 				}
279*6e54a631Smuffin 				break;
2807c478bd9Sstevel@tonic-gate 			default:
2817c478bd9Sstevel@tonic-gate 				yylval.id = atoi(yytext);
2827c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
283*6e54a631Smuffin 					(void) fprintf(newfp, "%s", yytext);
2847c478bd9Sstevel@tonic-gate 				}
2857c478bd9Sstevel@tonic-gate 				if (end_of_cat) {
2867c478bd9Sstevel@tonic-gate 					BEGIN 0;
2877c478bd9Sstevel@tonic-gate 				} else {
288*6e54a631Smuffin 					return (DIGIT);
2897c478bd9Sstevel@tonic-gate 				}
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate <CAT>[a-zA-Z0-9_\&][a-zA-Z0-9_\>\&\.]*	{
2947c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
295*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
2987c478bd9Sstevel@tonic-gate 				BEGIN 0;
2997c478bd9Sstevel@tonic-gate 			} else {
300*6e54a631Smuffin 				return (STR);
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate <CAT>\n		{
3057c478bd9Sstevel@tonic-gate 			lineno++;
3067c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
307*6e54a631Smuffin 				(void) fprintf(newfp, "\n");
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
3107c478bd9Sstevel@tonic-gate 				BEGIN 0;
3117c478bd9Sstevel@tonic-gate 			}
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate <CAT>.		{	/* not interested */
3157c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
316*6e54a631Smuffin 				(void) fprintf(newfp, "%c", yytext[0]);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			if (end_of_cat) {
3197c478bd9Sstevel@tonic-gate 				BEGIN 0;
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate -((([ \t]+)1)|1) {	/* -1 */
3247c478bd9Sstevel@tonic-gate 			if (end_of_cat == FALSE) {
3257c478bd9Sstevel@tonic-gate 				REJECT;
3267c478bd9Sstevel@tonic-gate 			} else if (IsActiveMode(ReplaceMode)) {
3277c478bd9Sstevel@tonic-gate 				if (IsActiveMode(PreProcessMode)) {
3287c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
3297c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
330*6e54a631Smuffin 						(void) fprintf(newfp, "%s",
331*6e54a631Smuffin 						    yytext);
3327c478bd9Sstevel@tonic-gate 					} else { /* could be -1. */
333*6e54a631Smuffin 						(void) fprintf(newfp, "%d", id);
3347c478bd9Sstevel@tonic-gate 					}
3357c478bd9Sstevel@tonic-gate 				} else {
336*6e54a631Smuffin 					(void) fprintf(newfp, "%s", yytext);
3377c478bd9Sstevel@tonic-gate 				}
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate [0-9]+		{
3427c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
3437c478bd9Sstevel@tonic-gate 				if (IsActiveMode(PreProcessMode) &&
3447c478bd9Sstevel@tonic-gate 					IsActiveMode(ReverseMode)) {
3457c478bd9Sstevel@tonic-gate 					int id = get_linemsgid(lineno);
3467c478bd9Sstevel@tonic-gate 					if (id == NOLINEMSG) {
347*6e54a631Smuffin 						(void) fprintf(newfp, "%s",
348*6e54a631Smuffin 						    yytext);
3497c478bd9Sstevel@tonic-gate 					} else if (id == NOMSGID) {
350*6e54a631Smuffin 						(void) fprintf(newfp, "%d", id);
3517c478bd9Sstevel@tonic-gate 					}
3527c478bd9Sstevel@tonic-gate 				} else {
353*6e54a631Smuffin 					(void) fprintf(newfp, "%s", yytext);
3547c478bd9Sstevel@tonic-gate 				}
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate ^#[ \t]*[0-9]+.*\n	{	/* pound for c-preprocessor */
3597c478bd9Sstevel@tonic-gate 			if (IsActiveMode(PreProcessMode)) {
3607c478bd9Sstevel@tonic-gate 				if (IsActiveMode(ReplaceMode)) {
361*6e54a631Smuffin 					(void) fprintf(newfp, "%s", yytext);
3627c478bd9Sstevel@tonic-gate 				} else {
3637c478bd9Sstevel@tonic-gate 					parse_cppline(yytext);
3647c478bd9Sstevel@tonic-gate 				}
3657c478bd9Sstevel@tonic-gate 			} else if (IsActiveMode(ReplaceMode)) {
366*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 			lineno++;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate "/*"		{	/* skip a comment block */
3727c478bd9Sstevel@tonic-gate 			char *comment = skip_comment();
3737c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
374*6e54a631Smuffin 				(void) fprintf(newfp, "%s", comment);
3757c478bd9Sstevel@tonic-gate 			} else {
3767c478bd9Sstevel@tonic-gate 				if (IsActiveMode(MsgCommentMode)) {
3777c478bd9Sstevel@tonic-gate 					add_comment(MsgCommentMode, comment);
3787c478bd9Sstevel@tonic-gate 				}
3797c478bd9Sstevel@tonic-gate 				if (IsActiveMode(SetCommentMode)) {
3807c478bd9Sstevel@tonic-gate 					add_comment(SetCommentMode, comment);
3817c478bd9Sstevel@tonic-gate 				}
3827c478bd9Sstevel@tonic-gate 			}
3837c478bd9Sstevel@tonic-gate 			free(comment);
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate "//".*\n	{	/* skip a c++ comment */
3877c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
388*6e54a631Smuffin 				(void) fprintf(newfp, "%s", yytext);
3897c478bd9Sstevel@tonic-gate 			} else {
3907c478bd9Sstevel@tonic-gate 				if (IsActiveMode(MsgCommentMode)) {
3917c478bd9Sstevel@tonic-gate 					add_comment(MsgCommentMode, yytext);
3927c478bd9Sstevel@tonic-gate 				}
3937c478bd9Sstevel@tonic-gate 				if (IsActiveMode(SetCommentMode)) {
3947c478bd9Sstevel@tonic-gate 					add_comment(SetCommentMode, yytext);
3957c478bd9Sstevel@tonic-gate 				}
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 			lineno++;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate \"		{	/* skip quoted string */
4017c478bd9Sstevel@tonic-gate 			char *qstr = skip_quoted('"');
4027c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
403*6e54a631Smuffin 				(void) fprintf(newfp, "\"%s\"", qstr);
4047c478bd9Sstevel@tonic-gate 			}
4057c478bd9Sstevel@tonic-gate 			free(qstr);
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate \'		{	/* skip single-quoted character */
4097c478bd9Sstevel@tonic-gate 			char *qchr = skip_quoted('\'');
4107c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
411*6e54a631Smuffin 				(void) fprintf(newfp, "\'%s\'", qchr);
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			free(qchr);
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate \n		{
4177c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
418*6e54a631Smuffin 				(void) fprintf(newfp, "\n");
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			lineno++;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate .		{
4247c478bd9Sstevel@tonic-gate 			if (IsActiveMode(ReplaceMode)) {
425*6e54a631Smuffin 				(void) fprintf(newfp, "%c", yytext[0]);
4267c478bd9Sstevel@tonic-gate 			}
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate %%
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static char *
4327c478bd9Sstevel@tonic-gate skip_quoted(int skip_ch)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	char *buf, *ptr;	/* saved buffer and its pointer */
4357c478bd9Sstevel@tonic-gate 	int bsize = BUFSIZ;	/* growing buffer size */
4367c478bd9Sstevel@tonic-gate 	int i = 0;		/* counter */
4377c478bd9Sstevel@tonic-gate 	int c, old = 0;		/* input character */
4387c478bd9Sstevel@tonic-gate 
439*6e54a631Smuffin 	if ((buf = ptr = malloc(bsize)) == NULL) {
4407c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
4417c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	for (; ; i++) {
4447c478bd9Sstevel@tonic-gate 		if (i == bsize) {
4457c478bd9Sstevel@tonic-gate 			bsize += BUFSIZ;
446*6e54a631Smuffin 			if ((buf = realloc(buf, bsize)) == NULL) {
4477c478bd9Sstevel@tonic-gate 				prg_err(gettext("fatal: out of memory"));
4487c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 			ptr = buf + i;
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		c = input();
4537c478bd9Sstevel@tonic-gate 		if (c == skip_ch && old != '\\') {
4547c478bd9Sstevel@tonic-gate 			break;
4557c478bd9Sstevel@tonic-gate 		} else if (c == '\n') {
4567c478bd9Sstevel@tonic-gate 			lineno++;
4577c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
4587c478bd9Sstevel@tonic-gate 			if (skip_ch == '"') {
4597c478bd9Sstevel@tonic-gate 				warning(gettext("warning: unmatched \""));
4607c478bd9Sstevel@tonic-gate 			} else if (skip_ch == '\'') {
4617c478bd9Sstevel@tonic-gate 				warning(gettext("warning: unmatched '"));
4627c478bd9Sstevel@tonic-gate 			} else {
4637c478bd9Sstevel@tonic-gate 				/* Should not happen */
464*6e54a631Smuffin 				warning(gettext(
465*6e54a631Smuffin 				    "warning: unmatched character"));
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		*ptr++ = c;
4707c478bd9Sstevel@tonic-gate 		if (old == '\\') {
4717c478bd9Sstevel@tonic-gate 			old = '\0';
4727c478bd9Sstevel@tonic-gate 		} else {
4737c478bd9Sstevel@tonic-gate 			old = c;
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	*ptr = '\0';
477*6e54a631Smuffin 	return (buf);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static char *
4817c478bd9Sstevel@tonic-gate skip_comment(void)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	char *buf, *ptr;	/* saved buffer and its pointer */
4847c478bd9Sstevel@tonic-gate 	int bsize = BUFSIZ;	/* growing buffer size */
4857c478bd9Sstevel@tonic-gate 	int i = 0;		/* counter */
4867c478bd9Sstevel@tonic-gate 	int c, old = 0;		/* input character */
4877c478bd9Sstevel@tonic-gate 
488*6e54a631Smuffin 	if ((buf = ptr = malloc(bsize)) == NULL) {
4897c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
4907c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	*ptr++ = '/';	i++;
4937c478bd9Sstevel@tonic-gate 	*ptr++ = '*';	i++;
4947c478bd9Sstevel@tonic-gate 	for (; ; i++) {
4957c478bd9Sstevel@tonic-gate 		if (i == bsize) {
4967c478bd9Sstevel@tonic-gate 			bsize += BUFSIZ;
497*6e54a631Smuffin 			if ((buf = realloc(buf, bsize)) == NULL) {
4987c478bd9Sstevel@tonic-gate 				prg_err(gettext("fatal: out of memory"));
4997c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			ptr = buf + i;
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		c = input();
5047c478bd9Sstevel@tonic-gate 		if (c == '/' && old == '*') {
5057c478bd9Sstevel@tonic-gate 			*ptr++ = c;
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 		} else if (c == '\n') {
5087c478bd9Sstevel@tonic-gate 			lineno++;
5097c478bd9Sstevel@tonic-gate 		} else if (c == 0) {
5107c478bd9Sstevel@tonic-gate 			warning(gettext("warning: unmatched /*"));
5117c478bd9Sstevel@tonic-gate 			break;
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		*ptr++ = old = c;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	*ptr = '\0';
516*6e54a631Smuffin 	return (buf);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * parse_cppline() parses the line control information that a C
5217c478bd9Sstevel@tonic-gate  * preprocessor generates to indicate the location in the original
5227c478bd9Sstevel@tonic-gate  * file.  See the cpp man in the details.
5237c478bd9Sstevel@tonic-gate  */
5247c478bd9Sstevel@tonic-gate static void
5257c478bd9Sstevel@tonic-gate parse_cppline(char *str)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	int n, line, len;
5287c478bd9Sstevel@tonic-gate 	char ch;
529*6e54a631Smuffin 	char file[BUFSIZ];
530*6e54a631Smuffin 	char *altfile = NULL;
531*6e54a631Smuffin 	char *pfile;
5327c478bd9Sstevel@tonic-gate 
533*6e54a631Smuffin 	len = strlen(str);
534*6e54a631Smuffin 	if (len >= sizeof (file)) {
535*6e54a631Smuffin 		if ((altfile = malloc(len + 1)) == NULL) {
536*6e54a631Smuffin 			prg_err(gettext("fatal: out of memory"));
537*6e54a631Smuffin 			exit(EXIT_FAILURE);
538*6e54a631Smuffin 		}
539*6e54a631Smuffin 		pfile = altfile;
540*6e54a631Smuffin 	} else {
541*6e54a631Smuffin 		pfile = file;
542*6e54a631Smuffin 	}
543*6e54a631Smuffin 	/* LINTED: E_SEC_SCANF_UNBOUNDED_COPY */
544*6e54a631Smuffin 	n = sscanf(str, "%c%d%s", &ch, &line, pfile);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/* 'file' is a quoted string but 'srcfile' is not. */
547*6e54a631Smuffin 	len = strlen(pfile) - 2;
548*6e54a631Smuffin 
5497c478bd9Sstevel@tonic-gate 	pfile++;
5507c478bd9Sstevel@tonic-gate 	if (n == 3 && (strncmp(pfile, srcfile, len) == 0)) {
5517c478bd9Sstevel@tonic-gate 		pound_is_mine = TRUE;
5527c478bd9Sstevel@tonic-gate 		lineno = line - 1;
5537c478bd9Sstevel@tonic-gate 	} else if (n == 2 && (pound_is_mine == TRUE)) {
5547c478bd9Sstevel@tonic-gate 		lineno = line - 1;
5557c478bd9Sstevel@tonic-gate 	} else {
5567c478bd9Sstevel@tonic-gate 		pound_is_mine = FALSE;
5577c478bd9Sstevel@tonic-gate 	}
558*6e54a631Smuffin 	if (altfile)
559*6e54a631Smuffin 		free(altfile);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate typedef struct {
5637c478bd9Sstevel@tonic-gate 	int line;
5647c478bd9Sstevel@tonic-gate 	int msgid;
5657c478bd9Sstevel@tonic-gate } LineMsgID;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate static LineMsgID line_msgid[NL_MSGMAX];
5687c478bd9Sstevel@tonic-gate static int line_msgcnt;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate init_lex(void)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	lineno = 1;
5747c478bd9Sstevel@tonic-gate 	end_of_cat = TRUE;
5757c478bd9Sstevel@tonic-gate 	pound_is_mine = FALSE;
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate void
5797c478bd9Sstevel@tonic-gate init_linemsgid(void)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	line_msgcnt = 0;
582*6e54a631Smuffin 	(void) memset(line_msgid, 0, sizeof (LineMsgID) * NL_MSGMAX);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate void
5867c478bd9Sstevel@tonic-gate set_linemsgid(int line, int msgid)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	if (line_msgcnt >= NL_MSGMAX) {
5897c478bd9Sstevel@tonic-gate 		return; /* oops */
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	line_msgid[line_msgcnt].line = line;
5927c478bd9Sstevel@tonic-gate 	line_msgid[line_msgcnt].msgid = msgid;
5937c478bd9Sstevel@tonic-gate 	line_msgcnt++;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate int
5977c478bd9Sstevel@tonic-gate get_linemsgid(int line)
5987c478bd9Sstevel@tonic-gate {
599*6e54a631Smuffin 	int i, left, right;
6007c478bd9Sstevel@tonic-gate 	left = 0;
6017c478bd9Sstevel@tonic-gate 	right = line_msgcnt - 1;
6027c478bd9Sstevel@tonic-gate 	while (left <= right) {
6037c478bd9Sstevel@tonic-gate 		i = (left + right) >> 1;
6047c478bd9Sstevel@tonic-gate 		if (line < line_msgid[i].line) {
6057c478bd9Sstevel@tonic-gate 			right = i - 1;
6067c478bd9Sstevel@tonic-gate 		} else if (line > line_msgid[i].line) {
6077c478bd9Sstevel@tonic-gate 			left = i + 1;
6087c478bd9Sstevel@tonic-gate 		} else {
609*6e54a631Smuffin 			return (line_msgid[i].msgid);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 	}
612*6e54a631Smuffin 	return (NOLINEMSG);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate void
6167c478bd9Sstevel@tonic-gate yyerror(char *s)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	if ((IsActiveMode(PreProcessMode) && pound_is_mine == FALSE) ||
6197c478bd9Sstevel@tonic-gate 	    IsActiveMode(ReplaceMode)) {
6207c478bd9Sstevel@tonic-gate 		return;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	src_err(srcfile, lineno, gettext("%s before or at: %s"), s, yytext);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate void
6267c478bd9Sstevel@tonic-gate warning(char *s)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	if ((IsActiveMode(PreProcessMode) && pound_is_mine == FALSE) ||
6297c478bd9Sstevel@tonic-gate 	    IsActiveMode(ReplaceMode)) {
6307c478bd9Sstevel@tonic-gate 		return;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	src_err(srcfile, lineno, "%s", s);
6337c478bd9Sstevel@tonic-gate }
634