1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
27 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
38 * ------------------------------------------------------------
39 ****************************************************************************/
45 #include <curl/curl.h>
48 char glob_buffer[URL_MAX_LENGTH];
51 int glob_word(char*, int);
53 int glob_set(char *pattern, int pos) {
54 /* processes a set expression with the point behind the opening '{'
55 ','-separated elements are collected until the next closing '}'
57 char* buf = glob_buffer;
60 pat = (URLPattern*)&glob_expand->pattern[glob_expand->size / 2];
61 /* patterns 0,1,2,... correspond to size=1,3,5,... */
63 pat->content.Set.size = 0;
64 pat->content.Set.ptr_s = 0;
65 pat->content.Set.elements = (char**)malloc(0);
70 case '\0': /* URL ended while set was still open */
71 printf("error: unmatched brace at pos %d\n", pos);
72 exit (URG_URL_MALFORMAT);
74 case '[': /* no nested expressions at this time */
75 printf("error: nested braces not supported %d\n", pos);
76 exit (URG_URL_MALFORMAT);
78 case '}': /* set element completed */
80 pat->content.Set.elements = realloc(pat->content.Set.elements, (pat->content.Set.size + 1) * sizeof(char*));
81 if (!pat->content.Set.elements) {
82 printf("out of memory in set pattern\n");
83 exit(URG_OUT_OF_MEMORY);
85 pat->content.Set.elements[pat->content.Set.size] = strdup(glob_buffer);
86 ++pat->content.Set.size;
88 if (*pattern == '}') /* entire set pattern completed */
89 /* always check for a literal (may be "") between patterns */
90 return pat->content.Set.size * glob_word(++pattern, ++pos);
96 case ']': /* illegal closing bracket */
97 printf("error: illegal pattern at pos %d\n", pos);
98 exit (URG_URL_MALFORMAT);
99 case '\\': /* escaped character, skip '\' */
100 if (*(buf+1) == '\0') { /* but no escaping of '\0'! */
101 printf("error: illegal pattern at pos %d\n", pos);
102 exit (URG_URL_MALFORMAT);
105 ++pos; /* intentional fallthrough */
107 *buf++ = *pattern++; /* copy character to set element */
111 exit (URG_FAILED_INIT);
114 int glob_range(char *pattern, int pos) {
115 /* processes a range expression with the point behind the opening '['
116 - char range: e.g. "a-z]", "B-Q]"
117 - num range: e.g. "0-9]", "17-2000]"
118 - num range with leading zeros: e.g. "001-999]"
119 expression is checked for well-formedness and collected until the next ']'
124 pat = (URLPattern*)&glob_expand->pattern[glob_expand->size / 2];
125 /* patterns 0,1,2,... correspond to size=1,3,5,... */
128 if (isalpha((int)*pattern)) { /* character range detected */
129 pat->type = UPTCharRange;
130 if (sscanf(pattern, "%c-%c]", &pat->content.CharRange.min_c, &pat->content.CharRange.max_c) != 2 ||
131 pat->content.CharRange.min_c >= pat->content.CharRange.max_c ||
132 pat->content.CharRange.max_c - pat->content.CharRange.min_c > 'z' - 'a') {
133 /* the pattern is not well-formed */
134 printf("error: illegal pattern or range specification after pos %d\n", pos);
135 exit (URG_URL_MALFORMAT);
137 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
138 /* always check for a literal (may be "") between patterns */
139 return (pat->content.CharRange.max_c - pat->content.CharRange.min_c + 1) *
140 glob_word(pattern + 4, pos + 4);
142 if (isdigit((int)*pattern)) { /* numeric range detected */
143 pat->type = UPTNumRange;
144 pat->content.NumRange.padlength = 0;
145 if (sscanf(pattern, "%d-%d]", &pat->content.NumRange.min_n, &pat->content.NumRange.max_n) != 2 ||
146 pat->content.NumRange.min_n >= pat->content.NumRange.max_n) {
147 /* the pattern is not well-formed */
148 printf("error: illegal pattern or range specification after pos %d\n", pos);
149 exit (URG_URL_MALFORMAT);
151 if (*pattern == '0') { /* leading zero specified */
153 while (isdigit((int)*c++))
154 ++pat->content.NumRange.padlength; /* padding length is set for all instances
157 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
158 c = (char*)(strchr(pattern, ']') + 1); /* continue after next ']' */
159 /* always check for a literal (may be "") between patterns */
160 return (pat->content.NumRange.max_n - pat->content.NumRange.min_n + 1) *
161 glob_word(c, pos + (c - pattern));
163 printf("error: illegal character in range specification at pos %d\n", pos);
164 exit (URG_URL_MALFORMAT);
167 int glob_word(char *pattern, int pos) {
168 /* processes a literal string component of a URL
169 special characters '{' and '[' branch to set/range processing functions
171 char* buf = glob_buffer;
174 while (*pattern != '\0' && *pattern != '{' && *pattern != '[') {
175 if (*pattern == '}' || *pattern == ']') {
176 printf("illegal character at position %d\n", pos);
177 exit (URG_URL_MALFORMAT);
179 if (*pattern == '\\') { /* escape character, skip '\' */
182 if (*pattern == '\0') { /* but no escaping of '\0'! */
183 printf("illegal character at position %d\n", pos);
184 exit (URG_URL_MALFORMAT);
187 *buf++ = *pattern++; /* copy character to literal */
191 litindex = glob_expand->size / 2;
192 /* literals 0,1,2,... correspond to size=0,2,4,... */
193 glob_expand->literal[litindex] = strdup(glob_buffer);
195 if (*pattern == '\0')
196 return 1; /* singular URL processed */
197 if (*pattern == '{') {
198 return glob_set(++pattern, ++pos); /* process set pattern */
200 if (*pattern == '[') {
201 return glob_range(++pattern, ++pos);/* process range pattern */
203 printf("internal error\n");
204 exit (URG_FAILED_INIT);
207 int glob_url(URLGlob** glob, char* url) {
208 int urlnum; /* counts instances of a globbed pattern */
210 glob_expand = (URLGlob*)malloc(sizeof(URLGlob));
211 glob_expand->size = 0;
212 urlnum = glob_word(url, 1);
217 char *next_url(URLGlob *glob) {
218 static int beenhere = 0;
219 char *buf = glob_buffer;
230 /* implement a counter over the index ranges of all patterns,
231 starting with the rightmost pattern */
232 for (i = glob->size / 2 - 1; carry && i >= 0; --i) {
234 pat = &glob->pattern[i];
237 if (++pat->content.Set.ptr_s == pat->content.Set.size) {
238 pat->content.Set.ptr_s = 0;
243 if (++pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
244 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
249 if (++pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
250 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
255 printf("internal error: invalid pattern type (%d)\n", pat->type);
256 exit (URG_FAILED_INIT);
259 if (carry) /* first pattern ptr has run into overflow, done! */
263 for (i = 0; i < glob->size; ++i) {
264 if (!(i % 2)) { /* every other term (i even) is a literal */
265 lit = glob->literal[i/2];
269 else { /* the rest (i odd) are patterns */
270 pat = &glob->pattern[i/2];
273 strcpy(buf, pat->content.Set.elements[pat->content.Set.ptr_s]);
274 buf += strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
277 *buf++ = pat->content.CharRange.ptr_c;
280 buf += sprintf(buf, "%0*d", pat->content.NumRange.padlength, pat->content.NumRange.ptr_n);
283 printf("internal error: invalid pattern type (%d)\n", pat->type);
284 exit (URG_FAILED_INIT);
289 return strdup(glob_buffer);
292 char *match_url(char *filename, URLGlob glob) {
293 char *buf = glob_buffer;
297 while (*filename != '\0') {
298 if (*filename == '#') {
299 if (!isdigit((int)*++filename) ||
300 *filename == '0') { /* only '#1' ... '#9' allowed */
301 printf("illegal matching expression\n");
302 exit(URG_URL_MALFORMAT);
305 if (i + 1 > glob.size / 2) {
306 printf("match against nonexisting pattern\n");
307 exit(URG_URL_MALFORMAT);
309 pat = glob.pattern[i];
312 strcpy(buf, pat.content.Set.elements[pat.content.Set.ptr_s]);
313 buf += strlen(pat.content.Set.elements[pat.content.Set.ptr_s]);
316 *buf++ = pat.content.CharRange.ptr_c;
319 buf += sprintf(buf, "%0*d", pat.content.NumRange.padlength, pat.content.NumRange.ptr_n);
322 printf("internal error: invalid pattern type (%d)\n", pat.type);
323 exit (URG_FAILED_INIT);
328 *buf++ = *filename++;
331 return strdup(glob_buffer);