Files
makronix/src/lib/libmxlinux/mx_termios.h
T
lotherk 6199effa3c $Makronix$ in libmxlinux, the rule in AGENTS.md, and build/logs for a clone
Three gaps, all found by running `make release' on a fresh clone instead of
trusting the tree that had been built in for weeks.

1. tools/userland.sh: fetch_binaries redirects into build/logs/, but nothing
   created that directory: a clone has no build/ below AGENTS.md, so step 2/7
   died with "Error code 1" from the shell, before pkg-binaries.sh ran, and
   the log it points at was never written.  mkdir -p first.  The other two
   redirects into build/logs (pkg-add, pkg-delete) come after it.

2. src/lib/libmxlinux: four files had no header at all (mx_termios.h,
   mxlinux.h, mx_locale.c, mx_fgetwln.c).  They now carry `/*\t$Makronix$\t*/'
   like the other 61 there, and all 71 files of the directory have one.
   Checked with gcc -fsyntax-only on each of the four.

3. AGENTS.md says the header rule once, in "Licence": `#\t$Makronix$' in a
   file Makronix wrote, `$NetBSD: <file>,v <rev> ... Exp $' in one taken from
   pkgsrc, never the unfilled template -- the detail is in ext/AGENTS.md
   ("Header").

Tested: tools/test-tools.sh 42 ok, 0 bad.  gcc -fsyntax-only clean on the four
files.  The directory builds through `make userland' (mobile/userland/mk/
mx.prog.mk: mx-lib), not standalone: a plain `bmake' in src/lib/libmxlinux has
no NETBSDSRCDIR (getopt.c comes through .PATH) -- not caused by this change,
the same command failed the same way before it.
2026-11-24 03:33:56 +00:00

315 lines
11 KiB
C

/* $Makronix$ */
/*
* mx_termios.h -- the NetBSD <termios.h> / <sys/ttycom.h> names that glibc
* does not have, and NetBSD's numeric speeds (cfsetospeed(t, 9600)), for
* every program of src/ on a Linux target: mxlinux.h includes it. Written
* for stty(1) (mobile/userland/stty, 2026-11-11), which still includes it
* itself in the old port frame.
*
* stty(1) is written against NetBSD's tty interface; Linux's is nearly the
* same (POSIX plus the BSD bits both kept), and where the two agree the
* glibc value is used unchanged -- CSIZE, PARENB, ICRNL, ECHO and the rest
* are not touched here. What is added is only what has no Linux
* equivalent, and each value is chosen so that the program stays harmless
* on a phone tty:
*
* - A flag Linux does not have (ALTWERASE, NOKERNINFO, MDMBUF, CDTRCTS)
* gets a bit that no Linux termios field uses: it is never reported as
* set (print.c only prints it when it differs from its default, and it
* never is), and setting it asks the kernel for something that does not
* exist there, which the kernel ignores.
* - A control character Linux does not have (VDSUSP, VSTATUS) gets an
* unused index of c_cc[] (Linux defines 0-16 of the 32 entries): the
* name is still recognized and its value remembered in the struct, and
* the kernel never acts on it.
* - OXTABS is not a name Linux knows, but the behaviour is Linux's TAB3
* ("expand tabs to spaces", oflag 0x1800): mapped, not invented.
*
* Included: <sys/ttydefaults.h>, which glibc does have, and which carries
* the C* default control characters (CDISCARD, CDSUSP, CSTATUS, CEOF ...)
* that NetBSD's <termios.h> defines; and <unistd.h> for _POSIX_VDISABLE
* (glibc defines it as '\0' in bits/posix_opt.h, NetBSD as 0xff -- stty
* only ever compares a c_cc entry with it or stores it, so either works).
*
* What is deliberately NOT defined: TIOCEXT and TTYDISC. key.c uses them
* inside #ifdef (key.c:197, :311), so leaving them out compiles the
* extproc/tty operands away instead of guessing a Linux ioctl they do not
* have -- an honest missing feature, not a wrong one. The line discipline
* ioctls that stty.c calls unconditionally are mapped to the Linux ioctl
* with the same job; see TIOCGLINED and TIOCGQSIZE below.
*
* This belongs in libmxlinux (docs/NETBSD-USERLAND.md A9) the day it
* exists: it is a property of the system, not of stty.
*/
#ifndef MX_TERMIOS_H
#define MX_TERMIOS_H
/*
* Not <sys/ioctl.h>: the macros below name ioctls only where stty expands
* them, and stty includes it itself (stty.h:37). Forced into every compile it made
* TIOCSTI visible to ksh, whose emacs.c:1309-1314 compiles only where it
* is not (it uses an undefined TTY) -- as on NetBSD, where emacs.c does
* not see <sys/ioctl.h>.
*/
#include <sys/ttydefaults.h> /* CDISCARD, CDSUSP, CSTATUS, CEOF, ... */
#include <termios.h>
#include <unistd.h> /* _POSIX_VDISABLE */
/*
* src/sys/sys/termios.h:116 -- NetBSD's name for "expand tabs to spaces".
* Linux spells the same thing TAB3 (asm-generic/termbits.h: XTABS).
*/
#ifndef OXTABS
#define OXTABS XTABS
#endif
/*
* src/sys/sys/termios.h:174 and :183 -- local flags Linux does not have.
* 0x2000 and 0x020000 are free in Linux's c_lflag (asm-generic/termbits.h:
* ISIG 0x1 ... ECHOCTL 0x200, ECHOPRT 0x400, ECHOKE 0x800, FLUSHO 0x1000,
* PENDIN 0x4000, IEXTEN 0x8000, EXTPROC 0x10000).
*/
#ifndef ALTWERASE
#define ALTWERASE 0x00002000U
#endif
#ifndef NOKERNINFO
#define NOKERNINFO 0x00020000U
#endif
/*
* src/sys/sys/termios.h:146-147 -- control flags Linux does not have.
* Free in Linux's c_cflag: CBAUD 0x100f, CSIZE 0x30, CSTOPB 0x40,
* CREAD 0x80, PARENB 0x100, PARODD 0x200, HUPCL 0x400, CLOCAL 0x800,
* CBAUDEX 0x1000, CIBAUD 0x100f0000, CRTSCTS 0x80000000.
*/
#ifndef MDMBUF
#define MDMBUF 0x00200000U
#endif
#ifndef CDTRCTS
#define CDTRCTS 0x00400000U
#endif
/*
* src/sys/sys/termios.h:65,76 -- NetBSD's VDSUSP (11) and VSTATUS (18) are
* two names Linux's c_cc[] does not have: Linux numbers 17 control
* characters (VINTR 0 ... VEOL2 16) and leaves 17..31 of c_cc[NCCS]
* unused (NCCS is 32), while 11 is VEOL on Linux. So each name gets one
* of the unused indices, and `stty dsusp ^Y` parses, prints back, and
* stays inside the array; the kernel never acts on either slot.
*/
#ifndef VDSUSP
#define VDSUSP 17
#endif
#ifndef VSTATUS
#define VSTATUS 18
#endif
/*
* src/sys/sys/ttycom.h:116,117 -- NetBSD reports the line discipline as a name
* (TIOCGLINED fills char[TTLINEDNAMELEN]), Linux as a number (TIOCGETD
* fills an int, N_TTY = 0). stty.c:102 calls TIOCGLINED unconditionally
* and warns on any error, and print.c:78 prints the buffer with %s.
*
* So: the buffer keeps NetBSD's type and size (char[32],
* src/sys/sys/ttycom.h:116), and the ioctl is Linux's TIOCGETD -- it
* succeeds, writes N_TTY (0) as the first byte, and print()'s %s sees an
* empty string: `line = ;` where NetBSD says `line = termios;`. A wrong
* ioctl number here (NetBSD's own _IOR('t', 66, linedn_t)) would fail on
* every run and put "stty: TIOCGLINED: Inappropriate ioctl for device" on
* stderr each time.
*/
#ifndef TTLINEDNAMELEN
#define TTLINEDNAMELEN 32
#endif
typedef char linedn_t[TTLINEDNAMELEN];
#ifndef TIOCGLINED
#define TIOCGLINED TIOCGETD
#endif
/*
* src/sys/sys/ttycom.h:173 -- TIOCGQSIZE returns the input queue size in an
* int; stty.c:106 calls it unconditionally too. Linux's closest is
* FIONREAD (bytes waiting in the input queue), which succeeds on a tty and
* is 0 when nothing is pending, so print.c's `if (queue)` leaves it out --
* the same effect as NetBSD's "queue = N" that only appears when the
* program's defaults differ.
*/
/*
* TCSASOFT, NetBSD <sys/termios.h>:209: a flag to tcsetattr's action, "leave
* the hardware state alone". OR-ed into the action, any value Linux does
* not know makes tcsetattr fail with EINVAL; Linux has no such state to
* protect, so 0 asks for exactly the same as without it.
*/
#ifndef TCSASOFT
#define TCSASOFT 0
#endif
#ifndef TIOCGQSIZE
#define TIOCGQSIZE FIONREAD
#endif
/*
* key.c:212 -- f_insane() fills the terminal with bytes from
* _PATH_URANDOM, which src/include/paths.h:100 defines as /dev/urandom.
* glibc's <paths.h> has no name for it (neither _PATH_URANDOM nor
* _PATH_RANDOM is there in 2.41), so it is spelled out.
*/
#ifndef _PATH_URANDOM
#define _PATH_URANDOM "/dev/urandom"
#endif
/*
* key.c:323,330 -- `stty ostart` / `stty ostop` (start/stop output, what
* ^Q and ^S do). NetBSD's TIOCSTOP/TIOCSTART (src/sys/sys/ttycom.h:140,141)
* have no Linux counterpart at all: Linux has no such ioctl, only the
* characters. Defining them as 0 means the call fails with ENOTTY and
* key.c's own `err(1, "TIOCSTART")` says so -- the operand is reported as
* unsupported instead of silently doing the wrong thing. (Leaving them
* out is not an option: the two functions are not inside #ifdef.)
*/
#ifndef TIOCSTART
#define TIOCSTART 0
#endif
#ifndef TIOCSTOP
#define TIOCSTOP 0
#endif
/*
* Baud rates. NetBSD's Bxxx constants ARE the rate (src/sys/sys/termios.h:
* B38400 is 38400), and stty(1) relies on that: it prints what
* cfgetospeed() returns as "speed %d baud;" (print.c:67), takes the number
* the user typed and hands it to cfsetospeed() (stty.c:137), and gfmt.c
* writes the same value into the gfmt1 form. Linux's Bxxx are small
* indices into a table instead (asm-generic/termbits.h: B9600 is 13,
* B38400 is 15), so on the phone the port would print "speed 15 baud;"
* for 38400 and `stty 9600` would ask for a speed that does not exist.
*
* So the four cf*speed() calls are wrapped: the program keeps seeing and
* giving rates, the kernel keeps getting codes. A rate Linux has no code
* for returns (speed_t)-1, which stty ignores -- the terminal keeps its
* speed instead of being set to something else.
*/
static inline speed_t
mx_speed_to_code(int rate)
{
switch (rate) {
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 500000: return B500000;
case 576000: return B576000;
case 921600: return B921600;
case 1000000: return B1000000;
case 1152000: return B1152000;
case 1500000: return B1500000;
case 2000000: return B2000000;
case 2500000: return B2500000;
case 3000000: return B3000000;
case 3500000: return B3500000;
case 4000000: return B4000000;
default: return (speed_t)-1;
}
}
static inline int
mx_code_to_speed(speed_t code)
{
switch (code) {
case B0: return 0;
case B50: return 50;
case B75: return 75;
case B110: return 110;
case B134: return 134;
case B150: return 150;
case B200: return 200;
case B300: return 300;
case B600: return 600;
case B1200: return 1200;
case B1800: return 1800;
case B2400: return 2400;
case B4800: return 4800;
case B9600: return 9600;
case B19200: return 19200;
case B38400: return 38400;
case B57600: return 57600;
case B115200: return 115200;
case B230400: return 230400;
case B460800: return 460800;
case B500000: return 500000;
case B576000: return 576000;
case B921600: return 921600;
case B1000000: return 1000000;
case B1152000: return 1152000;
case B1500000: return 1500000;
case B2000000: return 2000000;
case B2500000: return 2500000;
case B3000000: return 3000000;
case B3500000: return 3500000;
case B4000000: return 4000000;
default: return -1;
}
}
static inline int
mx_cfgetispeed(const struct termios *t)
{
return mx_code_to_speed(cfgetispeed(t));
}
static inline int
mx_cfgetospeed(const struct termios *t)
{
return mx_code_to_speed(cfgetospeed(t));
}
static inline speed_t
mx_cfsetispeed(struct termios *t, int rate)
{
return cfsetispeed(t, mx_speed_to_code(rate));
}
static inline speed_t
mx_cfsetospeed(struct termios *t, int rate)
{
return cfsetospeed(t, mx_speed_to_code(rate));
}
#define cfgetispeed(t) mx_cfgetispeed(t)
#define cfgetospeed(t) mx_cfgetospeed(t)
#define cfsetispeed(t, r) mx_cfsetispeed((t), (r))
#define cfsetospeed(t, r) mx_cfsetospeed((t), (r))
/*
* src/sys/sys/param.h:51 -- BSD4_4 is what makes gfmt.c:100,115 read the
* ispeed= and ospeed= keys of the gfmt1 form (gfmt.c:66-69 writes them
* unconditionally). Without it gprint() writes two keys that gread()
* rejects with "illegal gfmt1 option -- ispeed", so `stty -g` could not be
* handed back to stty -- which is exactly what the -g option of the man
* page promises. glibc's struct termios has c_ispeed/c_ospeed, so the two
* branches are all that is missing.
*/
#ifndef BSD4_4
#define BSD4_4 1
#endif
#endif /* MX_TERMIOS_H */