3 * @author Lunaixsky (lunaxisky@qq.com)
4 * @brief Line controller master that handle all POSIX-control code
8 * @copyright Copyright (c) 2023
12 #include <usr/lunaix/term.h>
14 #include <lunaix/process.h>
15 #include <lunaix/spike.h>
18 raise_sig(struct term* at_term, struct linebuffer* lbuf, int sig)
20 term_sendsig(at_term, SIGINT);
21 lbuf->sflags |= LSTATE_SIGRAISE;
24 static inline int must_inline optimize("-fipa-cp-clone")
25 lcntl_transform_seq(struct term* tdev, struct linebuffer* lbuf, bool out)
27 struct rbuffer* raw = lbuf->current;
28 struct rbuffer* cooked = lbuf->next;
29 struct rbuffer* output = tdev->line_out.current;
31 int i = 0, _if = tdev->iflags & -!out, _of = tdev->oflags & -!!out,
33 int allow_more = 1, latest_eol = cooked->ptr;
35 bool should_flush = false;
37 int (*lcntl_slave_put)(struct term*, struct linebuffer*, char) =
38 tdev->lcntl->process_and_put;
40 #define EOL tdev->cc[_VEOL]
41 #define EOF tdev->cc[_VEOF]
42 #define ERASE tdev->cc[_VERASE]
43 #define KILL tdev->cc[_VKILL]
44 #define INTR tdev->cc[_VINTR]
45 #define QUIT tdev->cc[_VQUIT]
46 #define SUSP tdev->cc[_VSUSP]
49 // Keep all cc's (except VMIN & VTIME) up to L2 cache.
50 for (size_t i = 0; i < _VMIN; i++) {
51 prefetch_rd(&tdev->cc[i], 2);
55 while (rbuffer_get(raw, &c) && allow_more) {
57 if (c == '\r' && ((_if & _ICRNL) || (_of & _OCRNL))) {
59 } else if (c == '\n') {
60 if ((_if & _INLCR) || (_of & (_ONLRET | _ONLCR))) {
66 if ((_if & _BRKINT)) {
67 raise_sig(tdev, lbuf, SIGINT);
71 if ((_if & _IGNBRK)) {
76 if ('a' <= c && c <= 'z') {
79 } else if ((_of & _OLCUC)) {
85 latest_eol = cooked->ptr + 1;
86 if (!out && (_lf & _ECHONL)) {
87 rbuffer_put(output, c);
96 // For input procesing
98 if (c == '\n' || c == EOL) {
99 lbuf->sflags |= LSTATE_EOL;
101 } else if (c == EOF) {
102 lbuf->sflags |= LSTATE_EOF;
105 } else if (c == INTR) {
106 raise_sig(tdev, lbuf, SIGINT);
107 } else if (c == QUIT) {
108 raise_sig(tdev, lbuf, SIGKILL);
110 } else if (c == SUSP) {
111 raise_sig(tdev, lbuf, SIGSTOP);
112 } else if (c == ERASE) {
113 rbuffer_erase(cooked);
114 } else if (c == KILL) {
115 // TODO shrink the rbuffer
124 rbuffer_put(output, c);
126 if ((_lf & _ECHOE) && c == ERASE) {
127 rbuffer_erase(output);
129 if ((_lf & _ECHOK) && c == KILL) {
130 rbuffer_put(output, c);
131 rbuffer_put(output, '\n');
135 if (!out && (_lf & _IEXTEN) && lcntl_slave_put) {
136 allow_more = lcntl_slave_put(tdev, lbuf, c);
138 allow_more = rbuffer_put(cooked, c);
142 if (should_flush && !(_lf & _NOFLSH)) {
152 lcntl_transform_inseq(struct term* tdev)
154 return lcntl_transform_seq(tdev, &tdev->line_in, false);
158 lcntl_transform_outseq(struct term* tdev)
160 return lcntl_transform_seq(tdev, &tdev->line_in, true);