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