1 #include <stdio.h>
2 #include <string.h>
3 
main(int argc,char * argv[])4 int main(int argc, char * argv[])
5 {
6     FILE *fpi, *fpo;
7     unsigned i = 0, j = 0;
8     // is_line_start is true to start out with
9     unsigned is_line_start = 1, is_slc = 0, is_mlc = 0;
10     unsigned last_is_slash = 0, last_is_star = 0;
11     char ibuf[1024], obuf[2048];
12 
13     if(argc != 3) {
14         printf("usage: cl2hdr <CLFILE> <HEADERFILE>\n");
15         return -1;
16     }
17 
18     fpi = fopen(argv[1], "r");
19     if(fpi < 0) {
20         printf("Unable to open %s for reading.\n", argv[1]);
21         return -1;
22     }
23 
24     fpo = fopen(argv[2], "w");
25     if(fpo < 0) {
26         printf("Unable to open %s for writing.\n", argv[2]);
27         return -1;
28     }
29 
30     fprintf(fpo, "%s", "#define KERNEL_STRING \"");
31 
32     memset(&ibuf, 0, sizeof(ibuf));
33 
34     while(fread(&ibuf, sizeof(char), 1024, fpi) > 0) {
35         j = 0;
36         memset(&obuf, '\0', sizeof(obuf));
37         for(i = 0; i < 1024 && ibuf[i] != '\0'; ++i) {
38             // if we're a single-line comment
39             if(last_is_slash && ibuf[i] == '/') {
40                 is_slc = 1;
41                 // only can be true if we're not a comment, which we are now
42                 last_is_slash = 0;
43                 continue;
44             }
45 
46             // if we're a multi-line comment
47             if(last_is_slash && ibuf[i] == '*') {
48                 is_mlc = 1;
49                 // only can be true if we're not a comment, which we are now
50                 last_is_slash = 0;
51                 continue;
52             }
53 
54             if(last_is_slash && !(is_mlc || is_slc)) {
55                 obuf[j++] = '/';
56             }
57 
58             /* last_is_star must also include is_mlc because
59                it can only be turned on during is_mlc,
60                and we don't print a '*' for last_is_star
61                because of just this fact. */
62             if(last_is_star && ibuf[i] == '/') {
63                 is_mlc = 0;
64                 continue;
65             }
66 
67             // check this corner-case, if the last of the buf is a slash
68             if(!(is_mlc || is_slc) && ibuf[i] == '/') {
69                 last_is_slash = 1;
70                 continue;
71                 // we'll print the slash if it's not a comment
72             } else {
73                 last_is_slash = 0;
74             }
75 
76             /* if we're a multi-line comment
77                we need to check for the terminal part */
78             if(is_mlc && ibuf[i] == '*') {
79                 last_is_star = 1;
80                 continue;
81                 // we'll print the star if it's not a comment
82             } else {
83                 last_is_star = 0;
84             }
85 
86             if(is_line_start && (ibuf[i] == '\t' || ibuf[i] == ' ')) {
87                 continue;
88             }
89 
90             /* if it's a newline, escape it, add it, and continue
91                ignoring comments, because we want to preserve line numbers */
92             if (ibuf[i] == '\n') {
93                 // get rid of trailing whitespace
94                 for(; j > 1 && (obuf[j-1] == ' ' || obuf[j-1] == '\t'); --j);
95                 obuf[j++] = '\\';
96                 obuf[j++] = 'n';
97                 is_line_start = 1;
98                 is_slc = 0;
99                 continue;
100             } else {
101                 is_line_start = 0;
102             }
103 
104             // if it's a comment or we're at the start of a line
105             // and we have whitespace
106             if(is_slc || is_mlc) {
107                 continue;
108             }
109 
110             // if it's a double-quote or backslash, escape it
111             if(ibuf[i] == '"' || ibuf[i] == '\\') {
112                 obuf[j++] = '\\';
113             }
114 
115             obuf[j++] = ibuf[i];
116         }
117         // make sure we terminate the output buffer properly
118         obuf[j] = '\0';
119         fprintf(fpo, "%s", obuf);
120         memset(&ibuf, '\0', sizeof(ibuf));
121     }
122 
123     fprintf(fpo, "%s", "\\n\"\n\n");
124 
125     return 0;
126 }
127