Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2010, Gabriel Dos Reis.
5
All rights reserved.
6
Copyright (C) 2009-2010, Alfredo Portes.
7
All rights reserved.
8
9
Redistribution and use in source and binary forms, with or without
10
modification, are permitted provided that the following conditions are
11
met:
12
13
- Redistributions of source code must retain the above copyright
14
notice, this list of conditions and the following disclaimer.
15
16
- Redistributions in binary form must reproduce the above copyright
17
notice, this list of conditions and the following disclaimer in
18
the documentation and/or other materials provided with the
19
distribution.
20
21
- Neither the name of The Numerical Algorithms Group Ltd. nor the
22
names of its contributors may be used to endorse or promote products
23
derived from this software without specific prior written permission.
24
25
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
29
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
*/
37
38
/*
39
* htsearch: is used by Hyperdoc to search for keywords in
40
* Hyperdoc pages and create a HyperDoc page of the search
41
* result.
42
*/
43
44
#include "openaxiom-c-macros.h"
45
46
#include <stdlib.h>
47
#include <stdio.h>
48
#include <string.h>
49
#include <string>
50
#include <iostream>
51
#include "cfuns.h"
52
53
// FIXME: Remove kludge
54
using namespace OpenAxiom;
55
56
// Path to the directory containing the hyperdoc pages.
57
static std::string htpagedir;
58
59
// Path to the hthits program.
60
static std::string hthitscmd;
61
62
// Path to the database file of the hyperdoc pages.
63
static std::string htdbfile;
64
65
/*
66
* pages_cmp compares two strings.
67
*
68
* @param str1 string to be compared.
69
* @param str2 string to be compared.
70
*
71
* @returns 0 when the strings are equal, a negative integer
72
* when str1 is less than str2, or a positive integer if str1 is
73
* greater than str2, according to the lexicographical order.
74
*/
75
static int
76
pages_cmp(const void* str1, const void* str2)
77
{
78
return strcmp((const char*)str1, (const char*)str2);
79
}
80
81
/*
82
* sort_pages sorts a list of HyperDoc pages links.
83
*
84
* @param links is an array containing the HyperDoc
85
* links to be sorted.
86
*
87
* @size of the number of elements in the links array.
88
*/
89
static void
90
sort_pages(char** links, int size)
91
{
92
qsort(links, size, sizeof (char*), pages_cmp);
93
}
94
95
/*
96
* presea function constructs a HyperDoc
97
* page with the links to HyperDoc pages
98
* that contain the given pattern.
99
*
100
* @param links array with the links to the matched
101
* HyperDoc pages.
102
* @param n number of elements in the links array.
103
* @param cases flag indicating if there was any matches.
104
* @param pattern searched in the HyperDoc pages.
105
*/
106
static void
107
presea(char** links, int n, int cases, const char* pattern)
108
{
109
int m = 0;
110
char** tokens = NULL;
111
static const char* delimiter = "{";
112
113
for (int i = 0; i < n; i++) {
114
int j = 0;
115
116
tokens = oa_split(links[i],delimiter,&j);
117
if (j >= 2)
118
m = m + atol(oa_substr(tokens[1],0,strlen(tokens[1])-2));
119
}
120
121
if (cases==1)
122
printf("\\begin{page}{staticsearchpage}{No matches found}\n");
123
else if ( n==0 || m==0 )
124
printf("\\begin{page}{staticsearchpage}{No matches found for {\\em %s}}\n",pattern);
125
else
126
printf("\\begin{page}{staticsearchpage}{%d matches found in %d pages for {\\em %s}}\n",m,n,pattern);
127
printf("Matches\\tab{8}in Page\n");
128
printf("\\beginscroll\n");
129
printf("\\beginmenu\n");
130
for(int i = n-1; i >= 0; i--)
131
printf ("%s\n",links[i]);
132
printf("\\endmenu\n");
133
printf("\\endscroll\n");
134
printf("\\end{page}\n");
135
}
136
137
/*
138
* Set global variables with the locations of the
139
* Hyperdoc pages, the hthits program and the Hyperdoc
140
* pages database.
141
*/
142
static void
143
set_variables() {
144
const std::string systemdir(oa_getenv("AXIOM"));
145
146
if (systemdir.empty()) {
147
std::cerr << "Could not locate OpenAxiom installation." << std::endl;
148
exit(-1);
149
}
150
151
htpagedir = systemdir + "/share/hypertex/pages/";
152
hthitscmd = systemdir + "/lib/hthits";
153
htdbfile = htpagedir + "ht.db";
154
}
155
156
/*
157
* htsearch invokes hthits to search for pattern
158
* in the HyperDoc pages.
159
*
160
* @param pattern string to be searched in the
161
* HyperDoc pages.
162
*/
163
static void
164
htsearch(const char* pattern)
165
{
166
FILE* hits;
167
char buf[1024];
168
char** sorted_hits;
169
const char* matches = "";
170
int size = 0;
171
static const char* delimiter = "\n";
172
173
set_variables();
174
175
if (strcmp(pattern,"") == 0)
176
presea(NULL,size,1,pattern);
177
else {
178
179
// hthits requires to change directory
180
// to where the HyperDoc pages reside.
181
if (oa_chdir(htpagedir.c_str()) == -1) {
182
std::cerr << "Cannot change the page directory: "
183
<< htpagedir << std::endl;
184
exit(-1);
185
}
186
187
// Call hthits with: hthits pattern ht.db
188
hthitscmd = hthitscmd + " " + pattern + " " + htdbfile;
189
if ((hits = popen(hthitscmd.c_str(), "r")) != NULL) {
190
while (fgets(buf, 1024, hits) != NULL)
191
matches = oa_strcat(matches,buf);
192
pclose(hits);
193
}
194
else {
195
std::cerr << "Could not execute " << hthitscmd << std::endl;
196
exit(-1);
197
}
198
199
sorted_hits = oa_split(matches,delimiter,&size);
200
sort_pages(sorted_hits, size);
201
presea(sorted_hits,size,0,pattern);
202
}
203
}
204
205
/*
206
* Display how to use the htsearch program.
207
*/
208
static void
209
usage()
210
{
211
std::cerr << "Usage: htsearch pattern" << std::endl;
212
exit(1);
213
}
214
215
216
/* Main routine */
217
int
218
main(int argc, char** argv)
219
{
220
if (argc == 1)
221
htsearch("");
222
else if (argc == 2) {
223
224
if (strcmp(argv[1],"--help") == 0)
225
usage();
226
else
227
htsearch(argv[1]);
228
}
229
else
230
usage();
231
return 0;
232
}
233
234