[322] | 1 | //
|
---|
| 2 | // ICeCoffEEParser.m
|
---|
| 3 | // ICeCoffEE
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on 6/21/07.
|
---|
| 6 | // Copyright 2007 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "ICeCoffEEParser.h"
|
---|
| 10 | #import "ICeCoffEE.h"
|
---|
| 11 |
|
---|
| 12 | void ICCF_Delimiters(NSCharacterSet **leftPtr, NSCharacterSet **rightPtr) {
|
---|
| 13 | static NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
|
---|
| 14 |
|
---|
| 15 | if (urlLeftDelimiters == nil || urlRightDelimiters == nil) {
|
---|
| 16 | NSMutableCharacterSet *set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
|
---|
| 17 | NSMutableCharacterSet *tmpSet;
|
---|
| 18 | [urlLeftDelimiters release];
|
---|
| 19 | [urlRightDelimiters release];
|
---|
| 20 |
|
---|
| 21 | [set autorelease];
|
---|
| 22 | [set formUnionWithCharacterSet: [[NSCharacterSet characterSetWithRange: NSMakeRange(0x21, 0x5e)] invertedSet]]; // nonprintable and non-ASCII characters
|
---|
| 23 | [set formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
|
---|
| 24 | // XXX obsoleted by RFC 3986 now... use §2.1, 2.2, 2.3
|
---|
[383] | 25 | [set removeCharactersInString: @";/?:@&=+$,-_.!~*'(){}[]%#"]; // RFC 2396 §2.2, 2.3, 2.4, plus % and # from "delims" set and {}, []
|
---|
[322] | 26 |
|
---|
| 27 | tmpSet = [[set mutableCopy] autorelease];
|
---|
[384] | 28 | [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @"<(["]];
|
---|
[322] | 29 | urlLeftDelimiters = [tmpSet copy]; // make immutable again - for efficiency
|
---|
| 30 |
|
---|
| 31 | tmpSet = [[set mutableCopy] autorelease];
|
---|
[384] | 32 | [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @">)]"]];
|
---|
[322] | 33 | urlRightDelimiters = [tmpSet copy]; // make immutable again - for efficiency
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | *leftPtr = urlLeftDelimiters; *rightPtr = urlRightDelimiters;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | static ICInstance ICCF_icInst = NULL;
|
---|
| 40 |
|
---|
| 41 | void ICCF_StartIC() {
|
---|
| 42 | OSStatus err;
|
---|
| 43 |
|
---|
| 44 | if (ICCF_icInst != NULL) {
|
---|
| 45 | ICLog(@"ICCF_StartIC: Internet Config is already running!");
|
---|
| 46 | ICCF_StopIC();
|
---|
| 47 | }
|
---|
| 48 | err = ICStart(&ICCF_icInst, kICCFCreator);
|
---|
| 49 | NSCAssert1(err == noErr, ICCF_LocalizedString(@"Unable to start Internet Config (error %d)"), err);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void ICCF_StopIC() {
|
---|
| 53 | if (ICCF_icInst == NULL) {
|
---|
| 54 | ICLog(@"ICCF_StopIC: Internet Config is not running!");
|
---|
| 55 | } else {
|
---|
| 56 | ICStop(ICCF_icInst);
|
---|
| 57 | ICCF_icInst = NULL;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | ICInstance ICCF_GetInst() {
|
---|
| 62 | NSCAssert(ICCF_icInst != NULL, @"Internal error: Called ICCF_GetInst without ICCF_StartIC");
|
---|
| 63 | return ICCF_icInst;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // input/output 'range' is the range of source document which contains 'string'
|
---|
| 67 | void ICCF_ParseURL(NSString *string, NSRange *range) {
|
---|
| 68 | OSStatus err;
|
---|
| 69 | Handle h;
|
---|
| 70 | long selStart = 0, selEnd = range->length; // local offsets within 'string'
|
---|
| 71 | char *urlData = NULL;
|
---|
| 72 |
|
---|
| 73 | NSCAssert(selEnd == [string length], @"Internal error: URL string is wrong length");
|
---|
| 74 |
|
---|
| 75 | NS_DURING
|
---|
| 76 | if ([[NSCharacterSet characterSetWithCharactersInString: @";,."] characterIsMember:
|
---|
| 77 | [string characterAtIndex: selEnd - 1]]) {
|
---|
| 78 | selEnd--;
|
---|
| 79 | }
|
---|
| 80 | NSCharacterSet *alphanumericCharacterSet = [NSCharacterSet alphanumericCharacterSet];
|
---|
| 81 | while (![alphanumericCharacterSet characterIsMember: [string characterAtIndex: selStart]]) {
|
---|
| 82 | selStart++;
|
---|
| 83 | NSCAssert(selStart < selEnd, @"No URL is selected");
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | string = [string substringWithRange: NSMakeRange(selStart, selEnd - selStart)];
|
---|
| 87 |
|
---|
| 88 | ICLog(@"Parsing URL |%@|", string);
|
---|
| 89 |
|
---|
| 90 | NSCAssert([string canBeConvertedToEncoding: NSASCIIStringEncoding], @"No URL is selected");
|
---|
| 91 |
|
---|
| 92 | urlData = (char *)malloc( (range->length + 1) * sizeof(char));
|
---|
| 93 | NSCAssert(urlData != NULL, @"Internal error: can't allocate memory for URL string");
|
---|
| 94 |
|
---|
| 95 | // XXX getCString: is deprecated in 10.4, but this is safe and shouldn't assert because we've already verified the string can be converted to ASCII, which should be a subset of any possible system encoding. The replacement (getCString:maxLength:encoding:) is not available until 10.4, so we leave this until we dump Internet Config and gain IDN friendliness.
|
---|
| 96 | [string getCString: urlData];
|
---|
| 97 |
|
---|
| 98 | h = NewHandle(0);
|
---|
| 99 | NSCAssert(h != NULL, @"Internal error: can't allocate URL handle");
|
---|
| 100 |
|
---|
| 101 | err = ICParseURL(ICCF_GetInst(), "\pmailto", urlData, range->length, &selStart, &selEnd, h);
|
---|
| 102 | DisposeHandle(h);
|
---|
| 103 |
|
---|
| 104 | ICCF_OSErrCAssert(err, @"ICParseURL");
|
---|
| 105 |
|
---|
| 106 | range->length = selEnd - selStart;
|
---|
| 107 | range->location += selStart;
|
---|
| 108 | NS_HANDLER
|
---|
| 109 | free(urlData);
|
---|
| 110 | [localException raise];
|
---|
| 111 | NS_ENDHANDLER
|
---|
| 112 |
|
---|
| 113 | free(urlData);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[383] | 116 | static BOOL ICCF_StringIncludesCharacter(NSString *s, unichar character, NSRange range) {
|
---|
[388] | 117 | NSRange result = [s rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:
|
---|
[383] | 118 | [NSString stringWithCharacters: &character length: 1]]
|
---|
[388] | 119 | options: NSLiteralSearch range: range];
|
---|
| 120 | return (result.location != NSNotFound);
|
---|
[383] | 121 | }
|
---|
| 122 |
|
---|
| 123 | static BOOL ICCF_IsLikelyURI(NSString *s, NSRange range) {
|
---|
| 124 | return ([s rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @":/.@"]
|
---|
| 125 | options: NSLiteralSearch range: range].location != NSNotFound);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | static BOOL ICCF_IsLikelyIPv6Address(NSString *s, NSRange range) {
|
---|
| 129 | return ([s rangeOfCharacterFromSet:
|
---|
| 130 | [[NSCharacterSet characterSetWithCharactersInString: @"ABCDEFabcdef0123456789:"] invertedSet]
|
---|
| 131 | options: NSLiteralSearch range: range].location == NSNotFound);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[322] | 134 | NSRange ICCF_URLEnclosingRange(NSString *s, NSRange range) {
|
---|
| 135 | NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
|
---|
| 136 | NSRange delimiterRange;
|
---|
| 137 | unsigned extraLen;
|
---|
[388] | 138 | BOOL multiLine = NO;
|
---|
[322] | 139 |
|
---|
| 140 | ICCF_CheckRange(range);
|
---|
| 141 |
|
---|
| 142 | ICCF_Delimiters(&urlLeftDelimiters, &urlRightDelimiters);
|
---|
| 143 |
|
---|
[383] | 144 | // right delimiter selected? Yes, this can break with ...)URL(.... Oh well.
|
---|
| 145 | if (range.location > 0 && [urlRightDelimiters characterIsMember: [s characterAtIndex: range.location]]) {
|
---|
| 146 | --range.location;
|
---|
| 147 | ++range.length;
|
---|
| 148 | ICLog(@"expanding past initial %c, now |%@|", [s characterAtIndex: range.location + 1],
|
---|
| 149 | [s substringWithRange: range]);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[322] | 152 | expandFront:
|
---|
[375] | 153 | // XXX instead of 0, make this stop at the max URL length to prevent protracted searches
|
---|
[383] | 154 | // XXX backport to ICeCoffEETerminal
|
---|
[375] | 155 | // add 1 to range to trap delimiters that are on the edge of the selection (i.e., <...)
|
---|
| 156 | delimiterRange = [s rangeOfCharacterFromSet: urlLeftDelimiters
|
---|
| 157 | options: NSLiteralSearch | NSBackwardsSearch
|
---|
| 158 | range: NSMakeRange(0, range.location + (range.location != [s length]))];
|
---|
[322] | 159 | if (delimiterRange.location == NSNotFound) {
|
---|
| 160 | // extend to beginning of string
|
---|
| 161 | range.length += range.location;
|
---|
| 162 | range.location = 0;
|
---|
| 163 | } else {
|
---|
| 164 | NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
|
---|
[388] | 165 | if ([s characterAtIndex: delimiterRange.location] == '<') { // XXX move to expandBoth to handle clicking in middle
|
---|
| 166 | multiLine = YES;
|
---|
| 167 | urlRightDelimiters = [NSCharacterSet characterSetWithCharactersInString: @">"];
|
---|
| 168 | }
|
---|
[322] | 169 | range.length += range.location - delimiterRange.location - 1;
|
---|
| 170 | range.location = delimiterRange.location + 1;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[388] | 173 |
|
---|
[322] | 174 | expandBack:
|
---|
[375] | 175 | // XXX instead of length of string, make this stop at the max URL length to prevent protracted searches
|
---|
| 176 | // add 1 to range to trap delimiters that are on the edge of the selection (i.e., ...>)
|
---|
| 177 | extraLen = [s length] - range.location - range.length;
|
---|
[322] | 178 | delimiterRange = [s rangeOfCharacterFromSet: urlRightDelimiters
|
---|
| 179 | options: NSLiteralSearch
|
---|
| 180 | range: NSMakeRange(range.location + range.length - (range.length != 0),
|
---|
| 181 | extraLen + (range.length != 0))];
|
---|
| 182 | if (delimiterRange.location == NSNotFound) {
|
---|
| 183 | // extend to end of string
|
---|
| 184 | range.length += extraLen;
|
---|
[383] | 185 | extraLen = 0;
|
---|
[322] | 186 | } else {
|
---|
| 187 | NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
|
---|
| 188 | range.length += delimiterRange.location - range.location - range.length;
|
---|
[383] | 189 | extraLen = [s length] - NSMaxRange(range);
|
---|
| 190 |
|
---|
[388] | 191 | unichar opening, closing = [s characterAtIndex: delimiterRange.location];
|
---|
| 192 | if (closing == '>' && !multiLine && ICCF_StringIncludesCharacter(s, '<', NSMakeRange(0, range.location))) {
|
---|
| 193 | urlLeftDelimiters = [NSCharacterSet characterSetWithCharactersInString: @"<"];
|
---|
| 194 | goto expandFront; // XXX move to expandBoth to handle clicking in middle
|
---|
| 195 | }
|
---|
[383] | 196 | // grow URL past closing paren/brace/bracket if we've seen an open paren/brace/bracket
|
---|
| 197 | if (closing == ')') opening = '(';
|
---|
| 198 | else if (closing == '}') opening = '{';
|
---|
| 199 | else if (closing == ']') opening = '[';
|
---|
| 200 | else goto expandBoth;
|
---|
| 201 | if (!ICCF_StringIncludesCharacter(s, opening, range))
|
---|
| 202 | goto expandBoth;
|
---|
[322] | 203 |
|
---|
[383] | 204 | if (extraLen == 1) {
|
---|
[375] | 205 | range.length += 1;
|
---|
[383] | 206 | --extraLen;
|
---|
[375] | 207 | ICLog(@"expanding past %c, now |%@|", closing, [s substringWithRange: range]);
|
---|
| 208 | } else {
|
---|
[322] | 209 | range.length += 2;
|
---|
[375] | 210 | ICLog(@"expanding past %c, now |%@|", closing, [s substringWithRange: range]);
|
---|
[322] | 211 | goto expandBack;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[383] | 215 | expandBoth:
|
---|
| 216 | if (range.location <= 1)
|
---|
| 217 | goto checkRange; // nowhere to expand
|
---|
| 218 | unichar opening = [s characterAtIndex: range.location - 1], closing;
|
---|
| 219 | if (opening == '(') closing = ')';
|
---|
| 220 | else if (opening == '{') closing = '}';
|
---|
| 221 | else if (opening == '[') closing = ']';
|
---|
| 222 | else goto checkRange;
|
---|
| 223 |
|
---|
| 224 | ICLog(@"extraLen = %d", extraLen);
|
---|
| 225 | // check if we're inside a partial delimited URL: not foolproof, but handles (foo), {UUID} and [IPv6]
|
---|
| 226 | if (delimiterRange.location != NSNotFound && [s characterAtIndex: delimiterRange.location] == closing &&
|
---|
| 227 | ((opening == '[' && ICCF_IsLikelyIPv6Address(s, range)) || !ICCF_IsLikelyURI(s, range))) {
|
---|
| 228 | ICLog(@"expanding past %c...%c, was |%@|", opening, closing, [s substringWithRange: range]);
|
---|
| 229 | range.location -= 2;
|
---|
| 230 | if (extraLen > 1)
|
---|
| 231 | range.length += 4;
|
---|
| 232 | else
|
---|
| 233 | range.length += 2 + extraLen;
|
---|
| 234 | ICLog(@"expanding past %c...%c, now |%@|", opening, closing, [s substringWithRange: range]);
|
---|
| 235 | goto expandFront;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | if (ICCF_StringIncludesCharacter(s, closing, range) &&
|
---|
| 239 | ((opening == '[' &&
|
---|
| 240 | ICCF_IsLikelyIPv6Address(s, NSMakeRange(range.location,
|
---|
| 241 | [s rangeOfString: @"]"].location - range.location)))
|
---|
| 242 | || !ICCF_IsLikelyURI(s, range))) {
|
---|
| 243 | range.location -= 2;
|
---|
| 244 | range.length += 2;
|
---|
| 245 | ICLog(@"expanding past %c, now |%@|", opening, [s substringWithRange: range]);
|
---|
| 246 | goto expandFront;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[375] | 249 | checkRange:
|
---|
[322] | 250 | ICCF_CheckRange(range);
|
---|
| 251 |
|
---|
| 252 | ICCF_ParseURL([s substringWithRange: range], &range);
|
---|
| 253 |
|
---|
| 254 | return range;
|
---|
| 255 | } |
---|