shellmatta.c 25 KB

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