xref: /titanic_51/usr/src/cmd/fm/eversholt/common/eftwrite.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 2007 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 
133 #ifdef DEBUG
134 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
135 	    "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR,
136 	    VERSION_MINOR, Args);
137 #else
138 	(void) snprintf(hdr.comment, EFT_HDR_MAXCOMMENT,
139 	    "Built using esc-%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
140 #endif
141 
142 	if ((fp = fopen(fname, "w")) == NULL)
143 		out(O_DIE|O_SYS, "can't open output file: %s", fname);
144 
145 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
146 		char *ptr;
147 
148 		for (ptr = buf; ptr < &buf[cc]; ptr++)
149 			hdr.csum += (uint32_t)*ptr;
150 	}
151 	if (ferror(tfp))
152 		out(O_DIE|O_SYS, "fread on tmpfile");
153 	rewind(tfp);
154 
155 	hdr.magic = htonl(hdr.magic);
156 	hdr.major = htons(hdr.major);
157 	hdr.minor = htons(hdr.minor);
158 	hdr.cmajor = htons(hdr.cmajor);
159 	hdr.cminor = htons(hdr.cminor);
160 	hdr.identlen = htonl(hdr.identlen);
161 	hdr.dictlen = htonl(hdr.dictlen);
162 	hdr.csum = htonl(hdr.csum);
163 
164 	fwrite(&hdr, sizeof (hdr), 1, fp);
165 	if (ferror(fp))
166 		out(O_DIE|O_SYS, "%s: can't write header", fname);
167 	stats_counter_add(Outbytes, sizeof (hdr));
168 
169 	lut_walk(Ident, (lut_cb)ident_printer, (void *)fp);
170 	stats_counter_add(Outbytes, Identlen);
171 	lut_walk(Dicts, (lut_cb)dict_printer, (void *)fp);
172 	stats_counter_add(Outbytes, Dictlen);
173 
174 	while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
175 		char *ptr;
176 
177 		for (ptr = buf; ptr < &buf[cc]; ptr++)
178 			*ptr = ~((unsigned char)*ptr);
179 		if (cc != fwrite(buf, 1, cc, fp) || ferror(fp))
180 			out(O_DIE|O_SYS, "fwrite on %s", fname);
181 		stats_counter_add(Outbytes, cc);
182 	}
183 	if (ferror(tfp))
184 		out(O_DIE|O_SYS, "fread on tmpfile");
185 	fclose(tfp);
186 	fclose(fp);
187 }
188