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]
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.
48 /* for uintX_t, htonl(), etc */
49 #include <sys/types.h>
50 #include <netinet/in.h>
55 static struct stats
*Outbytes
;
63 Outbytes
= stats_new_counter("eftwrite.total", "bytes written", 1);
68 ident_lencalc(const char *s
, void *rhs
, void *arg
)
70 Identlen
+= strlen(s
) + 1;
75 dict_lencalc(const char *s
, void *rhs
, void *arg
)
77 Dictlen
+= strlen(s
) + 1;
82 ident_printer(const char *s
, void *rhs
, void *arg
)
84 FILE *fp
= (FILE *)arg
;
86 (void) fwrite(s
, strlen(s
) + 1, 1, fp
);
91 dict_printer(const char *s
, void *rhs
, void *arg
)
93 FILE *fp
= (FILE *)arg
;
95 (void) fwrite(s
, strlen(s
) + 1, 1, fp
);
99 eftwrite(const char *fname
)
103 struct eftheader hdr
;
108 if ((tfp
= tmpfile()) == NULL
)
109 out(O_DIE
|O_SYS
, "cannot create temporary file");
111 /* XXX switch stdout to tfp temporarily */
114 ptree(O_ALTFP
, tree_root(NULL
), 0, 1);
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';
132 (void) snprintf(hdr
.comment
, EFT_HDR_MAXCOMMENT
,
133 "Built using esc-%d.%d\tArgs: \"%s\"\n", VERSION_MAJOR
,
134 VERSION_MINOR
, Args
);
136 (void) snprintf(hdr
.comment
, EFT_HDR_MAXCOMMENT
,
137 "Built using esc-%d.%d\n", VERSION_MAJOR
, VERSION_MINOR
);
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) {
146 for (ptr
= buf
; ptr
< &buf
[cc
]; ptr
++)
147 hdr
.csum
+= (uint32_t)*ptr
;
150 out(O_DIE
|O_SYS
, "fread on tmpfile");
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
);
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) {
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
);
182 out(O_DIE
|O_SYS
, "fread on tmpfile");