@@ -64,31 +64,18 @@ bool Terminal::Impl::executeCmd(SessionContext *s, const std::string &cmdline)
6464 return true ;
6565 }
6666
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);
8371 } else if (cmd[0 ] == ' !' ) {
8472 return executeRunHistoryCmd (s, args);
8573 } else {
86- executeUserCmd (s, args);
74+ return executeUserCmd (s, args);
8775 }
88- return true ;
8976}
9077
91- void Terminal::Impl::executeCdCmd (SessionContext *s, const Args &args)
78+ bool Terminal::Impl::executeCdCmd (SessionContext *s, const Args &args)
9279{
9380 string path_str = " /" ;
9481 if (args.size () >= 2 )
@@ -113,13 +100,14 @@ void Terminal::Impl::executeCdCmd(SessionContext *s, const Args &args)
113100 }
114101
115102 s->wp_conn ->send (s->token , oss.str ());
103+ return true ;
116104}
117105
118- void Terminal::Impl::executeHelpCmd (SessionContext *s, const Args &args)
106+ bool Terminal::Impl::executeHelpCmd (SessionContext *s, const Args &args)
119107{
120108 if (args.size () < 2 ) {
121109 printHelp (s);
122- return ;
110+ return true ;
123111 }
124112
125113 ostringstream oss;
@@ -140,9 +128,10 @@ void Terminal::Impl::executeHelpCmd(SessionContext *s, const Args &args)
140128 }
141129
142130 s->wp_conn ->send (s->token , oss.str ());
131+ return true ;
143132}
144133
145- void Terminal::Impl::executeLsCmd (SessionContext *s, const Args &args)
134+ bool Terminal::Impl::executeLsCmd (SessionContext *s, const Args &args)
146135{
147136 string path_str = " ." ;
148137 if (args.size () >= 2 )
@@ -171,7 +160,6 @@ void Terminal::Impl::executeLsCmd(SessionContext *s, const Args &args)
171160 oss << ' /' ;
172161 oss << " \r\n " ;
173162 }
174- oss << " \r\n " ;
175163 } else {
176164 oss << path_str << " is function" << " .\r\n " ;
177165 }
@@ -180,19 +168,21 @@ void Terminal::Impl::executeLsCmd(SessionContext *s, const Args &args)
180168 }
181169
182170 s->wp_conn ->send (s->token , oss.str ());
171+ return true ;
183172}
184173
185- void Terminal::Impl::executeHistoryCmd (SessionContext *s, const Args &)
174+ bool Terminal::Impl::executeHistoryCmd (SessionContext *s, const Args &)
186175{
187176 ostringstream oss;
188177 for (size_t i = 0 ; i < s->history .size (); ++i) {
189178 const auto &cmd = s->history .at (i);
190179 oss << setw (2 ) << i << " " << cmd << " \r\n " ;
191180 }
192181 s->wp_conn ->send (s->token , oss.str ());
182+ return false ;
193183}
194184
195- void Terminal::Impl::executeExitCmd (SessionContext *s, const Args &)
185+ bool Terminal::Impl::executeExitCmd (SessionContext *s, const Args &)
196186{
197187 if (!(s->options & kQuietMode ))
198188 s->wp_conn ->send (s->token , " Bye!\r\n " );
@@ -204,9 +194,11 @@ void Terminal::Impl::executeExitCmd(SessionContext *s, const Args &)
204194 },
205195 __func__
206196 );
197+
198+ return false ;
207199}
208200
209- void Terminal::Impl::executeTreeCmd (SessionContext *s, const Args &args)
201+ bool Terminal::Impl::executeTreeCmd (SessionContext *s, const Args &args)
210202{
211203 string path_str = " ." ;
212204 if (args.size () >= 2 )
@@ -314,9 +306,10 @@ void Terminal::Impl::executeTreeCmd(SessionContext *s, const Args &args)
314306 }
315307
316308 s->wp_conn ->send (s->token , oss.str ());
309+ return true ;
317310}
318311
319- void Terminal::Impl::executePwdCmd (SessionContext *s, const Args &)
312+ bool Terminal::Impl::executePwdCmd (SessionContext *s, const Args &)
320313{
321314 ostringstream oss;
322315 oss << ' /' ;
@@ -327,6 +320,7 @@ void Terminal::Impl::executePwdCmd(SessionContext *s, const Args &)
327320 }
328321 oss << " \r\n " ;
329322 s->wp_conn ->send (s->token , oss.str ());
323+ return true ;
330324}
331325
332326bool Terminal::Impl::executeRunHistoryCmd (SessionContext *s, const Args &args)
@@ -337,7 +331,7 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
337331 return execute (s);
338332 }
339333
340- size_t index = 0 ;
334+ int index = 0 ;
341335
342336 if (util::StringTo (sub_cmd, index)) {
343337 bool is_index_valid = false ;
@@ -366,7 +360,7 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
366360 return false ;
367361}
368362
369- void Terminal::Impl::executeUserCmd (SessionContext *s, const Args &args)
363+ bool Terminal::Impl::executeUserCmd (SessionContext *s, const Args &args)
370364{
371365 ostringstream oss;
372366
@@ -391,6 +385,7 @@ void Terminal::Impl::executeUserCmd(SessionContext *s, const Args &args)
391385 }
392386
393387 s->wp_conn ->send (s->token , oss.str ());
388+ return true ;
394389}
395390
396391}
0 commit comments