xref: /illumos-gate/usr/src/cmd/msgfmt/xgettext.lx.l (revision 2a8bcb4e)
1 %{
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://www.opensolaris.org/os/licensing.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Copyright (c) 1991, 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 #define	TRUE	1
31 #define	FALSE	0
32 
33 extern int	stdin_only;
34 extern char	curr_file[];	/* Name of file currently being read. */
35 extern int	curr_linenum;	/* line number in the current input file */
36 extern int	warn_linenum;	/* line number of current warning message */
37 extern int	optind;
38 extern int	gargc;
39 extern char	**gargv;
40 
41 extern void	handle_macro_line(void);
42 extern void	handle_cplus_comment_line(void);
43 extern void	handle_open_comment(void);
44 extern void	handle_close_comment(void);
45 extern void	handle_gettext(void);
46 extern void	handle_dgettext(void);
47 extern void	handle_dcgettext(void);
48 extern void	handle_textdomain(void);
49 extern void	handle_character(void);
50 extern void	handle_open_paren(void);
51 extern void	handle_close_paren(void);
52 extern void	handle_esc_newline(void);
53 extern void	handle_comma(void);
54 extern void	handle_newline(void);
55 extern void	handle_quote(void);
56 extern void	handle_spaces(void);
57 extern void	handle_spaces(void);
58 extern void	handle_character(void);
59 
60 /*
61  * The following lex rule basically wants to recognize tokens
62  * that can change the current state of scanning source code.
63  * Evertime such tokens are recognized, the specific handler will be
64  * executed. All other tokens are treated as regular characters and
65  * they are all handled the same way.
66  * The tricky part was not to miss any specification in ANSI-C code
67  * that looks like a meaningful token but not a meaningful one and
68  * should be treated as regular characters.
69  * For example,
70  *	c= '"';d='"'; printf("\"" "\(\)\\\"");
71  *	c = ABgettext("Not a gettext");
72  *	c = gettextXY("Not a gettext");
73  *	c = ABgettextXY("Not a gettext");
74  */
75 %}
76 
77 IDCHARS		[a-zA-Z0-9_]
78 
79 %%
80 ^#(.*\\\n)**.*\n	{ handle_macro_line(); }
81 
82 \/\/		{ handle_cplus_comment_line(); }
83 
84 \/\* 		{ handle_open_comment(); }
85 
86 \*\/ 		{ handle_close_comment(); }
87 
88 dcgettext	{ handle_dcgettext(); }
89 
90 dgettext	{ handle_dgettext(); }
91 
92 gettext		{ handle_gettext(); }
93 
94 textdomain	{ handle_textdomain(); }
95 
96 {IDCHARS}+	|
97 \'\"\'		|
98 \'\\\"\'	|
99 \\\\		|
100 \\\"		|
101 \\\(		|
102 \\\)		{ handle_character(); }
103 
104 \(		{ handle_open_paren(); }
105 
106 \)		{ handle_close_paren(); }
107 
108 \\\n		{ handle_esc_newline(); }
109 
110 \,		{ handle_comma(); }
111 
112 \n		{ handle_newline(); }
113 
114 \"		{ handle_quote(); }
115 
116 " "		{ handle_spaces(); }
117 
118 "\t"		{ handle_spaces(); }
119 
120 .		{ handle_character(); }
121 
122 %%
123 
124 /*
125  * Since multiple files can be processed, yywrap() should keep feeding
126  * all input files specified.
127  */
128 int
129 yywrap(void)
130 {
131 	FILE	*fp;
132 
133 	if ((optind >= gargc) || (stdin_only == TRUE)) {
134 		return (1);
135 	} else {
136 		/*
137 		 * gargv still contains not-processed input files.
138 		 */
139 		(void) freopen(gargv[optind], "r", stdin);
140 		if ((fp = freopen(gargv[optind], "r", stdin)) == NULL) {
141 			(void) fprintf(stderr, "ERROR, can't open input file: %s\n",
142 					gargv[optind]);
143 		}
144 #ifdef DEBUG
145 		(void) printf("In yywrap(), opening file  %d, <%s>\n",
146 				optind, gargv[optind]);
147 #endif
148 		/*
149 		 * Reset global file name and line number for the new file.
150 		 */
151 		(void) strcpy(curr_file, gargv[optind]);
152 		curr_linenum = 0;
153 		warn_linenum = 0;
154 
155 		optind++;
156 
157 		return (0);
158 	}
159 
160 } /* yywrap */
161