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

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

Webmaster