edline.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright 2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \file gorp/edline/edline.c
  36. * \brief Simple line editor.
  37. *
  38. * \verbatim
  39. * $Id$
  40. * \endverbatim
  41. */
  42. #include <gorp/edline.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <ctype.h>
  46. #include <string.h>
  47. /*!
  48. * \addtogroup xgEdLine
  49. */
  50. /*@{*/
  51. /*! \brief Maximum number of history entries.
  52. *
  53. * The first entry with index 0 is used to restore the default value.
  54. */
  55. #if !defined(EDIT_MAX_HISTORY) && !defined(EDIT_DISABLE_HISTORY)
  56. #define EDIT_MAX_HISTORY 16
  57. #endif
  58. /*!
  59. * \brief Default input routine.
  60. *
  61. * Uses stdin and may be replaced by calling EdLineRegisterInput().
  62. *
  63. * \param param Optional parameter, ignored here.
  64. *
  65. * \return Character read or EOF in case of an error.
  66. */
  67. static int EdLineGetChar(void *param)
  68. {
  69. return getchar();
  70. }
  71. /*!
  72. * \brief Default output routine.
  73. *
  74. * Uses stdout and may be replaced by calling EdLineRegisterOutput().
  75. *
  76. * \param param Optional parameter, ignored here.
  77. *
  78. * \return Character written or EOF in case of an error.
  79. */
  80. static int EdLinePutChar(void *param, int ch)
  81. {
  82. return putchar(ch);
  83. }
  84. /*!
  85. * \brief Register an input routine.
  86. *
  87. * Applications may use this function to replace the default character
  88. * input routine.
  89. *
  90. * \param el Pointer to an \ref EDLINE structure, obtained by a
  91. * previous call to \ref EdLineOpen.
  92. * \param get Pointer to the new input routine. If NULL, then the
  93. * default routine is restored.
  94. * \param param Optional parameter, which is passed to the input
  95. * routine.
  96. */
  97. void EdLineRegisterInput(EDLINE *el, EDLINEGET get, void *param)
  98. {
  99. if (get) {
  100. el->el_get = get;
  101. } else {
  102. el->el_get = EdLineGetChar;
  103. }
  104. el->el_iparm = param;
  105. }
  106. /*!
  107. * \brief Register an output routine.
  108. *
  109. * Applications may use this function to replace the default character
  110. * output routine.
  111. *
  112. * \param el Pointer to an \ref EDLINE structure, obtained by a
  113. * previous call to \ref EdLineOpen.
  114. * \param put Pointer to the new output routine. If NULL, then the
  115. * default routine is restored.
  116. * \param param Optional parameter, which is passed to the output
  117. * routine.
  118. */
  119. void EdLineRegisterOutput(EDLINE *el, EDLINEPUT put, void *param)
  120. {
  121. if (put) {
  122. el->el_put = put;
  123. } else {
  124. el->el_put = EdLinePutChar;
  125. }
  126. el->el_oparm = param;
  127. }
  128. /*!
  129. * \brief Default key mapping routine.
  130. *
  131. * Replaces carriage returns and linefeeds by \ref EDIT_KEY_ENTER.
  132. * Linefeeds immediately following carriage returns are ignored.
  133. * May be replaced by calling EdLineRegisterKeymap().
  134. *
  135. * This routine may be called by other key mapping routines.
  136. *
  137. * \param key Input character to remap.
  138. * \param seq Pointer to an integer, which is used by the key mapping
  139. * routine to store the current state. Note, that its value
  140. * must be preserved by the caller between calls to
  141. * \ref EdLineRead.
  142. *
  143. * \return The mapped character. If \ref EDIT_KEY_IGNORE is returned
  144. * the caller must ignore this character.
  145. */
  146. int EdLineKeyMap(int key, int *seq)
  147. {
  148. if (key == '\r') {
  149. *seq = -1;
  150. key = EDIT_KEY_ENTER;
  151. }
  152. else {
  153. if (key == '\n') {
  154. if (*seq < 0) {
  155. key = EDIT_KEY_IGNORE;
  156. } else {
  157. key = EDIT_KEY_ENTER;
  158. }
  159. }
  160. *seq = 0;
  161. }
  162. return key;
  163. }
  164. /*!
  165. * \brief Register a key mapping routine.
  166. *
  167. * \param el Pointer to an \ref EDLINE structure, obtained by a
  168. * previous call to \ref EdLineOpen.
  169. * \param map Pointer to the new mapping routine. If NULL, then the
  170. * default routine is restored.
  171. */
  172. void EdLineRegisterKeymap(EDLINE *el, EDLINEMAP map)
  173. {
  174. if (map) {
  175. el->el_map = map;
  176. } else {
  177. el->el_map = EdLineKeyMap;
  178. }
  179. }
  180. /*!
  181. * \brief Open a line editor.
  182. *
  183. * \param mode Mode flags, may contain any combination of
  184. * - EDIT_MODE_ECHO enables echoing of input characters.
  185. * - EDIT_MODE_BINARY enables binary mode (currently unused).
  186. * - EDIT_MODE_HISTORY enables input line history.
  187. *
  188. * \return Pointer to a new EDLINE structure.
  189. */
  190. EDLINE *EdLineOpen(uint16_t mode)
  191. {
  192. EDLINE *el;
  193. el = calloc(1, sizeof(EDLINE));
  194. if (el) {
  195. el->el_mode = mode;
  196. EdLineRegisterInput(el, NULL, stdin);
  197. EdLineRegisterOutput(el, NULL, stdout);
  198. EdLineRegisterKeymap(el, NULL);
  199. #ifndef EDIT_DISABLE_HISTORY
  200. if (mode & EDIT_MODE_HISTORY) {
  201. el->el_hist = EditHistoryCreate(EDIT_MAX_HISTORY);
  202. }
  203. #endif
  204. }
  205. return el;
  206. }
  207. /*!
  208. * \brief Close a line editor.
  209. *
  210. * Releases all occupied memory.
  211. *
  212. * \param el Pointer to an \ref EDLINE structure, obtained by a
  213. * previous call to \ref EdLineOpen.
  214. */
  215. void EdLineClose(EDLINE *el)
  216. {
  217. if (el) {
  218. #ifndef EDIT_DISABLE_HISTORY
  219. EditHistoryDestroy(el->el_hist);
  220. #endif
  221. free(el);
  222. }
  223. }
  224. /*!
  225. * \brief Print a string.
  226. *
  227. * Uses the currently registered character output routine.
  228. *
  229. * \param el Pointer to an \ref EDLINE structure, obtained by a
  230. * previous call to \ref EdLineOpen.
  231. */
  232. static int PrintString(EDLINE *el, char *str)
  233. {
  234. while (*str) {
  235. (*el->el_put)(el->el_oparm, *str);
  236. str++;
  237. }
  238. return 0;
  239. }
  240. /*!
  241. * \brief Print a character multiple times.
  242. *
  243. * Uses the currently registered character output routine.
  244. *
  245. * \param el Pointer to an \ref EDLINE structure, obtained by a
  246. * previous call to \ref EdLineOpen.
  247. * \param ch The character to print.
  248. * \param num The number of times the character should be printed.
  249. */
  250. static void PrintCharacter(EDLINE *el, int ch, int num)
  251. {
  252. while (num-- > 0) {
  253. (*el->el_put)(el->el_oparm, ch);
  254. }
  255. }
  256. #ifndef EDIT_DISABLE_HISTORY
  257. /*!
  258. * \brief Clear a given number of characters on the right side.
  259. *
  260. * Replaces a given number of characters on the right side of the
  261. * current cursor position. The cursor position is restored by
  262. * printing the same number of backspace characters.
  263. *
  264. * \param el Pointer to an \ref EDLINE structure, obtained by a
  265. * previous call to \ref EdLineOpen.
  266. * \param num Number of characters to clear.
  267. */
  268. static void ClearLineEnd(EDLINE *el, int num)
  269. {
  270. PrintCharacter(el, EDIT_CHAR_SPACE, num);
  271. PrintCharacter(el, EDIT_CHAR_BACKSPACE, num);
  272. }
  273. #endif
  274. /*!
  275. * \brief Read a line.
  276. *
  277. * This functions offers some editing capabilities and is typically
  278. * used for text input by human users.
  279. *
  280. * Line editing can be done by entering any of the following control
  281. * characters:
  282. *
  283. * - \ref EDIT_KEY_RESTORE Restores initial default line (default ESC)
  284. * - \ref EDIT_KEY_REMOVE Deletes character on the left of the cursor (default CTRL-H)
  285. * - \ref EDIT_KEY_HOME Moves cursor to the beginning of the line (default Ctrl-A).
  286. * - \ref EDIT_KEY_END Moves cursor to the beginning of the line (default CTRL-E)
  287. * - \ref EDIT_KEY_LEFT Moves cursor one character to the left (default CTRL-B)
  288. * - \ref EDIT_KEY_RIGHT Moves cursor one character to the right (default CTRL-F)
  289. * - \ref EDIT_KEY_UP Walks up the list of previously entered lines (default CTRL-R)
  290. * - \ref EDIT_KEY_DOWN Walks down the list of previously entered lines (default CTRL-V)
  291. *
  292. * Note, that these commands may be modified by the currently registered
  293. * remapping routine.
  294. *
  295. * \param el Pointer to an \ref EDLINE structure, obtained by a
  296. * previous call to \ref EdLineOpen.
  297. * \param buf Pointer to the buffer that receives the text line.
  298. * If it contains a string on entry, this will be used
  299. * as the default value.
  300. * \param siz Number of bytes available in the buffer. The maximum
  301. * length of the text string is 1 less, so the string
  302. * is always properly terminated.
  303. *
  304. * \return Number of characters or -1 on errors.
  305. *
  306. * \todo Hidden entry for password input.
  307. */
  308. int EdLineRead(EDLINE *el, char *buf, int siz)
  309. {
  310. int ch;
  311. int cpos;
  312. int i;
  313. #ifndef EDIT_DISABLE_HISTORY
  314. int ipos;
  315. int hidx = 0;
  316. int refresh = 0;
  317. #endif
  318. /* Make sure that the string is terminated. */
  319. buf[siz - 1] = '\0';
  320. #ifndef EDIT_DISABLE_HISTORY
  321. EditHistorySet(el->el_hist, 0, buf);
  322. #endif
  323. PrintString(el, buf);
  324. cpos = strlen(buf);
  325. for (;;) {
  326. ch = (*el->el_get)(el->el_iparm);
  327. if (ch == EOF) {
  328. return -1;
  329. }
  330. ch = (*el->el_map)(ch, &el->el_seq);
  331. if (ch == EDIT_KEY_ENTER) {
  332. break;
  333. }
  334. /* Backspace removes the character in front of the cursor. */
  335. if (ch == EDIT_KEY_REMOVE) {
  336. if (cpos) {
  337. cpos--;
  338. for (i = cpos; i < siz - 1; i++) {
  339. buf[i] = buf[i + 1];
  340. }
  341. if (el->el_mode & EDIT_MODE_ECHO) {
  342. (*el->el_put)(el->el_oparm, EDIT_CHAR_BACKSPACE);
  343. }
  344. PrintString(el, &buf[cpos]);
  345. (*el->el_put)(el->el_oparm, EDIT_CHAR_SPACE);
  346. PrintCharacter(el, EDIT_CHAR_BACKSPACE, strlen(buf) + 1 - cpos);
  347. }
  348. }
  349. #ifndef EDIT_DISABLE_HISTORY
  350. else if (ch == EDIT_KEY_RESTORE) {
  351. hidx = 0;
  352. refresh = 1;
  353. }
  354. else if (ch == EDIT_KEY_UP) {
  355. if (EditHistoryGet(el->el_hist, hidx + 1, NULL, 0) >= 0) {
  356. hidx++;
  357. refresh = 1;
  358. }
  359. }
  360. else if (ch == EDIT_KEY_DOWN) {
  361. if (hidx > 0) {
  362. hidx--;
  363. refresh = 1;
  364. }
  365. }
  366. #endif
  367. else if (ch == EDIT_KEY_RIGHT) {
  368. if (cpos < strlen(buf)) {
  369. if (el->el_mode & EDIT_MODE_ECHO) {
  370. (*el->el_put)(el->el_oparm, buf[cpos]);
  371. }
  372. cpos++;
  373. }
  374. }
  375. else if (ch == EDIT_KEY_LEFT) {
  376. if (cpos) {
  377. if (el->el_mode & EDIT_MODE_ECHO) {
  378. (*el->el_put)(el->el_oparm, EDIT_CHAR_BACKSPACE);
  379. }
  380. cpos--;
  381. }
  382. }
  383. else if (ch == EDIT_KEY_HOME) {
  384. PrintCharacter(el, EDIT_CHAR_BACKSPACE, cpos);
  385. cpos = 0;
  386. }
  387. else if (ch == EDIT_KEY_END) {
  388. PrintString(el, &buf[cpos]);
  389. cpos = strlen(buf);
  390. }
  391. /* Normal character, insert at cursor position if buffer is not full. */
  392. else if (isprint(ch) && strlen(buf) < siz - 1) {
  393. for (i = siz - 1; i > cpos; i--) {
  394. buf[i] = buf[i - 1];
  395. }
  396. buf[cpos++] = ch;
  397. if (el->el_mode & EDIT_MODE_ECHO) {
  398. (*el->el_put)(el->el_oparm, ch);
  399. }
  400. PrintString(el, &buf[cpos]);
  401. PrintCharacter(el, EDIT_CHAR_BACKSPACE, strlen(buf) - cpos);
  402. } else {
  403. /* Beep on buffer overflow. */
  404. (*el->el_put)(el->el_oparm, EDIT_CHAR_ALARM);
  405. }
  406. #ifndef EDIT_DISABLE_HISTORY
  407. if (refresh && (el->el_mode & EDIT_MODE_HISTORY) != 0) {
  408. refresh = 0;
  409. PrintCharacter(el, EDIT_CHAR_BACKSPACE, cpos);
  410. ipos = strlen(buf);
  411. EditHistoryGet(el->el_hist, hidx, buf, siz);
  412. cpos = strlen(buf);
  413. PrintString(el, buf);
  414. ClearLineEnd(el, ipos - cpos);
  415. }
  416. #endif
  417. }
  418. PrintString(el, EDIT_STR_EOL);
  419. #ifndef EDIT_DISABLE_HISTORY
  420. EditHistoryInsert(el->el_hist, 1, buf);
  421. #endif
  422. return strlen(buf);
  423. }
  424. /*@}*/