xref: /titanic_41/usr/src/cmd/fm/eversholt/common/eftwrite.c (revision c037192b037119c9fd354732fc29b38ce097d356)
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 2009 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 #include <stdio.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include "out.h"
38 #include "stats.h"
39 #include "stable.h"
40 #include "lut.h"
41 #include "tree.h"
42 #include "eft.h"
43 #include "eftwrite.h"
44 #include "esclex.h"
45 #include "version.h"
46 #include "ptree.h"
47 
48 /* for uintX_t, htonl(), etc */
49 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <inttypes.h>
52 
53 extern char Args[];
54 
55 static struct stats *Outbytes;
56 
57 static int Identlen;
58 static int Dictlen;
59 
60 void
61 eftwrite_init(void)
62 {
63 	Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1);
64 }
65 
66 /*ARGSUSED*/
67 static void
68 ident_lencalc(const char *s, void *rhs, void *arg)
69 {
70 	Identlen += strlen(s) + 1;
71 }
72 
73 /*ARGSUSED*/
74 static void
75 dict_lencalc(const char *s, void *rhs, void *arg)
76 {
77 	Dictlen += strlen(s) + 1;
78 }
79 
80 /*ARGSUSED*/
81 static void
82 ident_printer(const char *s, void *rhs, void *arg)
83 {
84 	FILE *fp = (FILE *)arg;
85 
86 	(void) fwrite(s, strlen(s) + 1, 1, fp);
87 }
88 
89 /*ARGSUSED*/
90 static void
91 dict_printer(const char *s, void *rhs, void *arg)
92 {
93 	FILE *fp = (FILE *)arg;
94 
95 	(void) fwrite(s, strlen(s) + 1, 1, fp);
96 }
97 
98 void
99 eftwrite(const char *fname)
100 {
101 	FILE *fp;
102 	FILE *tfp;
103 	struct eftheader hdr;
104 #define	BUFLEN	8192
105 	char buf[BUFLEN];
106 	int cc;
107 
108 	if ((tfp = tmpfile()) == NULL)
109 		out(O_DIE|O_SYS, "cannot create temporary file");
110 
111 	/* XXX switch stdout to tfp temporarily */
112 	/* XXX for now */
113 	out_altfp(tfp);
114 	ptree(O_ALTFP, tree_root(NULL), 0, 1);
115 
116 	rewind(tfp);
117 
118 	lut_walk(Ident, (lut_cb)ident_lencalc, (void *)0);
119 	lut_walk(Dicts, (lut_cb)dict_lencalc, (void *)0);
120 
121 	bzero(&hdr, sizeof (hdr));
122 	hdr.magic = EFT_HDR_MAGIC;
123 	hdr.major = EFT_HDR_MAJOR;
124 	hdr.minor = EFT_HDR_MINOR;
125 	hdr.cmajor = VERSION_MAJOR;
126 	hdr.cminor = VERSION_MINOR;
127 	hdr.identlen = Identlen;
128 	hdr.dictlen = Dictlen;
129 	buf[BUFLEN - 1] = '\0';
130 
131 #ifdef DEBUG
132 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
133 	    "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR,
134 	    VERSION_MINOR, Args);
135 #else
136 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
137 	    "Built using esc-%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
138 #endif
139 
140 	if ((fp = fopen(fname, "w")) == NULL)
141 		out(O_DIE|O_SYS, "can't open output file: %s", fname);
142 
143 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
144 		char *ptr;
145 
146 		for (ptr = buf; ptr < &buf[cc]; ptr++)
147 			hdr.csum += (uint32_t)*ptr;
148 	}
149 	if (ferror(tfp))
150 		out(O_DIE|O_SYS, "fread on tmpfile");
151 	rewind(tfp);
152 
153 	hdr.magic = htonl(hdr.magic);
154 	hdr.major = htons(hdr.major);
155 	hdr.minor = htons(hdr.minor);
156 	hdr.cmajor = htons(hdr.cmajor);
157 	hdr.cminor = htons(hdr.cminor);
158 	hdr.identlen = htonl(hdr.identlen);
159 	hdr.dictlen = htonl(hdr.dictlen);
160 	hdr.csum = htonl(hdr.csum);
161 
162 	(void) fwrite(&hdr, sizeof (hdr), 1, fp);
163 	if (ferror(fp))
164 		out(O_DIE|O_SYS, "%s: can't write header", fname);
165 	stats_counter_add(Outbytes, sizeof (hdr));
166 
167 	lut_walk(Ident, (lut_cb)ident_printer, (void *)fp);
168 	stats_counter_add(Outbytes, Identlen);
169 	lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp);
170 	stats_counter_add(Outbytes, Dictlen);
171 
172 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
173 		char *ptr;
174 
175 		for (ptr = buf; ptr < &buf[cc]; ptr++)
176 			*ptr = ~((unsigned char)*ptr);
177 		if (cc != fwrite(buf, 1, cc, fp) || ferror(fp))
178 			out(O_DIE|O_SYS, "fwrite on %s", fname);
179 		stats_counter_add(Outbytes, cc);
180 	}
181 	if (ferror(tfp))
182 		out(O_DIE|O_SYS, "fread on tmpfile");
183 	(void) fclose(tfp);
184 	(void) fclose(fp);
185 }
186