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

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

Webmaster