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

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

Webmaster