shellmatta_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * @todo this function shall check buffer overflows
  150. */
  151. void utils_insertChars( shellmatta_instance_t *inst,
  152. char *data,
  153. uint32_t length)
  154. {
  155. if(0u != length)
  156. {
  157. /** -# check if we have to move chars in the buffer */
  158. if( (inst->cursor != inst->inputCount)
  159. && (SHELLMATTA_MODE_INSERT == inst->mode))
  160. {
  161. /** -# move the existing chars */
  162. for ( uint32_t i = inst->inputCount;
  163. i > inst->cursor;
  164. i --)
  165. {
  166. inst->buffer[i + length - 1] = inst->buffer[i - 1];
  167. }
  168. /** -# store and print the new chars */
  169. memcpy(&(inst->buffer[inst->cursor]), data, length);
  170. utils_writeEcho(inst, data, length);
  171. /** -# print the other chars and restore the cursor to this position */
  172. utils_eraseLine(inst);
  173. utils_saveCursorPos(inst);
  174. utils_writeEcho( inst,
  175. &(inst->buffer[inst->cursor + length]),
  176. inst->inputCount - inst->cursor);
  177. utils_restoreCursorPos(inst);
  178. inst->cursor += length;
  179. inst->inputCount += length;
  180. }
  181. /** -# overwrite - if the cursor reaches the end of the input it is pushed further */
  182. else
  183. {
  184. memcpy(&(inst->buffer[inst->cursor]), data, length);
  185. utils_writeEcho(inst, data, length);
  186. inst->cursor += length;
  187. if(inst->cursor > inst->inputCount)
  188. {
  189. inst->inputCount = inst->cursor;
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * @brief removes the given amount of characters from the current cursor
  196. * position
  197. * @param[in] inst pointer to a shellmatta instance
  198. * @param[in] length number of characters to remove
  199. * @param[in] backspace remove characters left of the cursor
  200. */
  201. void utils_removeChars( shellmatta_instance_t *inst,
  202. uint32_t length,
  203. bool backspace)
  204. {
  205. if(0u != length)
  206. {
  207. /** -# rewind the cursor in case of backspace */
  208. if(true == backspace)
  209. {
  210. length = SHELLMATTA_MIN (length, inst->cursor);
  211. utils_rewindCursor(inst, length);
  212. }
  213. else
  214. {
  215. length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
  216. }
  217. /** -# delete the char at the cursor position */
  218. for ( uint32_t i = inst->cursor;
  219. i < (inst->inputCount - length);
  220. i++)
  221. {
  222. inst->buffer[i] = inst->buffer[i + length];
  223. }
  224. /** -# print the rest of the line again */
  225. utils_eraseLine(inst);
  226. utils_saveCursorPos(inst);
  227. utils_writeEcho( inst,
  228. &(inst->buffer[inst->cursor]),
  229. (inst->inputCount - inst->cursor - length));
  230. utils_restoreCursorPos(inst);
  231. inst->inputCount -= length;
  232. }
  233. }
  234. /**
  235. * @brief clears the input buffer and removes the currend command from
  236. * the terminal output
  237. * @param[in] inst pointer to a shellmatta instance
  238. */
  239. void utils_clearInput(shellmatta_instance_t *inst)
  240. {
  241. utils_rewindCursor(inst, inst->cursor);
  242. utils_eraseLine(inst);
  243. inst->inputCount = 0u;
  244. inst->dirty = false;
  245. }
  246. /**
  247. * @brief prints all possible commands with description and usage
  248. * @param[in] handle handle shellmatta instance handle
  249. * @param[in] arguments not used here
  250. * @param[in] length not used here
  251. * @return #SHELLMATTA_OK
  252. * #SHELLMATTA_ERROR (buffer overflow)
  253. */
  254. static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  255. {
  256. shellmatta_retCode_t ret = SHELLMATTA_OK;
  257. shellmatta_instance_t *inst = (shellmatta_instance_t*) handle;
  258. shellmatta_cmd_t *cmd = inst->cmdList;
  259. size_t maxCmdLen = 0u;
  260. size_t maxCmdAliasLen = 0u;
  261. size_t maxCmdHelpLen = 0u;
  262. size_t cmdLen = 0u;
  263. size_t cmdAliasLen = 0u;
  264. size_t cmdHelpLen = 0u;
  265. uint32_t tabCnt = 0u;
  266. static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
  267. ' ', ' ', ' ', ' ',
  268. ' ', ' ', ' ', ' ',
  269. ' ', ' ', ' ', ' '};
  270. /** -# loop through all commands to determine the lengths of each cmd */
  271. while(NULL != cmd)
  272. {
  273. maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
  274. if(NULL != cmd->cmdAlias)
  275. {
  276. maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
  277. }
  278. if(NULL != cmd->helpText)
  279. {
  280. maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
  281. }
  282. cmd = cmd->next;
  283. }
  284. /** -# loop through all commands and print all possible information */
  285. cmd = inst->cmdList;
  286. while(NULL != cmd)
  287. {
  288. /** -# determine the length of each field to add padding */
  289. cmdLen = strlen(cmd->cmd);
  290. cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
  291. cmdHelpLen = (NULL != cmd->helpText) ? strlen(cmd->helpText) : 0u;
  292. inst->write(cmd->cmd, strlen(cmd->cmd));
  293. tabCnt = (maxCmdLen - cmdLen) + 2u;
  294. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  295. if(NULL != cmd->cmdAlias)
  296. {
  297. inst->write(cmd->cmdAlias, cmdAliasLen);
  298. }
  299. tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
  300. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  301. if(NULL != cmd->helpText)
  302. {
  303. inst->write(cmd->helpText, cmdHelpLen);
  304. }
  305. tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
  306. SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
  307. if(NULL != cmd->usageText)
  308. {
  309. inst->write(cmd->usageText, strlen(cmd->usageText));
  310. }
  311. inst->write("\r\n", 2u);
  312. cmd = cmd->next;
  313. }
  314. (void)arguments;
  315. (void)length;
  316. return ret;
  317. }
  318. const shellmatta_cmd_t helpCmd = {SHELLMATTA_HELP_COMMAND
  319. , SHELLMATTA_HELP_ALIAS
  320. , SHELLMATTA_HELP_HELP_TEXT
  321. , SHELLMATTA_HELP_USAGE_TEXT
  322. , helpCmdFct
  323. , NULL};
  324. /**
  325. * @brief terminates an input and prints the prompt again
  326. * @param[in] inst pointer to a shellmatta instance
  327. */
  328. void utils_terminateInput(shellmatta_instance_t *inst)
  329. {
  330. inst->inputCount = 0u;
  331. inst->lastNewlineIdx = 0u;
  332. inst->hereLength = 0u;
  333. inst->cursor = 0u;
  334. inst->stdinIdx = 0u;
  335. inst->stdinLength = 0u;
  336. inst->continuousCmd = NULL;
  337. inst->busyCmd = NULL;
  338. inst->write("\r\n", 2u);
  339. inst->write(inst->prompt, strlen(inst->prompt));
  340. }
  341. /**
  342. * @}
  343. */