/* * C comment ispell filter * Reads C source from stdin, outputs noncomments as spaces, outputs comments verbatim. * Output is exact same length as input, as required by Ispell. * To be used with International Ispell's -F option. * Copyright 2003, Dan Kegel. Licensed under GPL. */ #include void output(char c) { /* don't output asterisks, it confuses some spellcheckers */ if (c == '*') c = ' '; putchar(c); } int main(int argc, char **argv) { int c; enum state_t { NONCOMMENT, SLASH, COMMENT, STAR, EOLCOMMENT }; enum state_t state = NONCOMMENT; while ((c = getchar()) != EOF) { switch (state) { case NONCOMMENT: if (c == '/') state = SLASH; if (c == '\n') output('\n'); else output(' '); break; case SLASH: if (c == '*') state = COMMENT; else if (c == '/') state = EOLCOMMENT; else { state = NONCOMMENT; } output(' '); break; case COMMENT: if (c == '*') state = STAR; output(c); break; case STAR: if (c == '/') state = NONCOMMENT; else if (c != '*') state = COMMENT; output(c); break; case EOLCOMMENT: if (c == '\n') state = NONCOMMENT; output(c); break; } } return 0; }