annotate output.c @ 21:e3fab8f1b52c

strip comments.
author David A. Holland
date Mon, 20 Dec 2010 04:15:02 -0500
parents 40748b097655
children cef2dc916269
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
1 #include <unistd.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
2 #include <fcntl.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
3 #include <err.h>
40748b097655 add output.
David A. Holland
parents:
diff changeset
4
40748b097655 add output.
David A. Holland
parents:
diff changeset
5 #include "utils.h"
40748b097655 add output.
David A. Holland
parents:
diff changeset
6 #include "mode.h"
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
7 #include "place.h"
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
8 #include "output.h"
40748b097655 add output.
David A. Holland
parents:
diff changeset
9
40748b097655 add output.
David A. Holland
parents:
diff changeset
10 static int outputfd = -1;
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
11 static bool incomment = false;
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
12
40748b097655 add output.
David A. Holland
parents:
diff changeset
13 static
40748b097655 add output.
David A. Holland
parents:
diff changeset
14 void
40748b097655 add output.
David A. Holland
parents:
diff changeset
15 output_open(void)
40748b097655 add output.
David A. Holland
parents:
diff changeset
16 {
40748b097655 add output.
David A. Holland
parents:
diff changeset
17 outputfd = open(mode.output_file, O_WRONLY|O_CREAT|O_TRUNC, 0664);
40748b097655 add output.
David A. Holland
parents:
diff changeset
18 if (outputfd < 0) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
19 warn("%s", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
20 die();
40748b097655 add output.
David A. Holland
parents:
diff changeset
21 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
22 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
23
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
24 static
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
25 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
26 dowrite(const char *buf, size_t len)
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
27 {
40748b097655 add output.
David A. Holland
parents:
diff changeset
28 size_t done;
40748b097655 add output.
David A. Holland
parents:
diff changeset
29 ssize_t result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
30 static unsigned write_errors = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
31
40748b097655 add output.
David A. Holland
parents:
diff changeset
32 if (outputfd < 0) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
33 output_open();
40748b097655 add output.
David A. Holland
parents:
diff changeset
34 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
35
40748b097655 add output.
David A. Holland
parents:
diff changeset
36 /* XXX this will often come by ones and twos, should buffer */
40748b097655 add output.
David A. Holland
parents:
diff changeset
37
40748b097655 add output.
David A. Holland
parents:
diff changeset
38 done = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
39 while (done < len) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
40 result = write(outputfd, buf+done, len-done);
40748b097655 add output.
David A. Holland
parents:
diff changeset
41 if (result == -1) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
42 warn("%s: write", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
43 complain_failed();
40748b097655 add output.
David A. Holland
parents:
diff changeset
44 write_errors++;
40748b097655 add output.
David A. Holland
parents:
diff changeset
45 if (write_errors > 5) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
46 warnx("%s: giving up", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
47 die();
40748b097655 add output.
David A. Holland
parents:
diff changeset
48 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
49 /* XXX is this really a good idea? */
40748b097655 add output.
David A. Holland
parents:
diff changeset
50 sleep(1);
40748b097655 add output.
David A. Holland
parents:
diff changeset
51 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
52 done += (size_t)result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
53 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
54 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
55
40748b097655 add output.
David A. Holland
parents:
diff changeset
56 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
57 output(const struct place *p, const char *buf, size_t len)
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
58 {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
59 size_t pos, start;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
60 struct place p2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
61
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
62 start = 0;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
63 for (pos = 0; pos < len - 1; pos++) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
64 if (buf[pos] == '/' && buf[pos+1] == '*') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
65 if (incomment && warns.nestcomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
66 p2 = *p;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
67 p2.column += pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
68 complain(p, "Warning: %c%c within comment",
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
69 '/', '*');
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
70 if (mode.werror) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
71 complain_failed();
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
72 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
73 } else if (!incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
74 if (pos > start) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
75 dowrite(buf + start, pos - start);
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
76 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
77 start = pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
78 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
79 incomment = true;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
80 /* cancel out the loop's pos++ */
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
81 pos--;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
82 continue;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
83 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
84 } else if (buf[pos] == '*' && buf[pos+1] == '/') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
85 if (incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
86 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
87 if (mode.output_retain_comments) {
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 = false;
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 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
98 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
99 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
100
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
101 void
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
102 output_eof(void)
40748b097655 add output.
David A. Holland
parents:
diff changeset
103 {
40748b097655 add output.
David A. Holland
parents:
diff changeset
104 if (outputfd >= 0) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
105 close(outputfd);
40748b097655 add output.
David A. Holland
parents:
diff changeset
106 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
107 outputfd = -1;
40748b097655 add output.
David A. Holland
parents:
diff changeset
108 }