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

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

Webmaster