You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readline.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <console.h>
  21. #include <gpxe/keys.h>
  22. #include <gpxe/editstring.h>
  23. #include <readline/readline.h>
  24. /** @file
  25. *
  26. * Minimal readline
  27. *
  28. */
  29. #define READLINE_MAX 256
  30. /**
  31. * Synchronise console with edited string
  32. *
  33. * @v string Editable string
  34. */
  35. static void sync_console ( struct edit_string *string ) {
  36. unsigned int mod_start = string->mod_start;
  37. unsigned int mod_end = string->mod_end;
  38. unsigned int cursor = string->last_cursor;
  39. size_t len = strlen ( string->buf );
  40. /* Expand region back to old cursor position if applicable */
  41. if ( mod_start > string->last_cursor )
  42. mod_start = string->last_cursor;
  43. /* Expand region forward to new cursor position if applicable */
  44. if ( mod_end < string->cursor )
  45. mod_end = string->cursor;
  46. /* Backspace to start of region */
  47. while ( cursor > mod_start ) {
  48. putchar ( '\b' );
  49. cursor--;
  50. }
  51. /* Print modified region */
  52. while ( cursor < mod_end ) {
  53. putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
  54. cursor++;
  55. }
  56. /* Backspace to new cursor position */
  57. while ( cursor > string->cursor ) {
  58. putchar ( '\b' );
  59. cursor--;
  60. }
  61. }
  62. /**
  63. * Read line from console
  64. *
  65. * @v prompt Prompt string
  66. * @ret line Line read from console (excluding terminating newline)
  67. *
  68. * The returned line is allocated with malloc(); the caller must
  69. * eventually call free() to release the storage.
  70. */
  71. char * readline ( const char *prompt ) {
  72. char buf[READLINE_MAX];
  73. struct edit_string string = {
  74. .buf = buf,
  75. .len = sizeof ( buf ),
  76. .cursor = 0,
  77. };
  78. int key;
  79. char *line;
  80. if ( prompt )
  81. printf ( "%s", prompt );
  82. buf[0] = '\0';
  83. while ( 1 ) {
  84. key = edit_string ( &string, getkey() );
  85. sync_console ( &string );
  86. switch ( key ) {
  87. case CR:
  88. case LF:
  89. putchar ( '\n' );
  90. line = strdup ( buf );
  91. if ( ! line )
  92. printf ( "Out of memory\n" );
  93. return line;
  94. case CTRL_C:
  95. putchar ( '\n' );
  96. return NULL;
  97. default:
  98. /* Do nothing */
  99. break;
  100. }
  101. }
  102. }