shellmatta.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file shellmatta.c
  10. * @brief Main implementation of the Shellmatta terminal implementation
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta
  15. * @{
  16. * @addtogroup shellmatta_private
  17. * @{
  18. */
  19. #include "shellmatta.h"
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. /* defines */
  25. #ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE
  26. #define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u
  27. #endif
  28. #define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a))
  29. #define SHELLMATTA_MAX(a,b) (((a) < (b)) ? (b) : (a))
  30. #define SHELLMATTA_PRINT_BUFFER(buffer,cnt,fct) \
  31. while((cnt) > sizeof((buffer))) \
  32. { \
  33. (cnt) -= sizeof((buffer)); \
  34. (fct)((buffer), sizeof((buffer))); \
  35. } \
  36. if((cnt) != 0u) \
  37. { \
  38. (fct)((buffer), (cnt)); \
  39. }
  40. #define SHELLMATTA_MAGIC 0x5101E110u
  41. /* static functions */
  42. /**
  43. * @brief function to write an echo to the output depending on
  44. * the echo enabled state of the instance
  45. * @param[in] inst pointer to a shellmatta instance
  46. * @param[in] data pointer to the data so send
  47. * @param[in] length length of the data to send (in byte)
  48. */
  49. static void writeEcho( shellmatta_instance_t *inst,
  50. const char *data,
  51. uint32_t length)
  52. {
  53. if(true == inst->echoEnabled)
  54. {
  55. inst->write(data, length);
  56. }
  57. }
  58. /**
  59. * @brief appends a byte to the history ring stack buffer
  60. * @param[in] inst pointer to a shellmatta instance
  61. * @param[in] byte byte to append to the history buffer
  62. */
  63. static void appendHistoryByte(shellmatta_instance_t *inst, char byte)
  64. {
  65. /** -# calculate the new history buffer index */
  66. inst->historyEnd ++;
  67. if(inst->historyEnd >= inst->historyBufferSize)
  68. {
  69. inst->historyEnd = 0u;
  70. }
  71. /** -# append the byte */
  72. inst->historyBuffer[inst->historyEnd] = byte;
  73. /** -# check if the we overwrite an existing stored command */
  74. if(inst->historyEnd == inst->historyStart)
  75. {
  76. /** -# move the start pointer to the next termination (0) */
  77. do
  78. {
  79. inst->historyStart ++;
  80. if(inst->historyStart >= inst->historyBufferSize)
  81. {
  82. inst->historyStart = 0u;
  83. }
  84. }while(0u != inst->historyBuffer[inst->historyStart]);
  85. }
  86. }
  87. /**
  88. * @brief reads a byte from the history buffer and decreases the read index
  89. * @param[in] inst pointer to a shellmatta instance
  90. * @param[out] byte pointer to a char where the read out byte will be stored
  91. * @return
  92. */
  93. static bool getHistoryByte(shellmatta_instance_t *inst, char *byte)
  94. {
  95. bool ret = false;
  96. /** -# check if we have reached the end of the buffer already */
  97. if(inst->historyRead != inst->historyStart)
  98. {
  99. /** -# read out one byte and decrease the read index */
  100. *byte = inst->historyBuffer[inst->historyRead];
  101. if(0u == inst->historyRead)
  102. {
  103. inst->historyRead = inst->historyBufferSize;
  104. }
  105. inst->historyRead --;
  106. ret = true;
  107. }
  108. return ret;
  109. }
  110. /**
  111. * @brief stores the current command from the instances buffer into the
  112. * history buffer
  113. * @param[in] inst pointer to a shellmatta instance
  114. */
  115. static void storeHistoryCmd(shellmatta_instance_t *inst)
  116. {
  117. uint32_t i;
  118. /** -# check if we have enough room for the command in the history buffer
  119. * and there is a new command to be stored */
  120. if( (inst->historyBufferSize > inst->inputCount)
  121. && (0u != inst->inputCount)
  122. && (true == inst->dirty))
  123. {
  124. /** -# append the command termination */
  125. appendHistoryByte(inst, 0u);
  126. /** -# append the command byte wise in reverse direction */
  127. for(i = inst->inputCount; i > 0u; i --)
  128. {
  129. appendHistoryByte(inst, inst->buffer[i - 1u]);
  130. }
  131. }
  132. /** -# remove the dirty flag - everything is nice and saved */
  133. inst->dirty = false;
  134. }
  135. /**
  136. * @brief navigates in the history buffer by the given number of commands
  137. * @param[in] inst pointer to a shellmatta instance
  138. * @param[in] cnt direction and count to navigate
  139. * @return
  140. */
  141. static bool navigateHistoryCmd(shellmatta_instance_t *inst, int32_t cnt)
  142. {
  143. bool ret = true;
  144. uint32_t tempReadIdx = 0u;
  145. while((cnt > 0) && (true == ret))
  146. {
  147. if(inst->historyRead != inst->historyEnd)
  148. {
  149. inst->historyRead ++;
  150. }
  151. while(inst->historyRead != inst->historyEnd)
  152. {
  153. inst->historyRead ++;
  154. if(inst->historyRead >= inst->historyBufferSize)
  155. {
  156. inst->historyRead = 0u;
  157. }
  158. if( (inst->historyRead != inst->historyEnd)
  159. && (0u == inst->historyBuffer[inst->historyRead]))
  160. {
  161. if(0u == inst->historyRead)
  162. {
  163. inst->historyRead = inst->historyBufferSize;
  164. }
  165. inst->historyRead --;
  166. cnt -= 1;
  167. break;
  168. }
  169. }
  170. if(inst->historyRead == inst->historyEnd)
  171. {
  172. ret = false;
  173. }
  174. }
  175. while((cnt < 0) && (true == ret))
  176. {
  177. tempReadIdx = inst->historyRead;
  178. while(inst->historyRead != inst->historyStart)
  179. {
  180. if(0u == inst->historyRead)
  181. {
  182. inst->historyRead = inst->historyBufferSize;
  183. }
  184. inst->historyRead --;
  185. if( (inst->historyRead != inst->historyStart)
  186. && (0u == inst->historyBuffer[inst->historyRead]))
  187. {
  188. if(0u == inst->historyRead)
  189. {
  190. inst->historyRead = inst->historyBufferSize;
  191. }
  192. inst->historyRead --;
  193. cnt += 1;
  194. break;
  195. }
  196. }
  197. if(inst->historyRead == inst->historyStart)
  198. {
  199. inst->historyRead = tempReadIdx;
  200. inst->historyReadUp = false;
  201. ret = false;
  202. }
  203. }
  204. return ret;
  205. }
  206. /**
  207. * @brief restores the command from the history buffer where the read
  208. * index points on
  209. * @param[in] inst pointer to a shellmatta instance
  210. */
  211. static void restoreHistoryCmd(shellmatta_instance_t *inst)
  212. {
  213. char byte;
  214. bool ret = true;
  215. ret = getHistoryByte(inst, &byte);
  216. while((ret == true) && (byte != 0u))
  217. {
  218. inst->buffer[inst->inputCount] = byte;
  219. inst->inputCount ++;
  220. inst->cursor ++;
  221. ret = getHistoryByte(inst, &byte);
  222. }
  223. writeEcho(inst, inst->buffer, inst->inputCount);
  224. navigateHistoryCmd(inst, 1);
  225. inst->dirty = false;
  226. }
  227. /**
  228. * @brief resets the history buffer pointers to show to the most recent
  229. * command again
  230. * @param[in] inst pointer to a shellmatta instance
  231. */
  232. static void resetHistoryBuffer(shellmatta_instance_t *inst)
  233. {
  234. inst->historyRead = inst->historyEnd;
  235. inst->historyReadUp = true;
  236. }
  237. /**
  238. * @brief tells the terminal to save the current cursor position
  239. * @param[in] inst pointer to shellmatta instance
  240. */
  241. static void saveCursorPos(shellmatta_instance_t *inst)
  242. {
  243. writeEcho(inst, "\e[s", 3u);
  244. }
  245. /**
  246. * @brief tells the terminal to restore the saved cursor position
  247. * @param[in] inst pointer to shellmatta instance
  248. */
  249. static void restoreCursorPos(shellmatta_instance_t *inst)
  250. {
  251. writeEcho(inst, "\e[u", 3u);
  252. }
  253. /**
  254. * @brief tells the terminal to erase the line on the right side of the
  255. * cursor
  256. * @param[in] inst pointer to shellmatta instance
  257. */
  258. static void eraseLine(shellmatta_instance_t *inst)
  259. {
  260. writeEcho(inst, "\e[K", 3u);
  261. }
  262. /**
  263. * @brief moves the cursor back by the given amoung of characters
  264. * @param[in] inst pointer to shellmatta instance
  265. * @param[in] length number of characters to rewind
  266. */
  267. static void rewindCursor(shellmatta_instance_t *inst, uint32_t length)
  268. {
  269. char terminalCmd[16];
  270. size_t size;
  271. length = SHELLMATTA_MIN (length, inst->cursor);
  272. if(length > 0u)
  273. {
  274. size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uD", length);
  275. writeEcho(inst, terminalCmd, size);
  276. inst->cursor -= length;
  277. }
  278. }
  279. /**
  280. * @brief moves the cursor forward by the given amoung of characters
  281. * @param[in] inst pointer to shellmatta instance
  282. * @param[in] length number of characters to move forward
  283. */
  284. static void forwardCursor(shellmatta_instance_t *inst, uint32_t length)
  285. {
  286. char terminalCmd[16];
  287. size_t size;
  288. length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor));
  289. if (length > 0u)
  290. {
  291. size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uC", length);
  292. writeEcho(inst, terminalCmd, size);
  293. inst->cursor += length;
  294. }
  295. }
  296. /**
  297. * @brief inserts the given amount of characters at the cursor position
  298. * @param[in] inst pointer to shellmatta instance
  299. * @param[in] data pointer to the data to be inserted
  300. * @param[in] length length of the data to be inserted
  301. */
  302. static void insertChars(shellmatta_instance_t *inst,
  303. char *data,
  304. uint32_t length)
  305. {
  306. if(0u != length)
  307. {
  308. /** -# check if we have to move chars in the buffer */
  309. if( (inst->cursor != inst->inputCount)
  310. && (SHELLMATTA_MODE_INSERT == inst->mode))
  311. {
  312. /** -# move the existing chars */
  313. for ( uint32_t i = inst->inputCount;
  314. i > inst->cursor;
  315. i --)
  316. {
  317. inst->buffer[i + length - 1] = inst->buffer[i - 1];
  318. }
  319. /** -# store and print the new chars */
  320. memcpy(&(inst->buffer[inst->cursor]), data, length);
  321. writeEcho(inst, data, length);
  322. /** -# print the other chars and restore the cursor to this position */
  323. eraseLine(inst);
  324. saveCursorPos(inst);
  325. writeEcho( inst,
  326. &(inst->buffer[inst->cursor + length]),
  327. inst->inputCount - inst->cursor);
  328. restoreCursorPos(inst);
  329. }
  330. /** -# just overwrite/append the chars */
  331. else
  332. {
  333. memcpy(&(inst->buffer[inst->cursor]), data, length);
  334. writeEcho(inst, data, length);
  335. }
  336. inst->inputCount += length;
  337. inst->cursor += length;
  338. }
  339. }
  340. /**
  341. * @brief removes the given amount of characters from the current cursor
  342. * position
  343. * @param[in] inst pointer to a shellmatta instance
  344. * @param[in] length number of characters to remove
  345. * @param[in] backspace remove characters left of the cursor
  346. */
  347. static void removeChars(shellmatta_instance_t *inst,
  348. uint32_t length,
  349. bool backspace)
  350. {
  351. if(0u != length)
  352. {
  353. /** -# rewind the cursor in case of backspace */
  354. if(true == backspace)
  355. {
  356. length = SHELLMATTA_MIN (length, inst->cursor);
  357. rewindCursor(inst, length);
  358. }
  359. else
  360. {
  361. length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
  362. }
  363. /** -# delete the char at the cursor position */
  364. for ( uint32_t i = inst->cursor;
  365. i < (inst->inputCount - length);
  366. i++)
  367. {
  368. inst->buffer[i] = inst->buffer[i + length];
  369. }
  370. /** -# print the rest of the line again */
  371. eraseLine(inst);
  372. saveCursorPos(inst);
  373. writeEcho( inst,
  374. &(inst->buffer[inst->cursor]),
  375. (inst->inputCount - inst->cursor - length));
  376. restoreCursorPos(inst);
  377. inst->inputCount -= length;
  378. }
  379. }
  380. /**
  381. * @brief clears the input buffer and removes the currend command from
  382. * the terminal output
  383. * @param[in] inst pointer to a shellmatta instance
  384. */
  385. static void clearInput(shellmatta_instance_t *inst)
  386. {
  387. rewindCursor(inst, inst->cursor);
  388. eraseLine(inst);
  389. inst->inputCount = 0u;
  390. inst->dirty = false;
  391. }
  392. /**
  393. * @brief processes the excape character stream of the instance and chacks
  394. * if an arrow key was pressed
  395. * @param[in] inst pointer to a shellmatta instance
  396. * @return #SHELLMATTA_OK if an arrow key was pressed
  397. */
  398. static shellmatta_retCode_t processArrowKeys(shellmatta_instance_t *inst)
  399. {
  400. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  401. if ('[' == inst->escapeChars[0])
  402. {
  403. ret = SHELLMATTA_OK;
  404. switch (inst->escapeChars[1])
  405. {
  406. case 'A': /* arrow up */
  407. storeHistoryCmd(inst);
  408. if(false == inst->historyReadUp)
  409. {
  410. navigateHistoryCmd(inst, -1);
  411. }
  412. inst->historyReadUp = true;
  413. clearInput(inst);
  414. restoreHistoryCmd(inst);
  415. navigateHistoryCmd(inst, -1);
  416. break;
  417. case 'B': /* arrow down */
  418. if((inst->historyRead != inst->historyEnd))
  419. {
  420. storeHistoryCmd(inst);
  421. if(true == inst->historyReadUp)
  422. {
  423. navigateHistoryCmd(inst, 1);
  424. }
  425. inst->historyReadUp = false;
  426. navigateHistoryCmd(inst, 1);
  427. clearInput(inst);
  428. restoreHistoryCmd(inst);
  429. }
  430. break;
  431. case 'C': /* arrow right */
  432. forwardCursor(inst, 1u);
  433. break;
  434. case 'D': /* arrow left */
  435. rewindCursor(inst, 1u);
  436. break;
  437. default:
  438. /** ignore unknown escape */
  439. ret = SHELLMATTA_USE_FAULT;
  440. break;
  441. }
  442. }
  443. return ret;
  444. }
  445. /**
  446. * @brief handles a ANSI escape sequence to be able to react to some
  447. * special keys
  448. * @param[in] inst pointer to a shellmatta instance
  449. * @param[in] data new received character of the escape sequence
  450. */
  451. static void handleEscapeSequence(shellmatta_instance_t *inst, char data)
  452. {
  453. switch (inst->escapeCounter)
  454. {
  455. case 1u:
  456. inst->escapeChars[inst->escapeCounter - 1] = data;
  457. inst->escapeCounter ++;
  458. break;
  459. case 2u:
  460. inst->escapeChars[inst->escapeCounter - 1] = data;
  461. /** -# check if an arrow key was pressed */
  462. if(SHELLMATTA_OK == processArrowKeys(inst))
  463. {
  464. inst->escapeCounter = 0u;
  465. }
  466. /** -# check if end was pressed */
  467. else if ( (0x4f == inst->escapeChars[0])
  468. && (0x46 == inst->escapeChars[1]))
  469. {
  470. forwardCursor(inst, inst->inputCount - inst->cursor);
  471. inst->escapeCounter = 0u;
  472. }
  473. /** -# if the highest bit is not set this is usually not the end of the
  474. * sequence - so we go on */
  475. else if(0u == (0x40 & data))
  476. {
  477. inst->escapeCounter ++;
  478. }
  479. else
  480. {
  481. inst->escapeCounter = 0u;
  482. }
  483. break;
  484. case 3u:
  485. /** -# check if delete was pressed */
  486. inst->escapeChars[inst->escapeCounter - 1] = data;
  487. if( (0x5b == inst->escapeChars[0])
  488. && (0x33 == inst->escapeChars[1])
  489. && (0x7e == inst->escapeChars[2]))
  490. {
  491. removeChars(inst, 1u, false);
  492. inst->escapeCounter = 0u;
  493. }
  494. /** -# check if pos1 was pressed */
  495. else if ( (0x5bu == inst->escapeChars[0])
  496. && (0x31u == inst->escapeChars[1])
  497. && (0x7eu == inst->escapeChars[2]))
  498. {
  499. rewindCursor(inst, inst->cursor);
  500. inst->escapeCounter = 0u;
  501. }
  502. else if(0u == (0x40u & data))
  503. {
  504. inst->escapeCounter ++;
  505. }
  506. else
  507. {
  508. inst->escapeCounter = 0u;
  509. }
  510. break;
  511. default:
  512. inst->escapeCounter = 0u;
  513. break;
  514. }
  515. }
  516. /**
  517. * @brief terminates an input and prints the prompt again
  518. * @param[in] inst pointer to a shellmatta instance
  519. */
  520. static void terminateInput(shellmatta_instance_t *inst)
  521. {
  522. inst->inputCount = 0u;
  523. inst->cursor = 0u;
  524. inst->write("\r\n", 2u);
  525. inst->write(inst->prompt, strlen(inst->prompt));
  526. }
  527. /**
  528. * @brief searches the registered commands for matching ones and prints
  529. * the common part of all matching commands on the output
  530. * if called twice all matching commands are printed
  531. * @param[in] inst pointer to a shellmatta instance
  532. */
  533. static void doAutocomplete(shellmatta_instance_t *inst)
  534. {
  535. shellmatta_cmd_t *cmd = inst->cmdList;
  536. char *tempCmd = NULL;
  537. uint32_t minLen = 0u;
  538. uint32_t sizeDiff = 0u;
  539. bool exactMatch = true;
  540. uint32_t printedLen = 0u;
  541. uint32_t tempCursor = 0u;
  542. /** -# increase the tab counter to print all matching commands next time */
  543. inst->tabCounter++;
  544. /** -# on douple tab show all matching commands */
  545. if (1u < inst->tabCounter)
  546. {
  547. inst->tabCounter = 0u;
  548. /** -# loop through all registered commands */
  549. while (NULL != cmd)
  550. {
  551. /** -# check if command matches the input */
  552. if( (strlen(cmd->cmd) >= inst->cursor)
  553. && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
  554. {
  555. /** -# add newline on first find */
  556. if(0u == printedLen)
  557. {
  558. inst->write("\r\n", 2u);
  559. }
  560. inst->write(cmd->cmd, strlen(cmd->cmd));
  561. printedLen += strlen(cmd->cmd);
  562. inst->write(" ", 4u);
  563. printedLen += 4u;
  564. }
  565. /** -# check if command alias matches the input */
  566. if( (strlen(cmd->cmdAlias) >= inst->cursor)
  567. && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
  568. {
  569. /** -# add newline on first find */
  570. if(0u == printedLen)
  571. {
  572. inst->write("\r\n", 2u);
  573. }
  574. inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
  575. printedLen += strlen(cmd->cmdAlias);
  576. inst->write(" ", 4u);
  577. printedLen += 4u;
  578. }
  579. cmd = cmd->next;
  580. }
  581. /** -# print input again if a commands was found */
  582. if(printedLen > 0u)
  583. {
  584. writeEcho(inst, "\r\n", 2u);
  585. writeEcho(inst, inst->prompt, strlen(inst->prompt));
  586. writeEcho(inst, inst->buffer, inst->inputCount);
  587. tempCursor = inst->cursor;
  588. inst->cursor = inst->inputCount;
  589. rewindCursor(inst, inst->inputCount - tempCursor);
  590. }
  591. }
  592. /** -# on single tab autocomplete as far as possible */
  593. else if(0u != inst->cursor)
  594. {
  595. /** -# loop through all registered commands */
  596. while (NULL != cmd)
  597. {
  598. /** -# check if command matches the input */
  599. if( (strlen(cmd->cmd) >= inst->cursor)
  600. && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
  601. {
  602. /** -# store first match */
  603. if(NULL == tempCmd)
  604. {
  605. tempCmd = cmd->cmd;
  606. minLen = strlen(cmd->cmd);
  607. }
  608. /** -# find common part of the matching commands */
  609. else
  610. {
  611. exactMatch = false;
  612. minLen = SHELLMATTA_MIN(strlen(cmd->cmd), minLen);
  613. for(uint32_t i = 0u; i < minLen; i++)
  614. {
  615. if(cmd->cmd[i] != tempCmd[i])
  616. {
  617. minLen = i;
  618. }
  619. }
  620. }
  621. }
  622. /** -# check if command Alias matches the input */
  623. if( (strlen(cmd->cmdAlias) >= inst->cursor)
  624. && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
  625. {
  626. /** -# store first match */
  627. if(NULL == tempCmd)
  628. {
  629. tempCmd = cmd->cmdAlias;
  630. minLen = strlen(cmd->cmdAlias);
  631. }
  632. /** -# find common part of the matches */
  633. else
  634. {
  635. exactMatch = false;
  636. minLen = SHELLMATTA_MIN(strlen(cmd->cmdAlias), minLen);
  637. for(uint32_t i = 0u; i < minLen; i++)
  638. {
  639. if(cmd->cmdAlias[i] != tempCmd[i])
  640. {
  641. minLen = i;
  642. }
  643. }
  644. }
  645. }
  646. cmd = cmd->next;
  647. }
  648. /** -# autocomplete found command */
  649. if(NULL != tempCmd)
  650. {
  651. /** -# calculate the size of the command to be inserted */
  652. sizeDiff = minLen - inst->cursor;
  653. /** -# copy the found part into the buffer and display it */
  654. insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff);
  655. /** -# on exact match there is no need to double Tab to display all */
  656. if(true == exactMatch)
  657. {
  658. inst->tabCounter = 0u;
  659. }
  660. }
  661. }
  662. else
  663. {
  664. /* nothing to do here */
  665. }
  666. }
  667. /**
  668. * @brief prints all possible commands with description and usage
  669. * @param[in] handle handle shellmatta instance handle
  670. * @param[in] arguments not used here
  671. * @param[in] length not used here
  672. * @return #SHELLMATTA_OK
  673. * #SHELLMATTA_ERROR (buffer overflow)
  674. */
  675. static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  676. {
  677. shellmatta_retCode_t ret = SHELLMATTA_OK;
  678. shellmatta_instance_t *inst = (shellmatta_instance_t*) handle;
  679. shellmatta_cmd_t *cmd = inst->cmdList;
  680. size_t maxCmdLen = 0u;
  681. size_t maxCmdAliasLen = 0u;
  682. size_t maxCmdHelpLen = 0u;
  683. size_t cmdLen = 0u;
  684. size_t cmdAliasLen = 0u;
  685. size_t cmdHelpLen = 0u;
  686. uint32_t tabCnt = 0u;
  687. static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
  688. ' ', ' ', ' ', ' ',
  689. ' ', ' ', ' ', ' ',
  690. ' ', ' ', ' ', ' '};
  691. /** -# loop through all commands to determine the lengths of each cmd */
  692. while(NULL != cmd)
  693. {
  694. maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
  695. maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
  696. maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
  697. cmd = cmd->next;
  698. }
  699. /** -# loop through all commands and print all possible information */
  700. cmd = inst->cmdList;
  701. while(NULL != cmd)
  702. {
  703. /** -# determine the length of each field to add padding */
  704. cmdLen = strlen(cmd->cmd);
  705. cmdAliasLen = strlen(cmd->cmdAlias);
  706. cmdHelpLen = strlen(cmd->helpText);
  707. inst->write(cmd->cmd, strlen(cmd->cmd));
  708. tabCnt = (maxCmdLen - cmdLen) + 2u;
  709. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  710. inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
  711. tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
  712. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  713. inst->write(cmd->helpText, strlen(cmd->helpText));
  714. tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
  715. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  716. inst->write(cmd->usageText, strlen(cmd->usageText));
  717. inst->write("\r\n", 2u);
  718. cmd = cmd->next;
  719. }
  720. (void)arguments;
  721. (void)length;
  722. return ret;
  723. }
  724. shellmatta_cmd_t helpCmd = {"help", "h", "Print this help text", "help", helpCmdFct, NULL};
  725. /* interface functions */
  726. /**
  727. * @}
  728. * @addtogroup shellmatta_api
  729. * @{
  730. */
  731. /**
  732. * @brief initialize the shellmatta terminal and provide all callbacks
  733. * @param[in,out] inst pointer to a shellmatta instance
  734. * @param[out] handle pointer to shellmatta handle -
  735. * has to be used for all furterh api calls
  736. * @param[in] buffer pointer to the input buffer to use
  737. * @param[in] bufferSize size of the provided input buffer
  738. * @param[in] historyBuffer pointer to the history buffer to use
  739. * NULL in case of no history buffer
  740. * @param[in] historyBufferSize size of the history buffer
  741. * 0 in case of no history buffer
  742. * @param[in] prompt pointer to prompt string - is printed
  743. * after each command
  744. * @param[in] cmdList constant command list if no dynamic
  745. * adding of commands is desired
  746. * @param[in] writeFct function pointer to output function
  747. */
  748. shellmatta_retCode_t shellmatta_doInit(
  749. shellmatta_instance_t *inst,
  750. shellmatta_handle_t *handle,
  751. char *buffer,
  752. uint32_t bufferSize,
  753. char *historyBuffer,
  754. uint32_t historyBufferSize,
  755. const char *prompt,
  756. const shellmatta_cmd_t *cmdList,
  757. shellmatta_write_t writeFct)
  758. {
  759. /** -# check parameters for plausibility */
  760. if( (NULL != inst)
  761. && (NULL != handle)
  762. && (NULL != buffer)
  763. && (0u != bufferSize)
  764. && (NULL != prompt)
  765. && (NULL != writeFct)
  766. && ((NULL != historyBuffer) || (0u == historyBufferSize)))
  767. {
  768. /** -# copy all provided buffers into the shellmatta instance */
  769. inst->buffer = buffer;
  770. inst->bufferSize = bufferSize;
  771. inst->inputCount = 0u;
  772. inst->cursor = 0u;
  773. inst->historyBuffer = historyBuffer;
  774. inst->historyBufferSize = historyBufferSize;
  775. inst->historyStart = 0u;
  776. inst->historyEnd = 0u;
  777. inst->historyRead = 0u;
  778. inst->historyReadUp = true;
  779. inst->write = writeFct;
  780. inst->prompt = prompt;
  781. inst->echoEnabled = true;
  782. inst->dirty = false;
  783. inst->tabCounter = 0u;
  784. inst->escapeCounter = 0u;
  785. inst->mode = SHELLMATTA_MODE_INSERT;
  786. inst->cmdList = &helpCmd;
  787. inst->cmdListIsConst = false;
  788. if(NULL != cmdList)
  789. {
  790. helpCmd.next = (shellmatta_cmd_t *) cmdList;
  791. inst->cmdListIsConst = true;
  792. }
  793. inst->magic = SHELLMATTA_MAGIC;
  794. *handle = (shellmatta_handle_t)inst;
  795. /** -# print the first prompt */
  796. terminateInput(inst);
  797. }
  798. return SHELLMATTA_OK;
  799. }
  800. /**
  801. * @brief adds a command to the command list alphabetically ordered
  802. * @param[in] handle shellmatta instance handle
  803. * @param[in] cmd pointer to the command to add type #shellmatta_cmd_t
  804. * @return errorcode #SHELLMATTA_OK
  805. * #SHELLMATTA_USE_FAULT (param err)
  806. * SHELLMATTA_DUPLICATE
  807. */
  808. shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
  809. {
  810. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  811. shellmatta_cmd_t *tempCmd;
  812. shellmatta_cmd_t **prevCmd;
  813. bool cmdPlaced = false;
  814. shellmatta_retCode_t ret = SHELLMATTA_OK;
  815. int cmdDiff = 0;
  816. int aliasDiff = 0;
  817. /** -# check parameters for plausibility */
  818. if( (NULL != inst)
  819. && (SHELLMATTA_MAGIC == inst->magic)
  820. && (false == inst->cmdListIsConst))
  821. {
  822. tempCmd = inst->cmdList;
  823. prevCmd = &inst->cmdList;
  824. /** -# register first command as list entry */
  825. if (NULL == tempCmd)
  826. {
  827. inst->cmdList = cmd;
  828. cmd->next = NULL;
  829. }
  830. /** -# append the new command sorted */
  831. else
  832. {
  833. while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
  834. {
  835. cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
  836. aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
  837. /** -# check for a duplicate command */
  838. if((0u == cmdDiff) || (0u == aliasDiff))
  839. {
  840. ret = SHELLMATTA_DUPLICATE;
  841. }
  842. else if(0 < cmdDiff)
  843. {
  844. cmd->next = tempCmd;
  845. *prevCmd = cmd;
  846. cmdPlaced = true;
  847. }
  848. else if(NULL == tempCmd->next)
  849. {
  850. tempCmd->next = cmd;
  851. cmd->next = NULL;
  852. cmdPlaced = true;
  853. }
  854. else
  855. {
  856. /* nothing to do */
  857. }
  858. prevCmd = &tempCmd;
  859. tempCmd = tempCmd->next;
  860. }
  861. }
  862. }
  863. else
  864. {
  865. ret = SHELLMATTA_USE_FAULT;
  866. }
  867. return ret;
  868. }
  869. /**
  870. * @brief processes the passed amount of data
  871. * @param[in] handle shellmatta instance handle
  872. * @param[in] data pointer to input data to process
  873. * @param[in] size length of input data to process
  874. * @return errorcode #SHELLMATTA_OK
  875. * #SHELLMATTA_USE_FAULT
  876. */
  877. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  878. char *data,
  879. uint32_t size)
  880. {
  881. shellmatta_cmd_t *cmd;
  882. uint8_t cmdExecuted = 0u;
  883. shellmatta_retCode_t ret = SHELLMATTA_OK;
  884. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  885. /** -# check parameters for plausibility */
  886. if( (NULL != inst)
  887. && (SHELLMATTA_MAGIC == inst->magic))
  888. {
  889. /** -# process byte wise */
  890. for (uint32_t i = 0u; i < size; i++)
  891. {
  892. /** -# handle escape sequences */
  893. if(inst->escapeCounter != 0u)
  894. {
  895. handleEscapeSequence(inst, *data);
  896. }
  897. /** -# handle return as start of processing the command */
  898. else if ('\r' == *data)
  899. {
  900. cmd = inst->cmdList;
  901. inst->buffer[inst->inputCount] = 0u;
  902. /** -# store the current command and reset the history buffer */
  903. inst->dirty = true;
  904. storeHistoryCmd(inst);
  905. resetHistoryBuffer(inst);
  906. /** -# search for a matching command */
  907. while (NULL != cmd)
  908. {
  909. if ( (0 == strcmp(inst->buffer, cmd->cmd))
  910. || (0 == strcmp(inst->buffer, cmd->cmdAlias)))
  911. {
  912. inst->write("\r\n", 2u);
  913. cmdExecuted = 1u;
  914. cmd->cmdFct(inst, inst->buffer, inst->inputCount);
  915. cmd = NULL;
  916. }
  917. else
  918. {
  919. cmd = cmd->next;
  920. }
  921. }
  922. if ((cmdExecuted == 0u) && (inst->inputCount > 0))
  923. {
  924. inst->buffer[inst->inputCount] = '\0';
  925. inst->write("\r\nCommand: ", 11u);
  926. inst->write(inst->buffer, inst->inputCount);
  927. inst->write(" not found", 10u);
  928. }
  929. terminateInput(inst);
  930. }
  931. /** -# check for tabulator key - auto complete */
  932. else if('\t' == *data)
  933. {
  934. inst->dirty = true;
  935. doAutocomplete(inst);
  936. }
  937. /** -# check for cancel -
  938. * terminate current input and print prompt again */
  939. else if(3 == *data)
  940. {
  941. inst->dirty = false;
  942. resetHistoryBuffer(inst);
  943. terminateInput(inst);
  944. }
  945. /** -# check for backspace */
  946. else if('\b' == *data)
  947. {
  948. inst->dirty = true;
  949. removeChars(inst, 1u, true);
  950. }
  951. /** -# check for delete key */
  952. else if(0x7eu == *data)
  953. {
  954. inst->dirty = true;
  955. removeChars(inst, 1u, false);
  956. }
  957. /** -# check for start of escape sequence */
  958. else if('\e' == *data)
  959. {
  960. inst->escapeCounter = 1u;
  961. }
  962. else
  963. {
  964. inst->dirty = true;
  965. insertChars(inst, data, 1);
  966. }
  967. /** -# reset tab counter on not a tab */
  968. if ('\t' != *data)
  969. {
  970. inst->tabCounter = 0u;
  971. }
  972. data ++;
  973. }
  974. }
  975. else
  976. {
  977. ret = SHELLMATTA_USE_FAULT;
  978. }
  979. return ret;
  980. }
  981. /**
  982. * @brief simple write function to write a datastream to the output of an instance
  983. * @param[in] handle shellmatta instance handle
  984. * @param[in] data data to be written
  985. * @param[in] length amount of data to be written
  986. * @return
  987. */
  988. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  989. char *data,
  990. uint32_t length)
  991. {
  992. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  993. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  994. /** -# check parameters for plausibility */
  995. if( (NULL != inst)
  996. && (SHELLMATTA_MAGIC == inst->magic))
  997. {
  998. /** -# pass the data to the write function of the instance */
  999. ret = inst->write(data, length);
  1000. }
  1001. return ret;
  1002. }
  1003. #ifndef SHELLMATTA_STRIP_PRINTF
  1004. /**
  1005. * @brief printf like function to print output to the instances output
  1006. * @param[in] handle shellmatta instance handle
  1007. * @param[in] fmt format string and parameters
  1008. * @return errorcode #SHELLMATTA_OK
  1009. * #SHELLMATTA_USE_FAULT (no valid instance)
  1010. * #SHELLMATTA_ERROR (buffer overflow or format err)
  1011. */
  1012. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  1013. const char *fmt,
  1014. ...)
  1015. {
  1016. char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE];
  1017. va_list arg;
  1018. int length;
  1019. shellmatta_retCode_t ret = SHELLMATTA_OK;
  1020. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  1021. /** -# check parameters for plausibility */
  1022. if( (NULL != inst)
  1023. && (SHELLMATTA_MAGIC == inst->magic))
  1024. {
  1025. /** -# assemble output string and write it */
  1026. va_start(arg, fmt);
  1027. length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
  1028. va_end(arg);
  1029. if(length < 0)
  1030. {
  1031. ret = SHELLMATTA_ERROR;
  1032. }
  1033. else
  1034. {
  1035. inst->write(outputBuffer, length);
  1036. }
  1037. }
  1038. else
  1039. {
  1040. ret = SHELLMATTA_USE_FAULT;
  1041. }
  1042. return ret;
  1043. }
  1044. #endif
  1045. /** @} */
  1046. /** @} */