/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2010, Gabriel Dos Reis.4All rights reserved.5Copyright (C) 2009-2010, Alfredo Portes.6All rights reserved.78Redistribution and use in source and binary forms, with or without9modification, are permitted provided that the following conditions are10met:1112- Redistributions of source code must retain the above copyright13notice, this list of conditions and the following disclaimer.1415- Redistributions in binary form must reproduce the above copyright16notice, this list of conditions and the following disclaimer in17the documentation and/or other materials provided with the18distribution.1920- Neither the name of The Numerical Algorithms Group Ltd. nor the21names of its contributors may be used to endorse or promote products22derived from this software without specific prior written permission.2324THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS25IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED26TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A27PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER28OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,29EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,30PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR31PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF32LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING33NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS34SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.35*/3637/*38* htsearch: is used by Hyperdoc to search for keywords in39* Hyperdoc pages and create a HyperDoc page of the search40* result.41*/4243#include "openaxiom-c-macros.h"4445#include <stdlib.h>46#include <stdio.h>47#include <string.h>48#include <string>49#include <iostream>50#include "cfuns.h"5152// FIXME: Remove kludge53using namespace OpenAxiom;5455// Path to the directory containing the hyperdoc pages.56static std::string htpagedir;5758// Path to the hthits program.59static std::string hthitscmd;6061// Path to the database file of the hyperdoc pages.62static std::string htdbfile;6364/*65* pages_cmp compares two strings.66*67* @param str1 string to be compared.68* @param str2 string to be compared.69*70* @returns 0 when the strings are equal, a negative integer71* when str1 is less than str2, or a positive integer if str1 is72* greater than str2, according to the lexicographical order.73*/74static int75pages_cmp(const void* str1, const void* str2)76{77return strcmp((const char*)str1, (const char*)str2);78}7980/*81* sort_pages sorts a list of HyperDoc pages links.82*83* @param links is an array containing the HyperDoc84* links to be sorted.85*86* @size of the number of elements in the links array.87*/88static void89sort_pages(char** links, int size)90{91qsort(links, size, sizeof (char*), pages_cmp);92}9394/*95* presea function constructs a HyperDoc96* page with the links to HyperDoc pages97* that contain the given pattern.98*99* @param links array with the links to the matched100* HyperDoc pages.101* @param n number of elements in the links array.102* @param cases flag indicating if there was any matches.103* @param pattern searched in the HyperDoc pages.104*/105static void106presea(char** links, int n, int cases, const char* pattern)107{108int m = 0;109char** tokens = NULL;110static const char* delimiter = "{";111112for (int i = 0; i < n; i++) {113int j = 0;114115tokens = oa_split(links[i],delimiter,&j);116if (j >= 2)117m = m + atol(oa_substr(tokens[1],0,strlen(tokens[1])-2));118}119120if (cases==1)121printf("\\begin{page}{staticsearchpage}{No matches found}\n");122else if ( n==0 || m==0 )123printf("\\begin{page}{staticsearchpage}{No matches found for {\\em %s}}\n",pattern);124else125printf("\\begin{page}{staticsearchpage}{%d matches found in %d pages for {\\em %s}}\n",m,n,pattern);126printf("Matches\\tab{8}in Page\n");127printf("\\beginscroll\n");128printf("\\beginmenu\n");129for(int i = n-1; i >= 0; i--)130printf ("%s\n",links[i]);131printf("\\endmenu\n");132printf("\\endscroll\n");133printf("\\end{page}\n");134}135136/*137* Set global variables with the locations of the138* Hyperdoc pages, the hthits program and the Hyperdoc139* pages database.140*/141static void142set_variables() {143const std::string systemdir(oa_getenv("AXIOM"));144145if (systemdir.empty()) {146std::cerr << "Could not locate OpenAxiom installation." << std::endl;147exit(-1);148}149150htpagedir = systemdir + "/share/hypertex/pages/";151hthitscmd = systemdir + "/lib/hthits";152htdbfile = htpagedir + "ht.db";153}154155/*156* htsearch invokes hthits to search for pattern157* in the HyperDoc pages.158*159* @param pattern string to be searched in the160* HyperDoc pages.161*/162static void163htsearch(const char* pattern)164{165FILE* hits;166char buf[1024];167char** sorted_hits;168const char* matches = "";169int size = 0;170static const char* delimiter = "\n";171172set_variables();173174if (strcmp(pattern,"") == 0)175presea(NULL,size,1,pattern);176else {177178// hthits requires to change directory179// to where the HyperDoc pages reside.180if (oa_chdir(htpagedir.c_str()) == -1) {181std::cerr << "Cannot change the page directory: "182<< htpagedir << std::endl;183exit(-1);184}185186// Call hthits with: hthits pattern ht.db187hthitscmd = hthitscmd + " " + pattern + " " + htdbfile;188if ((hits = popen(hthitscmd.c_str(), "r")) != NULL) {189while (fgets(buf, 1024, hits) != NULL)190matches = oa_strcat(matches,buf);191pclose(hits);192}193else {194std::cerr << "Could not execute " << hthitscmd << std::endl;195exit(-1);196}197198sorted_hits = oa_split(matches,delimiter,&size);199sort_pages(sorted_hits, size);200presea(sorted_hits,size,0,pattern);201}202}203204/*205* Display how to use the htsearch program.206*/207static void208usage()209{210std::cerr << "Usage: htsearch pattern" << std::endl;211exit(1);212}213214215/* Main routine */216int217main(int argc, char** argv)218{219if (argc == 1)220htsearch("");221else if (argc == 2) {222223if (strcmp(argv[1],"--help") == 0)224usage();225else226htsearch(argv[1]);227}228else229usage();230return 0;231}232233234