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

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

Webmaster