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

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

Webmaster