feat: a better boot command line parser
[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 (c == '\n') {
84             latest_eol = cooked->ptr + 1;
85             if (!out && (_lf & _ECHONL)) {
86                 rbuffer_put(output, c);
87             }
88         }
89
90         if (out) {
91             goto do_out;
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             rbuffer_erase(cooked);
111         } else if (c == KILL) {
112             // TODO shrink the rbuffer
113         } else {
114             goto keep;
115         }
116
117         if ((_lf & _ECHOE) && c == ERASE) {
118             rbuffer_put(output, '\x8'); 
119             rbuffer_put(output, ' '); 
120             rbuffer_put(output, '\x8'); 
121         }
122         if ((_lf & _ECHOK) && c == KILL) {
123             rbuffer_put(output, c);
124             rbuffer_put(output, '\n');
125         }
126
127         continue;
128
129     keep:
130         if ((_lf & _ECHO)) {
131             rbuffer_put(output, c);
132         }
133
134         goto put_char;
135
136     do_out:
137         if (c == '\n' && (_of & _ONLCR)) {
138             rbuffer_put(cooked, '\r');
139         }
140
141     put_char:
142         if (!out && (_lf & _IEXTEN) && lcntl_slave_put) {
143             allow_more = lcntl_slave_put(tdev, lbuf, c);
144         } else {
145             allow_more = rbuffer_put(cooked, c);
146         }
147     }
148
149     if (!rbuffer_empty(output) && !(_lf & _NOFLSH)) {
150         term_flush(tdev);
151     }
152
153     line_flip(lbuf);
154
155     return i;
156 }
157
158 int
159 lcntl_transform_inseq(struct term* tdev)
160 {
161     return lcntl_transform_seq(tdev, &tdev->line_in, false);
162 }
163
164 int
165 lcntl_transform_outseq(struct term* tdev)
166 {
167     return lcntl_transform_seq(tdev, &tdev->line_out, true);
168 }