stripccomments.c 6.4 KB

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