/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2008, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#include "openaxiom-c-macros.h"3637#include <sys/stat.h>38#include <errno.h>39#include <string.h>40#include <stdlib.h>4142#include "debug.h"43#include "cfuns.h"44#include "halloc.h"45#include "sockio.h"46#include "addfile.h"4748/* FIXME: Remove this kludge */49using namespace OpenAxiom;505152static int build_ht_filename(char*, char*, char*);53static int pathname(char*);5455char *gDatabasePath = NULL;5657/* Return non-zero if the string T is a postfix of S. */58static int59strpostfix(const char *s, const char *t)60{61const int slen = strlen(s);62const int tlen = strlen(t);63return tlen <= slen && strcmp(s + slen - tlen, t) == 0;64}6566/* extend_ht : just checks the name and adds a .ht if needed */6768void69extend_ht(char *name)70{7172if (!strpostfix(name, ".ht") && !strpostfix(name, ".pht"))73strcat(name, ".ht");74return;75}7677#define cwd(n) ((n[0] == '.' && n[1] == '/')?(1):(0))7879/*80* This procedure is sent a filename, and from it tries to build the full81* filename, this it returns in the fullname variable. If the file is not82* found, then it returns a -1. The fname is the fullpath name for the file,83* including the .ht extension. The aname is the filename minus the added .ht84* extension, and the pathname.85*/8687static int88build_ht_filename(char *fname, char *aname, char *name)89{90char *c_dir;91char *HTPATH;92char *trace;93char *trace2;94int ht_file;9596if (cwd(name)) {97/* user wants to use the current working directory */98c_dir = oa_getcwd();99strcpy(fname, c_dir);100free(c_dir);101102/* Now add the rest of the filename */103strcat(fname, "/");104strcat(fname, &name[2]);105106/** now copy the actual file name to addname **/107for (trace = &name[strlen(name)]; trace != name &&108(*trace != '/'); trace--);109if (trace == name) {110fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",111name);112exit(-1);113}114trace++;115strcpy(aname, trace);116117/** add the .ht extension if needed **/118extend_ht(aname);119extend_ht(fname);120121/* Now just try to access the file */122return oa_access_file_for_read(fname);123}124else if (pathname(name)) {125/* filename already has the path specified */126strcpy(fname, name);127128/** now copy the actual file name to addname **/129for (trace = &name[strlen(name)]; trace != name &&130(*trace != '/'); trace--);131if (trace == name) {132fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n",133name);134exit(-1);135}136trace++;137strcpy(aname, trace);138139/** add the .ht extension if needed **/140extend_ht(aname);141extend_ht(fname);142143/* Now just try to access the file */144return oa_access_file_for_read(fname);145}146else {/** If not I am going to have to append path names to it **/147HTPATH = oa_getenv("HTPATH");148if (HTPATH == NULL) {149/** The user does not have a HTPATH, so I will use the the directory150$AXIOM/share/hypertex/pages/ as the default path ***/151char *spad = oa_getenv("AXIOM");152if (spad == NULL) {153fprintf(stderr,154"ht_file_open:Cannot find ht data base: setenv HTPATH or AXIOM\n");155exit(-1);156}157HTPATH = (char *) halloc(1024 * sizeof(char), "HTPATH");158strcpy(HTPATH, spad);159strcat(HTPATH, "/share/hypertex/pages");160}161162/** Now that I have filled HTPATH, I should try to open a file by the163given name **/164strcpy(aname, name);165extend_ht(aname);166for (ht_file = -1, trace2 = HTPATH;167ht_file == -1 && *trace2 != '\0';) {168for (trace = fname; *trace2 != '\0' && (*trace2 != ':');)169*trace++ = *trace2++;170*trace++ = '/';171*trace = 0;172if (!strcmp(fname, "./")) {173/** The person wishes me to check the current directory too **/174c_dir = oa_getcwd();175strcpy(fname,c_dir);176free(c_dir);177strcat(fname, "/");178}179if (*trace2)180trace2++;181strcat(fname, aname);182ht_file = oa_access_file_for_read(fname);183}184return (ht_file);185}186}187188static int189pathname(char *name)190{191while (*name)192if (*name++ == '/')193return 1;194195return 0;196}197198/** This procedure opens the proper HT file **/199200FILE *201ht_file_open(char *fname, char *aname, char *name)202{203FILE *ht_fp;204int ret_value;205206ret_value = build_ht_filename(fname, aname, name);207if (ret_value == -1) {208fprintf(stderr, "ht_file_open: Unknown file %s\n", fname);209exit(-1);210}211212ht_fp = fopen(fname, "r");213if (ht_fp == NULL) {214perror("ht_file_open");215exit(-1);216}217return (ht_fp);218}219220/*221* This function is responsible for actually opening the database file. For the222* moment it gets the $AXIOM environment variable, and appends to it223* "share/hypertex/ht.db", and then opens it224*/225226/*227* Modified on 12/3/89 to take a second argument. This argument tells the228* open routine whether it is reading the db file, or writing it. If writing229* is true, then I should check to insure I have proper write access.230* -JMW231*/232233/*234* Modified again on 12/9/89 so that it now uses HTPATH as the path name. Now235* it initially loads up the path name into a static variable. Then upon236* every trip, it gets the next ht.db found. It returns NULL when no ht.db is237* found. -JMW238*/239240241FILE *242db_file_open(char *db_file)243{244static char *db_path_trace = NULL;245char *db_file_trace;246FILE *db_fp;247char *spad;248249/*250* The first time through is the only time this could be true. If so, then251* create the default HTPATH for gDatabasePath.252*/253/* fprintf(stderr,"addfile:db_file_open: entered db_file=%s\n",db_file);*/254if (gDatabasePath == NULL) {255gDatabasePath = oa_getenv("HTPATH");256if (gDatabasePath == NULL) {257spad = oa_getenv("AXIOM");258if (spad == NULL) {259/* fprintf(stderr,260"addfile:db_file_open: Cannot find ht data base path:\n");*/261exit(-1);262}263gDatabasePath = (char *) halloc(sizeof(char) * 1024, "db_file_open");264strcpy(gDatabasePath, spad);265strcat(gDatabasePath, "/share/hypertex/pages");266}267db_path_trace = gDatabasePath;268}269/*fprintf(stderr,"addfile:db_file_open: db_path_trace=%s\n",db_path_trace);*/270/*271* Now Loop until I find one with okay filename272*/273274for (db_fp = NULL; db_fp == NULL && *db_path_trace != '\0';) {275for (db_file_trace = db_file; *db_path_trace != ':' &&276*db_path_trace != '\0'; db_path_trace++)277*db_file_trace++ = *db_path_trace;278*db_file_trace = '\0';279strcat(db_file_trace, "/ht.db");280/*fprintf(stderr,"addfile:db_file_open: db_file_trace=%s\n",db_file_trace);*/281/*fprintf(stderr,"addfile:db_file_open: db_file=%s\n",db_file);*/282283db_fp = fopen(db_file, "r");284285if (*db_path_trace != '\0')286db_path_trace++;287}288/* if (db_fp == NULL)289fprintf(stderr,"addfile:db_file_open: exit (null)\n");290else291fprintf(stderr,"addfile:db_file_open: exit opened\n");292*/293return (db_fp);294}295296297FILE *298temp_file_open(char *temp_db_file)299{300FILE *temp_db_fp;301302/** Just make the name and open it **/303304strcpy(temp_db_file, oa_get_tmpdir());305strcat(temp_db_file, "/ht2.db" /* db_file_name */ );306temp_db_fp = fopen(temp_db_file, "w");307308if (temp_db_fp == NULL) {309perror("temp_file_open");310exit(-1);311}312return temp_db_fp;313}314315316