forked from SWI-Prolog/swipl-devel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.c
More file actions
60 lines (47 loc) · 1.19 KB
/
Copy pathcalc.c
File metadata and controls
60 lines (47 loc) · 1.19 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SWI-Prolog.h>
int
main(int argc, char **argv)
{ char *expression;
char *program = argv[0];
if ( argc < 2 )
{ fprintf(stderr, "Usage: %s expression\n", program);
exit(1);
}
/* Determine length of joined arguments */
size_t len = 1 + argc - 2;
for(int n=1; n<argc; n++)
len += strlen(argv[n]);
if ( !(expression = malloc(len)) ) // freed on exit
{ perror("allocate");
exit(1);
}
/* combine all the arguments as a single string */
char *e = expression;
for(int n=1; n<argc; n++)
{ if ( n != 1 )
*e++ = ' ';
strcpy(e, argv[n]);
e += strlen(e);
}
/* make the argument vector for Prolog */
int plac=0;
char *plav[2];
plav[plac++] = program;
plav[plac] = NULL;
/* initialise Prolog */
if ( !PL_initialise(plac, plav) )
PL_halt(1);
/* Lookup calc/1 and make the arguments and call */
{ predicate_t pred = PL_predicate("calc", 1, "user");
bool rc;
term_t h0;
rc = ( (h0=PL_new_term_refs(1)) &&
PL_unify_chars(h0, PL_STRING|REP_MB, (size_t)-1, expression) &&
PL_call_predicate(NULL, PL_Q_NORMAL, pred, h0) );
PL_halt(!rc);
}
return 0;
}