annotate output.c @ 30:76c114899f63

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