Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

readline.c 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <ipxe/console.h>
  29. #include <ipxe/keys.h>
  30. #include <ipxe/editstring.h>
  31. #include <readline/readline.h>
  32. /** @file
  33. *
  34. * Minimal readline
  35. *
  36. */
  37. #define READLINE_MAX 256
  38. /**
  39. * Synchronise console with edited string
  40. *
  41. * @v string Editable string
  42. */
  43. static void sync_console ( struct edit_string *string ) {
  44. unsigned int mod_start = string->mod_start;
  45. unsigned int mod_end = string->mod_end;
  46. unsigned int cursor = string->last_cursor;
  47. size_t len = strlen ( string->buf );
  48. /* Expand region back to old cursor position if applicable */
  49. if ( mod_start > string->last_cursor )
  50. mod_start = string->last_cursor;
  51. /* Expand region forward to new cursor position if applicable */
  52. if ( mod_end < string->cursor )
  53. mod_end = string->cursor;
  54. /* Backspace to start of region */
  55. while ( cursor > mod_start ) {
  56. putchar ( '\b' );
  57. cursor--;
  58. }
  59. /* Print modified region */
  60. while ( cursor < mod_end ) {
  61. putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
  62. cursor++;
  63. }
  64. /* Backspace to new cursor position */
  65. while ( cursor > string->cursor ) {
  66. putchar ( '\b' );
  67. cursor--;
  68. }
  69. }
  70. /**
  71. * Locate history entry
  72. *
  73. * @v history History buffer
  74. * @v depth Depth within history buffer
  75. * @ret entry History entry
  76. */
  77. static struct readline_history_entry *
  78. history_entry ( struct readline_history *history, unsigned int depth ) {
  79. unsigned int offset;
  80. offset = ( ( history->next - depth ) %
  81. ( sizeof ( history->entries ) /
  82. sizeof ( history->entries[0] ) ) );
  83. return &history->entries[offset];
  84. }
  85. /**
  86. * Read string from history buffer
  87. *
  88. * @v history History buffer
  89. * @v depth Depth within history buffer
  90. * @ret string String
  91. */
  92. static const char * history_fetch ( struct readline_history *history,
  93. unsigned int depth ) {
  94. struct readline_history_entry *entry;
  95. /* Return the temporary copy if it exists, otherwise return
  96. * the persistent copy.
  97. */
  98. entry = history_entry ( history, depth );
  99. return ( entry->temp ? entry->temp : entry->string );
  100. }
  101. /**
  102. * Write temporary string copy to history buffer
  103. *
  104. * @v history History buffer
  105. * @v depth Depth within history buffer
  106. * @v string String
  107. */
  108. static void history_store ( struct readline_history *history,
  109. unsigned int depth, const char *string ) {
  110. struct readline_history_entry *entry;
  111. char *temp;
  112. /* Create temporary copy of string */
  113. temp = strdup ( string );
  114. if ( ! temp ) {
  115. /* Just discard the string; there's nothing we can do */
  116. DBGC ( history, "READLINE %p could not store string\n",
  117. history );
  118. return;
  119. }
  120. /* Store temporary copy */
  121. entry = history_entry ( history, depth );
  122. free ( entry->temp );
  123. entry->temp = temp;
  124. }
  125. /**
  126. * Move to new history depth
  127. *
  128. * @v history History buffer
  129. * @v offset Offset by which to change depth
  130. * @v old_string String (possibly modified) at current depth
  131. * @ret new_string String at new depth, or NULL for no movement
  132. */
  133. static const char * history_move ( struct readline_history *history,
  134. int offset, const char *old_string ) {
  135. unsigned int new_depth = ( history->depth + offset );
  136. const char * new_string = history_fetch ( history, new_depth );
  137. /* Depth checks */
  138. if ( new_depth > READLINE_HISTORY_MAX_DEPTH )
  139. return NULL;
  140. if ( ! new_string )
  141. return NULL;
  142. /* Store temporary copy of old string at current depth */
  143. history_store ( history, history->depth, old_string );
  144. /* Update depth */
  145. history->depth = new_depth;
  146. /* Return new string */
  147. return new_string;
  148. }
  149. /**
  150. * Append new history entry
  151. *
  152. * @v history History buffer
  153. * @v string String
  154. */
  155. static void history_append ( struct readline_history *history,
  156. const char *string ) {
  157. struct readline_history_entry *entry;
  158. /* Store new entry */
  159. entry = history_entry ( history, 0 );
  160. assert ( entry->string == NULL );
  161. entry->string = strdup ( string );
  162. if ( ! entry->string ) {
  163. /* Just discard the string; there's nothing we can do */
  164. DBGC ( history, "READLINE %p could not append string\n",
  165. history );
  166. return;
  167. }
  168. /* Increment history position */
  169. history->next++;
  170. /* Prepare empty "next" slot */
  171. entry = history_entry ( history, 0 );
  172. free ( entry->string );
  173. entry->string = NULL;
  174. }
  175. /**
  176. * Clean up history after editing
  177. *
  178. * @v history History buffer
  179. */
  180. static void history_cleanup ( struct readline_history *history ) {
  181. struct readline_history_entry *entry;
  182. unsigned int i;
  183. /* Discard any temporary strings */
  184. for ( i = 0 ; i < ( sizeof ( history->entries ) /
  185. sizeof ( history->entries[0] ) ) ; i++ ) {
  186. entry = &history->entries[i];
  187. free ( entry->temp );
  188. entry->temp = NULL;
  189. }
  190. /* Reset depth */
  191. history->depth = 0;
  192. /* Sanity check */
  193. entry = history_entry ( history, 0 );
  194. assert ( entry->string == NULL );
  195. }
  196. /**
  197. * Free history buffer
  198. *
  199. * @v history History buffer
  200. */
  201. void history_free ( struct readline_history *history ) {
  202. struct readline_history_entry *entry;
  203. unsigned int i;
  204. /* Discard any temporary strings */
  205. for ( i = 0 ; i < ( sizeof ( history->entries ) /
  206. sizeof ( history->entries[0] ) ) ; i++ ) {
  207. entry = &history->entries[i];
  208. assert ( entry->temp == NULL );
  209. free ( entry->string );
  210. }
  211. }
  212. /**
  213. * Read line from console (with history)
  214. *
  215. * @v prompt Prompt string
  216. * @v prefill Prefill string, or NULL for no prefill
  217. * @v history History buffer, or NULL for no history
  218. * @ret line Line read from console (excluding terminating newline)
  219. * @ret rc Return status code
  220. *
  221. * The returned line is allocated with malloc(); the caller must
  222. * eventually call free() to release the storage.
  223. */
  224. int readline_history ( const char *prompt, const char *prefill,
  225. struct readline_history *history, char **line ) {
  226. char buf[READLINE_MAX];
  227. struct edit_string string;
  228. int key;
  229. int move_by;
  230. const char *new_string;
  231. int rc;
  232. /* Avoid returning uninitialised data on error */
  233. *line = NULL;
  234. /* Display prompt, if applicable */
  235. if ( prompt )
  236. printf ( "%s", prompt );
  237. /* Ensure cursor is visible */
  238. printf ( "\033[?25h" );
  239. /* Initialise editable string */
  240. memset ( &string, 0, sizeof ( string ) );
  241. init_editstring ( &string, buf, sizeof ( buf ) );
  242. buf[0] = '\0';
  243. /* Prefill string, if applicable */
  244. if ( prefill ) {
  245. replace_string ( &string, prefill );
  246. sync_console ( &string );
  247. }
  248. while ( 1 ) {
  249. /* Handle keypress */
  250. key = edit_string ( &string, getkey ( 0 ) );
  251. sync_console ( &string );
  252. move_by = 0;
  253. switch ( key ) {
  254. case CR:
  255. case LF:
  256. *line = strdup ( buf );
  257. rc = ( ( *line ) ? 0 : -ENOMEM );
  258. goto done;
  259. case CTRL_C:
  260. rc = -ECANCELED;
  261. goto done;
  262. case KEY_UP:
  263. move_by = 1;
  264. break;
  265. case KEY_DOWN:
  266. move_by = -1;
  267. break;
  268. default:
  269. /* Do nothing */
  270. break;
  271. }
  272. /* Handle history movement, if applicable */
  273. if ( move_by && history ) {
  274. new_string = history_move ( history, move_by, buf );
  275. if ( new_string ) {
  276. replace_string ( &string, new_string );
  277. sync_console ( &string );
  278. }
  279. }
  280. }
  281. done:
  282. putchar ( '\n' );
  283. if ( history ) {
  284. if ( *line && (*line)[0] )
  285. history_append ( history, *line );
  286. history_cleanup ( history );
  287. }
  288. assert ( ( rc == 0 ) ^ ( *line == NULL ) );
  289. return rc;
  290. }
  291. /**
  292. * Read line from console
  293. *
  294. * @v prompt Prompt string
  295. * @ret line Line read from console (excluding terminating newline)
  296. *
  297. * The returned line is allocated with malloc(); the caller must
  298. * eventually call free() to release the storage.
  299. */
  300. char * readline ( const char *prompt ) {
  301. char *line;
  302. readline_history ( prompt, NULL, NULL, &line );
  303. return line;
  304. }