xref: /onnv-gate/usr/src/cmd/fm/eversholt/common/eftwrite.c (revision 11050:be69f645ce17)
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
52538Sesaxe  * Common Development and Distribution License (the "License").
62538Sesaxe  * 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*11050SRobert.Johnston@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * eftwrite.c -- routines for writing .eft files
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * this module emits the table resulting from compilation of the
280Sstevel@tonic-gate  * source files.  this code done nothing unless the -o option
290Sstevel@tonic-gate  * was given on the command line.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include "out.h"
380Sstevel@tonic-gate #include "stats.h"
390Sstevel@tonic-gate #include "stable.h"
400Sstevel@tonic-gate #include "lut.h"
410Sstevel@tonic-gate #include "tree.h"
420Sstevel@tonic-gate #include "eft.h"
430Sstevel@tonic-gate #include "eftwrite.h"
440Sstevel@tonic-gate #include "esclex.h"
450Sstevel@tonic-gate #include "version.h"
460Sstevel@tonic-gate #include "ptree.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* for uintX_t, htonl(), etc */
490Sstevel@tonic-gate #include <sys/types.h>
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <inttypes.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate extern char Args[];
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static struct stats *Outbytes;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static int Identlen;
580Sstevel@tonic-gate static int Dictlen;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate void
eftwrite_init(void)610Sstevel@tonic-gate eftwrite_init(void)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*ARGSUSED*/
670Sstevel@tonic-gate static void
ident_lencalc(const char * s,void * rhs,void * arg)680Sstevel@tonic-gate ident_lencalc(const char *s, void *rhs, void *arg)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	Identlen += strlen(s) + 1;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*ARGSUSED*/
740Sstevel@tonic-gate static void
dict_lencalc(const char * s,void * rhs,void * arg)750Sstevel@tonic-gate dict_lencalc(const char *s, void *rhs, void *arg)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	Dictlen += strlen(s) + 1;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*ARGSUSED*/
810Sstevel@tonic-gate static void
ident_printer(const char * s,void * rhs,void * arg)820Sstevel@tonic-gate ident_printer(const char *s, void *rhs, void *arg)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	FILE *fp = (FILE *)arg;
850Sstevel@tonic-gate 
86*11050SRobert.Johnston@Sun.COM 	(void) fwrite(s, strlen(s) + 1, 1, fp);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*ARGSUSED*/
900Sstevel@tonic-gate static void
dict_printer(const char * s,void * rhs,void * arg)910Sstevel@tonic-gate dict_printer(const char *s, void *rhs, void *arg)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	FILE *fp = (FILE *)arg;
940Sstevel@tonic-gate 
95*11050SRobert.Johnston@Sun.COM 	(void) fwrite(s, strlen(s) + 1, 1, fp);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate void
eftwrite(const char * fname)990Sstevel@tonic-gate eftwrite(const char *fname)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	FILE *fp;
1020Sstevel@tonic-gate 	FILE *tfp;
1030Sstevel@tonic-gate 	struct eftheader hdr;
1040Sstevel@tonic-gate #define	BUFLEN	8192
1050Sstevel@tonic-gate 	char buf[BUFLEN];
1060Sstevel@tonic-gate 	int cc;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if ((tfp = tmpfile()) == NULL)
1090Sstevel@tonic-gate 		out(O_DIE|O_SYS, "cannot create temporary file");
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/* XXX switch stdout to tfp temporarily */
1120Sstevel@tonic-gate 	/* XXX for now */
1130Sstevel@tonic-gate 	out_altfp(tfp);
1140Sstevel@tonic-gate 	ptree(O_ALTFP, tree_root(NULL), 0, 1);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	rewind(tfp);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	lut_walk(Ident, (lut_cb)ident_lencalc, (void *)0);
1190Sstevel@tonic-gate 	lut_walk(Dicts, (lut_cb)dict_lencalc, (void *)0);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	bzero(&hdr, sizeof (hdr));
1220Sstevel@tonic-gate 	hdr.magic = EFT_HDR_MAGIC;
1230Sstevel@tonic-gate 	hdr.major = EFT_HDR_MAJOR;
1240Sstevel@tonic-gate 	hdr.minor = EFT_HDR_MINOR;
1250Sstevel@tonic-gate 	hdr.cmajor = VERSION_MAJOR;
1260Sstevel@tonic-gate 	hdr.cminor = VERSION_MINOR;
1270Sstevel@tonic-gate 	hdr.identlen = Identlen;
1280Sstevel@tonic-gate 	hdr.dictlen = Dictlen;
1290Sstevel@tonic-gate 	buf[BUFLEN - 1] = '\0';
1305609Scy152378 
1315609Scy152378 #ifdef DEBUG
1320Sstevel@tonic-gate 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
1332538Sesaxe 	    "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR,
1342538Sesaxe 	    VERSION_MINOR, Args);
1355609Scy152378 #else
1365609Scy152378 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
1375609Scy152378 	    "Built using esc-%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
1385609Scy152378 #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((fp = fopen(fname, "w")) == NULL)
1410Sstevel@tonic-gate 		out(O_DIE|O_SYS, "can't open output file: %s", fname);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
1440Sstevel@tonic-gate 		char *ptr;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		for (ptr = buf; ptr < &buf[cc]; ptr++)
1470Sstevel@tonic-gate 			hdr.csum += (uint32_t)*ptr;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	if (ferror(tfp))
1500Sstevel@tonic-gate 		out(O_DIE|O_SYS, "fread on tmpfile");
1510Sstevel@tonic-gate 	rewind(tfp);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	hdr.magic = htonl(hdr.magic);
1540Sstevel@tonic-gate 	hdr.major = htons(hdr.major);
1550Sstevel@tonic-gate 	hdr.minor = htons(hdr.minor);
1560Sstevel@tonic-gate 	hdr.cmajor = htons(hdr.cmajor);
1570Sstevel@tonic-gate 	hdr.cminor = htons(hdr.cminor);
1580Sstevel@tonic-gate 	hdr.identlen = htonl(hdr.identlen);
1590Sstevel@tonic-gate 	hdr.dictlen = htonl(hdr.dictlen);
1600Sstevel@tonic-gate 	hdr.csum = htonl(hdr.csum);
1610Sstevel@tonic-gate 
162*11050SRobert.Johnston@Sun.COM 	(void) fwrite(&hdr, sizeof (hdr), 1, fp);
1630Sstevel@tonic-gate 	if (ferror(fp))
1640Sstevel@tonic-gate 		out(O_DIE|O_SYS, "%s: can't write header", fname);
1650Sstevel@tonic-gate 	stats_counter_add(Outbytes, sizeof (hdr));
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	lut_walk(Ident, (lut_cb)ident_printer, (void *)fp);
1680Sstevel@tonic-gate 	stats_counter_add(Outbytes, Identlen);
1690Sstevel@tonic-gate 	lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp);
1700Sstevel@tonic-gate 	stats_counter_add(Outbytes, Dictlen);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
1730Sstevel@tonic-gate 		char *ptr;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		for (ptr = buf; ptr < &buf[cc]; ptr++)
1760Sstevel@tonic-gate 			*ptr = ~((unsigned char)*ptr);
1770Sstevel@tonic-gate 		if (cc != fwrite(buf, 1, cc, fp) || ferror(fp))
1780Sstevel@tonic-gate 			out(O_DIE|O_SYS, "fwrite on %s", fname);
1790Sstevel@tonic-gate 		stats_counter_add(Outbytes, cc);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	if (ferror(tfp))
1820Sstevel@tonic-gate 		out(O_DIE|O_SYS, "fread on tmpfile");
183*11050SRobert.Johnston@Sun.COM 	(void) fclose(tfp);
184*11050SRobert.Johnston@Sun.COM 	(void) fclose(fp);
1850Sstevel@tonic-gate }
186