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.8KB

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