xref: /titanic_51/usr/src/cmd/fm/eversholt/common/eftwrite.c (revision 6f45ec7b0b964c3be967c4880e8867ac1e7763a5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * eftwrite.c -- routines for writing .eft files
26  *
27  * this module emits the table resulting from compilation of the
28  * source files.  this code done nothing unless the -o option
29  * was given on the command line.
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include "out.h"
40 #include "stats.h"
41 #include "stable.h"
42 #include "lut.h"
43 #include "tree.h"
44 #include "eft.h"
45 #include "eftwrite.h"
46 #include "esclex.h"
47 #include "version.h"
48 #include "ptree.h"
49 
50 /* for uintX_t, htonl(), etc */
51 #include <sys/types.h>
52 #include <netinet/in.h>
53 #include <inttypes.h>
54 
55 extern char Args[];
56 
57 static struct stats *Outbytes;
58 
59 static int Identlen;
60 static int Dictlen;
61 
62 void
63 eftwrite_init(void)
64 {
65 	Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1);
66 }
67 
68 /*ARGSUSED*/
69 static void
70 ident_lencalc(const char *s, void *rhs, void *arg)
71 {
72 	Identlen += strlen(s) + 1;
73 }
74 
75 /*ARGSUSED*/
76 static void
77 dict_lencalc(const char *s, void *rhs, void *arg)
78 {
79 	Dictlen += strlen(s) + 1;
80 }
81 
82 /*ARGSUSED*/
83 static void
84 ident_printer(const char *s, void *rhs, void *arg)
85 {
86 	FILE *fp = (FILE *)arg;
87 
88 	fwrite(s, strlen(s) + 1, 1, fp);
89 }
90 
91 /*ARGSUSED*/
92 static void
93 dict_printer(const char *s, void *rhs, void *arg)
94 {
95 	FILE *fp = (FILE *)arg;
96 
97 	fwrite(s, strlen(s) + 1, 1, fp);
98 }
99 
100 void
101 eftwrite(const char *fname)
102 {
103 	FILE *fp;
104 	FILE *tfp;
105 	struct eftheader hdr;
106 #define	BUFLEN	8192
107 	char buf[BUFLEN];
108 	int cc;
109 
110 	if ((tfp = tmpfile()) == NULL)
111 		out(O_DIE|O_SYS, "cannot create temporary file");
112 
113 	/* XXX switch stdout to tfp temporarily */
114 	/* XXX for now */
115 	out_altfp(tfp);
116 	ptree(O_ALTFP, tree_root(NULL), 0, 1);
117 
118 	rewind(tfp);
119 
120 	lut_walk(Ident, (lut_cb)ident_lencalc, (void *)0);
121 	lut_walk(Dicts, (lut_cb)dict_lencalc, (void *)0);
122 
123 	bzero(&hdr, sizeof (hdr));
124 	hdr.magic = EFT_HDR_MAGIC;
125 	hdr.major = EFT_HDR_MAJOR;
126 	hdr.minor = EFT_HDR_MINOR;
127 	hdr.cmajor = VERSION_MAJOR;
128 	hdr.cminor = VERSION_MINOR;
129 	hdr.identlen = Identlen;
130 	hdr.dictlen = Dictlen;
131 	buf[BUFLEN - 1] = '\0';
132 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
133 	    "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR,
134 	    VERSION_MINOR, Args);
135 
136 	if ((fp = fopen(fname, "w")) == NULL)
137 		out(O_DIE|O_SYS, "can't open output file: %s", fname);
138 
139 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
140 		char *ptr;
141 
142 		for (ptr = buf; ptr < &buf[cc]; ptr++)
143 			hdr.csum += (uint32_t)*ptr;
144 	}
145 	if (ferror(tfp))
146 		out(O_DIE|O_SYS, "fread on tmpfile");
147 	rewind(tfp);
148 
149 	hdr.magic = htonl(hdr.magic);
150 	hdr.major = htons(hdr.major);
151 	hdr.minor = htons(hdr.minor);
152 	hdr.cmajor = htons(hdr.cmajor);
153 	hdr.cminor = htons(hdr.cminor);
154 	hdr.identlen = htonl(hdr.identlen);
155 	hdr.dictlen = htonl(hdr.dictlen);
156 	hdr.csum = htonl(hdr.csum);
157 
158 	fwrite(&hdr, sizeof (hdr), 1, fp);
159 	if (ferror(fp))
160 		out(O_DIE|O_SYS, "%s: can't write header", fname);
161 	stats_counter_add(Outbytes, sizeof (hdr));
162 
163 	lut_walk(Ident, (lut_cb)ident_printer, (void *)fp);
164 	stats_counter_add(Outbytes, Identlen);
165 	lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp);
166 	stats_counter_add(Outbytes, Dictlen);
167 
168 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
169 		char *ptr;
170 
171 		for (ptr = buf; ptr < &buf[cc]; ptr++)
172 			*ptr = ~((unsigned char)*ptr);
173 		if (cc != fwrite(buf, 1, cc, fp) || ferror(fp))
174 			out(O_DIE|O_SYS, "fwrite on %s", fname);
175 		stats_counter_add(Outbytes, cc);
176 	}
177 	if (ferror(tfp))
178 		out(O_DIE|O_SYS, "fread on tmpfile");
179 	fclose(tfp);
180 	fclose(fp);
181 }
182