kgets.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2001
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <lib.h>
  31. /*
  32. * Do a backspace in typed input.
  33. * We overwrite the current character with a space in case we're on
  34. * a terminal where backspace is nondestructive.
  35. */
  36. static
  37. void
  38. backsp(void)
  39. {
  40. putch('\b');
  41. putch(' ');
  42. putch('\b');
  43. }
  44. /*
  45. * Read a string off the console. Support a few of the more useful
  46. * common control characters. Do not include the terminating newline
  47. * in the buffer passed back.
  48. */
  49. void
  50. kgets(char *buf, size_t maxlen)
  51. {
  52. size_t pos = 0;
  53. int ch;
  54. while (1) {
  55. ch = getch();
  56. if (ch=='\n' || ch=='\r') {
  57. putch('\n');
  58. break;
  59. }
  60. /* Only allow the normal 7-bit ascii */
  61. if (ch>=32 && ch<127 && pos < maxlen-1) {
  62. putch(ch);
  63. buf[pos++] = ch;
  64. }
  65. else if ((ch=='\b' || ch==127) && pos>0) {
  66. /* backspace */
  67. backsp();
  68. pos--;
  69. }
  70. else if (ch==3) {
  71. /* ^C - return empty string */
  72. putch('^');
  73. putch('C');
  74. putch('\n');
  75. pos = 0;
  76. break;
  77. }
  78. else if (ch==18) {
  79. /* ^R - reprint input */
  80. buf[pos] = 0;
  81. kprintf("^R\n%s", buf);
  82. }
  83. else if (ch==21) {
  84. /* ^U - erase line */
  85. while (pos > 0) {
  86. backsp();
  87. pos--;
  88. }
  89. }
  90. else if (ch==23) {
  91. /* ^W - erase word */
  92. while (pos > 0 && buf[pos-1]==' ') {
  93. backsp();
  94. pos--;
  95. }
  96. while (pos > 0 && buf[pos-1]!=' ') {
  97. backsp();
  98. pos--;
  99. }
  100. }
  101. else {
  102. beep();
  103. }
  104. }
  105. buf[pos] = 0;
  106. }