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

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

Webmaster