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

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

Webmaster