Annotation of Amaya/amaya/AHTURLTools.c, revision 1.115
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: /* & */
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)
1.114 cvs 119: buffer = (char *) 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;
1.115 ! kahan 892:
! 893: /* remove any preceding whitespace */
! 894: while (*src && *src == ' ')
! 895: {
! 896: src++;
! 897: change = 1;
! 898: }
! 899:
1.90 cvs 900: while (*src)
901: {
902: switch (*src)
903: {
1.106 cvs 904: case '\n':
1.90 cvs 905: /* don't copy the newline */
906: change = 1;
907: break;
908: default:
909: *dest = *src;
910: dest++;
911: break;
912: }
913: src++;
914: }
915: /* copy the last EOS char */
916: *dest = *src;
917:
918: return (change);
919: }
920:
921: /*----------------------------------------------------------------------
922: CleanCopyFileURL
923: Copies a file url from a src string to destination string.
1.97 cvs 924: convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
925: we want to do).
1.90 cvs 926: ----------------------------------------------------------------------*/
1.106 cvs 927: static void CleanCopyFileURL (char *dest, char *src,
928: ConvertionType convertion)
1.90 cvs 929: {
930: while (*src)
1.89 cvs 931: {
1.90 cvs 932: switch (*src)
1.89 cvs 933: {
934: #ifdef _WINDOWS
1.106 cvs 935: case URL_SEP:
1.96 cvs 936: /* make DIR_SEP transformation */
1.97 cvs 937: if (convertion & AM_CONV_URL_SEP)
1.106 cvs 938: *dest = DIR_SEP;
1.96 cvs 939: else
940: *dest = *src;
1.90 cvs 941: dest++;
1.96 cvs 942: src++;
1.90 cvs 943: break;
1.89 cvs 944: #endif /* _WINDOWS */
1.96 cvs 945:
1.106 cvs 946: case '%':
1.97 cvs 947: if (convertion & AM_CONV_PERCENT)
1.96 cvs 948: {
1.97 cvs 949: /* (code adapted from libwww's HTUnEscape function */
1.96 cvs 950: src++;
1.106 cvs 951: if (*src != EOS)
1.97 cvs 952: {
953: *dest = UnEscapeChar (*src) * 16;
954: src++;
955: }
1.106 cvs 956: if (*src != EOS)
1.97 cvs 957: {
958: *dest = *dest + UnEscapeChar (*src);
959: src++;
960: }
961: dest++;
1.96 cvs 962: }
1.97 cvs 963: else
1.96 cvs 964: {
1.97 cvs 965: *dest = *src;
966: dest++;
1.96 cvs 967: src++;
968: }
969: break;
970:
1.90 cvs 971: default:
972: *dest = *src;
1.89 cvs 973: dest++;
1.96 cvs 974: src++;
1.90 cvs 975: break;
1.89 cvs 976: }
977: }
1.90 cvs 978: /* copy the EOS char */
979: *dest = *src;
1.73 cvs 980: }
1.40 cvs 981:
982: /*----------------------------------------------------------------------
1.9 cvs 983: NormalizeURL
984: normalizes orgName according to a base associated with doc, and
985: following the standard URL format rules.
1.113 cvs 986: if doc is < 0, use as a base the URL of the document that contains
987: (or contained) the elements that are now in the copy/cut buffer.
1.53 cvs 988: if doc is 0 and otherPath not NULL, normalizes orgName according to this
989: other path.
1.9 cvs 990: The function returns the new complete and normalized URL
1.12 cvs 991: or file name path (newName) and the name of the document (docName).
1.9 cvs 992: N.B. If the function can't find out what's the docName, it assigns
993: the name "noname.html".
1.4 cvs 994: ----------------------------------------------------------------------*/
1.106 cvs 995: void NormalizeURL (char *orgName, Document doc, char *newName,
996: char *docName, char *otherPath)
997: {
998: char *basename;
999: char tempOrgName[MAX_LENGTH];
1000: char *ptr;
1001: char used_sep;
1.84 cvs 1002: int length;
1003: ThotBool check;
1.5 cvs 1004:
1.110 cvs 1005: #ifdef _WINDOWS
1.44 cvs 1006: int ndx;
1.110 cvs 1007: #endif /* _WINDOWS */
1.44 cvs 1008:
1.5 cvs 1009: if (!newName || !docName)
1010: return;
1.18 cvs 1011:
1.113 cvs 1012: if (doc < 0)
1013: basename = TtaStrdup (SavedDocumentURL);
1014: else if (doc > 0)
1.53 cvs 1015: basename = GetBaseURL (doc);
1016: else if (otherPath != NULL)
1.108 cvs 1017: basename = TtaStrdup (otherPath);
1.32 cvs 1018: else
1.53 cvs 1019: basename = NULL;
1.32 cvs 1020:
1.18 cvs 1021: /*
1.31 cvs 1022: * Clean orgName
1023: * Make sure we have a complete orgName, without any leading or trailing
1024: * white spaces, or trailinbg new lines
1025: */
1.5 cvs 1026: ptr = orgName;
1.18 cvs 1027: /* skip leading white space and new line characters */
1.106 cvs 1028: while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
1029: strncpy (tempOrgName, ptr, MAX_LENGTH -1);
1030: tempOrgName[MAX_LENGTH -1] = EOS;
1.18 cvs 1031: /*
1.31 cvs 1032: * Make orgName a complete URL
1033: * If the URL does not include a protocol, then try to calculate
1034: * one using the doc's base element (if it exists),
1035: */
1.106 cvs 1036: if (tempOrgName[0] == EOS)
1.53 cvs 1037: {
1.106 cvs 1038: newName[0] = EOS;
1039: docName[0] = EOS;
1.53 cvs 1040: TtaFreeMemory (basename);
1041: return;
1042: }
1.49 cvs 1043:
1044: /* clean trailing white space */
1.106 cvs 1045: length = strlen (tempOrgName) - 1;
1046: while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53 cvs 1047: {
1.106 cvs 1048: tempOrgName[length] = EOS;
1.53 cvs 1049: length--;
1050: }
1.50 cvs 1051:
1.55 cvs 1052: /* remove extra dot (which dot???) */
1053: /* ugly, but faster than a strcmp */
1.106 cvs 1054: if (tempOrgName[length] == '.'
1055: && (length == 0 || tempOrgName[length-1] != '.'))
1056: tempOrgName[length] = EOS;
1.50 cvs 1057:
1.94 cvs 1058: if (IsW3Path (tempOrgName))
1.53 cvs 1059: {
1060: /* the name is complete, go to the Sixth Step */
1.106 cvs 1061: strcpy (newName, tempOrgName);
1.53 cvs 1062: SimplifyUrl (&newName);
1063: /* verify if the URL has the form "protocol://server:port" */
1.110 cvs 1064: ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
1065: AMAYA_PARSE_HOST |
1066: AMAYA_PARSE_PUNCTUATION);
1067: if (ptr && !strcmp (ptr, newName))
1068: /* it has this form, we complete it by adding a DIR_STR */
1.106 cvs 1069: strcat (newName, URL_STR);
1.49 cvs 1070:
1.53 cvs 1071: if (ptr)
1.50 cvs 1072: TtaFreeMemory (ptr);
1.53 cvs 1073: }
1.113 cvs 1074: else if (basename == NULL)
1.53 cvs 1075: /* the name is complete, go to the Sixth Step */
1.106 cvs 1076: strcpy (newName, tempOrgName);
1.53 cvs 1077: else
1078: {
1.31 cvs 1079: /* Calculate the absolute URL, using the base or document URL */
1.110 cvs 1080: #ifdef _WINDOWS
1.53 cvs 1081: if (!IsW3Path (basename))
1082: {
1.106 cvs 1083: length = strlen (tempOrgName);
1.53 cvs 1084: for (ndx = 0; ndx < length; ndx++)
1.106 cvs 1085: if (tempOrgName [ndx] == '/')
1086: tempOrgName [ndx] = '\\';
1.53 cvs 1087: }
1.110 cvs 1088: #endif /* _WINDOWS */
1.25 cvs 1089: ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53 cvs 1090: if (ptr)
1091: {
1092: SimplifyUrl (&ptr);
1.106 cvs 1093: strcpy (newName, ptr);
1.53 cvs 1094: TtaFreeMemory (ptr);
1095: }
1096: else
1.106 cvs 1097: newName[0] = EOS;
1.53 cvs 1098: }
1.36 cvs 1099:
1100: TtaFreeMemory (basename);
1.18 cvs 1101: /*
1.31 cvs 1102: * Prepare the docname that will refer to this ressource in the
1103: * .amaya directory. If the new URL finishes on DIR_SEP, then use
1104: * noname.html as a default ressource name
1.18 cvs 1105: */
1.106 cvs 1106: if (newName[0] != EOS)
1.53 cvs 1107: {
1.106 cvs 1108: length = strlen (newName) - 1;
1109: if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53 cvs 1110: {
1111: used_sep = newName[length];
1112: check = TRUE;
1113: while (check)
1114: {
1.50 cvs 1115: length--;
1116: while (length >= 0 && newName[length] != used_sep)
1.53 cvs 1117: length--;
1.106 cvs 1118: if (!strncmp (&newName[length+1], "..", 2))
1.53 cvs 1119: {
1.106 cvs 1120: newName[length+1] = EOS;
1.53 cvs 1121: /* remove also previous directory */
1122: length--;
1123: while (length >= 0 && newName[length] != used_sep)
1124: length--;
1.106 cvs 1125: if (strncmp (&newName[length+1], "//", 2))
1.53 cvs 1126: /* don't remove server name */
1.106 cvs 1127: newName[length+1] = EOS;
1.53 cvs 1128: }
1.106 cvs 1129: else if (!strncmp (&newName[length+1], ".", 1))
1130: newName[length+1] = EOS;
1.50 cvs 1131: else
1.53 cvs 1132: check = FALSE;
1133: }
1134: /* docname was not comprised inside the URL, so let's */
1135: /* assign the default ressource name */
1.106 cvs 1136: strcpy (docName, "noname.html");
1.53 cvs 1137: }
1138: else
1139: { /* docname is comprised inside the URL */
1.110 cvs 1140: while (length >= 0 && newName[length] != URL_SEP &&
1141: newName[length] != DIR_SEP)
1.53 cvs 1142: length--;
1143: if (length < 0)
1.106 cvs 1144: strcpy (docName, newName);
1.53 cvs 1145: else
1.106 cvs 1146: strcpy (docName, &newName[length+1]);
1.53 cvs 1147: }
1148: }
1149: else
1.106 cvs 1150: docName[0] = EOS;
1.18 cvs 1151: }
1.3 cvs 1152:
1.4 cvs 1153: /*----------------------------------------------------------------------
1.9 cvs 1154: IsSameHost
1.4 cvs 1155: ----------------------------------------------------------------------*/
1.106 cvs 1156: ThotBool IsSameHost (const char *url1, const char *url2)
1.3 cvs 1157: {
1.106 cvs 1158: char *basename_ptr1, *basename_ptr2;
1159: ThotBool result;
1.3 cvs 1160:
1.106 cvs 1161: basename_ptr1 = AmayaParseUrl (url1, "",
1162: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1163: basename_ptr2 = AmayaParseUrl (url2, "",
1164: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3 cvs 1165:
1.106 cvs 1166: if (strcmp (basename_ptr1, basename_ptr2))
1167: result = FALSE;
1168: else
1169: result = TRUE;
1170: TtaFreeMemory (basename_ptr1);
1171: TtaFreeMemory (basename_ptr2);
1172: return (result);
1.3 cvs 1173: }
1174:
1175:
1.4 cvs 1176: /*----------------------------------------------------------------------
1.22 cvs 1177: HasKnownFileSuffix
1178: returns TRUE if path points to a file ending with a suffix.
1179: ----------------------------------------------------------------------*/
1.106 cvs 1180: ThotBool HasKnownFileSuffix (const char *path)
1181: {
1182: char *root;
1183: char temppath[MAX_LENGTH];
1184: char suffix[MAX_LENGTH];
1.22 cvs 1185:
1.106 cvs 1186: if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22 cvs 1187: return (FALSE);
1188:
1.106 cvs 1189: root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22 cvs 1190:
1191: if (root)
1192: {
1.106 cvs 1193: strcpy (temppath, root);
1.25 cvs 1194: TtaFreeMemory (root);
1.22 cvs 1195: /* Get the suffix */
1196: ExtractSuffix (temppath, suffix);
1197:
1.106 cvs 1198: if( suffix[0] == EOS)
1.22 cvs 1199: /* no suffix */
1200: return (FALSE);
1201:
1202: /* Normalize the suffix */
1203: ConvertToLowerCase (suffix);
1204:
1.106 cvs 1205: if (!strcmp (suffix, "gz"))
1.22 cvs 1206: /* skip the compressed suffix */
1207: {
1208: ExtractSuffix (temppath, suffix);
1.106 cvs 1209: if(suffix[0] == EOS)
1.22 cvs 1210: /* no suffix */
1211: return (FALSE);
1212: /* Normalize the suffix */
1213: ConvertToLowerCase (suffix);
1214: }
1215:
1.106 cvs 1216: if (strcmp (suffix, "gif") &&
1217: strcmp (suffix, "xbm") &&
1218: strcmp (suffix, "xpm") &&
1219: strcmp (suffix, "jpg") &&
1220: strcmp (suffix, "pdf") &&
1221: strcmp (suffix, "png") &&
1222: strcmp (suffix, "tgz") &&
1223: strcmp (suffix, "xpg") &&
1224: strcmp (suffix, "xpd") &&
1225: strcmp (suffix, "ps") &&
1226: strcmp (suffix, "au") &&
1227: strcmp (suffix, "html") &&
1228: strcmp (suffix, "htm") &&
1229: strcmp (suffix, "shtml") &&
1230: strcmp (suffix, "xht") &&
1231: strcmp (suffix, "xhtm") &&
1232: strcmp (suffix, "xhtml") &&
1233: strcmp (suffix, "txt") &&
1234: strcmp (suffix, "css") &&
1235: strcmp (suffix, "eps"))
1.22 cvs 1236: return (FALSE);
1237: else
1238: return (TRUE);
1239: }
1240: else
1241: return (FALSE);
1242: }
1243:
1244:
1245: /*----------------------------------------------------------------------
1.24 cvs 1246: ChopURL
1247: Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL).
1248: If inputURL is bigger than that size, outputURL receives
1249: MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...",
1250: and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
1251: If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
1252: copied into outputURL.
1253: N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
1254: chars.
1255: ----------------------------------------------------------------------*/
1.106 cvs 1256: void ChopURL (char *outputURL, const char *inputURL)
1.24 cvs 1257: {
1258: int len;
1.9 cvs 1259:
1.106 cvs 1260: len = strlen (inputURL);
1.24 cvs 1261: if (len <= MAX_PRINT_URL_LENGTH)
1.106 cvs 1262: strcpy (outputURL, inputURL);
1.24 cvs 1263: else
1264: /* make a truncated urlName on the status window */
1265: {
1.106 cvs 1266: strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
1267: outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
1268: strcat (outputURL, "...");
1269: strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24 cvs 1270: }
1.25 cvs 1271: }
1272:
1273:
1274: /*----------------------------------------------------------------------
1275: scan
1.47 cvs 1276: Scan a filename for its constituents
1.25 cvs 1277: -----------------------------------
1278:
1279: On entry,
1280: name points to a document name which may be incomplete.
1281: On exit,
1282: absolute or relative may be nonzero (but not both).
1283: host, fragment and access may be nonzero if they were specified.
1284: Any which are nonzero point to zero terminated strings.
1285: ----------------------------------------------------------------------*/
1.106 cvs 1286: static void scan (char *name, HTURI *parts)
1.25 cvs 1287: {
1.106 cvs 1288: char * p;
1289: char * after_access = name;
1.32 cvs 1290:
1.43 cvs 1291: memset (parts, '\0', sizeof (HTURI));
1.28 cvs 1292: /* Look for fragment identifier */
1.106 cvs 1293: if ((p = strchr(name, '#')) != NULL)
1.28 cvs 1294: {
1.106 cvs 1295: *p++ = '\0';
1.28 cvs 1296: parts->fragment = p;
1.25 cvs 1297: }
1298:
1.28 cvs 1299: for (p=name; *p; p++)
1300: {
1.106 cvs 1301: if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28 cvs 1302: break;
1.106 cvs 1303: if (*p == ':')
1.28 cvs 1304: {
1305: *p = 0;
1306: parts->access = after_access; /* Scheme has been specified */
1307:
1308: /* The combination of gcc, the "-O" flag and the HP platform is
1309: unhealthy. The following three lines is a quick & dirty fix, but is
1310: not recommended. Rather, turn off "-O". */
1311:
1312: /* after_access = p;*/
1313: /* while (*after_access == 0)*/
1314: /* after_access++;*/
1315: after_access = p+1;
1.106 cvs 1316: if (!strcasecmp("URL", parts->access))
1.28 cvs 1317: /* Ignore IETF's URL: pre-prefix */
1318: parts->access = NULL;
1319: else
1.25 cvs 1320: break;
1321: }
1322: }
1323:
1324: p = after_access;
1.43 cvs 1325: if (*p == URL_SEP || *p == DIR_SEP)
1.28 cvs 1326: {
1.43 cvs 1327: if (p[1] == URL_SEP)
1.28 cvs 1328: {
1.25 cvs 1329: parts->host = p+2; /* host has been specified */
1.28 cvs 1330: *p = 0; /* Terminate access */
1331: /* look for end of host name if any */
1.106 cvs 1332: p = strchr (parts->host, URL_SEP);
1.28 cvs 1333: if (p)
1334: {
1.106 cvs 1335: *p = EOS; /* Terminate host */
1.25 cvs 1336: parts->absolute = p+1; /* Root has been found */
1.28 cvs 1337: }
1338: }
1339: else
1340: /* Root found but no host */
1341: parts->absolute = p+1;
1342: }
1343: else
1344: {
1.25 cvs 1345: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28 cvs 1346: }
1.25 cvs 1347: }
1348:
1349:
1350: /*----------------------------------------------------------------------
1.28 cvs 1351: AmayaParseUrl: parse a Name relative to another name
1352:
1353: This returns those parts of a name which are given (and requested)
1354: substituting bits from the related name where necessary.
1.25 cvs 1355:
1.28 cvs 1356: On entry,
1.25 cvs 1357: aName A filename given
1358: relatedName A name relative to which aName is to be parsed. Give
1359: it an empty string if aName is absolute.
1360: wanted A mask for the bits which are wanted.
1361:
1.28 cvs 1362: On exit,
1.25 cvs 1363: returns A pointer to a malloc'd string which MUST BE FREED
1364: ----------------------------------------------------------------------*/
1.106 cvs 1365: char *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1366: {
1367: char *return_value;
1368: char result[MAX_LENGTH];
1369: char name[MAX_LENGTH];
1370: char rel[MAX_LENGTH];
1371: char *p, *access;
1.29 cvs 1372: HTURI given, related;
1373: int len;
1.106 cvs 1374: char used_sep;
1375: char *used_str;
1.32 cvs 1376:
1.106 cvs 1377: if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33 cvs 1378: {
1.106 cvs 1379: used_str = DIR_STR;
1380: used_sep = DIR_SEP;
1.33 cvs 1381: }
1.32 cvs 1382: else
1.33 cvs 1383: {
1.106 cvs 1384: used_str = URL_STR;
1385: used_sep = URL_SEP;
1.33 cvs 1386: }
1.32 cvs 1387:
1.29 cvs 1388: /* Make working copies of input strings to cut up: */
1389: return_value = NULL;
1390: result[0] = 0; /* Clear string */
1.106 cvs 1391: strcpy (name, aName);
1.29 cvs 1392: if (relatedName != NULL)
1.106 cvs 1393: strcpy (rel, relatedName);
1.29 cvs 1394: else
1.106 cvs 1395: relatedName[0] = EOS;
1.29 cvs 1396:
1397: scan (name, &given);
1398: scan (rel, &related);
1399: access = given.access ? given.access : related.access;
1400: if (wanted & AMAYA_PARSE_ACCESS)
1401: if (access)
1402: {
1.106 cvs 1403: strcat (result, access);
1.29 cvs 1404: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1405: strcat (result, ":");
1.29 cvs 1406: }
1407:
1408: if (given.access && related.access)
1409: /* If different, inherit nothing. */
1.106 cvs 1410: if (strcmp (given.access, related.access) != 0)
1.29 cvs 1411: {
1412: related.host = 0;
1413: related.absolute = 0;
1414: related.relative = 0;
1415: related.fragment = 0;
1416: }
1417:
1418: if (wanted & AMAYA_PARSE_HOST)
1419: if(given.host || related.host)
1420: {
1421: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1422: strcat (result, "//");
1423: strcat (result, given.host ? given.host : related.host);
1.29 cvs 1424: }
1425:
1426: if (given.host && related.host)
1427: /* If different hosts, inherit no path. */
1.106 cvs 1428: if (strcmp (given.host, related.host) != 0)
1.29 cvs 1429: {
1430: related.absolute = 0;
1431: related.relative = 0;
1432: related.fragment = 0;
1433: }
1434:
1435: if (wanted & AMAYA_PARSE_PATH)
1436: {
1437: if (given.absolute)
1438: {
1439: /* All is given */
1440: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1441: strcat (result, used_str);
1442: strcat (result, given.absolute);
1.25 cvs 1443: }
1.29 cvs 1444: else if (related.absolute)
1445: {
1446: /* Adopt path not name */
1.106 cvs 1447: strcat (result, used_str);
1448: strcat (result, related.absolute);
1.29 cvs 1449: if (given.relative)
1450: {
1451: /* Search part? */
1.106 cvs 1452: p = strchr (result, '?');
1.29 cvs 1453: if (!p)
1.106 cvs 1454: p=result+strlen(result)-1;
1.33 cvs 1455: for (; *p!=used_sep; p--); /* last / */
1.29 cvs 1456: /* Remove filename */
1457: p[1]=0;
1458: /* Add given one */
1.106 cvs 1459: strcat (result, given.relative);
1.25 cvs 1460: }
1461: }
1.29 cvs 1462: else if (given.relative)
1463: /* what we've got */
1.106 cvs 1464: strcat (result, given.relative);
1.29 cvs 1465: else if (related.relative)
1.106 cvs 1466: strcat (result, related.relative);
1.29 cvs 1467: else
1468: /* No inheritance */
1.106 cvs 1469: strcat (result, used_str);
1.25 cvs 1470: }
1.29 cvs 1471:
1472: if (wanted & AMAYA_PARSE_ANCHOR)
1473: if (given.fragment || related.fragment)
1474: {
1475: if (given.absolute && given.fragment)
1476: {
1477: /*Fixes for relURLs...*/
1478: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1479: strcat (result, "#");
1480: strcat (result, given.fragment);
1.29 cvs 1481: }
1482: else if (!(given.absolute) && !(given.fragment))
1.106 cvs 1483: strcat (result, "");
1.29 cvs 1484: else
1485: {
1.110 cvs 1486: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1487: strcat (result, "#");
1.110 cvs 1488: strcat (result, given.fragment ? given.fragment : related.fragment);
1.29 cvs 1489: }
1490: }
1.106 cvs 1491: len = strlen (result);
1492: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1493: strcpy (return_value, result);
1.29 cvs 1494: return (return_value); /* exactly the right length */
1.25 cvs 1495: }
1496:
1497: /*----------------------------------------------------------------------
1498: HTCanon
1499: Canonicalizes the URL in the following manner starting from the host
1500: pointer:
1501:
1502: 1) The host name is converted to lowercase
1503: 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
1504:
1505: Return: OK The position of the current path part of the URL
1506: which might be the old one or a new one.
1507:
1508: ----------------------------------------------------------------------*/
1.106 cvs 1509: static char *HTCanon (char **filename, char *host)
1510: {
1511: char *newname = NULL;
1512: char used_sep;
1513: char *path;
1514: char *strptr;
1515: char *port;
1516: char *access = host-3;
1517:
1518: if (*filename && strchr (*filename, URL_SEP))
1519: used_sep = URL_SEP;
1.33 cvs 1520: else
1.106 cvs 1521: used_sep = DIR_SEP;
1.32 cvs 1522:
1.110 cvs 1523: while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25 cvs 1524: access--;
1.110 cvs 1525: if ((path = strchr (host, used_sep)) == NULL) /* Find path */
1.106 cvs 1526: path = host + strlen (host);
1527: if ((strptr = strchr (host, '@')) != NULL && strptr < path) /* UserId */
1.82 cvs 1528: host = strptr;
1.110 cvs 1529: if ((port = strchr (host, ':')) != NULL && port > path) /* Port number */
1.82 cvs 1530: port = NULL;
1.25 cvs 1531:
1532: strptr = host; /* Convert to lower-case */
1.82 cvs 1533: while (strptr < path)
1.33 cvs 1534: {
1.84 cvs 1535: *strptr = utolower (*strptr);
1.82 cvs 1536: strptr++;
1.33 cvs 1537: }
1.25 cvs 1538:
1539: /* Does the URL contain a full domain name? This also works for a
1540: numerical host name. The domain name is already made lower-case
1541: and without a trailing dot. */
1542: {
1.106 cvs 1543: char *dot = port ? port : path;
1544: if (dot > *filename && *--dot == '.')
1.33 cvs 1545: {
1.106 cvs 1546: char *orig = dot;
1547: char *dest = dot + 1;
1.82 cvs 1548: while ((*orig++ = *dest++));
1549: if (port) port--;
1.33 cvs 1550: path--;
1.25 cvs 1551: }
1552: }
1553: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33 cvs 1554: if (port)
1555: {
1.82 cvs 1556: if (!*(port+1) || *(port+1) == used_sep)
1.33 cvs 1557: {
1558: if (!newname)
1559: {
1.106 cvs 1560: char *orig = port;
1561: char *dest = port + 1;
1.82 cvs 1562: while ((*orig++ = *dest++));
1.33 cvs 1563: }
1564: }
1.106 cvs 1565: else if ((!strncmp (access, "http", 4) &&
1566: (*(port + 1) == '8' &&
1567: *(port+2) == '0' &&
1.82 cvs 1568: (*(port+3) == used_sep || !*(port + 3)))) ||
1.106 cvs 1569: (!strncmp (access, "gopher", 6) &&
1570: (*(port+1) == '7' &&
1571: *(port+2) == '0' &&
1.82 cvs 1572: (*(port+3) == used_sep || !*(port+3)))) ||
1.106 cvs 1573: (!strncmp (access, "ftp", 3) &&
1574: (*(port+1) == '2' &&
1575: *(port + 2) == '1' &&
1.82 cvs 1576: (*(port+3) == used_sep || !*(port+3))))) {
1.33 cvs 1577: if (!newname)
1578: {
1.106 cvs 1579: char *orig = port;
1580: char *dest = port + 3;
1.33 cvs 1581: while((*orig++ = *dest++));
1582: /* Update path position, Henry Minsky */
1583: path -= 3;
1.25 cvs 1584: }
1.33 cvs 1585: }
1586: else if (newname)
1.106 cvs 1587: strncat (newname, port, (int) (path - port));
1.33 cvs 1588: }
1.25 cvs 1589:
1.33 cvs 1590: if (newname)
1591: {
1.106 cvs 1592: char *newpath = newname + strlen (newname);
1593: strcat (newname, path);
1.25 cvs 1594: path = newpath;
1.28 cvs 1595: /* Free old copy */
1596: TtaFreeMemory(*filename);
1.25 cvs 1597: *filename = newname;
1.33 cvs 1598: }
1.25 cvs 1599: return path;
1600: }
1601:
1602:
1603: /*----------------------------------------------------------------------
1.29 cvs 1604: SimplifyUrl: simplify a URI
1.32 cvs 1605: A URI is allowed to contain the sequence xxx/../ which may be
1606: replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28 cvs 1607: Simplification helps us recognize duplicate URIs.
1.25 cvs 1608:
1.28 cvs 1609: Thus, /etc/junk/../fred becomes /etc/fred
1610: /etc/junk/./fred becomes /etc/junk/fred
1.25 cvs 1611:
1.28 cvs 1612: but we should NOT change
1613: http://fred.xxx.edu/../..
1.25 cvs 1614:
1615: or ../../albert.html
1616:
1.28 cvs 1617: In order to avoid empty URLs the following URLs become:
1.25 cvs 1618:
1619: /fred/.. becomes /fred/..
1620: /fred/././.. becomes /fred/..
1621: /fred/.././junk/.././ becomes /fred/..
1622:
1.28 cvs 1623: If more than one set of `://' is found (several proxies in cascade) then
1624: only the part after the last `://' is simplified.
1.25 cvs 1625:
1.28 cvs 1626: Returns: A string which might be the old one or a new one.
1.25 cvs 1627: ----------------------------------------------------------------------*/
1.106 cvs 1628: void SimplifyUrl (char **url)
1629: {
1630: char *path;
1631: char *access;
1632: char *newptr;
1633: char *p;
1634: char *orig, *dest, *end;
1.28 cvs 1635:
1.106 cvs 1636: char used_sep;
1.77 cvs 1637: ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
1638: something/../ simplification in relative URLs when they start with a ../ */
1.32 cvs 1639:
1640:
1.28 cvs 1641: if (!url || !*url)
1642: return;
1643:
1.106 cvs 1644: if (strchr (*url, URL_SEP))
1645: used_sep = URL_SEP;
1.32 cvs 1646: else
1.106 cvs 1647: used_sep = DIR_SEP;
1.32 cvs 1648:
1.77 cvs 1649: /* should we simplify double dot? */
1650: path = *url;
1.106 cvs 1651: if (*path == '.' && *(path + 1) == '.')
1.77 cvs 1652: ddot_simplify = FALSE;
1653: else
1654: ddot_simplify = TRUE;
1655:
1.28 cvs 1656: /* Find any scheme name */
1.106 cvs 1657: if ((path = strstr (*url, "://")) != NULL)
1.33 cvs 1658: {
1659: /* Find host name */
1.28 cvs 1660: access = *url;
1.84 cvs 1661: while (access < path && (*access = utolower (*access)))
1.82 cvs 1662: access++;
1.28 cvs 1663: path += 3;
1.106 cvs 1664: while ((newptr = strstr (path, "://")) != NULL)
1.82 cvs 1665: /* For proxies */
1.106 cvs 1666: path = newptr + 3;
1.82 cvs 1667: /* We have a host name */
1.84 cvs 1668: path = HTCanon (url, path);
1.25 cvs 1669: }
1.106 cvs 1670: else if ((path = strstr (*url, ":/")) != NULL)
1.28 cvs 1671: path += 2;
1672: else
1673: path = *url;
1.25 cvs 1674:
1.84 cvs 1675: if (*path == used_sep && *(path+1) == used_sep)
1.28 cvs 1676: /* Some URLs start //<foo> */
1677: path += 1;
1.94 cvs 1678: else if (IsFilePath (path))
1679: {
1680: /* doesn't need to do anything more */
1681: return;
1682: }
1.106 cvs 1683: else if (!strncmp (path, "news:", 5))
1.28 cvs 1684: {
1.106 cvs 1685: newptr = strchr (path+5, '@');
1.28 cvs 1686: if (!newptr)
1687: newptr = path + 5;
1688: while (*newptr)
1689: {
1690: /* Make group or host lower case */
1.84 cvs 1691: *newptr = utolower (*newptr);
1.28 cvs 1692: newptr++;
1.25 cvs 1693: }
1.28 cvs 1694: /* Doesn't need to do any more */
1695: return;
1.25 cvs 1696: }
1.28 cvs 1697:
1698: if ((p = path))
1699: {
1.106 cvs 1700: if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
1701: (end = strchr (path, '#'))))
1702: end = path + strlen (path);
1.28 cvs 1703:
1704: /* Parse string second time to simplify */
1705: p = path;
1706: while (p < end)
1707: {
1.110 cvs 1708: /* if we're pointing to a char, it's safe to reactivate the
1709: ../ convertion */
1.106 cvs 1710: if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77 cvs 1711: ddot_simplify = TRUE;
1712:
1.33 cvs 1713: if (*p==used_sep)
1.28 cvs 1714: {
1.106 cvs 1715: if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28 cvs 1716: {
1717: orig = p + 1;
1.84 cvs 1718: dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52 cvs 1719: while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28 cvs 1720: end = orig - 1;
1721: }
1.106 cvs 1722: else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.'
1.77 cvs 1723: && (*(p+3) == used_sep || !*(p+3)))
1.28 cvs 1724: {
1725: newptr = p;
1.52 cvs 1726: while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
1727: if (*newptr == used_sep)
1728: orig = newptr + 1;
1.28 cvs 1729: else
1.52 cvs 1730: orig = newptr;
1731:
1732: dest = (*(p+3) != used_sep) ? p+3 : p+4;
1733: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
1734: end = orig-1;
1735: /* Start again with prev slash */
1736: p = newptr;
1.28 cvs 1737: }
1.33 cvs 1738: else if (*(p+1) == used_sep)
1.28 cvs 1739: {
1.33 cvs 1740: while (*(p+1) == used_sep)
1.28 cvs 1741: {
1742: orig = p;
1743: dest = p + 1;
1744: while ((*orig++ = *dest++)); /* Remove multiple /'s */
1745: end = orig-1;
1746: }
1747: }
1748: else
1.25 cvs 1749: p++;
1.28 cvs 1750: }
1751: else
1752: p++;
1.25 cvs 1753: }
1754: }
1.51 cvs 1755:
1756: /*
1757: ** Check for host/../.. kind of things
1758: */
1.106 cvs 1759: if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.'
1.77 cvs 1760: && (!*(path+3) || *(path+3) == used_sep))
1.106 cvs 1761: *(path+1) = EOS;
1.51 cvs 1762:
1.28 cvs 1763: return;
1764: }
1765:
1766:
1767: /*----------------------------------------------------------------------
1.96 cvs 1768: NormalizeFile normalizes local names.
1.28 cvs 1769: Return TRUE if target and src differ.
1770: ----------------------------------------------------------------------*/
1.106 cvs 1771: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28 cvs 1772: {
1.110 cvs 1773: #ifndef _WINDOWS
1.106 cvs 1774: char *s;
1.93 cvs 1775: int i;
1.110 cvs 1776: #endif /* !_WINDOWS */
1.82 cvs 1777: ThotBool change;
1.90 cvs 1778: int start_index; /* the first char that we'll copy */
1.28 cvs 1779:
1.54 cvs 1780: change = FALSE;
1.90 cvs 1781: start_index = 0;
1782:
1.106 cvs 1783: if (!src || src[0] == EOS)
1.96 cvs 1784: {
1.106 cvs 1785: target[0] = EOS;
1.96 cvs 1786: return FALSE;
1787: }
1.90 cvs 1788:
1789: /* @@ do I need file: or file:/ here? */
1.106 cvs 1790: if (strncmp (src, "file:", 5) == 0)
1.28 cvs 1791: {
1.90 cvs 1792: /* remove the prefix file: */
1793: start_index += 5;
1794:
1795: /* remove the localhost prefix */
1.106 cvs 1796: if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94 cvs 1797: start_index += 11;
1798:
1799: /* remove the first two slashes in / / /path */
1800: while (src[start_index] &&
1.106 cvs 1801: src[start_index] == '/'
1802: && src[start_index + 1] == '/')
1.94 cvs 1803: start_index++;
1804:
1805: #ifdef _WINDOWS
1806: /* remove any extra slash before the drive name */
1.106 cvs 1807: if (src[start_index] == '/'
1808: &&src[start_index+2] == ':')
1.94 cvs 1809: start_index++;
1810: #endif /* _WINDOWS */
1.90 cvs 1811:
1.106 cvs 1812: if (src[start_index] == EOS)
1.90 cvs 1813: /* if there's nothing afterwards, add a DIR_STR */
1.106 cvs 1814: strcpy (target, DIR_STR);
1.90 cvs 1815: else
1.97 cvs 1816: /* as we're inside a file: URL, we'll apply all the convertions
1817: we know */
1818: CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96 cvs 1819:
1820: change = TRUE;
1821: }
1.97 cvs 1822: else if (convertion != AM_CONV_NONE)
1.96 cvs 1823: {
1824: /* we are following a "local" relative link, we do all the
1825: convertions except for the HOME_DIR ~ one */
1.97 cvs 1826: CleanCopyFileURL (target, src, convertion);
1.28 cvs 1827: }
1.90 cvs 1828: #ifndef _WINDOWS
1.106 cvs 1829: else if (src[0] == '~')
1.53 cvs 1830: {
1.96 cvs 1831: /* it must be a URL typed in a text input field */
1832: /* do the HOME_DIR ~ substitution */
1.82 cvs 1833: s = TtaGetEnvString ("HOME");
1.106 cvs 1834: strcpy (target, s);
1.90 cvs 1835: #if 0
1.96 cvs 1836: /* JK: invalidated this part of the code as it's simpler
1837: to add the DIR_SEP whenever we have something to add
1838: to the path rather than adding it systematically */
1.106 cvs 1839: if (src[1] != DIR_SEP)
1840: strcat (target, DIR_STR);
1.90 cvs 1841: #endif
1.106 cvs 1842: i = strlen (target);
1843: strcpy (&target[i], &src[1]);
1.54 cvs 1844: change = TRUE;
1.53 cvs 1845: }
1.90 cvs 1846: #endif /* _WINDOWS */
1.28 cvs 1847: else
1.96 cvs 1848: /* leave it as it is */
1.106 cvs 1849: strcpy (target, src);
1.96 cvs 1850:
1.28 cvs 1851: /* remove /../ and /./ */
1.29 cvs 1852: SimplifyUrl (&target);
1.54 cvs 1853: if (!change)
1.106 cvs 1854: change = strcmp (src, target);
1.28 cvs 1855: return (change);
1.25 cvs 1856: }
1857:
1.28 cvs 1858:
1.25 cvs 1859: /*----------------------------------------------------------------------
1.31 cvs 1860: MakeRelativeURL: make relative name
1.25 cvs 1861:
1.28 cvs 1862: This function creates and returns a string which gives an expression of
1863: one address as related to another. Where there is no relation, an absolute
1864: address is retured.
1.25 cvs 1865:
1.28 cvs 1866: On entry,
1.25 cvs 1867: Both names must be absolute, fully qualified names of nodes
1868: (no fragment bits)
1869:
1.28 cvs 1870: On exit,
1.25 cvs 1871: The return result points to a newly allocated name which, if
1872: parsed by AmayaParseUrl relative to relatedName, will yield aName.
1873: The caller is responsible for freeing the resulting name later.
1874: ----------------------------------------------------------------------*/
1.106 cvs 1875: char *MakeRelativeURL (char *aName, char *relatedName)
1876: {
1877: char *return_value;
1878: char result[MAX_LENGTH];
1879: char *p;
1880: char *q;
1881: char *after_access;
1882: char *last_slash = NULL;
1883: int slashes, levels, len;
1.110 cvs 1884: #ifdef _WINDOWS
1.44 cvs 1885: int ndx;
1.110 cvs 1886: #endif /* _WINDOWS */
1.44 cvs 1887:
1.29 cvs 1888: if (aName == NULL || relatedName == NULL)
1889: return (NULL);
1890:
1891: slashes = 0;
1892: after_access = NULL;
1893: p = aName;
1894: q = relatedName;
1895: for (; *p && (*p == *q); p++, q++)
1.27 cvs 1896: {
1897: /* Find extent of match */
1.106 cvs 1898: if (*p == ':')
1.29 cvs 1899: after_access = p + 1;
1.28 cvs 1900: if (*p == DIR_SEP)
1.27 cvs 1901: {
1.29 cvs 1902: /* memorize the last slash position and count them */
1.27 cvs 1903: last_slash = p;
1904: slashes++;
1.25 cvs 1905: }
1906: }
1907:
1.31 cvs 1908: /* q, p point to the first non-matching character or zero */
1.106 cvs 1909: if (*q == EOS)
1.31 cvs 1910: {
1911: /* New name is a subset of the related name */
1912: /* exactly the right length */
1.106 cvs 1913: len = strlen (p);
1914: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1915: strcpy (return_value, p);
1.31 cvs 1916: }
1917: else if ((slashes < 2 && after_access == NULL)
1918: || (slashes < 3 && after_access != NULL))
1919: {
1920: /* Two names whitout common path */
1921: /* exactly the right length */
1.106 cvs 1922: len = strlen (aName);
1923: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1924: strcpy (return_value, aName);
1.31 cvs 1925: }
1926: else
1927: {
1928: /* Some path in common */
1.106 cvs 1929: if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31 cvs 1930: /* just the same server */
1.106 cvs 1931: strcpy (result, last_slash);
1.31 cvs 1932: else
1933: {
1934: levels= 0;
1.106 cvs 1935: for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31 cvs 1936: if (*q == DIR_SEP)
1937: levels++;
1938:
1.106 cvs 1939: result[0] = EOS;
1.31 cvs 1940: for (;levels; levels--)
1.106 cvs 1941: strcat (result, "../");
1942: strcat (result, last_slash+1);
1.31 cvs 1943: }
1.52 cvs 1944:
1945: if (!*result)
1.106 cvs 1946: strcat (result, "./");
1.52 cvs 1947:
1.31 cvs 1948: /* exactly the right length */
1.106 cvs 1949: len = strlen (result);
1950: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1951: strcpy (return_value, result);
1.52 cvs 1952:
1.25 cvs 1953: }
1.110 cvs 1954: #ifdef _WINDOWS
1.106 cvs 1955: len = strlen (return_value);
1.44 cvs 1956: for (ndx = 0; ndx < len; ndx ++)
1.106 cvs 1957: if (return_value[ndx] == '\\')
1958: return_value[ndx] = '/' ;
1.110 cvs 1959: #endif /* _WINDOWS */
1.29 cvs 1960: return (return_value);
1.24 cvs 1961: }
1.35 cvs 1962:
1.104 kahan 1963: /*----------------------------------------------------------------------
1964: AM_GetFileSize
1965: Returns TRUE and the filesize in the 2nd parameter.
1966: Otherwise, in case of a system error, returns FALSE, with a
1967: filesize of 0L.
1968: ---------------------------------------------------------------------*/
1.106 cvs 1969: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104 kahan 1970: {
1.106 cvs 1971: ThotFileHandle handle = ThotFile_BADHANDLE;
1972: ThotFileInfo info;
1.35 cvs 1973:
1.104 kahan 1974: *file_size = 0L;
1975: if (!TtaFileExist (filename))
1976: return FALSE;
1977:
1978: handle = TtaFileOpen (filename, ThotFile_READWRITE);
1979: if (handle == ThotFile_BADHANDLE)
1980: /* ThotFile_BADHANDLE */
1981: return FALSE;
1982: if (TtaFileStat (handle, &info) == 0)
1983: /* bad stat */
1984: info.size = 0L;
1985: TtaFileClose (handle);
1986: *file_size = (unsigned long) info.size;
1987: return TRUE;
1988: }
Webmaster