2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/internal.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/calls/struct/metatermios.internal.h"
|
2023-09-07 23:03:19 +00:00
|
|
|
#include "libc/calls/syscall-nt.internal.h"
|
|
|
|
#include "libc/calls/termios.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/calls/termios.internal.h"
|
2023-08-18 11:55:57 +00:00
|
|
|
#include "libc/calls/ttydefaults.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/dce.h"
|
2023-09-12 04:34:53 +00:00
|
|
|
#include "libc/errno.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2023-08-18 11:55:57 +00:00
|
|
|
#include "libc/intrin/nomultics.internal.h"
|
2023-04-27 03:45:01 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-09-07 23:03:19 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/console.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/nt/enum/consolemodeflags.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/enum/version.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
2022-05-23 17:15:53 +00:00
|
|
|
#include "libc/nt/version.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/str/str.h"
|
2023-09-07 23:03:19 +00:00
|
|
|
#include "libc/sysv/consts/fileno.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/sysv/consts/sicode.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/termios.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2023-08-19 13:41:06 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-08-08 11:00:29 +00:00
|
|
|
textwindows int tcsetattr_nt(int fd, int opt, const struct termios *tio) {
|
|
|
|
bool32 ok;
|
|
|
|
int64_t hInput, hOutput;
|
2020-06-15 14:18:57 +00:00
|
|
|
uint32_t inmode, outmode;
|
2023-09-07 23:03:19 +00:00
|
|
|
|
2023-08-08 11:00:29 +00:00
|
|
|
if (__isfdkind(fd, kFdConsole)) {
|
|
|
|
hInput = g_fds.p[fd].handle;
|
|
|
|
hOutput = g_fds.p[fd].extra;
|
2023-09-07 23:03:19 +00:00
|
|
|
} else if (fd == STDIN_FILENO || //
|
|
|
|
fd == STDOUT_FILENO || //
|
|
|
|
fd == STDERR_FILENO) {
|
|
|
|
hInput = g_fds.p[STDIN_FILENO].handle;
|
|
|
|
hOutput = g_fds.p[MAX(STDOUT_FILENO, fd)].handle;
|
2023-08-08 11:00:29 +00:00
|
|
|
} else {
|
|
|
|
return enotty();
|
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
if (!GetConsoleMode(hInput, &inmode) || !GetConsoleMode(hOutput, &outmode)) {
|
|
|
|
return enotty();
|
|
|
|
}
|
2022-04-24 16:59:22 +00:00
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
if (opt == TCSAFLUSH) {
|
2023-09-21 17:10:20 +00:00
|
|
|
FlushConsoleInputBytes(hInput);
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
|
|
|
inmode &= ~(kNtEnableLineInput | kNtEnableEchoInput |
|
|
|
|
kNtEnableProcessedInput | kNtEnableVirtualTerminalInput);
|
|
|
|
inmode |= kNtEnableWindowInput;
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.magic = 0;
|
2023-09-07 23:03:19 +00:00
|
|
|
if (tio->c_lflag & ICANON) {
|
2023-09-19 18:42:38 +00:00
|
|
|
inmode |=
|
|
|
|
kNtEnableLineInput | kNtEnableProcessedInput | kNtEnableQuickEditMode;
|
2023-09-07 23:03:19 +00:00
|
|
|
} else {
|
2023-09-19 18:42:38 +00:00
|
|
|
inmode &= ~kNtEnableQuickEditMode;
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.magic |= kTtyUncanon;
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
|
|
|
if (!(tio->c_iflag & ICRNL)) {
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.magic |= kTtyNoCr2Nl;
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
|
|
|
if (!(tio->c_lflag & ECHOCTL)) {
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.magic |= kTtyEchoRaw;
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
|
|
|
if (tio->c_lflag & ECHO) {
|
|
|
|
// "kNtEnableEchoInput can be used only if the
|
|
|
|
// kNtEnableLineInput mode is also enabled." -MSDN
|
|
|
|
if (tio->c_lflag & ICANON) {
|
|
|
|
inmode |= kNtEnableEchoInput;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-10-04 05:34:45 +00:00
|
|
|
} else {
|
|
|
|
__ttyconf.magic |= kTtySilence;
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
|
|
|
if (!(tio->c_lflag & ISIG)) {
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.magic |= kTtyNoIsigs;
|
2023-09-07 23:03:19 +00:00
|
|
|
}
|
2023-10-04 05:34:45 +00:00
|
|
|
memcpy(__ttyconf.c_cc, tio->c_cc, NCCS);
|
|
|
|
if ((tio->c_lflag & ISIG) && __ttyconf.vintr == CTRL('C')) {
|
2023-09-07 23:03:19 +00:00
|
|
|
// allows ctrl-c to be delivered asynchronously via win32
|
|
|
|
inmode |= kNtEnableProcessedInput;
|
|
|
|
}
|
|
|
|
ok = SetConsoleMode(hInput, inmode);
|
|
|
|
(void)ok;
|
|
|
|
NTTRACE("SetConsoleMode(%p, %s) → %hhhd", hInput,
|
|
|
|
DescribeNtConsoleInFlags(inmode), ok);
|
2022-04-24 16:59:22 +00:00
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
outmode &= ~kNtDisableNewlineAutoReturn;
|
|
|
|
outmode |= kNtEnableProcessedOutput;
|
|
|
|
if (!(tio->c_oflag & ONLCR)) {
|
|
|
|
outmode |= kNtDisableNewlineAutoReturn;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-09-07 23:03:19 +00:00
|
|
|
if (IsAtLeastWindows10()) {
|
|
|
|
outmode |= kNtEnableVirtualTerminalProcessing;
|
|
|
|
}
|
|
|
|
ok = SetConsoleMode(hOutput, outmode);
|
|
|
|
(void)ok;
|
|
|
|
NTTRACE("SetConsoleMode(%p, %s) → %hhhd", hOutput,
|
|
|
|
DescribeNtConsoleOutFlags(outmode), ok);
|
|
|
|
|
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|