00001 /* File: cmain.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: cmain.c,v 1.7 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 /* on Windows, be sure to set the cinterf.h include file path 00048 correctly, and also myargv[0]. When compiling this file, be sure to 00049 include the XSB_DLL or XSB_DLL_C flag as was included when the xsb.dll 00050 was compiled. */ 00051 00052 #include <stdio.h> 00053 00054 /* The following include is necessary to get the macros and routine 00055 headers */ 00056 00057 #include "cinterf.h" 00058 extern char *xsb_executable_full_path(char *); 00059 extern char *strip_names_from_path(char*, int); 00060 00061 int main(int argc, char *argv[]) 00062 { 00063 int rcode; 00064 int myargc = 3; 00065 char *myargv[3]; 00066 00067 /* xsb_init relies on the calling program to pass the absolute or relative 00068 path name of the XSB installation directory. We assume that the current 00069 program is sitting in the directory .../examples/c_calling_xsb/ 00070 To get installation directory, we strip 3 file names from the path. */ 00071 myargv[0] = strip_names_from_path(xsb_executable_full_path(argv[0]), 3); 00072 myargv[1] = "-n"; 00073 myargv[2] = "-e writeln(hello). writeln(kkk)."; 00074 00075 /* Initialize xsb */ 00076 xsb_init(myargc,myargv); /* depend on user to put in right options (-n) */ 00077 00078 /* Create command to consult a file: ctest.P, and send it. */ 00079 c2p_functor("consult",1,reg_term(1)); 00080 c2p_string("ctest",p2p_arg(reg_term(1),1)); 00081 if (xsb_command()) { 00082 printf("Error consulting ctest.P.\n"); 00083 fflush(stdout); 00084 } 00085 00086 if (xsb_command_string("consult(basics).")) { 00087 printf("Error (string) consulting basics.\n"); 00088 fflush(stdout); 00089 } 00090 00091 /* Create the query p(300,X,Y) and send it. */ 00092 c2p_functor("p",3,reg_term(1)); 00093 c2p_int(300,p2p_arg(reg_term(1),1)); 00094 00095 rcode = xsb_query(); 00096 00097 /* Print out answer and retrieve next one. */ 00098 while (!rcode) { 00099 if (!(is_string(p2p_arg(reg_term(2),1)) & 00100 is_string(p2p_arg(reg_term(2),2)))) 00101 printf("2nd and 3rd subfields must be atoms\n"); 00102 else 00103 printf("Answer: %d, %s(%s), %s(%s)\n", 00104 p2c_int(p2p_arg(reg_term(1),1)), 00105 p2c_string(p2p_arg(reg_term(1),2)), 00106 xsb_var_string(1), 00107 p2c_string(p2p_arg(reg_term(1),3)), 00108 xsb_var_string(2) 00109 ); 00110 fflush(stdout); 00111 rcode = xsb_next(); 00112 } 00113 00114 /* Create the string query p(300,X,Y) and send it, use higher-level 00115 routines. */ 00116 00117 xsb_make_vars(3); 00118 xsb_set_var_int(300,1); 00119 rcode = xsb_query_string("p(X,Y,Z)."); 00120 00121 /* Print out answer and retrieve next one. */ 00122 while (!rcode) { 00123 if (!(is_string(p2p_arg(reg_term(2),2)) & 00124 is_string(p2p_arg(reg_term(2),3)))) 00125 printf("2nd and 3rd subfields must be atoms\n"); 00126 else 00127 printf("Answer: %d, %s, %s\n", 00128 xsb_var_int(1), 00129 xsb_var_string(2), 00130 xsb_var_string(3) 00131 ); 00132 fflush(stdout); 00133 rcode = xsb_next(); 00134 } 00135 00136 00137 00138 /* Close connection */ 00139 xsb_close(); 00140 printf("cmain exit\n"); 00141 return(0); 00142 }