shellmatta.c 31 KB

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