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
|
#include <u.h>
#include <libc.h>
/*
* prompt for a string with a possible default response
*/
char*
readcons(char *prompt, char *def, int raw)
{
int fdin, fdout, ctl, n;
char *s, *p;
s = p = nil;
fdout = ctl = -1;
if((fdin = open("/dev/cons", OREAD|OCEXEC)) < 0)
goto Out;
if((fdout = open("/dev/cons", OWRITE|OCEXEC)) < 0)
goto Out;
if(raw){
if((ctl = open("/dev/consctl", OWRITE|OCEXEC)) < 0)
goto Out;
write(ctl, "rawon", 5);
}
if(def != nil)
fprint(fdout, "%s[%s]: ", prompt, def);
else
fprint(fdout, "%s: ", prompt);
for(;;){
n = p - s;
if((n % 32) == 0){
if((p = realloc(s, n+32)) == nil)
break;
s = p, p += n;
}
n = read(fdin, p, 1);
if(n < 0)
break;
if(n == 0 || *p == 0x7f){
werrstr("input aborted");
break;
}
if(*p == '\n' || *p == '\r'){
if(p == s && def != nil){
free(s);
s = strdup(def);
} else
*p = 0;
if(raw)
write(fdout, "\n", 1);
goto Out;
} else if(*p == '\b') {
while(p > s && (p[-1] & 0xc0) == 0x80)
*p-- = 0;
if(p > s)
*p-- = 0;
} else if(*p == 0x15) { /* ^U: line kill */
if(def != nil)
fprint(fdout, "\n%s[%s]: ", prompt, def);
else
fprint(fdout, "\n%s: ", prompt);
while(p > s)
*p-- = 0;
} else if(*p >= ' ')
p++;
}
free(s);
s = nil;
if(raw)
write(fdout, "\n", 1);
Out:
if(ctl >= 0){
write(ctl, "rawoff", 6);
close(ctl);
}
if(fdin >= 0)
close(fdin);
if(fdout >= 0)
close(fdout);
return s;
}
|