Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/misc/uncomment.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/* uncomment - remove c comments */

#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <bio.h>

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);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.