annotate output.c @ 28:8a955e3dda2c posted-20101220

two more tests, more fixes
author David A. Holland
date Mon, 20 Dec 2010 05:42:15 -0500
parents 01c3a2088ab4
children 76c114899f63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
1 #include <string.h>
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
2 #include <unistd.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
3 #include <fcntl.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
4 #include <err.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
5
40748b097655 add output.
David A. Holland
parents:
diff changeset
6 #include "utils.h"
40748b097655 add output.
David A. Holland
parents:
diff changeset
7 #include "mode.h"
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
8 #include "place.h"
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
9 #include "output.h"
40748b097655 add output.
David A. Holland
parents:
diff changeset
10
40748b097655 add output.
David A. Holland
parents:
diff changeset
11 static int outputfd = -1;
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
12 static bool incomment = false;
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
13 static char *linebuf;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
14 static size_t linebufpos, linebufmax;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
15 static struct place linebufplace;
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
16
40748b097655 add output.
David A. Holland
parents:
diff changeset
17 static
40748b097655 add output.
David A. Holland
parents:
diff changeset
18 void
40748b097655 add output.
David A. Holland
parents:
diff changeset
19 output_open(void)
40748b097655 add output.
David A. Holland
parents:
diff changeset
20 {
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
21 if (mode.output_file == NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
22 outputfd = STDOUT_FILENO;
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
23 } else {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
24 outputfd = open(mode.output_file, O_WRONLY|O_CREAT|O_TRUNC,
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
25 0664);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
26 if (outputfd < 0) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
27 warn("%s", mode.output_file);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
28 die();
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
29 }
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
30 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
31 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
32
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
33 static
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
34 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
35 dowrite(const char *buf, size_t len)
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
36 {
40748b097655 add output.
David A. Holland
parents:
diff changeset
37 size_t done;
40748b097655 add output.
David A. Holland
parents:
diff changeset
38 ssize_t result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
39 static unsigned write_errors = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
40
22
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
41 if (!mode.do_output) {
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
42 return;
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
43 }
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
44
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
45 if (outputfd < 0) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
46 output_open();
40748b097655 add output.
David A. Holland
parents:
diff changeset
47 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
48
40748b097655 add output.
David A. Holland
parents:
diff changeset
49 done = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
50 while (done < len) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
51 result = write(outputfd, buf+done, len-done);
40748b097655 add output.
David A. Holland
parents:
diff changeset
52 if (result == -1) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
53 warn("%s: write", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
54 complain_failed();
40748b097655 add output.
David A. Holland
parents:
diff changeset
55 write_errors++;
40748b097655 add output.
David A. Holland
parents:
diff changeset
56 if (write_errors > 5) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
57 warnx("%s: giving up", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
58 die();
40748b097655 add output.
David A. Holland
parents:
diff changeset
59 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
60 /* XXX is this really a good idea? */
40748b097655 add output.
David A. Holland
parents:
diff changeset
61 sleep(1);
40748b097655 add output.
David A. Holland
parents:
diff changeset
62 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
63 done += (size_t)result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
64 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
65 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
66
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
67
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
68 static
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
69 void
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
70 filter_output(const struct place *p, const char *buf, size_t len)
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
71 {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
72 size_t pos, start;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
73 struct place p2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
74
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
75 start = 0;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
76 for (pos = 0; pos < len - 1; pos++) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
77 if (buf[pos] == '/' && buf[pos+1] == '*') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
78 if (incomment && warns.nestcomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
79 p2 = *p;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
80 p2.column += pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
81 complain(p, "Warning: %c%c within comment",
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
82 '/', '*');
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
83 if (mode.werror) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
84 complain_failed();
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
85 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
86 } else if (!incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
87 if (pos > start) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
88 dowrite(buf + start, pos - start);
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
89 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
90 start = pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
91 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
92 incomment = true;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
93 /* cancel out the loop's pos++ */
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
94 pos--;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
95 continue;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
96 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
97 } else if (buf[pos] == '*' && buf[pos+1] == '/') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
98 if (incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
99 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
100 if (mode.output_retain_comments) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
101 dowrite(buf + start, pos - start);
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
102 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
103 start = pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
104 incomment = false;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
105 /* cancel out the loop's pos++ */
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
106 pos--;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
107 continue;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
108 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
109 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
110 }
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
111 pos++;
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
112
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
113 if (pos > start) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
114 if (!incomment || mode.output_retain_comments) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
115 dowrite(buf + start, pos - start);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
116 }
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
117 }
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
118 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
119
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
120 void
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
121 output(const struct place *p, const char *buf, size_t len)
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
122 {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
123 if (linebufpos + len > linebufmax) {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
124 if (linebufmax == 0) {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
125 linebufmax = 64;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
126 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
127 while (linebufpos + len > linebufmax) {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
128 linebufmax *= 2;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
129 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
130 linebuf = dorealloc(linebuf, linebufmax);
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
131 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
132 if (linebufpos == 0) {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
133 linebufplace = *p;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
134 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
135 memcpy(linebuf + linebufpos, buf, len);
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
136 linebufpos += len;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
137
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
138 if (len == 1 && buf[0] == '\n') {
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
139 filter_output(&linebufplace, linebuf, linebufpos);
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
140 linebufpos = 0;
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
141 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
142 }
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
143
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 24
diff changeset
144 void
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
145 output_eof(void)
40748b097655 add output.
David A. Holland
parents:
diff changeset
146 {
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
147 if (mode.output_file != NULL && outputfd >= 0) {
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
148 close(outputfd);
40748b097655 add output.
David A. Holland
parents:
diff changeset
149 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
150 outputfd = -1;
40748b097655 add output.
David A. Holland
parents:
diff changeset
151 }