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