case KEYB_R_ACK:
sgetc(int noblock){#if PCVT_KBD_FIFO	int		s;#endif	u_char *cp, dt, key;	u_short	type;	static u_char kbd_lastkey = 0; /* last keystroke */#ifdef XSERVER	static char	keybuf[2] = {0}; /* the second 0 is a delimiter! */#endif /* XSERVER */	static struct {		u_char extended: 1;	/* extended prefix seen */		u_char ext1: 1;		/* extended prefix 1 seen */		u_char breakseen: 1;	/* break code seen */		u_char vshift: 1;	/* virtual shift pending */		u_char vcontrol: 1;	/* virtual control pending */		u_char sysrq: 1;	/* sysrq pressed */	} kbd_status = {0};loop:#ifdef XSERVER#if PCVT_KBD_FIFO	if (noblock == 31337) {		vsp->scrolling = 1;		goto scroll_reset;	}	/* see if there is data from the keyboard available either from */	/* the keyboard fifo or from the 8042 keyboard controller	*/	if ((( noblock) && (pcvt_kbd_count)) ||	    ((!noblock) && (inb(CONTROLLER_CTRL) & STATUS_OUTPBF))) {		if (!noblock) {		/* source = 8042 */			PCVT_KBD_DELAY();	/* 7 us delay */			dt = inb(CONTROLLER_DATA);	/* get from obuf */		}		else {			/* source = keyboard fifo */			dt = pcvt_kbd_fifo[pcvt_kbd_rptr++];			s = spltty();			pcvt_kbd_count--;			splx(s);			if (pcvt_kbd_rptr >= PCVT_KBD_FIFO_SZ)				pcvt_kbd_rptr = 0;		}#else /* !PCVT_KB_FIFO */	/* see if there is data from the keyboard available from the 8042 */	if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF) {		PCVT_KBD_DELAY();		/* 7 us delay */		dt = inb(CONTROLLER_DATA);	/* yes, get data */#endif /* !PCVT_KBD_FIFO */		/*		 * If x mode is active, only care for locking keys, then		 * return the scan code instead of any key translation.		 * Additionally, this prevents us from any attempts to		 * execute pcvt internal functions caused by keys (such		 * as screen flipping).		 */ 		if (vsp->kbd_state == K_RAW) {			keybuf[0] = dt;			return ((u_char *)keybuf);		}	}#else /* !XSERVER */#  if PCVT_KBD_FIFO	/* see if there is data from the keyboard available either from */	/* the keyboard fifo or from the 8042 keyboard controller	*/	if ((( noblock) && (pcvt_kbd_count)) ||	    ((!noblock) && (inb(CONTROLLER_CTRL) & STATUS_OUTPBF))) {		if (!noblock) {		/* source = 8042 */			PCVT_KBD_DELAY();	/* 7 us delay */			dt = inb(CONTROLLER_DATA);		}		else {			/* source = keyboard fifo */			dt = pcvt_kbd_fifo[pcvt_kbd_rptr++]; /* yes, get it ! */			s = spltty();			pcvt_kbd_count--;			splx(s);			if (pcvt_kbd_rptr >= PCVT_KBD_FIFO_SZ)				pcvt_kbd_rptr = 0;		}	}#else /* !PCVT_KBD_FIFO */	/* see if there is data from the keyboard available from the 8042 */	if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF) {		PCVT_KBD_DELAY();		/* 7 us delay */		dt = inb(CONTROLLER_DATA);	/* yes, get data ! */	}#endif /* !PCVT_KBD_FIFO */#endif /* !XSERVER */	else {		if (noblock)			return (NULL);		else			goto loop;	}	/* lets look what we got */	switch (dt) {	case KEYB_R_OVERRUN0:	/* keyboard buffer overflow */#if PCVT_SCANSET == 2	case KEYB_R_SELFOK:	/* keyboard selftest ok */#endif /* PCVT_SCANSET == 2 */	case KEYB_R_ECHO:	/* keyboard response to KEYB_C_ECHO */	case KEYB_R_ACK:	/* acknowledge after command has rx'd*/	case KEYB_R_SELFBAD:	/* keyboard selftest FAILED */	case KEYB_R_DIAGBAD:	/* keyboard self diagnostic failure */	case KEYB_R_RESEND:	/* keyboard wants us to resend cmnd */	case KEYB_R_OVERRUN1:	/* keyboard buffer overflow */		break;	case KEYB_R_EXT1:	/* keyboard extended scancode pfx 2 */		kbd_status.ext1 = 1;		/* FALLTHROUGH */	case KEYB_R_EXT0:	/* keyboard extended scancode pfx 1 */		kbd_status.extended = 1;		break;#if PCVT_SCANSET == 2	case KEYB_R_BREAKPFX:	/* break code prefix for set 2 and 3 */		kbd_status.breakseen = 1;		break;#endif /* PCVT_SCANSET == 2 */	default:		goto regular;	/* regular key */	}	if (noblock)		return (NULL);	else		goto loop;	/* got a normal scan key */regular:#if PCVT_SCANSET == 1	kbd_status.breakseen = dt & 0x80 ? 1 : 0;	dt &= 0x7f;#endif	/* PCVT_SCANSET == 1 */	/*   make a keycode from scan code	*/	if (dt >= sizeof scantokey / sizeof(u_char))		key = 0;	else		key = kbd_status.extended ? extscantokey[dt] : scantokey[dt];	if (kbd_status.ext1 && key == 64)		/* virtual control key */		key = 129;	kbd_status.extended = kbd_status.ext1 = 0;	if ((key == 85) && shift_down &&	    (kbd_lastkey != 85 || !kbd_status.breakseen)) {		/* removing of visual regions for mouse console support */		if (IS_SEL_EXISTS(vsp)) 			remove_selection(); /* remove current selection before 					       leaving this screen */		if (IS_MOUSE_VISIBLE(vsp)) {			mouse_hide();			vsp->mouse_flags &= ~MOUSE_VISIBLE;		}		/* end of visual regions part */		if (vsp->scr_offset && vsp->scr_offset >= vsp->row) {			if (!vsp->scrolling) {				vsp->scrolling += vsp->row - 1;				if (vsp->Scrollback) {					scrollback_save_screen();					if (vsp->scr_offset == vsp->max_off) {						bcopy(vsp->Scrollback +						      vsp->maxcol,						      vsp->Scrollback,						      vsp->maxcol *						      vsp->max_off * CHR);						vsp->scr_offset--;					}					bcopy(vsp->Crtat + vsp->cur_offset -					      vsp->col, vsp->Scrollback +				      	      (vsp->scr_offset + 1) *					      vsp->maxcol, vsp->maxcol * CHR);				}				if (vsp->cursor_on)					sw_cursor(0);			}			vsp->scrolling += vsp->screen_rows - 1;			if (vsp->scrolling > vsp->scr_offset)				vsp->scrolling = vsp->scr_offset;			bcopy(vsp->Scrollback + ((vsp->scr_offset -			      vsp->scrolling) * vsp->maxcol), vsp->Crtat,			      vsp->screen_rows * vsp->maxcol * CHR);		}		kbd_lastkey = 85;		goto loop;	}	else if ((key == 86) && shift_down &&		(kbd_lastkey != 86 || !kbd_status.breakseen)) {scroll_reset:		/* removing of visual regions for mouse console support */		if (IS_SEL_EXISTS(vsp)) 			remove_selection(); /* remove current selection before 					       leaving this screen */		if (IS_MOUSE_VISIBLE(vsp)) {			mouse_hide();			vsp->mouse_flags &= ~MOUSE_VISIBLE;		}		/* end of visual regions part */		if (vsp->scrolling > 0) {			vsp->scrolling -= vsp->screen_rows - 1;			if (vsp->scrolling < 0)				vsp->scrolling = 0;			if (vsp->scrolling <= vsp->row) {				vsp->scrolling = 0;				scrollback_restore_screen();			}			else {				if (vsp->scrolling + 2 < vsp->screen_rows)					fillw(user_attr | ' ',					      (caddr_t)vsp->Crtat,					      vsp->screen_rows * vsp->maxcol);				bcopy(vsp->Scrollback + ((vsp->scr_offset -			      	      vsp->scrolling) * vsp->maxcol),			              vsp->Crtat, (vsp->scrolling + 2 <				      vsp->screen_rows ? vsp->scrolling + 2 :				      vsp->screen_rows) * vsp->maxcol * CHR);			}		}		if (vsp->scrolling == 0) {			if (vsp->cursor_on)				sw_cursor(1);		}		if (noblock == 31337)			return (NULL);		if (key != 86)			goto regular;		else {			kbd_lastkey = 86;			goto loop;		}	}	else if (vsp->scrolling && key != 128 && key != 44 && key != 57 &&		 key != 85 && key != 86) {			vsp->scrolling = 1;			goto scroll_reset;	     }	if (kbd_reset && (key == 76) && ctrl_down && (meta_down||altgr_down)) {		printf("\nconsole halt requested: going down.\n");		kbd_reset = 0;		psignal(initproc, SIGUSR1);	}#if NDDB > 0 || defined(DDB)		 /*   Check for cntl-alt-esc	*/  	if ((key == 110) && ctrl_down && (meta_down || altgr_down)) { 		static u_char in_Debugger; 		if (!in_Debugger) { 			in_Debugger = 1;			if (db_console)	 			Debugger(); 			in_Debugger = 0; 			if (noblock) 				return (NULL); 			else 				goto loop; 		} 	}#endif /* NDDB > 0 || defined(DDB) */	/* look for keys with special handling */	if (key == 128) {		/*		 * virtual shift; sent around PrtScr, and around the arrow		 * keys if the NumLck LED is on		 */		kbd_status.vshift = !kbd_status.breakseen;		key = 0;	/* no key */	}	else if (key == 129) {		/*		 * virtual control - the most ugly thingie at all		 * the Pause key sends:		 * <virtual control make> <numlock make> <virtual control		 * break> <numlock break>		 */		if (!kbd_status.breakseen)			kbd_status.vcontrol = 1;		/* else: let the numlock hook clear this */		key = 0;	/* no key */	}	else if (key == 90) {		/* NumLock, look whether this is rather a Pause */		if (kbd_status.vcontrol)			key = 126;		/*		 * if this is the final break code of a Pause key,		 * clear the virtual control status, too		 */		if (kbd_status.vcontrol && kbd_status.breakseen)			kbd_status.vcontrol = 0;	}	else if (key == 127) {		/*		 * a SysRq; some keyboards are brain-dead enough to		 * repeat the SysRq key make code by sending PrtScr		 * make codes; other keyboards do not repeat SysRq		 * at all. We keep track of the SysRq state here.		 */		kbd_status.sysrq = !kbd_status.breakseen;	}	else if (key == 124) {		/*		 * PrtScr; look whether this is really PrtScr or rather		 * a silly repeat of a SysRq key		 */		if (kbd_status.sysrq)			/* ignore the garbage */			key = 0;	}	/* in NOREPEAT MODE ignore the key if it was the same as before */	if (!kbrepflag && key == kbd_lastkey && !kbd_status.breakseen) {		if (noblock)			return (NULL);		else			goto loop;	}	type = key2ascii[key].type;	if (type & KBD_OVERLOAD)		type = ovltbl[key2ascii[key].ovlindex].type;	type &= KBD_MASK;	switch (type) {	case KBD_SHFTLOCK:		if (!kbd_status.breakseen && key != kbd_lastkey)			vsp->shift_lock ^= 1;		break;	case KBD_CAPS:		if (!kbd_status.breakseen && key != kbd_lastkey) {			vsp->caps_lock ^= 1;			update_led();		}		break;	case KBD_SCROLL:		if (!kbd_status.breakseen && key != kbd_lastkey) {			vsp->scroll_lock ^= 1;			update_led();			if (!(vsp->scroll_lock))				wakeup((caddr_t)&(vsp->scroll_lock));		}		break;	case KBD_SHIFT:		shift_down = kbd_status.breakseen ? 0 : 1;		break;	case KBD_META:		meta_down = kbd_status.breakseen ? 0 : 0x80;		break;	case KBD_ALTGR:		altgr_down = kbd_status.breakseen ? 0 : 1;		break;	case KBD_CTL:		ctrl_down = kbd_status.breakseen ? 0 : 1;		break;	case KBD_NONE:	default:		break;			/* deliver a key */	}	if (kbd_status.breakseen) {		key |= 0x80;		kbd_status.breakseen = 0;		kbd_lastkey = 0; /* -hv- I know this is a bug with */	}			 /* N-Key-Rollover, but I ignore that */	else			 /* because avoidance is too complicated */		kbd_lastkey = key;	cp = xlatkey2ascii(key);	/* have a key */	if (cp == NULL && !noblock)		goto loop;	return (cp);}