shellmatta.c 29 KB

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