shellmatta.c 34 KB

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