📄 cmdline_8c-source.html
字号:
00225 cmdlineOutputFunc(ASCII_CR);00226 cmdlineOutputFunc(ASCII_LF);00227 <span class="comment">// add null termination to command</span>00228 CmdlineBuffer[CmdlineBufferLength++] = 0;00229 CmdlineBufferEditPos++;00230 <span class="comment">// command is complete, process it</span>00231 cmdlineProcessInputString();00232 <span class="comment">// reset buffer</span>00233 CmdlineBufferLength = 0;00234 CmdlineBufferEditPos = 0;00235 }00236 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == ASCII_BS)00237 {00238 <span class="keywordflow">if</span>(CmdlineBufferEditPos)00239 {00240 <span class="comment">// is this a simple delete (off the end of the line)</span>00241 <span class="keywordflow">if</span>(CmdlineBufferEditPos == CmdlineBufferLength)00242 {00243 <span class="comment">// destructive backspace</span>00244 <span class="comment">// echo backspace-space-backspace</span>00245 cmdlineOutputFunc(ASCII_BS);00246 cmdlineOutputFunc(<span class="charliteral">' '</span>);00247 cmdlineOutputFunc(ASCII_BS);00248 <span class="comment">// decrement our buffer length and edit position</span>00249 CmdlineBufferLength--;00250 CmdlineBufferEditPos--;00251 }00252 <span class="keywordflow">else</span>00253 {00254 <span class="comment">// edit/cursor position != end of buffer</span>00255 <span class="comment">// we're deleting characters at a mid-line edit position</span>00256 <span class="comment">// shift characters down, effectively deleting</span>00257 CmdlineBufferLength--;00258 CmdlineBufferEditPos--;00259 <span class="keywordflow">for</span>(i=CmdlineBufferEditPos; i<CmdlineBufferLength; i++)00260 CmdlineBuffer[i] = CmdlineBuffer[i+1];00261 <span class="comment">// repaint</span>00262 cmdlineRepaint();00263 <span class="comment">// add space to clear leftover characters</span>00264 cmdlineOutputFunc(<span class="charliteral">' '</span>);00265 <span class="comment">// reposition cursor</span>00266 <span class="keywordflow">for</span>(i=CmdlineBufferEditPos; i<(CmdlineBufferLength+1); i++)00267 cmdlineOutputFunc(ASCII_BS);00268 }00269 }00270 <span class="keywordflow">else</span>00271 {00272 <span class="comment">// else, ring the bell</span>00273 cmdlineOutputFunc(ASCII_BEL);00274 }00275 }00276 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == ASCII_DEL)00277 {00278 <span class="comment">// not yet handled</span>00279 }00280 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == ASCII_ESC)00281 {00282 CmdlineInputVT100State = 1;00283 }00284 }00285 00286 <span class="keywordtype">void</span> cmdlineRepaint(<span class="keywordtype">void</span>)00287 {00288 u08* ptr;00289 u08 i;00290 00291 <span class="comment">// carriage return</span>00292 cmdlineOutputFunc(ASCII_CR);00293 <span class="comment">// print fresh prompt</span>00294 cmdlinePrintPrompt();00295 <span class="comment">// print the new command line buffer</span>00296 i = CmdlineBufferLength;00297 ptr = CmdlineBuffer;00298 <span class="keywordflow">while</span>(i--) cmdlineOutputFunc(*ptr++);00299 }00300 00301 <span class="keywordtype">void</span> cmdlineDoHistory(u08 action)00302 {00303 <span class="keywordflow">switch</span>(action)00304 {00305 <span class="keywordflow">case</span> CMDLINE_HISTORY_SAVE:00306 <span class="comment">// copy CmdlineBuffer to history if not null string</span>00307 <span class="keywordflow">if</span>( strlen(CmdlineBuffer) )00308 strcpy(CmdlineHistory[0], CmdlineBuffer);00309 <span class="keywordflow">break</span>;00310 <span class="keywordflow">case</span> CMDLINE_HISTORY_PREV:00311 <span class="comment">// copy history to current buffer</span>00312 strcpy(CmdlineBuffer, CmdlineHistory[0]);00313 <span class="comment">// set the buffer position to the end of the line</span>00314 CmdlineBufferLength = strlen(CmdlineBuffer);00315 CmdlineBufferEditPos = CmdlineBufferLength;00316 <span class="comment">// "re-paint" line</span>00317 cmdlineRepaint();00318 <span class="keywordflow">break</span>;00319 <span class="keywordflow">case</span> CMDLINE_HISTORY_NEXT:00320 <span class="keywordflow">break</span>;00321 }00322 }00323 00324 <span class="keywordtype">void</span> cmdlineProcessInputString(<span class="keywordtype">void</span>)00325 {00326 u08 cmdIndex;00327 u08 i=0;00328 00329 <span class="comment">// save command in history</span>00330 cmdlineDoHistory(CMDLINE_HISTORY_SAVE);00331 00332 <span class="comment">// find the end of the command (excluding arguments)</span>00333 <span class="comment">// find first whitespace character in CmdlineBuffer</span>00334 <span class="keywordflow">while</span>( !((CmdlineBuffer[i] == <span class="charliteral">' '</span>) || (CmdlineBuffer[i] == 0)) ) i++;00335 00336 <span class="keywordflow">if</span>(!i)00337 {00338 <span class="comment">// command was null or empty</span>00339 <span class="comment">// output a new prompt</span>00340 cmdlinePrintPrompt();00341 <span class="comment">// we're done</span>00342 <span class="keywordflow">return</span>;00343 }00344 00345 <span class="comment">// search command list for match with entered command</span>00346 <span class="keywordflow">for</span>(cmdIndex=0; cmdIndex<CmdlineNumCommands; cmdIndex++)00347 {00348 <span class="keywordflow">if</span>( !strncmp(CmdlineCommandList[cmdIndex], CmdlineBuffer, i) )00349 {00350 <span class="comment">// user-entered command matched a command in the list (database)</span>00351 <span class="comment">// run the corresponding function</span>00352 CmdlineExecFunction = CmdlineFunctionList[cmdIndex];00353 <span class="comment">// new prompt will be output after user function runs</span>00354 <span class="comment">// and we're done</span>00355 <span class="keywordflow">return</span>;00356 }00357 }00358 00359 <span class="comment">// if we did not get a match</span>00360 <span class="comment">// output an error message</span>00361 cmdlinePrintError();00362 <span class="comment">// output a new prompt</span>00363 cmdlinePrintPrompt();00364 }00365 <a name="l00366"></a><a class="code" href="group__cmdline.html#ga5">00366</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga5">cmdlineMainLoop</a>(<span class="keywordtype">void</span>)00367 {00368 <span class="comment">// do we have a command/function to be executed</span>00369 <span class="keywordflow">if</span>(CmdlineExecFunction)00370 {00371 <span class="comment">// run it</span>00372 CmdlineExecFunction();00373 <span class="comment">// reset</span>00374 CmdlineExecFunction = 0;00375 <span class="comment">// output new prompt</span>00376 cmdlinePrintPrompt();00377 }00378 }00379 00380 <span class="keywordtype">void</span> cmdlinePrintPrompt(<span class="keywordtype">void</span>)00381 {00382 <span class="comment">// print a new command prompt</span>00383 u08* ptr = CmdlinePrompt;00384 <span class="keywordflow">while</span>(pgm_read_byte(ptr)) cmdlineOutputFunc( pgm_read_byte(ptr++) );00385 }00386 00387 <span class="keywordtype">void</span> cmdlinePrintError(<span class="keywordtype">void</span>)00388 {00389 u08 * ptr;00390 00391 <span class="comment">// print a notice header</span>00392 <span class="comment">// (u08*) cast used to avoid compiler warning</span>00393 ptr = (u08*)CmdlineNotice;00394 <span class="keywordflow">while</span>(pgm_read_byte(ptr)) cmdlineOutputFunc( pgm_read_byte(ptr++) );00395 00396 <span class="comment">// print the offending command</span>00397 ptr = CmdlineBuffer;00398 <span class="keywordflow">while</span>((*ptr) && (*ptr != <span class="charliteral">' '</span>)) cmdlineOutputFunc(*ptr++);00399 00400 cmdlineOutputFunc(<span class="charliteral">':'</span>);00401 cmdlineOutputFunc(<span class="charliteral">' '</span>);00402 00403 <span class="comment">// print the not-found message</span>00404 <span class="comment">// (u08*) cast used to avoid compiler warning</span>00405 ptr = (u08*)CmdlineCmdNotFound;00406 <span class="keywordflow">while</span>(pgm_read_byte(ptr)) cmdlineOutputFunc( pgm_read_byte(ptr++) );00407 00408 cmdlineOutputFunc(<span class="charliteral">'\r'</span>);00409 cmdlineOutputFunc(<span class="charliteral">'\n'</span>);00410 }00411 00412 <span class="comment">// argument retrieval commands</span>00413 00414 <span class="comment">// return string pointer to argument [argnum]</span><a name="l00415"></a><a class="code" href="group__cmdline.html#ga11">00415</a> u08* <a class="code" href="group__cmdline.html#ga11">cmdlineGetArgStr</a>(u08 argnum)00416 {00417 <span class="comment">// find the offset of argument number [argnum]</span>00418 u08 idx=0;00419 u08 arg;00420 00421 <span class="comment">// find the first non-whitespace character</span>00422 <span class="keywordflow">while</span>( (CmdlineBuffer[idx] != 0) && (CmdlineBuffer[idx] == <span class="charliteral">' '</span>)) idx++;00423 00424 <span class="comment">// we are at the first argument</span>00425 <span class="keywordflow">for</span>(arg=0; arg<argnum; arg++)00426 {00427 <span class="comment">// find the next whitespace character</span>00428 <span class="keywordflow">while</span>( (CmdlineBuffer[idx] != 0) && (CmdlineBuffer[idx] != <span class="charliteral">' '</span>)) idx++;00429 <span class="comment">// find the first non-whitespace character</span>00430 <span class="keywordflow">while</span>( (CmdlineBuffer[idx] != 0) && (CmdlineBuffer[idx] == <span class="charliteral">' '</span>)) idx++;00431 }00432 <span class="comment">// we are at the requested argument or the end of the buffer</span>00433 <span class="keywordflow">return</span> &CmdlineBuffer[idx];00434 }00435 00436 <span class="comment">// return argument [argnum] interpreted as a decimal integer</span><a name="l00437"></a><a class="code" href="group__cmdline.html#ga12">00437</a> <span class="keywordtype">long</span> <a class="code" href="group__cmdline.html#ga12">cmdlineGetArgInt</a>(u08 argnum)00438 {00439 <span class="keywordtype">char</span>* endptr;00440 <span class="keywordflow">return</span> strtol(<a class="code" href="group__cmdline.html#ga11">cmdlineGetArgStr</a>(argnum), &endptr, 10);00441 }00442 00443 <span class="comment">// return argument [argnum] interpreted as a hex integer</span><a name="l00444"></a><a class="code" href="group__cmdline.html#ga13">00444</a> <span class="keywordtype">long</span> <a class="code" href="group__cmdline.html#ga13">cmdlineGetArgHex</a>(u08 argnum)00445 {00446 <span class="keywordtype">char</span>* endptr;00447 <span class="keywordflow">return</span> strtol(<a class="code" href="group__cmdline.html#ga11">cmdlineGetArgStr</a>(argnum), &endptr, 16);00448 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Oct 24 16:03:54 2005 for Procyon AVRlib by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -