Annotation of libwww/Library/src/HTHome.c, revision 2.22

2.1       frystyk     1: /*                                                                  HTHome.c
2.2       frystyk     2: **     ANCHOR TRANSLATIONS
2.1       frystyk     3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.22    ! frystyk     6: **     @(#) $Id: HTHome.c,v 2.21 1996/07/08 19:10:44 frystyk Exp $
2.1       frystyk     7: **
                      8: ** Authors
                      9: **     TBL     Tim Berners-Lee timbl@w3.org
                     10: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
                     11: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
2.2       frystyk    12: **     HFN     Henrik Frystyk
2.1       frystyk    13: ** History
                     14: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     15: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
                     16: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
                     17: **     17 Dec 92 Tn3270 added, bug fix. DD
                     18: **      4 Feb 93 Access registration, Search escapes bad chars TBL
                     19: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     20: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
                     21: **        Dec 93 Bug change around, more reentrant, etc
                     22: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
2.13      frystyk    23: **      8 Jul 94 Insulate free from _free structure element.
2.1       frystyk    24: **        Sep 95 Rewritten, HFN
                     25: **        Nov 95 Spawned from HTAccess.c
                     26: */
                     27: 
                     28: /* Library include files */
                     29: #include "WWWLib.h"
                     30: #include "HTHome.h"                                     /* Implemented here */
                     31: 
                     32: /* ------------------------------------------------------------------------- */
                     33: 
                     34: /*     Find Related Name
                     35: **     -----------------
                     36: **     Creates a string that can be used as a related name when 
                     37: **     calling HTParse initially. 
                     38: **  
                     39: **     The code for this routine originates from the Linemode 
                     40: **     browser and was moved here by howcome@w3.org
                     41: **     in order for all clients to take advantage.
2.18      frystyk    42: **     The string returned must be freed by the caller
2.1       frystyk    43: */
2.18      frystyk    44: PUBLIC char * HTGetCurrentDirectoryURL (void)
2.1       frystyk    45: {
                     46:     char* default_default = NULL;            /* Parse home relative to this */
2.16      frystyk    47:     char * host = HTGetHostName(); 
2.1       frystyk    48:     StrAllocCopy(default_default, "file://");
2.16      frystyk    49:     if (host) {
2.1       frystyk    50:        StrAllocCat(default_default, host);
2.16      frystyk    51:        HT_FREE(host);
                     52:     } else
2.1       frystyk    53:        StrAllocCat(default_default, "localhost");
                     54:     {
                     55:        char wd[HT_MAX_PATH+1];
                     56: 
2.14      frystyk    57: #ifdef HAVE_GETCWD           /* System V variant SIGN CHANGED TBL 921006 !! */
                     58:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                     59: #else
2.12      frystyk    60: #ifdef HAVE_GETWD
                     61:        char *result = (char *) getwd(wd);
                     62: #else
                     63: #error "This platform does not support neither getwd nor getcwd\n"
2.1       frystyk    64:        char *result = NULL;
2.14      frystyk    65: #endif /* HAVE_GETWD */
2.12      frystyk    66: #endif /* HAVE_GETCWD */
2.1       frystyk    67:        *(wd+HT_MAX_PATH) = '\0';
                     68:        if (result) {
                     69: #ifdef VMS 
                     70:             /* convert directory name to Unix-style syntax */
                     71:            char * disk = strchr (wd, ':');
                     72:            char * dir = strchr (wd, '[');
                     73:            if (disk) {
                     74:                *disk = '\0';
                     75:                StrAllocCat (default_default, "/");  /* needs delimiter */
                     76:                StrAllocCat (default_default, wd);
                     77:            }
                     78:            if (dir) {
                     79:                char *p;
                     80:                *dir = '/';  /* Convert leading '[' */
                     81:                for (p = dir ; *p != ']'; ++p)
                     82:                        if (*p == '.') *p = '/';
                     83:                *p = '\0';  /* Cut on final ']' */
                     84:                StrAllocCat (default_default, dir);
                     85:            }
                     86: #else  /* not VMS */
                     87: #ifdef WIN32
                     88:            char * p = wd ;     /* a colon */
                     89:            StrAllocCat(default_default, "/");
                     90:            while( *p != 0 ) { 
                     91:                if (*p == '\\')                  /* change to one true slash */
                     92:                    *p = '/' ;
                     93:                p++;
                     94:            }
                     95:            StrAllocCat( default_default, wd);
                     96: #else /* not WIN32 */
                     97:            StrAllocCat (default_default, wd);
                     98: #endif /* not WIN32 */
                     99: #endif /* not VMS */
                    100:        }
                    101:     }
                    102:     StrAllocCat(default_default, "/default.html");
                    103:     return default_default;
                    104: }
                    105: 
                    106: 
                    107: /*     Generate the anchor for the home page
                    108: **     -------------------------------------
                    109: **
                    110: **     As it involves file access, this should only be done once
                    111: **     when the program first runs.
                    112: **     This is a default algorithm -- browser don't HAVE to use this.
                    113: **     But consistency betwen browsers is STRONGLY recommended!
                    114: **
                    115: **     Priority order is:
                    116: **
                    117: **             1       WWW_HOME environment variable (logical name, etc)
                    118: **             2       ~/WWW/default.html
                    119: **             3       /usr/local/bin/default.html
                    120: **             4       http://www.w3.org/default.html
                    121: **
                    122: */
                    123: PUBLIC HTParentAnchor * HTHomeAnchor (void)
                    124: {
                    125:     char * my_home_document = NULL;
                    126:     char * home = (char *) getenv(LOGICAL_DEFAULT);
                    127:     char * ref;
                    128:     HTParentAnchor * anchor;
                    129:     
                    130:     /* Someone telnets in, they get a special home */
                    131:     if (home) {
                    132:         StrAllocCopy(my_home_document, home);
                    133:     } else  if (HTLib_secure()) {                                  /* Telnet server */
                    134:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    135:        char * status;
                    136:        if (fp) {
2.10      frystyk   137:            if ((my_home_document = (char *) HT_MALLOC(HT_MAX_PATH)) == NULL)
                    138:                HT_OUTOFMEM("my_home_document ");
2.1       frystyk   139:            status = fgets(my_home_document, HT_MAX_PATH, fp);
                    140:            if (!status) {
2.10      frystyk   141:                HT_FREE(my_home_document);
2.1       frystyk   142:                my_home_document = NULL;
                    143:            }
                    144:            fclose(fp);
                    145:        }
                    146:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    147:     }
                    148: 
                    149:     if (!my_home_document) {
                    150:        FILE * fp = NULL;
                    151:        char * home = (char *) getenv("HOME");
                    152:        if (home) { 
2.10      frystyk   153:            if ((my_home_document = (char  *) HT_MALLOC(strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1)) == NULL)
                    154:                HT_OUTOFMEM("HTLocalName");
2.1       frystyk   155:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    156:            fp = fopen(my_home_document, "r");
                    157:        }
                    158:        
                    159:        if (!fp) {
                    160:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    161:            fp = fopen(my_home_document, "r");
                    162:        }
                    163:        if (fp) {
                    164:            fclose(fp);
                    165:        } else {
                    166:            if (WWWTRACE)
2.12      frystyk   167:                HTTrace("Home Anchor. No local home document in ~/%s or %s\n",
2.1       frystyk   168:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
2.10      frystyk   169:            HT_FREE(my_home_document);
2.1       frystyk   170:            my_home_document = NULL;
                    171:        }
                    172:     }
2.12      frystyk   173: 
2.1       frystyk   174:     ref = HTParse(my_home_document ? my_home_document :
                    175:                  HTLib_secure() ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    176:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    177:     if (my_home_document) {
                    178:        if (WWWTRACE)
2.12      frystyk   179:            HTTrace("Home Anchor. `%s\' used for custom home page as\n`%s\'\n",
2.1       frystyk   180:                    my_home_document, ref);
2.10      frystyk   181:        HT_FREE(my_home_document);
2.1       frystyk   182:     }
                    183:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
2.10      frystyk   184:     HT_FREE(ref);
2.1       frystyk   185:     return anchor;
2.19      frystyk   186: }
                    187: 
                    188: /*
2.22    ! frystyk   189: **     Creates a temporary anchor that doesn't exist
        !           190: */
        !           191: PUBLIC HTParentAnchor * HTTmpAnchor (HTUserProfile * up)
        !           192: {
        !           193:     static int offset = 0;                         /* Just keep counting... */
        !           194:     time_t t = time(NULL);
        !           195:     char * tmpfile = HTGetTmpFileName(HTUserProfile_tmp(up));
        !           196:     char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
        !           197:     if (tmpfile && tmpurl && t >= 0) {
        !           198:        char * result;
        !           199:        if (!(result = (char *) HT_MALLOC(strlen(tmpurl)+20)))
        !           200:            HT_OUTOFMEM("HTTmpAnchor");
        !           201:        sprintf(result, "%s.%d.%d", tmpurl, t, offset++);
        !           202:        if (APP_TRACE) HTTrace("Tmp Anchor.. With location `%s\'\n", result);
        !           203:        return HTAnchor_parent(HTAnchor_findAddress(result));
        !           204:        HT_FREE(result);
        !           205:     }
        !           206:     HT_FREE(tmpfile);
        !           207:     HT_FREE(tmpurl);
        !           208:     return NULL;
        !           209: }
        !           210: 
        !           211: /*
2.19      frystyk   212: **     Standard interface to libwww TRACE messages. Pass this function a
                    213: **     string of characters and it will set up the appropriate TRACE flags.
                    214: **     The shortnames for the trace messages are not as intuitive as they
                    215: **     could be :-(. The string must be null terminated
                    216: */
                    217: PUBLIC int HTSetTraceMessageMask (const char * shortnames)
                    218: {
                    219: #ifdef WWWTRACE
                    220:     WWWTRACE = 0;
                    221:     if (shortnames) {
2.21      frystyk   222:        char * ptr = (char *) shortnames;
                    223:        for(; *ptr; ptr++) {
2.19      frystyk   224:            switch (*ptr) {
2.22    ! frystyk   225:            case 'f': WWWTRACE |= SHOW_UTIL_TRACE;      break;
        !           226:            case 'l': WWWTRACE |= SHOW_APP_TRACE;       break;
2.19      frystyk   227:            case 'c': WWWTRACE |= SHOW_CACHE_TRACE;     break;
                    228:            case 'g': WWWTRACE |= SHOW_SGML_TRACE;      break;
2.22    ! frystyk   229:            case 'b': WWWTRACE |= SHOW_BIND_TRACE;      break;
        !           230:            case 't': WWWTRACE |= SHOW_THREAD_TRACE;    break;
        !           231:            case 's': WWWTRACE |= SHOW_STREAM_TRACE;    break;
        !           232:            case 'p': WWWTRACE |= SHOW_PROTOCOL_TRACE;  break;
        !           233:            case 'm': WWWTRACE |= SHOW_MEM_TRACE;       break;
        !           234:            case 'u': WWWTRACE |= SHOW_URI_TRACE;       break;
2.19      frystyk   235:            case 'h': WWWTRACE |= SHOW_AUTH_TRACE;      break;
2.22    ! frystyk   236:            case 'a': WWWTRACE |= SHOW_ANCHOR_TRACE;    break;
2.19      frystyk   237:            case 'i': WWWTRACE |= SHOW_PICS_TRACE;      break;
                    238:            case 'o': WWWTRACE |= SHOW_CORE_TRACE;      break;
                    239:            default:
                    240:                if (WWWTRACE) HTTrace("Trace....... Bad argument\n");
                    241:            }
                    242:        }
2.20      frystyk   243:        if (!WWWTRACE) WWWTRACE = SHOW_ALL_TRACE;
2.19      frystyk   244:     }
                    245:     return WWWTRACE;
                    246: #else
                    247:     return 0;
                    248: #endif
2.1       frystyk   249: }

Webmaster