shellmatta.c 31 KB

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