shellmatta_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_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. #include <string.h>
  20. /**
  21. * @brief function to write an echo to the output depending on
  22. * the echo enabled state of the instance
  23. * @param[in] inst pointer to a shellmatta instance
  24. * @param[in] data pointer to the data so send
  25. * @param[in] length length of the data to send (in byte)
  26. */
  27. void utils_writeEcho( shellmatta_instance_t *inst,
  28. const char *data,
  29. uint32_t length)
  30. {
  31. if(true == inst->echoEnabled)
  32. {
  33. inst->write(data, length);
  34. }
  35. }
  36. /**
  37. * @brief itoa like function to convert int to an ascii string
  38. * @warning you have to provide a large enough buffer
  39. * @param[in] value
  40. * @param[in,out] buffer
  41. * @param[in] base
  42. * @return number of bytes in string
  43. */
  44. uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
  45. {
  46. char tempBuffer[34u];
  47. uint32_t i;
  48. uint32_t bufferIdx = 0u;
  49. char digitValue;
  50. /** -# check the base for plausibility */
  51. if((2 <= base) && (16 >= base))
  52. {
  53. /** -# check for sign */
  54. if(0 > value)
  55. {
  56. value = value * (-1);
  57. buffer[0u] = '-';
  58. bufferIdx += 1u;
  59. }
  60. /** -# loop through all digits in reverse order */
  61. i = 0u;
  62. do
  63. {
  64. digitValue = (char) (value % base);
  65. tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : (('A' - 10) + digitValue);
  66. value /= base;
  67. i ++;
  68. }while(value > 0);
  69. /** -# store the string in the correct order onto the buffer */
  70. while(i > 0u)
  71. {
  72. buffer[bufferIdx] = tempBuffer[i - 1u];
  73. i --;
  74. bufferIdx ++;
  75. }
  76. }
  77. return bufferIdx;
  78. }
  79. /**
  80. * @brief tells the terminal to save the current cursor position
  81. * @param[in] inst pointer to shellmatta instance
  82. */
  83. void utils_saveCursorPos(shellmatta_instance_t *inst)
  84. {
  85. utils_writeEcho(inst, "\x1b[s", 3u);
  86. }
  87. /**
  88. * @brief tells the terminal to restore the saved cursor position
  89. * @param[in] inst pointer to shellmatta instance
  90. */
  91. void utils_restoreCursorPos(shellmatta_instance_t *inst)
  92. {
  93. utils_writeEcho(inst, "\x1b[u", 3u);
  94. }
  95. /**
  96. * @brief tells the terminal to erase the line on the right side of the
  97. * cursor
  98. * @param[in] inst pointer to shellmatta instance
  99. */
  100. void utils_eraseLine(shellmatta_instance_t *inst)
  101. {
  102. utils_writeEcho(inst, "\x1b[K", 3u);
  103. }
  104. /**
  105. * @brief moves the cursor back by the given amoung of characters
  106. * @param[in] inst pointer to shellmatta instance
  107. * @param[in] length number of characters to rewind
  108. */
  109. void utils_rewindCursor(shellmatta_instance_t *inst, uint32_t length)
  110. {
  111. char terminalCmd[16];
  112. size_t size;
  113. length = SHELLMATTA_MIN (length, inst->cursor);
  114. if(length > 0u)
  115. {
  116. terminalCmd[0] = '\x1b';
  117. terminalCmd[1] = '[';
  118. size = 2u + utils_shellItoa(length, &terminalCmd[2], 10);
  119. terminalCmd[size] = 'D';
  120. utils_writeEcho(inst, terminalCmd, size + 1u);
  121. inst->cursor -= length;
  122. }
  123. }
  124. /**
  125. * @brief moves the cursor forward by the given amoung of characters
  126. * @param[in] inst pointer to shellmatta instance
  127. * @param[in] length number of characters to move forward
  128. */
  129. void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length)
  130. {
  131. char terminalCmd[16];
  132. size_t size;
  133. length = SHELLMATTA_MIN (length, (inst->inputCount - inst->cursor));
  134. if (length > 0u)
  135. {
  136. terminalCmd[0] = '\x1b';
  137. terminalCmd[1] = '[';
  138. size = 2u + utils_shellItoa(length, &terminalCmd[2], 10);
  139. terminalCmd[size] = 'C';
  140. utils_writeEcho(inst, terminalCmd, size + 1u);
  141. inst->cursor += length;
  142. }
  143. }
  144. /**
  145. * @brief inserts the given amount of characters at the cursor position
  146. * @param[in] inst pointer to shellmatta instance
  147. * @param[in] data pointer to the data to be inserted
  148. * @param[in] length length of the data to be inserted
  149. */
  150. void utils_insertChars( shellmatta_instance_t *inst,
  151. char *data,
  152. uint32_t length)
  153. {
  154. if(0u != length)
  155. {
  156. /** -# check if we have to move chars in the buffer */
  157. if( (inst->cursor != inst->inputCount)
  158. && (SHELLMATTA_MODE_INSERT == inst->mode))
  159. {
  160. /** -# move the existing chars */
  161. for ( uint32_t i = inst->inputCount;
  162. i > inst->cursor;
  163. i --)
  164. {
  165. inst->buffer[i + length - 1] = inst->buffer[i - 1];
  166. }
  167. /** -# store and print the new chars */
  168. memcpy(&(inst->buffer[inst->cursor]), data, length);
  169. utils_writeEcho(inst, data, length);
  170. /** -# print the other chars and restore the cursor to this position */
  171. utils_eraseLine(inst);
  172. utils_saveCursorPos(inst);
  173. utils_writeEcho( inst,
  174. &(inst->buffer[inst->cursor + length]),
  175. inst->inputCount - inst->cursor);
  176. utils_restoreCursorPos(inst);
  177. }
  178. /** -# just overwrite/append the chars */
  179. else
  180. {
  181. memcpy(&(inst->buffer[inst->cursor]), data, length);
  182. utils_writeEcho(inst, data, length);
  183. }
  184. inst->inputCount += length;
  185. inst->cursor += length;
  186. }
  187. }
  188. /**
  189. * @brief removes the given amount of characters from the current cursor
  190. * position
  191. * @param[in] inst pointer to a shellmatta instance
  192. * @param[in] length number of characters to remove
  193. * @param[in] backspace remove characters left of the cursor
  194. */
  195. void utils_removeChars( shellmatta_instance_t *inst,
  196. uint32_t length,
  197. bool backspace)
  198. {
  199. if(0u != length)
  200. {
  201. /** -# rewind the cursor in case of backspace */
  202. if(true == backspace)
  203. {
  204. length = SHELLMATTA_MIN (length, inst->cursor);
  205. utils_rewindCursor(inst, length);
  206. }
  207. else
  208. {
  209. length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
  210. }
  211. /** -# delete the char at the cursor position */
  212. for ( uint32_t i = inst->cursor;
  213. i < (inst->inputCount - length);
  214. i++)
  215. {
  216. inst->buffer[i] = inst->buffer[i + length];
  217. }
  218. /** -# print the rest of the line again */
  219. utils_eraseLine(inst);
  220. utils_saveCursorPos(inst);
  221. utils_writeEcho( inst,
  222. &(inst->buffer[inst->cursor]),
  223. (inst->inputCount - inst->cursor - length));
  224. utils_restoreCursorPos(inst);
  225. inst->inputCount -= length;
  226. }
  227. }
  228. /**
  229. * @brief clears the input buffer and removes the currend command from
  230. * the terminal output
  231. * @param[in] inst pointer to a shellmatta instance
  232. */
  233. void utils_clearInput(shellmatta_instance_t *inst)
  234. {
  235. utils_rewindCursor(inst, inst->cursor);
  236. utils_eraseLine(inst);
  237. inst->inputCount = 0u;
  238. inst->dirty = false;
  239. }
  240. /**
  241. * @brief prints all possible commands with description and usage
  242. * @param[in] handle handle shellmatta instance handle
  243. * @param[in] arguments not used here
  244. * @param[in] length not used here
  245. * @return #SHELLMATTA_OK
  246. * #SHELLMATTA_ERROR (buffer overflow)
  247. */
  248. static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  249. {
  250. shellmatta_retCode_t ret = SHELLMATTA_OK;
  251. shellmatta_instance_t *inst = (shellmatta_instance_t*) handle;
  252. shellmatta_cmd_t *cmd = inst->cmdList;
  253. size_t maxCmdLen = 0u;
  254. size_t maxCmdAliasLen = 0u;
  255. size_t maxCmdHelpLen = 0u;
  256. size_t cmdLen = 0u;
  257. size_t cmdAliasLen = 0u;
  258. size_t cmdHelpLen = 0u;
  259. uint32_t tabCnt = 0u;
  260. static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
  261. ' ', ' ', ' ', ' ',
  262. ' ', ' ', ' ', ' ',
  263. ' ', ' ', ' ', ' '};
  264. /** -# loop through all commands to determine the lengths of each cmd */
  265. while(NULL != cmd)
  266. {
  267. maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
  268. if(NULL != cmd->cmdAlias)
  269. {
  270. maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
  271. }
  272. if(NULL != cmd->helpText)
  273. {
  274. maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
  275. }
  276. cmd = cmd->next;
  277. }
  278. /** -# loop through all commands and print all possible information */
  279. cmd = inst->cmdList;
  280. while(NULL != cmd)
  281. {
  282. /** -# determine the length of each field to add padding */
  283. cmdLen = strlen(cmd->cmd);
  284. cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
  285. cmdHelpLen = (NULL != cmd->helpText) ? strlen(cmd->helpText) : 0u;
  286. inst->write(cmd->cmd, strlen(cmd->cmd));
  287. tabCnt = (maxCmdLen - cmdLen) + 2u;
  288. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  289. if(NULL != cmd->cmdAlias)
  290. {
  291. inst->write(cmd->cmdAlias, cmdAliasLen);
  292. }
  293. tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
  294. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  295. if(NULL != cmd->helpText)
  296. {
  297. inst->write(cmd->helpText, cmdHelpLen);
  298. }
  299. tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
  300. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  301. if(NULL != cmd->usageText)
  302. {
  303. inst->write(cmd->usageText, strlen(cmd->usageText));
  304. }
  305. inst->write("\r\n", 2u);
  306. cmd = cmd->next;
  307. }
  308. (void)arguments;
  309. (void)length;
  310. return ret;
  311. }
  312. shellmatta_cmd_t helpCmd = { SHELLMATTA_HELP_COMMAND
  313. , SHELLMATTA_HELP_ALIAS
  314. , SHELLMATTA_HELP_HELP_TEXT
  315. , SHELLMATTA_HELP_USAGE_TEXT
  316. , helpCmdFct
  317. , NULL};
  318. /**
  319. * @brief terminates an input and prints the prompt again
  320. * @param[in] inst pointer to a shellmatta instance
  321. */
  322. void utils_terminateInput(shellmatta_instance_t *inst)
  323. {
  324. inst->inputCount = 0u;
  325. inst->lastNewlineIdx = 0u;
  326. inst->hereLength = 0u;
  327. inst->cursor = 0u;
  328. inst->write("\r\n", 2u);
  329. inst->write(inst->prompt, strlen(inst->prompt));
  330. }
  331. /**
  332. * @}
  333. */