首页 > 解决方案 > 在 Linux 中中断“无回显的控制台输入”

问题描述

DOS 有int 21h / AH=08H: Console input without echo

Linux有类似的东西吗?如果我需要在终端显示之前处理输入的值。

标签: linuxassemblytty

解决方案


在 Linux 下,在将输入的字符“发送”到请求程序之前缓冲输入字符的是 tty。
这是通过终端模式控制的:生(无缓冲)或熟(也分别称为非规范和规范模式)。

这些模式实际上是 tty 的属性,可以通过tcgetattrtcsetattr进行控制。

例如,可以在此处找到将终端设置为不带回显的非规范模式的代码(有关VTIME和控制字符的更多信息VMIN可以在此处找到)。

那是C,所以我们需要把它翻译成汇编。从源代码中tcgetattr我们可以看到,tty 属性是通过 IOCTL 使用命令TCGETS(值 0x5401)检索到标准输入的,类似地,它们是通过使用命令TCSETS(值 0x5402)的 IOCTL 设置的。
读取的结构不是struct termios struct __kernel_termios,它基本上是前者的缩短版本。IOCTL 必须发送到文件(值为 0的stdin文件描述符)。 知道如何实现,我们只需要获取常量的值(like和similar)。 我建议使用编译器(例如这里STDIN_FILENO
tcgetattrtcsetattrICANON
) 来查找公共常量的值并检查结构的偏移量。
对于非公共常量(在其翻译单元之外不可见),我们必须求助于阅读源代码(这不是特别难,但必须注意找到正确的源代码)。

在调用 IOCTL 以获取-修改-设置 TTY 属性以启用原始模式的 64 位程序下方。
然后程序等待单个字符并显示它递增(例如a -> b)。
请注意,该程序已在 Linux (5.x) 下进行了测试,并且由于各种常量在不同的 Unix 克隆之间更改值,因此它不可移植。
我使用了 NASM 并为 定义了一个结构struct __kernel_termios,我还使用了很多符号常量来使代码更具可读性。我真的不喜欢在汇编中使用结构,但 NASM 只是一个薄的宏层(如果你还没有习惯它们,那就更好了)。
最后,我假设熟悉 64 位 Linux 汇编编程。

BITS 64

GLOBAL _start

;
; System calls numbers
;
%define SYS_READ    0
%define SYS_WRITE   1
%define SYS_IOCTL   16
%define SYS_EXIT    60

;
; __kernel_termios structure
;
%define KERNEL_NCC 19
struc termios
    .c_iflag:   resd    1       ;input mode flags
    .c_oflag:   resd    1       ;output mode flags
    .c_cflag:   resd    1               ;control mode flags
    .c_lflag:   resd    1               ;local mode flags
    .c_line:    resb    1               ;line discipline
    .c_cc:      resb    KERNEL_NCC      ;control characters
endstruc

;
; IOCTL commands
;
%define TCGETS      0x5401
%define TCSETS      0x5402

;
; TTY local flags
;
%define ECHO        8
%define ICANON      2

;
; TTY control chars
;
%define VMIN        6
%define VTIME       5

;
; Standard file descriptors
;
%define STDIN_FILENO    0
%define STDOUT_FILENO   1


SECTION .bss

    ;The char read (reserve a DWORD to make termios_data be aligned on DWORDs boundary)
    data        resd 1
    
    ;The TTY attributes
    termios_data    resb termios_size
    
SECTION .text

_start:
    ;
    ;Get the terminal settings by sending the TCGETS IOCTL to stdin
    ;
    mov edi, STDIN_FILENO       ;Send IOCTL to stdin (Less efficient but more readable)
    mov esi, TCGETS         ;The TCGETS command
    lea rdx, [REL termios_data] ;The arg, the buffer where to store the TTY attribs
    
    mov eax, SYS_IOCTL      ;Do the syscall     
    syscall

    ;
    ;Set the raw mode by clearing ECHO and ICANON and setting VMIN = 1, VTIME = 0
    ;
    and DWORD [REL termios_data + termios.c_lflag], ~(ICANON | ECHO)    ;Clear ECHO and ICANON
    mov BYTE [REL termios_data + termios.c_cc + VMIN], 1
    mov BYTE [REL termios_data + termios.c_cc + VTIME], 0
    
    ;
    ;Set the terminal settings
    ;
    mov edi, STDIN_FILENO       ;Send to stdin (Less efficient but more readable)
    mov esi, TCSETS         ;Use TCSETS as the command
    lea rdx, [REL termios_data] ;Use the same data read (and altered) before
    
    mov eax, SYS_IOCTL      ;Do the syscall
    syscall

    ;
    ;Read a char
    ;
    mov edi, STDIN_FILENO       ;Read from stdin (Less efficient but more readable)
    lea rsi, [REL data]     ;Read into data
    mov edx, 1          ;Read only 1 char
    
    mov eax, SYS_READ       ;Do the syscall (Less efficient but more readable)
    syscall
    
    ;
    ;Increment the char (as an example)
    ;
    inc BYTE [REL data]
    
    ;
    ;Print the char
    ;
    mov edi, STDOUT_FILENO      ;Write to stdout
    lea rsi, [REL data]     ;Write the altered char
    mov edx, 1          ;Only 1 char to write
    
    mov eax, SYS_WRITE      ;Do the syscall
    syscall
    
    ;
    ;Restore the terminal settins (similar to the code above)
    ;
    mov edi, STDIN_FILENO
    mov esi, TCGETS
    lea rdx, [REL termios_data]
    mov eax, SYS_IOCTL
    syscall

    ;Set ECHO and ICANON
    or DWORD [REL termios_data + termios.c_lflag], ICANON | ECHO

    mov edi, STDIN_FILENO   
    mov esi, TCSETS
    lea rdx, [REL termios_data]
    mov eax, SYS_IOCTL
    syscall 
    
    ;
    ;Exit
    ;
    xor edi, edi
    mov eax, SYS_EXIT
    syscall

推荐阅读