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