@@ -64,31 +64,18 @@ bool Terminal::Impl::executeCmd(SessionContext *s, const std::string &cmdline)
64
64
return true ;
65
65
}
66
66
67
- const auto &cmd = args[0 ];
68
- if (cmd == " ls" ) {
69
- executeLsCmd (s, args);
70
- } else if (cmd == " pwd" ) {
71
- executePwdCmd (s, args);
72
- } else if (cmd == " cd" ) {
73
- executeCdCmd (s, args);
74
- } else if (cmd == " help" ) {
75
- executeHelpCmd (s, args);
76
- } else if (cmd == " history" ) {
77
- executeHistoryCmd (s, args);
78
- return false ; // ! 查看历史命令不要再存历史
79
- } else if (cmd == " exit" || cmd == " quit" ) {
80
- executeExitCmd (s, args);
81
- } else if (cmd == " tree" ) {
82
- executeTreeCmd (s, args);
67
+ auto &cmd = args[0 ];
68
+ auto iter = buildin_cmd_map_.find (cmd);
69
+ if (iter != buildin_cmd_map_.end ()) {
70
+ return iter->second (s, args);
83
71
} else if (cmd[0 ] == ' !' ) {
84
72
return executeRunHistoryCmd (s, args);
85
73
} else {
86
- executeUserCmd (s, args);
74
+ return executeUserCmd (s, args);
87
75
}
88
- return true ;
89
76
}
90
77
91
- void Terminal::Impl::executeCdCmd (SessionContext *s, const Args &args)
78
+ bool Terminal::Impl::executeCdCmd (SessionContext *s, const Args &args)
92
79
{
93
80
string path_str = " /" ;
94
81
if (args.size () >= 2 )
@@ -113,13 +100,14 @@ void Terminal::Impl::executeCdCmd(SessionContext *s, const Args &args)
113
100
}
114
101
115
102
s->wp_conn ->send (s->token , oss.str ());
103
+ return true ;
116
104
}
117
105
118
- void Terminal::Impl::executeHelpCmd (SessionContext *s, const Args &args)
106
+ bool Terminal::Impl::executeHelpCmd (SessionContext *s, const Args &args)
119
107
{
120
108
if (args.size () < 2 ) {
121
109
printHelp (s);
122
- return ;
110
+ return true ;
123
111
}
124
112
125
113
ostringstream oss;
@@ -140,9 +128,10 @@ void Terminal::Impl::executeHelpCmd(SessionContext *s, const Args &args)
140
128
}
141
129
142
130
s->wp_conn ->send (s->token , oss.str ());
131
+ return true ;
143
132
}
144
133
145
- void Terminal::Impl::executeLsCmd (SessionContext *s, const Args &args)
134
+ bool Terminal::Impl::executeLsCmd (SessionContext *s, const Args &args)
146
135
{
147
136
string path_str = " ." ;
148
137
if (args.size () >= 2 )
@@ -171,7 +160,6 @@ void Terminal::Impl::executeLsCmd(SessionContext *s, const Args &args)
171
160
oss << ' /' ;
172
161
oss << " \r\n " ;
173
162
}
174
- oss << " \r\n " ;
175
163
} else {
176
164
oss << path_str << " is function" << " .\r\n " ;
177
165
}
@@ -180,19 +168,21 @@ void Terminal::Impl::executeLsCmd(SessionContext *s, const Args &args)
180
168
}
181
169
182
170
s->wp_conn ->send (s->token , oss.str ());
171
+ return true ;
183
172
}
184
173
185
- void Terminal::Impl::executeHistoryCmd (SessionContext *s, const Args &)
174
+ bool Terminal::Impl::executeHistoryCmd (SessionContext *s, const Args &)
186
175
{
187
176
ostringstream oss;
188
177
for (size_t i = 0 ; i < s->history .size (); ++i) {
189
178
const auto &cmd = s->history .at (i);
190
179
oss << setw (2 ) << i << " " << cmd << " \r\n " ;
191
180
}
192
181
s->wp_conn ->send (s->token , oss.str ());
182
+ return false ;
193
183
}
194
184
195
- void Terminal::Impl::executeExitCmd (SessionContext *s, const Args &)
185
+ bool Terminal::Impl::executeExitCmd (SessionContext *s, const Args &)
196
186
{
197
187
if (!(s->options & kQuietMode ))
198
188
s->wp_conn ->send (s->token , " Bye!\r\n " );
@@ -204,9 +194,11 @@ void Terminal::Impl::executeExitCmd(SessionContext *s, const Args &)
204
194
},
205
195
__func__
206
196
);
197
+
198
+ return false ;
207
199
}
208
200
209
- void Terminal::Impl::executeTreeCmd (SessionContext *s, const Args &args)
201
+ bool Terminal::Impl::executeTreeCmd (SessionContext *s, const Args &args)
210
202
{
211
203
string path_str = " ." ;
212
204
if (args.size () >= 2 )
@@ -314,9 +306,10 @@ void Terminal::Impl::executeTreeCmd(SessionContext *s, const Args &args)
314
306
}
315
307
316
308
s->wp_conn ->send (s->token , oss.str ());
309
+ return true ;
317
310
}
318
311
319
- void Terminal::Impl::executePwdCmd (SessionContext *s, const Args &)
312
+ bool Terminal::Impl::executePwdCmd (SessionContext *s, const Args &)
320
313
{
321
314
ostringstream oss;
322
315
oss << ' /' ;
@@ -327,6 +320,7 @@ void Terminal::Impl::executePwdCmd(SessionContext *s, const Args &)
327
320
}
328
321
oss << " \r\n " ;
329
322
s->wp_conn ->send (s->token , oss.str ());
323
+ return true ;
330
324
}
331
325
332
326
bool Terminal::Impl::executeRunHistoryCmd (SessionContext *s, const Args &args)
@@ -337,7 +331,7 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
337
331
return execute (s);
338
332
}
339
333
340
- size_t index = 0 ;
334
+ int index = 0 ;
341
335
342
336
if (util::StringTo (sub_cmd, index)) {
343
337
bool is_index_valid = false ;
@@ -366,7 +360,7 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
366
360
return false ;
367
361
}
368
362
369
- void Terminal::Impl::executeUserCmd (SessionContext *s, const Args &args)
363
+ bool Terminal::Impl::executeUserCmd (SessionContext *s, const Args &args)
370
364
{
371
365
ostringstream oss;
372
366
@@ -391,6 +385,7 @@ void Terminal::Impl::executeUserCmd(SessionContext *s, const Args &args)
391
385
}
392
386
393
387
s->wp_conn ->send (s->token , oss.str ());
388
+ return true ;
394
389
}
395
390
396
391
}
0 commit comments