2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-07-02 08:28:57 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 09:20:26 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-02-01 10:36:56 -05:00
|
|
|
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-07-02 08:28:57 -04:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* $Id$
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2001-11-29 07:47:41 -05:00
|
|
|
/* client-local setup.h */
|
|
|
|
#include "setup.h"
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <curl/curl.h>
|
2003-07-23 04:21:21 -04:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
#include "urlglob.h"
|
|
|
|
|
2004-05-12 04:22:04 -04:00
|
|
|
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
|
|
|
|
#include "memdebug.h"
|
2000-10-09 07:13:17 -04:00
|
|
|
#endif
|
|
|
|
|
2001-01-23 05:14:43 -05:00
|
|
|
typedef enum {
|
|
|
|
GLOB_OK,
|
|
|
|
GLOB_ERROR
|
|
|
|
} GlobCode;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2001-01-23 05:14:43 -05:00
|
|
|
/*
|
|
|
|
* glob_word()
|
|
|
|
*
|
|
|
|
* Input a full globbed string, set the forth argument to the amount of
|
|
|
|
* strings we get out of this. Return GlobCode.
|
|
|
|
*/
|
2003-06-10 05:42:22 -04:00
|
|
|
static GlobCode glob_word(URLGlob *, /* object anchor */
|
|
|
|
char *, /* globbed string */
|
2004-03-23 04:12:51 -05:00
|
|
|
size_t, /* position */
|
2003-06-10 05:42:22 -04:00
|
|
|
int *); /* returned number of strings */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2004-03-23 04:12:51 -05:00
|
|
|
static GlobCode glob_set(URLGlob *glob, char *pattern,
|
|
|
|
size_t pos, int *amount)
|
2000-12-06 05:10:31 -05:00
|
|
|
{
|
1999-12-29 09:20:26 -05:00
|
|
|
/* processes a set expression with the point behind the opening '{'
|
|
|
|
','-separated elements are collected until the next closing '}'
|
|
|
|
*/
|
2006-04-07 08:10:34 -04:00
|
|
|
bool done = FALSE;
|
2000-12-06 05:10:31 -05:00
|
|
|
char* buf = glob->glob_buffer;
|
1999-12-29 09:20:26 -05:00
|
|
|
URLPattern *pat;
|
|
|
|
|
2000-12-06 05:10:31 -05:00
|
|
|
pat = (URLPattern*)&glob->pattern[glob->size / 2];
|
1999-12-29 09:20:26 -05:00
|
|
|
/* patterns 0,1,2,... correspond to size=1,3,5,... */
|
|
|
|
pat->type = UPTSet;
|
|
|
|
pat->content.Set.size = 0;
|
|
|
|
pat->content.Set.ptr_s = 0;
|
|
|
|
pat->content.Set.elements = (char**)malloc(0);
|
2000-12-06 05:10:31 -05:00
|
|
|
++glob->size;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2006-04-07 08:10:34 -04:00
|
|
|
while (!done) {
|
2004-12-15 04:23:24 -05:00
|
|
|
bool skip;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
switch (*pattern) {
|
2004-10-06 03:50:18 -04:00
|
|
|
case '\0': /* URL ended while set was still open */
|
2004-03-08 07:47:37 -05:00
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
2004-04-06 03:48:29 -04:00
|
|
|
"unmatched brace at pos %d\n", (int)pos);
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case '{':
|
2004-10-06 03:50:18 -04:00
|
|
|
case '[': /* no nested expressions at this time */
|
2004-03-08 07:47:37 -05:00
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
2004-04-06 03:48:29 -04:00
|
|
|
"nested braces not supported at pos %d\n", (int)pos);
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case ',':
|
2004-10-06 03:50:18 -04:00
|
|
|
case '}': /* set element completed */
|
1999-12-29 09:20:26 -05:00
|
|
|
*buf = '\0';
|
2000-12-06 05:10:31 -05:00
|
|
|
pat->content.Set.elements =
|
|
|
|
realloc(pat->content.Set.elements,
|
|
|
|
(pat->content.Set.size + 1) * sizeof(char*));
|
1999-12-29 09:20:26 -05:00
|
|
|
if (!pat->content.Set.elements) {
|
2004-03-08 07:47:37 -05:00
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg), "out of memory");
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
pat->content.Set.elements[pat->content.Set.size] =
|
|
|
|
strdup(glob->glob_buffer);
|
1999-12-29 09:20:26 -05:00
|
|
|
++pat->content.Set.size;
|
|
|
|
|
2001-01-23 05:14:43 -05:00
|
|
|
if (*pattern == '}') {
|
|
|
|
/* entire set pattern completed */
|
|
|
|
int wordamount;
|
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
/* always check for a literal (may be "") between patterns */
|
2001-01-23 05:14:43 -05:00
|
|
|
if(GLOB_ERROR == glob_word(glob, ++pattern, ++pos, &wordamount))
|
|
|
|
wordamount=1;
|
2004-10-06 03:50:18 -04:00
|
|
|
*amount = pat->content.Set.size * wordamount;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2006-04-07 08:10:34 -04:00
|
|
|
done = TRUE;
|
|
|
|
continue;
|
2001-01-23 05:14:43 -05:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2000-12-06 05:10:31 -05:00
|
|
|
buf = glob->glob_buffer;
|
1999-12-29 09:20:26 -05:00
|
|
|
++pattern;
|
|
|
|
++pos;
|
|
|
|
break;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
case ']': /* illegal closing bracket */
|
2004-07-02 08:28:57 -04:00
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
2004-04-06 03:48:29 -04:00
|
|
|
"illegal pattern at pos %d\n", (int)pos);
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
case '\\': /* escaped character, skip '\' */
|
2004-12-15 04:23:24 -05:00
|
|
|
switch(pattern[1]) {
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case ',':
|
|
|
|
skip = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
skip = FALSE;
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2004-12-15 04:23:24 -05:00
|
|
|
if(skip) {
|
|
|
|
if (*(buf+1) == '\0') { /* but no escaping of '\0'! */
|
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
|
|
|
"illegal pattern at pos %d\n", (int)pos);
|
|
|
|
return GLOB_ERROR;
|
|
|
|
}
|
|
|
|
++pattern;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
/* intentional fallthrough */
|
1999-12-29 09:20:26 -05:00
|
|
|
default:
|
2004-10-06 03:50:18 -04:00
|
|
|
*buf++ = *pattern++; /* copy character to set element */
|
1999-12-29 09:20:26 -05:00
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
2006-04-07 08:10:34 -04:00
|
|
|
return GLOB_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2004-07-02 08:28:57 -04:00
|
|
|
static GlobCode glob_range(URLGlob *glob, char *pattern,
|
|
|
|
size_t pos, int *amount)
|
2000-12-06 05:10:31 -05:00
|
|
|
{
|
1999-12-29 09:20:26 -05:00
|
|
|
/* processes a range expression with the point behind the opening '['
|
|
|
|
- char range: e.g. "a-z]", "B-Q]"
|
|
|
|
- num range: e.g. "0-9]", "17-2000]"
|
|
|
|
- num range with leading zeros: e.g. "001-999]"
|
|
|
|
expression is checked for well-formedness and collected until the next ']'
|
|
|
|
*/
|
|
|
|
URLPattern *pat;
|
|
|
|
char *c;
|
2001-01-23 05:14:43 -05:00
|
|
|
int wordamount=1;
|
2005-11-10 17:11:01 -05:00
|
|
|
char sep;
|
|
|
|
char sep2;
|
|
|
|
int step;
|
|
|
|
int rc;
|
2004-07-02 08:28:57 -04:00
|
|
|
|
2000-12-06 05:10:31 -05:00
|
|
|
pat = (URLPattern*)&glob->pattern[glob->size / 2];
|
1999-12-29 09:20:26 -05:00
|
|
|
/* patterns 0,1,2,... correspond to size=1,3,5,... */
|
2000-12-06 05:10:31 -05:00
|
|
|
++glob->size;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2006-10-17 17:32:56 -04:00
|
|
|
if (ISALPHA(*pattern)) { /* character range detected */
|
2005-11-10 17:11:01 -05:00
|
|
|
char min_c;
|
|
|
|
char max_c;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
pat->type = UPTCharRange;
|
2005-11-10 17:11:01 -05:00
|
|
|
rc = sscanf(pattern, "%c-%c%c%d%c", &min_c, &max_c, &sep, &step, &sep2);
|
|
|
|
if ((rc < 3) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a'))) {
|
2004-07-02 08:28:57 -04:00
|
|
|
/* the pattern is not well-formed */
|
2004-03-08 07:47:37 -05:00
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
2007-09-19 20:37:08 -04:00
|
|
|
"error: bad range specification after pos %d\n", pos);
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
/* check the (first) separating character */
|
|
|
|
if((sep != ']') && (sep != ':')) {
|
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
|
|
|
"error: unsupported character (%c) after range at pos %d\n",
|
|
|
|
sep, pos);
|
|
|
|
return GLOB_ERROR;
|
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
/* if there was a ":[num]" thing, use that as step or else use 1 */
|
|
|
|
pat->content.CharRange.step =
|
|
|
|
((sep == ':') && (rc == 5) && (sep2 == ']'))?step:1;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
pat->content.CharRange.ptr_c = pat->content.CharRange.min_c = min_c;
|
|
|
|
pat->content.CharRange.max_c = max_c;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2006-10-17 17:32:56 -04:00
|
|
|
else if (ISDIGIT(*pattern)) { /* numeric range detected */
|
2005-11-10 17:11:01 -05:00
|
|
|
int min_n;
|
|
|
|
int max_n;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
pat->type = UPTNumRange;
|
|
|
|
pat->content.NumRange.padlength = 0;
|
2005-11-10 17:11:01 -05:00
|
|
|
|
|
|
|
rc = sscanf(pattern, "%d-%d%c%d%c", &min_n, &max_n, &sep, &step, &sep2);
|
|
|
|
|
2007-03-15 18:05:01 -04:00
|
|
|
if ((rc < 2) || (min_n > max_n)) {
|
2004-07-02 08:28:57 -04:00
|
|
|
/* the pattern is not well-formed */
|
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
2005-11-10 17:11:01 -05:00
|
|
|
"error: bad range specification after pos %d\n", pos);
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2005-11-10 17:11:01 -05:00
|
|
|
pat->content.NumRange.ptr_n = pat->content.NumRange.min_n = min_n;
|
|
|
|
pat->content.NumRange.max_n = max_n;
|
|
|
|
|
|
|
|
/* if there was a ":[num]" thing, use that as step or else use 1 */
|
|
|
|
pat->content.NumRange.step =
|
|
|
|
((sep == ':') && (rc == 5) && (sep2 == ']'))?step:1;
|
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
if (*pattern == '0') { /* leading zero specified */
|
2004-07-02 08:28:57 -04:00
|
|
|
c = pattern;
|
2006-10-17 17:32:56 -04:00
|
|
|
while (ISDIGIT(*c)) {
|
|
|
|
c++;
|
2004-10-06 03:50:18 -04:00
|
|
|
++pat->content.NumRange.padlength; /* padding length is set for all
|
2001-01-23 05:14:43 -05:00
|
|
|
instances of this pattern */
|
2006-10-17 17:32:56 -04:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg),
|
|
|
|
"illegal character in range specification at pos %d\n", pos);
|
|
|
|
return GLOB_ERROR;
|
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
c = (char*)strchr(pattern, ']'); /* continue after next ']' */
|
|
|
|
if(c)
|
|
|
|
c++;
|
|
|
|
else {
|
|
|
|
snprintf(glob->errormsg, sizeof(glob->errormsg), "missing ']'");
|
|
|
|
return GLOB_ERROR; /* missing ']' */
|
|
|
|
}
|
2004-07-02 08:28:57 -04:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
/* always check for a literal (may be "") between patterns */
|
|
|
|
|
|
|
|
if(GLOB_ERROR == glob_word(glob, c, pos + (c - pattern), &wordamount))
|
|
|
|
wordamount = 1;
|
|
|
|
|
|
|
|
if(pat->type == UPTCharRange)
|
|
|
|
*amount = (pat->content.CharRange.max_c -
|
|
|
|
pat->content.CharRange.min_c + 1) *
|
2001-01-23 05:14:43 -05:00
|
|
|
wordamount;
|
2005-11-10 17:11:01 -05:00
|
|
|
else
|
|
|
|
*amount = (pat->content.NumRange.max_n -
|
|
|
|
pat->content.NumRange.min_n + 1) * wordamount;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2005-11-10 17:11:01 -05:00
|
|
|
return GLOB_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2004-03-23 04:12:51 -05:00
|
|
|
static GlobCode glob_word(URLGlob *glob, char *pattern,
|
|
|
|
size_t pos, int *amount)
|
2000-12-06 05:10:31 -05:00
|
|
|
{
|
1999-12-29 09:20:26 -05:00
|
|
|
/* processes a literal string component of a URL
|
|
|
|
special characters '{' and '[' branch to set/range processing functions
|
2004-07-02 08:28:57 -04:00
|
|
|
*/
|
2000-12-06 05:10:31 -05:00
|
|
|
char* buf = glob->glob_buffer;
|
2004-03-23 04:12:51 -05:00
|
|
|
size_t litindex;
|
2003-08-06 19:47:01 -04:00
|
|
|
GlobCode res = GLOB_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2001-01-23 05:14:43 -05:00
|
|
|
*amount = 1; /* default is one single string */
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
while (*pattern != '\0' && *pattern != '{' && *pattern != '[') {
|
2002-03-06 17:52:00 -05:00
|
|
|
if (*pattern == '}' || *pattern == ']')
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
2002-03-06 17:52:00 -05:00
|
|
|
|
|
|
|
/* only allow \ to escape known "special letters" */
|
|
|
|
if (*pattern == '\\' &&
|
|
|
|
(*(pattern+1) == '{' || *(pattern+1) == '[' ||
|
|
|
|
*(pattern+1) == '}' || *(pattern+1) == ']') ) {
|
|
|
|
|
|
|
|
/* escape character, skip '\' */
|
1999-12-29 09:20:26 -05:00
|
|
|
++pattern;
|
|
|
|
++pos;
|
2004-10-06 03:50:18 -04:00
|
|
|
if (*pattern == '\0') /* but no escaping of '\0'! */
|
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
*buf++ = *pattern++; /* copy character to literal */
|
1999-12-29 09:20:26 -05:00
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
2000-12-06 05:10:31 -05:00
|
|
|
litindex = glob->size / 2;
|
1999-12-29 09:20:26 -05:00
|
|
|
/* literals 0,1,2,... correspond to size=0,2,4,... */
|
2000-12-06 05:10:31 -05:00
|
|
|
glob->literal[litindex] = strdup(glob->glob_buffer);
|
2003-08-14 11:01:20 -04:00
|
|
|
if(!glob->literal[litindex])
|
|
|
|
return GLOB_ERROR;
|
2000-12-06 05:10:31 -05:00
|
|
|
++glob->size;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
switch (*pattern) {
|
|
|
|
case '\0':
|
2004-10-06 03:50:18 -04:00
|
|
|
break; /* singular URL processed */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
case '{':
|
|
|
|
/* process set pattern */
|
2003-08-06 19:47:01 -04:00
|
|
|
res = glob_set(glob, ++pattern, ++pos, amount);
|
|
|
|
break;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
case '[':
|
|
|
|
/* process range pattern */
|
2003-08-06 19:47:01 -04:00
|
|
|
res= glob_range(glob, ++pattern, ++pos, amount);
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2003-08-06 19:47:01 -04:00
|
|
|
if(GLOB_OK != res)
|
|
|
|
/* free that strdup'ed string again */
|
|
|
|
free(glob->literal[litindex]);
|
|
|
|
|
|
|
|
return res; /* something got wrong */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2003-06-10 05:42:22 -04:00
|
|
|
int glob_url(URLGlob** glob, char* url, int *urlnum, FILE *error)
|
2000-05-09 08:29:28 -04:00
|
|
|
{
|
2000-11-20 03:54:32 -05:00
|
|
|
/*
|
|
|
|
* We can deal with any-size, just make a buffer with the same length
|
|
|
|
* as the specified URL!
|
|
|
|
*/
|
2000-12-06 05:10:31 -05:00
|
|
|
URLGlob *glob_expand;
|
2001-01-23 05:14:43 -05:00
|
|
|
int amount;
|
2000-12-06 05:10:31 -05:00
|
|
|
char *glob_buffer=(char *)malloc(strlen(url)+1);
|
2001-01-23 05:14:43 -05:00
|
|
|
|
2004-03-08 07:47:37 -05:00
|
|
|
*glob = NULL;
|
|
|
|
if(NULL == glob_buffer)
|
2000-11-20 03:54:32 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-01-29 18:46:27 -05:00
|
|
|
glob_expand = (URLGlob*)calloc(sizeof(URLGlob), 1);
|
2000-12-06 05:10:31 -05:00
|
|
|
if(NULL == glob_expand) {
|
|
|
|
free(glob_buffer);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
glob_expand->size = 0;
|
2000-12-06 05:10:31 -05:00
|
|
|
glob_expand->urllen = strlen(url);
|
|
|
|
glob_expand->glob_buffer = glob_buffer;
|
2001-01-08 02:37:44 -05:00
|
|
|
glob_expand->beenhere=0;
|
2001-01-23 05:14:43 -05:00
|
|
|
if(GLOB_OK == glob_word(glob_expand, url, 1, &amount))
|
|
|
|
*urlnum = amount;
|
|
|
|
else {
|
2004-03-08 07:47:37 -05:00
|
|
|
if(error && glob_expand->errormsg[0]) {
|
|
|
|
/* send error description to the error-stream */
|
|
|
|
fprintf(error, "curl: (%d) [globbing] %s\n",
|
|
|
|
CURLE_URL_MALFORMAT, glob_expand->errormsg);
|
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
/* it failed, we cleanup */
|
|
|
|
free(glob_buffer);
|
|
|
|
free(glob_expand);
|
|
|
|
glob_expand = NULL;
|
|
|
|
*urlnum = 1;
|
2003-06-10 05:42:22 -04:00
|
|
|
return CURLE_URL_MALFORMAT;
|
2001-01-23 05:14:43 -05:00
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
*glob = glob_expand;
|
2000-05-22 10:12:12 -04:00
|
|
|
return CURLE_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2000-12-06 05:10:31 -05:00
|
|
|
void glob_cleanup(URLGlob* glob)
|
|
|
|
{
|
2004-07-02 08:28:57 -04:00
|
|
|
size_t i;
|
|
|
|
int elem;
|
2000-10-12 05:12:24 -04:00
|
|
|
|
2004-07-02 08:28:57 -04:00
|
|
|
for (i = glob->size - 1; i < glob->size; --i) {
|
2004-10-06 03:50:18 -04:00
|
|
|
if (!(i & 1)) { /* even indexes contain literals */
|
2000-10-12 05:12:24 -04:00
|
|
|
free(glob->literal[i/2]);
|
2003-08-14 11:01:20 -04:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
else { /* odd indexes contain sets or ranges */
|
2000-10-12 05:12:24 -04:00
|
|
|
if (glob->pattern[i/2].type == UPTSet) {
|
2004-10-06 03:50:18 -04:00
|
|
|
for (elem = glob->pattern[i/2].content.Set.size - 1;
|
2003-08-14 11:01:20 -04:00
|
|
|
elem >= 0;
|
|
|
|
--elem) {
|
2004-10-06 03:50:18 -04:00
|
|
|
free(glob->pattern[i/2].content.Set.elements[elem]);
|
|
|
|
}
|
|
|
|
free(glob->pattern[i/2].content.Set.elements);
|
2000-10-12 05:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
free(glob->glob_buffer);
|
2000-10-12 05:12:24 -04:00
|
|
|
free(glob);
|
|
|
|
}
|
|
|
|
|
2003-06-10 05:42:22 -04:00
|
|
|
char *glob_next_url(URLGlob *glob)
|
2000-05-09 08:29:28 -04:00
|
|
|
{
|
2000-12-06 05:10:31 -05:00
|
|
|
char *buf = glob->glob_buffer;
|
1999-12-29 09:20:26 -05:00
|
|
|
URLPattern *pat;
|
|
|
|
char *lit;
|
2004-07-02 08:28:57 -04:00
|
|
|
size_t i;
|
2004-03-23 04:12:51 -05:00
|
|
|
size_t j;
|
2006-03-28 02:51:59 -05:00
|
|
|
size_t buflen = glob->urllen+1;
|
|
|
|
size_t len;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2001-01-08 02:37:44 -05:00
|
|
|
if (!glob->beenhere)
|
|
|
|
glob->beenhere = 1;
|
1999-12-29 09:20:26 -05:00
|
|
|
else {
|
2005-11-10 17:11:01 -05:00
|
|
|
bool carry = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
/* implement a counter over the index ranges of all patterns,
|
|
|
|
starting with the rightmost pattern */
|
2004-07-02 08:28:57 -04:00
|
|
|
for (i = glob->size / 2 - 1; carry && i < glob->size; --i) {
|
2005-11-10 17:11:01 -05:00
|
|
|
carry = FALSE;
|
1999-12-29 09:20:26 -05:00
|
|
|
pat = &glob->pattern[i];
|
|
|
|
switch (pat->type) {
|
|
|
|
case UPTSet:
|
2004-10-06 03:50:18 -04:00
|
|
|
if (++pat->content.Set.ptr_s == pat->content.Set.size) {
|
|
|
|
pat->content.Set.ptr_s = 0;
|
2005-11-10 17:11:01 -05:00
|
|
|
carry = TRUE;
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
case UPTCharRange:
|
2007-02-02 12:16:06 -05:00
|
|
|
pat->content.CharRange.ptr_c = (char)(pat->content.CharRange.step +
|
|
|
|
(int)((unsigned char)pat->content.CharRange.ptr_c));
|
2005-11-10 17:11:01 -05:00
|
|
|
if (pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
|
2004-10-06 03:50:18 -04:00
|
|
|
pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
|
2005-11-10 17:11:01 -05:00
|
|
|
carry = TRUE;
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
case UPTNumRange:
|
2005-11-10 17:11:01 -05:00
|
|
|
pat->content.NumRange.ptr_n += pat->content.NumRange.step;
|
|
|
|
if (pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
|
2004-10-06 03:50:18 -04:00
|
|
|
pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
|
2005-11-10 17:11:01 -05:00
|
|
|
carry = TRUE;
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
default:
|
2004-10-06 03:50:18 -04:00
|
|
|
printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
|
|
|
|
exit (CURLE_FAILED_INIT);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
if (carry) /* first pattern ptr has run into overflow, done! */
|
1999-12-29 09:20:26 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-23 04:12:51 -05:00
|
|
|
for (j = 0; j < glob->size; ++j) {
|
|
|
|
if (!(j&1)) { /* every other term (j even) is a literal */
|
|
|
|
lit = glob->literal[j/2];
|
2006-03-28 02:51:59 -05:00
|
|
|
len = snprintf(buf, buflen, "%s", lit);
|
|
|
|
buf += len;
|
|
|
|
buflen -= len;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
else { /* the rest (i odd) are patterns */
|
2004-03-23 04:12:51 -05:00
|
|
|
pat = &glob->pattern[j/2];
|
1999-12-29 09:20:26 -05:00
|
|
|
switch(pat->type) {
|
|
|
|
case UPTSet:
|
2006-03-28 02:51:59 -05:00
|
|
|
len = strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
|
|
|
|
snprintf(buf, buflen, "%s",
|
|
|
|
pat->content.Set.elements[pat->content.Set.ptr_s]);
|
|
|
|
buf += len;
|
|
|
|
buflen -= len;
|
2004-10-06 03:50:18 -04:00
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
case UPTCharRange:
|
2004-10-06 03:50:18 -04:00
|
|
|
*buf++ = pat->content.CharRange.ptr_c;
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
case UPTNumRange:
|
2006-03-28 02:51:59 -05:00
|
|
|
len = snprintf(buf, buflen, "%0*d",
|
|
|
|
pat->content.NumRange.padlength,
|
|
|
|
pat->content.NumRange.ptr_n);
|
|
|
|
buf += len;
|
|
|
|
buflen -= len;
|
2004-10-06 03:50:18 -04:00
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
default:
|
2004-10-06 03:50:18 -04:00
|
|
|
printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
|
|
|
|
exit (CURLE_FAILED_INIT);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
2000-12-06 05:10:31 -05:00
|
|
|
return strdup(glob->glob_buffer);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2003-06-10 05:42:22 -04:00
|
|
|
char *glob_match_url(char *filename, URLGlob *glob)
|
2000-12-06 05:10:31 -05:00
|
|
|
{
|
|
|
|
char *target;
|
2004-03-23 04:12:51 -05:00
|
|
|
size_t allocsize;
|
|
|
|
size_t stringlen=0;
|
2000-12-06 05:10:31 -05:00
|
|
|
char numbuf[18];
|
2004-01-16 04:17:04 -05:00
|
|
|
char *appendthis = NULL;
|
2004-03-23 04:12:51 -05:00
|
|
|
size_t appendlen = 0;
|
2000-12-06 05:10:31 -05:00
|
|
|
|
|
|
|
/* We cannot use the glob_buffer for storage here since the filename may
|
|
|
|
* be longer than the URL we use. We allocate a good start size, then
|
|
|
|
* we need to realloc in case of need.
|
|
|
|
*/
|
|
|
|
allocsize=strlen(filename);
|
|
|
|
target = malloc(allocsize);
|
|
|
|
if(NULL == target)
|
|
|
|
return NULL; /* major failure */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2003-08-14 07:53:09 -04:00
|
|
|
while (*filename) {
|
2006-10-17 17:32:56 -04:00
|
|
|
if (*filename == '#' && ISDIGIT(filename[1])) {
|
2004-03-23 04:12:51 -05:00
|
|
|
unsigned long i;
|
2004-07-26 05:11:10 -04:00
|
|
|
char *ptr = filename;
|
2003-08-14 07:53:09 -04:00
|
|
|
unsigned long num = strtoul(&filename[1], &filename, 10);
|
|
|
|
i = num-1;
|
|
|
|
|
|
|
|
if (num && (i <= glob->size / 2)) {
|
|
|
|
URLPattern pat = glob->pattern[i];
|
|
|
|
switch (pat.type) {
|
|
|
|
case UPTSet:
|
|
|
|
appendthis = pat.content.Set.elements[pat.content.Set.ptr_s];
|
2004-03-23 04:12:51 -05:00
|
|
|
appendlen = strlen(pat.content.Set.elements[pat.content.Set.ptr_s]);
|
2003-08-14 07:53:09 -04:00
|
|
|
break;
|
|
|
|
case UPTCharRange:
|
|
|
|
numbuf[0]=pat.content.CharRange.ptr_c;
|
|
|
|
numbuf[1]=0;
|
|
|
|
appendthis=numbuf;
|
|
|
|
appendlen=1;
|
|
|
|
break;
|
|
|
|
case UPTNumRange:
|
2006-03-28 02:51:59 -05:00
|
|
|
snprintf(numbuf, sizeof(numbuf), "%0*d",
|
|
|
|
pat.content.NumRange.padlength,
|
|
|
|
pat.content.NumRange.ptr_n);
|
2003-08-14 07:53:09 -04:00
|
|
|
appendthis = numbuf;
|
2004-03-23 04:12:51 -05:00
|
|
|
appendlen = strlen(numbuf);
|
2003-08-14 07:53:09 -04:00
|
|
|
break;
|
|
|
|
default:
|
2004-03-08 07:51:13 -05:00
|
|
|
printf("internal error: invalid pattern type (%d)\n",
|
|
|
|
(int)pat.type);
|
2003-08-14 09:37:55 -04:00
|
|
|
free(target);
|
2003-08-14 07:53:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2004-07-26 05:11:10 -04:00
|
|
|
else {
|
|
|
|
/* #[num] out of range, use the #[num] in the output */
|
|
|
|
filename = ptr;
|
|
|
|
appendthis=filename++;
|
|
|
|
appendlen=1;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
else {
|
|
|
|
appendthis=filename++;
|
|
|
|
appendlen=1;
|
|
|
|
}
|
|
|
|
if(appendlen + stringlen >= allocsize) {
|
|
|
|
char *newstr;
|
|
|
|
allocsize = (appendlen + stringlen)*2;
|
|
|
|
newstr=realloc(target, allocsize);
|
|
|
|
if(NULL ==newstr) {
|
|
|
|
free(target);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
target=newstr;
|
|
|
|
}
|
|
|
|
memcpy(&target[stringlen], appendthis, appendlen);
|
|
|
|
stringlen += appendlen;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
target[stringlen]= '\0';
|
|
|
|
return target;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|