#include <sys/termios.h>
#include <sys/queue.h>
#include <sys/selinfo.h>
#include <sys/timeout.h>
#define KERN_TTY_TKNIN 1
#define KERN_TTY_TKNOUT 2
#define KERN_TTY_TKRAWCC 3
#define KERN_TTY_TKCANCC 4
#define KERN_TTY_INFO 5
#define KERN_TTY_MAXID 8
#define CTL_KERN_TTY_NAMES { \
{ 0, 0 }, \
{ "tk_nin", CTLTYPE_QUAD }, \
{ "tk_nout", CTLTYPE_QUAD }, \
{ "tk_rawcc", CTLTYPE_QUAD }, \
{ "tk_cancc", CTLTYPE_QUAD }, \
{ "ttyinfo", CTLTYPE_STRUCT }, \
{ "gap", 0 }, \
{ "gap", 0 }, \
}
struct ptmget {
int cfd;
int sfd;
char cn[16];
char sn[16];
};
#define PTMGET _IOR('t', 1, struct ptmget)
#define PATH_PTMDEV "/dev/ptm"
#define TTY_GID 4
struct clist {
int c_cc;
int c_cn;
u_char *c_cf;
u_char *c_cl;
u_char *c_cs;
u_char *c_ce;
u_char *c_cq;
};
struct tty {
TAILQ_ENTRY(tty) tty_link;
struct clist t_rawq;
long t_rawcc;
struct clist t_canq;
long t_cancc;
struct clist t_outq;
long t_outcc;
int t_qlen;
u_char t_line;
dev_t t_dev;
int t_state;
int t_flags;
struct pgrp *t_pgrp;
struct session *t_session;
struct selinfo t_rsel;
struct selinfo t_wsel;
struct termios t_termios;
struct winsize t_winsize;
void (*t_oproc)(struct tty *);
int (*t_param)(struct tty *, struct termios *);
int (*t_hwiflow)(struct tty *tp, int flag);
void *t_sc;
short t_column;
short t_rocount, t_rocol;
short t_hiwat;
short t_lowat;
short t_gen;
struct timeout t_rstrt_to;
struct timeval t_tv;
};
struct itty {
dev_t t_dev;
int t_rawq_c_cc;
int t_canq_c_cc;
int t_outq_c_cc;
short t_hiwat;
short t_lowat;
short t_column;
int t_state;
struct session *t_session;
pid_t t_pgrp_pg_id;
u_char t_line;
};
#define t_cc t_termios.c_cc
#define t_cflag t_termios.c_cflag
#define t_iflag t_termios.c_iflag
#define t_ispeed t_termios.c_ispeed
#define t_lflag t_termios.c_lflag
#define t_min t_termios.c_min
#define t_oflag t_termios.c_oflag
#define t_ospeed t_termios.c_ospeed
#define t_time t_termios.c_time
#define TTIPRI 25
#define TTOPRI 26
#define OBUFSIZ 512
#define TTYHOG(tp) (tp)->t_qlen
#ifdef _KERNEL
#define TTMAXLOWAT 256
#define TTMINLOWAT 32
#define TTMINHIWAT 100
#define TTHIWATMINSPACE 200
#endif
#define TS_ASLEEP 0x00001
#define TS_ASYNC 0x00002
#define TS_BUSY 0x00004
#define TS_CARR_ON 0x00008
#define TS_FLUSH 0x00010
#define TS_ISOPEN 0x00020
#define TS_TBLOCK 0x00040
#define TS_TIMEOUT 0x00080
#define TS_TTSTOP 0x00100
#define TS_WOPEN 0x00200
#define TS_XCLUDE 0x00400
#define TS_BKSL 0x00800
#define TS_CNTTB 0x01000
#define TS_ERASE 0x02000
#define TS_LNCH 0x04000
#define TS_TYPEN 0x08000
#define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
#define TS_TSTAMPDCDSET 0x10000
#define TS_TSTAMPDCDCLR 0x20000
#define TS_TSTAMPCTSSET 0x40000
#define TS_TSTAMPCTSCLR 0x80000
#define ORDINARY 0
#define CONTROL 1
#define BACKSPACE 2
#define NEWLINE 3
#define TAB 4
#define VTAB 5
#define RETURN 6
struct speedtab {
int sp_speed;
int sp_code;
};
#define DMSET 0
#define DMBIS 1
#define DMBIC 2
#define DMGET 3
#define TTY_CHARMASK 0x000000ff
#define TTY_QUOTE 0x00000100
#define TTY_ERRORMASK 0xff000000
#define TTY_FE 0x01000000
#define TTY_PE 0x02000000
#define isctty(pr, tp) \
((pr)->ps_session == (tp)->t_session && (pr)->ps_flags & PS_CONTROLT)
#define isbackground(pr, tp) \
(isctty((pr), (tp)) && (pr)->ps_pgrp != (tp)->t_pgrp)
TAILQ_HEAD(ttylist_head, tty);
#ifdef _KERNEL
extern int tty_count;
extern char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[];
extern int64_t tk_cancc, tk_nin, tk_nout, tk_rawcc;
int sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
int sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
int b_to_q(u_char *cp, int cc, struct clist *q);
void catq(struct clist *from, struct clist *to);
int getc(struct clist *q);
void ndflush(struct clist *q, int cc);
int ndqb(struct clist *q, int flag);
u_char *firstc(struct clist *clp, int *c, int *cc);
u_char *nextc(struct clist *q, u_char *cp, int *c, int *cc);
int putc(int c, struct clist *q);
int q_to_b(struct clist *q, u_char *cp, int cc);
int unputc(struct clist *q);
int nullmodem(struct tty *tp, int flag);
int tputchar(int c, struct tty *tp);
int ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
struct proc *p);
int ttread(struct tty *tp, struct uio *uio, int flag);
void ttrstrt(void *tp);
int ttkqfilter(dev_t dev, struct knote *kn);
void ttsetwater(struct tty *tp);
int ttspeedtab(int speed, const struct speedtab *table);
int ttstart(struct tty *tp);
void ttwakeupwr(struct tty *tp);
void ttwakeup(struct tty *tp);
int ttwrite(struct tty *tp, struct uio *uio, int flag);
void ttychars(struct tty *tp);
int ttycheckoutq(struct tty *tp, int wait);
int ttyclose(struct tty *tp);
void ttyflush(struct tty *tp, int rw);
void ttyinfo(struct tty *tp);
int ttyinput(int c, struct tty *tp);
int ttylclose(struct tty *tp, int flag, struct proc *p);
int ttymodem(struct tty *tp, int flag);
int ttyopen(dev_t device, struct tty *tp, struct proc *p);
int ttyoutput(int c, struct tty *tp);
void ttypend(struct tty *tp);
int ttyretype(struct tty *tp);
int ttyrub(int c, struct tty *tp);
int ttysleep(struct tty *tp, void *chan, int pri, char *wmesg);
int ttysleep_nsec(struct tty *, void *, int, char *, uint64_t);
int ttywait(struct tty *tp);
int ttywflush(struct tty *tp);
void ttytstamp(struct tty *tp, int octs, int ncts, int odcd, int ndcd);
void tty_init(void);
struct tty *ttymalloc(int);
void ttyfree(struct tty *);
int cttyopen(dev_t, int, int, struct proc *);
int cttyread(dev_t, struct uio *, int);
int cttywrite(dev_t, struct uio *, int);
int cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
void clalloc(struct clist *, int, int);
void clfree(struct clist *);
int nullioctl(struct tty *, u_long, caddr_t, int, struct proc *);
int pppopen(dev_t dev, struct tty *, struct proc *);
int pppclose(struct tty *, int, struct proc *);
int ppptioctl(struct tty *, u_long, caddr_t, int, struct proc *);
int pppinput(int c, struct tty *);
int pppstart(struct tty *);
int pppread(struct tty *, struct uio *, int);
int pppwrite(struct tty *, struct uio *, int);
int nmeaopen(dev_t, struct tty *, struct proc *);
int nmeaclose(struct tty *, int, struct proc *);
int nmeainput(int, struct tty *);
int mstsopen(dev_t, struct tty *, struct proc *);
int mstsclose(struct tty *, int, struct proc *);
int mstsinput(int, struct tty *);
int endrunopen(dev_t, struct tty *, struct proc *);
int endrunclose(struct tty *, int, struct proc *);
int endruninput(int, struct tty *);
#endif