shellmatta.c 38 KB

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