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

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

Webmaster