nonstdio.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 1996-2005 Paul Mackerras.
  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
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/string.h>
  10. #include <asm/time.h>
  11. #include "nonstdio.h"
  12. int xmon_putchar(int c)
  13. {
  14. char ch = c;
  15. if (c == '\n')
  16. xmon_putchar('\r');
  17. return xmon_write(&ch, 1) == 1? c: -1;
  18. }
  19. static char line[256];
  20. static char *lineptr;
  21. static int lineleft;
  22. int xmon_getchar(void)
  23. {
  24. int c;
  25. if (lineleft == 0) {
  26. lineptr = line;
  27. for (;;) {
  28. c = xmon_readchar();
  29. if (c == -1 || c == 4)
  30. break;
  31. if (c == '\r' || c == '\n') {
  32. *lineptr++ = '\n';
  33. xmon_putchar('\n');
  34. break;
  35. }
  36. switch (c) {
  37. case 0177:
  38. case '\b':
  39. if (lineptr > line) {
  40. xmon_putchar('\b');
  41. xmon_putchar(' ');
  42. xmon_putchar('\b');
  43. --lineptr;
  44. }
  45. break;
  46. case 'U' & 0x1F:
  47. while (lineptr > line) {
  48. xmon_putchar('\b');
  49. xmon_putchar(' ');
  50. xmon_putchar('\b');
  51. --lineptr;
  52. }
  53. break;
  54. default:
  55. if (lineptr >= &line[sizeof(line) - 1])
  56. xmon_putchar('\a');
  57. else {
  58. xmon_putchar(c);
  59. *lineptr++ = c;
  60. }
  61. }
  62. }
  63. lineleft = lineptr - line;
  64. lineptr = line;
  65. }
  66. if (lineleft == 0)
  67. return -1;
  68. --lineleft;
  69. return *lineptr++;
  70. }
  71. char *xmon_gets(char *str, int nb)
  72. {
  73. char *p;
  74. int c;
  75. for (p = str; p < str + nb - 1; ) {
  76. c = xmon_getchar();
  77. if (c == -1) {
  78. if (p == str)
  79. return NULL;
  80. break;
  81. }
  82. *p++ = c;
  83. if (c == '\n')
  84. break;
  85. }
  86. *p = 0;
  87. return str;
  88. }
  89. void xmon_printf(const char *format, ...)
  90. {
  91. va_list args;
  92. int n;
  93. static char xmon_outbuf[1024];
  94. va_start(args, format);
  95. n = vsnprintf(xmon_outbuf, sizeof(xmon_outbuf), format, args);
  96. va_end(args);
  97. xmon_write(xmon_outbuf, n);
  98. }
  99. void xmon_puts(const char *str)
  100. {
  101. xmon_write(str, strlen(str));
  102. }