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

1.7       cvs         1: /*
                      2:  *
1.190     vatton      3:  *  (c) COPYRIGHT INRIA and W3C, 1996-2005
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;
1.192     vatton    917:    if (!strcmp (&temptype[i], "gif") ||
                    918:        !strcmp (&temptype[i], "x-xbitmap") ||
                    919:        !strcmp (&temptype[i], "x-xpixmap") ||
                    920:        !strcmp (&temptype[i], "jpeg") ||
1.166     vatton    921:        !strcmp (&temptype[i], "png"))
1.183     quint     922:      ret = TRUE;
                    923:    else
                    924:      ret = FALSE;
                    925:    TtaFreeMemory (temptype);
                    926:    return (ret);
1.58      cvs       927: }
                    928: 
                    929: /*----------------------------------------------------------------------
1.9       cvs       930:   IsTextName                                                         
1.4       cvs       931:   ----------------------------------------------------------------------*/
1.111     cvs       932: ThotBool IsTextName (const char *path)
1.106     cvs       933: {
1.183     quint     934:    char                *temppath;
                    935:    char                *suffix;
1.106     cvs       936:    char                nsuffix[MAX_LENGTH];
1.5       cvs       937:    int                 i;
1.183     quint     938:    ThotBool            ret;
1.5       cvs       939: 
                    940:    if (!path)
1.13      cvs       941:      return (FALSE);
1.5       cvs       942: 
1.183     quint     943:    temppath = TtaStrdup ((char *)path);
                    944:    suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124     vatton    945:    TtaExtractSuffix (temppath, suffix);
1.5       cvs       946: 
                    947:    /* Normalize the suffix */
                    948:    i = 0;
1.106     cvs       949:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5       cvs       950:      {
1.25      cvs       951:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       952:        i++;
                    953:      }
1.106     cvs       954:    nsuffix[i] = EOS;
1.5       cvs       955: 
1.192     vatton    956:    if (!strcmp (nsuffix, "txt") ||
                    957:        !strcmp (nsuffix, "dtd"))
1.183     quint     958:       ret = TRUE;
1.106     cvs       959:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       960:      {
1.39      cvs       961:        /* take into account compressed files */
1.124     vatton    962:        TtaExtractSuffix (temppath, suffix);       
1.13      cvs       963:        /* Normalize the suffix */
                    964:        i = 0;
1.106     cvs       965:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       966:         {
1.25      cvs       967:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       968:           i++;
                    969:         }
1.106     cvs       970:        nsuffix[i] = EOS;
1.192     vatton    971:        if (!strcmp (nsuffix, "txt") ||
                    972:           !strcmp (nsuffix, "dtd"))
1.183     quint     973:         ret = TRUE;
1.13      cvs       974:        else
1.183     quint     975:         ret = FALSE;
1.13      cvs       976:      }
                    977:    else
1.183     quint     978:      ret = FALSE;
                    979: 
                    980:    TtaFreeMemory (temppath);
                    981:    TtaFreeMemory (suffix);
                    982:    return (ret);
1.3       cvs       983: }
                    984: 
1.4       cvs       985: /*----------------------------------------------------------------------
1.9       cvs       986:   IsHTTPPath                                     
                    987:   returns TRUE if path is in fact an http URL.
1.4       cvs       988:   ----------------------------------------------------------------------*/
1.112     cvs       989: ThotBool IsHTTPPath (const char *path)
1.3       cvs       990: {
1.5       cvs       991:    if (!path)
                    992:       return FALSE;
1.3       cvs       993: 
1.106     cvs       994:    if ((!strncmp (path, "http:", 5) != 0)
                    995:        || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
                    996:        || !strncmp (path, "internal:", 9))
1.58      cvs       997:       return TRUE;
                    998:    return FALSE;
1.3       cvs       999: }
                   1000: 
1.4       cvs      1001: /*----------------------------------------------------------------------
1.9       cvs      1002:   IsWithParameters                           
                   1003:   returns TRUE if url has a concatenated query string.
1.4       cvs      1004:   ----------------------------------------------------------------------*/
1.133     vatton   1005: ThotBool IsWithParameters (const char *url)
1.3       cvs      1006: {
1.5       cvs      1007:    int                 i;
1.3       cvs      1008: 
1.9       cvs      1009:    if ((!url) || (url[0] == EOS))
1.5       cvs      1010:       return FALSE;
1.3       cvs      1011: 
1.9       cvs      1012:    i = strlen (url) - 1;
                   1013:    while (i > 0 && url[i--] != '?')
1.5       cvs      1014:       if (i < 0)
                   1015:         return FALSE;
1.3       cvs      1016: 
1.5       cvs      1017:    /* There is a parameter */
                   1018:    return TRUE;
1.3       cvs      1019: }
                   1020: 
1.4       cvs      1021: /*----------------------------------------------------------------------
1.9       cvs      1022:   IsW3Path                                           
                   1023:   returns TRUE if path is in fact a URL.
1.4       cvs      1024:   ----------------------------------------------------------------------*/
1.133     vatton   1025: ThotBool IsW3Path (const char *path)
1.106     cvs      1026: {
1.170     quint    1027:   if (path == NULL)
                   1028:     return FALSE;
1.106     cvs      1029:   if (strncmp (path, "http:", 5)   && 
                   1030:       strncmp (path, "ftp:", 4)    &&
                   1031:       strncmp (path, "telnet:", 7) && 
                   1032:       strncmp (path, "wais:", 5)   &&
                   1033:       strncmp (path, "news:", 5)   && 
                   1034:       strncmp (path, "gopher:", 7) &&
                   1035:       strncmp (path, "mailto:", 7) && 
1.132     cheyroul 1036:       strncmp (path, "archie:", 7) &&
                   1037:       strncmp (path, "https:", 6))
1.72      cvs      1038:     return FALSE;
                   1039:   return TRUE;
1.3       cvs      1040: }
                   1041: 
1.4       cvs      1042: /*----------------------------------------------------------------------
1.90      cvs      1043:   IsFilePath                                           
                   1044:   returns TRUE if path is in fact a URL.
                   1045:   ----------------------------------------------------------------------*/
1.133     vatton   1046: ThotBool IsFilePath (const char *path)
1.90      cvs      1047: {
1.106     cvs      1048:   if (strncmp (path, "file:", 5))
1.90      cvs      1049:     return FALSE;
                   1050:   return TRUE;
                   1051: }
                   1052: 
                   1053: /*----------------------------------------------------------------------
1.9       cvs      1054:   IsValidProtocol                                                    
                   1055:   returns true if the url protocol is supported by Amaya.
1.4       cvs      1056:   ----------------------------------------------------------------------*/
1.133     vatton   1057: ThotBool IsValidProtocol (const char *url)
1.106     cvs      1058: {
                   1059:    if (!strncmp (url, "http:", 5)
                   1060:       || !strncmp (url, "internal:", 9)
                   1061:       || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22      cvs      1062:        /* experimental */
1.24      cvs      1063:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs      1064:       return (TRUE);
1.5       cvs      1065:    else
1.8       cvs      1066:       return (FALSE);
1.3       cvs      1067: }
                   1068: 
1.31      cvs      1069: 
                   1070: /*----------------------------------------------------------------------
                   1071:    GetBaseURL
                   1072:    normalizes orgName according to a base associated with doc, and
                   1073:    following the standard URL format rules.
                   1074:    The function returns the base used to solve relative URL and SRC:
                   1075:       - the base of the document,
                   1076:       - or the document path (without document name).
                   1077:   ----------------------------------------------------------------------*/
1.106     cvs      1078: char  *GetBaseURL (Document doc)
1.31      cvs      1079: {
                   1080:   Element             el;
                   1081:   ElementType         elType;
                   1082:   AttributeType       attrType;
                   1083:   Attribute           attr;
1.176     vatton   1084:   char               *ptr, *basename, *utf8path;
1.31      cvs      1085:   int                 length;
1.151     kahan    1086:   ThotBool            hasDocBase;
1.31      cvs      1087: 
1.113     cvs      1088:   if (doc == 0 || !DocumentURLs[doc])
1.110     cvs      1089:      return NULL;
1.148     kahan    1090:   /* the other functions expect basename to have no more than MAX_LENGTH chars */
1.171     gully    1091:   basename = (char *)TtaGetMemory (MAX_LENGTH);
1.148     kahan    1092:   basename[0] = EOS;
1.31      cvs      1093:   length = MAX_LENGTH -1;
1.151     kahan    1094:   hasDocBase = FALSE;
                   1095: 
                   1096:   /* If the document has a base URL, it has a priority over the headers. */
                   1097:   /*  @@ We need to do this too when we support XML:base */
                   1098: 
                   1099:   /* is it a HTML document ? */
                   1100:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
                   1101:   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
                   1102:     /* it's a HTML document */
                   1103:     {
                   1104:       /* get the document element */
                   1105:       el = TtaGetMainRoot (doc);
                   1106:       /* search the BASE element */
                   1107:       elType.ElTypeNum = HTML_EL_HEAD;
                   1108:       el = TtaSearchTypedElement (elType, SearchForward, el);
                   1109:       if (el)
                   1110:        /* there is a HEAD element */
                   1111:        {
                   1112:          /* look for a BASE element within the HEAD */
                   1113:          elType.ElTypeNum = HTML_EL_BASE;
                   1114:          el = TtaSearchTypedElement (elType, SearchInTree, el);
                   1115:        }
                   1116:       if (el)
                   1117:        {
                   1118:          /*  The document has a BASE element. Get the HREF attribute of the
                   1119:              BASE element */
                   1120:          hasDocBase = TRUE;
                   1121:          attrType.AttrSSchema = elType.ElSSchema;
                   1122:          attrType.AttrTypeNum = HTML_ATTR_HREF_;
                   1123:          attr = TtaGetAttribute (el, attrType);
                   1124:          if (attr)
                   1125:            {
                   1126:              /* Use the base path of the document */
                   1127:              TtaGiveTextAttributeValue (attr, basename, &length);
1.176     vatton   1128:              utf8path = basename;
                   1129:              basename = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
                   1130:                                                      TtaGetDefaultCharset ());
                   1131:              TtaFreeMemory (utf8path);
1.151     kahan    1132:            }
                   1133:        }
                   1134:     }
                   1135: 
                   1136:   /* there was no BASE. Do we have a location header? */
                   1137:   if (!hasDocBase && DocumentMeta[doc] && DocumentMeta[doc]->full_content_location
1.148     kahan    1138:       && DocumentMeta[doc]->full_content_location[0] != EOS)
1.65      cvs      1139:     {
1.148     kahan    1140:       strncpy (basename, DocumentMeta[doc]->full_content_location, MAX_LENGTH-1);
                   1141:       basename[MAX_LENGTH-1] = EOS;
                   1142:       length = strlen (basename);
                   1143:     }
                   1144: 
                   1145:   if (basename[0] != EOS)
                   1146:     {
                   1147:       /* base and orgName have to be separated by a DIR_SEP */
                   1148:       length--;
                   1149:       if (basename[0] != EOS && basename[length] != URL_SEP &&
                   1150:          basename[length] != DIR_SEP) 
                   1151:        /* verify if the base has the form "protocol://server:port" */
1.31      cvs      1152:        {
1.148     kahan    1153:          ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
                   1154:                               AMAYA_PARSE_HOST |
                   1155:                               AMAYA_PARSE_PUNCTUATION);
                   1156:          if (ptr && !strcmp (ptr, basename))
1.31      cvs      1157:            {
1.148     kahan    1158:              /* it has this form, complete it by adding a URL_STR  */
                   1159:              if (strchr (basename, DIR_SEP))
                   1160:                strcat (basename, DIR_STR);
                   1161:              else
                   1162:                strcat (basename, URL_STR);
                   1163:              length++;
1.31      cvs      1164:            }
1.149     kahan    1165:          else if (!ptr || ptr[0] == EOS)
                   1166:            {
                   1167:              /* no host was detected, we may have a relative URL. We test
                   1168:                 if it begins with a URL_SEP, DIR_SEP or period. If yes, it's
                   1169:                 relative. */
                   1170:              if (! (basename[0] == '.' || basename[0] == URL_SEP 
                   1171:                     || basename[0] == DIR_SEP))
                   1172:                basename[0] = EOS;
                   1173:            }
1.148     kahan    1174:          if (ptr)
                   1175:            TtaFreeMemory (ptr);
1.31      cvs      1176:        }
1.113     cvs      1177:     }
                   1178: 
1.148     kahan    1179:   /* there was no base element and no location header, we use the DocumentURL  */
                   1180:   if (basename[0] == EOS)
                   1181:     {
                   1182:       strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
                   1183:       basename[MAX_LENGTH-1] = EOS;
                   1184:     }
                   1185:   
1.31      cvs      1186:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                   1187:    * then search for the first ":" char, hoping that what's before that is a
                   1188:    * protocol. If found, end the string there. If neither char is found,
                   1189:    * then discard the whole base element.
                   1190:    */
1.106     cvs      1191:   length = strlen (basename) - 1;
1.31      cvs      1192:   /* search for the last DIR_SEP char */
1.106     cvs      1193:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs      1194:     length--;
                   1195:   if (length >= 0)
                   1196:     /* found the last DIR_SEP char, end the string there */
1.106     cvs      1197:     basename[length + 1] = EOS;                   
1.31      cvs      1198:   else
                   1199:     /* search for the first PATH_STR char */
                   1200:     {
1.106     cvs      1201:       for (length = 0; basename[length] != ':' && 
                   1202:             basename[length] != EOS; length ++);
                   1203:       if (basename[length] == ':')
1.31      cvs      1204:        /* found, so end the string there */
1.106     cvs      1205:        basename[length + 1] = EOS;
1.31      cvs      1206:       else
                   1207:        /* not found, discard the base */
1.106     cvs      1208:        basename[0] = EOS;
1.31      cvs      1209:     }
                   1210:   return (basename);
                   1211: }
                   1212: 
                   1213: 
1.4       cvs      1214: /*----------------------------------------------------------------------
1.40      cvs      1215:    GetLocalPath
                   1216:    Allocate and return the local document path associated to the url
                   1217:   ----------------------------------------------------------------------*/
1.150     vatton   1218: char *GetLocalPath (Document doc, char  *url)
1.106     cvs      1219: {
                   1220:   char     *ptr;
                   1221:   char     *n;
                   1222:   char     *documentname;
                   1223:   char      url_sep;
1.83      cvs      1224:   int       len;
1.67      cvs      1225:   ThotBool  noFile;
1.40      cvs      1226: 
1.153     vatton   1227:   if (url)
1.40      cvs      1228:     {
                   1229:       /* check whether the file name exists */
1.106     cvs      1230:       len = strlen (url) - 1;
1.71      cvs      1231:       if (IsW3Path (url))
1.157     kahan    1232:          url_sep = URL_SEP;
1.41      cvs      1233:       else 
1.106     cvs      1234:           url_sep = DIR_SEP;
1.41      cvs      1235:       noFile = (url[len] == url_sep);
1.40      cvs      1236:       if (noFile)
1.106     cvs      1237:          url[len] = EOS;
1.171     gully    1238:       ptr = (char *)TtaGetMemory (MAX_LENGTH);
                   1239:       documentname = (char *)TtaGetMemory (MAX_LENGTH);
1.78      cvs      1240:       TtaExtractName (url, ptr, documentname);
1.106     cvs      1241:       sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40      cvs      1242:       if (!TtaCheckDirectory (ptr))
                   1243:        /* directory did not exist */
1.72      cvs      1244:        TtaMakeDirectory (ptr);
1.47      cvs      1245: 
1.153     vatton   1246:       if (doc == 0)
                   1247:        {
                   1248:          n = strrchr (documentname, '.');
                   1249:          if (n)
                   1250:            *n = EOS;
                   1251:          if (documentname[0] == EOS)
                   1252:            strcpy (documentname, "noname");
                   1253:          n = GetTempName (ptr, documentname);
                   1254:          TtaFreeMemory (ptr);
                   1255:          ptr = n;
                   1256:        }
1.69      cvs      1257:       else
1.153     vatton   1258:        {
                   1259:          /* don't include the query string within document name */
                   1260:          n = strrchr (documentname, '?');
                   1261:          if (n)
                   1262:            *n = EOS;
                   1263:          /* don't include ':' within document name */
                   1264:          n = strchr (documentname, ':');
                   1265:          if (n)
                   1266:            *n = EOS;
                   1267:          /* if after all this operations document name
                   1268:             is empty, let's use noname.html instead */
                   1269:          if (documentname[0] == EOS)
                   1270:            strcat (ptr, "noname.html");
                   1271:          else
                   1272:            strcat (ptr, documentname);
                   1273:        }
1.40      cvs      1274:       TtaFreeMemory (documentname);
1.157     kahan    1275:       /* substitute invalid chars in file names by a _ */
1.188     gully    1276:       n = strrchr(ptr,DIR_SEP);
1.157     kahan    1277:       while (*n)
                   1278:        {
                   1279:          if (*n == '*' || *n == ',')
                   1280:            *n = '_';
1.187     cvs      1281:          if ((unsigned char)*n >= 0x80) /* avoid non-ASCII */
                   1282:                  *n = 'A' + ((unsigned char)*n % 26);
1.157     kahan    1283:          n++;
                   1284:        }
1.40      cvs      1285:       /* restore the url */
                   1286:       if (noFile)
1.41      cvs      1287:        url[len] = url_sep;
1.40      cvs      1288:       return (ptr);
                   1289:     }
                   1290:   else
                   1291:     return (NULL);
                   1292: }
                   1293: 
1.73      cvs      1294: /*----------------------------------------------------------------------
1.79      cvs      1295:    ExtractTarget extract the target name from document nane.        
                   1296:   ----------------------------------------------------------------------*/
1.150     vatton   1297: void ExtractTarget (char *aName, char *target)
1.79      cvs      1298: {
1.194   ! cvs      1299:    long int    lg, i;
1.106     cvs      1300:    char  *ptr;
                   1301:    char  *oldptr;
1.79      cvs      1302: 
                   1303:    if (!target || !aName)
                   1304:      /* bad target */
                   1305:      return;
                   1306: 
1.106     cvs      1307:    target[0] = EOS;
                   1308:    lg = strlen (aName);
1.79      cvs      1309:    if (lg)
                   1310:      {
                   1311:        /* the name is not empty */
                   1312:        oldptr = ptr = &aName[0];
                   1313:        do
                   1314:          {
1.106     cvs      1315:             ptr = strrchr (oldptr, '#');
1.79      cvs      1316:             if (ptr)
                   1317:                oldptr = &ptr[1];
                   1318:          }
                   1319:        while (ptr);
                   1320: 
1.194   ! cvs      1321:        i = (long int) (oldptr) - (long int) (aName);   /* name length */
1.79      cvs      1322:        if (i > 1)
                   1323:          {
1.106     cvs      1324:             aName[i - 1] = EOS;
1.79      cvs      1325:             if (i != lg)
1.106     cvs      1326:                strcpy (target, oldptr);
1.79      cvs      1327:          }
                   1328:      }
                   1329: }
                   1330: 
                   1331: /*----------------------------------------------------------------------
1.90      cvs      1332:    RemoveNewLines (text)
                   1333:    Removes any '\n' chars that are found in text. 
                   1334:    Returns TRUE if it did the operation, FALSE otherwise.
1.73      cvs      1335:   ----------------------------------------------------------------------*/
1.106     cvs      1336: ThotBool RemoveNewLines (char *text)
                   1337: {
                   1338:   ThotBool   change = FALSE;
                   1339:   char      *src;
                   1340:   char      *dest;
1.90      cvs      1341: 
                   1342:   src = text;
                   1343:   dest = text;
1.115     kahan    1344: 
                   1345:   /* remove any preceding whitespace */
                   1346:   while (*src && *src == ' ')
                   1347:     {
                   1348:       src++;
                   1349:       change = 1;
                   1350:     }
                   1351:   
1.90      cvs      1352:   while (*src)
                   1353:     {
                   1354:       switch (*src)
                   1355:        {
1.106     cvs      1356:        case '\n':
1.90      cvs      1357:          /* don't copy the newline */
                   1358:          change = 1;
                   1359:          break;
                   1360:        default:
                   1361:          *dest = *src;
                   1362:          dest++;
                   1363:          break;
                   1364:        }
                   1365:       src++;
                   1366:     }
                   1367:   /* copy the last EOS char */
                   1368:   *dest = *src;
                   1369: 
                   1370:   return (change);
                   1371: }
                   1372: 
                   1373: /*----------------------------------------------------------------------
                   1374:    CleanCopyFileURL
                   1375:    Copies a file url from a src string to destination string.
1.97      cvs      1376:    convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
                   1377:    we want to do).
1.90      cvs      1378:   ----------------------------------------------------------------------*/
1.106     cvs      1379: static void CleanCopyFileURL (char *dest, char *src,
                   1380:                              ConvertionType convertion)
1.90      cvs      1381: {
                   1382:   while (*src)
1.89      cvs      1383:     {
1.90      cvs      1384:       switch (*src)
1.89      cvs      1385:        {
1.184     gully    1386: #ifdef _WINDOWS
1.106     cvs      1387:        case URL_SEP:
1.96      cvs      1388:          /* make DIR_SEP transformation */
1.97      cvs      1389:          if (convertion & AM_CONV_URL_SEP)
1.106     cvs      1390:            *dest = DIR_SEP;
1.96      cvs      1391:          else
                   1392:            *dest = *src;
1.90      cvs      1393:          dest++;
1.96      cvs      1394:          src++;
1.90      cvs      1395:          break;
1.184     gully    1396: #endif /* _WINDOWS */
1.96      cvs      1397: 
1.106     cvs      1398:        case '%':
1.97      cvs      1399:          if (convertion & AM_CONV_PERCENT)
1.96      cvs      1400:            {
1.97      cvs      1401:              /* (code adapted from libwww's HTUnEscape function */
1.96      cvs      1402:              src++;
1.106     cvs      1403:              if (*src != EOS)
1.97      cvs      1404:                {
                   1405:                  *dest = UnEscapeChar (*src) * 16;
                   1406:                  src++;
                   1407:                }
1.106     cvs      1408:              if (*src != EOS)
1.97      cvs      1409:                {
                   1410:                  *dest = *dest + UnEscapeChar (*src);
                   1411:                  src++;
                   1412:                }
                   1413:              dest++;
1.96      cvs      1414:            }
1.97      cvs      1415:          else
1.96      cvs      1416:            {
1.97      cvs      1417:              *dest = *src;
                   1418:              dest++;
1.96      cvs      1419:              src++;
                   1420:            }
                   1421:          break;
                   1422: 
1.90      cvs      1423:        default:
                   1424:          *dest = *src;
1.89      cvs      1425:          dest++;
1.96      cvs      1426:          src++;
1.90      cvs      1427:          break;
1.89      cvs      1428:        }
                   1429:     }
1.90      cvs      1430:   /* copy the EOS char */
                   1431:   *dest = *src;
1.73      cvs      1432: }
1.40      cvs      1433: 
                   1434: /*----------------------------------------------------------------------
1.9       cvs      1435:    NormalizeURL
                   1436:    normalizes orgName according to a base associated with doc, and
                   1437:    following the standard URL format rules.
1.113     cvs      1438:    if doc is < 0, use as a base the URL of the document that contains
                   1439:    (or contained) the elements that are now in the copy/cut buffer.
1.53      cvs      1440:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                   1441:    other path.
1.9       cvs      1442:    The function returns the new complete and normalized URL 
1.12      cvs      1443:    or file name path (newName) and the name of the document (docName).        
1.9       cvs      1444:    N.B. If the function can't find out what's the docName, it assigns
                   1445:    the name "noname.html".
1.4       cvs      1446:   ----------------------------------------------------------------------*/
1.106     cvs      1447: void NormalizeURL (char *orgName, Document doc, char *newName,
                   1448:                   char *docName, char *otherPath)
                   1449: {
                   1450:    char          *basename;
                   1451:    char           tempOrgName[MAX_LENGTH];
                   1452:    char          *ptr;
                   1453:    char           used_sep;
1.84      cvs      1454:    int            length;
                   1455:    ThotBool       check;
1.5       cvs      1456: 
1.184     gully    1457: #ifdef _WINDOWS
1.44      cvs      1458:    int ndx;
1.184     gully    1459: #endif /* _WINDOWS */
1.44      cvs      1460: 
1.5       cvs      1461:    if (!newName || !docName)
                   1462:       return;
1.18      cvs      1463: 
1.113     cvs      1464:    if (doc < 0)
                   1465:      basename = TtaStrdup (SavedDocumentURL);
                   1466:    else if (doc > 0)
1.53      cvs      1467:      basename = GetBaseURL (doc);
                   1468:    else if (otherPath != NULL)
1.108     cvs      1469:      basename = TtaStrdup (otherPath);
1.32      cvs      1470:    else
1.53      cvs      1471:      basename = NULL;
1.32      cvs      1472: 
1.18      cvs      1473:    /*
1.31      cvs      1474:     * Clean orgName
                   1475:     * Make sure we have a complete orgName, without any leading or trailing
                   1476:     * white spaces, or trailinbg new lines
                   1477:     */
1.5       cvs      1478:    ptr = orgName;
1.18      cvs      1479:    /* skip leading white space and new line characters */
1.106     cvs      1480:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                   1481:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                   1482:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs      1483:    /*
1.31      cvs      1484:     * Make orgName a complete URL
                   1485:     * If the URL does not include a protocol, then try to calculate
                   1486:     * one using the doc's base element (if it exists),
                   1487:     */
1.106     cvs      1488:    if (tempOrgName[0] == EOS)
1.53      cvs      1489:      {
1.106     cvs      1490:        newName[0] = EOS;
                   1491:        docName[0] = EOS;
1.53      cvs      1492:        TtaFreeMemory (basename);
                   1493:        return;
                   1494:      }
1.49      cvs      1495: 
                   1496:    /* clean trailing white space */
1.106     cvs      1497:    length = strlen (tempOrgName) - 1;
                   1498:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53      cvs      1499:      {
1.106     cvs      1500:        tempOrgName[length] = EOS;
1.53      cvs      1501:        length--;
                   1502:      }
1.50      cvs      1503: 
1.55      cvs      1504:    /* remove extra dot (which dot???) */
                   1505:    /* ugly, but faster than a strcmp */
1.106     cvs      1506:    if (tempOrgName[length] == '.'
                   1507:        && (length == 0 || tempOrgName[length-1] != '.'))
                   1508:         tempOrgName[length] = EOS;
1.50      cvs      1509: 
1.94      cvs      1510:    if (IsW3Path (tempOrgName))
1.53      cvs      1511:      {
                   1512:        /* the name is complete, go to the Sixth Step */
1.106     cvs      1513:        strcpy (newName, tempOrgName);
1.53      cvs      1514:        SimplifyUrl (&newName);
                   1515:        /* verify if the URL has the form "protocol://server:port" */
1.110     cvs      1516:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
                   1517:                                         AMAYA_PARSE_HOST |
                   1518:                                         AMAYA_PARSE_PUNCTUATION);
                   1519:        if (ptr && !strcmp (ptr, newName))
                   1520:         /* it has this form, we complete it by adding a DIR_STR  */
1.106     cvs      1521:          strcat (newName, URL_STR);
1.49      cvs      1522: 
1.53      cvs      1523:        if (ptr)
1.50      cvs      1524:          TtaFreeMemory (ptr);
1.53      cvs      1525:      }
1.113     cvs      1526:    else if (basename == NULL)
1.53      cvs      1527:      /* the name is complete, go to the Sixth Step */
1.106     cvs      1528:      strcpy (newName, tempOrgName);
1.53      cvs      1529:    else
                   1530:      {
1.31      cvs      1531:        /* Calculate the absolute URL, using the base or document URL */
1.184     gully    1532: #ifdef _WINDOWS
1.53      cvs      1533:        if (!IsW3Path (basename))
                   1534:         {
1.106     cvs      1535:           length = strlen (tempOrgName);
1.53      cvs      1536:           for (ndx = 0; ndx < length; ndx++)
1.106     cvs      1537:             if (tempOrgName [ndx] == '/')
                   1538:               tempOrgName [ndx] = '\\';
1.53      cvs      1539:         }
1.184     gully    1540: #endif /* _WINDOWS */
1.25      cvs      1541:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1542:        if (ptr)
                   1543:         {
                   1544:           SimplifyUrl (&ptr);
1.106     cvs      1545:           strcpy (newName, ptr);
1.53      cvs      1546:           TtaFreeMemory (ptr);
                   1547:         }
                   1548:        else
1.106     cvs      1549:         newName[0] = EOS;
1.53      cvs      1550:      }
1.36      cvs      1551: 
                   1552:    TtaFreeMemory (basename);
1.18      cvs      1553:    /*
1.31      cvs      1554:     * Prepare the docname that will refer to this ressource in the
                   1555:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1556:     * noname.html as a default ressource name
1.18      cvs      1557:    */
1.106     cvs      1558:    if (newName[0] != EOS)
1.53      cvs      1559:      {
1.106     cvs      1560:        length = strlen (newName) - 1;
                   1561:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53      cvs      1562:         {
                   1563:           used_sep = newName[length];
                   1564:           check = TRUE;
                   1565:           while (check)
                   1566:             {
1.50      cvs      1567:                length--;
                   1568:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1569:                 length--;
1.106     cvs      1570:                if (!strncmp (&newName[length+1], "..", 2))
1.53      cvs      1571:                 {
1.106     cvs      1572:                   newName[length+1] = EOS;
1.53      cvs      1573:                   /* remove also previous directory */
                   1574:                   length--;
                   1575:                   while (length >= 0 && newName[length] != used_sep)
                   1576:                     length--;
1.106     cvs      1577:                   if (strncmp (&newName[length+1], "//", 2))
1.131     cheyroul 1578:                     /* don't remove server name */
1.106     cvs      1579:                      newName[length+1] = EOS;
1.53      cvs      1580:                 }
1.106     cvs      1581:               else if (!strncmp (&newName[length+1], ".", 1))
                   1582:                 newName[length+1] = EOS;
1.50      cvs      1583:                else
1.53      cvs      1584:                 check = FALSE;
                   1585:             }
                   1586:           /* docname was not comprised inside the URL, so let's */
                   1587:           /* assign the default ressource name */
1.106     cvs      1588:           strcpy (docName, "noname.html");
1.53      cvs      1589:         }
                   1590:        else
                   1591:         { /* docname is comprised inside the URL */
1.110     cvs      1592:            while (length >= 0 && newName[length] != URL_SEP &&
                   1593:                  newName[length] != DIR_SEP)
1.53      cvs      1594:             length--;
                   1595:           if (length < 0)
1.106     cvs      1596:              strcpy (docName, newName);
1.53      cvs      1597:           else
1.106     cvs      1598:             strcpy (docName, &newName[length+1]);
1.53      cvs      1599:         }
                   1600:      }
                   1601:    else
1.106     cvs      1602:      docName[0] = EOS;
1.18      cvs      1603: } 
1.3       cvs      1604: 
1.4       cvs      1605: /*----------------------------------------------------------------------
1.9       cvs      1606:   IsSameHost                                                         
1.4       cvs      1607:   ----------------------------------------------------------------------*/
1.106     cvs      1608: ThotBool IsSameHost (const char *url1, const char *url2)
1.3       cvs      1609: {
1.106     cvs      1610:   char          *basename_ptr1, *basename_ptr2;
                   1611:   ThotBool       result;
1.3       cvs      1612: 
1.106     cvs      1613:   basename_ptr1 = AmayaParseUrl (url1, "",
                   1614:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1615:   basename_ptr2 = AmayaParseUrl (url2, "",
                   1616:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1617: 
1.106     cvs      1618:   if (strcmp (basename_ptr1, basename_ptr2))
                   1619:     result = FALSE;
                   1620:   else
                   1621:     result = TRUE;
                   1622:   TtaFreeMemory (basename_ptr1);
                   1623:   TtaFreeMemory (basename_ptr2);
                   1624:   return (result);
1.3       cvs      1625: }
                   1626: 
                   1627: 
1.4       cvs      1628: /*----------------------------------------------------------------------
1.22      cvs      1629:   HasKnownFileSuffix
                   1630:   returns TRUE if path points to a file ending with a suffix.
                   1631:   ----------------------------------------------------------------------*/
1.153     vatton   1632: ThotBool HasKnownFileSuffix (const char *path)
1.106     cvs      1633: {
                   1634:    char       *root;
                   1635:    char        temppath[MAX_LENGTH];
                   1636:    char        suffix[MAX_LENGTH];
1.22      cvs      1637: 
1.106     cvs      1638:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs      1639:      return (FALSE);
                   1640: 
1.106     cvs      1641:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1642: 
                   1643:    if (root) 
                   1644:      {
1.106     cvs      1645:        strcpy (temppath, root);
1.25      cvs      1646:        TtaFreeMemory (root);
1.22      cvs      1647:        /* Get the suffix */
1.124     vatton   1648:        TtaExtractSuffix (temppath, suffix); 
1.22      cvs      1649: 
1.106     cvs      1650:        if( suffix[0] == EOS)
1.22      cvs      1651:         /* no suffix */
                   1652:         return (FALSE);
                   1653: 
                   1654:        /* Normalize the suffix */
                   1655:        ConvertToLowerCase (suffix);
                   1656: 
1.106     cvs      1657:        if (!strcmp (suffix, "gz"))
1.22      cvs      1658:         /* skip the compressed suffix */
                   1659:         {
1.124     vatton   1660:         TtaExtractSuffix (temppath, suffix);
1.106     cvs      1661:         if(suffix[0] == EOS)
1.22      cvs      1662:           /* no suffix */
                   1663:           return (FALSE);
                   1664:          /* Normalize the suffix */
                   1665:          ConvertToLowerCase (suffix);
                   1666:         }
                   1667: 
1.106     cvs      1668:        if (strcmp (suffix, "gif") &&
                   1669:           strcmp (suffix, "xbm") &&
                   1670:           strcmp (suffix, "xpm") &&
                   1671:           strcmp (suffix, "jpg") &&
                   1672:           strcmp (suffix, "pdf") &&
                   1673:           strcmp (suffix, "png") &&
                   1674:           strcmp (suffix, "tgz") &&
                   1675:           strcmp (suffix, "xpg") &&
                   1676:           strcmp (suffix, "xpd") &&
                   1677:           strcmp (suffix, "ps") &&
                   1678:           strcmp (suffix, "au") &&
                   1679:           strcmp (suffix, "html") &&
                   1680:           strcmp (suffix, "htm") &&
                   1681:           strcmp (suffix, "shtml") &&
                   1682:           strcmp (suffix, "xht") &&
                   1683:           strcmp (suffix, "xhtm") &&
                   1684:           strcmp (suffix, "xhtml") &&
                   1685:           strcmp (suffix, "txt") &&
                   1686:           strcmp (suffix, "css") &&
                   1687:           strcmp (suffix, "eps"))
1.22      cvs      1688:         return (FALSE);
                   1689:        else
                   1690:         return (TRUE);
                   1691:      }
                   1692:    else
                   1693:      return (FALSE);
                   1694: }
                   1695: 
                   1696: 
                   1697: /*----------------------------------------------------------------------
1.24      cvs      1698:   ChopURL
                   1699:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1700:   If inputURL is  bigger than that size, outputURL receives
                   1701:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1702:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1703:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1704:   copied into outputURL. 
                   1705:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1706:   chars.
                   1707:   ----------------------------------------------------------------------*/
1.106     cvs      1708: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1709: {
                   1710:   int len;
1.9       cvs      1711: 
1.106     cvs      1712:   len = strlen (inputURL);
1.24      cvs      1713:   if (len <= MAX_PRINT_URL_LENGTH) 
1.106     cvs      1714:     strcpy (outputURL, inputURL);
1.24      cvs      1715:   else
                   1716:     /* make a truncated urlName on the status window */
                   1717:     {
1.106     cvs      1718:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1719:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1720:       strcat (outputURL, "...");
                   1721:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24      cvs      1722:     }
1.25      cvs      1723: }
                   1724: 
                   1725: 
                   1726: /*----------------------------------------------------------------------
                   1727:    scan
1.47      cvs      1728:        Scan a filename for its constituents
1.25      cvs      1729:        -----------------------------------
                   1730:   
                   1731:    On entry,
                   1732:        name    points to a document name which may be incomplete.
                   1733:    On exit,
                   1734:         absolute or relative may be nonzero (but not both).
                   1735:        host, fragment and access may be nonzero if they were specified.
                   1736:        Any which are nonzero point to zero terminated strings.
                   1737:   ----------------------------------------------------------------------*/
1.106     cvs      1738: static void scan (char *name, HTURI *parts)
1.25      cvs      1739: {
1.106     cvs      1740:   char *   p;
                   1741:   char *   after_access = name;
1.32      cvs      1742: 
1.43      cvs      1743:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1744:   /* Look for fragment identifier */
1.106     cvs      1745:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs      1746:     {
1.106     cvs      1747:       *p++ = '\0';
1.28      cvs      1748:       parts->fragment = p;
1.25      cvs      1749:     }
                   1750:     
1.28      cvs      1751:   for (p=name; *p; p++)
                   1752:     {
1.106     cvs      1753:       if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28      cvs      1754:        break;
1.106     cvs      1755:       if (*p == ':')
1.28      cvs      1756:        {
                   1757:          *p = 0;
                   1758:          parts->access = after_access; /* Scheme has been specified */
                   1759: 
                   1760:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1761:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1762:             not recommended. Rather, turn off "-O". */
                   1763: 
                   1764:          /*            after_access = p;*/
                   1765:          /*            while (*after_access == 0)*/
                   1766:          /*                after_access++;*/
                   1767:          after_access = p+1;
1.106     cvs      1768:          if (!strcasecmp("URL", parts->access))
1.28      cvs      1769:            /* Ignore IETF's URL: pre-prefix */
                   1770:            parts->access = NULL;
                   1771:          else
1.25      cvs      1772:            break;
                   1773:        }
                   1774:     }
                   1775:     
                   1776:     p = after_access;
1.43      cvs      1777:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1778:       {
1.43      cvs      1779:        if (p[1] == URL_SEP)
1.28      cvs      1780:          {
1.25      cvs      1781:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1782:            *p = 0;                     /* Terminate access             */
                   1783:            /* look for end of host name if any */
1.106     cvs      1784:            p = strchr (parts->host, URL_SEP);
1.28      cvs      1785:            if (p)
                   1786:              {
1.106     cvs      1787:                *p = EOS;                       /* Terminate host */
1.25      cvs      1788:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1789:              }
                   1790:          }
                   1791:        else
                   1792:          /* Root found but no host */
                   1793:          parts->absolute = p+1;
                   1794:       }
                   1795:     else
                   1796:       {
1.25      cvs      1797:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1798:       }
1.25      cvs      1799: }
                   1800: 
                   1801: 
                   1802: /*----------------------------------------------------------------------
1.28      cvs      1803:   AmayaParseUrl: parse a Name relative to another name
                   1804: 
                   1805:   This returns those parts of a name which are given (and requested)
                   1806:   substituting bits from the related name where necessary.
1.25      cvs      1807:   
1.28      cvs      1808:   On entry,
1.25      cvs      1809:        aName           A filename given
                   1810:         relatedName     A name relative to which aName is to be parsed. Give
                   1811:                         it an empty string if aName is absolute.
                   1812:         wanted          A mask for the bits which are wanted.
                   1813:   
1.28      cvs      1814:   On exit,
1.25      cvs      1815:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1816:   ----------------------------------------------------------------------*/
1.106     cvs      1817: char   *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
                   1818: {
                   1819:   char      *return_value;
                   1820:   char       result[MAX_LENGTH];
                   1821:   char       name[MAX_LENGTH];
                   1822:   char       rel[MAX_LENGTH];
                   1823:   char      *p, *access;
1.29      cvs      1824:   HTURI      given, related;
                   1825:   int        len;
1.106     cvs      1826:   char       used_sep;
                   1827:   char      *used_str;
1.32      cvs      1828: 
1.106     cvs      1829:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1830:     {
1.106     cvs      1831:       used_str = DIR_STR;
                   1832:       used_sep = DIR_SEP;
1.33      cvs      1833:     }
1.32      cvs      1834:   else
1.33      cvs      1835:     {
1.106     cvs      1836:       used_str = URL_STR;
                   1837:       used_sep = URL_SEP;
1.33      cvs      1838:     }
1.32      cvs      1839: 
1.29      cvs      1840:   /* Make working copies of input strings to cut up: */
                   1841:   return_value = NULL;
                   1842:   result[0] = 0;               /* Clear string  */
1.169     quint    1843:   rel[0] = EOS;
                   1844:   strncpy (name, aName, MAX_LENGTH - 1);
                   1845:   name[MAX_LENGTH - 1] = EOS;
                   1846:   if (relatedName != NULL)
                   1847:     {
                   1848:       strncpy (rel, relatedName, MAX_LENGTH - 1);
                   1849:       rel[MAX_LENGTH - 1] = EOS;
                   1850:     }
1.29      cvs      1851:   else
1.106     cvs      1852:     relatedName[0] = EOS;
1.29      cvs      1853:   
                   1854:   scan (name, &given);
                   1855:   scan (rel,  &related); 
                   1856:   access = given.access ? given.access : related.access;
                   1857:   if (wanted & AMAYA_PARSE_ACCESS)
                   1858:     if (access)
                   1859:       {
1.106     cvs      1860:        strcat (result, access);
1.29      cvs      1861:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1862:                strcat (result, ":");
1.29      cvs      1863:       }
                   1864:   
                   1865:   if (given.access && related.access)
                   1866:     /* If different, inherit nothing. */
1.106     cvs      1867:     if (strcmp (given.access, related.access) != 0)
1.29      cvs      1868:       {
                   1869:        related.host = 0;
                   1870:        related.absolute = 0;
                   1871:        related.relative = 0;
                   1872:        related.fragment = 0;
                   1873:       }
                   1874:   
                   1875:   if (wanted & AMAYA_PARSE_HOST)
                   1876:     if(given.host || related.host)
                   1877:       {
                   1878:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1879:          strcat (result, "//");
                   1880:        strcat (result, given.host ? given.host : related.host);
1.29      cvs      1881:       }
                   1882:   
                   1883:   if (given.host && related.host)
                   1884:     /* If different hosts, inherit no path. */
1.106     cvs      1885:     if (strcmp (given.host, related.host) != 0)
1.29      cvs      1886:       {
                   1887:        related.absolute = 0;
                   1888:        related.relative = 0;
                   1889:        related.fragment = 0;
                   1890:       }
                   1891:   
                   1892:   if (wanted & AMAYA_PARSE_PATH)
                   1893:     {
                   1894:       if (given.absolute)
                   1895:        {
                   1896:          /* All is given */
                   1897:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1898:            strcat (result, used_str);
                   1899:          strcat (result, given.absolute);
1.25      cvs      1900:        }
1.29      cvs      1901:       else if (related.absolute)
                   1902:        {
                   1903:          /* Adopt path not name */
1.106     cvs      1904:          strcat (result, used_str);
                   1905:          strcat (result, related.absolute);
1.29      cvs      1906:          if (given.relative)
                   1907:            {
                   1908:              /* Search part? */
1.106     cvs      1909:              p = strchr (result, '?');
1.29      cvs      1910:              if (!p)
1.106     cvs      1911:                p=result+strlen(result)-1;
1.33      cvs      1912:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1913:              /* Remove filename */
                   1914:              p[1]=0;
                   1915:              /* Add given one */
1.106     cvs      1916:              strcat (result, given.relative);
1.25      cvs      1917:            }
                   1918:        }
1.29      cvs      1919:       else if (given.relative)
                   1920:        /* what we've got */
1.106     cvs      1921:        strcat (result, given.relative);
1.29      cvs      1922:       else if (related.relative)
1.106     cvs      1923:        strcat (result, related.relative);
1.29      cvs      1924:       else
                   1925:        /* No inheritance */
1.106     cvs      1926:        strcat (result, used_str);
1.25      cvs      1927:     }
1.29      cvs      1928:   
                   1929:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1930:     if (given.fragment || related.fragment)
                   1931:       {
                   1932:        if (given.absolute && given.fragment)
                   1933:          {
                   1934:            /*Fixes for relURLs...*/
                   1935:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1936:              strcat (result, "#");
                   1937:            strcat (result, given.fragment); 
1.29      cvs      1938:          }
                   1939:        else if (!(given.absolute) && !(given.fragment))
1.106     cvs      1940:          strcat (result, "");
1.29      cvs      1941:        else
                   1942:          {
1.110     cvs      1943:           if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1944:              strcat (result, "#");
1.110     cvs      1945:           strcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1946:          }
                   1947:       }
1.106     cvs      1948:   len = strlen (result);
1.171     gully    1949:   if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106     cvs      1950:     strcpy (return_value, result);
1.29      cvs      1951:   return (return_value);               /* exactly the right length */
1.25      cvs      1952: }
                   1953: 
                   1954: /*----------------------------------------------------------------------
                   1955:      HTCanon
                   1956:        Canonicalizes the URL in the following manner starting from the host
                   1957:        pointer:
                   1958:   
                   1959:        1) The host name is converted to lowercase
                   1960:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1961:   
                   1962:        Return: OK      The position of the current path part of the URL
                   1963:                        which might be the old one or a new one.
                   1964:   
                   1965:   ----------------------------------------------------------------------*/
1.106     cvs      1966: static char   *HTCanon (char **filename, char *host)
                   1967: {
                   1968:     char   *newname = NULL;
                   1969:     char    used_sep;
                   1970:     char   *path;
                   1971:     char   *strptr;
                   1972:     char   *port;
                   1973:     char   *access = host-3;
                   1974:   
                   1975:      if (*filename && strchr (*filename, URL_SEP))
                   1976:         used_sep = URL_SEP;
1.33      cvs      1977:      else
1.106     cvs      1978:         used_sep = DIR_SEP;
1.32      cvs      1979:   
1.110     cvs      1980:     while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25      cvs      1981:        access--;
1.110     cvs      1982:     if ((path = strchr (host, used_sep)) == NULL)              /* Find path */
1.106     cvs      1983:        path = host + strlen (host);
                   1984:     if ((strptr = strchr (host, '@')) != NULL && strptr < path)           /* UserId */
1.82      cvs      1985:        host = strptr;
1.110     cvs      1986:     if ((port = strchr (host, ':')) != NULL && port > path)   /* Port number */
1.82      cvs      1987:        port = NULL;
1.25      cvs      1988: 
                   1989:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1990:     while (strptr < path)
1.33      cvs      1991:       {
1.123     vatton   1992:          *strptr = tolower (*strptr);
1.82      cvs      1993:          strptr++;
1.33      cvs      1994:       }
1.25      cvs      1995:     
                   1996:     /* Does the URL contain a full domain name? This also works for a
                   1997:        numerical host name. The domain name is already made lower-case
                   1998:        and without a trailing dot. */
                   1999:     {
1.106     cvs      2000:       char  *dot = port ? port : path;
                   2001:       if (dot > *filename && *--dot == '.')
1.33      cvs      2002:        {
1.106     cvs      2003:          char  *orig = dot;
                   2004:          char  *dest = dot + 1;
1.82      cvs      2005:          while ((*orig++ = *dest++));
                   2006:             if (port) port--;
1.33      cvs      2007:          path--;
1.25      cvs      2008:        }
                   2009:     }
                   2010:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      2011:     if (port)
                   2012:       {
1.82      cvs      2013:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      2014:          {
                   2015:            if (!newname)
                   2016:              {
1.106     cvs      2017:                char  *orig = port; 
                   2018:                char  *dest = port + 1;
1.82      cvs      2019:                while ((*orig++ = *dest++));
1.33      cvs      2020:              }
                   2021:          }
1.106     cvs      2022:        else if ((!strncmp (access, "http", 4)   &&
                   2023:              (*(port + 1) == '8'                    && 
                   2024:              *(port+2) == '0'                       && 
1.82      cvs      2025:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.106     cvs      2026:              (!strncmp (access, "gopher", 6) &&
                   2027:              (*(port+1) == '7'                      && 
                   2028:              *(port+2) == '0'                       && 
1.82      cvs      2029:              (*(port+3) == used_sep || !*(port+3))))         ||
1.106     cvs      2030:              (!strncmp (access, "ftp", 3)    &&
                   2031:              (*(port+1) == '2'                      && 
                   2032:              *(port + 2) == '1'                     && 
1.82      cvs      2033:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      2034:          if (!newname)
                   2035:            {
1.106     cvs      2036:              char  *orig = port; 
                   2037:              char  *dest = port + 3;
1.33      cvs      2038:              while((*orig++ = *dest++));
                   2039:              /* Update path position, Henry Minsky */
                   2040:              path -= 3;
1.25      cvs      2041:            }
1.33      cvs      2042:        }
                   2043:        else if (newname)
1.106     cvs      2044:          strncat (newname, port, (int) (path - port));
1.33      cvs      2045:       }
1.25      cvs      2046: 
1.33      cvs      2047:     if (newname)
                   2048:       {
1.106     cvs      2049:        char  *newpath = newname + strlen (newname);
                   2050:        strcat (newname, path);
1.25      cvs      2051:        path = newpath;
1.28      cvs      2052:        /* Free old copy */
                   2053:        TtaFreeMemory(*filename);
1.25      cvs      2054:        *filename = newname;
1.33      cvs      2055:       }
1.25      cvs      2056:     return path;
                   2057: }
                   2058: 
                   2059: 
                   2060: /*----------------------------------------------------------------------
1.29      cvs      2061:   SimplifyUrl: simplify a URI
1.32      cvs      2062:   A URI is allowed to contain the sequence xxx/../ which may be
                   2063:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      2064:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      2065:   
1.28      cvs      2066:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   2067:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      2068:   
1.28      cvs      2069:   but we should NOT change
                   2070:                 http://fred.xxx.edu/../..
1.25      cvs      2071:   
                   2072:        or      ../../albert.html
                   2073:   
1.28      cvs      2074:   In order to avoid empty URLs the following URLs become:
1.25      cvs      2075:   
                   2076:                /fred/..                becomes /fred/..
                   2077:                /fred/././..            becomes /fred/..
                   2078:                /fred/.././junk/.././   becomes /fred/..
                   2079:   
1.28      cvs      2080:   If more than one set of `://' is found (several proxies in cascade) then
                   2081:   only the part after the last `://' is simplified.
1.25      cvs      2082:   
1.28      cvs      2083:   Returns: A string which might be the old one or a new one.
1.25      cvs      2084:   ----------------------------------------------------------------------*/
1.106     cvs      2085: void         SimplifyUrl (char **url)
                   2086: {
                   2087:   char   *path;
                   2088:   char   *access;
                   2089:   char   *newptr; 
                   2090:   char   *p;
                   2091:   char   *orig, *dest, *end;
1.28      cvs      2092: 
1.106     cvs      2093:   char      used_sep;
1.77      cvs      2094:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   2095:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      2096: 
1.28      cvs      2097:   if (!url || !*url)
                   2098:     return;
                   2099: 
1.106     cvs      2100:   if (strchr (*url, URL_SEP))
                   2101:       used_sep = URL_SEP;
1.32      cvs      2102:   else
1.106     cvs      2103:       used_sep = DIR_SEP;
1.32      cvs      2104: 
1.77      cvs      2105:   /* should we simplify double dot? */
                   2106:   path = *url;
1.106     cvs      2107:   if (*path == '.' && *(path + 1) == '.')
1.77      cvs      2108:     ddot_simplify = FALSE;
                   2109:   else
                   2110:     ddot_simplify = TRUE;
                   2111: 
1.28      cvs      2112:   /* Find any scheme name */
1.106     cvs      2113:   if ((path = strstr (*url, "://")) != NULL)
1.33      cvs      2114:     {
                   2115:       /* Find host name */
1.28      cvs      2116:       access = *url;
1.123     vatton   2117:       while (access < path && (*access = tolower (*access)))
1.82      cvs      2118:             access++;
1.28      cvs      2119:       path += 3;
1.106     cvs      2120:       while ((newptr = strstr (path, "://")) != NULL)
1.82      cvs      2121:             /* For proxies */
1.106     cvs      2122:             path = newptr + 3;
1.82      cvs      2123:      /* We have a host name */
1.84      cvs      2124:       path = HTCanon (url, path);
1.25      cvs      2125:     }
1.106     cvs      2126:   else if ((path = strstr (*url, ":/")) != NULL)
1.28      cvs      2127:     path += 2;
                   2128:   else
                   2129:     path = *url;
1.84      cvs      2130:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      2131:     /* Some URLs start //<foo> */
                   2132:     path += 1;
1.94      cvs      2133:   else if (IsFilePath (path))
                   2134:     {
                   2135:       /* doesn't need to do anything more */
                   2136:       return;
                   2137:     }
1.106     cvs      2138:   else if (!strncmp (path, "news:", 5))
1.28      cvs      2139:     {
1.106     cvs      2140:       newptr = strchr (path+5, '@');
1.28      cvs      2141:       if (!newptr)
                   2142:        newptr = path + 5;
                   2143:       while (*newptr)
                   2144:        {
                   2145:          /* Make group or host lower case */
1.123     vatton   2146:          *newptr = tolower (*newptr);
1.28      cvs      2147:          newptr++;
1.25      cvs      2148:        }
1.28      cvs      2149:       /* Doesn't need to do any more */
                   2150:       return;
1.25      cvs      2151:     }
1.126     cheyroul 2152: 
1.189     vatton   2153:   /* remove initial multiple /'s */
                   2154:   p = path - 1;
                   2155:   if (path != *url && *p == used_sep && *path == used_sep)
                   2156:     {
                   2157:       while (*path == used_sep)
                   2158:        {
                   2159:          orig = path;
                   2160:          dest = path + 1;
                   2161:          while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   2162:          end = orig-1;
                   2163:        }
                   2164:     }
                   2165: 
                   2166:   if (path)
1.28      cvs      2167:     {
1.106     cvs      2168:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   2169:            (end = strchr (path, '#'))))
                   2170:        end = path + strlen (path);
1.28      cvs      2171:       
                   2172:       /* Parse string second time to simplify */
                   2173:       p = path;
                   2174:       while (p < end)
                   2175:        {
1.110     cvs      2176:          /* if we're pointing to a char, it's safe to reactivate the 
                   2177:             ../ convertion */
1.106     cvs      2178:          if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77      cvs      2179:            ddot_simplify = TRUE;
                   2180: 
1.33      cvs      2181:          if (*p==used_sep)
1.28      cvs      2182:            {
1.106     cvs      2183:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      2184:                {
                   2185:                  orig = p + 1;
1.84      cvs      2186:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      2187:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      2188:                  end = orig - 1;
                   2189:                }
1.106     cvs      2190:              else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.' 
1.77      cvs      2191:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      2192:                {
                   2193:                  newptr = p;
1.52      cvs      2194:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   2195:                  if (*newptr == used_sep)
                   2196:                    orig = newptr + 1;
1.28      cvs      2197:                  else
1.52      cvs      2198:                    orig = newptr;
                   2199: 
                   2200:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   2201:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   2202:                  end = orig-1;
                   2203:                  /* Start again with prev slash */
                   2204:                  p = newptr;
1.28      cvs      2205:                }
1.33      cvs      2206:              else if (*(p+1) == used_sep)
1.28      cvs      2207:                {
1.33      cvs      2208:                  while (*(p+1) == used_sep)
1.28      cvs      2209:                    {
                   2210:                      orig = p;
                   2211:                      dest = p + 1;
                   2212:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   2213:                      end = orig-1;
                   2214:                    }
                   2215:                }
                   2216:              else
1.25      cvs      2217:                p++;
1.28      cvs      2218:            }
                   2219:          else
                   2220:            p++;
1.25      cvs      2221:        }
                   2222:     }
1.51      cvs      2223:     /*
                   2224:     **  Check for host/../.. kind of things
                   2225:     */
1.106     cvs      2226:     if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.' 
1.77      cvs      2227:        && (!*(path+3) || *(path+3) == used_sep))
1.106     cvs      2228:        *(path+1) = EOS;
1.28      cvs      2229:   return;
                   2230: }
                   2231: 
                   2232: 
                   2233: /*----------------------------------------------------------------------
1.96      cvs      2234:    NormalizeFile normalizes local names.                             
1.28      cvs      2235:    Return TRUE if target and src differ.                           
                   2236:   ----------------------------------------------------------------------*/
1.106     cvs      2237: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28      cvs      2238: {
1.184     gully    2239: #ifndef _WINDOWS
1.106     cvs      2240:    char             *s;
1.93      cvs      2241:    int               i;
1.184     gully    2242: #endif /* _WINDOWS */
1.82      cvs      2243:    ThotBool          change;
1.90      cvs      2244:    int               start_index; /* the first char that we'll copy */
1.28      cvs      2245: 
1.54      cvs      2246:    change = FALSE;
1.90      cvs      2247:    start_index = 0;
                   2248: 
1.106     cvs      2249:    if (!src || src[0] == EOS)
1.96      cvs      2250:      {
1.106     cvs      2251:        target[0] = EOS;
1.96      cvs      2252:        return FALSE;
                   2253:      }
1.90      cvs      2254: 
                   2255:    /* @@ do I need file: or file:/ here? */
1.106     cvs      2256:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      2257:      {
1.90      cvs      2258:        /* remove the prefix file: */
                   2259:        start_index += 5;
                   2260:    
                   2261:        /* remove the localhost prefix */
1.106     cvs      2262:        if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94      cvs      2263:           start_index += 11;
                   2264:        
                   2265:        /* remove the first two slashes in / / /path */
                   2266:        while (src[start_index] &&
1.106     cvs      2267:              src[start_index] == '/' 
                   2268:              && src[start_index + 1] == '/')
1.94      cvs      2269:         start_index++;
                   2270: 
1.184     gully    2271: #ifdef _WINDOWS
1.94      cvs      2272:        /* remove any extra slash before the drive name */
1.106     cvs      2273:        if (src[start_index] == '/'
                   2274:           &&src[start_index+2] == ':')
1.94      cvs      2275:         start_index++;
1.184     gully    2276: #endif /* _WINDOWS */
1.90      cvs      2277: 
1.106     cvs      2278:        if (src[start_index] == EOS)
1.90      cvs      2279:        /* if there's nothing afterwards, add a DIR_STR */
1.106     cvs      2280:         strcpy (target, DIR_STR);
1.90      cvs      2281:        else
1.97      cvs      2282:         /* as we're inside a file: URL, we'll apply all the convertions
                   2283:            we know */
                   2284:         CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96      cvs      2285: 
                   2286:        change = TRUE;
                   2287:      }
1.97      cvs      2288:    else if (convertion != AM_CONV_NONE)
1.96      cvs      2289:      {
                   2290:        /* we are following a "local" relative link, we do all the
                   2291:          convertions except for the HOME_DIR ~ one */
1.97      cvs      2292:        CleanCopyFileURL (target, src, convertion);
1.28      cvs      2293:      }
1.184     gully    2294: #ifndef _WINDOWS
1.106     cvs      2295:    else if (src[0] == '~')
1.53      cvs      2296:      {
1.96      cvs      2297:        /* it must be a URL typed in a text input field */
                   2298:        /* do the HOME_DIR ~ substitution */
1.82      cvs      2299:        s = TtaGetEnvString ("HOME");
1.106     cvs      2300:        strcpy (target, s);
1.90      cvs      2301: #if 0
1.96      cvs      2302:        /* JK: invalidated this part of the code as it's simpler
                   2303:           to add the DIR_SEP whenever we have something to add
                   2304:           to the path rather than adding it systematically */
1.106     cvs      2305:        if (src[1] != DIR_SEP)
                   2306:          strcat (target, DIR_STR);
1.90      cvs      2307: #endif
1.106     cvs      2308:        i = strlen (target);
                   2309:        strcpy (&target[i], &src[1]);
1.54      cvs      2310:        change = TRUE;
1.53      cvs      2311:      }
1.184     gully    2312: #endif /* _WINDOWS */
1.28      cvs      2313:    else
1.96      cvs      2314:    /* leave it as it is */
1.106     cvs      2315:      strcpy (target, src);
1.96      cvs      2316:    
1.28      cvs      2317:    /* remove /../ and /./ */
1.29      cvs      2318:    SimplifyUrl (&target);
1.54      cvs      2319:    if (!change)
1.191     gully    2320:      change = (strcmp (src, target) != 0);
1.28      cvs      2321:    return (change);
1.25      cvs      2322: }
                   2323: 
1.28      cvs      2324: 
1.25      cvs      2325: /*----------------------------------------------------------------------
1.31      cvs      2326:   MakeRelativeURL: make relative name
1.25      cvs      2327:   
1.28      cvs      2328:   This function creates and returns a string which gives an expression of
                   2329:   one address as related to another. Where there is no relation, an absolute
                   2330:   address is retured.
1.25      cvs      2331:   
1.28      cvs      2332:   On entry,
1.25      cvs      2333:        Both names must be absolute, fully qualified names of nodes
                   2334:        (no fragment bits)
                   2335:   
1.28      cvs      2336:   On exit,
1.25      cvs      2337:        The return result points to a newly allocated name which, if
                   2338:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   2339:        The caller is responsible for freeing the resulting name later.
                   2340:   ----------------------------------------------------------------------*/
1.106     cvs      2341: char      *MakeRelativeURL (char *aName, char *relatedName)
                   2342: {
                   2343:   char  *return_value;
                   2344:   char   result[MAX_LENGTH];
                   2345:   char  *p;
                   2346:   char  *q;
                   2347:   char  *after_access;
                   2348:   char  *last_slash = NULL;
                   2349:   int    slashes, levels, len;
1.184     gully    2350: #ifdef _WINDOWS
1.44      cvs      2351:   int ndx;
1.184     gully    2352: #endif /* _WINDOWS */
1.44      cvs      2353: 
1.29      cvs      2354:   if (aName == NULL || relatedName == NULL)
                   2355:     return (NULL);
                   2356: 
                   2357:   slashes = 0;
                   2358:   after_access = NULL;
                   2359:   p = aName;
                   2360:   q = relatedName;
1.147     vatton   2361:   len = 0;
                   2362:   for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27      cvs      2363:     {
                   2364:       /* Find extent of match */
1.106     cvs      2365:       if (*p == ':')
1.146     cvs      2366:          {
1.168     cvs      2367:                after_access = p + 1;
1.184     gully    2368: #ifdef _WINDOWS
1.168     cvs      2369:            if (len == 1)
                   2370:                {
                   2371:              /* it's a local Windows path like c:... */
                   2372:              slashes+=2;
                   2373:                }
1.184     gully    2374: #endif /* _WINDOWS */
1.168     cvs      2375:          }
                   2376:       if (*p == DIR_SEP)
                   2377:          {
                   2378:            /* memorize the last slash position and count them */
                   2379:            last_slash = p;
1.147     vatton   2380:            slashes++;
1.146     cvs      2381:          }
1.25      cvs      2382:     }
                   2383:     
1.31      cvs      2384:   /* q, p point to the first non-matching character or zero */
1.106     cvs      2385:   if (*q == EOS)
1.31      cvs      2386:     {
                   2387:       /* New name is a subset of the related name */
                   2388:       /* exactly the right length */
1.106     cvs      2389:       len = strlen (p);
1.171     gully    2390:       if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106     cvs      2391:        strcpy (return_value, p);
1.31      cvs      2392:     }
                   2393:   else if ((slashes < 2 && after_access == NULL)
                   2394:       || (slashes < 3 && after_access != NULL))
1.168     cvs      2395:    {
1.31      cvs      2396:       /* Two names whitout common path */
                   2397:       /* exactly the right length */
1.106     cvs      2398:       len = strlen (aName);
1.171     gully    2399:       if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106     cvs      2400:        strcpy (return_value, aName);
1.31      cvs      2401:     }
                   2402:   else
                   2403:     {
                   2404:       /* Some path in common */
1.106     cvs      2405:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31      cvs      2406:        /* just the same server */
1.106     cvs      2407:        strcpy (result, last_slash);
1.31      cvs      2408:       else
                   2409:        {
                   2410:          levels= 0; 
1.106     cvs      2411:          for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31      cvs      2412:            if (*q == DIR_SEP)
                   2413:              levels++;
                   2414:          
1.106     cvs      2415:          result[0] = EOS;
1.31      cvs      2416:          for (;levels; levels--)
1.106     cvs      2417:            strcat (result, "../");
                   2418:          strcat (result, last_slash+1);
1.31      cvs      2419:        } 
1.52      cvs      2420: 
                   2421:       if (!*result)
1.106     cvs      2422:        strcat (result, "./");
1.52      cvs      2423: 
1.31      cvs      2424:       /* exactly the right length */
1.106     cvs      2425:       len = strlen (result);
1.171     gully    2426:       if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106     cvs      2427:        strcpy (return_value, result);
1.52      cvs      2428: 
1.25      cvs      2429:     }
1.184     gully    2430: #ifdef _WINDOWS
1.106     cvs      2431:   len = strlen (return_value);
1.168     cvs      2432:    for (ndx = 0; ndx < len; ndx ++)
1.106     cvs      2433:          if (return_value[ndx] == '\\')
                   2434:             return_value[ndx] = '/' ;
1.184     gully    2435: #endif /* _WINDOWS */
1.29      cvs      2436:   return (return_value);
1.24      cvs      2437: }
1.35      cvs      2438: 
1.104     kahan    2439: /*----------------------------------------------------------------------
                   2440:   AM_GetFileSize
                   2441:   Returns TRUE and the filesize in the 2nd parameter.
                   2442:   Otherwise, in case of a system error, returns FALSE, with a 
                   2443:   filesize of 0L.
                   2444:   ---------------------------------------------------------------------*/
1.106     cvs      2445: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104     kahan    2446: {
                   2447:   if (!TtaFileExist (filename))
                   2448:     return FALSE;
1.185     vatton   2449:   *file_size = TtaGetFileSize (filename);
                   2450:   return TRUE;
1.104     kahan    2451: }
1.139     kahan    2452: 
                   2453: /*----------------------------------------------------------------------
                   2454:   AM_UseXHTMLMimeType
                   2455:   Returns TRUE if the user has configured Amaya to use this MIME type,
                   2456:   FALSE otherwise.
                   2457:   ---------------------------------------------------------------------*/
                   2458: ThotBool AM_UseXHTMLMimeType (void)
                   2459: {
                   2460:   ThotBool xhtml_mimetype;
                   2461:   
                   2462:   /* does the user wants to use the new MIME type? */
                   2463:   TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
                   2464: 
                   2465:   return (xhtml_mimetype);
1.152     kahan    2466: }
                   2467: 
1.154     kahan    2468: 
                   2469: /********************************************
                   2470:  The following routines were adapted from the GNU libc functions
                   2471:  for generating a tmpnam.
                   2472: *********************************************/
                   2473: 
                   2474: /* These are the characters used in temporary filenames.  */
                   2475: static const char letters[] =
                   2476: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                   2477: 
                   2478: /* Generate a temporary file name based on TMPL.  TMPL must match the
                   2479:    rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
                   2480:    does not exist at the time of the call to __gen_tempname.  TMPL is
                   2481:    overwritten with the result.
                   2482: 
                   2483:    We use a clever algorithm to get hard-to-predict names. */
                   2484: void
                   2485: AM_gen_tempname (char *tmpl)
                   2486: {
                   2487:   int len;
                   2488:   char *XXXXXX;
                   2489:   static uint64_t value;
                   2490:   uint64_t random_time_bits;
                   2491:   unsigned int count;
                   2492:   int save_errno = errno;
                   2493:   struct stat st;
                   2494: 
                   2495:   /* A lower bound on the number of temporary files to attempt to
                   2496:      generate.  The maximum total number of temporary file names that
                   2497:      can exist for a given template is 62**6.  It should never be
                   2498:      necessary to try all these combinations.  Instead if a reasonable
                   2499:      number of names is tried (we define reasonable as 62**3) fail to
                   2500:      give the system administrator the chance to remove the problems.  */
                   2501:   unsigned int attempts_min = 62 * 62 * 62;
                   2502: 
                   2503:   /* The number of times to attempt to generate a temporary file.  To
                   2504:      conform to POSIX, this must be no smaller than TMP_MAX.  */
                   2505:   unsigned int attempts = attempts_min < TMP_MAX ? TMP_MAX : attempts_min;
                   2506: 
                   2507:   len = strlen (tmpl);
                   2508:   if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
                   2509:     {
                   2510:       /* @@ JK ? */
                   2511:       errno = EINVAL;
                   2512:       return;
                   2513:     }
                   2514: 
                   2515:   /* This is where the Xs start.  */
                   2516:   XXXXXX = &tmpl[len - 6];
                   2517: 
                   2518:   /* Get some more or less random data.  */
                   2519: #ifdef RANDOM_BITS
                   2520:   RANDOM_BITS (random_time_bits);
                   2521: #else
                   2522: # if HAVE_GETTIMEOFDAY || _LIBC
                   2523:   {
                   2524:     struct timeval tv;
                   2525:     gettimeofday (&tv, NULL);
                   2526:     random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
                   2527:   }
                   2528: # else
                   2529:   random_time_bits = time (NULL);
                   2530: # endif
                   2531: #endif
                   2532:   value += random_time_bits ^ getpid ();
                   2533: 
                   2534:   for (count = 0; count < attempts; value += 7777, ++count)
                   2535:     {
                   2536:       uint64_t v = value;
                   2537: 
                   2538:       /* Fill in the random bits.  */
                   2539:       XXXXXX[0] = letters[v % 62];
                   2540:       v /= 62;
                   2541:       XXXXXX[1] = letters[v % 62];
                   2542:       v /= 62;
                   2543:       XXXXXX[2] = letters[v % 62];
                   2544:       v /= 62;
                   2545:       XXXXXX[3] = letters[v % 62];
                   2546:       v /= 62;
                   2547:       XXXXXX[4] = letters[v % 62];
                   2548:       v /= 62;
                   2549:       XXXXXX[5] = letters[v % 62];
                   2550: 
                   2551:       /* This case is backward from the other three.  AM_gen_tempname
                   2552:         succeeds if __xstat fails because the name does not exist.
                   2553:         Note the continue to bypass the common logic at the bottom
                   2554:         of the loop.  */
                   2555:       if (stat (tmpl, &st) < 0)
                   2556:          break;
                   2557: 
                   2558:       continue;
                   2559:     }
                   2560:   
                   2561:   if (count == attempts || errno != ENOENT)
                   2562:     tmpl[0] = EOS;
                   2563:   else
                   2564:     errno = save_errno;
                   2565: 
                   2566:   return;
                   2567: }
                   2568: 
                   2569: #define JOSE 1
                   2570: 
1.152     kahan    2571: /*-----------------------------------------------------------------------
                   2572:   GetTempName
                   2573:   Front end to the Unix tempnam function, which is independent of the
                   2574:   value of the TMPDIR env value 
                   2575:   Returns a dynamically allocated string with a tempname. The user
                   2576:   must free this memory.
                   2577:   -----------------------------------------------------------------------*/
                   2578: char *GetTempName (const char *dir, const char *prefix)
                   2579: {
1.154     kahan    2580: #ifdef JOSE
                   2581: 
1.162     kahan    2582:   static char tmpbufmem[PATH_MAX + 1];
1.154     kahan    2583:   int len;
                   2584:   int i;
                   2585: 
1.155     cvs      2586:   if (!dir || *dir == EOS || !TtaDirExists (dir))
1.154     kahan    2587:     return NULL;
                   2588: 
1.162     kahan    2589:   /* make sure that the name is no bigger than PATH_MAX + the 6 tempname chars we
                   2590:    will add */
1.154     kahan    2591: 
1.156     cvs      2592:   len = strlen (dir);
1.162     kahan    2593:   if (len + 6 > PATH_MAX)
1.154     kahan    2594:     return NULL;
                   2595: 
                   2596:   /* copy the dir name, and add a DIR_SEP if it's missing */
                   2597:   if (dir[strlen (dir) - 1] == DIR_SEP)
                   2598:     strcpy (tmpbufmem, dir);
                   2599:   else
1.156     cvs      2600:   {
1.154     kahan    2601:     sprintf (tmpbufmem, "%s%c", dir, DIR_SEP);
1.156     cvs      2602:        len++;
                   2603:   }
1.154     kahan    2604: 
1.161     kahan    2605:   /* copy the prefix (no more than L_tmpnam chars, to respect POSIX). Save
1.156     cvs      2606:      space for the 6 X and EOS chars that will become the random bits */
                   2607:   if (prefix)
                   2608:   { 
                   2609:       i = 0;
1.167     cheyroul 2610:          while (prefix[i] != EOS && i < L_tmpnam - 8)
1.156     cvs      2611:            tmpbufmem[len++] = prefix[i++];
                   2612:          tmpbufmem[len] = EOS;
                   2613:   }
                   2614: 
                   2615:   /* Add the 6 X chars */
                   2616:   len = strlen (tmpbufmem);
                   2617:   i = 0;
                   2618:   while (i < 6)
                   2619:   {
                   2620:        tmpbufmem[len++] = 'X';
                   2621:        i++;
                   2622:   }     
                   2623:   tmpbufmem[len] = EOS;
1.154     kahan    2624: 
                   2625:   AM_gen_tempname (tmpbufmem);
                   2626: 
                   2627:   if (tmpbufmem[0] == EOS)
                   2628:     return NULL;
                   2629:   else
                   2630:     return (TtaStrdup (tmpbufmem));
                   2631: 
                   2632: #else
1.152     kahan    2633:   char *tmpdir;
1.181     vatton   2634:   char *tmp = NULL;
1.152     kahan    2635:   char *name = NULL;
                   2636: 
                   2637:   /* save the value of TMPDIR */
1.181     vatton   2638:   tmpdir = getenv ("TMPDIR");
1.152     kahan    2639:   if (tmpdir)
                   2640:     {
1.181     vatton   2641:       /* remove TMPDIR from the environment */
                   2642:       tmp = TtaGetMemory (strlen (tmpdir) + 20);
                   2643:       sprintf (tmp, "TMPDIR=");
1.184     gully    2644: #ifdef _WINDOWS
1.152     kahan    2645:       _putenv (tmp);
                   2646: #else
                   2647:       putenv (tmp);
1.184     gully    2648: #endif /* _WINDOWS */
1.181     vatton   2649:       /* prepare the string to restore the value of TMPDIR */
                   2650:       strrcat (tmp, tmpdir);
1.152     kahan    2651:     }
                   2652: 
                   2653:   /* create the tempname */
1.184     gully    2654: #ifdef _WINDOWS
1.152     kahan    2655:   /* Under Windows, _tempnam returns the same name until the file is created */
                   2656:   {
                   2657:     char *altprefix;
                   2658:     name = tmpnam (NULL);      /* get a possibly unique string */
1.181     vatton   2659:     altprefix = TtaGetMemory (strlen (prefix) + strlen(name) + 1);
1.152     kahan    2660:     sprintf (altprefix, "%s%s", prefix, name + strlen(_P_tmpdir));
                   2661:     name = _tempnam (dir, altprefix); /* get a name that isn't yet in use */
                   2662:     TtaFreeMemory (altprefix);
                   2663:   }
                   2664: #else
                   2665:   name = tempnam (dir, prefix);
1.184     gully    2666: #endif /* _WINDOWS */
1.152     kahan    2667: 
                   2668:   if (tmpdir)
                   2669:     {
1.181     vatton   2670:       /* restore the value of TMPDIR */
1.184     gully    2671: #ifdef _WINDOWS
1.152     kahan    2672:       _putenv (tmpdir);
                   2673: #else
                   2674:       putenv (tmpdir);
1.184     gully    2675: #endif /* _WINDOWS */
1.181     vatton   2676:       TtaFreeMemory (tmpdir);
1.152     kahan    2677:     }
                   2678:   return (name);
1.154     kahan    2679: #endif
1.139     kahan    2680: }

Webmaster