/* cmtzap.c A comment zapper Reads from stdin, writes to stdout Doesn't work with PREPROC'D quotes or comments. (c) Copyright 1998 PEG Holdings, Inc. V 1.01 24Jul98 Need to turn off have_tilde after "~~" V 1.02 24Jul98 Nested comments bug fixed. V 1.03 19Dec98 Slash Quote produces odd results. V 1.04 18Dec99 Having tired of hearing Joe Bob Briggs say "TNT.COM Forward Slash" as in the opposite of BackSlash, I've decided to rename the Backslash character to Burn. Thus, we have SLASH / and BURN \ */ #include #include void process_char ( int ch ); void examine_char ( int ch ); #define SQ '\'' #define DQ '"' #define SLASH '/' #define STAR '*' #define TILDE '~' #define BURN '\\' int in_a_quote = 0; int in_a_comment = 0; int have_tilde = 0; int have_star = 0; int have_slash = 0; int quote_char = '"'; main() { int c; while( ( c = getchar()) != EOF ) { examine_char ( c ); } } void examine_char ( int c ) { switch ( c ) { case SLASH : case STAR : case BURN : case TILDE : case DQ : case SQ : process_char( c ); break; default: if ( in_a_comment == 0 ) { if ( have_slash ) putchar (SLASH); putchar(c); } have_tilde = have_slash = have_star = 0; break; } } void process_char ( int ch ) { int c; if ( in_a_comment ) { /* In a comment, we worry only about slashes and stars */ if ( (ch == STAR) && have_slash ) { in_a_comment++; have_slash = have_star = 0; } else if ( ch == STAR ) { have_star = 1; in_a_quote = have_tilde = have_slash = 0; } else if ( (ch == SLASH) && have_star ) { in_a_comment--; in_a_quote = have_tilde = have_slash = have_star = 0; } else if ( ch == SLASH ) { have_slash = 1; } else { in_a_quote = have_tilde = have_slash = have_star = 0; } } else if ( in_a_quote ) { /* In a quote, we worry only about quote char, tilde and Bslash */ switch (ch) { case DQ : case SQ : if ( ( ch != quote_char ) || have_tilde ) { putchar( ch ); have_tilde = 0; in_a_comment = have_tilde = have_star = have_slash = 0; } else { /* ch == quote_char and not have tilde */ c = getchar(); if ( c == ch ) { putchar(ch); putchar(c); } else { putchar( ch ); in_a_quote = in_a_comment = have_tilde = have_star = have_slash = 0; if ( c != EOF ) examine_char (c) ; } } break; case TILDE : case BURN : have_tilde = ( have_tilde ? 0 : 1 ); putchar (ch); break; default: putchar(ch); break; } } else { switch ( ch ) { case SLASH : if ( have_slash ) putchar (SLASH) ; have_slash = 1; in_a_comment = in_a_quote = have_tilde = have_star = 0; break; case STAR : if ( have_slash ) { in_a_comment++ ; have_slash = have_star = have_tilde = in_a_quote = 0; } else { putchar(ch); } break; case SQ : case DQ : if ( have_slash ) { putchar (SLASH) ; have_slash = 0; } if ( have_tilde && ( ch == quote_char ) ) { putchar( ch ); have_slash = have_star = have_tilde = in_a_quote = 0; } else { quote_char = ch; in_a_quote = 1; in_a_comment = have_slash = have_tilde = have_star = 0; putchar (ch); } break; case TILDE : if ( have_slash ) putchar (SLASH) ; have_tilde = 1; in_a_comment = in_a_quote = have_slash = have_star = 0; putchar( ch ); break; default: if ( have_slash ) putchar (SLASH) ; in_a_comment = in_a_quote = have_slash = have_star = have_tilde = 0; putchar (ch); break; } } }