xref: /illumos-gate/usr/src/cmd/genmsg/genmsg.y (revision 2a8bcb4e)
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
66e54a631Smuffin  * Common Development and Distribution License (the "License").
76e54a631Smuffin  * 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
217c478bd9Sstevel@tonic-gate  */
226e54a631Smuffin /*
236e54a631Smuffin  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246e54a631Smuffin  * Use is subject to license terms.
256e54a631Smuffin  */
266e54a631Smuffin 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <libintl.h>
29*2a8bcb4eSToomas Soome #include <limits.h>
307c478bd9Sstevel@tonic-gate #include "genmsg.h"
317c478bd9Sstevel@tonic-gate extern int is_cat_found;		/* from main.c */
327c478bd9Sstevel@tonic-gate extern int lineno;			/* genmsg.l */
337c478bd9Sstevel@tonic-gate extern int msg_line;			/* genmsg.l */
347c478bd9Sstevel@tonic-gate extern int end_of_cat;			/* from genmsg.l */
357c478bd9Sstevel@tonic-gate extern void set_linemsgid(int, int);	/* from genmsg.l */
366e54a631Smuffin extern void add_msg(int, int, char *, char *, int, int); /* from util.c */
376e54a631Smuffin extern void set_msgid(int, int);
386e54a631Smuffin extern int get_msgid(char *, int, int, char *);
396e54a631Smuffin extern void warning(char *);
406e54a631Smuffin extern void yyerror(char *);
416e54a631Smuffin 
426e54a631Smuffin static void do_catgets(int, int, char *);
436e54a631Smuffin static char *add_qstring(char *, char *);
447c478bd9Sstevel@tonic-gate %}
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate %union {
477c478bd9Sstevel@tonic-gate 	char *str;
487c478bd9Sstevel@tonic-gate 	int id;
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate %token CATGETS
527c478bd9Sstevel@tonic-gate %token CONST
537c478bd9Sstevel@tonic-gate %token CATD
547c478bd9Sstevel@tonic-gate %token INT, CHAR, INC
557c478bd9Sstevel@tonic-gate %token <str> STR
567c478bd9Sstevel@tonic-gate %token <id> SETID, MSGID, DIGIT
577c478bd9Sstevel@tonic-gate %token <str> QSTR
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate %type <id> cast_setid, setid, cast_msgid, msgid, cast_digit, digit
606e54a631Smuffin %type <str> catd, arg_list, arg_def, arg_func, arg_exp, str,
617c478bd9Sstevel@tonic-gate 	cast_qstr, paren_qstr, qstr_list
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate %left '-' '+'
647c478bd9Sstevel@tonic-gate %left '*' '/'
657c478bd9Sstevel@tonic-gate %nonassoc UMINUS
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate %%
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate genmsg_list:	/* empty */
707c478bd9Sstevel@tonic-gate 		{
717c478bd9Sstevel@tonic-gate 			if (!IsActiveMode(ReplaceMode)) {
727c478bd9Sstevel@tonic-gate 				src_err(srcfile, (lineno - 1),
736e54a631Smuffin 				    gettext("catgets not found"));
747c478bd9Sstevel@tonic-gate 			}
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	|	genmsg			{ is_cat_found = TRUE; }
777c478bd9Sstevel@tonic-gate 	;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate genmsg:		catgets			{ end_of_cat = TRUE; }
807c478bd9Sstevel@tonic-gate 	|	genmsg catgets		{ end_of_cat = TRUE; }
817c478bd9Sstevel@tonic-gate 	;
827c478bd9Sstevel@tonic-gate 
836e54a631Smuffin catgets:	CATGETS '(' catd ',' cast_setid ',' cast_msgid ',' cast_qstr ')'
846e54a631Smuffin 		{
857c478bd9Sstevel@tonic-gate 			do_catgets($5, $7, $9); free($9);
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	|	error
887c478bd9Sstevel@tonic-gate 	;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate catd:		'(' CATD ')' arg_list		{ $$ = $4; }
917c478bd9Sstevel@tonic-gate 	|	'(' CONST CATD ')' arg_list	{ $$ = $5; }
927c478bd9Sstevel@tonic-gate 	|	arg_list
937c478bd9Sstevel@tonic-gate 	;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate arg_list:	arg_def
967c478bd9Sstevel@tonic-gate 	|	arg_list '-' '>' arg_def
976e54a631Smuffin 	|	'(' arg_list '-' '>' arg_def ')' { $$ = $2; }
987c478bd9Sstevel@tonic-gate 	;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate arg_def:	arg_func
1017c478bd9Sstevel@tonic-gate 	|	arg_exp
1027c478bd9Sstevel@tonic-gate 	|	str
1037c478bd9Sstevel@tonic-gate 	;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate arg_func:	'(' arg_func ')'	{ $$ = $2; }
1067c478bd9Sstevel@tonic-gate 	|	str '(' ')'
1077c478bd9Sstevel@tonic-gate 	|	str '(' str ')'
1087c478bd9Sstevel@tonic-gate 	|	str '(' cast_digit ')'
1097c478bd9Sstevel@tonic-gate 	|	str '(' cast_qstr ')'	{ free($3); }
1107c478bd9Sstevel@tonic-gate 	;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate arg_exp:	'(' arg_exp ')'		{ $$ = $2; }
1137c478bd9Sstevel@tonic-gate 	|	str INC
1147c478bd9Sstevel@tonic-gate 	|	INC str			{ $$ = $2; }
1157c478bd9Sstevel@tonic-gate 	;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate str:		'(' str ')'		{ $$ = $2; }
1187c478bd9Sstevel@tonic-gate 	|	'*' str			{ $$ = $2; }
1197c478bd9Sstevel@tonic-gate 	|	STR
1207c478bd9Sstevel@tonic-gate 	;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate cast_setid:	'(' INT ')' setid	{ $$ = $4; }
1237c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' setid	{ $$ = $5; }
1247c478bd9Sstevel@tonic-gate 	|	setid
1257c478bd9Sstevel@tonic-gate 	;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate setid:		setid '+' setid		{ $$ = $1 + $3; }
1287c478bd9Sstevel@tonic-gate 	|	setid '-' setid		{ $$ = $1 - $3; }
1297c478bd9Sstevel@tonic-gate 	|	setid '*' setid		{ $$ = $1 * $3; }
1307c478bd9Sstevel@tonic-gate 	|	setid '/' setid
1316e54a631Smuffin 		{
1327c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1337c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero divide"));
1347c478bd9Sstevel@tonic-gate 			} else {
1356e54a631Smuffin 				$$ = $1 / $3;
1367c478bd9Sstevel@tonic-gate 			}
1377c478bd9Sstevel@tonic-gate 		}
1386e54a631Smuffin 	|	'-' setid %prec UMINUS	{ $$ = -$2; }
1397c478bd9Sstevel@tonic-gate 	|	'(' setid ')'		{ $$ = $2; }
1407c478bd9Sstevel@tonic-gate 	|	SETID
1417c478bd9Sstevel@tonic-gate 	;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate cast_msgid:	'(' INT ')' msgid	{ $$ = $4; }
1447c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' msgid	{ $$ = $5; }
1457c478bd9Sstevel@tonic-gate 	|	msgid
1467c478bd9Sstevel@tonic-gate 	;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate msgid:		msgid '+' msgid		{ $$ = $1 + $3; }
1497c478bd9Sstevel@tonic-gate 	|	msgid '-' msgid		{ $$ = $1 - $3; }
1507c478bd9Sstevel@tonic-gate 	|	msgid '*' msgid		{ $$ = $1 * $3; }
1516e54a631Smuffin 	|	msgid '/' msgid
1526e54a631Smuffin 		{
1537c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1547c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero devide"));
1557c478bd9Sstevel@tonic-gate 			} else {
1566e54a631Smuffin 				$$ = $1 / $3;
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 		}
1596e54a631Smuffin 	|	'-' msgid %prec UMINUS	{ $$ = -$2; }
1607c478bd9Sstevel@tonic-gate 	|	'(' msgid ')'		{ $$ = $2; }
1617c478bd9Sstevel@tonic-gate 	|	MSGID
1627c478bd9Sstevel@tonic-gate 	;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate cast_digit:	'(' INT ')' digit	{ $$ = $4; }
1657c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' digit	{ $$ = $5; }
1667c478bd9Sstevel@tonic-gate 	|	digit
1677c478bd9Sstevel@tonic-gate 	;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate digit:		digit '+' digit		{ $$ = $1 + $3; }
1707c478bd9Sstevel@tonic-gate 	|	digit '-' digit		{ $$ = $1 - $3; }
1717c478bd9Sstevel@tonic-gate 	|	digit '*' digit		{ $$ = $1 * $3; }
1727c478bd9Sstevel@tonic-gate 	|	digit '/' digit
1736e54a631Smuffin 		{
1747c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1757c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero divide"));
1767c478bd9Sstevel@tonic-gate 			} else {
1776e54a631Smuffin 				$$ = $1 / $3;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 		}
1806e54a631Smuffin 	|	'-' digit %prec UMINUS	{ $$ = -$2; }
1817c478bd9Sstevel@tonic-gate 	|	'(' digit ')'		{ $$ = $2; }
1827c478bd9Sstevel@tonic-gate 	|	DIGIT
1837c478bd9Sstevel@tonic-gate 	;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate cast_qstr:	'(' CHAR '*' ')' paren_qstr		{ $$ = $5; }
1867c478bd9Sstevel@tonic-gate 	|	'(' CONST CHAR '*' ')' paren_qstr	{ $$ = $6; }
1877c478bd9Sstevel@tonic-gate 	|	paren_qstr
1887c478bd9Sstevel@tonic-gate 	;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate paren_qstr:	'(' qstr_list ')'	{ $$ = $2; }
1917c478bd9Sstevel@tonic-gate 	|	qstr_list
1927c478bd9Sstevel@tonic-gate 	;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate qstr_list:	QSTR
1957c478bd9Sstevel@tonic-gate 	|	qstr_list QSTR		{ $$ = add_qstring($1, $2); }
1967c478bd9Sstevel@tonic-gate 	;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate %%
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void
2017c478bd9Sstevel@tonic-gate do_catgets(int setid, int msgid, char *str)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int id = msgid;
2047c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ReplaceMode)) {
2057c478bd9Sstevel@tonic-gate 		return;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	if (setid == 0 || setid > NL_SETMAX) {
2086e54a631Smuffin 		src_err(srcfile, lineno,
2096e54a631Smuffin 		    gettext("improper set number: %d"), setid);
2107c478bd9Sstevel@tonic-gate 		return;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ProjectMode)) {
2137c478bd9Sstevel@tonic-gate 		set_msgid(setid, id);
2147c478bd9Sstevel@tonic-gate 		add_msg(setid, id, str, srcfile, lineno, TRUE);
2157c478bd9Sstevel@tonic-gate 	} else if (IsActiveMode(ReverseMode)) {
2167c478bd9Sstevel@tonic-gate 		set_linemsgid(msg_line, NOMSGID);
2177c478bd9Sstevel@tonic-gate 	} else if (IsActiveMode(AutoNumMode)) {
2187c478bd9Sstevel@tonic-gate 		if (id == NOMSGID) {
2197c478bd9Sstevel@tonic-gate 			id = get_msgid(srcfile, msg_line, setid, str);
2207c478bd9Sstevel@tonic-gate 			set_linemsgid(msg_line, id);
2216e54a631Smuffin 		}
2227c478bd9Sstevel@tonic-gate 		if (id != NOMSGID) {
2237c478bd9Sstevel@tonic-gate 			set_msgid(setid, id);
2247c478bd9Sstevel@tonic-gate 			add_msg(setid, id, str, srcfile, lineno, FALSE);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	} else if (id == NOMSGID) {
2277c478bd9Sstevel@tonic-gate 		warning(gettext("improper message number: -1"));
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 		add_msg(setid, id, str, srcfile, lineno, FALSE);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static char *
add_qstring(char * str,char * add)2347c478bd9Sstevel@tonic-gate add_qstring(char *str, char *add)
2357c478bd9Sstevel@tonic-gate {
2366e54a631Smuffin 	int len = strlen(str) + strlen(add) + 3;
2377c478bd9Sstevel@tonic-gate 	/* 3 includes '\', '\n' and '\0' */
2386e54a631Smuffin 	char *tmp = malloc(len);
2396e54a631Smuffin 	if (tmp == NULL) {
2407c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
2417c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2427c478bd9Sstevel@tonic-gate 	}
2436e54a631Smuffin 	(void) snprintf(tmp, len, "%s\\\n%s", str, add);
2447c478bd9Sstevel@tonic-gate 	free(str);
2457c478bd9Sstevel@tonic-gate 	free(add);
2466e54a631Smuffin 	return (tmp);
2477c478bd9Sstevel@tonic-gate }
248