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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/ne-foss-org/nekernel
#ifndef KERNELKIT_DEBUGOUTPUT_H
#define KERNELKIT_DEBUGOUTPUT_H
#include <CompilerKit/CompilerKit.h>
#include <KernelKit/DeviceMgr.h>
#include <NeKit/OwnPtr.h>
#include <NeKit/Stream.h>
#include <NeKit/Utils.h>
namespace Kernel {
class TerminalDevice;
class DTraceDevice;
class NeTraceDevice;
class Utf8TerminalDevice;
inline TerminalDevice end_line();
inline TerminalDevice number(const Long& x);
inline TerminalDevice hex_number(const Long& x);
// @brief Emulates a VT100 terminal.
class TerminalDevice final NE_DEVICE<const Char*> {
public:
TerminalDevice(void (*print)(IDevice*, const Char*), void (*gets)(IDevice*, const Char*))
: IDevice<const Char*>(print, gets) {}
~TerminalDevice() override;
/// @brief returns device name (terminal name)
/// @return string type (const Char*)
const Char* Name() const override { return ("TerminalDevice"); }
NE_COPY_DEFAULT(TerminalDevice)
STATIC TerminalDevice The();
};
class Utf8TerminalDevice final NE_DEVICE<const Utf8Char*> {
public:
Utf8TerminalDevice(void (*print)(IDevice*, const Utf8Char*),
void (*gets)(IDevice*, const Utf8Char*))
: IDevice<const Utf8Char*>(print, gets) {}
~Utf8TerminalDevice() override;
/// @brief returns device name (terminal name)
/// @return string type (const Char*)
const Char* Name() const override { return ("Utf8TerminalDevice"); }
NE_COPY_DEFAULT(Utf8TerminalDevice)
STATIC Utf8TerminalDevice The();
};
inline TerminalDevice end_line() {
TerminalDevice self = TerminalDevice::The();
self.operator<<("\r");
return self;
}
inline Utf8TerminalDevice utf_end_line() {
Utf8TerminalDevice self = Utf8TerminalDevice::The();
self.operator<<(u8"\r");
return self;
}
inline TerminalDevice carriage_return() {
TerminalDevice self = TerminalDevice::The();
self.operator<<("\r");
return self;
}
inline TerminalDevice tabulate() {
TerminalDevice self = TerminalDevice::The();
self.operator<<("\t");
return self;
}
/// @brief emulate a terminal bell, like the VT100 does.
inline TerminalDevice bell() {
TerminalDevice self = TerminalDevice::The();
self.operator<<("\a");
return self;
}
namespace Detail {
inline TerminalDevice _write_number(const Long& x, TerminalDevice& term) {
UInt64 y = (x > 0 ? x : -x) / 10;
UInt64 h = (x > 0 ? x : -x) % 10;
if (y) _write_number(y, term);
/* fail if the number is not base-10 */
if (h > 10) {
_write_number('?', term);
return term;
}
if (y == ~0UL) y = -y;
const Char kNumbers[11] = "0123456789";
Char buf[2];
buf[0] = kNumbers[h];
buf[1] = 0;
term.operator<<(buf);
return term;
}
inline TerminalDevice _write_number_hex(const Long& x, TerminalDevice& term) {
UInt64 y = (x > 0 ? x : -x) / 16;
UInt64 h = (x > 0 ? x : -x) % 16;
if (y) _write_number_hex(y, term);
/* fail if the hex number is not base-16 */
if (h > 16) {
_write_number_hex('?', term);
return term;
}
if (y == ~0UL) y = -y;
const Char kNumbers[17] = "0123456789ABCDEF";
Char buf[2];
buf[0] = kNumbers[h];
buf[1] = 0;
term.operator<<(buf);
return term;
}
} // namespace Detail
inline TerminalDevice hex_number(const Long& x) {
TerminalDevice self = TerminalDevice::The();
self << "0x";
Detail::_write_number_hex(x, self);
return self;
}
inline TerminalDevice number(const Long& x) {
TerminalDevice self = TerminalDevice::The();
Detail::_write_number(x, self);
return self;
}
inline TerminalDevice get_console_in(Char* buf) {
TerminalDevice self = TerminalDevice::The();
self >> buf;
return self;
}
inline constexpr auto kDebugPort = 51820;
inline constexpr auto kDebugMagic = "VMK1.0.0;";
inline constexpr auto kDebugVersion = 0x0100;
inline constexpr SizeT kDebugCmdLen = 256U;
typedef Char rt_debug_cmd[kDebugCmdLen];
inline TerminalDevice& operator<<(TerminalDevice& src, const Long& num) {
src = number(num);
return src;
}
} // namespace Kernel
#ifdef kout
#undef kout
#endif // ifdef kout
#define kout TerminalDevice::The()
#ifdef kendl
#undef kendl
#endif // ifdef kendl
#define kendl end_line()
#ifdef kout8
#undef kout8
#endif // ifdef kout8
#define kout8 Utf8TerminalDevice::The()
#ifdef kendl8
#undef kendl8
#endif // ifdef kendl8
#define kendl8 utf_end_line()
#endif
|