Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copied from liwc, a collection of tools for manipulating C source code
* Retrieved from https://github.com/ajkaijanaho/liwc
*
* Copyright (c) 2012 Antti-Juhani Kaijanaho
* Copyright (c) 1994-2003 Lars Wirzenius
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
/*
* ccmtcnvt.c -- convert C++ comments to C comments
*
* Usage: ccmtcnvt [-hv] [--help] [--version] <file>
* All output is to the standard output.
*
* This program converts the C++ style comments into traditional C style
* comments.
*
* This is written as a finite state machine (FSM), which reads (presumably
* correct) C code, and when it finds a // that is not inside a comment, or
* string or character literal, it converts it to a C start of comment and
* adds a space and a comment delimiter (unreproducible here :-) before the
* next newline.
*
* Known problems:
*
* a) Include file names are not recognized being special, so that
*
* #include <sys//stat.h>
*
* will be mangled. I've never seen such names used in source code, though,
* so I don't think ignoring them will cause too much harm.
*
* b) Does not understand trigraphs or digraphs.
*
* c) Does not understand line continuation (\ at end of line) outside
* string literals and character constants.
*
* Patches for fixing these are welcome. They're not common enough for me
* to worry about them, at least for now.
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* Special marks for input characters used in the description of the FSM.
*/
#define DFL (int)UCHAR_MAX+1 /* any character */
#define NONE (int)UCHAR_MAX+2 /* don't print any character */
#define SELF (int)UCHAR_MAX+3 /* print last input character */
/*
* Possible states for the FSM.
*/
enum state {
code, chr_lit, chr_esc, str_lit, str_esc,
slash, cpp_cmt, cpp_ast, c_cmt, star
};
/*
* Rules for describing the state changes and associated actions for the
* FSM.
*/
struct rule {
enum state state;
int c;
enum state new_state;
int print1, print2, print3, print4;
const char *warning;
};
/*
* The FSM itself.
*/
static const struct rule fsm[] = {
{ code, '"', str_lit, SELF, NONE, NONE, NONE, NULL },
{ code, '\'', chr_lit, SELF, NONE, NONE, NONE, NULL },
{ code, '/', slash, NONE, NONE, NONE, NONE, NULL },
{ code, DFL, code, SELF, NONE, NONE, NONE, NULL },
{ str_lit, '\\', str_esc, SELF, NONE, NONE, NONE, NULL },
{ str_lit, '"', code, SELF, NONE, NONE, NONE, NULL },
{ str_lit, DFL, str_lit, SELF, NONE, NONE, NONE, NULL },
{ str_esc, DFL, str_lit, SELF, NONE, NONE, NONE, NULL },
{ chr_lit, '\\', chr_esc, SELF, NONE, NONE, NONE, NULL },
{ chr_lit, '\'', code, SELF, NONE, NONE, NONE, NULL },
{ chr_lit, DFL, chr_lit, SELF, NONE, NONE, NONE, NULL },
{ chr_esc, DFL, chr_lit, SELF, NONE, NONE, NONE, NULL },
{ slash, '/', cpp_cmt, '/', '*', NONE, NONE, NULL },
{ slash, '*', c_cmt, '/', '*', NONE, NONE, NULL },
{ slash, DFL, code, '/', SELF, NONE, NONE, NULL },
{ cpp_cmt, '\n', code, ' ', '*', '/', SELF, NULL },
{ cpp_cmt, '*', cpp_ast, SELF, NONE, NONE, NONE, NULL },
{ cpp_cmt, DFL, cpp_cmt, SELF, NONE, NONE, NONE, NULL },
{ cpp_ast, '\n', code, ' ', '*', '/', SELF, NULL },
{ cpp_ast, '*', cpp_ast, SELF, NONE, NONE, NONE, NULL },
{ cpp_ast, '/', cpp_cmt, ' ', SELF, NONE, NONE,
"converted a '*/' inside a C++ comment to '* /'." },
{ cpp_ast, DFL, cpp_cmt, SELF, NONE, NONE, NONE, NULL },
{ c_cmt, '*', star, SELF, NONE, NONE, NONE, NULL },
{ c_cmt, DFL, c_cmt, SELF, NONE, NONE, NONE, NULL },
{ star, '/', code, SELF, NONE, NONE, NONE, NULL },
{ star, '*', star, SELF, NONE, NONE, NONE, NULL },
{ star, DFL, c_cmt, SELF, NONE, NONE, NONE, NULL },
};
static int convert(char *filename);
static char usage[] = "Usage: %s [-hv] <file>\n";
static char version[] = "version 1.1-without-publib\n";
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "hv")) != EOF) {
switch (opt) {
case 'h':
case '?':
fprintf(stderr, usage, argv[0]);
case 'v':
fprintf(stderr, "%s", version);
}
}
if (optind != argc-1) {
fprintf(stderr, usage, argv[0]);
return EXIT_FAILURE;
}
if (convert(argv[optind]) != 0) {
fprintf(stderr, "conversion incomplete.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#define print(x) (void)((x) != NONE && printf("%c", (x) == SELF ? c : (x)))
static int convert(char *filename) {
enum state state;
const struct rule *p;
int c, i;
FILE *f;
f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "couldn't open file %s for reading: %s\n",
filename, strerror(errno));
return -1;
}
state = code;
for (;;) {
c = getc(f);
if (c == EOF) {
if (ferror(f)) {
fprintf(stderr, "error reading %s: %s",
filename, strerror(errno));
return -1;
}
switch (state) {
case cpp_cmt:
/* need to terminate the last // comment */
printf(" */");
break;
case code:
/* do nothing 'cause all's good */
break;
default:
fprintf(stderr, "%s error, %s ends funnily",
__func__, filename);
return -1;
}
if (fflush(stdout) == EOF || ferror(stdout))
return -1;
return 0; /* everything seems to be OK */
}
for (i = 0; i < sizeof(fsm) / sizeof(fsm[0]); ++i) {
p = &fsm[i];
if (p->state == state && (p->c == c || p->c == DFL)) {
state = p->new_state;
print(p->print1);
print(p->print2);
print(p->print3);
print(p->print4);
if (p->warning != NULL)
fprintf(stderr, "%s %s %s", __func__,
filename, p->warning);
break;
}
}
}
/*NOTREACHED*/
}