cmain2.c

00001 /* File:      cmain2.c
00002 ** Author(s): David S. Warren
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: cmain2.c,v 1.6 2000/06/08 06:50:38 kifer Exp $
00022 ** 
00023 */
00024 
00025 /***   Simple example file showing how to call XSB from C   ***/
00026 
00027 /*
00028  * This file contains the C "main()" function.  To create an executable,
00029  * link this with the XSB code:
00030  * 1. compile cmain to create cmain.o
00031  * 2. build the regular xsb system
00032  *      (really just interested in the object files)
00033  * 3. link together cmain.o, from step (1) above, with XSB's object
00034  *    files, from step (2) above, EXCEPT for the file xmain.o--this is
00035  *    used to create a "standard" XSB executable--into a new executable,
00036  *    say, cmain.  Remember to include any necessary linking options.
00037  *    For example, here's how one would create an executable for a
00038  *    Sun Microsystems machine running Solaris:
00039  *      gcc -o cmain cmain.o <XSB object files> -lm -lnsl -ldl -lsocket
00040  *
00041  * A good idea would be to look at the make file in this directory.
00042  * Note: the XSB executable must be in the directory
00043  *            <xsb install dir>/config/<your architecture>/bin/
00044  *
00045  */
00046 
00047 #include <stdio.h>
00048 
00049 
00050 /* The following include is necessary to get the macros and routine
00051    headers */
00052 
00053 #include "cinterf.h"
00054 extern char *xsb_executable_full_path(char *);
00055 extern char *strip_names_from_path(char*, int);
00056 
00057 int getline(s, lim)
00058 char s[];
00059 int lim;
00060 {
00061   int c=0, i;
00062   for (i=0; i<lim-1 && (c=getc(stdin))>=0 && c!='\n'; ++i)
00063     s[i] = c;
00064   if (c == '\n') {
00065     s[i] = c;
00066     ++i;
00067   }
00068   s[i] = '\0';
00069   return(i);
00070 }
00071 
00072 void printpterm(prolog_term term)
00073 {
00074   int i;
00075 
00076   if (is_var(term)) fprintf(stdout,"_%p", (void *)term);
00077   else if (is_int(term)) fprintf(stdout,"%d",p2c_int(term));
00078   else if (is_float(term)) fprintf(stdout,"%f",p2c_float(term));
00079   else if (is_nil(term)) fprintf(stdout,"[]");
00080   else if (is_string(term)) fprintf(stdout,"%s",p2c_string(term));
00081   else if (is_list(term)) {
00082     fprintf(stdout,"[");
00083     printpterm(p2p_car(term));
00084     term = p2p_cdr(term);
00085     while (is_list(term)) {
00086       fprintf(stdout,","),
00087       printpterm(p2p_car(term));
00088       term = p2p_cdr(term);
00089     }
00090     if (!is_nil(term)) {
00091       fprintf(stdout,"|");
00092       printpterm(term);
00093     }
00094     fprintf(stdout,"]");
00095   } else if (is_functor(term)) {
00096     fprintf(stdout,"%s",p2c_functor(term));
00097     if (p2c_arity(term) > 0) {
00098       fprintf(stdout,"(");
00099       printpterm(p2p_arg(term,1));
00100       for (i = 2; i <= p2c_arity(term); i++) {
00101         fprintf(stdout,",");
00102         printpterm(p2p_arg(term,i));
00103       }
00104       fprintf(stdout,")");
00105     }
00106   } else fprintf(stdout,"error, unrecognized type");
00107 }
00108 
00109 int main(int argc, char *argv[])
00110 { 
00111   int rcode;
00112   int myargc = 2;
00113   char *myargv[2];
00114   char query[256];
00115 
00116   /* xsb_init relies on the calling program to pass the absolute or relative
00117      path name of the XSB installation directory. We assume that the current
00118      program is sitting in the directory
00119      .../examples/c_calling_xsb/
00120      To get installation directory, we strip 3 file names from the path. */
00121   myargv[0] = strip_names_from_path(xsb_executable_full_path(argv[0]), 3);
00122   myargv[1] = "-n";
00123 
00124   /* Initialize xsb */
00125   xsb_init(myargc,myargv);  /* depend on user to put in right options (-n) */
00126 
00127   /* Create command to consult a file: ctest.P, and send it. */
00128   c2p_functor("consult",1,reg_term(1));
00129   c2p_string("ctest",p2p_arg(reg_term(1),1));
00130   if (xsb_command()) printf("Error consulting ctest.P.\n");
00131 
00132   if (xsb_command_string("consult(basics).")) 
00133     printf("Error (string) consulting basics.\n");
00134 
00135   /* Create the query p(300,X,Y) and send it. */
00136   c2p_functor("p",3,reg_term(1));
00137   c2p_int(300,p2p_arg(reg_term(1),1));
00138 
00139   rcode = xsb_query();
00140 
00141   /* Print out answer and retrieve next one. */
00142   while (!rcode) {
00143     if (!(is_string(p2p_arg(reg_term(2),1)) & 
00144           is_string(p2p_arg(reg_term(2),2))))
00145        printf("2nd and 3rd subfields must be atoms\n");
00146     else
00147       printf("Answer: %d, %s(%s), %s(%s)\n",
00148              p2c_int(p2p_arg(reg_term(1),1)),
00149              p2c_string(p2p_arg(reg_term(1),2)),
00150              xsb_var_string(1),
00151              p2c_string(p2p_arg(reg_term(1),3)),
00152              xsb_var_string(2)
00153              );
00154     rcode = xsb_next();
00155   }
00156 
00157   /* Create the string query p(300,X,Y) and send it, use higher-level
00158      routines. */
00159 
00160   xsb_make_vars(3);
00161   xsb_set_var_int(300,1);
00162   rcode = xsb_query_string("p(X,Y,Z).");
00163 
00164   /* Print out answer and retrieve next one. */
00165   while (!rcode) {
00166     if (!(is_string(p2p_arg(reg_term(2),2)) & 
00167           is_string(p2p_arg(reg_term(2),3))))
00168        printf("2nd and 3rd subfields must be atoms\n");
00169     else
00170       printf("Answer: %d, %s, %s\n",
00171              xsb_var_int(1),
00172              xsb_var_string(2),
00173              xsb_var_string(3)
00174              );
00175     rcode = xsb_next();
00176   }
00177 
00178   /* read query from user, call, and print answers */
00179   fprintf(stdout,"Enter Prolog queries (^d for eof to exit)\n");
00180   fprintf(stdout,"| ?- ");
00181   getline(query,255);
00182   while (query[0] != '\0') {
00183     rcode = xsb_query_string(query);
00184     while (!rcode) {
00185       printpterm(reg_term(2));
00186       fprintf(stdout,"\n");
00187       rcode = xsb_next();
00188     }
00189     fprintf(stdout,"| ?- ");
00190     getline(query,255);
00191   }
00192 
00193 
00194   /* Close connection */
00195   xsb_close();
00196   printf("cmain exit\n");
00197   return(0);
00198 }

Generated on Wed Jul 26 13:30:43 2006 for XSB by  doxygen 1.4.5