shellmatta.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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->historyBuffer = historyBuffer;
  76. inst->historyBufferSize = historyBufferSize;
  77. inst->historyStart = 0u;
  78. inst->historyEnd = 0u;
  79. inst->historyRead = 0u;
  80. inst->historyReadUp = true;
  81. inst->write = writeFct;
  82. inst->prompt = prompt;
  83. inst->echoEnabled = true;
  84. inst->dirty = false;
  85. inst->tabCounter = 0u;
  86. inst->escapeCounter = 0u;
  87. inst->hereStartIdx = 0u;
  88. inst->hereDelimiterIdx = 0u;
  89. inst->hereLength = 0u;
  90. inst->mode = SHELLMATTA_MODE_INSERT;
  91. inst->cmdList = &helpCmd;
  92. inst->cmdListIsConst = false;
  93. if(NULL != cmdList)
  94. {
  95. helpCmd.next = (shellmatta_cmd_t *) cmdList;
  96. inst->cmdListIsConst = true;
  97. }
  98. inst->magic = SHELLMATTA_MAGIC;
  99. *handle = (shellmatta_handle_t)inst;
  100. /** -# print the first prompt */
  101. utils_terminateInput(inst);
  102. }
  103. return SHELLMATTA_OK;
  104. }
  105. /**
  106. * @brief resets the whole shellmatta instance
  107. * @param[in] handle shellmatta instance handle
  108. * @param[in] printPrompt print a new command prompt
  109. *
  110. * This function can be used e.g. when working with connection based interfaces (e.g. sockets) to clear
  111. * the shell from old content when a new connection is opened.
  112. * It resets all internal states - the buffers are left as they are - they will be overwritten.
  113. * The history buffer is deleted as well.
  114. */
  115. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle, bool printPrompt)
  116. {
  117. shellmatta_instance_t *inst = (shellmatta_instance_t *)handle;
  118. shellmatta_retCode_t ret = SHELLMATTA_OK;
  119. /*! -# check if the instance is plausible */
  120. if( (NULL != handle)
  121. && (SHELLMATTA_MAGIC == inst->magic))
  122. {
  123. inst->inputCount = 0u;
  124. inst->lastNewlineIdx = 0u;
  125. inst->cursor = 0u;
  126. inst->historyStart = 0u;
  127. inst->historyEnd = 0u;
  128. inst->historyRead = 0u;
  129. inst->historyReadUp = true;
  130. inst->dirty = false;
  131. inst->tabCounter = 0u;
  132. inst->escapeCounter = 0u;
  133. inst->hereStartIdx = 0u;
  134. inst->hereDelimiterIdx = 0u;
  135. inst->hereLength = 0u;
  136. if(true == printPrompt)
  137. {
  138. /** -# print a prompt if requested */
  139. utils_terminateInput(inst);
  140. }
  141. }
  142. else
  143. {
  144. ret = SHELLMATTA_USE_FAULT;
  145. }
  146. return ret;
  147. }
  148. /**
  149. * @brief adds a command to the command list alphabetically ordered
  150. * @param[in] handle shellmatta instance handle
  151. * @param[in] cmd pointer to the command to add type #shellmatta_cmd_t
  152. * @return errorcode #SHELLMATTA_OK
  153. * #SHELLMATTA_USE_FAULT (param err)
  154. * SHELLMATTA_DUPLICATE
  155. *
  156. * The cmd name is mandatory, the rest of the command parameters (alias, helpText and usageText) are optional
  157. * and can be set to NULL if not used.
  158. */
  159. shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
  160. {
  161. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  162. shellmatta_cmd_t *tempCmd;
  163. shellmatta_cmd_t **prevCmd;
  164. bool cmdPlaced = false;
  165. shellmatta_retCode_t ret = SHELLMATTA_OK;
  166. int cmdDiff = 0;
  167. int aliasDiff = 0;
  168. /** -# check parameters for plausibility */
  169. if( (NULL != inst)
  170. && (SHELLMATTA_MAGIC == inst->magic)
  171. && (false == inst->cmdListIsConst)
  172. && (NULL != cmd)
  173. && (NULL != cmd->cmd))
  174. {
  175. tempCmd = inst->cmdList;
  176. prevCmd = &inst->cmdList;
  177. /** -# register first command as list entry */
  178. if (NULL == tempCmd)
  179. {
  180. inst->cmdList = cmd;
  181. cmd->next = NULL;
  182. }
  183. /** -# append the new command sorted */
  184. else
  185. {
  186. while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
  187. {
  188. cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
  189. if( (NULL != cmd->cmdAlias)
  190. && (NULL != tempCmd->cmdAlias))
  191. {
  192. aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
  193. }
  194. else
  195. {
  196. aliasDiff = 1;
  197. }
  198. /** -# check for a duplicate command */
  199. if((0u == cmdDiff) || (0u == aliasDiff))
  200. {
  201. ret = SHELLMATTA_DUPLICATE;
  202. }
  203. else if(0 < cmdDiff)
  204. {
  205. cmd->next = tempCmd;
  206. *prevCmd = cmd;
  207. cmdPlaced = true;
  208. }
  209. else if(NULL == tempCmd->next)
  210. {
  211. tempCmd->next = cmd;
  212. cmd->next = NULL;
  213. cmdPlaced = true;
  214. }
  215. else
  216. {
  217. /* nothing to do */
  218. }
  219. prevCmd = &(tempCmd->next);
  220. tempCmd = tempCmd->next;
  221. }
  222. }
  223. }
  224. else
  225. {
  226. ret = SHELLMATTA_USE_FAULT;
  227. }
  228. return ret;
  229. }
  230. /**
  231. * @brief removes a command from the command list
  232. * @param[in] handle shellmatta instance handle
  233. * @param[in] cmd pointer to the command to remove type #shellmatta_cmd_t
  234. * @return errorcode #SHELLMATTA_OK
  235. * #SHELLMATTA_USE_FAULT (param err)
  236. */
  237. shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
  238. {
  239. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  240. shellmatta_cmd_t *prevCmd;
  241. shellmatta_cmd_t *tempCmd;
  242. shellmatta_retCode_t ret = SHELLMATTA_OK;
  243. /** -# check parameters for plausibility */
  244. if( (NULL != inst)
  245. && (SHELLMATTA_MAGIC == inst->magic)
  246. && (false == inst->cmdListIsConst)
  247. && (NULL != cmd)
  248. && (NULL != cmd->cmd))
  249. {
  250. tempCmd = inst->cmdList;
  251. prevCmd = NULL;
  252. /*! -# loop through command list */
  253. while(NULL != tempCmd)
  254. {
  255. /*! -# compare command strings to find the command to delete */
  256. if (0 == strcmp( tempCmd->cmd,
  257. cmd->cmd)
  258. && (strlen(tempCmd->cmd) == strlen(cmd->cmd)))
  259. {
  260. /*! -# first command removed */
  261. if(NULL == prevCmd)
  262. {
  263. inst->cmdList = tempCmd->next;
  264. }
  265. /*! -# last command removed */
  266. else if(NULL == tempCmd->next)
  267. {
  268. prevCmd->next = NULL;
  269. }
  270. /*! -# command removed from the middle of the list */
  271. else
  272. {
  273. prevCmd->next = tempCmd->next;
  274. }
  275. break;
  276. }
  277. prevCmd = tempCmd;
  278. tempCmd = tempCmd->next;
  279. }
  280. }
  281. else
  282. {
  283. ret = SHELLMATTA_USE_FAULT;
  284. }
  285. return ret;
  286. }
  287. /**
  288. * @brief changes configuration of a shellmatta instance
  289. * @param[in] handle shellmatta instance handle
  290. * @param[in] mode insert mode of the shellmatta type #shellmatta_mode_t
  291. * @param[in] echoEnabled true: echo received chars to the output
  292. * @return errorcode #SHELLMATTA_OK
  293. * #SHELLMATTA_USE_FAULT (param err)
  294. */
  295. shellmatta_retCode_t shellmatta_configure(shellmatta_handle_t handle, shellmatta_mode_t mode, bool echoEnabled)
  296. {
  297. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  298. shellmatta_retCode_t ret = SHELLMATTA_OK;
  299. /** -# check parameters for plausibility */
  300. if( (NULL != inst)
  301. && (SHELLMATTA_MAGIC == inst->magic)
  302. && ((mode == SHELLMATTA_MODE_INSERT) || (mode == SHELLMATTA_MODE_OVERWRITE)))
  303. {
  304. inst->mode = mode;
  305. inst->echoEnabled = echoEnabled;
  306. }
  307. else
  308. {
  309. ret = SHELLMATTA_USE_FAULT;
  310. }
  311. return ret;
  312. }
  313. /**
  314. * @brief processes the passed amount of data
  315. * @param[in] handle shellmatta instance handle
  316. * @param[in] data pointer to input data to process
  317. * @param[in] size length of input data to process
  318. * @return errorcode #SHELLMATTA_OK
  319. * #SHELLMATTA_USE_FAULT
  320. */
  321. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  322. char *data,
  323. uint32_t size)
  324. {
  325. shellmatta_cmd_t *cmd;
  326. uint8_t cmdExecuted = 0u;
  327. uint32_t cmdLen;
  328. char *tempString;
  329. char *argumentString;
  330. uint32_t argumentLength;
  331. uint32_t byteCounter;
  332. uint32_t idx;
  333. shellmatta_retCode_t ret = SHELLMATTA_OK;
  334. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  335. /** -# check parameters for plausibility */
  336. if( (NULL != inst)
  337. && (SHELLMATTA_MAGIC == inst->magic))
  338. {
  339. /** -# process byte wise */
  340. for (byteCounter = 0u; byteCounter < size; byteCounter++)
  341. {
  342. /*! -# set default values for the command argument - can be overwritten by heredoc */
  343. argumentString = inst->buffer;
  344. argumentLength = inst->inputCount;
  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. argumentLength = inst->lastNewlineIdx;
  423. /** -# store the current command and reset the history buffer */
  424. inst->dirty = true;
  425. history_storeCmd(inst);
  426. history_reset(inst);
  427. /* TODO it is difficult to store the complete command in the history buffer if it is restructured before...
  428. * So this should be an extra function that can be called after parsing the command and before calling the command function */
  429. for(idx = 1u; idx <= inst->hereStartIdx; idx++)
  430. {
  431. inst->buffer[inst->hereDelimiterIdx + inst->hereLength - idx] = inst->buffer[inst->hereStartIdx - idx];
  432. }
  433. argumentString = &(inst->buffer[inst->hereDelimiterIdx + inst->hereLength - inst->hereStartIdx]);
  434. argumentLength = inst->lastNewlineIdx - ((inst->hereDelimiterIdx + inst->hereLength) - inst->hereStartIdx);
  435. inst->hereLength = 0u;
  436. }
  437. else
  438. {
  439. /*! -# the party goes on - print the \r and add a \n to satisfy most terminals */
  440. inst->lastNewlineIdx = inst->inputCount;
  441. utils_insertChars(inst, data, 1u);
  442. }
  443. }
  444. if(0u == inst->hereLength)
  445. {
  446. cmd = inst->cmdList;
  447. argumentString[argumentLength] = 0u;
  448. /** -# determine the cmd len (chars until first space or \0 is found */
  449. cmdLen = 0u;
  450. while( (cmdLen < argumentLength)
  451. && (' ' != argumentString[cmdLen])
  452. && ('\r' != argumentString[cmdLen])
  453. && ('\n' != argumentString[cmdLen])
  454. && ('\0' != argumentString[cmdLen]))
  455. {
  456. cmdLen ++;
  457. }
  458. /** -# search for a matching command */
  459. while (NULL != cmd)
  460. {
  461. /** -# compare command and alias string and length */
  462. if ( ((0 == strncmp( argumentString,
  463. cmd->cmd,
  464. cmdLen))
  465. && (cmdLen == strlen(cmd->cmd)))
  466. || ((NULL != cmd->cmdAlias)
  467. && ((0 == strncmp( argumentString,
  468. cmd->cmdAlias,
  469. cmdLen))
  470. && (cmdLen == strlen(cmd->cmdAlias)))))
  471. {
  472. utils_writeEcho(inst, "\r\n", 2u);
  473. shellmatta_opt_init(inst, cmdLen + 1u);
  474. cmdExecuted = 1u;
  475. cmd->cmdFct(inst, argumentString, argumentLength);
  476. cmd = NULL;
  477. }
  478. else
  479. {
  480. cmd = cmd->next;
  481. }
  482. }
  483. if ((cmdExecuted == 0u) && (inst->inputCount > 0))
  484. {
  485. inst->write("\r\nCommand: ", 11u);
  486. inst->write(argumentString, argumentLength);
  487. inst->write(" not found", 10u);
  488. }
  489. utils_terminateInput(inst);
  490. }
  491. }
  492. /** -# check for tabulator key - auto complete */
  493. else if('\t' == *data)
  494. {
  495. inst->dirty = true;
  496. autocomplete_run(inst);
  497. }
  498. /** -# check for cancel -
  499. * terminate current input and print prompt again */
  500. else if(3 == *data)
  501. {
  502. inst->dirty = false;
  503. history_reset(inst);
  504. utils_terminateInput(inst);
  505. }
  506. /** -# check for backspace */
  507. else if( ('\b' == *data)
  508. || ('\x7f' == *data))
  509. {
  510. inst->dirty = true;
  511. utils_removeChars(inst, 1u, true);
  512. }
  513. /** -# check for start of escape sequence */
  514. else if('\x1b' == *data)
  515. {
  516. inst->escapeCounter = 1u;
  517. }
  518. else
  519. {
  520. inst->dirty = true;
  521. utils_insertChars(inst, data, 1);
  522. }
  523. /** -# reset tab counter on not a tab */
  524. if ('\t' != *data)
  525. {
  526. inst->tabCounter = 0u;
  527. }
  528. data ++;
  529. }
  530. }
  531. else
  532. {
  533. ret = SHELLMATTA_USE_FAULT;
  534. }
  535. return ret;
  536. }
  537. /**
  538. * @brief simple write function to write a datastream to the output of an instance
  539. * @param[in] handle shellmatta instance handle
  540. * @param[in] data data to be written
  541. * @param[in] length amount of data to be written
  542. * @return
  543. */
  544. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  545. char *data,
  546. uint32_t length)
  547. {
  548. shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
  549. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  550. /** -# check parameters for plausibility */
  551. if( (NULL != inst)
  552. && (SHELLMATTA_MAGIC == inst->magic))
  553. {
  554. /** -# pass the data to the write function of the instance */
  555. ret = inst->write(data, length);
  556. }
  557. return ret;
  558. }
  559. #ifndef SHELLMATTA_STRIP_PRINTF
  560. /**
  561. * @brief printf like function to print output to the instances output
  562. * @param[in] handle shellmatta instance handle
  563. * @param[in] fmt format string and parameters
  564. * @return errorcode #SHELLMATTA_OK
  565. * #SHELLMATTA_USE_FAULT (no valid instance)
  566. * #SHELLMATTA_ERROR (buffer overflow or format err)
  567. */
  568. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  569. const char *fmt,
  570. ...)
  571. {
  572. char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE];
  573. va_list arg;
  574. int length;
  575. shellmatta_retCode_t ret = SHELLMATTA_OK;
  576. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  577. /** -# check parameters for plausibility */
  578. if( (NULL != inst)
  579. && (SHELLMATTA_MAGIC == inst->magic))
  580. {
  581. /** -# assemble output string and write it */
  582. va_start(arg, fmt);
  583. length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
  584. va_end(arg);
  585. if(length < 0)
  586. {
  587. ret = SHELLMATTA_ERROR;
  588. }
  589. else
  590. {
  591. inst->write(outputBuffer, length);
  592. }
  593. }
  594. else
  595. {
  596. ret = SHELLMATTA_USE_FAULT;
  597. }
  598. return ret;
  599. }
  600. #endif
  601. /** @} */