📄 cmdline_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: cmdline.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div><h1>cmdline.c</h1><a href="cmdline_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file cmdline.c \brief Command-Line Interface Library. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'cmdline.c'</span>00005 <span class="comment">// Title : Command-Line Interface Library</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2003</span>00007 <span class="comment">// Created : 2003.07.16</span>00008 <span class="comment">// Revised : 2003.07.23</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : Atmel AVR Series</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>00016 <span class="comment">//</span>00017 <span class="comment">// This code is distributed under the GNU Public License</span>00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="comment">//----- Include Files ---------------------------------------------------------</span>00023 <span class="preprocessor">#include <avr/io.h></span> <span class="comment">// include I/O definitions (port names, pin names, etc)</span>00024 <span class="preprocessor">#include <avr/signal.h></span> <span class="comment">// include "signal" names (interrupt names)</span>00025 <span class="preprocessor">#include <avr/interrupt.h></span> <span class="comment">// include interrupt support</span>00026 <span class="preprocessor">#include <avr/pgmspace.h></span> <span class="comment">// include AVR program memory support</span>00027 <span class="preprocessor">#include <string.h></span> <span class="comment">// include standard C string functions</span>00028 <span class="preprocessor">#include <stdlib.h></span> <span class="comment">// include stdlib for string conversion functions</span>00029 00030 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> <span class="comment">// include our global settings</span>00031 <span class="preprocessor">#include "<a class="code" href="cmdline_8h.html">cmdline.h</a>"</span>00032 00033 <span class="comment">// include project-specific configuration</span>00034 <span class="preprocessor">#include "<a class="code" href="cmdlineconf_8h.html">cmdlineconf.h</a>"</span>00035 00036 <span class="comment">// defines</span>00037 <span class="preprocessor">#define ASCII_BEL 0x07</span>00038 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_BS 0x08</span>00039 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_CR 0x0D</span>00040 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_LF 0x0A</span>00041 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_ESC 0x1B</span>00042 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_DEL 0x7F</span>00043 <span class="preprocessor"></span>00044 <span class="preprocessor">#define VT100_ARROWUP 'A'</span>00045 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWDOWN 'B'</span>00046 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWRIGHT 'C'</span>00047 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWLEFT 'D'</span>00048 <span class="preprocessor"></span>00049 <span class="preprocessor">#define CMDLINE_HISTORY_SAVE 0</span>00050 <span class="preprocessor"></span><span class="preprocessor">#define CMDLINE_HISTORY_PREV 1</span>00051 <span class="preprocessor"></span><span class="preprocessor">#define CMDLINE_HISTORY_NEXT 2</span>00052 <span class="preprocessor"></span>00053 00054 <span class="comment">// Global variables</span>00055 00056 <span class="comment">// strings</span>00057 u08 PROGMEM CmdlinePrompt[] = <span class="stringliteral">"cmd>"</span>;00058 u08 PROGMEM CmdlineNotice[] = <span class="stringliteral">"cmdline: "</span>;00059 u08 PROGMEM CmdlineCmdNotFound[] = <span class="stringliteral">"command not found"</span>;00060 00061 <span class="comment">// command list</span>00062 <span class="comment">// -commands are null-terminated strings</span>00063 <span class="keyword">static</span> <span class="keywordtype">char</span> CmdlineCommandList[CMDLINE_MAX_COMMANDS][CMDLINE_MAX_CMD_LENGTH];00064 <span class="comment">// command function pointer list</span>00065 <span class="keyword">static</span> CmdlineFuncPtrType CmdlineFunctionList[CMDLINE_MAX_COMMANDS];00066 <span class="comment">// number of commands currently registered</span>00067 u08 CmdlineNumCommands;00068 00069 u08 CmdlineBuffer[CMDLINE_BUFFERSIZE];00070 u08 CmdlineBufferLength;00071 u08 CmdlineBufferEditPos;00072 u08 CmdlineInputVT100State;00073 u08 CmdlineHistory[CMDLINE_HISTORYSIZE][CMDLINE_BUFFERSIZE];00074 CmdlineFuncPtrType CmdlineExecFunction;00075 00076 <span class="comment">// Functions</span>00077 00078 <span class="comment">// function pointer to single character output routine</span>00079 <span class="keyword">static</span> void (*cmdlineOutputFunc)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c);00080 <a name="l00081"></a><a class="code" href="group__cmdline.html#ga1">00081</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga1">cmdlineInit</a>(<span class="keywordtype">void</span>)00082 {00083 <span class="comment">// reset vt100 processing state</span>00084 CmdlineInputVT100State = 0;00085 <span class="comment">// initialize input buffer</span>00086 CmdlineBufferLength = 0;00087 CmdlineBufferEditPos = 0;00088 <span class="comment">// initialize executing function</span>00089 CmdlineExecFunction = 0;00090 <span class="comment">// initialize command list</span>00091 CmdlineNumCommands = 0;00092 }00093 <a name="l00094"></a><a class="code" href="group__cmdline.html#ga2">00094</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga2">cmdlineAddCommand</a>(u08* newCmdString, CmdlineFuncPtrType newCmdFuncPtr)00095 {00096 <span class="comment">// add command string to end of command list</span>00097 strcpy(CmdlineCommandList[CmdlineNumCommands], newCmdString);00098 <span class="comment">// add command function ptr to end of function list</span>00099 CmdlineFunctionList[CmdlineNumCommands] = newCmdFuncPtr;00100 <span class="comment">// increment number of registered commands</span>00101 CmdlineNumCommands++;00102 }00103 <a name="l00104"></a><a class="code" href="group__cmdline.html#ga3">00104</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga3">cmdlineSetOutputFunc</a>(<span class="keywordtype">void</span> (*output_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))00105 {00106 <span class="comment">// set new output function</span>00107 cmdlineOutputFunc = output_func;00108 00109 <span class="comment">// should we really do this?</span>00110 <span class="comment">// print a prompt </span>00111 <span class="comment">//cmdlinePrintPrompt();</span>00112 }00113 <a name="l00114"></a><a class="code" href="group__cmdline.html#ga4">00114</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga4">cmdlineInputFunc</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c)00115 {00116 u08 i;00117 <span class="comment">// process the received character</span>00118 00119 <span class="comment">// VT100 handling</span>00120 <span class="comment">// are we processing a VT100 command?</span>00121 <span class="keywordflow">if</span>(CmdlineInputVT100State == 2)00122 {00123 <span class="comment">// we have already received ESC and [</span>00124 <span class="comment">// now process the vt100 code</span>00125 <span class="keywordflow">switch</span>(c)00126 {00127 <span class="keywordflow">case</span> VT100_ARROWUP:00128 cmdlineDoHistory(CMDLINE_HISTORY_PREV);00129 <span class="keywordflow">break</span>;00130 <span class="keywordflow">case</span> VT100_ARROWDOWN:00131 cmdlineDoHistory(CMDLINE_HISTORY_NEXT);00132 <span class="keywordflow">break</span>;00133 <span class="keywordflow">case</span> VT100_ARROWRIGHT:00134 <span class="comment">// if the edit position less than current string length</span>00135 <span class="keywordflow">if</span>(CmdlineBufferEditPos < CmdlineBufferLength)00136 {00137 <span class="comment">// increment the edit position</span>00138 CmdlineBufferEditPos++;00139 <span class="comment">// move cursor forward one space (no erase)</span>00140 cmdlineOutputFunc(ASCII_ESC);00141 cmdlineOutputFunc(<span class="charliteral">'['</span>);00142 cmdlineOutputFunc(VT100_ARROWRIGHT);00143 }00144 <span class="keywordflow">else</span>00145 {00146 <span class="comment">// else, ring the bell</span>00147 cmdlineOutputFunc(ASCII_BEL);00148 }00149 <span class="keywordflow">break</span>;00150 <span class="keywordflow">case</span> VT100_ARROWLEFT:00151 <span class="comment">// if the edit position is non-zero</span>00152 <span class="keywordflow">if</span>(CmdlineBufferEditPos)00153 {00154 <span class="comment">// decrement the edit position</span>00155 CmdlineBufferEditPos--;00156 <span class="comment">// move cursor back one space (no erase)</span>00157 cmdlineOutputFunc(ASCII_BS);00158 }00159 <span class="keywordflow">else</span>00160 {00161 <span class="comment">// else, ring the bell</span>00162 cmdlineOutputFunc(ASCII_BEL);00163 }00164 <span class="keywordflow">break</span>;00165 <span class="keywordflow">default</span>:00166 <span class="keywordflow">break</span>;00167 }00168 <span class="comment">// done, reset state</span>00169 CmdlineInputVT100State = 0;00170 <span class="keywordflow">return</span>;00171 }00172 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(CmdlineInputVT100State == 1)00173 {00174 <span class="comment">// we last received [ESC]</span>00175 <span class="keywordflow">if</span>(c == <span class="charliteral">'['</span>)00176 {00177 CmdlineInputVT100State = 2;00178 <span class="keywordflow">return</span>;00179 }00180 <span class="keywordflow">else</span>00181 CmdlineInputVT100State = 0;00182 }00183 <span class="keywordflow">else</span>00184 {00185 <span class="comment">// anything else, reset state</span>00186 CmdlineInputVT100State = 0;00187 }00188 00189 <span class="comment">// Regular handling</span>00190 <span class="keywordflow">if</span>( (c >= 0x20) && (c < 0x7F) )00191 {00192 <span class="comment">// character is printable</span>00193 <span class="comment">// is this a simple append</span>00194 <span class="keywordflow">if</span>(CmdlineBufferEditPos == CmdlineBufferLength)00195 {00196 <span class="comment">// echo character to the output</span>00197 cmdlineOutputFunc(c);00198 <span class="comment">// add it to the command line buffer</span>00199 CmdlineBuffer[CmdlineBufferEditPos++] = c;00200 <span class="comment">// update buffer length</span>00201 CmdlineBufferLength++;00202 }00203 <span class="keywordflow">else</span>00204 {00205 <span class="comment">// edit/cursor position != end of buffer</span>00206 <span class="comment">// we're inserting characters at a mid-line edit position</span>00207 <span class="comment">// make room at the insert point</span>00208 CmdlineBufferLength++;00209 <span class="keywordflow">for</span>(i=CmdlineBufferLength; i>CmdlineBufferEditPos; i--)00210 CmdlineBuffer[i] = CmdlineBuffer[i-1];00211 <span class="comment">// insert character</span>00212 CmdlineBuffer[CmdlineBufferEditPos++] = c;00213 <span class="comment">// repaint</span>00214 cmdlineRepaint();00215 <span class="comment">// reposition cursor</span>00216 <span class="keywordflow">for</span>(i=CmdlineBufferEditPos; i<CmdlineBufferLength; i++)00217 cmdlineOutputFunc(ASCII_BS);00218 }00219 }00220 <span class="comment">// handle special characters</span>00221 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == ASCII_CR)00222 {00223 <span class="comment">// user pressed [ENTER]</span>00224 <span class="comment">// echo CR and LF to terminal</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -