shellmatta.c 26 KB

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