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>
#include <bio.h>
#include "dat.h"
#include "fns.h"
Rune parsefindmore(int);
void dogc(void);
void
repl(void)
{
int fd = 0; /* Standard input */
while(1){
print("?- ");
replquery = parse(fd, nil, 1);
replbindings = nil;
choicestack = nil;
goalstack = nil;
int success;
int firsttime = 1;
FindMore:
success = evalquery(replquery, &replbindings);
dogc();
if(firsttime){
print(" ");
firsttime = 0;
}
if(success == 0)
print(" false.\n");
else{
if(replbindings == nil)
print(" true");
else{
while(replbindings){
print(" %S = %S%s",
replbindings->name,
prettyprint(replbindings->value, 0, 0, 0, nil),
replbindings->next ? ",\n " : "");
replbindings = replbindings->next;
}
}
if(choicestack != nil){
print("\n");
if(parsefindmore(fd) == L';'){
print(";");
goto FindMore;
}else
print(".\n");
}else{
print(".\n");
}
}
}
}
Rune
parsefindmore(int fd)
{
int consctl = open("/dev/consctl", OWRITE);
if(consctl > 0)
write(consctl, "rawon", 5);
else{
print("Could not open /dev/consctl\n");
exits("open");
}
fd = dup(fd, -1);
Biobuf *input = Bfdopen(fd, OREAD);
Rune peek = Bgetrune(input);
Bterm(input);
if(consctl > 0){
write(consctl, "rawoff", 6);
close(consctl);
}
return peek;
}
void
dogc(void)
{
vlong amount = collectgarbage();
if(amount != 0 && debug)
print("Collected %lld bytes of garbage\n", amount);
}
|