shellmatta.c 33 KB

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