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

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

Webmaster