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

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

Webmaster