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