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

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

Webmaster