dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / eversholt / common / eftwrite.c
blob4329465de2b4df36695934df641c8e6857701dcf
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * eftwrite.c -- routines for writing .eft files
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.
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"
48 /* for uintX_t, htonl(), etc */
49 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <inttypes.h>
53 extern char Args[];
55 static struct stats *Outbytes;
57 static int Identlen;
58 static int Dictlen;
60 void
61 eftwrite_init(void)
63 Outbytes = stats_new_counter("eftwrite.total", "bytes written", 1);
66 /*ARGSUSED*/
67 static void
68 ident_lencalc(const char *s, void *rhs, void *arg)
70 Identlen += strlen(s) + 1;
73 /*ARGSUSED*/
74 static void
75 dict_lencalc(const char *s, void *rhs, void *arg)
77 Dictlen += strlen(s) + 1;
80 /*ARGSUSED*/
81 static void
82 ident_printer(const char *s, void *rhs, void *arg)
84 FILE *fp = (FILE *)arg;
86 (void) fwrite(s, strlen(s) + 1, 1, fp);
89 /*ARGSUSED*/
90 static void
91 dict_printer(const char *s, void *rhs, void *arg)
93 FILE *fp = (FILE *)arg;
95 (void) fwrite(s, strlen(s) + 1, 1, fp);
98 void
99 eftwrite(const char *fname)
101 FILE *fp;
102 FILE *tfp;
103 struct eftheader hdr;
104 #define BUFLEN 8192
105 char buf[BUFLEN];
106 int cc;
108 if ((tfp = tmpfile()) == NULL)
109 out(O_DIE|O_SYS, "cannot create temporary file");
111 /* XXX switch stdout to tfp temporarily */
112 /* XXX for now */
113 out_altfp(tfp);
114 ptree(O_ALTFP, tree_root(NULL), 0, 1);
116 rewind(tfp);
118 lut_walk(Ident, (lut_cb)ident_lencalc, NULL);
119 lut_walk(Dicts, (lut_cb)dict_lencalc, NULL);
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';
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
140 if ((fp = fopen(fname, "w")) == NULL)
141 out(O_DIE|O_SYS, "can't open output file: %s", fname);
143 while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
144 char *ptr;
146 for (ptr = buf; ptr < &buf[cc]; ptr++)
147 hdr.csum += (uint32_t)*ptr;
149 if (ferror(tfp))
150 out(O_DIE|O_SYS, "fread on tmpfile");
151 rewind(tfp);
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);
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));
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);
172 while ((cc = fread(buf, 1, BUFLEN, tfp)) > 0) {
173 char *ptr;
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);
181 if (ferror(tfp))
182 out(O_DIE|O_SYS, "fread on tmpfile");
183 (void) fclose(tfp);
184 (void) fclose(fp);