

<!DOCTYPE html>
<!-- Server: sfs-forge-4 -->


    

















<html lang="en" class="no-js">
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
    <title>
  XSB / Src  (SVN)-old /
  [r5501]
  /trunk/XSB/docs/userman/ccallingxsb.tex
</title>
    

<meta name="viewport" content="width=1100">


<link rel="icon" sizes="180x180" href="https://a.fsdn.com/con/img/sandiego/logo-180x180.png" type="image/png">
<link rel="icon" sizes="any" href="https://a.fsdn.com/con/img/sandiego/svg/originals/sf-icon-orange-no_sf.svg" type="image/svg+xml">
<link rel="apple-touch-icon" sizes="180x180" href="https://a.fsdn.com/con/img/sandiego/logo-180x180.png">
<link rel="mask-icon" href="https://a.fsdn.com/con/img/sandiego/svg/originals/sf-icon-orange-no_sf.svg" color="#FF6600">


<script src="https://a.fsdn.com/allura/nf/1751484103/_ew_/theme/sftheme/js/sftheme/cmp2.js"></script>






    
    
    
    
    
    
    
    
    <link rel="stylesheet" href="https://a.fsdn.com/con/css/lato.css?">


<script>
if (!window.SF) { window.SF = {}; }
SF.sandiego = false;
SF.sandiego_chrome = true;
SF.cdn = "https://a.fsdn.com/con";
SF.fpid = '294352d4-9189-4c6e-95fd-f9e92fd04baf';
SF.billboard_route = '/software/product/$slug/';
</script>
    
        <!-- ew:head_css -->

    
        <link rel="stylesheet"
                type="text/css"
                href="https://a.fsdn.com/allura/nf/1751484103/_ew_/_slim/css?href=allura%2Fcss%2Fforge%2Fhilite.css%3Ballura%2Fcss%2Fforge%2Ftooltipster.css"
                >
    
        <link rel="stylesheet"
                type="text/css"
                href="https://a.fsdn.com/allura/nf/1751484103/_ew_/allura/css/font-awesome.min.css"
                >
    
        <link rel="stylesheet"
                type="text/css"
                href="https://a.fsdn.com/allura/nf/1751484103/_ew_/theme/sftheme/css/forge.css"
                >
    
        
<!-- /ew:head_css -->

    
    
        <!-- ew:head_js -->

    
        <script type="text/javascript" src="https://a.fsdn.com/allura/nf/1751484103/_ew_/_slim/js?href=allura%2Fjs%2Fjquery-3.6.0.min.js%3Ballura%2Fjs%2Fjquery.cookie-1.4.1.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fshared_head.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Ftypescript%2Fcompliance.js"></script>
    
        
<!-- /ew:head_js -->

    

    
        <style type="text/css">
            /* make URL &amp; Project custom fields in support tickets not get hidden as much */
.project-forge.mountpoint-site-support .view_holder .grid-4:nth-child(5),
.project-forge.mountpoint-site-support .view_holder .grid-4:nth-child(6) {
  overflow: visible;
  font-size: 22px;
  width: 340px;
  line-height: 1;
  margin-bottom: 5px;
}
.project-forge.mountpoint-site-support .view_holder .grid-4:nth-child(5) label, .project-forge.mountpoint-site-support .view_holder .grid-4:nth-child(6) label {
  font-size: 14px;
}
        </style>
    
    
    <link rel="alternate" type="application/rss+xml" title="RSS" href="/p/xsb/src/feed.rss"/>
    <link rel="alternate" type="application/atom+xml" title="Atom" href="/p/xsb/src/feed.atom"/>
    <style type="text/css">
        #access_urls .btn-set {
            
            min-width: 14em;
        }
    </style>

    <style>.XYRDkfCiRpONsOwFEbVQqRGEGNCzCtw {
        display: none
    }</style>

    
    
    
        <link rel="canonical" href="https://sourceforge.net/p/xsb/src/5501/tree/trunk/XSB/docs/userman/ccallingxsb.tex" />
    


    

<script>
    var data = {hem: null };
    bizx.uids.gatherAndClean(data);
</script>



<script>
    function initPiwik(){
      var _paq = window._paq = window._paq || [];
      _paq.push(['trackPageView', document.title, {
            dimension1: 'xsb',
            dimension2: 'svn'
      }]);
      _paq.push(['enableLinkTracking']);
      (function() {
        var u="//analytics.slashdotmedia.com/";
        _paq.push(['setTrackerUrl', u+'sf.php']);
        _paq.push(['setSiteId', 39]);
        
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
        g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'sf.js'; s.parentNode.insertBefore(g,s);
      })();
    }
    bizx.cmp.ifConsent({ purposes: ['storage', 'measurement'], vendors: 'sdm'}, initPiwik);
</script>






<!-- Hubspot tracking -->






 <script>
    $(function() {
        bizx.cmp.ifConsent({ purposes: 'all' , vendors: 'narrative'}, function() {
            var current_time = (new Date()).getTime();
            let imageUrl = "https://io.narrative.io/?companyId=2440&id=first_party%3A294352d4-9189-4c6e-95fd-f9e92fd04baf&id=site_name%3Asourceforge.net&id=url%3A%2Fp%2Fxsb%2Fsrc%2F5501%2Ftree%2Ftrunk%2FXSB%2Fdocs%2Fuserman%2Fccallingxsb.tex&id=pagetitle%3A%24PAGE_TITLE&id=vertical%3AOpen+Source+Software&id=product%3AXSB";
            imageUrl = imageUrl.replace(encodeURIComponent("$PAGE_TITLE"), document.title);  

            imageUrl = URL.parse(imageUrl);
            let hem = bizx.uids.getHem();
            if (hem) {
                imageUrl.searchParams.append('id', 'hem:' + hem);
            }
            imageUrl.searchParams.append('rand', current_time);

            var image = new Image();
            image.src = imageUrl.toString();
            image.style.display = "none";
            image.style.height = 0;
            image.style.width = 0;
            document.body.appendChild(image);
        });
    });
</script>



</head>

<body class="
body_class sandiego_chrome  legacy_chrome  l-allow-natural-width 
" id="forge">

    
        <!-- ew:body_top_js -->

    
        
<!-- /ew:body_top_js -->

    





<div class="off-canvas position-right" id="offCanvas" data-off-canvas>
    <!-- Menu -->
    <ul class="header-nav-menulist">
        <li class="highlight search">
            
            
                
            
            
    <form method="get" action="/directory/" class="m-search-form">
    
        <input type="text" placeholder="Search for software or solutions" autocomplete="off" name="q" >
        
        <label >
            <input type="submit" class="bt" value="">
            


<svg  data-name="search" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1216 832q0-185-131.5-316.5t-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5 316.5-131.5 131.5-316.5zm512 832q0 52-38 90t-90 38q-54 0-90-38l-343-342q-179 124-399 124-143 0-273.5-55.5t-225-150-150-225-55.5-273.5 55.5-273.5 150-225 225-150 273.5-55.5 273.5 55.5 225 150 150 225 55.5 273.5q0 220-124 399l343 343q37 37 37 90z"/></svg>
        </label>
    </form>
        </li>
        
        <li class="highlight"><a href="/auth/">Join/Login</a></li>
        
        <li><a href="/software/">Business Software</a></li>
        <li><a href="/directory/">Open Source Software</a></li>
        <li><a  href="/software/vendors/" title="For Vendors">For Vendors</a></li>
        <li><a href="/blog/" title="Blog">Blog</a></li>
        <li><a href="/about">About</a></li>
        <li><a id="header-nav-more" data-toggle="header-nav-more header-nav-more-content" data-toggler=".toggled">More</a></li>
        <li>
            <ul id="header-nav-more-content" class="toggled" data-toggler=".toggled">
                
    

    
    
        <li><a href="/articles/">Articles</a></li>
    
    

    
 
                
                <li><a href="/create">Create</a></li>
                
                <li><a href="https://sourceforge.net/articles/category/sourceforge-podcast/">SourceForge Podcast</a></li>
                
                    <li><a href="https://sourceforge.net/p/forge/documentation/Docs%20Home/">Site Documentation</a></li>
                
                <li><a href="/user/newsletters">Subscribe to our Newsletter</a></li>
                <li><a href="/support">Support Request</a></li>
            </ul>
        </li>
    </ul>
</div>
<div class="off-canvas-content" data-off-canvas-content>

    




<script>
    SF.linkout_icon = '<svg  data-name="sf-linkout-icon" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 300 300" style="enable-background:new 0 0 300 300;" xml:space="preserve"><polygon class="st0" points="243.2,243.2 56.8,243.2 56.8,56.8 123,56.8 123,9 9,9 9,291 291,291 291,179.4 243.2,179 "/><polygon class="st0" points="128.5,213 155,186.5 176,165.5 206.7,196.3 235.5,132.5 248.9,102.6 290.6,9.8 291,9 290.6,9.2 197.4,51.1 169.1,63.8 103.7,93.3 137,126.5 115.9,147.5 89.5,174 "/></svg>';
</script>


<div class="l-header-nav  sandiego">

    <section class="sandiego l-header-nav-top show-for-large">
        <div class="row">
            <a href="/" title="Home" class="sf-logo">
                
    
    <img src="https://a.fsdn.com/con/images/sandiego/sf-logo-full.svg"  alt="SourceForge logo" class="sf-logo-full"/>
            </a>
            <nav class="links">
                
                    <a href="/user/newsletters" title="Subscribe to our newsletter"><span class="newsletter-icon">


<svg  data-name="mmSF_11mail" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 42 42" enable-background="new 0 0 42 42" xml:space="preserve"><path fill="#FFFFFF" d="M0,6v30h42V6H0z M24.2,21.2c-0.8,0.8-2.3,2-3.2,2c-0.9,0-2.4-1.2-3.2-2L5.8,9.7h30.3L24.2,21.2z M13.7,21l-9.9,9.4V11.6L13.7,21z M15.7,23L15.7,23c0.5,0.5,2.9,2.9,5.3,2.9c2.4,0,4.8-2.4,5.2-2.8l0.1-0.1l9.8,9.4H5.8L15.7,23z M28.3,21l9.9-9.5v18.9L28.3,21z"/></svg></span></a>
                
                <a href="/software/vendors/" title="For Vendors">For Vendors</a>
                
                
                    <a href="/support" title="Help">Help</a>
                    <a href="/create/" title="Create">Create</a>
                

                
                <a href="/user/registration" title="Join" >Join</a>
                <a href="/auth/" title="Login">Login</a>
                
            </nav>
        </div>
    </section>

    <section class="sandiego l-header-nav-top hide-for-large">
        <div class="row">
            <a href="/" title="Home" class="sf-logo">
                
    
    <img src="https://a.fsdn.com/con/images/sandiego/sf-logo-full.svg"  alt="SourceForge logo" class="sf-logo-full"/>
            </a>
            <div class="title-bar-right">
                <button type="button" aria-label="Toggle Main Menu" class="menu-icon" data-toggle="offCanvas"></button>
            </div>
        </div>
    </section>
    <section class="sandiego l-header-nav-bottom">
        <nav class="row">
            <a href="/" title="Home" class="sf-logo">
                
    
    <img src="https://a.fsdn.com/con/images/sandiego/sf-logo-full.svg"  alt="SourceForge logo" class="sf-logo-full"/>
            </a>
            <div class="links">
                
    
        <div class="nav-dropdown">
            <a href="/software/">Business Software</a>
            
        </div>
        <div class="nav-dropdown">
            <a href="/directory/" title="Browse">Open Source Software</a>
            
        </div>
        <div class="nav-dropdown">
            <a href="https://sourceforge.net/articles/category/sourceforge-podcast/">SourceForge Podcast</a>
        </div>
        <div class="nav-dropdown">
            <a>Resources</a>
            <ul class="nav-dropdown-menu">
                  <li><a href="/articles/">Articles</a></li>
                  
                  <li><a href="/software/case-studies/">Case Studies</a></li>
                  
                  <li><a href="/blog/">Blog</a></li>
            </ul>
        </div>
    

                <div class="dev-menu-when-stuck">
                    Menu
                    <ul class="dev-menu-dropdown header-nav-menulist">
                        <li><a href="/support">Help</a></li>
                        <li><a href="/create">Create</a></li>
                        <li><a href="/user/registration/" title="Join" >Join</a></li>
                        <li><a href="/auth/" title="Login">Login</a></li>
                    </ul>
                </div>
                <div class="search-toggle-when-stuck">
                    <a class="search-toggle">
                        


<svg  data-name="search" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1216 832q0-185-131.5-316.5t-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5 316.5-131.5 131.5-316.5zm512 832q0 52-38 90t-90 38q-54 0-90-38l-343-342q-179 124-399 124-143 0-273.5-55.5t-225-150-150-225-55.5-273.5 55.5-273.5 150-225 225-150 273.5-55.5 273.5 55.5 225 150 150 225 55.5 273.5q0 220-124 399l343 343q37 37 37 90z"/></svg>
                    </a>
                </div>
            </div>

            <div class="search">
                
                
    
    
        <div class="main-nav-link">
            
            
            <a href="https://www.avast.com/lp-free-av?full_trSrc=mmm_srf_dlp_008_406_a&amp;utm_medium=referral&amp;utm_source=sourceforge&amp;utm_campaign=srf_dlp_008_406_a:site_banner" rel="nofollow" target="_blank" id="main-nav-badge-link" data-label="avastjuly">
                <img src="https://a.fsdn.com/con/assets/maxnav/sourceforge/nav32-347199d0-d5c91966.png"  srcset="https://a.fsdn.com/con/assets/maxnav/sourceforge/nav64-34727d78-d5cf3d96.png 2x"  alt="" id="main-nav-image"/>
            </a>
        </div>
    


                
                    
                
                
                
                
    <form method="get" action="/directory/" class="m-search-form">
    
    <div class="typeahead__container">
      <div class="typeahead__field">
        <div class="typeahead__query">
        
        <input type="text" placeholder="Search for software or solutions" autocomplete="off" name="q" >
        
        </div>
        
        <label >
            <input type="submit" class="bt" value="">
            


<svg  data-name="search" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1216 832q0-185-131.5-316.5t-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5 316.5-131.5 131.5-316.5zm512 832q0 52-38 90t-90 38q-54 0-90-38l-343-342q-179 124-399 124-143 0-273.5-55.5t-225-150-150-225-55.5-273.5 55.5-273.5 150-225 225-150 273.5-55.5 273.5 55.5 225 150 150 225 55.5 273.5q0 220-124 399l343 343q37 37 37 90z"/></svg>
        </label>
      </div>
    </div>
    
    </form>
                
            </div>
        </nav>
        
    </section>
    
</div>
    
    
    <div class="notification-on-project-page">
    
        
    
    
    
    </div>
    
    


    <section id="page-body" class=" neighborhood-Projects project-xsb mountpoint-src 

">
        <div id="nav_menu_holder">
            
                
                    
    <div class="sandiego"> 
            
            
            
            
<section class="project-masthead"> 
    
    

    
<div class="backdrop" style="box-sizing: content-box; padding-bottom: 24px"></div>

    <div class="content">
    
        
    <nav id="breadcrumbs" class="breadcrumbs rtl">
        <ul itemscope itemtype="http://schema.org/BreadcrumbList">
            <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                <a itemprop="item" href="/"><span itemprop="name">Home</span></a>
                <meta itemprop="position" content="1"/>
            </li>
            <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                <a itemprop="item" href="/directory/"><span itemprop="name">Browse</span></a>
                <meta itemprop="position" content="2"/>
            </li>
            
            
                
                
            
            
                
            
            
                <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                    <a itemprop="item" href="/p/xsb/"><span itemprop="name">XSB</span></a>
                <meta itemprop="position" content="3"/>
                </li>
            
                <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                    <span itemprop="name">Src  (SVN)-old</span>
                <meta itemprop="position" content="4"/>
                </li>
            
        </ul>
    </nav>
    
    
    
        
                
                
<div class="overview">
    


<div class="project-icon   " >
    
    
    <img itemprop="image" alt="XSB" title="XSB" 
    
    
    src="https://a.fsdn.com/allura/p/xsb/icon?1368563712"
    /></div>


    
    <div class="title "> 

        <meta itemprop="name" content="XSB"/>
        <h1  >XSB Src  (SVN)-old
        </h1>
        
            
            
          
        
            
            
            <div class="as-h3 brought-by">
                
                Brought to you by:
                
                    <a href="/u/carlandersen/profile/">carlandersen</a>,
                    
                
                    <a href="/u/dwarren/profile/">dwarren</a>,
                    
                
                    <a href="/u/evansbj/profile/">evansbj</a>,
                    
                
                    <a href="/u/kifer/profile/">kifer</a>,
                    
                
                    
                        and <a href="/p/xsb/_members/">3 others</a>
                    
                
            </div>
            
        

        

        
    </div>


    

</div>

                
                
    
  
            
            
        
    </div>
</section>

        </div>
    

                
            
        </div>
        <div id="top_nav" class="">
            
                
<div id="top_nav_admin">
<ul class="dropdown">
  
  
    <li class="">
        <a href="/projects/xsb/" class="tool-summary-32" >
            Summary
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/projects/xsb/files/" class="tool-files-sf-32" >
            Files
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/projects/xsb/reviews/" class="tool-reviews-32" >
            Reviews
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/projects/xsb/support" class="tool-support-32" >
            Support
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/p/xsb/mailman/" class="tool-mailman-32" >
            Mail Lists
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/p/xsb/_list/tickets" class="tool-tickets-32" >
            Tickets ▾
        </a>
        
        
            <ul>
                
                    <li class=""><a href="/p/xsb/bugs/" >Bugs</a></li>
                
                    <li class=""><a href="/p/xsb/feature-requests/" >Feature Requests</a></li>
                
                    <li class=""><a href="/p/xsb/support-requests/" >Support Requests</a></li>
                
                    <li class=""><a href="/p/xsb/patches/" >Patches</a></li>
                
            </ul>
        
    </li>
  
    <li class="">
        <a href="/p/xsb/news/" class="tool-blog-32" >
            News
        </a>
        
        
    </li>
  
    <li class="">
        <a href="/p/xsb/code/" class="tool-git-32" >
            Sources (GIT)
        </a>
        
        
    </li>
  
    <li class="selected">
        <a href="/p/xsb/src/" class="tool-svn-32" >
            Src  (SVN)-old
        </a>
        
        
    </li>
  
  
</ul>
</div>


            
        </div>
        <div id="content_base">
            

    
                
                    
<a id="sidebar-activate" href="#">
    <span class="btn-label">Menu</span>
    <span class="btn-arrow-down">▾</span>
    <span class="btn-arrow-up">▴</span>
</a>

                        


<div id="sidebar">
  
    <div class="placeholder-no-searchbox">&nbsp;</div>
  
    
    
      
      
        
    
      <ul class="sidebarmenu">
      
    
  <li>
      
        <a class="icon" href="/p/xsb/src/commit_browser" rel="nofollow" title="Browse Commits"><i class="fa fa-list"></i>
      
      <span>Browse Commits</span>
      </a>
  </li>
  
      
        
    
  <li>
      
        <a href="/p/xsb/src/"  rel="nofollow" >
      
      <span>Browse Files</span>
      </a>
  </li>
  
      
    
    
      </ul>
      
    
    
</div>



<script>
$('#sidebar-admin-header').on('click', function(e){
    $("#sidebar-admin-menu").toggleClass("hidden");
    $(this).toggleClass("expanded");
    e.preventDefault();
});
</script>

                    

                    
                
                
                    
                
                <div class="grid-20 pad">
                    <h2 class="dark title">
                        <span>
<a href="/p/xsb/src/5501/" rel="nofollow">[r5501]</a>:

  
      <a href="./../../../../" rel="nofollow"><b class="fa fa-folder-open-o" title="Root directory"></b></a> /
  
  
    <a href="./../../../" rel="nofollow">trunk</a> /
  
    <a href="./../../" rel="nofollow">XSB</a> /
  
    <a href="./../" rel="nofollow">docs</a> /
  
    <a href="./" rel="nofollow">userman</a> /
  
 ccallingxsb.tex
</span>
                        <!-- actions -->
                        <small>
                            

    
    <a class="icon" href="#" id="maximize-content" title="Maximize"><i class="fa fa-expand"></i>&nbsp;Maximize</a>
    <a class="icon" href="#" id="restore-content" title="Restore"><i class="fa fa-compress"></i>&nbsp;Restore</a>
<a class="icon" href="/p/xsb/src/5501/log/?path=/trunk/XSB/docs/userman/ccallingxsb.tex" rel="nofollow" title="History"><i class="fa fa-calendar"></i>&nbsp;History</a>

                        </small>
                        <!-- /actions -->
                    </h2>
                    
                    <div>
                        
  

                        
  
    <p><a rel="nofollow" href="?format=raw">Download this file</a></p>
    <div class="clip grid-19 codebrowser">
      <h3>
        1495 lines (1317 with data), 64.7 kB
      </h3>
      
        <div class="codehilite"><table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">   1</span>
<span class="normal">   2</span>
<span class="normal">   3</span>
<span class="normal">   4</span>
<span class="normal">   5</span>
<span class="normal">   6</span>
<span class="normal">   7</span>
<span class="normal">   8</span>
<span class="normal">   9</span>
<span class="normal">  10</span>
<span class="normal">  11</span>
<span class="normal">  12</span>
<span class="normal">  13</span>
<span class="normal">  14</span>
<span class="normal">  15</span>
<span class="normal">  16</span>
<span class="normal">  17</span>
<span class="normal">  18</span>
<span class="normal">  19</span>
<span class="normal">  20</span>
<span class="normal">  21</span>
<span class="normal">  22</span>
<span class="normal">  23</span>
<span class="normal">  24</span>
<span class="normal">  25</span>
<span class="normal">  26</span>
<span class="normal">  27</span>
<span class="normal">  28</span>
<span class="normal">  29</span>
<span class="normal">  30</span>
<span class="normal">  31</span>
<span class="normal">  32</span>
<span class="normal">  33</span>
<span class="normal">  34</span>
<span class="normal">  35</span>
<span class="normal">  36</span>
<span class="normal">  37</span>
<span class="normal">  38</span>
<span class="normal">  39</span>
<span class="normal">  40</span>
<span class="normal">  41</span>
<span class="normal">  42</span>
<span class="normal">  43</span>
<span class="normal">  44</span>
<span class="normal">  45</span>
<span class="normal">  46</span>
<span class="normal">  47</span>
<span class="normal">  48</span>
<span class="normal">  49</span>
<span class="normal">  50</span>
<span class="normal">  51</span>
<span class="normal">  52</span>
<span class="normal">  53</span>
<span class="normal">  54</span>
<span class="normal">  55</span>
<span class="normal">  56</span>
<span class="normal">  57</span>
<span class="normal">  58</span>
<span class="normal">  59</span>
<span class="normal">  60</span>
<span class="normal">  61</span>
<span class="normal">  62</span>
<span class="normal">  63</span>
<span class="normal">  64</span>
<span class="normal">  65</span>
<span class="normal">  66</span>
<span class="normal">  67</span>
<span class="normal">  68</span>
<span class="normal">  69</span>
<span class="normal">  70</span>
<span class="normal">  71</span>
<span class="normal">  72</span>
<span class="normal">  73</span>
<span class="normal">  74</span>
<span class="normal">  75</span>
<span class="normal">  76</span>
<span class="normal">  77</span>
<span class="normal">  78</span>
<span class="normal">  79</span>
<span class="normal">  80</span>
<span class="normal">  81</span>
<span class="normal">  82</span>
<span class="normal">  83</span>
<span class="normal">  84</span>
<span class="normal">  85</span>
<span class="normal">  86</span>
<span class="normal">  87</span>
<span class="normal">  88</span>
<span class="normal">  89</span>
<span class="normal">  90</span>
<span class="normal">  91</span>
<span class="normal">  92</span>
<span class="normal">  93</span>
<span class="normal">  94</span>
<span class="normal">  95</span>
<span class="normal">  96</span>
<span class="normal">  97</span>
<span class="normal">  98</span>
<span class="normal">  99</span>
<span class="normal"> 100</span>
<span class="normal"> 101</span>
<span class="normal"> 102</span>
<span class="normal"> 103</span>
<span class="normal"> 104</span>
<span class="normal"> 105</span>
<span class="normal"> 106</span>
<span class="normal"> 107</span>
<span class="normal"> 108</span>
<span class="normal"> 109</span>
<span class="normal"> 110</span>
<span class="normal"> 111</span>
<span class="normal"> 112</span>
<span class="normal"> 113</span>
<span class="normal"> 114</span>
<span class="normal"> 115</span>
<span class="normal"> 116</span>
<span class="normal"> 117</span>
<span class="normal"> 118</span>
<span class="normal"> 119</span>
<span class="normal"> 120</span>
<span class="normal"> 121</span>
<span class="normal"> 122</span>
<span class="normal"> 123</span>
<span class="normal"> 124</span>
<span class="normal"> 125</span>
<span class="normal"> 126</span>
<span class="normal"> 127</span>
<span class="normal"> 128</span>
<span class="normal"> 129</span>
<span class="normal"> 130</span>
<span class="normal"> 131</span>
<span class="normal"> 132</span>
<span class="normal"> 133</span>
<span class="normal"> 134</span>
<span class="normal"> 135</span>
<span class="normal"> 136</span>
<span class="normal"> 137</span>
<span class="normal"> 138</span>
<span class="normal"> 139</span>
<span class="normal"> 140</span>
<span class="normal"> 141</span>
<span class="normal"> 142</span>
<span class="normal"> 143</span>
<span class="normal"> 144</span>
<span class="normal"> 145</span>
<span class="normal"> 146</span>
<span class="normal"> 147</span>
<span class="normal"> 148</span>
<span class="normal"> 149</span>
<span class="normal"> 150</span>
<span class="normal"> 151</span>
<span class="normal"> 152</span>
<span class="normal"> 153</span>
<span class="normal"> 154</span>
<span class="normal"> 155</span>
<span class="normal"> 156</span>
<span class="normal"> 157</span>
<span class="normal"> 158</span>
<span class="normal"> 159</span>
<span class="normal"> 160</span>
<span class="normal"> 161</span>
<span class="normal"> 162</span>
<span class="normal"> 163</span>
<span class="normal"> 164</span>
<span class="normal"> 165</span>
<span class="normal"> 166</span>
<span class="normal"> 167</span>
<span class="normal"> 168</span>
<span class="normal"> 169</span>
<span class="normal"> 170</span>
<span class="normal"> 171</span>
<span class="normal"> 172</span>
<span class="normal"> 173</span>
<span class="normal"> 174</span>
<span class="normal"> 175</span>
<span class="normal"> 176</span>
<span class="normal"> 177</span>
<span class="normal"> 178</span>
<span class="normal"> 179</span>
<span class="normal"> 180</span>
<span class="normal"> 181</span>
<span class="normal"> 182</span>
<span class="normal"> 183</span>
<span class="normal"> 184</span>
<span class="normal"> 185</span>
<span class="normal"> 186</span>
<span class="normal"> 187</span>
<span class="normal"> 188</span>
<span class="normal"> 189</span>
<span class="normal"> 190</span>
<span class="normal"> 191</span>
<span class="normal"> 192</span>
<span class="normal"> 193</span>
<span class="normal"> 194</span>
<span class="normal"> 195</span>
<span class="normal"> 196</span>
<span class="normal"> 197</span>
<span class="normal"> 198</span>
<span class="normal"> 199</span>
<span class="normal"> 200</span>
<span class="normal"> 201</span>
<span class="normal"> 202</span>
<span class="normal"> 203</span>
<span class="normal"> 204</span>
<span class="normal"> 205</span>
<span class="normal"> 206</span>
<span class="normal"> 207</span>
<span class="normal"> 208</span>
<span class="normal"> 209</span>
<span class="normal"> 210</span>
<span class="normal"> 211</span>
<span class="normal"> 212</span>
<span class="normal"> 213</span>
<span class="normal"> 214</span>
<span class="normal"> 215</span>
<span class="normal"> 216</span>
<span class="normal"> 217</span>
<span class="normal"> 218</span>
<span class="normal"> 219</span>
<span class="normal"> 220</span>
<span class="normal"> 221</span>
<span class="normal"> 222</span>
<span class="normal"> 223</span>
<span class="normal"> 224</span>
<span class="normal"> 225</span>
<span class="normal"> 226</span>
<span class="normal"> 227</span>
<span class="normal"> 228</span>
<span class="normal"> 229</span>
<span class="normal"> 230</span>
<span class="normal"> 231</span>
<span class="normal"> 232</span>
<span class="normal"> 233</span>
<span class="normal"> 234</span>
<span class="normal"> 235</span>
<span class="normal"> 236</span>
<span class="normal"> 237</span>
<span class="normal"> 238</span>
<span class="normal"> 239</span>
<span class="normal"> 240</span>
<span class="normal"> 241</span>
<span class="normal"> 242</span>
<span class="normal"> 243</span>
<span class="normal"> 244</span>
<span class="normal"> 245</span>
<span class="normal"> 246</span>
<span class="normal"> 247</span>
<span class="normal"> 248</span>
<span class="normal"> 249</span>
<span class="normal"> 250</span>
<span class="normal"> 251</span>
<span class="normal"> 252</span>
<span class="normal"> 253</span>
<span class="normal"> 254</span>
<span class="normal"> 255</span>
<span class="normal"> 256</span>
<span class="normal"> 257</span>
<span class="normal"> 258</span>
<span class="normal"> 259</span>
<span class="normal"> 260</span>
<span class="normal"> 261</span>
<span class="normal"> 262</span>
<span class="normal"> 263</span>
<span class="normal"> 264</span>
<span class="normal"> 265</span>
<span class="normal"> 266</span>
<span class="normal"> 267</span>
<span class="normal"> 268</span>
<span class="normal"> 269</span>
<span class="normal"> 270</span>
<span class="normal"> 271</span>
<span class="normal"> 272</span>
<span class="normal"> 273</span>
<span class="normal"> 274</span>
<span class="normal"> 275</span>
<span class="normal"> 276</span>
<span class="normal"> 277</span>
<span class="normal"> 278</span>
<span class="normal"> 279</span>
<span class="normal"> 280</span>
<span class="normal"> 281</span>
<span class="normal"> 282</span>
<span class="normal"> 283</span>
<span class="normal"> 284</span>
<span class="normal"> 285</span>
<span class="normal"> 286</span>
<span class="normal"> 287</span>
<span class="normal"> 288</span>
<span class="normal"> 289</span>
<span class="normal"> 290</span>
<span class="normal"> 291</span>
<span class="normal"> 292</span>
<span class="normal"> 293</span>
<span class="normal"> 294</span>
<span class="normal"> 295</span>
<span class="normal"> 296</span>
<span class="normal"> 297</span>
<span class="normal"> 298</span>
<span class="normal"> 299</span>
<span class="normal"> 300</span>
<span class="normal"> 301</span>
<span class="normal"> 302</span>
<span class="normal"> 303</span>
<span class="normal"> 304</span>
<span class="normal"> 305</span>
<span class="normal"> 306</span>
<span class="normal"> 307</span>
<span class="normal"> 308</span>
<span class="normal"> 309</span>
<span class="normal"> 310</span>
<span class="normal"> 311</span>
<span class="normal"> 312</span>
<span class="normal"> 313</span>
<span class="normal"> 314</span>
<span class="normal"> 315</span>
<span class="normal"> 316</span>
<span class="normal"> 317</span>
<span class="normal"> 318</span>
<span class="normal"> 319</span>
<span class="normal"> 320</span>
<span class="normal"> 321</span>
<span class="normal"> 322</span>
<span class="normal"> 323</span>
<span class="normal"> 324</span>
<span class="normal"> 325</span>
<span class="normal"> 326</span>
<span class="normal"> 327</span>
<span class="normal"> 328</span>
<span class="normal"> 329</span>
<span class="normal"> 330</span>
<span class="normal"> 331</span>
<span class="normal"> 332</span>
<span class="normal"> 333</span>
<span class="normal"> 334</span>
<span class="normal"> 335</span>
<span class="normal"> 336</span>
<span class="normal"> 337</span>
<span class="normal"> 338</span>
<span class="normal"> 339</span>
<span class="normal"> 340</span>
<span class="normal"> 341</span>
<span class="normal"> 342</span>
<span class="normal"> 343</span>
<span class="normal"> 344</span>
<span class="normal"> 345</span>
<span class="normal"> 346</span>
<span class="normal"> 347</span>
<span class="normal"> 348</span>
<span class="normal"> 349</span>
<span class="normal"> 350</span>
<span class="normal"> 351</span>
<span class="normal"> 352</span>
<span class="normal"> 353</span>
<span class="normal"> 354</span>
<span class="normal"> 355</span>
<span class="normal"> 356</span>
<span class="normal"> 357</span>
<span class="normal"> 358</span>
<span class="normal"> 359</span>
<span class="normal"> 360</span>
<span class="normal"> 361</span>
<span class="normal"> 362</span>
<span class="normal"> 363</span>
<span class="normal"> 364</span>
<span class="normal"> 365</span>
<span class="normal"> 366</span>
<span class="normal"> 367</span>
<span class="normal"> 368</span>
<span class="normal"> 369</span>
<span class="normal"> 370</span>
<span class="normal"> 371</span>
<span class="normal"> 372</span>
<span class="normal"> 373</span>
<span class="normal"> 374</span>
<span class="normal"> 375</span>
<span class="normal"> 376</span>
<span class="normal"> 377</span>
<span class="normal"> 378</span>
<span class="normal"> 379</span>
<span class="normal"> 380</span>
<span class="normal"> 381</span>
<span class="normal"> 382</span>
<span class="normal"> 383</span>
<span class="normal"> 384</span>
<span class="normal"> 385</span>
<span class="normal"> 386</span>
<span class="normal"> 387</span>
<span class="normal"> 388</span>
<span class="normal"> 389</span>
<span class="normal"> 390</span>
<span class="normal"> 391</span>
<span class="normal"> 392</span>
<span class="normal"> 393</span>
<span class="normal"> 394</span>
<span class="normal"> 395</span>
<span class="normal"> 396</span>
<span class="normal"> 397</span>
<span class="normal"> 398</span>
<span class="normal"> 399</span>
<span class="normal"> 400</span>
<span class="normal"> 401</span>
<span class="normal"> 402</span>
<span class="normal"> 403</span>
<span class="normal"> 404</span>
<span class="normal"> 405</span>
<span class="normal"> 406</span>
<span class="normal"> 407</span>
<span class="normal"> 408</span>
<span class="normal"> 409</span>
<span class="normal"> 410</span>
<span class="normal"> 411</span>
<span class="normal"> 412</span>
<span class="normal"> 413</span>
<span class="normal"> 414</span>
<span class="normal"> 415</span>
<span class="normal"> 416</span>
<span class="normal"> 417</span>
<span class="normal"> 418</span>
<span class="normal"> 419</span>
<span class="normal"> 420</span>
<span class="normal"> 421</span>
<span class="normal"> 422</span>
<span class="normal"> 423</span>
<span class="normal"> 424</span>
<span class="normal"> 425</span>
<span class="normal"> 426</span>
<span class="normal"> 427</span>
<span class="normal"> 428</span>
<span class="normal"> 429</span>
<span class="normal"> 430</span>
<span class="normal"> 431</span>
<span class="normal"> 432</span>
<span class="normal"> 433</span>
<span class="normal"> 434</span>
<span class="normal"> 435</span>
<span class="normal"> 436</span>
<span class="normal"> 437</span>
<span class="normal"> 438</span>
<span class="normal"> 439</span>
<span class="normal"> 440</span>
<span class="normal"> 441</span>
<span class="normal"> 442</span>
<span class="normal"> 443</span>
<span class="normal"> 444</span>
<span class="normal"> 445</span>
<span class="normal"> 446</span>
<span class="normal"> 447</span>
<span class="normal"> 448</span>
<span class="normal"> 449</span>
<span class="normal"> 450</span>
<span class="normal"> 451</span>
<span class="normal"> 452</span>
<span class="normal"> 453</span>
<span class="normal"> 454</span>
<span class="normal"> 455</span>
<span class="normal"> 456</span>
<span class="normal"> 457</span>
<span class="normal"> 458</span>
<span class="normal"> 459</span>
<span class="normal"> 460</span>
<span class="normal"> 461</span>
<span class="normal"> 462</span>
<span class="normal"> 463</span>
<span class="normal"> 464</span>
<span class="normal"> 465</span>
<span class="normal"> 466</span>
<span class="normal"> 467</span>
<span class="normal"> 468</span>
<span class="normal"> 469</span>
<span class="normal"> 470</span>
<span class="normal"> 471</span>
<span class="normal"> 472</span>
<span class="normal"> 473</span>
<span class="normal"> 474</span>
<span class="normal"> 475</span>
<span class="normal"> 476</span>
<span class="normal"> 477</span>
<span class="normal"> 478</span>
<span class="normal"> 479</span>
<span class="normal"> 480</span>
<span class="normal"> 481</span>
<span class="normal"> 482</span>
<span class="normal"> 483</span>
<span class="normal"> 484</span>
<span class="normal"> 485</span>
<span class="normal"> 486</span>
<span class="normal"> 487</span>
<span class="normal"> 488</span>
<span class="normal"> 489</span>
<span class="normal"> 490</span>
<span class="normal"> 491</span>
<span class="normal"> 492</span>
<span class="normal"> 493</span>
<span class="normal"> 494</span>
<span class="normal"> 495</span>
<span class="normal"> 496</span>
<span class="normal"> 497</span>
<span class="normal"> 498</span>
<span class="normal"> 499</span>
<span class="normal"> 500</span>
<span class="normal"> 501</span>
<span class="normal"> 502</span>
<span class="normal"> 503</span>
<span class="normal"> 504</span>
<span class="normal"> 505</span>
<span class="normal"> 506</span>
<span class="normal"> 507</span>
<span class="normal"> 508</span>
<span class="normal"> 509</span>
<span class="normal"> 510</span>
<span class="normal"> 511</span>
<span class="normal"> 512</span>
<span class="normal"> 513</span>
<span class="normal"> 514</span>
<span class="normal"> 515</span>
<span class="normal"> 516</span>
<span class="normal"> 517</span>
<span class="normal"> 518</span>
<span class="normal"> 519</span>
<span class="normal"> 520</span>
<span class="normal"> 521</span>
<span class="normal"> 522</span>
<span class="normal"> 523</span>
<span class="normal"> 524</span>
<span class="normal"> 525</span>
<span class="normal"> 526</span>
<span class="normal"> 527</span>
<span class="normal"> 528</span>
<span class="normal"> 529</span>
<span class="normal"> 530</span>
<span class="normal"> 531</span>
<span class="normal"> 532</span>
<span class="normal"> 533</span>
<span class="normal"> 534</span>
<span class="normal"> 535</span>
<span class="normal"> 536</span>
<span class="normal"> 537</span>
<span class="normal"> 538</span>
<span class="normal"> 539</span>
<span class="normal"> 540</span>
<span class="normal"> 541</span>
<span class="normal"> 542</span>
<span class="normal"> 543</span>
<span class="normal"> 544</span>
<span class="normal"> 545</span>
<span class="normal"> 546</span>
<span class="normal"> 547</span>
<span class="normal"> 548</span>
<span class="normal"> 549</span>
<span class="normal"> 550</span>
<span class="normal"> 551</span>
<span class="normal"> 552</span>
<span class="normal"> 553</span>
<span class="normal"> 554</span>
<span class="normal"> 555</span>
<span class="normal"> 556</span>
<span class="normal"> 557</span>
<span class="normal"> 558</span>
<span class="normal"> 559</span>
<span class="normal"> 560</span>
<span class="normal"> 561</span>
<span class="normal"> 562</span>
<span class="normal"> 563</span>
<span class="normal"> 564</span>
<span class="normal"> 565</span>
<span class="normal"> 566</span>
<span class="normal"> 567</span>
<span class="normal"> 568</span>
<span class="normal"> 569</span>
<span class="normal"> 570</span>
<span class="normal"> 571</span>
<span class="normal"> 572</span>
<span class="normal"> 573</span>
<span class="normal"> 574</span>
<span class="normal"> 575</span>
<span class="normal"> 576</span>
<span class="normal"> 577</span>
<span class="normal"> 578</span>
<span class="normal"> 579</span>
<span class="normal"> 580</span>
<span class="normal"> 581</span>
<span class="normal"> 582</span>
<span class="normal"> 583</span>
<span class="normal"> 584</span>
<span class="normal"> 585</span>
<span class="normal"> 586</span>
<span class="normal"> 587</span>
<span class="normal"> 588</span>
<span class="normal"> 589</span>
<span class="normal"> 590</span>
<span class="normal"> 591</span>
<span class="normal"> 592</span>
<span class="normal"> 593</span>
<span class="normal"> 594</span>
<span class="normal"> 595</span>
<span class="normal"> 596</span>
<span class="normal"> 597</span>
<span class="normal"> 598</span>
<span class="normal"> 599</span>
<span class="normal"> 600</span>
<span class="normal"> 601</span>
<span class="normal"> 602</span>
<span class="normal"> 603</span>
<span class="normal"> 604</span>
<span class="normal"> 605</span>
<span class="normal"> 606</span>
<span class="normal"> 607</span>
<span class="normal"> 608</span>
<span class="normal"> 609</span>
<span class="normal"> 610</span>
<span class="normal"> 611</span>
<span class="normal"> 612</span>
<span class="normal"> 613</span>
<span class="normal"> 614</span>
<span class="normal"> 615</span>
<span class="normal"> 616</span>
<span class="normal"> 617</span>
<span class="normal"> 618</span>
<span class="normal"> 619</span>
<span class="normal"> 620</span>
<span class="normal"> 621</span>
<span class="normal"> 622</span>
<span class="normal"> 623</span>
<span class="normal"> 624</span>
<span class="normal"> 625</span>
<span class="normal"> 626</span>
<span class="normal"> 627</span>
<span class="normal"> 628</span>
<span class="normal"> 629</span>
<span class="normal"> 630</span>
<span class="normal"> 631</span>
<span class="normal"> 632</span>
<span class="normal"> 633</span>
<span class="normal"> 634</span>
<span class="normal"> 635</span>
<span class="normal"> 636</span>
<span class="normal"> 637</span>
<span class="normal"> 638</span>
<span class="normal"> 639</span>
<span class="normal"> 640</span>
<span class="normal"> 641</span>
<span class="normal"> 642</span>
<span class="normal"> 643</span>
<span class="normal"> 644</span>
<span class="normal"> 645</span>
<span class="normal"> 646</span>
<span class="normal"> 647</span>
<span class="normal"> 648</span>
<span class="normal"> 649</span>
<span class="normal"> 650</span>
<span class="normal"> 651</span>
<span class="normal"> 652</span>
<span class="normal"> 653</span>
<span class="normal"> 654</span>
<span class="normal"> 655</span>
<span class="normal"> 656</span>
<span class="normal"> 657</span>
<span class="normal"> 658</span>
<span class="normal"> 659</span>
<span class="normal"> 660</span>
<span class="normal"> 661</span>
<span class="normal"> 662</span>
<span class="normal"> 663</span>
<span class="normal"> 664</span>
<span class="normal"> 665</span>
<span class="normal"> 666</span>
<span class="normal"> 667</span>
<span class="normal"> 668</span>
<span class="normal"> 669</span>
<span class="normal"> 670</span>
<span class="normal"> 671</span>
<span class="normal"> 672</span>
<span class="normal"> 673</span>
<span class="normal"> 674</span>
<span class="normal"> 675</span>
<span class="normal"> 676</span>
<span class="normal"> 677</span>
<span class="normal"> 678</span>
<span class="normal"> 679</span>
<span class="normal"> 680</span>
<span class="normal"> 681</span>
<span class="normal"> 682</span>
<span class="normal"> 683</span>
<span class="normal"> 684</span>
<span class="normal"> 685</span>
<span class="normal"> 686</span>
<span class="normal"> 687</span>
<span class="normal"> 688</span>
<span class="normal"> 689</span>
<span class="normal"> 690</span>
<span class="normal"> 691</span>
<span class="normal"> 692</span>
<span class="normal"> 693</span>
<span class="normal"> 694</span>
<span class="normal"> 695</span>
<span class="normal"> 696</span>
<span class="normal"> 697</span>
<span class="normal"> 698</span>
<span class="normal"> 699</span>
<span class="normal"> 700</span>
<span class="normal"> 701</span>
<span class="normal"> 702</span>
<span class="normal"> 703</span>
<span class="normal"> 704</span>
<span class="normal"> 705</span>
<span class="normal"> 706</span>
<span class="normal"> 707</span>
<span class="normal"> 708</span>
<span class="normal"> 709</span>
<span class="normal"> 710</span>
<span class="normal"> 711</span>
<span class="normal"> 712</span>
<span class="normal"> 713</span>
<span class="normal"> 714</span>
<span class="normal"> 715</span>
<span class="normal"> 716</span>
<span class="normal"> 717</span>
<span class="normal"> 718</span>
<span class="normal"> 719</span>
<span class="normal"> 720</span>
<span class="normal"> 721</span>
<span class="normal"> 722</span>
<span class="normal"> 723</span>
<span class="normal"> 724</span>
<span class="normal"> 725</span>
<span class="normal"> 726</span>
<span class="normal"> 727</span>
<span class="normal"> 728</span>
<span class="normal"> 729</span>
<span class="normal"> 730</span>
<span class="normal"> 731</span>
<span class="normal"> 732</span>
<span class="normal"> 733</span>
<span class="normal"> 734</span>
<span class="normal"> 735</span>
<span class="normal"> 736</span>
<span class="normal"> 737</span>
<span class="normal"> 738</span>
<span class="normal"> 739</span>
<span class="normal"> 740</span>
<span class="normal"> 741</span>
<span class="normal"> 742</span>
<span class="normal"> 743</span>
<span class="normal"> 744</span>
<span class="normal"> 745</span>
<span class="normal"> 746</span>
<span class="normal"> 747</span>
<span class="normal"> 748</span>
<span class="normal"> 749</span>
<span class="normal"> 750</span>
<span class="normal"> 751</span>
<span class="normal"> 752</span>
<span class="normal"> 753</span>
<span class="normal"> 754</span>
<span class="normal"> 755</span>
<span class="normal"> 756</span>
<span class="normal"> 757</span>
<span class="normal"> 758</span>
<span class="normal"> 759</span>
<span class="normal"> 760</span>
<span class="normal"> 761</span>
<span class="normal"> 762</span>
<span class="normal"> 763</span>
<span class="normal"> 764</span>
<span class="normal"> 765</span>
<span class="normal"> 766</span>
<span class="normal"> 767</span>
<span class="normal"> 768</span>
<span class="normal"> 769</span>
<span class="normal"> 770</span>
<span class="normal"> 771</span>
<span class="normal"> 772</span>
<span class="normal"> 773</span>
<span class="normal"> 774</span>
<span class="normal"> 775</span>
<span class="normal"> 776</span>
<span class="normal"> 777</span>
<span class="normal"> 778</span>
<span class="normal"> 779</span>
<span class="normal"> 780</span>
<span class="normal"> 781</span>
<span class="normal"> 782</span>
<span class="normal"> 783</span>
<span class="normal"> 784</span>
<span class="normal"> 785</span>
<span class="normal"> 786</span>
<span class="normal"> 787</span>
<span class="normal"> 788</span>
<span class="normal"> 789</span>
<span class="normal"> 790</span>
<span class="normal"> 791</span>
<span class="normal"> 792</span>
<span class="normal"> 793</span>
<span class="normal"> 794</span>
<span class="normal"> 795</span>
<span class="normal"> 796</span>
<span class="normal"> 797</span>
<span class="normal"> 798</span>
<span class="normal"> 799</span>
<span class="normal"> 800</span>
<span class="normal"> 801</span>
<span class="normal"> 802</span>
<span class="normal"> 803</span>
<span class="normal"> 804</span>
<span class="normal"> 805</span>
<span class="normal"> 806</span>
<span class="normal"> 807</span>
<span class="normal"> 808</span>
<span class="normal"> 809</span>
<span class="normal"> 810</span>
<span class="normal"> 811</span>
<span class="normal"> 812</span>
<span class="normal"> 813</span>
<span class="normal"> 814</span>
<span class="normal"> 815</span>
<span class="normal"> 816</span>
<span class="normal"> 817</span>
<span class="normal"> 818</span>
<span class="normal"> 819</span>
<span class="normal"> 820</span>
<span class="normal"> 821</span>
<span class="normal"> 822</span>
<span class="normal"> 823</span>
<span class="normal"> 824</span>
<span class="normal"> 825</span>
<span class="normal"> 826</span>
<span class="normal"> 827</span>
<span class="normal"> 828</span>
<span class="normal"> 829</span>
<span class="normal"> 830</span>
<span class="normal"> 831</span>
<span class="normal"> 832</span>
<span class="normal"> 833</span>
<span class="normal"> 834</span>
<span class="normal"> 835</span>
<span class="normal"> 836</span>
<span class="normal"> 837</span>
<span class="normal"> 838</span>
<span class="normal"> 839</span>
<span class="normal"> 840</span>
<span class="normal"> 841</span>
<span class="normal"> 842</span>
<span class="normal"> 843</span>
<span class="normal"> 844</span>
<span class="normal"> 845</span>
<span class="normal"> 846</span>
<span class="normal"> 847</span>
<span class="normal"> 848</span>
<span class="normal"> 849</span>
<span class="normal"> 850</span>
<span class="normal"> 851</span>
<span class="normal"> 852</span>
<span class="normal"> 853</span>
<span class="normal"> 854</span>
<span class="normal"> 855</span>
<span class="normal"> 856</span>
<span class="normal"> 857</span>
<span class="normal"> 858</span>
<span class="normal"> 859</span>
<span class="normal"> 860</span>
<span class="normal"> 861</span>
<span class="normal"> 862</span>
<span class="normal"> 863</span>
<span class="normal"> 864</span>
<span class="normal"> 865</span>
<span class="normal"> 866</span>
<span class="normal"> 867</span>
<span class="normal"> 868</span>
<span class="normal"> 869</span>
<span class="normal"> 870</span>
<span class="normal"> 871</span>
<span class="normal"> 872</span>
<span class="normal"> 873</span>
<span class="normal"> 874</span>
<span class="normal"> 875</span>
<span class="normal"> 876</span>
<span class="normal"> 877</span>
<span class="normal"> 878</span>
<span class="normal"> 879</span>
<span class="normal"> 880</span>
<span class="normal"> 881</span>
<span class="normal"> 882</span>
<span class="normal"> 883</span>
<span class="normal"> 884</span>
<span class="normal"> 885</span>
<span class="normal"> 886</span>
<span class="normal"> 887</span>
<span class="normal"> 888</span>
<span class="normal"> 889</span>
<span class="normal"> 890</span>
<span class="normal"> 891</span>
<span class="normal"> 892</span>
<span class="normal"> 893</span>
<span class="normal"> 894</span>
<span class="normal"> 895</span>
<span class="normal"> 896</span>
<span class="normal"> 897</span>
<span class="normal"> 898</span>
<span class="normal"> 899</span>
<span class="normal"> 900</span>
<span class="normal"> 901</span>
<span class="normal"> 902</span>
<span class="normal"> 903</span>
<span class="normal"> 904</span>
<span class="normal"> 905</span>
<span class="normal"> 906</span>
<span class="normal"> 907</span>
<span class="normal"> 908</span>
<span class="normal"> 909</span>
<span class="normal"> 910</span>
<span class="normal"> 911</span>
<span class="normal"> 912</span>
<span class="normal"> 913</span>
<span class="normal"> 914</span>
<span class="normal"> 915</span>
<span class="normal"> 916</span>
<span class="normal"> 917</span>
<span class="normal"> 918</span>
<span class="normal"> 919</span>
<span class="normal"> 920</span>
<span class="normal"> 921</span>
<span class="normal"> 922</span>
<span class="normal"> 923</span>
<span class="normal"> 924</span>
<span class="normal"> 925</span>
<span class="normal"> 926</span>
<span class="normal"> 927</span>
<span class="normal"> 928</span>
<span class="normal"> 929</span>
<span class="normal"> 930</span>
<span class="normal"> 931</span>
<span class="normal"> 932</span>
<span class="normal"> 933</span>
<span class="normal"> 934</span>
<span class="normal"> 935</span>
<span class="normal"> 936</span>
<span class="normal"> 937</span>
<span class="normal"> 938</span>
<span class="normal"> 939</span>
<span class="normal"> 940</span>
<span class="normal"> 941</span>
<span class="normal"> 942</span>
<span class="normal"> 943</span>
<span class="normal"> 944</span>
<span class="normal"> 945</span>
<span class="normal"> 946</span>
<span class="normal"> 947</span>
<span class="normal"> 948</span>
<span class="normal"> 949</span>
<span class="normal"> 950</span>
<span class="normal"> 951</span>
<span class="normal"> 952</span>
<span class="normal"> 953</span>
<span class="normal"> 954</span>
<span class="normal"> 955</span>
<span class="normal"> 956</span>
<span class="normal"> 957</span>
<span class="normal"> 958</span>
<span class="normal"> 959</span>
<span class="normal"> 960</span>
<span class="normal"> 961</span>
<span class="normal"> 962</span>
<span class="normal"> 963</span>
<span class="normal"> 964</span>
<span class="normal"> 965</span>
<span class="normal"> 966</span>
<span class="normal"> 967</span>
<span class="normal"> 968</span>
<span class="normal"> 969</span>
<span class="normal"> 970</span>
<span class="normal"> 971</span>
<span class="normal"> 972</span>
<span class="normal"> 973</span>
<span class="normal"> 974</span>
<span class="normal"> 975</span>
<span class="normal"> 976</span>
<span class="normal"> 977</span>
<span class="normal"> 978</span>
<span class="normal"> 979</span>
<span class="normal"> 980</span>
<span class="normal"> 981</span>
<span class="normal"> 982</span>
<span class="normal"> 983</span>
<span class="normal"> 984</span>
<span class="normal"> 985</span>
<span class="normal"> 986</span>
<span class="normal"> 987</span>
<span class="normal"> 988</span>
<span class="normal"> 989</span>
<span class="normal"> 990</span>
<span class="normal"> 991</span>
<span class="normal"> 992</span>
<span class="normal"> 993</span>
<span class="normal"> 994</span>
<span class="normal"> 995</span>
<span class="normal"> 996</span>
<span class="normal"> 997</span>
<span class="normal"> 998</span>
<span class="normal"> 999</span>
<span class="normal">1000</span>
<span class="normal">1001</span>
<span class="normal">1002</span>
<span class="normal">1003</span>
<span class="normal">1004</span>
<span class="normal">1005</span>
<span class="normal">1006</span>
<span class="normal">1007</span>
<span class="normal">1008</span>
<span class="normal">1009</span>
<span class="normal">1010</span>
<span class="normal">1011</span>
<span class="normal">1012</span>
<span class="normal">1013</span>
<span class="normal">1014</span>
<span class="normal">1015</span>
<span class="normal">1016</span>
<span class="normal">1017</span>
<span class="normal">1018</span>
<span class="normal">1019</span>
<span class="normal">1020</span>
<span class="normal">1021</span>
<span class="normal">1022</span>
<span class="normal">1023</span>
<span class="normal">1024</span>
<span class="normal">1025</span>
<span class="normal">1026</span>
<span class="normal">1027</span>
<span class="normal">1028</span>
<span class="normal">1029</span>
<span class="normal">1030</span>
<span class="normal">1031</span>
<span class="normal">1032</span>
<span class="normal">1033</span>
<span class="normal">1034</span>
<span class="normal">1035</span>
<span class="normal">1036</span>
<span class="normal">1037</span>
<span class="normal">1038</span>
<span class="normal">1039</span>
<span class="normal">1040</span>
<span class="normal">1041</span>
<span class="normal">1042</span>
<span class="normal">1043</span>
<span class="normal">1044</span>
<span class="normal">1045</span>
<span class="normal">1046</span>
<span class="normal">1047</span>
<span class="normal">1048</span>
<span class="normal">1049</span>
<span class="normal">1050</span>
<span class="normal">1051</span>
<span class="normal">1052</span>
<span class="normal">1053</span>
<span class="normal">1054</span>
<span class="normal">1055</span>
<span class="normal">1056</span>
<span class="normal">1057</span>
<span class="normal">1058</span>
<span class="normal">1059</span>
<span class="normal">1060</span>
<span class="normal">1061</span>
<span class="normal">1062</span>
<span class="normal">1063</span>
<span class="normal">1064</span>
<span class="normal">1065</span>
<span class="normal">1066</span>
<span class="normal">1067</span>
<span class="normal">1068</span>
<span class="normal">1069</span>
<span class="normal">1070</span>
<span class="normal">1071</span>
<span class="normal">1072</span>
<span class="normal">1073</span>
<span class="normal">1074</span>
<span class="normal">1075</span>
<span class="normal">1076</span>
<span class="normal">1077</span>
<span class="normal">1078</span>
<span class="normal">1079</span>
<span class="normal">1080</span>
<span class="normal">1081</span>
<span class="normal">1082</span>
<span class="normal">1083</span>
<span class="normal">1084</span>
<span class="normal">1085</span>
<span class="normal">1086</span>
<span class="normal">1087</span>
<span class="normal">1088</span>
<span class="normal">1089</span>
<span class="normal">1090</span>
<span class="normal">1091</span>
<span class="normal">1092</span>
<span class="normal">1093</span>
<span class="normal">1094</span>
<span class="normal">1095</span>
<span class="normal">1096</span>
<span class="normal">1097</span>
<span class="normal">1098</span>
<span class="normal">1099</span>
<span class="normal">1100</span>
<span class="normal">1101</span>
<span class="normal">1102</span>
<span class="normal">1103</span>
<span class="normal">1104</span>
<span class="normal">1105</span>
<span class="normal">1106</span>
<span class="normal">1107</span>
<span class="normal">1108</span>
<span class="normal">1109</span>
<span class="normal">1110</span>
<span class="normal">1111</span>
<span class="normal">1112</span>
<span class="normal">1113</span>
<span class="normal">1114</span>
<span class="normal">1115</span>
<span class="normal">1116</span>
<span class="normal">1117</span>
<span class="normal">1118</span>
<span class="normal">1119</span>
<span class="normal">1120</span>
<span class="normal">1121</span>
<span class="normal">1122</span>
<span class="normal">1123</span>
<span class="normal">1124</span>
<span class="normal">1125</span>
<span class="normal">1126</span>
<span class="normal">1127</span>
<span class="normal">1128</span>
<span class="normal">1129</span>
<span class="normal">1130</span>
<span class="normal">1131</span>
<span class="normal">1132</span>
<span class="normal">1133</span>
<span class="normal">1134</span>
<span class="normal">1135</span>
<span class="normal">1136</span>
<span class="normal">1137</span>
<span class="normal">1138</span>
<span class="normal">1139</span>
<span class="normal">1140</span>
<span class="normal">1141</span>
<span class="normal">1142</span>
<span class="normal">1143</span>
<span class="normal">1144</span>
<span class="normal">1145</span>
<span class="normal">1146</span>
<span class="normal">1147</span>
<span class="normal">1148</span>
<span class="normal">1149</span>
<span class="normal">1150</span>
<span class="normal">1151</span>
<span class="normal">1152</span>
<span class="normal">1153</span>
<span class="normal">1154</span>
<span class="normal">1155</span>
<span class="normal">1156</span>
<span class="normal">1157</span>
<span class="normal">1158</span>
<span class="normal">1159</span>
<span class="normal">1160</span>
<span class="normal">1161</span>
<span class="normal">1162</span>
<span class="normal">1163</span>
<span class="normal">1164</span>
<span class="normal">1165</span>
<span class="normal">1166</span>
<span class="normal">1167</span>
<span class="normal">1168</span>
<span class="normal">1169</span>
<span class="normal">1170</span>
<span class="normal">1171</span>
<span class="normal">1172</span>
<span class="normal">1173</span>
<span class="normal">1174</span>
<span class="normal">1175</span>
<span class="normal">1176</span>
<span class="normal">1177</span>
<span class="normal">1178</span>
<span class="normal">1179</span>
<span class="normal">1180</span>
<span class="normal">1181</span>
<span class="normal">1182</span>
<span class="normal">1183</span>
<span class="normal">1184</span>
<span class="normal">1185</span>
<span class="normal">1186</span>
<span class="normal">1187</span>
<span class="normal">1188</span>
<span class="normal">1189</span>
<span class="normal">1190</span>
<span class="normal">1191</span>
<span class="normal">1192</span>
<span class="normal">1193</span>
<span class="normal">1194</span>
<span class="normal">1195</span>
<span class="normal">1196</span>
<span class="normal">1197</span>
<span class="normal">1198</span>
<span class="normal">1199</span>
<span class="normal">1200</span>
<span class="normal">1201</span>
<span class="normal">1202</span>
<span class="normal">1203</span>
<span class="normal">1204</span>
<span class="normal">1205</span>
<span class="normal">1206</span>
<span class="normal">1207</span>
<span class="normal">1208</span>
<span class="normal">1209</span>
<span class="normal">1210</span>
<span class="normal">1211</span>
<span class="normal">1212</span>
<span class="normal">1213</span>
<span class="normal">1214</span>
<span class="normal">1215</span>
<span class="normal">1216</span>
<span class="normal">1217</span>
<span class="normal">1218</span>
<span class="normal">1219</span>
<span class="normal">1220</span>
<span class="normal">1221</span>
<span class="normal">1222</span>
<span class="normal">1223</span>
<span class="normal">1224</span>
<span class="normal">1225</span>
<span class="normal">1226</span>
<span class="normal">1227</span>
<span class="normal">1228</span>
<span class="normal">1229</span>
<span class="normal">1230</span>
<span class="normal">1231</span>
<span class="normal">1232</span>
<span class="normal">1233</span>
<span class="normal">1234</span>
<span class="normal">1235</span>
<span class="normal">1236</span>
<span class="normal">1237</span>
<span class="normal">1238</span>
<span class="normal">1239</span>
<span class="normal">1240</span>
<span class="normal">1241</span>
<span class="normal">1242</span>
<span class="normal">1243</span>
<span class="normal">1244</span>
<span class="normal">1245</span>
<span class="normal">1246</span>
<span class="normal">1247</span>
<span class="normal">1248</span>
<span class="normal">1249</span>
<span class="normal">1250</span>
<span class="normal">1251</span>
<span class="normal">1252</span>
<span class="normal">1253</span>
<span class="normal">1254</span>
<span class="normal">1255</span>
<span class="normal">1256</span>
<span class="normal">1257</span>
<span class="normal">1258</span>
<span class="normal">1259</span>
<span class="normal">1260</span>
<span class="normal">1261</span>
<span class="normal">1262</span>
<span class="normal">1263</span>
<span class="normal">1264</span>
<span class="normal">1265</span>
<span class="normal">1266</span>
<span class="normal">1267</span>
<span class="normal">1268</span>
<span class="normal">1269</span>
<span class="normal">1270</span>
<span class="normal">1271</span>
<span class="normal">1272</span>
<span class="normal">1273</span>
<span class="normal">1274</span>
<span class="normal">1275</span>
<span class="normal">1276</span>
<span class="normal">1277</span>
<span class="normal">1278</span>
<span class="normal">1279</span>
<span class="normal">1280</span>
<span class="normal">1281</span>
<span class="normal">1282</span>
<span class="normal">1283</span>
<span class="normal">1284</span>
<span class="normal">1285</span>
<span class="normal">1286</span>
<span class="normal">1287</span>
<span class="normal">1288</span>
<span class="normal">1289</span>
<span class="normal">1290</span>
<span class="normal">1291</span>
<span class="normal">1292</span>
<span class="normal">1293</span>
<span class="normal">1294</span>
<span class="normal">1295</span>
<span class="normal">1296</span>
<span class="normal">1297</span>
<span class="normal">1298</span>
<span class="normal">1299</span>
<span class="normal">1300</span>
<span class="normal">1301</span>
<span class="normal">1302</span>
<span class="normal">1303</span>
<span class="normal">1304</span>
<span class="normal">1305</span>
<span class="normal">1306</span>
<span class="normal">1307</span>
<span class="normal">1308</span>
<span class="normal">1309</span>
<span class="normal">1310</span>
<span class="normal">1311</span>
<span class="normal">1312</span>
<span class="normal">1313</span>
<span class="normal">1314</span>
<span class="normal">1315</span>
<span class="normal">1316</span>
<span class="normal">1317</span>
<span class="normal">1318</span>
<span class="normal">1319</span>
<span class="normal">1320</span>
<span class="normal">1321</span>
<span class="normal">1322</span>
<span class="normal">1323</span>
<span class="normal">1324</span>
<span class="normal">1325</span>
<span class="normal">1326</span>
<span class="normal">1327</span>
<span class="normal">1328</span>
<span class="normal">1329</span>
<span class="normal">1330</span>
<span class="normal">1331</span>
<span class="normal">1332</span>
<span class="normal">1333</span>
<span class="normal">1334</span>
<span class="normal">1335</span>
<span class="normal">1336</span>
<span class="normal">1337</span>
<span class="normal">1338</span>
<span class="normal">1339</span>
<span class="normal">1340</span>
<span class="normal">1341</span>
<span class="normal">1342</span>
<span class="normal">1343</span>
<span class="normal">1344</span>
<span class="normal">1345</span>
<span class="normal">1346</span>
<span class="normal">1347</span>
<span class="normal">1348</span>
<span class="normal">1349</span>
<span class="normal">1350</span>
<span class="normal">1351</span>
<span class="normal">1352</span>
<span class="normal">1353</span>
<span class="normal">1354</span>
<span class="normal">1355</span>
<span class="normal">1356</span>
<span class="normal">1357</span>
<span class="normal">1358</span>
<span class="normal">1359</span>
<span class="normal">1360</span>
<span class="normal">1361</span>
<span class="normal">1362</span>
<span class="normal">1363</span>
<span class="normal">1364</span>
<span class="normal">1365</span>
<span class="normal">1366</span>
<span class="normal">1367</span>
<span class="normal">1368</span>
<span class="normal">1369</span>
<span class="normal">1370</span>
<span class="normal">1371</span>
<span class="normal">1372</span>
<span class="normal">1373</span>
<span class="normal">1374</span>
<span class="normal">1375</span>
<span class="normal">1376</span>
<span class="normal">1377</span>
<span class="normal">1378</span>
<span class="normal">1379</span>
<span class="normal">1380</span>
<span class="normal">1381</span>
<span class="normal">1382</span>
<span class="normal">1383</span>
<span class="normal">1384</span>
<span class="normal">1385</span>
<span class="normal">1386</span>
<span class="normal">1387</span>
<span class="normal">1388</span>
<span class="normal">1389</span>
<span class="normal">1390</span>
<span class="normal">1391</span>
<span class="normal">1392</span>
<span class="normal">1393</span>
<span class="normal">1394</span>
<span class="normal">1395</span>
<span class="normal">1396</span>
<span class="normal">1397</span>
<span class="normal">1398</span>
<span class="normal">1399</span>
<span class="normal">1400</span>
<span class="normal">1401</span>
<span class="normal">1402</span>
<span class="normal">1403</span>
<span class="normal">1404</span>
<span class="normal">1405</span>
<span class="normal">1406</span>
<span class="normal">1407</span>
<span class="normal">1408</span>
<span class="normal">1409</span>
<span class="normal">1410</span>
<span class="normal">1411</span>
<span class="normal">1412</span>
<span class="normal">1413</span>
<span class="normal">1414</span>
<span class="normal">1415</span>
<span class="normal">1416</span>
<span class="normal">1417</span>
<span class="normal">1418</span>
<span class="normal">1419</span>
<span class="normal">1420</span>
<span class="normal">1421</span>
<span class="normal">1422</span>
<span class="normal">1423</span>
<span class="normal">1424</span>
<span class="normal">1425</span>
<span class="normal">1426</span>
<span class="normal">1427</span>
<span class="normal">1428</span>
<span class="normal">1429</span>
<span class="normal">1430</span>
<span class="normal">1431</span>
<span class="normal">1432</span>
<span class="normal">1433</span>
<span class="normal">1434</span>
<span class="normal">1435</span>
<span class="normal">1436</span>
<span class="normal">1437</span>
<span class="normal">1438</span>
<span class="normal">1439</span>
<span class="normal">1440</span>
<span class="normal">1441</span>
<span class="normal">1442</span>
<span class="normal">1443</span>
<span class="normal">1444</span>
<span class="normal">1445</span>
<span class="normal">1446</span>
<span class="normal">1447</span>
<span class="normal">1448</span>
<span class="normal">1449</span>
<span class="normal">1450</span>
<span class="normal">1451</span>
<span class="normal">1452</span>
<span class="normal">1453</span>
<span class="normal">1454</span>
<span class="normal">1455</span>
<span class="normal">1456</span>
<span class="normal">1457</span>
<span class="normal">1458</span>
<span class="normal">1459</span>
<span class="normal">1460</span>
<span class="normal">1461</span>
<span class="normal">1462</span>
<span class="normal">1463</span>
<span class="normal">1464</span>
<span class="normal">1465</span>
<span class="normal">1466</span>
<span class="normal">1467</span>
<span class="normal">1468</span>
<span class="normal">1469</span>
<span class="normal">1470</span>
<span class="normal">1471</span>
<span class="normal">1472</span>
<span class="normal">1473</span>
<span class="normal">1474</span>
<span class="normal">1475</span>
<span class="normal">1476</span>
<span class="normal">1477</span>
<span class="normal">1478</span>
<span class="normal">1479</span>
<span class="normal">1480</span>
<span class="normal">1481</span>
<span class="normal">1482</span>
<span class="normal">1483</span>
<span class="normal">1484</span>
<span class="normal">1485</span>
<span class="normal">1486</span>
<span class="normal">1487</span>
<span class="normal">1488</span>
<span class="normal">1489</span>
<span class="normal">1490</span>
<span class="normal">1491</span>
<span class="normal">1492</span>
<span class="normal">1493</span>
<span class="normal">1494</span></pre></div></td><td class="code"><div><pre><div id="l1" class="code_block"><span class="k">\chapter</span><span class="nb">{</span>Embedding XSB in a Process<span class="nb">}</span>
</div><div id="l2" class="code_block"><span class="c">%===================================</span>
</div><div id="l3" class="code_block"><span class="k">\label</span><span class="nb">{</span>ccallingxsb<span class="nb">}</span>
</div><div id="l4" class="code_block">
</div><div id="l5" class="code_block">There are many situations in which it is desirable to use XSB as a
</div><div id="l6" class="code_block">rule- or constraint- processing subcomponent of a larger system that
</div><div id="l7" class="code_block">is written in another language.  Depending on the intended
</div><div id="l8" class="code_block">architecture, it may be appropriate for XSB to reside in its own
</div><div id="l9" class="code_block">process, separate from other components of an application, and
</div><div id="l10" class="code_block">communicating through sockets, a database, or some other mechanism.
</div><div id="l11" class="code_block">However it is often useful for XSB to reside in the same process as
</div><div id="l12" class="code_block">other components.  To do this, one wants to be able to <span class="nb">{</span><span class="k">\em</span> call<span class="nb">}</span><span class="k">\</span> XSB
</div><div id="l13" class="code_block">from the host language, providing queries for XSB to evaluate, and
</div><div id="l14" class="code_block">retrieving back the answers.  An interface for calling XSB from C is
</div><div id="l15" class="code_block">provided for this purpose and is described in this chapter.  Based on
</div><div id="l16" class="code_block">this C interface, XSB can also be called from Java either through a
</div><div id="l17" class="code_block">JNI or a socket-based interface, as described in the
</div><div id="l18" class="code_block">documentation for Interprolog, available through <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l19" class="code_block">  xsb.sourceforge.net<span class="nb">}</span>.  To call XSB from Visual Basic, a DLL is
</div><div id="l20" class="code_block">created as described in this chapter, and additional declarations must
</div><div id="l21" class="code_block">be made in visual basic as described in the web page ``How to use XSB
</div><div id="l22" class="code_block">DLL from Visual Basic&#39;&#39; <span class="k">\url</span><span class="nb">{</span>http://xsb.sourceforge.net/vbdll.html<span class="nb">}</span>.
</div><div id="l23" class="code_block">In addition, the interface described in this chapter has also been
</div><div id="l24" class="code_block">extended to allow XSB to be called from Delphi and Ruby.  However,
</div><div id="l25" class="code_block">since all of these interfaces -- Java, Ruby, Delphi and Visual Basic --
</div><div id="l26" class="code_block">depend on XSB&#39;s C API, we refer in this chapter to C programs or
</div><div id="l27" class="code_block">threads calling XSB, although each of the examples suitably modified
</div><div id="l28" class="code_block">can be extended to other calling languages.
</div><div id="l29" class="code_block">
</div><div id="l30" class="code_block">New to Version 3.1 are extensions to the C API to allow multiple XSB
</div><div id="l31" class="code_block">threads to be called from multiple C threads~<span class="k">\footnote</span><span class="nb">{</span>XSB&#39;s threading
</div><div id="l32" class="code_block">  model is based on POSIX threads, which can be called in Windows
</div><div id="l33" class="code_block">  through a variety of POSIX APIs -- see Volume 1 chapter 8 <span class="nb">{</span><span class="k">\em</span>
</div><div id="l34" class="code_block">    Multi-threaded Programming in XSB<span class="nb">}</span>.<span class="nb">}</span>.  In this Chapter, we provide
</div><div id="l35" class="code_block">an overview of XSB&#39;s C API, and then elaborate its use through a
</div><div id="l36" class="code_block">series of examples, beginning with a single XSB thread called by a
</div><div id="l37" class="code_block">single C thread, then showing how a C thread can interact with
</div><div id="l38" class="code_block">multiple XSB threads, and finally discuss how multiple XSB threads can
</div><div id="l39" class="code_block">interact with multiple POSIX threads.  Finally, Section~<span class="k">\ref</span><span class="nb">{</span>sec:CAPI<span class="nb">}</span>
</div><div id="l40" class="code_block">describes each C function in the API.
</div><div id="l41" class="code_block">
</div><div id="l42" class="code_block"><span class="c">%</span>
</div><div id="l43" class="code_block"><span class="k">\section</span><span class="nb">{</span>Calling XSB from C<span class="nb">}</span>
</div><div id="l44" class="code_block">
</div><div id="l45" class="code_block">XSB provides several C functions (declared in <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l46" class="code_block">  <span class="k">\$</span>XSBDIR/emu/cinterf.h<span class="nb">}</span> and defined in <span class="k">\\</span> <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l47" class="code_block">  <span class="k">\$</span>XSBDIR/emu/cinterf.c<span class="nb">}</span>), which can be called from C to interact
</div><div id="l48" class="code_block">with XSB as a subroutine. These functions allow a C program to
</div><div id="l49" class="code_block">interact with XSB in a number of ways.
</div><div id="l50" class="code_block"><span class="k">\begin</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l51" class="code_block"><span class="k">\item</span> XSB may be initialized, using most of the parameters available
</div><div id="l52" class="code_block">  from the command-line. 
</div><div id="l53" class="code_block"><span class="c">%</span>
</div><div id="l54" class="code_block"><span class="k">\item</span> XSB may then execute a series of <span class="nb">{</span><span class="k">\em</span> commands<span class="nb">}</span> or <span class="nb">{</span><span class="k">\em</span>
</div><div id="l55" class="code_block">  queries<span class="nb">}</span>.  A command is a deterministic query which simply succeeds
</div><div id="l56" class="code_block">  or fails without performing any unification on the query term.  On
</div><div id="l57" class="code_block">  the other hand, a non-deterministic query can be evaluated so that
</div><div id="l58" class="code_block">  its answer substitutions are retrieved one at a time, as they are
</div><div id="l59" class="code_block">  produced, just as if XSB were called on a command line.  Alternately
</div><div id="l60" class="code_block">  a non-deterministic query can be closed in the case where not every
</div><div id="l61" class="code_block">  answer to the query is needed.  Only one query per thread can be
</div><div id="l62" class="code_block">  active at a time.  I.e., an application must completely finish
</div><div id="l63" class="code_block">  processing one query to a given thread <span class="s">$</span><span class="nb">T</span><span class="s">$</span> (either by retrieving all
</div><div id="l64" class="code_block">  the answers for it, or by issuing a call to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l65" class="code_block">    xsb<span class="k">\_close\_query</span>()<span class="nb">}</span>, before trying to evaluate another using <span class="s">$</span><span class="nb">T</span><span class="s">$</span>.
</div><div id="l66" class="code_block"><span class="c">%</span>
</div><div id="l67" class="code_block"><span class="k">\item</span> Finally, XSB can be closed, so that no more queries can be made
</div><div id="l68" class="code_block">  to any XSB threads.
</div><div id="l69" class="code_block"><span class="k">\end</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l70" class="code_block">
</div><div id="l71" class="code_block">In general, while any functions in the C API to XSB can be intermixed,
</div><div id="l72" class="code_block">the functions can be classified as belonging to three different
</div><div id="l73" class="code_block">levels.
</div><div id="l74" class="code_block"><span class="c">%</span>
</div><div id="l75" class="code_block"><span class="k">\begin</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l76" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\em</span> A VarString level<span class="nb">}</span> which uses an XSB-specific C-type
</div><div id="l77" class="code_block">  definition for variable-length strings
</div><div id="l78" class="code_block">  (Section~<span class="k">\ref</span><span class="nb">{</span>sec-varstring<span class="nb">}</span>), to return answers.
</div><div id="l79" class="code_block"><span class="c">%</span>
</div><div id="l80" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\em</span> A fixed-string level<span class="nb">}</span> provides routines that return answers
</div><div id="l81" class="code_block">  in fixed-length strings.
</div><div id="l82" class="code_block"><span class="c">%</span>
</div><div id="l83" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\em</span> A register-oriented level<span class="nb">}</span> that requires users to set up
</div><div id="l84" class="code_block">  queries by setting registers for XSB which are made globally
</div><div id="l85" class="code_block">  available to calling functions.  The mechanisms for this resemble
</div><div id="l86" class="code_block">  the lower-level C interface discussed in Chapter~<span class="k">\ref</span><span class="nb">{</span>foreign<span class="nb">}</span>.
</div><div id="l87" class="code_block">  This level of interface should only be used for the single-threaded
</div><div id="l88" class="code_block">  applications, as it is difficult to prevent race-conditions at this
</div><div id="l89" class="code_block">  level of interface when multiple C threads are used to call XSB.
</div><div id="l90" class="code_block">
</div><div id="l91" class="code_block"><span class="k">\end</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l92" class="code_block"><span class="c">%</span>
</div><div id="l93" class="code_block">The appropriate level to use depends on the nature of the calling
</div><div id="l94" class="code_block">program, the speed desired, and the expertise of the programmer.  By
</div><div id="l95" class="code_block">and large, functions in the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> level are the the easiest
</div><div id="l96" class="code_block">and safest to use, but they depend on a C type definition that may not
</div><div id="l97" class="code_block">be available to all calling programs (e.g. it may be difficult to use
</div><div id="l98" class="code_block">if the calling program is not directly based on C, such as Visual
</div><div id="l99" class="code_block">Basic or Delphi).  For such applications functions from the
</div><div id="l100" class="code_block">fixed-string level would need to be used instead.  In general, most
</div><div id="l101" class="code_block">applications should use either functions from the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> or
</div><div id="l102" class="code_block">the fixed-string level, rather than the register-oriented level.  This
</div><div id="l103" class="code_block">latter level should only be used by programmers who are willing to
</div><div id="l104" class="code_block">work at a low interface level, when the utmost speed is needed by an
</div><div id="l105" class="code_block">application, and when multiple threads do not need to interact with
</div><div id="l106" class="code_block">XSB.
</div><div id="l107" class="code_block">
</div><div id="l108" class="code_block">
</div><div id="l109" class="code_block"><span class="k">\section</span><span class="nb">{</span>Examples of Calling XSB<span class="nb">}</span>
</div><div id="l110" class="code_block">
</div><div id="l111" class="code_block">We introduce a series of examples of how XSB would be called using the
</div><div id="l112" class="code_block">string-level interfaces.  Simple examples of the register-level
</div><div id="l113" class="code_block">interface are given in the <span class="nb">{</span><span class="k">\tt</span> XSB/examples/c<span class="k">\_calling\_XSB</span><span class="nb">}</span>
</div><div id="l114" class="code_block">subdirectory, in files <span class="nb">{</span><span class="k">\tt</span> cmain.c<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> cmain2.c<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> ctest.P<span class="nb">}</span>,
</div><div id="l115" class="code_block">and <span class="nb">{</span><span class="k">\tt</span> Makefile<span class="nb">}</span>, but are not discussed in this section.
</div><div id="l116" class="code_block">
</div><div id="l117" class="code_block">We structure out discussion by first showing how to construct a C
</div><div id="l118" class="code_block">program to call the single-threaded engine alone in
</div><div id="l119" class="code_block">Section~<span class="k">\ref</span><span class="nb">{</span>sec:CXSBSeq<span class="nb">}</span>.  This example is mostly pedagogic: with a
</div><div id="l120" class="code_block">small amount of extra coding a C program can be constructed to call
</div><div id="l121" class="code_block">both the single- and the multi-threaded engine, and these extensions
</div><div id="l122" class="code_block">are discussed in Section~<span class="k">\ref</span><span class="nb">{</span>sec:CXSB<span class="nb">}</span>.  Next, we show how to a C
</div><div id="l123" class="code_block">program can call and manage multiple XSB threads in Section~<span class="k">\ref</span><span class="nb">{</span>sec:CXSBMT<span class="nb">}</span>.
</div><div id="l124" class="code_block">Finally, we show how multiple XSB threads can interact with multiple C
</div><div id="l125" class="code_block">threads in Section~<span class="k">\ref</span><span class="nb">{</span>CXSBMTMT<span class="nb">}</span>.
</div><div id="l126" class="code_block">
</div><div id="l127" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>The XSB API for the Sequential Engine Only<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>sec:CXSBSeq<span class="nb">}</span>
</div><div id="l128" class="code_block">
</div><div id="l129" class="code_block">We start with a simple program shown, in Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span>,
</div><div id="l130" class="code_block">that will call the following XSB predicate
</div><div id="l131" class="code_block"><span class="c">%</span>
</div><div id="l132" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l133" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l134" class="code_block">  p(a,b,c).
</div><div id="l135" class="code_block">  p(1,2,3).
</div><div id="l136" class="code_block">  p([1,2],[3,4],[5,6]).
</div><div id="l137" class="code_block">  p(A,B,A).
</div><div id="l138" class="code_block">
</div><div id="l139" class="code_block">r(c,b,a).
</div><div id="l140" class="code_block">r(3,2,1).
</div><div id="l141" class="code_block">r([5,6],[3,4],[1,2]).
</div><div id="l142" class="code_block">r(<span class="nb">_</span>A,B,B).
</div><div id="l143" class="code_block">
</div><div id="l144" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l145" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l146" class="code_block"><span class="c">%</span>
</div><div id="l147" class="code_block">and backtrack through unifying answers (cf. <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l148" class="code_block">  <span class="k">\$</span>XSBDIR/examples/c<span class="k">\_calling\_xsb</span>/edb.P<span class="nb">}</span>).  .  This example will
</div><div id="l149" class="code_block">only compile properly if the sequential engine is used, and its style
</div><div id="l150" class="code_block">is <span class="nb">{</span><span class="k">\tt</span> not<span class="nb">}</span> recommended: it will be shown in Section~<span class="k">\ref</span><span class="nb">{</span>sec:CXSB<span class="nb">}</span>
</div><div id="l151" class="code_block">how to extend the style.
</div><div id="l152" class="code_block"><span class="c">%</span>
</div><div id="l153" class="code_block"><span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>[hbtp]
</div><div id="l154" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l155" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l156" class="code_block">#include &lt;stdio.h&gt;
</div><div id="l157" class="code_block">#include &lt;string.h&gt;
</div><div id="l158" class="code_block">
</div><div id="l159" class="code_block">/* cinterf.h is necessary for the XSB API, as well as the path manipulation routines*/
</div><div id="l160" class="code_block">#include &quot;cinterf.h&quot;
</div><div id="l161" class="code_block">
</div><div id="l162" class="code_block">extern char *xsb<span class="nb">_</span>executable<span class="nb">_</span>full<span class="nb">_</span>path(char *);
</div><div id="l163" class="code_block">extern char *strip<span class="nb">_</span>names<span class="nb">_</span>from<span class="nb">_</span>path(char*, int);
</div><div id="l164" class="code_block">
</div><div id="l165" class="code_block">int main(int argc, char *argv[]) <span class="nb">{</span> 
</div><div id="l166" class="code_block">
</div><div id="l167" class="code_block">  char init<span class="nb">_</span>string[1024];
</div><div id="l168" class="code_block">  int rc;
</div><div id="l169" class="code_block">  XSB<span class="nb">_</span>StrDefine(return<span class="nb">_</span>string);
</div><div id="l170" class="code_block">
</div><div id="l171" class="code_block">  /* xsb<span class="nb">_</span>init<span class="nb">_</span>string() relies on the calling program to pass the absolute or relative
</div><div id="l172" class="code_block">     path name of the XSB installation directory. We assume that the current
</div><div id="l173" class="code_block">     program is sitting in the directory ../examples/c<span class="nb">_</span>calling<span class="nb">_</span>xsb/
</div><div id="l174" class="code_block">     To get the installation directory, we strip 3 file names from the path. */
</div><div id="l175" class="code_block">
</div><div id="l176" class="code_block">  strcpy(init<span class="nb">_</span>string,strip<span class="nb">_</span>names<span class="nb">_</span>from<span class="nb">_</span>path(xsb<span class="nb">_</span>executable<span class="nb">_</span>full<span class="nb">_</span>path(argv[0]),3));
</div><div id="l177" class="code_block">
</div><div id="l178" class="code_block">  if (xsb<span class="nb">_</span>init<span class="nb">_</span>string(init<span class="nb">_</span>string) == XSB<span class="nb">_</span>ERROR) <span class="nb">{</span>
</div><div id="l179" class="code_block">    fprintf(stderr,&quot;++initializing XSB: <span class="c">%s/%s\n&quot;,xsb_get_init_error_type(),</span>
</div><div id="l180" class="code_block">            xsb<span class="nb">_</span>get<span class="nb">_</span>init<span class="nb">_</span>error<span class="nb">_</span>message());
</div><div id="l181" class="code_block">    exit(XSB<span class="nb">_</span>ERROR);
</div><div id="l182" class="code_block">  <span class="nb">}</span>
</div><div id="l183" class="code_block">
</div><div id="l184" class="code_block">  /* Create command to consult a file: edb.P, and send it. */
</div><div id="l185" class="code_block">  if (xsb<span class="nb">_</span>command<span class="nb">_</span>string(&quot;consult(&#39;edb.P&#39;).&quot;) == XSB<span class="nb">_</span>ERROR)
</div><div id="l186" class="code_block">    fprintf(stderr,&quot;++Error consulting edb.P: <span class="c">%s/%s\n&quot;,xsb_get_error_type(),xsb_get_error_message());</span>
</div><div id="l187" class="code_block">
</div><div id="l188" class="code_block">  rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(&quot;p(X,Y,Z).&quot;,<span class="nb">&amp;</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l189" class="code_block">  while (rc == XSB<span class="nb">_</span>SUCCESS) <span class="nb">{</span>
</div><div id="l190" class="code_block">    printf(&quot;Return <span class="c">%s\n&quot;,(return_string.string));</span>
</div><div id="l191" class="code_block">    rc = xsb<span class="nb">_</span>next<span class="nb">_</span>string(<span class="nb">&amp;</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l192" class="code_block">  <span class="nb">}</span>
</div><div id="l193" class="code_block"> 
</div><div id="l194" class="code_block"> if (rc == XSB<span class="nb">_</span>ERROR) 
</div><div id="l195" class="code_block">    fprintf(stderr,&quot;++Query Error: <span class="c">%s/%s\n&quot;xsb_get_error_type(),xsb_get_error_message());</span>
</div><div id="l196" class="code_block">
</div><div id="l197" class="code_block">  xsb<span class="nb">_</span>close();     
</div><div id="l198" class="code_block"><span class="nb">}</span>
</div><div id="l199" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l200" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l201" class="code_block"><span class="k">\caption</span><span class="nb">{</span>Calling the Sequential Engine Using the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> Interface<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span>
</div><div id="l202" class="code_block"><span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
</div><div id="l203" class="code_block">
</div><div id="l204" class="code_block">We discuss the program in Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span> in detail.
</div><div id="l205" class="code_block">This program, slightly modified so that it compiles with the
</div><div id="l206" class="code_block">multi-threaded engine is in <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l207" class="code_block">  <span class="k">\$</span>XSBDIR/examples/c<span class="k">\_calling\_xsb</span>/cvartest.c<span class="nb">}</span>.  An executable for
</div><div id="l208" class="code_block">this program can be make most easily by calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l209" class="code_block">  <span class="k">\$</span>XSBDIR/examples/c<span class="k">\_calling\_xsb</span>/make.P<span class="nb">}</span>, which makes the
</div><div id="l210" class="code_block">executable <span class="nb">{</span><span class="k">\tt</span> cvstest<span class="nb">}</span>.
</div><div id="l211" class="code_block">
</div><div id="l212" class="code_block">The program begins by including some standard C headers: note that
</div><div id="l213" class="code_block"><span class="nb">{</span><span class="k">\tt</span> string.h<span class="nb">}</span> is needed for string manipulation routines such as <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l214" class="code_block">  strcpy<span class="nb">}</span>.  In addition, the XSB library header <span class="nb">{</span><span class="k">\tt</span> cinterf.h<span class="nb">}</span> is
</div><div id="l215" class="code_block">necessary for the XSB C API.  Since the program in
</div><div id="l216" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span> uses functions in the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>
</div><div id="l217" class="code_block">interface, within <span class="nb">{</span><span class="k">\tt</span> main()<span class="nb">}</span> the routine <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l218" class="code_block">  XSB<span class="k">\_StrDefine</span>(return<span class="k">\_string</span>)<span class="nb">}</span> declares and initializes a structure
</div><div id="l219" class="code_block">of type <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>, named <span class="nb">{</span><span class="k">\tt</span> return<span class="k">\_string</span><span class="nb">}</span>.
</div><div id="l220" class="code_block">
</div><div id="l221" class="code_block">The next order of business is to initialize XSB.  In order to do this,
</div><div id="l222" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> needs to know the installation directory for
</div><div id="l223" class="code_block">XSB, which must be passed as part of the initialization string.  In
</div><div id="l224" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span> this is done by manipulating the path of
</div><div id="l225" class="code_block">the executable (<span class="nb">{</span><span class="k">\tt</span> cvstest<span class="nb">}</span>) that calls XSB.  In fact any other
</div><div id="l226" class="code_block">approach would also work as long as the XSB installation directory
</div><div id="l227" class="code_block">were passed.  Within the initialization string, other command line
</div><div id="l228" class="code_block">arguments can be passed to XSB if desired with the following
</div><div id="l229" class="code_block">exceptions: the arguments <span class="nb">{</span><span class="k">\tt</span> -B<span class="nb">}</span> (boot module), <span class="nb">{</span><span class="k">\tt</span> -D<span class="nb">}</span> (command
</div><div id="l230" class="code_block">loop driver), <span class="nb">{</span><span class="k">\tt</span> -i<span class="nb">}</span> (interpreter) and <span class="nb">{</span><span class="k">\tt</span> -d<span class="nb">}</span> (disassembler)
</div><div id="l231" class="code_block">cannot be used when calling XSB from a foreign language~<span class="k">\footnote</span><span class="nb">{</span>In
</div><div id="l232" class="code_block">  previous versions of XSB, initialization from the C level required a
</div><div id="l233" class="code_block">  <span class="nb">{</span><span class="k">\tt</span> -n<span class="nb">}</span> option to be passed.  This is no longer required.<span class="nb">}</span>.  As a
</div><div id="l234" class="code_block">final point on initialization, note that the function <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l235" class="code_block">  xsb<span class="k">\_init</span>()<span class="nb">}</span> can also be used to initialize XSB based on an argument
</div><div id="l236" class="code_block">vector and count (see Section~<span class="k">\ref</span><span class="nb">{</span>sec:CAPI<span class="nb">}</span>).
</div><div id="l237" class="code_block">
</div><div id="l238" class="code_block">Note that the calling program checks for any errors returned by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l239" class="code_block">  xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> and other API commands.  In general, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l240" class="code_block">  xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> may throw an error if the XSB&#39;s installation
</div><div id="l241" class="code_block">directory has become corrupted, or for similar reasons.  This
</div><div id="l242" class="code_block">mechanism for error handling is different than that used if XSB is
</div><div id="l243" class="code_block">called in its usual stand-alone mode, in which case such an error
</div><div id="l244" class="code_block">would cause XSB to exit).  An error returned by XSB&#39;s API are similar
</div><div id="l245" class="code_block">to an error ball described in Volume 1 <span class="nb">{</span><span class="k">\em</span> Exception Handling<span class="nb">}</span> in
</div><div id="l246" class="code_block">that it has both a <span class="nb">{</span><span class="k">\em</span> type<span class="nb">}</span> and a <span class="nb">{</span><span class="k">\em</span> message<span class="nb">}</span>.  For normal Prolog
</div><div id="l247" class="code_block">exceptions, XSB&#39;s API will throw the same kinds of errors as XSB
</div><div id="l248" class="code_block">called in a stand-alone (or server) mode, i.e. instantiation errors,
</div><div id="l249" class="code_block">type errors, etc.  However XSB&#39;s API adds two new error types:
</div><div id="l250" class="code_block"><span class="c">%</span>
</div><div id="l251" class="code_block"><span class="k">\begin</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l252" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> init<span class="k">\_error</span><span class="nb">}</span> is used as the type of an error discovered
</div><div id="l253" class="code_block">  upon initialization of XSB, before query and command processing has
</div><div id="l254" class="code_block">  begun.  If an <span class="nb">{</span><span class="k">\tt</span> init<span class="k">\_error</span><span class="nb">}</span> is raised, XSB has not been properly
</div><div id="l255" class="code_block">  initialized and will not run.
</div><div id="l256" class="code_block"><span class="c">%</span>
</div><div id="l257" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> unrecoverable<span class="k">\_error</span><span class="nb">}</span> is used to indicate that XSB has
</div><div id="l258" class="code_block">  encountered an error, (such as a memory allocation error), during
</div><div id="l259" class="code_block">  command or query processing from which it cannot recover.  Such an
</div><div id="l260" class="code_block">  error would cause XSB to immediately exit if it were called in a
</div><div id="l261" class="code_block">  stand-alone mode.  In general the calling program should handle
</div><div id="l262" class="code_block">  unrecoverable errors as fatal since there is a good chance that the
</div><div id="l263" class="code_block">  error conditions will affect the calling program as well as XSB.
</div><div id="l264" class="code_block"><span class="k">\end</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l265" class="code_block"><span class="c">%</span>
</div><div id="l266" class="code_block">Errors raised by <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> usually have type <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l267" class="code_block">  init<span class="k">\_type</span><span class="nb">}</span>.  
</div><div id="l268" class="code_block">
</div><div id="l269" class="code_block"> and a string pointer to the associated message can be
</div><div id="l270" class="code_block">found by the function <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_init\_error\_message</span>()<span class="nb">}</span>.  
</div><div id="l271" class="code_block">
</div><div id="l272" class="code_block">As can be seen from the example, handling errors from commands is done
</div><div id="l273" class="code_block">in manner similar to that of initialization.  For non-initialization
</div><div id="l274" class="code_block">errors, a string pointer to the type can be obtained by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l275" class="code_block">  xsb<span class="k">\_get\_error\_type</span>()<span class="nb">}</span>, while a string pointer to the message can
</div><div id="l276" class="code_block">be obtained by <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_error\_message</span>()<span class="nb">}</span>.  
</div><div id="l277" class="code_block"><span class="c">%The reason that</span>
</div><div id="l278" class="code_block"><span class="c">%initialization errors are handled separately is discussed in</span>
</div><div id="l279" class="code_block"><span class="c">%Section~\ref{}.  </span>
</div><div id="l280" class="code_block">
</div><div id="l281" class="code_block">Next in Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span> the file <span class="nb">{</span><span class="k">\tt</span> edb.P<span class="nb">}</span> is consulted
</div><div id="l282" class="code_block">(containing the <span class="nb">{</span><span class="k">\tt</span> p/3<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> r/3<span class="nb">}</span> predicates shown above).
</div><div id="l283" class="code_block">Note, that the argument to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command\_string</span><span class="nb">}</span> must be a
</div><div id="l284" class="code_block">syntactically valid Prolog term ending with a period, otherwise a
</div><div id="l285" class="code_block">syntax error will be thrown, which may be displayed through <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l286" class="code_block">  xsb<span class="k">\_get\_error\_type</span>()<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l287" class="code_block">  xsb<span class="k">\_get\_error\_message</span>()<span class="nb">}</span>~<span class="k">\footnote</span><span class="nb">{</span>Most XSB errors are handled in
</div><div id="l288" class="code_block">  this manner when XSB is called through its API.  A few errors will
</div><div id="l289" class="code_block">  print directly to <span class="nb">{</span><span class="k">\tt</span> stderr<span class="nb">}</span> and some XSB warnings will print to
</div><div id="l290" class="code_block">  <span class="nb">{</span><span class="k">\tt</span> stdwarn<span class="nb">}</span> which upon startup is dup-ed to <span class="nb">{</span><span class="k">\tt</span> stderr<span class="nb">}</span>.<span class="nb">}</span>.
</div><div id="l291" class="code_block">
</div><div id="l292" class="code_block">Queries to XSB are a little more complicated than commands.  Since a
</div><div id="l293" class="code_block">query may return multiple solutions, a query should usually be called
</div><div id="l294" class="code_block">from inside a loop.  In Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex<span class="nb">}</span>, the query is
</div><div id="l295" class="code_block">opened with <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span>()<span class="nb">}</span>.  If the query has at least one
</div><div id="l296" class="code_block">answer, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span>()<span class="nb">}</span> will return <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span>; if
</div><div id="l297" class="code_block">the query fails, it will return <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span>, and if there is an
</div><div id="l298" class="code_block">exception it will return <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> as usual.  Any answer will
</div><div id="l299" class="code_block">be returned as a string in the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> <span class="nb">{</span><span class="k">\tt</span> return<span class="k">\_string</span><span class="nb">}</span>,
</div><div id="l300" class="code_block">and each argument of the query will be separated by the character <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l301" class="code_block">  |<span class="nb">}</span>.  Thus, in our example, the first answer will write the string
</div><div id="l302" class="code_block"><span class="c">%</span>
</div><div id="l303" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l304" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l305" class="code_block">a|b|c
</div><div id="l306" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l307" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l308" class="code_block"><span class="c">% </span>
</div><div id="l309" class="code_block">Once a query has been opened, subsequent answers can be obtained via
</div><div id="l310" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string</span>()<span class="nb">}</span>.  These answers are written to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l311" class="code_block">  return<span class="k">\_string</span><span class="nb">}</span> in the same manner as <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l312" class="code_block">  xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span>.
</div><div id="l313" class="code_block"><span class="c">%</span>
</div><div id="l314" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l315" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l316" class="code_block">1|2|3
</div><div id="l317" class="code_block">[1,2]|[3,4]|[5,6]
</div><div id="l318" class="code_block"><span class="nb">_</span>h102|<span class="nb">_</span>h116|<span class="nb">_</span>h102
</div><div id="l319" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l320" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l321" class="code_block"><span class="c">%</span>
</div><div id="l322" class="code_block">A query is automatically closed when no more answers can be derived
</div><div id="l323" class="code_block">from it.  Alternately, a query that may have answers remaining can be
</div><div id="l324" class="code_block">closed using the command <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_close\_query</span>()<span class="nb">}</span>.  If the calling
</div><div id="l325" class="code_block">application will need to pass more queries or commands to XSB nothing
</div><div id="l326" class="code_block">need be done at this point: a new queries or commands can be invoked
</div><div id="l327" class="code_block">using one of the functions just discussed.  However if the calling
</div><div id="l328" class="code_block">process is finished with XSB and will never need it again during the
</div><div id="l329" class="code_block">life of the process, it can call <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_close</span>()<span class="nb">}</span>.
</div><div id="l330" class="code_block">
</div><div id="l331" class="code_block"><span class="k">\subsubsection</span><span class="nb">{</span>An Example using Fixed Strings<span class="nb">}</span>
</div><div id="l332" class="code_block">
</div><div id="l333" class="code_block"><span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>[hbtp]
</div><div id="l334" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l335" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l336" class="code_block">  int retsize = 15;
</div><div id="l337" class="code_block">  char *return<span class="nb">_</span>string;
</div><div id="l338" class="code_block">  int anslen;
</div><div id="l339" class="code_block">
</div><div id="l340" class="code_block">  return<span class="nb">_</span>string = malloc(retsize);
</div><div id="l341" class="code_block">
</div><div id="l342" class="code_block">  rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string<span class="nb">_</span>b(CTXTc &quot;p(X,Y,Z).&quot;,return<span class="nb">_</span>string,retsize,<span class="nb">&amp;</span>anslen,&quot;|&quot;);
</div><div id="l343" class="code_block">
</div><div id="l344" class="code_block">  while (rc == XSB<span class="nb">_</span>SUCCESS || rc == XSB<span class="nb">_</span>OVERFLOW) <span class="nb">{</span>
</div><div id="l345" class="code_block">  
</div><div id="l346" class="code_block">    if (rc == XSB<span class="nb">_</span>OVERFLOW) <span class="nb">{</span>
</div><div id="l347" class="code_block">      return<span class="nb">_</span>string = (char *) realloc(return<span class="nb">_</span>string,anslen);
</div><div id="l348" class="code_block">      return<span class="nb">_</span>size = anslen;
</div><div id="l349" class="code_block">      rc = xsb<span class="nb">_</span>get<span class="nb">_</span>last<span class="nb">_</span>answer<span class="nb">_</span>string(CTXTc return<span class="nb">_</span>string,retsize,<span class="nb">&amp;</span>anslen);
</div><div id="l350" class="code_block">    <span class="nb">}</span>    
</div><div id="l351" class="code_block">
</div><div id="l352" class="code_block">    printf(&quot;Return <span class="c">%s %d\n&quot;,return_string,anslen);</span>
</div><div id="l353" class="code_block">    rc = xsb<span class="nb">_</span>next<span class="nb">_</span>string<span class="nb">_</span>b(CTXTc return<span class="nb">_</span>string,15,<span class="nb">&amp;</span>anslen,&quot;|&quot;);
</div><div id="l354" class="code_block">  <span class="nb">}</span>
</div><div id="l355" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l356" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l357" class="code_block"><span class="k">\caption</span><span class="nb">{</span>Calling XSB using the Fixed String Interface<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>fig:fixedstringex<span class="nb">}</span>
</div><div id="l358" class="code_block"><span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
</div><div id="l359" class="code_block">
</div><div id="l360" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:fixedstringex<span class="nb">}</span> shows a fragment of code indicating how
</div><div id="l361" class="code_block">the previous example would be modified if the fixed-string interface
</div><div id="l362" class="code_block">were used.  Note that <span class="nb">{</span><span class="k">\tt</span> return<span class="k">\_string</span><span class="nb">}</span> now becomes a pointer to
</div><div id="l363" class="code_block">explicitly malloc-ed memory.  To open the query <span class="nb">{</span><span class="k">\tt</span> p(X,Y,Z)<span class="nb">}</span> the
</div><div id="l364" class="code_block">function <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span> is called, with the
</div><div id="l365" class="code_block"><span class="nb">{</span><span class="k">\tt</span> <span class="k">\_b</span><span class="nb">}</span> indicating that a fixed buffer is being used rather than a
</div><div id="l366" class="code_block"><span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>.  The call is similar to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l367" class="code_block">  xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span>, except that the length <span class="nb">{</span><span class="k">\tt</span> anslen<span class="nb">}</span>
</div><div id="l368" class="code_block">of the buffer pointed to by <span class="nb">{</span><span class="k">\tt</span> return<span class="k">\_string</span><span class="nb">}</span> is now also required.
</div><div id="l369" class="code_block">If the answer to be returned (including separators) is longer than
</div><div id="l370" class="code_block"><span class="nb">{</span><span class="k">\tt</span> anslen<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span> will return <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l371" class="code_block">  XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span>.  If this happens, a new answer buffer can be used
</div><div id="l372" class="code_block">(here the old one is realloc-ed) and the answer retrieved via <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l373" class="code_block">  xsb<span class="k">\_get\_last\_answer\_string</span><span class="nb">}</span>.  Similarly, further answers are
</div><div id="l374" class="code_block">obtained via <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string\_b</span>()<span class="nb">}</span> whose length must be
</div><div id="l375" class="code_block">checked.  Thus the only difference between the fixed-string level and
</div><div id="l376" class="code_block">the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> level is that the length of each answer should be
</div><div id="l377" class="code_block">checked and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_last\_answer\_string</span>()<span class="nb">}</span> called if
</div><div id="l378" class="code_block">necessary.
</div><div id="l379" class="code_block">
</div><div id="l380" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>The General XSB API<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>sec:CXSB<span class="nb">}</span>
</div><div id="l381" class="code_block">
</div><div id="l382" class="code_block">The previous section showed how to use the XSB API with both the <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l383" class="code_block">  VarString<span class="nb">}</span> type and without, but did not consider the multi-threaded
</div><div id="l384" class="code_block">engine.  In fact, there are different ways to use XSB&#39;s
</div><div id="l385" class="code_block">multi-threading that can have advantages for various situations.  In
</div><div id="l386" class="code_block">the first mode, threads are managed from Prolog, with a single XSB
</div><div id="l387" class="code_block">thread called from the API; that XSB thread can then create another
</div><div id="l388" class="code_block">XSB thread that does work, and the first thread can return almost
</div><div id="l389" class="code_block">immediately to handle more requests from the API&#39;s caller.  A second
</div><div id="l390" class="code_block">model allows the caller to manipulate a pool of several XSB threads,
</div><div id="l391" class="code_block">so that different XSB threads may be called from different threads
</div><div id="l392" class="code_block">over the API.  In this model each C, Java, Ruby, or other thread could
</div><div id="l393" class="code_block">a number of different Prolog threads.  In this section we sketch how
</div><div id="l394" class="code_block">to use the API to illustrate the first model, and sketch the second
</div><div id="l395" class="code_block">model in the next section.
</div><div id="l396" class="code_block">
</div><div id="l397" class="code_block"><span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>[hbtp]
</div><div id="l398" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l399" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l400" class="code_block">.....
</div><div id="l401" class="code_block">
</div><div id="l402" class="code_block">/* context.h is necessary for the type of a thread context. */
</div><div id="l403" class="code_block">#include &quot;context.h&quot;
</div><div id="l404" class="code_block">
</div><div id="l405" class="code_block">int main(int argc, char *argv[])
</div><div id="l406" class="code_block"><span class="nb">{</span> 
</div><div id="l407" class="code_block">
</div><div id="l408" class="code_block">  char init<span class="nb">_</span>string[MAXPATHLEN];
</div><div id="l409" class="code_block">  int rc;
</div><div id="l410" class="code_block">  XSB<span class="nb">_</span>StrDefine(return<span class="nb">_</span>string);
</div><div id="l411" class="code_block">
</div><div id="l412" class="code_block">  strcpy(init<span class="nb">_</span>string,strip<span class="nb">_</span>names<span class="nb">_</span>from<span class="nb">_</span>path(xsb<span class="nb">_</span>executable<span class="nb">_</span>full<span class="nb">_</span>path(argv[0]),3));
</div><div id="l413" class="code_block">  if (xsb<span class="nb">_</span>init<span class="nb">_</span>string(init<span class="nb">_</span>string) == XSB<span class="nb">_</span>ERROR) <span class="nb">{</span>
</div><div id="l414" class="code_block">    fprintf(stderr,&quot;++initializing XSB: <span class="c">%s/%s\n&quot;,xsb_get_init_error_type(),</span>
</div><div id="l415" class="code_block">            xsb<span class="nb">_</span>get<span class="nb">_</span>init<span class="nb">_</span>error<span class="nb">_</span>message());
</div><div id="l416" class="code_block">    exit(XSB<span class="nb">_</span>ERROR);
</div><div id="l417" class="code_block">  <span class="nb">}</span>
</div><div id="l418" class="code_block">
</div><div id="l419" class="code_block">#ifdef MULTI<span class="nb">_</span>THREAD
</div><div id="l420" class="code_block">  th<span class="nb">_</span>context *th = xsb<span class="nb">_</span>get<span class="nb">_</span>main<span class="nb">_</span>thread();
</div><div id="l421" class="code_block">#endif
</div><div id="l422" class="code_block">
</div><div id="l423" class="code_block">  /* Create command to consult a file: edb.P, and send it. */
</div><div id="l424" class="code_block">  if (xsb<span class="nb">_</span>command<span class="nb">_</span>string(CTXTc &quot;consult(&#39;edb.P&#39;).&quot;) == XSB<span class="nb">_</span>ERROR)
</div><div id="l425" class="code_block">    fprintf(stderr,&quot;++Error consulting edb.P: <span class="c">%s/%s\n&quot;,xsb_get_error_type(CTXT),</span>
</div><div id="l426" class="code_block">            xsb<span class="nb">_</span>get<span class="nb">_</span>error<span class="nb">_</span>message(CTXT));
</div><div id="l427" class="code_block">
</div><div id="l428" class="code_block">  rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(CTXTc &quot;p(X,Y,Z).&quot;,<span class="nb">&amp;</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l429" class="code_block">  while (rc == XSB<span class="nb">_</span>SUCCESS) <span class="nb">{</span>
</div><div id="l430" class="code_block">    printf(&quot;Return <span class="c">%s\n&quot;,(return_string.string));</span>
</div><div id="l431" class="code_block">    rc = xsb<span class="nb">_</span>next<span class="nb">_</span>string(CTXTc <span class="nb">&amp;</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l432" class="code_block">  <span class="nb">}</span>
</div><div id="l433" class="code_block"> 
</div><div id="l434" class="code_block"> if (rc == XSB<span class="nb">_</span>ERROR) 
</div><div id="l435" class="code_block">    fprintf(stderr,&quot;++Query Error: <span class="c">%s/%s\n&quot;,xsb_get_error_type(CTXT),xsb_get_error_message(CTXT));</span>
</div><div id="l436" class="code_block">
</div><div id="l437" class="code_block">  xsb<span class="nb">_</span>close();     
</div><div id="l438" class="code_block"><span class="nb">}</span>
</div><div id="l439" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l440" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l441" class="code_block"><span class="k">\caption</span><span class="nb">{</span>Calling the Single- or Multi-Threaded Engine Using the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> Interface<span class="nb">}</span> 
</div><div id="l442" class="code_block"><span class="k">\label</span><span class="nb">{</span>fig:varstringex1<span class="nb">}</span>
</div><div id="l443" class="code_block"><span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
</div><div id="l444" class="code_block">
</div><div id="l445" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex1<span class="nb">}</span> shows how relevant portions of the
</div><div id="l446" class="code_block">previous <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> example can be adapted to use the
</div><div id="l447" class="code_block">multi-threaded engine.  The main change is that a new variable is
</div><div id="l448" class="code_block">introduced on the C side that points to the context of the main
</div><div id="l449" class="code_block">thread.  As pointed out in Chapter~<span class="k">\ref</span><span class="nb">{</span>foreign<span class="nb">}</span>, each thread in the
</div><div id="l450" class="code_block">multi-threaded engine has a <span class="nb">{</span><span class="k">\em</span> context<span class="nb">}</span> in which is kept much of its
</div><div id="l451" class="code_block">thread-specific data (excluding tables and dynamic code).  Of the
</div><div id="l452" class="code_block">threads running in the multi-threaded engine the thread created upon
</div><div id="l453" class="code_block">the call to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init</span>()<span class="nb">}</span> is designated as the <span class="nb">{</span><span class="k">\tt</span> main thread<span class="nb">}</span>,
</div><div id="l454" class="code_block">and is closed only upon calling <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_close</span>()<span class="nb">}</span>.
</div><div id="l455" class="code_block">
</div><div id="l456" class="code_block">Within the multi-threaded engine, a call to an API function such as
</div><div id="l457" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span> is actually a call to a specific
</div><div id="l458" class="code_block">thread to do some work (using a thread context pointer).  Accordingly,
</div><div id="l459" class="code_block">since any errors produced will be specific to a given thread, all
</div><div id="l460" class="code_block">calls to error reporting functions are also thread-specific.  If no
</div><div id="l461" class="code_block">specific thread is needed, it may be best just to use the main thread,
</div><div id="l462" class="code_block">which is what is done in Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex1<span class="nb">}</span>.  The thread
</div><div id="l463" class="code_block">context pointer <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> is initialized to the main thread using the
</div><div id="l464" class="code_block">API macro <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_main\_thread</span>()<span class="nb">}</span>.  Afterwards, this pointer is
</div><div id="l465" class="code_block">passed into the various interface functions by making use of XSB
</div><div id="l466" class="code_block">macros defined in <span class="nb">{</span><span class="k">\tt</span> context.h<span class="nb">}</span> In the multi-threaded engine, these
</div><div id="l467" class="code_block">macros are defined as
</div><div id="l468" class="code_block"><span class="c">%</span>
</div><div id="l469" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l470" class="code_block">#define CTXT th
</div><div id="l471" class="code_block">#define CTXTc th,
</div><div id="l472" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l473" class="code_block"><span class="c">%</span>
</div><div id="l474" class="code_block">while in the single-threaded engine they are defined as empty strings,
</div><div id="l475" class="code_block">as is <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_main\_thread</span>()<span class="nb">}</span>.  As a result the code in
</div><div id="l476" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex1<span class="nb">}</span> will compile and run properly both for
</div><div id="l477" class="code_block">the single-threaded and the multi-threaded engines.
</div><div id="l478" class="code_block">
</div><div id="l479" class="code_block">At this stage, suppose one wanted a new thread to execute a specific
</div><div id="l480" class="code_block">command, say <span class="nb">{</span><span class="k">\tt</span> do<span class="k">\_foo</span><span class="nb">}</span>.  In this case, a C call such as 
</div><div id="l481" class="code_block"><span class="c">%</span>
</div><div id="l482" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l483" class="code_block">xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(CTXTc &quot;thread<span class="nb">_</span>create(do<span class="nb">_</span>foo,Id).&quot;,<span class="nb">&amp;</span>return<span class="nb">_</span>string,&quot;|&quot;)
</div><div id="l484" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l485" class="code_block">creates a thread to execute the command, and returns the thread id of
</div><div id="l486" class="code_block">the newly created thread in <span class="nb">{</span><span class="k">\tt</span> return<span class="k">\_string</span><span class="nb">}</span>.  The behavior of
</div><div id="l487" class="code_block">this newly created thread is exactly the same as if it were created
</div><div id="l488" class="code_block">from the XSB command line: in particular the newly created thread will
</div><div id="l489" class="code_block">automatically exit upon completion of its command.  As a somewhat
</div><div id="l490" class="code_block">technical point, there are two different ways of referring to XSB
</div><div id="l491" class="code_block">threads.  The foreign language interfaces described in
</div><div id="l492" class="code_block">Chapter~<span class="k">\ref</span><span class="nb">{</span>foreign<span class="nb">}</span> and here use pointers to thread contexts so that
</div><div id="l493" class="code_block">the interfaces use much of the same code as the XSB engine.  However
</div><div id="l494" class="code_block">Prolog refers to threads using thread identifiers.  The two different
</div><div id="l495" class="code_block">forms can be converted into each other by the functions <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l496" class="code_block">  xsb<span class="k">\_thread\_id\_to\_context</span>()<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l497" class="code_block">  xsb<span class="k">\_thread\_context\_to\_id</span>()<span class="nb">}</span>.
</div><div id="l498" class="code_block">
</div><div id="l499" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Managing Multiple XSB Threads through the API<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>sec:CXSBMT<span class="nb">}</span>
</div><div id="l500" class="code_block">
</div><div id="l501" class="code_block">The ability to pass thread contexts into query and command functions
</div><div id="l502" class="code_block">allows a great deal of flexibility~<span class="k">\footnote</span><span class="nb">{</span>For the sake of brevity,
</div><div id="l503" class="code_block">  we sometimes abuse notation and do not always distinguish between
</div><div id="l504" class="code_block">  thread-contexts and their pointers.<span class="nb">}</span>.  Once XSB is initialized, XSB
</div><div id="l505" class="code_block">threads can be created from C and can execute independently of each
</div><div id="l506" class="code_block">other, effectively giving the ability for different calling threads to
</div><div id="l507" class="code_block">query XSB in a mechanism reminiscent of database cursors.
</div><div id="l508" class="code_block">
</div><div id="l509" class="code_block"><span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>[hbtp]
</div><div id="l510" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l511" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l512" class="code_block">.....
</div><div id="l513" class="code_block">/* context.h is necessary for the type of a thread context. */
</div><div id="l514" class="code_block">#include &quot;context.h&quot;
</div><div id="l515" class="code_block">
</div><div id="l516" class="code_block">int main(int argc, char *argv[])
</div><div id="l517" class="code_block"><span class="nb">{</span> 
</div><div id="l518" class="code_block">
</div><div id="l519" class="code_block">  static th<span class="nb">_</span>context *p<span class="nb">_</span>th, *r<span class="nb">_</span>th;
</div><div id="l520" class="code_block">  char init<span class="nb">_</span>string[MAXPATHLEN];
</div><div id="l521" class="code_block">  int rcp, rcr;
</div><div id="l522" class="code_block">  XSB<span class="nb">_</span>StrDefine(p<span class="nb">_</span>return<span class="nb">_</span>string);
</div><div id="l523" class="code_block">  XSB<span class="nb">_</span>StrDefine(r<span class="nb">_</span>return<span class="nb">_</span>string);
</div><div id="l524" class="code_block">
</div><div id="l525" class="code_block">  strcpy(init<span class="nb">_</span>string,strip<span class="nb">_</span>names<span class="nb">_</span>from<span class="nb">_</span>path(xsb<span class="nb">_</span>executable<span class="nb">_</span>full<span class="nb">_</span>path(argv[0]),3));
</div><div id="l526" class="code_block">
</div><div id="l527" class="code_block">  if (xsb<span class="nb">_</span>init<span class="nb">_</span>string(init<span class="nb">_</span>string)) <span class="nb">{</span>
</div><div id="l528" class="code_block">    fprintf(stderr,&quot;<span class="c">%s initializing XSB: %s/%s\n&quot;,xsb_get_init_error_type(),</span>
</div><div id="l529" class="code_block">            xsb<span class="nb">_</span>get<span class="nb">_</span>init<span class="nb">_</span>error<span class="nb">_</span>message());cin
</div><div id="l530" class="code_block">    exit(XSB<span class="nb">_</span>ERROR);
</div><div id="l531" class="code_block">  <span class="nb">}</span>
</div><div id="l532" class="code_block">
</div><div id="l533" class="code_block">  p<span class="nb">_</span>th = xsb<span class="nb">_</span>get<span class="nb">_</span>main<span class="nb">_</span>thread();
</div><div id="l534" class="code_block">
</div><div id="l535" class="code_block">  /* Create command to consult a file: edb.P, and send it. */
</div><div id="l536" class="code_block">  if (xsb<span class="nb">_</span>command<span class="nb">_</span>string(p<span class="nb">_</span>th, &quot;consult(&#39;edb.P&#39;).&quot;) == XSB<span class="nb">_</span>ERROR)
</div><div id="l537" class="code_block">    fprintf(stderr,&quot;++Error consulting edb.P: <span class="c">%s/%s\n&quot;,xsb_get_error_type(p_th),</span>
</div><div id="l538" class="code_block">	    xsb<span class="nb">_</span>get<span class="nb">_</span>error<span class="nb">_</span>message(p<span class="nb">_</span>th));
</div><div id="l539" class="code_block">
</div><div id="l540" class="code_block">  xsb<span class="nb">_</span>ccall<span class="nb">_</span>thread<span class="nb">_</span>create(p<span class="nb">_</span>th,<span class="nb">&amp;</span>r<span class="nb">_</span>th);
</div><div id="l541" class="code_block">
</div><div id="l542" class="code_block">  rcp = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(p<span class="nb">_</span>th,&quot;p(X,Y,Z).&quot;,<span class="nb">&amp;</span>p<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l543" class="code_block">  rcr = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(r<span class="nb">_</span>th,&quot;r(X,Y,Z).&quot;,<span class="nb">&amp;</span>r<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l544" class="code_block">
</div><div id="l545" class="code_block">  while (rcp == XSB<span class="nb">_</span>SUCCESS <span class="nb">&amp;&amp;</span> rcr == XSB<span class="nb">_</span>SUCCESS) <span class="nb">{</span>
</div><div id="l546" class="code_block">
</div><div id="l547" class="code_block">    printf(&quot;Return p <span class="c">%s\n&quot;,(p_return_string.string));</span>
</div><div id="l548" class="code_block">    rcp = xsb<span class="nb">_</span>next<span class="nb">_</span>string(p<span class="nb">_</span>th, <span class="nb">&amp;</span>p<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l549" class="code_block">
</div><div id="l550" class="code_block">    printf(&quot;Return r <span class="c">%s\n&quot;,(r_return_string.string));</span>
</div><div id="l551" class="code_block">    rcr = xsb<span class="nb">_</span>next<span class="nb">_</span>string(r<span class="nb">_</span>th, <span class="nb">&amp;</span>r<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l552" class="code_block">  <span class="nb">}</span>
</div><div id="l553" class="code_block">
</div><div id="l554" class="code_block"> if (rcp == XSB<span class="nb">_</span>ERROR) 
</div><div id="l555" class="code_block">   fprintf(stderr,&quot;++Query Error p: <span class="c">%s/%s\n&quot;,xsb_get_error_type(p_th),xsb_get_error_message(p_th));</span>
</div><div id="l556" class="code_block"> if (rcr == XSB<span class="nb">_</span>ERROR) 
</div><div id="l557" class="code_block">   fprintf(stderr,&quot;++Query Error r: <span class="c">%s/%s\n&quot;,xsb_get_error_type(r_th),xsb_get_error_message(r_th));</span>
</div><div id="l558" class="code_block">
</div><div id="l559" class="code_block"> xsb<span class="nb">_</span>close();      
</div><div id="l560" class="code_block"><span class="nb">}</span>
</div><div id="l561" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l562" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l563" class="code_block"><span class="k">\caption</span><span class="nb">{</span>Manipulating Multiple Threads Using the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> Interface<span class="nb">}</span> 
</div><div id="l564" class="code_block"><span class="k">\label</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span>
</div><div id="l565" class="code_block"><span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
</div><div id="l566" class="code_block">
</div><div id="l567" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span> illustrates a very simple example of
</div><div id="l568" class="code_block">this.  XSB is initialized and the file <span class="nb">{</span><span class="k">\tt</span> edb.P<span class="nb">}</span> consulted exactly as
</div><div id="l569" class="code_block">in Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span>.  However, the function <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l570" class="code_block">  xsb<span class="k">\_ccall\_thread\_create</span>()<span class="nb">}</span> causes the XSB thread <span class="nb">{</span><span class="k">\tt</span> p<span class="k">\_th</span><span class="nb">}</span> to
</div><div id="l571" class="code_block">create a new thread, causes the new thread to call the same command
</div><div id="l572" class="code_block">loop as the main thread, and sets <span class="nb">{</span><span class="k">\tt</span> r<span class="k">\_th</span><span class="nb">}</span> to point to the context
</div><div id="l573" class="code_block">of the new thread.  The new thread <span class="nb">{</span><span class="k">\tt</span> r<span class="k">\_th</span><span class="nb">}</span> can be used for
</div><div id="l574" class="code_block">commands or queries just as <span class="nb">{</span><span class="k">\tt</span> p<span class="k">\_th</span><span class="nb">}</span>.
</div><div id="l575" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span> shows that queries to the two threads
</div><div id="l576" class="code_block">can be interleaved, and errors for both threads can be checked and
</div><div id="l577" class="code_block">reported independently.
</div><div id="l578" class="code_block"> 
</div><div id="l579" class="code_block">It is important to note that since each thread created by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l580" class="code_block">  xsb<span class="k">\_ccall\_thread\_create</span>()<span class="nb">}</span> goes into a command-loop similar to
</div><div id="l581" class="code_block">the command loop, it will stay around until it is explicitly killed or
</div><div id="l582" class="code_block">until XSB is closed.  The call 
</div><div id="l583" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l584" class="code_block">  xsb<span class="nb">_</span>kill<span class="nb">_</span>thread(r<span class="nb">_</span>th);
</div><div id="l585" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l586" class="code_block">is needed to make <span class="nb">{</span><span class="k">\tt</span> r<span class="k">\_th</span><span class="nb">}</span> to exit.  Once a thread is exited, all
</div><div id="l587" class="code_block">of its data structures will be freed, including those that support
</div><div id="l588" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_error\_type</span>()<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l589" class="code_block">  xsb<span class="k">\_get\_error\_message</span>()<span class="nb">}</span>~<span class="k">\footnote</span><span class="nb">{</span>Note that causing XSB&#39;s main
</div><div id="l590" class="code_block">  thread to exit will cause the entire process to exit -- not just
</div><div id="l591" class="code_block">  XSB.<span class="nb">}</span>.
</div><div id="l592" class="code_block">
</div><div id="l593" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Calling Multiple XSB Threads using Multiple C Threads<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>sec:CXSBMTMT<span class="nb">}</span>
</div><div id="l594" class="code_block">
</div><div id="l595" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span> shows how two XSB threads can be
</div><div id="l596" class="code_block">created, can receive different queries and can interleave their
</div><div id="l597" class="code_block">backtracking and answer return.  Although
</div><div id="l598" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span> demonstrated only backtracking through
</div><div id="l599" class="code_block">simple predicates, the mechanism employed works for complicated
</div><div id="l600" class="code_block">examples using tabling, dynamic code, and other features.  All this
</div><div id="l601" class="code_block">provides a sophisticated interface, but it is not ``fully&#39;&#39;
</div><div id="l602" class="code_block">multi-threaded in the following sense.  When a C thread <span class="s">$</span><span class="nb">T</span><span class="s">$</span> causes XSB
</div><div id="l603" class="code_block">to execute a command or query the thread must wait until the calling
</div><div id="l604" class="code_block">function returns before proceeding.  In certain applications it may be
</div><div id="l605" class="code_block">useful, for example, for <span class="s">$</span><span class="nb">T</span><span class="s">$</span> to create a C thread <span class="s">$</span><span class="nb">T_{new}</span><span class="s">$</span> which runs
</div><div id="l606" class="code_block">asynchronously from <span class="s">$</span><span class="nb">T</span><span class="s">$</span>, executing the XSB command or query and then
</div><div id="l607" class="code_block">exiting.  Alternately, an application may want to have a pool of C
</div><div id="l608" class="code_block">threads that can interact with a pool of XSB threads.
</div><div id="l609" class="code_block">
</div><div id="l610" class="code_block">XSB&#39;s C API has been designed to support these features.
</div><div id="l611" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex3<span class="nb">}</span> shows fragments of
</div><div id="l612" class="code_block">Figure~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex2<span class="nb">}</span> rewritten so that the routines to print
</div><div id="l613" class="code_block">out the answers to the queries <span class="nb">{</span><span class="k">\tt</span> p(X,Y,Z)<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> r(X,Y,Z)<span class="nb">}</span> can
</div><div id="l614" class="code_block">be called from C threads specially designed for this purpose.  More
</div><div id="l615" class="code_block">specifically, the routine <span class="nb">{</span><span class="k">\tt</span> query<span class="k">\_ps</span>()<span class="nb">}</span> calls <span class="nb">{</span><span class="k">\tt</span> p<span class="k">\_th</span><span class="nb">}</span> to query
</div><div id="l616" class="code_block"><span class="nb">{</span><span class="k">\tt</span> p(X,Y,Z)<span class="nb">}</span> and backtrack through its answers -- its use of a
</div><div id="l617" class="code_block">single <span class="nb">{</span><span class="k">\tt</span> void *<span class="nb">}</span> argument and a <span class="nb">{</span><span class="k">\tt</span> void *<span class="nb">}</span> return reflect the
</div><div id="l618" class="code_block">requirements of functions that are to be called using <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l619" class="code_block">  pthread<span class="k">\_create</span>()<span class="nb">}</span>.
</div><div id="l620" class="code_block">
</div><div id="l621" class="code_block">We note several points about this example.  First the XSB API is a
</div><div id="l622" class="code_block">low-level API that can be used to build application specific
</div><div id="l623" class="code_block">interfaces, and some experience with pthread programming is useful if
</div><div id="l624" class="code_block">multiple XSB threads are called from multiple C threads.  For
</div><div id="l625" class="code_block">instance, one issue is fairness.  When called from the C API each XSB
</div><div id="l626" class="code_block">thread <span class="s">$</span><span class="nb">X_T</span><span class="s">$</span> makes use of mutexes to ensure that it answers only one
</div><div id="l627" class="code_block">query or command at a time.  If multiple C threads are are waiting for
</div><div id="l628" class="code_block"><span class="s">$</span><span class="nb">X_T</span><span class="s">$</span> to respond to requests or queries, there is no guarantee that
</div><div id="l629" class="code_block">the requests will be processed in any sort of order, or even that a
</div><div id="l630" class="code_block">request will eventually be handled (In order to ensure this, the
</div><div id="l631" class="code_block">calling program would have to use a queue or some other scheduling
</div><div id="l632" class="code_block">mechanism to send requests to the XSB thread).  In addition, it is
</div><div id="l633" class="code_block">important to note that, <span class="nb">{</span><span class="k">\em</span> the main XSB thread should only be called
</div><div id="l634" class="code_block">  from the C thread that initialized XSB.<span class="nb">}</span>.  This restriction is due
</div><div id="l635" class="code_block">to the current design of synchronizing an XSB thread with calling
</div><div id="l636" class="code_block">threads, and may be lifted in the future.
</div><div id="l637" class="code_block">
</div><div id="l638" class="code_block"><span class="k">\begin</span><span class="nb">{</span>figure<span class="nb">}</span>[hbtp]
</div><div id="l639" class="code_block"><span class="k">\begin</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l640" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l641" class="code_block">.....
</div><div id="l642" class="code_block">
</div><div id="l643" class="code_block">void *query<span class="nb">_</span>ps(void * arg) <span class="nb">{</span>
</div><div id="l644" class="code_block">  int rc;
</div><div id="l645" class="code_block">  th<span class="nb">_</span>context *p<span class="nb">_</span>th;
</div><div id="l646" class="code_block">  XSB<span class="nb">_</span>StrDefine(p<span class="nb">_</span>return<span class="nb">_</span>string);
</div><div id="l647" class="code_block">  p<span class="nb">_</span>th = (th<span class="nb">_</span>context *)arg;
</div><div id="l648" class="code_block">  
</div><div id="l649" class="code_block">  rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(p<span class="nb">_</span>th,&quot;p(X,Y,Z).&quot;,<span class="nb">&amp;</span>p<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l650" class="code_block">  while (rc == XSB<span class="nb">_</span>SUCCESS) <span class="nb">{</span>
</div><div id="l651" class="code_block">    printf(&quot;Return p <span class="c">%s\n&quot;,(p_return_string.string));</span>
</div><div id="l652" class="code_block">    rc = xsb<span class="nb">_</span>next<span class="nb">_</span>string(p<span class="nb">_</span>th, <span class="nb">&amp;</span>p<span class="nb">_</span>return<span class="nb">_</span>string,&quot;|&quot;);
</div><div id="l653" class="code_block">  <span class="nb">}</span>
</div><div id="l654" class="code_block">
</div><div id="l655" class="code_block"> if (rc == XSB<span class="nb">_</span>ERROR) 
</div><div id="l656" class="code_block">   fprintf(stderr,&quot;++Query Error p: <span class="c">%s/%s\n&quot;,xsb_get_error_type(p_th),xsb_get_error_message(p_th));</span>
</div><div id="l657" class="code_block"> return NULL;
</div><div id="l658" class="code_block"><span class="nb">}</span>
</div><div id="l659" class="code_block">
</div><div id="l660" class="code_block">
</div><div id="l661" class="code_block">int main(int argc, char *argv[]) <span class="nb">{</span> 
</div><div id="l662" class="code_block">
</div><div id="l663" class="code_block">  char init<span class="nb">_</span>string[MAXPATHLEN];
</div><div id="l664" class="code_block">  static th<span class="nb">_</span>context *p<span class="nb">_</span>th, *r<span class="nb">_</span>th;
</div><div id="l665" class="code_block">  int pstatus, rstatus;
</div><div id="l666" class="code_block">  pthread<span class="nb">_</span>t pthread<span class="nb">_</span>id,rthread<span class="nb">_</span>id;
</div><div id="l667" class="code_block">  XSB<span class="nb">_</span>StrDefine(p<span class="nb">_</span>return<span class="nb">_</span>string);
</div><div id="l668" class="code_block">  XSB<span class="nb">_</span>StrDefine(r<span class="nb">_</span>return<span class="nb">_</span>string);
</div><div id="l669" class="code_block">
</div><div id="l670" class="code_block">.....
</div><div id="l671" class="code_block">
</div><div id="l672" class="code_block">  main<span class="nb">_</span>th = xsb<span class="nb">_</span>get<span class="nb">_</span>main<span class="nb">_</span>thread();
</div><div id="l673" class="code_block">
</div><div id="l674" class="code_block">  /* Create command to consult a file: edb.P, and send it. */
</div><div id="l675" class="code_block">  if (xsb<span class="nb">_</span>command<span class="nb">_</span>string(xsb<span class="nb">_</span>get<span class="nb">_</span>main<span class="nb">_</span>thread(), &quot;consult(&#39;edb.P&#39;).&quot;) == XSB<span class="nb">_</span>ERROR)
</div><div id="l676" class="code_block">    fprintf(stderr,&quot;++Error consulting edb.P: <span class="c">%s/%s\n&quot;,xsb_get_error_type(main_th),</span>
</div><div id="l677" class="code_block">	    xsb<span class="nb">_</span>get<span class="nb">_</span>error<span class="nb">_</span>message(main<span class="nb">_</span>th));
</div><div id="l678" class="code_block">
</div><div id="l679" class="code_block">  xsb<span class="nb">_</span>ccall<span class="nb">_</span>thread<span class="nb">_</span>create(main<span class="nb">_</span>th,<span class="nb">&amp;</span>r<span class="nb">_</span>th);
</div><div id="l680" class="code_block">  xsb<span class="nb">_</span>ccall<span class="nb">_</span>thread<span class="nb">_</span>create(main<span class="nb">_</span>th,<span class="nb">&amp;</span>p<span class="nb">_</span>th);
</div><div id="l681" class="code_block">
</div><div id="l682" class="code_block">  pthread<span class="nb">_</span>create(<span class="nb">&amp;</span>rthread<span class="nb">_</span>id,NULL,command<span class="nb">_</span>rs,r<span class="nb">_</span>th);
</div><div id="l683" class="code_block">  pthread<span class="nb">_</span>create(<span class="nb">&amp;</span>pthread<span class="nb">_</span>id,NULL,command<span class="nb">_</span>ps,p<span class="nb">_</span>th);
</div><div id="l684" class="code_block">  pthread<span class="nb">_</span>create(<span class="nb">&amp;</span>rthread<span class="nb">_</span>id,NULL,command<span class="nb">_</span>rs,r<span class="nb">_</span>th);
</div><div id="l685" class="code_block">  pthread<span class="nb">_</span>create(<span class="nb">&amp;</span>pthread<span class="nb">_</span>id,NULL,command<span class="nb">_</span>ps,p<span class="nb">_</span>th);
</div><div id="l686" class="code_block">
</div><div id="l687" class="code_block">  rstatus = pthread<span class="nb">_</span>join(rthread<span class="nb">_</span>id,<span class="nb">&amp;</span>rreturn);
</div><div id="l688" class="code_block">  if (rstatus != 0) fprintf(stderr,&quot;R join returns status <span class="c">%d\n&quot;,rstatus);</span>
</div><div id="l689" class="code_block">  pstatus = pthread<span class="nb">_</span>join(pthread<span class="nb">_</span>id,<span class="nb">&amp;</span>preturn);
</div><div id="l690" class="code_block">  if (pstatus != 0) fprintf(stderr,&quot;P join returns status <span class="c">%d\n&quot;,pstatus);</span>
</div><div id="l691" class="code_block">
</div><div id="l692" class="code_block"> xsb<span class="nb">_</span>kill<span class="nb">_</span>thread(r<span class="nb">_</span>th);
</div><div id="l693" class="code_block"> xsb<span class="nb">_</span>close();      
</div><div id="l694" class="code_block"><span class="nb">}</span>
</div><div id="l695" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l696" class="code_block"><span class="k">\end</span><span class="nb">{</span>small<span class="nb">}</span>
</div><div id="l697" class="code_block"><span class="k">\caption</span><span class="nb">{</span>Manipulating Multiple XSB Threads Using Multiple C Threads<span class="nb">}</span> 
</div><div id="l698" class="code_block"><span class="k">\label</span><span class="nb">{</span>fig:varstringex3<span class="nb">}</span>
</div><div id="l699" class="code_block"><span class="k">\end</span><span class="nb">{</span>figure<span class="nb">}</span>
</div><div id="l700" class="code_block">
</div><div id="l701" class="code_block"><span class="k">\subsubsection</span><span class="nb">{</span>Protected and Non-Protected API Functions<span class="nb">}</span> 
</div><div id="l702" class="code_block">
</div><div id="l703" class="code_block">Example~<span class="k">\ref</span><span class="nb">{</span>fig:varstringex3<span class="nb">}</span> shows that, when the <span class="nb">{</span><span class="k">\tt</span> Varstring<span class="nb">}</span>
</div><div id="l704" class="code_block">functions are used, if a single calling thread opens a query to an XSB
</div><div id="l705" class="code_block">thread <span class="s">$</span><span class="nb">X_T</span><span class="s">$</span>, <span class="s">$</span><span class="nb">X_T</span><span class="s">$</span> will be protected from queries and commands posed
</div><div id="l706" class="code_block">by other C threads until the query is closed, failed out of, or exits
</div><div id="l707" class="code_block">via an error.  In fact, queries (and commands) are protected when the
</div><div id="l708" class="code_block"><span class="nb">{</span><span class="k">\tt</span> Varstring<span class="nb">}</span> or fixed string interfaces are used.  However,
</div><div id="l709" class="code_block">consider what may happen when the register level interface is used.
</div><div id="l710" class="code_block">In this case, a calling thread may call one or more API functions to
</div><div id="l711" class="code_block">set up the registers, execute a command or query, call several more
</div><div id="l712" class="code_block">API functions to obtain the output, and so on.  For this reason, if an
</div><div id="l713" class="code_block">application uses API commands that depend on user manipulation of
</div><div id="l714" class="code_block">registers (<span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command</span>()<span class="nb">}</span>,<span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span>,<span class="nb">{</span><span class="k">\tt</span>
</div><div id="l715" class="code_block">  xsb<span class="k">\_query\_string</span>()<span class="nb">}</span>, and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next</span>()<span class="nb">}</span>) the user must ensure
</div><div id="l716" class="code_block">that only one calling thread interacts with an XSB thread when that
</div><div id="l717" class="code_block">thread in the course of executing a command or query.  See <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l718" class="code_block">  <span class="k">\$</span>XSB<span class="k">\_DIR</span>/examples/c<span class="k">\_calling\_xsb</span>/cregs<span class="k">\_thread</span>2.c<span class="nb">}</span> for an example
</div><div id="l719" class="code_block">of how mutexes can be used to protect XSB threads.
</div><div id="l720" class="code_block">
</div><div id="l721" class="code_block">
</div><div id="l722" class="code_block"><span class="k">\section</span><span class="nb">{</span>A C API for XSB<span class="nb">}</span> <span class="k">\label</span><span class="nb">{</span>sec:CAPI<span class="nb">}</span>
</div><div id="l723" class="code_block"><span class="c">%</span>
</div><div id="l724" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Initializing and Closing XSB<span class="nb">}</span>
</div><div id="l725" class="code_block">
</div><div id="l726" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l727" class="code_block">
</div><div id="l728" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_init\_string</span>(char *options)<span class="nb">}</span>
</div><div id="l729" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_init\_string</span><span class="nb">}}</span> This function is used to initialize
</div><div id="l730" class="code_block">XSB via an initialization string <span class="nb">{</span><span class="k">\tt</span> *options<span class="nb">}</span>, and must be called
</div><div id="l731" class="code_block">before any other calls can be made.  The initialization string must
</div><div id="l732" class="code_block">include the path to the XSB directory installation directory <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l733" class="code_block"><span class="k">\$</span>XSB<span class="k">\_DIR</span><span class="nb">}</span>, which is expanded to an absolute path by XSB.  Any other
</div><div id="l734" class="code_block">  command line options may be included just as in a command line
</div><div id="l735" class="code_block">  except <span class="nb">{</span><span class="k">\tt</span> -D<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> -d<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> -B<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> -i<span class="nb">}</span>.  For example, a
</div><div id="l736" class="code_block">  call from an executable in a sibling directory of XSB might have the
</div><div id="l737" class="code_block">  form
</div><div id="l738" class="code_block"><span class="c">%%</span>
</div><div id="l739" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l740" class="code_block">   xsb<span class="nb">_</span>init<span class="nb">_</span>string(&quot;../XSB -e startup.&quot;);
</div><div id="l741" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l742" class="code_block"><span class="c">%%</span>
</div><div id="l743" class="code_block">which initializes XSB with the goal <span class="nb">{</span><span class="k">\tt</span> ?- startup.<span class="nb">}</span>
</div><div id="l744" class="code_block">
</div><div id="l745" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l746" class="code_block"><span class="k">\bi</span>
</div><div id="l747" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that initialization returned
</div><div id="l748" class="code_block">  successfully.
</div><div id="l749" class="code_block"><span class="c">%</span>
</div><div id="l750" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l751" class="code_block"><span class="k">\bi</span>
</div><div id="l752" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> init<span class="k">\_error</span><span class="nb">}</span> if any error occurred during initialization. 
</div><div id="l753" class="code_block">
</div><div id="l754" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> is called
</div><div id="l755" class="code_block">  after XSB has already been correctly initialized.  
</div><div id="l756" class="code_block"><span class="k">\ei</span>
</div><div id="l757" class="code_block"><span class="c">%</span>
</div><div id="l758" class="code_block"><span class="k">\ei</span>
</div><div id="l759" class="code_block">
</div><div id="l760" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_init</span>(int argc, char *argv[])<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_init</span><span class="nb">}}</span>
</div><div id="l761" class="code_block"><span class="c">%</span>
</div><div id="l762" class="code_block">This function is a variant of <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> which passes
</div><div id="l763" class="code_block">initialization arguments as an argument vector: <span class="nb">{</span><span class="k">\tt</span> argc<span class="nb">}</span> is the
</div><div id="l764" class="code_block">count of the number of arguments in the <span class="nb">{</span><span class="k">\tt</span> argv<span class="nb">}</span> vector.  The <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l765" class="code_block">  argv<span class="nb">}</span> vector is exactly as would be passed from the command line to
</div><div id="l766" class="code_block">XSB.
</div><div id="l767" class="code_block"><span class="c">%%</span>
</div><div id="l768" class="code_block"><span class="k">\begin</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l769" class="code_block"><span class="k">\item</span> <span class="s">$</span><span class="nv">\tt</span><span class="nb"> argv</span><span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="s">$</span> must be an absolute or relative path name of the XSB
</div><div id="l770" class="code_block">  installation directory (<span class="nb">{</span><span class="k">\it</span> i.e.<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> <span class="k">\$</span>XSB<span class="k">\_DIR</span><span class="nb">}</span>).  Here is an
</div><div id="l771" class="code_block">  example, which assumes that we invoke the C program from the XSB
</div><div id="l772" class="code_block">  installation directory.
</div><div id="l773" class="code_block">    <span class="c">%%</span>
</div><div id="l774" class="code_block">    <span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l775" class="code_block">int main(int argc, char *argv[])
</div><div id="l776" class="code_block"><span class="nb">{</span> 
</div><div id="l777" class="code_block">  int myargc = 1;
</div><div id="l778" class="code_block">  char *myargv[1];
</div><div id="l779" class="code_block">
</div><div id="l780" class="code_block">  /* XSB<span class="nb">_</span>init relies on the calling program to pass the addr of the XSB
</div><div id="l781" class="code_block">     installation directory. From here, it will find all the libraries */
</div><div id="l782" class="code_block">  myargv[0] = &quot;.&quot;;
</div><div id="l783" class="code_block">
</div><div id="l784" class="code_block">  /* Initialize xsb */
</div><div id="l785" class="code_block">  xsb<span class="nb">_</span>init(myargc,myargv);
</div><div id="l786" class="code_block"><span class="nb">}</span>
</div><div id="l787" class="code_block">    <span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l788" class="code_block">    <span class="c">%%</span>
</div><div id="l789" class="code_block"><span class="k">\end</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l790" class="code_block"><span class="c">%%</span>
</div><div id="l791" class="code_block">The return codes for <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init</span>()<span class="nb">}</span> are the same as those for <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l792" class="code_block">  xsb<span class="k">\_init\_string</span>()<span class="nb">}</span>.
</div><div id="l793" class="code_block">
</div><div id="l794" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_close</span>()<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_close</span><span class="nb">}}</span>
</div><div id="l795" class="code_block"><span class="c">%</span>
</div><div id="l796" class="code_block">This routine closes the entire connection to XSB .  After this, no
</div><div id="l797" class="code_block">more calls can be made (not even calls to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> or
</div><div id="l798" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init</span>()<span class="nb">}</span>).  In <span class="k">\version</span><span class="nb">{}</span>, no guarantee is made that all
</div><div id="l799" class="code_block">space used by XSB will be restored to the process (even when the
</div><div id="l800" class="code_block">process has dynamically linked to XSB), but space for any XSB tables
</div><div id="l801" class="code_block">is freed.
</div><div id="l802" class="code_block">
</div><div id="l803" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span> 
</div><div id="l804" class="code_block"><span class="k">\bi</span>
</div><div id="l805" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that XSB was closed successfully.
</div><div id="l806" class="code_block"><span class="c">%</span>
</div><div id="l807" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l808" class="code_block"><span class="k">\bi</span>
</div><div id="l809" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_closed</span>()<span class="nb">}</span> when XSB has not been
</div><div id="l810" class="code_block">(correctly) initialized.  
</div><div id="l811" class="code_block"><span class="k">\ei</span>
</div><div id="l812" class="code_block"><span class="c">%</span>
</div><div id="l813" class="code_block"><span class="k">\ei</span>
</div><div id="l814" class="code_block">
</div><div id="l815" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l816" class="code_block">
</div><div id="l817" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Passing Commands to XSB<span class="nb">}</span>
</div><div id="l818" class="code_block">
</div><div id="l819" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l820" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_command\_string</span>(th<span class="k">\_context</span> *th, char *cmd)<span class="nb">}</span> 
</div><div id="l821" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_command\_string</span><span class="nb">}}</span>
</div><div id="l822" class="code_block"><span class="c">%</span>
</div><div id="l823" class="code_block">This function passes a command to the XSB thread designated by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l824" class="code_block">  th<span class="nb">}</span> (the first argument is not used in the single-threaded engine).
</div><div id="l825" class="code_block">No query can be active in <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> when the command is called.  The
</div><div id="l826" class="code_block">command is a string consisting of a Prolog (or HiLog) term terminated
</div><div id="l827" class="code_block">by a period (.).
</div><div id="l828" class="code_block">
</div><div id="l829" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command\_string</span><span class="nb">}</span>
</div><div id="l830" class="code_block">protects the called thread from API calls from other pthreads until
</div><div id="l831" class="code_block">the command is finished.
</div><div id="l832" class="code_block">
</div><div id="l833" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l834" class="code_block"><span class="k">\bi</span>
</div><div id="l835" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the command succeeded.
</div><div id="l836" class="code_block"><span class="c">%</span>
</div><div id="l837" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that the command failed.
</div><div id="l838" class="code_block"><span class="c">%</span>
</div><div id="l839" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l840" class="code_block"><span class="k">\bi</span>
</div><div id="l841" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command\_string</span>()<span class="nb">}</span> is
</div><div id="l842" class="code_block">  called while a query is open in <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>.
</div><div id="l843" class="code_block"><span class="c">%</span>
</div><div id="l844" class="code_block"><span class="k">\item</span> Otherwise, any queries thrown during execution of the command
</div><div id="l845" class="code_block">  are accessable through <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_error\_type</span>(th)<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l846" class="code_block">    xsb<span class="k">\_get\_error\_message</span>(th)<span class="nb">}</span>.
</div><div id="l847" class="code_block"><span class="k">\ei</span>
</div><div id="l848" class="code_block"><span class="c">%</span>
</div><div id="l849" class="code_block"><span class="k">\ei</span>
</div><div id="l850" class="code_block">
</div><div id="l851" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_command</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_command</span><span class="nb">}}</span>
</div><div id="l852" class="code_block"><span class="c">%</span>
</div><div id="l853" class="code_block">This function passes a command to the XSB thread designated by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l854" class="code_block">  th<span class="nb">}</span> (the first argument is not used in the single-threaded engine).
</div><div id="l855" class="code_block">Any previous query must have already been closed.  Before calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l856" class="code_block">  xsb<span class="k">\_command</span>()<span class="nb">}</span>, the calling program must construct the term
</div><div id="l857" class="code_block">representing the command in register 1 in the XSB thread&#39;s space.
</div><div id="l858" class="code_block">This can be done by using the <span class="nb">{</span><span class="k">\tt</span> c2p<span class="k">\_*</span><span class="nb">}</span> (and <span class="nb">{</span><span class="k">\tt</span> p2p<span class="k">\_*</span><span class="nb">}</span>)
</div><div id="l859" class="code_block">routines, which are described in Section <span class="k">\ref</span><span class="nb">{</span>c2p<span class="nb">_</span>p2p<span class="nb">_</span>p2c<span class="nb">}</span> below.
</div><div id="l860" class="code_block">Register 2 may also be set before the call to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span>
</div><div id="l861" class="code_block">(using <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_make\_vars</span>(int)<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_set\_var\_*</span>()<span class="nb">}</span>) in
</div><div id="l862" class="code_block">which case any variables set to values in the <span class="nb">{</span><span class="k">\tt</span> ret/n<span class="nb">}</span> term will be
</div><div id="l863" class="code_block">so bound in the call to the command goal.  <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command</span><span class="nb">}</span> invokes
</div><div id="l864" class="code_block">the command represented in register 1 and returns <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span>
</div><div id="l865" class="code_block">if the command succeeds, <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> if it fails, and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l866" class="code_block">  XSB<span class="k">\_ERROR</span><span class="nb">}</span> if an error is thrown while executing the command.
</div><div id="l867" class="code_block">
</div><div id="l868" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command\_string</span><span class="nb">}</span>
</div><div id="l869" class="code_block"><span class="nb">{</span><span class="k">\em</span> does not protect<span class="nb">}</span> the called thread from API calls from other
</div><div id="l870" class="code_block">pthreads until the command is finished.  It is the user&#39;s
</div><div id="l871" class="code_block">responsibility to protect the XSB thread, using a mutex or other
</div><div id="l872" class="code_block">concurrency control, from the time the goal begins to be constructed
</div><div id="l873" class="code_block">in the register 1 until the command has completed.
</div><div id="l874" class="code_block">
</div><div id="l875" class="code_block">Apart from the steps necessary to formulate the query and the lack of
</div><div id="l876" class="code_block">protection of the XSB thread, the behavior of <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command</span>()<span class="nb">}</span> is
</div><div id="l877" class="code_block">similar to that of <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command\_string</span>()<span class="nb">}</span>, including its return
</div><div id="l878" class="code_block">codes.
</div><div id="l879" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l880" class="code_block">
</div><div id="l881" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Querying XSB<span class="nb">}</span>
</div><div id="l882" class="code_block">
</div><div id="l883" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l884" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_query\_string\_string</span>(th<span class="k">\_context</span> *th, char *query, VarString *buff,
</div><div id="l885" class="code_block">char *sep)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_query\_string\_string</span><span class="nb">}}</span> 
</div><div id="l886" class="code_block"><span class="c">%</span>
</div><div id="l887" class="code_block">This function opens a query to the XSB thread designated by <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>
</div><div id="l888" class="code_block">(the first argument is not used in the single-threaded engine); it
</div><div id="l889" class="code_block">returns the first answer (if there is one) as a <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>.  Any
</div><div id="l890" class="code_block">previous query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> must have already been closed.  Any query
</div><div id="l891" class="code_block">may return multiple data answers.  The first is found and made
</div><div id="l892" class="code_block">available to the caller as a result of this call.  To get any
</div><div id="l893" class="code_block">subsequent answers, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string</span>()<span class="nb">}</span> must be called.  An
</div><div id="l894" class="code_block">example call is:
</div><div id="l895" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l896" class="code_block">rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(th, &quot;append(X,Y,[a,b,c]).&quot;,buff,&quot;;&quot;);
</div><div id="l897" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l898" class="code_block">The second argument is the period-terminated query string.  The third
</div><div id="l899" class="code_block">argument is a pointer to a variable string buffer in which the
</div><div id="l900" class="code_block">subroutine returns the answer (if any.) The variable string data type
</div><div id="l901" class="code_block"><span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> is explained in Section~<span class="k">\ref</span><span class="nb">{</span>sec-varstring<span class="nb">}</span>. (Use <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l902" class="code_block">  xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span> if you cannot declare a parameter
</div><div id="l903" class="code_block">of this type in your programming language.)  The last argument is a
</div><div id="l904" class="code_block">string provided by the caller, which is used to separate arguments in
</div><div id="l905" class="code_block">the returned answer.  For the example query, <span class="nb">{</span><span class="k">\tt</span> buff<span class="nb">}</span> would be set
</div><div id="l906" class="code_block">to the string:
</div><div id="l907" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l908" class="code_block">        [];[a,b,c]
</div><div id="l909" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l910" class="code_block">which is the first answer to the append query.  There are two fields
</div><div id="l911" class="code_block">of this answer, corresponding to the two variables in the query,
</div><div id="l912" class="code_block"><span class="k">\verb</span>|X| and <span class="k">\verb</span>|Y|.  The bindings of those variables make up the
</div><div id="l913" class="code_block">answer and the individual fields are separated by the <span class="k">\verb</span>|sep|
</div><div id="l914" class="code_block">string, here the semicolon (<span class="k">\verb</span>|;|).  In the answer string, XSB
</div><div id="l915" class="code_block">atoms are printed without quotes.  Complex terms are printed in a
</div><div id="l916" class="code_block">canonical form, with atoms quoted if necessary, and lists produced in
</div><div id="l917" class="code_block">the normal list notation.
</div><div id="l918" class="code_block">
</div><div id="l919" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l920" class="code_block">  xsb<span class="k">\_query\_string\_string</span><span class="nb">}</span> protects the called thread from API
</div><div id="l921" class="code_block">calls from other pthreads until the entire query is finished.
</div><div id="l922" class="code_block">
</div><div id="l923" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l924" class="code_block"><span class="k">\bi</span>
</div><div id="l925" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the query succeeded.
</div><div id="l926" class="code_block"><span class="c">%</span>
</div><div id="l927" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that the query failed.
</div><div id="l928" class="code_block"><span class="c">%</span>
</div><div id="l929" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l930" class="code_block"><span class="k">\bi</span>
</div><div id="l931" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span> is
</div><div id="l932" class="code_block">  called while a query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> is open.
</div><div id="l933" class="code_block"><span class="c">%</span>
</div><div id="l934" class="code_block"><span class="k">\item</span> Otherwise, any errors thrown during execution of the query are
</div><div id="l935" class="code_block">  accessable through <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_error\_type</span>()<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l936" class="code_block">    xsb<span class="k">\_get\_error\_message</span>()<span class="nb">}</span>.  
</div><div id="l937" class="code_block"><span class="k">\ei</span> 
</div><div id="l938" class="code_block"><span class="k">\ei</span>
</div><div id="l939" class="code_block">
</div><div id="l940" class="code_block"><span class="k">\item</span><span class="na">[{\mbox{\tt {\small int xsb\_query\_string\_string\_b(th\_context *th,char *query,char *buff,int bufflen,int *anslen,char *sep)}}}]</span>
</div><div id="l941" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_query\_string\_string\_b</span><span class="nb">}}</span> 
</div><div id="l942" class="code_block"><span class="c">%</span>
</div><div id="l943" class="code_block">This function provides a lower-level alternative to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l944" class="code_block">  xsb<span class="k">\_query\_string\_string</span><span class="nb">}</span> (not using the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> type),
</div><div id="l945" class="code_block">which makes it easier for non-C callers (such as Visual Basic or
</div><div id="l946" class="code_block">Delphi) to access XSB functionality.  Any previous query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>
</div><div id="l947" class="code_block">must have already been closed.  Any query may return possibly multiple
</div><div id="l948" class="code_block">data answers.  The first is found and made available to the caller as
</div><div id="l949" class="code_block">a result of this call.  To get any subsequent answers, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l950" class="code_block">  xsb<span class="k">\_next\_string\_b</span>()<span class="nb">}</span> or a similar function must be called.  The
</div><div id="l951" class="code_block">first and last arguments are the same as in <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l952" class="code_block">  xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span>.  The <span class="k">\verb</span>|buff|, <span class="k">\verb</span>|bufflen|, and
</div><div id="l953" class="code_block"><span class="k">\verb</span>|anslen| parameters are used to pass the answer (if any) back to
</div><div id="l954" class="code_block">the caller.  <span class="k">\verb</span>|buff| is a character array provided by the caller
</div><div id="l955" class="code_block">in which the answer is returned.  <span class="k">\verb</span>|bufflen| is the length of the
</div><div id="l956" class="code_block">buffer (<span class="k">\verb</span>|buff|) and is provided by the caller.  <span class="k">\verb</span>|anslen| is
</div><div id="l957" class="code_block">returned by this routine and is the length of the computed answer.  If
</div><div id="l958" class="code_block">that length is less than <span class="k">\verb</span>|bufflen|, then the answer is put in
</div><div id="l959" class="code_block"><span class="k">\verb</span>|buff| (and null-terminated).  If the answer is longer than will
</div><div id="l960" class="code_block">fit in the buffer (including the null terminator), then the answer is
</div><div id="l961" class="code_block">not copied to the buffer and <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span> is returned.  In this
</div><div id="l962" class="code_block">case the caller can retrieve the answer by providing a bigger buffer
</div><div id="l963" class="code_block">(of size greater than the returned <span class="k">\verb</span>|anslen|) in a call to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l964" class="code_block">  xsb<span class="k">\_get\_last\_answer\_string</span>()<span class="nb">}</span>.
</div><div id="l965" class="code_block">
</div><div id="l966" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l967" class="code_block">  xsb<span class="k">\_query\_string\_string\_b</span><span class="nb">}</span> protects the called thread from API
</div><div id="l968" class="code_block">calls from other pthreads until the entire query is finished.
</div><div id="l969" class="code_block">
</div><div id="l970" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l971" class="code_block"><span class="k">\bi</span>
</div><div id="l972" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the query succeeded.
</div><div id="l973" class="code_block"><span class="c">%</span>
</div><div id="l974" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that the query failed.
</div><div id="l975" class="code_block"><span class="c">%</span>
</div><div id="l976" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l977" class="code_block"><span class="k">\bi</span>
</div><div id="l978" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span>
</div><div id="l979" class="code_block">  is called while a query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> is open.
</div><div id="l980" class="code_block"><span class="c">%</span>
</div><div id="l981" class="code_block"><span class="k">\item</span> Otherwise, any queries thrown during execution of the command
</div><div id="l982" class="code_block">  are accessable through <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_error\_type</span>()<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l983" class="code_block">    xsb<span class="k">\_get\_error\_message</span>()<span class="nb">}</span>.
</div><div id="l984" class="code_block"><span class="k">\ei</span>
</div><div id="l985" class="code_block">
</div><div id="l986" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span> indicates that the query succeeded, but the
</div><div id="l987" class="code_block">  answer was too long for the buffer.
</div><div id="l988" class="code_block"><span class="c">%</span>
</div><div id="l989" class="code_block"><span class="k">\ei</span>
</div><div id="l990" class="code_block">
</div><div id="l991" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_query</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_query</span><span class="nb">}}</span> 
</div><div id="l992" class="code_block"><span class="c">%</span>
</div><div id="l993" class="code_block">This function passes a query to the XSB thread <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>.  Any previous
</div><div id="l994" class="code_block">query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> must have already been closed.  Any query may return
</div><div id="l995" class="code_block">possibly multiple data answers.  The first is found and made available
</div><div id="l996" class="code_block">to the caller as a result of this call.  To get any subsequent
</div><div id="l997" class="code_block">answers, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next</span>()<span class="nb">}</span> or a similar function must be called.
</div><div id="l998" class="code_block">Before calling <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span> the caller must construct the term
</div><div id="l999" class="code_block">representing the query in the XSB thread&#39;s register 1 (using routines
</div><div id="l1000" class="code_block">described in Section <span class="k">\ref</span><span class="nb">{</span>c2p<span class="nb">_</span>p2p<span class="nb">_</span>p2c<span class="nb">}</span> below.)  If the query has no
</div><div id="l1001" class="code_block">answers (i.e., just fails), register 1 is set back to a free variable
</div><div id="l1002" class="code_block">and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span> returns <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span>.  If the query has
</div><div id="l1003" class="code_block">at least one answer, the variables in the query term in register 1 are
</div><div id="l1004" class="code_block">bound to those answers and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span> returns <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1005" class="code_block">  XSB<span class="k">\_SUCCESS</span><span class="nb">}</span>.  In addition, register 2 is bound to a term whose
</div><div id="l1006" class="code_block">main functor symbol is <span class="nb">{</span><span class="k">\tt</span> ret/n<span class="nb">}</span>, where n is the number of variables
</div><div id="l1007" class="code_block">in the query. The main subfields of this term are set to the variable
</div><div id="l1008" class="code_block">values for the first answer. (These fields can be accessed by the
</div><div id="l1009" class="code_block">functions <span class="nb">{</span><span class="k">\tt</span> p2c<span class="k">\_*</span><span class="nb">}</span>, or the functions <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_var\_*</span><span class="nb">}</span>, described
</div><div id="l1010" class="code_block">in Section <span class="k">\ref</span><span class="nb">{</span>c2p<span class="nb">_</span>p2p<span class="nb">_</span>p2c<span class="nb">}</span> below.)  Thus there are two places the
</div><div id="l1011" class="code_block">answers are returned. Register 2 is used to make it easier to access
</div><div id="l1012" class="code_block">them.  Register 2 may also be set before the call to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span>
</div><div id="l1013" class="code_block">(using <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_make\_vars</span>(int)<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_set\_var\_*</span>()<span class="nb">}</span>) in
</div><div id="l1014" class="code_block">which case any variables set to values in the <span class="nb">{</span><span class="k">\tt</span> ret/n<span class="nb">}</span> term will be
</div><div id="l1015" class="code_block">so bound in the call to the goal.
</div><div id="l1016" class="code_block">
</div><div id="l1017" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> <span class="nb">{</span><span class="k">\em</span> does not
</div><div id="l1018" class="code_block">  protect<span class="nb">}</span> the called thread from API calls from other pthreads until
</div><div id="l1019" class="code_block">the query is finished, or even when the registers are being accessed.
</div><div id="l1020" class="code_block">It is the user&#39;s responsibility to protect the XSB thread, using a
</div><div id="l1021" class="code_block">mutex or other concurrency control, from the time the goal begins to
</div><div id="l1022" class="code_block">be constructed in the register 1 until the query is closed, failed, or
</div><div id="l1023" class="code_block">exited upon error.
</div><div id="l1024" class="code_block">
</div><div id="l1025" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_get\_last\_answer\_string</span>(th<span class="k">\_context</span> *th, char
</div><div id="l1026" class="code_block">  *buff, int bufflen, int *anslen)<span class="nb">}</span>
</div><div id="l1027" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_get\_last\_answer\_string\_b</span><span class="nb">}}</span>
</div><div id="l1028" class="code_block"><span class="c">%</span>
</div><div id="l1029" class="code_block">This function is used only when a call <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1030" class="code_block">  xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string\_b</span>()<span class="nb">}</span> to
</div><div id="l1031" class="code_block"><span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> returns <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span>, indicating that the buffer
</div><div id="l1032" class="code_block">provided was not big enough to contain the computed answer.  In that
</div><div id="l1033" class="code_block">case the user may allocate a larger buffer and then call this routine
</div><div id="l1034" class="code_block">to retrieve the answer (that had been saved.)  Only one answer is
</div><div id="l1035" class="code_block">saved per thread, so this routine must called immediately after the
</div><div id="l1036" class="code_block">failing call in order to get the right answer.  The parameters are the
</div><div id="l1037" class="code_block">same as the 2nd through 4th parameters of <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1038" class="code_block">  xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span>.
</div><div id="l1039" class="code_block">
</div><div id="l1040" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l1041" class="code_block"><span class="k">\bi</span>
</div><div id="l1042" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span> indicates that the answer was <span class="nb">{</span><span class="k">\tt</span> still<span class="nb">}</span>
</div><div id="l1043" class="code_block">  too long for the buffer.  
</div><div id="l1044" class="code_block"><span class="k">\ei</span>
</div><div id="l1045" class="code_block">
</div><div id="l1046" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_query\_string</span>(th<span class="k">\_context</span> *th,char *query)<span class="nb">}</span>
</div><div id="l1047" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_query\_string</span><span class="nb">}}</span> 
</div><div id="l1048" class="code_block"><span class="c">%</span>
</div><div id="l1049" class="code_block">This function passes a query to the XSB thread <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>.  The query is
</div><div id="l1050" class="code_block">a string consisting of a term that can be read by the XSB reader.  The
</div><div id="l1051" class="code_block">string must be terminated with a period (.).  Any previous query must
</div><div id="l1052" class="code_block">have already been closed.  In all other respects, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1053" class="code_block">  xsb<span class="k">\_query\_string</span>()<span class="nb">}</span> is similar to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span>, except the
</div><div id="l1054" class="code_block">only way to retrieve answers is through Register 2.  The ability to
</div><div id="l1055" class="code_block">create the return structure and bind variables in it is particularly
</div><div id="l1056" class="code_block">useful in this function.
</div><div id="l1057" class="code_block">
</div><div id="l1058" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span><span class="nb">}</span> <span class="nb">{</span><span class="k">\em</span>
</div><div id="l1059" class="code_block">  does not protect<span class="nb">}</span> the called thread from API calls from other
</div><div id="l1060" class="code_block">pthreads until the query is finished, or even when the registers are
</div><div id="l1061" class="code_block">being accessed.  It is the user&#39;s responsibility to protect the XSB
</div><div id="l1062" class="code_block">thread, using a mutex or other concurrency control, from the time the
</div><div id="l1063" class="code_block">goal begins to be constructed in the register 1 until the query is
</div><div id="l1064" class="code_block">closed, failed, or exited upon error.
</div><div id="l1065" class="code_block">
</div><div id="l1066" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l1067" class="code_block"><span class="k">\bi</span>
</div><div id="l1068" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the query succeeded.
</div><div id="l1069" class="code_block"><span class="c">%</span>
</div><div id="l1070" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that the query failed.
</div><div id="l1071" class="code_block"><span class="c">%</span>
</div><div id="l1072" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> indicates that an error occurred while
</div><div id="l1073" class="code_block">  executing the query.  
</div><div id="l1074" class="code_block"><span class="c">%</span>
</div><div id="l1075" class="code_block"><span class="k">\ei</span>
</div><div id="l1076" class="code_block">
</div><div id="l1077" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_next\_string</span>(th<span class="k">\_context</span> *th,VarString *buff,char *sep)<span class="nb">}</span>
</div><div id="l1078" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_next\_string</span><span class="nb">}}</span>  
</div><div id="l1079" class="code_block"><span class="c">%</span>
</div><div id="l1080" class="code_block">This routine is called after <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span>()<span class="nb">}</span> to retrieve a
</div><div id="l1081" class="code_block">subsequent answer in <span class="nb">{</span><span class="k">\tt</span> buff<span class="nb">}</span>.  If a query is not open in <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span>,
</div><div id="l1082" class="code_block">an error is returned.  This function treats answers just as <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1083" class="code_block">  xsb<span class="k">\_query\_string\_string</span>()<span class="nb">}</span>.  For example after the example call
</div><div id="l1084" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1085" class="code_block">rc = xsb<span class="nb">_</span>query<span class="nb">_</span>string<span class="nb">_</span>string(th,&quot;append(X,Y,[a,b,c]).&quot;,buff,&quot;;&quot;);
</div><div id="l1086" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1087" class="code_block">which returns with buff set to 
</div><div id="l1088" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1089" class="code_block">        [];[a,b,c]
</div><div id="l1090" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1091" class="code_block">Then a call:
</div><div id="l1092" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1093" class="code_block">rc = xsb<span class="nb">_</span>next<span class="nb">_</span>string(th,buff,&quot;;&quot;);
</div><div id="l1094" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1095" class="code_block">returns with buff set to 
</div><div id="l1096" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1097" class="code_block">        [a];[b,c]
</div><div id="l1098" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1099" class="code_block">the second answer to the indicated query. 
</div><div id="l1100" class="code_block">
</div><div id="l1101" class="code_block">In the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string</span>()<span class="nb">}</span> protects the
</div><div id="l1102" class="code_block">XSB thread from concurrent access by other threads as long as the
</div><div id="l1103" class="code_block">query was invoked by <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string</span>(<span class="k">\_b</span>)<span class="nb">}</span>.
</div><div id="l1104" class="code_block">
</div><div id="l1105" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l1106" class="code_block"><span class="k">\bi</span>
</div><div id="l1107" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the query succeeded.
</div><div id="l1108" class="code_block"><span class="c">%</span>
</div><div id="l1109" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that the query failed.
</div><div id="l1110" class="code_block"><span class="c">%</span>
</div><div id="l1111" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> indicates that an error occurred while
</div><div id="l1112" class="code_block">  executing the query.  
</div><div id="l1113" class="code_block"><span class="c">%</span>
</div><div id="l1114" class="code_block"><span class="k">\ei</span>
</div><div id="l1115" class="code_block">
</div><div id="l1116" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_next\_string\_b</span>(th<span class="k">\_context</span> *th, char *buff, int bufflen, int
</div><div id="l1117" class="code_block">*anslen, char *sep)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_next\_string</span><span class="nb">}}</span> 
</div><div id="l1118" class="code_block"><span class="c">%</span>
</div><div id="l1119" class="code_block">This function is a variant of <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string</span>()<span class="nb">}</span> that does not
</div><div id="l1120" class="code_block">use the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> type.  Its parameters are the same as the 3rd
</div><div id="l1121" class="code_block">through 6th parameters of <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string\_b</span>()<span class="nb">}</span>.  The
</div><div id="l1122" class="code_block">next answer to the current query is returned in <span class="k">\verb</span>|buff|, if there
</div><div id="l1123" class="code_block">is enough space.  If the buffer would overflow, this routine returns
</div><div id="l1124" class="code_block"><span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span>, and the answer can be retrieved by providing a
</div><div id="l1125" class="code_block">larger buffer in a call to <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_get\_last\_answer\_string\_b</span>()<span class="nb">}</span>.
</div><div id="l1126" class="code_block">In any case, the length of the answer is returned in <span class="k">\verb</span>|anslen|.
</div><div id="l1127" class="code_block">
</div><div id="l1128" class="code_block">In the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next\_string</span>()<span class="nb">}</span> protects the
</div><div id="l1129" class="code_block">XSB thread from concurrent access by other threads as long as the
</div><div id="l1130" class="code_block">query was invoked by <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string</span>(<span class="k">\_b</span>)<span class="nb">}</span>.
</div><div id="l1131" class="code_block">
</div><div id="l1132" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l1133" class="code_block"><span class="k">\bi</span>
</div><div id="l1134" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that backtracking into the query succeeded.
</div><div id="l1135" class="code_block"><span class="c">%</span>
</div><div id="l1136" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_FAILURE</span><span class="nb">}</span> indicates that backtracking into the query failed.
</div><div id="l1137" class="code_block"><span class="c">%</span>
</div><div id="l1138" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> indicates that an error occurred while further
</div><div id="l1139" class="code_block">  executing the query.
</div><div id="l1140" class="code_block">
</div><div id="l1141" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_OVERFLOW</span><span class="nb">}</span> indicates that backtracking into the query succeeded, but the
</div><div id="l1142" class="code_block">  new answer was too long for the buffer.
</div><div id="l1143" class="code_block"><span class="c">%</span>
</div><div id="l1144" class="code_block"><span class="k">\ei</span>
</div><div id="l1145" class="code_block">
</div><div id="l1146" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_next</span>(th<span class="k">\_context</span> *)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_next</span><span class="nb">}}</span> 
</div><div id="l1147" class="code_block"><span class="c">%</span>
</div><div id="l1148" class="code_block">This function is called after <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span>()<span class="nb">}</span> (which must have
</div><div id="l1149" class="code_block">returned <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span>) to retrieve more answers.  It rebinds the
</div><div id="l1150" class="code_block">query variables in the term in register 1 and rebinds the argument
</div><div id="l1151" class="code_block">fields of the <span class="nb">{</span><span class="k">\tt</span> ret/n<span class="nb">}</span> answer term in register 2 to reflect the
</div><div id="l1152" class="code_block">next answer to the query.  Its return codes are as with <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1153" class="code_block">  xsb<span class="k">\_next\_string</span>()<span class="nb">}</span>.
</div><div id="l1154" class="code_block">
</div><div id="l1155" class="code_block">When used in the multi-threaded engine, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_next</span><span class="nb">}</span> <span class="nb">{</span><span class="k">\em</span> does not
</div><div id="l1156" class="code_block">  protect<span class="nb">}</span> the called thread from API calls from other pthreads until
</div><div id="l1157" class="code_block">the query is finished, or even when the registers are being accessed.
</div><div id="l1158" class="code_block">It is the user&#39;s responsibility to protect the XSB thread, using a
</div><div id="l1159" class="code_block">mutex or other concurrency control, through the time that registers
</div><div id="l1160" class="code_block">are accessed by the calling program.
</div><div id="l1161" class="code_block">
</div><div id="l1162" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_close\_query</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_close\_query</span><span class="nb">}}</span>
</div><div id="l1163" class="code_block"><span class="c">%</span>
</div><div id="l1164" class="code_block">This function allows a user to close a query to <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> before all
</div><div id="l1165" class="code_block">its answers have been retrieved.  Since XSB is (usually) a
</div><div id="l1166" class="code_block">tuple-at-a-time system, answers that are not retrieved are not
</div><div id="l1167" class="code_block">computed so that closing a query may save time.  If a given query <span class="s">$</span><span class="nb">Q</span><span class="s">$</span>
</div><div id="l1168" class="code_block">is open, it is an error to open a new query without closing <span class="s">$</span><span class="nb">Q</span><span class="s">$</span> either
</div><div id="l1169" class="code_block">by retrieving all its answers or explicitly calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1170" class="code_block">  xsb<span class="k">\_close\_query</span>()<span class="nb">}</span> to close <span class="s">$</span><span class="nb">Q</span><span class="s">$</span>.  Calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1171" class="code_block">  xsb<span class="k">\_close\_query</span>()<span class="nb">}</span> when no query is open gives an error message,
</div><div id="l1172" class="code_block">but otherwise has no effect.
</div><div id="l1173" class="code_block">
</div><div id="l1174" class="code_block"><span class="nb">{</span><span class="k">\bf</span> Return Codes<span class="nb">}</span>  
</div><div id="l1175" class="code_block"><span class="k">\bi</span>
</div><div id="l1176" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_SUCCESS</span><span class="nb">}</span> indicates that the current query was closed.
</div><div id="l1177" class="code_block"><span class="c">%</span>
</div><div id="l1178" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> 
</div><div id="l1179" class="code_block"><span class="k">\bi</span>
</div><div id="l1180" class="code_block"><span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> permission<span class="k">\_error</span><span class="nb">}</span> if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_close\_query</span>()<span class="nb">}</span> is
</div><div id="l1181" class="code_block">  called while no query is open.
</div><div id="l1182" class="code_block"><span class="k">\ei</span>
</div><div id="l1183" class="code_block"><span class="k">\ei</span>
</div><div id="l1184" class="code_block">
</div><div id="l1185" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1186" class="code_block">
</div><div id="l1187" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Obtaining Information about Errors<span class="nb">}</span>
</div><div id="l1188" class="code_block">
</div><div id="l1189" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1190" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>char * xsb<span class="k">\_get\_init\_error\_message</span>()<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_get\_error\_message</span><span class="nb">}</span>
</div><div id="l1191" class="code_block"><span class="c">%</span>
</div><div id="l1192" class="code_block">Used to find error messages if <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_init\_string</span>()<span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1193" class="code_block">  xsb<span class="k">\_init</span>()<span class="nb">}</span> returns <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span>.  Any errors returned by these
</div><div id="l1194" class="code_block">functions have type <span class="nb">{</span><span class="k">\tt</span> init<span class="k">\_error</span><span class="nb">}</span>.  Because initialization errors
</div><div id="l1195" class="code_block">occur before XSB or any of its threads have been initialized,
</div><div id="l1196" class="code_block">initialization errors do not require a thread context for input.
</div><div id="l1197" class="code_block">
</div><div id="l1198" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>char * xsb<span class="k">\_get\_error\_type</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_get\_error\_type</span><span class="nb">}</span>
</div><div id="l1199" class="code_block"><span class="c">%</span>
</div><div id="l1200" class="code_block">If a function called for <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> returned <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> this
</div><div id="l1201" class="code_block">function provides a pointer to a string representing the type of the
</div><div id="l1202" class="code_block">error.  Types are as in Volume 1 <span class="nb">{</span><span class="k">\em</span> Exception Handling<span class="nb">}</span> with the
</div><div id="l1203" class="code_block">addition of <span class="nb">{</span><span class="k">\tt</span> init<span class="k">\_error</span><span class="nb">}</span> for errors that occur during
</div><div id="l1204" class="code_block">initialization of XSB, and <span class="nb">{</span><span class="k">\tt</span> unrecoverable<span class="k">\_error</span><span class="nb">}</span> for errors from
</div><div id="l1205" class="code_block">which no recovery is possible for XSB (e.g. inability to allocate new
</div><div id="l1206" class="code_block">memory).  
</div><div id="l1207" class="code_block">
</div><div id="l1208" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>char *xsb<span class="k">\_get\_error\_message</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span><span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_get\_error\_message</span><span class="nb">}</span>
</div><div id="l1209" class="code_block"><span class="c">%</span>
</div><div id="l1210" class="code_block">If a function called for <span class="nb">{</span><span class="k">\tt</span> th<span class="nb">}</span> returned <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_ERROR</span><span class="nb">}</span> this
</div><div id="l1211" class="code_block">function provides a pointer to a string representing a message
</div><div id="l1212" class="code_block">associated with the error.  For errors raised within the Prolog
</div><div id="l1213" class="code_block">portion of execution, messages are as in Volume 1 <span class="nb">{</span><span class="k">\em</span> Exception
</div><div id="l1214" class="code_block">  Handling<span class="nb">}</span>.
</div><div id="l1215" class="code_block">
</div><div id="l1216" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1217" class="code_block">
</div><div id="l1218" class="code_block"><span class="k">\subsection</span><span class="nb">{</span>Thread Management from Calling Programs<span class="nb">}</span>
</div><div id="l1219" class="code_block">
</div><div id="l1220" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1221" class="code_block">
</div><div id="l1222" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>int xsb<span class="k">\_ccall\_thread\_create</span>(th<span class="k">\_context</span> *callingThread, th<span class="k">\_context</span> **newThread)<span class="nb">}</span>
</div><div id="l1223" class="code_block"><span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_ccall\_thread\_create</span><span class="nb">}</span>
</div><div id="l1224" class="code_block"><span class="c">%</span>
</div><div id="l1225" class="code_block">Causes <span class="nb">{</span><span class="k">\tt</span> callingThread<span class="nb">}</span> to create a thread pointed to by <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1226" class="code_block">  newThread<span class="nb">}</span>.  <span class="nb">{</span><span class="k">\tt</span> newThread<span class="nb">}</span> runs exactly the same interpreter loop
</div><div id="l1227" class="code_block">as <span class="nb">{</span><span class="k">\tt</span> callingThread<span class="nb">}</span> and all API functions will work on <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1228" class="code_block">  newThread<span class="nb">}</span> just as on the main thread, or any other thread.  <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1229" class="code_block">  newThread<span class="nb">}</span> will be non-detached, and will inherit any private
</div><div id="l1230" class="code_block">parameters from <span class="nb">{</span><span class="k">\tt</span> callingThread<span class="nb">}</span>.  To create a thread to do a
</div><div id="l1231" class="code_block">specific task or a detached thread, rather than one that executes a
</div><div id="l1232" class="code_block">command loop, simply call the query <span class="nb">{</span><span class="k">\tt</span> thread<span class="k">\_create</span>/[2,3]<span class="nb">}</span> from
</div><div id="l1233" class="code_block">one of the query functions.
</div><div id="l1234" class="code_block">
</div><div id="l1235" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>th<span class="k">\_context</span> *xsb<span class="k">\_get\_main\_thread</span>()<span class="nb">}</span><span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_get\_main\_thread</span><span class="nb">}</span>
</div><div id="l1236" class="code_block"><span class="c">%</span>
</div><div id="l1237" class="code_block">Returns a pointer to the thread context of XSB&#39;s main thread.  If XSB
</div><div id="l1238" class="code_block">has not been initialized or has been closed this function returns 0.
</div><div id="l1239" class="code_block">
</div><div id="l1240" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>xsb<span class="k">\_tid</span> xsb<span class="k">\_thread\_id\_to\_context</span>(th<span class="k">\_context</span> *th)<span class="nb">}</span>
</div><div id="l1241" class="code_block"><span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_thread\_id\_to\_context</span><span class="nb">}</span>
</div><div id="l1242" class="code_block">
</div><div id="l1243" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>th<span class="k">\_context</span> *xsb<span class="k">\_thread\_context\_to\_id</span>(xsb<span class="k">\_tid</span> id)<span class="nb">}</span>
</div><div id="l1244" class="code_block"><span class="k">\index</span><span class="nb">{</span>xsb<span class="k">\_thread\_context\_to\_id</span><span class="nb">}</span>
</div><div id="l1245" class="code_block">
</div><div id="l1246" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1247" class="code_block">
</div><div id="l1248" class="code_block"><span class="k">\section</span><span class="nb">{</span>The Variable-length String Data Type<span class="nb">}</span><span class="k">\label</span><span class="nb">{</span>sec-varstring<span class="nb">}</span>
</div><div id="l1249" class="code_block">
</div><div id="l1250" class="code_block"><span class="k">\index</span><span class="nb">{</span>VarString<span class="nb">}</span> XSB uses variable-length strings to communicate with
</div><div id="l1251" class="code_block">certain C subroutines when the size of the output that needs to be passed
</div><div id="l1252" class="code_block">from the Prolog side to the C side is not known. Variable-length strings
</div><div id="l1253" class="code_block">adjust themselves depending on the size of the data they must hold and are
</div><div id="l1254" class="code_block">ideal for this situation. For instance, as we have seem the two subroutines
</div><div id="l1255" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string\_string</span>(query,buff,sep)<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1256" class="code_block">  xsb<span class="k">\_next\_string</span>(buff,sep)<span class="nb">}</span> use the variable string data type, <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1257" class="code_block">  VarString<span class="nb">}</span>, for their second argument.  To use this data type, make sure
</div><div id="l1258" class="code_block">that
</div><div id="l1259" class="code_block"><span class="c">%%</span>
</div><div id="l1260" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1261" class="code_block">#include &quot;cinterf.h&quot;  
</div><div id="l1262" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1263" class="code_block"><span class="c">%%</span>
</div><div id="l1264" class="code_block">appears at the top of the program file.  Variables of the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>
</div><div id="l1265" class="code_block">type are declared using a macro that must appear in the declaration section
</div><div id="l1266" class="code_block">of the program:
</div><div id="l1267" class="code_block"><span class="c">%%</span>
</div><div id="l1268" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1269" class="code_block">XSB<span class="nb">_</span>StrDefine(buf);  
</div><div id="l1270" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1271" class="code_block"><span class="c">%%</span>
</div><div id="l1272" class="code_block">There is one important consideration concerning VarString with the
</div><div id="l1273" class="code_block"><span class="k">\emph</span><span class="nb">{</span>automatic<span class="nb">}</span> storage class: they must be
</div><div id="l1274" class="code_block"><span class="k">\emph</span><span class="nb">{</span>destroyed<span class="nb">}</span> on exit (see <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrDestroy</span><span class="nb">}</span>, below) from the procedure
</div><div id="l1275" class="code_block">that defines them, or else there will be a memory leak. 
</div><div id="l1276" class="code_block">It is not necessary to destroy static <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>&#39;s.
</div><div id="l1277" class="code_block">
</div><div id="l1278" class="code_block">The public attributes of the type are <span class="nb">{</span><span class="k">\tt</span> int length<span class="nb">}</span> and <span class="nb">{</span><span class="k">\tt</span> char *string<span class="nb">}</span>.
</div><div id="l1279" class="code_block">Thus, <span class="nb">{</span><span class="k">\tt</span> buf.string<span class="nb">}</span> represents the actual contents of the buffer and
</div><div id="l1280" class="code_block"><span class="nb">{</span><span class="k">\tt</span> buf.length<span class="nb">}</span> is the length of that data. Although the length and the
</div><div id="l1281" class="code_block">contents of a <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> string is readily accessible, the user <span class="nb">{</span><span class="k">\bf</span> must
</div><div id="l1282" class="code_block">not<span class="nb">}</span> modify these items directly. Instead, he should use the macros
</div><div id="l1283" class="code_block">provided for that purpose:
</div><div id="l1284" class="code_block"><span class="c">%%</span>
</div><div id="l1285" class="code_block"><span class="k">\begin</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l1286" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrSet</span>(VarString *vstr, char *str)<span class="nb">}</span>:~
</div><div id="l1287" class="code_block">    Assign the value of the regular null-terminated C string to the
</div><div id="l1288" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span>. The size of <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span> is adjusted
</div><div id="l1289" class="code_block">    automatically.
</div><div id="l1290" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrSetV</span>(VarString *vstr1, VarString *vstr2)<span class="nb">}</span>:~
</div><div id="l1291" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrSet</span><span class="nb">}</span>, but the second argument is a variable-length
</div><div id="l1292" class="code_block">    string, not a regular C string.
</div><div id="l1293" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppend</span>(VarString *vstr, char *str)<span class="nb">}</span>:~
</div><div id="l1294" class="code_block">    Append the null-terminated string <span class="nb">{</span><span class="k">\tt</span> str<span class="nb">}</span> to the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1295" class="code_block">      vstr<span class="nb">}</span>. The size of <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span> is adjusted.
</div><div id="l1296" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrPrepend</span>(VarString *vstr, char *str)<span class="nb">}</span>:~
</div><div id="l1297" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppend</span><span class="nb">}</span>, except that <span class="nb">{</span><span class="k">\tt</span> str<span class="nb">}</span> is prepended.
</div><div id="l1298" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppendV</span>(VarString *vstr1, VarString *vstr2)<span class="nb">}</span>:~
</div><div id="l1299" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppend</span><span class="nb">}</span>, except that the second string is also a
</div><div id="l1300" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>.
</div><div id="l1301" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrPrependV</span>(VarString *vstr1, VarString *vstr2)<span class="nb">}</span>:~
</div><div id="l1302" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppendV</span><span class="nb">}</span>, except that the second string is prepended.
</div><div id="l1303" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrCompare</span>(VarString *vstr1, VarString *vstr2)<span class="nb">}</span>:~
</div><div id="l1304" class="code_block">    Compares two <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>. If the first one is lexicographically
</div><div id="l1305" class="code_block">    larger, then the result is positive; if the first string is smaller,
</div><div id="l1306" class="code_block">    than the result is negative; if the two strings have the same content
</div><div id="l1307" class="code_block">    (<span class="nb">{</span><span class="k">\it</span> i.e.<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> vstr1-&gt;string<span class="nb">}</span> equals <span class="nb">{</span><span class="k">\tt</span> vstr2-&gt;string<span class="nb">}</span> then the
</div><div id="l1308" class="code_block">    result is zero.
</div><div id="l1309" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrCmp</span>(VarString *vstr, char *str)<span class="nb">}</span>:~
</div><div id="l1310" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrCompare</span><span class="nb">}</span> but the second argument is a regular,
</div><div id="l1311" class="code_block">    null-terminated string.
</div><div id="l1312" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppendBlk</span>(VarString *vstr, char *blk, int size)<span class="nb">}</span>:~
</div><div id="l1313" class="code_block">    This is like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppend</span><span class="nb">}</span>, but the second argument is not assumed
</div><div id="l1314" class="code_block">    to be null-terminated. Instead, <span class="nb">{</span><span class="k">\tt</span> size<span class="nb">}</span> characters pointed to by
</div><div id="l1315" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> blk<span class="nb">}</span> are appended to <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span>. The size of <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span> is
</div><div id="l1316" class="code_block">    adjusted, but the content is <span class="k">\emph</span><span class="nb">{</span>not<span class="nb">}</span> null terminated.
</div><div id="l1317" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrPrependBlk</span>(VarString *vstr, char *blk, int size)<span class="nb">}</span>:~
</div><div id="l1318" class="code_block">    Like <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrPrepend</span><span class="nb">}</span>, but <span class="nb">{</span><span class="k">\tt</span> blk<span class="nb">}</span> is not assumed to point to a
</div><div id="l1319" class="code_block">    null-terminated string. Instead, <span class="nb">{</span><span class="k">\tt</span> size<span class="nb">}</span> characters from the region
</div><div id="l1320" class="code_block">    pointed to by <span class="nb">{</span><span class="k">\tt</span> blk<span class="nb">}</span> are prepended to <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span>.
</div><div id="l1321" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrNullTerminate</span>(VarString *vstr)<span class="nb">}</span>:~
</div><div id="l1322" class="code_block">    Null-terminates the <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>  string <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span>. This is used in
</div><div id="l1323" class="code_block">    conjunction with <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrAppendBlk</span><span class="nb">}</span>, because the latter does not
</div><div id="l1324" class="code_block">    null-terminate variable-length strings.
</div><div id="l1325" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrEnsureSize</span>(VarString *vstr, int minsize)<span class="nb">}</span>:~
</div><div id="l1326" class="code_block">    Ensure that the string has room for at least <span class="nb">{</span><span class="k">\tt</span> minsize<span class="nb">}</span> bytes.
</div><div id="l1327" class="code_block">    This is a low-level routine, which is used to interface to procedures
</div><div id="l1328" class="code_block">    that do not use <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> internally. If the string is larger
</div><div id="l1329" class="code_block">    than <span class="nb">{</span><span class="k">\tt</span> minsize<span class="nb">}</span>, the size might actually shrink to the nearest
</div><div id="l1330" class="code_block">    increment that is larger <span class="nb">{</span><span class="k">\tt</span> minsize<span class="nb">}</span>.
</div><div id="l1331" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrShrink</span>(VarString *vstr, int increment)<span class="nb">}</span>:~ Shrink the
</div><div id="l1332" class="code_block">    size of <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span> to the minimum necessary to hold the data. <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1333" class="code_block">      increment<span class="nb">}</span> becomes the new increment by which <span class="nb">{</span><span class="k">\tt</span> vstr<span class="nb">}</span> is adjusted.
</div><div id="l1334" class="code_block">    Since <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> is automatically shrunk by <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrSet</span><span class="nb">}</span>, it
</div><div id="l1335" class="code_block">    is rarely necessary to shrink a <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span> explicitly.  However,
</div><div id="l1336" class="code_block">    one might want to change the adjustment increment using this macro (the
</div><div id="l1337" class="code_block">    default increment is 128).
</div><div id="l1338" class="code_block">  <span class="k">\item</span> <span class="nb">{</span><span class="k">\tt</span> XSB<span class="k">\_StrDestroy</span>(VarString *vstr)<span class="nb">}</span>:~
</div><div id="l1339" class="code_block">    Destroys a <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>.  Explicit destruction is necessary for
</div><div id="l1340" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> VarString<span class="nb">}</span>&#39;s with the automatic storage class. Otherwise, memory
</div><div id="l1341" class="code_block">    leak is possible.
</div><div id="l1342" class="code_block"><span class="k">\end</span><span class="nb">{</span>itemize<span class="nb">}</span>
</div><div id="l1343" class="code_block"><span class="c">%%</span>
</div><div id="l1344" class="code_block">
</div><div id="l1345" class="code_block">
</div><div id="l1346" class="code_block"><span class="k">\section</span><span class="nb">{</span>Passing Data into an XSB Module<span class="nb">}</span>
</div><div id="l1347" class="code_block">
</div><div id="l1348" class="code_block">The previous chapter described the low-level XSB/C interface that supports
</div><div id="l1349" class="code_block">passing the data of arbitrary complexity between XSB and C. However, in
</div><div id="l1350" class="code_block">cases when data needs to be passed into an executable XSB module by the
</div><div id="l1351" class="code_block">main C program, the following higher-level interface should suffice.  (This
</div><div id="l1352" class="code_block">interface is actually implemented using macros that call the lower level
</div><div id="l1353" class="code_block">functions.)  These routines can be used to construct commands and queries
</div><div id="l1354" class="code_block">into XSB &#39;s register 1, which is necessary before calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1355" class="code_block">  xsb<span class="k">\_query</span>()<span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command</span>()<span class="nb">}</span>.
</div><div id="l1356" class="code_block">
</div><div id="l1357" class="code_block">
</div><div id="l1358" class="code_block"><span class="k">\begin</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1359" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>void xsb<span class="k">\_make\_vars</span>((int) N)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_make\_vars</span><span class="nb">}}</span>
</div><div id="l1360" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_make\_vars</span><span class="nb">}</span> creates a return structure of arity <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span>
</div><div id="l1361" class="code_block">in Register 2.  So this routine may called before calling any of <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1362" class="code_block">xsb<span class="k">\_query</span><span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span><span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_command</span><span class="nb">}</span>, or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1363" class="code_block">xsb<span class="k">\_command\_string</span><span class="nb">}</span> if parameters are to be set to be sent to the
</div><div id="l1364" class="code_block">goal.  It must be called before calling one of the <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1365" class="code_block">xsb<span class="k">\_set\_var\_*</span><span class="nb">}</span> routines can be called. <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span> must be the number
</div><div id="l1366" class="code_block">of variables in the query that is to be evaluated.
</div><div id="l1367" class="code_block">
</div><div id="l1368" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>void xsb<span class="k">\_set\_var\_int</span>((int) Val, (int) N)<span class="nb">}</span> 
</div><div id="l1369" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_set\_var\_int</span><span class="nb">}}</span>
</div><div id="l1370" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> set<span class="k">\_and\_int</span><span class="nb">}</span> sets the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> field in the return
</div><div id="l1371" class="code_block">structure to the integer value <span class="nb">{</span><span class="k">\tt</span> Val<span class="nb">}</span>.  It is used to set the value of
</div><div id="l1372" class="code_block">the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in a query before calling <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> or
</div><div id="l1373" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span><span class="nb">}</span>.  When called in XSB, the query will
</div><div id="l1374" class="code_block">have the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable set to this value.
</div><div id="l1375" class="code_block">
</div><div id="l1376" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>void xsb<span class="k">\_set\_var\_string</span>((char *) Val, (int) N)<span class="nb">}</span> 
</div><div id="l1377" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_set\_var\_string</span><span class="nb">}}</span>
</div><div id="l1378" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> set<span class="k">\_and\_string</span><span class="nb">}</span> sets the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> field in the return
</div><div id="l1379" class="code_block">structure to the atom with name <span class="nb">{</span><span class="k">\tt</span> Val<span class="nb">}</span>.  It is used to set the
</div><div id="l1380" class="code_block">value of the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in a query before calling <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1381" class="code_block">xsb<span class="k">\_query</span><span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span><span class="nb">}</span>.  When called in XSB,
</div><div id="l1382" class="code_block">the query will have the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable set to this value.
</div><div id="l1383" class="code_block">
</div><div id="l1384" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>void xsb<span class="k">\_set\_var\_float</span>((float) Val, (int) N)<span class="nb">}</span> 
</div><div id="l1385" class="code_block"><span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_set\_var\_float</span><span class="nb">}}</span>
</div><div id="l1386" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> set<span class="k">\_and\_float</span><span class="nb">}</span> sets the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> field in the return
</div><div id="l1387" class="code_block">structure to the floating point number with value <span class="nb">{</span><span class="k">\tt</span> Val<span class="nb">}</span>.  It is
</div><div id="l1388" class="code_block">used to set the value of the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in a query before
</div><div id="l1389" class="code_block">calling <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query\_string</span><span class="nb">}</span>.  When called in
</div><div id="l1390" class="code_block">XSB, the query will have the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable set to this
</div><div id="l1391" class="code_block">value.
</div><div id="l1392" class="code_block">
</div><div id="l1393" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>prolog<span class="k">\_int</span> xsb<span class="k">\_var\_int</span>((int) N)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_var\_int</span><span class="nb">}}</span> 
</div><div id="l1394" class="code_block"><span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_var\_int</span><span class="nb">}</span> is called after <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1395" class="code_block">xsb<span class="k">\_query\_string</span><span class="nb">}</span> returns an answer.  It returns the value of the
</div><div id="l1396" class="code_block"><span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in the query as set in the returned answer.
</div><div id="l1397" class="code_block">This variable must have an integer value (which is cast to <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1398" class="code_block">long<span class="nb">}</span> in a 64-bit architecture).
</div><div id="l1399" class="code_block">
</div><div id="l1400" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>char* xsb<span class="k">\_var\_string</span>((int) N)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_var\_string</span><span class="nb">}}</span>
</div><div id="l1401" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_var\_string</span><span class="nb">}</span> is called after <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1402" class="code_block">xsb<span class="k">\_query\_string</span><span class="nb">}</span> returns an answer.  It returns the value of the
</div><div id="l1403" class="code_block"><span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in the query as set in the returned answer.
</div><div id="l1404" class="code_block">This variable must have an atom value.
</div><div id="l1405" class="code_block">
</div><div id="l1406" class="code_block"><span class="k">\ouritem</span><span class="nb">{</span>prolog<span class="k">\_float</span> xsb<span class="k">\_var\_float</span>((int) N)<span class="nb">}</span> <span class="k">\index</span><span class="nb">{</span><span class="k">\texttt</span><span class="nb">{</span>xsb<span class="k">\_var\_float</span><span class="nb">}}</span>
</div><div id="l1407" class="code_block">    <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_var\_float</span><span class="nb">}</span> is called after <span class="nb">{</span><span class="k">\tt</span> xsb<span class="k">\_query</span><span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1408" class="code_block">    xsb<span class="k">\_query\_string</span><span class="nb">}</span> returns an answer.  It returns the value of
</div><div id="l1409" class="code_block">    the <span class="nb">{</span><span class="k">\tt</span> N<span class="nb">}</span><span class="s">$</span><span class="nb">^{th}</span><span class="s">$</span> variable in the query as set in the returned
</div><div id="l1410" class="code_block">    answer.  This variable must have a floating point value (which is
</div><div id="l1411" class="code_block">    cast to <span class="nb">{</span><span class="k">\tt</span> double<span class="nb">}</span> in a 64-bit architecture).
</div><div id="l1412" class="code_block">
</div><div id="l1413" class="code_block">
</div><div id="l1414" class="code_block"><span class="k">\end</span><span class="nb">{</span>description<span class="nb">}</span>
</div><div id="l1415" class="code_block">
</div><div id="l1416" class="code_block">
</div><div id="l1417" class="code_block"><span class="k">\section</span><span class="nb">{</span>Creating an XSB Module that Can be Called from C<span class="nb">}</span>
</div><div id="l1418" class="code_block">
</div><div id="l1419" class="code_block">To create an executable that includes calls to the above C functions,
</div><div id="l1420" class="code_block">these routines, and the XSB<span class="k">\</span> routines that they call, must be
</div><div id="l1421" class="code_block">included in the link (<span class="nb">{</span><span class="k">\tt</span> ld<span class="nb">}</span>) step.
</div><div id="l1422" class="code_block">
</div><div id="l1423" class="code_block"><span class="k">\paragraph</span><span class="nb">{</span>Unix instructions:<span class="nb">}</span>
</div><div id="l1424" class="code_block">You must link your C program, which should include the main procedure, with
</div><div id="l1425" class="code_block">the XSB object file located in
</div><div id="l1426" class="code_block"><span class="c">%%</span>
</div><div id="l1427" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1428" class="code_block"> <span class="s">$</span><span class="nb">XSBDIR</span><span class="o">/</span><span class="nb">config</span><span class="o">/</span><span class="nb">&lt;your</span><span class="o">-</span><span class="nb">system</span><span class="o">-</span><span class="nb">architecture&gt;</span><span class="o">/</span><span class="nb">saved.o</span><span class="o">/</span><span class="nb">xsb.o  </span>
</div><div id="l1429" class="code_block"><span class="nv">\end</span><span class="nb">{verbatim}</span>
</div><div id="l1430" class="code_block"><span class="c">%%$</span>
</div><div id="l1431" class="code_block"><span class="nb">Your program should include the file {</span><span class="nv">\tt</span><span class="nb"> cinterf.h} located in the {</span><span class="nv">\tt</span>
</div><div id="l1432" class="code_block"><span class="nb">  XSB</span><span class="o">/</span><span class="nb">emu} subdirectory, which defines the routines described earlier,</span>
</div><div id="l1433" class="code_block"><span class="nb">which you will need to use in order to talk to XSB.  It is therefore</span>
</div><div id="l1434" class="code_block"><span class="nb">recommended to compile your program with the option</span>
</div><div id="l1435" class="code_block"><span class="nv">\verb</span><span class="nb">|</span><span class="o">-</span><span class="nb">I</span><span class="s">$</span>XSB<span class="nb">_</span>DIR/XSB/emu|.
</div><div id="l1436" class="code_block"><span class="c">%%$</span>
</div><div id="l1437" class="code_block">
</div><div id="l1438" class="code_block">The file <span class="nb">{</span><span class="k">\tt</span> <span class="k">\$</span>XSB<span class="k">\_DIR</span>/config/your-system-architecture/modMakefile<span class="nb">}</span> is a
</div><div id="l1439" class="code_block">makefile you can use to build your programs and link them with XSB.  It is
</div><div id="l1440" class="code_block">generated automatically and contains all the right settings for your
</div><div id="l1441" class="code_block">architecture, but you will have to fill in the name of your program, etc.
</div><div id="l1442" class="code_block">
</div><div id="l1443" class="code_block"><span class="k">\index</span><span class="nb">{</span>64-bit architectures<span class="nb">}</span>
</div><div id="l1444" class="code_block">It is also possible to compile and link your program with XSB using XSB
</div><div id="l1445" class="code_block">itself as follows:
</div><div id="l1446" class="code_block"><span class="c">%%</span>
</div><div id="l1447" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1448" class="code_block">:- xsb<span class="nb">_</span>configuration(compiler<span class="nb">_</span>flags,CFLAGS),
</div><div id="l1449" class="code_block">        xsb<span class="nb">_</span>configuration(loader<span class="nb">_</span>flags,LDFLAGS),
</div><div id="l1450" class="code_block">        xsb<span class="nb">_</span>configuration(config<span class="nb">_</span>dir,CONFDIR),
</div><div id="l1451" class="code_block">        xsb<span class="nb">_</span>configuration(emudir,EMUDIR),
</div><div id="l1452" class="code_block">        xsb<span class="nb">_</span>configuration(compiler,Compiler),
</div><div id="l1453" class="code_block">        str<span class="nb">_</span>cat(CONFDIR, &#39;/saved.o/&#39;, ObjDir),
</div><div id="l1454" class="code_block">        write(&#39;Compiling myprog.c ... &#39;),
</div><div id="l1455" class="code_block">        shell([Compiler, &#39; -I&#39;, EMUDIR, &#39; -c &#39;, CFLAGS, &#39; myprog.c &#39;]),
</div><div id="l1456" class="code_block">        shell([Compiler, CFLAGE, &#39; -o &#39;, &#39;./myprog &#39;,
</div><div id="l1457" class="code_block">               ObjDir, &#39;xsb.o &#39;, &#39; myprog.o &#39;, LDFLAGS]),
</div><div id="l1458" class="code_block">        writeln(done).  
</div><div id="l1459" class="code_block"><span class="k">\end</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1460" class="code_block"><span class="c">%%</span>
</div><div id="l1461" class="code_block">This works for every architecture and is often more convenient than
</div><div id="l1462" class="code_block">using the make files~<span class="k">\footnote</span><span class="nb">{</span>The variable <span class="nb">{</span><span class="k">\tt</span> CFLAGS<span class="nb">}</span> is needed in
</div><div id="l1463" class="code_block">  the linking stage in order to ensure that the appropriate memory
</div><div id="l1464" class="code_block">  option is passed if XSB is configured <span class="nb">{</span><span class="k">\tt</span> --with-bits32<span class="nb">}</span> or <span class="nb">{</span><span class="k">\tt</span>
</div><div id="l1465" class="code_block">    --with-bits64<span class="nb">}</span> to override the default on a 64-bit platform.<span class="nb">}</span>.
</div><div id="l1466" class="code_block">There are simple examples of C programs calling XSB<span class="k">\</span> in the
</div><div id="l1467" class="code_block"><span class="nb">{</span><span class="k">\tt</span> <span class="k">\$</span>XSB<span class="k">\_DIR</span>/examples/c<span class="k">\_calling\_XSB</span><span class="nb">}</span> directory, in files <span class="nb">{</span><span class="k">\tt</span> cmain.c<span class="nb">}</span>,
</div><div id="l1468" class="code_block"><span class="nb">{</span><span class="k">\tt</span> ctest.P<span class="nb">}</span>, <span class="nb">{</span><span class="k">\tt</span> cmain2.c<span class="nb">}</span>.
</div><div id="l1469" class="code_block">
</div><div id="l1470" class="code_block"><span class="k">\paragraph</span><span class="nb">{</span>Windows instructions:<span class="nb">}</span>
</div><div id="l1471" class="code_block">To call XSB from C, you must build it as a DLL, which is done as follows:
</div><div id="l1472" class="code_block"><span class="c">%%</span>
</div><div id="l1473" class="code_block"><span class="k">\begin</span><span class="nb">{</span>verbatim<span class="nb">}</span>
</div><div id="l1474" class="code_block">  cd <span class="s">$</span><span class="nb">XSB_DIR</span><span class="nv">\XSB\build</span>
</div><div id="l1475" class="code_block"><span class="nb">  makexsb_wind DLL</span><span class="o">=</span><span class="nb">&quot;yes&quot;</span>
</div><div id="l1476" class="code_block"><span class="nv">\end</span><span class="nb">{verbatim}</span>
</div><div id="l1477" class="code_block"><span class="c">%%$</span>
</div><div id="l1478" class="code_block"><span class="nb">The DLL, which you can call dynamically from your program is then found in </span>
</div><div id="l1479" class="code_block"><span class="c">%%</span>
</div><div id="l1480" class="code_block"><span class="nv">\[</span>
</div><div id="l1481" class="code_block"><span class="nb"> </span><span class="nv">\tt</span>
</div><div id="l1482" class="code_block"><span class="nb"> </span><span class="nv">\$</span><span class="nb">XSB</span><span class="nv">\_</span><span class="nb">DIR</span><span class="nv">\backslash</span><span class="nb"> config</span><span class="nv">\backslash</span><span class="nb"> </span><span class="nv">\mbox</span><span class="nb">{</span><span class="nv">\tt</span><span class="nb"> x</span><span class="m">86</span><span class="o">-</span><span class="nb">pc</span><span class="o">-</span><span class="nb">windows}</span><span class="nv">\backslash</span>
</div><div id="l1483" class="code_block"><span class="nb"> bin </span><span class="nv">\backslash</span><span class="nb"> xsb.dll</span>
</div><div id="l1484" class="code_block"><span class="nv">\]</span>
</div><div id="l1485" class="code_block"><span class="c">%%</span>
</div><div id="l1486" class="code_block"><span class="nb">Since your program must include the file {</span><span class="nv">\tt</span><span class="nb"> cinterf.h}, it is recommended</span>
</div><div id="l1487" class="code_block"><span class="nb">to compile it with the option </span><span class="nv">\verb</span><span class="nb">|</span><span class="o">/</span><span class="nb">I</span><span class="s">$</span>XSB<span class="nb">_</span>DIR<span class="k">\XSB\emu</span>|.
</div><div id="l1488" class="code_block"><span class="c">%%$</span>
</div><div id="l1489" class="code_block">
</div><div id="l1490" class="code_block">
</div><div id="l1491" class="code_block"><span class="c">%%% Local Variables: </span>
</div><div id="l1492" class="code_block"><span class="c">%%% mode: latex</span>
</div><div id="l1493" class="code_block"><span class="c">%%% TeX-master: &quot;manual2&quot;</span>
</div><div id="l1494" class="code_block"><span class="c">%%% End: </span>
</div></pre></div></td></tr></table></div>

      
    </div>
  

                    </div>
                    
                    
                </div>
            



        </div>
    </section>
      

    <footer class="sandiego">
    <div class="as-row">
        <div class="footer-wrapper">
            <nav aria-label="Site Links" role="navigation">
                <section>
                    <div class="as-h2">SourceForge</div>
                    <ul>
                    
                        <li><a href="/create/" title="Create a Project">Create a Project</a></li>
                    
                        <li><a href="/directory/" title="Open Source Software Directory">Open Source Software</a></li>
                        <li><a href="/software/" title="Business Software Directory">Business Software</a></li>
                        
                            
                            <li><a href="/top" title="Top Open Source Projects">Top Downloaded Projects</a></li>
                        

                    </ul>
                </section>
            </nav>
            <nav aria-label="Company Links" role="navigation">
                <section>
                    <div class="as-h2">Company</div>
                    <ul>
                        <li><a href="/about">About</a></li>
                        <li><a href="/about/leadership" title="Open Source Software Directory">Team</a></li>
                        <li class="h-card">
                            <address>
                                <span class="p-name p-org">SourceForge Headquarters</span><br>
                                <span class="p-street-address">225 Broadway Suite 1600</span><br>
                                <span class="p-locality">San Diego, CA <span class="p-postal-code">92101</span></span><br>
                                <span class="p-tel">+1 (858) 422-6466</span><br>
                            </address>
                        </li>
                        <li id="social">
                            
<span></span>
<a href="https://twitter.com/sourceforge" class="twitter" rel="nofollow" target="_blank" title="SourceForge on X">


<svg  class="vertical-icon-fix" data-name="twitter" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1684 408q-67 98-162 167 1 14 1 42 0 130-38 259.5t-115.5 248.5-184.5 210.5-258 146-323 54.5q-271 0-496-145 35 4 78 4 225 0 401-138-105-2-188-64.5t-114-159.5q33 5 61 5 43 0 85-11-112-23-185.5-111.5t-73.5-205.5v-4q68 38 146 41-66-44-105-115t-39-154q0-88 44-163 121 149 294.5 238.5t371.5 99.5q-8-38-8-74 0-134 94.5-228.5t228.5-94.5q140 0 236 102 109-21 205-78-37 115-142 178 93-10 186-50z"/></svg></a>
<a href="https://fosstodon.org/@sourceforge" rel="me nofollow" target="_blank" title="SourceForge on Mastodon">


<svg  class="vertical-icon-fix" data-name="mastodon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M433 179.11c0-97.2-63.71-125.7-63.71-125.7-62.52-28.7-228.56-28.4-290.48 0 0 0-63.72 28.5-63.72 125.7 0 115.7-6.6 259.4 105.63 289.1 40.51 10.7 75.32 13 103.33 11.4 50.81-2.8 79.32-18.1 79.32-18.1l-1.7-36.9s-36.31 11.4-77.12 10.1c-40.41-1.4-83-4.4-89.63-54a102.54 102.54 0 0 1-.9-13.9c85.63 20.9 158.65 9.1 178.75 6.7 56.12-6.7 105-41.3 111.23-72.9 9.8-49.8 9-121.5 9-121.5zm-75.12 125.2h-46.63v-114.2c0-49.7-64-51.6-64 6.9v62.5h-46.33V197c0-58.5-64-56.6-64-6.9v114.2H90.19c0-122.1-5.2-147.9 18.41-175 25.9-28.9 79.82-30.8 103.83 6.1l11.6 19.5 11.6-19.5c24.11-37.1 78.12-34.8 103.83-6.1 23.71 27.3 18.4 53 18.4 175z"/></svg></a>
<a href="https://www.facebook.com/sourceforgenet/" class="facebook" rel="nofollow" target="_blank" title="SourceForge on Facebook">


<svg  data-name="facebook" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1343 12v264h-157q-86 0-116 36t-30 108v189h293l-39 296h-254v759h-306v-759h-255v-296h255v-218q0-186 104-288.5t277-102.5q147 0 228 12z"/></svg></a>
<a href="https://www.linkedin.com/company/sourceforge.net" class="linkedin" rel="nofollow" target="_blank" title="SourceForge on LinkedIn">


<svg  data-name="linkedin" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M477 625v991h-330v-991h330zm21-306q1 73-50.5 122t-135.5 49h-2q-82 0-132-49t-50-122q0-74 51.5-122.5t134.5-48.5 133 48.5 51 122.5zm1166 729v568h-329v-530q0-105-40.5-164.5t-126.5-59.5q-63 0-105.5 34.5t-63.5 85.5q-11 30-11 81v553h-329q2-399 2-647t-1-296l-1-48h329v144h-2q20-32 41-56t56.5-52 87-43.5 114.5-15.5q171 0 275 113.5t104 332.5z"/></svg></a>

<a href="/user/newsletters" rel=nofollow class="newsletter" title="Subscribe to our newsletter">


<svg  class="vertical-icon-fix" data-name="mmSF_11mail" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 42 42" enable-background="new 0 0 42 42" xml:space="preserve"><path fill="#FFFFFF" d="M0,6v30h42V6H0z M24.2,21.2c-0.8,0.8-2.3,2-3.2,2c-0.9,0-2.4-1.2-3.2-2L5.8,9.7h30.3L24.2,21.2z M13.7,21l-9.9,9.4V11.6L13.7,21z M15.7,23L15.7,23c0.5,0.5,2.9,2.9,5.3,2.9c2.4,0,4.8-2.4,5.2-2.8l0.1-0.1l9.8,9.4H5.8L15.7,23z M28.3,21l9.9-9.5v18.9L28.3,21z"/></svg></a>

<span></span>
                        </li>
                    </ul>
                </section>
            </nav>
             <nav aria-label="Resources Links" role="navigation">
                <section>
                    <div class="as-h2">Resources</div>
                    <ul>
                        
                            <li><a href="/support" title="Support Section">Support</a></li>
                            <li><a href="/p/forge/documentation/Docs%20Home/" title="Site Documentation">Site Documentation</a></li>
                        
                        <li><a href="https://fosstodon.org/@sourceforgestatus" title="Site Status" rel="me nofollow">Site Status</a></li>
                        <li><a href="/reviews" title="Reviews" rel="me nofollow">SourceForge Reviews</a></li>

                    </ul>
                </section>
            </nav>
            <section class="footer-logo">
                <a href="/" title="Home" class="sf-logo">
                    
    
    <img src="https://a.fsdn.com/con/images/sandiego/sf-logo-full.svg"  alt="SourceForge logo" class="sf-logo-full"/>
                </a>
            </section>
        </div>
    </div>
    <section class="l-nav-bottom">
        <nav class="row">
            
                
            
        
    <div class="columns small-12 large-6 copyright-notice">
        &copy; 2025 Slashdot Media. All Rights Reserved.
    </div>
    <div class="columns large-6 links links-right">
        

    
    

    <a href="https://slashdotmedia.com/terms-of-use" target="_blank" title="Terms" rel="nofollow">Terms</a>
    <a href="https://slashdotmedia.com/privacy-statement/" target="_blank" title="Privacy" rel="nofollow">Privacy</a>

    
    
    

    
        <a href="https://slashdotmedia.com/opt-out-choices/" target="_blank" title="Opt Out" rel="nofollow">Opt Out</a>
    

    
        <a href="https://slashdotmedia.com/contact/" target="_blank" title="Advertise" rel="nofollow">Advertise</a>
    
    </div>
        </nav>
    </section>

    </footer>
    





    
    
    <div id="newsletter-floating" class="sandiego newsletter-floating-new">
        <a class="close-button btn-closer" data-close aria-label="Close modal">
            <span aria-hidden="true">&times;</span>
        </a>
        <div class="as-h2">Want the latest updates on software, tech news, and AI?</div>
        <div class="row">
            <div class="column small-2 medium-2 large-2" style="padding-left: 0px; padding-right: 0px;"><img src="/images/sandiego/sf_email_icon.svg"  alt="" /></div>
            <div class="as-h3 column small-10 medium-10 large-10">Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.</div>
        </div>
        <form action="/user/newsletters/" method="post">
            <div class="row">
              <input class="newsletter-input" type="email" placeholder="Enter your email address" autocomplete="off" name="newsletter_email"
                     value="">
              <input type="hidden" name="source" value="floating">
              <button type="submit" class="button blue newsletter-button">Submit</button>
            </div>
        </form>
    </div>
    






</div>


<div id="messages">
    
</div>


    <!-- ew:body_js -->


    <script type="text/javascript" src="https://a.fsdn.com/allura/nf/1751484103/_ew_/_slim/js?href=allura%2Fjs%2Fjquery.notify.js%3Ballura%2Fjs%2Fjquery.tooltipster.js%3Ballura%2Fjs%2Fallura-base.js%3Ballura%2Fjs%2Fchecklist.js%3Ballura%2Fjs%2Fadmin_modal.js%3Bjs%2Fjquery.lightbox_me.js%3Ballura%2Fjs%2Fmemorable.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fshared.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fsticky.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fsandiego%2Fchrome.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fsandiego%2Futilities.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.core.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.util.mediaQuery.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.util.keyboard.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.util.box.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.util.nest.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.dropdownMenu.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.offcanvas.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.toggler.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fsandiego%2Fproject.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Fjquery.typeahead.min.js%3Ballura%2Fjs%2Fmaximize-content.js"></script>

    
<!-- /ew:body_js -->



    <!-- ew:body_js_tail -->


    <script type="text/javascript" src="https://a.fsdn.com/allura/nf/1751484103/_ew_/_slim/js?href=theme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.util.triggers.min.js%3Btheme%2Fsftheme%2Fjs%2Fsftheme%2Fvendor%2Ffoundation6%2Fplugins%2Ffoundation.reveal.min.js"></script>

    
<!-- /ew:body_js_tail -->




<script type="text/javascript">(function() {
  $('#access_urls .btn').click(function(evt){
    evt.preventDefault();
    var parent = $(this).parents('.btn-bar');
    var checkout_cmd = $(this).attr('data-url');
    $(parent).find('input').val(checkout_cmd);
    $(parent).find('span').text($(this).attr('title')+' access');
    $(this).parent().children('.btn').removeClass('active');
    $(this).addClass('active');
    if (checkout_cmd.indexOf(' http://') !== -1 || checkout_cmd.indexOf(' https://') !== -1 ) {
      $('#http-2fa-msg').show();
    } else {
      $('#http-2fa-msg').hide();
    }
  });
  $('#access_urls .btn').first().click();

  
  var repo_status = document.getElementById('repo_status');
  // The repo_status div will only be present if repo.status != 'ready'
  if (repo_status) {
    $('.spinner').show()
    var delay = 500;
    function check_status() {
        $.get('/p/xsb/src/status', function(data) {
            if (data.status === 'ready') {
                $('.spinner').hide()
                $('#repo_status h2').html('Repo status: ready. <a href="." rel="nofollow">Click here to refresh this page.</a>');
            }
            else {
                $('#repo_status h2 span').html(data.status);
                if (delay < 60000){
                    delay = delay * 2;
                }
                window.setTimeout(check_status, delay);
            }
        });
    }
    var status_checker = window.setTimeout(check_status, delay);
    
  }
}());
</script>

<script type="text/javascript">(function() {
  $(window).on('hashchange', function(e) {
    var hash = window.location.hash.substring(1);
    if ('originalEvent' in e && 'oldURL' in e.originalEvent) {
      $('#' + e.originalEvent.oldURL.split('#')[1]).css('background-color', 'transparent');
    }
    if (hash !== '' && hash.substring(0, 1) === 'l' && !isNaN(hash.substring(1))) {
      $('#' + hash).css('background-color', '#ffff99');
    }
  }).trigger('hashchange');

  var clicks = 0;
  $('.code_block').each(function(index, element) {
    $(element).on('click', function() {
      // Trick to ignore double and triple clicks
      clicks++;
      if (clicks === 1) {
        setTimeout(function() {
          if (clicks === 1) {
            var hash = window.location.hash.substring(1);
            if (hash !== '' && hash.substring(0, 1) === 'l' && !isNaN(hash.substring(1))) {
              $('#' + hash).css('background-color', 'transparent');
            }
            $(element).css('background-color', '#ffff99');
            window.history.pushState({}, document.title, window.location.pathname + window.location.search + '#' + $(element).attr('id'));
          }
          clicks = 0;
        }, 500);
      }
    });
  });
}());
</script>


    


    
    
    <noscript><p><img src="https://analytics.slashdotmedia.com/index.php?idsite=39" style="border:0;position:absolute;top:0;" alt="" /></p></noscript>
    
    <script>
        $(document).foundation();
    </script>
    








    

<!-- CCM Tag -->
<script>
    (function () {
        bizx.cmp.ifConsent({ purposes: 'all', vendors: 'bombora'}, function () {
            /*global _ml:true, window */
            _ml = window._ml || {};
            
            _ml.eid = '771';
            _ml.fp = '294352d4-9189-4c6e-95fd-f9e92fd04baf';  
            var s = document.getElementsByTagName('script')[0], cd = new Date(), mltag = document.createElement('script');
            mltag.type = 'text/javascript';
            mltag.async = true;
            mltag.defer = true;
            mltag.src = '//ml314.com/tag.aspx?' + cd.getDate() + cd.getMonth() + cd.getFullYear();
            s.parentNode.insertBefore(mltag, s);
        });
    })();
</script>
<!-- End CCM Tag -->




<script>
    $(document).ready(function () {
        $(".tooltip, .m-tooltip").each(function(){
            let $el = $(this);
            $el.tooltipster({
                animation: 'fade',
                delay: 200,
                theme: 'tooltipster-light',
                trigger: 'hover',
                position: $el.data('position') || 'right',
                iconCloning: false,
                maxWidth: 300,
                contentAsHTML: true,
                interactive: true
            }).focus(function () {
                $(this).tooltipster('show');
            }).blur(function () {
                $(this).tooltipster('hide');
            });
        });
    });
</script>

    



<script type="text/javascript">
    bizx.cmp.ifConsent({ purposes: ['storage', 'measurement'], vendors: 'google-analytics'}, function () {
        /* jshint ignore:start */
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
        /* jshint ignore:end */

        window.dataLayer = window.dataLayer || [];
        function gtag(){ window.dataLayer.push(arguments); }
        window.gtag = window.gtag || gtag;
        bizx.cmp.embedScript("https://www.googletagmanager.com/gtag/js");
        gtag('js', new Date());
        gtag('set', {
            'page_location': 'https://sourceforge.net/p/xsb/src/5501/tree/trunk/XSB/docs/userman/ccallingxsb.tex',
        });
    });
</script>










<script>
        function gam(id){
            bizx.cmp.ifConsent({ purposes: 'all', vendors: 'google-ads'}, function () {
                bizx.cmp.embedScript(`https://pagead2.googlesyndication.com/pagead/js/pcd.js?${id}`,
                    true,  // async
                    'head', // location
                    null, // callback
                    false, // defer
                    {id: `google-pcd-tag-${id}`, 'data-audience-pixel': 'dc_iu=/41014381/DFPAudiencePixel;dc_seg=' + id});
            });
        }

        
            gam("8901705213");gam("8901712041");</script>

<script type="text/javascript">
    /*global ga, gtag */

    
        
        bizx.cmp.ifConsent({ purposes: ['storage', 'measurement'], vendors: 'google-analytics'}, function () {
            gtag('config', 'G-1H226E4E4L', {
                
                send_page_view: false,
                'SF_Page_Type': '', 
                user_properties: {
                    'SF_Logged_in': 'Logged Out', 
                },
                'devicePixelRatio': SF.devicePixelRatio, 
            });
            gtag('event', 'page_view', { send_to: 'G-1H226E4E4L' });
        });
    
    
    
        
</script>

</body>
</html>