interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / core / line_rx.c

107 lines · 3192 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* line_rx.c -- operator line assembler (see line_rx.h). Pure logic. */
18#include "line_rx.h"
19
20void ck_line_rx_init(struct ck_line_rx *l)
21{
22 l->len = 0;
23 l->overflow = l->damaged = l->done = 0;
24 l->buf[0] = 0;
25 l->overlong = l->damaged_lines = l->dropped = 0;
26}
27
28static void clear(struct ck_line_rx *l)
29{
30 l->len = 0;
31 l->overflow = l->damaged = 0;
32 l->buf[0] = 0;
33}
34
35enum ck_line_ev ck_line_rx_feed(struct ck_line_rx *l, int b)
36{
37 if (l->done) { /* the previous call handed out a complete line */
38 l->done = 0;
39 clear(l);
40 }
41 if (b == CK_LINE_RX_ERR) {
42 l->damaged = 1;
43 return CK_LINE_NONE;
44 }
45 if (b < 0 || b > 0xff)
46 return CK_LINE_NONE;
47 char c = (char)b;
48 if (c == '\r' || c == '\n') {
49 if (l->overflow) {
50 l->overlong++;
51 clear(l);
52 return CK_LINE_OVERLONG;
53 }
54 if (l->damaged) {
55 l->damaged_lines++;
56 clear(l);
57 return CK_LINE_DAMAGED;
58 }
59 l->buf[l->len] = 0;
60 l->done = 1;
61 return CK_LINE_DONE;
62 }
63 if (c == 0x7f || c == 0x08) {
64 if (l->len && !l->overflow) {
65 l->len--;
66 return CK_LINE_ERASE;
67 }
68 return CK_LINE_NONE;
69 }
70 if (b < 0x20 || b > 0x7e) {
71 l->dropped++;
72 return CK_LINE_NONE;
73 }
74 if (l->overflow)
75 return CK_LINE_NONE;
76 if (l->len >= CK_LC_LINE_MAX) {
77 l->overflow = 1;
78 return CK_LINE_NONE;
79 }
80 l->buf[l->len++] = c;
81 return CK_LINE_ECHO;
82}
83
84int ck_line_rx_cancel_poll(struct ck_line_rx *l, int (*poll)(void), void (*refuse)(enum ck_lc_verb v, const char *reason))
85{
86 for (unsigned n = 0; n < CK_LINE_RX_BURST; n++) {
87 int b = poll();
88 if (b == CK_LINE_RX_EMPTY)
89 return 0;
90 switch (ck_line_rx_feed(l, b)) {
91 case CK_LINE_DONE: {
92 const char *arg;
93 size_t arg_len;
94 enum ck_lc_verb v = l->len ? ck_lc_decode(l->buf, l->len, &arg, &arg_len) : CK_LC_V_EMPTY;
95 if (v == CK_LC_V_CANCEL)
96 return 1;
97 if (v != CK_LC_V_EMPTY)
98 refuse(v, "generating: only cancel is accepted");
99 break;
100 }
101 case CK_LINE_OVERLONG: refuse(CK_LC_V_INVALID, "line longer than the limit"); break;
102 case CK_LINE_DAMAGED: refuse(CK_LC_V_INVALID, "receive error in the line (overrun, parity, framing or break): type it again"); break;
103 default: break;
104 }
105 }
106 return 0;
107}