summaryrefslogtreecommitdiff
path: root/uswipl.c
diff options
context:
space:
mode:
Diffstat (limited to 'uswipl.c')
-rw-r--r--uswipl.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/uswipl.c b/uswipl.c
new file mode 100644
index 0000000..1c92515
--- /dev/null
+++ b/uswipl.c
@@ -0,0 +1,68 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <SWI-Prolog.h>
+
+int main(int argc, char **argv)
+{
+ char *input;
+ size_t input_size;
+ ssize_t input_len;
+ char *output;
+ term_t term;
+ predicate_t pred;
+ qid_t query;
+
+ if (!PL_initialise(argc, argv))
+ return EXIT_FAILURE;
+
+ input = NULL;
+ while (errno = 0, (input_len = getline(&input, &input_size, stdin)) != -1) {
+ switch (input[0]) {
+ case '!':
+ if ((term = PL_new_term_ref()) == (term_t)0)
+ return EXIT_FAILURE;
+
+ if (PL_put_term_from_chars(term, REP_UTF8 | CVT_EXCEPTION, -1, &input[1]) == FALSE)
+ return EXIT_FAILURE;
+
+ if (PL_assert(term, NULL, PL_ASSERTZ) == FALSE)
+ return EXIT_FAILURE;
+
+ break;
+
+ case '?':
+ input[input_len - 1] = '\0';
+ if ((pred = PL_predicate(&input[1], 1, NULL)) == (predicate_t)0)
+ return EXIT_FAILURE;
+
+ if ((term = PL_new_term_ref()) == (term_t)0)
+ return EXIT_FAILURE;
+
+ query = PL_open_query(NULL, PL_Q_NODEBUG, pred, term);
+ while (PL_next_solution(query) == TRUE) {
+ if (PL_get_chars(term, &output, CVT_ALL | CVT_VARIABLE | CVT_EXCEPTION | BUF_MALLOC | REP_UTF8) == FALSE)
+ return EXIT_FAILURE;
+ puts(output);
+ PL_free(output);
+ }
+ PL_close_query(query);
+
+ break;
+
+ default:
+ fputs("Unexpected input\n", stderr);
+ return EXIT_FAILURE;
+ }
+ }
+
+ free(input);
+
+ if (errno) {
+ perror("Failed to read line from standard input");
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}