Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions man/calc.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SWI-Prolog.h>

#define MAXLINE 1024

int
main(int argc, char **argv)
{ char expression[MAXLINE];
char *e = expression;
{ char *expression;
char *program = argv[0];
char *plav[2];
int n;

/* combine all the arguments in a single string */
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);
}

for(n=1; n<argc; n++)
/* 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]);
Expand All @@ -22,24 +33,27 @@ main(int argc, char **argv)

/* make the argument vector for Prolog */

plav[0] = program;
plav[1] = NULL;
int plac=0;
char *plav[2];
plav[plac++] = program;
plav[plac] = NULL;

/* initialise Prolog */

if ( !PL_initialise(1, plav) )
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");
term_t h0 = PL_new_term_refs(1);
int rval;
bool rc;
term_t h0;

PL_put_atom_chars(h0, expression);
rval = PL_call_predicate(NULL, PL_Q_NORMAL, pred, 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(rval ? 0 : 1);
PL_halt(!rc);
}

return 0;
Expand Down
Loading