1 /*
2 * hostConsole.c
3 *
4 * This module contains the API for console support.
5 *
6 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
7 *
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the
19 * distribution.
20 *
21 * Neither the name of Texas Instruments Incorporated nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
39 /*********************************************************************
40 * INCLUDES
41 */
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <conio.h>
45 #include <unistd.h>
46 #include <string.h>
48 #include "hostConsole.h"
49 #include "dbgPrint.h"
52 /*********************************************************************
53 * MACROS
54 */
56 /*********************************************************************
57 * LOCAL VARIABLE
58 */
60 /*********************************************************************
61 * LOCAL FUNCTIONS
62 */
64 /*********************************************************************
65 * API FUNCTIONS
66 */
68 /**************************************************************************************************
69 * @fn consoleGetCh
70 *
71 * @brief This function gets a char from the console
72 *
73 * input parameters
74 *
75 * @param
76 *
77 * output parameters
78 *
79 * None.
80 *
81 * @return None.
82 **************************************************************************************************
83 */
84 char consoleGetCh() {\r
85 unsigned char ch = _getch();\r
86 static int isDir = 0;\r
87 switch (ch)\r
88 {\r
89 case 8:\r
90 ch = 127;\r
91 break;\r
92 case 13:\r
93 ch = '\n';\r
94 break;\r
95 case 224://is direction key\r
96 isDir = 1;\r
97 ch = 27;\r
98 break;\r
99 case 72://up\r
100 if(isDir == 1)\r
101 {\r
102 ch = 'A';\r
103 isDir = 0;\r
104 }\r
105 break;\r
106 case 80://down\r
107 if(isDir == 1)\r
108 {\r
109 ch = 'B';\r
110 isDir = 0;\r
111 }\r
112 break;\r
113 case 75://left\r
114 if(isDir == 1)\r
115 {\r
116 ch = 'D';\r
117 isDir = 0;\r
118 }\r
119 break;\r
120 case 77://right\r
121 if(isDir == 1)\r
122 {\r
123 ch = 'C';\r
124 isDir = 0;\r
125 }\r
126 \r
127 break;\r
128 }
129 return ch;
130 }
132 int idxpHist = 0;
133 int pHistIsRollover = 0;\r
135 int pHistMax = 256;
136 char pHist[256][256];
137 /**************************************************************************************************
138 * @fn consoleGetLine
139 *
140 * @brief This function gets a line from the console.
141 *
142 * input parameters
143 *
144 * @param
145 *
146 * output parameters
147 *
148 * None.
149 *
150 * @return None.
151 **************************************************************************************************
152 */
153 int consoleGetLine(char *ln, int maxLen)
154 {
155 int chIdx, isDir, cPos, currpHist;
156 char ch;
157 char tempStr[256];
158 char temppHist[256];
159 chIdx = isDir = cPos = currpHist = 0;
160 ln[0] = '\0';\r
162 while (chIdx < maxLen)
163 {
164 ch = consoleGetCh();\r
165 //consoleClearLn();\r
167 if ((ch == '\n') || (chIdx == (maxLen - 1)))
168 {
170 if ((idxpHist == 0 && pHistIsRollover == 0)
171 || strcmp(pHist[(pHistMax + idxpHist - 1) % pHistMax], ln)
172 != 0)
173 {
174 strcpy(pHist[idxpHist], ln);
175 idxpHist = ((idxpHist + 1) % pHistMax);
176 }
177 if (idxpHist == 0)
178 {
179 pHistIsRollover = 1;
180 }
181 //ln[chIdx++] = '\n';\r
182 //ln[chIdx] = '\0';\r
183 printf("\n");
184 break;
185 }
186 switch (ch)
187 {
188 case 127:
189 //backspace was press
190 //clear line
191 if (chIdx > 0)
192 {
193 if (cPos < chIdx)
194 {
195 int x;
196 for (x = 0; x < cPos; x++)
197 {
198 ln[chIdx - cPos + x - 1] = tempStr[cPos - x - 1];
199 }
200 ln[--chIdx] = '\0';
201 }
202 //consoleClearLn();
203 //printf("\r%s", ln);\r
204 printf("\b \b");
206 } else
207 {
208 consoleClearLn();
209 printf("\r");
210 }
211 break;
212 case 27:
213 isDir = 1;
214 if (chIdx > 0)
215 {
216 consoleClearLn();
217 printf("\r%s", ln);
219 } else
220 {
221 consoleClearLn();
222 printf("\r");
223 }
225 break;
226 default:
227 if (isDir)
228 {
229 if (ch == '[')
230 {
231 isDir = 1;
232 } else if (ch == 'A')
233 {
234 if (currpHist < idxpHist)
235 {
236 cPos = 0;
237 if (currpHist)
238 {
239 strcpy(pHist[idxpHist - currpHist], ln);
240 }
241 if (currpHist == 0)
242 strcpy(temppHist, ln);
243 currpHist++;
244 strcpy(ln, pHist[idxpHist - currpHist]);
245 chIdx = strlen(ln);
247 }
249 isDir = 0;
250 } else if (ch == 'B')
251 {
252 if (currpHist)
253 {
254 strcpy(pHist[idxpHist - currpHist], ln);
255 currpHist--;
256 if (currpHist)
257 {
258 strcpy(ln, pHist[idxpHist - currpHist]);
259 } else
260 {
261 strcpy(ln, temppHist);
262 }
263 chIdx = strlen(ln);
265 }
266 isDir = 0;
267 } else if (ch == 'C')
268 {
269 if (cPos > 0)
270 {
271 cPos--;
272 }
274 isDir = 0;
275 } else if (ch == 'D')
276 {
277 if (cPos < chIdx)
278 {
279 tempStr[cPos] = ln[chIdx - cPos - 1];
280 cPos++;
281 }
282 isDir = 0;
283 } else
284 {
285 isDir = 0;
286 }
287 //consoleClearLn();
288 //printf("\r%s", ln);
289 } else
290 {
291 if (cPos != 0)
292 {
293 ln[chIdx - cPos] = ch;
294 int x;
295 for (x = 1; x <= cPos; x++)
296 {
297 ln[chIdx - cPos + x] = tempStr[cPos - x];
298 }
299 ln[++chIdx] = '\0';
300 } else
301 {
302 ln[chIdx++] = ch;
303 ln[chIdx] = '\0';
304 }
306 }
307 consoleClearLn();
308 printf("\r%s", ln);
309 break;
310 }
311 int x;
312 for (x = 1; x <= cPos; x++)
313 {
314 consolePrint("\b");
315 }
316 }
318 return chIdx;
319 }