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

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.67      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)))
1.5       cvs       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)
        !           561:       || !strncmp (url, "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.41      cvs       695:       if (IsW3Path)
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.59      cvs       713: #   ifdef _WINDOWS
1.67      cvs       714:        umkdir (ptr);
1.59      cvs       715: #   else  /* !_WINDOWS */
1.67      cvs       716:        umkdir (ptr, S_IRWXU);
1.59      cvs       717: #   endif /* !_WINDOWS */
1.47      cvs       718: 
                    719:       /* don't include the query string within document name */
1.67      cvs       720:       n = ustrrchr(documentname, TEXT('?'));
1.47      cvs       721:       if (n != NULL)
                    722:        *n = EOS;
1.46      cvs       723:       /* don't include ':' within document name */
1.67      cvs       724:       n = ustrchr (documentname, TEXT(':'));
1.46      cvs       725:       if (n != NULL)
                    726:        *n = EOS;
1.69    ! cvs       727:       /* if after all this operations document name
        !           728:         is empty, let's use noname.html instead */
        !           729:       if (documentname[0] == EOS)
        !           730:        ustrcat (ptr, TEXT("noname.html"));
        !           731:       else
        !           732:        ustrcat (ptr, documentname);
1.40      cvs       733:       TtaFreeMemory (documentname);
                    734:       /* restore the url */
                    735:       if (noFile)
1.41      cvs       736:        url[len] = url_sep;
1.40      cvs       737:       return (ptr);
                    738:     }
                    739:   else
                    740:     return (NULL);
                    741: }
                    742: 
                    743: 
                    744: /*----------------------------------------------------------------------
1.9       cvs       745:    NormalizeURL
                    746:    normalizes orgName according to a base associated with doc, and
                    747:    following the standard URL format rules.
1.53      cvs       748:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    749:    other path.
1.9       cvs       750:    The function returns the new complete and normalized URL 
1.12      cvs       751:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       752:    N.B. If the function can't find out what's the docName, it assigns
                    753:    the name "noname.html".
1.4       cvs       754:   ----------------------------------------------------------------------*/
1.3       cvs       755: #ifdef __STDC__
1.67      cvs       756: void                NormalizeURL (STRING orgName, Document doc, STRING newName, STRING docName, STRING otherPath)
1.3       cvs       757: #else  /* __STDC__ */
1.53      cvs       758: void                NormalizeURL (orgName, doc, newName, docName, otherPath)
1.67      cvs       759: STRING              orgName;
1.3       cvs       760: Document            doc;
1.67      cvs       761: STRING              newName;
                    762: STRING              docName;
                    763: STRING              otherPath;
1.3       cvs       764: #endif /* __STDC__ */
                    765: {
1.67      cvs       766:    STRING             basename;
                    767:    CHAR_T             tempOrgName[MAX_LENGTH];
                    768:    STRING             ptr;
                    769:    CHAR_T             used_sep;
                    770:    int                length;
1.66      cvs       771:    ThotBool            check;
1.5       cvs       772: 
1.44      cvs       773: #  ifdef _WINDOWS
                    774:    int ndx;
                    775: #  endif /* _WINDOWS */
                    776: 
1.5       cvs       777:    if (!newName || !docName)
                    778:       return;
1.18      cvs       779: 
1.32      cvs       780:    if (doc != 0)
1.53      cvs       781:      basename = GetBaseURL (doc);
                    782:    else if (otherPath != NULL)
                    783:      basename = TtaStrdup (otherPath);
1.32      cvs       784:    else
1.53      cvs       785:      basename = NULL;
1.32      cvs       786: 
1.18      cvs       787:    /*
1.31      cvs       788:     * Clean orgName
                    789:     * Make sure we have a complete orgName, without any leading or trailing
                    790:     * white spaces, or trailinbg new lines
                    791:     */
1.5       cvs       792:    ptr = orgName;
1.18      cvs       793:    /* skip leading white space and new line characters */
1.67      cvs       794:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                    795:    ustrncpy (tempOrgName, ptr, MAX_LENGTH -1);
1.39      cvs       796:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs       797:    /*
1.31      cvs       798:     * Make orgName a complete URL
                    799:     * If the URL does not include a protocol, then try to calculate
                    800:     * one using the doc's base element (if it exists),
                    801:     */
1.53      cvs       802:    if (tempOrgName[0] == EOS)
                    803:      {
                    804:        newName[0] = EOS;
                    805:        TtaFreeMemory (basename);
                    806:        return;
                    807:      }
1.49      cvs       808: 
                    809:    /* clean trailing white space */
1.67      cvs       810:    length = ustrlen (tempOrgName) - 1;
1.53      cvs       811:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
                    812:      {
                    813:        tempOrgName[length] = EOS;
                    814:        length--;
                    815:      }
1.50      cvs       816: 
1.55      cvs       817:    /* remove extra dot (which dot???) */
                    818:    /* ugly, but faster than a strcmp */
1.67      cvs       819:    if (tempOrgName[length] == TEXT('.')
                    820:        && (length == 0 || tempOrgName[length-1] != TEXT('.')))
1.55      cvs       821:         tempOrgName[length] = EOS;
1.50      cvs       822: 
1.53      cvs       823:    if (IsW3Path (tempOrgName))
                    824:      {
                    825:        /* the name is complete, go to the Sixth Step */
1.67      cvs       826:        ustrcpy (newName, tempOrgName);
1.53      cvs       827:        SimplifyUrl (&newName);
                    828:        /* verify if the URL has the form "protocol://server:port" */
1.67      cvs       829:        ptr = AmayaParseUrl (newName, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    830:        if (ptr && !ustrcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
                    831:          ustrcat (newName, URL_STR);
1.49      cvs       832: 
1.53      cvs       833:        if (ptr)
1.50      cvs       834:          TtaFreeMemory (ptr);
1.53      cvs       835:      }
                    836:    else if ( basename == NULL)
                    837:      /* the name is complete, go to the Sixth Step */
1.67      cvs       838:      ustrcpy (newName, tempOrgName);
1.53      cvs       839:    else
                    840:      {
1.31      cvs       841:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs       842: #      ifdef _WINDOWS
1.53      cvs       843:        if (!IsW3Path (basename))
                    844:         {
1.67      cvs       845:           length = ustrlen (tempOrgName);
1.53      cvs       846:           for (ndx = 0; ndx < length; ndx++)
1.67      cvs       847:             if (tempOrgName [ndx] == TEXT('/'))
                    848:               tempOrgName [ndx] = TEXT('\\');
1.53      cvs       849:         }
1.44      cvs       850: #      endif /* _WINDOWS */
1.25      cvs       851:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs       852:        if (ptr)
                    853:         {
                    854:           SimplifyUrl (&ptr);
1.67      cvs       855:           ustrcpy (newName, ptr);
1.53      cvs       856:           TtaFreeMemory (ptr);
                    857:         }
                    858:        else
                    859:         newName[0] = EOS;
                    860:      }
1.36      cvs       861: 
                    862:    TtaFreeMemory (basename);
1.18      cvs       863:    /*
1.31      cvs       864:     * Prepare the docname that will refer to this ressource in the
                    865:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                    866:     * noname.html as a default ressource name
1.18      cvs       867:    */
1.53      cvs       868:    if (newName[0] != EOS)
                    869:      {
1.67      cvs       870:        length = ustrlen (newName) - 1;
1.53      cvs       871:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
                    872:         {
                    873:           used_sep = newName[length];
                    874:           check = TRUE;
                    875:           while (check)
                    876:             {
1.50      cvs       877:                length--;
                    878:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs       879:                 length--;
1.67      cvs       880:                if (!ustrncmp (&newName[length+1], TEXT(".."), 2))
1.53      cvs       881:                 {
                    882:                   newName[length+1] = EOS;
                    883:                   /* remove also previous directory */
                    884:                   length--;
                    885:                   while (length >= 0 && newName[length] != used_sep)
                    886:                     length--;
1.67      cvs       887:                   if (ustrncmp (&newName[length+1], TEXT("//"), 2))
1.53      cvs       888:                     /* don't remove server name */
1.50      cvs       889:                      newName[length+1] = EOS;
1.53      cvs       890:                 }
1.67      cvs       891:               else if (!ustrncmp (&newName[length+1], TEXT("."), 1))
1.53      cvs       892:                 newName[length+1] = EOS;
1.50      cvs       893:                else
1.53      cvs       894:                 check = FALSE;
                    895:             }
1.67      cvs       896:           ustrcpy (docName, TEXT("noname.html"));             
1.53      cvs       897:           /* docname was not comprised inside the URL, so let's */
                    898:           /* assign the default ressource name */
1.67      cvs       899:           ustrcpy (docName, TEXT("noname.html"));
1.53      cvs       900:         }
                    901:        else
                    902:         { /* docname is comprised inside the URL */
1.50      cvs       903:            while (length >= 0 && newName[length] != URL_SEP && newName[length] != DIR_SEP)
1.53      cvs       904:             length--;
                    905:           if (length < 0)
1.67      cvs       906:              ustrcpy (docName, newName);
1.53      cvs       907:           else
1.67      cvs       908:             ustrcpy (docName, &newName[length+1]);
1.53      cvs       909:         }
                    910:      }
                    911:    else
                    912:      docName[0] = EOS;
1.18      cvs       913: } 
1.3       cvs       914: 
1.4       cvs       915: /*----------------------------------------------------------------------
1.9       cvs       916:   IsSameHost                                                         
1.4       cvs       917:   ----------------------------------------------------------------------*/
1.3       cvs       918: #ifdef __STDC__
1.67      cvs       919: ThotBool             IsSameHost (const STRING url1, const STRING url2)
1.3       cvs       920: #else  /* __STDC__ */
1.67      cvs       921: ThotBool             IsSameHost (url1, url2)
                    922: const STRING       url1;
                    923: const STRING       url2;
1.3       cvs       924: #endif /* __STDC__ */
                    925: {
1.67      cvs       926:    STRING           basename_ptr1, basename_ptr2;
                    927:    ThotBool          result;
1.3       cvs       928: 
1.67      cvs       929:    basename_ptr1 = AmayaParseUrl (url1, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    930:    basename_ptr2 = AmayaParseUrl (url2, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs       931: 
1.67      cvs       932:    if (ustrcmp (basename_ptr1, basename_ptr2))
1.8       cvs       933:       result = FALSE;
1.5       cvs       934:    else
1.8       cvs       935:       result = TRUE;
1.3       cvs       936: 
1.25      cvs       937:    TtaFreeMemory (basename_ptr1);
                    938:    TtaFreeMemory (basename_ptr2);
1.5       cvs       939:    return (result);
1.3       cvs       940: }
                    941: 
                    942: 
1.4       cvs       943: /*----------------------------------------------------------------------
1.22      cvs       944:   HasKnownFileSuffix
                    945:   returns TRUE if path points to a file ending with a suffix.
                    946:   ----------------------------------------------------------------------*/
                    947: #ifdef __STDC__
1.67      cvs       948: ThotBool             HasKnownFileSuffix (const STRING path)
1.22      cvs       949: #else  /* __STDC__ */
1.67      cvs       950: ThotBool             HasKnownFileSuffix (path)
                    951: const STRING    path;
1.22      cvs       952: #endif /* __STDC__ */
                    953: {
1.67      cvs       954:    STRING      root;
                    955:    CHAR_T      temppath[MAX_LENGTH];
                    956:    CHAR_T      suffix[MAX_LENGTH];
1.22      cvs       957: 
1.67      cvs       958:    if (!path || path[0] == EOS || path[ustrlen(path)] == DIR_SEP)
1.22      cvs       959:      return (FALSE);
                    960: 
1.67      cvs       961:    root = AmayaParseUrl(path, _EMPTYSTR_, AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs       962: 
                    963:    if (root) 
                    964:      {
1.67      cvs       965:        ustrcpy (temppath, root);
1.25      cvs       966:        TtaFreeMemory (root);
1.22      cvs       967:        /* Get the suffix */
                    968:        ExtractSuffix (temppath, suffix); 
                    969: 
                    970:        if( suffix[0] == EOS)
                    971:         /* no suffix */
                    972:         return (FALSE);
                    973: 
                    974:        /* Normalize the suffix */
                    975:        ConvertToLowerCase (suffix);
                    976: 
1.67      cvs       977:        if (!ustrcmp (suffix, TEXT("gz")))
1.22      cvs       978:         /* skip the compressed suffix */
                    979:         {
                    980:         ExtractSuffix (temppath, suffix);
                    981:         if(suffix[0] == EOS)
                    982:           /* no suffix */
                    983:           return (FALSE);
                    984:          /* Normalize the suffix */
                    985:          ConvertToLowerCase (suffix);
                    986:         }
                    987: 
1.67      cvs       988:        if (ustrcmp (suffix, TEXT("gif")) &&
                    989:           ustrcmp (suffix, TEXT("xbm")) &&
                    990:           ustrcmp (suffix, TEXT("xpm")) &&
                    991:           ustrcmp (suffix, TEXT("jpg")) &&
                    992:           ustrcmp (suffix, TEXT("pdf")) &&
                    993:           ustrcmp (suffix, TEXT("png")) &&
                    994:           ustrcmp (suffix, TEXT("tgz")) &&
                    995:           ustrcmp (suffix, TEXT("xpg")) &&
                    996:           ustrcmp (suffix, TEXT("xpd")) &&
                    997:           ustrcmp (suffix, TEXT("ps")) &&
                    998:           ustrcmp (suffix, TEXT("au")) &&
                    999:           ustrcmp (suffix, TEXT("html")) &&
                   1000:           ustrcmp (suffix, TEXT("htm")) &&
                   1001:           ustrcmp (suffix, TEXT("shtml")) &&
                   1002:           ustrcmp (suffix, TEXT("xht")) &&
                   1003:           ustrcmp (suffix, TEXT("xhtm")) &&
                   1004:           ustrcmp (suffix, TEXT("xhtml")) &&
                   1005:           ustrcmp (suffix, TEXT("txt")) &&
                   1006:           ustrcmp (suffix, TEXT("css")) &&
                   1007:           ustrcmp (suffix, TEXT("eps")))
1.22      cvs      1008:         return (FALSE);
                   1009:        else
                   1010:         return (TRUE);
                   1011:      }
                   1012:    else
                   1013:      return (FALSE);
                   1014: }
                   1015: 
                   1016: 
                   1017: /*----------------------------------------------------------------------
1.24      cvs      1018:   ChopURL
                   1019:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1020:   If inputURL is  bigger than that size, outputURL receives
                   1021:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1022:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1023:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1024:   copied into outputURL. 
                   1025:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1026:   chars.
                   1027:   ----------------------------------------------------------------------*/
                   1028: #ifdef __STDC__
1.34      cvs      1029: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1030: #else
                   1031: void ChopURL (outputURL, inputURL)
1.34      cvs      1032: char       *outputURL;
                   1033: const char *inputURL;
1.24      cvs      1034: #endif
1.22      cvs      1035: 
1.24      cvs      1036: {
                   1037:   int len;
1.9       cvs      1038: 
1.24      cvs      1039:   len = strlen (inputURL);
                   1040:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs      1041:     strcpy (outputURL, inputURL);
1.24      cvs      1042:   else
                   1043:     /* make a truncated urlName on the status window */
                   1044:     {
                   1045:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1046:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1047:       strcat (outputURL, "...");
                   1048:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                   1049:     }
1.25      cvs      1050: }
                   1051: 
                   1052: 
                   1053: /*----------------------------------------------------------------------
                   1054:    scan
1.47      cvs      1055:        Scan a filename for its constituents
1.25      cvs      1056:        -----------------------------------
                   1057:   
                   1058:    On entry,
                   1059:        name    points to a document name which may be incomplete.
                   1060:    On exit,
                   1061:         absolute or relative may be nonzero (but not both).
                   1062:        host, fragment and access may be nonzero if they were specified.
                   1063:        Any which are nonzero point to zero terminated strings.
                   1064:   ----------------------------------------------------------------------*/
                   1065: #ifdef __STDC__
1.67      cvs      1066: static void scan (STRING name, HTURI * parts)
1.25      cvs      1067: #else  /* __STDC__ */
                   1068: static void scan (name, parts)
1.67      cvs      1069: STRING      name;
                   1070: HTURI       *parts;
1.25      cvs      1071: 
                   1072: #endif /* __STDC__ */
                   1073: {
1.67      cvs      1074:   STRING    p;
                   1075:   STRING    after_access = name;
1.32      cvs      1076: 
1.43      cvs      1077:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1078:   /* Look for fragment identifier */
1.67      cvs      1079:   if ((p = ustrchr(name, TEXT('#'))) != NULL)
1.28      cvs      1080:     {
1.67      cvs      1081:       *p++ = TEXT('\0');
1.28      cvs      1082:       parts->fragment = p;
1.25      cvs      1083:     }
                   1084:     
1.28      cvs      1085:   for (p=name; *p; p++)
                   1086:     {
1.67      cvs      1087:       if (*p == URL_SEP || *p == DIR_SEP || *p == TEXT('#') || *p == TEXT('?'))
1.28      cvs      1088:        break;
1.67      cvs      1089:       if (*p == TEXT(':'))
1.28      cvs      1090:        {
                   1091:          *p = 0;
                   1092:          parts->access = after_access; /* Scheme has been specified */
                   1093: 
                   1094:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1095:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1096:             not recommended. Rather, turn off "-O". */
                   1097: 
                   1098:          /*            after_access = p;*/
                   1099:          /*            while (*after_access == 0)*/
                   1100:          /*                after_access++;*/
                   1101:          after_access = p+1;
1.67      cvs      1102:          if (!ustrcasecmp(TEXT("URL"), parts->access))
1.28      cvs      1103:            /* Ignore IETF's URL: pre-prefix */
                   1104:            parts->access = NULL;
                   1105:          else
1.25      cvs      1106:            break;
                   1107:        }
                   1108:     }
                   1109:     
                   1110:     p = after_access;
1.43      cvs      1111:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1112:       {
1.43      cvs      1113:        if (p[1] == URL_SEP)
1.28      cvs      1114:          {
1.25      cvs      1115:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1116:            *p = 0;                     /* Terminate access             */
                   1117:            /* look for end of host name if any */
1.67      cvs      1118:            p = ustrchr (parts->host, URL_SEP);
1.28      cvs      1119:            if (p)
                   1120:              {
1.43      cvs      1121:                *p = EOS;                       /* Terminate host */
1.25      cvs      1122:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1123:              }
                   1124:          }
                   1125:        else
                   1126:          /* Root found but no host */
                   1127:          parts->absolute = p+1;
                   1128:       }
                   1129:     else
                   1130:       {
1.25      cvs      1131:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1132:       }
1.25      cvs      1133: }
                   1134: 
                   1135: 
                   1136: /*----------------------------------------------------------------------
1.28      cvs      1137:   AmayaParseUrl: parse a Name relative to another name
                   1138: 
                   1139:   This returns those parts of a name which are given (and requested)
                   1140:   substituting bits from the related name where necessary.
1.25      cvs      1141:   
1.28      cvs      1142:   On entry,
1.25      cvs      1143:        aName           A filename given
                   1144:         relatedName     A name relative to which aName is to be parsed. Give
                   1145:                         it an empty string if aName is absolute.
                   1146:         wanted          A mask for the bits which are wanted.
                   1147:   
1.28      cvs      1148:   On exit,
1.25      cvs      1149:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1150:   ----------------------------------------------------------------------*/
                   1151: #ifdef __STDC__
1.67      cvs      1152: STRING        AmayaParseUrl (const STRING aName, STRING relatedName, int wanted)
1.25      cvs      1153: #else  /* __STDC__ */
1.67      cvs      1154: STRING        AmayaParseUrl (aName, relatedName, wanted)
                   1155: const STRING  aName;
                   1156: STRING        relatedName;
1.28      cvs      1157: int            wanted;
1.25      cvs      1158: 
                   1159: #endif /* __STDC__ */
                   1160: {
1.67      cvs      1161:   STRING     return_value;
                   1162:   CHAR_T     result[MAX_LENGTH];
                   1163:   CHAR_T     name[MAX_LENGTH];
                   1164:   CHAR_T     rel[MAX_LENGTH];
                   1165:   STRING     p, access;
1.29      cvs      1166:   HTURI      given, related;
                   1167:   int        len;
1.67      cvs      1168:   CHAR_T     used_sep;
                   1169:   STRING     used_str;
1.32      cvs      1170: 
1.67      cvs      1171:   if (ustrchr (aName, DIR_SEP) || ustrchr (relatedName, DIR_SEP))
1.33      cvs      1172:     {
1.42      cvs      1173:       used_str = DIR_STR;
                   1174:       used_sep = DIR_SEP;
1.33      cvs      1175:     }
1.32      cvs      1176:   else
1.33      cvs      1177:     {
1.42      cvs      1178:       used_str = URL_STR;
                   1179:       used_sep = URL_SEP;
1.33      cvs      1180:     }
1.32      cvs      1181: 
1.29      cvs      1182:   /* Make working copies of input strings to cut up: */
                   1183:   return_value = NULL;
                   1184:   result[0] = 0;               /* Clear string  */
1.67      cvs      1185:   ustrcpy (name, aName);
1.29      cvs      1186:   if (relatedName != NULL)  
1.67      cvs      1187:     ustrcpy (rel, relatedName);
1.29      cvs      1188:   else
                   1189:     relatedName[0] = EOS;
                   1190:   
                   1191:   scan (name, &given);
                   1192:   scan (rel,  &related); 
                   1193:   access = given.access ? given.access : related.access;
                   1194:   if (wanted & AMAYA_PARSE_ACCESS)
                   1195:     if (access)
                   1196:       {
1.67      cvs      1197:        ustrcat (result, access);
1.29      cvs      1198:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1199:                ustrcat (result, TEXT(":"));
1.29      cvs      1200:       }
                   1201:   
                   1202:   if (given.access && related.access)
                   1203:     /* If different, inherit nothing. */
1.67      cvs      1204:     if (ustrcmp (given.access, related.access) != 0)
1.29      cvs      1205:       {
                   1206:        related.host = 0;
                   1207:        related.absolute = 0;
                   1208:        related.relative = 0;
                   1209:        related.fragment = 0;
                   1210:       }
                   1211:   
                   1212:   if (wanted & AMAYA_PARSE_HOST)
                   1213:     if(given.host || related.host)
                   1214:       {
                   1215:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1216:          ustrcat (result, TEXT("//"));
                   1217:        ustrcat (result, given.host ? given.host : related.host);
1.29      cvs      1218:       }
                   1219:   
                   1220:   if (given.host && related.host)
                   1221:     /* If different hosts, inherit no path. */
1.67      cvs      1222:     if (ustrcmp (given.host, related.host) != 0)
1.29      cvs      1223:       {
                   1224:        related.absolute = 0;
                   1225:        related.relative = 0;
                   1226:        related.fragment = 0;
                   1227:       }
                   1228:   
                   1229:   if (wanted & AMAYA_PARSE_PATH)
                   1230:     {
                   1231:       if (given.absolute)
                   1232:        {
                   1233:          /* All is given */
                   1234:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1235:            ustrcat (result, used_str);
                   1236:          ustrcat (result, given.absolute);
1.25      cvs      1237:        }
1.29      cvs      1238:       else if (related.absolute)
                   1239:        {
                   1240:          /* Adopt path not name */
1.67      cvs      1241:          ustrcat (result, used_str);
                   1242:          ustrcat (result, related.absolute);
1.29      cvs      1243:          if (given.relative)
                   1244:            {
                   1245:              /* Search part? */
1.67      cvs      1246:              p = ustrchr (result, TEXT('?'));
1.29      cvs      1247:              if (!p)
1.67      cvs      1248:                p=result+ustrlen(result)-1;
1.33      cvs      1249:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1250:              /* Remove filename */
                   1251:              p[1]=0;
                   1252:              /* Add given one */
1.67      cvs      1253:              ustrcat (result, given.relative);
1.25      cvs      1254:            }
                   1255:        }
1.29      cvs      1256:       else if (given.relative)
                   1257:        /* what we've got */
1.67      cvs      1258:        ustrcat (result, given.relative);
1.29      cvs      1259:       else if (related.relative)
1.67      cvs      1260:        ustrcat (result, related.relative);
1.29      cvs      1261:       else
                   1262:        /* No inheritance */
1.67      cvs      1263:        ustrcat (result, used_str);
1.25      cvs      1264:     }
1.29      cvs      1265:   
                   1266:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1267:     if (given.fragment || related.fragment)
                   1268:       {
                   1269:        if (given.absolute && given.fragment)
                   1270:          {
                   1271:            /*Fixes for relURLs...*/
                   1272:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1273:              ustrcat (result, TEXT("#"));
                   1274:            ustrcat (result, given.fragment); 
1.29      cvs      1275:          }
                   1276:        else if (!(given.absolute) && !(given.fragment))
1.67      cvs      1277:          ustrcat (result, _EMPTYSTR_);
1.29      cvs      1278:        else
                   1279:          {
                   1280:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1281:              ustrcat (result, TEXT("#"));
                   1282:            ustrcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1283:          }
                   1284:       }
1.67      cvs      1285:   len = ustrlen (result);
                   1286:   if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1287:     ustrcpy (return_value, result);
1.29      cvs      1288:   return (return_value);               /* exactly the right length */
1.25      cvs      1289: }
                   1290: 
                   1291: /*----------------------------------------------------------------------
                   1292:      HTCanon
                   1293:        Canonicalizes the URL in the following manner starting from the host
                   1294:        pointer:
                   1295:   
                   1296:        1) The host name is converted to lowercase
                   1297:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1298:   
                   1299:        Return: OK      The position of the current path part of the URL
                   1300:                        which might be the old one or a new one.
                   1301:   
                   1302:   ----------------------------------------------------------------------*/
                   1303: #ifdef __STDC__
1.67      cvs      1304: static STRING HTCanon (STRING* filename, STRING host)
1.25      cvs      1305: #else  /* __STDC__ */
1.67      cvs      1306: static STRING HTCanon (filename, host)
                   1307: STRING      *filename;
                   1308: STRING      host;
1.25      cvs      1309: #endif /* __STDC__ */
                   1310: {
1.67      cvs      1311:     STRING  newname = NULL;
                   1312:     STRING  port;
                   1313:     STRING  strptr;
                   1314:     STRING  path;
                   1315:     STRING  access = host-3;
                   1316:     CHAR_T  used_sep;
1.32      cvs      1317: 
                   1318:   
1.67      cvs      1319:      if (*filename && ustrchr (*filename, URL_SEP))
1.33      cvs      1320:        {
                   1321:         used_sep = URL_SEP;
                   1322:        }
                   1323:      else
                   1324:        {
                   1325:         used_sep = DIR_SEP;
                   1326:        }
1.32      cvs      1327:   
1.33      cvs      1328:     while (access>*filename && *(access-1)!= used_sep)       /* Find access method */
1.25      cvs      1329:        access--;
1.67      cvs      1330:     if ((path = ustrchr(host, used_sep)) == NULL)                      /* Find path */
                   1331:        path = host + ustrlen(host);
                   1332:     if ((strptr = ustrchr(host, TEXT('@'))) != NULL && strptr<path)       /* UserId */
1.25      cvs      1333:        host = strptr;
1.67      cvs      1334:     if ((port = ustrchr(host, TEXT(':'))) != NULL && port>path)      /* Port number */
1.25      cvs      1335:        port = NULL;
                   1336: 
                   1337:     strptr = host;                                 /* Convert to lower-case */
1.33      cvs      1338:     while (strptr<path)
                   1339:       {
1.25      cvs      1340:        *strptr = tolower(*strptr);
                   1341:        strptr++;
1.33      cvs      1342:       }
1.25      cvs      1343:     
                   1344:     /* Does the URL contain a full domain name? This also works for a
                   1345:        numerical host name. The domain name is already made lower-case
                   1346:        and without a trailing dot. */
                   1347:     {
1.67      cvs      1348:       STRING dot = port ? port : path;
                   1349:       if (dot > *filename && *--dot == TEXT('.'))
1.33      cvs      1350:        {
1.67      cvs      1351:          STRING orig=dot, dest=dot+1;
1.33      cvs      1352:          while((*orig++ = *dest++));
                   1353:          if (port) port--;
                   1354:          path--;
1.25      cvs      1355:        }
                   1356:     }
                   1357:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1358:     if (port)
                   1359:       {
                   1360:        if (!*(port+1) || *(port+1)==used_sep)
                   1361:          {
                   1362:            if (!newname)
                   1363:              {
1.67      cvs      1364:                STRING orig=port, dest=port+1;
1.25      cvs      1365:                while((*orig++ = *dest++));
1.33      cvs      1366:              }
                   1367:          }
1.67      cvs      1368:        else if ((!ustrncmp(access, TEXT("http"), 4) &&
                   1369:                  (*(port+1) == TEXT('8') && *(port+2) == TEXT('0') && (*(port+3) == used_sep || !*(port + 3)))) ||
                   1370:                 (!ustrncmp (access, TEXT("gopher"), 6) &&
                   1371:                  (*(port+1) == TEXT('7') && *(port+2) == TEXT('0') && (*(port+3) == used_sep || !*(port+3)))) ||
                   1372:                 (!ustrncmp(access, TEXT("ftp"), 3) &&
                   1373:                  (*(port+1) == TEXT('2') && *(port + 2) == TEXT('1') && (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1374:          if (!newname)
                   1375:            {
1.67      cvs      1376:              STRING orig=port, dest=port+3;
1.33      cvs      1377:              while((*orig++ = *dest++));
                   1378:              /* Update path position, Henry Minsky */
                   1379:              path -= 3;
1.25      cvs      1380:            }
1.33      cvs      1381:        }
                   1382:        else if (newname)
1.67      cvs      1383:          ustrncat(newname, port, (int) (path-port));
1.33      cvs      1384:       }
1.25      cvs      1385: 
1.33      cvs      1386:     if (newname)
                   1387:       {
1.67      cvs      1388:        STRING newpath = newname + ustrlen (newname);
                   1389:        ustrcat(newname, path);
1.25      cvs      1390:        path = newpath;
1.28      cvs      1391:        /* Free old copy */
                   1392:        TtaFreeMemory(*filename);
1.25      cvs      1393:        *filename = newname;
1.33      cvs      1394:       }
1.25      cvs      1395:     return path;
                   1396: }
                   1397: 
                   1398: 
                   1399: /*----------------------------------------------------------------------
1.29      cvs      1400:   SimplifyUrl: simplify a URI
1.32      cvs      1401:   A URI is allowed to contain the sequence xxx/../ which may be
                   1402:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1403:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1404:   
1.28      cvs      1405:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1406:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1407:   
1.28      cvs      1408:   but we should NOT change
                   1409:                 http://fred.xxx.edu/../..
1.25      cvs      1410:   
                   1411:        or      ../../albert.html
                   1412:   
1.28      cvs      1413:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1414:   
                   1415:                /fred/..                becomes /fred/..
                   1416:                /fred/././..            becomes /fred/..
                   1417:                /fred/.././junk/.././   becomes /fred/..
                   1418:   
1.28      cvs      1419:   If more than one set of `://' is found (several proxies in cascade) then
                   1420:   only the part after the last `://' is simplified.
1.25      cvs      1421:   
1.28      cvs      1422:   Returns: A string which might be the old one or a new one.
1.25      cvs      1423:   ----------------------------------------------------------------------*/
                   1424: #ifdef __STDC__
1.67      cvs      1425: void         SimplifyUrl (STRING* url)
1.25      cvs      1426: #else  /* __STDC__ */
1.29      cvs      1427: void         SimplifyUrl (url)
1.67      cvs      1428: STRING       *url;
1.25      cvs      1429: #endif /* __STDC__ */
                   1430: {
1.67      cvs      1431:   STRING   path, p;
                   1432:   STRING   newptr, access;
                   1433:   STRING   orig, dest, end;
1.28      cvs      1434: 
1.67      cvs      1435:   CHAR_T   used_sep;
1.32      cvs      1436: 
                   1437: 
1.28      cvs      1438:   if (!url || !*url)
                   1439:     return;
                   1440: 
1.67      cvs      1441:   if (ustrchr (*url, URL_SEP))
1.33      cvs      1442:     {
                   1443:       used_sep = URL_SEP;
                   1444:     }
1.32      cvs      1445:   else
1.33      cvs      1446:     {
                   1447:       used_sep = DIR_SEP;
                   1448:     }
1.32      cvs      1449: 
1.28      cvs      1450:   /* Find any scheme name */
1.67      cvs      1451:   if ((path = ustrstr(*url, TEXT("://"))) != NULL)
1.33      cvs      1452:     {
                   1453:       /* Find host name */
1.28      cvs      1454:       access = *url;
                   1455:       while (access<path && (*access=tolower(*access)))
                   1456:        access++;
                   1457:       path += 3;
1.67      cvs      1458:       while ((newptr = ustrstr(path, TEXT("://"))) != NULL)
1.28      cvs      1459:         /* For proxies */
                   1460:        path = newptr+3;
                   1461:       /* We have a host name */
                   1462:       path = HTCanon(url, path);
1.25      cvs      1463:     }
1.67      cvs      1464:   else if ((path = ustrstr(*url, TEXT(":/"))) != NULL)
1.28      cvs      1465:     path += 2;
                   1466:   else
                   1467:     path = *url;
1.25      cvs      1468: 
1.33      cvs      1469:   if (*path == used_sep && *(path+1)==used_sep)
1.28      cvs      1470:     /* Some URLs start //<foo> */
                   1471:     path += 1;
1.67      cvs      1472:   else if (!ustrncmp(path, TEXT("news:"), 5))
1.28      cvs      1473:     {
1.67      cvs      1474:       newptr = ustrchr(path+5, TEXT('@'));
1.28      cvs      1475:       if (!newptr)
                   1476:        newptr = path + 5;
                   1477:       while (*newptr)
                   1478:        {
                   1479:          /* Make group or host lower case */
                   1480:          *newptr = tolower (*newptr);
                   1481:          newptr++;
1.25      cvs      1482:        }
1.28      cvs      1483:       /* Doesn't need to do any more */
                   1484:       return;
1.25      cvs      1485:     }
1.28      cvs      1486: 
                   1487:   if ((p = path))
                   1488:     {
1.67      cvs      1489:       if (!((end = ustrchr (path, TEXT(';'))) || (end = ustrchr (path, TEXT('?'))) ||
                   1490:            (end = ustrchr (path, TEXT('#')))))
                   1491:        end = path + ustrlen (path);
1.28      cvs      1492:       
                   1493:       /* Parse string second time to simplify */
                   1494:       p = path;
                   1495:       while (p < end)
                   1496:        {
1.33      cvs      1497:          if (*p==used_sep)
1.28      cvs      1498:            {
1.67      cvs      1499:              if (p > *url && *(p+1) == TEXT('.') && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1500:                {
                   1501:                  orig = p + 1;
1.33      cvs      1502:                  dest = (*(p+2)!=used_sep) ? p+2 : p+3;
1.52      cvs      1503:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1504:                  end = orig - 1;
                   1505:                }
1.67      cvs      1506:              else if (*(p+1) == TEXT('.') && *(p+2) == TEXT('.') && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1507:                {
                   1508:                  newptr = p;
1.52      cvs      1509:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1510:                  if (*newptr == used_sep)
                   1511:                    orig = newptr + 1;
1.28      cvs      1512:                  else
1.52      cvs      1513:                    orig = newptr;
                   1514: 
                   1515:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1516:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1517:                  end = orig-1;
                   1518:                  /* Start again with prev slash */
                   1519:                  p = newptr;
1.28      cvs      1520:                }
1.33      cvs      1521:              else if (*(p+1) == used_sep)
1.28      cvs      1522:                {
1.33      cvs      1523:                  while (*(p+1) == used_sep)
1.28      cvs      1524:                    {
                   1525:                      orig = p;
                   1526:                      dest = p + 1;
                   1527:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1528:                      end = orig-1;
                   1529:                    }
                   1530:                }
                   1531:              else
1.25      cvs      1532:                p++;
1.28      cvs      1533:            }
                   1534:          else
                   1535:            p++;
1.25      cvs      1536:        }
                   1537:     }
1.51      cvs      1538: 
                   1539:     /*
                   1540:     **  Check for host/../.. kind of things
                   1541:     */
1.67      cvs      1542:     if (*path == used_sep && *(path+1) == TEXT('.') && *(path+2) == TEXT('.') && (!*(path+3) || *(path+3) == used_sep))
1.51      cvs      1543:        *(path+1) = EOS;
                   1544: 
1.28      cvs      1545:   return;
                   1546: }
                   1547: 
                   1548: 
                   1549: /*----------------------------------------------------------------------
                   1550:    NormalizeFile normalizes  local names.                             
                   1551:    Return TRUE if target and src differ.                           
                   1552:   ----------------------------------------------------------------------*/
                   1553: #ifdef __STDC__
1.67      cvs      1554: ThotBool             NormalizeFile (STRING src, STRING target)
1.28      cvs      1555: #else
1.67      cvs      1556: ThotBool             NormalizeFile (src, target)
                   1557: STRING              src;
                   1558: STRING              target;
1.28      cvs      1559: 
                   1560: #endif
                   1561: {
1.67      cvs      1562:    STRING             s;
1.66      cvs      1563:    ThotBool            change;
1.28      cvs      1564: 
1.54      cvs      1565:    change = FALSE;
1.67      cvs      1566:    if (ustrncmp (src, TEXT("file:"), 5) == 0)
1.28      cvs      1567:      {
                   1568:        /* remove the prefix file: */
                   1569:        if (src[5] == EOS)
1.67      cvs      1570:           ustrcpy (target, DIR_STR);
                   1571:        else if (src[0] == TEXT('~'))
1.28      cvs      1572:          {
                   1573:            /* replace ~ */
1.67      cvs      1574:            s = TtaGetEnvString (TEXT("HOME"));
                   1575:            ustrcpy (target, s);
                   1576:            ustrcat (target, &src[5]);
1.28      cvs      1577:          }
                   1578:        else
1.67      cvs      1579:           ustrcpy (target, &src[5]);
1.54      cvs      1580:        change = TRUE;
1.28      cvs      1581:      }
1.53      cvs      1582: #  ifndef _WINDOWS
1.67      cvs      1583:    else if (src[0] == TEXT('~'))
1.53      cvs      1584:      {
                   1585:        /* replace ~ */
                   1586:        s = (char *) TtaGetEnvString ("HOME");
                   1587:        strcpy (target, s);
                   1588:        if (src[1] != DIR_SEP)
                   1589:          strcat (target, DIR_STR);
                   1590:        strcat (target, &src[1]);
1.54      cvs      1591:        change = TRUE;
1.53      cvs      1592:      }
                   1593: #   endif /* _WINDOWS */
1.28      cvs      1594:    else
1.67      cvs      1595:       ustrcpy (target, src);
1.28      cvs      1596: 
                   1597:    /* remove /../ and /./ */
1.29      cvs      1598:    SimplifyUrl (&target);
1.54      cvs      1599:    if (!change)
1.67      cvs      1600:      change = ustrcmp (src, target);
1.28      cvs      1601:    return (change);
1.25      cvs      1602: }
                   1603: 
1.28      cvs      1604: 
1.25      cvs      1605: /*----------------------------------------------------------------------
1.31      cvs      1606:   MakeRelativeURL: make relative name
1.25      cvs      1607:   
1.28      cvs      1608:   This function creates and returns a string which gives an expression of
                   1609:   one address as related to another. Where there is no relation, an absolute
                   1610:   address is retured.
1.25      cvs      1611:   
1.28      cvs      1612:   On entry,
1.25      cvs      1613:        Both names must be absolute, fully qualified names of nodes
                   1614:        (no fragment bits)
                   1615:   
1.28      cvs      1616:   On exit,
1.25      cvs      1617:        The return result points to a newly allocated name which, if
                   1618:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1619:        The caller is responsible for freeing the resulting name later.
                   1620:   ----------------------------------------------------------------------*/
                   1621: #ifdef __STDC__
1.67      cvs      1622: STRING           MakeRelativeURL (STRING aName, STRING relatedName)
1.25      cvs      1623: #else  /* __STDC__ */
1.67      cvs      1624: STRING           MakeRelativeURL (aName, relatedName)
                   1625: STRING           aName;
                   1626: STRING           relatedName;
1.25      cvs      1627: #endif  /* __STDC__ */
                   1628: {
1.67      cvs      1629:   STRING         return_value;
                   1630:   CHAR_T         result[MAX_LENGTH];
                   1631:   STRING         p;
                   1632:   STRING         q;
                   1633:   STRING         after_access;
                   1634:   STRING         last_slash = NULL;
1.29      cvs      1635:   int            slashes, levels, len;
                   1636: 
1.44      cvs      1637: # ifdef _WINDOWS
                   1638:   int ndx;
                   1639: # endif /* _WINDOWS */
                   1640: 
1.29      cvs      1641:   if (aName == NULL || relatedName == NULL)
                   1642:     return (NULL);
                   1643: 
                   1644:   slashes = 0;
                   1645:   after_access = NULL;
                   1646:   p = aName;
                   1647:   q = relatedName;
                   1648:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1649:     {
                   1650:       /* Find extent of match */
1.67      cvs      1651:       if (*p == TEXT(':'))
1.29      cvs      1652:        after_access = p + 1;
1.28      cvs      1653:       if (*p == DIR_SEP)
1.27      cvs      1654:        {
1.29      cvs      1655:          /* memorize the last slash position and count them */
1.27      cvs      1656:          last_slash = p;
                   1657:          slashes++;
1.25      cvs      1658:        }
                   1659:     }
                   1660:     
1.31      cvs      1661:   /* q, p point to the first non-matching character or zero */
                   1662:   if (*q == EOS)
                   1663:     {
                   1664:       /* New name is a subset of the related name */
                   1665:       /* exactly the right length */
1.67      cvs      1666:       len = ustrlen (p);
                   1667:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1668:        ustrcpy (return_value, p);
1.31      cvs      1669:     }
                   1670:   else if ((slashes < 2 && after_access == NULL)
                   1671:       || (slashes < 3 && after_access != NULL))
                   1672:     {
                   1673:       /* Two names whitout common path */
                   1674:       /* exactly the right length */
1.67      cvs      1675:       len = ustrlen (aName);
                   1676:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1677:        ustrcpy (return_value, aName);
1.31      cvs      1678:     }
                   1679:   else
                   1680:     {
                   1681:       /* Some path in common */
1.67      cvs      1682:       if (slashes == 3 && ustrncmp (aName, TEXT("http:"), 5) == 0)
1.31      cvs      1683:        /* just the same server */
1.67      cvs      1684:        ustrcpy (result, last_slash);
1.31      cvs      1685:       else
                   1686:        {
                   1687:          levels= 0; 
1.67      cvs      1688:          for (; *q && *q != TEXT('#') && *q != TEXT(';') && *q != TEXT('?'); q++)
1.31      cvs      1689:            if (*q == DIR_SEP)
                   1690:              levels++;
                   1691:          
1.52      cvs      1692:          result[0] = EOS;
1.31      cvs      1693:          for (;levels; levels--)
1.67      cvs      1694:            ustrcat (result, TEXT("../"));
                   1695:          ustrcat (result, last_slash+1);
1.31      cvs      1696:        } 
1.52      cvs      1697: 
                   1698:       if (!*result)
1.67      cvs      1699:        ustrcat (result, TEXT("./"));
1.52      cvs      1700: 
1.31      cvs      1701:       /* exactly the right length */
1.67      cvs      1702:       len = ustrlen (result);
                   1703:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1704:        ustrcpy (return_value, result);
1.52      cvs      1705: 
1.25      cvs      1706:     }
1.44      cvs      1707: # ifdef _WINDOWS
1.67      cvs      1708:   len = ustrlen (return_value);
1.44      cvs      1709:   for (ndx = 0; ndx < len; ndx ++)
1.67      cvs      1710:          if (return_value[ndx] == TEXT('\\'))
                   1711:             return_value[ndx] = TEXT('/') ;
1.44      cvs      1712: # endif /* _WINDOWS */
1.29      cvs      1713:   return (return_value);
1.24      cvs      1714: }
1.35      cvs      1715: 
                   1716: 

Webmaster