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