shellmatta.c 32 KB

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