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

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

Webmaster