aboutsummaryrefslogtreecommitdiff
path: root/src/line/mode.cc
blob: 626557cbb0c8a2ec1c2883b0f482bb04f7ff6c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include "line/line_editor.h"

#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <print>

#include "history.h"
#include "signal_handler.h"

using namespace std;

namespace ov4
{

void disable_raw()
{
    raw_enabled = false;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr);
}

void enable_raw()
{
    if (raw_enabled) return;
     
    raw_enabled = true;
    termios attr_raw;
    tcgetattr(STDIN_FILENO, &attr);
    tcgetattr(STDIN_FILENO, &attr_raw);

    // LOCAL

    // disable
    // echo
    // canonical
    // signal
    // disable post-input processing, aka control-V
    attr_raw.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);

    // INPUT

    // disable 
    // software flow control, aka control-S/Q
    // \n and \r conversion
    attr_raw.c_iflag &= ~(IXON | ICRNL);

    // OUTPUT

    // disable
    // post-output processing
    attr_raw.c_oflag &= ~(OPOST);

    // misc
    attr_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP);
    attr_raw.c_cflag |= (CS8);

    atexit(disable_raw);

    Signal(SIGWINCH, sigwinch_handler);

    // TCSAFLUSH
    // the change shall occur after all output written to fildes is transmitted
    // and all input so far received but not read shall be discarded before the change is made
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr_raw);
}

void editor_exit(int code)
{
    disable_raw();
    exit(code);
}


}