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