start.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  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 <stdarg.h>
  10. #include "of1275.h"
  11. extern int strlen(const char *s);
  12. extern void boot(int a1, int a2, void *prom);
  13. phandle stdin;
  14. phandle stdout;
  15. phandle stderr;
  16. void printk(char *fmt, ...);
  17. void
  18. start(int a1, int a2, void *promptr)
  19. {
  20. ofinit(promptr);
  21. if (ofstdio(&stdin, &stdout, &stderr))
  22. exit();
  23. boot(a1, a2, promptr);
  24. for (;;)
  25. exit();
  26. }
  27. int writestring(void *f, char *ptr, int nb)
  28. {
  29. int w = 0, i;
  30. char *ret = "\r";
  31. for (i = 0; i < nb; ++i) {
  32. if (ptr[i] == '\n') {
  33. if (i > w) {
  34. write(f, ptr + w, i - w);
  35. w = i;
  36. }
  37. write(f, ret, 1);
  38. }
  39. }
  40. if (w < nb)
  41. write(f, ptr + w, nb - w);
  42. return nb;
  43. }
  44. int
  45. putc(int c, void *f)
  46. {
  47. char ch = c;
  48. return writestring(f, &ch, 1) == 1? c: -1;
  49. }
  50. int
  51. putchar(int c)
  52. {
  53. return putc(c, stdout);
  54. }
  55. int
  56. fputs(char *str, void *f)
  57. {
  58. int n = strlen(str);
  59. return writestring(f, str, n) == n? 0: -1;
  60. }
  61. int
  62. readchar(void)
  63. {
  64. char ch;
  65. for (;;) {
  66. switch (read(stdin, &ch, 1)) {
  67. case 1:
  68. return ch;
  69. case -1:
  70. printk("read(stdin) returned -1\n");
  71. return -1;
  72. }
  73. }
  74. }
  75. static char line[256];
  76. static char *lineptr;
  77. static int lineleft;
  78. int
  79. getchar(void)
  80. {
  81. int c;
  82. if (lineleft == 0) {
  83. lineptr = line;
  84. for (;;) {
  85. c = readchar();
  86. if (c == -1 || c == 4)
  87. break;
  88. if (c == '\r' || c == '\n') {
  89. *lineptr++ = '\n';
  90. putchar('\n');
  91. break;
  92. }
  93. switch (c) {
  94. case 0177:
  95. case '\b':
  96. if (lineptr > line) {
  97. putchar('\b');
  98. putchar(' ');
  99. putchar('\b');
  100. --lineptr;
  101. }
  102. break;
  103. case 'U' & 0x1F:
  104. while (lineptr > line) {
  105. putchar('\b');
  106. putchar(' ');
  107. putchar('\b');
  108. --lineptr;
  109. }
  110. break;
  111. default:
  112. if (lineptr >= &line[sizeof(line) - 1])
  113. putchar('\a');
  114. else {
  115. putchar(c);
  116. *lineptr++ = c;
  117. }
  118. }
  119. }
  120. lineleft = lineptr - line;
  121. lineptr = line;
  122. }
  123. if (lineleft == 0)
  124. return -1;
  125. --lineleft;
  126. return *lineptr++;
  127. }
  128. extern int vsprintf(char *buf, const char *fmt, va_list args);
  129. static char sprint_buf[1024];
  130. void
  131. printk(char *fmt, ...)
  132. {
  133. va_list args;
  134. int n;
  135. va_start(args, fmt);
  136. n = vsprintf(sprint_buf, fmt, args);
  137. va_end(args);
  138. writestring(stdout, sprint_buf, n);
  139. }
  140. int
  141. printf(char *fmt, ...)
  142. {
  143. va_list args;
  144. int n;
  145. va_start(args, fmt);
  146. n = vsprintf(sprint_buf, fmt, args);
  147. va_end(args);
  148. writestring(stdout, sprint_buf, n);
  149. return n;
  150. }