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

1.7       cvs         1: /*
                      2:  *
1.133     vatton      3:  *  (c) COPYRIGHT MIT and INRIA, 1996-2002
1.7       cvs         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.106     cvs        14:  *          R. Guetari: 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"
1.100     kahan      23: #include "query_f.h"
1.8       cvs        24: 
1.24      cvs        25: #define MAX_PRINT_URL_LENGTH 50
1.106     cvs        26: typedef struct _HTURI
                     27: {
                     28:     char *access;              /* Now known as "scheme" */
                     29:     char *host;
                     30:     char *absolute;
                     31:     char *relative;
                     32:     char *fragment;
1.29      cvs        33: } HTURI;
1.24      cvs        34: 
1.28      cvs        35: 
                     36: /*----------------------------------------------------------------------
                     37:   ConvertToLowerCase
                     38:   Converts a string to lowercase.
                     39:   ----------------------------------------------------------------------*/
1.124     vatton     40: void ConvertToLowerCase (char *string)
1.28      cvs        41: {
                     42:  int i;
1.93      cvs        43:  
1.28      cvs        44:  if (!string)
                     45:    return;
                     46: 
1.106     cvs        47:  for (i = 0; string[i] != EOS; i++)
1.123     vatton     48:    string[i] = tolower (string[i]);
1.28      cvs        49: }
1.22      cvs        50: 
1.8       cvs        51: /*----------------------------------------------------------------------
1.75      cvs        52:   EscapeChar
                     53:   writes the equivalent escape code of a char in a string
                     54:   ----------------------------------------------------------------------*/
1.109     cvs        55: void EscapeChar (char *string, char c)
1.75      cvs        56: {
1.109     cvs        57:   unsigned int i;
                     58: 
                     59:    i = (unsigned char) c & 0xFF;
                     60:    sprintf (string, "%02x", i);
1.75      cvs        61: }
                     62: 
                     63: /*----------------------------------------------------------------------
1.96      cvs        64:   UnEscapeChar
                     65:   writes the equivalent hex code to a %xx coded char
                     66:   ----------------------------------------------------------------------*/
1.109     cvs        67: static char UnEscapeChar (char c)
1.96      cvs        68: {
1.106     cvs        69:     return  c >= '0' && c <= '9' ?  c - '0'
                     70:             : c >= 'A' && c <= 'F' ? c - 'A' + 10
                     71:             : c - 'a' + 10;   /* accept small letters just in case */
1.96      cvs        72: }
                     73: 
                     74: /*----------------------------------------------------------------------
1.75      cvs        75:   EscapeURL
                     76:   Takes a URL and escapes all protected chars into
                     77:   %xx sequences. Also, removes any leading white spaces
                     78:   Returns either NULL or a new buffer, which must be freed by the caller
                     79:   ----------------------------------------------------------------------*/
1.106     cvs        80: char *EscapeURL (const char *url)
                     81: {
                     82:   char *buffer;
                     83:   int   buffer_len;
                     84:   int   buffer_free_mem;
                     85:   char *ptr;
                     86:   int   new_chars;
1.75      cvs        87:   void *status;
                     88: 
                     89:   if (url && *url)
                     90:     {
1.106     cvs        91:       buffer_free_mem = strlen (url) + 20;
                     92:       buffer = TtaGetMemory (buffer_free_mem + 1);
1.107     kahan      93:       ptr = (char *) url;
1.75      cvs        94:       buffer_len = 0;
                     95: 
                     96:       while (*ptr)
                     97:         {
                     98:           switch (*ptr)
                     99:             {
                    100:               /* put here below all the chars that need to
                    101:                  be escaped into %xx */
1.81      cvs       102:             case 0x20: /* space */
1.143     vatton    103:              /*case 0x26:*/ /* &amp */
1.140     kahan     104:             case 0x27: /* antislash */
1.75      cvs       105:               new_chars = 3; 
                    106:               break;
                    107: 
                    108:             default:
1.122     kahan     109:              if ((unsigned char )*ptr > 127)
                    110:                new_chars = 3;
                    111:              else
                    112:                new_chars = 1; 
1.75      cvs       113:               break;
                    114:             }
                    115: 
                    116:           /* see if we need extra room in the buffer */
                    117:           if (new_chars > buffer_free_mem)
                    118:             {
1.76      cvs       119:               buffer_free_mem = 20;
1.106     cvs       120:               status = TtaRealloc (buffer, sizeof (char) 
1.75      cvs       121:                                   * (buffer_len + buffer_free_mem + 1));
                    122:               if (status)
1.114     cvs       123:                 buffer = (char *) status;
1.106     cvs       124:               else
                    125:                {
                    126:                  /* @@ maybe we should do some other behavior here, like
                    127:                     freeing the buffer and return a void thing */
                    128:                  buffer[buffer_len] = EOS;
                    129:                  break;
                    130:                }
1.75      cvs       131:             }
                    132:          /* escape the char */
1.140     kahan     133:          if (new_chars == 3)
                    134:            {
1.106     cvs       135:               buffer[buffer_len] = '%';
1.75      cvs       136:               EscapeChar (&buffer[buffer_len+1], *ptr);
                    137:             }
1.140     kahan     138:           else
                    139:             buffer[buffer_len] = *ptr;
                    140: 
                    141:           /* update the status */
                    142:           buffer_len += new_chars;
                    143:           buffer_free_mem -= new_chars;
                    144:           /* examine the next char */
                    145:           ptr++;
                    146:         }
                    147:       buffer[buffer_len] = EOS;
                    148:     }
                    149:   else
                    150:     buffer = NULL;
                    151: 
                    152:   return (buffer);
                    153: }
                    154: 
                    155: /*----------------------------------------------------------------------
                    156:   EscapeXML
                    157:   Takes a string and escapes all protected chars into entity
                    158:   sequences.
                    159:   Returns either NULL or a new buffer, which must be freed by the caller
                    160:   ----------------------------------------------------------------------*/
                    161: char *EscapeXML (const char *string)
                    162: {
                    163:   char *buffer;
                    164:   int   buffer_len;
                    165:   int   buffer_free_mem;
                    166:   char *ptr;
                    167:   char *entity = NULL;
                    168:   int   new_chars;
                    169:   void *status;
                    170: 
                    171:   if (string && *string)
                    172:     {
                    173:       buffer_free_mem = strlen (string) + 20;
                    174:       buffer = TtaGetMemory (buffer_free_mem + 1);
                    175:       ptr = (char *) string;
                    176:       buffer_len = 0;
                    177: 
                    178:       while (*ptr)
                    179:         {
                    180:           switch (*ptr)
                    181:             {
                    182:              case 0x26: /* &amp */
                    183:               entity = "&amp;";
1.141     kahan     184:               new_chars = sizeof (entity) - 1;     
                    185:               break;
1.140     kahan     186:               
1.141     kahan     187:            case '<':  /* &lt; */
1.140     kahan     188:              entity = "&lt;";
1.141     kahan     189:              new_chars = sizeof (entity) - 1;      
1.140     kahan     190:              break;
                    191: 
1.141     kahan     192:            case '>':  /* &gt; */
1.140     kahan     193:              entity = "&gt;";
1.141     kahan     194:              new_chars = sizeof (entity) - 1;      
                    195:              break;
                    196: 
                    197:            case '"':  /* &quote; */
                    198:              entity = "&quote;";
                    199:              new_chars = sizeof (entity) - 1;      
1.140     kahan     200:              break;
                    201: 
                    202:             default:
                    203:              new_chars = 1; 
                    204:               break;
                    205:             }
                    206: 
                    207:           /* see if we need extra room in the buffer */
                    208:           if (new_chars > buffer_free_mem)
                    209:             {
                    210:               buffer_free_mem = 20;
                    211:               status = TtaRealloc (buffer, sizeof (char) 
                    212:                                   * (buffer_len + buffer_free_mem + 1));
                    213:               if (status)
                    214:                 buffer = (char *) status;
                    215:               else
                    216:                {
                    217:                  /* @@ maybe we should do some other behavior here, like
                    218:                     freeing the buffer and return a void thing */
                    219:                  buffer[buffer_len] = EOS;
                    220:                  break;
                    221:                }
                    222:             }
                    223:          /* escape the char */
                    224:          if (entity)
                    225:            {
                    226:              sprintf (&buffer[buffer_len], "%s", entity);
                    227:              entity = NULL;
                    228:            }
1.75      cvs       229:           else
                    230:             buffer[buffer_len] = *ptr;
                    231: 
                    232:           /* update the status */
                    233:           buffer_len += new_chars;
                    234:           buffer_free_mem -= new_chars;
                    235:           /* examine the next char */
                    236:           ptr++;
                    237:         }
1.106     cvs       238:       buffer[buffer_len] = EOS;
1.75      cvs       239:     }
1.76      cvs       240:   else
                    241:     buffer = NULL;
                    242: 
1.75      cvs       243:   return (buffer);
1.122     kahan     244: }
                    245: 
1.75      cvs       246: 
                    247: /*----------------------------------------------------------------------
1.11      cvs       248:   ExplodeURL 
1.8       cvs       249:   ----------------------------------------------------------------------*/
1.106     cvs       250: void ExplodeURL (char *url, char **proto, char **host, char **dir,
                    251:                 char **file)
1.8       cvs       252: {
1.33      cvs       253:    char            *curr, *temp;
                    254:    char             used_sep;
1.32      cvs       255: 
1.33      cvs       256:    if (url && strchr (url, URL_SEP))
                    257:      used_sep = URL_SEP;
                    258:    else
                    259:      used_sep = DIR_SEP;
1.8       cvs       260: 
                    261:    if ((url == NULL) || (proto == NULL) || (host == NULL) ||
                    262:        (dir == NULL) || (file == NULL))
                    263:       return;
                    264: 
                    265:    /* initialize every pointer */
                    266:    *proto = *host = *dir = *file = NULL;
                    267: 
                    268:    /* skip any leading space */
                    269:    while ((*url == SPACE) || (*url == TAB))
                    270:       url++;
1.9       cvs       271:    curr = url;
                    272:    if (*curr == 0)
1.8       cvs       273:       goto finished;
                    274: 
                    275:    /* go to the end of the URL */
1.68      cvs       276:    while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
                    277:          (*curr != __CR__) && (*curr != EOL))
1.9       cvs       278:       curr++;
1.8       cvs       279: 
                    280:    /* mark the end of the chain */
1.9       cvs       281:    *curr = EOS;
                    282:    curr--;
                    283:    if (curr <= url)
1.8       cvs       284:       goto finished;
                    285: 
                    286:    /* search the next DIR_SEP indicating the beginning of the file name */
                    287:    do
1.11      cvs       288:      curr--;
1.33      cvs       289:    while ((curr >= url) && (*curr != used_sep));
1.11      cvs       290: 
1.9       cvs       291:    if (curr < url)
1.8       cvs       292:       goto finished;
1.9       cvs       293:    *file = curr + 1;
1.8       cvs       294: 
                    295:    /* mark the end of the dir */
1.9       cvs       296:    *curr = EOS;
                    297:    curr--;
                    298:    if (curr < url)
1.8       cvs       299:       goto finished;
                    300: 
1.29      cvs       301:    /* search for the DIR_STR indicating the host name start */
1.33      cvs       302:    while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9       cvs       303:       curr--;
1.8       cvs       304: 
                    305:    /* if we found it, separate the host name from the directory */
1.102     kahan     306:    if ((*curr == used_sep) && (*(curr + 1) == used_sep))
1.8       cvs       307:      {
1.9       cvs       308:        *host = temp = curr + 2;
1.33      cvs       309:        while ((*temp != 0) && (*temp != used_sep))
1.8       cvs       310:           temp++;
1.33      cvs       311:        if (*temp == used_sep)
1.8       cvs       312:          {
                    313:             *temp = EOS;
                    314:             *dir = temp + 1;
                    315:          }
                    316:      }
                    317:    else
1.11      cvs       318:      *dir = curr;
                    319: 
1.9       cvs       320:    if (curr <= url)
1.8       cvs       321:       goto finished;
                    322: 
                    323:    /* mark the end of the proto */
1.9       cvs       324:    *curr = EOS;
                    325:    curr--;
                    326:    if (curr < url)
1.8       cvs       327:       goto finished;
                    328: 
1.106     cvs       329:    if (*curr == ':')
1.8       cvs       330:      {
1.9       cvs       331:        *curr = EOS;
                    332:        curr--;
1.8       cvs       333:      }
                    334:    else
                    335:       goto finished;
1.11      cvs       336: 
1.9       cvs       337:    if (curr < url)
1.8       cvs       338:       goto finished;
1.9       cvs       339:    while ((curr > url) && (isalpha (*curr)))
                    340:       curr--;
                    341:    *proto = curr;
1.8       cvs       342: 
                    343:  finished:;
                    344: 
                    345: #ifdef AMAYA_DEBUG
                    346:    fprintf (stderr, "ExplodeURL(%s)\n\t", url);
                    347:    if (*proto)
                    348:       fprintf (stderr, "proto : %s, ", *proto);
                    349:    if (*host)
                    350:       fprintf (stderr, "host : %s, ", *host);
                    351:    if (*dir)
                    352:       fprintf (stderr, "dir : %s, ", *dir);
                    353:    if (*file)
                    354:       fprintf (stderr, "file : %s ", *file);
                    355:    fprintf (stderr, "\n");
                    356: #endif
                    357: 
                    358: }
1.3       cvs       359: 
1.116     kahan     360: /*----------------------------------------------------------------------
                    361:    PicTypeToMime
                    362:    Converts a Thot PicType into the equivalent MIME type. If no convertion
                    363:    is possible, it returns NULL.
                    364:   ----------------------------------------------------------------------*/
                    365: char *PicTypeToMIME (PicType contentType)
                    366: {
                    367:   char *mime_type;
                    368:   
                    369:   switch (contentType)
                    370:     {
                    371:     case xbm_type:
                    372:       mime_type ="image/x-xbitmap";
                    373:       break;
                    374:     case eps_type:
                    375:       mime_type ="application/postscript";
                    376:       break;
                    377:    case xpm_type:
                    378:       mime_type ="image/x-xpicmap";
                    379:      break;
                    380:     case gif_type:
                    381:       mime_type ="image/gif";
                    382:       break;
                    383:     case jpeg_type:
                    384:       mime_type ="image/jpeg";
                    385:       break;
                    386:     case png_type:
                    387:       mime_type ="image/png";
                    388:       break;
                    389:     case svg_type:
1.138     kahan     390:       mime_type =AM_SVG_MIME_TYPE;
1.116     kahan     391:       break;
                    392:    case unknown_type:
                    393:    default:
                    394:      mime_type = NULL;
                    395:    }
                    396: 
                    397:   return mime_type;
                    398: }
1.61      cvs       399: 
                    400: /*----------------------------------------------------------------------
1.117     kahan     401:    ImageElement
                    402:    Returns the element (image parameter) and URL (url parameter) of an
                    403:    image in a docImage document. The user must free the memory associated
1.120     kahan     404:    with the url parameter if the function is succesful. 
                    405:    If the url parameter is NULL, we won't initialize it.
1.117     kahan     406:    Returns TRUE if succesful, FALSE otherwise.
                    407:   ----------------------------------------------------------------------*/
                    408: ThotBool ImageElement (Document doc, char **url, Element *image)
                    409: {
                    410:   Element             el, imgEl;
                    411:   Attribute           attr, srcAttr;
                    412:   AttributeType       attrType;
                    413:   int                 length;
                    414:   char               *value;
                    415: 
                    416:   if (DocumentTypes[doc] != docImage)
                    417:     return FALSE;
                    418: 
                    419:   /* find the value of the src attribute */
                    420:   attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
                    421:   attrType.AttrTypeNum = HTML_ATTR_SRC;
                    422:   el = TtaGetRootElement (doc);
                    423:   TtaSearchAttribute (attrType, SearchInTree, el, &imgEl, &srcAttr);
                    424: 
                    425:   if (!imgEl)
                    426:     return FALSE;
                    427:   *image = imgEl;
                    428: 
1.120     kahan     429:   if (url)
                    430:     {
                    431:       attr = TtaGetAttribute (imgEl, attrType);
                    432:       length = TtaGetTextAttributeLength (srcAttr) + 1;
                    433:       value = TtaGetMemory (length);
                    434:       TtaGiveTextAttributeValue (srcAttr, value, &length);
                    435:       *url = value;
                    436:     }
1.117     kahan     437:   return TRUE;
                    438: }
                    439: 
                    440: /*----------------------------------------------------------------------
                    441:    DocImageMimeType
                    442:    Returns the MIME type of a docImage document.
                    443:   ----------------------------------------------------------------------*/
                    444: char *DocImageMimeType (Document doc)
                    445: {
                    446:   char *mime_type;
                    447:   LoadedImageDesc *pImage;
                    448:   PicType type;
                    449:   Element image;
                    450: 
                    451:   if (DocumentTypes[doc] != docImage)
                    452:     return NULL;
                    453: 
                    454:   mime_type = NULL;
                    455:   if (!IsHTTPPath (DocumentURLs[doc]))
                    456:     {
                    457:       /* it is a local image */
1.120     kahan     458:       if (ImageElement (doc, NULL, &image))
1.117     kahan     459:        {
                    460:          type = TtaGetPictureType (image);
                    461:          mime_type = PicTypeToMIME (type);
                    462:        }
                    463:     }
                    464:   else
                    465:     {
                    466:       /* find the value of the src attribute */
                    467:       pImage = ImageURLs;
                    468:       while (pImage != NULL)
                    469:        {
                    470:          if (pImage->document == doc)
                    471:            {
                    472:              if (pImage->content_type)
                    473:                mime_type = pImage->content_type;
                    474:              else if (pImage->elImage && pImage->elImage->currentElement)
                    475:                {
                    476:                  type = TtaGetPictureType (pImage->elImage->currentElement);
                    477:                  mime_type = PicTypeToMIME (type);
                    478:                }
                    479:              break;
                    480:            }  
                    481:        }
                    482:     }
                    483:   return (mime_type);
                    484: }
                    485: 
1.4       cvs       486: /*----------------------------------------------------------------------
1.9       cvs       487:   IsHTMLName                                                         
                    488:   returns TRUE if path points to an HTML resource.
1.4       cvs       489:   ----------------------------------------------------------------------*/
1.109     cvs       490: ThotBool IsHTMLName (const char *path)
1.106     cvs       491: {
1.136     cvs       492:   char      temppath[MAX_LENGTH];
                    493:   char      suffix[MAX_LENGTH];
                    494:   char      nsuffix[MAX_LENGTH];
                    495:   int       i; 
1.5       cvs       496: 
1.101     cvs       497:   if (!path)
                    498:     return (FALSE);
1.5       cvs       499: 
1.106     cvs       500:   strcpy (temppath, path);
1.124     vatton    501:   TtaExtractSuffix (temppath, suffix);
1.101     cvs       502:   i = 0;
1.106     cvs       503:   while (suffix[i] != EOS)
1.101     cvs       504:     {
                    505:       /* Normalize the suffix */
                    506:       i = 0;
1.106     cvs       507:       while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101     cvs       508:        {
1.123     vatton    509:          nsuffix[i] = tolower (suffix[i]);
1.101     cvs       510:          i++;
                    511:        }
1.106     cvs       512:       nsuffix[i] = EOS;
                    513:       if (!strcmp (nsuffix, "html") ||
                    514:          !strcmp (nsuffix, "htm") ||
                    515:          !strcmp (nsuffix, "shtml") ||
                    516:          !strcmp (nsuffix, "jsp") ||
                    517:          !strcmp (nsuffix, "xht") ||
                    518:          !strcmp (nsuffix, "xhtm") ||
1.144     cvs       519:          !strcmp (nsuffix, "lhtml") ||
1.106     cvs       520:          !strcmp (nsuffix, "xhtml"))
1.101     cvs       521:        return (TRUE);
1.106     cvs       522:       else if (!strcmp (nsuffix, "gz"))
1.101     cvs       523:        {
                    524:          /* take into account compressed files */
1.124     vatton    525:          TtaExtractSuffix (temppath, suffix);       
1.101     cvs       526:          /* Normalize the suffix */
                    527:          i = 0;
1.106     cvs       528:          while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101     cvs       529:            {
1.123     vatton    530:              nsuffix[i] = tolower (suffix[i]);
1.101     cvs       531:              i++;
                    532:            }
1.106     cvs       533:          nsuffix[i] = EOS;
                    534:          if (!strcmp (nsuffix, "html") ||
                    535:              !strcmp (nsuffix, "htm") ||
                    536:              !strcmp (nsuffix, "shtml") ||
                    537:              !strcmp (nsuffix, "jsp") ||
                    538:              !strcmp (nsuffix, "xht") ||
                    539:              !strcmp (nsuffix, "xhtm") ||
1.144     cvs       540:              !strcmp (nsuffix, "lhtml") ||
1.106     cvs       541:              !strcmp (nsuffix, "xhtml"))
1.101     cvs       542:            return (TRUE);
                    543:          else
                    544:            return (FALSE);
                    545:        }
                    546:       else
                    547:        /* check if there is another suffix */
1.124     vatton    548:        TtaExtractSuffix (temppath, suffix);
1.101     cvs       549:     }
1.88      cvs       550:    return (FALSE);
1.3       cvs       551: }
                    552: 
1.4       cvs       553: /*----------------------------------------------------------------------
1.136     cvs       554:   IsMathMLName                                                         
                    555:   returns TRUE if path points to an MathML resource.
1.56      cvs       556:   ----------------------------------------------------------------------*/
1.136     cvs       557: ThotBool IsMathMLName (const char *path)
1.56      cvs       558: {
1.136     cvs       559:    char        temppath[MAX_LENGTH];
                    560:    char        suffix[MAX_LENGTH];
1.56      cvs       561: 
                    562:    if (!path)
                    563:       return (FALSE);
                    564: 
1.106     cvs       565:    strcpy (temppath, path);
1.124     vatton    566:    TtaExtractSuffix (temppath, suffix);
1.56      cvs       567: 
1.136     cvs       568:    if (!strcasecmp (suffix, "mml"))
1.56      cvs       569:      return (TRUE);
1.106     cvs       570:    else if (!strcmp (suffix, "gz"))
1.56      cvs       571:      {
                    572:        /* take into account compressed files */
1.124     vatton    573:        TtaExtractSuffix (temppath, suffix);       
1.136     cvs       574:        if (!strcasecmp (suffix, "mml"))
1.60      cvs       575:         return (TRUE);
                    576:        else
                    577:         return (FALSE);
                    578:      }
                    579:    else
                    580:      return (FALSE);
                    581: }
                    582: 
                    583: /*----------------------------------------------------------------------
1.136     cvs       584:   IsSVGName                                                         
                    585:   returns TRUE if path points to an SVG resource.
1.133     vatton    586:   ----------------------------------------------------------------------*/
1.136     cvs       587: ThotBool IsSVGName (const char *path)
1.133     vatton    588: {
1.136     cvs       589:    char        temppath[MAX_LENGTH];
                    590:    char        suffix[MAX_LENGTH];
1.133     vatton    591: 
                    592:    if (!path)
                    593:       return (FALSE);
                    594: 
                    595:    strcpy (temppath, path);
                    596:    TtaExtractSuffix (temppath, suffix);
                    597: 
1.136     cvs       598:    if (!strcasecmp (suffix, "svg"))
1.133     vatton    599:      return (TRUE);
                    600:    else if (!strcmp (suffix, "gz"))
                    601:      {
                    602:        /* take into account compressed files */
                    603:        TtaExtractSuffix (temppath, suffix);       
1.136     cvs       604:        if (!strcasecmp (suffix, "svg"))
1.133     vatton    605:         return (TRUE);
                    606:        else
                    607:         return (FALSE);
                    608:      }
                    609:    else
                    610:      return (FALSE);
                    611: }
                    612: 
                    613: /*----------------------------------------------------------------------
1.136     cvs       614:   IsXMLName                                                         
                    615:   returns TRUE if path points to an XML resource.
1.103     cvs       616:   ----------------------------------------------------------------------*/
1.136     cvs       617: ThotBool IsXMLName (const char *path)
1.103     cvs       618: {
1.136     cvs       619:    char        temppath[MAX_LENGTH];
                    620:    char        suffix[MAX_LENGTH];
1.103     cvs       621: 
                    622:    if (!path)
                    623:       return (FALSE);
                    624: 
1.106     cvs       625:    strcpy (temppath, path);
1.124     vatton    626:    TtaExtractSuffix (temppath, suffix);
1.103     cvs       627: 
1.136     cvs       628:    if (!strcasecmp (suffix, "xml") ||
                    629:        !strcasecmp (suffix, "xht") ||
                    630:        !strcmp (suffix, "xhtm") ||
1.145     kahan     631:        !strcmp (suffix, "xhtml") ||
                    632:        !strcmp (suffix, "smi"))
1.103     cvs       633:      return (TRUE);
1.106     cvs       634:    else if (!strcmp (suffix, "gz"))
1.103     cvs       635:      {
                    636:        /* take into account compressed files */
1.124     vatton    637:        TtaExtractSuffix (temppath, suffix);       
1.136     cvs       638:        if (!strcasecmp (suffix, "xml") ||
                    639:           !strcasecmp (suffix, "xht") ||
                    640:           !strcmp (suffix, "xhtm") ||
1.145     kahan     641:           !strcmp (suffix, "xhtml") ||
                    642:           !strcmp (suffix, "smi"))
1.103     cvs       643:         return (TRUE);
                    644:        else
                    645:         return (FALSE);
                    646:      }
                    647:    else
                    648:      return (FALSE);
                    649: }
                    650: 
                    651: /*----------------------------------------------------------------------
1.136     cvs       652:   IsUndisplayedName                                                         
                    653:   returns TRUE if path points to an undisplayed resource.
1.103     cvs       654:   ----------------------------------------------------------------------*/
1.136     cvs       655: ThotBool IsUndisplayedName (const char *path)
1.103     cvs       656: {
1.106     cvs       657:    char                temppath[MAX_LENGTH];
                    658:    char                suffix[MAX_LENGTH];
1.103     cvs       659: 
                    660:    if (!path)
                    661:       return (FALSE);
                    662: 
1.106     cvs       663:    strcpy (temppath, path);
1.124     vatton    664:    TtaExtractSuffix (temppath, suffix);
1.103     cvs       665: 
1.136     cvs       666:    if (!strcasecmp (suffix, "exe") ||
                    667:        !strcasecmp (suffix, "zip") ||
                    668:        !strcasecmp (suffix, "ppt") ||
                    669:        !strcasecmp (suffix, "pdf") ||
                    670:        !strcasecmp (suffix, "ps")  ||
                    671:        !strcasecmp (suffix, "eps") ||
                    672:        !strcasecmp (suffix, "tar") ||
                    673:        !strcasecmp (suffix, "tgz") ||
                    674:        !strcasecmp (suffix, "ddl") ||
                    675:        !strcasecmp (suffix, "o"))
1.103     cvs       676:      return (TRUE);
1.106     cvs       677:    else if (!strcmp (suffix, "gz"))
1.103     cvs       678:      {
                    679:        /* take into account compressed files */
1.124     vatton    680:        TtaExtractSuffix (temppath, suffix);       
1.136     cvs       681:        if (!strcasecmp (suffix, "exe") ||
                    682:           !strcasecmp (suffix, "zip") ||
                    683:           !strcasecmp (suffix, "ppt") ||
                    684:           !strcasecmp (suffix, "pdf") ||
                    685:           !strcasecmp (suffix, "ps")  ||
                    686:           !strcasecmp (suffix, "eps") ||
                    687:           !strcasecmp (suffix, "tar") ||
                    688:           !strcasecmp (suffix, "ddl") ||
                    689:           !strcasecmp (suffix, "o"))
1.103     cvs       690:         return (TRUE);
                    691:        else
                    692:         return (FALSE);
                    693:      }
                    694:    else
                    695:      return (FALSE);
                    696: }
                    697: 
                    698: /*----------------------------------------------------------------------
1.60      cvs       699:   IsCSSName                                                         
                    700:   returns TRUE if path points to an XML resource.
                    701:   ----------------------------------------------------------------------*/
1.111     cvs       702: ThotBool IsCSSName (const char *path)
1.60      cvs       703: {
1.106     cvs       704:    char                temppath[MAX_LENGTH];
                    705:    char                suffix[MAX_LENGTH];
1.60      cvs       706: 
                    707:    if (!path)
                    708:       return (FALSE);
                    709: 
1.106     cvs       710:    strcpy (temppath, path);
1.124     vatton    711:    TtaExtractSuffix (temppath, suffix);
1.60      cvs       712: 
1.106     cvs       713:    if (!strcasecmp (suffix, "css"))
1.60      cvs       714:      return (TRUE);
1.106     cvs       715:    else if (!strcmp (suffix, "gz"))
1.60      cvs       716:      {
                    717:        /* take into account compressed files */
1.124     vatton    718:        TtaExtractSuffix (temppath, suffix);       
1.106     cvs       719:        if (!strcasecmp (suffix, "css"))
1.56      cvs       720:         return (TRUE);
                    721:        else
                    722:         return (FALSE);
                    723:      }
                    724:    else
                    725:      return (FALSE);
                    726: }
                    727: 
                    728: /*----------------------------------------------------------------------
1.9       cvs       729:   IsImageName                                
                    730:   returns TRUE if path points to an image resource.
1.4       cvs       731:   ----------------------------------------------------------------------*/
1.111     cvs       732: ThotBool IsImageName (const char *path)
1.106     cvs       733: {
                    734:    char                temppath[MAX_LENGTH];
                    735:    char                suffix[MAX_LENGTH];
                    736:    char                nsuffix[MAX_LENGTH];
1.5       cvs       737:    int                 i;
                    738: 
                    739:    if (!path)
1.13      cvs       740:       return (FALSE);
1.5       cvs       741: 
1.106     cvs       742:    strcpy (temppath, path);
1.124     vatton    743:    TtaExtractSuffix (temppath, suffix);
1.5       cvs       744: 
                    745:    /* Normalize the suffix */
                    746:    i = 0;
1.106     cvs       747:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       748:      {
1.123     vatton    749:        nsuffix[i] = tolower (suffix[i]);
1.13      cvs       750:        i++;
                    751:      }
1.106     cvs       752:    nsuffix[i] = EOS;
                    753:    if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
                    754:        (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
                    755:        (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
1.39      cvs       756:       return (TRUE);
                    757:    return (FALSE);
1.3       cvs       758: }
                    759: 
1.4       cvs       760: /*----------------------------------------------------------------------
1.58      cvs       761:   IsImageType                                
                    762:   returns TRUE if type points to an image resource.
                    763:   ----------------------------------------------------------------------*/
1.111     cvs       764: ThotBool IsImageType (const char *type)
1.58      cvs       765: {
1.106     cvs       766:    char                temptype[MAX_LENGTH];
1.58      cvs       767:    int                 i;
                    768: 
                    769:    if (!type)
                    770:       return (FALSE);
                    771: 
1.106     cvs       772:    strcpy (temptype, type);
1.58      cvs       773:    /* Normalize the type */
                    774:    i = 0;
1.106     cvs       775:    while (temptype[i] != EOS)
1.58      cvs       776:      {
                    777:        temptype[i] = tolower (temptype[i]);
                    778:        i++;
                    779:      }
1.111     cvs       780:    if (!strcmp (temptype, "gif") || !strcmp (temptype, "x-xbitmap") ||
                    781:        !strcmp (temptype, "x-xpixmap") || !strcmp (temptype, "jpeg") ||
                    782:        !strcmp (temptype, "png"))
1.58      cvs       783:       return (TRUE);
                    784:    return (FALSE);
                    785: }
                    786: 
                    787: /*----------------------------------------------------------------------
1.9       cvs       788:   IsTextName                                                         
1.4       cvs       789:   ----------------------------------------------------------------------*/
1.111     cvs       790: ThotBool IsTextName (const char *path)
1.106     cvs       791: {
                    792:    char                temppath[MAX_LENGTH];
                    793:    char                suffix[MAX_LENGTH];
                    794:    char                nsuffix[MAX_LENGTH];
1.5       cvs       795:    int                 i;
                    796: 
                    797:    if (!path)
1.13      cvs       798:      return (FALSE);
1.5       cvs       799: 
1.106     cvs       800:    strcpy (temppath, path);
1.124     vatton    801:    TtaExtractSuffix (temppath, suffix);
1.5       cvs       802: 
                    803:    /* Normalize the suffix */
                    804:    i = 0;
1.106     cvs       805:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5       cvs       806:      {
1.25      cvs       807:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       808:        i++;
                    809:      }
1.106     cvs       810:    nsuffix[i] = EOS;
1.5       cvs       811: 
1.111     cvs       812:    if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13      cvs       813:       return (TRUE);
1.106     cvs       814:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       815:      {
1.39      cvs       816:        /* take into account compressed files */
1.124     vatton    817:        TtaExtractSuffix (temppath, suffix);       
1.13      cvs       818:        /* Normalize the suffix */
                    819:        i = 0;
1.106     cvs       820:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       821:         {
1.25      cvs       822:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       823:           i++;
                    824:         }
1.106     cvs       825:        nsuffix[i] = EOS;
1.111     cvs       826:        if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13      cvs       827:         return (TRUE);
                    828:        else
                    829:         return (FALSE);
                    830:      }
                    831:    else
                    832:      return (FALSE);
1.3       cvs       833: }
                    834: 
1.4       cvs       835: /*----------------------------------------------------------------------
1.9       cvs       836:   IsHTTPPath                                     
                    837:   returns TRUE if path is in fact an http URL.
1.4       cvs       838:   ----------------------------------------------------------------------*/
1.112     cvs       839: ThotBool IsHTTPPath (const char *path)
1.3       cvs       840: {
1.5       cvs       841:    if (!path)
                    842:       return FALSE;
1.3       cvs       843: 
1.106     cvs       844:    if ((!strncmp (path, "http:", 5) != 0)
                    845:        || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
                    846:        || !strncmp (path, "internal:", 9))
1.58      cvs       847:       return TRUE;
                    848:    return FALSE;
1.3       cvs       849: }
                    850: 
1.4       cvs       851: /*----------------------------------------------------------------------
1.9       cvs       852:   IsWithParameters                           
                    853:   returns TRUE if url has a concatenated query string.
1.4       cvs       854:   ----------------------------------------------------------------------*/
1.133     vatton    855: ThotBool IsWithParameters (const char *url)
1.3       cvs       856: {
1.5       cvs       857:    int                 i;
1.3       cvs       858: 
1.9       cvs       859:    if ((!url) || (url[0] == EOS))
1.5       cvs       860:       return FALSE;
1.3       cvs       861: 
1.9       cvs       862:    i = strlen (url) - 1;
                    863:    while (i > 0 && url[i--] != '?')
1.5       cvs       864:       if (i < 0)
                    865:         return FALSE;
1.3       cvs       866: 
1.5       cvs       867:    /* There is a parameter */
                    868:    return TRUE;
1.3       cvs       869: }
                    870: 
1.4       cvs       871: /*----------------------------------------------------------------------
1.9       cvs       872:   IsW3Path                                           
                    873:   returns TRUE if path is in fact a URL.
1.4       cvs       874:   ----------------------------------------------------------------------*/
1.133     vatton    875: ThotBool IsW3Path (const char *path)
1.106     cvs       876: {
                    877:   if (strncmp (path, "http:", 5)   && 
                    878:       strncmp (path, "ftp:", 4)    &&
                    879:       strncmp (path, "telnet:", 7) && 
                    880:       strncmp (path, "wais:", 5)   &&
                    881:       strncmp (path, "news:", 5)   && 
                    882:       strncmp (path, "gopher:", 7) &&
                    883:       strncmp (path, "mailto:", 7) && 
1.132     cheyroul  884:       strncmp (path, "archie:", 7) &&
                    885:       strncmp (path, "https:", 6))
1.72      cvs       886:     return FALSE;
                    887:   return TRUE;
1.3       cvs       888: }
                    889: 
1.4       cvs       890: /*----------------------------------------------------------------------
1.90      cvs       891:   IsFilePath                                           
                    892:   returns TRUE if path is in fact a URL.
                    893:   ----------------------------------------------------------------------*/
1.133     vatton    894: ThotBool IsFilePath (const char *path)
1.90      cvs       895: {
1.106     cvs       896:   if (strncmp (path, "file:", 5))
1.90      cvs       897:     return FALSE;
                    898:   return TRUE;
                    899: }
                    900: 
                    901: /*----------------------------------------------------------------------
1.9       cvs       902:   IsValidProtocol                                                    
                    903:   returns true if the url protocol is supported by Amaya.
1.4       cvs       904:   ----------------------------------------------------------------------*/
1.133     vatton    905: ThotBool IsValidProtocol (const char *url)
1.106     cvs       906: {
                    907:    if (!strncmp (url, "http:", 5)
                    908:       || !strncmp (url, "internal:", 9)
                    909:       || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22      cvs       910:        /* experimental */
1.24      cvs       911:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs       912:       return (TRUE);
1.5       cvs       913:    else
1.8       cvs       914:       return (FALSE);
1.3       cvs       915: }
                    916: 
1.31      cvs       917: 
                    918: /*----------------------------------------------------------------------
                    919:    GetBaseURL
                    920:    normalizes orgName according to a base associated with doc, and
                    921:    following the standard URL format rules.
                    922:    The function returns the base used to solve relative URL and SRC:
                    923:       - the base of the document,
                    924:       - or the document path (without document name).
                    925:   ----------------------------------------------------------------------*/
1.106     cvs       926: char  *GetBaseURL (Document doc)
1.31      cvs       927: {
                    928:   Element             el;
                    929:   ElementType         elType;
                    930:   AttributeType       attrType;
                    931:   Attribute           attr;
1.106     cvs       932:   char               *ptr, *basename;
1.31      cvs       933:   int                 length;
1.151   ! kahan     934:   ThotBool            hasDocBase;
1.31      cvs       935: 
1.113     cvs       936:   if (doc == 0 || !DocumentURLs[doc])
1.110     cvs       937:      return NULL;
1.148     kahan     938:   /* the other functions expect basename to have no more than MAX_LENGTH chars */
1.106     cvs       939:   basename = TtaGetMemory (MAX_LENGTH);
1.148     kahan     940:   basename[0] = EOS;
1.31      cvs       941:   length = MAX_LENGTH -1;
1.151   ! kahan     942:   hasDocBase = FALSE;
        !           943: 
        !           944:   /* If the document has a base URL, it has a priority over the headers. */
        !           945:   /*  @@ We need to do this too when we support XML:base */
        !           946: 
        !           947:   /* is it a HTML document ? */
        !           948:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
        !           949:   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
        !           950:     /* it's a HTML document */
        !           951:     {
        !           952:       /* get the document element */
        !           953:       el = TtaGetMainRoot (doc);
        !           954:       /* search the BASE element */
        !           955:       elType.ElTypeNum = HTML_EL_HEAD;
        !           956:       el = TtaSearchTypedElement (elType, SearchForward, el);
        !           957:       if (el)
        !           958:        /* there is a HEAD element */
        !           959:        {
        !           960:          /* look for a BASE element within the HEAD */
        !           961:          elType.ElTypeNum = HTML_EL_BASE;
        !           962:          el = TtaSearchTypedElement (elType, SearchInTree, el);
        !           963:        }
        !           964:       if (el)
        !           965:        {
        !           966:          /*  The document has a BASE element. Get the HREF attribute of the
        !           967:              BASE element */
        !           968:          hasDocBase = TRUE;
        !           969:          attrType.AttrSSchema = elType.ElSSchema;
        !           970:          attrType.AttrTypeNum = HTML_ATTR_HREF_;
        !           971:          attr = TtaGetAttribute (el, attrType);
        !           972:          if (attr)
        !           973:            {
        !           974:              /* Use the base path of the document */
        !           975:              TtaGiveTextAttributeValue (attr, basename, &length);
        !           976:            }
        !           977:        }
        !           978:     }
        !           979: 
        !           980:   /* there was no BASE. Do we have a location header? */
        !           981:   if (!hasDocBase && DocumentMeta[doc] && DocumentMeta[doc]->full_content_location
1.148     kahan     982:       && DocumentMeta[doc]->full_content_location[0] != EOS)
1.65      cvs       983:     {
1.148     kahan     984:       strncpy (basename, DocumentMeta[doc]->full_content_location, MAX_LENGTH-1);
                    985:       basename[MAX_LENGTH-1] = EOS;
                    986:       length = strlen (basename);
                    987:     }
                    988: 
                    989:   if (basename[0] != EOS)
                    990:     {
                    991:       /* base and orgName have to be separated by a DIR_SEP */
                    992:       length--;
                    993:       if (basename[0] != EOS && basename[length] != URL_SEP &&
                    994:          basename[length] != DIR_SEP) 
                    995:        /* verify if the base has the form "protocol://server:port" */
1.31      cvs       996:        {
1.148     kahan     997:          ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
                    998:                               AMAYA_PARSE_HOST |
                    999:                               AMAYA_PARSE_PUNCTUATION);
                   1000:          if (ptr && !strcmp (ptr, basename))
1.31      cvs      1001:            {
1.148     kahan    1002:              /* it has this form, complete it by adding a URL_STR  */
                   1003:              if (strchr (basename, DIR_SEP))
                   1004:                strcat (basename, DIR_STR);
                   1005:              else
                   1006:                strcat (basename, URL_STR);
                   1007:              length++;
1.31      cvs      1008:            }
1.149     kahan    1009:          else if (!ptr || ptr[0] == EOS)
                   1010:            {
                   1011:              /* no host was detected, we may have a relative URL. We test
                   1012:                 if it begins with a URL_SEP, DIR_SEP or period. If yes, it's
                   1013:                 relative. */
                   1014:              if (! (basename[0] == '.' || basename[0] == URL_SEP 
                   1015:                     || basename[0] == DIR_SEP))
                   1016:                basename[0] = EOS;
                   1017:            }
1.148     kahan    1018:          if (ptr)
                   1019:            TtaFreeMemory (ptr);
1.31      cvs      1020:        }
1.113     cvs      1021:     }
                   1022: 
1.148     kahan    1023:   /* there was no base element and no location header, we use the DocumentURL  */
                   1024:   if (basename[0] == EOS)
                   1025:     {
                   1026:       strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
                   1027:       basename[MAX_LENGTH-1] = EOS;
                   1028:     }
                   1029:   
1.31      cvs      1030:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                   1031:    * then search for the first ":" char, hoping that what's before that is a
                   1032:    * protocol. If found, end the string there. If neither char is found,
                   1033:    * then discard the whole base element.
                   1034:    */
1.106     cvs      1035:   length = strlen (basename) - 1;
1.31      cvs      1036:   /* search for the last DIR_SEP char */
1.106     cvs      1037:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs      1038:     length--;
                   1039:   if (length >= 0)
                   1040:     /* found the last DIR_SEP char, end the string there */
1.106     cvs      1041:     basename[length + 1] = EOS;                   
1.31      cvs      1042:   else
                   1043:     /* search for the first PATH_STR char */
                   1044:     {
1.106     cvs      1045:       for (length = 0; basename[length] != ':' && 
                   1046:             basename[length] != EOS; length ++);
                   1047:       if (basename[length] == ':')
1.31      cvs      1048:        /* found, so end the string there */
1.106     cvs      1049:        basename[length + 1] = EOS;
1.31      cvs      1050:       else
                   1051:        /* not found, discard the base */
1.106     cvs      1052:        basename[0] = EOS;
1.31      cvs      1053:     }
                   1054:   return (basename);
                   1055: }
                   1056: 
                   1057: 
1.4       cvs      1058: /*----------------------------------------------------------------------
1.40      cvs      1059:    GetLocalPath
                   1060:    Allocate and return the local document path associated to the url
                   1061:   ----------------------------------------------------------------------*/
1.150     vatton   1062: char *GetLocalPath (Document doc, char  *url)
1.106     cvs      1063: {
                   1064:   char     *ptr;
                   1065:   char     *n;
                   1066:   char     *documentname;
                   1067:   char      url_sep;
1.83      cvs      1068:   int       len;
1.67      cvs      1069:   ThotBool  noFile;
1.40      cvs      1070: 
                   1071:   if (url != NULL)
                   1072:     {
                   1073:       /* check whether the file name exists */
1.106     cvs      1074:       len = strlen (url) - 1;
1.71      cvs      1075:       if (IsW3Path (url))
1.106     cvs      1076:          url_sep = '/';
1.41      cvs      1077:       else 
1.106     cvs      1078:           url_sep = DIR_SEP;
1.41      cvs      1079:       noFile = (url[len] == url_sep);
1.40      cvs      1080:       if (noFile)
1.106     cvs      1081:          url[len] = EOS;
                   1082:       ptr = TtaGetMemory (MAX_LENGTH);
                   1083:       documentname = TtaGetMemory (MAX_LENGTH);
1.78      cvs      1084:       TtaExtractName (url, ptr, documentname);
1.106     cvs      1085:       sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40      cvs      1086:       if (!TtaCheckDirectory (ptr))
                   1087:        /* directory did not exist */
1.72      cvs      1088:        TtaMakeDirectory (ptr);
1.47      cvs      1089: 
                   1090:       /* don't include the query string within document name */
1.106     cvs      1091:       n = strrchr (documentname, '?');
1.47      cvs      1092:       if (n != NULL)
1.106     cvs      1093:          *n = EOS;
1.46      cvs      1094:       /* don't include ':' within document name */
1.106     cvs      1095:       n = strchr (documentname, ':');
1.46      cvs      1096:       if (n != NULL)
1.106     cvs      1097:          *n = EOS;
1.69      cvs      1098:       /* if after all this operations document name
                   1099:         is empty, let's use noname.html instead */
1.106     cvs      1100:       if (documentname[0] == EOS)
                   1101:          strcat (ptr, "noname.html");
1.69      cvs      1102:       else
1.106     cvs      1103:           strcat (ptr, documentname);
1.40      cvs      1104:       TtaFreeMemory (documentname);
                   1105:       /* restore the url */
                   1106:       if (noFile)
1.41      cvs      1107:        url[len] = url_sep;
1.40      cvs      1108:       return (ptr);
                   1109:     }
                   1110:   else
                   1111:     return (NULL);
                   1112: }
                   1113: 
1.73      cvs      1114: /*----------------------------------------------------------------------
1.79      cvs      1115:    ExtractTarget extract the target name from document nane.        
                   1116:   ----------------------------------------------------------------------*/
1.150     vatton   1117: void ExtractTarget (char *aName, char *target)
1.79      cvs      1118: {
1.106     cvs      1119:    int    lg, i;
                   1120:    char  *ptr;
                   1121:    char  *oldptr;
1.79      cvs      1122: 
                   1123:    if (!target || !aName)
                   1124:      /* bad target */
                   1125:      return;
                   1126: 
1.106     cvs      1127:    target[0] = EOS;
                   1128:    lg = strlen (aName);
1.79      cvs      1129:    if (lg)
                   1130:      {
                   1131:        /* the name is not empty */
                   1132:        oldptr = ptr = &aName[0];
                   1133:        do
                   1134:          {
1.106     cvs      1135:             ptr = strrchr (oldptr, '#');
1.79      cvs      1136:             if (ptr)
                   1137:                oldptr = &ptr[1];
                   1138:          }
                   1139:        while (ptr);
                   1140: 
                   1141:        i = (int) (oldptr) - (int) (aName);     /* name length */
                   1142:        if (i > 1)
                   1143:          {
1.106     cvs      1144:             aName[i - 1] = EOS;
1.79      cvs      1145:             if (i != lg)
1.106     cvs      1146:                strcpy (target, oldptr);
1.79      cvs      1147:          }
                   1148:      }
                   1149: }
                   1150: 
                   1151: /*----------------------------------------------------------------------
1.90      cvs      1152:    RemoveNewLines (text)
                   1153:    Removes any '\n' chars that are found in text. 
                   1154:    Returns TRUE if it did the operation, FALSE otherwise.
1.73      cvs      1155:   ----------------------------------------------------------------------*/
1.106     cvs      1156: ThotBool RemoveNewLines (char *text)
                   1157: {
                   1158:   ThotBool   change = FALSE;
                   1159:   char      *src;
                   1160:   char      *dest;
1.90      cvs      1161: 
                   1162:   src = text;
                   1163:   dest = text;
1.115     kahan    1164: 
                   1165:   /* remove any preceding whitespace */
                   1166:   while (*src && *src == ' ')
                   1167:     {
                   1168:       src++;
                   1169:       change = 1;
                   1170:     }
                   1171:   
1.90      cvs      1172:   while (*src)
                   1173:     {
                   1174:       switch (*src)
                   1175:        {
1.106     cvs      1176:        case '\n':
1.90      cvs      1177:          /* don't copy the newline */
                   1178:          change = 1;
                   1179:          break;
                   1180:        default:
                   1181:          *dest = *src;
                   1182:          dest++;
                   1183:          break;
                   1184:        }
                   1185:       src++;
                   1186:     }
                   1187:   /* copy the last EOS char */
                   1188:   *dest = *src;
                   1189: 
                   1190:   return (change);
                   1191: }
                   1192: 
                   1193: /*----------------------------------------------------------------------
                   1194:    CleanCopyFileURL
                   1195:    Copies a file url from a src string to destination string.
1.97      cvs      1196:    convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
                   1197:    we want to do).
1.90      cvs      1198:   ----------------------------------------------------------------------*/
1.106     cvs      1199: static void CleanCopyFileURL (char *dest, char *src,
                   1200:                              ConvertionType convertion)
1.90      cvs      1201: {
                   1202:   while (*src)
1.89      cvs      1203:     {
1.90      cvs      1204:       switch (*src)
1.89      cvs      1205:        {
                   1206: #ifdef _WINDOWS
1.106     cvs      1207:        case URL_SEP:
1.96      cvs      1208:          /* make DIR_SEP transformation */
1.97      cvs      1209:          if (convertion & AM_CONV_URL_SEP)
1.106     cvs      1210:            *dest = DIR_SEP;
1.96      cvs      1211:          else
                   1212:            *dest = *src;
1.90      cvs      1213:          dest++;
1.96      cvs      1214:          src++;
1.90      cvs      1215:          break;
1.89      cvs      1216: #endif /* _WINDOWS */
1.96      cvs      1217: 
1.106     cvs      1218:        case '%':
1.97      cvs      1219:          if (convertion & AM_CONV_PERCENT)
1.96      cvs      1220:            {
1.97      cvs      1221:              /* (code adapted from libwww's HTUnEscape function */
1.96      cvs      1222:              src++;
1.106     cvs      1223:              if (*src != EOS)
1.97      cvs      1224:                {
                   1225:                  *dest = UnEscapeChar (*src) * 16;
                   1226:                  src++;
                   1227:                }
1.106     cvs      1228:              if (*src != EOS)
1.97      cvs      1229:                {
                   1230:                  *dest = *dest + UnEscapeChar (*src);
                   1231:                  src++;
                   1232:                }
                   1233:              dest++;
1.96      cvs      1234:            }
1.97      cvs      1235:          else
1.96      cvs      1236:            {
1.97      cvs      1237:              *dest = *src;
                   1238:              dest++;
1.96      cvs      1239:              src++;
                   1240:            }
                   1241:          break;
                   1242: 
1.90      cvs      1243:        default:
                   1244:          *dest = *src;
1.89      cvs      1245:          dest++;
1.96      cvs      1246:          src++;
1.90      cvs      1247:          break;
1.89      cvs      1248:        }
                   1249:     }
1.90      cvs      1250:   /* copy the EOS char */
                   1251:   *dest = *src;
1.73      cvs      1252: }
1.40      cvs      1253: 
                   1254: /*----------------------------------------------------------------------
1.9       cvs      1255:    NormalizeURL
                   1256:    normalizes orgName according to a base associated with doc, and
                   1257:    following the standard URL format rules.
1.113     cvs      1258:    if doc is < 0, use as a base the URL of the document that contains
                   1259:    (or contained) the elements that are now in the copy/cut buffer.
1.53      cvs      1260:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                   1261:    other path.
1.9       cvs      1262:    The function returns the new complete and normalized URL 
1.12      cvs      1263:    or file name path (newName) and the name of the document (docName).        
1.9       cvs      1264:    N.B. If the function can't find out what's the docName, it assigns
                   1265:    the name "noname.html".
1.4       cvs      1266:   ----------------------------------------------------------------------*/
1.106     cvs      1267: void NormalizeURL (char *orgName, Document doc, char *newName,
                   1268:                   char *docName, char *otherPath)
                   1269: {
                   1270:    char          *basename;
                   1271:    char           tempOrgName[MAX_LENGTH];
                   1272:    char          *ptr;
                   1273:    char           used_sep;
1.84      cvs      1274:    int            length;
                   1275:    ThotBool       check;
1.5       cvs      1276: 
1.110     cvs      1277: #ifdef _WINDOWS
1.44      cvs      1278:    int ndx;
1.110     cvs      1279: #endif /* _WINDOWS */
1.44      cvs      1280: 
1.5       cvs      1281:    if (!newName || !docName)
                   1282:       return;
1.18      cvs      1283: 
1.113     cvs      1284:    if (doc < 0)
                   1285:      basename = TtaStrdup (SavedDocumentURL);
                   1286:    else if (doc > 0)
1.53      cvs      1287:      basename = GetBaseURL (doc);
                   1288:    else if (otherPath != NULL)
1.108     cvs      1289:      basename = TtaStrdup (otherPath);
1.32      cvs      1290:    else
1.53      cvs      1291:      basename = NULL;
1.32      cvs      1292: 
1.18      cvs      1293:    /*
1.31      cvs      1294:     * Clean orgName
                   1295:     * Make sure we have a complete orgName, without any leading or trailing
                   1296:     * white spaces, or trailinbg new lines
                   1297:     */
1.5       cvs      1298:    ptr = orgName;
1.18      cvs      1299:    /* skip leading white space and new line characters */
1.106     cvs      1300:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                   1301:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                   1302:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs      1303:    /*
1.31      cvs      1304:     * Make orgName a complete URL
                   1305:     * If the URL does not include a protocol, then try to calculate
                   1306:     * one using the doc's base element (if it exists),
                   1307:     */
1.106     cvs      1308:    if (tempOrgName[0] == EOS)
1.53      cvs      1309:      {
1.106     cvs      1310:        newName[0] = EOS;
                   1311:        docName[0] = EOS;
1.53      cvs      1312:        TtaFreeMemory (basename);
                   1313:        return;
                   1314:      }
1.49      cvs      1315: 
                   1316:    /* clean trailing white space */
1.106     cvs      1317:    length = strlen (tempOrgName) - 1;
                   1318:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53      cvs      1319:      {
1.106     cvs      1320:        tempOrgName[length] = EOS;
1.53      cvs      1321:        length--;
                   1322:      }
1.50      cvs      1323: 
1.55      cvs      1324:    /* remove extra dot (which dot???) */
                   1325:    /* ugly, but faster than a strcmp */
1.106     cvs      1326:    if (tempOrgName[length] == '.'
                   1327:        && (length == 0 || tempOrgName[length-1] != '.'))
                   1328:         tempOrgName[length] = EOS;
1.50      cvs      1329: 
1.94      cvs      1330:    if (IsW3Path (tempOrgName))
1.53      cvs      1331:      {
                   1332:        /* the name is complete, go to the Sixth Step */
1.106     cvs      1333:        strcpy (newName, tempOrgName);
1.53      cvs      1334:        SimplifyUrl (&newName);
                   1335:        /* verify if the URL has the form "protocol://server:port" */
1.110     cvs      1336:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
                   1337:                                         AMAYA_PARSE_HOST |
                   1338:                                         AMAYA_PARSE_PUNCTUATION);
                   1339:        if (ptr && !strcmp (ptr, newName))
                   1340:         /* it has this form, we complete it by adding a DIR_STR  */
1.106     cvs      1341:          strcat (newName, URL_STR);
1.49      cvs      1342: 
1.53      cvs      1343:        if (ptr)
1.50      cvs      1344:          TtaFreeMemory (ptr);
1.53      cvs      1345:      }
1.113     cvs      1346:    else if (basename == NULL)
1.53      cvs      1347:      /* the name is complete, go to the Sixth Step */
1.106     cvs      1348:      strcpy (newName, tempOrgName);
1.53      cvs      1349:    else
                   1350:      {
1.31      cvs      1351:        /* Calculate the absolute URL, using the base or document URL */
1.110     cvs      1352: #ifdef _WINDOWS
1.53      cvs      1353:        if (!IsW3Path (basename))
                   1354:         {
1.106     cvs      1355:           length = strlen (tempOrgName);
1.53      cvs      1356:           for (ndx = 0; ndx < length; ndx++)
1.106     cvs      1357:             if (tempOrgName [ndx] == '/')
                   1358:               tempOrgName [ndx] = '\\';
1.53      cvs      1359:         }
1.110     cvs      1360: #endif /* _WINDOWS */
1.25      cvs      1361:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1362:        if (ptr)
                   1363:         {
                   1364:           SimplifyUrl (&ptr);
1.106     cvs      1365:           strcpy (newName, ptr);
1.53      cvs      1366:           TtaFreeMemory (ptr);
                   1367:         }
                   1368:        else
1.106     cvs      1369:         newName[0] = EOS;
1.53      cvs      1370:      }
1.36      cvs      1371: 
                   1372:    TtaFreeMemory (basename);
1.18      cvs      1373:    /*
1.31      cvs      1374:     * Prepare the docname that will refer to this ressource in the
                   1375:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1376:     * noname.html as a default ressource name
1.18      cvs      1377:    */
1.106     cvs      1378:    if (newName[0] != EOS)
1.53      cvs      1379:      {
1.106     cvs      1380:        length = strlen (newName) - 1;
                   1381:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53      cvs      1382:         {
                   1383:           used_sep = newName[length];
                   1384:           check = TRUE;
                   1385:           while (check)
                   1386:             {
1.50      cvs      1387:                length--;
                   1388:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1389:                 length--;
1.106     cvs      1390:                if (!strncmp (&newName[length+1], "..", 2))
1.53      cvs      1391:                 {
1.106     cvs      1392:                   newName[length+1] = EOS;
1.53      cvs      1393:                   /* remove also previous directory */
                   1394:                   length--;
                   1395:                   while (length >= 0 && newName[length] != used_sep)
                   1396:                     length--;
1.106     cvs      1397:                   if (strncmp (&newName[length+1], "//", 2))
1.131     cheyroul 1398:                     /* don't remove server name */
1.106     cvs      1399:                      newName[length+1] = EOS;
1.53      cvs      1400:                 }
1.106     cvs      1401:               else if (!strncmp (&newName[length+1], ".", 1))
                   1402:                 newName[length+1] = EOS;
1.50      cvs      1403:                else
1.53      cvs      1404:                 check = FALSE;
                   1405:             }
                   1406:           /* docname was not comprised inside the URL, so let's */
                   1407:           /* assign the default ressource name */
1.106     cvs      1408:           strcpy (docName, "noname.html");
1.53      cvs      1409:         }
                   1410:        else
                   1411:         { /* docname is comprised inside the URL */
1.110     cvs      1412:            while (length >= 0 && newName[length] != URL_SEP &&
                   1413:                  newName[length] != DIR_SEP)
1.53      cvs      1414:             length--;
                   1415:           if (length < 0)
1.106     cvs      1416:              strcpy (docName, newName);
1.53      cvs      1417:           else
1.106     cvs      1418:             strcpy (docName, &newName[length+1]);
1.53      cvs      1419:         }
                   1420:      }
                   1421:    else
1.106     cvs      1422:      docName[0] = EOS;
1.18      cvs      1423: } 
1.3       cvs      1424: 
1.4       cvs      1425: /*----------------------------------------------------------------------
1.9       cvs      1426:   IsSameHost                                                         
1.4       cvs      1427:   ----------------------------------------------------------------------*/
1.106     cvs      1428: ThotBool IsSameHost (const char *url1, const char *url2)
1.3       cvs      1429: {
1.106     cvs      1430:   char          *basename_ptr1, *basename_ptr2;
                   1431:   ThotBool       result;
1.3       cvs      1432: 
1.106     cvs      1433:   basename_ptr1 = AmayaParseUrl (url1, "",
                   1434:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1435:   basename_ptr2 = AmayaParseUrl (url2, "",
                   1436:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1437: 
1.106     cvs      1438:   if (strcmp (basename_ptr1, basename_ptr2))
                   1439:     result = FALSE;
                   1440:   else
                   1441:     result = TRUE;
                   1442:   TtaFreeMemory (basename_ptr1);
                   1443:   TtaFreeMemory (basename_ptr2);
                   1444:   return (result);
1.3       cvs      1445: }
                   1446: 
                   1447: 
1.4       cvs      1448: /*----------------------------------------------------------------------
1.22      cvs      1449:   HasKnownFileSuffix
                   1450:   returns TRUE if path points to a file ending with a suffix.
                   1451:   ----------------------------------------------------------------------*/
1.106     cvs      1452: ThotBool             HasKnownFileSuffix (const char *path)
                   1453: {
                   1454:    char       *root;
                   1455:    char        temppath[MAX_LENGTH];
                   1456:    char        suffix[MAX_LENGTH];
1.22      cvs      1457: 
1.106     cvs      1458:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs      1459:      return (FALSE);
                   1460: 
1.106     cvs      1461:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1462: 
                   1463:    if (root) 
                   1464:      {
1.106     cvs      1465:        strcpy (temppath, root);
1.25      cvs      1466:        TtaFreeMemory (root);
1.22      cvs      1467:        /* Get the suffix */
1.124     vatton   1468:        TtaExtractSuffix (temppath, suffix); 
1.22      cvs      1469: 
1.106     cvs      1470:        if( suffix[0] == EOS)
1.22      cvs      1471:         /* no suffix */
                   1472:         return (FALSE);
                   1473: 
                   1474:        /* Normalize the suffix */
                   1475:        ConvertToLowerCase (suffix);
                   1476: 
1.106     cvs      1477:        if (!strcmp (suffix, "gz"))
1.22      cvs      1478:         /* skip the compressed suffix */
                   1479:         {
1.124     vatton   1480:         TtaExtractSuffix (temppath, suffix);
1.106     cvs      1481:         if(suffix[0] == EOS)
1.22      cvs      1482:           /* no suffix */
                   1483:           return (FALSE);
                   1484:          /* Normalize the suffix */
                   1485:          ConvertToLowerCase (suffix);
                   1486:         }
                   1487: 
1.106     cvs      1488:        if (strcmp (suffix, "gif") &&
                   1489:           strcmp (suffix, "xbm") &&
                   1490:           strcmp (suffix, "xpm") &&
                   1491:           strcmp (suffix, "jpg") &&
                   1492:           strcmp (suffix, "pdf") &&
                   1493:           strcmp (suffix, "png") &&
                   1494:           strcmp (suffix, "tgz") &&
                   1495:           strcmp (suffix, "xpg") &&
                   1496:           strcmp (suffix, "xpd") &&
                   1497:           strcmp (suffix, "ps") &&
                   1498:           strcmp (suffix, "au") &&
                   1499:           strcmp (suffix, "html") &&
                   1500:           strcmp (suffix, "htm") &&
                   1501:           strcmp (suffix, "shtml") &&
                   1502:           strcmp (suffix, "xht") &&
                   1503:           strcmp (suffix, "xhtm") &&
                   1504:           strcmp (suffix, "xhtml") &&
                   1505:           strcmp (suffix, "txt") &&
                   1506:           strcmp (suffix, "css") &&
                   1507:           strcmp (suffix, "eps"))
1.22      cvs      1508:         return (FALSE);
                   1509:        else
                   1510:         return (TRUE);
                   1511:      }
                   1512:    else
                   1513:      return (FALSE);
                   1514: }
                   1515: 
                   1516: 
                   1517: /*----------------------------------------------------------------------
1.24      cvs      1518:   ChopURL
                   1519:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1520:   If inputURL is  bigger than that size, outputURL receives
                   1521:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1522:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1523:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1524:   copied into outputURL. 
                   1525:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1526:   chars.
                   1527:   ----------------------------------------------------------------------*/
1.106     cvs      1528: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1529: {
                   1530:   int len;
1.9       cvs      1531: 
1.106     cvs      1532:   len = strlen (inputURL);
1.24      cvs      1533:   if (len <= MAX_PRINT_URL_LENGTH) 
1.106     cvs      1534:     strcpy (outputURL, inputURL);
1.24      cvs      1535:   else
                   1536:     /* make a truncated urlName on the status window */
                   1537:     {
1.106     cvs      1538:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1539:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1540:       strcat (outputURL, "...");
                   1541:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24      cvs      1542:     }
1.25      cvs      1543: }
                   1544: 
                   1545: 
                   1546: /*----------------------------------------------------------------------
                   1547:    scan
1.47      cvs      1548:        Scan a filename for its constituents
1.25      cvs      1549:        -----------------------------------
                   1550:   
                   1551:    On entry,
                   1552:        name    points to a document name which may be incomplete.
                   1553:    On exit,
                   1554:         absolute or relative may be nonzero (but not both).
                   1555:        host, fragment and access may be nonzero if they were specified.
                   1556:        Any which are nonzero point to zero terminated strings.
                   1557:   ----------------------------------------------------------------------*/
1.106     cvs      1558: static void scan (char *name, HTURI *parts)
1.25      cvs      1559: {
1.106     cvs      1560:   char *   p;
                   1561:   char *   after_access = name;
1.32      cvs      1562: 
1.43      cvs      1563:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1564:   /* Look for fragment identifier */
1.106     cvs      1565:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs      1566:     {
1.106     cvs      1567:       *p++ = '\0';
1.28      cvs      1568:       parts->fragment = p;
1.25      cvs      1569:     }
                   1570:     
1.28      cvs      1571:   for (p=name; *p; p++)
                   1572:     {
1.106     cvs      1573:       if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28      cvs      1574:        break;
1.106     cvs      1575:       if (*p == ':')
1.28      cvs      1576:        {
                   1577:          *p = 0;
                   1578:          parts->access = after_access; /* Scheme has been specified */
                   1579: 
                   1580:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1581:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1582:             not recommended. Rather, turn off "-O". */
                   1583: 
                   1584:          /*            after_access = p;*/
                   1585:          /*            while (*after_access == 0)*/
                   1586:          /*                after_access++;*/
                   1587:          after_access = p+1;
1.106     cvs      1588:          if (!strcasecmp("URL", parts->access))
1.28      cvs      1589:            /* Ignore IETF's URL: pre-prefix */
                   1590:            parts->access = NULL;
                   1591:          else
1.25      cvs      1592:            break;
                   1593:        }
                   1594:     }
                   1595:     
                   1596:     p = after_access;
1.43      cvs      1597:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1598:       {
1.43      cvs      1599:        if (p[1] == URL_SEP)
1.28      cvs      1600:          {
1.25      cvs      1601:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1602:            *p = 0;                     /* Terminate access             */
                   1603:            /* look for end of host name if any */
1.106     cvs      1604:            p = strchr (parts->host, URL_SEP);
1.28      cvs      1605:            if (p)
                   1606:              {
1.106     cvs      1607:                *p = EOS;                       /* Terminate host */
1.25      cvs      1608:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1609:              }
                   1610:          }
                   1611:        else
                   1612:          /* Root found but no host */
                   1613:          parts->absolute = p+1;
                   1614:       }
                   1615:     else
                   1616:       {
1.25      cvs      1617:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1618:       }
1.25      cvs      1619: }
                   1620: 
                   1621: 
                   1622: /*----------------------------------------------------------------------
1.28      cvs      1623:   AmayaParseUrl: parse a Name relative to another name
                   1624: 
                   1625:   This returns those parts of a name which are given (and requested)
                   1626:   substituting bits from the related name where necessary.
1.25      cvs      1627:   
1.28      cvs      1628:   On entry,
1.25      cvs      1629:        aName           A filename given
                   1630:         relatedName     A name relative to which aName is to be parsed. Give
                   1631:                         it an empty string if aName is absolute.
                   1632:         wanted          A mask for the bits which are wanted.
                   1633:   
1.28      cvs      1634:   On exit,
1.25      cvs      1635:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1636:   ----------------------------------------------------------------------*/
1.106     cvs      1637: char   *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
                   1638: {
                   1639:   char      *return_value;
                   1640:   char       result[MAX_LENGTH];
                   1641:   char       name[MAX_LENGTH];
                   1642:   char       rel[MAX_LENGTH];
                   1643:   char      *p, *access;
1.29      cvs      1644:   HTURI      given, related;
                   1645:   int        len;
1.106     cvs      1646:   char       used_sep;
                   1647:   char      *used_str;
1.32      cvs      1648: 
1.106     cvs      1649:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1650:     {
1.106     cvs      1651:       used_str = DIR_STR;
                   1652:       used_sep = DIR_SEP;
1.33      cvs      1653:     }
1.32      cvs      1654:   else
1.33      cvs      1655:     {
1.106     cvs      1656:       used_str = URL_STR;
                   1657:       used_sep = URL_SEP;
1.33      cvs      1658:     }
1.32      cvs      1659: 
1.29      cvs      1660:   /* Make working copies of input strings to cut up: */
                   1661:   return_value = NULL;
                   1662:   result[0] = 0;               /* Clear string  */
1.106     cvs      1663:   strcpy (name, aName);
1.29      cvs      1664:   if (relatedName != NULL)  
1.106     cvs      1665:     strcpy (rel, relatedName);
1.29      cvs      1666:   else
1.106     cvs      1667:     relatedName[0] = EOS;
1.29      cvs      1668:   
                   1669:   scan (name, &given);
                   1670:   scan (rel,  &related); 
                   1671:   access = given.access ? given.access : related.access;
                   1672:   if (wanted & AMAYA_PARSE_ACCESS)
                   1673:     if (access)
                   1674:       {
1.106     cvs      1675:        strcat (result, access);
1.29      cvs      1676:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1677:                strcat (result, ":");
1.29      cvs      1678:       }
                   1679:   
                   1680:   if (given.access && related.access)
                   1681:     /* If different, inherit nothing. */
1.106     cvs      1682:     if (strcmp (given.access, related.access) != 0)
1.29      cvs      1683:       {
                   1684:        related.host = 0;
                   1685:        related.absolute = 0;
                   1686:        related.relative = 0;
                   1687:        related.fragment = 0;
                   1688:       }
                   1689:   
                   1690:   if (wanted & AMAYA_PARSE_HOST)
                   1691:     if(given.host || related.host)
                   1692:       {
                   1693:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1694:          strcat (result, "//");
                   1695:        strcat (result, given.host ? given.host : related.host);
1.29      cvs      1696:       }
                   1697:   
                   1698:   if (given.host && related.host)
                   1699:     /* If different hosts, inherit no path. */
1.106     cvs      1700:     if (strcmp (given.host, related.host) != 0)
1.29      cvs      1701:       {
                   1702:        related.absolute = 0;
                   1703:        related.relative = 0;
                   1704:        related.fragment = 0;
                   1705:       }
                   1706:   
                   1707:   if (wanted & AMAYA_PARSE_PATH)
                   1708:     {
                   1709:       if (given.absolute)
                   1710:        {
                   1711:          /* All is given */
                   1712:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1713:            strcat (result, used_str);
                   1714:          strcat (result, given.absolute);
1.25      cvs      1715:        }
1.29      cvs      1716:       else if (related.absolute)
                   1717:        {
                   1718:          /* Adopt path not name */
1.106     cvs      1719:          strcat (result, used_str);
                   1720:          strcat (result, related.absolute);
1.29      cvs      1721:          if (given.relative)
                   1722:            {
                   1723:              /* Search part? */
1.106     cvs      1724:              p = strchr (result, '?');
1.29      cvs      1725:              if (!p)
1.106     cvs      1726:                p=result+strlen(result)-1;
1.33      cvs      1727:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1728:              /* Remove filename */
                   1729:              p[1]=0;
                   1730:              /* Add given one */
1.106     cvs      1731:              strcat (result, given.relative);
1.25      cvs      1732:            }
                   1733:        }
1.29      cvs      1734:       else if (given.relative)
                   1735:        /* what we've got */
1.106     cvs      1736:        strcat (result, given.relative);
1.29      cvs      1737:       else if (related.relative)
1.106     cvs      1738:        strcat (result, related.relative);
1.29      cvs      1739:       else
                   1740:        /* No inheritance */
1.106     cvs      1741:        strcat (result, used_str);
1.25      cvs      1742:     }
1.29      cvs      1743:   
                   1744:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1745:     if (given.fragment || related.fragment)
                   1746:       {
                   1747:        if (given.absolute && given.fragment)
                   1748:          {
                   1749:            /*Fixes for relURLs...*/
                   1750:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1751:              strcat (result, "#");
                   1752:            strcat (result, given.fragment); 
1.29      cvs      1753:          }
                   1754:        else if (!(given.absolute) && !(given.fragment))
1.106     cvs      1755:          strcat (result, "");
1.29      cvs      1756:        else
                   1757:          {
1.110     cvs      1758:           if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1759:              strcat (result, "#");
1.110     cvs      1760:           strcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1761:          }
                   1762:       }
1.106     cvs      1763:   len = strlen (result);
                   1764:   if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1765:     strcpy (return_value, result);
1.29      cvs      1766:   return (return_value);               /* exactly the right length */
1.25      cvs      1767: }
                   1768: 
                   1769: /*----------------------------------------------------------------------
                   1770:      HTCanon
                   1771:        Canonicalizes the URL in the following manner starting from the host
                   1772:        pointer:
                   1773:   
                   1774:        1) The host name is converted to lowercase
                   1775:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1776:   
                   1777:        Return: OK      The position of the current path part of the URL
                   1778:                        which might be the old one or a new one.
                   1779:   
                   1780:   ----------------------------------------------------------------------*/
1.106     cvs      1781: static char   *HTCanon (char **filename, char *host)
                   1782: {
                   1783:     char   *newname = NULL;
                   1784:     char    used_sep;
                   1785:     char   *path;
                   1786:     char   *strptr;
                   1787:     char   *port;
                   1788:     char   *access = host-3;
                   1789:   
                   1790:      if (*filename && strchr (*filename, URL_SEP))
                   1791:         used_sep = URL_SEP;
1.33      cvs      1792:      else
1.106     cvs      1793:         used_sep = DIR_SEP;
1.32      cvs      1794:   
1.110     cvs      1795:     while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25      cvs      1796:        access--;
1.110     cvs      1797:     if ((path = strchr (host, used_sep)) == NULL)              /* Find path */
1.106     cvs      1798:        path = host + strlen (host);
                   1799:     if ((strptr = strchr (host, '@')) != NULL && strptr < path)           /* UserId */
1.82      cvs      1800:        host = strptr;
1.110     cvs      1801:     if ((port = strchr (host, ':')) != NULL && port > path)   /* Port number */
1.82      cvs      1802:        port = NULL;
1.25      cvs      1803: 
                   1804:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1805:     while (strptr < path)
1.33      cvs      1806:       {
1.123     vatton   1807:          *strptr = tolower (*strptr);
1.82      cvs      1808:          strptr++;
1.33      cvs      1809:       }
1.25      cvs      1810:     
                   1811:     /* Does the URL contain a full domain name? This also works for a
                   1812:        numerical host name. The domain name is already made lower-case
                   1813:        and without a trailing dot. */
                   1814:     {
1.106     cvs      1815:       char  *dot = port ? port : path;
                   1816:       if (dot > *filename && *--dot == '.')
1.33      cvs      1817:        {
1.106     cvs      1818:          char  *orig = dot;
                   1819:          char  *dest = dot + 1;
1.82      cvs      1820:          while ((*orig++ = *dest++));
                   1821:             if (port) port--;
1.33      cvs      1822:          path--;
1.25      cvs      1823:        }
                   1824:     }
                   1825:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1826:     if (port)
                   1827:       {
1.82      cvs      1828:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      1829:          {
                   1830:            if (!newname)
                   1831:              {
1.106     cvs      1832:                char  *orig = port; 
                   1833:                char  *dest = port + 1;
1.82      cvs      1834:                while ((*orig++ = *dest++));
1.33      cvs      1835:              }
                   1836:          }
1.106     cvs      1837:        else if ((!strncmp (access, "http", 4)   &&
                   1838:              (*(port + 1) == '8'                    && 
                   1839:              *(port+2) == '0'                       && 
1.82      cvs      1840:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.106     cvs      1841:              (!strncmp (access, "gopher", 6) &&
                   1842:              (*(port+1) == '7'                      && 
                   1843:              *(port+2) == '0'                       && 
1.82      cvs      1844:              (*(port+3) == used_sep || !*(port+3))))         ||
1.106     cvs      1845:              (!strncmp (access, "ftp", 3)    &&
                   1846:              (*(port+1) == '2'                      && 
                   1847:              *(port + 2) == '1'                     && 
1.82      cvs      1848:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1849:          if (!newname)
                   1850:            {
1.106     cvs      1851:              char  *orig = port; 
                   1852:              char  *dest = port + 3;
1.33      cvs      1853:              while((*orig++ = *dest++));
                   1854:              /* Update path position, Henry Minsky */
                   1855:              path -= 3;
1.25      cvs      1856:            }
1.33      cvs      1857:        }
                   1858:        else if (newname)
1.106     cvs      1859:          strncat (newname, port, (int) (path - port));
1.33      cvs      1860:       }
1.25      cvs      1861: 
1.33      cvs      1862:     if (newname)
                   1863:       {
1.106     cvs      1864:        char  *newpath = newname + strlen (newname);
                   1865:        strcat (newname, path);
1.25      cvs      1866:        path = newpath;
1.28      cvs      1867:        /* Free old copy */
                   1868:        TtaFreeMemory(*filename);
1.25      cvs      1869:        *filename = newname;
1.33      cvs      1870:       }
1.25      cvs      1871:     return path;
                   1872: }
                   1873: 
                   1874: 
                   1875: /*----------------------------------------------------------------------
1.29      cvs      1876:   SimplifyUrl: simplify a URI
1.32      cvs      1877:   A URI is allowed to contain the sequence xxx/../ which may be
                   1878:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1879:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1880:   
1.28      cvs      1881:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1882:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1883:   
1.28      cvs      1884:   but we should NOT change
                   1885:                 http://fred.xxx.edu/../..
1.25      cvs      1886:   
                   1887:        or      ../../albert.html
                   1888:   
1.28      cvs      1889:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1890:   
                   1891:                /fred/..                becomes /fred/..
                   1892:                /fred/././..            becomes /fred/..
                   1893:                /fred/.././junk/.././   becomes /fred/..
                   1894:   
1.28      cvs      1895:   If more than one set of `://' is found (several proxies in cascade) then
                   1896:   only the part after the last `://' is simplified.
1.25      cvs      1897:   
1.28      cvs      1898:   Returns: A string which might be the old one or a new one.
1.25      cvs      1899:   ----------------------------------------------------------------------*/
1.106     cvs      1900: void         SimplifyUrl (char **url)
                   1901: {
                   1902:   char   *path;
                   1903:   char   *access;
                   1904:   char   *newptr; 
                   1905:   char   *p;
                   1906:   char   *orig, *dest, *end;
1.28      cvs      1907: 
1.106     cvs      1908:   char      used_sep;
1.77      cvs      1909:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   1910:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      1911: 
1.28      cvs      1912:   if (!url || !*url)
                   1913:     return;
                   1914: 
1.106     cvs      1915:   if (strchr (*url, URL_SEP))
                   1916:       used_sep = URL_SEP;
1.32      cvs      1917:   else
1.106     cvs      1918:       used_sep = DIR_SEP;
1.32      cvs      1919: 
1.77      cvs      1920:   /* should we simplify double dot? */
                   1921:   path = *url;
1.106     cvs      1922:   if (*path == '.' && *(path + 1) == '.')
1.77      cvs      1923:     ddot_simplify = FALSE;
                   1924:   else
                   1925:     ddot_simplify = TRUE;
                   1926: 
1.28      cvs      1927:   /* Find any scheme name */
1.106     cvs      1928:   if ((path = strstr (*url, "://")) != NULL)
1.33      cvs      1929:     {
                   1930:       /* Find host name */
1.28      cvs      1931:       access = *url;
1.123     vatton   1932:       while (access < path && (*access = tolower (*access)))
1.82      cvs      1933:             access++;
1.28      cvs      1934:       path += 3;
1.106     cvs      1935:       while ((newptr = strstr (path, "://")) != NULL)
1.82      cvs      1936:             /* For proxies */
1.106     cvs      1937:             path = newptr + 3;
1.82      cvs      1938:      /* We have a host name */
1.84      cvs      1939:       path = HTCanon (url, path);
1.25      cvs      1940:     }
1.106     cvs      1941:   else if ((path = strstr (*url, ":/")) != NULL)
1.28      cvs      1942:     path += 2;
                   1943:   else
                   1944:     path = *url;
1.84      cvs      1945:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      1946:     /* Some URLs start //<foo> */
                   1947:     path += 1;
1.94      cvs      1948:   else if (IsFilePath (path))
                   1949:     {
                   1950:       /* doesn't need to do anything more */
                   1951:       return;
                   1952:     }
1.106     cvs      1953:   else if (!strncmp (path, "news:", 5))
1.28      cvs      1954:     {
1.106     cvs      1955:       newptr = strchr (path+5, '@');
1.28      cvs      1956:       if (!newptr)
                   1957:        newptr = path + 5;
                   1958:       while (*newptr)
                   1959:        {
                   1960:          /* Make group or host lower case */
1.123     vatton   1961:          *newptr = tolower (*newptr);
1.28      cvs      1962:          newptr++;
1.25      cvs      1963:        }
1.28      cvs      1964:       /* Doesn't need to do any more */
                   1965:       return;
1.25      cvs      1966:     }
1.130     cheyroul 1967:    
1.126     cheyroul 1968: 
1.28      cvs      1969:   if ((p = path))
                   1970:     {
1.106     cvs      1971:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1972:            (end = strchr (path, '#'))))
                   1973:        end = path + strlen (path);
1.28      cvs      1974:       
                   1975:       /* Parse string second time to simplify */
                   1976:       p = path;
                   1977:       while (p < end)
                   1978:        {
1.110     cvs      1979:          /* if we're pointing to a char, it's safe to reactivate the 
                   1980:             ../ convertion */
1.106     cvs      1981:          if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77      cvs      1982:            ddot_simplify = TRUE;
                   1983: 
1.33      cvs      1984:          if (*p==used_sep)
1.28      cvs      1985:            {
1.106     cvs      1986:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1987:                {
                   1988:                  orig = p + 1;
1.84      cvs      1989:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      1990:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1991:                  end = orig - 1;
                   1992:                }
1.106     cvs      1993:              else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.' 
1.77      cvs      1994:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1995:                {
                   1996:                  newptr = p;
1.52      cvs      1997:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1998:                  if (*newptr == used_sep)
                   1999:                    orig = newptr + 1;
1.28      cvs      2000:                  else
1.52      cvs      2001:                    orig = newptr;
                   2002: 
                   2003:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   2004:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   2005:                  end = orig-1;
                   2006:                  /* Start again with prev slash */
                   2007:                  p = newptr;
1.28      cvs      2008:                }
1.33      cvs      2009:              else if (*(p+1) == used_sep)
1.28      cvs      2010:                {
1.33      cvs      2011:                  while (*(p+1) == used_sep)
1.28      cvs      2012:                    {
                   2013:                      orig = p;
                   2014:                      dest = p + 1;
                   2015:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   2016:                      end = orig-1;
                   2017:                    }
                   2018:                }
                   2019:              else
1.25      cvs      2020:                p++;
1.28      cvs      2021:            }
                   2022:          else
                   2023:            p++;
1.25      cvs      2024:        }
                   2025:     }
1.51      cvs      2026:     /*
                   2027:     **  Check for host/../.. kind of things
                   2028:     */
1.106     cvs      2029:     if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.' 
1.77      cvs      2030:        && (!*(path+3) || *(path+3) == used_sep))
1.106     cvs      2031:        *(path+1) = EOS;
1.28      cvs      2032:   return;
                   2033: }
                   2034: 
                   2035: 
                   2036: /*----------------------------------------------------------------------
1.96      cvs      2037:    NormalizeFile normalizes local names.                             
1.28      cvs      2038:    Return TRUE if target and src differ.                           
                   2039:   ----------------------------------------------------------------------*/
1.106     cvs      2040: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28      cvs      2041: {
1.110     cvs      2042: #ifndef _WINDOWS
1.106     cvs      2043:    char             *s;
1.93      cvs      2044:    int               i;
1.110     cvs      2045: #endif /* !_WINDOWS */
1.82      cvs      2046:    ThotBool          change;
1.90      cvs      2047:    int               start_index; /* the first char that we'll copy */
1.28      cvs      2048: 
1.54      cvs      2049:    change = FALSE;
1.90      cvs      2050:    start_index = 0;
                   2051: 
1.106     cvs      2052:    if (!src || src[0] == EOS)
1.96      cvs      2053:      {
1.106     cvs      2054:        target[0] = EOS;
1.96      cvs      2055:        return FALSE;
                   2056:      }
1.90      cvs      2057: 
                   2058:    /* @@ do I need file: or file:/ here? */
1.106     cvs      2059:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      2060:      {
1.90      cvs      2061:        /* remove the prefix file: */
                   2062:        start_index += 5;
                   2063:    
                   2064:        /* remove the localhost prefix */
1.106     cvs      2065:        if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94      cvs      2066:           start_index += 11;
                   2067:        
                   2068:        /* remove the first two slashes in / / /path */
                   2069:        while (src[start_index] &&
1.106     cvs      2070:              src[start_index] == '/' 
                   2071:              && src[start_index + 1] == '/')
1.94      cvs      2072:         start_index++;
                   2073: 
                   2074: #ifdef _WINDOWS
                   2075:        /* remove any extra slash before the drive name */
1.106     cvs      2076:        if (src[start_index] == '/'
                   2077:           &&src[start_index+2] == ':')
1.94      cvs      2078:         start_index++;
                   2079: #endif /* _WINDOWS */
1.90      cvs      2080: 
1.106     cvs      2081:        if (src[start_index] == EOS)
1.90      cvs      2082:        /* if there's nothing afterwards, add a DIR_STR */
1.106     cvs      2083:         strcpy (target, DIR_STR);
1.90      cvs      2084:        else
1.97      cvs      2085:         /* as we're inside a file: URL, we'll apply all the convertions
                   2086:            we know */
                   2087:         CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96      cvs      2088: 
                   2089:        change = TRUE;
                   2090:      }
1.97      cvs      2091:    else if (convertion != AM_CONV_NONE)
1.96      cvs      2092:      {
                   2093:        /* we are following a "local" relative link, we do all the
                   2094:          convertions except for the HOME_DIR ~ one */
1.97      cvs      2095:        CleanCopyFileURL (target, src, convertion);
1.28      cvs      2096:      }
1.90      cvs      2097: #ifndef _WINDOWS
1.106     cvs      2098:    else if (src[0] == '~')
1.53      cvs      2099:      {
1.96      cvs      2100:        /* it must be a URL typed in a text input field */
                   2101:        /* do the HOME_DIR ~ substitution */
1.82      cvs      2102:        s = TtaGetEnvString ("HOME");
1.106     cvs      2103:        strcpy (target, s);
1.90      cvs      2104: #if 0
1.96      cvs      2105:        /* JK: invalidated this part of the code as it's simpler
                   2106:           to add the DIR_SEP whenever we have something to add
                   2107:           to the path rather than adding it systematically */
1.106     cvs      2108:        if (src[1] != DIR_SEP)
                   2109:          strcat (target, DIR_STR);
1.90      cvs      2110: #endif
1.106     cvs      2111:        i = strlen (target);
                   2112:        strcpy (&target[i], &src[1]);
1.54      cvs      2113:        change = TRUE;
1.53      cvs      2114:      }
1.90      cvs      2115: #endif /* _WINDOWS */
1.28      cvs      2116:    else
1.96      cvs      2117:    /* leave it as it is */
1.106     cvs      2118:      strcpy (target, src);
1.96      cvs      2119:    
1.28      cvs      2120:    /* remove /../ and /./ */
1.29      cvs      2121:    SimplifyUrl (&target);
1.54      cvs      2122:    if (!change)
1.106     cvs      2123:      change = strcmp (src, target);
1.28      cvs      2124:    return (change);
1.25      cvs      2125: }
                   2126: 
1.28      cvs      2127: 
1.25      cvs      2128: /*----------------------------------------------------------------------
1.31      cvs      2129:   MakeRelativeURL: make relative name
1.25      cvs      2130:   
1.28      cvs      2131:   This function creates and returns a string which gives an expression of
                   2132:   one address as related to another. Where there is no relation, an absolute
                   2133:   address is retured.
1.25      cvs      2134:   
1.28      cvs      2135:   On entry,
1.25      cvs      2136:        Both names must be absolute, fully qualified names of nodes
                   2137:        (no fragment bits)
                   2138:   
1.28      cvs      2139:   On exit,
1.25      cvs      2140:        The return result points to a newly allocated name which, if
                   2141:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   2142:        The caller is responsible for freeing the resulting name later.
                   2143:   ----------------------------------------------------------------------*/
1.106     cvs      2144: char      *MakeRelativeURL (char *aName, char *relatedName)
                   2145: {
                   2146:   char  *return_value;
                   2147:   char   result[MAX_LENGTH];
                   2148:   char  *p;
                   2149:   char  *q;
                   2150:   char  *after_access;
                   2151:   char  *last_slash = NULL;
                   2152:   int    slashes, levels, len;
1.110     cvs      2153: #ifdef _WINDOWS
1.44      cvs      2154:   int ndx;
1.110     cvs      2155: #endif /* _WINDOWS */
1.44      cvs      2156: 
1.29      cvs      2157:   if (aName == NULL || relatedName == NULL)
                   2158:     return (NULL);
                   2159: 
                   2160:   slashes = 0;
                   2161:   after_access = NULL;
                   2162:   p = aName;
                   2163:   q = relatedName;
1.147     vatton   2164:   len = 0;
                   2165:   for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27      cvs      2166:     {
                   2167:       /* Find extent of match */
1.106     cvs      2168:       if (*p == ':')
1.146     cvs      2169:          {
                   2170:          after_access = p + 1;
1.147     vatton   2171:          if (len == 1)
                   2172:            /* it's a local Windows path like c:... */
                   2173:            slashes++;
1.146     cvs      2174:          }
1.28      cvs      2175:       if (*p == DIR_SEP)
1.27      cvs      2176:        {
1.29      cvs      2177:          /* memorize the last slash position and count them */
1.27      cvs      2178:          last_slash = p;
                   2179:          slashes++;
1.25      cvs      2180:        }
                   2181:     }
                   2182:     
1.31      cvs      2183:   /* q, p point to the first non-matching character or zero */
1.106     cvs      2184:   if (*q == EOS)
1.31      cvs      2185:     {
                   2186:       /* New name is a subset of the related name */
                   2187:       /* exactly the right length */
1.106     cvs      2188:       len = strlen (p);
                   2189:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2190:        strcpy (return_value, p);
1.31      cvs      2191:     }
                   2192:   else if ((slashes < 2 && after_access == NULL)
                   2193:       || (slashes < 3 && after_access != NULL))
                   2194:     {
                   2195:       /* Two names whitout common path */
                   2196:       /* exactly the right length */
1.106     cvs      2197:       len = strlen (aName);
                   2198:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2199:        strcpy (return_value, aName);
1.31      cvs      2200:     }
                   2201:   else
                   2202:     {
                   2203:       /* Some path in common */
1.106     cvs      2204:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31      cvs      2205:        /* just the same server */
1.106     cvs      2206:        strcpy (result, last_slash);
1.31      cvs      2207:       else
                   2208:        {
                   2209:          levels= 0; 
1.106     cvs      2210:          for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31      cvs      2211:            if (*q == DIR_SEP)
                   2212:              levels++;
                   2213:          
1.106     cvs      2214:          result[0] = EOS;
1.31      cvs      2215:          for (;levels; levels--)
1.106     cvs      2216:            strcat (result, "../");
                   2217:          strcat (result, last_slash+1);
1.31      cvs      2218:        } 
1.52      cvs      2219: 
                   2220:       if (!*result)
1.106     cvs      2221:        strcat (result, "./");
1.52      cvs      2222: 
1.31      cvs      2223:       /* exactly the right length */
1.106     cvs      2224:       len = strlen (result);
                   2225:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2226:        strcpy (return_value, result);
1.52      cvs      2227: 
1.25      cvs      2228:     }
1.110     cvs      2229: #ifdef _WINDOWS
1.106     cvs      2230:   len = strlen (return_value);
1.44      cvs      2231:   for (ndx = 0; ndx < len; ndx ++)
1.106     cvs      2232:          if (return_value[ndx] == '\\')
                   2233:             return_value[ndx] = '/' ;
1.110     cvs      2234: #endif /* _WINDOWS */
1.29      cvs      2235:   return (return_value);
1.24      cvs      2236: }
1.35      cvs      2237: 
1.104     kahan    2238: /*----------------------------------------------------------------------
                   2239:   AM_GetFileSize
                   2240:   Returns TRUE and the filesize in the 2nd parameter.
                   2241:   Otherwise, in case of a system error, returns FALSE, with a 
                   2242:   filesize of 0L.
                   2243:   ---------------------------------------------------------------------*/
1.106     cvs      2244: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104     kahan    2245: {
1.106     cvs      2246:   ThotFileHandle   handle = ThotFile_BADHANDLE;
                   2247:   ThotFileInfo     info;
1.35      cvs      2248: 
1.104     kahan    2249:   *file_size = 0L;
                   2250:   if (!TtaFileExist (filename))
                   2251:     return FALSE;
                   2252: 
                   2253:   handle = TtaFileOpen (filename, ThotFile_READWRITE);
                   2254:   if (handle == ThotFile_BADHANDLE)
                   2255:     /* ThotFile_BADHANDLE */
                   2256:     return FALSE;
                   2257:    if (TtaFileStat (handle, &info) == 0)
                   2258:      /* bad stat */
                   2259:      info.size = 0L;
                   2260:    TtaFileClose (handle);
                   2261:    *file_size = (unsigned long) info.size;
                   2262:    return TRUE;
                   2263: }
1.139     kahan    2264: 
                   2265: /*----------------------------------------------------------------------
                   2266:   AM_UseXHTMLMimeType
                   2267:   Returns TRUE if the user has configured Amaya to use this MIME type,
                   2268:   FALSE otherwise.
                   2269:   ---------------------------------------------------------------------*/
                   2270: ThotBool AM_UseXHTMLMimeType (void)
                   2271: {
                   2272:   ThotBool xhtml_mimetype;
                   2273:   
                   2274:   /* does the user wants to use the new MIME type? */
                   2275:   TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
                   2276: 
                   2277:   return (xhtml_mimetype);
                   2278: }

Webmaster