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

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

Webmaster