emit.c revision 1.3 1 1.3 tv /* $NetBSD: emit.c,v 1.3 2002/01/31 19:36:53 tv 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.3 tv #if defined(__RCSID) && !defined(lint)
36 1.3 tv __RCSID("$NetBSD: emit.c,v 1.3 2002/01/31 19:36:53 tv Exp $");
37 1.1 thorpej #endif
38 1.1 thorpej
39 1.2 tv #include <ctype.h>
40 1.1 thorpej #include <stdio.h>
41 1.1 thorpej #include <string.h>
42 1.1 thorpej
43 1.1 thorpej #include "lint.h"
44 1.1 thorpej
45 1.1 thorpej /* name and handle of output file */
46 1.1 thorpej static const char *loname;
47 1.1 thorpej static FILE *lout;
48 1.1 thorpej
49 1.1 thorpej /* output buffer data */
50 1.1 thorpej ob_t ob;
51 1.1 thorpej
52 1.1 thorpej static void outxbuf(void);
53 1.1 thorpej
54 1.1 thorpej
55 1.1 thorpej /*
56 1.1 thorpej * initialize output
57 1.1 thorpej */
58 1.1 thorpej void
59 1.1 thorpej outopen(const char *name)
60 1.1 thorpej {
61 1.1 thorpej
62 1.1 thorpej loname = name;
63 1.1 thorpej
64 1.1 thorpej /* Open output file */
65 1.1 thorpej if ((lout = fopen(name, "w")) == NULL)
66 1.1 thorpej err(1, "cannot open '%s'", name);
67 1.1 thorpej
68 1.1 thorpej /* Create output buffer */
69 1.1 thorpej ob.o_len = 1024;
70 1.1 thorpej ob.o_end = (ob.o_buf = ob.o_nxt = xmalloc(ob.o_len)) + ob.o_len;
71 1.1 thorpej }
72 1.1 thorpej
73 1.1 thorpej /*
74 1.1 thorpej * flush output buffer and close file
75 1.1 thorpej */
76 1.1 thorpej void
77 1.1 thorpej outclose(void)
78 1.1 thorpej {
79 1.1 thorpej
80 1.1 thorpej outclr();
81 1.1 thorpej if (fclose(lout) == EOF)
82 1.1 thorpej err(1, "cannot close '%s'", loname);
83 1.1 thorpej }
84 1.1 thorpej
85 1.1 thorpej /*
86 1.1 thorpej * resize output buffer
87 1.1 thorpej */
88 1.1 thorpej static void
89 1.1 thorpej outxbuf(void)
90 1.1 thorpej {
91 1.1 thorpej ptrdiff_t coffs;
92 1.1 thorpej
93 1.1 thorpej coffs = ob.o_nxt - ob.o_buf;
94 1.1 thorpej ob.o_len *= 2;
95 1.1 thorpej ob.o_end = (ob.o_buf = xrealloc(ob.o_buf, ob.o_len)) + ob.o_len;
96 1.1 thorpej ob.o_nxt = ob.o_buf + coffs;
97 1.1 thorpej }
98 1.1 thorpej
99 1.1 thorpej /*
100 1.1 thorpej * reset output buffer
101 1.1 thorpej * if it is not empty, it is flushed
102 1.1 thorpej */
103 1.1 thorpej void
104 1.1 thorpej outclr(void)
105 1.1 thorpej {
106 1.1 thorpej size_t sz;
107 1.1 thorpej
108 1.1 thorpej if (ob.o_buf != ob.o_nxt) {
109 1.1 thorpej outchar('\n');
110 1.1 thorpej sz = ob.o_nxt - ob.o_buf;
111 1.1 thorpej if (sz > ob.o_len)
112 1.1 thorpej errx(1, "internal error: outclr() 1");
113 1.1 thorpej if (fwrite(ob.o_buf, sz, 1, lout) != 1)
114 1.1 thorpej err(1, "cannot write to %s", loname);
115 1.1 thorpej ob.o_nxt = ob.o_buf;
116 1.1 thorpej }
117 1.1 thorpej }
118 1.1 thorpej
119 1.1 thorpej /*
120 1.1 thorpej * write a character to the output buffer
121 1.1 thorpej */
122 1.1 thorpej void
123 1.1 thorpej outchar(int c)
124 1.1 thorpej {
125 1.1 thorpej
126 1.1 thorpej if (ob.o_nxt == ob.o_end)
127 1.1 thorpej outxbuf();
128 1.1 thorpej *ob.o_nxt++ = (char)c;
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej /*
132 1.1 thorpej * write a character to the output buffer, qouted if necessary
133 1.1 thorpej */
134 1.1 thorpej void
135 1.1 thorpej outqchar(int c)
136 1.1 thorpej {
137 1.1 thorpej
138 1.1 thorpej if (isprint(c) && c != '\\' && c != '"' && c != '\'') {
139 1.1 thorpej outchar(c);
140 1.1 thorpej } else {
141 1.1 thorpej outchar('\\');
142 1.1 thorpej switch (c) {
143 1.1 thorpej case '\\':
144 1.1 thorpej outchar('\\');
145 1.1 thorpej break;
146 1.1 thorpej case '"':
147 1.1 thorpej outchar('"');
148 1.1 thorpej break;
149 1.1 thorpej case '\'':
150 1.1 thorpej outchar('\'');
151 1.1 thorpej break;
152 1.1 thorpej case '\b':
153 1.1 thorpej outchar('b');
154 1.1 thorpej break;
155 1.1 thorpej case '\t':
156 1.1 thorpej outchar('t');
157 1.1 thorpej break;
158 1.1 thorpej case '\n':
159 1.1 thorpej outchar('n');
160 1.1 thorpej break;
161 1.1 thorpej case '\f':
162 1.1 thorpej outchar('f');
163 1.1 thorpej break;
164 1.1 thorpej case '\r':
165 1.1 thorpej outchar('r');
166 1.1 thorpej break;
167 1.1 thorpej case '\v':
168 1.1 thorpej outchar('v');
169 1.1 thorpej break;
170 1.1 thorpej case '\a':
171 1.1 thorpej outchar('a');
172 1.1 thorpej break;
173 1.1 thorpej default:
174 1.1 thorpej outchar((((u_int)c >> 6) & 07) + '0');
175 1.1 thorpej outchar((((u_int)c >> 3) & 07) + '0');
176 1.1 thorpej outchar((c & 07) + '0');
177 1.1 thorpej break;
178 1.1 thorpej }
179 1.1 thorpej }
180 1.1 thorpej }
181 1.1 thorpej
182 1.1 thorpej /*
183 1.1 thorpej * write a strint to the output buffer
184 1.1 thorpej * the string must not contain any characters which
185 1.1 thorpej * should be quoted
186 1.1 thorpej */
187 1.1 thorpej void
188 1.1 thorpej outstrg(const char *s)
189 1.1 thorpej {
190 1.1 thorpej
191 1.1 thorpej while (*s != '\0') {
192 1.1 thorpej if (ob.o_nxt == ob.o_end)
193 1.1 thorpej outxbuf();
194 1.1 thorpej *ob.o_nxt++ = *s++;
195 1.1 thorpej }
196 1.1 thorpej }
197 1.1 thorpej
198 1.1 thorpej /*
199 1.1 thorpej * write an integer value to toe output buffer
200 1.1 thorpej */
201 1.1 thorpej void
202 1.1 thorpej outint(int i)
203 1.1 thorpej {
204 1.1 thorpej
205 1.1 thorpej if ((ob.o_end - ob.o_nxt) < 3 * sizeof (int))
206 1.1 thorpej outxbuf();
207 1.1 thorpej ob.o_nxt += sprintf(ob.o_nxt, "%d", i);
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej /*
211 1.1 thorpej * write the name of a symbol to the output buffer
212 1.1 thorpej * the name is preceded by its length
213 1.1 thorpej */
214 1.1 thorpej void
215 1.1 thorpej outname(const char *name)
216 1.1 thorpej {
217 1.1 thorpej
218 1.1 thorpej if (name == NULL)
219 1.1 thorpej errx(1, "internal error: outname() 1");
220 1.1 thorpej outint((int)strlen(name));
221 1.1 thorpej outstrg(name);
222 1.1 thorpej }
223 1.1 thorpej
224 1.1 thorpej /*
225 1.1 thorpej * write the name of the .c source
226 1.1 thorpej */
227 1.1 thorpej void
228 1.1 thorpej outsrc(const char *name)
229 1.1 thorpej {
230 1.1 thorpej
231 1.1 thorpej outclr();
232 1.1 thorpej outchar('S');
233 1.1 thorpej outstrg(name);
234 1.1 thorpej }
235