Merge branch 'master' of github.com:Minep/lunaix-os
[lunaix-os.git] / lunaix-os / hal / term / lcntls / lcntl.c
1 /**
2  * @file lcntl.c
3  * @author Lunaixsky (lunaxisky@qq.com)
4  * @brief Line controller master that handle all POSIX-control code
5  * @version 0.1
6  * @date 2023-11-25
7  *
8  * @copyright Copyright (c) 2023
9  *
10  */
11 #include <hal/term.h>
12 #include <usr/lunaix/term.h>
13
14 #include <lunaix/process.h>
15 #include <lunaix/spike.h>
16
17 static inline void
18 raise_sig(struct term* at_term, struct linebuffer* lbuf, int sig)
19 {
20     term_sendsig(at_term, SIGINT);
21     lbuf->sflags |= LSTATE_SIGRAISE;
22 }
23
24 static inline int must_inline optimize("-fipa-cp-clone")
25 lcntl_transform_seq(struct term* tdev, struct linebuffer* lbuf, bool out)
26 {
27     struct rbuffer* raw = lbuf->current;
28     struct rbuffer* cooked = lbuf->next;
29     struct rbuffer* output = tdev->line_out.current;
30
31     int i = 0, _if = tdev->iflags & -!out, _of = tdev->oflags & -!!out,
32         _lf = tdev->lflags;
33     int allow_more = 1, latest_eol = cooked->ptr;
34     char c;
35
36     int (*lcntl_slave_put)(struct term*, struct linebuffer*, char) =
37         tdev->lcntl->process_and_put;
38
39 #define EOL tdev->cc[_VEOL]
40 #define EOF tdev->cc[_VEOF]
41 #define ERASE tdev->cc[_VERASE]
42 #define KILL tdev->cc[_VKILL]
43 #define INTR tdev->cc[_VINTR]
44 #define QUIT tdev->cc[_VQUIT]
45 #define SUSP tdev->cc[_VSUSP]
46
47     if (!out) {
48         // Keep all cc's (except VMIN & VTIME) up to L2 cache.
49         for (size_t i = 0; i < _VMIN; i++) {
50             prefetch_rd(&tdev->cc[i], 2);
51         }
52     }
53
54     while (rbuffer_get(raw, &c) && allow_more) {
55
56         if (c == '\r' && ((_if & _ICRNL) || (_of & _OCRNL))) {
57             c = '\n';
58         } else if (c == '\n') {
59             if ((_if & _INLCR) || (_of & (_ONLRET))) {
60                 c = '\r';
61             }
62         }
63
64         if (c == '\0') {
65             if ((_if & _IGNBRK)) {
66                 continue;
67             }
68
69             if ((_if & _BRKINT)) {
70                 raise_sig(tdev, lbuf, SIGINT);
71                 break;
72             }
73         }
74
75         if ('a' <= c && c <= 'z') {
76             if ((_if & _IUCLC)) {
77                 c = c | 0b100000;
78             } else if ((_of & _OLCUC)) {
79                 c = c & ~0b100000;
80             }
81         }
82
83         if (out) {
84             goto do_out;
85         }
86
87         if (c == '\n') {
88             latest_eol = cooked->ptr + 1;
89             if ((_lf & _ECHONL)) {
90                 rbuffer_put(output, c);
91             }
92         }
93
94         // For input procesing
95
96         if (c == '\n' || c == EOL) {
97             lbuf->sflags |= LSTATE_EOL;
98         } else if (c == EOF) {
99             lbuf->sflags |= LSTATE_EOF;
100             rbuffer_clear(raw);
101             break;
102         } else if (c == INTR) {
103             raise_sig(tdev, lbuf, SIGINT);
104         } else if (c == QUIT) {
105             raise_sig(tdev, lbuf, SIGKILL);
106             break;
107         } else if (c == SUSP) {
108             raise_sig(tdev, lbuf, SIGSTOP);
109         } else if (c == ERASE) {
110             if (!rbuffer_erase(cooked))
111                 continue;
112         } else if (c == KILL) {
113             // TODO shrink the rbuffer
114         } else {
115             goto keep;
116         }
117
118         if ((_lf & _ECHOE) && c == ERASE) {
119             rbuffer_put(output, '\x8'); 
120             rbuffer_put(output, ' '); 
121             rbuffer_put(output, '\x8'); 
122         }
123         if ((_lf & _ECHOK) && c == KILL) {
124             rbuffer_put(output, c);
125             rbuffer_put(output, '\n');
126         }
127
128         continue;
129
130     keep:
131         if ((_lf & _ECHO)) {
132             rbuffer_put(output, c);
133         }
134
135         goto put_char;
136
137     do_out:
138         if (c == '\n' && (_of & _ONLCR)) {
139             rbuffer_put(cooked, '\r');
140         }
141
142     put_char:
143         if (!out && (_lf & _IEXTEN) && lcntl_slave_put) {
144             allow_more = lcntl_slave_put(tdev, lbuf, c);
145         } else {
146             allow_more = rbuffer_put(cooked, c);
147         }
148     }
149
150     if (!out && !rbuffer_empty(output) && !(_lf & _NOFLSH)) {
151         term_flush(tdev);
152     }
153
154     line_flip(lbuf);
155
156     return i;
157 }
158
159 int
160 lcntl_transform_inseq(struct term* tdev)
161 {
162     return lcntl_transform_seq(tdev, &tdev->line_in, false);
163 }
164
165 int
166 lcntl_transform_outseq(struct term* tdev)
167 {
168     return lcntl_transform_seq(tdev, &tdev->line_out, true);
169 }