shellmatta_utils.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) 2019 - 2024 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_utils.c
  10. * @brief util/helper functions of shellmatta
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta_utils
  15. * @{
  16. */
  17. #include "shellmatta_utils.h"
  18. #include "shellmatta.h"
  19. #ifdef SHELLMATTA_AUTHENTICATION
  20. #include "shellmatta_auth.h"
  21. #endif
  22. #include <string.h>
  23. /**
  24. * @brief function to write an echo to the output depending on
  25. * the echo enabled state of the instance
  26. * @param[in] inst pointer to a shellmatta instance
  27. * @param[in] data pointer to the data so send
  28. * @param[in] length length of the data to send (in byte)
  29. */
  30. void utils_writeEcho( shellmatta_instance_t *inst,
  31. const char *data,
  32. uint32_t length)
  33. {
  34. if(true == inst->echoEnabled)
  35. {
  36. SHELLMATTA_WRITE(data, length);
  37. }
  38. }
  39. /**
  40. * @brief itoa like function to convert int to an ascii string
  41. * @warning you have to provide a large enough buffer
  42. * @param[in] value
  43. * @param[in,out] buffer
  44. * @param[in] base
  45. * @return number of bytes in string
  46. */
  47. uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
  48. {
  49. char tempBuffer[34u];
  50. uint32_t i;
  51. uint32_t bufferIdx = 0u;
  52. int8_t digitValue;
  53. /** -# check the base for plausibility */
  54. if((base >= 2) && (base <= 16))
  55. {
  56. /** -# check for sign */
  57. if(value < 0)
  58. {
  59. value = value * (-1);
  60. buffer[0u] = '-';
  61. bufferIdx += 1u;
  62. }
  63. /** -# loop through all digits in reverse order */
  64. i = 0u;
  65. do
  66. {
  67. digitValue = (int8_t) (value % base);
  68. tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : ('A' + (digitValue - 10));
  69. value /= base;
  70. i ++;
  71. }while(value > 0);
  72. /** -# store the string in the correct order onto the buffer */
  73. while(i > 0u)
  74. {
  75. buffer[bufferIdx] = tempBuffer[i - 1u];
  76. i --;
  77. bufferIdx ++;
  78. }
  79. }
  80. return bufferIdx;
  81. }
  82. /**
  83. * @brief tells the terminal to save the current cursor position
  84. * @param[in] inst pointer to shellmatta instance
  85. */
  86. void utils_saveCursorPos(shellmatta_instance_t *inst)
  87. {
  88. utils_writeEcho(inst, "\x1b" "[s", 3u);
  89. }
  90. /**
  91. * @brief tells the terminal to restore the saved cursor position
  92. * @param[in] inst pointer to shellmatta instance
  93. */
  94. void utils_restoreCursorPos(shellmatta_instance_t *inst)
  95. {
  96. utils_writeEcho(inst, "\x1b" "[u", 3u);
  97. }
  98. /**
  99. * @brief tells the terminal to erase the line on the right side of the
  100. * cursor
  101. * @param[in] inst pointer to shellmatta instance
  102. */
  103. void utils_eraseLine(shellmatta_instance_t *inst)
  104. {
  105. utils_writeEcho(inst, "\x1b" "[K", 3u);
  106. }
  107. /**
  108. * @brief moves the cursor back by the given amoung of characters
  109. * @param[in] inst pointer to shellmatta instance
  110. * @param[in] length number of characters to rewind
  111. */
  112. void utils_rewindCursor(shellmatta_instance_t *inst, uint32_t length)
  113. {
  114. char terminalCmd[16];
  115. size_t size;
  116. length = SHELLMATTA_MIN (length, inst->cursor);
  117. if(length > 0u)
  118. {
  119. terminalCmd[0] = '\x1b';
  120. terminalCmd[1] = '[';
  121. size = 2u + utils_shellItoa(length, &terminalCmd[2], 10);
  122. terminalCmd[size] = 'D';
  123. utils_writeEcho(inst, terminalCmd, size + 1u);
  124. inst->cursor -= length;
  125. }
  126. }
  127. /**
  128. * @brief moves the cursor forward by the given amoung of characters
  129. * @param[in] inst pointer to shellmatta instance
  130. * @param[in] length number of characters to move forward
  131. */
  132. void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length)
  133. {
  134. char terminalCmd[16];
  135. size_t size;
  136. length = SHELLMATTA_MIN (length, (inst->inputCount - inst->cursor));
  137. if (length > 0u)
  138. {
  139. terminalCmd[0] = '\x1b';
  140. terminalCmd[1] = '[';
  141. size = 2u + utils_shellItoa(length, &terminalCmd[2], 10);
  142. terminalCmd[size] = 'C';
  143. utils_writeEcho(inst, terminalCmd, size + 1u);
  144. inst->cursor += length;
  145. }
  146. }
  147. /**
  148. * @brief inserts the given amount of characters at the cursor position
  149. * @param[in] inst pointer to shellmatta instance
  150. * @param[in] data pointer to the data to be inserted
  151. * @param[in] length length of the data to be inserted
  152. */
  153. void utils_insertChars( shellmatta_instance_t *inst,
  154. const char *data,
  155. uint32_t length)
  156. {
  157. uint32_t tmpLength = length;
  158. /** -# limit the length to the space left in the buffer */
  159. if((inst->inputCount + tmpLength) > inst->bufferSize)
  160. {
  161. tmpLength = inst->bufferSize - inst->inputCount;
  162. }
  163. if(0u != tmpLength)
  164. {
  165. /** -# check if we have to move chars in the buffer */
  166. if( (inst->cursor != inst->inputCount)
  167. && (SHELLMATTA_MODE_INSERT == inst->mode))
  168. {
  169. /** -# move the existing chars */
  170. for (uint32_t i = inst->inputCount; i > inst->cursor; i --)
  171. {
  172. inst->buffer[i + tmpLength - 1] = inst->buffer[i - 1];
  173. }
  174. /** -# store and print the new chars */
  175. memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
  176. utils_writeEcho(inst, data, tmpLength);
  177. /** -# print the other chars and restore the cursor to this position */
  178. utils_eraseLine(inst);
  179. utils_saveCursorPos(inst);
  180. utils_writeEcho( inst,
  181. &(inst->buffer[inst->cursor + tmpLength]),
  182. inst->inputCount - inst->cursor);
  183. utils_restoreCursorPos(inst);
  184. inst->cursor += tmpLength;
  185. inst->inputCount += tmpLength;
  186. }
  187. /** -# overwrite - if the cursor reaches the end of the input it is pushed further */
  188. else
  189. {
  190. memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
  191. utils_writeEcho(inst, data, tmpLength);
  192. inst->cursor += tmpLength;
  193. if(inst->cursor > inst->inputCount)
  194. {
  195. inst->inputCount = inst->cursor;
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * @brief removes the given amount of characters from the current cursor
  202. * position
  203. * @param[in] inst pointer to a shellmatta instance
  204. * @param[in] length number of characters to remove
  205. * @param[in] backspace true ==> remove characters left of the cursor
  206. * false ==> remove characters right of the cursor
  207. */
  208. void utils_removeChars( shellmatta_instance_t *inst,
  209. uint32_t length,
  210. bool backspace)
  211. {
  212. if((0u != length) && (inst->inputCount >= inst->cursor))
  213. {
  214. /** -# rewind the cursor in case of backspace */
  215. if(true == backspace)
  216. {
  217. length = SHELLMATTA_MIN (length, inst->cursor);
  218. utils_rewindCursor(inst, length);
  219. }
  220. else
  221. {
  222. length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
  223. }
  224. /** -# delete the char at the cursor position */
  225. for ( uint32_t i = inst->cursor;
  226. i < (inst->inputCount - length);
  227. i++)
  228. {
  229. inst->buffer[i] = inst->buffer[i + length];
  230. }
  231. /** -# print the rest of the line again */
  232. utils_eraseLine(inst);
  233. utils_saveCursorPos(inst);
  234. utils_writeEcho( inst,
  235. &(inst->buffer[inst->cursor]),
  236. (inst->inputCount - inst->cursor - length));
  237. utils_restoreCursorPos(inst);
  238. inst->inputCount -= length;
  239. }
  240. }
  241. /**
  242. * @brief clears the input buffer and removes the currend command from
  243. * the terminal output
  244. * @param[in] inst pointer to a shellmatta instance
  245. */
  246. void utils_clearInput(shellmatta_instance_t *inst)
  247. {
  248. utils_rewindCursor(inst, inst->cursor);
  249. utils_eraseLine(inst);
  250. inst->inputCount = 0u;
  251. inst->dirty = false;
  252. }
  253. /**
  254. * @brief prints the usage information of one passed command
  255. * @param[in] inst handle shellmatta instance handle
  256. * @param[in] cmd pointer to a command structure to print
  257. * @return #SHELLMATTA_OK
  258. * #SHELLMATTA_ERROR
  259. */
  260. static shellmatta_retCode_t printUsage(const shellmatta_instance_t *inst, const shellmatta_cmd_t *cmd)
  261. {
  262. shellmatta_retCode_t ret = SHELLMATTA_OK;
  263. /** -# write the command and alias if configured */
  264. SHELLMATTA_RET(ret, SHELLMATTA_WRITE("Help for command: ", 18u));
  265. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->cmd, strlen(cmd->cmd)));
  266. if((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
  267. {
  268. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(" (", 2u));
  269. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->cmdAlias, strlen(cmd->cmdAlias)));
  270. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(")", 1u));
  271. }
  272. /** -# write the help text if configured */
  273. if((NULL != cmd->helpText) && (0u != strlen(cmd->helpText)))
  274. {
  275. SHELLMATTA_RET(ret, SHELLMATTA_WRITE("\r\n\r\n", 4u));
  276. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->helpText, strlen(cmd->helpText)));
  277. }
  278. /** -# write the usage text if configured */
  279. if((NULL != cmd->usageText) && (0u != strlen(cmd->usageText)))
  280. {
  281. SHELLMATTA_RET(ret, SHELLMATTA_WRITE("\r\n\r\nUsage: \r\n", 13u));
  282. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->usageText, strlen(cmd->usageText)));
  283. }
  284. SHELLMATTA_RET(ret, SHELLMATTA_WRITE("\r\n", 2u));
  285. return ret;
  286. }
  287. /**
  288. * @brief prints all possible commands with description and usage
  289. * @param[in] handle handle shellmatta instance handle
  290. * @param[in] arguments arguments containing a command name or alias
  291. * @param[in] length length of the arguments
  292. * @return #SHELLMATTA_OK
  293. * #SHELLMATTA_ERROR (buffer overflow)
  294. */
  295. static shellmatta_retCode_t helpCmdFct(const shellmatta_handle_t handle, const char *arguments, uint32_t length)
  296. {
  297. shellmatta_retCode_t ret = SHELLMATTA_OK;
  298. const shellmatta_instance_t *inst = (const shellmatta_instance_t*) handle;
  299. shellmatta_cmd_t *cmd = NULL;
  300. size_t maxCmdLen = 0u;
  301. size_t maxCmdAliasLen = 0u;
  302. size_t cmdLen = 0u;
  303. const char *subCmd = NULL;
  304. size_t cmdAliasLen;
  305. uint32_t tabCnt;
  306. uint32_t i;
  307. static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
  308. ' ', ' ', ' ', ' ',
  309. ' ', ' ', ' ', ' ',
  310. ' ', ' ', ' ', ' '};
  311. /** -# check if help is called with a command - find first space */
  312. for(i = 1u; i < length; i ++)
  313. {
  314. if(' ' == arguments[i - 1])
  315. {
  316. subCmd = &(arguments[i]);
  317. /** -# determine subcommand length*/
  318. cmdLen = 0u;
  319. while( ((i + cmdLen) < length)
  320. && (' ' != arguments[i + cmdLen])
  321. && ('\r' != arguments[i + cmdLen])
  322. && ('\n' != arguments[i + cmdLen])
  323. && ('\0' != arguments[i + cmdLen]))
  324. {
  325. cmdLen ++;
  326. }
  327. break;
  328. }
  329. }
  330. /* print detailled help */
  331. if(NULL != subCmd)
  332. {
  333. cmd = inst->cmdList;
  334. /** -# search for a matching command */
  335. while (NULL != cmd)
  336. {
  337. /** -# compare command and alias string and length */
  338. if ( ((cmdLen == strlen(cmd->cmd))
  339. && (0 == strncmp(subCmd, cmd->cmd, cmdLen)))
  340. || ((NULL != cmd->cmdAlias)
  341. && (cmdLen == strlen(cmd->cmdAlias))
  342. && (0 == strncmp(subCmd, cmd->cmdAlias, cmdLen))))
  343. {
  344. SHELLMATTA_RET(ret, printUsage(inst, cmd));
  345. break;
  346. }
  347. cmd = cmd->next;
  348. }
  349. }
  350. /** -# print help list if no sub cmd was found */
  351. if(NULL == cmd)
  352. {
  353. /** -# loop through all commands to determine the lengths of each cmd */
  354. cmd = inst->cmdList;
  355. while(NULL != cmd)
  356. {
  357. #ifdef SHELLMATTA_AUTHENTICATION
  358. if (SHELLMATTA_OK != shellmatta_auth_is_cmd_permitted(inst, cmd))
  359. {
  360. cmd = cmd->next;
  361. continue;
  362. }
  363. #endif
  364. maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
  365. if(NULL != cmd->cmdAlias)
  366. {
  367. maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
  368. }
  369. cmd = cmd->next;
  370. }
  371. /** -# loop through all commands and print all possible information */
  372. cmd = inst->cmdList;
  373. while(NULL != cmd)
  374. {
  375. #ifdef SHELLMATTA_AUTHENTICATION
  376. if (SHELLMATTA_OK != shellmatta_auth_is_cmd_permitted(inst, cmd))
  377. {
  378. cmd = cmd->next;
  379. continue;
  380. }
  381. #endif
  382. /** -# determine the length of each field to add padding */
  383. cmdLen = strlen(cmd->cmd);
  384. cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
  385. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->cmd, strlen(cmd->cmd)));
  386. tabCnt = (maxCmdLen - cmdLen) + 2u;
  387. /** -# add padding if there is anything to be printed afterwards */
  388. if( ((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
  389. || ((NULL != cmd->helpText) && (0u != strlen(cmd->helpText))))
  390. {
  391. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt);
  392. }
  393. if((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
  394. {
  395. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->cmdAlias, cmdAliasLen));
  396. }
  397. tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
  398. if((NULL != cmd->helpText) && (0u != strlen(cmd->helpText)))
  399. {
  400. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt);
  401. SHELLMATTA_RET(ret, SHELLMATTA_WRITE(cmd->helpText, strlen(cmd->helpText)));
  402. }
  403. SHELLMATTA_RET(ret, SHELLMATTA_WRITE("\r\n", 2u));
  404. cmd = cmd->next;
  405. }
  406. }
  407. return ret;
  408. }
  409. const shellmatta_cmd_t helpCmd = {SHELLMATTA_HELP_COMMAND
  410. , SHELLMATTA_HELP_ALIAS
  411. , SHELLMATTA_HELP_HELP_TEXT
  412. , SHELLMATTA_HELP_USAGE_TEXT
  413. , helpCmdFct
  414. , NULL
  415. #ifdef SHELLMATTA_AUTHENTICATION
  416. , NULL
  417. #endif
  418. };
  419. /**
  420. * @brief terminates an input and prints the prompt again
  421. * @param[in] inst pointer to a shellmatta instance
  422. */
  423. void utils_terminateInput(shellmatta_instance_t *inst)
  424. {
  425. inst->inputCount = 0u;
  426. inst->lastNewlineIdx = 0u;
  427. inst->hereLength = 0u;
  428. inst->cursor = 0u;
  429. inst->stdinIdx = 0u;
  430. inst->stdinLength = 0u;
  431. inst->continuousCmd = NULL;
  432. inst->busyCmd = NULL;
  433. SHELLMATTA_WRITE("\r\n", 2u);
  434. #ifdef SHELLMATTA_AUTHENTICATION
  435. inst->loginState = SHELLMATTA_AUTH_IDLE;
  436. if (NULL != inst->userPointer)
  437. {
  438. SHELLMATTA_WRITE(inst->userPointer->username, strlen(inst->userPointer->username));
  439. SHELLMATTA_WRITE("@", 1);
  440. }
  441. #endif
  442. SHELLMATTA_WRITE(inst->prompt, strlen(inst->prompt));
  443. }
  444. /**
  445. * @}
  446. */