emit.c revision 1.22 1 1.22 rillig /* $NetBSD: emit.c,v 1.22 2023/07/13 08:40:38 rillig 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.22 rillig * 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.4 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.4 jmc #include "nbtool_config.h"
36 1.4 jmc #endif
37 1.4 jmc
38 1.1 thorpej #include <sys/cdefs.h>
39 1.17 rillig #if defined(__RCSID)
40 1.22 rillig __RCSID("$NetBSD: emit.c,v 1.22 2023/07/13 08:40:38 rillig Exp $");
41 1.1 thorpej #endif
42 1.1 thorpej
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <string.h>
45 1.1 thorpej
46 1.1 thorpej #include "lint.h"
47 1.1 thorpej
48 1.1 thorpej /* name and handle of output file */
49 1.1 thorpej static const char *loname;
50 1.1 thorpej static FILE *lout;
51 1.1 thorpej
52 1.1 thorpej /* output buffer data */
53 1.15 rillig static ob_t ob;
54 1.1 thorpej
55 1.1 thorpej static void outxbuf(void);
56 1.1 thorpej
57 1.1 thorpej
58 1.1 thorpej /*
59 1.1 thorpej * initialize output
60 1.1 thorpej */
61 1.1 thorpej void
62 1.1 thorpej outopen(const char *name)
63 1.1 thorpej {
64 1.1 thorpej
65 1.1 thorpej loname = name;
66 1.1 thorpej
67 1.1 thorpej /* Open output file */
68 1.1 thorpej if ((lout = fopen(name, "w")) == NULL)
69 1.1 thorpej err(1, "cannot open '%s'", name);
70 1.1 thorpej
71 1.1 thorpej /* Create output buffer */
72 1.1 thorpej ob.o_len = 1024;
73 1.8 rillig ob.o_end = (ob.o_buf = ob.o_next = xmalloc(ob.o_len)) + ob.o_len;
74 1.1 thorpej }
75 1.1 thorpej
76 1.1 thorpej /*
77 1.1 thorpej * flush output buffer and close file
78 1.1 thorpej */
79 1.1 thorpej void
80 1.1 thorpej outclose(void)
81 1.1 thorpej {
82 1.1 thorpej
83 1.1 thorpej outclr();
84 1.1 thorpej if (fclose(lout) == EOF)
85 1.1 thorpej err(1, "cannot close '%s'", loname);
86 1.1 thorpej }
87 1.1 thorpej
88 1.1 thorpej /*
89 1.1 thorpej * resize output buffer
90 1.1 thorpej */
91 1.1 thorpej static void
92 1.1 thorpej outxbuf(void)
93 1.1 thorpej {
94 1.1 thorpej
95 1.20 rillig size_t next = (size_t)(ob.o_next - ob.o_buf);
96 1.1 thorpej ob.o_len *= 2;
97 1.20 rillig ob.o_buf = xrealloc(ob.o_buf, ob.o_len);
98 1.20 rillig ob.o_end = ob.o_buf + ob.o_len;
99 1.20 rillig ob.o_next = ob.o_buf + next;
100 1.1 thorpej }
101 1.1 thorpej
102 1.1 thorpej /*
103 1.1 thorpej * reset output buffer
104 1.1 thorpej * if it is not empty, it is flushed
105 1.1 thorpej */
106 1.1 thorpej void
107 1.1 thorpej outclr(void)
108 1.1 thorpej {
109 1.1 thorpej
110 1.8 rillig if (ob.o_buf != ob.o_next) {
111 1.1 thorpej outchar('\n');
112 1.20 rillig size_t sz = (size_t)(ob.o_next - ob.o_buf);
113 1.1 thorpej if (sz > ob.o_len)
114 1.18 rillig errx(1, "internal error: outclr");
115 1.1 thorpej if (fwrite(ob.o_buf, sz, 1, lout) != 1)
116 1.1 thorpej err(1, "cannot write to %s", loname);
117 1.8 rillig ob.o_next = ob.o_buf;
118 1.1 thorpej }
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej /*
122 1.1 thorpej * write a character to the output buffer
123 1.1 thorpej */
124 1.1 thorpej void
125 1.14 rillig outchar(char c)
126 1.1 thorpej {
127 1.1 thorpej
128 1.8 rillig if (ob.o_next == ob.o_end)
129 1.1 thorpej outxbuf();
130 1.14 rillig *ob.o_next++ = c;
131 1.1 thorpej }
132 1.1 thorpej
133 1.1 thorpej /*
134 1.7 rillig * write a string to the output buffer
135 1.1 thorpej * the string must not contain any characters which
136 1.1 thorpej * should be quoted
137 1.1 thorpej */
138 1.1 thorpej void
139 1.1 thorpej outstrg(const char *s)
140 1.1 thorpej {
141 1.1 thorpej
142 1.1 thorpej while (*s != '\0') {
143 1.8 rillig if (ob.o_next == ob.o_end)
144 1.1 thorpej outxbuf();
145 1.8 rillig *ob.o_next++ = *s++;
146 1.1 thorpej }
147 1.1 thorpej }
148 1.1 thorpej
149 1.15 rillig /* write an integer value to the output buffer */
150 1.1 thorpej void
151 1.1 thorpej outint(int i)
152 1.1 thorpej {
153 1.1 thorpej
154 1.11 rillig if ((size_t)(ob.o_end - ob.o_next) < 3 * sizeof(int))
155 1.1 thorpej outxbuf();
156 1.21 rillig ob.o_next += snprintf(ob.o_next, ob.o_end - ob.o_next, "%d", i);
157 1.1 thorpej }
158 1.1 thorpej
159 1.15 rillig /* write a name to the output buffer, preceded by its length */
160 1.1 thorpej void
161 1.15 rillig outname(const char *name)
162 1.1 thorpej {
163 1.1 thorpej outint((int)strlen(name));
164 1.1 thorpej outstrg(name);
165 1.1 thorpej }
166 1.1 thorpej
167 1.15 rillig /* write the name of the .c source */
168 1.1 thorpej void
169 1.1 thorpej outsrc(const char *name)
170 1.1 thorpej {
171 1.1 thorpej
172 1.1 thorpej outclr();
173 1.1 thorpej outchar('S');
174 1.1 thorpej outstrg(name);
175 1.1 thorpej }
176