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-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#include "openaxiom-c-macros.h"
37
38
#include <sys/stat.h>
39
#include <errno.h>
40
#include <string.h>
41
#include <stdlib.h>
42
43
#include "debug.h"
44
#include "cfuns.h"
45
#include "halloc.h"
46
#include "sockio.h"
47
#include "addfile.h"
48
49
/* FIXME: Remove this kludge */
50
using namespace OpenAxiom;
51
52
53
static int build_ht_filename(char*, char*, char*);
54
static int pathname(char*);
55
56
char *gDatabasePath = NULL;
57
58
/* Return non-zero if the string T is a postfix of S. */
59
static int
60
strpostfix(const char *s, const char *t)
61
{
62
const int slen = strlen(s);
63
const int tlen = strlen(t);
64
return tlen <= slen && strcmp(s + slen - tlen, t) == 0;
65
}
66
67
/* extend_ht : just checks the name and adds a .ht if needed */
68
69
void
70
extend_ht(char *name)
71
{
72
73
if (!strpostfix(name, ".ht") && !strpostfix(name, ".pht"))
74
strcat(name, ".ht");
75
return;
76
}
77
78
#define cwd(n) ((n[0] == '.' && n[1] == '/')?(1):(0))
79
80
/*
81
* This procedure is sent a filename, and from it tries to build the full
82
* filename, this it returns in the fullname variable. If the file is not
83
* found, then it returns a -1. The fname is the fullpath name for the file,
84
* including the .ht extension. The aname is the filename minus the added .ht
85
* extension, and the pathname.
86
*/
87
88
static int
89
build_ht_filename(char *fname, char *aname, char *name)
90
{
91
char *c_dir;
92
char *HTPATH;
93
char *trace;
94
char *trace2;
95
int ht_file;
96
97
if (cwd(name)) {
98
/* user wants to use the current working directory */
99
c_dir = oa_getcwd();
100
strcpy(fname, c_dir);
101
free(c_dir);
102
103
/* Now add the rest of the filename */
104
strcat(fname, "/");
105
strcat(fname, &name[2]);
106
107
/** now copy the actual file name to addname **/
108
for (trace = &name[strlen(name)]; trace != name &&
109
(*trace != '/'); trace--);
110
if (trace == name) {
111
fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",
112
name);
113
exit(-1);
114
}
115
trace++;
116
strcpy(aname, trace);
117
118
/** add the .ht extension if needed **/
119
extend_ht(aname);
120
extend_ht(fname);
121
122
/* Now just try to access the file */
123
return oa_access_file_for_read(fname);
124
}
125
else if (pathname(name)) {
126
/* filename already has the path specified */
127
strcpy(fname, name);
128
129
/** now copy the actual file name to addname **/
130
for (trace = &name[strlen(name)]; trace != name &&
131
(*trace != '/'); trace--);
132
if (trace == name) {
133
fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",
134
name);
135
exit(-1);
136
}
137
trace++;
138
strcpy(aname, trace);
139
140
/** add the .ht extension if needed **/
141
extend_ht(aname);
142
extend_ht(fname);
143
144
/* Now just try to access the file */
145
return oa_access_file_for_read(fname);
146
}
147
else {/** If not I am going to have to append path names to it **/
148
HTPATH = oa_getenv("HTPATH");
149
if (HTPATH == NULL) {
150
/** The user does not have a HTPATH, so I will use the the directory
151
$AXIOM/share/hypertex/pages/ as the default path ***/
152
char *spad = oa_getenv("AXIOM");
153
if (spad == NULL) {
154
fprintf(stderr,
155
"ht_file_open:Cannot find ht data base: setenv HTPATH or AXIOM\n");
156
exit(-1);
157
}
158
HTPATH = (char *) halloc(1024 * sizeof(char), "HTPATH");
159
strcpy(HTPATH, spad);
160
strcat(HTPATH, "/share/hypertex/pages");
161
}
162
163
/** Now that I have filled HTPATH, I should try to open a file by the
164
given name **/
165
strcpy(aname, name);
166
extend_ht(aname);
167
for (ht_file = -1, trace2 = HTPATH;
168
ht_file == -1 && *trace2 != '\0';) {
169
for (trace = fname; *trace2 != '\0' && (*trace2 != ':');)
170
*trace++ = *trace2++;
171
*trace++ = '/';
172
*trace = 0;
173
if (!strcmp(fname, "./")) {
174
/** The person wishes me to check the current directory too **/
175
c_dir = oa_getcwd();
176
strcpy(fname,c_dir);
177
free(c_dir);
178
strcat(fname, "/");
179
}
180
if (*trace2)
181
trace2++;
182
strcat(fname, aname);
183
ht_file = oa_access_file_for_read(fname);
184
}
185
return (ht_file);
186
}
187
}
188
189
static int
190
pathname(char *name)
191
{
192
while (*name)
193
if (*name++ == '/')
194
return 1;
195
196
return 0;
197
}
198
199
/** This procedure opens the proper HT file **/
200
201
FILE *
202
ht_file_open(char *fname, char *aname, char *name)
203
{
204
FILE *ht_fp;
205
int ret_value;
206
207
ret_value = build_ht_filename(fname, aname, name);
208
if (ret_value == -1) {
209
fprintf(stderr, "ht_file_open: Unknown file %s\n", fname);
210
exit(-1);
211
}
212
213
ht_fp = fopen(fname, "r");
214
if (ht_fp == NULL) {
215
perror("ht_file_open");
216
exit(-1);
217
}
218
return (ht_fp);
219
}
220
221
/*
222
* This function is responsible for actually opening the database file. For the
223
* moment it gets the $AXIOM environment variable, and appends to it
224
* "share/hypertex/ht.db", and then opens it
225
*/
226
227
/*
228
* Modified on 12/3/89 to take a second argument. This argument tells the
229
* open routine whether it is reading the db file, or writing it. If writing
230
* is true, then I should check to insure I have proper write access.
231
* -JMW
232
*/
233
234
/*
235
* Modified again on 12/9/89 so that it now uses HTPATH as the path name. Now
236
* it initially loads up the path name into a static variable. Then upon
237
* every trip, it gets the next ht.db found. It returns NULL when no ht.db is
238
* found. -JMW
239
*/
240
241
242
FILE *
243
db_file_open(char *db_file)
244
{
245
static char *db_path_trace = NULL;
246
char *db_file_trace;
247
FILE *db_fp;
248
char *spad;
249
250
/*
251
* The first time through is the only time this could be true. If so, then
252
* create the default HTPATH for gDatabasePath.
253
*/
254
/* fprintf(stderr,"addfile:db_file_open: entered db_file=%s\n",db_file);*/
255
if (gDatabasePath == NULL) {
256
gDatabasePath = oa_getenv("HTPATH");
257
if (gDatabasePath == NULL) {
258
spad = oa_getenv("AXIOM");
259
if (spad == NULL) {
260
/* fprintf(stderr,
261
"addfile:db_file_open: Cannot find ht data base path:\n");*/
262
exit(-1);
263
}
264
gDatabasePath = (char *) halloc(sizeof(char) * 1024, "db_file_open");
265
strcpy(gDatabasePath, spad);
266
strcat(gDatabasePath, "/share/hypertex/pages");
267
}
268
db_path_trace = gDatabasePath;
269
}
270
/*fprintf(stderr,"addfile:db_file_open: db_path_trace=%s\n",db_path_trace);*/
271
/*
272
* Now Loop until I find one with okay filename
273
*/
274
275
for (db_fp = NULL; db_fp == NULL && *db_path_trace != '\0';) {
276
for (db_file_trace = db_file; *db_path_trace != ':' &&
277
*db_path_trace != '\0'; db_path_trace++)
278
*db_file_trace++ = *db_path_trace;
279
*db_file_trace = '\0';
280
strcat(db_file_trace, "/ht.db");
281
/*fprintf(stderr,"addfile:db_file_open: db_file_trace=%s\n",db_file_trace);*/
282
/*fprintf(stderr,"addfile:db_file_open: db_file=%s\n",db_file);*/
283
284
db_fp = fopen(db_file, "r");
285
286
if (*db_path_trace != '\0')
287
db_path_trace++;
288
}
289
/* if (db_fp == NULL)
290
fprintf(stderr,"addfile:db_file_open: exit (null)\n");
291
else
292
fprintf(stderr,"addfile:db_file_open: exit opened\n");
293
*/
294
return (db_fp);
295
}
296
297
298
FILE *
299
temp_file_open(char *temp_db_file)
300
{
301
FILE *temp_db_fp;
302
303
/** Just make the name and open it **/
304
305
strcpy(temp_db_file, oa_get_tmpdir());
306
strcat(temp_db_file, "/ht2.db" /* db_file_name */ );
307
temp_db_fp = fopen(temp_db_file, "w");
308
309
if (temp_db_fp == NULL) {
310
perror("temp_file_open");
311
exit(-1);
312
}
313
return temp_db_fp;
314
}
315
316