CONS(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸŽ€ 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 functions cngetc(), cngetsn() and cnpollc() 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 within mountroothooks. once the system boots, user input is read via the standard tty(4) facilities. periodt.

now here's the rundown, one function at a time 🧚

  • cnbell() β€” rings a bell at an appropriate pitch, for a duration of period milliseconds, at the given volume. ngl though, the volume value 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. returns 0 if there's no console input device. and bestie, cnpollc() must be called before cngetc() 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 uses cnpollc() and cngetc(). the number of read characters is size at 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 make cngetsn() 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: cp is 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 if on is nonzero, or back to interrupt driven mode if on is zero. cnpollc() should be used during kernel startup only. πŸ”₯
  • cnputc() β€” the console kernel output character routine. commonly, kernel code uses printf(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