📄 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/interrupt.h></span> <span class="comment">// include interrupt support</span>00025 <span class="preprocessor">#include <avr/pgmspace.h></span> <span class="comment">// include AVR program memory support</span>00026 <span class="preprocessor">#include <string.h></span> <span class="comment">// include standard C string functions</span>00027 <span class="preprocessor">#include <stdlib.h></span> <span class="comment">// include stdlib for string conversion functions</span>00028 00029 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> <span class="comment">// include our global settings</span>00030 <span class="preprocessor">#include "<a class="code" href="cmdline_8h.html">cmdline.h</a>"</span>00031 00032 <span class="comment">// include project-specific configuration</span>00033 <span class="preprocessor">#include "<a class="code" href="cmdlineconf_8h.html">cmdlineconf.h</a>"</span>00034 00035 <span class="comment">// defines</span>00036 <span class="preprocessor">#define ASCII_BEL 0x07</span>00037 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_BS 0x08</span>00038 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_CR 0x0D</span>00039 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_LF 0x0A</span>00040 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_ESC 0x1B</span>00041 <span class="preprocessor"></span><span class="preprocessor">#define ASCII_DEL 0x7F</span>00042 <span class="preprocessor"></span>00043 <span class="preprocessor">#define VT100_ARROWUP 'A'</span>00044 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWDOWN 'B'</span>00045 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWRIGHT 'C'</span>00046 <span class="preprocessor"></span><span class="preprocessor">#define VT100_ARROWLEFT 'D'</span>00047 <span class="preprocessor"></span>00048 <span class="preprocessor">#define CMDLINE_HISTORY_SAVE 0</span>00049 <span class="preprocessor"></span><span class="preprocessor">#define CMDLINE_HISTORY_PREV 1</span>00050 <span class="preprocessor"></span><span class="preprocessor">#define CMDLINE_HISTORY_NEXT 2</span>00051 <span class="preprocessor"></span>00052 00053 <span class="comment">// Global variables</span>00054 00055 <span class="comment">// strings</span>00056 u08 PROGMEM CmdlinePrompt[] = <span class="stringliteral">"cmd>"</span>;00057 u08 PROGMEM CmdlineNotice[] = <span class="stringliteral">"cmdline: "</span>;00058 u08 PROGMEM CmdlineCmdNotFound[] = <span class="stringliteral">"command not found"</span>;00059 00060 <span class="comment">// command list</span>00061 <span class="comment">// -commands are null-terminated strings</span>00062 <span class="keyword">static</span> <span class="keywordtype">char</span> CmdlineCommandList[CMDLINE_MAX_COMMANDS][CMDLINE_MAX_CMD_LENGTH];00063 <span class="comment">// command function pointer list</span>00064 <span class="keyword">static</span> CmdlineFuncPtrType CmdlineFunctionList[CMDLINE_MAX_COMMANDS];00065 <span class="comment">// number of commands currently registered</span>00066 u08 CmdlineNumCommands;00067 00068 u08 CmdlineBuffer[CMDLINE_BUFFERSIZE];00069 u08 CmdlineBufferLength;00070 u08 CmdlineBufferEditPos;00071 u08 CmdlineInputVT100State;00072 u08 CmdlineHistory[CMDLINE_HISTORYSIZE][CMDLINE_BUFFERSIZE];00073 CmdlineFuncPtrType CmdlineExecFunction;00074 00075 <span class="comment">// Functions</span>00076 00077 <span class="comment">// function pointer to single character output routine</span>00078 <span class="keyword">static</span> void (*cmdlineOutputFunc)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c);00079 <a name="l00080"></a><a class="code" href="group__cmdline.html#ga1">00080</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga1">cmdlineInit</a>(<span class="keywordtype">void</span>)00081 {00082 <span class="comment">// reset vt100 processing state</span>00083 CmdlineInputVT100State = 0;00084 <span class="comment">// initialize input buffer</span>00085 CmdlineBufferLength = 0;00086 CmdlineBufferEditPos = 0;00087 <span class="comment">// initialize executing function</span>00088 CmdlineExecFunction = 0;00089 <span class="comment">// initialize command list</span>00090 CmdlineNumCommands = 0;00091 }00092 <a name="l00093"></a><a class="code" href="group__cmdline.html#ga2">00093</a> <span class="keywordtype">void</span> <a class="code" href="group__cmdline.html#ga2">cmdlineAddCommand</a>(u08* newCmdString, CmdlineFuncPtrType newCmdFuncPtr)00094 {00095 <span class="comment">// add command string to end of command list</span>00096 strcpy(CmdlineCommandList[CmdlineNumCommands], newCmdString);00097 <span class="comment">// add command function ptr to end of function list</span>00098 CmdlineFunctionList[CmdlineNumCommands] = newCmdFuncPtr;00099 <span class="comment">// increment number of registered commands</span>00100 CmdlineNumCommands++;00101 }00102 <a name="l00103"></a><a class="code" href="group__cmdline.html#ga3">00103</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))00104 {00105 <span class="comment">// set new output function</span>00106 cmdlineOutputFunc = output_func;00107 00108 <span class="comment">// should we really do this?</span>00109 <span class="comment">// print a prompt </span>00110 <span class="comment">//cmdlinePrintPrompt();</span>00111 }00112 <a name="l00113"></a><a class="code" href="group__cmdline.html#ga4">00113</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)00114 {00115 u08 i;00116 <span class="comment">// process the received character</span>00117 00118 <span class="comment">// VT100 handling</span>00119 <span class="comment">// are we processing a VT100 command?</span>00120 <span class="keywordflow">if</span>(CmdlineInputVT100State == 2)00121 {00122 <span class="comment">// we have already received ESC and [</span>00123 <span class="comment">// now process the vt100 code</span>00124 <span class="keywordflow">switch</span>(c)00125 {00126 <span class="keywordflow">case</span> VT100_ARROWUP:00127 cmdlineDoHistory(CMDLINE_HISTORY_PREV);00128 <span class="keywordflow">break</span>;00129 <span class="keywordflow">case</span> VT100_ARROWDOWN:00130 cmdlineDoHistory(CMDLINE_HISTORY_NEXT);00131 <span class="keywordflow">break</span>;00132 <span class="keywordflow">case</span> VT100_ARROWRIGHT:00133 <span class="comment">// if the edit position less than current string length</span>00134 <span class="keywordflow">if</span>(CmdlineBufferEditPos < CmdlineBufferLength)00135 {00136 <span class="comment">// increment the edit position</span>00137 CmdlineBufferEditPos++;00138 <span class="comment">// move cursor forward one space (no erase)</span>00139 cmdlineOutputFunc(ASCII_ESC);00140 cmdlineOutputFunc(<span class="charliteral">'['</span>);00141 cmdlineOutputFunc(VT100_ARROWRIGHT);00142 }00143 <span class="keywordflow">else</span>00144 {00145 <span class="comment">// else, ring the bell</span>00146 cmdlineOutputFunc(ASCII_BEL);00147 }00148 <span class="keywordflow">break</span>;00149 <span class="keywordflow">case</span> VT100_ARROWLEFT:00150 <span class="comment">// if the edit position is non-zero</span>00151 <span class="keywordflow">if</span>(CmdlineBufferEditPos)00152 {00153 <span class="comment">// decrement the edit position</span>00154 CmdlineBufferEditPos--;00155 <span class="comment">// move cursor back one space (no erase)</span>00156 cmdlineOutputFunc(ASCII_BS);00157 }00158 <span class="keywordflow">else</span>00159 {00160 <span class="comment">// else, ring the bell</span>00161 cmdlineOutputFunc(ASCII_BEL);00162 }00163 <span class="keywordflow">break</span>;00164 <span class="keywordflow">default</span>:00165 <span class="keywordflow">break</span>;00166 }00167 <span class="comment">// done, reset state</span>00168 CmdlineInputVT100State = 0;00169 <span class="keywordflow">return</span>;00170 }00171 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(CmdlineInputVT100State == 1)00172 {00173 <span class="comment">// we last received [ESC]</span>00174 <span class="keywordflow">if</span>(c == <span class="charliteral">'['</span>)00175 {00176 CmdlineInputVT100State = 2;00177 <span class="keywordflow">return</span>;00178 }00179 <span class="keywordflow">else</span>00180 CmdlineInputVT100State = 0;00181 }00182 <span class="keywordflow">else</span>00183 {00184 <span class="comment">// anything else, reset state</span>00185 CmdlineInputVT100State = 0;00186 }00187 00188 <span class="comment">// Regular handling</span>00189 <span class="keywordflow">if</span>( (c >= 0x20) && (c < 0x7F) )00190 {00191 <span class="comment">// character is printable</span>00192 <span class="comment">// is this a simple append</span>00193 <span class="keywordflow">if</span>(CmdlineBufferEditPos == CmdlineBufferLength)00194 {00195 <span class="comment">// echo character to the output</span>00196 cmdlineOutputFunc(c);00197 <span class="comment">// add it to the command line buffer</span>00198 CmdlineBuffer[CmdlineBufferEditPos++] = c;00199 <span class="comment">// update buffer length</span>00200 CmdlineBufferLength++;00201 }00202 <span class="keywordflow">else</span>00203 {00204 <span class="comment">// edit/cursor position != end of buffer</span>00205 <span class="comment">// we're inserting characters at a mid-line edit position</span>00206 <span class="comment">// make room at the insert point</span>00207 CmdlineBufferLength++;00208 <span class="keywordflow">for</span>(i=CmdlineBufferLength; i>CmdlineBufferEditPos; i--)00209 CmdlineBuffer[i] = CmdlineBuffer[i-1];00210 <span class="comment">// insert character</span>00211 CmdlineBuffer[CmdlineBufferEditPos++] = c;00212 <span class="comment">// repaint</span>00213 cmdlineRepaint();00214 <span class="comment">// reposition cursor</span>00215 <span class="keywordflow">for</span>(i=CmdlineBufferEditPos; i<CmdlineBufferLength; i++)00216 cmdlineOutputFunc(ASCII_BS);00217 }00218 }00219 <span class="comment">// handle special characters</span>00220 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == ASCII_CR)00221 {00222 <span class="comment">// user pressed [ENTER]</span>00223 <span class="comment">// echo CR and LF to terminal</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -