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

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

Webmaster