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

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

Webmaster