Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <console.h>
  22. #include <gpxe/keys.h>
  23. #include <gpxe/editstring.h>
  24. #include <readline/readline.h>
  25. /** @file
  26. *
  27. * Minimal readline
  28. *
  29. */
  30. #define READLINE_MAX 256
  31. /**
  32. * Synchronise console with edited string
  33. *
  34. * @v string Editable string
  35. */
  36. static void sync_console ( struct edit_string *string ) {
  37. unsigned int mod_start = string->mod_start;
  38. unsigned int mod_end = string->mod_end;
  39. unsigned int cursor = string->last_cursor;
  40. size_t len = strlen ( string->buf );
  41. /* Expand region back to old cursor position if applicable */
  42. if ( mod_start > string->last_cursor )
  43. mod_start = string->last_cursor;
  44. /* Expand region forward to new cursor position if applicable */
  45. if ( mod_end < string->cursor )
  46. mod_end = string->cursor;
  47. /* Backspace to start of region */
  48. while ( cursor > mod_start ) {
  49. putchar ( '\b' );
  50. cursor--;
  51. }
  52. /* Print modified region */
  53. while ( cursor < mod_end ) {
  54. putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
  55. cursor++;
  56. }
  57. /* Backspace to new cursor position */
  58. while ( cursor > string->cursor ) {
  59. putchar ( '\b' );
  60. cursor--;
  61. }
  62. }
  63. /**
  64. * Read line from console
  65. *
  66. * @v prompt Prompt string
  67. * @ret line Line read from console (excluding terminating newline)
  68. *
  69. * The returned line is allocated with malloc(); the caller must
  70. * eventually call free() to release the storage.
  71. */
  72. char * readline ( const char *prompt ) {
  73. char buf[READLINE_MAX];
  74. struct edit_string string;
  75. int key;
  76. char *line;
  77. if ( prompt )
  78. printf ( "%s", prompt );
  79. memset ( &string, 0, sizeof ( string ) );
  80. string.buf = buf;
  81. string.len = sizeof ( buf );
  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. }