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