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

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: 
1.153   ! vatton   1076:   if (url)
1.40      cvs      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: 
1.153   ! vatton   1095:       if (doc == 0)
        !          1096:        {
        !          1097:          n = strrchr (documentname, '.');
        !          1098:          if (n)
        !          1099:            *n = EOS;
        !          1100:          if (documentname[0] == EOS)
        !          1101:            strcpy (documentname, "noname");
        !          1102:          n = GetTempName (ptr, documentname);
        !          1103:          TtaFreeMemory (ptr);
        !          1104:          ptr = n;
        !          1105:        }
1.69      cvs      1106:       else
1.153   ! vatton   1107:        {
        !          1108:          /* don't include the query string within document name */
        !          1109:          n = strrchr (documentname, '?');
        !          1110:          if (n)
        !          1111:            *n = EOS;
        !          1112:          /* don't include ':' within document name */
        !          1113:          n = strchr (documentname, ':');
        !          1114:          if (n)
        !          1115:            *n = EOS;
        !          1116:          /* if after all this operations document name
        !          1117:             is empty, let's use noname.html instead */
        !          1118:          if (documentname[0] == EOS)
        !          1119:            strcat (ptr, "noname.html");
        !          1120:          else
        !          1121:            strcat (ptr, documentname);
        !          1122:        }
1.40      cvs      1123:       TtaFreeMemory (documentname);
                   1124:       /* restore the url */
                   1125:       if (noFile)
1.41      cvs      1126:        url[len] = url_sep;
1.40      cvs      1127:       return (ptr);
                   1128:     }
                   1129:   else
                   1130:     return (NULL);
                   1131: }
                   1132: 
1.73      cvs      1133: /*----------------------------------------------------------------------
1.79      cvs      1134:    ExtractTarget extract the target name from document nane.        
                   1135:   ----------------------------------------------------------------------*/
1.150     vatton   1136: void ExtractTarget (char *aName, char *target)
1.79      cvs      1137: {
1.106     cvs      1138:    int    lg, i;
                   1139:    char  *ptr;
                   1140:    char  *oldptr;
1.79      cvs      1141: 
                   1142:    if (!target || !aName)
                   1143:      /* bad target */
                   1144:      return;
                   1145: 
1.106     cvs      1146:    target[0] = EOS;
                   1147:    lg = strlen (aName);
1.79      cvs      1148:    if (lg)
                   1149:      {
                   1150:        /* the name is not empty */
                   1151:        oldptr = ptr = &aName[0];
                   1152:        do
                   1153:          {
1.106     cvs      1154:             ptr = strrchr (oldptr, '#');
1.79      cvs      1155:             if (ptr)
                   1156:                oldptr = &ptr[1];
                   1157:          }
                   1158:        while (ptr);
                   1159: 
                   1160:        i = (int) (oldptr) - (int) (aName);     /* name length */
                   1161:        if (i > 1)
                   1162:          {
1.106     cvs      1163:             aName[i - 1] = EOS;
1.79      cvs      1164:             if (i != lg)
1.106     cvs      1165:                strcpy (target, oldptr);
1.79      cvs      1166:          }
                   1167:      }
                   1168: }
                   1169: 
                   1170: /*----------------------------------------------------------------------
1.90      cvs      1171:    RemoveNewLines (text)
                   1172:    Removes any '\n' chars that are found in text. 
                   1173:    Returns TRUE if it did the operation, FALSE otherwise.
1.73      cvs      1174:   ----------------------------------------------------------------------*/
1.106     cvs      1175: ThotBool RemoveNewLines (char *text)
                   1176: {
                   1177:   ThotBool   change = FALSE;
                   1178:   char      *src;
                   1179:   char      *dest;
1.90      cvs      1180: 
                   1181:   src = text;
                   1182:   dest = text;
1.115     kahan    1183: 
                   1184:   /* remove any preceding whitespace */
                   1185:   while (*src && *src == ' ')
                   1186:     {
                   1187:       src++;
                   1188:       change = 1;
                   1189:     }
                   1190:   
1.90      cvs      1191:   while (*src)
                   1192:     {
                   1193:       switch (*src)
                   1194:        {
1.106     cvs      1195:        case '\n':
1.90      cvs      1196:          /* don't copy the newline */
                   1197:          change = 1;
                   1198:          break;
                   1199:        default:
                   1200:          *dest = *src;
                   1201:          dest++;
                   1202:          break;
                   1203:        }
                   1204:       src++;
                   1205:     }
                   1206:   /* copy the last EOS char */
                   1207:   *dest = *src;
                   1208: 
                   1209:   return (change);
                   1210: }
                   1211: 
                   1212: /*----------------------------------------------------------------------
                   1213:    CleanCopyFileURL
                   1214:    Copies a file url from a src string to destination string.
1.97      cvs      1215:    convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
                   1216:    we want to do).
1.90      cvs      1217:   ----------------------------------------------------------------------*/
1.106     cvs      1218: static void CleanCopyFileURL (char *dest, char *src,
                   1219:                              ConvertionType convertion)
1.90      cvs      1220: {
                   1221:   while (*src)
1.89      cvs      1222:     {
1.90      cvs      1223:       switch (*src)
1.89      cvs      1224:        {
                   1225: #ifdef _WINDOWS
1.106     cvs      1226:        case URL_SEP:
1.96      cvs      1227:          /* make DIR_SEP transformation */
1.97      cvs      1228:          if (convertion & AM_CONV_URL_SEP)
1.106     cvs      1229:            *dest = DIR_SEP;
1.96      cvs      1230:          else
                   1231:            *dest = *src;
1.90      cvs      1232:          dest++;
1.96      cvs      1233:          src++;
1.90      cvs      1234:          break;
1.89      cvs      1235: #endif /* _WINDOWS */
1.96      cvs      1236: 
1.106     cvs      1237:        case '%':
1.97      cvs      1238:          if (convertion & AM_CONV_PERCENT)
1.96      cvs      1239:            {
1.97      cvs      1240:              /* (code adapted from libwww's HTUnEscape function */
1.96      cvs      1241:              src++;
1.106     cvs      1242:              if (*src != EOS)
1.97      cvs      1243:                {
                   1244:                  *dest = UnEscapeChar (*src) * 16;
                   1245:                  src++;
                   1246:                }
1.106     cvs      1247:              if (*src != EOS)
1.97      cvs      1248:                {
                   1249:                  *dest = *dest + UnEscapeChar (*src);
                   1250:                  src++;
                   1251:                }
                   1252:              dest++;
1.96      cvs      1253:            }
1.97      cvs      1254:          else
1.96      cvs      1255:            {
1.97      cvs      1256:              *dest = *src;
                   1257:              dest++;
1.96      cvs      1258:              src++;
                   1259:            }
                   1260:          break;
                   1261: 
1.90      cvs      1262:        default:
                   1263:          *dest = *src;
1.89      cvs      1264:          dest++;
1.96      cvs      1265:          src++;
1.90      cvs      1266:          break;
1.89      cvs      1267:        }
                   1268:     }
1.90      cvs      1269:   /* copy the EOS char */
                   1270:   *dest = *src;
1.73      cvs      1271: }
1.40      cvs      1272: 
                   1273: /*----------------------------------------------------------------------
1.9       cvs      1274:    NormalizeURL
                   1275:    normalizes orgName according to a base associated with doc, and
                   1276:    following the standard URL format rules.
1.113     cvs      1277:    if doc is < 0, use as a base the URL of the document that contains
                   1278:    (or contained) the elements that are now in the copy/cut buffer.
1.53      cvs      1279:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                   1280:    other path.
1.9       cvs      1281:    The function returns the new complete and normalized URL 
1.12      cvs      1282:    or file name path (newName) and the name of the document (docName).        
1.9       cvs      1283:    N.B. If the function can't find out what's the docName, it assigns
                   1284:    the name "noname.html".
1.4       cvs      1285:   ----------------------------------------------------------------------*/
1.106     cvs      1286: void NormalizeURL (char *orgName, Document doc, char *newName,
                   1287:                   char *docName, char *otherPath)
                   1288: {
                   1289:    char          *basename;
                   1290:    char           tempOrgName[MAX_LENGTH];
                   1291:    char          *ptr;
                   1292:    char           used_sep;
1.84      cvs      1293:    int            length;
                   1294:    ThotBool       check;
1.5       cvs      1295: 
1.110     cvs      1296: #ifdef _WINDOWS
1.44      cvs      1297:    int ndx;
1.110     cvs      1298: #endif /* _WINDOWS */
1.44      cvs      1299: 
1.5       cvs      1300:    if (!newName || !docName)
                   1301:       return;
1.18      cvs      1302: 
1.113     cvs      1303:    if (doc < 0)
                   1304:      basename = TtaStrdup (SavedDocumentURL);
                   1305:    else if (doc > 0)
1.53      cvs      1306:      basename = GetBaseURL (doc);
                   1307:    else if (otherPath != NULL)
1.108     cvs      1308:      basename = TtaStrdup (otherPath);
1.32      cvs      1309:    else
1.53      cvs      1310:      basename = NULL;
1.32      cvs      1311: 
1.18      cvs      1312:    /*
1.31      cvs      1313:     * Clean orgName
                   1314:     * Make sure we have a complete orgName, without any leading or trailing
                   1315:     * white spaces, or trailinbg new lines
                   1316:     */
1.5       cvs      1317:    ptr = orgName;
1.18      cvs      1318:    /* skip leading white space and new line characters */
1.106     cvs      1319:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                   1320:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                   1321:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs      1322:    /*
1.31      cvs      1323:     * Make orgName a complete URL
                   1324:     * If the URL does not include a protocol, then try to calculate
                   1325:     * one using the doc's base element (if it exists),
                   1326:     */
1.106     cvs      1327:    if (tempOrgName[0] == EOS)
1.53      cvs      1328:      {
1.106     cvs      1329:        newName[0] = EOS;
                   1330:        docName[0] = EOS;
1.53      cvs      1331:        TtaFreeMemory (basename);
                   1332:        return;
                   1333:      }
1.49      cvs      1334: 
                   1335:    /* clean trailing white space */
1.106     cvs      1336:    length = strlen (tempOrgName) - 1;
                   1337:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53      cvs      1338:      {
1.106     cvs      1339:        tempOrgName[length] = EOS;
1.53      cvs      1340:        length--;
                   1341:      }
1.50      cvs      1342: 
1.55      cvs      1343:    /* remove extra dot (which dot???) */
                   1344:    /* ugly, but faster than a strcmp */
1.106     cvs      1345:    if (tempOrgName[length] == '.'
                   1346:        && (length == 0 || tempOrgName[length-1] != '.'))
                   1347:         tempOrgName[length] = EOS;
1.50      cvs      1348: 
1.94      cvs      1349:    if (IsW3Path (tempOrgName))
1.53      cvs      1350:      {
                   1351:        /* the name is complete, go to the Sixth Step */
1.106     cvs      1352:        strcpy (newName, tempOrgName);
1.53      cvs      1353:        SimplifyUrl (&newName);
                   1354:        /* verify if the URL has the form "protocol://server:port" */
1.110     cvs      1355:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
                   1356:                                         AMAYA_PARSE_HOST |
                   1357:                                         AMAYA_PARSE_PUNCTUATION);
                   1358:        if (ptr && !strcmp (ptr, newName))
                   1359:         /* it has this form, we complete it by adding a DIR_STR  */
1.106     cvs      1360:          strcat (newName, URL_STR);
1.49      cvs      1361: 
1.53      cvs      1362:        if (ptr)
1.50      cvs      1363:          TtaFreeMemory (ptr);
1.53      cvs      1364:      }
1.113     cvs      1365:    else if (basename == NULL)
1.53      cvs      1366:      /* the name is complete, go to the Sixth Step */
1.106     cvs      1367:      strcpy (newName, tempOrgName);
1.53      cvs      1368:    else
                   1369:      {
1.31      cvs      1370:        /* Calculate the absolute URL, using the base or document URL */
1.110     cvs      1371: #ifdef _WINDOWS
1.53      cvs      1372:        if (!IsW3Path (basename))
                   1373:         {
1.106     cvs      1374:           length = strlen (tempOrgName);
1.53      cvs      1375:           for (ndx = 0; ndx < length; ndx++)
1.106     cvs      1376:             if (tempOrgName [ndx] == '/')
                   1377:               tempOrgName [ndx] = '\\';
1.53      cvs      1378:         }
1.110     cvs      1379: #endif /* _WINDOWS */
1.25      cvs      1380:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1381:        if (ptr)
                   1382:         {
                   1383:           SimplifyUrl (&ptr);
1.106     cvs      1384:           strcpy (newName, ptr);
1.53      cvs      1385:           TtaFreeMemory (ptr);
                   1386:         }
                   1387:        else
1.106     cvs      1388:         newName[0] = EOS;
1.53      cvs      1389:      }
1.36      cvs      1390: 
                   1391:    TtaFreeMemory (basename);
1.18      cvs      1392:    /*
1.31      cvs      1393:     * Prepare the docname that will refer to this ressource in the
                   1394:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1395:     * noname.html as a default ressource name
1.18      cvs      1396:    */
1.106     cvs      1397:    if (newName[0] != EOS)
1.53      cvs      1398:      {
1.106     cvs      1399:        length = strlen (newName) - 1;
                   1400:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53      cvs      1401:         {
                   1402:           used_sep = newName[length];
                   1403:           check = TRUE;
                   1404:           while (check)
                   1405:             {
1.50      cvs      1406:                length--;
                   1407:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1408:                 length--;
1.106     cvs      1409:                if (!strncmp (&newName[length+1], "..", 2))
1.53      cvs      1410:                 {
1.106     cvs      1411:                   newName[length+1] = EOS;
1.53      cvs      1412:                   /* remove also previous directory */
                   1413:                   length--;
                   1414:                   while (length >= 0 && newName[length] != used_sep)
                   1415:                     length--;
1.106     cvs      1416:                   if (strncmp (&newName[length+1], "//", 2))
1.131     cheyroul 1417:                     /* don't remove server name */
1.106     cvs      1418:                      newName[length+1] = EOS;
1.53      cvs      1419:                 }
1.106     cvs      1420:               else if (!strncmp (&newName[length+1], ".", 1))
                   1421:                 newName[length+1] = EOS;
1.50      cvs      1422:                else
1.53      cvs      1423:                 check = FALSE;
                   1424:             }
                   1425:           /* docname was not comprised inside the URL, so let's */
                   1426:           /* assign the default ressource name */
1.106     cvs      1427:           strcpy (docName, "noname.html");
1.53      cvs      1428:         }
                   1429:        else
                   1430:         { /* docname is comprised inside the URL */
1.110     cvs      1431:            while (length >= 0 && newName[length] != URL_SEP &&
                   1432:                  newName[length] != DIR_SEP)
1.53      cvs      1433:             length--;
                   1434:           if (length < 0)
1.106     cvs      1435:              strcpy (docName, newName);
1.53      cvs      1436:           else
1.106     cvs      1437:             strcpy (docName, &newName[length+1]);
1.53      cvs      1438:         }
                   1439:      }
                   1440:    else
1.106     cvs      1441:      docName[0] = EOS;
1.18      cvs      1442: } 
1.3       cvs      1443: 
1.4       cvs      1444: /*----------------------------------------------------------------------
1.9       cvs      1445:   IsSameHost                                                         
1.4       cvs      1446:   ----------------------------------------------------------------------*/
1.106     cvs      1447: ThotBool IsSameHost (const char *url1, const char *url2)
1.3       cvs      1448: {
1.106     cvs      1449:   char          *basename_ptr1, *basename_ptr2;
                   1450:   ThotBool       result;
1.3       cvs      1451: 
1.106     cvs      1452:   basename_ptr1 = AmayaParseUrl (url1, "",
                   1453:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1454:   basename_ptr2 = AmayaParseUrl (url2, "",
                   1455:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1456: 
1.106     cvs      1457:   if (strcmp (basename_ptr1, basename_ptr2))
                   1458:     result = FALSE;
                   1459:   else
                   1460:     result = TRUE;
                   1461:   TtaFreeMemory (basename_ptr1);
                   1462:   TtaFreeMemory (basename_ptr2);
                   1463:   return (result);
1.3       cvs      1464: }
                   1465: 
                   1466: 
1.4       cvs      1467: /*----------------------------------------------------------------------
1.22      cvs      1468:   HasKnownFileSuffix
                   1469:   returns TRUE if path points to a file ending with a suffix.
                   1470:   ----------------------------------------------------------------------*/
1.153   ! vatton   1471: ThotBool HasKnownFileSuffix (const char *path)
1.106     cvs      1472: {
                   1473:    char       *root;
                   1474:    char        temppath[MAX_LENGTH];
                   1475:    char        suffix[MAX_LENGTH];
1.22      cvs      1476: 
1.106     cvs      1477:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs      1478:      return (FALSE);
                   1479: 
1.106     cvs      1480:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1481: 
                   1482:    if (root) 
                   1483:      {
1.106     cvs      1484:        strcpy (temppath, root);
1.25      cvs      1485:        TtaFreeMemory (root);
1.22      cvs      1486:        /* Get the suffix */
1.124     vatton   1487:        TtaExtractSuffix (temppath, suffix); 
1.22      cvs      1488: 
1.106     cvs      1489:        if( suffix[0] == EOS)
1.22      cvs      1490:         /* no suffix */
                   1491:         return (FALSE);
                   1492: 
                   1493:        /* Normalize the suffix */
                   1494:        ConvertToLowerCase (suffix);
                   1495: 
1.106     cvs      1496:        if (!strcmp (suffix, "gz"))
1.22      cvs      1497:         /* skip the compressed suffix */
                   1498:         {
1.124     vatton   1499:         TtaExtractSuffix (temppath, suffix);
1.106     cvs      1500:         if(suffix[0] == EOS)
1.22      cvs      1501:           /* no suffix */
                   1502:           return (FALSE);
                   1503:          /* Normalize the suffix */
                   1504:          ConvertToLowerCase (suffix);
                   1505:         }
                   1506: 
1.106     cvs      1507:        if (strcmp (suffix, "gif") &&
                   1508:           strcmp (suffix, "xbm") &&
                   1509:           strcmp (suffix, "xpm") &&
                   1510:           strcmp (suffix, "jpg") &&
                   1511:           strcmp (suffix, "pdf") &&
                   1512:           strcmp (suffix, "png") &&
                   1513:           strcmp (suffix, "tgz") &&
                   1514:           strcmp (suffix, "xpg") &&
                   1515:           strcmp (suffix, "xpd") &&
                   1516:           strcmp (suffix, "ps") &&
                   1517:           strcmp (suffix, "au") &&
                   1518:           strcmp (suffix, "html") &&
                   1519:           strcmp (suffix, "htm") &&
                   1520:           strcmp (suffix, "shtml") &&
                   1521:           strcmp (suffix, "xht") &&
                   1522:           strcmp (suffix, "xhtm") &&
                   1523:           strcmp (suffix, "xhtml") &&
                   1524:           strcmp (suffix, "txt") &&
                   1525:           strcmp (suffix, "css") &&
                   1526:           strcmp (suffix, "eps"))
1.22      cvs      1527:         return (FALSE);
                   1528:        else
                   1529:         return (TRUE);
                   1530:      }
                   1531:    else
                   1532:      return (FALSE);
                   1533: }
                   1534: 
                   1535: 
                   1536: /*----------------------------------------------------------------------
1.24      cvs      1537:   ChopURL
                   1538:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1539:   If inputURL is  bigger than that size, outputURL receives
                   1540:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1541:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1542:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1543:   copied into outputURL. 
                   1544:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1545:   chars.
                   1546:   ----------------------------------------------------------------------*/
1.106     cvs      1547: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1548: {
                   1549:   int len;
1.9       cvs      1550: 
1.106     cvs      1551:   len = strlen (inputURL);
1.24      cvs      1552:   if (len <= MAX_PRINT_URL_LENGTH) 
1.106     cvs      1553:     strcpy (outputURL, inputURL);
1.24      cvs      1554:   else
                   1555:     /* make a truncated urlName on the status window */
                   1556:     {
1.106     cvs      1557:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1558:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1559:       strcat (outputURL, "...");
                   1560:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24      cvs      1561:     }
1.25      cvs      1562: }
                   1563: 
                   1564: 
                   1565: /*----------------------------------------------------------------------
                   1566:    scan
1.47      cvs      1567:        Scan a filename for its constituents
1.25      cvs      1568:        -----------------------------------
                   1569:   
                   1570:    On entry,
                   1571:        name    points to a document name which may be incomplete.
                   1572:    On exit,
                   1573:         absolute or relative may be nonzero (but not both).
                   1574:        host, fragment and access may be nonzero if they were specified.
                   1575:        Any which are nonzero point to zero terminated strings.
                   1576:   ----------------------------------------------------------------------*/
1.106     cvs      1577: static void scan (char *name, HTURI *parts)
1.25      cvs      1578: {
1.106     cvs      1579:   char *   p;
                   1580:   char *   after_access = name;
1.32      cvs      1581: 
1.43      cvs      1582:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1583:   /* Look for fragment identifier */
1.106     cvs      1584:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs      1585:     {
1.106     cvs      1586:       *p++ = '\0';
1.28      cvs      1587:       parts->fragment = p;
1.25      cvs      1588:     }
                   1589:     
1.28      cvs      1590:   for (p=name; *p; p++)
                   1591:     {
1.106     cvs      1592:       if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28      cvs      1593:        break;
1.106     cvs      1594:       if (*p == ':')
1.28      cvs      1595:        {
                   1596:          *p = 0;
                   1597:          parts->access = after_access; /* Scheme has been specified */
                   1598: 
                   1599:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1600:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1601:             not recommended. Rather, turn off "-O". */
                   1602: 
                   1603:          /*            after_access = p;*/
                   1604:          /*            while (*after_access == 0)*/
                   1605:          /*                after_access++;*/
                   1606:          after_access = p+1;
1.106     cvs      1607:          if (!strcasecmp("URL", parts->access))
1.28      cvs      1608:            /* Ignore IETF's URL: pre-prefix */
                   1609:            parts->access = NULL;
                   1610:          else
1.25      cvs      1611:            break;
                   1612:        }
                   1613:     }
                   1614:     
                   1615:     p = after_access;
1.43      cvs      1616:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1617:       {
1.43      cvs      1618:        if (p[1] == URL_SEP)
1.28      cvs      1619:          {
1.25      cvs      1620:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1621:            *p = 0;                     /* Terminate access             */
                   1622:            /* look for end of host name if any */
1.106     cvs      1623:            p = strchr (parts->host, URL_SEP);
1.28      cvs      1624:            if (p)
                   1625:              {
1.106     cvs      1626:                *p = EOS;                       /* Terminate host */
1.25      cvs      1627:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1628:              }
                   1629:          }
                   1630:        else
                   1631:          /* Root found but no host */
                   1632:          parts->absolute = p+1;
                   1633:       }
                   1634:     else
                   1635:       {
1.25      cvs      1636:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1637:       }
1.25      cvs      1638: }
                   1639: 
                   1640: 
                   1641: /*----------------------------------------------------------------------
1.28      cvs      1642:   AmayaParseUrl: parse a Name relative to another name
                   1643: 
                   1644:   This returns those parts of a name which are given (and requested)
                   1645:   substituting bits from the related name where necessary.
1.25      cvs      1646:   
1.28      cvs      1647:   On entry,
1.25      cvs      1648:        aName           A filename given
                   1649:         relatedName     A name relative to which aName is to be parsed. Give
                   1650:                         it an empty string if aName is absolute.
                   1651:         wanted          A mask for the bits which are wanted.
                   1652:   
1.28      cvs      1653:   On exit,
1.25      cvs      1654:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1655:   ----------------------------------------------------------------------*/
1.106     cvs      1656: char   *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
                   1657: {
                   1658:   char      *return_value;
                   1659:   char       result[MAX_LENGTH];
                   1660:   char       name[MAX_LENGTH];
                   1661:   char       rel[MAX_LENGTH];
                   1662:   char      *p, *access;
1.29      cvs      1663:   HTURI      given, related;
                   1664:   int        len;
1.106     cvs      1665:   char       used_sep;
                   1666:   char      *used_str;
1.32      cvs      1667: 
1.106     cvs      1668:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1669:     {
1.106     cvs      1670:       used_str = DIR_STR;
                   1671:       used_sep = DIR_SEP;
1.33      cvs      1672:     }
1.32      cvs      1673:   else
1.33      cvs      1674:     {
1.106     cvs      1675:       used_str = URL_STR;
                   1676:       used_sep = URL_SEP;
1.33      cvs      1677:     }
1.32      cvs      1678: 
1.29      cvs      1679:   /* Make working copies of input strings to cut up: */
                   1680:   return_value = NULL;
                   1681:   result[0] = 0;               /* Clear string  */
1.106     cvs      1682:   strcpy (name, aName);
1.29      cvs      1683:   if (relatedName != NULL)  
1.106     cvs      1684:     strcpy (rel, relatedName);
1.29      cvs      1685:   else
1.106     cvs      1686:     relatedName[0] = EOS;
1.29      cvs      1687:   
                   1688:   scan (name, &given);
                   1689:   scan (rel,  &related); 
                   1690:   access = given.access ? given.access : related.access;
                   1691:   if (wanted & AMAYA_PARSE_ACCESS)
                   1692:     if (access)
                   1693:       {
1.106     cvs      1694:        strcat (result, access);
1.29      cvs      1695:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1696:                strcat (result, ":");
1.29      cvs      1697:       }
                   1698:   
                   1699:   if (given.access && related.access)
                   1700:     /* If different, inherit nothing. */
1.106     cvs      1701:     if (strcmp (given.access, related.access) != 0)
1.29      cvs      1702:       {
                   1703:        related.host = 0;
                   1704:        related.absolute = 0;
                   1705:        related.relative = 0;
                   1706:        related.fragment = 0;
                   1707:       }
                   1708:   
                   1709:   if (wanted & AMAYA_PARSE_HOST)
                   1710:     if(given.host || related.host)
                   1711:       {
                   1712:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1713:          strcat (result, "//");
                   1714:        strcat (result, given.host ? given.host : related.host);
1.29      cvs      1715:       }
                   1716:   
                   1717:   if (given.host && related.host)
                   1718:     /* If different hosts, inherit no path. */
1.106     cvs      1719:     if (strcmp (given.host, related.host) != 0)
1.29      cvs      1720:       {
                   1721:        related.absolute = 0;
                   1722:        related.relative = 0;
                   1723:        related.fragment = 0;
                   1724:       }
                   1725:   
                   1726:   if (wanted & AMAYA_PARSE_PATH)
                   1727:     {
                   1728:       if (given.absolute)
                   1729:        {
                   1730:          /* All is given */
                   1731:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1732:            strcat (result, used_str);
                   1733:          strcat (result, given.absolute);
1.25      cvs      1734:        }
1.29      cvs      1735:       else if (related.absolute)
                   1736:        {
                   1737:          /* Adopt path not name */
1.106     cvs      1738:          strcat (result, used_str);
                   1739:          strcat (result, related.absolute);
1.29      cvs      1740:          if (given.relative)
                   1741:            {
                   1742:              /* Search part? */
1.106     cvs      1743:              p = strchr (result, '?');
1.29      cvs      1744:              if (!p)
1.106     cvs      1745:                p=result+strlen(result)-1;
1.33      cvs      1746:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1747:              /* Remove filename */
                   1748:              p[1]=0;
                   1749:              /* Add given one */
1.106     cvs      1750:              strcat (result, given.relative);
1.25      cvs      1751:            }
                   1752:        }
1.29      cvs      1753:       else if (given.relative)
                   1754:        /* what we've got */
1.106     cvs      1755:        strcat (result, given.relative);
1.29      cvs      1756:       else if (related.relative)
1.106     cvs      1757:        strcat (result, related.relative);
1.29      cvs      1758:       else
                   1759:        /* No inheritance */
1.106     cvs      1760:        strcat (result, used_str);
1.25      cvs      1761:     }
1.29      cvs      1762:   
                   1763:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1764:     if (given.fragment || related.fragment)
                   1765:       {
                   1766:        if (given.absolute && given.fragment)
                   1767:          {
                   1768:            /*Fixes for relURLs...*/
                   1769:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1770:              strcat (result, "#");
                   1771:            strcat (result, given.fragment); 
1.29      cvs      1772:          }
                   1773:        else if (!(given.absolute) && !(given.fragment))
1.106     cvs      1774:          strcat (result, "");
1.29      cvs      1775:        else
                   1776:          {
1.110     cvs      1777:           if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1778:              strcat (result, "#");
1.110     cvs      1779:           strcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1780:          }
                   1781:       }
1.106     cvs      1782:   len = strlen (result);
                   1783:   if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1784:     strcpy (return_value, result);
1.29      cvs      1785:   return (return_value);               /* exactly the right length */
1.25      cvs      1786: }
                   1787: 
                   1788: /*----------------------------------------------------------------------
                   1789:      HTCanon
                   1790:        Canonicalizes the URL in the following manner starting from the host
                   1791:        pointer:
                   1792:   
                   1793:        1) The host name is converted to lowercase
                   1794:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1795:   
                   1796:        Return: OK      The position of the current path part of the URL
                   1797:                        which might be the old one or a new one.
                   1798:   
                   1799:   ----------------------------------------------------------------------*/
1.106     cvs      1800: static char   *HTCanon (char **filename, char *host)
                   1801: {
                   1802:     char   *newname = NULL;
                   1803:     char    used_sep;
                   1804:     char   *path;
                   1805:     char   *strptr;
                   1806:     char   *port;
                   1807:     char   *access = host-3;
                   1808:   
                   1809:      if (*filename && strchr (*filename, URL_SEP))
                   1810:         used_sep = URL_SEP;
1.33      cvs      1811:      else
1.106     cvs      1812:         used_sep = DIR_SEP;
1.32      cvs      1813:   
1.110     cvs      1814:     while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25      cvs      1815:        access--;
1.110     cvs      1816:     if ((path = strchr (host, used_sep)) == NULL)              /* Find path */
1.106     cvs      1817:        path = host + strlen (host);
                   1818:     if ((strptr = strchr (host, '@')) != NULL && strptr < path)           /* UserId */
1.82      cvs      1819:        host = strptr;
1.110     cvs      1820:     if ((port = strchr (host, ':')) != NULL && port > path)   /* Port number */
1.82      cvs      1821:        port = NULL;
1.25      cvs      1822: 
                   1823:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1824:     while (strptr < path)
1.33      cvs      1825:       {
1.123     vatton   1826:          *strptr = tolower (*strptr);
1.82      cvs      1827:          strptr++;
1.33      cvs      1828:       }
1.25      cvs      1829:     
                   1830:     /* Does the URL contain a full domain name? This also works for a
                   1831:        numerical host name. The domain name is already made lower-case
                   1832:        and without a trailing dot. */
                   1833:     {
1.106     cvs      1834:       char  *dot = port ? port : path;
                   1835:       if (dot > *filename && *--dot == '.')
1.33      cvs      1836:        {
1.106     cvs      1837:          char  *orig = dot;
                   1838:          char  *dest = dot + 1;
1.82      cvs      1839:          while ((*orig++ = *dest++));
                   1840:             if (port) port--;
1.33      cvs      1841:          path--;
1.25      cvs      1842:        }
                   1843:     }
                   1844:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1845:     if (port)
                   1846:       {
1.82      cvs      1847:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      1848:          {
                   1849:            if (!newname)
                   1850:              {
1.106     cvs      1851:                char  *orig = port; 
                   1852:                char  *dest = port + 1;
1.82      cvs      1853:                while ((*orig++ = *dest++));
1.33      cvs      1854:              }
                   1855:          }
1.106     cvs      1856:        else if ((!strncmp (access, "http", 4)   &&
                   1857:              (*(port + 1) == '8'                    && 
                   1858:              *(port+2) == '0'                       && 
1.82      cvs      1859:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.106     cvs      1860:              (!strncmp (access, "gopher", 6) &&
                   1861:              (*(port+1) == '7'                      && 
                   1862:              *(port+2) == '0'                       && 
1.82      cvs      1863:              (*(port+3) == used_sep || !*(port+3))))         ||
1.106     cvs      1864:              (!strncmp (access, "ftp", 3)    &&
                   1865:              (*(port+1) == '2'                      && 
                   1866:              *(port + 2) == '1'                     && 
1.82      cvs      1867:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1868:          if (!newname)
                   1869:            {
1.106     cvs      1870:              char  *orig = port; 
                   1871:              char  *dest = port + 3;
1.33      cvs      1872:              while((*orig++ = *dest++));
                   1873:              /* Update path position, Henry Minsky */
                   1874:              path -= 3;
1.25      cvs      1875:            }
1.33      cvs      1876:        }
                   1877:        else if (newname)
1.106     cvs      1878:          strncat (newname, port, (int) (path - port));
1.33      cvs      1879:       }
1.25      cvs      1880: 
1.33      cvs      1881:     if (newname)
                   1882:       {
1.106     cvs      1883:        char  *newpath = newname + strlen (newname);
                   1884:        strcat (newname, path);
1.25      cvs      1885:        path = newpath;
1.28      cvs      1886:        /* Free old copy */
                   1887:        TtaFreeMemory(*filename);
1.25      cvs      1888:        *filename = newname;
1.33      cvs      1889:       }
1.25      cvs      1890:     return path;
                   1891: }
                   1892: 
                   1893: 
                   1894: /*----------------------------------------------------------------------
1.29      cvs      1895:   SimplifyUrl: simplify a URI
1.32      cvs      1896:   A URI is allowed to contain the sequence xxx/../ which may be
                   1897:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1898:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1899:   
1.28      cvs      1900:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1901:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1902:   
1.28      cvs      1903:   but we should NOT change
                   1904:                 http://fred.xxx.edu/../..
1.25      cvs      1905:   
                   1906:        or      ../../albert.html
                   1907:   
1.28      cvs      1908:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1909:   
                   1910:                /fred/..                becomes /fred/..
                   1911:                /fred/././..            becomes /fred/..
                   1912:                /fred/.././junk/.././   becomes /fred/..
                   1913:   
1.28      cvs      1914:   If more than one set of `://' is found (several proxies in cascade) then
                   1915:   only the part after the last `://' is simplified.
1.25      cvs      1916:   
1.28      cvs      1917:   Returns: A string which might be the old one or a new one.
1.25      cvs      1918:   ----------------------------------------------------------------------*/
1.106     cvs      1919: void         SimplifyUrl (char **url)
                   1920: {
                   1921:   char   *path;
                   1922:   char   *access;
                   1923:   char   *newptr; 
                   1924:   char   *p;
                   1925:   char   *orig, *dest, *end;
1.28      cvs      1926: 
1.106     cvs      1927:   char      used_sep;
1.77      cvs      1928:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   1929:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      1930: 
1.28      cvs      1931:   if (!url || !*url)
                   1932:     return;
                   1933: 
1.106     cvs      1934:   if (strchr (*url, URL_SEP))
                   1935:       used_sep = URL_SEP;
1.32      cvs      1936:   else
1.106     cvs      1937:       used_sep = DIR_SEP;
1.32      cvs      1938: 
1.77      cvs      1939:   /* should we simplify double dot? */
                   1940:   path = *url;
1.106     cvs      1941:   if (*path == '.' && *(path + 1) == '.')
1.77      cvs      1942:     ddot_simplify = FALSE;
                   1943:   else
                   1944:     ddot_simplify = TRUE;
                   1945: 
1.28      cvs      1946:   /* Find any scheme name */
1.106     cvs      1947:   if ((path = strstr (*url, "://")) != NULL)
1.33      cvs      1948:     {
                   1949:       /* Find host name */
1.28      cvs      1950:       access = *url;
1.123     vatton   1951:       while (access < path && (*access = tolower (*access)))
1.82      cvs      1952:             access++;
1.28      cvs      1953:       path += 3;
1.106     cvs      1954:       while ((newptr = strstr (path, "://")) != NULL)
1.82      cvs      1955:             /* For proxies */
1.106     cvs      1956:             path = newptr + 3;
1.82      cvs      1957:      /* We have a host name */
1.84      cvs      1958:       path = HTCanon (url, path);
1.25      cvs      1959:     }
1.106     cvs      1960:   else if ((path = strstr (*url, ":/")) != NULL)
1.28      cvs      1961:     path += 2;
                   1962:   else
                   1963:     path = *url;
1.84      cvs      1964:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      1965:     /* Some URLs start //<foo> */
                   1966:     path += 1;
1.94      cvs      1967:   else if (IsFilePath (path))
                   1968:     {
                   1969:       /* doesn't need to do anything more */
                   1970:       return;
                   1971:     }
1.106     cvs      1972:   else if (!strncmp (path, "news:", 5))
1.28      cvs      1973:     {
1.106     cvs      1974:       newptr = strchr (path+5, '@');
1.28      cvs      1975:       if (!newptr)
                   1976:        newptr = path + 5;
                   1977:       while (*newptr)
                   1978:        {
                   1979:          /* Make group or host lower case */
1.123     vatton   1980:          *newptr = tolower (*newptr);
1.28      cvs      1981:          newptr++;
1.25      cvs      1982:        }
1.28      cvs      1983:       /* Doesn't need to do any more */
                   1984:       return;
1.25      cvs      1985:     }
1.130     cheyroul 1986:    
1.126     cheyroul 1987: 
1.28      cvs      1988:   if ((p = path))
                   1989:     {
1.106     cvs      1990:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1991:            (end = strchr (path, '#'))))
                   1992:        end = path + strlen (path);
1.28      cvs      1993:       
                   1994:       /* Parse string second time to simplify */
                   1995:       p = path;
                   1996:       while (p < end)
                   1997:        {
1.110     cvs      1998:          /* if we're pointing to a char, it's safe to reactivate the 
                   1999:             ../ convertion */
1.106     cvs      2000:          if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77      cvs      2001:            ddot_simplify = TRUE;
                   2002: 
1.33      cvs      2003:          if (*p==used_sep)
1.28      cvs      2004:            {
1.106     cvs      2005:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      2006:                {
                   2007:                  orig = p + 1;
1.84      cvs      2008:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      2009:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      2010:                  end = orig - 1;
                   2011:                }
1.106     cvs      2012:              else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.' 
1.77      cvs      2013:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      2014:                {
                   2015:                  newptr = p;
1.52      cvs      2016:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   2017:                  if (*newptr == used_sep)
                   2018:                    orig = newptr + 1;
1.28      cvs      2019:                  else
1.52      cvs      2020:                    orig = newptr;
                   2021: 
                   2022:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   2023:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   2024:                  end = orig-1;
                   2025:                  /* Start again with prev slash */
                   2026:                  p = newptr;
1.28      cvs      2027:                }
1.33      cvs      2028:              else if (*(p+1) == used_sep)
1.28      cvs      2029:                {
1.33      cvs      2030:                  while (*(p+1) == used_sep)
1.28      cvs      2031:                    {
                   2032:                      orig = p;
                   2033:                      dest = p + 1;
                   2034:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   2035:                      end = orig-1;
                   2036:                    }
                   2037:                }
                   2038:              else
1.25      cvs      2039:                p++;
1.28      cvs      2040:            }
                   2041:          else
                   2042:            p++;
1.25      cvs      2043:        }
                   2044:     }
1.51      cvs      2045:     /*
                   2046:     **  Check for host/../.. kind of things
                   2047:     */
1.106     cvs      2048:     if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.' 
1.77      cvs      2049:        && (!*(path+3) || *(path+3) == used_sep))
1.106     cvs      2050:        *(path+1) = EOS;
1.28      cvs      2051:   return;
                   2052: }
                   2053: 
                   2054: 
                   2055: /*----------------------------------------------------------------------
1.96      cvs      2056:    NormalizeFile normalizes local names.                             
1.28      cvs      2057:    Return TRUE if target and src differ.                           
                   2058:   ----------------------------------------------------------------------*/
1.106     cvs      2059: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28      cvs      2060: {
1.110     cvs      2061: #ifndef _WINDOWS
1.106     cvs      2062:    char             *s;
1.93      cvs      2063:    int               i;
1.110     cvs      2064: #endif /* !_WINDOWS */
1.82      cvs      2065:    ThotBool          change;
1.90      cvs      2066:    int               start_index; /* the first char that we'll copy */
1.28      cvs      2067: 
1.54      cvs      2068:    change = FALSE;
1.90      cvs      2069:    start_index = 0;
                   2070: 
1.106     cvs      2071:    if (!src || src[0] == EOS)
1.96      cvs      2072:      {
1.106     cvs      2073:        target[0] = EOS;
1.96      cvs      2074:        return FALSE;
                   2075:      }
1.90      cvs      2076: 
                   2077:    /* @@ do I need file: or file:/ here? */
1.106     cvs      2078:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      2079:      {
1.90      cvs      2080:        /* remove the prefix file: */
                   2081:        start_index += 5;
                   2082:    
                   2083:        /* remove the localhost prefix */
1.106     cvs      2084:        if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94      cvs      2085:           start_index += 11;
                   2086:        
                   2087:        /* remove the first two slashes in / / /path */
                   2088:        while (src[start_index] &&
1.106     cvs      2089:              src[start_index] == '/' 
                   2090:              && src[start_index + 1] == '/')
1.94      cvs      2091:         start_index++;
                   2092: 
                   2093: #ifdef _WINDOWS
                   2094:        /* remove any extra slash before the drive name */
1.106     cvs      2095:        if (src[start_index] == '/'
                   2096:           &&src[start_index+2] == ':')
1.94      cvs      2097:         start_index++;
                   2098: #endif /* _WINDOWS */
1.90      cvs      2099: 
1.106     cvs      2100:        if (src[start_index] == EOS)
1.90      cvs      2101:        /* if there's nothing afterwards, add a DIR_STR */
1.106     cvs      2102:         strcpy (target, DIR_STR);
1.90      cvs      2103:        else
1.97      cvs      2104:         /* as we're inside a file: URL, we'll apply all the convertions
                   2105:            we know */
                   2106:         CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96      cvs      2107: 
                   2108:        change = TRUE;
                   2109:      }
1.97      cvs      2110:    else if (convertion != AM_CONV_NONE)
1.96      cvs      2111:      {
                   2112:        /* we are following a "local" relative link, we do all the
                   2113:          convertions except for the HOME_DIR ~ one */
1.97      cvs      2114:        CleanCopyFileURL (target, src, convertion);
1.28      cvs      2115:      }
1.90      cvs      2116: #ifndef _WINDOWS
1.106     cvs      2117:    else if (src[0] == '~')
1.53      cvs      2118:      {
1.96      cvs      2119:        /* it must be a URL typed in a text input field */
                   2120:        /* do the HOME_DIR ~ substitution */
1.82      cvs      2121:        s = TtaGetEnvString ("HOME");
1.106     cvs      2122:        strcpy (target, s);
1.90      cvs      2123: #if 0
1.96      cvs      2124:        /* JK: invalidated this part of the code as it's simpler
                   2125:           to add the DIR_SEP whenever we have something to add
                   2126:           to the path rather than adding it systematically */
1.106     cvs      2127:        if (src[1] != DIR_SEP)
                   2128:          strcat (target, DIR_STR);
1.90      cvs      2129: #endif
1.106     cvs      2130:        i = strlen (target);
                   2131:        strcpy (&target[i], &src[1]);
1.54      cvs      2132:        change = TRUE;
1.53      cvs      2133:      }
1.90      cvs      2134: #endif /* _WINDOWS */
1.28      cvs      2135:    else
1.96      cvs      2136:    /* leave it as it is */
1.106     cvs      2137:      strcpy (target, src);
1.96      cvs      2138:    
1.28      cvs      2139:    /* remove /../ and /./ */
1.29      cvs      2140:    SimplifyUrl (&target);
1.54      cvs      2141:    if (!change)
1.106     cvs      2142:      change = strcmp (src, target);
1.28      cvs      2143:    return (change);
1.25      cvs      2144: }
                   2145: 
1.28      cvs      2146: 
1.25      cvs      2147: /*----------------------------------------------------------------------
1.31      cvs      2148:   MakeRelativeURL: make relative name
1.25      cvs      2149:   
1.28      cvs      2150:   This function creates and returns a string which gives an expression of
                   2151:   one address as related to another. Where there is no relation, an absolute
                   2152:   address is retured.
1.25      cvs      2153:   
1.28      cvs      2154:   On entry,
1.25      cvs      2155:        Both names must be absolute, fully qualified names of nodes
                   2156:        (no fragment bits)
                   2157:   
1.28      cvs      2158:   On exit,
1.25      cvs      2159:        The return result points to a newly allocated name which, if
                   2160:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   2161:        The caller is responsible for freeing the resulting name later.
                   2162:   ----------------------------------------------------------------------*/
1.106     cvs      2163: char      *MakeRelativeURL (char *aName, char *relatedName)
                   2164: {
                   2165:   char  *return_value;
                   2166:   char   result[MAX_LENGTH];
                   2167:   char  *p;
                   2168:   char  *q;
                   2169:   char  *after_access;
                   2170:   char  *last_slash = NULL;
                   2171:   int    slashes, levels, len;
1.110     cvs      2172: #ifdef _WINDOWS
1.44      cvs      2173:   int ndx;
1.110     cvs      2174: #endif /* _WINDOWS */
1.44      cvs      2175: 
1.29      cvs      2176:   if (aName == NULL || relatedName == NULL)
                   2177:     return (NULL);
                   2178: 
                   2179:   slashes = 0;
                   2180:   after_access = NULL;
                   2181:   p = aName;
                   2182:   q = relatedName;
1.147     vatton   2183:   len = 0;
                   2184:   for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27      cvs      2185:     {
                   2186:       /* Find extent of match */
1.106     cvs      2187:       if (*p == ':')
1.146     cvs      2188:          {
                   2189:          after_access = p + 1;
1.147     vatton   2190:          if (len == 1)
                   2191:            /* it's a local Windows path like c:... */
                   2192:            slashes++;
1.146     cvs      2193:          }
1.28      cvs      2194:       if (*p == DIR_SEP)
1.27      cvs      2195:        {
1.29      cvs      2196:          /* memorize the last slash position and count them */
1.27      cvs      2197:          last_slash = p;
                   2198:          slashes++;
1.25      cvs      2199:        }
                   2200:     }
                   2201:     
1.31      cvs      2202:   /* q, p point to the first non-matching character or zero */
1.106     cvs      2203:   if (*q == EOS)
1.31      cvs      2204:     {
                   2205:       /* New name is a subset of the related name */
                   2206:       /* exactly the right length */
1.106     cvs      2207:       len = strlen (p);
                   2208:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2209:        strcpy (return_value, p);
1.31      cvs      2210:     }
                   2211:   else if ((slashes < 2 && after_access == NULL)
                   2212:       || (slashes < 3 && after_access != NULL))
                   2213:     {
                   2214:       /* Two names whitout common path */
                   2215:       /* exactly the right length */
1.106     cvs      2216:       len = strlen (aName);
                   2217:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2218:        strcpy (return_value, aName);
1.31      cvs      2219:     }
                   2220:   else
                   2221:     {
                   2222:       /* Some path in common */
1.106     cvs      2223:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31      cvs      2224:        /* just the same server */
1.106     cvs      2225:        strcpy (result, last_slash);
1.31      cvs      2226:       else
                   2227:        {
                   2228:          levels= 0; 
1.106     cvs      2229:          for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31      cvs      2230:            if (*q == DIR_SEP)
                   2231:              levels++;
                   2232:          
1.106     cvs      2233:          result[0] = EOS;
1.31      cvs      2234:          for (;levels; levels--)
1.106     cvs      2235:            strcat (result, "../");
                   2236:          strcat (result, last_slash+1);
1.31      cvs      2237:        } 
1.52      cvs      2238: 
                   2239:       if (!*result)
1.106     cvs      2240:        strcat (result, "./");
1.52      cvs      2241: 
1.31      cvs      2242:       /* exactly the right length */
1.106     cvs      2243:       len = strlen (result);
                   2244:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   2245:        strcpy (return_value, result);
1.52      cvs      2246: 
1.25      cvs      2247:     }
1.110     cvs      2248: #ifdef _WINDOWS
1.106     cvs      2249:   len = strlen (return_value);
1.44      cvs      2250:   for (ndx = 0; ndx < len; ndx ++)
1.106     cvs      2251:          if (return_value[ndx] == '\\')
                   2252:             return_value[ndx] = '/' ;
1.110     cvs      2253: #endif /* _WINDOWS */
1.29      cvs      2254:   return (return_value);
1.24      cvs      2255: }
1.35      cvs      2256: 
1.104     kahan    2257: /*----------------------------------------------------------------------
                   2258:   AM_GetFileSize
                   2259:   Returns TRUE and the filesize in the 2nd parameter.
                   2260:   Otherwise, in case of a system error, returns FALSE, with a 
                   2261:   filesize of 0L.
                   2262:   ---------------------------------------------------------------------*/
1.106     cvs      2263: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104     kahan    2264: {
1.106     cvs      2265:   ThotFileHandle   handle = ThotFile_BADHANDLE;
                   2266:   ThotFileInfo     info;
1.35      cvs      2267: 
1.104     kahan    2268:   *file_size = 0L;
                   2269:   if (!TtaFileExist (filename))
                   2270:     return FALSE;
                   2271: 
                   2272:   handle = TtaFileOpen (filename, ThotFile_READWRITE);
                   2273:   if (handle == ThotFile_BADHANDLE)
                   2274:     /* ThotFile_BADHANDLE */
                   2275:     return FALSE;
                   2276:    if (TtaFileStat (handle, &info) == 0)
                   2277:      /* bad stat */
                   2278:      info.size = 0L;
                   2279:    TtaFileClose (handle);
                   2280:    *file_size = (unsigned long) info.size;
                   2281:    return TRUE;
                   2282: }
1.139     kahan    2283: 
                   2284: /*----------------------------------------------------------------------
                   2285:   AM_UseXHTMLMimeType
                   2286:   Returns TRUE if the user has configured Amaya to use this MIME type,
                   2287:   FALSE otherwise.
                   2288:   ---------------------------------------------------------------------*/
                   2289: ThotBool AM_UseXHTMLMimeType (void)
                   2290: {
                   2291:   ThotBool xhtml_mimetype;
                   2292:   
                   2293:   /* does the user wants to use the new MIME type? */
                   2294:   TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
                   2295: 
                   2296:   return (xhtml_mimetype);
1.152     kahan    2297: }
                   2298: 
                   2299: /*-----------------------------------------------------------------------
                   2300:   GetTempName
                   2301:   Front end to the Unix tempnam function, which is independent of the
                   2302:   value of the TMPDIR env value 
                   2303:   Returns a dynamically allocated string with a tempname. The user
                   2304:   must free this memory.
                   2305:   -----------------------------------------------------------------------*/
                   2306: char *GetTempName (const char *dir, const char *prefix)
                   2307: {
                   2308:   char *tmpdir;
                   2309:   char *tmp;
                   2310:   char *name = NULL;
                   2311: 
                   2312:   /* save the value of TMPDIR */
                   2313:   tmp = getenv (TMPDIR);
                   2314: 
                   2315:   if (tmp)
                   2316:     {
                   2317:       tmpdir = TtaStrdup (tmp);
                   2318:     }
                   2319:   else
                   2320:     tmpdir = NULL;
                   2321: 
                   2322:   /* remove TMPDIR from the environment */
                   2323:   if (tmpdir)
                   2324:     {
                   2325:       tmp = TtaGetMemory (strlen (tmpdir) + 2);
                   2326:       sprintf (tmp, "%s=", TMPDIR);
                   2327: #ifdef _WINDOWS
                   2328:       _putenv (tmp);
                   2329: #else
                   2330:       putenv (tmp);
                   2331: #endif /* _WINDOWS */
                   2332:     }
                   2333: 
                   2334:   /* create the tempname */
                   2335: #ifdef _WINDOWS
                   2336:   /* Under Windows, _tempnam returns the same name until the file is created */
                   2337:   {
                   2338:     char *altprefix;
                   2339:     name = tmpnam (NULL);      /* get a possibly unique string */
                   2340:     altprefix = TtaGetMemory(strlen (prefix) + strlen(name) + 1);
                   2341:     sprintf (altprefix, "%s%s", prefix, name + strlen(_P_tmpdir));
                   2342:     name = _tempnam (dir, altprefix); /* get a name that isn't yet in use */
                   2343:     TtaFreeMemory (altprefix);
                   2344:   }
                   2345: #else
                   2346:   name = tempnam (dir, prefix);
                   2347: #endif /* _WINDOWS */
                   2348: 
                   2349:   /* restore the value of TMPDIR */
                   2350:   if (tmpdir)
                   2351:     {
                   2352: #ifdef _WINDOWS
                   2353:       _putenv (tmpdir);
                   2354: #else
                   2355:       putenv (tmpdir);
                   2356: #endif /* _WINDOWS */
                   2357:       /* Shouldn't be free (see man for putenv ()) */
                   2358:       /* TtaFreeMemory (tmpdir); */
                   2359:     }
                   2360:   return (name);
1.139     kahan    2361: }

Webmaster