emit.c revision 1.1 1 1.1 thorpej /* $NetBSD: emit.c,v 1.1 2002/01/18 20:39:22 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994, 1995 Jochen Pohl
5 1.1 thorpej * All Rights Reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Jochen Pohl for
18 1.1 thorpej * The NetBSD Project.
19 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
20 1.1 thorpej * derived from this software without specific prior written permission.
21 1.1 thorpej *
22 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/cdefs.h>
35 1.1 thorpej #ifndef lint
36 1.1 thorpej __RCSID("$NetBSD: emit.c,v 1.1 2002/01/18 20:39:22 thorpej Exp $");
37 1.1 thorpej #endif
38 1.1 thorpej
39 1.1 thorpej #include <stdio.h>
40 1.1 thorpej #include <string.h>
41 1.1 thorpej #include <ctype.h>
42 1.1 thorpej #include <err.h>
43 1.1 thorpej
44 1.1 thorpej #include "lint.h"
45 1.1 thorpej
46 1.1 thorpej /* name and handle of output file */
47 1.1 thorpej static const char *loname;
48 1.1 thorpej static FILE *lout;
49 1.1 thorpej
50 1.1 thorpej /* output buffer data */
51 1.1 thorpej ob_t ob;
52 1.1 thorpej
53 1.1 thorpej static void outxbuf(void);
54 1.1 thorpej
55 1.1 thorpej
56 1.1 thorpej /*
57 1.1 thorpej * initialize output
58 1.1 thorpej */
59 1.1 thorpej void
60 1.1 thorpej outopen(const char *name)
61 1.1 thorpej {
62 1.1 thorpej
63 1.1 thorpej loname = name;
64 1.1 thorpej
65 1.1 thorpej /* Open output file */
66 1.1 thorpej if ((lout = fopen(name, "w")) == NULL)
67 1.1 thorpej err(1, "cannot open '%s'", name);
68 1.1 thorpej
69 1.1 thorpej /* Create output buffer */
70 1.1 thorpej ob.o_len = 1024;
71 1.1 thorpej ob.o_end = (ob.o_buf = ob.o_nxt = xmalloc(ob.o_len)) + ob.o_len;
72 1.1 thorpej }
73 1.1 thorpej
74 1.1 thorpej /*
75 1.1 thorpej * flush output buffer and close file
76 1.1 thorpej */
77 1.1 thorpej void
78 1.1 thorpej outclose(void)
79 1.1 thorpej {
80 1.1 thorpej
81 1.1 thorpej outclr();
82 1.1 thorpej if (fclose(lout) == EOF)
83 1.1 thorpej err(1, "cannot close '%s'", loname);
84 1.1 thorpej }
85 1.1 thorpej
86 1.1 thorpej /*
87 1.1 thorpej * resize output buffer
88 1.1 thorpej */
89 1.1 thorpej static void
90 1.1 thorpej outxbuf(void)
91 1.1 thorpej {
92 1.1 thorpej ptrdiff_t coffs;
93 1.1 thorpej
94 1.1 thorpej coffs = ob.o_nxt - ob.o_buf;
95 1.1 thorpej ob.o_len *= 2;
96 1.1 thorpej ob.o_end = (ob.o_buf = xrealloc(ob.o_buf, ob.o_len)) + ob.o_len;
97 1.1 thorpej ob.o_nxt = ob.o_buf + coffs;
98 1.1 thorpej }
99 1.1 thorpej
100 1.1 thorpej /*
101 1.1 thorpej * reset output buffer
102 1.1 thorpej * if it is not empty, it is flushed
103 1.1 thorpej */
104 1.1 thorpej void
105 1.1 thorpej outclr(void)
106 1.1 thorpej {
107 1.1 thorpej size_t sz;
108 1.1 thorpej
109 1.1 thorpej if (ob.o_buf != ob.o_nxt) {
110 1.1 thorpej outchar('\n');
111 1.1 thorpej sz = ob.o_nxt - ob.o_buf;
112 1.1 thorpej if (sz > ob.o_len)
113 1.1 thorpej errx(1, "internal error: outclr() 1");
114 1.1 thorpej if (fwrite(ob.o_buf, sz, 1, lout) != 1)
115 1.1 thorpej err(1, "cannot write to %s", loname);
116 1.1 thorpej ob.o_nxt = ob.o_buf;
117 1.1 thorpej }
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej /*
121 1.1 thorpej * write a character to the output buffer
122 1.1 thorpej */
123 1.1 thorpej void
124 1.1 thorpej outchar(int c)
125 1.1 thorpej {
126 1.1 thorpej
127 1.1 thorpej if (ob.o_nxt == ob.o_end)
128 1.1 thorpej outxbuf();
129 1.1 thorpej *ob.o_nxt++ = (char)c;
130 1.1 thorpej }
131 1.1 thorpej
132 1.1 thorpej /*
133 1.1 thorpej * write a character to the output buffer, qouted if necessary
134 1.1 thorpej */
135 1.1 thorpej void
136 1.1 thorpej outqchar(int c)
137 1.1 thorpej {
138 1.1 thorpej
139 1.1 thorpej if (isprint(c) && c != '\\' && c != '"' && c != '\'') {
140 1.1 thorpej outchar(c);
141 1.1 thorpej } else {
142 1.1 thorpej outchar('\\');
143 1.1 thorpej switch (c) {
144 1.1 thorpej case '\\':
145 1.1 thorpej outchar('\\');
146 1.1 thorpej break;
147 1.1 thorpej case '"':
148 1.1 thorpej outchar('"');
149 1.1 thorpej break;
150 1.1 thorpej case '\'':
151 1.1 thorpej outchar('\'');
152 1.1 thorpej break;
153 1.1 thorpej case '\b':
154 1.1 thorpej outchar('b');
155 1.1 thorpej break;
156 1.1 thorpej case '\t':
157 1.1 thorpej outchar('t');
158 1.1 thorpej break;
159 1.1 thorpej case '\n':
160 1.1 thorpej outchar('n');
161 1.1 thorpej break;
162 1.1 thorpej case '\f':
163 1.1 thorpej outchar('f');
164 1.1 thorpej break;
165 1.1 thorpej case '\r':
166 1.1 thorpej outchar('r');
167 1.1 thorpej break;
168 1.1 thorpej case '\v':
169 1.1 thorpej outchar('v');
170 1.1 thorpej break;
171 1.1 thorpej case '\a':
172 1.1 thorpej outchar('a');
173 1.1 thorpej break;
174 1.1 thorpej default:
175 1.1 thorpej outchar((((u_int)c >> 6) & 07) + '0');
176 1.1 thorpej outchar((((u_int)c >> 3) & 07) + '0');
177 1.1 thorpej outchar((c & 07) + '0');
178 1.1 thorpej break;
179 1.1 thorpej }
180 1.1 thorpej }
181 1.1 thorpej }
182 1.1 thorpej
183 1.1 thorpej /*
184 1.1 thorpej * write a strint to the output buffer
185 1.1 thorpej * the string must not contain any characters which
186 1.1 thorpej * should be quoted
187 1.1 thorpej */
188 1.1 thorpej void
189 1.1 thorpej outstrg(const char *s)
190 1.1 thorpej {
191 1.1 thorpej
192 1.1 thorpej while (*s != '\0') {
193 1.1 thorpej if (ob.o_nxt == ob.o_end)
194 1.1 thorpej outxbuf();
195 1.1 thorpej *ob.o_nxt++ = *s++;
196 1.1 thorpej }
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej /*
200 1.1 thorpej * write an integer value to toe output buffer
201 1.1 thorpej */
202 1.1 thorpej void
203 1.1 thorpej outint(int i)
204 1.1 thorpej {
205 1.1 thorpej
206 1.1 thorpej if ((ob.o_end - ob.o_nxt) < 3 * sizeof (int))
207 1.1 thorpej outxbuf();
208 1.1 thorpej ob.o_nxt += sprintf(ob.o_nxt, "%d", i);
209 1.1 thorpej }
210 1.1 thorpej
211 1.1 thorpej /*
212 1.1 thorpej * write the name of a symbol to the output buffer
213 1.1 thorpej * the name is preceded by its length
214 1.1 thorpej */
215 1.1 thorpej void
216 1.1 thorpej outname(const char *name)
217 1.1 thorpej {
218 1.1 thorpej
219 1.1 thorpej if (name == NULL)
220 1.1 thorpej errx(1, "internal error: outname() 1");
221 1.1 thorpej outint((int)strlen(name));
222 1.1 thorpej outstrg(name);
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej /*
226 1.1 thorpej * write the name of the .c source
227 1.1 thorpej */
228 1.1 thorpej void
229 1.1 thorpej outsrc(const char *name)
230 1.1 thorpej {
231 1.1 thorpej
232 1.1 thorpej outclr();
233 1.1 thorpej outchar('S');
234 1.1 thorpej outstrg(name);
235 1.1 thorpej }
236