annotate output.c @ 22:cef2dc916269

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