int ch, fd, i, iflag, max_colors, did_reset; long val;  iflag = 0; did_reset = 0;  fd = initialize(_PATH_CONSOLE, &is, &ib, &newis, &newib);  while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:f:h:ip:t:v:w:x:y:")) != EOF) { switch (ch) { case 'D':
int ch, fd, iflag, max_colors; int use_is, use_ib;  max_colors = use_is = use_ib = iflag = 0;   while ((ch = getopt(argc, argv, "B:D:H:P:T:V:W:X:Y:b:d:f:h:ip:t:v:w:x:y:")) != -1) { switch (tolower(ch)) { case 'd': case 'h': case 'w': case 'x': case 'y': use_is = 1; break; case 'f': file = optarg; break; case 'p': case 't': case 'v': use_ib = 1; break; case 'b': case 'i': break; default: usage(); } }  fd = open(file, O_RDONLY, O_NONBLOCK); if(fd == -1) err(1, "open \"%s\"", file);  if (argc > optind) use_is = 1;  if (use_is) { xioctl(fd, ITEIOCGWINSZ, &is); memcpy(&newis, &is, sizeof is); max_colors = 1 << is.depth; } if (use_ib) { xioctl(fd, ITEIOCGBELL, &ib); memcpy(&newib, &ib, sizeof ib); }  optind = 1; optreset = 1;   while ((ch = getopt(argc, argv, "B:D:H:P:T:V:W:X:Y:b:d:f:h:ip:t:v:w:x:y:")) != -1) { switch (tolower(ch)) { case 'i': iflag = 1; break; case 'f': break;
colormap_t *xgetcmap __P((int, int));long	xstrtol __P((char *));int	initialize __P((char *, struct itewinsize *, struct itebell *,			struct itewinsize *, struct itebell *));intmain(argc, argv)	int argc;	char **argv;{	struct itewinsize is, newis;	struct itebell ib, newib;	struct winsize ws;	colormap_t *cm;	char *file = _PATH_CONSOLE;	int ch, fd, i, iflag, max_colors, did_reset;	long val;	iflag = 0;	did_reset = 0;	fd = initialize(_PATH_CONSOLE, &is, &ib, &newis, &newib);	while ((ch = getopt(argc, argv, "D:H:P:T:V:W:X:Y:d:f:h:ip:t:v:w:x:y:"))	    != EOF) {		switch (ch) {		case 'D':		/* undocumented backward compat */		case 'd':			newis.depth = xstrtol(optarg);			break;		case 'f':			if (did_reset)				break;			if (fd != -1)				close(fd);			file = optarg;			fd = initialize(optarg, &is, &ib, &newis, &newib);			did_reset = optreset = optind = 1;			break;		case 'H':		/* undocumented backward compat */		case 'h':			newis.height = xstrtol(optarg);			break;		case 'i':			iflag = 1;			break;		case 'p':			newib.pitch = xstrtol(optarg);			break;		case 't':			newib.msec = xstrtol(optarg);			break;		case 'V':		/* undocumented backward compat */		case 'v':			newib.volume = xstrtol(optarg);			break;		case 'W':		/* undocumented backward compat */		case 'w':			newis.width = xstrtol(optarg);			break;		case 'X':		/* undocumented backward compat */		case 'x':			newis.x = xstrtol(optarg);			break;		case 'Y':		/* undocumented backward compat */		case 'y':			newis.y = xstrtol(optarg);			break;		case '?':		default:			usage();			/* NOTREACHED */		}	}	argc -= optind;	argv += optind;	if(fd == -1)		err(1, "open \"%s\"", file);	if (memcmp(&newis, &is, sizeof(is))) {		xioctl(fd, ITEIOCSWINSZ, &newis);		xioctl(fd, ITEIOCGWINSZ, &is);	}	if (memcmp(&newib, &ib, sizeof(ib))) {		xioctl(fd, ITEIOCSBELL, &newib);		xioctl(fd, ITEIOCGBELL, &ib);	}		/*	 * get, set and get colors again	 */	i = 0;	max_colors = 1 << is.depth;	cm = xgetcmap(fd, max_colors);	while (argc--) {		val = xstrtol(*argv++);		if (i >= max_colors) {			warnx("warning: too many colors");			break;		}		cm->entry[i] = val;		i++;	}	xioctl(fd, VIOCSCMAP, cm);	free(cm);	cm = xgetcmap(fd, max_colors);	/* do tty stuff to get it to register the changes. */	xioctl(fd, TIOCGWINSZ, &ws);	if (iflag) {		printf("tty size: rows %d cols %d\n", ws.ws_row, ws.ws_col);		printf("ite size: w: %d  h: %d  d: %d  [x: %d  y: %d]\n",		    is.width, is.height, is.depth, is.x, is.y);		printf("ite bell: vol: %d  millisec: %d  pitch: %d\n",		    ib.volume, ib.msec, ib.pitch);		printcmap(cm, ws.ws_col);	}	close(fd);	exit(0);}voidxioctl(fd, cmd, addr)	int fd, cmd;	void *addr;{	if (ioctl(fd, cmd, addr) == -1) 		err(1, "ioctl");}longxstrtol(s)	char *s;{	long rv;	rv = strtol(s, NULL, 0);	if (errno == ERANGE && (rv == LONG_MIN || rv == LONG_MAX))		err(1, "bad format: \"%s\"", s);	return(rv);}colormap_t *xgetcmap(fd, ncolors)	int fd;	int ncolors;{	colormap_t *cm;	cm = malloc(sizeof(colormap_t) + ncolors * sizeof(u_long));	if (cm == NULL)		err(1, "malloc");	cm->first = 0;	cm->size = ncolors;	cm->entry = (u_long *) & cm[1];	xioctl(fd, VIOCGCMAP, cm);	return(cm);}