nonstdio.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/udbg.h>
  11. #include <asm/time.h>
  12. #include "nonstdio.h"
  13. static int xmon_write(const void *ptr, int nb)
  14. {
  15. return udbg_write(ptr, nb);
  16. }
  17. static int xmon_readchar(void)
  18. {
  19. if (udbg_getc)
  20. return udbg_getc();
  21. return -1;
  22. }
  23. int xmon_putchar(int c)
  24. {
  25. char ch = c;
  26. if (c == '\n')
  27. xmon_putchar('\r');
  28. return xmon_write(&ch, 1) == 1? c: -1;
  29. }
  30. static char line[256];
  31. static char *lineptr;
  32. static int lineleft;
  33. static int xmon_getchar(void)
  34. {
  35. int c;
  36. if (lineleft == 0) {
  37. lineptr = line;
  38. for (;;) {
  39. c = xmon_readchar();
  40. if (c == -1 || c == 4)
  41. break;
  42. if (c == '\r' || c == '\n') {
  43. *lineptr++ = '\n';
  44. xmon_putchar('\n');
  45. break;
  46. }
  47. switch (c) {
  48. case 0177:
  49. case '\b':
  50. if (lineptr > line) {
  51. xmon_putchar('\b');
  52. xmon_putchar(' ');
  53. xmon_putchar('\b');
  54. --lineptr;
  55. }
  56. break;
  57. case 'U' & 0x1F:
  58. while (lineptr > line) {
  59. xmon_putchar('\b');
  60. xmon_putchar(' ');
  61. xmon_putchar('\b');
  62. --lineptr;
  63. }
  64. break;
  65. default:
  66. if (lineptr >= &line[sizeof(line) - 1])
  67. xmon_putchar('\a');
  68. else {
  69. xmon_putchar(c);
  70. *lineptr++ = c;
  71. }
  72. }
  73. }
  74. lineleft = lineptr - line;
  75. lineptr = line;
  76. }
  77. if (lineleft == 0)
  78. return -1;
  79. --lineleft;
  80. return *lineptr++;
  81. }
  82. char *xmon_gets(char *str, int nb)
  83. {
  84. char *p;
  85. int c;
  86. for (p = str; p < str + nb - 1; ) {
  87. c = xmon_getchar();
  88. if (c == -1) {
  89. if (p == str)
  90. return NULL;
  91. break;
  92. }
  93. *p++ = c;
  94. if (c == '\n')
  95. break;
  96. }
  97. *p = 0;
  98. return str;
  99. }
  100. void xmon_printf(const char *format, ...)
  101. {
  102. va_list args;
  103. static char xmon_outbuf[1024];
  104. int rc, n;
  105. va_start(args, format);
  106. n = vsnprintf(xmon_outbuf, sizeof(xmon_outbuf), format, args);
  107. va_end(args);
  108. rc = xmon_write(xmon_outbuf, n);
  109. if (n && rc == 0) {
  110. /* No udbg hooks, fallback to printk() - dangerous */
  111. printk(xmon_outbuf);
  112. }
  113. }
  114. void xmon_puts(const char *str)
  115. {
  116. xmon_write(str, strlen(str));
  117. }