Annotation of Amaya/amaya/AHTURLTools.c, revision 1.186
1.7 cvs 1: /*
2: *
1.176 vatton 3: * (c) COPYRIGHT INRIA and W3C, 1996-2004
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.15 cvs 17: #define THOT_EXPORT extern
1.3 cvs 18: #include "amaya.h"
19:
1.8 cvs 20: #include "init_f.h"
21: #include "AHTURLTools_f.h"
1.100 kahan 22: #include "query_f.h"
1.8 cvs 23:
1.24 cvs 24: #define MAX_PRINT_URL_LENGTH 50
1.106 cvs 25: typedef struct _HTURI
26: {
27: char *access; /* Now known as "scheme" */
28: char *host;
29: char *absolute;
30: char *relative;
31: char *fragment;
1.29 cvs 32: } HTURI;
1.24 cvs 33:
1.175 cvs 34: #ifdef _WINDOWS
1.164 kahan 35: #ifndef PATH_MAX
1.163 cvs 36: #define PATH_MAX MAX_PATH
1.164 kahan 37: #endif
1.155 cvs 38: #define stat _stat
39: #define uint64_t unsigned __int64
40: #define getpid _getpid
1.175 cvs 41: #else /* _WINDOWS */
1.161 kahan 42: #if HAVE_STDINT_H
43: #include <stdint.h>
44: #endif /* HAVE_STDINT_H */
1.175 cvs 45: #endif /* _WINDOWS */
1.28 cvs 46:
47: /*----------------------------------------------------------------------
48: ConvertToLowerCase
49: Converts a string to lowercase.
50: ----------------------------------------------------------------------*/
1.124 vatton 51: void ConvertToLowerCase (char *string)
1.28 cvs 52: {
53: int i;
1.93 cvs 54:
1.28 cvs 55: if (!string)
56: return;
57:
1.106 cvs 58: for (i = 0; string[i] != EOS; i++)
1.123 vatton 59: string[i] = tolower (string[i]);
1.28 cvs 60: }
1.22 cvs 61:
1.8 cvs 62: /*----------------------------------------------------------------------
1.75 cvs 63: EscapeChar
64: writes the equivalent escape code of a char in a string
65: ----------------------------------------------------------------------*/
1.109 cvs 66: void EscapeChar (char *string, char c)
1.75 cvs 67: {
1.109 cvs 68: unsigned int i;
69:
70: i = (unsigned char) c & 0xFF;
71: sprintf (string, "%02x", i);
1.75 cvs 72: }
73:
74: /*----------------------------------------------------------------------
1.96 cvs 75: UnEscapeChar
76: writes the equivalent hex code to a %xx coded char
77: ----------------------------------------------------------------------*/
1.109 cvs 78: static char UnEscapeChar (char c)
1.96 cvs 79: {
1.106 cvs 80: return c >= '0' && c <= '9' ? c - '0'
81: : c >= 'A' && c <= 'F' ? c - 'A' + 10
82: : c - 'a' + 10; /* accept small letters just in case */
1.96 cvs 83: }
84:
85: /*----------------------------------------------------------------------
1.75 cvs 86: EscapeURL
87: Takes a URL and escapes all protected chars into
88: %xx sequences. Also, removes any leading white spaces
89: Returns either NULL or a new buffer, which must be freed by the caller
90: ----------------------------------------------------------------------*/
1.106 cvs 91: char *EscapeURL (const char *url)
92: {
93: char *buffer;
94: int buffer_len;
95: int buffer_free_mem;
96: char *ptr;
97: int new_chars;
1.75 cvs 98: void *status;
99:
100: if (url && *url)
101: {
1.106 cvs 102: buffer_free_mem = strlen (url) + 20;
1.171 gully 103: buffer = (char *)TtaGetMemory (buffer_free_mem + 1);
1.107 kahan 104: ptr = (char *) url;
1.75 cvs 105: buffer_len = 0;
106:
107: while (*ptr)
108: {
109: switch (*ptr)
110: {
111: /* put here below all the chars that need to
112: be escaped into %xx */
1.81 cvs 113: case 0x20: /* space */
1.143 vatton 114: /*case 0x26:*/ /* & */
1.140 kahan 115: case 0x27: /* antislash */
1.75 cvs 116: new_chars = 3;
117: break;
118:
119: default:
1.122 kahan 120: if ((unsigned char )*ptr > 127)
121: new_chars = 3;
122: else
123: new_chars = 1;
1.75 cvs 124: break;
125: }
126:
127: /* see if we need extra room in the buffer */
128: if (new_chars > buffer_free_mem)
129: {
1.76 cvs 130: buffer_free_mem = 20;
1.106 cvs 131: status = TtaRealloc (buffer, sizeof (char)
1.75 cvs 132: * (buffer_len + buffer_free_mem + 1));
133: if (status)
1.114 cvs 134: buffer = (char *) status;
1.106 cvs 135: else
136: {
137: /* @@ maybe we should do some other behavior here, like
138: freeing the buffer and return a void thing */
139: buffer[buffer_len] = EOS;
140: break;
141: }
1.75 cvs 142: }
143: /* escape the char */
1.140 kahan 144: if (new_chars == 3)
145: {
1.106 cvs 146: buffer[buffer_len] = '%';
1.75 cvs 147: EscapeChar (&buffer[buffer_len+1], *ptr);
148: }
1.140 kahan 149: else
150: buffer[buffer_len] = *ptr;
151:
152: /* update the status */
153: buffer_len += new_chars;
154: buffer_free_mem -= new_chars;
155: /* examine the next char */
156: ptr++;
157: }
158: buffer[buffer_len] = EOS;
159: }
160: else
161: buffer = NULL;
162:
163: return (buffer);
164: }
165:
166: /*----------------------------------------------------------------------
167: EscapeXML
168: Takes a string and escapes all protected chars into entity
169: sequences.
170: Returns either NULL or a new buffer, which must be freed by the caller
171: ----------------------------------------------------------------------*/
172: char *EscapeXML (const char *string)
173: {
174: char *buffer;
175: int buffer_len;
176: int buffer_free_mem;
177: char *ptr;
178: char *entity = NULL;
179: int new_chars;
180: void *status;
181:
182: if (string && *string)
183: {
184: buffer_free_mem = strlen (string) + 20;
1.171 gully 185: buffer = (char *)TtaGetMemory (buffer_free_mem + 1);
1.140 kahan 186: ptr = (char *) string;
187: buffer_len = 0;
188:
189: while (*ptr)
190: {
191: switch (*ptr)
192: {
193: case 0x26: /* & */
194: entity = "&";
1.141 kahan 195: new_chars = sizeof (entity) - 1;
196: break;
1.140 kahan 197:
1.141 kahan 198: case '<': /* < */
1.140 kahan 199: entity = "<";
1.141 kahan 200: new_chars = sizeof (entity) - 1;
1.140 kahan 201: break;
202:
1.141 kahan 203: case '>': /* > */
1.140 kahan 204: entity = ">";
1.141 kahan 205: new_chars = sizeof (entity) - 1;
206: break;
207:
208: case '"': /* "e; */
209: entity = ""e;";
210: new_chars = sizeof (entity) - 1;
1.140 kahan 211: break;
212:
213: default:
214: new_chars = 1;
215: break;
216: }
217:
218: /* see if we need extra room in the buffer */
219: if (new_chars > buffer_free_mem)
220: {
221: buffer_free_mem = 20;
222: status = TtaRealloc (buffer, sizeof (char)
223: * (buffer_len + buffer_free_mem + 1));
224: if (status)
225: buffer = (char *) status;
226: else
227: {
228: /* @@ maybe we should do some other behavior here, like
229: freeing the buffer and return a void thing */
230: buffer[buffer_len] = EOS;
231: break;
232: }
233: }
234: /* escape the char */
235: if (entity)
236: {
237: sprintf (&buffer[buffer_len], "%s", entity);
238: entity = NULL;
239: }
1.75 cvs 240: else
241: buffer[buffer_len] = *ptr;
242:
243: /* update the status */
244: buffer_len += new_chars;
245: buffer_free_mem -= new_chars;
246: /* examine the next char */
247: ptr++;
248: }
1.106 cvs 249: buffer[buffer_len] = EOS;
1.75 cvs 250: }
1.76 cvs 251: else
252: buffer = NULL;
253:
1.75 cvs 254: return (buffer);
1.122 kahan 255: }
256:
1.75 cvs 257:
258: /*----------------------------------------------------------------------
1.11 cvs 259: ExplodeURL
1.8 cvs 260: ----------------------------------------------------------------------*/
1.106 cvs 261: void ExplodeURL (char *url, char **proto, char **host, char **dir,
262: char **file)
1.8 cvs 263: {
1.33 cvs 264: char *curr, *temp;
265: char used_sep;
1.32 cvs 266:
1.33 cvs 267: if (url && strchr (url, URL_SEP))
268: used_sep = URL_SEP;
269: else
270: used_sep = DIR_SEP;
1.8 cvs 271:
272: if ((url == NULL) || (proto == NULL) || (host == NULL) ||
273: (dir == NULL) || (file == NULL))
274: return;
275:
276: /* initialize every pointer */
277: *proto = *host = *dir = *file = NULL;
278:
279: /* skip any leading space */
280: while ((*url == SPACE) || (*url == TAB))
281: url++;
1.9 cvs 282: curr = url;
283: if (*curr == 0)
1.8 cvs 284: goto finished;
285:
286: /* go to the end of the URL */
1.68 cvs 287: while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
288: (*curr != __CR__) && (*curr != EOL))
1.9 cvs 289: curr++;
1.8 cvs 290:
291: /* mark the end of the chain */
1.9 cvs 292: *curr = EOS;
293: curr--;
294: if (curr <= url)
1.8 cvs 295: goto finished;
296:
297: /* search the next DIR_SEP indicating the beginning of the file name */
298: do
1.11 cvs 299: curr--;
1.33 cvs 300: while ((curr >= url) && (*curr != used_sep));
1.11 cvs 301:
1.9 cvs 302: if (curr < url)
1.8 cvs 303: goto finished;
1.9 cvs 304: *file = curr + 1;
1.8 cvs 305:
306: /* mark the end of the dir */
1.9 cvs 307: *curr = EOS;
308: curr--;
309: if (curr < url)
1.8 cvs 310: goto finished;
311:
1.29 cvs 312: /* search for the DIR_STR indicating the host name start */
1.33 cvs 313: while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9 cvs 314: curr--;
1.8 cvs 315:
316: /* if we found it, separate the host name from the directory */
1.102 kahan 317: if ((*curr == used_sep) && (*(curr + 1) == used_sep))
1.8 cvs 318: {
1.9 cvs 319: *host = temp = curr + 2;
1.33 cvs 320: while ((*temp != 0) && (*temp != used_sep))
1.8 cvs 321: temp++;
1.33 cvs 322: if (*temp == used_sep)
1.8 cvs 323: {
324: *temp = EOS;
325: *dir = temp + 1;
326: }
327: }
328: else
1.11 cvs 329: *dir = curr;
330:
1.9 cvs 331: if (curr <= url)
1.8 cvs 332: goto finished;
333:
334: /* mark the end of the proto */
1.9 cvs 335: *curr = EOS;
336: curr--;
337: if (curr < url)
1.8 cvs 338: goto finished;
339:
1.106 cvs 340: if (*curr == ':')
1.8 cvs 341: {
1.9 cvs 342: *curr = EOS;
343: curr--;
1.8 cvs 344: }
345: else
346: goto finished;
1.11 cvs 347:
1.9 cvs 348: if (curr < url)
1.8 cvs 349: goto finished;
1.9 cvs 350: while ((curr > url) && (isalpha (*curr)))
351: curr--;
352: *proto = curr;
1.8 cvs 353:
354: finished:;
355:
356: #ifdef AMAYA_DEBUG
357: fprintf (stderr, "ExplodeURL(%s)\n\t", url);
358: if (*proto)
359: fprintf (stderr, "proto : %s, ", *proto);
360: if (*host)
361: fprintf (stderr, "host : %s, ", *host);
362: if (*dir)
363: fprintf (stderr, "dir : %s, ", *dir);
364: if (*file)
365: fprintf (stderr, "file : %s ", *file);
366: fprintf (stderr, "\n");
367: #endif
368:
369: }
1.3 cvs 370:
1.116 kahan 371: /*----------------------------------------------------------------------
372: PicTypeToMime
373: Converts a Thot PicType into the equivalent MIME type. If no convertion
374: is possible, it returns NULL.
375: ----------------------------------------------------------------------*/
376: char *PicTypeToMIME (PicType contentType)
377: {
378: char *mime_type;
379:
380: switch (contentType)
381: {
1.179 vatton 382: case xbm_type:
383: mime_type ="image/x-xbitmap";
384: break;
1.116 kahan 385: case eps_type:
386: mime_type ="application/postscript";
387: break;
388: case xpm_type:
389: mime_type ="image/x-xpicmap";
390: break;
391: case gif_type:
392: mime_type ="image/gif";
393: break;
394: case jpeg_type:
395: mime_type ="image/jpeg";
396: break;
397: case png_type:
398: mime_type ="image/png";
399: break;
400: case svg_type:
1.165 cvs 401: mime_type = AM_SVG_MIME_TYPE;
402: break;
403: case html_type:
404: mime_type = AM_XHTML_MIME_TYPE;
405: break;
406: case mathml_type:
407: mime_type = AM_MATHML_MIME_TYPE;
1.116 kahan 408: break;
409: case unknown_type:
410: default:
411: mime_type = NULL;
412: }
413:
414: return mime_type;
415: }
1.61 cvs 416:
417: /*----------------------------------------------------------------------
1.117 kahan 418: ImageElement
419: Returns the element (image parameter) and URL (url parameter) of an
420: image in a docImage document. The user must free the memory associated
1.120 kahan 421: with the url parameter if the function is succesful.
422: If the url parameter is NULL, we won't initialize it.
1.117 kahan 423: Returns TRUE if succesful, FALSE otherwise.
424: ----------------------------------------------------------------------*/
425: ThotBool ImageElement (Document doc, char **url, Element *image)
426: {
427: Element el, imgEl;
428: Attribute attr, srcAttr;
429: AttributeType attrType;
430: int length;
1.176 vatton 431: char *utf8value;
1.117 kahan 432:
433: if (DocumentTypes[doc] != docImage)
434: return FALSE;
435:
436: /* find the value of the src attribute */
437: attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
438: attrType.AttrTypeNum = HTML_ATTR_SRC;
439: el = TtaGetRootElement (doc);
440: TtaSearchAttribute (attrType, SearchInTree, el, &imgEl, &srcAttr);
441:
442: if (!imgEl)
443: return FALSE;
444: *image = imgEl;
445:
1.120 kahan 446: if (url)
447: {
448: attr = TtaGetAttribute (imgEl, attrType);
449: length = TtaGetTextAttributeLength (srcAttr) + 1;
1.176 vatton 450: utf8value = (char *)TtaGetMemory (length);
451: TtaGiveTextAttributeValue (srcAttr, utf8value, &length);
452: *url = (char *)TtaConvertMbsToByte ((unsigned char *)utf8value,
453: TtaGetDefaultCharset ());
454: TtaFreeMemory (utf8value);
1.120 kahan 455: }
1.117 kahan 456: return TRUE;
457: }
458:
459: /*----------------------------------------------------------------------
460: DocImageMimeType
461: Returns the MIME type of a docImage document.
462: ----------------------------------------------------------------------*/
463: char *DocImageMimeType (Document doc)
464: {
465: char *mime_type;
466: LoadedImageDesc *pImage;
467: PicType type;
468: Element image;
469:
470: if (DocumentTypes[doc] != docImage)
471: return NULL;
472:
473: mime_type = NULL;
474: if (!IsHTTPPath (DocumentURLs[doc]))
475: {
476: /* it is a local image */
1.120 kahan 477: if (ImageElement (doc, NULL, &image))
1.117 kahan 478: {
479: type = TtaGetPictureType (image);
480: mime_type = PicTypeToMIME (type);
481: }
482: }
483: else
484: {
485: /* find the value of the src attribute */
486: pImage = ImageURLs;
487: while (pImage != NULL)
488: {
489: if (pImage->document == doc)
490: {
491: if (pImage->content_type)
492: mime_type = pImage->content_type;
493: else if (pImage->elImage && pImage->elImage->currentElement)
494: {
495: type = TtaGetPictureType (pImage->elImage->currentElement);
496: mime_type = PicTypeToMIME (type);
497: }
498: break;
499: }
500: }
501: }
502: return (mime_type);
503: }
504:
1.4 cvs 505: /*----------------------------------------------------------------------
1.9 cvs 506: IsHTMLName
507: returns TRUE if path points to an HTML resource.
1.4 cvs 508: ----------------------------------------------------------------------*/
1.109 cvs 509: ThotBool IsHTMLName (const char *path)
1.106 cvs 510: {
1.183 quint 511: char *temppath;
512: char *suffix;
1.136 cvs 513: char nsuffix[MAX_LENGTH];
514: int i;
1.5 cvs 515:
1.101 cvs 516: if (!path)
517: return (FALSE);
1.5 cvs 518:
1.183 quint 519: temppath = TtaStrdup ((char *)path);
520: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 521: TtaExtractSuffix (temppath, suffix);
1.101 cvs 522: i = 0;
1.106 cvs 523: while (suffix[i] != EOS)
1.101 cvs 524: {
525: /* Normalize the suffix */
526: i = 0;
1.106 cvs 527: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 528: {
1.123 vatton 529: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 530: i++;
531: }
1.106 cvs 532: nsuffix[i] = EOS;
533: if (!strcmp (nsuffix, "html") ||
534: !strcmp (nsuffix, "htm") ||
535: !strcmp (nsuffix, "shtml") ||
536: !strcmp (nsuffix, "jsp") ||
1.159 vatton 537: !strcmp (nsuffix, "tpl") ||
1.106 cvs 538: !strcmp (nsuffix, "xht") ||
539: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 540: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 541: !strcmp (nsuffix, "xhtml"))
1.183 quint 542: {
543: TtaFreeMemory (temppath);
544: TtaFreeMemory (suffix);
545: return (TRUE);
546: }
1.106 cvs 547: else if (!strcmp (nsuffix, "gz"))
1.101 cvs 548: {
549: /* take into account compressed files */
1.124 vatton 550: TtaExtractSuffix (temppath, suffix);
1.101 cvs 551: /* Normalize the suffix */
552: i = 0;
1.106 cvs 553: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 554: {
1.123 vatton 555: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 556: i++;
557: }
1.106 cvs 558: nsuffix[i] = EOS;
1.183 quint 559: TtaFreeMemory (temppath);
560: TtaFreeMemory (suffix);
1.106 cvs 561: if (!strcmp (nsuffix, "html") ||
562: !strcmp (nsuffix, "htm") ||
563: !strcmp (nsuffix, "shtml") ||
564: !strcmp (nsuffix, "jsp") ||
1.159 vatton 565: !strcmp (nsuffix, "tpl") ||
1.106 cvs 566: !strcmp (nsuffix, "xht") ||
567: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 568: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 569: !strcmp (nsuffix, "xhtml"))
1.101 cvs 570: return (TRUE);
571: else
572: return (FALSE);
573: }
574: else
575: /* check if there is another suffix */
1.124 vatton 576: TtaExtractSuffix (temppath, suffix);
1.101 cvs 577: }
1.183 quint 578: TtaFreeMemory (temppath);
579: TtaFreeMemory (suffix);
580: return (FALSE);
1.3 cvs 581: }
582:
1.4 cvs 583: /*----------------------------------------------------------------------
1.136 cvs 584: IsMathMLName
585: returns TRUE if path points to an MathML resource.
1.56 cvs 586: ----------------------------------------------------------------------*/
1.136 cvs 587: ThotBool IsMathMLName (const char *path)
1.56 cvs 588: {
1.183 quint 589: char *temppath;
590: char *suffix;
591: ThotBool ret;
1.56 cvs 592:
593: if (!path)
1.183 quint 594: return (FALSE);
1.56 cvs 595:
1.183 quint 596: temppath = TtaStrdup ((char *)path);
597: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 598: TtaExtractSuffix (temppath, suffix);
1.56 cvs 599:
1.136 cvs 600: if (!strcasecmp (suffix, "mml"))
1.183 quint 601: ret = TRUE;
1.106 cvs 602: else if (!strcmp (suffix, "gz"))
1.56 cvs 603: {
604: /* take into account compressed files */
1.124 vatton 605: TtaExtractSuffix (temppath, suffix);
1.136 cvs 606: if (!strcasecmp (suffix, "mml"))
1.183 quint 607: ret = TRUE;
1.60 cvs 608: else
1.183 quint 609: ret = FALSE;
1.60 cvs 610: }
611: else
1.183 quint 612: ret = FALSE;
613:
614: TtaFreeMemory (temppath);
615: TtaFreeMemory (suffix);
616: return (ret);
1.60 cvs 617: }
618:
619: /*----------------------------------------------------------------------
1.136 cvs 620: IsSVGName
621: returns TRUE if path points to an SVG resource.
1.133 vatton 622: ----------------------------------------------------------------------*/
1.136 cvs 623: ThotBool IsSVGName (const char *path)
1.133 vatton 624: {
1.183 quint 625: char *temppath;
626: char *suffix;
627: ThotBool ret;
1.133 vatton 628:
629: if (!path)
630: return (FALSE);
631:
1.183 quint 632: temppath = TtaStrdup ((char *)path);
633: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.133 vatton 634: TtaExtractSuffix (temppath, suffix);
635:
1.158 vatton 636: if (!strcasecmp (suffix, "svg") || !strcasecmp (suffix, "svgz"))
1.183 quint 637: ret = TRUE;
1.133 vatton 638: else if (!strcmp (suffix, "gz"))
639: {
640: /* take into account compressed files */
641: TtaExtractSuffix (temppath, suffix);
1.136 cvs 642: if (!strcasecmp (suffix, "svg"))
1.183 quint 643: ret = TRUE;
1.133 vatton 644: else
1.183 quint 645: ret = FALSE;
1.133 vatton 646: }
647: else
1.183 quint 648: ret = FALSE;
649:
650: TtaFreeMemory (temppath);
651: TtaFreeMemory (suffix);
652: return (ret);
1.133 vatton 653: }
654:
655: /*----------------------------------------------------------------------
1.136 cvs 656: IsXMLName
657: returns TRUE if path points to an XML resource.
1.103 cvs 658: ----------------------------------------------------------------------*/
1.136 cvs 659: ThotBool IsXMLName (const char *path)
1.103 cvs 660: {
1.183 quint 661: char *temppath;
662: char *suffix;
663: ThotBool ret;
1.103 cvs 664:
665: if (!path)
666: return (FALSE);
667:
1.183 quint 668: temppath = TtaStrdup ((char *)path);
669: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 670: TtaExtractSuffix (temppath, suffix);
1.103 cvs 671:
1.136 cvs 672: if (!strcasecmp (suffix, "xml") ||
673: !strcasecmp (suffix, "xht") ||
674: !strcmp (suffix, "xhtm") ||
1.145 kahan 675: !strcmp (suffix, "xhtml") ||
1.182 vatton 676: !strcmp (suffix, "smi") ||
677: !strcmp (suffix, "zsl"))
1.183 quint 678: ret = TRUE;
1.106 cvs 679: else if (!strcmp (suffix, "gz"))
1.103 cvs 680: {
681: /* take into account compressed files */
1.124 vatton 682: TtaExtractSuffix (temppath, suffix);
1.136 cvs 683: if (!strcasecmp (suffix, "xml") ||
684: !strcasecmp (suffix, "xht") ||
685: !strcmp (suffix, "xhtm") ||
1.145 kahan 686: !strcmp (suffix, "xhtml") ||
1.182 vatton 687: !strcmp (suffix, "smi") |
688: !strcmp (suffix, "xsl"))
1.183 quint 689: ret = TRUE;
1.103 cvs 690: else
1.183 quint 691: ret = FALSE;
1.103 cvs 692: }
693: else
1.183 quint 694: ret = FALSE;
695:
696: TtaFreeMemory (temppath);
697: TtaFreeMemory (suffix);
698: return (ret);
1.103 cvs 699: }
700:
701: /*----------------------------------------------------------------------
1.136 cvs 702: IsUndisplayedName
703: returns TRUE if path points to an undisplayed resource.
1.103 cvs 704: ----------------------------------------------------------------------*/
1.136 cvs 705: ThotBool IsUndisplayedName (const char *path)
1.103 cvs 706: {
1.183 quint 707: char *temppath;
708: char *suffix;
709: ThotBool ret;
1.103 cvs 710:
711: if (!path)
712: return (FALSE);
713:
1.183 quint 714: temppath = TtaStrdup ((char *)path);
715: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 716: TtaExtractSuffix (temppath, suffix);
1.103 cvs 717:
1.136 cvs 718: if (!strcasecmp (suffix, "exe") ||
719: !strcasecmp (suffix, "zip") ||
720: !strcasecmp (suffix, "ppt") ||
721: !strcasecmp (suffix, "pdf") ||
722: !strcasecmp (suffix, "ps") ||
723: !strcasecmp (suffix, "eps") ||
724: !strcasecmp (suffix, "tar") ||
725: !strcasecmp (suffix, "tgz") ||
726: !strcasecmp (suffix, "ddl") ||
1.186 ! quint 727: !strcasecmp (suffix, "ddl") ||
! 728: !strcasecmp (suffix, "mpg") ||
! 729: !strcasecmp (suffix, "mpeg") ||
! 730: !strcasecmp (suffix, "wmv") ||
! 731: !strcasecmp (suffix, "wma") ||
1.136 cvs 732: !strcasecmp (suffix, "o"))
1.183 quint 733: ret = TRUE;
1.106 cvs 734: else if (!strcmp (suffix, "gz"))
1.103 cvs 735: {
736: /* take into account compressed files */
1.124 vatton 737: TtaExtractSuffix (temppath, suffix);
1.136 cvs 738: if (!strcasecmp (suffix, "exe") ||
739: !strcasecmp (suffix, "zip") ||
740: !strcasecmp (suffix, "ppt") ||
741: !strcasecmp (suffix, "pdf") ||
742: !strcasecmp (suffix, "ps") ||
743: !strcasecmp (suffix, "eps") ||
744: !strcasecmp (suffix, "tar") ||
745: !strcasecmp (suffix, "ddl") ||
1.186 ! quint 746: !strcasecmp (suffix, "mpg") ||
! 747: !strcasecmp (suffix, "mpeg") ||
! 748: !strcasecmp (suffix, "wmv") ||
! 749: !strcasecmp (suffix, "wma") ||
1.136 cvs 750: !strcasecmp (suffix, "o"))
1.183 quint 751: ret = TRUE;
1.103 cvs 752: else
1.183 quint 753: ret = FALSE;
1.103 cvs 754: }
755: else
1.183 quint 756: ret = FALSE;
757:
758: TtaFreeMemory (temppath);
759: TtaFreeMemory (suffix);
760: return (ret);
1.103 cvs 761: }
762:
763: /*----------------------------------------------------------------------
1.60 cvs 764: IsCSSName
765: returns TRUE if path points to an XML resource.
766: ----------------------------------------------------------------------*/
1.111 cvs 767: ThotBool IsCSSName (const char *path)
1.60 cvs 768: {
1.183 quint 769: char *temppath;
770: char *suffix;
771: ThotBool ret;
1.60 cvs 772:
773: if (!path)
1.183 quint 774: return (FALSE);
1.60 cvs 775:
1.183 quint 776: temppath = TtaStrdup ((char *)path);
777: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 778: TtaExtractSuffix (temppath, suffix);
1.60 cvs 779:
1.106 cvs 780: if (!strcasecmp (suffix, "css"))
1.183 quint 781: ret = TRUE;
1.106 cvs 782: else if (!strcmp (suffix, "gz"))
1.60 cvs 783: {
784: /* take into account compressed files */
1.124 vatton 785: TtaExtractSuffix (temppath, suffix);
1.106 cvs 786: if (!strcasecmp (suffix, "css"))
1.183 quint 787: ret = TRUE;
1.56 cvs 788: else
1.183 quint 789: ret = FALSE;
1.56 cvs 790: }
791: else
1.183 quint 792: ret = FALSE;
793:
794: TtaFreeMemory (temppath);
795: TtaFreeMemory (suffix);
796: return (ret);
1.56 cvs 797: }
798:
799: /*----------------------------------------------------------------------
1.177 kahan 800: MultipleBookmarks
801: returns the value of the multiple bookmarks environment variable.
802: (TRUE if enabled).
803: ----------------------------------------------------------------------*/
804: ThotBool MultipleBookmarks (void)
805: {
806: ThotBool multiple_bm;
807:
1.180 kahan 808: TtaGetEnvBoolean ("DISABLE_MULTIPLE_BM", &multiple_bm);
1.177 kahan 809:
1.180 kahan 810: return (multiple_bm == FALSE);
1.177 kahan 811: }
812:
813: /*----------------------------------------------------------------------
1.172 kahan 814: IsRDFName
815: returns TRUE if path points to an RDF resource.
816: ----------------------------------------------------------------------*/
817: ThotBool IsRDFName (const char *path)
818: {
1.183 quint 819: char *temppath;
820: char *suffix;
821: ThotBool ret;
1.172 kahan 822:
1.177 kahan 823: /* temporarily disabling this function */
824: if (!MultipleBookmarks ())
1.183 quint 825: return (FALSE);
1.177 kahan 826:
1.172 kahan 827: if (!path)
1.183 quint 828: return (FALSE);
1.172 kahan 829:
1.183 quint 830: temppath = TtaStrdup ((char *)path);
831: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.172 kahan 832: TtaExtractSuffix (temppath, suffix);
833:
834: if (!strcasecmp (suffix, "rdf"))
1.183 quint 835: ret = TRUE;
1.172 kahan 836: else if (!strcmp (suffix, "gz"))
837: {
838: /* take into account compressed files */
839: TtaExtractSuffix (temppath, suffix);
840: if (!strcasecmp (suffix, "rdf"))
1.183 quint 841: ret = TRUE;
1.172 kahan 842: else
1.183 quint 843: ret = FALSE;
1.172 kahan 844: }
845: else
1.183 quint 846: ret = FALSE;
847:
848: TtaFreeMemory (temppath);
849: TtaFreeMemory (suffix);
850: return (ret);
1.172 kahan 851: }
852:
853: /*----------------------------------------------------------------------
1.9 cvs 854: IsImageName
855: returns TRUE if path points to an image resource.
1.4 cvs 856: ----------------------------------------------------------------------*/
1.111 cvs 857: ThotBool IsImageName (const char *path)
1.106 cvs 858: {
1.183 quint 859: char *temppath;
860: char *suffix;
1.106 cvs 861: char nsuffix[MAX_LENGTH];
1.5 cvs 862: int i;
1.183 quint 863: ThotBool ret;
1.5 cvs 864:
865: if (!path)
1.13 cvs 866: return (FALSE);
1.5 cvs 867:
1.183 quint 868: temppath = TtaStrdup ((char *)path);
869: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 870: TtaExtractSuffix (temppath, suffix);
1.5 cvs 871:
872: /* Normalize the suffix */
873: i = 0;
1.106 cvs 874: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 875: {
1.123 vatton 876: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 877: i++;
878: }
1.106 cvs 879: nsuffix[i] = EOS;
880: if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
881: (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
882: (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
1.183 quint 883: ret = TRUE;
884: else
885: ret = FALSE;
886:
887: TtaFreeMemory (temppath);
888: TtaFreeMemory (suffix);
889: return (ret);
1.3 cvs 890: }
891:
1.4 cvs 892: /*----------------------------------------------------------------------
1.58 cvs 893: IsImageType
894: returns TRUE if type points to an image resource.
895: ----------------------------------------------------------------------*/
1.111 cvs 896: ThotBool IsImageType (const char *type)
1.58 cvs 897: {
1.183 quint 898: char *temptype;
1.58 cvs 899: int i;
1.183 quint 900: ThotBool ret;
1.58 cvs 901:
902: if (!type)
903: return (FALSE);
904:
1.183 quint 905: temptype = TtaStrdup ((char *)type);
1.58 cvs 906: /* Normalize the type */
907: i = 0;
1.106 cvs 908: while (temptype[i] != EOS)
1.58 cvs 909: {
910: temptype[i] = tolower (temptype[i]);
911: i++;
912: }
1.166 vatton 913: if (!strncmp (temptype, "image/", sizeof ("image/") - 1))
914: i = sizeof ("image/") - 1;
915: else
916: i = 0;
917: if (!strcmp (&temptype[i], "gif") || !strcmp (&temptype[i], "x-xbitmap") ||
918: !strcmp (&temptype[i], "x-xpixmap") || !strcmp (&temptype[i], "jpeg") ||
919: !strcmp (&temptype[i], "png"))
1.183 quint 920: ret = TRUE;
921: else
922: ret = FALSE;
923: TtaFreeMemory (temptype);
924: return (ret);
1.58 cvs 925: }
926:
927: /*----------------------------------------------------------------------
1.9 cvs 928: IsTextName
1.4 cvs 929: ----------------------------------------------------------------------*/
1.111 cvs 930: ThotBool IsTextName (const char *path)
1.106 cvs 931: {
1.183 quint 932: char *temppath;
933: char *suffix;
1.106 cvs 934: char nsuffix[MAX_LENGTH];
1.5 cvs 935: int i;
1.183 quint 936: ThotBool ret;
1.5 cvs 937:
938: if (!path)
1.13 cvs 939: return (FALSE);
1.5 cvs 940:
1.183 quint 941: temppath = TtaStrdup ((char *)path);
942: suffix = (char *)TtaGetMemory (strlen (path) + 1);
1.124 vatton 943: TtaExtractSuffix (temppath, suffix);
1.5 cvs 944:
945: /* Normalize the suffix */
946: i = 0;
1.106 cvs 947: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5 cvs 948: {
1.25 cvs 949: nsuffix[i] = tolower (suffix[i]);
1.5 cvs 950: i++;
951: }
1.106 cvs 952: nsuffix[i] = EOS;
1.5 cvs 953:
1.111 cvs 954: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.183 quint 955: ret = TRUE;
1.106 cvs 956: else if (!strcmp (nsuffix, "gz"))
1.13 cvs 957: {
1.39 cvs 958: /* take into account compressed files */
1.124 vatton 959: TtaExtractSuffix (temppath, suffix);
1.13 cvs 960: /* Normalize the suffix */
961: i = 0;
1.106 cvs 962: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 963: {
1.25 cvs 964: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 965: i++;
966: }
1.106 cvs 967: nsuffix[i] = EOS;
1.111 cvs 968: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.183 quint 969: ret = TRUE;
1.13 cvs 970: else
1.183 quint 971: ret = FALSE;
1.13 cvs 972: }
973: else
1.183 quint 974: ret = FALSE;
975:
976: TtaFreeMemory (temppath);
977: TtaFreeMemory (suffix);
978: return (ret);
1.3 cvs 979: }
980:
1.4 cvs 981: /*----------------------------------------------------------------------
1.9 cvs 982: IsHTTPPath
983: returns TRUE if path is in fact an http URL.
1.4 cvs 984: ----------------------------------------------------------------------*/
1.112 cvs 985: ThotBool IsHTTPPath (const char *path)
1.3 cvs 986: {
1.5 cvs 987: if (!path)
988: return FALSE;
1.3 cvs 989:
1.106 cvs 990: if ((!strncmp (path, "http:", 5) != 0)
991: || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
992: || !strncmp (path, "internal:", 9))
1.58 cvs 993: return TRUE;
994: return FALSE;
1.3 cvs 995: }
996:
1.4 cvs 997: /*----------------------------------------------------------------------
1.9 cvs 998: IsWithParameters
999: returns TRUE if url has a concatenated query string.
1.4 cvs 1000: ----------------------------------------------------------------------*/
1.133 vatton 1001: ThotBool IsWithParameters (const char *url)
1.3 cvs 1002: {
1.5 cvs 1003: int i;
1.3 cvs 1004:
1.9 cvs 1005: if ((!url) || (url[0] == EOS))
1.5 cvs 1006: return FALSE;
1.3 cvs 1007:
1.9 cvs 1008: i = strlen (url) - 1;
1009: while (i > 0 && url[i--] != '?')
1.5 cvs 1010: if (i < 0)
1011: return FALSE;
1.3 cvs 1012:
1.5 cvs 1013: /* There is a parameter */
1014: return TRUE;
1.3 cvs 1015: }
1016:
1.4 cvs 1017: /*----------------------------------------------------------------------
1.9 cvs 1018: IsW3Path
1019: returns TRUE if path is in fact a URL.
1.4 cvs 1020: ----------------------------------------------------------------------*/
1.133 vatton 1021: ThotBool IsW3Path (const char *path)
1.106 cvs 1022: {
1.170 quint 1023: if (path == NULL)
1024: return FALSE;
1.106 cvs 1025: if (strncmp (path, "http:", 5) &&
1026: strncmp (path, "ftp:", 4) &&
1027: strncmp (path, "telnet:", 7) &&
1028: strncmp (path, "wais:", 5) &&
1029: strncmp (path, "news:", 5) &&
1030: strncmp (path, "gopher:", 7) &&
1031: strncmp (path, "mailto:", 7) &&
1.132 cheyroul 1032: strncmp (path, "archie:", 7) &&
1033: strncmp (path, "https:", 6))
1.72 cvs 1034: return FALSE;
1035: return TRUE;
1.3 cvs 1036: }
1037:
1.4 cvs 1038: /*----------------------------------------------------------------------
1.90 cvs 1039: IsFilePath
1040: returns TRUE if path is in fact a URL.
1041: ----------------------------------------------------------------------*/
1.133 vatton 1042: ThotBool IsFilePath (const char *path)
1.90 cvs 1043: {
1.106 cvs 1044: if (strncmp (path, "file:", 5))
1.90 cvs 1045: return FALSE;
1046: return TRUE;
1047: }
1048:
1049: /*----------------------------------------------------------------------
1.9 cvs 1050: IsValidProtocol
1051: returns true if the url protocol is supported by Amaya.
1.4 cvs 1052: ----------------------------------------------------------------------*/
1.133 vatton 1053: ThotBool IsValidProtocol (const char *url)
1.106 cvs 1054: {
1055: if (!strncmp (url, "http:", 5)
1056: || !strncmp (url, "internal:", 9)
1057: || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22 cvs 1058: /* experimental */
1.24 cvs 1059: /*** || !strncmp (path, "news:", 5)***/
1.8 cvs 1060: return (TRUE);
1.5 cvs 1061: else
1.8 cvs 1062: return (FALSE);
1.3 cvs 1063: }
1064:
1.31 cvs 1065:
1066: /*----------------------------------------------------------------------
1067: GetBaseURL
1068: normalizes orgName according to a base associated with doc, and
1069: following the standard URL format rules.
1070: The function returns the base used to solve relative URL and SRC:
1071: - the base of the document,
1072: - or the document path (without document name).
1073: ----------------------------------------------------------------------*/
1.106 cvs 1074: char *GetBaseURL (Document doc)
1.31 cvs 1075: {
1076: Element el;
1077: ElementType elType;
1078: AttributeType attrType;
1079: Attribute attr;
1.176 vatton 1080: char *ptr, *basename, *utf8path;
1.31 cvs 1081: int length;
1.151 kahan 1082: ThotBool hasDocBase;
1.31 cvs 1083:
1.113 cvs 1084: if (doc == 0 || !DocumentURLs[doc])
1.110 cvs 1085: return NULL;
1.148 kahan 1086: /* the other functions expect basename to have no more than MAX_LENGTH chars */
1.171 gully 1087: basename = (char *)TtaGetMemory (MAX_LENGTH);
1.148 kahan 1088: basename[0] = EOS;
1.31 cvs 1089: length = MAX_LENGTH -1;
1.151 kahan 1090: hasDocBase = FALSE;
1091:
1092: /* If the document has a base URL, it has a priority over the headers. */
1093: /* @@ We need to do this too when we support XML:base */
1094:
1095: /* is it a HTML document ? */
1096: elType.ElSSchema = TtaGetDocumentSSchema (doc);
1097: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
1098: /* it's a HTML document */
1099: {
1100: /* get the document element */
1101: el = TtaGetMainRoot (doc);
1102: /* search the BASE element */
1103: elType.ElTypeNum = HTML_EL_HEAD;
1104: el = TtaSearchTypedElement (elType, SearchForward, el);
1105: if (el)
1106: /* there is a HEAD element */
1107: {
1108: /* look for a BASE element within the HEAD */
1109: elType.ElTypeNum = HTML_EL_BASE;
1110: el = TtaSearchTypedElement (elType, SearchInTree, el);
1111: }
1112: if (el)
1113: {
1114: /* The document has a BASE element. Get the HREF attribute of the
1115: BASE element */
1116: hasDocBase = TRUE;
1117: attrType.AttrSSchema = elType.ElSSchema;
1118: attrType.AttrTypeNum = HTML_ATTR_HREF_;
1119: attr = TtaGetAttribute (el, attrType);
1120: if (attr)
1121: {
1122: /* Use the base path of the document */
1123: TtaGiveTextAttributeValue (attr, basename, &length);
1.176 vatton 1124: utf8path = basename;
1125: basename = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
1126: TtaGetDefaultCharset ());
1127: TtaFreeMemory (utf8path);
1.151 kahan 1128: }
1129: }
1130: }
1131:
1132: /* there was no BASE. Do we have a location header? */
1133: if (!hasDocBase && DocumentMeta[doc] && DocumentMeta[doc]->full_content_location
1.148 kahan 1134: && DocumentMeta[doc]->full_content_location[0] != EOS)
1.65 cvs 1135: {
1.148 kahan 1136: strncpy (basename, DocumentMeta[doc]->full_content_location, MAX_LENGTH-1);
1137: basename[MAX_LENGTH-1] = EOS;
1138: length = strlen (basename);
1139: }
1140:
1141: if (basename[0] != EOS)
1142: {
1143: /* base and orgName have to be separated by a DIR_SEP */
1144: length--;
1145: if (basename[0] != EOS && basename[length] != URL_SEP &&
1146: basename[length] != DIR_SEP)
1147: /* verify if the base has the form "protocol://server:port" */
1.31 cvs 1148: {
1.148 kahan 1149: ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
1150: AMAYA_PARSE_HOST |
1151: AMAYA_PARSE_PUNCTUATION);
1152: if (ptr && !strcmp (ptr, basename))
1.31 cvs 1153: {
1.148 kahan 1154: /* it has this form, complete it by adding a URL_STR */
1155: if (strchr (basename, DIR_SEP))
1156: strcat (basename, DIR_STR);
1157: else
1158: strcat (basename, URL_STR);
1159: length++;
1.31 cvs 1160: }
1.149 kahan 1161: else if (!ptr || ptr[0] == EOS)
1162: {
1163: /* no host was detected, we may have a relative URL. We test
1164: if it begins with a URL_SEP, DIR_SEP or period. If yes, it's
1165: relative. */
1166: if (! (basename[0] == '.' || basename[0] == URL_SEP
1167: || basename[0] == DIR_SEP))
1168: basename[0] = EOS;
1169: }
1.148 kahan 1170: if (ptr)
1171: TtaFreeMemory (ptr);
1.31 cvs 1172: }
1.113 cvs 1173: }
1174:
1.148 kahan 1175: /* there was no base element and no location header, we use the DocumentURL */
1176: if (basename[0] == EOS)
1177: {
1178: strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
1179: basename[MAX_LENGTH-1] = EOS;
1180: }
1181:
1.31 cvs 1182: /* Remove anything after the last DIR_SEP char. If no such char is found,
1183: * then search for the first ":" char, hoping that what's before that is a
1184: * protocol. If found, end the string there. If neither char is found,
1185: * then discard the whole base element.
1186: */
1.106 cvs 1187: length = strlen (basename) - 1;
1.31 cvs 1188: /* search for the last DIR_SEP char */
1.106 cvs 1189: while (length >= 0 && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31 cvs 1190: length--;
1191: if (length >= 0)
1192: /* found the last DIR_SEP char, end the string there */
1.106 cvs 1193: basename[length + 1] = EOS;
1.31 cvs 1194: else
1195: /* search for the first PATH_STR char */
1196: {
1.106 cvs 1197: for (length = 0; basename[length] != ':' &&
1198: basename[length] != EOS; length ++);
1199: if (basename[length] == ':')
1.31 cvs 1200: /* found, so end the string there */
1.106 cvs 1201: basename[length + 1] = EOS;
1.31 cvs 1202: else
1203: /* not found, discard the base */
1.106 cvs 1204: basename[0] = EOS;
1.31 cvs 1205: }
1206: return (basename);
1207: }
1208:
1209:
1.4 cvs 1210: /*----------------------------------------------------------------------
1.40 cvs 1211: GetLocalPath
1212: Allocate and return the local document path associated to the url
1213: ----------------------------------------------------------------------*/
1.150 vatton 1214: char *GetLocalPath (Document doc, char *url)
1.106 cvs 1215: {
1216: char *ptr;
1217: char *n;
1218: char *documentname;
1219: char url_sep;
1.83 cvs 1220: int len;
1.67 cvs 1221: ThotBool noFile;
1.40 cvs 1222:
1.153 vatton 1223: if (url)
1.40 cvs 1224: {
1225: /* check whether the file name exists */
1.106 cvs 1226: len = strlen (url) - 1;
1.71 cvs 1227: if (IsW3Path (url))
1.157 kahan 1228: url_sep = URL_SEP;
1.41 cvs 1229: else
1.106 cvs 1230: url_sep = DIR_SEP;
1.41 cvs 1231: noFile = (url[len] == url_sep);
1.40 cvs 1232: if (noFile)
1.106 cvs 1233: url[len] = EOS;
1.171 gully 1234: ptr = (char *)TtaGetMemory (MAX_LENGTH);
1235: documentname = (char *)TtaGetMemory (MAX_LENGTH);
1.78 cvs 1236: TtaExtractName (url, ptr, documentname);
1.106 cvs 1237: sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40 cvs 1238: if (!TtaCheckDirectory (ptr))
1239: /* directory did not exist */
1.72 cvs 1240: TtaMakeDirectory (ptr);
1.47 cvs 1241:
1.153 vatton 1242: if (doc == 0)
1243: {
1244: n = strrchr (documentname, '.');
1245: if (n)
1246: *n = EOS;
1247: if (documentname[0] == EOS)
1248: strcpy (documentname, "noname");
1249: n = GetTempName (ptr, documentname);
1250: TtaFreeMemory (ptr);
1251: ptr = n;
1252: }
1.69 cvs 1253: else
1.153 vatton 1254: {
1255: /* don't include the query string within document name */
1256: n = strrchr (documentname, '?');
1257: if (n)
1258: *n = EOS;
1259: /* don't include ':' within document name */
1260: n = strchr (documentname, ':');
1261: if (n)
1262: *n = EOS;
1263: /* if after all this operations document name
1264: is empty, let's use noname.html instead */
1265: if (documentname[0] == EOS)
1266: strcat (ptr, "noname.html");
1267: else
1268: strcat (ptr, documentname);
1269: }
1.40 cvs 1270: TtaFreeMemory (documentname);
1.157 kahan 1271: /* substitute invalid chars in file names by a _ */
1272: n = ptr;
1273: while (*n)
1274: {
1275: if (*n == '*' || *n == ',')
1276: *n = '_';
1277: n++;
1278: }
1.40 cvs 1279: /* restore the url */
1280: if (noFile)
1.41 cvs 1281: url[len] = url_sep;
1.40 cvs 1282: return (ptr);
1283: }
1284: else
1285: return (NULL);
1286: }
1287:
1.73 cvs 1288: /*----------------------------------------------------------------------
1.79 cvs 1289: ExtractTarget extract the target name from document nane.
1290: ----------------------------------------------------------------------*/
1.150 vatton 1291: void ExtractTarget (char *aName, char *target)
1.79 cvs 1292: {
1.106 cvs 1293: int lg, i;
1294: char *ptr;
1295: char *oldptr;
1.79 cvs 1296:
1297: if (!target || !aName)
1298: /* bad target */
1299: return;
1300:
1.106 cvs 1301: target[0] = EOS;
1302: lg = strlen (aName);
1.79 cvs 1303: if (lg)
1304: {
1305: /* the name is not empty */
1306: oldptr = ptr = &aName[0];
1307: do
1308: {
1.106 cvs 1309: ptr = strrchr (oldptr, '#');
1.79 cvs 1310: if (ptr)
1311: oldptr = &ptr[1];
1312: }
1313: while (ptr);
1314:
1315: i = (int) (oldptr) - (int) (aName); /* name length */
1316: if (i > 1)
1317: {
1.106 cvs 1318: aName[i - 1] = EOS;
1.79 cvs 1319: if (i != lg)
1.106 cvs 1320: strcpy (target, oldptr);
1.79 cvs 1321: }
1322: }
1323: }
1324:
1325: /*----------------------------------------------------------------------
1.90 cvs 1326: RemoveNewLines (text)
1327: Removes any '\n' chars that are found in text.
1328: Returns TRUE if it did the operation, FALSE otherwise.
1.73 cvs 1329: ----------------------------------------------------------------------*/
1.106 cvs 1330: ThotBool RemoveNewLines (char *text)
1331: {
1332: ThotBool change = FALSE;
1333: char *src;
1334: char *dest;
1.90 cvs 1335:
1336: src = text;
1337: dest = text;
1.115 kahan 1338:
1339: /* remove any preceding whitespace */
1340: while (*src && *src == ' ')
1341: {
1342: src++;
1343: change = 1;
1344: }
1345:
1.90 cvs 1346: while (*src)
1347: {
1348: switch (*src)
1349: {
1.106 cvs 1350: case '\n':
1.90 cvs 1351: /* don't copy the newline */
1352: change = 1;
1353: break;
1354: default:
1355: *dest = *src;
1356: dest++;
1357: break;
1358: }
1359: src++;
1360: }
1361: /* copy the last EOS char */
1362: *dest = *src;
1363:
1364: return (change);
1365: }
1366:
1367: /*----------------------------------------------------------------------
1368: CleanCopyFileURL
1369: Copies a file url from a src string to destination string.
1.97 cvs 1370: convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
1371: we want to do).
1.90 cvs 1372: ----------------------------------------------------------------------*/
1.106 cvs 1373: static void CleanCopyFileURL (char *dest, char *src,
1374: ConvertionType convertion)
1.90 cvs 1375: {
1376: while (*src)
1.89 cvs 1377: {
1.90 cvs 1378: switch (*src)
1.89 cvs 1379: {
1.184 gully 1380: #ifdef _WINDOWS
1.106 cvs 1381: case URL_SEP:
1.96 cvs 1382: /* make DIR_SEP transformation */
1.97 cvs 1383: if (convertion & AM_CONV_URL_SEP)
1.106 cvs 1384: *dest = DIR_SEP;
1.96 cvs 1385: else
1386: *dest = *src;
1.90 cvs 1387: dest++;
1.96 cvs 1388: src++;
1.90 cvs 1389: break;
1.184 gully 1390: #endif /* _WINDOWS */
1.96 cvs 1391:
1.106 cvs 1392: case '%':
1.97 cvs 1393: if (convertion & AM_CONV_PERCENT)
1.96 cvs 1394: {
1.97 cvs 1395: /* (code adapted from libwww's HTUnEscape function */
1.96 cvs 1396: src++;
1.106 cvs 1397: if (*src != EOS)
1.97 cvs 1398: {
1399: *dest = UnEscapeChar (*src) * 16;
1400: src++;
1401: }
1.106 cvs 1402: if (*src != EOS)
1.97 cvs 1403: {
1404: *dest = *dest + UnEscapeChar (*src);
1405: src++;
1406: }
1407: dest++;
1.96 cvs 1408: }
1.97 cvs 1409: else
1.96 cvs 1410: {
1.97 cvs 1411: *dest = *src;
1412: dest++;
1.96 cvs 1413: src++;
1414: }
1415: break;
1416:
1.90 cvs 1417: default:
1418: *dest = *src;
1.89 cvs 1419: dest++;
1.96 cvs 1420: src++;
1.90 cvs 1421: break;
1.89 cvs 1422: }
1423: }
1.90 cvs 1424: /* copy the EOS char */
1425: *dest = *src;
1.73 cvs 1426: }
1.40 cvs 1427:
1428: /*----------------------------------------------------------------------
1.9 cvs 1429: NormalizeURL
1430: normalizes orgName according to a base associated with doc, and
1431: following the standard URL format rules.
1.113 cvs 1432: if doc is < 0, use as a base the URL of the document that contains
1433: (or contained) the elements that are now in the copy/cut buffer.
1.53 cvs 1434: if doc is 0 and otherPath not NULL, normalizes orgName according to this
1435: other path.
1.9 cvs 1436: The function returns the new complete and normalized URL
1.12 cvs 1437: or file name path (newName) and the name of the document (docName).
1.9 cvs 1438: N.B. If the function can't find out what's the docName, it assigns
1439: the name "noname.html".
1.4 cvs 1440: ----------------------------------------------------------------------*/
1.106 cvs 1441: void NormalizeURL (char *orgName, Document doc, char *newName,
1442: char *docName, char *otherPath)
1443: {
1444: char *basename;
1445: char tempOrgName[MAX_LENGTH];
1446: char *ptr;
1447: char used_sep;
1.84 cvs 1448: int length;
1449: ThotBool check;
1.5 cvs 1450:
1.184 gully 1451: #ifdef _WINDOWS
1.44 cvs 1452: int ndx;
1.184 gully 1453: #endif /* _WINDOWS */
1.44 cvs 1454:
1.5 cvs 1455: if (!newName || !docName)
1456: return;
1.18 cvs 1457:
1.113 cvs 1458: if (doc < 0)
1459: basename = TtaStrdup (SavedDocumentURL);
1460: else if (doc > 0)
1.53 cvs 1461: basename = GetBaseURL (doc);
1462: else if (otherPath != NULL)
1.108 cvs 1463: basename = TtaStrdup (otherPath);
1.32 cvs 1464: else
1.53 cvs 1465: basename = NULL;
1.32 cvs 1466:
1.18 cvs 1467: /*
1.31 cvs 1468: * Clean orgName
1469: * Make sure we have a complete orgName, without any leading or trailing
1470: * white spaces, or trailinbg new lines
1471: */
1.5 cvs 1472: ptr = orgName;
1.18 cvs 1473: /* skip leading white space and new line characters */
1.106 cvs 1474: while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
1475: strncpy (tempOrgName, ptr, MAX_LENGTH -1);
1476: tempOrgName[MAX_LENGTH -1] = EOS;
1.18 cvs 1477: /*
1.31 cvs 1478: * Make orgName a complete URL
1479: * If the URL does not include a protocol, then try to calculate
1480: * one using the doc's base element (if it exists),
1481: */
1.106 cvs 1482: if (tempOrgName[0] == EOS)
1.53 cvs 1483: {
1.106 cvs 1484: newName[0] = EOS;
1485: docName[0] = EOS;
1.53 cvs 1486: TtaFreeMemory (basename);
1487: return;
1488: }
1.49 cvs 1489:
1490: /* clean trailing white space */
1.106 cvs 1491: length = strlen (tempOrgName) - 1;
1492: while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53 cvs 1493: {
1.106 cvs 1494: tempOrgName[length] = EOS;
1.53 cvs 1495: length--;
1496: }
1.50 cvs 1497:
1.55 cvs 1498: /* remove extra dot (which dot???) */
1499: /* ugly, but faster than a strcmp */
1.106 cvs 1500: if (tempOrgName[length] == '.'
1501: && (length == 0 || tempOrgName[length-1] != '.'))
1502: tempOrgName[length] = EOS;
1.50 cvs 1503:
1.94 cvs 1504: if (IsW3Path (tempOrgName))
1.53 cvs 1505: {
1506: /* the name is complete, go to the Sixth Step */
1.106 cvs 1507: strcpy (newName, tempOrgName);
1.53 cvs 1508: SimplifyUrl (&newName);
1509: /* verify if the URL has the form "protocol://server:port" */
1.110 cvs 1510: ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
1511: AMAYA_PARSE_HOST |
1512: AMAYA_PARSE_PUNCTUATION);
1513: if (ptr && !strcmp (ptr, newName))
1514: /* it has this form, we complete it by adding a DIR_STR */
1.106 cvs 1515: strcat (newName, URL_STR);
1.49 cvs 1516:
1.53 cvs 1517: if (ptr)
1.50 cvs 1518: TtaFreeMemory (ptr);
1.53 cvs 1519: }
1.113 cvs 1520: else if (basename == NULL)
1.53 cvs 1521: /* the name is complete, go to the Sixth Step */
1.106 cvs 1522: strcpy (newName, tempOrgName);
1.53 cvs 1523: else
1524: {
1.31 cvs 1525: /* Calculate the absolute URL, using the base or document URL */
1.184 gully 1526: #ifdef _WINDOWS
1.53 cvs 1527: if (!IsW3Path (basename))
1528: {
1.106 cvs 1529: length = strlen (tempOrgName);
1.53 cvs 1530: for (ndx = 0; ndx < length; ndx++)
1.106 cvs 1531: if (tempOrgName [ndx] == '/')
1532: tempOrgName [ndx] = '\\';
1.53 cvs 1533: }
1.184 gully 1534: #endif /* _WINDOWS */
1.25 cvs 1535: ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53 cvs 1536: if (ptr)
1537: {
1538: SimplifyUrl (&ptr);
1.106 cvs 1539: strcpy (newName, ptr);
1.53 cvs 1540: TtaFreeMemory (ptr);
1541: }
1542: else
1.106 cvs 1543: newName[0] = EOS;
1.53 cvs 1544: }
1.36 cvs 1545:
1546: TtaFreeMemory (basename);
1.18 cvs 1547: /*
1.31 cvs 1548: * Prepare the docname that will refer to this ressource in the
1549: * .amaya directory. If the new URL finishes on DIR_SEP, then use
1550: * noname.html as a default ressource name
1.18 cvs 1551: */
1.106 cvs 1552: if (newName[0] != EOS)
1.53 cvs 1553: {
1.106 cvs 1554: length = strlen (newName) - 1;
1555: if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53 cvs 1556: {
1557: used_sep = newName[length];
1558: check = TRUE;
1559: while (check)
1560: {
1.50 cvs 1561: length--;
1562: while (length >= 0 && newName[length] != used_sep)
1.53 cvs 1563: length--;
1.106 cvs 1564: if (!strncmp (&newName[length+1], "..", 2))
1.53 cvs 1565: {
1.106 cvs 1566: newName[length+1] = EOS;
1.53 cvs 1567: /* remove also previous directory */
1568: length--;
1569: while (length >= 0 && newName[length] != used_sep)
1570: length--;
1.106 cvs 1571: if (strncmp (&newName[length+1], "//", 2))
1.131 cheyroul 1572: /* don't remove server name */
1.106 cvs 1573: newName[length+1] = EOS;
1.53 cvs 1574: }
1.106 cvs 1575: else if (!strncmp (&newName[length+1], ".", 1))
1576: newName[length+1] = EOS;
1.50 cvs 1577: else
1.53 cvs 1578: check = FALSE;
1579: }
1580: /* docname was not comprised inside the URL, so let's */
1581: /* assign the default ressource name */
1.106 cvs 1582: strcpy (docName, "noname.html");
1.53 cvs 1583: }
1584: else
1585: { /* docname is comprised inside the URL */
1.110 cvs 1586: while (length >= 0 && newName[length] != URL_SEP &&
1587: newName[length] != DIR_SEP)
1.53 cvs 1588: length--;
1589: if (length < 0)
1.106 cvs 1590: strcpy (docName, newName);
1.53 cvs 1591: else
1.106 cvs 1592: strcpy (docName, &newName[length+1]);
1.53 cvs 1593: }
1594: }
1595: else
1.106 cvs 1596: docName[0] = EOS;
1.18 cvs 1597: }
1.3 cvs 1598:
1.4 cvs 1599: /*----------------------------------------------------------------------
1.9 cvs 1600: IsSameHost
1.4 cvs 1601: ----------------------------------------------------------------------*/
1.106 cvs 1602: ThotBool IsSameHost (const char *url1, const char *url2)
1.3 cvs 1603: {
1.106 cvs 1604: char *basename_ptr1, *basename_ptr2;
1605: ThotBool result;
1.3 cvs 1606:
1.106 cvs 1607: basename_ptr1 = AmayaParseUrl (url1, "",
1608: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1609: basename_ptr2 = AmayaParseUrl (url2, "",
1610: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3 cvs 1611:
1.106 cvs 1612: if (strcmp (basename_ptr1, basename_ptr2))
1613: result = FALSE;
1614: else
1615: result = TRUE;
1616: TtaFreeMemory (basename_ptr1);
1617: TtaFreeMemory (basename_ptr2);
1618: return (result);
1.3 cvs 1619: }
1620:
1621:
1.4 cvs 1622: /*----------------------------------------------------------------------
1.22 cvs 1623: HasKnownFileSuffix
1624: returns TRUE if path points to a file ending with a suffix.
1625: ----------------------------------------------------------------------*/
1.153 vatton 1626: ThotBool HasKnownFileSuffix (const char *path)
1.106 cvs 1627: {
1628: char *root;
1629: char temppath[MAX_LENGTH];
1630: char suffix[MAX_LENGTH];
1.22 cvs 1631:
1.106 cvs 1632: if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22 cvs 1633: return (FALSE);
1634:
1.106 cvs 1635: root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22 cvs 1636:
1637: if (root)
1638: {
1.106 cvs 1639: strcpy (temppath, root);
1.25 cvs 1640: TtaFreeMemory (root);
1.22 cvs 1641: /* Get the suffix */
1.124 vatton 1642: TtaExtractSuffix (temppath, suffix);
1.22 cvs 1643:
1.106 cvs 1644: if( suffix[0] == EOS)
1.22 cvs 1645: /* no suffix */
1646: return (FALSE);
1647:
1648: /* Normalize the suffix */
1649: ConvertToLowerCase (suffix);
1650:
1.106 cvs 1651: if (!strcmp (suffix, "gz"))
1.22 cvs 1652: /* skip the compressed suffix */
1653: {
1.124 vatton 1654: TtaExtractSuffix (temppath, suffix);
1.106 cvs 1655: if(suffix[0] == EOS)
1.22 cvs 1656: /* no suffix */
1657: return (FALSE);
1658: /* Normalize the suffix */
1659: ConvertToLowerCase (suffix);
1660: }
1661:
1.106 cvs 1662: if (strcmp (suffix, "gif") &&
1663: strcmp (suffix, "xbm") &&
1664: strcmp (suffix, "xpm") &&
1665: strcmp (suffix, "jpg") &&
1666: strcmp (suffix, "pdf") &&
1667: strcmp (suffix, "png") &&
1668: strcmp (suffix, "tgz") &&
1669: strcmp (suffix, "xpg") &&
1670: strcmp (suffix, "xpd") &&
1671: strcmp (suffix, "ps") &&
1672: strcmp (suffix, "au") &&
1673: strcmp (suffix, "html") &&
1674: strcmp (suffix, "htm") &&
1675: strcmp (suffix, "shtml") &&
1676: strcmp (suffix, "xht") &&
1677: strcmp (suffix, "xhtm") &&
1678: strcmp (suffix, "xhtml") &&
1679: strcmp (suffix, "txt") &&
1680: strcmp (suffix, "css") &&
1681: strcmp (suffix, "eps"))
1.22 cvs 1682: return (FALSE);
1683: else
1684: return (TRUE);
1685: }
1686: else
1687: return (FALSE);
1688: }
1689:
1690:
1691: /*----------------------------------------------------------------------
1.24 cvs 1692: ChopURL
1693: Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL).
1694: If inputURL is bigger than that size, outputURL receives
1695: MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...",
1696: and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
1697: If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
1698: copied into outputURL.
1699: N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
1700: chars.
1701: ----------------------------------------------------------------------*/
1.106 cvs 1702: void ChopURL (char *outputURL, const char *inputURL)
1.24 cvs 1703: {
1704: int len;
1.9 cvs 1705:
1.106 cvs 1706: len = strlen (inputURL);
1.24 cvs 1707: if (len <= MAX_PRINT_URL_LENGTH)
1.106 cvs 1708: strcpy (outputURL, inputURL);
1.24 cvs 1709: else
1710: /* make a truncated urlName on the status window */
1711: {
1.106 cvs 1712: strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
1713: outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
1714: strcat (outputURL, "...");
1715: strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24 cvs 1716: }
1.25 cvs 1717: }
1718:
1719:
1720: /*----------------------------------------------------------------------
1721: scan
1.47 cvs 1722: Scan a filename for its constituents
1.25 cvs 1723: -----------------------------------
1724:
1725: On entry,
1726: name points to a document name which may be incomplete.
1727: On exit,
1728: absolute or relative may be nonzero (but not both).
1729: host, fragment and access may be nonzero if they were specified.
1730: Any which are nonzero point to zero terminated strings.
1731: ----------------------------------------------------------------------*/
1.106 cvs 1732: static void scan (char *name, HTURI *parts)
1.25 cvs 1733: {
1.106 cvs 1734: char * p;
1735: char * after_access = name;
1.32 cvs 1736:
1.43 cvs 1737: memset (parts, '\0', sizeof (HTURI));
1.28 cvs 1738: /* Look for fragment identifier */
1.106 cvs 1739: if ((p = strchr(name, '#')) != NULL)
1.28 cvs 1740: {
1.106 cvs 1741: *p++ = '\0';
1.28 cvs 1742: parts->fragment = p;
1.25 cvs 1743: }
1744:
1.28 cvs 1745: for (p=name; *p; p++)
1746: {
1.106 cvs 1747: if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28 cvs 1748: break;
1.106 cvs 1749: if (*p == ':')
1.28 cvs 1750: {
1751: *p = 0;
1752: parts->access = after_access; /* Scheme has been specified */
1753:
1754: /* The combination of gcc, the "-O" flag and the HP platform is
1755: unhealthy. The following three lines is a quick & dirty fix, but is
1756: not recommended. Rather, turn off "-O". */
1757:
1758: /* after_access = p;*/
1759: /* while (*after_access == 0)*/
1760: /* after_access++;*/
1761: after_access = p+1;
1.106 cvs 1762: if (!strcasecmp("URL", parts->access))
1.28 cvs 1763: /* Ignore IETF's URL: pre-prefix */
1764: parts->access = NULL;
1765: else
1.25 cvs 1766: break;
1767: }
1768: }
1769:
1770: p = after_access;
1.43 cvs 1771: if (*p == URL_SEP || *p == DIR_SEP)
1.28 cvs 1772: {
1.43 cvs 1773: if (p[1] == URL_SEP)
1.28 cvs 1774: {
1.25 cvs 1775: parts->host = p+2; /* host has been specified */
1.28 cvs 1776: *p = 0; /* Terminate access */
1777: /* look for end of host name if any */
1.106 cvs 1778: p = strchr (parts->host, URL_SEP);
1.28 cvs 1779: if (p)
1780: {
1.106 cvs 1781: *p = EOS; /* Terminate host */
1.25 cvs 1782: parts->absolute = p+1; /* Root has been found */
1.28 cvs 1783: }
1784: }
1785: else
1786: /* Root found but no host */
1787: parts->absolute = p+1;
1788: }
1789: else
1790: {
1.25 cvs 1791: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28 cvs 1792: }
1.25 cvs 1793: }
1794:
1795:
1796: /*----------------------------------------------------------------------
1.28 cvs 1797: AmayaParseUrl: parse a Name relative to another name
1798:
1799: This returns those parts of a name which are given (and requested)
1800: substituting bits from the related name where necessary.
1.25 cvs 1801:
1.28 cvs 1802: On entry,
1.25 cvs 1803: aName A filename given
1804: relatedName A name relative to which aName is to be parsed. Give
1805: it an empty string if aName is absolute.
1806: wanted A mask for the bits which are wanted.
1807:
1.28 cvs 1808: On exit,
1.25 cvs 1809: returns A pointer to a malloc'd string which MUST BE FREED
1810: ----------------------------------------------------------------------*/
1.106 cvs 1811: char *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1812: {
1813: char *return_value;
1814: char result[MAX_LENGTH];
1815: char name[MAX_LENGTH];
1816: char rel[MAX_LENGTH];
1817: char *p, *access;
1.29 cvs 1818: HTURI given, related;
1819: int len;
1.106 cvs 1820: char used_sep;
1821: char *used_str;
1.32 cvs 1822:
1.106 cvs 1823: if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33 cvs 1824: {
1.106 cvs 1825: used_str = DIR_STR;
1826: used_sep = DIR_SEP;
1.33 cvs 1827: }
1.32 cvs 1828: else
1.33 cvs 1829: {
1.106 cvs 1830: used_str = URL_STR;
1831: used_sep = URL_SEP;
1.33 cvs 1832: }
1.32 cvs 1833:
1.29 cvs 1834: /* Make working copies of input strings to cut up: */
1835: return_value = NULL;
1836: result[0] = 0; /* Clear string */
1.169 quint 1837: rel[0] = EOS;
1838: strncpy (name, aName, MAX_LENGTH - 1);
1839: name[MAX_LENGTH - 1] = EOS;
1840: if (relatedName != NULL)
1841: {
1842: strncpy (rel, relatedName, MAX_LENGTH - 1);
1843: rel[MAX_LENGTH - 1] = EOS;
1844: }
1.29 cvs 1845: else
1.106 cvs 1846: relatedName[0] = EOS;
1.29 cvs 1847:
1848: scan (name, &given);
1849: scan (rel, &related);
1850: access = given.access ? given.access : related.access;
1851: if (wanted & AMAYA_PARSE_ACCESS)
1852: if (access)
1853: {
1.106 cvs 1854: strcat (result, access);
1.29 cvs 1855: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1856: strcat (result, ":");
1.29 cvs 1857: }
1858:
1859: if (given.access && related.access)
1860: /* If different, inherit nothing. */
1.106 cvs 1861: if (strcmp (given.access, related.access) != 0)
1.29 cvs 1862: {
1863: related.host = 0;
1864: related.absolute = 0;
1865: related.relative = 0;
1866: related.fragment = 0;
1867: }
1868:
1869: if (wanted & AMAYA_PARSE_HOST)
1870: if(given.host || related.host)
1871: {
1872: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1873: strcat (result, "//");
1874: strcat (result, given.host ? given.host : related.host);
1.29 cvs 1875: }
1876:
1877: if (given.host && related.host)
1878: /* If different hosts, inherit no path. */
1.106 cvs 1879: if (strcmp (given.host, related.host) != 0)
1.29 cvs 1880: {
1881: related.absolute = 0;
1882: related.relative = 0;
1883: related.fragment = 0;
1884: }
1885:
1886: if (wanted & AMAYA_PARSE_PATH)
1887: {
1888: if (given.absolute)
1889: {
1890: /* All is given */
1891: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1892: strcat (result, used_str);
1893: strcat (result, given.absolute);
1.25 cvs 1894: }
1.29 cvs 1895: else if (related.absolute)
1896: {
1897: /* Adopt path not name */
1.106 cvs 1898: strcat (result, used_str);
1899: strcat (result, related.absolute);
1.29 cvs 1900: if (given.relative)
1901: {
1902: /* Search part? */
1.106 cvs 1903: p = strchr (result, '?');
1.29 cvs 1904: if (!p)
1.106 cvs 1905: p=result+strlen(result)-1;
1.33 cvs 1906: for (; *p!=used_sep; p--); /* last / */
1.29 cvs 1907: /* Remove filename */
1908: p[1]=0;
1909: /* Add given one */
1.106 cvs 1910: strcat (result, given.relative);
1.25 cvs 1911: }
1912: }
1.29 cvs 1913: else if (given.relative)
1914: /* what we've got */
1.106 cvs 1915: strcat (result, given.relative);
1.29 cvs 1916: else if (related.relative)
1.106 cvs 1917: strcat (result, related.relative);
1.29 cvs 1918: else
1919: /* No inheritance */
1.106 cvs 1920: strcat (result, used_str);
1.25 cvs 1921: }
1.29 cvs 1922:
1923: if (wanted & AMAYA_PARSE_ANCHOR)
1924: if (given.fragment || related.fragment)
1925: {
1926: if (given.absolute && given.fragment)
1927: {
1928: /*Fixes for relURLs...*/
1929: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1930: strcat (result, "#");
1931: strcat (result, given.fragment);
1.29 cvs 1932: }
1933: else if (!(given.absolute) && !(given.fragment))
1.106 cvs 1934: strcat (result, "");
1.29 cvs 1935: else
1936: {
1.110 cvs 1937: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1938: strcat (result, "#");
1.110 cvs 1939: strcat (result, given.fragment ? given.fragment : related.fragment);
1.29 cvs 1940: }
1941: }
1.106 cvs 1942: len = strlen (result);
1.171 gully 1943: if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106 cvs 1944: strcpy (return_value, result);
1.29 cvs 1945: return (return_value); /* exactly the right length */
1.25 cvs 1946: }
1947:
1948: /*----------------------------------------------------------------------
1949: HTCanon
1950: Canonicalizes the URL in the following manner starting from the host
1951: pointer:
1952:
1953: 1) The host name is converted to lowercase
1954: 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
1955:
1956: Return: OK The position of the current path part of the URL
1957: which might be the old one or a new one.
1958:
1959: ----------------------------------------------------------------------*/
1.106 cvs 1960: static char *HTCanon (char **filename, char *host)
1961: {
1962: char *newname = NULL;
1963: char used_sep;
1964: char *path;
1965: char *strptr;
1966: char *port;
1967: char *access = host-3;
1968:
1969: if (*filename && strchr (*filename, URL_SEP))
1970: used_sep = URL_SEP;
1.33 cvs 1971: else
1.106 cvs 1972: used_sep = DIR_SEP;
1.32 cvs 1973:
1.110 cvs 1974: while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25 cvs 1975: access--;
1.110 cvs 1976: if ((path = strchr (host, used_sep)) == NULL) /* Find path */
1.106 cvs 1977: path = host + strlen (host);
1978: if ((strptr = strchr (host, '@')) != NULL && strptr < path) /* UserId */
1.82 cvs 1979: host = strptr;
1.110 cvs 1980: if ((port = strchr (host, ':')) != NULL && port > path) /* Port number */
1.82 cvs 1981: port = NULL;
1.25 cvs 1982:
1983: strptr = host; /* Convert to lower-case */
1.82 cvs 1984: while (strptr < path)
1.33 cvs 1985: {
1.123 vatton 1986: *strptr = tolower (*strptr);
1.82 cvs 1987: strptr++;
1.33 cvs 1988: }
1.25 cvs 1989:
1990: /* Does the URL contain a full domain name? This also works for a
1991: numerical host name. The domain name is already made lower-case
1992: and without a trailing dot. */
1993: {
1.106 cvs 1994: char *dot = port ? port : path;
1995: if (dot > *filename && *--dot == '.')
1.33 cvs 1996: {
1.106 cvs 1997: char *orig = dot;
1998: char *dest = dot + 1;
1.82 cvs 1999: while ((*orig++ = *dest++));
2000: if (port) port--;
1.33 cvs 2001: path--;
1.25 cvs 2002: }
2003: }
2004: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33 cvs 2005: if (port)
2006: {
1.82 cvs 2007: if (!*(port+1) || *(port+1) == used_sep)
1.33 cvs 2008: {
2009: if (!newname)
2010: {
1.106 cvs 2011: char *orig = port;
2012: char *dest = port + 1;
1.82 cvs 2013: while ((*orig++ = *dest++));
1.33 cvs 2014: }
2015: }
1.106 cvs 2016: else if ((!strncmp (access, "http", 4) &&
2017: (*(port + 1) == '8' &&
2018: *(port+2) == '0' &&
1.82 cvs 2019: (*(port+3) == used_sep || !*(port + 3)))) ||
1.106 cvs 2020: (!strncmp (access, "gopher", 6) &&
2021: (*(port+1) == '7' &&
2022: *(port+2) == '0' &&
1.82 cvs 2023: (*(port+3) == used_sep || !*(port+3)))) ||
1.106 cvs 2024: (!strncmp (access, "ftp", 3) &&
2025: (*(port+1) == '2' &&
2026: *(port + 2) == '1' &&
1.82 cvs 2027: (*(port+3) == used_sep || !*(port+3))))) {
1.33 cvs 2028: if (!newname)
2029: {
1.106 cvs 2030: char *orig = port;
2031: char *dest = port + 3;
1.33 cvs 2032: while((*orig++ = *dest++));
2033: /* Update path position, Henry Minsky */
2034: path -= 3;
1.25 cvs 2035: }
1.33 cvs 2036: }
2037: else if (newname)
1.106 cvs 2038: strncat (newname, port, (int) (path - port));
1.33 cvs 2039: }
1.25 cvs 2040:
1.33 cvs 2041: if (newname)
2042: {
1.106 cvs 2043: char *newpath = newname + strlen (newname);
2044: strcat (newname, path);
1.25 cvs 2045: path = newpath;
1.28 cvs 2046: /* Free old copy */
2047: TtaFreeMemory(*filename);
1.25 cvs 2048: *filename = newname;
1.33 cvs 2049: }
1.25 cvs 2050: return path;
2051: }
2052:
2053:
2054: /*----------------------------------------------------------------------
1.29 cvs 2055: SimplifyUrl: simplify a URI
1.32 cvs 2056: A URI is allowed to contain the sequence xxx/../ which may be
2057: replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28 cvs 2058: Simplification helps us recognize duplicate URIs.
1.25 cvs 2059:
1.28 cvs 2060: Thus, /etc/junk/../fred becomes /etc/fred
2061: /etc/junk/./fred becomes /etc/junk/fred
1.25 cvs 2062:
1.28 cvs 2063: but we should NOT change
2064: http://fred.xxx.edu/../..
1.25 cvs 2065:
2066: or ../../albert.html
2067:
1.28 cvs 2068: In order to avoid empty URLs the following URLs become:
1.25 cvs 2069:
2070: /fred/.. becomes /fred/..
2071: /fred/././.. becomes /fred/..
2072: /fred/.././junk/.././ becomes /fred/..
2073:
1.28 cvs 2074: If more than one set of `://' is found (several proxies in cascade) then
2075: only the part after the last `://' is simplified.
1.25 cvs 2076:
1.28 cvs 2077: Returns: A string which might be the old one or a new one.
1.25 cvs 2078: ----------------------------------------------------------------------*/
1.106 cvs 2079: void SimplifyUrl (char **url)
2080: {
2081: char *path;
2082: char *access;
2083: char *newptr;
2084: char *p;
2085: char *orig, *dest, *end;
1.28 cvs 2086:
1.106 cvs 2087: char used_sep;
1.77 cvs 2088: ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
2089: something/../ simplification in relative URLs when they start with a ../ */
1.32 cvs 2090:
1.28 cvs 2091: if (!url || !*url)
2092: return;
2093:
1.106 cvs 2094: if (strchr (*url, URL_SEP))
2095: used_sep = URL_SEP;
1.32 cvs 2096: else
1.106 cvs 2097: used_sep = DIR_SEP;
1.32 cvs 2098:
1.77 cvs 2099: /* should we simplify double dot? */
2100: path = *url;
1.106 cvs 2101: if (*path == '.' && *(path + 1) == '.')
1.77 cvs 2102: ddot_simplify = FALSE;
2103: else
2104: ddot_simplify = TRUE;
2105:
1.28 cvs 2106: /* Find any scheme name */
1.106 cvs 2107: if ((path = strstr (*url, "://")) != NULL)
1.33 cvs 2108: {
2109: /* Find host name */
1.28 cvs 2110: access = *url;
1.123 vatton 2111: while (access < path && (*access = tolower (*access)))
1.82 cvs 2112: access++;
1.28 cvs 2113: path += 3;
1.106 cvs 2114: while ((newptr = strstr (path, "://")) != NULL)
1.82 cvs 2115: /* For proxies */
1.106 cvs 2116: path = newptr + 3;
1.82 cvs 2117: /* We have a host name */
1.84 cvs 2118: path = HTCanon (url, path);
1.25 cvs 2119: }
1.106 cvs 2120: else if ((path = strstr (*url, ":/")) != NULL)
1.28 cvs 2121: path += 2;
2122: else
2123: path = *url;
1.84 cvs 2124: if (*path == used_sep && *(path+1) == used_sep)
1.28 cvs 2125: /* Some URLs start //<foo> */
2126: path += 1;
1.94 cvs 2127: else if (IsFilePath (path))
2128: {
2129: /* doesn't need to do anything more */
2130: return;
2131: }
1.106 cvs 2132: else if (!strncmp (path, "news:", 5))
1.28 cvs 2133: {
1.106 cvs 2134: newptr = strchr (path+5, '@');
1.28 cvs 2135: if (!newptr)
2136: newptr = path + 5;
2137: while (*newptr)
2138: {
2139: /* Make group or host lower case */
1.123 vatton 2140: *newptr = tolower (*newptr);
1.28 cvs 2141: newptr++;
1.25 cvs 2142: }
1.28 cvs 2143: /* Doesn't need to do any more */
2144: return;
1.25 cvs 2145: }
1.130 cheyroul 2146:
1.126 cheyroul 2147:
1.28 cvs 2148: if ((p = path))
2149: {
1.106 cvs 2150: if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
2151: (end = strchr (path, '#'))))
2152: end = path + strlen (path);
1.28 cvs 2153:
2154: /* Parse string second time to simplify */
2155: p = path;
2156: while (p < end)
2157: {
1.110 cvs 2158: /* if we're pointing to a char, it's safe to reactivate the
2159: ../ convertion */
1.106 cvs 2160: if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77 cvs 2161: ddot_simplify = TRUE;
2162:
1.33 cvs 2163: if (*p==used_sep)
1.28 cvs 2164: {
1.106 cvs 2165: if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28 cvs 2166: {
2167: orig = p + 1;
1.84 cvs 2168: dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52 cvs 2169: while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28 cvs 2170: end = orig - 1;
2171: }
1.106 cvs 2172: else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.'
1.77 cvs 2173: && (*(p+3) == used_sep || !*(p+3)))
1.28 cvs 2174: {
2175: newptr = p;
1.52 cvs 2176: while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
2177: if (*newptr == used_sep)
2178: orig = newptr + 1;
1.28 cvs 2179: else
1.52 cvs 2180: orig = newptr;
2181:
2182: dest = (*(p+3) != used_sep) ? p+3 : p+4;
2183: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
2184: end = orig-1;
2185: /* Start again with prev slash */
2186: p = newptr;
1.28 cvs 2187: }
1.33 cvs 2188: else if (*(p+1) == used_sep)
1.28 cvs 2189: {
1.33 cvs 2190: while (*(p+1) == used_sep)
1.28 cvs 2191: {
2192: orig = p;
2193: dest = p + 1;
2194: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2195: end = orig-1;
2196: }
2197: }
2198: else
1.25 cvs 2199: p++;
1.28 cvs 2200: }
2201: else
2202: p++;
1.25 cvs 2203: }
2204: }
1.51 cvs 2205: /*
2206: ** Check for host/../.. kind of things
2207: */
1.106 cvs 2208: if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.'
1.77 cvs 2209: && (!*(path+3) || *(path+3) == used_sep))
1.106 cvs 2210: *(path+1) = EOS;
1.28 cvs 2211: return;
2212: }
2213:
2214:
2215: /*----------------------------------------------------------------------
1.96 cvs 2216: NormalizeFile normalizes local names.
1.28 cvs 2217: Return TRUE if target and src differ.
2218: ----------------------------------------------------------------------*/
1.106 cvs 2219: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28 cvs 2220: {
1.184 gully 2221: #ifndef _WINDOWS
1.106 cvs 2222: char *s;
1.93 cvs 2223: int i;
1.184 gully 2224: #endif /* _WINDOWS */
1.82 cvs 2225: ThotBool change;
1.90 cvs 2226: int start_index; /* the first char that we'll copy */
1.28 cvs 2227:
1.54 cvs 2228: change = FALSE;
1.90 cvs 2229: start_index = 0;
2230:
1.106 cvs 2231: if (!src || src[0] == EOS)
1.96 cvs 2232: {
1.106 cvs 2233: target[0] = EOS;
1.96 cvs 2234: return FALSE;
2235: }
1.90 cvs 2236:
2237: /* @@ do I need file: or file:/ here? */
1.106 cvs 2238: if (strncmp (src, "file:", 5) == 0)
1.28 cvs 2239: {
1.90 cvs 2240: /* remove the prefix file: */
2241: start_index += 5;
2242:
2243: /* remove the localhost prefix */
1.106 cvs 2244: if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94 cvs 2245: start_index += 11;
2246:
2247: /* remove the first two slashes in / / /path */
2248: while (src[start_index] &&
1.106 cvs 2249: src[start_index] == '/'
2250: && src[start_index + 1] == '/')
1.94 cvs 2251: start_index++;
2252:
1.184 gully 2253: #ifdef _WINDOWS
1.94 cvs 2254: /* remove any extra slash before the drive name */
1.106 cvs 2255: if (src[start_index] == '/'
2256: &&src[start_index+2] == ':')
1.94 cvs 2257: start_index++;
1.184 gully 2258: #endif /* _WINDOWS */
1.90 cvs 2259:
1.106 cvs 2260: if (src[start_index] == EOS)
1.90 cvs 2261: /* if there's nothing afterwards, add a DIR_STR */
1.106 cvs 2262: strcpy (target, DIR_STR);
1.90 cvs 2263: else
1.97 cvs 2264: /* as we're inside a file: URL, we'll apply all the convertions
2265: we know */
2266: CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96 cvs 2267:
2268: change = TRUE;
2269: }
1.97 cvs 2270: else if (convertion != AM_CONV_NONE)
1.96 cvs 2271: {
2272: /* we are following a "local" relative link, we do all the
2273: convertions except for the HOME_DIR ~ one */
1.97 cvs 2274: CleanCopyFileURL (target, src, convertion);
1.28 cvs 2275: }
1.184 gully 2276: #ifndef _WINDOWS
1.106 cvs 2277: else if (src[0] == '~')
1.53 cvs 2278: {
1.96 cvs 2279: /* it must be a URL typed in a text input field */
2280: /* do the HOME_DIR ~ substitution */
1.82 cvs 2281: s = TtaGetEnvString ("HOME");
1.106 cvs 2282: strcpy (target, s);
1.90 cvs 2283: #if 0
1.96 cvs 2284: /* JK: invalidated this part of the code as it's simpler
2285: to add the DIR_SEP whenever we have something to add
2286: to the path rather than adding it systematically */
1.106 cvs 2287: if (src[1] != DIR_SEP)
2288: strcat (target, DIR_STR);
1.90 cvs 2289: #endif
1.106 cvs 2290: i = strlen (target);
2291: strcpy (&target[i], &src[1]);
1.54 cvs 2292: change = TRUE;
1.53 cvs 2293: }
1.184 gully 2294: #endif /* _WINDOWS */
1.28 cvs 2295: else
1.96 cvs 2296: /* leave it as it is */
1.106 cvs 2297: strcpy (target, src);
1.96 cvs 2298:
1.28 cvs 2299: /* remove /../ and /./ */
1.29 cvs 2300: SimplifyUrl (&target);
1.54 cvs 2301: if (!change)
1.106 cvs 2302: change = strcmp (src, target);
1.28 cvs 2303: return (change);
1.25 cvs 2304: }
2305:
1.28 cvs 2306:
1.25 cvs 2307: /*----------------------------------------------------------------------
1.31 cvs 2308: MakeRelativeURL: make relative name
1.25 cvs 2309:
1.28 cvs 2310: This function creates and returns a string which gives an expression of
2311: one address as related to another. Where there is no relation, an absolute
2312: address is retured.
1.25 cvs 2313:
1.28 cvs 2314: On entry,
1.25 cvs 2315: Both names must be absolute, fully qualified names of nodes
2316: (no fragment bits)
2317:
1.28 cvs 2318: On exit,
1.25 cvs 2319: The return result points to a newly allocated name which, if
2320: parsed by AmayaParseUrl relative to relatedName, will yield aName.
2321: The caller is responsible for freeing the resulting name later.
2322: ----------------------------------------------------------------------*/
1.106 cvs 2323: char *MakeRelativeURL (char *aName, char *relatedName)
2324: {
2325: char *return_value;
2326: char result[MAX_LENGTH];
2327: char *p;
2328: char *q;
2329: char *after_access;
2330: char *last_slash = NULL;
2331: int slashes, levels, len;
1.184 gully 2332: #ifdef _WINDOWS
1.44 cvs 2333: int ndx;
1.184 gully 2334: #endif /* _WINDOWS */
1.44 cvs 2335:
1.29 cvs 2336: if (aName == NULL || relatedName == NULL)
2337: return (NULL);
2338:
2339: slashes = 0;
2340: after_access = NULL;
2341: p = aName;
2342: q = relatedName;
1.147 vatton 2343: len = 0;
2344: for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27 cvs 2345: {
2346: /* Find extent of match */
1.106 cvs 2347: if (*p == ':')
1.146 cvs 2348: {
1.168 cvs 2349: after_access = p + 1;
1.184 gully 2350: #ifdef _WINDOWS
1.168 cvs 2351: if (len == 1)
2352: {
2353: /* it's a local Windows path like c:... */
2354: slashes+=2;
2355: }
1.184 gully 2356: #endif /* _WINDOWS */
1.168 cvs 2357: }
2358: if (*p == DIR_SEP)
2359: {
2360: /* memorize the last slash position and count them */
2361: last_slash = p;
1.147 vatton 2362: slashes++;
1.146 cvs 2363: }
1.25 cvs 2364: }
2365:
1.31 cvs 2366: /* q, p point to the first non-matching character or zero */
1.106 cvs 2367: if (*q == EOS)
1.31 cvs 2368: {
2369: /* New name is a subset of the related name */
2370: /* exactly the right length */
1.106 cvs 2371: len = strlen (p);
1.171 gully 2372: if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106 cvs 2373: strcpy (return_value, p);
1.31 cvs 2374: }
2375: else if ((slashes < 2 && after_access == NULL)
2376: || (slashes < 3 && after_access != NULL))
1.168 cvs 2377: {
1.31 cvs 2378: /* Two names whitout common path */
2379: /* exactly the right length */
1.106 cvs 2380: len = strlen (aName);
1.171 gully 2381: if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106 cvs 2382: strcpy (return_value, aName);
1.31 cvs 2383: }
2384: else
2385: {
2386: /* Some path in common */
1.106 cvs 2387: if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31 cvs 2388: /* just the same server */
1.106 cvs 2389: strcpy (result, last_slash);
1.31 cvs 2390: else
2391: {
2392: levels= 0;
1.106 cvs 2393: for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31 cvs 2394: if (*q == DIR_SEP)
2395: levels++;
2396:
1.106 cvs 2397: result[0] = EOS;
1.31 cvs 2398: for (;levels; levels--)
1.106 cvs 2399: strcat (result, "../");
2400: strcat (result, last_slash+1);
1.31 cvs 2401: }
1.52 cvs 2402:
2403: if (!*result)
1.106 cvs 2404: strcat (result, "./");
1.52 cvs 2405:
1.31 cvs 2406: /* exactly the right length */
1.106 cvs 2407: len = strlen (result);
1.171 gully 2408: if ((return_value = (char *)TtaGetMemory (len + 1)) != NULL)
1.106 cvs 2409: strcpy (return_value, result);
1.52 cvs 2410:
1.25 cvs 2411: }
1.184 gully 2412: #ifdef _WINDOWS
1.106 cvs 2413: len = strlen (return_value);
1.168 cvs 2414: for (ndx = 0; ndx < len; ndx ++)
1.106 cvs 2415: if (return_value[ndx] == '\\')
2416: return_value[ndx] = '/' ;
1.184 gully 2417: #endif /* _WINDOWS */
1.29 cvs 2418: return (return_value);
1.24 cvs 2419: }
1.35 cvs 2420:
1.104 kahan 2421: /*----------------------------------------------------------------------
2422: AM_GetFileSize
2423: Returns TRUE and the filesize in the 2nd parameter.
2424: Otherwise, in case of a system error, returns FALSE, with a
2425: filesize of 0L.
2426: ---------------------------------------------------------------------*/
1.106 cvs 2427: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104 kahan 2428: {
2429: if (!TtaFileExist (filename))
2430: return FALSE;
1.185 vatton 2431: *file_size = TtaGetFileSize (filename);
2432: return TRUE;
1.104 kahan 2433: }
1.139 kahan 2434:
2435: /*----------------------------------------------------------------------
2436: AM_UseXHTMLMimeType
2437: Returns TRUE if the user has configured Amaya to use this MIME type,
2438: FALSE otherwise.
2439: ---------------------------------------------------------------------*/
2440: ThotBool AM_UseXHTMLMimeType (void)
2441: {
2442: ThotBool xhtml_mimetype;
2443:
2444: /* does the user wants to use the new MIME type? */
2445: TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
2446:
2447: return (xhtml_mimetype);
1.152 kahan 2448: }
2449:
1.154 kahan 2450:
2451: /********************************************
2452: The following routines were adapted from the GNU libc functions
2453: for generating a tmpnam.
2454: *********************************************/
2455:
2456: /* These are the characters used in temporary filenames. */
2457: static const char letters[] =
2458: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2459:
2460: /* Generate a temporary file name based on TMPL. TMPL must match the
2461: rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
2462: does not exist at the time of the call to __gen_tempname. TMPL is
2463: overwritten with the result.
2464:
2465: We use a clever algorithm to get hard-to-predict names. */
2466: void
2467: AM_gen_tempname (char *tmpl)
2468: {
2469: int len;
2470: char *XXXXXX;
2471: static uint64_t value;
2472: uint64_t random_time_bits;
2473: unsigned int count;
2474: int save_errno = errno;
2475: struct stat st;
2476:
2477: /* A lower bound on the number of temporary files to attempt to
2478: generate. The maximum total number of temporary file names that
2479: can exist for a given template is 62**6. It should never be
2480: necessary to try all these combinations. Instead if a reasonable
2481: number of names is tried (we define reasonable as 62**3) fail to
2482: give the system administrator the chance to remove the problems. */
2483: unsigned int attempts_min = 62 * 62 * 62;
2484:
2485: /* The number of times to attempt to generate a temporary file. To
2486: conform to POSIX, this must be no smaller than TMP_MAX. */
2487: unsigned int attempts = attempts_min < TMP_MAX ? TMP_MAX : attempts_min;
2488:
2489: len = strlen (tmpl);
2490: if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
2491: {
2492: /* @@ JK ? */
2493: errno = EINVAL;
2494: return;
2495: }
2496:
2497: /* This is where the Xs start. */
2498: XXXXXX = &tmpl[len - 6];
2499:
2500: /* Get some more or less random data. */
2501: #ifdef RANDOM_BITS
2502: RANDOM_BITS (random_time_bits);
2503: #else
2504: # if HAVE_GETTIMEOFDAY || _LIBC
2505: {
2506: struct timeval tv;
2507: gettimeofday (&tv, NULL);
2508: random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
2509: }
2510: # else
2511: random_time_bits = time (NULL);
2512: # endif
2513: #endif
2514: value += random_time_bits ^ getpid ();
2515:
2516: for (count = 0; count < attempts; value += 7777, ++count)
2517: {
2518: uint64_t v = value;
2519:
2520: /* Fill in the random bits. */
2521: XXXXXX[0] = letters[v % 62];
2522: v /= 62;
2523: XXXXXX[1] = letters[v % 62];
2524: v /= 62;
2525: XXXXXX[2] = letters[v % 62];
2526: v /= 62;
2527: XXXXXX[3] = letters[v % 62];
2528: v /= 62;
2529: XXXXXX[4] = letters[v % 62];
2530: v /= 62;
2531: XXXXXX[5] = letters[v % 62];
2532:
2533: /* This case is backward from the other three. AM_gen_tempname
2534: succeeds if __xstat fails because the name does not exist.
2535: Note the continue to bypass the common logic at the bottom
2536: of the loop. */
2537: if (stat (tmpl, &st) < 0)
2538: break;
2539:
2540: continue;
2541: }
2542:
2543: if (count == attempts || errno != ENOENT)
2544: tmpl[0] = EOS;
2545: else
2546: errno = save_errno;
2547:
2548: return;
2549: }
2550:
2551: #define JOSE 1
2552:
1.152 kahan 2553: /*-----------------------------------------------------------------------
2554: GetTempName
2555: Front end to the Unix tempnam function, which is independent of the
2556: value of the TMPDIR env value
2557: Returns a dynamically allocated string with a tempname. The user
2558: must free this memory.
2559: -----------------------------------------------------------------------*/
2560: char *GetTempName (const char *dir, const char *prefix)
2561: {
1.154 kahan 2562: #ifdef JOSE
2563:
1.162 kahan 2564: static char tmpbufmem[PATH_MAX + 1];
1.154 kahan 2565: int len;
2566: int i;
2567:
1.155 cvs 2568: if (!dir || *dir == EOS || !TtaDirExists (dir))
1.154 kahan 2569: return NULL;
2570:
1.162 kahan 2571: /* make sure that the name is no bigger than PATH_MAX + the 6 tempname chars we
2572: will add */
1.154 kahan 2573:
1.156 cvs 2574: len = strlen (dir);
1.162 kahan 2575: if (len + 6 > PATH_MAX)
1.154 kahan 2576: return NULL;
2577:
2578: /* copy the dir name, and add a DIR_SEP if it's missing */
2579: if (dir[strlen (dir) - 1] == DIR_SEP)
2580: strcpy (tmpbufmem, dir);
2581: else
1.156 cvs 2582: {
1.154 kahan 2583: sprintf (tmpbufmem, "%s%c", dir, DIR_SEP);
1.156 cvs 2584: len++;
2585: }
1.154 kahan 2586:
1.161 kahan 2587: /* copy the prefix (no more than L_tmpnam chars, to respect POSIX). Save
1.156 cvs 2588: space for the 6 X and EOS chars that will become the random bits */
2589: if (prefix)
2590: {
2591: i = 0;
1.167 cheyroul 2592: while (prefix[i] != EOS && i < L_tmpnam - 8)
1.156 cvs 2593: tmpbufmem[len++] = prefix[i++];
2594: tmpbufmem[len] = EOS;
2595: }
2596:
2597: /* Add the 6 X chars */
2598: len = strlen (tmpbufmem);
2599: i = 0;
2600: while (i < 6)
2601: {
2602: tmpbufmem[len++] = 'X';
2603: i++;
2604: }
2605: tmpbufmem[len] = EOS;
1.154 kahan 2606:
2607: AM_gen_tempname (tmpbufmem);
2608:
2609: if (tmpbufmem[0] == EOS)
2610: return NULL;
2611: else
2612: return (TtaStrdup (tmpbufmem));
2613:
2614: #else
1.152 kahan 2615: char *tmpdir;
1.181 vatton 2616: char *tmp = NULL;
1.152 kahan 2617: char *name = NULL;
2618:
2619: /* save the value of TMPDIR */
1.181 vatton 2620: tmpdir = getenv ("TMPDIR");
1.152 kahan 2621: if (tmpdir)
2622: {
1.181 vatton 2623: /* remove TMPDIR from the environment */
2624: tmp = TtaGetMemory (strlen (tmpdir) + 20);
2625: sprintf (tmp, "TMPDIR=");
1.184 gully 2626: #ifdef _WINDOWS
1.152 kahan 2627: _putenv (tmp);
2628: #else
2629: putenv (tmp);
1.184 gully 2630: #endif /* _WINDOWS */
1.181 vatton 2631: /* prepare the string to restore the value of TMPDIR */
2632: strrcat (tmp, tmpdir);
1.152 kahan 2633: }
2634:
2635: /* create the tempname */
1.184 gully 2636: #ifdef _WINDOWS
1.152 kahan 2637: /* Under Windows, _tempnam returns the same name until the file is created */
2638: {
2639: char *altprefix;
2640: name = tmpnam (NULL); /* get a possibly unique string */
1.181 vatton 2641: altprefix = TtaGetMemory (strlen (prefix) + strlen(name) + 1);
1.152 kahan 2642: sprintf (altprefix, "%s%s", prefix, name + strlen(_P_tmpdir));
2643: name = _tempnam (dir, altprefix); /* get a name that isn't yet in use */
2644: TtaFreeMemory (altprefix);
2645: }
2646: #else
2647: name = tempnam (dir, prefix);
1.184 gully 2648: #endif /* _WINDOWS */
1.152 kahan 2649:
2650: if (tmpdir)
2651: {
1.181 vatton 2652: /* restore the value of TMPDIR */
1.184 gully 2653: #ifdef _WINDOWS
1.152 kahan 2654: _putenv (tmpdir);
2655: #else
2656: putenv (tmpdir);
1.184 gully 2657: #endif /* _WINDOWS */
1.181 vatton 2658: TtaFreeMemory (tmpdir);
1.152 kahan 2659: }
2660: return (name);
1.154 kahan 2661: #endif
1.139 kahan 2662: }
Webmaster