CONS(9)
π CONS(9) β console access interface, bestie edition π
okay so this is CONS(9) from the Kernel Developer's Manual, and it's literally your all-access pass to the console device. let's get into it π
β¨ NAME β¨
cnbell, cnflush, cngetc, cngetsn, cnhalt, cnpollc, cnputc β the console access interface. that's the whole squad, seven functions, all icons π
πΈ SYNOPSIS πΈ
#include <dev/cons.h>
void
cnbell(u_int pitch, u_int period, u_int volume);
void
cnflush(void);
int
cngetc(void);
int
cngetsn(char *cp, int size);
void
cnhalt(void);
void
cnpollc(int on);
void
cnputc(int c);
π DESCRIPTION π
so these functions all operate over the current console device. and heads up, the console must be initialized before you can use any of them. no init, no vibes. π«
okay and this next bit is important so pay attention bestie:
the console input polling functionscngetc(),cngetsn()andcnpollc()are only to be used during initial system boot β like when you're asking for the root and dump device, or getting necessary user input withinmountroothooks. once the system boots, user input is read via the standardtty(4)facilities. periodt.
now here's the rundown, one function at a time π§
cnbell()β rings a bell at an appropriatepitch, for a duration ofperiodmilliseconds, at the givenvolume. ngl though, thevolumevalue is commonly ignored. rip. πcnflush()β waits for all pending output to finish. like when you send a text and wait for the "delivered" receipt. π«Άcngetc()β polls (busy waits) for an input and returns the input key. returns0if there's no console input device. and bestie,cnpollc()must be called beforecngetc()can be used.cngetc()should be used during kernel startup only. βcngetsn()β reads one line of user input, and stops reading once the newline key is input. input is echoed back. this usescnpollc()andcngetc(). the number of read characters issizeat maximum, and the user gets notified by console bell when the end of the input buffer is reached. the Backspace key works as expected (slay). @ or CTRL-u makecngetsn()discard input read so far, print newline, and wait for next input.cngetsn()returns the number of characters actually read, excluding the final newline. and note:cpis not zero-ended before return. πcngetsn()should be used during kernel startup only.cnhalt()β terminates the console device, i.e. cleanly shuts down the console hardware. it's giving graceful exit. πcnpollc()β switches the console driver to polling mode ifonis nonzero, or back to interrupt driven mode ifonis zero.cnpollc()should be used during kernel startup only. π₯cnputc()β the console kernel output character routine. commonly, kernel code usesprintf(9)rather than using this low-level interface. like yeah you could text them individually but why not just use the group chat, yk? π¬
π EXAMPLES π
this waits until a Enter key is pressed:
int c;
cnpollc(1);
for(;;) {
c = cngetc();
if ((c == '\r' || (c == '\n')) {
printf("\n");
break;
}
}
cnpollc(0);
no cap, that's the whole loop. iconic. π
π¦ SEE ALSO π¦
pckbd(4), pcppi(4), tty(4), wscons(4), wskbd(4), printf(9), spl(9), wscons(9)
go read those next, iykyk π
and that's the whole page!! this one's from the Debian crew, dated June 8, 2010 β yeah it's an older baddie but she still ate πβ¨π
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2001 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms