/* uncomment - remove c comments */ #include #include #include #include typedef enum { Norm, Slash, Star, Begin, End }; void uncomment(Biobuf * bi, Biobuf * bo) { int c; int state = Norm; while((c = Bgetc(bi)) != -1) switch(state){ case Norm: if(c == '/') state = Slash; else Bputc(bo, c); break; case Slash: switch (c){ case '*': state = Begin; break; case '/': state = End; break; default: Bputc(bo, '/'); Bputc(bo, c); state = Norm; break; } break; case Begin: if(c == '*') state = Star; break; case Star: switch (c){ case '/': state = Norm; break; case '*': break; default: state = Begin; break; } break; case End: if(c == '\n'){ Bputc(bo, c); state = Norm; } break; } } void usage(void) { fprint(2, "usage: %s file\n", argv0); exits("usage"); } void main(int argc, char **argv) { int i; Biobuf binp, bout, *bi, *bo; bi = &binp; bo = &bout; argv0 = argv[0]; Binit(bo, OWRITE, 1); if(argc == 1){ Binit(bi, OREAD, 0); uncomment(bi, bo); } for(i = 1; i < argc; i++){ if((bi = Bopen(argv[1], OREAD)) == nil){ fprint(2, "%s cannot open - %r\n", argv[1]); continue; } uncomment(bi, bo); Bterm(bo); } exits(nil); }