Annotation of Amaya/amaya/AHTURLTools.c, revision 1.72

1.7       cvs         1: /*
                      2:  *
                      3:  *  (c) COPYRIGHT MIT and INRIA, 1996.
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.9       cvs         7: 
1.10      cvs         8: /*
                      9:  * AHTURLTools.c: contains all the functions for testing, manipulating,
1.25      cvs        10:  * and normalizing URLs. It also contains a local copy of the libWWW
                     11:  * URL parsing functions.
1.10      cvs        12:  *
                     13:  * Authors: J. Kahan, I. Vatton
1.45      cvs        14:  *          R. Guetari (Stuff related to Windows).
1.10      cvs        15:  *
                     16:  */
1.7       cvs        17:  
1.15      cvs        18: #define THOT_EXPORT extern
1.3       cvs        19: #include "amaya.h"
                     20: 
1.8       cvs        21: #include "init_f.h"
                     22: #include "AHTURLTools_f.h"
                     23: 
1.24      cvs        24: #define MAX_PRINT_URL_LENGTH 50
1.29      cvs        25: typedef struct _HTURI {
1.67      cvs        26:     STRING  access;            /* Now known as "scheme" */
                     27:     STRING  host;
                     28:     STRING  absolute;
                     29:     STRING  relative;
                     30:     STRING  fragment;
1.29      cvs        31: } HTURI;
1.24      cvs        32: 
1.28      cvs        33: 
                     34: /*----------------------------------------------------------------------
                     35:   ConvertToLowerCase
                     36:   Converts a string to lowercase.
                     37:   ----------------------------------------------------------------------*/
1.22      cvs        38: #ifdef __STDC__
1.67      cvs        39: void         ConvertToLowerCase (STRING string)
1.28      cvs        40: #else  /* __STDC__ */
1.38      cvs        41: void         ConvertToLowerCase (string)
1.67      cvs        42: STRING       string;
1.28      cvs        43: 
                     44: #endif /* __STDC__ */
                     45: {
                     46:  int i;
                     47: 
                     48:  if (!string)
                     49:    return;
                     50: 
                     51:  for (i = 0; string[i] != EOS; i++)
1.67      cvs        52:    string[i] = utolower (string[i]);
1.28      cvs        53: }
1.22      cvs        54: 
1.8       cvs        55: /*----------------------------------------------------------------------
1.11      cvs        56:   ExplodeURL 
1.8       cvs        57:   ----------------------------------------------------------------------*/
                     58: #ifdef __STDC__
                     59: void                ExplodeURL (char *url, char **proto, char **host, char **dir, char **file)
                     60: #else
                     61: void                ExplodeURL (url, proto, host, dir, file)
                     62: char               *url;
                     63: char              **proto;
                     64: char              **host;
                     65: char              **dir;
                     66: char              **file;
                     67: 
                     68: #endif
                     69: {
1.33      cvs        70:    char            *curr, *temp;
                     71:    char             used_sep;
1.32      cvs        72: 
1.33      cvs        73:    if (url && strchr (url, URL_SEP))
                     74:      used_sep = URL_SEP;
                     75:    else
                     76:      used_sep = DIR_SEP;
1.8       cvs        77: 
                     78:    if ((url == NULL) || (proto == NULL) || (host == NULL) ||
                     79:        (dir == NULL) || (file == NULL))
                     80:       return;
                     81: 
                     82:    /* initialize every pointer */
                     83:    *proto = *host = *dir = *file = NULL;
                     84: 
                     85:    /* skip any leading space */
                     86:    while ((*url == SPACE) || (*url == TAB))
                     87:       url++;
1.9       cvs        88:    curr = url;
                     89:    if (*curr == 0)
1.8       cvs        90:       goto finished;
                     91: 
                     92:    /* go to the end of the URL */
1.68      cvs        93:    while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
                     94:          (*curr != __CR__) && (*curr != EOL))
1.9       cvs        95:       curr++;
1.8       cvs        96: 
                     97:    /* mark the end of the chain */
1.9       cvs        98:    *curr = EOS;
                     99:    curr--;
                    100:    if (curr <= url)
1.8       cvs       101:       goto finished;
                    102: 
                    103:    /* search the next DIR_SEP indicating the beginning of the file name */
                    104:    do
1.11      cvs       105:      curr--;
1.33      cvs       106:    while ((curr >= url) && (*curr != used_sep));
1.11      cvs       107: 
1.9       cvs       108:    if (curr < url)
1.8       cvs       109:       goto finished;
1.9       cvs       110:    *file = curr + 1;
1.8       cvs       111: 
                    112:    /* mark the end of the dir */
1.9       cvs       113:    *curr = EOS;
                    114:    curr--;
                    115:    if (curr < url)
1.8       cvs       116:       goto finished;
                    117: 
1.29      cvs       118:    /* search for the DIR_STR indicating the host name start */
1.33      cvs       119:    while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9       cvs       120:       curr--;
1.8       cvs       121: 
                    122:    /* if we found it, separate the host name from the directory */
1.33      cvs       123:    if ((*curr == DIR_SEP) && (*(curr + 1) == used_sep))
1.8       cvs       124:      {
1.9       cvs       125:        *host = temp = curr + 2;
1.33      cvs       126:        while ((*temp != 0) && (*temp != used_sep))
1.8       cvs       127:           temp++;
1.33      cvs       128:        if (*temp == used_sep)
1.8       cvs       129:          {
                    130:             *temp = EOS;
                    131:             *dir = temp + 1;
                    132:          }
                    133:      }
                    134:    else
1.11      cvs       135:      *dir = curr;
                    136: 
1.9       cvs       137:    if (curr <= url)
1.8       cvs       138:       goto finished;
                    139: 
                    140:    /* mark the end of the proto */
1.9       cvs       141:    *curr = EOS;
                    142:    curr--;
                    143:    if (curr < url)
1.8       cvs       144:       goto finished;
                    145: 
1.67      cvs       146:    if (*curr == TEXT(':'))
1.8       cvs       147:      {
1.9       cvs       148:        *curr = EOS;
                    149:        curr--;
1.8       cvs       150:      }
                    151:    else
                    152:       goto finished;
1.11      cvs       153: 
1.9       cvs       154:    if (curr < url)
1.8       cvs       155:       goto finished;
1.9       cvs       156:    while ((curr > url) && (isalpha (*curr)))
                    157:       curr--;
                    158:    *proto = curr;
1.8       cvs       159: 
                    160:  finished:;
                    161: 
                    162: #ifdef AMAYA_DEBUG
                    163:    fprintf (stderr, "ExplodeURL(%s)\n\t", url);
                    164:    if (*proto)
                    165:       fprintf (stderr, "proto : %s, ", *proto);
                    166:    if (*host)
                    167:       fprintf (stderr, "host : %s, ", *host);
                    168:    if (*dir)
                    169:       fprintf (stderr, "dir : %s, ", *dir);
                    170:    if (*file)
                    171:       fprintf (stderr, "file : %s ", *file);
                    172:    fprintf (stderr, "\n");
                    173: #endif
                    174: 
                    175: }
1.3       cvs       176: 
1.61      cvs       177: 
                    178: /*----------------------------------------------------------------------
                    179:    ExtractSuffix extract suffix from document nane.                
                    180:   ----------------------------------------------------------------------*/
                    181: #ifdef __STDC__
                    182: void                ExtractSuffix (STRING aName, STRING aSuffix)
                    183: #else
                    184: void                ExtractSuffix (aName, aSuffix)
                    185: STRING              aName;
                    186: STRING              aSuffix;
                    187: 
                    188: #endif
                    189: {
                    190:    int                 lg, i;
                    191:    STRING              ptr, oldptr;
                    192: 
                    193:    if (!aSuffix || !aName)
                    194:      /* bad suffix */
                    195:      return;
                    196: 
                    197:    aSuffix[0] = EOS;
                    198:    lg = ustrlen (aName);
                    199:    if (lg)
                    200:      {
                    201:        /* the name is not empty */
                    202:        oldptr = ptr = &aName[0];
                    203:        do
                    204:          {
1.67      cvs       205:             ptr = ustrrchr (oldptr, TEXT('.'));
1.61      cvs       206:             if (ptr)
                    207:                oldptr = &ptr[1];
                    208:          }
                    209:        while (ptr);
                    210: 
                    211:        i = (int) (oldptr) - (int) (aName);     /* name length */
                    212:        if (i > 1)
                    213:          {
                    214:             aName[i - 1] = EOS;
                    215:             if (i != lg)
                    216:                ustrcpy (aSuffix, oldptr);
                    217:          }
                    218:      }
                    219: }
                    220: 
1.4       cvs       221: /*----------------------------------------------------------------------
1.9       cvs       222:   IsHTMLName                                                         
                    223:   returns TRUE if path points to an HTML resource.
1.4       cvs       224:   ----------------------------------------------------------------------*/
1.3       cvs       225: #ifdef __STDC__
1.67      cvs       226: ThotBool             IsHTMLName (const STRING path)
1.3       cvs       227: #else  /* __STDC__ */
1.67      cvs       228: ThotBool             IsHTMLName (path)
                    229: const STRING        path;
1.3       cvs       230: #endif /* __STDC__ */
                    231: {
1.67      cvs       232:    CHAR_T              temppath[MAX_LENGTH];
                    233:    CHAR_T              suffix[MAX_LENGTH];
                    234:    CHAR_T              nsuffix[MAX_LENGTH];
1.5       cvs       235:    int                 i;
                    236: 
                    237:    if (!path)
1.37      cvs       238:       return (FALSE);
1.5       cvs       239: 
1.67      cvs       240:    ustrcpy (temppath, path);
1.5       cvs       241:    ExtractSuffix (temppath, suffix);
                    242: 
                    243:    /* Normalize the suffix */
                    244:    i = 0;
1.39      cvs       245:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       246:      {
1.67      cvs       247:        nsuffix[i] = utolower (suffix[i]);
1.13      cvs       248:        i++;
                    249:      }
1.5       cvs       250:    nsuffix[i] = EOS;
1.67      cvs       251:    if (!ustrcmp (nsuffix, TEXT("html")) ||
                    252:        !ustrcmp (nsuffix, TEXT("htm")) ||
                    253:        !ustrcmp (nsuffix, TEXT("shtml")) ||
                    254:        !ustrcmp (nsuffix, TEXT("xht")) ||
                    255:        !ustrcmp (nsuffix, TEXT("xhtm")) ||
                    256:        !ustrcmp (nsuffix, TEXT("xhtml")))
1.39      cvs       257:      return (TRUE);
1.67      cvs       258:    else if (!ustrcmp (nsuffix, TEXT("gz")))
1.13      cvs       259:      {
1.39      cvs       260:        /* take into account compressed files */
1.13      cvs       261:        ExtractSuffix (temppath, suffix);       
                    262:        /* Normalize the suffix */
                    263:        i = 0;
1.39      cvs       264:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       265:         {
1.25      cvs       266:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       267:           i++;
                    268:         }
                    269:        nsuffix[i] = EOS;
1.67      cvs       270:        if (!ustrcmp (nsuffix, TEXT("html")) ||
                    271:           !ustrcmp (nsuffix, TEXT("htm")) ||
                    272:           !ustrcmp (nsuffix, TEXT("shtml")) ||
                    273:           !ustrcmp (nsuffix, TEXT("xht")) ||
                    274:           !ustrcmp (nsuffix, TEXT("xhtm")) ||
                    275:           !ustrcmp (nsuffix, TEXT("xhtml")))
1.63      cvs       276:  
1.39      cvs       277:         return (TRUE);
                    278:        else
1.13      cvs       279:         return (FALSE);
                    280:      }
                    281:    else
1.39      cvs       282:      return (FALSE);
1.3       cvs       283: }
                    284: 
1.4       cvs       285: /*----------------------------------------------------------------------
1.56      cvs       286:   IsXMLName                                                         
                    287:   returns TRUE if path points to an XML resource.
                    288:   ----------------------------------------------------------------------*/
                    289: #ifdef __STDC__
1.67      cvs       290: ThotBool             IsXMLName (const STRING path)
1.56      cvs       291: #else  /* __STDC__ */
1.67      cvs       292: ThotBool             IsXMLName (path)
                    293: const STRING        path;
1.56      cvs       294: #endif /* __STDC__ */
                    295: {
1.67      cvs       296:    CHAR_T              temppath[MAX_LENGTH];
                    297:    CHAR_T              suffix[MAX_LENGTH];
1.56      cvs       298: 
                    299:    if (!path)
                    300:       return (FALSE);
                    301: 
1.67      cvs       302:    ustrcpy (temppath, path);
1.56      cvs       303:    ExtractSuffix (temppath, suffix);
                    304: 
1.67      cvs       305:    if (!ustrcasecmp (suffix, TEXT("xml")) ||
                    306:        !ustrcasecmp (suffix, TEXT("xht")) ||
                    307:        !ustrcmp (suffix, TEXT("xhtm")) ||
                    308:        !ustrcmp (suffix, TEXT("xhtml")))
1.56      cvs       309:      return (TRUE);
1.67      cvs       310:    else if (!ustrcmp (suffix, TEXT("gz")))
1.56      cvs       311:      {
                    312:        /* take into account compressed files */
                    313:        ExtractSuffix (temppath, suffix);       
1.67      cvs       314:        if (!ustrcasecmp (suffix, TEXT("xml")) ||
                    315:           !ustrcasecmp (suffix, TEXT("xht")) ||
                    316:           !ustrcmp (suffix, TEXT("xhtm")) ||
                    317:           !ustrcmp (suffix, TEXT("xhtml")))
1.60      cvs       318:         return (TRUE);
                    319:        else
                    320:         return (FALSE);
                    321:      }
                    322:    else
                    323:      return (FALSE);
                    324: }
                    325: 
                    326: /*----------------------------------------------------------------------
                    327:   IsCSSName                                                         
                    328:   returns TRUE if path points to an XML resource.
                    329:   ----------------------------------------------------------------------*/
                    330: #ifdef __STDC__
1.67      cvs       331: ThotBool             IsCSSName (const STRING path)
1.60      cvs       332: #else  /* __STDC__ */
1.67      cvs       333: ThotBool             IsCSSName (path)
                    334: const STRING        path;
1.60      cvs       335: #endif /* __STDC__ */
                    336: {
1.67      cvs       337:    CHAR_T              temppath[MAX_LENGTH];
                    338:    CHAR_T              suffix[MAX_LENGTH];
1.60      cvs       339: 
                    340:    if (!path)
                    341:       return (FALSE);
                    342: 
1.67      cvs       343:    ustrcpy (temppath, path);
1.60      cvs       344:    ExtractSuffix (temppath, suffix);
                    345: 
1.67      cvs       346:    if (!ustrcasecmp (suffix, TEXT("css")))
1.60      cvs       347:      return (TRUE);
1.67      cvs       348:    else if (!ustrcmp (suffix, TEXT("gz")))
1.60      cvs       349:      {
                    350:        /* take into account compressed files */
                    351:        ExtractSuffix (temppath, suffix);       
1.67      cvs       352:        if (!ustrcasecmp (suffix, TEXT("css")))
1.56      cvs       353:         return (TRUE);
                    354:        else
                    355:         return (FALSE);
                    356:      }
                    357:    else
                    358:      return (FALSE);
                    359: }
                    360: 
                    361: /*----------------------------------------------------------------------
1.9       cvs       362:   IsImageName                                
                    363:   returns TRUE if path points to an image resource.
1.4       cvs       364:   ----------------------------------------------------------------------*/
1.3       cvs       365: #ifdef __STDC__
1.67      cvs       366: ThotBool             IsImageName (const STRING path)
1.3       cvs       367: #else  /* __STDC__ */
1.67      cvs       368: ThotBool             IsImageName (path)
                    369: const STRING        path;
1.3       cvs       370: #endif /* __STDC__ */
                    371: {
1.67      cvs       372:    CHAR_T                temppath[MAX_LENGTH];
                    373:    CHAR_T                suffix[MAX_LENGTH];
                    374:    CHAR_T                nsuffix[MAX_LENGTH];
1.5       cvs       375:    int                 i;
                    376: 
                    377:    if (!path)
1.13      cvs       378:       return (FALSE);
1.5       cvs       379: 
1.67      cvs       380:    ustrcpy (temppath, path);
1.5       cvs       381:    ExtractSuffix (temppath, suffix);
                    382: 
                    383:    /* Normalize the suffix */
                    384:    i = 0;
1.39      cvs       385:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       386:      {
1.67      cvs       387:        nsuffix[i] = utolower (suffix[i]);
1.13      cvs       388:        i++;
                    389:      }
1.5       cvs       390:    nsuffix[i] = EOS;
1.67      cvs       391:    if ((!ustrcmp (nsuffix, TEXT("gif"))) || (!ustrcmp (nsuffix, TEXT("xbm"))) ||
                    392:        (!ustrcmp (nsuffix, TEXT("xpm"))) || (!ustrcmp (nsuffix, TEXT("jpg"))) ||
                    393:        (!ustrcmp (nsuffix, TEXT("png"))) || (!ustrcmp (nsuffix, TEXT("au"))))
1.39      cvs       394:       return (TRUE);
                    395:    return (FALSE);
1.3       cvs       396: }
                    397: 
1.4       cvs       398: /*----------------------------------------------------------------------
1.58      cvs       399:   IsImageType                                
                    400:   returns TRUE if type points to an image resource.
                    401:   ----------------------------------------------------------------------*/
                    402: #ifdef __STDC__
1.67      cvs       403: ThotBool             IsImageType (const STRING type)
1.58      cvs       404: #else  /* __STDC__ */
1.67      cvs       405: ThotBool             IsImageType (type)
                    406: const STRING     type;
1.58      cvs       407: #endif /* __STDC__ */
                    408: {
1.67      cvs       409:    CHAR_T              temptype[MAX_LENGTH];
1.58      cvs       410:    int                 i;
                    411: 
                    412:    if (!type)
                    413:       return (FALSE);
                    414: 
1.67      cvs       415:    ustrcpy (temptype, type);
1.58      cvs       416:    /* Normalize the type */
                    417:    i = 0;
                    418:    while (temptype[i] != EOS)
                    419:      {
                    420:        temptype[i] = tolower (temptype[i]);
                    421:        i++;
                    422:      }
1.67      cvs       423:    if ((!ustrcmp (temptype, TEXT("gif"))) || (!ustrcmp (temptype, TEXT("x-xbitmap"))) ||
                    424:        (!ustrcmp (temptype, TEXT("x-xpixmap"))) || (!ustrcmp (temptype, TEXT("jpeg"))) ||
                    425:        (!ustrcmp (temptype, TEXT("png"))))
1.58      cvs       426:       return (TRUE);
                    427:    return (FALSE);
                    428: }
                    429: 
                    430: /*----------------------------------------------------------------------
1.9       cvs       431:   IsTextName                                                         
1.4       cvs       432:   ----------------------------------------------------------------------*/
1.3       cvs       433: #ifdef __STDC__
1.67      cvs       434: ThotBool             IsTextName (const STRING path)
1.3       cvs       435: #else  /* __STDC__ */
1.67      cvs       436: ThotBool             IsTextName (path)
                    437: const STRING        path;
1.3       cvs       438: 
                    439: #endif /* __STDC__ */
                    440: {
1.67      cvs       441:    CHAR_T                temppath[MAX_LENGTH];
                    442:    CHAR_T                suffix[MAX_LENGTH];
                    443:    CHAR_T                nsuffix[MAX_LENGTH];
1.5       cvs       444:    int                 i;
                    445: 
                    446:    if (!path)
1.13      cvs       447:      return (FALSE);
1.5       cvs       448: 
1.67      cvs       449:    ustrcpy (temppath, path);
1.5       cvs       450:    ExtractSuffix (temppath, suffix);
                    451: 
                    452:    /* Normalize the suffix */
                    453:    i = 0;
1.39      cvs       454:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5       cvs       455:      {
1.25      cvs       456:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       457:        i++;
                    458:      }
                    459:    nsuffix[i] = EOS;
                    460: 
1.67      cvs       461:    if ((!ustrcmp (nsuffix, TEXT("txt"))) || (!ustrcmp (nsuffix, TEXT("dtd"))))
1.13      cvs       462:       return (TRUE);
1.67      cvs       463:    else if (!ustrcmp (nsuffix, TEXT("gz")))
1.13      cvs       464:      {
1.39      cvs       465:        /* take into account compressed files */
1.13      cvs       466:        ExtractSuffix (temppath, suffix);       
                    467:        /* Normalize the suffix */
                    468:        i = 0;
1.39      cvs       469:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       470:         {
1.25      cvs       471:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       472:           i++;
                    473:         }
                    474:        nsuffix[i] = EOS;
1.67      cvs       475:        if ((!ustrcmp (nsuffix, TEXT("txt"))) || (!ustrcmp (nsuffix, TEXT("dtd"))))
1.13      cvs       476:         return (TRUE);
                    477:        else
                    478:         return (FALSE);
                    479:      }
                    480:    else
                    481:      return (FALSE);
1.3       cvs       482: }
                    483: 
1.4       cvs       484: /*----------------------------------------------------------------------
1.9       cvs       485:   IsHTTPPath                                     
                    486:   returns TRUE if path is in fact an http URL.
1.4       cvs       487:   ----------------------------------------------------------------------*/
1.3       cvs       488: #ifdef __STDC__
1.67      cvs       489: ThotBool             IsHTTPPath (const STRING path)
1.3       cvs       490: #else  /* __STDC__ */
1.67      cvs       491: ThotBool             IsHTTPPath (path)
                    492: const STRING       path;
1.3       cvs       493: #endif /* __STDC__ */
                    494: {
1.5       cvs       495:    if (!path)
                    496:       return FALSE;
1.3       cvs       497: 
1.67      cvs       498:    if ((!ustrncmp (path, TEXT("http:"), 5) != 0)
                    499:        || !ustrncmp (path, TEXT("internal:"), 9))
1.58      cvs       500:       return TRUE;
                    501:    return FALSE;
1.3       cvs       502: }
                    503: 
1.4       cvs       504: /*----------------------------------------------------------------------
1.9       cvs       505:   IsWithParameters                           
                    506:   returns TRUE if url has a concatenated query string.
1.4       cvs       507:   ----------------------------------------------------------------------*/
1.3       cvs       508: #ifdef __STDC__
1.66      cvs       509: ThotBool            IsWithParameters (const char *url)
1.3       cvs       510: #else  /* __STDC__ */
1.66      cvs       511: ThotBool            IsWithParameters (url)
1.34      cvs       512: const char         *url;
1.3       cvs       513: #endif /* __STDC__ */
                    514: {
1.5       cvs       515:    int                 i;
1.3       cvs       516: 
1.9       cvs       517:    if ((!url) || (url[0] == EOS))
1.5       cvs       518:       return FALSE;
1.3       cvs       519: 
1.9       cvs       520:    i = strlen (url) - 1;
                    521:    while (i > 0 && url[i--] != '?')
1.5       cvs       522:       if (i < 0)
                    523:         return FALSE;
1.3       cvs       524: 
1.5       cvs       525:    /* There is a parameter */
                    526:    return TRUE;
1.3       cvs       527: }
                    528: 
1.4       cvs       529: /*----------------------------------------------------------------------
1.9       cvs       530:   IsW3Path                                           
                    531:   returns TRUE if path is in fact a URL.
1.4       cvs       532:   ----------------------------------------------------------------------*/
1.3       cvs       533: #ifdef __STDC__
1.67      cvs       534: ThotBool             IsW3Path (const STRING path)
1.3       cvs       535: #else  /* __STDC__ */
1.67      cvs       536: ThotBool             IsW3Path (path)
                    537: const STRING        path;
1.3       cvs       538: #endif /* __STDC__ */
                    539: {
1.72    ! cvs       540:   if (ustrncmp (path, TEXT("http:"), 5) && ustrncmp (path, TEXT("ftp:"), 4) &&
        !           541:       ustrncmp (path, TEXT("telnet:"), 7) && ustrncmp (path, TEXT("wais:"), 5) &&
        !           542:       ustrncmp (path, TEXT("news:"), 5) && ustrncmp (path, TEXT("gopher:"), 7) &&
        !           543:       ustrncmp (path, TEXT("mailto:"), 7) && ustrncmp (path, TEXT("archie:"), 7))
        !           544:     return FALSE;
        !           545:   return TRUE;
1.3       cvs       546: }
                    547: 
1.4       cvs       548: /*----------------------------------------------------------------------
1.9       cvs       549:   IsValidProtocol                                                    
                    550:   returns true if the url protocol is supported by Amaya.
1.4       cvs       551:   ----------------------------------------------------------------------*/
1.3       cvs       552: #ifdef __STDC__
1.67      cvs       553: ThotBool             IsValidProtocol (const STRING url)
1.3       cvs       554: #else  /* __STDC__ */
1.67      cvs       555: ThotBool             IsValidProtocol (url)
                    556: const STRING       url;
1.3       cvs       557: #endif /* __STDC__ */
                    558: {
1.67      cvs       559:    if (!ustrncmp (url, TEXT("http:"), 5)
1.69      cvs       560:       || !ustrncmp (url, TEXT("internal:"), 9)
1.70      cvs       561:       || !ustrncmp (url, TEXT("ftp:"), 4))
1.22      cvs       562:        /* experimental */
1.58      cvs       563:       /***  || !strncmp (url, "ftp:", 4) ***/
1.24      cvs       564:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs       565:       return (TRUE);
1.5       cvs       566:    else
1.8       cvs       567:       return (FALSE);
1.3       cvs       568: }
                    569: 
1.31      cvs       570: 
                    571: /*----------------------------------------------------------------------
                    572:    GetBaseURL
                    573:    normalizes orgName according to a base associated with doc, and
                    574:    following the standard URL format rules.
                    575:    The function returns the base used to solve relative URL and SRC:
                    576:       - the base of the document,
                    577:       - or the document path (without document name).
                    578:   ----------------------------------------------------------------------*/
                    579: #ifdef __STDC__
1.67      cvs       580: STRING              GetBaseURL (Document doc)
1.31      cvs       581: #else  /* __STDC__ */
1.67      cvs       582: STRING              GetBaseURL (doc)
1.31      cvs       583: Document            doc;
                    584: #endif /* __STDC__ */
                    585: {
                    586:   Element             el;
                    587:   ElementType         elType;
                    588:   AttributeType       attrType;
                    589:   Attribute           attr;
1.67      cvs       590:   STRING              ptr, basename;
1.31      cvs       591:   int                 length;
                    592: 
1.57      cvs       593:   /* @@@ irene */
                    594:   if (!DocumentURLs[doc])
                    595:          return NULL;
1.67      cvs       596:   basename = TtaAllocString (MAX_LENGTH);
                    597:   ustrncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
1.39      cvs       598:   basename[MAX_LENGTH-1] = EOS;
1.31      cvs       599:   length = MAX_LENGTH -1;
                    600:   /* get the root element    */
                    601:   el = TtaGetMainRoot (doc);
                    602:   /* search the BASE element */
                    603:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
1.65      cvs       604:   elType.ElTypeNum = HTML_EL_HEAD;
                    605:   el = TtaSearchTypedElement (elType, SearchForward, el);
                    606:   if (el)
                    607:     {
                    608:       elType.ElTypeNum = HTML_EL_BASE;
                    609:       el = TtaSearchTypedElement (elType, SearchInTree, el);
                    610:     }
1.31      cvs       611:   if (el)
                    612:     {
                    613:       /*  The document has a BASE element -> Get the HREF attribute */
                    614:       attrType.AttrSSchema = elType.ElSSchema;
                    615:       attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    616:       attr = TtaGetAttribute (el, attrType);
                    617:       if (attr)
                    618:        {
                    619:          /* Use the base path of the document */
                    620:          TtaGiveTextAttributeValue (attr, basename, &length);
                    621:          /* base and orgName have to be separated by a DIR_SEP */
                    622:          length--;
1.43      cvs       623:          if (basename[0] != EOS && basename[length] != URL_SEP && basename[length] != DIR_SEP) 
1.31      cvs       624:            /* verify if the base has the form "protocol://server:port" */
                    625:            {
1.67      cvs       626:              ptr = AmayaParseUrl (basename, _EMPTYSTR_, AMAYA_PARSE_ACCESS |
1.33      cvs       627:                                                 AMAYA_PARSE_HOST |
                    628:                                                 AMAYA_PARSE_PUNCTUATION);
1.67      cvs       629:              if (ptr && !ustrcmp (ptr, basename))
1.31      cvs       630:                {
1.43      cvs       631:                  /* it has this form, we complete it by adding a URL_STR  */
1.67      cvs       632:                  if (ustrchr (basename, DIR_SEP))
                    633:                    ustrcat (basename, DIR_STR);
1.43      cvs       634:                  else
1.67      cvs       635:                    ustrcat (basename, URL_STR);
1.31      cvs       636:                  length++;
                    637:                }
                    638:              if (ptr)
                    639:                TtaFreeMemory (ptr);
                    640:            }
                    641:        }
1.33      cvs       642:       }
                    643:   
1.31      cvs       644:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                    645:    * then search for the first ":" char, hoping that what's before that is a
                    646:    * protocol. If found, end the string there. If neither char is found,
                    647:    * then discard the whole base element.
                    648:    */
1.67      cvs       649:   length = ustrlen (basename) - 1;
1.31      cvs       650:   /* search for the last DIR_SEP char */
1.43      cvs       651:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs       652:     length--;
                    653:   if (length >= 0)
                    654:     /* found the last DIR_SEP char, end the string there */
                    655:     basename[length + 1] = EOS;                   
                    656:   else
                    657:     /* search for the first PATH_STR char */
                    658:     {
1.67      cvs       659:       for (length = 0; basename[length] != TEXT(':') && 
1.31      cvs       660:             basename[length] != EOS; length ++);
1.67      cvs       661:       if (basename[length] == TEXT(':'))
1.31      cvs       662:        /* found, so end the string there */
                    663:        basename[length + 1] = EOS;
                    664:       else
                    665:        /* not found, discard the base */
                    666:        basename[0] = EOS;
                    667:     }
                    668:   return (basename);
                    669: }
                    670: 
                    671: 
1.4       cvs       672: /*----------------------------------------------------------------------
1.40      cvs       673:    GetLocalPath
                    674:    Allocate and return the local document path associated to the url
                    675:   ----------------------------------------------------------------------*/
                    676: #ifdef __STDC__
1.67      cvs       677: STRING     GetLocalPath (Document doc, STRING url)
1.40      cvs       678: #else  /* __STDC__ */
1.67      cvs       679: STRING     GetLocalPath (doc, url)
1.40      cvs       680: Document   doc;
1.67      cvs       681: STRING     url;
1.40      cvs       682: #endif /* __STDC__ */
                    683: {
1.67      cvs       684:   STRING   ptr, n;
                    685:   STRING   documentname;
                    686:   CHAR_T   url_sep;
1.40      cvs       687:   int      len;
1.67      cvs       688:   STRING   tmpDir, tmpName;
                    689:   ThotBool  noFile;
1.40      cvs       690: 
                    691:   if (url != NULL)
                    692:     {
                    693:       /* check whether the file name exists */
1.67      cvs       694:       len = ustrlen (url) - 1;
1.71      cvs       695:       if (IsW3Path (url))
1.67      cvs       696:        url_sep = TEXT('/');
1.41      cvs       697:       else 
                    698:        url_sep = DIR_SEP;
                    699:       noFile = (url[len] == url_sep);
1.40      cvs       700:       if (noFile)
                    701:        url[len] = EOS;
1.67      cvs       702:       /* ptr = TtaAllocString (MAX_LENGTH);
                    703:       documentname = TtaAllocString (MAX_LENGTH); */
                    704:          tmpDir = TtaAllocString (MAX_LENGTH );
                    705:          tmpName = TtaAllocString(MAX_LENGTH );
                    706:       TtaExtractName (url, tmpDir, tmpName);
                    707:          ptr = tmpDir;
                    708:       documentname = tmpName;
                    709: 
                    710:       usprintf (ptr, TEXT("%s%s%d%s"), TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40      cvs       711:       if (!TtaCheckDirectory (ptr))
                    712:        /* directory did not exist */
1.72    ! cvs       713:        TtaMakeDirectory (ptr);
1.47      cvs       714: 
                    715:       /* don't include the query string within document name */
1.67      cvs       716:       n = ustrrchr(documentname, TEXT('?'));
1.47      cvs       717:       if (n != NULL)
                    718:        *n = EOS;
1.46      cvs       719:       /* don't include ':' within document name */
1.67      cvs       720:       n = ustrchr (documentname, TEXT(':'));
1.46      cvs       721:       if (n != NULL)
                    722:        *n = EOS;
1.69      cvs       723:       /* if after all this operations document name
                    724:         is empty, let's use noname.html instead */
                    725:       if (documentname[0] == EOS)
                    726:        ustrcat (ptr, TEXT("noname.html"));
                    727:       else
                    728:        ustrcat (ptr, documentname);
1.40      cvs       729:       TtaFreeMemory (documentname);
                    730:       /* restore the url */
                    731:       if (noFile)
1.41      cvs       732:        url[len] = url_sep;
1.40      cvs       733:       return (ptr);
                    734:     }
                    735:   else
                    736:     return (NULL);
                    737: }
                    738: 
                    739: 
                    740: /*----------------------------------------------------------------------
1.9       cvs       741:    NormalizeURL
                    742:    normalizes orgName according to a base associated with doc, and
                    743:    following the standard URL format rules.
1.53      cvs       744:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    745:    other path.
1.9       cvs       746:    The function returns the new complete and normalized URL 
1.12      cvs       747:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       748:    N.B. If the function can't find out what's the docName, it assigns
                    749:    the name "noname.html".
1.4       cvs       750:   ----------------------------------------------------------------------*/
1.3       cvs       751: #ifdef __STDC__
1.67      cvs       752: void                NormalizeURL (STRING orgName, Document doc, STRING newName, STRING docName, STRING otherPath)
1.3       cvs       753: #else  /* __STDC__ */
1.53      cvs       754: void                NormalizeURL (orgName, doc, newName, docName, otherPath)
1.67      cvs       755: STRING              orgName;
1.3       cvs       756: Document            doc;
1.67      cvs       757: STRING              newName;
                    758: STRING              docName;
                    759: STRING              otherPath;
1.3       cvs       760: #endif /* __STDC__ */
                    761: {
1.67      cvs       762:    STRING             basename;
                    763:    CHAR_T             tempOrgName[MAX_LENGTH];
                    764:    STRING             ptr;
                    765:    CHAR_T             used_sep;
                    766:    int                length;
1.66      cvs       767:    ThotBool            check;
1.5       cvs       768: 
1.44      cvs       769: #  ifdef _WINDOWS
                    770:    int ndx;
                    771: #  endif /* _WINDOWS */
                    772: 
1.5       cvs       773:    if (!newName || !docName)
                    774:       return;
1.18      cvs       775: 
1.32      cvs       776:    if (doc != 0)
1.53      cvs       777:      basename = GetBaseURL (doc);
                    778:    else if (otherPath != NULL)
                    779:      basename = TtaStrdup (otherPath);
1.32      cvs       780:    else
1.53      cvs       781:      basename = NULL;
1.32      cvs       782: 
1.18      cvs       783:    /*
1.31      cvs       784:     * Clean orgName
                    785:     * Make sure we have a complete orgName, without any leading or trailing
                    786:     * white spaces, or trailinbg new lines
                    787:     */
1.5       cvs       788:    ptr = orgName;
1.18      cvs       789:    /* skip leading white space and new line characters */
1.67      cvs       790:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                    791:    ustrncpy (tempOrgName, ptr, MAX_LENGTH -1);
1.39      cvs       792:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs       793:    /*
1.31      cvs       794:     * Make orgName a complete URL
                    795:     * If the URL does not include a protocol, then try to calculate
                    796:     * one using the doc's base element (if it exists),
                    797:     */
1.53      cvs       798:    if (tempOrgName[0] == EOS)
                    799:      {
                    800:        newName[0] = EOS;
                    801:        TtaFreeMemory (basename);
                    802:        return;
                    803:      }
1.49      cvs       804: 
                    805:    /* clean trailing white space */
1.67      cvs       806:    length = ustrlen (tempOrgName) - 1;
1.53      cvs       807:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
                    808:      {
                    809:        tempOrgName[length] = EOS;
                    810:        length--;
                    811:      }
1.50      cvs       812: 
1.55      cvs       813:    /* remove extra dot (which dot???) */
                    814:    /* ugly, but faster than a strcmp */
1.67      cvs       815:    if (tempOrgName[length] == TEXT('.')
                    816:        && (length == 0 || tempOrgName[length-1] != TEXT('.')))
1.55      cvs       817:         tempOrgName[length] = EOS;
1.50      cvs       818: 
1.53      cvs       819:    if (IsW3Path (tempOrgName))
                    820:      {
                    821:        /* the name is complete, go to the Sixth Step */
1.67      cvs       822:        ustrcpy (newName, tempOrgName);
1.53      cvs       823:        SimplifyUrl (&newName);
                    824:        /* verify if the URL has the form "protocol://server:port" */
1.67      cvs       825:        ptr = AmayaParseUrl (newName, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    826:        if (ptr && !ustrcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
                    827:          ustrcat (newName, URL_STR);
1.49      cvs       828: 
1.53      cvs       829:        if (ptr)
1.50      cvs       830:          TtaFreeMemory (ptr);
1.53      cvs       831:      }
                    832:    else if ( basename == NULL)
                    833:      /* the name is complete, go to the Sixth Step */
1.67      cvs       834:      ustrcpy (newName, tempOrgName);
1.53      cvs       835:    else
                    836:      {
1.31      cvs       837:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs       838: #      ifdef _WINDOWS
1.53      cvs       839:        if (!IsW3Path (basename))
                    840:         {
1.67      cvs       841:           length = ustrlen (tempOrgName);
1.53      cvs       842:           for (ndx = 0; ndx < length; ndx++)
1.67      cvs       843:             if (tempOrgName [ndx] == TEXT('/'))
                    844:               tempOrgName [ndx] = TEXT('\\');
1.53      cvs       845:         }
1.44      cvs       846: #      endif /* _WINDOWS */
1.25      cvs       847:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs       848:        if (ptr)
                    849:         {
                    850:           SimplifyUrl (&ptr);
1.67      cvs       851:           ustrcpy (newName, ptr);
1.53      cvs       852:           TtaFreeMemory (ptr);
                    853:         }
                    854:        else
                    855:         newName[0] = EOS;
                    856:      }
1.36      cvs       857: 
                    858:    TtaFreeMemory (basename);
1.18      cvs       859:    /*
1.31      cvs       860:     * Prepare the docname that will refer to this ressource in the
                    861:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                    862:     * noname.html as a default ressource name
1.18      cvs       863:    */
1.53      cvs       864:    if (newName[0] != EOS)
                    865:      {
1.67      cvs       866:        length = ustrlen (newName) - 1;
1.53      cvs       867:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
                    868:         {
                    869:           used_sep = newName[length];
                    870:           check = TRUE;
                    871:           while (check)
                    872:             {
1.50      cvs       873:                length--;
                    874:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs       875:                 length--;
1.67      cvs       876:                if (!ustrncmp (&newName[length+1], TEXT(".."), 2))
1.53      cvs       877:                 {
                    878:                   newName[length+1] = EOS;
                    879:                   /* remove also previous directory */
                    880:                   length--;
                    881:                   while (length >= 0 && newName[length] != used_sep)
                    882:                     length--;
1.67      cvs       883:                   if (ustrncmp (&newName[length+1], TEXT("//"), 2))
1.53      cvs       884:                     /* don't remove server name */
1.50      cvs       885:                      newName[length+1] = EOS;
1.53      cvs       886:                 }
1.67      cvs       887:               else if (!ustrncmp (&newName[length+1], TEXT("."), 1))
1.53      cvs       888:                 newName[length+1] = EOS;
1.50      cvs       889:                else
1.53      cvs       890:                 check = FALSE;
                    891:             }
1.67      cvs       892:           ustrcpy (docName, TEXT("noname.html"));             
1.53      cvs       893:           /* docname was not comprised inside the URL, so let's */
                    894:           /* assign the default ressource name */
1.67      cvs       895:           ustrcpy (docName, TEXT("noname.html"));
1.53      cvs       896:         }
                    897:        else
                    898:         { /* docname is comprised inside the URL */
1.50      cvs       899:            while (length >= 0 && newName[length] != URL_SEP && newName[length] != DIR_SEP)
1.53      cvs       900:             length--;
                    901:           if (length < 0)
1.67      cvs       902:              ustrcpy (docName, newName);
1.53      cvs       903:           else
1.67      cvs       904:             ustrcpy (docName, &newName[length+1]);
1.53      cvs       905:         }
                    906:      }
                    907:    else
                    908:      docName[0] = EOS;
1.18      cvs       909: } 
1.3       cvs       910: 
1.4       cvs       911: /*----------------------------------------------------------------------
1.9       cvs       912:   IsSameHost                                                         
1.4       cvs       913:   ----------------------------------------------------------------------*/
1.3       cvs       914: #ifdef __STDC__
1.67      cvs       915: ThotBool             IsSameHost (const STRING url1, const STRING url2)
1.3       cvs       916: #else  /* __STDC__ */
1.67      cvs       917: ThotBool             IsSameHost (url1, url2)
                    918: const STRING       url1;
                    919: const STRING       url2;
1.3       cvs       920: #endif /* __STDC__ */
                    921: {
1.67      cvs       922:    STRING           basename_ptr1, basename_ptr2;
                    923:    ThotBool          result;
1.3       cvs       924: 
1.67      cvs       925:    basename_ptr1 = AmayaParseUrl (url1, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    926:    basename_ptr2 = AmayaParseUrl (url2, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs       927: 
1.67      cvs       928:    if (ustrcmp (basename_ptr1, basename_ptr2))
1.8       cvs       929:       result = FALSE;
1.5       cvs       930:    else
1.8       cvs       931:       result = TRUE;
1.3       cvs       932: 
1.25      cvs       933:    TtaFreeMemory (basename_ptr1);
                    934:    TtaFreeMemory (basename_ptr2);
1.5       cvs       935:    return (result);
1.3       cvs       936: }
                    937: 
                    938: 
1.4       cvs       939: /*----------------------------------------------------------------------
1.22      cvs       940:   HasKnownFileSuffix
                    941:   returns TRUE if path points to a file ending with a suffix.
                    942:   ----------------------------------------------------------------------*/
                    943: #ifdef __STDC__
1.67      cvs       944: ThotBool             HasKnownFileSuffix (const STRING path)
1.22      cvs       945: #else  /* __STDC__ */
1.67      cvs       946: ThotBool             HasKnownFileSuffix (path)
                    947: const STRING    path;
1.22      cvs       948: #endif /* __STDC__ */
                    949: {
1.67      cvs       950:    STRING      root;
                    951:    CHAR_T      temppath[MAX_LENGTH];
                    952:    CHAR_T      suffix[MAX_LENGTH];
1.22      cvs       953: 
1.67      cvs       954:    if (!path || path[0] == EOS || path[ustrlen(path)] == DIR_SEP)
1.22      cvs       955:      return (FALSE);
                    956: 
1.67      cvs       957:    root = AmayaParseUrl(path, _EMPTYSTR_, AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs       958: 
                    959:    if (root) 
                    960:      {
1.67      cvs       961:        ustrcpy (temppath, root);
1.25      cvs       962:        TtaFreeMemory (root);
1.22      cvs       963:        /* Get the suffix */
                    964:        ExtractSuffix (temppath, suffix); 
                    965: 
                    966:        if( suffix[0] == EOS)
                    967:         /* no suffix */
                    968:         return (FALSE);
                    969: 
                    970:        /* Normalize the suffix */
                    971:        ConvertToLowerCase (suffix);
                    972: 
1.67      cvs       973:        if (!ustrcmp (suffix, TEXT("gz")))
1.22      cvs       974:         /* skip the compressed suffix */
                    975:         {
                    976:         ExtractSuffix (temppath, suffix);
                    977:         if(suffix[0] == EOS)
                    978:           /* no suffix */
                    979:           return (FALSE);
                    980:          /* Normalize the suffix */
                    981:          ConvertToLowerCase (suffix);
                    982:         }
                    983: 
1.67      cvs       984:        if (ustrcmp (suffix, TEXT("gif")) &&
                    985:           ustrcmp (suffix, TEXT("xbm")) &&
                    986:           ustrcmp (suffix, TEXT("xpm")) &&
                    987:           ustrcmp (suffix, TEXT("jpg")) &&
                    988:           ustrcmp (suffix, TEXT("pdf")) &&
                    989:           ustrcmp (suffix, TEXT("png")) &&
                    990:           ustrcmp (suffix, TEXT("tgz")) &&
                    991:           ustrcmp (suffix, TEXT("xpg")) &&
                    992:           ustrcmp (suffix, TEXT("xpd")) &&
                    993:           ustrcmp (suffix, TEXT("ps")) &&
                    994:           ustrcmp (suffix, TEXT("au")) &&
                    995:           ustrcmp (suffix, TEXT("html")) &&
                    996:           ustrcmp (suffix, TEXT("htm")) &&
                    997:           ustrcmp (suffix, TEXT("shtml")) &&
                    998:           ustrcmp (suffix, TEXT("xht")) &&
                    999:           ustrcmp (suffix, TEXT("xhtm")) &&
                   1000:           ustrcmp (suffix, TEXT("xhtml")) &&
                   1001:           ustrcmp (suffix, TEXT("txt")) &&
                   1002:           ustrcmp (suffix, TEXT("css")) &&
                   1003:           ustrcmp (suffix, TEXT("eps")))
1.22      cvs      1004:         return (FALSE);
                   1005:        else
                   1006:         return (TRUE);
                   1007:      }
                   1008:    else
                   1009:      return (FALSE);
                   1010: }
                   1011: 
                   1012: 
                   1013: /*----------------------------------------------------------------------
1.24      cvs      1014:   ChopURL
                   1015:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1016:   If inputURL is  bigger than that size, outputURL receives
                   1017:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1018:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1019:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1020:   copied into outputURL. 
                   1021:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1022:   chars.
                   1023:   ----------------------------------------------------------------------*/
                   1024: #ifdef __STDC__
1.34      cvs      1025: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1026: #else
                   1027: void ChopURL (outputURL, inputURL)
1.34      cvs      1028: char       *outputURL;
                   1029: const char *inputURL;
1.24      cvs      1030: #endif
1.22      cvs      1031: 
1.24      cvs      1032: {
                   1033:   int len;
1.9       cvs      1034: 
1.24      cvs      1035:   len = strlen (inputURL);
                   1036:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs      1037:     strcpy (outputURL, inputURL);
1.24      cvs      1038:   else
                   1039:     /* make a truncated urlName on the status window */
                   1040:     {
                   1041:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1042:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1043:       strcat (outputURL, "...");
                   1044:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                   1045:     }
1.25      cvs      1046: }
                   1047: 
                   1048: 
                   1049: /*----------------------------------------------------------------------
                   1050:    scan
1.47      cvs      1051:        Scan a filename for its constituents
1.25      cvs      1052:        -----------------------------------
                   1053:   
                   1054:    On entry,
                   1055:        name    points to a document name which may be incomplete.
                   1056:    On exit,
                   1057:         absolute or relative may be nonzero (but not both).
                   1058:        host, fragment and access may be nonzero if they were specified.
                   1059:        Any which are nonzero point to zero terminated strings.
                   1060:   ----------------------------------------------------------------------*/
                   1061: #ifdef __STDC__
1.67      cvs      1062: static void scan (STRING name, HTURI * parts)
1.25      cvs      1063: #else  /* __STDC__ */
                   1064: static void scan (name, parts)
1.67      cvs      1065: STRING      name;
                   1066: HTURI       *parts;
1.25      cvs      1067: 
                   1068: #endif /* __STDC__ */
                   1069: {
1.67      cvs      1070:   STRING    p;
                   1071:   STRING    after_access = name;
1.32      cvs      1072: 
1.43      cvs      1073:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1074:   /* Look for fragment identifier */
1.67      cvs      1075:   if ((p = ustrchr(name, TEXT('#'))) != NULL)
1.28      cvs      1076:     {
1.67      cvs      1077:       *p++ = TEXT('\0');
1.28      cvs      1078:       parts->fragment = p;
1.25      cvs      1079:     }
                   1080:     
1.28      cvs      1081:   for (p=name; *p; p++)
                   1082:     {
1.67      cvs      1083:       if (*p == URL_SEP || *p == DIR_SEP || *p == TEXT('#') || *p == TEXT('?'))
1.28      cvs      1084:        break;
1.67      cvs      1085:       if (*p == TEXT(':'))
1.28      cvs      1086:        {
                   1087:          *p = 0;
                   1088:          parts->access = after_access; /* Scheme has been specified */
                   1089: 
                   1090:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1091:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1092:             not recommended. Rather, turn off "-O". */
                   1093: 
                   1094:          /*            after_access = p;*/
                   1095:          /*            while (*after_access == 0)*/
                   1096:          /*                after_access++;*/
                   1097:          after_access = p+1;
1.67      cvs      1098:          if (!ustrcasecmp(TEXT("URL"), parts->access))
1.28      cvs      1099:            /* Ignore IETF's URL: pre-prefix */
                   1100:            parts->access = NULL;
                   1101:          else
1.25      cvs      1102:            break;
                   1103:        }
                   1104:     }
                   1105:     
                   1106:     p = after_access;
1.43      cvs      1107:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1108:       {
1.43      cvs      1109:        if (p[1] == URL_SEP)
1.28      cvs      1110:          {
1.25      cvs      1111:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1112:            *p = 0;                     /* Terminate access             */
                   1113:            /* look for end of host name if any */
1.67      cvs      1114:            p = ustrchr (parts->host, URL_SEP);
1.28      cvs      1115:            if (p)
                   1116:              {
1.43      cvs      1117:                *p = EOS;                       /* Terminate host */
1.25      cvs      1118:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1119:              }
                   1120:          }
                   1121:        else
                   1122:          /* Root found but no host */
                   1123:          parts->absolute = p+1;
                   1124:       }
                   1125:     else
                   1126:       {
1.25      cvs      1127:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1128:       }
1.25      cvs      1129: }
                   1130: 
                   1131: 
                   1132: /*----------------------------------------------------------------------
1.28      cvs      1133:   AmayaParseUrl: parse a Name relative to another name
                   1134: 
                   1135:   This returns those parts of a name which are given (and requested)
                   1136:   substituting bits from the related name where necessary.
1.25      cvs      1137:   
1.28      cvs      1138:   On entry,
1.25      cvs      1139:        aName           A filename given
                   1140:         relatedName     A name relative to which aName is to be parsed. Give
                   1141:                         it an empty string if aName is absolute.
                   1142:         wanted          A mask for the bits which are wanted.
                   1143:   
1.28      cvs      1144:   On exit,
1.25      cvs      1145:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1146:   ----------------------------------------------------------------------*/
                   1147: #ifdef __STDC__
1.67      cvs      1148: STRING        AmayaParseUrl (const STRING aName, STRING relatedName, int wanted)
1.25      cvs      1149: #else  /* __STDC__ */
1.67      cvs      1150: STRING        AmayaParseUrl (aName, relatedName, wanted)
                   1151: const STRING  aName;
                   1152: STRING        relatedName;
1.28      cvs      1153: int            wanted;
1.25      cvs      1154: 
                   1155: #endif /* __STDC__ */
                   1156: {
1.67      cvs      1157:   STRING     return_value;
                   1158:   CHAR_T     result[MAX_LENGTH];
                   1159:   CHAR_T     name[MAX_LENGTH];
                   1160:   CHAR_T     rel[MAX_LENGTH];
                   1161:   STRING     p, access;
1.29      cvs      1162:   HTURI      given, related;
                   1163:   int        len;
1.67      cvs      1164:   CHAR_T     used_sep;
                   1165:   STRING     used_str;
1.32      cvs      1166: 
1.67      cvs      1167:   if (ustrchr (aName, DIR_SEP) || ustrchr (relatedName, DIR_SEP))
1.33      cvs      1168:     {
1.42      cvs      1169:       used_str = DIR_STR;
                   1170:       used_sep = DIR_SEP;
1.33      cvs      1171:     }
1.32      cvs      1172:   else
1.33      cvs      1173:     {
1.42      cvs      1174:       used_str = URL_STR;
                   1175:       used_sep = URL_SEP;
1.33      cvs      1176:     }
1.32      cvs      1177: 
1.29      cvs      1178:   /* Make working copies of input strings to cut up: */
                   1179:   return_value = NULL;
                   1180:   result[0] = 0;               /* Clear string  */
1.67      cvs      1181:   ustrcpy (name, aName);
1.29      cvs      1182:   if (relatedName != NULL)  
1.67      cvs      1183:     ustrcpy (rel, relatedName);
1.29      cvs      1184:   else
                   1185:     relatedName[0] = EOS;
                   1186:   
                   1187:   scan (name, &given);
                   1188:   scan (rel,  &related); 
                   1189:   access = given.access ? given.access : related.access;
                   1190:   if (wanted & AMAYA_PARSE_ACCESS)
                   1191:     if (access)
                   1192:       {
1.67      cvs      1193:        ustrcat (result, access);
1.29      cvs      1194:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1195:                ustrcat (result, TEXT(":"));
1.29      cvs      1196:       }
                   1197:   
                   1198:   if (given.access && related.access)
                   1199:     /* If different, inherit nothing. */
1.67      cvs      1200:     if (ustrcmp (given.access, related.access) != 0)
1.29      cvs      1201:       {
                   1202:        related.host = 0;
                   1203:        related.absolute = 0;
                   1204:        related.relative = 0;
                   1205:        related.fragment = 0;
                   1206:       }
                   1207:   
                   1208:   if (wanted & AMAYA_PARSE_HOST)
                   1209:     if(given.host || related.host)
                   1210:       {
                   1211:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1212:          ustrcat (result, TEXT("//"));
                   1213:        ustrcat (result, given.host ? given.host : related.host);
1.29      cvs      1214:       }
                   1215:   
                   1216:   if (given.host && related.host)
                   1217:     /* If different hosts, inherit no path. */
1.67      cvs      1218:     if (ustrcmp (given.host, related.host) != 0)
1.29      cvs      1219:       {
                   1220:        related.absolute = 0;
                   1221:        related.relative = 0;
                   1222:        related.fragment = 0;
                   1223:       }
                   1224:   
                   1225:   if (wanted & AMAYA_PARSE_PATH)
                   1226:     {
                   1227:       if (given.absolute)
                   1228:        {
                   1229:          /* All is given */
                   1230:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1231:            ustrcat (result, used_str);
                   1232:          ustrcat (result, given.absolute);
1.25      cvs      1233:        }
1.29      cvs      1234:       else if (related.absolute)
                   1235:        {
                   1236:          /* Adopt path not name */
1.67      cvs      1237:          ustrcat (result, used_str);
                   1238:          ustrcat (result, related.absolute);
1.29      cvs      1239:          if (given.relative)
                   1240:            {
                   1241:              /* Search part? */
1.67      cvs      1242:              p = ustrchr (result, TEXT('?'));
1.29      cvs      1243:              if (!p)
1.67      cvs      1244:                p=result+ustrlen(result)-1;
1.33      cvs      1245:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1246:              /* Remove filename */
                   1247:              p[1]=0;
                   1248:              /* Add given one */
1.67      cvs      1249:              ustrcat (result, given.relative);
1.25      cvs      1250:            }
                   1251:        }
1.29      cvs      1252:       else if (given.relative)
                   1253:        /* what we've got */
1.67      cvs      1254:        ustrcat (result, given.relative);
1.29      cvs      1255:       else if (related.relative)
1.67      cvs      1256:        ustrcat (result, related.relative);
1.29      cvs      1257:       else
                   1258:        /* No inheritance */
1.67      cvs      1259:        ustrcat (result, used_str);
1.25      cvs      1260:     }
1.29      cvs      1261:   
                   1262:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1263:     if (given.fragment || related.fragment)
                   1264:       {
                   1265:        if (given.absolute && given.fragment)
                   1266:          {
                   1267:            /*Fixes for relURLs...*/
                   1268:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1269:              ustrcat (result, TEXT("#"));
                   1270:            ustrcat (result, given.fragment); 
1.29      cvs      1271:          }
                   1272:        else if (!(given.absolute) && !(given.fragment))
1.67      cvs      1273:          ustrcat (result, _EMPTYSTR_);
1.29      cvs      1274:        else
                   1275:          {
                   1276:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1277:              ustrcat (result, TEXT("#"));
                   1278:            ustrcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1279:          }
                   1280:       }
1.67      cvs      1281:   len = ustrlen (result);
                   1282:   if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1283:     ustrcpy (return_value, result);
1.29      cvs      1284:   return (return_value);               /* exactly the right length */
1.25      cvs      1285: }
                   1286: 
                   1287: /*----------------------------------------------------------------------
                   1288:      HTCanon
                   1289:        Canonicalizes the URL in the following manner starting from the host
                   1290:        pointer:
                   1291:   
                   1292:        1) The host name is converted to lowercase
                   1293:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1294:   
                   1295:        Return: OK      The position of the current path part of the URL
                   1296:                        which might be the old one or a new one.
                   1297:   
                   1298:   ----------------------------------------------------------------------*/
                   1299: #ifdef __STDC__
1.67      cvs      1300: static STRING HTCanon (STRING* filename, STRING host)
1.25      cvs      1301: #else  /* __STDC__ */
1.67      cvs      1302: static STRING HTCanon (filename, host)
                   1303: STRING      *filename;
                   1304: STRING      host;
1.25      cvs      1305: #endif /* __STDC__ */
                   1306: {
1.67      cvs      1307:     STRING  newname = NULL;
                   1308:     STRING  port;
                   1309:     STRING  strptr;
                   1310:     STRING  path;
                   1311:     STRING  access = host-3;
                   1312:     CHAR_T  used_sep;
1.32      cvs      1313: 
                   1314:   
1.67      cvs      1315:      if (*filename && ustrchr (*filename, URL_SEP))
1.33      cvs      1316:        {
                   1317:         used_sep = URL_SEP;
                   1318:        }
                   1319:      else
                   1320:        {
                   1321:         used_sep = DIR_SEP;
                   1322:        }
1.32      cvs      1323:   
1.33      cvs      1324:     while (access>*filename && *(access-1)!= used_sep)       /* Find access method */
1.25      cvs      1325:        access--;
1.67      cvs      1326:     if ((path = ustrchr(host, used_sep)) == NULL)                      /* Find path */
                   1327:        path = host + ustrlen(host);
                   1328:     if ((strptr = ustrchr(host, TEXT('@'))) != NULL && strptr<path)       /* UserId */
1.25      cvs      1329:        host = strptr;
1.67      cvs      1330:     if ((port = ustrchr(host, TEXT(':'))) != NULL && port>path)      /* Port number */
1.25      cvs      1331:        port = NULL;
                   1332: 
                   1333:     strptr = host;                                 /* Convert to lower-case */
1.33      cvs      1334:     while (strptr<path)
                   1335:       {
1.25      cvs      1336:        *strptr = tolower(*strptr);
                   1337:        strptr++;
1.33      cvs      1338:       }
1.25      cvs      1339:     
                   1340:     /* Does the URL contain a full domain name? This also works for a
                   1341:        numerical host name. The domain name is already made lower-case
                   1342:        and without a trailing dot. */
                   1343:     {
1.67      cvs      1344:       STRING dot = port ? port : path;
                   1345:       if (dot > *filename && *--dot == TEXT('.'))
1.33      cvs      1346:        {
1.67      cvs      1347:          STRING orig=dot, dest=dot+1;
1.33      cvs      1348:          while((*orig++ = *dest++));
                   1349:          if (port) port--;
                   1350:          path--;
1.25      cvs      1351:        }
                   1352:     }
                   1353:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1354:     if (port)
                   1355:       {
                   1356:        if (!*(port+1) || *(port+1)==used_sep)
                   1357:          {
                   1358:            if (!newname)
                   1359:              {
1.67      cvs      1360:                STRING orig=port, dest=port+1;
1.25      cvs      1361:                while((*orig++ = *dest++));
1.33      cvs      1362:              }
                   1363:          }
1.67      cvs      1364:        else if ((!ustrncmp(access, TEXT("http"), 4) &&
                   1365:                  (*(port+1) == TEXT('8') && *(port+2) == TEXT('0') && (*(port+3) == used_sep || !*(port + 3)))) ||
                   1366:                 (!ustrncmp (access, TEXT("gopher"), 6) &&
                   1367:                  (*(port+1) == TEXT('7') && *(port+2) == TEXT('0') && (*(port+3) == used_sep || !*(port+3)))) ||
                   1368:                 (!ustrncmp(access, TEXT("ftp"), 3) &&
                   1369:                  (*(port+1) == TEXT('2') && *(port + 2) == TEXT('1') && (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1370:          if (!newname)
                   1371:            {
1.67      cvs      1372:              STRING orig=port, dest=port+3;
1.33      cvs      1373:              while((*orig++ = *dest++));
                   1374:              /* Update path position, Henry Minsky */
                   1375:              path -= 3;
1.25      cvs      1376:            }
1.33      cvs      1377:        }
                   1378:        else if (newname)
1.67      cvs      1379:          ustrncat(newname, port, (int) (path-port));
1.33      cvs      1380:       }
1.25      cvs      1381: 
1.33      cvs      1382:     if (newname)
                   1383:       {
1.67      cvs      1384:        STRING newpath = newname + ustrlen (newname);
                   1385:        ustrcat(newname, path);
1.25      cvs      1386:        path = newpath;
1.28      cvs      1387:        /* Free old copy */
                   1388:        TtaFreeMemory(*filename);
1.25      cvs      1389:        *filename = newname;
1.33      cvs      1390:       }
1.25      cvs      1391:     return path;
                   1392: }
                   1393: 
                   1394: 
                   1395: /*----------------------------------------------------------------------
1.29      cvs      1396:   SimplifyUrl: simplify a URI
1.32      cvs      1397:   A URI is allowed to contain the sequence xxx/../ which may be
                   1398:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1399:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1400:   
1.28      cvs      1401:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1402:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1403:   
1.28      cvs      1404:   but we should NOT change
                   1405:                 http://fred.xxx.edu/../..
1.25      cvs      1406:   
                   1407:        or      ../../albert.html
                   1408:   
1.28      cvs      1409:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1410:   
                   1411:                /fred/..                becomes /fred/..
                   1412:                /fred/././..            becomes /fred/..
                   1413:                /fred/.././junk/.././   becomes /fred/..
                   1414:   
1.28      cvs      1415:   If more than one set of `://' is found (several proxies in cascade) then
                   1416:   only the part after the last `://' is simplified.
1.25      cvs      1417:   
1.28      cvs      1418:   Returns: A string which might be the old one or a new one.
1.25      cvs      1419:   ----------------------------------------------------------------------*/
                   1420: #ifdef __STDC__
1.67      cvs      1421: void         SimplifyUrl (STRING* url)
1.25      cvs      1422: #else  /* __STDC__ */
1.29      cvs      1423: void         SimplifyUrl (url)
1.67      cvs      1424: STRING       *url;
1.25      cvs      1425: #endif /* __STDC__ */
                   1426: {
1.67      cvs      1427:   STRING   path, p;
                   1428:   STRING   newptr, access;
                   1429:   STRING   orig, dest, end;
1.28      cvs      1430: 
1.67      cvs      1431:   CHAR_T   used_sep;
1.32      cvs      1432: 
                   1433: 
1.28      cvs      1434:   if (!url || !*url)
                   1435:     return;
                   1436: 
1.67      cvs      1437:   if (ustrchr (*url, URL_SEP))
1.33      cvs      1438:     {
                   1439:       used_sep = URL_SEP;
                   1440:     }
1.32      cvs      1441:   else
1.33      cvs      1442:     {
                   1443:       used_sep = DIR_SEP;
                   1444:     }
1.32      cvs      1445: 
1.28      cvs      1446:   /* Find any scheme name */
1.67      cvs      1447:   if ((path = ustrstr(*url, TEXT("://"))) != NULL)
1.33      cvs      1448:     {
                   1449:       /* Find host name */
1.28      cvs      1450:       access = *url;
                   1451:       while (access<path && (*access=tolower(*access)))
                   1452:        access++;
                   1453:       path += 3;
1.67      cvs      1454:       while ((newptr = ustrstr(path, TEXT("://"))) != NULL)
1.28      cvs      1455:         /* For proxies */
                   1456:        path = newptr+3;
                   1457:       /* We have a host name */
                   1458:       path = HTCanon(url, path);
1.25      cvs      1459:     }
1.67      cvs      1460:   else if ((path = ustrstr(*url, TEXT(":/"))) != NULL)
1.28      cvs      1461:     path += 2;
                   1462:   else
                   1463:     path = *url;
1.25      cvs      1464: 
1.33      cvs      1465:   if (*path == used_sep && *(path+1)==used_sep)
1.28      cvs      1466:     /* Some URLs start //<foo> */
                   1467:     path += 1;
1.67      cvs      1468:   else if (!ustrncmp(path, TEXT("news:"), 5))
1.28      cvs      1469:     {
1.67      cvs      1470:       newptr = ustrchr(path+5, TEXT('@'));
1.28      cvs      1471:       if (!newptr)
                   1472:        newptr = path + 5;
                   1473:       while (*newptr)
                   1474:        {
                   1475:          /* Make group or host lower case */
                   1476:          *newptr = tolower (*newptr);
                   1477:          newptr++;
1.25      cvs      1478:        }
1.28      cvs      1479:       /* Doesn't need to do any more */
                   1480:       return;
1.25      cvs      1481:     }
1.28      cvs      1482: 
                   1483:   if ((p = path))
                   1484:     {
1.67      cvs      1485:       if (!((end = ustrchr (path, TEXT(';'))) || (end = ustrchr (path, TEXT('?'))) ||
                   1486:            (end = ustrchr (path, TEXT('#')))))
                   1487:        end = path + ustrlen (path);
1.28      cvs      1488:       
                   1489:       /* Parse string second time to simplify */
                   1490:       p = path;
                   1491:       while (p < end)
                   1492:        {
1.33      cvs      1493:          if (*p==used_sep)
1.28      cvs      1494:            {
1.67      cvs      1495:              if (p > *url && *(p+1) == TEXT('.') && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1496:                {
                   1497:                  orig = p + 1;
1.33      cvs      1498:                  dest = (*(p+2)!=used_sep) ? p+2 : p+3;
1.52      cvs      1499:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1500:                  end = orig - 1;
                   1501:                }
1.67      cvs      1502:              else if (*(p+1) == TEXT('.') && *(p+2) == TEXT('.') && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1503:                {
                   1504:                  newptr = p;
1.52      cvs      1505:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1506:                  if (*newptr == used_sep)
                   1507:                    orig = newptr + 1;
1.28      cvs      1508:                  else
1.52      cvs      1509:                    orig = newptr;
                   1510: 
                   1511:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1512:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1513:                  end = orig-1;
                   1514:                  /* Start again with prev slash */
                   1515:                  p = newptr;
1.28      cvs      1516:                }
1.33      cvs      1517:              else if (*(p+1) == used_sep)
1.28      cvs      1518:                {
1.33      cvs      1519:                  while (*(p+1) == used_sep)
1.28      cvs      1520:                    {
                   1521:                      orig = p;
                   1522:                      dest = p + 1;
                   1523:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1524:                      end = orig-1;
                   1525:                    }
                   1526:                }
                   1527:              else
1.25      cvs      1528:                p++;
1.28      cvs      1529:            }
                   1530:          else
                   1531:            p++;
1.25      cvs      1532:        }
                   1533:     }
1.51      cvs      1534: 
                   1535:     /*
                   1536:     **  Check for host/../.. kind of things
                   1537:     */
1.67      cvs      1538:     if (*path == used_sep && *(path+1) == TEXT('.') && *(path+2) == TEXT('.') && (!*(path+3) || *(path+3) == used_sep))
1.51      cvs      1539:        *(path+1) = EOS;
                   1540: 
1.28      cvs      1541:   return;
                   1542: }
                   1543: 
                   1544: 
                   1545: /*----------------------------------------------------------------------
                   1546:    NormalizeFile normalizes  local names.                             
                   1547:    Return TRUE if target and src differ.                           
                   1548:   ----------------------------------------------------------------------*/
                   1549: #ifdef __STDC__
1.67      cvs      1550: ThotBool             NormalizeFile (STRING src, STRING target)
1.28      cvs      1551: #else
1.67      cvs      1552: ThotBool             NormalizeFile (src, target)
                   1553: STRING              src;
                   1554: STRING              target;
1.28      cvs      1555: 
                   1556: #endif
                   1557: {
1.67      cvs      1558:    STRING             s;
1.66      cvs      1559:    ThotBool            change;
1.28      cvs      1560: 
1.54      cvs      1561:    change = FALSE;
1.67      cvs      1562:    if (ustrncmp (src, TEXT("file:"), 5) == 0)
1.28      cvs      1563:      {
                   1564:        /* remove the prefix file: */
                   1565:        if (src[5] == EOS)
1.67      cvs      1566:           ustrcpy (target, DIR_STR);
                   1567:        else if (src[0] == TEXT('~'))
1.28      cvs      1568:          {
                   1569:            /* replace ~ */
1.67      cvs      1570:            s = TtaGetEnvString (TEXT("HOME"));
                   1571:            ustrcpy (target, s);
                   1572:            ustrcat (target, &src[5]);
1.28      cvs      1573:          }
                   1574:        else
1.67      cvs      1575:           ustrcpy (target, &src[5]);
1.54      cvs      1576:        change = TRUE;
1.28      cvs      1577:      }
1.53      cvs      1578: #  ifndef _WINDOWS
1.67      cvs      1579:    else if (src[0] == TEXT('~'))
1.53      cvs      1580:      {
                   1581:        /* replace ~ */
                   1582:        s = (char *) TtaGetEnvString ("HOME");
                   1583:        strcpy (target, s);
                   1584:        if (src[1] != DIR_SEP)
                   1585:          strcat (target, DIR_STR);
                   1586:        strcat (target, &src[1]);
1.54      cvs      1587:        change = TRUE;
1.53      cvs      1588:      }
                   1589: #   endif /* _WINDOWS */
1.28      cvs      1590:    else
1.67      cvs      1591:       ustrcpy (target, src);
1.28      cvs      1592: 
                   1593:    /* remove /../ and /./ */
1.29      cvs      1594:    SimplifyUrl (&target);
1.54      cvs      1595:    if (!change)
1.67      cvs      1596:      change = ustrcmp (src, target);
1.28      cvs      1597:    return (change);
1.25      cvs      1598: }
                   1599: 
1.28      cvs      1600: 
1.25      cvs      1601: /*----------------------------------------------------------------------
1.31      cvs      1602:   MakeRelativeURL: make relative name
1.25      cvs      1603:   
1.28      cvs      1604:   This function creates and returns a string which gives an expression of
                   1605:   one address as related to another. Where there is no relation, an absolute
                   1606:   address is retured.
1.25      cvs      1607:   
1.28      cvs      1608:   On entry,
1.25      cvs      1609:        Both names must be absolute, fully qualified names of nodes
                   1610:        (no fragment bits)
                   1611:   
1.28      cvs      1612:   On exit,
1.25      cvs      1613:        The return result points to a newly allocated name which, if
                   1614:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1615:        The caller is responsible for freeing the resulting name later.
                   1616:   ----------------------------------------------------------------------*/
                   1617: #ifdef __STDC__
1.67      cvs      1618: STRING           MakeRelativeURL (STRING aName, STRING relatedName)
1.25      cvs      1619: #else  /* __STDC__ */
1.67      cvs      1620: STRING           MakeRelativeURL (aName, relatedName)
                   1621: STRING           aName;
                   1622: STRING           relatedName;
1.25      cvs      1623: #endif  /* __STDC__ */
                   1624: {
1.67      cvs      1625:   STRING         return_value;
                   1626:   CHAR_T         result[MAX_LENGTH];
                   1627:   STRING         p;
                   1628:   STRING         q;
                   1629:   STRING         after_access;
                   1630:   STRING         last_slash = NULL;
1.29      cvs      1631:   int            slashes, levels, len;
                   1632: 
1.44      cvs      1633: # ifdef _WINDOWS
                   1634:   int ndx;
                   1635: # endif /* _WINDOWS */
                   1636: 
1.29      cvs      1637:   if (aName == NULL || relatedName == NULL)
                   1638:     return (NULL);
                   1639: 
                   1640:   slashes = 0;
                   1641:   after_access = NULL;
                   1642:   p = aName;
                   1643:   q = relatedName;
                   1644:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1645:     {
                   1646:       /* Find extent of match */
1.67      cvs      1647:       if (*p == TEXT(':'))
1.29      cvs      1648:        after_access = p + 1;
1.28      cvs      1649:       if (*p == DIR_SEP)
1.27      cvs      1650:        {
1.29      cvs      1651:          /* memorize the last slash position and count them */
1.27      cvs      1652:          last_slash = p;
                   1653:          slashes++;
1.25      cvs      1654:        }
                   1655:     }
                   1656:     
1.31      cvs      1657:   /* q, p point to the first non-matching character or zero */
                   1658:   if (*q == EOS)
                   1659:     {
                   1660:       /* New name is a subset of the related name */
                   1661:       /* exactly the right length */
1.67      cvs      1662:       len = ustrlen (p);
                   1663:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1664:        ustrcpy (return_value, p);
1.31      cvs      1665:     }
                   1666:   else if ((slashes < 2 && after_access == NULL)
                   1667:       || (slashes < 3 && after_access != NULL))
                   1668:     {
                   1669:       /* Two names whitout common path */
                   1670:       /* exactly the right length */
1.67      cvs      1671:       len = ustrlen (aName);
                   1672:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1673:        ustrcpy (return_value, aName);
1.31      cvs      1674:     }
                   1675:   else
                   1676:     {
                   1677:       /* Some path in common */
1.67      cvs      1678:       if (slashes == 3 && ustrncmp (aName, TEXT("http:"), 5) == 0)
1.31      cvs      1679:        /* just the same server */
1.67      cvs      1680:        ustrcpy (result, last_slash);
1.31      cvs      1681:       else
                   1682:        {
                   1683:          levels= 0; 
1.67      cvs      1684:          for (; *q && *q != TEXT('#') && *q != TEXT(';') && *q != TEXT('?'); q++)
1.31      cvs      1685:            if (*q == DIR_SEP)
                   1686:              levels++;
                   1687:          
1.52      cvs      1688:          result[0] = EOS;
1.31      cvs      1689:          for (;levels; levels--)
1.67      cvs      1690:            ustrcat (result, TEXT("../"));
                   1691:          ustrcat (result, last_slash+1);
1.31      cvs      1692:        } 
1.52      cvs      1693: 
                   1694:       if (!*result)
1.67      cvs      1695:        ustrcat (result, TEXT("./"));
1.52      cvs      1696: 
1.31      cvs      1697:       /* exactly the right length */
1.67      cvs      1698:       len = ustrlen (result);
                   1699:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1700:        ustrcpy (return_value, result);
1.52      cvs      1701: 
1.25      cvs      1702:     }
1.44      cvs      1703: # ifdef _WINDOWS
1.67      cvs      1704:   len = ustrlen (return_value);
1.44      cvs      1705:   for (ndx = 0; ndx < len; ndx ++)
1.67      cvs      1706:          if (return_value[ndx] == TEXT('\\'))
                   1707:             return_value[ndx] = TEXT('/') ;
1.44      cvs      1708: # endif /* _WINDOWS */
1.29      cvs      1709:   return (return_value);
1.24      cvs      1710: }
1.35      cvs      1711: 
                   1712: 

Webmaster