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

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

Webmaster