17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 596ccc8cbSesaxe * Common Development and Distribution License (the "License"). 696ccc8cbSesaxe * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*888e0559SRobert Johnston * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate * 257c478bd9Sstevel@tonic-gate * eftwrite.c -- routines for writing .eft files 267c478bd9Sstevel@tonic-gate * 277c478bd9Sstevel@tonic-gate * this module emits the table resulting from compilation of the 287c478bd9Sstevel@tonic-gate * source files. this code done nothing unless the -o option 297c478bd9Sstevel@tonic-gate * was given on the command line. 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include "out.h" 387c478bd9Sstevel@tonic-gate #include "stats.h" 397c478bd9Sstevel@tonic-gate #include "stable.h" 407c478bd9Sstevel@tonic-gate #include "lut.h" 417c478bd9Sstevel@tonic-gate #include "tree.h" 427c478bd9Sstevel@tonic-gate #include "eft.h" 437c478bd9Sstevel@tonic-gate #include "eftwrite.h" 447c478bd9Sstevel@tonic-gate #include "esclex.h" 457c478bd9Sstevel@tonic-gate #include "version.h" 467c478bd9Sstevel@tonic-gate #include "ptree.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* for uintX_t, htonl(), etc */ 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 507c478bd9Sstevel@tonic-gate #include <netinet/in.h> 517c478bd9Sstevel@tonic-gate #include <inttypes.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate extern char Args[]; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static struct stats *Outbytes; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static int Identlen; 587c478bd9Sstevel@tonic-gate static int Dictlen; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate void 617c478bd9Sstevel@tonic-gate eftwrite_init(void) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 677c478bd9Sstevel@tonic-gate static void 687c478bd9Sstevel@tonic-gate ident_lencalc(const char *s, void *rhs, void *arg) 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate Identlen += strlen(s) + 1; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 747c478bd9Sstevel@tonic-gate static void 757c478bd9Sstevel@tonic-gate dict_lencalc(const char *s, void *rhs, void *arg) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate Dictlen += strlen(s) + 1; 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 817c478bd9Sstevel@tonic-gate static void 827c478bd9Sstevel@tonic-gate ident_printer(const char *s, void *rhs, void *arg) 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate FILE *fp = (FILE *)arg; 857c478bd9Sstevel@tonic-gate 86*888e0559SRobert Johnston (void) fwrite(s, strlen(s) + 1, 1, fp); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 907c478bd9Sstevel@tonic-gate static void 917c478bd9Sstevel@tonic-gate dict_printer(const char *s, void *rhs, void *arg) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate FILE *fp = (FILE *)arg; 947c478bd9Sstevel@tonic-gate 95*888e0559SRobert Johnston (void) fwrite(s, strlen(s) + 1, 1, fp); 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate void 997c478bd9Sstevel@tonic-gate eftwrite(const char *fname) 1007c478bd9Sstevel@tonic-gate { 1017c478bd9Sstevel@tonic-gate FILE *fp; 1027c478bd9Sstevel@tonic-gate FILE *tfp; 1037c478bd9Sstevel@tonic-gate struct eftheader hdr; 1047c478bd9Sstevel@tonic-gate #define BUFLEN 8192 1057c478bd9Sstevel@tonic-gate char buf[BUFLEN]; 1067c478bd9Sstevel@tonic-gate int cc; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate if ((tfp = tmpfile()) == NULL) 1097c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "cannot create temporary file"); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* XXX switch stdout to tfp temporarily */ 1127c478bd9Sstevel@tonic-gate /* XXX for now */ 1137c478bd9Sstevel@tonic-gate out_altfp(tfp); 1147c478bd9Sstevel@tonic-gate ptree(O_ALTFP, tree_root(NULL), 0, 1); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate rewind(tfp); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate lut_walk(Ident, (lut_cb)ident_lencalc, (void *)0); 1197c478bd9Sstevel@tonic-gate lut_walk(Dicts, (lut_cb)dict_lencalc, (void *)0); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate bzero(&hdr, sizeof (hdr)); 1227c478bd9Sstevel@tonic-gate hdr.magic = EFT_HDR_MAGIC; 1237c478bd9Sstevel@tonic-gate hdr.major = EFT_HDR_MAJOR; 1247c478bd9Sstevel@tonic-gate hdr.minor = EFT_HDR_MINOR; 1257c478bd9Sstevel@tonic-gate hdr.cmajor = VERSION_MAJOR; 1267c478bd9Sstevel@tonic-gate hdr.cminor = VERSION_MINOR; 1277c478bd9Sstevel@tonic-gate hdr.identlen = Identlen; 1287c478bd9Sstevel@tonic-gate hdr.dictlen = Dictlen; 1297c478bd9Sstevel@tonic-gate buf[BUFLEN - 1] = '\0'; 13047911a7dScy152378 13147911a7dScy152378 #ifdef DEBUG 1327c478bd9Sstevel@tonic-gate (void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT, 13396ccc8cbSesaxe "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR, 13496ccc8cbSesaxe VERSION_MINOR, Args); 13547911a7dScy152378 #else 13647911a7dScy152378 (void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT, 13747911a7dScy152378 "Built using esc-%d.%d\n", VERSION_MAJOR, VERSION_MINOR); 13847911a7dScy152378 #endif 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if ((fp = fopen(fname, "w")) == NULL) 1417c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "can't open output file: %s", fname); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) { 1447c478bd9Sstevel@tonic-gate char *ptr; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate for (ptr = buf; ptr < &buf[cc]; ptr++) 1477c478bd9Sstevel@tonic-gate hdr.csum += (uint32_t)*ptr; 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate if (ferror(tfp)) 1507c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "fread on tmpfile"); 1517c478bd9Sstevel@tonic-gate rewind(tfp); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate hdr.magic = htonl(hdr.magic); 1547c478bd9Sstevel@tonic-gate hdr.major = htons(hdr.major); 1557c478bd9Sstevel@tonic-gate hdr.minor = htons(hdr.minor); 1567c478bd9Sstevel@tonic-gate hdr.cmajor = htons(hdr.cmajor); 1577c478bd9Sstevel@tonic-gate hdr.cminor = htons(hdr.cminor); 1587c478bd9Sstevel@tonic-gate hdr.identlen = htonl(hdr.identlen); 1597c478bd9Sstevel@tonic-gate hdr.dictlen = htonl(hdr.dictlen); 1607c478bd9Sstevel@tonic-gate hdr.csum = htonl(hdr.csum); 1617c478bd9Sstevel@tonic-gate 162*888e0559SRobert Johnston (void) fwrite(&hdr, sizeof (hdr), 1, fp); 1637c478bd9Sstevel@tonic-gate if (ferror(fp)) 1647c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "%s: can't write header", fname); 1657c478bd9Sstevel@tonic-gate stats_counter_add(Outbytes, sizeof (hdr)); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate lut_walk(Ident, (lut_cb)ident_printer, (void *)fp); 1687c478bd9Sstevel@tonic-gate stats_counter_add(Outbytes, Identlen); 1697c478bd9Sstevel@tonic-gate lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp); 1707c478bd9Sstevel@tonic-gate stats_counter_add(Outbytes, Dictlen); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) { 1737c478bd9Sstevel@tonic-gate char *ptr; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate for (ptr = buf; ptr < &buf[cc]; ptr++) 1767c478bd9Sstevel@tonic-gate *ptr = ~((unsigned char)*ptr); 1777c478bd9Sstevel@tonic-gate if (cc != fwrite(buf, 1, cc, fp) || ferror(fp)) 1787c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "fwrite on %s", fname); 1797c478bd9Sstevel@tonic-gate stats_counter_add(Outbytes, cc); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate if (ferror(tfp)) 1827c478bd9Sstevel@tonic-gate out(O_DIE|O_SYS, "fread on tmpfile"); 183*888e0559SRobert Johnston (void) fclose(tfp); 184*888e0559SRobert Johnston (void) fclose(fp); 1857c478bd9Sstevel@tonic-gate } 186