aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/addfile.c
blob: 7b6121b5a07542e5ccebe2a473190b086b7058cb (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
  Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
  All rights reserved.
  Copyright (C) 2007-2008, Gabriel Dos Reis.
  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.
*/

#include "openaxiom-c-macros.h"

#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include "debug.h"
#include "cfuns.h"
#include "halloc.h"
#include "sockio.h"
#include "addfile.h"



static int build_ht_filename(char*, char*, char*);
static int pathname(char*);
static int strpostfix(char*, char*);


char *gDatabasePath = NULL;

static int
strpostfix(char *s, char *t)
{
    int slen = strlen(s), tlen = strlen(t);

    if (tlen > slen)
        return 0;
    while (tlen > 0)
        if (s[--slen] != t[--tlen])
            return 0;
    return 1;
}

/* extend_ht : just checks the name and adds a .ht if needed */

void
extend_ht(char *name)
{

    if (!strpostfix(name, ".ht") && !strpostfix(name, ".pht"))
        strcat(name, ".ht");
    return;
}

#define cwd(n) ((n[0] == '.' && n[1] == '/')?(1):(0))

/*
 * This procedure is sent a filename, and from it tries to build the full
 * filename, this it returns in the fullname variable. If the file is not
 * found, then it returns a -1. The fname is the fullpath name for the file,
 * including the .ht extension. The aname is the filename minus the added .ht
 * extension, and the pathname.
 */

static int
build_ht_filename(char *fname, char *aname, char *name)
{
    char *c_dir;
    char *HTPATH;
    char *trace;
    char *trace2;
    int ht_file;

    if (cwd(name)) {
        /* user wants to use the current working directory */
        c_dir = oa_getcwd();
        strcpy(fname, c_dir);
        free(c_dir);

        /* Now add the rest of the filename */
        strcat(fname, "/");
        strcat(fname, &name[2]);

        /** now copy the actual file name to addname **/
        for (trace = &name[strlen(name)]; trace != name &&
             (*trace != '/'); trace--);
        if (trace == name) {
            fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",
                    name);
            exit(-1);
        }
        trace++;
        strcpy(aname, trace);

        /** add  the .ht extension if needed **/
        extend_ht(aname);
        extend_ht(fname);

        /* Now just try to access the file */
        return oa_access_file_for_read(fname);
    }
    else if (pathname(name)) {
        /* filename already has the path specified */
        strcpy(fname, name);

        /** now copy the actual file name to addname **/
        for (trace = &name[strlen(name)]; trace != name &&
             (*trace != '/'); trace--);
        if (trace == name) {
            fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",
                    name);
            exit(-1);
        }
        trace++;
        strcpy(aname, trace);

        /** add  the .ht extension if needed **/
        extend_ht(aname);
        extend_ht(fname);

        /* Now just try to access the file */
        return oa_access_file_for_read(fname);
    }
    else {/** If not I am going to have to append path names to it **/
        HTPATH = (char *) getenv("HTPATH");
        if (HTPATH == NULL) {
        /** The user does not have a HTPATH, so I will use the the directory
        $AXIOM/share/hypertex/pages/ as the default path ***/
          char *spad = (char *) getenv("AXIOM");
          if (spad == NULL) {
            fprintf(stderr,
            "ht_file_open:Cannot find ht data base: setenv HTPATH or AXIOM\n");
             exit(-1);
          }
          HTPATH = (char *) halloc(1024 * sizeof(char), "HTPATH");
          strcpy(HTPATH, spad);
          strcat(HTPATH, "/share/hypertex/pages");
        }

        /** Now that I have filled HTPATH, I should try to open a file by the
          given name **/
        strcpy(aname, name);
        extend_ht(aname);
        for (ht_file = -1, trace2 = HTPATH;
             ht_file == -1 && *trace2 != '\0';) {
            for (trace = fname; *trace2 != '\0' && (*trace2 != ':');)
                *trace++ = *trace2++;
            *trace++ = '/';
            *trace = 0;
            if (!strcmp(fname, "./")) {
                /** The person wishes me to check the current directory too **/
               c_dir = oa_getcwd();
               strcpy(fname,c_dir);
               free(c_dir);
               strcat(fname, "/");
            }
            if (*trace2)
                trace2++;
            strcat(fname, aname);
            ht_file = oa_access_file_for_read(fname);
        }
        return (ht_file);
    }
}

static int
pathname(char *name)
{
    while (*name)
        if (*name++ == '/')
            return 1;

    return 0;
}

/** This procedure opens the proper HT file **/

FILE *
ht_file_open(char *fname, char *aname, char *name)
{
    FILE *ht_fp;
    int ret_value;

    ret_value = build_ht_filename(fname, aname, name);
    if (ret_value == -1) {
        fprintf(stderr, "ht_file_open: Unknown file %s\n", fname);
        exit(-1);
    }

    ht_fp = fopen(fname, "r");
    if (ht_fp == NULL) {
        perror("ht_file_open");
        exit(-1);
    }
    return (ht_fp);
}

/*
 * This function is responsible for actually opening the database file. For the
 * moment it gets the $AXIOM environment variable, and appends to it
 * "share/hypertex/ht.db", and then opens it
 */

/*
 * Modified on 12/3/89 to take a second argument. This argument tells the
 * open routine whether it is reading the db file, or writing it. If writing
 * is true, then I should check to insure I have proper write access.
 * -JMW
 */

/*
 * Modified again on 12/9/89 so that it now uses HTPATH as the path name. Now
 * it initially loads up the path name into a static variable. Then upon
 * every trip, it gets the next ht.db found. It returns NULL when no ht.db is
 * found. -JMW
 */


FILE *
db_file_open(char *db_file)
{
    static char *db_path_trace = NULL;
    char *db_file_trace;
    FILE *db_fp;
    char *spad;

    /*
     * The first time through is the only time this could be true. If so, then
     * create the default HTPATH for gDatabasePath.
     */
/*    fprintf(stderr,"addfile:db_file_open: entered db_file=%s\n",db_file);*/
    if (gDatabasePath == NULL) {
        gDatabasePath = (char *) getenv("HTPATH");
        if (gDatabasePath == NULL) {
            spad = (char *) getenv("AXIOM");
            if (spad == NULL) {
/*                fprintf(stderr,
                   "addfile:db_file_open: Cannot find ht data base path:\n");*/
                exit(-1);
            }
            gDatabasePath = (char *) halloc(sizeof(char) * 1024, "db_file_open");
            strcpy(gDatabasePath, spad);
            strcat(gDatabasePath, "/share/hypertex/pages");
        }
        db_path_trace = gDatabasePath;
    }
/*fprintf(stderr,"addfile:db_file_open: db_path_trace=%s\n",db_path_trace);*/
    /*
     * Now Loop until I find one with okay filename
     */

    for (db_fp = NULL; db_fp == NULL && *db_path_trace != '\0';) {
        for (db_file_trace = db_file; *db_path_trace != ':' &&
             *db_path_trace != '\0'; db_path_trace++)
            *db_file_trace++ = *db_path_trace;
        *db_file_trace = '\0';
        strcat(db_file_trace, "/ht.db");
/*fprintf(stderr,"addfile:db_file_open: db_file_trace=%s\n",db_file_trace);*/
/*fprintf(stderr,"addfile:db_file_open: db_file=%s\n",db_file);*/

        db_fp = fopen(db_file, "r");

        if (*db_path_trace != '\0')
            db_path_trace++;
    }
/*    if (db_fp == NULL)
      fprintf(stderr,"addfile:db_file_open: exit (null)\n");
    else
      fprintf(stderr,"addfile:db_file_open: exit opened\n");
*/
    return (db_fp);
}


FILE *
temp_file_open(char *temp_db_file)
{
    FILE *temp_db_fp;

    /** Just make the name and open it **/

    strcpy(temp_db_file, oa_get_tmpdir());
    strcat(temp_db_file, "/ht2.db" /* db_file_name */ );
    temp_db_fp = fopen(temp_db_file, "w");

    if (temp_db_fp == NULL) {
        perror("temp_file_open");
        exit(-1);
    }
    return temp_db_fp;
}