annotate output.c @ 24:daa801fe719e

fix some bugs
author David A. Holland
date Mon, 20 Dec 2010 04:30:02 -0500
parents cef2dc916269
children 01c3a2088ab4
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 {
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
17 if (mode.output_file == NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
18 outputfd = STDOUT_FILENO;
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
19 } else {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
20 outputfd = open(mode.output_file, O_WRONLY|O_CREAT|O_TRUNC,
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
21 0664);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
22 if (outputfd < 0) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
23 warn("%s", mode.output_file);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
24 die();
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
25 }
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
26 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
27 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
28
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
29 static
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
30 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
31 dowrite(const char *buf, size_t len)
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
32 {
40748b097655 add output.
David A. Holland
parents:
diff changeset
33 size_t done;
40748b097655 add output.
David A. Holland
parents:
diff changeset
34 ssize_t result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
35 static unsigned write_errors = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
36
22
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
37 if (!mode.do_output) {
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
38 return;
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
39 }
cef2dc916269 honor mode.do_output
David A. Holland
parents: 21
diff changeset
40
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
41 if (outputfd < 0) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
42 output_open();
40748b097655 add output.
David A. Holland
parents:
diff changeset
43 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
44
40748b097655 add output.
David A. Holland
parents:
diff changeset
45 /* XXX this will often come by ones and twos, should buffer */
40748b097655 add output.
David A. Holland
parents:
diff changeset
46
40748b097655 add output.
David A. Holland
parents:
diff changeset
47 done = 0;
40748b097655 add output.
David A. Holland
parents:
diff changeset
48 while (done < len) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
49 result = write(outputfd, buf+done, len-done);
40748b097655 add output.
David A. Holland
parents:
diff changeset
50 if (result == -1) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
51 warn("%s: write", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
52 complain_failed();
40748b097655 add output.
David A. Holland
parents:
diff changeset
53 write_errors++;
40748b097655 add output.
David A. Holland
parents:
diff changeset
54 if (write_errors > 5) {
40748b097655 add output.
David A. Holland
parents:
diff changeset
55 warnx("%s: giving up", mode.output_file);
40748b097655 add output.
David A. Holland
parents:
diff changeset
56 die();
40748b097655 add output.
David A. Holland
parents:
diff changeset
57 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
58 /* XXX is this really a good idea? */
40748b097655 add output.
David A. Holland
parents:
diff changeset
59 sleep(1);
40748b097655 add output.
David A. Holland
parents:
diff changeset
60 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
61 done += (size_t)result;
40748b097655 add output.
David A. Holland
parents:
diff changeset
62 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
63 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
64
40748b097655 add output.
David A. Holland
parents:
diff changeset
65 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
66 output(const struct place *p, const char *buf, size_t len)
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
67 {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
68 size_t pos, start;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
69 struct place p2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
70
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
71 start = 0;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
72 for (pos = 0; pos < len - 1; pos++) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
73 if (buf[pos] == '/' && buf[pos+1] == '*') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
74 if (incomment && warns.nestcomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
75 p2 = *p;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
76 p2.column += pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
77 complain(p, "Warning: %c%c within comment",
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
78 '/', '*');
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
79 if (mode.werror) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
80 complain_failed();
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
81 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
82 } else if (!incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
83 if (pos > start) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
84 dowrite(buf + start, pos - start);
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
85 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
86 start = pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
87 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
88 incomment = true;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
89 /* cancel out the loop's pos++ */
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
90 pos--;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
91 continue;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
92 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
93 } else if (buf[pos] == '*' && buf[pos+1] == '/') {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
94 if (incomment) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
95 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
96 if (mode.output_retain_comments) {
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
97 dowrite(buf + start, pos - start);
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
98 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
99 start = pos;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
100 pos += 2;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
101 incomment = false;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
102 /* cancel out the loop's pos++ */
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
103 pos--;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
104 continue;
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
105 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
106 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
107 }
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
108 pos++;
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
109
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
110 if (pos > start) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
111 if (!incomment || mode.output_retain_comments) {
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
112 dowrite(buf + start, pos - start);
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
113 }
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
114 }
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
115 }
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
116
e3fab8f1b52c strip comments.
David A. Holland
parents: 20
diff changeset
117 void
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
118 output_eof(void)
40748b097655 add output.
David A. Holland
parents:
diff changeset
119 {
24
daa801fe719e fix some bugs
David A. Holland
parents: 22
diff changeset
120 if (mode.output_file != NULL && outputfd >= 0) {
20
40748b097655 add output.
David A. Holland
parents:
diff changeset
121 close(outputfd);
40748b097655 add output.
David A. Holland
parents:
diff changeset
122 }
40748b097655 add output.
David A. Holland
parents:
diff changeset
123 outputfd = -1;
40748b097655 add output.
David A. Holland
parents:
diff changeset
124 }