xref: /onnv-gate/usr/src/cmd/sgs/tools/common/sgsmsg.c (revision 12736:2d732c7bdbde)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55549Srie  * Common Development and Distribution License (the "License").
65549Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12736SAli.Bahrami@Oracle.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * sgsmsg generates several message files from an input template file.  Messages
250Sstevel@tonic-gate  * are constructed for use with gettext(3i) - the default - or catgets(3c).  The
260Sstevel@tonic-gate  * files generate are:
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * msg.h	a header file containing definitions for each message.  The -h
290Sstevel@tonic-gate  *		option triggers the creation of these definitions and specifies
300Sstevel@tonic-gate  *		the name to use.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * msg.c	a data array of message strings.  The msg.h definitions are
330Sstevel@tonic-gate  *		offsets into this array.  The -d option triggers the creation of
340Sstevel@tonic-gate  *		these definitions and specifies the name to use.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * messages	a message file suitable for catgets(3c) or gettext(3i) use.  The
370Sstevel@tonic-gate  *		-m option triggers this output and specifies the filename to be
380Sstevel@tonic-gate  *		used.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * The template file is processed based on the first character of each line:
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * # or $	entries are copied (as is) to the message file (messages).
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * @ token(s)	entries are translated.  Two translations are possible dependent
450Sstevel@tonic-gate  *		on whether one or more tokens are supplied:
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *		A single token is interpreted as one of two reserved message
480Sstevel@tonic-gate  *		output indicators, or a message identifier.  The reserved output
490Sstevel@tonic-gate  *		indicator _START_ enables output to the message file - Note that
500Sstevel@tonic-gate  *		the occurance of any other @ token will also enable message
510Sstevel@tonic-gate  *		output.  The reserved output indicator _END_ disables output to
520Sstevel@tonic-gate  *		the message file.  The use of these two indicators provides for
530Sstevel@tonic-gate  *		only those message strings that require translation to be output
540Sstevel@tonic-gate  *		to the message file.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *		Besides the reserved output indicators, a single token is taken
570Sstevel@tonic-gate  *		to be a message identifier which will be subsituted for a
580Sstevel@tonic-gate  *		`setid' for catgets(3c) output, or a `domain' name for
590Sstevel@tonic-gate  *		gettext(3i) output.  This value is determine by substituting the
600Sstevel@tonic-gate  *		token for the associated definition found in the message
610Sstevel@tonic-gate  *		identifier file (specified with the -i option).
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  *		Multiple tokens are taken to be a message definition followed by
640Sstevel@tonic-gate  *		the associated message string.  The message string is copied to
650Sstevel@tonic-gate  *		the data array being built in msg.c.  The index into this array
660Sstevel@tonic-gate  *		becomes the `message' identifier created in the msg.h file.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include	<fcntl.h>
700Sstevel@tonic-gate #include	<stdlib.h>
710Sstevel@tonic-gate #include	<stdio.h>
720Sstevel@tonic-gate #include	<unistd.h>
730Sstevel@tonic-gate #include	<limits.h>
740Sstevel@tonic-gate #include	<string.h>
750Sstevel@tonic-gate #include	<ctype.h>
760Sstevel@tonic-gate #include	<errno.h>
770Sstevel@tonic-gate #include	<sys/param.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include	<sgs.h>
805549Srie #include	<_string_table.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Define any error message strings.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate static const char
860Sstevel@tonic-gate 	* Errmsg_malt =	"sgsmsg: file %s: line %d: malformed input "
870Sstevel@tonic-gate 			"at line\n",
880Sstevel@tonic-gate 	* Errmsg_nmem =	"sgsmsg: memory allocation failed: %s\n",
890Sstevel@tonic-gate 	* Errmsg_opne =	"sgsmsg: file %s: open failed: %s\n",
900Sstevel@tonic-gate 	* Errmsg_wrte =	"sgsmsg: file %s: write failed: %s\n",
910Sstevel@tonic-gate 	* Errmsg_read =	"sgsmsg: file %s: read failed %s\n",
920Sstevel@tonic-gate 	* Errmsg_stnw =	"sgsmsg: st_new(): failed: %s\n",
930Sstevel@tonic-gate 	* Errmsg_stin =	"sgsmsg: Str_tbl insert failed: %s\n",
940Sstevel@tonic-gate 	* Errmsg_mnfn =	"sgsmsg: message not found in Str_tbl: %s\n",
950Sstevel@tonic-gate 	* Errmsg_use  =	"usage: sgsmsg [-clv] [-d mesgdata] [-h mesgdefs] "
960Sstevel@tonic-gate 			"[-m messages] [-n name] [-i mesgident] file ...\n";
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Define all output filenames and associated descriptors.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate static FILE	*fddefs, *fddata, *fdmsgs, *fdmids, *fddesc;
1020Sstevel@tonic-gate static char	*fldefs, *fldata, *flmsgs, *flmids, *fldesc;
1030Sstevel@tonic-gate static FILE	*fdlint;
1040Sstevel@tonic-gate static char	fllint[MAXPATHLEN];
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static uint_t		vflag;	/* verbose flag */
1070Sstevel@tonic-gate static Str_tbl		*stp;	/* string table */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Define any default strings.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate static const char
1130Sstevel@tonic-gate 	*nmlint =	"/tmp/sgsmsg.lint",
1140Sstevel@tonic-gate 	*interface =	"sgs_msg",
1150Sstevel@tonic-gate 	*start =	"_START_",
1160Sstevel@tonic-gate 	*end =		"_END_";
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Define any default flags and data items.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static int	cflag = 0, lflag = 0, prtmsgs = 0, line, ptr = 1, msgid = 0;
1220Sstevel@tonic-gate static char	*mesgid = 0, *setid = 0, *domain = 0;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate typedef struct msg_string {
1250Sstevel@tonic-gate 	char			*ms_defn;
1260Sstevel@tonic-gate 	char			*ms_message;
1270Sstevel@tonic-gate 	struct msg_string	*ms_next;
1280Sstevel@tonic-gate } msg_string;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static msg_string	*msg_head;
1310Sstevel@tonic-gate static msg_string	*msg_tail;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * message_append() is responsible for both inserting strings into
1350Sstevel@tonic-gate  * the master Str_tbl as well as maintaining a list of the
1360Sstevel@tonic-gate  * DEFINITIONS associated with each string.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  * The list of strings is traversed at the end once the full
1390Sstevel@tonic-gate  * Str_tbl has been constructed - and string offsets can be
1400Sstevel@tonic-gate  * assigned.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate static void
message_append(const char * defn,const char * message)1430Sstevel@tonic-gate message_append(const char *defn, const char *message)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	msg_string	*msg;
1460Sstevel@tonic-gate 	if ((msg = calloc(sizeof (msg_string), 1)) == 0) {
1470Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1480Sstevel@tonic-gate 		exit(1);
1490Sstevel@tonic-gate 	}
1505549Srie 
1515549Srie 	/*
1525549Srie 	 * Initialize the string table.
1535549Srie 	 */
1545549Srie 	if ((stp == 0) && ((stp = st_new(FLG_STNEW_COMPRESS)) == NULL)) {
1555549Srie 		(void) fprintf(stderr, Errmsg_stnw, strerror(errno));
1565549Srie 		exit(1);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if ((msg->ms_defn = strdup(defn)) == 0) {
1610Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1620Sstevel@tonic-gate 		exit(1);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if ((msg->ms_message = strdup(message)) == 0) {
1650Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1660Sstevel@tonic-gate 		exit(1);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (st_insert(stp, msg->ms_message) == -1) {
1700Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_stin,
1710Sstevel@tonic-gate 		    message);
1720Sstevel@tonic-gate 		exit(1);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (msg_head == 0) {
1760Sstevel@tonic-gate 		msg_head = msg_tail = msg;
1770Sstevel@tonic-gate 		return;
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 	msg_tail->ms_next = msg;
1800Sstevel@tonic-gate 	msg_tail = msg;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Initialize a setid value.  Given a setid definition determine its numeric
1850Sstevel@tonic-gate  * value from the specified message identifier file (specified with the -i
1860Sstevel@tonic-gate  * option).  Return a pointer to the numeric string.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate static int
getmesgid(char * id)1890Sstevel@tonic-gate getmesgid(char *id)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	char	*buffer, *token, *_mesgid = 0, *_setid = 0, *_domain = 0;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * If we're being asked to interpret a message id but the user didn't
1950Sstevel@tonic-gate 	 * provide the required message identifier file (-i option) we're in
1960Sstevel@tonic-gate 	 * trouble.
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	if (flmids == 0) {
1990Sstevel@tonic-gate 		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2000Sstevel@tonic-gate 		    "unable to process mesgid\n\t"
2010Sstevel@tonic-gate 		    "no message identifier file specified "
2020Sstevel@tonic-gate 		    "(see -i option)\n", fldesc, line, id);
2030Sstevel@tonic-gate 		return (1);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if ((buffer = malloc(LINE_MAX)) == 0) {
2070Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
2080Sstevel@tonic-gate 		return (1);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * Read the message identifier file and locate the required mesgid.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	rewind(fdmids);
2150Sstevel@tonic-gate 	while (fgets(buffer, LINE_MAX, fdmids) != NULL) {
2160Sstevel@tonic-gate 		if ((token = strstr(buffer, id)) == NULL)
2170Sstevel@tonic-gate 			continue;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * Establish individual strings for the mesgid, setid and domain
2210Sstevel@tonic-gate 		 * values.
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 		_mesgid = token;
2240Sstevel@tonic-gate 		while (!(isspace(*token)))
2250Sstevel@tonic-gate 			token++;
2260Sstevel@tonic-gate 		*token++ = 0;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		while (isspace(*token))
2290Sstevel@tonic-gate 			token++;
2300Sstevel@tonic-gate 		_setid = token;
2310Sstevel@tonic-gate 		while (!(isspace(*token)))
2320Sstevel@tonic-gate 			token++;
2330Sstevel@tonic-gate 		*token++ = 0;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		while (isspace(*token))
2360Sstevel@tonic-gate 			token++;
2370Sstevel@tonic-gate 		_domain = token;
2380Sstevel@tonic-gate 		while (!(isspace(*token)))
2390Sstevel@tonic-gate 			token++;
2400Sstevel@tonic-gate 		*token = 0;
2410Sstevel@tonic-gate 		break;
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Did we find a match?
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	if ((_mesgid == 0) || (_setid == 0) || (_domain == 0)) {
2480Sstevel@tonic-gate 		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2490Sstevel@tonic-gate 		    "unable to process mesgid\n\t"
2500Sstevel@tonic-gate 		    "identifier does not exist in file %s\n",
2510Sstevel@tonic-gate 		    fldesc, line, id, flmids);
2520Sstevel@tonic-gate 		return (1);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Have we been here before?
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	if (mesgid) {
2590Sstevel@tonic-gate 		if (cflag == 1) {
2600Sstevel@tonic-gate 			/*
2610Sstevel@tonic-gate 			 * If we're being asked to process more than one mesgid
2620Sstevel@tonic-gate 			 * warn the user that only one mesgid can be used for
2630Sstevel@tonic-gate 			 * the catgets(3c) call.
2640Sstevel@tonic-gate 			 */
2650Sstevel@tonic-gate 			(void) fprintf(stderr, "sgsmsg: file %s: line %d: "
2660Sstevel@tonic-gate 			    "setid %s: warning: multiple mesgids "
2670Sstevel@tonic-gate 			    "encountered\n\t"
2680Sstevel@tonic-gate 			    "last setting used in messaging code\n",
2690Sstevel@tonic-gate 			    fldesc, line, id);
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	mesgid = _mesgid;
2740Sstevel@tonic-gate 	setid = _setid;
2750Sstevel@tonic-gate 	domain = _domain;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Generate the message file output (insure output flag is enabled).
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	if (prtmsgs != -1)
2810Sstevel@tonic-gate 		prtmsgs = 1;
2820Sstevel@tonic-gate 	if (fdmsgs && (prtmsgs == 1)) {
2830Sstevel@tonic-gate 		if (cflag == 1) {
2840Sstevel@tonic-gate 			if (fprintf(fdmsgs, "$quote \"\n$set %s\n",
2850Sstevel@tonic-gate 			    setid) < 0) {
2860Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
2870Sstevel@tonic-gate 				    strerror(errno));
2880Sstevel@tonic-gate 				return (1);
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 		} else {
2910Sstevel@tonic-gate 			if (fprintf(fdmsgs, "domain\t\"%s\"\n", domain) < 0) {
2920Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
2930Sstevel@tonic-gate 				    strerror(errno));
2940Sstevel@tonic-gate 				return (1);
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/*
3000Sstevel@tonic-gate 	 * For catgets(3c) output generate a setid definition in the message
3010Sstevel@tonic-gate 	 * definition file.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate 	if (fddefs && (cflag == 1) &&
3040Sstevel@tonic-gate 	    (fprintf(fddefs, "#define\t%s\t%s\n\n", mesgid, setid) < 0)) {
3050Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3060Sstevel@tonic-gate 		return (1);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	return (0);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Dump contents of String Table to standard out
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate static void
dump_stringtab(Str_tbl * stp)3160Sstevel@tonic-gate dump_stringtab(Str_tbl *stp)
3170Sstevel@tonic-gate {
3185549Srie 	uint_t	cnt;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
3215892Sab196087 		(void) printf("string table full size: %ld: uncompressed\n",
3225549Srie 		    stp->st_fullstrsize);
3230Sstevel@tonic-gate 		return;
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3265892Sab196087 	(void) printf("string table full size: %ld compressed down to: %ld\n\n",
3275549Srie 	    stp->st_fullstrsize, stp->st_strsize);
3285549Srie 	(void) printf("string table compression information [%d buckets]:\n",
3295549Srie 	    stp->st_hbckcnt);
3300Sstevel@tonic-gate 
3315549Srie 	for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
3325549Srie 		Str_hash	*sthash = stp->st_hashbcks[cnt];
3335549Srie 
3345549Srie 		if (sthash == 0)
3355549Srie 			continue;
3365549Srie 
3375549Srie 		(void) printf(" bucket: [%d]\n", cnt);
3385549Srie 
3395549Srie 		while (sthash) {
3405892Sab196087 			size_t	stroff = sthash->hi_mstr->sm_strlen -
3415549Srie 			    sthash->hi_strlen;
3425549Srie 
3430Sstevel@tonic-gate 			if (stroff == 0) {
3445892Sab196087 				(void) printf("  [%ld]: '%s'  <master>\n",
3455549Srie 				    sthash->hi_refcnt, sthash->hi_mstr->sm_str);
3460Sstevel@tonic-gate 			} else {
3475892Sab196087 				(void) printf("  [%ld]: '%s'  <suffix of: "
3485549Srie 				    "'%s'>\n", sthash->hi_refcnt,
3495549Srie 				    &sthash->hi_mstr->sm_str[stroff],
3505549Srie 				    sthash->hi_mstr->sm_str);
3510Sstevel@tonic-gate 			}
3525549Srie 			sthash = sthash->hi_next;
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate }
3565549Srie 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Initialize the message definition header file stream.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate static int
init_defs(void)3610Sstevel@tonic-gate init_defs(void)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	static char	guard[FILENAME_MAX + 6];
3640Sstevel@tonic-gate 	char		*optr;
3650Sstevel@tonic-gate 	const char	*iptr, *_ptr;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * Establish a header guard name using the files basename.
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	for (iptr = 0, _ptr = fldefs; _ptr && (*_ptr != '\0'); _ptr++) {
3710Sstevel@tonic-gate 		if (*_ptr == '/')
3720Sstevel@tonic-gate 			iptr = _ptr + 1;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	if (iptr == 0)
3750Sstevel@tonic-gate 		iptr = fldefs;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	optr = guard;
3780Sstevel@tonic-gate 	for (*optr++ = '_'; iptr && (*iptr != '\0'); iptr++, optr++) {
3790Sstevel@tonic-gate 		if (*iptr == '.') {
3800Sstevel@tonic-gate 			*optr++ = '_';
3810Sstevel@tonic-gate 			*optr++ = 'D';
3820Sstevel@tonic-gate 			*optr++ = 'O';
3830Sstevel@tonic-gate 			*optr++ = 'T';
3840Sstevel@tonic-gate 			*optr = '_';
3850Sstevel@tonic-gate 		} else
3860Sstevel@tonic-gate 			*optr = toupper(*iptr);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (fprintf(fddefs, "#ifndef\t%s\n#define\t%s\n\n", guard, guard) < 0) {
3900Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3910Sstevel@tonic-gate 		return (1);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3949273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "#include <sgsmsg.h>\t/* Msg typedef */\n\n") < 0) {
3959273SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3969273SAli.Bahrami@Sun.COM 		return (1);
3979273SAli.Bahrami@Sun.COM 	}
3989273SAli.Bahrami@Sun.COM 
3990Sstevel@tonic-gate 	if (fprintf(fddefs, "#ifndef\t__lint\n\n") < 0) {
4000Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4010Sstevel@tonic-gate 		return (1);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/*
4059273SAli.Bahrami@Sun.COM 	 * The MSG_SGS_ARRAY_NAME macro supplies a generic way to
4069273SAli.Bahrami@Sun.COM 	 * reference the string table regardless of its name.
4070Sstevel@tonic-gate 	 */
4089273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "#define\tMSG_SGS_LOCAL_ARRAY\t__%s\n\n",
4099273SAli.Bahrami@Sun.COM 	    interface) < 0) {
4100Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4110Sstevel@tonic-gate 		return (1);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * If the associated data array is global define a prototype.
4160Sstevel@tonic-gate 	 * Define a macro to access the array elements.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	if (lflag == 0) {
4190Sstevel@tonic-gate 		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4200Sstevel@tonic-gate 		    interface) < 0) {
4210Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
4220Sstevel@tonic-gate 			    strerror(errno));
4230Sstevel@tonic-gate 			return (1);
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	}
4269273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs,
4279273SAli.Bahrami@Sun.COM 	    "#define\tMSG_ORIG_STRTAB(_x, _s)\t&_s[_x]\n\n") < 0) {
4289273SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4299273SAli.Bahrami@Sun.COM 		return (1);
4309273SAli.Bahrami@Sun.COM 	}
4319273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs,
4329273SAli.Bahrami@Sun.COM 	    "#define\tMSG_ORIG(x)\tMSG_ORIG_STRTAB(x, __%s)\n\n",
4330Sstevel@tonic-gate 	    interface) < 0) {
4340Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4350Sstevel@tonic-gate 		return (1);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Generate a prototype to access the associated data array.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4420Sstevel@tonic-gate 	    interface) < 0) {
4430Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4440Sstevel@tonic-gate 		return (1);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	if (fprintf(fddefs, "#define\tMSG_INTL(x)\t_%s(x)\n\n",
4470Sstevel@tonic-gate 	    interface) < 0) {
4480Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4490Sstevel@tonic-gate 		return (1);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	return (0);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * Finish the message definition header file.
4580Sstevel@tonic-gate  */
4590Sstevel@tonic-gate static int
fini_defs(void)4600Sstevel@tonic-gate fini_defs(void)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#else\t/* __lint */\n\n") < 0) {
4630Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4640Sstevel@tonic-gate 		return (1);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4679273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4689273SAli.Bahrami@Sun.COM 	    interface) < 0) {
4690Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4700Sstevel@tonic-gate 		return (1);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4739273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "#ifndef MSG_SGS_LOCAL_ARRAY\n"
4749273SAli.Bahrami@Sun.COM 	    "#define\tMSG_SGS_LOCAL_ARRAY\t\"\"\n#endif\n\n") < 0) {
4750Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4760Sstevel@tonic-gate 		return (1);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if (lflag == 0) {
4800Sstevel@tonic-gate 		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4810Sstevel@tonic-gate 		    interface) < 0) {
4820Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
4830Sstevel@tonic-gate 			    strerror(errno));
4840Sstevel@tonic-gate 			return (1);
4850Sstevel@tonic-gate 		}
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (fprintf(fddefs,
4899273SAli.Bahrami@Sun.COM 	    "#define MSG_ORIG_STRTAB(_x, _s)\t_x\n"
4900Sstevel@tonic-gate 	    "#define MSG_ORIG(x)\tx\n#define MSG_INTL(x)\tx\n") < 0) {
4910Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4920Sstevel@tonic-gate 		return (1);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4969273SAli.Bahrami@Sun.COM 	 * Provide a way to get the array and function declarations above
4979273SAli.Bahrami@Sun.COM 	 * without also getting the actual messages. This is useful in
4989273SAli.Bahrami@Sun.COM 	 * our lintsup.c files that include more than one message header.
4999273SAli.Bahrami@Sun.COM 	 * lintsup doesn't need the actual messages, and this prevents
5009273SAli.Bahrami@Sun.COM 	 * macro name collisions.
5019273SAli.Bahrami@Sun.COM 	 */
5029273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "\n#ifndef LINTSUP_SUPPRESS_STRINGS\n") < 0) {
5039273SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5049273SAli.Bahrami@Sun.COM 		return (1);
5059273SAli.Bahrami@Sun.COM 	}
5069273SAli.Bahrami@Sun.COM 
5079273SAli.Bahrami@Sun.COM 	/*
5080Sstevel@tonic-gate 	 * Copy the temporary lint defs file into the new header.
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	if (fdlint) {
5110Sstevel@tonic-gate 		long	size;
5120Sstevel@tonic-gate 		char	*buf;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		size = ftell(fdlint);
5150Sstevel@tonic-gate 		(void) rewind(fdlint);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		if ((buf = malloc(size)) == 0) {
5180Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
5190Sstevel@tonic-gate 			return (1);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 		if (fread(buf, size, 1, fdlint) == 0) {
5220Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_read, fllint,
5230Sstevel@tonic-gate 			    strerror(errno));
5240Sstevel@tonic-gate 			return (1);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		if (fwrite(buf, size, 1, fddefs) == 0) {
5270Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
5280Sstevel@tonic-gate 			    strerror(errno));
5290Sstevel@tonic-gate 			return (1);
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 		(void) free(buf);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5349273SAli.Bahrami@Sun.COM 	if (fprintf(fddefs, "\n#endif\t/* LINTSUP_SUPPRESS_STRINGS */\n") < 0) {
5359273SAli.Bahrami@Sun.COM 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5369273SAli.Bahrami@Sun.COM 		return (1);
5379273SAli.Bahrami@Sun.COM 	}
5389273SAli.Bahrami@Sun.COM 
5390Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#endif\t/* __lint */\n") < 0) {
5400Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5410Sstevel@tonic-gate 		return (1);
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#endif\n") < 0) {
5450Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5460Sstevel@tonic-gate 		return (1);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	return (0);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*
5530Sstevel@tonic-gate  * The entire messaging file has been scanned - and all strings have been
5540Sstevel@tonic-gate  * inserted into the string_table.  We can now walk the message queue
5550Sstevel@tonic-gate  * and create the '#define <DEFN>' for each string - with the strings
5560Sstevel@tonic-gate  * assigned offset into the string_table.
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate static int
output_defs(void)5590Sstevel@tonic-gate output_defs(void)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	msg_string	*msg;
5625892Sab196087 	size_t		stbufsize;
5630Sstevel@tonic-gate 	char		*stbuf;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	stbufsize = st_getstrtab_sz(stp);
5660Sstevel@tonic-gate 	if ((stbuf = malloc(stbufsize)) == 0) {
5670Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
5680Sstevel@tonic-gate 		exit(1);
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	(void) st_setstrbuf(stp, stbuf, stbufsize);
5710Sstevel@tonic-gate 	for (msg = msg_head; msg; msg = msg->ms_next) {
5725892Sab196087 		size_t	stoff;
5730Sstevel@tonic-gate 		if ((st_setstring(stp, msg->ms_message, &stoff)) == -1) {
5740Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_mnfn, msg->ms_message);
5750Sstevel@tonic-gate 			return (1);
5760Sstevel@tonic-gate 		}
5775892Sab196087 		if (fprintf(fddefs, "\n#define\t%s\t%ld\n",
5780Sstevel@tonic-gate 		    msg->ms_defn, stoff) < 0) {
5790Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte,
5800Sstevel@tonic-gate 			    fldefs, strerror(errno));
5810Sstevel@tonic-gate 			return (1);
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 		if (fddefs && fprintf(fddefs, "#define\t%s_SIZE\t%d\n",
5840Sstevel@tonic-gate 		    msg->ms_defn, strlen(msg->ms_message)) < 0) {
5850Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte,
5860Sstevel@tonic-gate 			    fldefs, strerror(errno));
5870Sstevel@tonic-gate 			return (1);
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	return (0);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate  * Finish off the data structure definition.
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate static int
output_data(void)5980Sstevel@tonic-gate output_data(void)
5990Sstevel@tonic-gate {
6005892Sab196087 	size_t		stbufsize;
6015892Sab196087 	size_t		ndx;
6025892Sab196087 	size_t		column = 1;
6030Sstevel@tonic-gate 	const char	*stbuf;
6040Sstevel@tonic-gate 	const char	*fmtstr;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	stbufsize = st_getstrtab_sz(stp);
6070Sstevel@tonic-gate 	stbuf = st_getstrbuf(stp);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	assert(stbuf);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * Determine from the local flag whether the data declaration should
6130Sstevel@tonic-gate 	 * be static.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if (lflag)
6160Sstevel@tonic-gate 		fmtstr = (const char *)"static const";
6170Sstevel@tonic-gate 	else
6180Sstevel@tonic-gate 		fmtstr = (const char *)"const";
6190Sstevel@tonic-gate 
6205892Sab196087 	if (fprintf(fddata, "\n%s char __%s[%ld] = { ",
6210Sstevel@tonic-gate 	    fmtstr, interface, stbufsize) < 0) {
6220Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
6230Sstevel@tonic-gate 		return (1);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	for (ndx = 0; ndx < (stbufsize - 1); ndx++) {
6270Sstevel@tonic-gate 		if (column == 1) {
6280Sstevel@tonic-gate 			if (fddata && fprintf(fddata,
6295892Sab196087 			    "\n/* %4ld */ 0x%.2x,", ndx,
6300Sstevel@tonic-gate 			    (unsigned char)stbuf[ndx]) < 0) {
6310Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte,
6320Sstevel@tonic-gate 				    fldata, strerror(errno));
6330Sstevel@tonic-gate 				return (1);
6340Sstevel@tonic-gate 			}
6350Sstevel@tonic-gate 		} else {
6360Sstevel@tonic-gate 			if (fddata && fprintf(fddata, "  0x%.2x,",
6370Sstevel@tonic-gate 			    (unsigned char)stbuf[ndx]) < 0) {
6380Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte,
6390Sstevel@tonic-gate 				    fldata, strerror(errno));
6400Sstevel@tonic-gate 				return (1);
6410Sstevel@tonic-gate 			}
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		if (column++ == 10)
6450Sstevel@tonic-gate 			column = 1;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (column == 1)
6490Sstevel@tonic-gate 		fmtstr = "\n\t0x%.2x };\n";
6500Sstevel@tonic-gate 	else
6510Sstevel@tonic-gate 		fmtstr = "  0x%.2x };\n";
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (fprintf(fddata, fmtstr, (unsigned char)stbuf[stbufsize - 1]) < 0) {
6540Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
6550Sstevel@tonic-gate 		return (1);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (0);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate static int
file()6620Sstevel@tonic-gate file()
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	char	buffer[LINE_MAX], * token;
6650Sstevel@tonic-gate 	uint_t	bufsize;
6660Sstevel@tonic-gate 	char	*token_buffer;
6670Sstevel@tonic-gate 	int	escape = 0;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if ((token_buffer = malloc(LINE_MAX)) == 0) {
6700Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
6710Sstevel@tonic-gate 		return (1);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	bufsize = LINE_MAX;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	line = 1;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	while ((token = fgets(buffer, LINE_MAX, fddesc)) != NULL) {
6780Sstevel@tonic-gate 		char	defn[PATH_MAX], * _defn, * str;
6790Sstevel@tonic-gate 		int	len;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		switch (*token) {
6820Sstevel@tonic-gate 		case '#':
6830Sstevel@tonic-gate 		case '$':
6840Sstevel@tonic-gate 			if (escape) {
6850Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
6860Sstevel@tonic-gate 				    line);
6870Sstevel@tonic-gate 				return (1);
6880Sstevel@tonic-gate 			}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 			/*
6910Sstevel@tonic-gate 			 * If a msgid has been output a msgstr must follow
6920Sstevel@tonic-gate 			 * before we digest the new token.  A msgid is only set
6930Sstevel@tonic-gate 			 * if fdmsgs is in use.
6940Sstevel@tonic-gate 			 */
6950Sstevel@tonic-gate 			if (msgid) {
6960Sstevel@tonic-gate 				msgid = 0;
6970Sstevel@tonic-gate 				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
6980Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
6990Sstevel@tonic-gate 					    flmsgs, strerror(errno));
7000Sstevel@tonic-gate 					return (1);
7010Sstevel@tonic-gate 				}
7020Sstevel@tonic-gate 			}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 			/*
7050Sstevel@tonic-gate 			 * Pass lines directly through to the output message
7060Sstevel@tonic-gate 			 * file.
7070Sstevel@tonic-gate 			 */
7080Sstevel@tonic-gate 			if (fdmsgs && (prtmsgs == 1)) {
7090Sstevel@tonic-gate 				char	comment;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 				if (cflag == 0)
7120Sstevel@tonic-gate 					comment = '#';
7130Sstevel@tonic-gate 				else
7140Sstevel@tonic-gate 					comment = '$';
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 				if (fprintf(fdmsgs, "%c%s", comment,
7170Sstevel@tonic-gate 				    ++token) < 0) {
7180Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
7190Sstevel@tonic-gate 					    flmsgs, strerror(errno));
7200Sstevel@tonic-gate 					return (1);
7210Sstevel@tonic-gate 				}
7220Sstevel@tonic-gate 			}
7230Sstevel@tonic-gate 			break;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		case '@':
7260Sstevel@tonic-gate 			if (escape) {
7270Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
7280Sstevel@tonic-gate 				    line);
7290Sstevel@tonic-gate 				return (1);
7300Sstevel@tonic-gate 			}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 			/*
7330Sstevel@tonic-gate 			 * If a msgid has been output a msgstr must follow
7340Sstevel@tonic-gate 			 * before we digest the new token.
7350Sstevel@tonic-gate 			 */
7360Sstevel@tonic-gate 			if (msgid) {
7370Sstevel@tonic-gate 				msgid = 0;
7380Sstevel@tonic-gate 				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
7390Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
7400Sstevel@tonic-gate 					    flmsgs, strerror(errno));
7410Sstevel@tonic-gate 					return (1);
7420Sstevel@tonic-gate 				}
7430Sstevel@tonic-gate 			}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 			/*
7460Sstevel@tonic-gate 			 * Determine whether we have one or more tokens.
7470Sstevel@tonic-gate 			 */
7480Sstevel@tonic-gate 			token++;
7490Sstevel@tonic-gate 			while (isspace(*token))		/* rid any whitespace */
7500Sstevel@tonic-gate 				token++;
7510Sstevel@tonic-gate 			_defn = token;			/* definition start */
7520Sstevel@tonic-gate 			while (!(isspace(*token)))
7530Sstevel@tonic-gate 				token++;
7540Sstevel@tonic-gate 			*token++ = 0;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 			while (isspace(*token))		/* rid any whitespace */
7570Sstevel@tonic-gate 				token++;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 			/*
7600Sstevel@tonic-gate 			 * Determine whether the single token is one of the
7610Sstevel@tonic-gate 			 * reserved message output delimiters otherwise
7620Sstevel@tonic-gate 			 * translate it as a message identifier.
7630Sstevel@tonic-gate 			 */
7640Sstevel@tonic-gate 			if (*token == 0) {
7650Sstevel@tonic-gate 				if (strcmp(_defn, start) == 0)
7660Sstevel@tonic-gate 					prtmsgs = 1;
7670Sstevel@tonic-gate 				else if (strcmp(_defn, end) == 0)
7680Sstevel@tonic-gate 					prtmsgs = -1;
7690Sstevel@tonic-gate 				else if (getmesgid(_defn) == 1)
7700Sstevel@tonic-gate 					return (1);
7710Sstevel@tonic-gate 				break;
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 			/*
7750Sstevel@tonic-gate 			 * Multiple tokens are translated by taking the first
7760Sstevel@tonic-gate 			 * token as the message definition, and the rest of the
7770Sstevel@tonic-gate 			 * line as the message itself.  A message line ending
7780Sstevel@tonic-gate 			 * with an escape ('\') is expected to be continued on
7790Sstevel@tonic-gate 			 * the next line.
7800Sstevel@tonic-gate 			 */
7810Sstevel@tonic-gate 			if (prtmsgs != -1)
7820Sstevel@tonic-gate 				prtmsgs = 1;
7830Sstevel@tonic-gate 			if (fdmsgs && (prtmsgs == 1)) {
7840Sstevel@tonic-gate 				/*
7850Sstevel@tonic-gate 				 * For catgets(3c) make sure a message
7860Sstevel@tonic-gate 				 * identifier has been established (this is
7870Sstevel@tonic-gate 				 * normally a domain for gettext(3i), but for
7880Sstevel@tonic-gate 				 * sgsmsg use this could be argued as being
7890Sstevel@tonic-gate 				 * redundent).  Also make sure that the message
7900Sstevel@tonic-gate 				 * definitions haven't exceeeded the maximum
7910Sstevel@tonic-gate 				 * value allowed by gencat(1) before generating
7920Sstevel@tonic-gate 				 * any message file entries.
7930Sstevel@tonic-gate 				 */
7940Sstevel@tonic-gate 				if (cflag == 1) {
7950Sstevel@tonic-gate 					if (setid == 0) {
7960Sstevel@tonic-gate 						(void) fprintf(stderr, "file "
7970Sstevel@tonic-gate 						    "%s: no message identifier "
7980Sstevel@tonic-gate 						    "has been established\n",
7990Sstevel@tonic-gate 						    fldesc);
8000Sstevel@tonic-gate 						return (1);
8010Sstevel@tonic-gate 					}
8020Sstevel@tonic-gate 					if (ptr	> NL_MSGMAX) {
8030Sstevel@tonic-gate 						(void) fprintf(stderr, "file "
8040Sstevel@tonic-gate 						    "%s: message definition "
8050Sstevel@tonic-gate 						    "(%d) exceeds allowable "
8060Sstevel@tonic-gate 						    "limit (NL_MSGMAX)\n",
8070Sstevel@tonic-gate 						    fldesc, ptr);
8080Sstevel@tonic-gate 						return (1);
8090Sstevel@tonic-gate 					}
8100Sstevel@tonic-gate 				}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 				/*
8130Sstevel@tonic-gate 				 * For catgets(3c) write the definition and the
8140Sstevel@tonic-gate 				 * message string to the message file.  For
8150Sstevel@tonic-gate 				 * gettext(3i) write the message string as a
8160Sstevel@tonic-gate 				 * mesgid - indicate a mesgid has been output
8170Sstevel@tonic-gate 				 * so that a msgstr can follow.
8180Sstevel@tonic-gate 				 */
8190Sstevel@tonic-gate 				if (cflag == 1) {
8200Sstevel@tonic-gate 					if (fprintf(fdmsgs, "%d\t%s", ptr,
8210Sstevel@tonic-gate 					    token) < 0) {
8220Sstevel@tonic-gate 						(void) fprintf(stderr,
8230Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
8240Sstevel@tonic-gate 						    strerror(errno));
8250Sstevel@tonic-gate 						return (1);
8260Sstevel@tonic-gate 					}
8270Sstevel@tonic-gate 				} else {
8280Sstevel@tonic-gate 					if (fprintf(fdmsgs, "msgid\t\"") < 0) {
8290Sstevel@tonic-gate 						(void) fprintf(stderr,
8300Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
8310Sstevel@tonic-gate 						    strerror(errno));
8320Sstevel@tonic-gate 						return (1);
8330Sstevel@tonic-gate 					}
8340Sstevel@tonic-gate 					msgid = 1;
8350Sstevel@tonic-gate 				}
8360Sstevel@tonic-gate 			}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 			/*
8390Sstevel@tonic-gate 			 * The message itself is a quoted string as this makes
8400Sstevel@tonic-gate 			 * embedding spaces at the start (or the end) of the
8410Sstevel@tonic-gate 			 * string very easy.
8420Sstevel@tonic-gate 			 */
8430Sstevel@tonic-gate 			if (*token != '"') {
8440Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
8450Sstevel@tonic-gate 				    line);
8460Sstevel@tonic-gate 				return (1);
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 			(void) strcpy(defn, _defn);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 			/*
8520Sstevel@tonic-gate 			 * Write the tag to the lint definitions.
8530Sstevel@tonic-gate 			 */
8540Sstevel@tonic-gate 			if (fdlint) {
8550Sstevel@tonic-gate 				if (fprintf(fdlint, "\n#define\t%s\t",
8560Sstevel@tonic-gate 				    _defn) < 0) {
8570Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
8580Sstevel@tonic-gate 					    fllint, strerror(errno));
8590Sstevel@tonic-gate 					return (1);
8600Sstevel@tonic-gate 				}
8610Sstevel@tonic-gate 			}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 			len = 0;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 			/*
8660Sstevel@tonic-gate 			 * Write each character of the message string to the
8670Sstevel@tonic-gate 			 * data array.  Translate any escaped characters - use
8680Sstevel@tonic-gate 			 * the same specially recognized characters as defined
8690Sstevel@tonic-gate 			 * by gencat(1).
8700Sstevel@tonic-gate 			 */
8710Sstevel@tonic-gate message:
8720Sstevel@tonic-gate 			if (*token == '"') {
8730Sstevel@tonic-gate 				if (fdlint &&
8740Sstevel@tonic-gate 				    (fprintf(fdlint, "%c", *token) < 0)) {
8750Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
8760Sstevel@tonic-gate 					    fllint, strerror(errno));
8770Sstevel@tonic-gate 					return (1);
8780Sstevel@tonic-gate 				}
8790Sstevel@tonic-gate 				token++;
8800Sstevel@tonic-gate 			}
8810Sstevel@tonic-gate 			while (*token) {
8820Sstevel@tonic-gate 				char	_token;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 				if ((*token == '\\') && (escape == 0)) {
8850Sstevel@tonic-gate 					escape = 1;
8860Sstevel@tonic-gate 					if (fdlint && (*(token + 1) != '\n') &&
8870Sstevel@tonic-gate 					    fprintf(fdlint, "%c", *token) < 0) {
8880Sstevel@tonic-gate 						(void) fprintf(stderr,
8890Sstevel@tonic-gate 						    Errmsg_wrte, fllint,
8900Sstevel@tonic-gate 						    strerror(errno));
8910Sstevel@tonic-gate 						return (1);
8920Sstevel@tonic-gate 					}
8930Sstevel@tonic-gate 					token++;
8940Sstevel@tonic-gate 					continue;
8950Sstevel@tonic-gate 				}
8960Sstevel@tonic-gate 				if (escape) {
8970Sstevel@tonic-gate 					if (*token == 'n')
8980Sstevel@tonic-gate 						_token = '\n';
8990Sstevel@tonic-gate 					else if (*token == 't')
9000Sstevel@tonic-gate 						_token = '\t';
9010Sstevel@tonic-gate 					else if (*token == 'v')
9020Sstevel@tonic-gate 						_token = '\v';
9030Sstevel@tonic-gate 					else if (*token == 'b')
9040Sstevel@tonic-gate 						_token = '\b';
9050Sstevel@tonic-gate 					else if (*token == 'f')
9060Sstevel@tonic-gate 						_token = '\f';
9070Sstevel@tonic-gate 					else if (*token == '\\')
9080Sstevel@tonic-gate 						_token = '\\';
9090Sstevel@tonic-gate 					else if (*token == '"')
9100Sstevel@tonic-gate 						_token = '"';
9110Sstevel@tonic-gate 					else if (*token == '\n')
9120Sstevel@tonic-gate 						break;
9130Sstevel@tonic-gate 					else
9140Sstevel@tonic-gate 						_token = *token;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 					if (fdmsgs && (prtmsgs == 1) &&
9170Sstevel@tonic-gate 					    (fprintf(fdmsgs, "\\") < 0)) {
9180Sstevel@tonic-gate 						(void) fprintf(stderr,
9190Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
9200Sstevel@tonic-gate 						    strerror(errno));
9210Sstevel@tonic-gate 						return (1);
9220Sstevel@tonic-gate 					}
9230Sstevel@tonic-gate 				} else {
9240Sstevel@tonic-gate 					/*
9250Sstevel@tonic-gate 					 * If this is the trailing quote then
9260Sstevel@tonic-gate 					 * thats the last of the message string.
9270Sstevel@tonic-gate 					 * Eat up any remaining white space and
9280Sstevel@tonic-gate 					 * unless an escape character is found
9290Sstevel@tonic-gate 					 * terminate the data string with a 0.
9300Sstevel@tonic-gate 					 */
9315549Srie 					/* BEGIN CSTYLED */
9320Sstevel@tonic-gate 					if (*token == '"') {
9330Sstevel@tonic-gate 					    if (fdlint && (fprintf(fdlint,
9340Sstevel@tonic-gate 						"%c", *token) < 0)) {
9350Sstevel@tonic-gate 						(void) fprintf(stderr,
9360Sstevel@tonic-gate 						    Errmsg_wrte, fllint,
9370Sstevel@tonic-gate 						    strerror(errno));
9380Sstevel@tonic-gate 						return (1);
9390Sstevel@tonic-gate 					    }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 					    if (fdmsgs && (prtmsgs == 1) &&
9420Sstevel@tonic-gate 						(fprintf(fdmsgs, "%c",
9430Sstevel@tonic-gate 						*token) < 0)) {
9440Sstevel@tonic-gate 						(void) fprintf(stderr,
9450Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
9460Sstevel@tonic-gate 						    strerror(errno));
9470Sstevel@tonic-gate 						return (1);
9480Sstevel@tonic-gate 					    }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 					    while (*++token) {
9510Sstevel@tonic-gate 						if (*token == '\n')
9520Sstevel@tonic-gate 							break;
9530Sstevel@tonic-gate 					    }
9540Sstevel@tonic-gate 					    _token = '\0';
9550Sstevel@tonic-gate 					} else
9560Sstevel@tonic-gate 					    _token = *token;
9575549Srie 					/* END CSTYLED */
9580Sstevel@tonic-gate 				}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 				if (fdmsgs && (prtmsgs == 1) &&
9610Sstevel@tonic-gate 				    (fprintf(fdmsgs, "%c", *token) < 0)) {
9620Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
9630Sstevel@tonic-gate 					    flmsgs, strerror(errno));
9640Sstevel@tonic-gate 					return (1);
9650Sstevel@tonic-gate 				}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 				if (fdlint && fprintf(fdlint,
9680Sstevel@tonic-gate 				    "%c", *token) < 0) {
9690Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
9700Sstevel@tonic-gate 					    fllint, strerror(errno));
9710Sstevel@tonic-gate 					return (1);
9720Sstevel@tonic-gate 				}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 				if (len >= bufsize) {
9750Sstevel@tonic-gate 					bufsize += LINE_MAX;
9760Sstevel@tonic-gate 					if ((token_buffer = realloc(
9770Sstevel@tonic-gate 					    token_buffer, bufsize)) == 0) {
9780Sstevel@tonic-gate 						(void) fprintf(stderr,
9790Sstevel@tonic-gate 						    Errmsg_nmem,
9800Sstevel@tonic-gate 						    strerror(errno));
9810Sstevel@tonic-gate 						return (1);
9820Sstevel@tonic-gate 					}
9830Sstevel@tonic-gate 				}
9840Sstevel@tonic-gate 				token_buffer[len] = _token;
9850Sstevel@tonic-gate 				ptr++, token++, len++;
9860Sstevel@tonic-gate 				escape = 0;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 				if (_token == '\0')
9890Sstevel@tonic-gate 					break;
9900Sstevel@tonic-gate 			}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 			/*
9930Sstevel@tonic-gate 			 * After the complete message string has been processed
9940Sstevel@tonic-gate 			 * (including its continuation beyond one line), create
9950Sstevel@tonic-gate 			 * a string size definition.
9960Sstevel@tonic-gate 			 */
9970Sstevel@tonic-gate 			if (escape == 0) {
9980Sstevel@tonic-gate 				const char *form = "#define\t%s_SIZE\t%d\n";
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 				token_buffer[len] = '\0';
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 				message_append(defn, token_buffer);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 				if (fdlint && fprintf(fdlint, form, defn,
10050Sstevel@tonic-gate 				    (len - 1)) < 0) {
10060Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
10070Sstevel@tonic-gate 					    fllint, strerror(errno));
10080Sstevel@tonic-gate 					return (1);
10090Sstevel@tonic-gate 				}
10100Sstevel@tonic-gate 			}
10110Sstevel@tonic-gate 			break;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		default:
10140Sstevel@tonic-gate 			/*
10150Sstevel@tonic-gate 			 * Empty lines are passed through to the message file.
10160Sstevel@tonic-gate 			 */
10170Sstevel@tonic-gate 			while (isspace(*token))
10180Sstevel@tonic-gate 				token++;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 			if (*token == 0) {
10210Sstevel@tonic-gate 				if (msgid || (fdmsgs && (prtmsgs == 1))) {
10220Sstevel@tonic-gate 					/*
10230Sstevel@tonic-gate 					 * If a msgid has been output a msgstr
10240Sstevel@tonic-gate 					 * must follow before we digest the new
10250Sstevel@tonic-gate 					 * token.
10260Sstevel@tonic-gate 					 */
10270Sstevel@tonic-gate 					if (msgid) {
10280Sstevel@tonic-gate 						msgid = 0;
10290Sstevel@tonic-gate 						str = "msgstr\t\"\"\n\n";
10300Sstevel@tonic-gate 					} else
10310Sstevel@tonic-gate 						str = "\n";
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 					if (fprintf(fdmsgs, str) < 0) {
10340Sstevel@tonic-gate 						(void) fprintf(stderr,
10350Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
10360Sstevel@tonic-gate 						    strerror(errno));
10370Sstevel@tonic-gate 						return (1);
10380Sstevel@tonic-gate 					}
10390Sstevel@tonic-gate 				}
10400Sstevel@tonic-gate 				break;
10410Sstevel@tonic-gate 			}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			/*
10440Sstevel@tonic-gate 			 * If an escape is in effect then any tokens are taken
10450Sstevel@tonic-gate 			 * to be message continuations.
10460Sstevel@tonic-gate 			 */
10470Sstevel@tonic-gate 			if (escape) {
10480Sstevel@tonic-gate 				escape = 0;
10490Sstevel@tonic-gate 				goto message;
10500Sstevel@tonic-gate 			}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 			(void) fprintf(stderr, "file %s: line %d: invalid "
10530Sstevel@tonic-gate 			    "input does not start with #, $ or @\n", fldesc,
10540Sstevel@tonic-gate 			    line);
10550Sstevel@tonic-gate 			return (1);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 		line++;
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	free(token_buffer);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	return (0);
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate int
main(int argc,char ** argv)10660Sstevel@tonic-gate main(int argc, char ** argv)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	opterr = 0;
10690Sstevel@tonic-gate 	while ((line = getopt(argc, argv, "cd:h:lm:n:i:v")) != EOF) {
10700Sstevel@tonic-gate 		switch (line) {
10710Sstevel@tonic-gate 		case 'c':			/* catgets instead of gettext */
10720Sstevel@tonic-gate 			cflag = 1;
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 		case 'd':			/* new message data filename */
10750Sstevel@tonic-gate 			fldata = optarg;	/*	(msg.c is default) */
10760Sstevel@tonic-gate 			break;
10770Sstevel@tonic-gate 		case 'h':			/* new message defs filename */
10780Sstevel@tonic-gate 			fldefs = optarg;	/*	(msg.h is default) */
10790Sstevel@tonic-gate 			break;
10800Sstevel@tonic-gate 		case 'i':			/* input message ids from */
10810Sstevel@tonic-gate 			flmids = optarg;	/*	from this file */
10820Sstevel@tonic-gate 			break;
10830Sstevel@tonic-gate 		case 'l':			/* define message data arrays */
10840Sstevel@tonic-gate 			lflag = 1;		/*	to be local (static) */
10850Sstevel@tonic-gate 			break;
10860Sstevel@tonic-gate 		case 'm':			/* generate message database */
10870Sstevel@tonic-gate 			flmsgs = optarg;	/*	to this file */
10880Sstevel@tonic-gate 			break;
10890Sstevel@tonic-gate 		case 'n':			/* new data array and func */
10900Sstevel@tonic-gate 			interface = optarg;	/*	name (msg is default) */
10910Sstevel@tonic-gate 			break;
10920Sstevel@tonic-gate 		case 'v':
10930Sstevel@tonic-gate 			vflag = 1;		/* set verbose flag */
10940Sstevel@tonic-gate 			break;
10950Sstevel@tonic-gate 		case '?':
10960Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_use, argv[0]);
10970Sstevel@tonic-gate 			exit(1);
10980Sstevel@tonic-gate 		default:
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	/*
11040Sstevel@tonic-gate 	 * Validate the we have been given at least one input file.
11050Sstevel@tonic-gate 	 */
11060Sstevel@tonic-gate 	if ((argc - optind) < 1) {
11070Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_use);
11080Sstevel@tonic-gate 		exit(1);
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/*
11120Sstevel@tonic-gate 	 * Open all the required output files.
11130Sstevel@tonic-gate 	 */
11140Sstevel@tonic-gate 	if (fldefs) {
11150Sstevel@tonic-gate 		if ((fddefs = fopen(fldefs, "w+")) == NULL) {
11160Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldefs,
11170Sstevel@tonic-gate 			    strerror(errno));
11180Sstevel@tonic-gate 			return (1);
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	if (fldata) {
11220Sstevel@tonic-gate 		if (fldefs && (strcmp(fldefs, fldata) == 0))
11230Sstevel@tonic-gate 			fddata = fddefs;
11240Sstevel@tonic-gate 		else if ((fddata = fopen(fldata, "w+")) == NULL) {
11250Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldata,
11260Sstevel@tonic-gate 			    strerror(errno));
11270Sstevel@tonic-gate 			return (1);
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 	if (fddefs && fddata) {
1131*12736SAli.Bahrami@Oracle.COM 		(void) sprintf(fllint, "%s.%d.XXXXXX", nmlint, (int)getpid());
1132*12736SAli.Bahrami@Oracle.COM 		if ((mkstemp(fllint) == -1) ||
1133*12736SAli.Bahrami@Oracle.COM 		    ((fdlint = fopen(fllint, "w+")) == NULL)) {
11340Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fllint,
11350Sstevel@tonic-gate 			    strerror(errno));
11360Sstevel@tonic-gate 			return (1);
11370Sstevel@tonic-gate 		}
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	if (flmsgs) {
11400Sstevel@tonic-gate 		if ((fdmsgs = fopen(flmsgs, "w+")) == NULL) {
11410Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, flmsgs,
11420Sstevel@tonic-gate 			    strerror(errno));
11430Sstevel@tonic-gate 			return (1);
11440Sstevel@tonic-gate 		}
11450Sstevel@tonic-gate 	}
11460Sstevel@tonic-gate 	if (flmids) {
11470Sstevel@tonic-gate 		if ((fdmids = fopen(flmids, "r")) == NULL) {
11480Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, flmids,
11490Sstevel@tonic-gate 			    strerror(errno));
11500Sstevel@tonic-gate 			return (1);
11510Sstevel@tonic-gate 		}
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * Initialize the message definition and message data streams.
11570Sstevel@tonic-gate 	 */
11580Sstevel@tonic-gate 	if (fddefs) {
11590Sstevel@tonic-gate 		if (init_defs())
11600Sstevel@tonic-gate 			return (1);
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	/*
11640Sstevel@tonic-gate 	 * Read the input message file, and for each line process accordingly.
11650Sstevel@tonic-gate 	 */
11660Sstevel@tonic-gate 	for (; optind < argc; optind++) {
11670Sstevel@tonic-gate 		int	err;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		fldesc = argv[optind];
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		if ((fddesc = fopen(fldesc, "r")) == NULL) {
11720Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldesc,
11730Sstevel@tonic-gate 			    strerror(errno));
11740Sstevel@tonic-gate 			return (1);
11750Sstevel@tonic-gate 		}
11760Sstevel@tonic-gate 		err = file();
11770Sstevel@tonic-gate 		(void) fclose(fddesc);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 		if (err != 0)
11800Sstevel@tonic-gate 			return (1);
11810Sstevel@tonic-gate 	}
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	/*
11840Sstevel@tonic-gate 	 * If a msgid has been output a msgstr must follow before we end the
11850Sstevel@tonic-gate 	 * file.
11860Sstevel@tonic-gate 	 */
11870Sstevel@tonic-gate 	if (msgid) {
11880Sstevel@tonic-gate 		msgid = 0;
11890Sstevel@tonic-gate 		if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
11900Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, flmsgs,
11910Sstevel@tonic-gate 			    strerror(errno));
11920Sstevel@tonic-gate 			return (1);
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	if (fdmids)
11970Sstevel@tonic-gate 		(void) fclose(fdmids);
11980Sstevel@tonic-gate 	if (fdmsgs)
11990Sstevel@tonic-gate 		(void) fclose(fdmsgs);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (fddefs) {
12020Sstevel@tonic-gate 		if (output_defs())
12030Sstevel@tonic-gate 			return (1);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	/*
12070Sstevel@tonic-gate 	 * Finish off any generated data and header file.
12080Sstevel@tonic-gate 	 */
12090Sstevel@tonic-gate 	if (fldata) {
12100Sstevel@tonic-gate 		if (output_data())
12110Sstevel@tonic-gate 			return (1);
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 	if (fddefs) {
12140Sstevel@tonic-gate 		if (fini_defs())
12150Sstevel@tonic-gate 			return (1);
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (vflag)
12190Sstevel@tonic-gate 		dump_stringtab(stp);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/*
12220Sstevel@tonic-gate 	 * Close up everything and go home.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate 	if (fddata)
12250Sstevel@tonic-gate 		(void) fclose(fddata);
12260Sstevel@tonic-gate 	if (fddefs && (fddefs != fddata))
12270Sstevel@tonic-gate 		(void) fclose(fddefs);
12280Sstevel@tonic-gate 	if (fddefs && fddata) {
12290Sstevel@tonic-gate 		(void) fclose(fdlint);
12300Sstevel@tonic-gate 		(void) unlink(fllint);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (stp)
12340Sstevel@tonic-gate 		st_destroy(stp);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	return (0);
12370Sstevel@tonic-gate }
1238