aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/htsearch.cc
blob: d67c607493928343fb793f7a7e399064a7beed5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 
  Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
  All rights reserved.
  Copyright (C) 2007-2010, Gabriel Dos Reis.
  All rights reserved.
  Copyright (C) 2009-2010, Alfredo Portes.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      - Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

      - Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in
        the documentation and/or other materials provided with the
        distribution.

      - Neither the name of The Numerical Algorithms Group Ltd. nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* 
 * htsearch: is used by Hyperdoc to search for keywords in 
 * Hyperdoc pages and create a HyperDoc page of the search 
 * result.
 */

#include "openaxiom-c-macros.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include "cfuns.h"

 // Path to the directory containing the hyperdoc pages.
static std::string htpagedir;

// Path to the  hthits program.
static std::string hthitscmd;

// Path to the database file of the hyperdoc pages.
static std::string htdbfile;

/*
 * pages_cmp compares two strings.
 *
 * @param str1 string to be compared.
 * @param str2 string to be compared.
 *
 * @returns 0 when the strings are equal, a negative integer 
 * when str1 is less than str2, or a positive integer if str1 is 
 * greater than str2, according to the lexicographical order.
 */
static int 
pages_cmp(const void* str1, const void* str2)
{
    return strcmp((const char*)str1, (const char*)str2);
}

/*
 * sort_pages sorts a list of HyperDoc pages links.
 *
 * @param links is an array containing the HyperDoc 
 * links to be sorted.
 *
 * @size of the number of elements in the links array.
 */
static void 
sort_pages(char** links, int size)
{
    qsort(links, size, sizeof (char*), pages_cmp);
}

/*
 * presea function constructs a HyperDoc
 * page with the links to HyperDoc pages 
 * that contain the given pattern.
 *
 * @param links array with the links to the matched
 *        HyperDoc pages.
 * @param n number of elements in the links array.
 * @param cases flag indicating if there was any matches.
 * @param pattern searched in the HyperDoc pages.
 */
static void 
presea(char** links, int n, int cases, const char* pattern)
{
   int m = 0;
   char** tokens = NULL;
   static const char* delimiter = "{";

   for (int i = 0; i < n; i++) {
      int j = 0;
      
      tokens = oa_split(links[i],delimiter,&j);
      if (j >= 2)
         m = m + atol(oa_substr(tokens[1],0,strlen(tokens[1])-2));
   }
   
   if (cases==1)
      printf("\\begin{page}{staticsearchpage}{No matches found}\n");
   else if ( n==0 || m==0 )
      printf("\\begin{page}{staticsearchpage}{No matches found for {\\em %s}}\n",pattern);
   else
      printf("\\begin{page}{staticsearchpage}{%d matches found in %d pages for {\\em %s}}\n",m,n,pattern);
   printf("Matches\\tab{8}in Page\n");
   printf("\\beginscroll\n");
   printf("\\beginmenu\n");
   for(int i = n-1; i >= 0; i--)
      printf ("%s\n",links[i]);
   printf("\\endmenu\n");
   printf("\\endscroll\n");
   printf("\\end{page}\n");
}

/*
 * Set global variables with the locations of the
 * Hyperdoc pages, the hthits program and the Hyperdoc
 * pages database.
 */
static void
set_variables(void) {
   const std::string systemdir(oa_getenv("AXIOM"));
   
   if (systemdir.empty()) {
      std::cerr << "Could not locate OpenAxiom installation." << std::endl;
      exit(-1);
   }
   
   htpagedir = systemdir + "/share/hypertex/pages/";
   hthitscmd = systemdir + "/lib/hthits";
   htdbfile = htpagedir + "ht.db";
}

/* 
 * htsearch invokes hthits to search for pattern
 * in the HyperDoc pages.
 *
 * @param pattern string to be searched in the
 * HyperDoc pages.
 */
static void 
htsearch(const char* pattern)
{
    FILE* hits;
    char buf[1024];
    char** sorted_hits;
    const char* matches = "";
    int size = 0;
    static const char* delimiter = "\n";

    set_variables();

    if (strcmp(pattern,"") == 0)
        presea(NULL,size,1,pattern);
    else {

        // hthits requires to change directory
        // to where the HyperDoc pages reside.
       if (oa_chdir(htpagedir.c_str()) == -1) {
          std::cerr << "Cannot change the page directory: "
                    << htpagedir << std::endl;
            exit(-1);
        }
        
        // Call hthits with: hthits pattern ht.db
        hthitscmd = hthitscmd + " " + pattern + " " + htdbfile;
        if ((hits = popen(hthitscmd.c_str(), "r")) != NULL) {
            while (fgets(buf, 1024, hits) != NULL)
                matches = oa_strcat(matches,buf);
            pclose(hits);
        }
        else {
           std::cerr << "Could not execute " << hthitscmd << std::endl;
            exit(-1);
        }

        sorted_hits = oa_split(matches,delimiter,&size);
        sort_pages(sorted_hits, size);
        presea(sorted_hits,size,0,pattern);
    }
}

/*
 * Display how to use the htsearch program.  
 */
static void
usage(void)
{
   std::cerr << "Usage: htsearch pattern" << std::endl;
    exit(1);
}


/* Main routine */
int 
main(int argc, char** argv)
{
   if (argc == 1)
      htsearch("");
   else if (argc == 2) {
      
      if (strcmp(argv[1],"--help") == 0)
         usage();
      else
         htsearch(argv[1]);
   }
   else
      usage();
   return 0;
}