xref: /titanic_50/usr/src/cmd/genmsg/genmsg.y (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 <libintl.h>
31*6e54a631Smuffin #include <limits.h>
327c478bd9Sstevel@tonic-gate #include "genmsg.h"
337c478bd9Sstevel@tonic-gate extern int is_cat_found;		/* from main.c */
347c478bd9Sstevel@tonic-gate extern int lineno;			/* genmsg.l */
357c478bd9Sstevel@tonic-gate extern int msg_line;			/* genmsg.l */
367c478bd9Sstevel@tonic-gate extern int end_of_cat;			/* from genmsg.l */
377c478bd9Sstevel@tonic-gate extern void set_linemsgid(int, int);	/* from genmsg.l */
387c478bd9Sstevel@tonic-gate extern void add_msg(int, int, char *, char *, int, int); /* from util.c */
39*6e54a631Smuffin extern void set_msgid(int, int);
40*6e54a631Smuffin extern int get_msgid(char *, int, int, char *);
41*6e54a631Smuffin extern void warning(char *);
42*6e54a631Smuffin extern void yyerror(char *);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static void do_catgets(int, int, char *);
457c478bd9Sstevel@tonic-gate static char *add_qstring(char *, char *);
467c478bd9Sstevel@tonic-gate %}
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate %union {
497c478bd9Sstevel@tonic-gate 	char *str;
507c478bd9Sstevel@tonic-gate 	int id;
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate %token CATGETS
547c478bd9Sstevel@tonic-gate %token CONST
557c478bd9Sstevel@tonic-gate %token CATD
567c478bd9Sstevel@tonic-gate %token INT, CHAR, INC
577c478bd9Sstevel@tonic-gate %token <str> STR
587c478bd9Sstevel@tonic-gate %token <id> SETID, MSGID, DIGIT
597c478bd9Sstevel@tonic-gate %token <str> QSTR
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate %type <id> cast_setid, setid, cast_msgid, msgid, cast_digit, digit
627c478bd9Sstevel@tonic-gate %type <str> catd, arg_list, arg_def, arg_func, arg_exp, str,
637c478bd9Sstevel@tonic-gate 	cast_qstr, paren_qstr, qstr_list
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate %left '-' '+'
667c478bd9Sstevel@tonic-gate %left '*' '/'
677c478bd9Sstevel@tonic-gate %nonassoc UMINUS
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate %%
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate genmsg_list:	/* empty */
727c478bd9Sstevel@tonic-gate 		{
737c478bd9Sstevel@tonic-gate 			if (!IsActiveMode(ReplaceMode)) {
747c478bd9Sstevel@tonic-gate 				src_err(srcfile, (lineno - 1),
757c478bd9Sstevel@tonic-gate 				    gettext("catgets not found"));
767c478bd9Sstevel@tonic-gate 			}
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 	|	genmsg			{ is_cat_found = TRUE; }
797c478bd9Sstevel@tonic-gate 	;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate genmsg:		catgets			{ end_of_cat = TRUE; }
827c478bd9Sstevel@tonic-gate 	|	genmsg catgets		{ end_of_cat = TRUE; }
837c478bd9Sstevel@tonic-gate 	;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate catgets:	CATGETS '(' catd ',' cast_setid ',' cast_msgid ',' cast_qstr ')'
867c478bd9Sstevel@tonic-gate 		{
877c478bd9Sstevel@tonic-gate 			do_catgets($5, $7, $9); free($9);
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	|	error
907c478bd9Sstevel@tonic-gate 	;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate catd:		'(' CATD ')' arg_list		{ $$ = $4; }
937c478bd9Sstevel@tonic-gate 	|	'(' CONST CATD ')' arg_list	{ $$ = $5; }
947c478bd9Sstevel@tonic-gate 	|	arg_list
957c478bd9Sstevel@tonic-gate 	;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate arg_list:	arg_def
987c478bd9Sstevel@tonic-gate 	|	arg_list '-' '>' arg_def
997c478bd9Sstevel@tonic-gate 	|	'(' arg_list '-' '>' arg_def ')' { $$ = $2; }
1007c478bd9Sstevel@tonic-gate 	;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate arg_def:	arg_func
1037c478bd9Sstevel@tonic-gate 	|	arg_exp
1047c478bd9Sstevel@tonic-gate 	|	str
1057c478bd9Sstevel@tonic-gate 	;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate arg_func:	'(' arg_func ')'	{ $$ = $2; }
1087c478bd9Sstevel@tonic-gate 	|	str '(' ')'
1097c478bd9Sstevel@tonic-gate 	|	str '(' str ')'
1107c478bd9Sstevel@tonic-gate 	|	str '(' cast_digit ')'
1117c478bd9Sstevel@tonic-gate 	|	str '(' cast_qstr ')'	{ free($3); }
1127c478bd9Sstevel@tonic-gate 	;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate arg_exp:	'(' arg_exp ')'		{ $$ = $2; }
1157c478bd9Sstevel@tonic-gate 	|	str INC
1167c478bd9Sstevel@tonic-gate 	|	INC str			{ $$ = $2; }
1177c478bd9Sstevel@tonic-gate 	;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate str:		'(' str ')'		{ $$ = $2; }
1207c478bd9Sstevel@tonic-gate 	|	'*' str			{ $$ = $2; }
1217c478bd9Sstevel@tonic-gate 	|	STR
1227c478bd9Sstevel@tonic-gate 	;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate cast_setid:	'(' INT ')' setid	{ $$ = $4; }
1257c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' setid	{ $$ = $5; }
1267c478bd9Sstevel@tonic-gate 	|	setid
1277c478bd9Sstevel@tonic-gate 	;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate setid:		setid '+' setid		{ $$ = $1 + $3; }
1307c478bd9Sstevel@tonic-gate 	|	setid '-' setid		{ $$ = $1 - $3; }
1317c478bd9Sstevel@tonic-gate 	|	setid '*' setid		{ $$ = $1 * $3; }
1327c478bd9Sstevel@tonic-gate 	|	setid '/' setid
1337c478bd9Sstevel@tonic-gate 		{
1347c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1357c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero divide"));
1367c478bd9Sstevel@tonic-gate 			} else {
1377c478bd9Sstevel@tonic-gate 				$$ = $1 / $3;
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	|	'-' setid %prec UMINUS	{ $$ = -$2; }
1417c478bd9Sstevel@tonic-gate 	|	'(' setid ')'		{ $$ = $2; }
1427c478bd9Sstevel@tonic-gate 	|	SETID
1437c478bd9Sstevel@tonic-gate 	;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate cast_msgid:	'(' INT ')' msgid	{ $$ = $4; }
1467c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' msgid	{ $$ = $5; }
1477c478bd9Sstevel@tonic-gate 	|	msgid
1487c478bd9Sstevel@tonic-gate 	;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate msgid:		msgid '+' msgid		{ $$ = $1 + $3; }
1517c478bd9Sstevel@tonic-gate 	|	msgid '-' msgid		{ $$ = $1 - $3; }
1527c478bd9Sstevel@tonic-gate 	|	msgid '*' msgid		{ $$ = $1 * $3; }
1537c478bd9Sstevel@tonic-gate 	|	msgid '/' msgid
1547c478bd9Sstevel@tonic-gate 		{
1557c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1567c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero devide"));
1577c478bd9Sstevel@tonic-gate 			} else {
1587c478bd9Sstevel@tonic-gate 				$$ = $1 / $3;
1597c478bd9Sstevel@tonic-gate 			}
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 	|	'-' msgid %prec UMINUS	{ $$ = -$2; }
1627c478bd9Sstevel@tonic-gate 	|	'(' msgid ')'		{ $$ = $2; }
1637c478bd9Sstevel@tonic-gate 	|	MSGID
1647c478bd9Sstevel@tonic-gate 	;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate cast_digit:	'(' INT ')' digit	{ $$ = $4; }
1677c478bd9Sstevel@tonic-gate 	|	'(' CONST INT ')' digit	{ $$ = $5; }
1687c478bd9Sstevel@tonic-gate 	|	digit
1697c478bd9Sstevel@tonic-gate 	;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate digit:		digit '+' digit		{ $$ = $1 + $3; }
1727c478bd9Sstevel@tonic-gate 	|	digit '-' digit		{ $$ = $1 - $3; }
1737c478bd9Sstevel@tonic-gate 	|	digit '*' digit		{ $$ = $1 * $3; }
1747c478bd9Sstevel@tonic-gate 	|	digit '/' digit
1757c478bd9Sstevel@tonic-gate 		{
1767c478bd9Sstevel@tonic-gate 			if ($3 == 0) {
1777c478bd9Sstevel@tonic-gate 				yyerror(gettext("zero divide"));
1787c478bd9Sstevel@tonic-gate 			} else {
1797c478bd9Sstevel@tonic-gate 				$$ = $1 / $3;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	|	'-' digit %prec UMINUS	{ $$ = -$2; }
1837c478bd9Sstevel@tonic-gate 	|	'(' digit ')'		{ $$ = $2; }
1847c478bd9Sstevel@tonic-gate 	|	DIGIT
1857c478bd9Sstevel@tonic-gate 	;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate cast_qstr:	'(' CHAR '*' ')' paren_qstr		{ $$ = $5; }
1887c478bd9Sstevel@tonic-gate 	|	'(' CONST CHAR '*' ')' paren_qstr	{ $$ = $6; }
1897c478bd9Sstevel@tonic-gate 	|	paren_qstr
1907c478bd9Sstevel@tonic-gate 	;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate paren_qstr:	'(' qstr_list ')'	{ $$ = $2; }
1937c478bd9Sstevel@tonic-gate 	|	qstr_list
1947c478bd9Sstevel@tonic-gate 	;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate qstr_list:	QSTR
1977c478bd9Sstevel@tonic-gate 	|	qstr_list QSTR		{ $$ = add_qstring($1, $2); }
1987c478bd9Sstevel@tonic-gate 	;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate %%
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static void
2037c478bd9Sstevel@tonic-gate do_catgets(int setid, int msgid, char *str)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	int id = msgid;
2067c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ReplaceMode)) {
2077c478bd9Sstevel@tonic-gate 		return;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	if (setid == 0 || setid > NL_SETMAX) {
2107c478bd9Sstevel@tonic-gate 		src_err(srcfile, lineno,
2117c478bd9Sstevel@tonic-gate 		    gettext("improper set number: %d"), setid);
2127c478bd9Sstevel@tonic-gate 		return;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	if (IsActiveMode(ProjectMode)) {
2157c478bd9Sstevel@tonic-gate 		set_msgid(setid, id);
2167c478bd9Sstevel@tonic-gate 		add_msg(setid, id, str, srcfile, lineno, TRUE);
2177c478bd9Sstevel@tonic-gate 	} else if (IsActiveMode(ReverseMode)) {
2187c478bd9Sstevel@tonic-gate 		set_linemsgid(msg_line, NOMSGID);
2197c478bd9Sstevel@tonic-gate 	} else if (IsActiveMode(AutoNumMode)) {
2207c478bd9Sstevel@tonic-gate 		if (id == NOMSGID) {
2217c478bd9Sstevel@tonic-gate 			id = get_msgid(srcfile, msg_line, setid, str);
2227c478bd9Sstevel@tonic-gate 			set_linemsgid(msg_line, id);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		if (id != NOMSGID) {
2257c478bd9Sstevel@tonic-gate 			set_msgid(setid, id);
2267c478bd9Sstevel@tonic-gate 			add_msg(setid, id, str, srcfile, lineno, FALSE);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	} else if (id == NOMSGID) {
2297c478bd9Sstevel@tonic-gate 		warning(gettext("improper message number: -1"));
2307c478bd9Sstevel@tonic-gate 	} else {
2317c478bd9Sstevel@tonic-gate 		add_msg(setid, id, str, srcfile, lineno, FALSE);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static char *
add_qstring(char * str,char * add)2367c478bd9Sstevel@tonic-gate add_qstring(char *str, char *add)
2377c478bd9Sstevel@tonic-gate {
238*6e54a631Smuffin 	int len = strlen(str) + strlen(add) + 3;
2397c478bd9Sstevel@tonic-gate 	/* 3 includes '\', '\n' and '\0' */
240*6e54a631Smuffin 	char *tmp = malloc(len);
241*6e54a631Smuffin 	if (tmp == NULL) {
2427c478bd9Sstevel@tonic-gate 		prg_err(gettext("fatal: out of memory"));
2437c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2447c478bd9Sstevel@tonic-gate 	}
245*6e54a631Smuffin 	(void) snprintf(tmp, len, "%s\\\n%s", str, add);
2467c478bd9Sstevel@tonic-gate 	free(str);
2477c478bd9Sstevel@tonic-gate 	free(add);
248*6e54a631Smuffin 	return (tmp);
2497c478bd9Sstevel@tonic-gate }
250