console.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. IOQ consiq;
  8. IOQ consoq;
  9. static int useuart;
  10. int debug = 0;
  11. void
  12. kbdchar(int c)
  13. {
  14. c &= 0x7F;
  15. if(c == 0x10)
  16. warp86("\n^P\n", 0);
  17. if(c == 0x12)
  18. debug = !debug;
  19. consiq.putc(&consiq, c);
  20. }
  21. static int
  22. consputc(void)
  23. {
  24. return consoq.getc(&consoq);
  25. }
  26. void
  27. kbdinit(void)
  28. {
  29. i8042init();
  30. qinit(&consiq);
  31. }
  32. void
  33. consinit(char* name, char* speed)
  34. {
  35. int baud, port;
  36. if(name == nil || cistrcmp(name, "cga") == 0)
  37. return;
  38. port = strtoul(name, 0, 0);
  39. if(port < 0 || port > 1)
  40. return;
  41. if(speed == nil || (baud = strtoul(speed, 0, 0)) == 0)
  42. baud = 9600;
  43. qinit(&consoq);
  44. uartspecial(port, kbdchar, consputc, baud);
  45. useuart = 1;
  46. uartputs(&consoq, "\n", 1);
  47. }
  48. void
  49. consdrain(void)
  50. {
  51. if(useuart)
  52. uartdrain();
  53. }
  54. void
  55. consputs(char* s, int n)
  56. {
  57. cgascreenputs(s, n);
  58. if(useuart)
  59. uartputs(&consoq, s, n);
  60. }
  61. void
  62. warp86(char* s, ulong)
  63. {
  64. if(s != nil)
  65. print(s);
  66. spllo();
  67. consdrain();
  68. i8042reset();
  69. print("Takes a licking and keeps on ticking...\n");
  70. for(;;)
  71. idle();
  72. }
  73. static int
  74. getline(char *buf, int size, int timeout)
  75. {
  76. int c, i=0;
  77. ulong start;
  78. char echo;
  79. for (;;) {
  80. start = m->ticks;
  81. do{
  82. /* timeout seconds to first char */
  83. if(timeout && ((m->ticks - start) > timeout*HZ))
  84. return -2;
  85. c = consiq.getc(&consiq);
  86. }while(c == -1);
  87. timeout = 0;
  88. if(c == '\r')
  89. c = '\n'; /* turn carriage return into newline */
  90. if(c == '\177')
  91. c = '\010'; /* turn delete into backspace */
  92. if(c == '\025')
  93. echo = '\n'; /* echo ^U as a newline */
  94. else
  95. echo = c;
  96. consputs(&echo, 1);
  97. if(c == '\010'){
  98. if(i > 0)
  99. i--; /* bs deletes last character */
  100. continue;
  101. }
  102. /* a newline ends a line */
  103. if (c == '\n')
  104. break;
  105. /* ^U wipes out the line */
  106. if (c =='\025')
  107. return -1;
  108. if(i == size)
  109. return size;
  110. buf[i++] = c;
  111. }
  112. buf[i] = 0;
  113. return i;
  114. }
  115. int
  116. getstr(char *prompt, char *buf, int size, char *def, int timeout)
  117. {
  118. int len, isdefault;
  119. char pbuf[PRINTSIZE];
  120. buf[0] = 0;
  121. isdefault = (def && *def);
  122. if(isdefault == 0){
  123. timeout = 0;
  124. sprint(pbuf, "%s: ", prompt);
  125. }
  126. else if(timeout)
  127. sprint(pbuf, "%s[default==%s (%ds timeout)]: ", prompt, def, timeout);
  128. else
  129. sprint(pbuf, "%s[default==%s]: ", prompt, def);
  130. for (;;) {
  131. print(pbuf);
  132. len = getline(buf, size, timeout);
  133. switch(len){
  134. case 0:
  135. /* RETURN */
  136. if(isdefault)
  137. break;
  138. continue;
  139. case -1:
  140. /* ^U typed */
  141. continue;
  142. case -2:
  143. /* timeout, use default */
  144. consputs("\n", 1);
  145. len = 0;
  146. break;
  147. default:
  148. break;
  149. }
  150. if(len >= size){
  151. print("line too long\n");
  152. continue;
  153. }
  154. break;
  155. }
  156. if(len == 0 && isdefault)
  157. strcpy(buf, def);
  158. return 0;
  159. }
  160. int
  161. sprint(char *s, char *fmt, ...)
  162. {
  163. return donprint(s, s+PRINTSIZE, fmt, (&fmt+1)) - s;
  164. }
  165. int
  166. snprint(char *s, int n, char *fmt, ...)
  167. {
  168. return donprint(s, s+n, fmt, (&fmt+1)) - s;
  169. }
  170. int
  171. print(char *fmt, ...)
  172. {
  173. char buf[PRINTSIZE];
  174. int n;
  175. n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
  176. consputs(buf, n);
  177. return n;
  178. }
  179. void
  180. panic(char *fmt, ...)
  181. {
  182. int n;
  183. char buf[PRINTSIZE];
  184. consputs("panic: ", 7);
  185. n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
  186. consputs(buf, n);
  187. consputs("\n", 1);
  188. //floppymemwrite();
  189. //splhi(); for(;;);
  190. if(etherdetach)
  191. etherdetach();
  192. if(sddetach)
  193. sddetach();
  194. consputs("\nPress almost any key to reset...", 32);
  195. spllo();
  196. while(consiq.getc(&consiq) == -1)
  197. ;
  198. warp86(nil, 0);
  199. }