summaryrefslogtreecommitdiff
path: root/os/boot/rpcg/console.c
blob: 9a05c122027f54e0a8c9b04689b781a7ece6a9c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "u.h"
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"

static Queue*	consiq;
static Queue*	consoq;

void
bothputs(char *s, int n)
{
	uartputs(s, n);
	screenputs(s, n);
}

static void (*consputs)(char*, int) = bothputs;	/* or screenputs */

void
consinit(void)
{
	char *p;
	int baud, port;
	static int cgadone;

	p = getconf("console");
	if(0)
	if(p == 0 || strcmp(p, "lcd") == 0 || strcmp(p, "screen") == 0){
		consiq = qopen(4*1024, 0, 0, 0);
		consoq = qopen(8*1024, 0, 0, 0);
		consputs = screenputs;
		return;
	}
	if(p!=0 && strstr(p, "lcd") == 0)
		consputs = bothputs;
	else
		consputs = uartputs;
//consputs = screenputs;
	port = 0;
	if(p)
		port = strtoul(p, 0, 0);
	baud = 0;
	if(p = getconf("baud"))
		baud = strtoul(p, 0, 0);
	if(baud == 0)
		baud = 9600;
	uartspecial(port, baud, &consiq, &consoq, kbdchar);
}

void
kbdchar(Queue *q, int c)
{
	c &= 0x7F;
	if(c == 0x10)
		panic("^p");
	qbputc(q, c);
}

static int
getline(char *buf, int size, int dotimeout)
{
	int c, i=0;
	ulong start;
	char echo;

	for (;;) {
		start = m->ticks;
		do{
			if(dotimeout && ((m->ticks - start) > 5*HZ))
				return -2;
			c = qbgetc(consiq);
		}while(c == -1);
		if(c == '\r')
			c = '\n'; 		/* turn carriage return into newline */
		if(c == '\177')
			c = '\010';		/* turn delete into backspace */
		if(c == '\025')
			echo = '\n';		/* echo ^U as a newline */
		else
			echo = c;
		(*consputs)(&echo, 1);

		if(c == '\010'){
			if(i > 0)
				i--; /* bs deletes last character */
			continue;
		}
		/* a newline ends a line */
		if (c == '\n')
			break;
		/* ^U wipes out the line */
		if (c =='\025')
			return -1;
		if(i == size)
			return size;
		buf[i++] = c;
	}
	buf[i] = 0;
	return i;
}

int
getstr(char *prompt, char *buf, int size, char *def)
{
	int len, isdefault;

	buf[0] = 0;
	isdefault = (def && *def);
	for (;;) {
		if(isdefault)
			print("%s[default==%s]: ", prompt, def);
		else
			print("%s: ", prompt);
		len = getline(buf, size, isdefault);
		switch(len){
		case -1:
			/* ^U typed */
			continue;
		case -2:
			/* timeout, use default */
			(*consputs)("\n", 1);
			len = 0;
			break;
		default:
			break;
		}
		if(len >= size){
			print("line too long\n");
			continue;
		}
		break;
	}
	if(len == 0 && isdefault)
		strcpy(buf, def);
	return 0;
}

int
sprint(char *s, char *fmt, ...)
{
	return donprint(s, s+PRINTSIZE, fmt, (&fmt+1)) - s;
}

int
print(char *fmt, ...)
{
	char buf[PRINTSIZE];
	int n;

	if(consputs == 0)
		return 0;
	n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
	(*consputs)(buf, n);
	return n;
}

void
panic(char *fmt, ...)
{
	char buf[PRINTSIZE];
	int n;

	if(consputs){
		(*consputs)("panic: ", 7);
		n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
		(*consputs)(buf, n);
		(*consputs)("\n", 1);
	}
	spllo();
	for(;;)
		idle();
}