sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / tool / stripccomments.c
| 1 | /** |
| 2 | Strips C- and C++-style comments from stdin, sending the results to |
| 3 | stdout. It assumes that its input is legal C-like code, and does |
| 4 | only little error handling. |
| 5 | |
| 6 | It treats string literals as anything starting and ending with |
| 7 | matching double OR single quotes OR backticks (for use with |
| 8 | scripting languages which use those). It assumes that a quote |
| 9 | character within a string which uses the same quote type is escaped |
| 10 | by a backslash. It should not be used on any code which might |
| 11 | contain C/C++ comments inside heredocs, and similar constructs, as |
| 12 | it will strip those out. |
| 13 | |
| 14 | Usage: $0 [--keep-first|-k] < input > output |
| 15 | |
| 16 | The --keep-first (-k) flag tells it to retain the first comment in the |
| 17 | input stream (which is often a license or attribution block). It |
| 18 | may be given repeatedly, each one incrementing the number of |
| 19 | retained comments by one. |
| 20 | |
| 21 | License: Public Domain |
| 22 | Author: Stephan Beal (stephan@wanderinghorse.net) |
| 23 | */ |
| 24 | #include <stdio.h> |
| 25 | #include <assert.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #if 1 |
| 29 | #define MARKER(pfexp) \ |
| 30 | do{ printf("MARKER: %s:%d:\t",__FILE__,__LINE__); \ |
| 31 | printf pfexp; \ |
| 32 | } while(0) |
| 33 | #else |
| 34 | #define MARKER(exp) if(0) printf |
| 35 | #endif |
| 36 | |
| 37 | struct { |
| 38 | FILE * input; |
| 39 | FILE * output; |
| 40 | int rc; |
| 41 | int keepFirst; |
| 42 | } App = { |
| 43 | 0/*input*/, |
| 44 | 0/*output*/, |
| 45 | 0/*rc*/, |
| 46 | 0/*keepFirst*/ |
| 47 | }; |
| 48 | |
| 49 | void do_it_all(void){ |
| 50 | enum states { |
| 51 | S_NONE = 0 /* not in comment */, |
| 52 | S_SLASH1 = 1 /* slash - possibly comment prefix */, |
| 53 | S_CPP = 2 /* in C++ comment */, |
| 54 | S_C = 3 /* in C comment */ |
| 55 | }; |
| 56 | int ch, prev = EOF; |
| 57 | FILE * out = App.output; |
| 58 | int const slash = '/'; |
| 59 | int const star = '*'; |
| 60 | int line = 1; |
| 61 | int col = 0; |
| 62 | enum states state = S_NONE /* current state */; |
| 63 | int elide = 0 /* true if currently eliding output */; |
| 64 | int state3Col = -99 |
| 65 | /* huge kludge for odd corner case: */ |
| 66 | /*/ <--- here. state3Col marks the source column in which a C-style |
| 67 | comment starts, so that it can tell if star-slash inside a |
| 68 | C-style comment is the end of the comment or is the weird corner |
| 69 | case marked at the start of _this_ comment block. */; |
| 70 | for( ; EOF != (ch = fgetc(App.input)); prev = ch, |
| 71 | ++col){ |
| 72 | switch(state){ |
| 73 | case S_NONE: |
| 74 | if('\''==ch || '"'==ch || '`'==ch){ |
| 75 | /* Read string literal... |
| 76 | needed to properly catch comments in strings. */ |
| 77 | int const quote = ch, |
| 78 | startLine = line, startCol = col; |
| 79 | int ch2, escaped = 0, endOfString = 0; |
| 80 | fputc(ch, out); |
| 81 | for( ++col; !endOfString && EOF != (ch2 = fgetc(App.input)); |
| 82 | ++col ){ |
| 83 | switch(ch2){ |
| 84 | case '\\': escaped = !escaped; |
| 85 | break; |
| 86 | case '`': |
| 87 | case '\'': |
| 88 | case '"': |
| 89 | if(!escaped && quote == ch2) endOfString = 1; |
| 90 | escaped = 0; |
| 91 | break; |
| 92 | default: |
| 93 | escaped = 0; |
| 94 | break; |
| 95 | } |
| 96 | if('\n'==ch2){ |
| 97 | ++line; |
| 98 | col = 0; |
| 99 | } |
| 100 | fputc(ch2, out); |
| 101 | } |
| 102 | if(EOF == ch2){ |
| 103 | fprintf(stderr, "Unexpected EOF while reading %s literal " |
| 104 | "on line %d column %d.\n", |
| 105 | ('\''==ch) ? "char" : "string", |
| 106 | startLine, startCol); |
| 107 | App.rc = 1; |
| 108 | return; |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | else if(slash == ch){ |
| 113 | /* MARKER(("state 0 ==> 1 @ %d:%d\n", line, col)); */ |
| 114 | if( '\\'==prev ){ |
| 115 | /** |
| 116 | JS regexes may contain slash-asterisks, as happened at: |
| 117 | |
| 118 | https://github.com/emscripten-core/emscripten/issues/23412 |
| 119 | |
| 120 | Such regexes will always necessarily be preceded by a |
| 121 | backslash, though. |
| 122 | |
| 123 | It is hypothetically possible for a legitimate comment |
| 124 | slash-asterisk to appear immediately before a |
| 125 | backslash, but that seems like an even rarer corner |
| 126 | case than the JS regex case. |
| 127 | */ |
| 128 | fputc(ch, out); |
| 129 | }else{ |
| 130 | state = S_SLASH1; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | fputc(ch, out); |
| 135 | break; |
| 136 | case S_SLASH1: /* 1 slash */ |
| 137 | /* MARKER(("SLASH1 @ %d:%d App.keepFirst=%d\n", |
| 138 | line, col, App.keepFirst)); */ |
| 139 | switch(ch){ |
| 140 | case '*': |
| 141 | /* Enter C comment */ |
| 142 | if(App.keepFirst>0){ |
| 143 | elide = 0; |
| 144 | --App.keepFirst; |
| 145 | }else{ |
| 146 | elide = 1; |
| 147 | } |
| 148 | /*MARKER(("state 1 ==> 3 @ %d:%d\n", line, col));*/ |
| 149 | state = S_C; |
| 150 | state3Col = col-1; |
| 151 | if(!elide){ |
| 152 | fputc(prev, out); |
| 153 | fputc(ch, out); |
| 154 | } |
| 155 | break; |
| 156 | case '/': |
| 157 | /* Enter C++ comment */ |
| 158 | if(App.keepFirst>0){ |
| 159 | elide = 0; |
| 160 | --App.keepFirst; |
| 161 | }else{ |
| 162 | elide = 1; |
| 163 | } |
| 164 | /*MARKER(("state 1 ==> 2 @ %d:%d\n", line, col));*/ |
| 165 | state = S_CPP; |
| 166 | if(!elide){ |
| 167 | fputc(prev, out); |
| 168 | fputc(ch, out); |
| 169 | } |
| 170 | break; |
| 171 | default: |
| 172 | /* It wasn't a comment after all. */ |
| 173 | state = S_NONE; |
| 174 | if(!elide){ |
| 175 | fputc(prev, out); |
| 176 | fputc(ch, out); |
| 177 | } |
| 178 | } |
| 179 | break; |
| 180 | case S_CPP: /* C++ comment */ |
| 181 | if('\n' == ch){ |
| 182 | /* MARKER(("state 2 ==> 0 @ %d:%d\n", line, col)); */ |
| 183 | state = S_NONE; |
| 184 | elide = 0; |
| 185 | } |
| 186 | if(!elide){ |
| 187 | fputc(ch, out); |
| 188 | } |
| 189 | break; |
| 190 | case S_C: /* C comment */ |
| 191 | if(!elide){ |
| 192 | fputc(ch, out); |
| 193 | } |
| 194 | if(slash == ch){ |
| 195 | if(star == prev){ |
| 196 | /* MARKER(("state 3 ==> 0 @ %d:%d\n", line, col)); */ |
| 197 | /* Corner case which breaks this: */ |
| 198 | /*/ <-- slash there */ |
| 199 | /* That shows up twice in a piece of 3rd-party |
| 200 | code i use. */ |
| 201 | /* And thus state3Col was introduced :/ */ |
| 202 | if(col!=state3Col+2){ |
| 203 | state = S_NONE; |
| 204 | elide = 0; |
| 205 | state3Col = -99; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | break; |
| 210 | default: |
| 211 | assert(!"impossible!"); |
| 212 | break; |
| 213 | } |
| 214 | if('\n' == ch){ |
| 215 | ++line; |
| 216 | col = 0; |
| 217 | state3Col = -99; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void usage(char const *zAppName){ |
| 223 | fprintf(stderr, "Strips C- and C++-style comments from stdin and sends " |
| 224 | "the results to stdout.\n"); |
| 225 | fprintf(stderr, "Usage: %s [--keep-first|-k] < input > output\n", zAppName); |
| 226 | } |
| 227 | |
| 228 | int main( int argc, char const * const * argv ){ |
| 229 | int i; |
| 230 | for(i = 1; i < argc; ++i){ |
| 231 | char const * zArg = argv[i]; |
| 232 | while( '-'==*zArg ) ++zArg; |
| 233 | if( 0==strcmp(zArg,"k") |
| 234 | || 0==strcmp(zArg,"keep-first") ){ |
| 235 | ++App.keepFirst; |
| 236 | }else{ |
| 237 | usage(argv[0]); |
| 238 | return 1; |
| 239 | } |
| 240 | } |
| 241 | App.input = stdin; |
| 242 | App.output = stdout; |
| 243 | do_it_all(); |
| 244 | return App.rc ? 1 : 0; |
| 245 | } |