shellmatta.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (c) 2019 - 2021 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_private
  15. * @{
  16. */
  17. #include "shellmatta.h"
  18. #include "shellmatta_autocomplete.h"
  19. #include "shellmatta_history.h"
  20. #include "shellmatta_utils.h"
  21. #include "shellmatta_escape.h"
  22. #include "shellmatta_opt.h"
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. /**
  28. * @}
  29. * @addtogroup shellmatta_api
  30. * @{
  31. */
  32. /**
  33. * @brief initialize the shellmatta terminal and provide all callbacks
  34. * @param[in,out] inst pointer to a shellmatta instance
  35. * @param[out] handle pointer to shellmatta handle -
  36. * has to be used for all furterh api calls
  37. * @param[in] buffer pointer to the input buffer to use
  38. * @param[in] bufferSize size of the provided input buffer
  39. * @param[in] historyBuffer pointer to the history buffer to use
  40. * NULL in case of no history buffer
  41. * @param[in] historyBufferSize size of the history buffer
  42. * 0 in case of no history buffer
  43. * @param[in] prompt pointer to prompt string - is printed
  44. * after each command
  45. * @param[in] cmdList constant command list if no dynamic
  46. * adding of commands is desired
  47. * @param[in] writeFct function pointer to output function
  48. */
  49. shellmatta_retCode_t shellmatta_doInit(
  50. shellmatta_instance_t *inst,
  51. shellmatta_handle_t *handle,
  52. char *buffer,
  53. uint32_t bufferSize,
  54. char *historyBuffer,
  55. uint32_t historyBufferSize,
  56. const char *prompt,
  57. const shellmatta_cmd_t *cmdList,
  58. shellmatta_write_t writeFct)
  59. {
  60. /** -# check parameters for plausibility */
  61. if( (NULL != inst)
  62. && (NULL != handle)
  63. && (NULL != buffer)
  64. && (0u != bufferSize)
  65. && (NULL != prompt)
  66. && (NULL != writeFct)
  67. && ((NULL != historyBuffer) || (0u == historyBufferSize)))
  68. {
  69. /** -# copy all provided buffers into the shellmatta instance */
  70. inst->buffer = buffer;
  71. inst->bufferSize = bufferSize;
  72. inst->inputCount = 0u;
  73. inst->byteCounter = 0u;
  74. inst->lastNewlineIdx = 0u;
  75. inst->cursor = 0u;
  76. inst->stdinIdx = 0u;
  77. inst->stdinLength = 0u;
  78. inst->historyBuffer = historyBuffer;
  79. inst->historyBufferSize = historyBufferSize;
  80. inst->historyStart = 0u;
  81. inst->historyEnd = 0u;
  82. inst->historyRead = 0u;
  83. inst->historyReadUp = true;
  84. inst->write = writeFct;
  85. inst->prompt = prompt;
  86. inst->echoEnabled = true;
  87. inst->dirty = false;
  88. inst->tabCounter = 0u;
  89. inst->escapeCounter = 0u;
  90. inst->hereStartIdx = 0u;
  91. inst->hereDelimiterIdx = 0u;
  92. inst->hereLength = 0u;
  93. inst->delimiter = '\r';
  94. inst->mode = SHELLMATTA_MODE_INSERT;
  95. inst->cmdList = &(inst->helpCmd);
  96. inst->continuousCmd = NULL;
  97. inst->busyCmd = NULL;
  98. inst->cmdListIsConst = false;
  99. shellmatta_opt_init(inst, 0u);
  100. /** -# copy the help command structure to this instance */
  101. memcpy(&(inst->helpCmd), &helpCmd, sizeof(shellmatta_cmd_t));
  102. if(NULL != cmdList)
  103. {
  104. inst->helpCmd.next = (shellmatta_cmd_t *) cmdList;
  105. inst->cmdListIsConst = true;
  106. }
  107. inst->magic = SHELLMATTA_MAGIC;
  108. *handle = (shellmatta_handle_t)inst;
  109. /** -# print the first prompt */
  110. utils_terminateInput(inst);
  111. }
  112. return SHELLMATTA_OK;
  113. }
  114. /**
  115. * @brief resets the whole shellmatta instance
  116. * @param[in] handle shellmatta instance handle
  117. * @param[in] printPrompt print a new command prompt
  118. *
  119. * This function can be used e.g. when working with connection based interfaces (e.g. sockets) to clear
  120. * the shell from old content when a new connection is opened.
  121. * It resets all internal states - the buffers are left as they are - they will be overwritten.
  122. * The history buffer is deleted as well.
  123. */
  124. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle, bool printPrompt)
  125. {
  126. shellmatta_instance_t *inst = (shellmatta_instance_t *)handle;
  127. shellmatta_retCode_t ret = SHELLMATTA_OK;
  128. /** -# check if the instance is plausible */
  129. if( (NULL != handle)
  130. && (SHELLMATTA_MAGIC == inst->magic))
  131. {
  132. inst->inputCount = 0u;
  133. inst->byteCounter = 0u;
  134. inst->continuousCmd = NULL;
  135. inst->busyCmd = NULL;
  136. inst->lastNewlineIdx = 0u;
  137. inst->cursor = 0u;
  138. inst->stdinIdx = 0u;
  139. inst->stdinLength = 0u;
  140. inst->historyStart = 0u;
  141. inst->historyEnd = 0u;
  142. inst->historyRead = 0u;
  143. inst->historyReadUp = true;
  144. inst->dirty = false;
  145. inst->tabCounter = 0u;
  146. inst->escapeCounter = 0u;
  147. inst->hereStartIdx = 0u;
  148. inst->hereDelimiterIdx = 0u;
  149. inst->hereLength = 0u;
  150. shellmatta_opt_init(inst, 0u);
  151. if(true == printPrompt)
  152. {
  153. /** -# print a prompt if requested */
  154. utils_terminateInput(inst);
  155. }
  156. }
  157. else
  158. {
  159. ret = SHELLMATTA_USE_FAULT;
  160. }
  161. return ret;
  162. }
  163. /**
  164. * @brief adds a command to the command list alphabetically ordered
  165. * @param[in] handle shellmatta instance handle
  166. * @param[in] cmd pointer to the command to add type #shellmatta_cmd_t
  167. * @return errorcode #SHELLMATTA_OK
  168. * #SHELLMATTA_USE_FAULT (param err)
  169. * SHELLMATTA_DUPLICATE
  170. *
  171. * The cmd name is mandatory, the rest of the command parameters (alias, helpText and usageText) are optional
  172. * and can be set to NULL if not used.
  173. */
  174. shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
  175. {
  176. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  177. shellmatta_cmd_t *tempCmd;
  178. shellmatta_cmd_t **prevCmd;
  179. bool cmdPlaced = false;
  180. shellmatta_retCode_t ret = SHELLMATTA_OK;
  181. int cmdDiff = 0;
  182. int aliasDiff = 0;
  183. /** -# check parameters for plausibility */
  184. if( (NULL != inst)
  185. && (SHELLMATTA_MAGIC == inst->magic)
  186. && (false == inst->cmdListIsConst)
  187. && (NULL != cmd)
  188. && (NULL != cmd->cmd))
  189. {
  190. tempCmd = inst->cmdList;
  191. prevCmd = &inst->cmdList;
  192. /** -# register first command as list entry */
  193. if (NULL == tempCmd)
  194. {
  195. inst->cmdList = cmd;
  196. cmd->next = NULL;
  197. }
  198. /** -# append the new command sorted */
  199. else
  200. {
  201. while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
  202. {
  203. cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
  204. if( (NULL != cmd->cmdAlias)
  205. && (NULL != tempCmd->cmdAlias))
  206. {
  207. aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
  208. }
  209. else
  210. {
  211. aliasDiff = 1;
  212. }
  213. /** -# check for a duplicate command */
  214. if((0u == cmdDiff) || (0u == aliasDiff))
  215. {
  216. ret = SHELLMATTA_DUPLICATE;
  217. }
  218. else if(0 < cmdDiff)
  219. {
  220. cmd->next = tempCmd;
  221. *prevCmd = cmd;
  222. cmdPlaced = true;
  223. }
  224. else if(NULL == tempCmd->next)
  225. {
  226. tempCmd->next = cmd;
  227. cmd->next = NULL;
  228. cmdPlaced = true;
  229. }
  230. else
  231. {
  232. /* nothing to do */
  233. }
  234. prevCmd = &(tempCmd->next);
  235. tempCmd = tempCmd->next;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. ret = SHELLMATTA_USE_FAULT;
  242. }
  243. return ret;
  244. }
  245. /**
  246. * @brief removes a command from the command list
  247. * @param[in] handle shellmatta instance handle
  248. * @param[in] cmd pointer to the command to remove type #shellmatta_cmd_t
  249. * @return errorcode #SHELLMATTA_OK
  250. * #SHELLMATTA_USE_FAULT (param err)
  251. */
  252. shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
  253. {
  254. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  255. shellmatta_cmd_t *prevCmd;
  256. shellmatta_cmd_t *tempCmd;
  257. shellmatta_retCode_t ret = SHELLMATTA_OK;
  258. /** -# check parameters for plausibility */
  259. if( (NULL != inst)
  260. && (SHELLMATTA_MAGIC == inst->magic)
  261. && (false == inst->cmdListIsConst)
  262. && (NULL != cmd)
  263. && (NULL != cmd->cmd))
  264. {
  265. tempCmd = inst->cmdList;
  266. prevCmd = NULL;
  267. /** -# loop through command list */
  268. while(NULL != tempCmd)
  269. {
  270. /** -# compare command strings to find the command to delete */
  271. if (0 == strcmp( tempCmd->cmd,
  272. cmd->cmd)
  273. && (strlen(tempCmd->cmd) == strlen(cmd->cmd)))
  274. {
  275. /** -# first command removed */
  276. if(NULL == prevCmd)
  277. {
  278. inst->cmdList = tempCmd->next;
  279. }
  280. /** -# last command removed */
  281. else if(NULL == tempCmd->next)
  282. {
  283. prevCmd->next = NULL;
  284. }
  285. /** -# command removed from the middle of the list */
  286. else
  287. {
  288. prevCmd->next = tempCmd->next;
  289. }
  290. break;
  291. }
  292. prevCmd = tempCmd;
  293. tempCmd = tempCmd->next;
  294. }
  295. }
  296. else
  297. {
  298. ret = SHELLMATTA_USE_FAULT;
  299. }
  300. return ret;
  301. }
  302. /**
  303. * @brief changes configuration of a shellmatta instance
  304. * @param[in] handle shellmatta instance handle
  305. * @param[in] mode insert mode of the shellmatta type #shellmatta_mode_t
  306. * @param[in] echoEnabled true: echo received chars to the output
  307. * @param[in] delimiter delimiter used to detect the end of a cmd (default "\r")
  308. * @return errorcode #SHELLMATTA_OK
  309. * #SHELLMATTA_USE_FAULT (param err)
  310. */
  311. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  312. shellmatta_mode_t mode,
  313. bool echoEnabled,
  314. char delimiter)
  315. {
  316. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  317. shellmatta_retCode_t ret = SHELLMATTA_OK;
  318. /** -# check parameters for plausibility */
  319. if( (NULL != inst)
  320. && (SHELLMATTA_MAGIC == inst->magic)
  321. && ((mode == SHELLMATTA_MODE_INSERT) || (mode == SHELLMATTA_MODE_OVERWRITE)))
  322. {
  323. inst->mode = mode;
  324. inst->echoEnabled = echoEnabled;
  325. inst->delimiter = delimiter;
  326. }
  327. else
  328. {
  329. ret = SHELLMATTA_USE_FAULT;
  330. }
  331. return ret;
  332. }
  333. /**
  334. * @brief processes the passed amount of data
  335. * @param[in] handle shellmatta instance handle
  336. * @param[in] data pointer to input data to process
  337. * @param[in] size length of input data to process
  338. * @return errorcode #SHELLMATTA_OK
  339. * #SHELLMATTA_USE_FAULT
  340. */
  341. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  342. char *data,
  343. uint32_t size)
  344. {
  345. shellmatta_cmd_t *cmd;
  346. uint8_t cmdExecuted = 0u;
  347. uint32_t cmdLen;
  348. char *tempString;
  349. shellmatta_retCode_t ret = SHELLMATTA_OK;
  350. shellmatta_retCode_t cmdRet;
  351. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  352. /** -# check parameters for plausibility */
  353. if( (NULL != inst)
  354. && (SHELLMATTA_MAGIC == inst->magic))
  355. {
  356. /** -# in busy mode - keep calling this command */
  357. if(NULL != inst->busyCmd)
  358. {
  359. /** -# just call the function until it is not busy anymore */
  360. (void)shellmatta_opt_reInit(inst);
  361. ret = inst->busyCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  362. if(SHELLMATTA_BUSY == ret)
  363. {
  364. /** -# do nothing - still busy */
  365. }
  366. else if(SHELLMATTA_CONTINUE == ret)
  367. {
  368. inst->continuousCmd = inst->busyCmd;
  369. inst->busyCmd = NULL;
  370. }
  371. else
  372. {
  373. utils_terminateInput(inst);
  374. }
  375. }
  376. /** -# call continuous function even if there is no data */
  377. else if((0u == size) && (NULL != inst->continuousCmd))
  378. {
  379. /** -# just call the function without any new data */
  380. inst->stdinLength = 0u;
  381. inst->buffer[inst->stdinIdx] = '\0';
  382. (void)shellmatta_opt_reInit(inst);
  383. ret = inst->continuousCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  384. if(SHELLMATTA_CONTINUE == ret)
  385. {
  386. /** -# do nothing just continue */
  387. }
  388. else if(SHELLMATTA_BUSY == ret)
  389. {
  390. inst->busyCmd = inst->continuousCmd;
  391. inst->continuousCmd = NULL;
  392. }
  393. else
  394. {
  395. utils_terminateInput(inst);
  396. }
  397. }
  398. else
  399. {
  400. /* nothing to do here - continue parsing the command */
  401. }
  402. /** -# process byte wise */
  403. for (; (inst->byteCounter < size) && (NULL == inst->busyCmd); inst->byteCounter++)
  404. {
  405. /** -# in continuous mode - pass data directly to the command */
  406. if(NULL != inst->continuousCmd)
  407. {
  408. /** -# copy data and call command function */
  409. inst->buffer[inst->stdinIdx] = data[inst->byteCounter];
  410. inst->buffer[inst->stdinIdx + 1u] = '\0';
  411. inst->stdinLength = 1u;
  412. (void)shellmatta_opt_reInit(inst);
  413. ret = inst->continuousCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  414. /** -# check if continuous mode is canceled or interrupted by busy mode */
  415. if(SHELLMATTA_BUSY == ret)
  416. {
  417. inst->busyCmd = inst->continuousCmd;
  418. inst->continuousCmd = NULL;
  419. }
  420. else if(('\x03' == data[inst->byteCounter]))
  421. {
  422. /** -# cancel continue session */
  423. utils_terminateInput(inst);
  424. ret = SHELLMATTA_OK;
  425. }
  426. else if(SHELLMATTA_CONTINUE == ret)
  427. {
  428. /** -# do nothing - continue */
  429. }
  430. else
  431. {
  432. utils_terminateInput(inst);
  433. }
  434. }
  435. /** -# handle escape sequences */
  436. else if(inst->escapeCounter != 0u)
  437. {
  438. escape_handleSequence(inst, data[inst->byteCounter]);
  439. }
  440. /** -# handle delimiter as start of processing the command */
  441. else if (inst->delimiter == data[inst->byteCounter])
  442. {
  443. if(0u == inst->hereLength)
  444. {
  445. /**
  446. * @dot
  447. * digraph heredocParser {
  448. * start -> wait [ label="<< in first line - store delimiter" ];
  449. * wait -> wait [ label="delimiter not detected" ];
  450. * wait -> end [ label="delimiter found - remove all heredoc stuff and send the input to the command" ];
  451. * }
  452. * @enddot */
  453. /** -# check for heredoc - add string delimiter to stop strstr from searching too far */
  454. inst->buffer[inst->inputCount] = '\0';
  455. tempString = strstr(inst->buffer, "<<");
  456. if(NULL != tempString)
  457. {
  458. /*' -# check if length of heredoc delimiter is valid */
  459. if(inst->inputCount > ((uint32_t)(tempString - inst->buffer) + 2u))
  460. {
  461. inst->hereStartIdx = (uint32_t)(tempString - inst->buffer);
  462. inst->hereDelimiterIdx = inst->hereStartIdx + 2u;
  463. while((inst->hereDelimiterIdx < inst->inputCount)
  464. && ( ('\0' == inst->buffer[inst->hereDelimiterIdx])
  465. || (' ' == inst->buffer[inst->hereDelimiterIdx])))
  466. {
  467. inst->hereDelimiterIdx ++;
  468. }
  469. inst->hereLength = inst->inputCount - inst->hereDelimiterIdx;
  470. inst->dirty = true;
  471. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  472. inst->lastNewlineIdx = inst->inputCount;
  473. }
  474. else
  475. {
  476. inst->hereLength = 0u;
  477. /** -# store the current command and reset the history buffer */
  478. inst->dirty = true;
  479. history_storeCmd(inst);
  480. history_reset(inst);
  481. }
  482. }
  483. else
  484. {
  485. /** -# store the current command and reset the history buffer */
  486. inst->dirty = true;
  487. history_storeCmd(inst);
  488. history_reset(inst);
  489. }
  490. }
  491. else
  492. {
  493. tempString = &inst->buffer[inst->lastNewlineIdx];
  494. cmdLen = inst->inputCount - inst->lastNewlineIdx;
  495. /** -# skip newline characters before comparison */
  496. while(('\n' == *tempString) || ('\r' == *tempString))
  497. {
  498. tempString ++;
  499. cmdLen --;
  500. }
  501. if( (inst->hereLength == cmdLen)
  502. && (0 == strncmp( &inst->buffer[inst->hereDelimiterIdx],
  503. tempString,
  504. inst->hereLength)))
  505. {
  506. /** -# store the current command and reset the history buffer */
  507. inst->dirty = true;
  508. history_storeCmd(inst);
  509. history_reset(inst);
  510. /** -# process heredoc as stdin like input */
  511. /** -# find start of heredoc data */
  512. inst->stdinIdx = inst->hereDelimiterIdx + inst->hereLength;
  513. while( ('\n' == inst->buffer[inst->stdinIdx])
  514. || ('\r' == inst->buffer[inst->stdinIdx]))
  515. {
  516. inst->stdinIdx ++;
  517. }
  518. /** -# calculate length and terminate stdin string */
  519. if(inst->stdinIdx < inst->lastNewlineIdx)
  520. {
  521. inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx;
  522. inst->buffer[inst->lastNewlineIdx] = '\0';
  523. }
  524. else
  525. {
  526. inst->stdinLength = 0u;
  527. }
  528. /** -# calculate length and terminate argument string */
  529. inst->inputCount = inst->hereStartIdx;
  530. inst->buffer[inst->hereStartIdx] = '\0';
  531. /** -# terminate heredoc session */
  532. inst->hereLength = 0u;
  533. }
  534. else
  535. {
  536. /** -# the party goes on - just print the delimiter and store the position */
  537. inst->lastNewlineIdx = inst->inputCount;
  538. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  539. }
  540. }
  541. if(0u == inst->hereLength)
  542. {
  543. cmd = inst->cmdList;
  544. /** -# determine the cmd len (chars until first space or \0 is found */
  545. cmdLen = 0u;
  546. while( (cmdLen < inst->inputCount)
  547. && (' ' != inst->buffer[cmdLen])
  548. && ('\r' != inst->buffer[cmdLen])
  549. && ('\n' != inst->buffer[cmdLen])
  550. && ('\0' != inst->buffer[cmdLen]))
  551. {
  552. cmdLen ++;
  553. }
  554. /** -# search for a matching command */
  555. while (NULL != cmd)
  556. {
  557. /** -# compare command and alias string and length */
  558. if ( ((cmdLen == strlen(cmd->cmd))
  559. && (0 == strncmp( inst->buffer,
  560. cmd->cmd,
  561. cmdLen)))
  562. || ((NULL != cmd->cmdAlias)
  563. && (cmdLen == strlen(cmd->cmdAlias))
  564. && (0 == strncmp( inst->buffer,
  565. cmd->cmdAlias,
  566. cmdLen))))
  567. {
  568. utils_writeEcho(inst, "\r\n", 2u);
  569. shellmatta_opt_init(inst, cmdLen + 1u);
  570. cmdExecuted = 1u;
  571. cmdRet = cmd->cmdFct(handle, inst->buffer, inst->inputCount);
  572. switch(cmdRet)
  573. {
  574. case SHELLMATTA_CONTINUE:
  575. /** -# initialize stdin buffer and continuous cmd */
  576. inst->stdinIdx = inst->inputCount + 1u;
  577. inst->stdinLength = 0u;
  578. inst->continuousCmd = cmd;
  579. ret = cmdRet;
  580. break;
  581. case SHELLMATTA_BUSY:
  582. inst->busyCmd = cmd;
  583. ret = cmdRet;
  584. break;
  585. default:
  586. /* nothing to do - everything ok */
  587. break;
  588. }
  589. cmd = NULL;
  590. }
  591. else
  592. {
  593. cmd = cmd->next;
  594. }
  595. }
  596. if ((cmdExecuted == 0u) && (inst->inputCount > 0))
  597. {
  598. inst->write("\r\nCommand: ", 11u);
  599. inst->write(inst->buffer, inst->inputCount);
  600. inst->write(" not found", 10u);
  601. }
  602. /** -# terminate this session if no continuous mode is requested */
  603. if( (NULL == inst->continuousCmd)
  604. && (NULL == inst->busyCmd))
  605. {
  606. utils_terminateInput(inst);
  607. }
  608. }
  609. }
  610. /** -# ignore newline as first character (to be compatible to
  611. * terminals sending newline after return */
  612. else if((0u == inst->inputCount) && ('\n' == data[inst->byteCounter]))
  613. {
  614. /* do nothing */
  615. }
  616. /** -# check for tabulator key - auto complete */
  617. else if('\t' == data[inst->byteCounter])
  618. {
  619. inst->dirty = true;
  620. autocomplete_run(inst);
  621. }
  622. /** -# check for cancel -
  623. * terminate current input and print prompt again */
  624. else if('\x03' == data[inst->byteCounter])
  625. {
  626. inst->dirty = false;
  627. history_reset(inst);
  628. utils_terminateInput(inst);
  629. }
  630. /** -# check for backspace */
  631. else if( ('\b' == data[inst->byteCounter])
  632. || ('\x7f' == data[inst->byteCounter]))
  633. {
  634. inst->dirty = true;
  635. utils_removeChars(inst, 1u, true);
  636. }
  637. /** -# check for start of escape sequence */
  638. else if('\x1b' == data[inst->byteCounter])
  639. {
  640. inst->escapeCounter = 1u;
  641. }
  642. else
  643. {
  644. inst->dirty = true;
  645. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  646. }
  647. /** -# reset tab counter on not a tab */
  648. if ('\t' != data[inst->byteCounter])
  649. {
  650. inst->tabCounter = 0u;
  651. }
  652. }
  653. /** -# initialize the byte buffer if processing of the input is finished */
  654. if(ret != SHELLMATTA_BUSY)
  655. {
  656. inst->byteCounter = 0u;
  657. }
  658. }
  659. else
  660. {
  661. ret = SHELLMATTA_USE_FAULT;
  662. }
  663. return ret;
  664. }
  665. /**
  666. * @brief simple write function to write a datastream to the output of an instance
  667. * @param[in] handle shellmatta instance handle
  668. * @param[in] data data to be written
  669. * @param[in] length amount of data to be written
  670. * @return
  671. */
  672. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  673. char *data,
  674. uint32_t length)
  675. {
  676. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  677. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  678. /** -# check parameters for plausibility */
  679. if( (NULL != inst)
  680. && (SHELLMATTA_MAGIC == inst->magic))
  681. {
  682. /** -# pass the data to the write function of the instance */
  683. ret = inst->write(data, length);
  684. }
  685. return ret;
  686. }
  687. /**
  688. * @brief reads the stdin like buffer
  689. * @param[in] handle shellmatta instance handle
  690. * @param[out] data stdin data or NULL
  691. * @param[out] length size of the stdin data
  692. * @return
  693. */
  694. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  695. char **data,
  696. uint32_t *length)
  697. {
  698. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  699. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  700. /** -# check parameters for plausibility */
  701. if( (NULL != inst)
  702. && (SHELLMATTA_MAGIC == inst->magic)
  703. && (NULL != data)
  704. && (NULL != length))
  705. {
  706. /** -# return a pointer to the data or NULL */
  707. if(0u == inst->stdinLength)
  708. {
  709. *data = NULL;
  710. }
  711. else
  712. {
  713. *data = &(inst->buffer[inst->stdinIdx]);
  714. }
  715. *length = inst->stdinLength;
  716. }
  717. return ret;
  718. }
  719. #ifndef SHELLMATTA_STRIP_PRINTF
  720. /**
  721. * @brief printf like function to print output to the instances output
  722. * @param[in] handle shellmatta instance handle
  723. * @param[in] fmt format string and parameters
  724. * @return errorcode #SHELLMATTA_OK
  725. * #SHELLMATTA_USE_FAULT (no valid instance)
  726. * #SHELLMATTA_ERROR (buffer overflow or format err)
  727. */
  728. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  729. const char *fmt,
  730. ...)
  731. {
  732. char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE];
  733. va_list arg;
  734. int length;
  735. shellmatta_retCode_t ret = SHELLMATTA_OK;
  736. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  737. /** -# check parameters for plausibility */
  738. if( (NULL != inst)
  739. && (SHELLMATTA_MAGIC == inst->magic))
  740. {
  741. /** -# assemble output string and write it */
  742. va_start(arg, fmt);
  743. length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
  744. va_end(arg);
  745. if(length < 0)
  746. {
  747. ret = SHELLMATTA_ERROR;
  748. }
  749. else
  750. {
  751. inst->write(outputBuffer, length);
  752. }
  753. }
  754. else
  755. {
  756. ret = SHELLMATTA_USE_FAULT;
  757. }
  758. return ret;
  759. }
  760. #endif
  761. /** @} */