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