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 5*2538Sesaxe * Common Development and Distribution License (the "License"). 6*2538Sesaxe * 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*2538Sesaxe * Copyright 2006 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 #pragma ident "%Z%%M% %I% %E% SMI" 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #include <string.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <unistd.h> 380Sstevel@tonic-gate #include <errno.h> 390Sstevel@tonic-gate #include "out.h" 400Sstevel@tonic-gate #include "stats.h" 410Sstevel@tonic-gate #include "stable.h" 420Sstevel@tonic-gate #include "lut.h" 430Sstevel@tonic-gate #include "tree.h" 440Sstevel@tonic-gate #include "eft.h" 450Sstevel@tonic-gate #include "eftwrite.h" 460Sstevel@tonic-gate #include "esclex.h" 470Sstevel@tonic-gate #include "version.h" 480Sstevel@tonic-gate #include "ptree.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* for uintX_t, htonl(), etc */ 510Sstevel@tonic-gate #include <sys/types.h> 520Sstevel@tonic-gate #include <netinet/in.h> 530Sstevel@tonic-gate #include <inttypes.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate extern char Args[]; 560Sstevel@tonic-gate 570Sstevel@tonic-gate static struct stats *Outbytes; 580Sstevel@tonic-gate 590Sstevel@tonic-gate static int Identlen; 600Sstevel@tonic-gate static int Dictlen; 610Sstevel@tonic-gate 620Sstevel@tonic-gate void 630Sstevel@tonic-gate eftwrite_init(void) 640Sstevel@tonic-gate { 650Sstevel@tonic-gate Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1); 660Sstevel@tonic-gate } 670Sstevel@tonic-gate 680Sstevel@tonic-gate /*ARGSUSED*/ 690Sstevel@tonic-gate static void 700Sstevel@tonic-gate ident_lencalc(const char *s, void *rhs, void *arg) 710Sstevel@tonic-gate { 720Sstevel@tonic-gate Identlen += strlen(s) + 1; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate /*ARGSUSED*/ 760Sstevel@tonic-gate static void 770Sstevel@tonic-gate dict_lencalc(const char *s, void *rhs, void *arg) 780Sstevel@tonic-gate { 790Sstevel@tonic-gate Dictlen += strlen(s) + 1; 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate /*ARGSUSED*/ 830Sstevel@tonic-gate static void 840Sstevel@tonic-gate ident_printer(const char *s, void *rhs, void *arg) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate FILE *fp = (FILE *)arg; 870Sstevel@tonic-gate 880Sstevel@tonic-gate fwrite(s, strlen(s) + 1, 1, fp); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate /*ARGSUSED*/ 920Sstevel@tonic-gate static void 930Sstevel@tonic-gate dict_printer(const char *s, void *rhs, void *arg) 940Sstevel@tonic-gate { 950Sstevel@tonic-gate FILE *fp = (FILE *)arg; 960Sstevel@tonic-gate 970Sstevel@tonic-gate fwrite(s, strlen(s) + 1, 1, fp); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 1000Sstevel@tonic-gate void 1010Sstevel@tonic-gate eftwrite(const char *fname) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate FILE *fp; 1040Sstevel@tonic-gate FILE *tfp; 1050Sstevel@tonic-gate struct eftheader hdr; 1060Sstevel@tonic-gate #define BUFLEN 8192 1070Sstevel@tonic-gate char buf[BUFLEN]; 1080Sstevel@tonic-gate int cc; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate if ((tfp = tmpfile()) == NULL) 1110Sstevel@tonic-gate out(O_DIE|O_SYS, "cannot create temporary file"); 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* XXX switch stdout to tfp temporarily */ 1140Sstevel@tonic-gate /* XXX for now */ 1150Sstevel@tonic-gate out_altfp(tfp); 1160Sstevel@tonic-gate ptree(O_ALTFP, tree_root(NULL), 0, 1); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate rewind(tfp); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate lut_walk(Ident, (lut_cb)ident_lencalc, (void *)0); 1210Sstevel@tonic-gate lut_walk(Dicts, (lut_cb)dict_lencalc, (void *)0); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate bzero(&hdr, sizeof (hdr)); 1240Sstevel@tonic-gate hdr.magic = EFT_HDR_MAGIC; 1250Sstevel@tonic-gate hdr.major = EFT_HDR_MAJOR; 1260Sstevel@tonic-gate hdr.minor = EFT_HDR_MINOR; 1270Sstevel@tonic-gate hdr.cmajor = VERSION_MAJOR; 1280Sstevel@tonic-gate hdr.cminor = VERSION_MINOR; 1290Sstevel@tonic-gate hdr.identlen = Identlen; 1300Sstevel@tonic-gate hdr.dictlen = Dictlen; 1310Sstevel@tonic-gate buf[BUFLEN - 1] = '\0'; 1320Sstevel@tonic-gate (void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT, 133*2538Sesaxe "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR, 134*2538Sesaxe VERSION_MINOR, Args); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate if ((fp = fopen(fname, "w")) == NULL) 1370Sstevel@tonic-gate out(O_DIE|O_SYS, "can't open output file: %s", fname); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) { 1400Sstevel@tonic-gate char *ptr; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate for (ptr = buf; ptr < &buf[cc]; ptr++) 1430Sstevel@tonic-gate hdr.csum += (uint32_t)*ptr; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate if (ferror(tfp)) 1460Sstevel@tonic-gate out(O_DIE|O_SYS, "fread on tmpfile"); 1470Sstevel@tonic-gate rewind(tfp); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate hdr.magic = htonl(hdr.magic); 1500Sstevel@tonic-gate hdr.major = htons(hdr.major); 1510Sstevel@tonic-gate hdr.minor = htons(hdr.minor); 1520Sstevel@tonic-gate hdr.cmajor = htons(hdr.cmajor); 1530Sstevel@tonic-gate hdr.cminor = htons(hdr.cminor); 1540Sstevel@tonic-gate hdr.identlen = htonl(hdr.identlen); 1550Sstevel@tonic-gate hdr.dictlen = htonl(hdr.dictlen); 1560Sstevel@tonic-gate hdr.csum = htonl(hdr.csum); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate fwrite(&hdr, sizeof (hdr), 1, fp); 1590Sstevel@tonic-gate if (ferror(fp)) 1600Sstevel@tonic-gate out(O_DIE|O_SYS, "%s: can't write header", fname); 1610Sstevel@tonic-gate stats_counter_add(Outbytes, sizeof (hdr)); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate lut_walk(Ident, (lut_cb)ident_printer, (void *)fp); 1640Sstevel@tonic-gate stats_counter_add(Outbytes, Identlen); 1650Sstevel@tonic-gate lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp); 1660Sstevel@tonic-gate stats_counter_add(Outbytes, Dictlen); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) { 1690Sstevel@tonic-gate char *ptr; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate for (ptr = buf; ptr < &buf[cc]; ptr++) 1720Sstevel@tonic-gate *ptr = ~((unsigned char)*ptr); 1730Sstevel@tonic-gate if (cc != fwrite(buf, 1, cc, fp) || ferror(fp)) 1740Sstevel@tonic-gate out(O_DIE|O_SYS, "fwrite on %s", fname); 1750Sstevel@tonic-gate stats_counter_add(Outbytes, cc); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate if (ferror(tfp)) 1780Sstevel@tonic-gate out(O_DIE|O_SYS, "fread on tmpfile"); 1790Sstevel@tonic-gate fclose(tfp); 1800Sstevel@tonic-gate fclose(fp); 1810Sstevel@tonic-gate } 182