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

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

Webmaster