shellmatta.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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 = 0u;
  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 = (uint8_t)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. /* processes payload by forwarding it recursively without transport layer to processData() */
  422. if (transportLayerInst.state == STATE_PROCESS_PAYLOAD)
  423. {
  424. /* replace inst->write function pointer with transport layer write function */
  425. transportLayerInst.originalWrite = inst->write;
  426. inst->write = shellmatta_write_transport;
  427. /* recursive call with complete payload */
  428. shellmatta_processData(handle, payloadBuffer, payloadLength);
  429. /* set back inst->write function pointer to original */
  430. inst->write = transportLayerInst.originalWrite;
  431. shellmatta_handle_transport_fsm(data);
  432. return SHELLMATTA_OK;
  433. }
  434. }
  435. }
  436. if ( (transportLayerInst.active)
  437. && (transportLayerInst.state != STATE_PROCESS_PAYLOAD)
  438. && (transportLayerInst.state != STATE_MANUAL_INPUT))
  439. {
  440. return SHELLMATTA_OK;
  441. }
  442. /** -# in busy mode - keep calling this command */
  443. if(NULL != inst->busyCmd)
  444. {
  445. /** -# just call the function until it is not busy anymore */
  446. (void)shellmatta_opt_reInit(inst);
  447. ret = inst->busyCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  448. if(SHELLMATTA_BUSY == ret)
  449. {
  450. /** -# do nothing - still busy */
  451. }
  452. else if(SHELLMATTA_CONTINUE == ret)
  453. {
  454. inst->continuousCmd = inst->busyCmd;
  455. inst->busyCmd = NULL;
  456. }
  457. else
  458. {
  459. utils_terminateInput(inst);
  460. }
  461. }
  462. /** -# call continuous function even if there is no data */
  463. else if((0u == size) && (NULL != inst->continuousCmd))
  464. {
  465. /** -# just call the function without any new data */
  466. inst->stdinLength = 0u;
  467. inst->buffer[inst->stdinIdx] = '\0';
  468. (void)shellmatta_opt_reInit(inst);
  469. ret = inst->continuousCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  470. if(SHELLMATTA_CONTINUE == ret)
  471. {
  472. /** -# do nothing just continue */
  473. }
  474. else if(SHELLMATTA_BUSY == ret)
  475. {
  476. inst->busyCmd = inst->continuousCmd;
  477. inst->continuousCmd = NULL;
  478. }
  479. else
  480. {
  481. utils_terminateInput(inst);
  482. }
  483. }
  484. else
  485. {
  486. /* nothing to do here - continue parsing the command */
  487. }
  488. /** -# process byte wise */
  489. for (; (inst->byteCounter < size) && (NULL == inst->busyCmd); inst->byteCounter++)
  490. {
  491. /** -# in continuous mode - pass data directly to the command */
  492. if(NULL != inst->continuousCmd)
  493. {
  494. /** -# copy data and call command function */
  495. inst->buffer[inst->stdinIdx] = data[inst->byteCounter];
  496. inst->buffer[inst->stdinIdx + 1u] = '\0';
  497. inst->stdinLength = 1u;
  498. (void)shellmatta_opt_reInit(inst);
  499. ret = inst->continuousCmd->cmdFct(handle, inst->buffer, inst->inputCount);
  500. /** -# check if continuous mode is canceled or interrupted by busy mode */
  501. if(SHELLMATTA_BUSY == ret)
  502. {
  503. inst->busyCmd = inst->continuousCmd;
  504. inst->continuousCmd = NULL;
  505. }
  506. else if(('\x03' == data[inst->byteCounter]))
  507. {
  508. /** -# cancel continue session */
  509. utils_terminateInput(inst);
  510. ret = SHELLMATTA_OK;
  511. }
  512. else if(SHELLMATTA_CONTINUE == ret)
  513. {
  514. /** -# do nothing - continue */
  515. }
  516. else
  517. {
  518. utils_terminateInput(inst);
  519. }
  520. }
  521. /** -# handle escape sequences */
  522. else if(inst->escapeCounter != 0u)
  523. {
  524. escape_handleSequence(inst, data[inst->byteCounter]);
  525. }
  526. /** -# handle delimiter as start of processing the command */
  527. else if (inst->delimiter == data[inst->byteCounter])
  528. {
  529. if(0u == inst->hereLength)
  530. {
  531. /**
  532. * @dot
  533. * digraph heredocParser {
  534. * start -> wait [ label="<< in first line - store delimiter" ];
  535. * wait -> wait [ label="delimiter not detected" ];
  536. * wait -> end [ label="delimiter found - remove all heredoc stuff and send the input to the command" ];
  537. * }
  538. * @enddot */
  539. /** -# check for heredoc - add string delimiter to stop strstr from searching too far */
  540. inst->buffer[inst->inputCount] = '\0';
  541. tempString = strstr(inst->buffer, "<<");
  542. if(NULL != tempString)
  543. {
  544. /*' -# check if length of heredoc delimiter is valid */
  545. if(inst->inputCount > ((uint32_t)(tempString - inst->buffer) + 2u))
  546. {
  547. inst->hereStartIdx = (uint32_t)(tempString - inst->buffer);
  548. inst->hereDelimiterIdx = inst->hereStartIdx + 2u;
  549. while((inst->hereDelimiterIdx < inst->inputCount)
  550. && ( ('\0' == inst->buffer[inst->hereDelimiterIdx])
  551. || (' ' == inst->buffer[inst->hereDelimiterIdx])))
  552. {
  553. inst->hereDelimiterIdx ++;
  554. }
  555. inst->hereLength = inst->inputCount - inst->hereDelimiterIdx;
  556. inst->dirty = true;
  557. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  558. inst->lastNewlineIdx = inst->inputCount;
  559. }
  560. else
  561. {
  562. inst->hereLength = 0u;
  563. /** -# store the current command and reset the history buffer */
  564. inst->dirty = true;
  565. history_storeCmd(inst);
  566. history_reset(inst);
  567. }
  568. }
  569. else
  570. {
  571. /** -# store the current command and reset the history buffer */
  572. inst->dirty = true;
  573. history_storeCmd(inst);
  574. history_reset(inst);
  575. }
  576. }
  577. else
  578. {
  579. tempString = &inst->buffer[inst->lastNewlineIdx];
  580. cmdLen = inst->inputCount - inst->lastNewlineIdx;
  581. /** -# skip newline characters before comparison */
  582. while(('\n' == *tempString) || ('\r' == *tempString))
  583. {
  584. tempString ++;
  585. cmdLen --;
  586. }
  587. if( (inst->hereLength == cmdLen)
  588. && (0 == strncmp( &inst->buffer[inst->hereDelimiterIdx],
  589. tempString,
  590. inst->hereLength)))
  591. {
  592. /** -# store the current command and reset the history buffer */
  593. inst->dirty = true;
  594. history_storeCmd(inst);
  595. history_reset(inst);
  596. /** -# process heredoc as stdin like input */
  597. /** -# find start of heredoc data */
  598. inst->stdinIdx = inst->hereDelimiterIdx + inst->hereLength;
  599. while( ('\n' == inst->buffer[inst->stdinIdx])
  600. || ('\r' == inst->buffer[inst->stdinIdx]))
  601. {
  602. inst->stdinIdx ++;
  603. }
  604. /** -# calculate length and terminate stdin string */
  605. if(inst->stdinIdx < inst->lastNewlineIdx)
  606. {
  607. inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx;
  608. inst->buffer[inst->lastNewlineIdx] = '\0';
  609. }
  610. else
  611. {
  612. inst->stdinLength = 0u;
  613. }
  614. /** -# calculate length and terminate argument string */
  615. inst->inputCount = inst->hereStartIdx;
  616. inst->buffer[inst->hereStartIdx] = '\0';
  617. /** -# terminate heredoc session */
  618. inst->hereLength = 0u;
  619. }
  620. else
  621. {
  622. /** -# the party goes on - just print the delimiter and store the position */
  623. inst->lastNewlineIdx = inst->inputCount;
  624. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  625. }
  626. }
  627. if(0u == inst->hereLength)
  628. {
  629. cmd = inst->cmdList;
  630. /** -# determine the cmd len (chars until first space or \0 is found */
  631. cmdLen = 0u;
  632. while( (cmdLen < inst->inputCount)
  633. && (' ' != inst->buffer[cmdLen])
  634. && ('\r' != inst->buffer[cmdLen])
  635. && ('\n' != inst->buffer[cmdLen])
  636. && ('\0' != inst->buffer[cmdLen]))
  637. {
  638. cmdLen ++;
  639. }
  640. /** -# search for a matching command */
  641. while (NULL != cmd)
  642. {
  643. /** -# compare command and alias string and length */
  644. if ( ((cmdLen == strlen(cmd->cmd))
  645. && (0 == strncmp(inst->buffer, cmd->cmd, cmdLen)))
  646. || ((NULL != cmd->cmdAlias)
  647. && (cmdLen == strlen(cmd->cmdAlias))
  648. && (0 == strncmp(inst->buffer, cmd->cmdAlias, cmdLen))))
  649. {
  650. utils_writeEcho(inst, "\r\n", 2u);
  651. shellmatta_opt_init(inst, cmdLen + 1u);
  652. cmdExecuted = 1u;
  653. cmdRet = cmd->cmdFct(handle, inst->buffer, inst->inputCount);
  654. switch(cmdRet)
  655. {
  656. case SHELLMATTA_CONTINUE:
  657. /** -# initialize stdin buffer and continuous cmd */
  658. inst->stdinIdx = inst->inputCount + 1u;
  659. inst->stdinLength = 0u;
  660. inst->continuousCmd = cmd;
  661. ret = cmdRet;
  662. break;
  663. case SHELLMATTA_BUSY:
  664. inst->busyCmd = cmd;
  665. ret = cmdRet;
  666. break;
  667. default:
  668. /* nothing to do - everything ok */
  669. break;
  670. }
  671. cmd = NULL;
  672. }
  673. else
  674. {
  675. cmd = cmd->next;
  676. }
  677. }
  678. if ((0u == cmdExecuted) && (inst->inputCount > 0))
  679. {
  680. inst->write("\r\nCommand: ", 11u);
  681. inst->write(inst->buffer, inst->inputCount);
  682. inst->write(" not found", 10u);
  683. }
  684. /** -# terminate this session if no continuous mode is requested */
  685. if( (NULL == inst->continuousCmd)
  686. && (NULL == inst->busyCmd))
  687. {
  688. utils_terminateInput(inst);
  689. }
  690. }
  691. }
  692. /** -# ignore newline as first character (to be compatible to
  693. * terminals sending newline after return) */
  694. else if((0u == inst->inputCount) && ('\n' == data[inst->byteCounter]))
  695. {
  696. /* do nothing */
  697. }
  698. /** -# check for tabulator key - auto complete */
  699. else if('\t' == data[inst->byteCounter])
  700. {
  701. inst->dirty = true;
  702. autocomplete_run(inst);
  703. }
  704. /** -# check for cancel -
  705. * terminate current input and print prompt again */
  706. else if('\x03' == data[inst->byteCounter])
  707. {
  708. inst->dirty = false;
  709. history_reset(inst);
  710. utils_terminateInput(inst);
  711. }
  712. /** -# check for backspace */
  713. else if( ('\b' == data[inst->byteCounter])
  714. || ('\x7f' == data[inst->byteCounter]))
  715. {
  716. inst->dirty = true;
  717. utils_removeChars(inst, 1u, true);
  718. }
  719. /** -# check for start of escape sequence */
  720. else if('\x1b' == data[inst->byteCounter])
  721. {
  722. inst->escapeCounter = 1u;
  723. }
  724. else
  725. {
  726. inst->dirty = true;
  727. utils_insertChars(inst, &data[inst->byteCounter], 1u);
  728. }
  729. /** -# reset tab counter on not a tab */
  730. if ('\t' != data[inst->byteCounter])
  731. {
  732. inst->tabCounter = 0u;
  733. }
  734. }
  735. /** -# initialize the byte buffer if processing of the input is finished */
  736. if(ret != SHELLMATTA_BUSY)
  737. {
  738. inst->byteCounter = 0u;
  739. }
  740. }
  741. else
  742. {
  743. ret = SHELLMATTA_USE_FAULT;
  744. }
  745. /* if manual input happened, reset transport layer fsm back to initial state */
  746. if (transportLayerInst.state == STATE_MANUAL_INPUT)
  747. {
  748. transportLayerInst.state = STATE_GET_SOH;
  749. }
  750. return ret;
  751. }
  752. /**
  753. * @brief simple write function to write a datastream to the output of an instance
  754. * @param[in] handle shellmatta instance handle
  755. * @param[in] data data to be written
  756. * @param[in] length amount of data to be written
  757. * @return
  758. */
  759. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  760. char *data,
  761. uint32_t length)
  762. {
  763. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  764. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  765. /** -# check parameters for plausibility */
  766. if( (NULL != inst)
  767. && (SHELLMATTA_MAGIC == inst->magic))
  768. {
  769. /** -# pass the data to the write function of the instance */
  770. ret = inst->write(data, length);
  771. }
  772. return ret;
  773. }
  774. /**
  775. * @brief reads the stdin like buffer
  776. * @param[in] handle shellmatta instance handle
  777. * @param[out] data stdin data or NULL
  778. * @param[out] length size of the stdin data
  779. * @return
  780. */
  781. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  782. char **data,
  783. uint32_t *length)
  784. {
  785. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  786. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  787. /** -# check parameters for plausibility */
  788. if( (NULL != inst)
  789. && (SHELLMATTA_MAGIC == inst->magic)
  790. && (NULL != data)
  791. && (NULL != length))
  792. {
  793. /** -# return a pointer to the data or NULL */
  794. if(0u == inst->stdinLength)
  795. {
  796. *data = NULL;
  797. }
  798. else
  799. {
  800. *data = &(inst->buffer[inst->stdinIdx]);
  801. }
  802. *length = inst->stdinLength;
  803. }
  804. return ret;
  805. }
  806. #ifndef SHELLMATTA_STRIP_PRINTF
  807. /**
  808. * @brief printf like function to print output to the instances output
  809. * @param[in] handle shellmatta instance handle
  810. * @param[in] fmt format string and parameters
  811. * @return errorcode #SHELLMATTA_OK
  812. * #SHELLMATTA_USE_FAULT (no valid instance)
  813. * #SHELLMATTA_ERROR (buffer overflow or format err)
  814. */
  815. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  816. const char *fmt,
  817. ...)
  818. {
  819. char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE];
  820. va_list arg;
  821. int length;
  822. shellmatta_retCode_t ret = SHELLMATTA_OK;
  823. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  824. /** -# check parameters for plausibility */
  825. if( (NULL != inst)
  826. && (SHELLMATTA_MAGIC == inst->magic))
  827. {
  828. /** -# assemble output string and write it */
  829. va_start(arg, fmt);
  830. length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
  831. va_end(arg);
  832. if(length < 0)
  833. {
  834. ret = SHELLMATTA_ERROR;
  835. }
  836. else
  837. {
  838. inst->write(outputBuffer, length);
  839. }
  840. }
  841. else
  842. {
  843. ret = SHELLMATTA_USE_FAULT;
  844. }
  845. return ret;
  846. }
  847. #endif
  848. /** @} */