xref: /onnv-gate/usr/src/cmd/sgs/tools/common/sgsmsg.c (revision 5892:b863dde33f1b)
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*5892Sab196087  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include	<fcntl.h>
720Sstevel@tonic-gate #include	<stdlib.h>
730Sstevel@tonic-gate #include	<stdio.h>
740Sstevel@tonic-gate #include	<unistd.h>
750Sstevel@tonic-gate #include	<limits.h>
760Sstevel@tonic-gate #include	<string.h>
770Sstevel@tonic-gate #include	<ctype.h>
780Sstevel@tonic-gate #include	<errno.h>
790Sstevel@tonic-gate #include	<sys/param.h>
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #include	<sgs.h>
825549Srie #include	<_string_table.h>
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Define any error message strings.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static const char
880Sstevel@tonic-gate 	* Errmsg_malt =	"sgsmsg: file %s: line %d: malformed input "
890Sstevel@tonic-gate 			"at line\n",
900Sstevel@tonic-gate 	* Errmsg_nmem =	"sgsmsg: memory allocation failed: %s\n",
910Sstevel@tonic-gate 	* Errmsg_opne =	"sgsmsg: file %s: open failed: %s\n",
920Sstevel@tonic-gate 	* Errmsg_wrte =	"sgsmsg: file %s: write failed: %s\n",
930Sstevel@tonic-gate 	* Errmsg_read =	"sgsmsg: file %s: read failed %s\n",
940Sstevel@tonic-gate 	* Errmsg_stnw =	"sgsmsg: st_new(): failed: %s\n",
950Sstevel@tonic-gate 	* Errmsg_stin =	"sgsmsg: Str_tbl insert failed: %s\n",
960Sstevel@tonic-gate 	* Errmsg_mnfn =	"sgsmsg: message not found in Str_tbl: %s\n",
970Sstevel@tonic-gate 	* Errmsg_use  =	"usage: sgsmsg [-clv] [-d mesgdata] [-h mesgdefs] "
980Sstevel@tonic-gate 			"[-m messages] [-n name] [-i mesgident] file ...\n";
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Define all output filenames and associated descriptors.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static FILE	*fddefs, *fddata, *fdmsgs, *fdmids, *fddesc;
1040Sstevel@tonic-gate static char	*fldefs, *fldata, *flmsgs, *flmids, *fldesc;
1050Sstevel@tonic-gate static FILE	*fdlint;
1060Sstevel@tonic-gate static char	fllint[MAXPATHLEN];
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static uint_t		vflag;	/* verbose flag */
1090Sstevel@tonic-gate static Str_tbl		*stp;	/* string table */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Define any default strings.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate static const char
1150Sstevel@tonic-gate 	*nmlint =	"/tmp/sgsmsg.lint",
1160Sstevel@tonic-gate 	*interface =	"sgs_msg",
1170Sstevel@tonic-gate 	*start =	"_START_",
1180Sstevel@tonic-gate 	*end =		"_END_";
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Define any default flags and data items.
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate static int	cflag = 0, lflag = 0, prtmsgs = 0, line, ptr = 1, msgid = 0;
1240Sstevel@tonic-gate static char	*mesgid = 0, *setid = 0, *domain = 0;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate typedef struct msg_string {
1270Sstevel@tonic-gate 	char			*ms_defn;
1280Sstevel@tonic-gate 	char			*ms_message;
1290Sstevel@tonic-gate 	struct msg_string	*ms_next;
1300Sstevel@tonic-gate } msg_string;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static msg_string	*msg_head;
1330Sstevel@tonic-gate static msg_string	*msg_tail;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * message_append() is responsible for both inserting strings into
1370Sstevel@tonic-gate  * the master Str_tbl as well as maintaining a list of the
1380Sstevel@tonic-gate  * DEFINITIONS associated with each string.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  * The list of strings is traversed at the end once the full
1410Sstevel@tonic-gate  * Str_tbl has been constructed - and string offsets can be
1420Sstevel@tonic-gate  * assigned.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static void
1450Sstevel@tonic-gate message_append(const char *defn, const char *message)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	msg_string	*msg;
1480Sstevel@tonic-gate 	if ((msg = calloc(sizeof (msg_string), 1)) == 0) {
1490Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1500Sstevel@tonic-gate 		exit(1);
1510Sstevel@tonic-gate 	}
1525549Srie 
1535549Srie 	/*
1545549Srie 	 * Initialize the string table.
1555549Srie 	 */
1565549Srie 	if ((stp == 0) && ((stp = st_new(FLG_STNEW_COMPRESS)) == NULL)) {
1575549Srie 		(void) fprintf(stderr, Errmsg_stnw, strerror(errno));
1585549Srie 		exit(1);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if ((msg->ms_defn = strdup(defn)) == 0) {
1630Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1640Sstevel@tonic-gate 		exit(1);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	if ((msg->ms_message = strdup(message)) == 0) {
1670Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
1680Sstevel@tonic-gate 		exit(1);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (st_insert(stp, msg->ms_message) == -1) {
1720Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_stin,
1730Sstevel@tonic-gate 		    message);
1740Sstevel@tonic-gate 		exit(1);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (msg_head == 0) {
1780Sstevel@tonic-gate 		msg_head = msg_tail = msg;
1790Sstevel@tonic-gate 		return;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	msg_tail->ms_next = msg;
1820Sstevel@tonic-gate 	msg_tail = msg;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Initialize a setid value.  Given a setid definition determine its numeric
1870Sstevel@tonic-gate  * value from the specified message identifier file (specified with the -i
1880Sstevel@tonic-gate  * option).  Return a pointer to the numeric string.
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate static int
1910Sstevel@tonic-gate getmesgid(char *id)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	char	*buffer, *token, *_mesgid = 0, *_setid = 0, *_domain = 0;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * If we're being asked to interpret a message id but the user didn't
1970Sstevel@tonic-gate 	 * provide the required message identifier file (-i option) we're in
1980Sstevel@tonic-gate 	 * trouble.
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	if (flmids == 0) {
2010Sstevel@tonic-gate 		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2020Sstevel@tonic-gate 		    "unable to process mesgid\n\t"
2030Sstevel@tonic-gate 		    "no message identifier file specified "
2040Sstevel@tonic-gate 		    "(see -i option)\n", fldesc, line, id);
2050Sstevel@tonic-gate 		return (1);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if ((buffer = malloc(LINE_MAX)) == 0) {
2090Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
2100Sstevel@tonic-gate 		return (1);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Read the message identifier file and locate the required mesgid.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	rewind(fdmids);
2170Sstevel@tonic-gate 	while (fgets(buffer, LINE_MAX, fdmids) != NULL) {
2180Sstevel@tonic-gate 		if ((token = strstr(buffer, id)) == NULL)
2190Sstevel@tonic-gate 			continue;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		/*
2220Sstevel@tonic-gate 		 * Establish individual strings for the mesgid, setid and domain
2230Sstevel@tonic-gate 		 * values.
2240Sstevel@tonic-gate 		 */
2250Sstevel@tonic-gate 		_mesgid = token;
2260Sstevel@tonic-gate 		while (!(isspace(*token)))
2270Sstevel@tonic-gate 			token++;
2280Sstevel@tonic-gate 		*token++ = 0;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		while (isspace(*token))
2310Sstevel@tonic-gate 			token++;
2320Sstevel@tonic-gate 		_setid = token;
2330Sstevel@tonic-gate 		while (!(isspace(*token)))
2340Sstevel@tonic-gate 			token++;
2350Sstevel@tonic-gate 		*token++ = 0;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		while (isspace(*token))
2380Sstevel@tonic-gate 			token++;
2390Sstevel@tonic-gate 		_domain = token;
2400Sstevel@tonic-gate 		while (!(isspace(*token)))
2410Sstevel@tonic-gate 			token++;
2420Sstevel@tonic-gate 		*token = 0;
2430Sstevel@tonic-gate 		break;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Did we find a match?
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	if ((_mesgid == 0) || (_setid == 0) || (_domain == 0)) {
2500Sstevel@tonic-gate 		(void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
2510Sstevel@tonic-gate 		    "unable to process mesgid\n\t"
2520Sstevel@tonic-gate 		    "identifier does not exist in file %s\n",
2530Sstevel@tonic-gate 		    fldesc, line, id, flmids);
2540Sstevel@tonic-gate 		return (1);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Have we been here before?
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if (mesgid) {
2610Sstevel@tonic-gate 		if (cflag == 1) {
2620Sstevel@tonic-gate 			/*
2630Sstevel@tonic-gate 			 * If we're being asked to process more than one mesgid
2640Sstevel@tonic-gate 			 * warn the user that only one mesgid can be used for
2650Sstevel@tonic-gate 			 * the catgets(3c) call.
2660Sstevel@tonic-gate 			 */
2670Sstevel@tonic-gate 			(void) fprintf(stderr, "sgsmsg: file %s: line %d: "
2680Sstevel@tonic-gate 			    "setid %s: warning: multiple mesgids "
2690Sstevel@tonic-gate 			    "encountered\n\t"
2700Sstevel@tonic-gate 			    "last setting used in messaging code\n",
2710Sstevel@tonic-gate 			    fldesc, line, id);
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	mesgid = _mesgid;
2760Sstevel@tonic-gate 	setid = _setid;
2770Sstevel@tonic-gate 	domain = _domain;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Generate the message file output (insure output flag is enabled).
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	if (prtmsgs != -1)
2830Sstevel@tonic-gate 		prtmsgs = 1;
2840Sstevel@tonic-gate 	if (fdmsgs && (prtmsgs == 1)) {
2850Sstevel@tonic-gate 		if (cflag == 1) {
2860Sstevel@tonic-gate 			if (fprintf(fdmsgs, "$quote \"\n$set %s\n",
2870Sstevel@tonic-gate 			    setid) < 0) {
2880Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
2890Sstevel@tonic-gate 				    strerror(errno));
2900Sstevel@tonic-gate 				return (1);
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 		} else {
2930Sstevel@tonic-gate 			if (fprintf(fdmsgs, "domain\t\"%s\"\n", domain) < 0) {
2940Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte, flmsgs,
2950Sstevel@tonic-gate 				    strerror(errno));
2960Sstevel@tonic-gate 				return (1);
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/*
3020Sstevel@tonic-gate 	 * For catgets(3c) output generate a setid definition in the message
3030Sstevel@tonic-gate 	 * definition file.
3040Sstevel@tonic-gate 	 */
3050Sstevel@tonic-gate 	if (fddefs && (cflag == 1) &&
3060Sstevel@tonic-gate 	    (fprintf(fddefs, "#define\t%s\t%s\n\n", mesgid, setid) < 0)) {
3070Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3080Sstevel@tonic-gate 		return (1);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	return (0);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Dump contents of String Table to standard out
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static void
3180Sstevel@tonic-gate dump_stringtab(Str_tbl *stp)
3190Sstevel@tonic-gate {
3205549Srie 	uint_t	cnt;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
323*5892Sab196087 		(void) printf("string table full size: %ld: uncompressed\n",
3245549Srie 		    stp->st_fullstrsize);
3250Sstevel@tonic-gate 		return;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
328*5892Sab196087 	(void) printf("string table full size: %ld compressed down to: %ld\n\n",
3295549Srie 	    stp->st_fullstrsize, stp->st_strsize);
3305549Srie 	(void) printf("string table compression information [%d buckets]:\n",
3315549Srie 	    stp->st_hbckcnt);
3320Sstevel@tonic-gate 
3335549Srie 	for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
3345549Srie 		Str_hash	*sthash = stp->st_hashbcks[cnt];
3355549Srie 
3365549Srie 		if (sthash == 0)
3375549Srie 			continue;
3385549Srie 
3395549Srie 		(void) printf(" bucket: [%d]\n", cnt);
3405549Srie 
3415549Srie 		while (sthash) {
342*5892Sab196087 			size_t	stroff = sthash->hi_mstr->sm_strlen -
3435549Srie 			    sthash->hi_strlen;
3445549Srie 
3450Sstevel@tonic-gate 			if (stroff == 0) {
346*5892Sab196087 				(void) printf("  [%ld]: '%s'  <master>\n",
3475549Srie 				    sthash->hi_refcnt, sthash->hi_mstr->sm_str);
3480Sstevel@tonic-gate 			} else {
349*5892Sab196087 				(void) printf("  [%ld]: '%s'  <suffix of: "
3505549Srie 				    "'%s'>\n", sthash->hi_refcnt,
3515549Srie 				    &sthash->hi_mstr->sm_str[stroff],
3525549Srie 				    sthash->hi_mstr->sm_str);
3530Sstevel@tonic-gate 			}
3545549Srie 			sthash = sthash->hi_next;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate }
3585549Srie 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * Initialize the message definition header file stream.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate static int
3630Sstevel@tonic-gate init_defs(void)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	static char	guard[FILENAME_MAX + 6];
3660Sstevel@tonic-gate 	char		*optr;
3670Sstevel@tonic-gate 	const char	*iptr, *_ptr;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * Establish a header guard name using the files basename.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	for (iptr = 0, _ptr = fldefs; _ptr && (*_ptr != '\0'); _ptr++) {
3730Sstevel@tonic-gate 		if (*_ptr == '/')
3740Sstevel@tonic-gate 			iptr = _ptr + 1;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	if (iptr == 0)
3770Sstevel@tonic-gate 		iptr = fldefs;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	optr = guard;
3800Sstevel@tonic-gate 	for (*optr++ = '_'; iptr && (*iptr != '\0'); iptr++, optr++) {
3810Sstevel@tonic-gate 		if (*iptr == '.') {
3820Sstevel@tonic-gate 			*optr++ = '_';
3830Sstevel@tonic-gate 			*optr++ = 'D';
3840Sstevel@tonic-gate 			*optr++ = 'O';
3850Sstevel@tonic-gate 			*optr++ = 'T';
3860Sstevel@tonic-gate 			*optr = '_';
3870Sstevel@tonic-gate 		} else
3880Sstevel@tonic-gate 			*optr = toupper(*iptr);
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (fprintf(fddefs, "#ifndef\t%s\n#define\t%s\n\n", guard, guard) < 0) {
3920Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3930Sstevel@tonic-gate 		return (1);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (fprintf(fddefs, "#ifndef\t__lint\n\n") < 0) {
3970Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
3980Sstevel@tonic-gate 		return (1);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	/*
4020Sstevel@tonic-gate 	 * add "typedef int	Msg;"
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	if (fprintf(fddefs, "typedef int\tMsg;\n\n") < 0) {
4050Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4060Sstevel@tonic-gate 		return (1);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * If the associated data array is global define a prototype.
4110Sstevel@tonic-gate 	 * Define a macro to access the array elements.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	if (lflag == 0) {
4140Sstevel@tonic-gate 		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4150Sstevel@tonic-gate 		    interface) < 0) {
4160Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
4170Sstevel@tonic-gate 			    strerror(errno));
4180Sstevel@tonic-gate 			return (1);
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	if (fprintf(fddefs, "#define\tMSG_ORIG(x)\t&__%s[x]\n\n",
4220Sstevel@tonic-gate 	    interface) < 0) {
4230Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4240Sstevel@tonic-gate 		return (1);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Generate a prototype to access the associated data array.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4310Sstevel@tonic-gate 	    interface) < 0) {
4320Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4330Sstevel@tonic-gate 		return (1);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 	if (fprintf(fddefs, "#define\tMSG_INTL(x)\t_%s(x)\n\n",
4360Sstevel@tonic-gate 	    interface) < 0) {
4370Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4380Sstevel@tonic-gate 		return (1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return (0);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate  * Finish the message definition header file.
4470Sstevel@tonic-gate  */
4480Sstevel@tonic-gate static int
4490Sstevel@tonic-gate fini_defs(void)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#else\t/* __lint */\n\n") < 0) {
4520Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4530Sstevel@tonic-gate 		return (1);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * When __lint is defined, Msg is a char *.  This allows lint to
4580Sstevel@tonic-gate 	 * check our format strings against it's arguments.
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	if (fprintf(fddefs, "\ntypedef char *\tMsg;\n\n") < 0) {
4610Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4620Sstevel@tonic-gate 		return (1);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
4660Sstevel@tonic-gate 	    interface) < 0) {
4670Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4680Sstevel@tonic-gate 		return (1);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (lflag == 0) {
4720Sstevel@tonic-gate 		if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
4730Sstevel@tonic-gate 		    interface) < 0) {
4740Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
4750Sstevel@tonic-gate 			    strerror(errno));
4760Sstevel@tonic-gate 			return (1);
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (fprintf(fddefs,
4810Sstevel@tonic-gate 	    "#define MSG_ORIG(x)\tx\n#define MSG_INTL(x)\tx\n") < 0) {
4820Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
4830Sstevel@tonic-gate 		return (1);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * Copy the temporary lint defs file into the new header.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	if (fdlint) {
4900Sstevel@tonic-gate 		long	size;
4910Sstevel@tonic-gate 		char	*buf;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		size = ftell(fdlint);
4940Sstevel@tonic-gate 		(void) rewind(fdlint);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		if ((buf = malloc(size)) == 0) {
4970Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
4980Sstevel@tonic-gate 			return (1);
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 		if (fread(buf, size, 1, fdlint) == 0) {
5010Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_read, fllint,
5020Sstevel@tonic-gate 			    strerror(errno));
5030Sstevel@tonic-gate 			return (1);
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate 		if (fwrite(buf, size, 1, fddefs) == 0) {
5060Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, fldefs,
5070Sstevel@tonic-gate 			    strerror(errno));
5080Sstevel@tonic-gate 			return (1);
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 		(void) free(buf);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#endif\t/* __lint */\n") < 0) {
5140Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5150Sstevel@tonic-gate 		return (1);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (fprintf(fddefs, "\n#endif\n") < 0) {
5190Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
5200Sstevel@tonic-gate 		return (1);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (0);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * The entire messaging file has been scanned - and all strings have been
5280Sstevel@tonic-gate  * inserted into the string_table.  We can now walk the message queue
5290Sstevel@tonic-gate  * and create the '#define <DEFN>' for each string - with the strings
5300Sstevel@tonic-gate  * assigned offset into the string_table.
5310Sstevel@tonic-gate  */
5320Sstevel@tonic-gate static int
5330Sstevel@tonic-gate output_defs(void)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	msg_string	*msg;
536*5892Sab196087 	size_t		stbufsize;
5370Sstevel@tonic-gate 	char		*stbuf;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	stbufsize = st_getstrtab_sz(stp);
5400Sstevel@tonic-gate 	if ((stbuf = malloc(stbufsize)) == 0) {
5410Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
5420Sstevel@tonic-gate 		exit(1);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	(void) st_setstrbuf(stp, stbuf, stbufsize);
5450Sstevel@tonic-gate 	for (msg = msg_head; msg; msg = msg->ms_next) {
546*5892Sab196087 		size_t	stoff;
5470Sstevel@tonic-gate 		if ((st_setstring(stp, msg->ms_message, &stoff)) == -1) {
5480Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_mnfn, msg->ms_message);
5490Sstevel@tonic-gate 			return (1);
5500Sstevel@tonic-gate 		}
551*5892Sab196087 		if (fprintf(fddefs, "\n#define\t%s\t%ld\n",
5520Sstevel@tonic-gate 		    msg->ms_defn, stoff) < 0) {
5530Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte,
5540Sstevel@tonic-gate 			    fldefs, strerror(errno));
5550Sstevel@tonic-gate 			return (1);
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		if (fddefs && fprintf(fddefs, "#define\t%s_SIZE\t%d\n",
5580Sstevel@tonic-gate 		    msg->ms_defn, strlen(msg->ms_message)) < 0) {
5590Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte,
5600Sstevel@tonic-gate 			    fldefs, strerror(errno));
5610Sstevel@tonic-gate 			return (1);
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 	return (0);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate  * Finish off the data structure definition.
5700Sstevel@tonic-gate  */
5710Sstevel@tonic-gate static int
5720Sstevel@tonic-gate output_data(void)
5730Sstevel@tonic-gate {
574*5892Sab196087 	size_t		stbufsize;
575*5892Sab196087 	size_t		ndx;
576*5892Sab196087 	size_t		column = 1;
5770Sstevel@tonic-gate 	const char	*stbuf;
5780Sstevel@tonic-gate 	const char	*fmtstr;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	stbufsize = st_getstrtab_sz(stp);
5810Sstevel@tonic-gate 	stbuf = st_getstrbuf(stp);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	assert(stbuf);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * Determine from the local flag whether the data declaration should
5870Sstevel@tonic-gate 	 * be static.
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	if (lflag)
5900Sstevel@tonic-gate 		fmtstr = (const char *)"static const";
5910Sstevel@tonic-gate 	else
5920Sstevel@tonic-gate 		fmtstr = (const char *)"const";
5930Sstevel@tonic-gate 
594*5892Sab196087 	if (fprintf(fddata, "\n%s char __%s[%ld] = { ",
5950Sstevel@tonic-gate 	    fmtstr, interface, stbufsize) < 0) {
5960Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
5970Sstevel@tonic-gate 		return (1);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	for (ndx = 0; ndx < (stbufsize - 1); ndx++) {
6010Sstevel@tonic-gate 		if (column == 1) {
6020Sstevel@tonic-gate 			if (fddata && fprintf(fddata,
603*5892Sab196087 			    "\n/* %4ld */ 0x%.2x,", ndx,
6040Sstevel@tonic-gate 			    (unsigned char)stbuf[ndx]) < 0) {
6050Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte,
6060Sstevel@tonic-gate 				    fldata, strerror(errno));
6070Sstevel@tonic-gate 				return (1);
6080Sstevel@tonic-gate 			}
6090Sstevel@tonic-gate 		} else {
6100Sstevel@tonic-gate 			if (fddata && fprintf(fddata, "  0x%.2x,",
6110Sstevel@tonic-gate 			    (unsigned char)stbuf[ndx]) < 0) {
6120Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_wrte,
6130Sstevel@tonic-gate 				    fldata, strerror(errno));
6140Sstevel@tonic-gate 				return (1);
6150Sstevel@tonic-gate 			}
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		if (column++ == 10)
6190Sstevel@tonic-gate 			column = 1;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (column == 1)
6230Sstevel@tonic-gate 		fmtstr = "\n\t0x%.2x };\n";
6240Sstevel@tonic-gate 	else
6250Sstevel@tonic-gate 		fmtstr = "  0x%.2x };\n";
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (fprintf(fddata, fmtstr, (unsigned char)stbuf[stbufsize - 1]) < 0) {
6280Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
6290Sstevel@tonic-gate 		return (1);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	return (0);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate static int
6360Sstevel@tonic-gate file()
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	char	buffer[LINE_MAX], * token;
6390Sstevel@tonic-gate 	uint_t	bufsize;
6400Sstevel@tonic-gate 	char	*token_buffer;
6410Sstevel@tonic-gate 	int	escape = 0;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if ((token_buffer = malloc(LINE_MAX)) == 0) {
6440Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_nmem, strerror(errno));
6450Sstevel@tonic-gate 		return (1);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	bufsize = LINE_MAX;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	line = 1;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	while ((token = fgets(buffer, LINE_MAX, fddesc)) != NULL) {
6520Sstevel@tonic-gate 		char	defn[PATH_MAX], * _defn, * str;
6530Sstevel@tonic-gate 		int	len;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		switch (*token) {
6560Sstevel@tonic-gate 		case '#':
6570Sstevel@tonic-gate 		case '$':
6580Sstevel@tonic-gate 			if (escape) {
6590Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
6600Sstevel@tonic-gate 				    line);
6610Sstevel@tonic-gate 				return (1);
6620Sstevel@tonic-gate 			}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 			/*
6650Sstevel@tonic-gate 			 * If a msgid has been output a msgstr must follow
6660Sstevel@tonic-gate 			 * before we digest the new token.  A msgid is only set
6670Sstevel@tonic-gate 			 * if fdmsgs is in use.
6680Sstevel@tonic-gate 			 */
6690Sstevel@tonic-gate 			if (msgid) {
6700Sstevel@tonic-gate 				msgid = 0;
6710Sstevel@tonic-gate 				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
6720Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
6730Sstevel@tonic-gate 					    flmsgs, strerror(errno));
6740Sstevel@tonic-gate 					return (1);
6750Sstevel@tonic-gate 				}
6760Sstevel@tonic-gate 			}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 			/*
6790Sstevel@tonic-gate 			 * Pass lines directly through to the output message
6800Sstevel@tonic-gate 			 * file.
6810Sstevel@tonic-gate 			 */
6820Sstevel@tonic-gate 			if (fdmsgs && (prtmsgs == 1)) {
6830Sstevel@tonic-gate 				char	comment;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 				if (cflag == 0)
6860Sstevel@tonic-gate 					comment = '#';
6870Sstevel@tonic-gate 				else
6880Sstevel@tonic-gate 					comment = '$';
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 				if (fprintf(fdmsgs, "%c%s", comment,
6910Sstevel@tonic-gate 				    ++token) < 0) {
6920Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
6930Sstevel@tonic-gate 					    flmsgs, strerror(errno));
6940Sstevel@tonic-gate 					return (1);
6950Sstevel@tonic-gate 				}
6960Sstevel@tonic-gate 			}
6970Sstevel@tonic-gate 			break;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		case '@':
7000Sstevel@tonic-gate 			if (escape) {
7010Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
7020Sstevel@tonic-gate 				    line);
7030Sstevel@tonic-gate 				return (1);
7040Sstevel@tonic-gate 			}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 			/*
7070Sstevel@tonic-gate 			 * If a msgid has been output a msgstr must follow
7080Sstevel@tonic-gate 			 * before we digest the new token.
7090Sstevel@tonic-gate 			 */
7100Sstevel@tonic-gate 			if (msgid) {
7110Sstevel@tonic-gate 				msgid = 0;
7120Sstevel@tonic-gate 				if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
7130Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
7140Sstevel@tonic-gate 					    flmsgs, strerror(errno));
7150Sstevel@tonic-gate 					return (1);
7160Sstevel@tonic-gate 				}
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 			/*
7200Sstevel@tonic-gate 			 * Determine whether we have one or more tokens.
7210Sstevel@tonic-gate 			 */
7220Sstevel@tonic-gate 			token++;
7230Sstevel@tonic-gate 			while (isspace(*token))		/* rid any whitespace */
7240Sstevel@tonic-gate 				token++;
7250Sstevel@tonic-gate 			_defn = token;			/* definition start */
7260Sstevel@tonic-gate 			while (!(isspace(*token)))
7270Sstevel@tonic-gate 				token++;
7280Sstevel@tonic-gate 			*token++ = 0;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 			while (isspace(*token))		/* rid any whitespace */
7310Sstevel@tonic-gate 				token++;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 			/*
7340Sstevel@tonic-gate 			 * Determine whether the single token is one of the
7350Sstevel@tonic-gate 			 * reserved message output delimiters otherwise
7360Sstevel@tonic-gate 			 * translate it as a message identifier.
7370Sstevel@tonic-gate 			 */
7380Sstevel@tonic-gate 			if (*token == 0) {
7390Sstevel@tonic-gate 				if (strcmp(_defn, start) == 0)
7400Sstevel@tonic-gate 					prtmsgs = 1;
7410Sstevel@tonic-gate 				else if (strcmp(_defn, end) == 0)
7420Sstevel@tonic-gate 					prtmsgs = -1;
7430Sstevel@tonic-gate 				else if (getmesgid(_defn) == 1)
7440Sstevel@tonic-gate 					return (1);
7450Sstevel@tonic-gate 				break;
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 			/*
7490Sstevel@tonic-gate 			 * Multiple tokens are translated by taking the first
7500Sstevel@tonic-gate 			 * token as the message definition, and the rest of the
7510Sstevel@tonic-gate 			 * line as the message itself.  A message line ending
7520Sstevel@tonic-gate 			 * with an escape ('\') is expected to be continued on
7530Sstevel@tonic-gate 			 * the next line.
7540Sstevel@tonic-gate 			 */
7550Sstevel@tonic-gate 			if (prtmsgs != -1)
7560Sstevel@tonic-gate 				prtmsgs = 1;
7570Sstevel@tonic-gate 			if (fdmsgs && (prtmsgs == 1)) {
7580Sstevel@tonic-gate 				/*
7590Sstevel@tonic-gate 				 * For catgets(3c) make sure a message
7600Sstevel@tonic-gate 				 * identifier has been established (this is
7610Sstevel@tonic-gate 				 * normally a domain for gettext(3i), but for
7620Sstevel@tonic-gate 				 * sgsmsg use this could be argued as being
7630Sstevel@tonic-gate 				 * redundent).  Also make sure that the message
7640Sstevel@tonic-gate 				 * definitions haven't exceeeded the maximum
7650Sstevel@tonic-gate 				 * value allowed by gencat(1) before generating
7660Sstevel@tonic-gate 				 * any message file entries.
7670Sstevel@tonic-gate 				 */
7680Sstevel@tonic-gate 				if (cflag == 1) {
7690Sstevel@tonic-gate 					if (setid == 0) {
7700Sstevel@tonic-gate 						(void) fprintf(stderr, "file "
7710Sstevel@tonic-gate 						    "%s: no message identifier "
7720Sstevel@tonic-gate 						    "has been established\n",
7730Sstevel@tonic-gate 						    fldesc);
7740Sstevel@tonic-gate 						return (1);
7750Sstevel@tonic-gate 					}
7760Sstevel@tonic-gate 					if (ptr	> NL_MSGMAX) {
7770Sstevel@tonic-gate 						(void) fprintf(stderr, "file "
7780Sstevel@tonic-gate 						    "%s: message definition "
7790Sstevel@tonic-gate 						    "(%d) exceeds allowable "
7800Sstevel@tonic-gate 						    "limit (NL_MSGMAX)\n",
7810Sstevel@tonic-gate 						    fldesc, ptr);
7820Sstevel@tonic-gate 						return (1);
7830Sstevel@tonic-gate 					}
7840Sstevel@tonic-gate 				}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 				/*
7870Sstevel@tonic-gate 				 * For catgets(3c) write the definition and the
7880Sstevel@tonic-gate 				 * message string to the message file.  For
7890Sstevel@tonic-gate 				 * gettext(3i) write the message string as a
7900Sstevel@tonic-gate 				 * mesgid - indicate a mesgid has been output
7910Sstevel@tonic-gate 				 * so that a msgstr can follow.
7920Sstevel@tonic-gate 				 */
7930Sstevel@tonic-gate 				if (cflag == 1) {
7940Sstevel@tonic-gate 					if (fprintf(fdmsgs, "%d\t%s", ptr,
7950Sstevel@tonic-gate 					    token) < 0) {
7960Sstevel@tonic-gate 						(void) fprintf(stderr,
7970Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
7980Sstevel@tonic-gate 						    strerror(errno));
7990Sstevel@tonic-gate 						return (1);
8000Sstevel@tonic-gate 					}
8010Sstevel@tonic-gate 				} else {
8020Sstevel@tonic-gate 					if (fprintf(fdmsgs, "msgid\t\"") < 0) {
8030Sstevel@tonic-gate 						(void) fprintf(stderr,
8040Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
8050Sstevel@tonic-gate 						    strerror(errno));
8060Sstevel@tonic-gate 						return (1);
8070Sstevel@tonic-gate 					}
8080Sstevel@tonic-gate 					msgid = 1;
8090Sstevel@tonic-gate 				}
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 			/*
8130Sstevel@tonic-gate 			 * The message itself is a quoted string as this makes
8140Sstevel@tonic-gate 			 * embedding spaces at the start (or the end) of the
8150Sstevel@tonic-gate 			 * string very easy.
8160Sstevel@tonic-gate 			 */
8170Sstevel@tonic-gate 			if (*token != '"') {
8180Sstevel@tonic-gate 				(void) fprintf(stderr, Errmsg_malt, fldesc,
8190Sstevel@tonic-gate 				    line);
8200Sstevel@tonic-gate 				return (1);
8210Sstevel@tonic-gate 			}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 			(void) strcpy(defn, _defn);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 			/*
8260Sstevel@tonic-gate 			 * Write the tag to the lint definitions.
8270Sstevel@tonic-gate 			 */
8280Sstevel@tonic-gate 			if (fdlint) {
8290Sstevel@tonic-gate 				if (fprintf(fdlint, "\n#define\t%s\t",
8300Sstevel@tonic-gate 				    _defn) < 0) {
8310Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
8320Sstevel@tonic-gate 					    fllint, strerror(errno));
8330Sstevel@tonic-gate 					return (1);
8340Sstevel@tonic-gate 				}
8350Sstevel@tonic-gate 			}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 			len = 0;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 			/*
8400Sstevel@tonic-gate 			 * Write each character of the message string to the
8410Sstevel@tonic-gate 			 * data array.  Translate any escaped characters - use
8420Sstevel@tonic-gate 			 * the same specially recognized characters as defined
8430Sstevel@tonic-gate 			 * by gencat(1).
8440Sstevel@tonic-gate 			 */
8450Sstevel@tonic-gate message:
8460Sstevel@tonic-gate 			if (*token == '"') {
8470Sstevel@tonic-gate 				if (fdlint &&
8480Sstevel@tonic-gate 				    (fprintf(fdlint, "%c", *token) < 0)) {
8490Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
8500Sstevel@tonic-gate 					    fllint, strerror(errno));
8510Sstevel@tonic-gate 					return (1);
8520Sstevel@tonic-gate 				}
8530Sstevel@tonic-gate 				token++;
8540Sstevel@tonic-gate 			}
8550Sstevel@tonic-gate 			while (*token) {
8560Sstevel@tonic-gate 				char	_token;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 				if ((*token == '\\') && (escape == 0)) {
8590Sstevel@tonic-gate 					escape = 1;
8600Sstevel@tonic-gate 					if (fdlint && (*(token + 1) != '\n') &&
8610Sstevel@tonic-gate 					    fprintf(fdlint, "%c", *token) < 0) {
8620Sstevel@tonic-gate 						(void) fprintf(stderr,
8630Sstevel@tonic-gate 						    Errmsg_wrte, fllint,
8640Sstevel@tonic-gate 						    strerror(errno));
8650Sstevel@tonic-gate 						return (1);
8660Sstevel@tonic-gate 					}
8670Sstevel@tonic-gate 					token++;
8680Sstevel@tonic-gate 					continue;
8690Sstevel@tonic-gate 				}
8700Sstevel@tonic-gate 				if (escape) {
8710Sstevel@tonic-gate 					if (*token == 'n')
8720Sstevel@tonic-gate 						_token = '\n';
8730Sstevel@tonic-gate 					else if (*token == 't')
8740Sstevel@tonic-gate 						_token = '\t';
8750Sstevel@tonic-gate 					else if (*token == 'v')
8760Sstevel@tonic-gate 						_token = '\v';
8770Sstevel@tonic-gate 					else if (*token == 'b')
8780Sstevel@tonic-gate 						_token = '\b';
8790Sstevel@tonic-gate 					else if (*token == 'f')
8800Sstevel@tonic-gate 						_token = '\f';
8810Sstevel@tonic-gate 					else if (*token == '\\')
8820Sstevel@tonic-gate 						_token = '\\';
8830Sstevel@tonic-gate 					else if (*token == '"')
8840Sstevel@tonic-gate 						_token = '"';
8850Sstevel@tonic-gate 					else if (*token == '\n')
8860Sstevel@tonic-gate 						break;
8870Sstevel@tonic-gate 					else
8880Sstevel@tonic-gate 						_token = *token;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 					if (fdmsgs && (prtmsgs == 1) &&
8910Sstevel@tonic-gate 					    (fprintf(fdmsgs, "\\") < 0)) {
8920Sstevel@tonic-gate 						(void) fprintf(stderr,
8930Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
8940Sstevel@tonic-gate 						    strerror(errno));
8950Sstevel@tonic-gate 						return (1);
8960Sstevel@tonic-gate 					}
8970Sstevel@tonic-gate 				} else {
8980Sstevel@tonic-gate 					/*
8990Sstevel@tonic-gate 					 * If this is the trailing quote then
9000Sstevel@tonic-gate 					 * thats the last of the message string.
9010Sstevel@tonic-gate 					 * Eat up any remaining white space and
9020Sstevel@tonic-gate 					 * unless an escape character is found
9030Sstevel@tonic-gate 					 * terminate the data string with a 0.
9040Sstevel@tonic-gate 					 */
9055549Srie 					/* BEGIN CSTYLED */
9060Sstevel@tonic-gate 					if (*token == '"') {
9070Sstevel@tonic-gate 					    if (fdlint && (fprintf(fdlint,
9080Sstevel@tonic-gate 						"%c", *token) < 0)) {
9090Sstevel@tonic-gate 						(void) fprintf(stderr,
9100Sstevel@tonic-gate 						    Errmsg_wrte, fllint,
9110Sstevel@tonic-gate 						    strerror(errno));
9120Sstevel@tonic-gate 						return (1);
9130Sstevel@tonic-gate 					    }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 					    if (fdmsgs && (prtmsgs == 1) &&
9160Sstevel@tonic-gate 						(fprintf(fdmsgs, "%c",
9170Sstevel@tonic-gate 						*token) < 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 
9240Sstevel@tonic-gate 					    while (*++token) {
9250Sstevel@tonic-gate 						if (*token == '\n')
9260Sstevel@tonic-gate 							break;
9270Sstevel@tonic-gate 					    }
9280Sstevel@tonic-gate 					    _token = '\0';
9290Sstevel@tonic-gate 					} else
9300Sstevel@tonic-gate 					    _token = *token;
9315549Srie 					/* END CSTYLED */
9320Sstevel@tonic-gate 				}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 				if (fdmsgs && (prtmsgs == 1) &&
9350Sstevel@tonic-gate 				    (fprintf(fdmsgs, "%c", *token) < 0)) {
9360Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
9370Sstevel@tonic-gate 					    flmsgs, strerror(errno));
9380Sstevel@tonic-gate 					return (1);
9390Sstevel@tonic-gate 				}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 				if (fdlint && fprintf(fdlint,
9420Sstevel@tonic-gate 				    "%c", *token) < 0) {
9430Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
9440Sstevel@tonic-gate 					    fllint, strerror(errno));
9450Sstevel@tonic-gate 					return (1);
9460Sstevel@tonic-gate 				}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 				if (len >= bufsize) {
9490Sstevel@tonic-gate 					bufsize += LINE_MAX;
9500Sstevel@tonic-gate 					if ((token_buffer = realloc(
9510Sstevel@tonic-gate 					    token_buffer, bufsize)) == 0) {
9520Sstevel@tonic-gate 						(void) fprintf(stderr,
9530Sstevel@tonic-gate 						    Errmsg_nmem,
9540Sstevel@tonic-gate 						    strerror(errno));
9550Sstevel@tonic-gate 						return (1);
9560Sstevel@tonic-gate 					}
9570Sstevel@tonic-gate 				}
9580Sstevel@tonic-gate 				token_buffer[len] = _token;
9590Sstevel@tonic-gate 				ptr++, token++, len++;
9600Sstevel@tonic-gate 				escape = 0;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 				if (_token == '\0')
9630Sstevel@tonic-gate 					break;
9640Sstevel@tonic-gate 			}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 			/*
9670Sstevel@tonic-gate 			 * After the complete message string has been processed
9680Sstevel@tonic-gate 			 * (including its continuation beyond one line), create
9690Sstevel@tonic-gate 			 * a string size definition.
9700Sstevel@tonic-gate 			 */
9710Sstevel@tonic-gate 			if (escape == 0) {
9720Sstevel@tonic-gate 				const char *form = "#define\t%s_SIZE\t%d\n";
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 				token_buffer[len] = '\0';
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 				message_append(defn, token_buffer);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 				if (fdlint && fprintf(fdlint, form, defn,
9790Sstevel@tonic-gate 				    (len - 1)) < 0) {
9800Sstevel@tonic-gate 					(void) fprintf(stderr, Errmsg_wrte,
9810Sstevel@tonic-gate 					    fllint, strerror(errno));
9820Sstevel@tonic-gate 					return (1);
9830Sstevel@tonic-gate 				}
9840Sstevel@tonic-gate 			}
9850Sstevel@tonic-gate 			break;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		default:
9880Sstevel@tonic-gate 			/*
9890Sstevel@tonic-gate 			 * Empty lines are passed through to the message file.
9900Sstevel@tonic-gate 			 */
9910Sstevel@tonic-gate 			while (isspace(*token))
9920Sstevel@tonic-gate 				token++;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 			if (*token == 0) {
9950Sstevel@tonic-gate 				if (msgid || (fdmsgs && (prtmsgs == 1))) {
9960Sstevel@tonic-gate 					/*
9970Sstevel@tonic-gate 					 * If a msgid has been output a msgstr
9980Sstevel@tonic-gate 					 * must follow before we digest the new
9990Sstevel@tonic-gate 					 * token.
10000Sstevel@tonic-gate 					 */
10010Sstevel@tonic-gate 					if (msgid) {
10020Sstevel@tonic-gate 						msgid = 0;
10030Sstevel@tonic-gate 						str = "msgstr\t\"\"\n\n";
10040Sstevel@tonic-gate 					} else
10050Sstevel@tonic-gate 						str = "\n";
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 					if (fprintf(fdmsgs, str) < 0) {
10080Sstevel@tonic-gate 						(void) fprintf(stderr,
10090Sstevel@tonic-gate 						    Errmsg_wrte, flmsgs,
10100Sstevel@tonic-gate 						    strerror(errno));
10110Sstevel@tonic-gate 						return (1);
10120Sstevel@tonic-gate 					}
10130Sstevel@tonic-gate 				}
10140Sstevel@tonic-gate 				break;
10150Sstevel@tonic-gate 			}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 			/*
10180Sstevel@tonic-gate 			 * If an escape is in effect then any tokens are taken
10190Sstevel@tonic-gate 			 * to be message continuations.
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 			if (escape) {
10220Sstevel@tonic-gate 				escape = 0;
10230Sstevel@tonic-gate 				goto message;
10240Sstevel@tonic-gate 			}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 			(void) fprintf(stderr, "file %s: line %d: invalid "
10270Sstevel@tonic-gate 			    "input does not start with #, $ or @\n", fldesc,
10280Sstevel@tonic-gate 			    line);
10290Sstevel@tonic-gate 			return (1);
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 		line++;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	free(token_buffer);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	return (0);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate int
10400Sstevel@tonic-gate main(int argc, char ** argv)
10410Sstevel@tonic-gate {
10420Sstevel@tonic-gate 	opterr = 0;
10430Sstevel@tonic-gate 	while ((line = getopt(argc, argv, "cd:h:lm:n:i:v")) != EOF) {
10440Sstevel@tonic-gate 		switch (line) {
10450Sstevel@tonic-gate 		case 'c':			/* catgets instead of gettext */
10460Sstevel@tonic-gate 			cflag = 1;
10470Sstevel@tonic-gate 			break;
10480Sstevel@tonic-gate 		case 'd':			/* new message data filename */
10490Sstevel@tonic-gate 			fldata = optarg;	/*	(msg.c is default) */
10500Sstevel@tonic-gate 			break;
10510Sstevel@tonic-gate 		case 'h':			/* new message defs filename */
10520Sstevel@tonic-gate 			fldefs = optarg;	/*	(msg.h is default) */
10530Sstevel@tonic-gate 			break;
10540Sstevel@tonic-gate 		case 'i':			/* input message ids from */
10550Sstevel@tonic-gate 			flmids = optarg;	/*	from this file */
10560Sstevel@tonic-gate 			break;
10570Sstevel@tonic-gate 		case 'l':			/* define message data arrays */
10580Sstevel@tonic-gate 			lflag = 1;		/*	to be local (static) */
10590Sstevel@tonic-gate 			break;
10600Sstevel@tonic-gate 		case 'm':			/* generate message database */
10610Sstevel@tonic-gate 			flmsgs = optarg;	/*	to this file */
10620Sstevel@tonic-gate 			break;
10630Sstevel@tonic-gate 		case 'n':			/* new data array and func */
10640Sstevel@tonic-gate 			interface = optarg;	/*	name (msg is default) */
10650Sstevel@tonic-gate 			break;
10660Sstevel@tonic-gate 		case 'v':
10670Sstevel@tonic-gate 			vflag = 1;		/* set verbose flag */
10680Sstevel@tonic-gate 			break;
10690Sstevel@tonic-gate 		case '?':
10700Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_use, argv[0]);
10710Sstevel@tonic-gate 			exit(1);
10720Sstevel@tonic-gate 		default:
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/*
10780Sstevel@tonic-gate 	 * Validate the we have been given at least one input file.
10790Sstevel@tonic-gate 	 */
10800Sstevel@tonic-gate 	if ((argc - optind) < 1) {
10810Sstevel@tonic-gate 		(void) fprintf(stderr, Errmsg_use);
10820Sstevel@tonic-gate 		exit(1);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * Open all the required output files.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	if (fldefs) {
10890Sstevel@tonic-gate 		if ((fddefs = fopen(fldefs, "w+")) == NULL) {
10900Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldefs,
10910Sstevel@tonic-gate 			    strerror(errno));
10920Sstevel@tonic-gate 			return (1);
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	if (fldata) {
10960Sstevel@tonic-gate 		if (fldefs && (strcmp(fldefs, fldata) == 0))
10970Sstevel@tonic-gate 			fddata = fddefs;
10980Sstevel@tonic-gate 		else if ((fddata = fopen(fldata, "w+")) == NULL) {
10990Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldata,
11000Sstevel@tonic-gate 			    strerror(errno));
11010Sstevel@tonic-gate 			return (1);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 	if (fddefs && fddata) {
11050Sstevel@tonic-gate 		(void) sprintf(fllint, "%s.%d", nmlint, (int)getpid());
11060Sstevel@tonic-gate 		if ((fdlint = fopen(fllint, "w+")) == NULL) {
11070Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fllint,
11080Sstevel@tonic-gate 			    strerror(errno));
11090Sstevel@tonic-gate 			return (1);
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate 	if (flmsgs) {
11130Sstevel@tonic-gate 		if ((fdmsgs = fopen(flmsgs, "w+")) == NULL) {
11140Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, flmsgs,
11150Sstevel@tonic-gate 			    strerror(errno));
11160Sstevel@tonic-gate 			return (1);
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 	if (flmids) {
11200Sstevel@tonic-gate 		if ((fdmids = fopen(flmids, "r")) == NULL) {
11210Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, flmids,
11220Sstevel@tonic-gate 			    strerror(errno));
11230Sstevel@tonic-gate 			return (1);
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Initialize the message definition and message data streams.
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	if (fddefs) {
11320Sstevel@tonic-gate 		if (init_defs())
11330Sstevel@tonic-gate 			return (1);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/*
11370Sstevel@tonic-gate 	 * Read the input message file, and for each line process accordingly.
11380Sstevel@tonic-gate 	 */
11390Sstevel@tonic-gate 	for (; optind < argc; optind++) {
11400Sstevel@tonic-gate 		int	err;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		fldesc = argv[optind];
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 		if ((fddesc = fopen(fldesc, "r")) == NULL) {
11450Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_opne, fldesc,
11460Sstevel@tonic-gate 			    strerror(errno));
11470Sstevel@tonic-gate 			return (1);
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 		err = file();
11500Sstevel@tonic-gate 		(void) fclose(fddesc);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		if (err != 0)
11530Sstevel@tonic-gate 			return (1);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/*
11570Sstevel@tonic-gate 	 * If a msgid has been output a msgstr must follow before we end the
11580Sstevel@tonic-gate 	 * file.
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	if (msgid) {
11610Sstevel@tonic-gate 		msgid = 0;
11620Sstevel@tonic-gate 		if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
11630Sstevel@tonic-gate 			(void) fprintf(stderr, Errmsg_wrte, flmsgs,
11640Sstevel@tonic-gate 			    strerror(errno));
11650Sstevel@tonic-gate 			return (1);
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if (fdmids)
11700Sstevel@tonic-gate 		(void) fclose(fdmids);
11710Sstevel@tonic-gate 	if (fdmsgs)
11720Sstevel@tonic-gate 		(void) fclose(fdmsgs);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if (fddefs) {
11750Sstevel@tonic-gate 		if (output_defs())
11760Sstevel@tonic-gate 			return (1);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/*
11800Sstevel@tonic-gate 	 * Finish off any generated data and header file.
11810Sstevel@tonic-gate 	 */
11820Sstevel@tonic-gate 	if (fldata) {
11830Sstevel@tonic-gate 		if (output_data())
11840Sstevel@tonic-gate 			return (1);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 	if (fddefs) {
11870Sstevel@tonic-gate 		if (fini_defs())
11880Sstevel@tonic-gate 			return (1);
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (vflag)
11920Sstevel@tonic-gate 		dump_stringtab(stp);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * Close up everything and go home.
11960Sstevel@tonic-gate 	 */
11970Sstevel@tonic-gate 	if (fddata)
11980Sstevel@tonic-gate 		(void) fclose(fddata);
11990Sstevel@tonic-gate 	if (fddefs && (fddefs != fddata))
12000Sstevel@tonic-gate 		(void) fclose(fddefs);
12010Sstevel@tonic-gate 	if (fddefs && fddata) {
12020Sstevel@tonic-gate 		(void) fclose(fdlint);
12030Sstevel@tonic-gate 		(void) unlink(fllint);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	if (stp)
12070Sstevel@tonic-gate 		st_destroy(stp);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	return (0);
12100Sstevel@tonic-gate }
1211