xref: /openbsd-src/libexec/tradcpp/output.c (revision f9343feac8314427583210aa64cbdde958db9030)
1a9b3ff1aSjsg /*-
2a9b3ff1aSjsg  * Copyright (c) 2010 The NetBSD Foundation, Inc.
3a9b3ff1aSjsg  * All rights reserved.
4a9b3ff1aSjsg  *
5a9b3ff1aSjsg  * This code is derived from software contributed to The NetBSD Foundation
6a9b3ff1aSjsg  * by David A. Holland.
7a9b3ff1aSjsg  *
8a9b3ff1aSjsg  * Redistribution and use in source and binary forms, with or without
9a9b3ff1aSjsg  * modification, are permitted provided that the following conditions
10a9b3ff1aSjsg  * are met:
11a9b3ff1aSjsg  * 1. Redistributions of source code must retain the above copyright
12a9b3ff1aSjsg  *    notice, this list of conditions and the following disclaimer.
13a9b3ff1aSjsg  * 2. Redistributions in binary form must reproduce the above copyright
14a9b3ff1aSjsg  *    notice, this list of conditions and the following disclaimer in the
15a9b3ff1aSjsg  *    documentation and/or other materials provided with the distribution.
16a9b3ff1aSjsg  *
17a9b3ff1aSjsg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18a9b3ff1aSjsg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19a9b3ff1aSjsg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20a9b3ff1aSjsg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21a9b3ff1aSjsg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22a9b3ff1aSjsg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23a9b3ff1aSjsg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24a9b3ff1aSjsg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25a9b3ff1aSjsg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26a9b3ff1aSjsg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27a9b3ff1aSjsg  * POSSIBILITY OF SUCH DAMAGE.
28a9b3ff1aSjsg  */
29a9b3ff1aSjsg 
30*f9343feaSjsg #include <stdio.h>
31a9b3ff1aSjsg #include <string.h>
32a9b3ff1aSjsg #include <unistd.h>
33a9b3ff1aSjsg #include <fcntl.h>
34a9b3ff1aSjsg #include <errno.h>
35a9b3ff1aSjsg 
36a9b3ff1aSjsg #include "utils.h"
37a9b3ff1aSjsg #include "mode.h"
38a9b3ff1aSjsg #include "place.h"
39a9b3ff1aSjsg #include "output.h"
40a9b3ff1aSjsg 
41a9b3ff1aSjsg static int outputfd = -1;
42a9b3ff1aSjsg static bool incomment = false;
43a9b3ff1aSjsg static char *linebuf;
44a9b3ff1aSjsg static size_t linebufpos, linebufmax;
45a9b3ff1aSjsg static struct place linebufplace;
46a9b3ff1aSjsg 
47a9b3ff1aSjsg static
48a9b3ff1aSjsg void
output_open(void)49a9b3ff1aSjsg output_open(void)
50a9b3ff1aSjsg {
51*f9343feaSjsg 	if (mode.output_file == NULL) {
52a9b3ff1aSjsg 		outputfd = STDOUT_FILENO;
53a9b3ff1aSjsg 	} else {
54a9b3ff1aSjsg 		outputfd = open(mode.output_file, O_WRONLY|O_CREAT|O_TRUNC,
55a9b3ff1aSjsg 				0664);
56a9b3ff1aSjsg 		if (outputfd < 0) {
57a9b3ff1aSjsg 			complain(NULL, "%s: %s",
58a9b3ff1aSjsg 				 mode.output_file, strerror(errno));
59a9b3ff1aSjsg 			die();
60a9b3ff1aSjsg 		}
61a9b3ff1aSjsg 	}
62a9b3ff1aSjsg }
63a9b3ff1aSjsg 
64a9b3ff1aSjsg static
65a9b3ff1aSjsg void
dowrite(const char * buf,size_t len)66a9b3ff1aSjsg dowrite(const char *buf, size_t len)
67a9b3ff1aSjsg {
68a9b3ff1aSjsg 	size_t done;
69a9b3ff1aSjsg 	ssize_t result;
70a9b3ff1aSjsg 	static unsigned write_errors = 0;
71a9b3ff1aSjsg 
72a9b3ff1aSjsg 	if (!mode.do_output) {
73a9b3ff1aSjsg 		return;
74a9b3ff1aSjsg 	}
75a9b3ff1aSjsg 
76a9b3ff1aSjsg 	if (outputfd < 0) {
77a9b3ff1aSjsg 		output_open();
78a9b3ff1aSjsg 	}
79a9b3ff1aSjsg 
80a9b3ff1aSjsg 	done = 0;
81a9b3ff1aSjsg 	while (done < len) {
82a9b3ff1aSjsg 		result = write(outputfd, buf+done, len-done);
83a9b3ff1aSjsg 		if (result == -1) {
84a9b3ff1aSjsg 			complain(NULL, "%s: write: %s",
85a9b3ff1aSjsg 				 mode.output_file, strerror(errno));
86a9b3ff1aSjsg 			complain_failed();
87a9b3ff1aSjsg 			write_errors++;
88a9b3ff1aSjsg 			if (write_errors > 5) {
89a9b3ff1aSjsg 				complain(NULL, "%s: giving up",
90a9b3ff1aSjsg 					 mode.output_file);
91a9b3ff1aSjsg 				die();
92a9b3ff1aSjsg 			}
93a9b3ff1aSjsg 			/* XXX is this really a good idea? */
94a9b3ff1aSjsg 			sleep(1);
95a9b3ff1aSjsg 		}
96a9b3ff1aSjsg 		done += (size_t)result;
97a9b3ff1aSjsg 	}
98a9b3ff1aSjsg }
99a9b3ff1aSjsg 
100a9b3ff1aSjsg 
101a9b3ff1aSjsg static
102a9b3ff1aSjsg void
filter_output(const char * buf,size_t len)103a9b3ff1aSjsg filter_output(const char *buf, size_t len)
104a9b3ff1aSjsg {
105a9b3ff1aSjsg 	size_t pos, start;
106a9b3ff1aSjsg 	bool inesc = false;
107a9b3ff1aSjsg 	bool inquote = false;
108a9b3ff1aSjsg 	char quote = '\0';
109a9b3ff1aSjsg 
110a9b3ff1aSjsg 	start = 0;
111a9b3ff1aSjsg 	for (pos = 0; pos < len - 1; pos++) {
112a9b3ff1aSjsg 		if (!inquote && buf[pos] == '/' && buf[pos+1] == '*') {
113a9b3ff1aSjsg 			if (!incomment) {
114a9b3ff1aSjsg 				if (pos > start) {
115a9b3ff1aSjsg 					dowrite(buf + start, pos - start);
116a9b3ff1aSjsg 				}
117a9b3ff1aSjsg 				start = pos;
118a9b3ff1aSjsg 				pos += 2;
119a9b3ff1aSjsg 				incomment = true;
120a9b3ff1aSjsg 				/* cancel out the loop's pos++ */
121a9b3ff1aSjsg 				pos--;
122a9b3ff1aSjsg 				continue;
123a9b3ff1aSjsg 			}
124a9b3ff1aSjsg 		} else if (buf[pos] == '*' && buf[pos+1] == '/') {
125a9b3ff1aSjsg 			if (incomment) {
126a9b3ff1aSjsg 				pos += 2;
127a9b3ff1aSjsg 				if (mode.output_retain_comments) {
128a9b3ff1aSjsg 					dowrite(buf + start, pos - start);
129a9b3ff1aSjsg 				}
130a9b3ff1aSjsg 				start = pos;
131a9b3ff1aSjsg 				incomment = false;
132a9b3ff1aSjsg 				/* cancel out the loop's pos++ */
133a9b3ff1aSjsg 				pos--;
134a9b3ff1aSjsg 				continue;
135a9b3ff1aSjsg 			}
136a9b3ff1aSjsg 		}
137a9b3ff1aSjsg 
138a9b3ff1aSjsg 		if (incomment) {
139a9b3ff1aSjsg 			/* nothing */
140a9b3ff1aSjsg 		} else if (inesc) {
141a9b3ff1aSjsg 			inesc = false;
142a9b3ff1aSjsg 		} else if (buf[pos] == '\\') {
143a9b3ff1aSjsg 			inesc = true;
144a9b3ff1aSjsg 		} else if (!inquote && (buf[pos] == '"' || buf[pos] == '\'')) {
145a9b3ff1aSjsg 			inquote = true;
146a9b3ff1aSjsg 			quote = buf[pos];
147a9b3ff1aSjsg 		} else if (inquote && buf[pos] == quote) {
148a9b3ff1aSjsg 			inquote = false;
149a9b3ff1aSjsg 		}
150a9b3ff1aSjsg 	}
151a9b3ff1aSjsg 	pos++;
152a9b3ff1aSjsg 
153a9b3ff1aSjsg 	if (pos > start) {
154a9b3ff1aSjsg 		if (!incomment || mode.output_retain_comments) {
155a9b3ff1aSjsg 			dowrite(buf + start, pos - start);
156a9b3ff1aSjsg 		}
157a9b3ff1aSjsg 	}
158a9b3ff1aSjsg }
159a9b3ff1aSjsg 
160a9b3ff1aSjsg void
output(const struct place * p,const char * buf,size_t len)161a9b3ff1aSjsg output(const struct place *p, const char *buf, size_t len)
162a9b3ff1aSjsg {
163a9b3ff1aSjsg 	size_t oldmax;
164a9b3ff1aSjsg 
165a9b3ff1aSjsg 	if (linebufpos + len > linebufmax) {
166a9b3ff1aSjsg 		oldmax = linebufmax;
167a9b3ff1aSjsg 		if (linebufmax == 0) {
168a9b3ff1aSjsg 			linebufmax = 64;
169a9b3ff1aSjsg 		}
170a9b3ff1aSjsg 		while (linebufpos + len > linebufmax) {
171a9b3ff1aSjsg 			linebufmax *= 2;
172a9b3ff1aSjsg 		}
173a9b3ff1aSjsg 		linebuf = dorealloc(linebuf, oldmax, linebufmax);
174a9b3ff1aSjsg 	}
175a9b3ff1aSjsg 	if (linebufpos == 0) {
176*f9343feaSjsg 		if (!place_samefile(&linebufplace, p)) {
177*f9343feaSjsg 			if (mode.output_cheaplinenumbers) {
178*f9343feaSjsg 				char str[256];
179*f9343feaSjsg 
180*f9343feaSjsg 				snprintf(str, sizeof(str), "# %u \"%s\"\n",
181*f9343feaSjsg 					 p->line, place_getname(p));
182*f9343feaSjsg 				dowrite(str, strlen(str));
183*f9343feaSjsg 			}
184*f9343feaSjsg 		}
185a9b3ff1aSjsg 		linebufplace = *p;
186a9b3ff1aSjsg 	}
187a9b3ff1aSjsg 	memcpy(linebuf + linebufpos, buf, len);
188a9b3ff1aSjsg 	linebufpos += len;
189a9b3ff1aSjsg 
190a9b3ff1aSjsg 	if (len == 1 && buf[0] == '\n') {
191a9b3ff1aSjsg 		filter_output(linebuf, linebufpos);
192a9b3ff1aSjsg 		linebufpos = 0;
193a9b3ff1aSjsg 	}
194a9b3ff1aSjsg }
195a9b3ff1aSjsg 
196a9b3ff1aSjsg void
output_eof(void)197a9b3ff1aSjsg output_eof(void)
198a9b3ff1aSjsg {
199a9b3ff1aSjsg 	if (mode.output_file != NULL && outputfd >= 0) {
200a9b3ff1aSjsg 		close(outputfd);
201a9b3ff1aSjsg 	}
202a9b3ff1aSjsg 	outputfd = -1;
203a9b3ff1aSjsg }
204