shithub: pprolog

ref: 66a7040df6897e60ee1a513b95f4b04e687bbf0a
dir: /repl.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "dat.h"
#include "fns.h"

Rune parsefindmore(int);

void
repl(Term *database)
{
	int fd = 0; /* Standard input */
	while(1){
		print("?- ");
		Term *query = parse(fd, nil, 1);
		Binding *bindings = nil;
		int success;
FindMore:
		success = evalquery(database, query, &bindings);
		if(success == 0)
			print("false.\n");
		else{
			if(bindings == nil)
				print("true.\n");
			else{
				while(bindings){
					print("  %S = %S%s", 
						bindings->name, 
						prettyprint(bindings->value, 0, 0, 0), 
						bindings->next ? " ,\n" : "");
					bindings = bindings->next;
				}
			}
			if(choicestack != nil){
				print(" ");
				if(parsefindmore(fd) == L';'){
					print(";\n");
					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;
}