-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand.hpp
294 lines (262 loc) · 7.18 KB
/
command.hpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once
//=====================================================================//
/*! @file
@brief コマンド入力クラス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2015, 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/R8C/blob/master/LICENSE
*/
//=====================================================================//
#include <cstring>
extern "C" {
void sci_putch(char ch);
void sci_puts(const char *str);
char sci_getch(void);
uint16_t sci_length(void);
};
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief command class
@param[in] buffsize バッファサイズ(最小でも9)
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <int16_t buffsize>
class command {
char buff_[buffsize];
int16_t bpos_;
int16_t pos_;
int16_t len_;
int16_t tab_top_;
const char* prompt_;
bool tab_;
// VT-100 ESC シーケンス
static void clear_line_() {
sci_putch(0x1b);
sci_putch('[');
sci_putch('0');
sci_putch('J');
}
static void save_cursor_() {
sci_putch(0x1b);
sci_putch('7');
}
static void load_cursor_() {
sci_putch(0x1b);
sci_putch('8');
}
static void crlf_() {
sci_putch('\r'); ///< CR
sci_putch('\n'); ///< LF
}
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
command() : bpos_(-1), pos_(0), len_(0), tab_top_(-1),
prompt_(nullptr), tab_(false) { buff_[0] = 0; }
//-----------------------------------------------------------------//
/*!
@brief プロムプト文字列を設定
@param[in] text 文字列
*/
//-----------------------------------------------------------------//
void set_prompt(const char* text) { prompt_ = text; }
//-----------------------------------------------------------------//
/*!
@brief サービス @n
定期的に呼び出す
@return 「Enter」キーが押されたら「true」
*/
//-----------------------------------------------------------------//
bool service() {
if(bpos_ < 0 && pos_ == 0) {
if(prompt_) sci_puts(prompt_);
}
bpos_ = pos_;
tab_ = false;
while(sci_length()) {
if(pos_ >= (buffsize - 1)) { ///< バッファが溢れた・・
sci_putch('\\'); ///< バックスラッシュ
buff_[buffsize - 1] = 0;
pos_ = 0;
bpos_ = -1;
crlf_();
return false;
} else if(pos_ >= (buffsize - 8)) { ///< バッファが溢れそうな警告
sci_putch('G' - 0x40); ///< Ctrl-G
}
char ch = sci_getch();
switch(ch) {
case '\r': // Enter キー
buff_[pos_] = 0;
len_ = pos_;
clear_line_();
crlf_();
pos_ = 0;
bpos_ = -1;
tab_top_ = -1;
return true;
case 0x08: // バックスペース
if(pos_) {
--pos_;
sci_putch(0x08);
if(buff_[pos_] < 0x20) {
sci_putch(0x08);
}
} else {
pos_ = 0;
bpos_ = -1;
crlf_();
}
break;
case '\t': // TAB キー
if(tab_top_ < 0) {
tab_top_ = pos_;
save_cursor_();
}
tab_ = true;
break;
default:
if(ch < 0x20) { ///< 他の ctrl コード
buff_[pos_] = ch;
++pos_;
sci_putch('^');
sci_putch(ch + 0x40);
} else {
buff_[pos_] = ch;
++pos_;
sci_putch(ch);
}
buff_[pos_] = 0;
break;
}
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief コマンド行を取得
@return コマンド行
*/
//-----------------------------------------------------------------//
const char* get_command() const { return buff_; }
//-----------------------------------------------------------------//
/*!
@brief ワード数を取得
@return ワード数を返す
*/
//-----------------------------------------------------------------//
uint8_t get_words() const {
const char* p = buff_;
char bc = ' ';
uint8_t n = 0;
while(1) {
char ch = *p++;
if(ch == 0) break;
if(bc == ' ' && ch != ' ') {
++n;
}
bc = ch;
}
return n;
}
//-----------------------------------------------------------------//
/*!
@brief ワードを取得
@param[in] argc ワード位置
@param[in] limit ワード文字列リミット数
@param[out] word ワード文字列格納ポインター
@return 取得できたら「true」を返す
*/
//-----------------------------------------------------------------//
bool get_word(uint8_t argc, uint8_t limit, char* word) const {
const char* p = buff_;
char bc = ' ';
const char* wd = p;
while(1) {
char ch = *p;
if(bc == ' ' && ch != ' ') {
wd = p;
}
if(bc != ' ' && (ch == ' ' || ch == 0)) {
if(argc == 0) {
uint8_t i;
for(i = 0; i < (p - wd); ++i) {
--limit;
if(limit == 0) break;
word[i] = wd[i];
}
word[i] = 0;
return true;
}
--argc;
}
if(ch == 0) break;
bc = ch;
++p;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief ワードを比較
@param[in] argc ワード位置
@param[in] key 比較文字列
@return
*/
//-----------------------------------------------------------------//
bool cmp_word(uint8_t argc, const char* key) const {
if(key == nullptr) return false;
const char* p = buff_;
char bc = ' ';
int keylen = std::strlen(key);
while(1) {
const char* top;
char ch = *p;
if(bc == ' ' && ch != ' ') {
top = p;
}
if(bc != ' ' && (ch == ' ' || ch == 0)) {
int len = p - top;
if(argc == 0 && len == keylen) {
return std::strncmp(key, top, keylen) == 0;
}
--argc;
}
if(ch == 0) break;
bc = ch;
++p;
}
return false;
}
//-----------------------------------------------------------------//
/*!
@brief TAB キーが押されたか
@return 押されたら「true」
*/
//-----------------------------------------------------------------//
bool probe_tab() const { return tab_; }
//-----------------------------------------------------------------//
/*!
@brief TAB 注入位置のリセット
*/
//-----------------------------------------------------------------//
void reset_tab() { tab_top_ = -1; }
//-----------------------------------------------------------------//
/*!
@brief TAB キーの候補を注入
@param[in] key 注入文字列
*/
//-----------------------------------------------------------------//
void injection_tab(const char* key) {
if(tab_top_ < 0) return;
std::strcpy(&buff_[tab_top_], key);
load_cursor_();
sci_puts(key);
}
};
}