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

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

Webmaster