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

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

Webmaster