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