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

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

Webmaster