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

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

Webmaster