Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/src/tiff/bconv.c

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


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

enum {
	Minbytes	= 64,
};

static void
putc(Biobuf *b, int c)
{
	if(c>=0)
		Bputc(b, c);
}

static void
bconv(Biobuf *bin, Biobuf *bout){
	int c;
	char b[Minbytes+1], *p, *e;

	b[Minbytes] = 0;
	e = &b[Minbytes];
	for(;;){
		switch(c = Bgetc(bin)){
		case -1:
			return;
		default:
			Bputc(bout, c);
			break;
		case '/':
			Bputc(bout, c);
			switch(c = Bgetc(bin)){
			default:
				putc(bout, c);
				break;
			case '*':
				do{
					putc(bout, c);
					c = Bgetc(bin);
					if(c < 0)
						break;
					if(c != '*')
						continue;
					Bputc(bout, c);
					c = Bgetc(bin);
				} while(c != '/');
				putc(bout, c);
				break;
			case '/':
				Bputc(bout, c);
				while((c = Bgetc(bin)) != '\n' && c>0)
					Bputc(bout, c);
				putc(bout, c);
			}
			break;
		case '"':
			Bputc(bout, c);
			for(;;){
				c = Bgetc(bin);
				switch(c){
				case -1:
				case '"':
					goto endstr;
				case '\\':
					Bputc(bout, c);
					c = Bgetc(bin);
				default:
					Bputc(bout, c);
				}
			}
		endstr:
			putc(bout, c);
			break;
		case '0':
			c = Bgetc(bin);
			if(c != 'b' && c != 'B'){
				Bputc(bout, '0');
				Bputc(bout, c);
				continue;
			}
			for(p = b; p < e && (c = Bgetc(bin)) == '0' || c == '1'; )
				*p++ = c;
			*p = 0;
			Bprint(bout, "0x%ulx", strtol(b, 0, 2));
			putc(bout, c);
			break;
		}
	}
}

void
main(int argc, char **argv)
{
	Biobuf b, bout, *p;
	int status;

	ARGBEGIN{
	default:
		fprint(2, "usage: bconv [file] ...\n");
		exits("usage");
	}ARGEND;

	status = 0;
	Binit(&bout, 1, OWRITE);

	if(*argv == nil){
		if(Binit(&b, 0, OREAD) == -1){
			fprint(2, "stdin: %r\n");
			exits("read");
		}
		bconv(&b, &bout);
		Bterm(&b);
		exits("");
	}

	for(; *argv; argv++)
		if(p = Bopen(*argv, OREAD)){
			bconv(p, &bout);
			Bterm(p);
		} else{
			fprint(2, "%s: %r\n", *argv);
			status = -1;
		}

	Bterm(&bout);
	if(status == -1)
		exits("read");
	exits("");
}

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.