1999-12-29 09:20:26 -05:00
|
|
|
/*****************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* In order to be useful for every potential user, curl and libcurl are
|
|
|
|
* dual-licensed under the MPL and the MIT/X-derivate licenses.
|
1999-12-29 09:20:26 -05: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
|
|
|
|
* furnished to do so, under the terms of the MPL or the MIT/X-derivate
|
|
|
|
* licenses. You may pick one of these licenses.
|
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$
|
|
|
|
*****************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "urlglob.h"
|
|
|
|
|
2000-10-09 07:13:17 -04:00
|
|
|
#ifdef MALLOCDEBUG
|
|
|
|
#include "../lib/memdebug.h"
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
GlobCode glob_word(URLGlob *, /* object anchor */
|
|
|
|
char *, /* globbed string */
|
|
|
|
int, /* position */
|
|
|
|
int *); /* returned number of strings */
|
|
|
|
|
|
|
|
GlobCode glob_set(URLGlob *glob, char *pattern, int 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 '}'
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
while (1) {
|
|
|
|
switch (*pattern) {
|
2001-01-23 05:14:43 -05:00
|
|
|
case '\0': /* URL ended while set was still open */
|
|
|
|
/*printf("error: unmatched brace at pos %d\n", pos);*/
|
|
|
|
return GLOB_ERROR;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case '{':
|
2001-01-23 05:14:43 -05:00
|
|
|
case '[': /* no nested expressions at this time */
|
|
|
|
/*printf("error: nested braces not supported %d\n", pos);*/
|
|
|
|
return GLOB_ERROR;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case ',':
|
|
|
|
case '}': /* set element completed */
|
|
|
|
*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) {
|
2001-01-23 05:14:43 -05:00
|
|
|
/*printf("out of memory in set pattern\n");*/
|
|
|
|
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;
|
|
|
|
|
1999-12-29 09:20:26 -05: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;
|
|
|
|
*amount = pat->content.Set.size * wordamount;
|
|
|
|
|
|
|
|
return GLOB_OK;
|
|
|
|
}
|
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
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case ']': /* illegal closing bracket */
|
2001-01-23 05:14:43 -05:00
|
|
|
/*printf("error: illegal pattern at pos %d\n", pos);*/
|
|
|
|
return GLOB_ERROR;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
case '\\': /* escaped character, skip '\' */
|
|
|
|
if (*(buf+1) == '\0') { /* but no escaping of '\0'! */
|
2001-01-23 05:14:43 -05:00
|
|
|
/*printf("error: illegal pattern at pos %d\n", pos); */
|
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
++pattern;
|
|
|
|
++pos; /* intentional fallthrough */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
default:
|
|
|
|
*buf++ = *pattern++; /* copy character to set element */
|
|
|
|
++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
|
|
|
GlobCode glob_range(URLGlob *glob, char *pattern, int 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;
|
1999-12-29 09:20:26 -05: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
|
|
|
|
|
|
|
if (isalpha((int)*pattern)) { /* character range detected */
|
|
|
|
pat->type = UPTCharRange;
|
2001-01-23 05:14:43 -05:00
|
|
|
if (sscanf(pattern, "%c-%c]", &pat->content.CharRange.min_c,
|
|
|
|
&pat->content.CharRange.max_c) != 2 ||
|
1999-12-29 09:20:26 -05:00
|
|
|
pat->content.CharRange.min_c >= pat->content.CharRange.max_c ||
|
|
|
|
pat->content.CharRange.max_c - pat->content.CharRange.min_c > 'z' - 'a') {
|
|
|
|
/* the pattern is not well-formed */
|
2001-01-23 05:14:43 -05:00
|
|
|
#if 0
|
|
|
|
printf("error: illegal pattern or range specification after pos %d\n",
|
|
|
|
pos);
|
|
|
|
#endif
|
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
|
|
|
|
/* always check for a literal (may be "") between patterns */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
if(GLOB_ERROR == glob_word(glob, pattern + 4, pos + 4, &wordamount))
|
|
|
|
wordamount=1;
|
|
|
|
|
|
|
|
*amount = (pat->content.CharRange.max_c -
|
|
|
|
pat->content.CharRange.min_c + 1) *
|
|
|
|
wordamount;
|
|
|
|
|
|
|
|
return GLOB_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
if (isdigit((int)*pattern)) { /* numeric range detected */
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
pat->type = UPTNumRange;
|
|
|
|
pat->content.NumRange.padlength = 0;
|
2001-01-23 05:14:43 -05:00
|
|
|
if (sscanf(pattern, "%d-%d]",
|
|
|
|
&pat->content.NumRange.min_n,
|
|
|
|
&pat->content.NumRange.max_n) != 2 ||
|
1999-12-29 09:20:26 -05:00
|
|
|
pat->content.NumRange.min_n >= pat->content.NumRange.max_n) {
|
|
|
|
/* the pattern is not well-formed */
|
2001-01-23 05:14:43 -05:00
|
|
|
#if 0
|
|
|
|
printf("error: illegal pattern or range specification after pos %d\n",
|
|
|
|
pos);
|
|
|
|
#endif
|
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
if (*pattern == '0') { /* leading zero specified */
|
|
|
|
c = pattern;
|
|
|
|
while (isdigit((int)*c++))
|
2001-01-23 05:14:43 -05:00
|
|
|
++pat->content.NumRange.padlength; /* padding length is set for all
|
|
|
|
instances of this pattern */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
|
|
|
|
c = (char*)(strchr(pattern, ']') + 1); /* continue after next ']' */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
/* always check for a literal (may be "") between patterns */
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
if(GLOB_ERROR == glob_word(glob, c, pos + (c - pattern), &wordamount))
|
|
|
|
wordamount = 1;
|
|
|
|
|
|
|
|
*amount = (pat->content.NumRange.max_n -
|
|
|
|
pat->content.NumRange.min_n + 1) *
|
|
|
|
wordamount;
|
|
|
|
|
|
|
|
return GLOB_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
/*printf("error: illegal character in range specification at pos %d\n",
|
|
|
|
pos);*/
|
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2001-01-23 05:14:43 -05:00
|
|
|
GlobCode glob_word(URLGlob *glob, char *pattern, int 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
|
|
|
|
*/
|
2000-12-06 05:10:31 -05:00
|
|
|
char* buf = glob->glob_buffer;
|
1999-12-29 09:20:26 -05:00
|
|
|
int litindex;
|
|
|
|
|
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 != '[') {
|
|
|
|
if (*pattern == '}' || *pattern == ']') {
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
if (*pattern == '\\') { /* escape character, skip '\' */
|
|
|
|
++pattern;
|
|
|
|
++pos;
|
|
|
|
if (*pattern == '\0') { /* but no escaping of '\0'! */
|
2001-01-23 05:14:43 -05:00
|
|
|
return GLOB_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*buf++ = *pattern++; /* copy character to literal */
|
|
|
|
++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);
|
|
|
|
++glob->size;
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
switch (*pattern) {
|
|
|
|
case '\0':
|
|
|
|
return GLOB_OK; /* singular URL processed */
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
/* process set pattern */
|
|
|
|
return glob_set(glob, ++pattern, ++pos, amount);
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
/* process range pattern */
|
|
|
|
return glob_range(glob, ++pattern, ++pos, amount);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-01-23 05:14:43 -05:00
|
|
|
|
|
|
|
return GLOB_ERROR; /* something got wrong */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2000-05-09 08:29:28 -04:00
|
|
|
int glob_url(URLGlob** glob, char* url, int *urlnum)
|
|
|
|
{
|
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
|
|
|
|
2000-11-20 03:54:32 -05:00
|
|
|
if(NULL == glob_buffer)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
glob_expand = (URLGlob*)malloc(sizeof(URLGlob));
|
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 {
|
|
|
|
/* it failed, we cleanup */
|
|
|
|
free(glob_buffer);
|
|
|
|
free(glob_expand);
|
|
|
|
glob_expand = NULL;
|
|
|
|
*urlnum = 1;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2000-10-12 05:12:24 -04:00
|
|
|
int i, elem;
|
|
|
|
|
|
|
|
for (i = glob->size - 1; i >= 0; --i) {
|
|
|
|
if (!(i & 1)) { /* even indexes contain literals */
|
|
|
|
free(glob->literal[i/2]);
|
|
|
|
} else { /* odd indexes contain sets or ranges */
|
|
|
|
if (glob->pattern[i/2].type == UPTSet) {
|
|
|
|
for (elem = glob->pattern[i/2].content.Set.size - 1; elem >= 0; --elem) {
|
|
|
|
free(glob->pattern[i/2].content.Set.elements[elem]);
|
|
|
|
}
|
|
|
|
free(glob->pattern[i/2].content.Set.elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
free(glob->glob_buffer);
|
2000-10-12 05:12:24 -04:00
|
|
|
free(glob);
|
|
|
|
}
|
|
|
|
|
2000-05-09 08:29:28 -04:00
|
|
|
char *next_url(URLGlob *glob)
|
|
|
|
{
|
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;
|
|
|
|
signed int i;
|
|
|
|
int carry;
|
|
|
|
|
2001-01-08 02:37:44 -05:00
|
|
|
if (!glob->beenhere)
|
|
|
|
glob->beenhere = 1;
|
1999-12-29 09:20:26 -05:00
|
|
|
else {
|
|
|
|
carry = 1;
|
|
|
|
|
|
|
|
/* implement a counter over the index ranges of all patterns,
|
|
|
|
starting with the rightmost pattern */
|
|
|
|
for (i = glob->size / 2 - 1; carry && i >= 0; --i) {
|
|
|
|
carry = 0;
|
|
|
|
pat = &glob->pattern[i];
|
|
|
|
switch (pat->type) {
|
|
|
|
case UPTSet:
|
|
|
|
if (++pat->content.Set.ptr_s == pat->content.Set.size) {
|
|
|
|
pat->content.Set.ptr_s = 0;
|
|
|
|
carry = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UPTCharRange:
|
|
|
|
if (++pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
|
|
|
|
pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
|
|
|
|
carry = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UPTNumRange:
|
|
|
|
if (++pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
|
|
|
|
pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
|
|
|
|
carry = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("internal error: invalid pattern type (%d)\n", pat->type);
|
2000-05-22 10:12:12 -04:00
|
|
|
exit (CURLE_FAILED_INIT);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (carry) /* first pattern ptr has run into overflow, done! */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < glob->size; ++i) {
|
|
|
|
if (!(i % 2)) { /* every other term (i even) is a literal */
|
|
|
|
lit = glob->literal[i/2];
|
|
|
|
strcpy(buf, lit);
|
|
|
|
buf += strlen(lit);
|
|
|
|
}
|
|
|
|
else { /* the rest (i odd) are patterns */
|
|
|
|
pat = &glob->pattern[i/2];
|
|
|
|
switch(pat->type) {
|
|
|
|
case UPTSet:
|
|
|
|
strcpy(buf, pat->content.Set.elements[pat->content.Set.ptr_s]);
|
|
|
|
buf += strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
|
|
|
|
break;
|
|
|
|
case UPTCharRange:
|
|
|
|
*buf++ = pat->content.CharRange.ptr_c;
|
|
|
|
break;
|
|
|
|
case UPTNumRange:
|
2000-02-07 19:27:21 -05:00
|
|
|
sprintf(buf, "%0*d", pat->content.NumRange.padlength, pat->content.NumRange.ptr_n);
|
|
|
|
buf += strlen(buf); /* make no sprint() return code assumptions */
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("internal error: invalid pattern type (%d)\n", pat->type);
|
2000-05-22 10:12:12 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-12-06 05:10:31 -05:00
|
|
|
char *match_url(char *filename, URLGlob *glob)
|
|
|
|
{
|
|
|
|
char *target;
|
1999-12-29 09:20:26 -05:00
|
|
|
URLPattern pat;
|
|
|
|
int i;
|
2000-12-06 05:10:31 -05:00
|
|
|
int allocsize;
|
|
|
|
int stringlen=0;
|
|
|
|
char numbuf[18];
|
|
|
|
char *appendthis;
|
2001-08-14 05:16:46 -04:00
|
|
|
int appendlen;
|
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
|
|
|
|
|
|
|
while (*filename != '\0') {
|
|
|
|
if (*filename == '#') {
|
|
|
|
if (!isdigit((int)*++filename) ||
|
|
|
|
*filename == '0') { /* only '#1' ... '#9' allowed */
|
2000-12-06 05:10:31 -05:00
|
|
|
/* printf("illegal matching expression\n");
|
|
|
|
exit(CURLE_URL_MALFORMAT);*/
|
|
|
|
continue;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
i = *filename - '1';
|
2000-12-06 05:10:31 -05:00
|
|
|
if (i + 1 > glob->size / 2) {
|
|
|
|
/*printf("match against nonexisting pattern\n");
|
|
|
|
exit(CURLE_URL_MALFORMAT);*/
|
|
|
|
continue;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2000-12-06 05:10:31 -05:00
|
|
|
pat = glob->pattern[i];
|
1999-12-29 09:20:26 -05:00
|
|
|
switch (pat.type) {
|
|
|
|
case UPTSet:
|
2000-12-06 05:10:31 -05:00
|
|
|
appendthis = pat.content.Set.elements[pat.content.Set.ptr_s];
|
2001-08-14 05:16:46 -04:00
|
|
|
appendlen = (int)strlen(pat.content.Set.elements[pat.content.Set.ptr_s]);
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case UPTCharRange:
|
2000-12-06 05:10:31 -05:00
|
|
|
numbuf[0]=pat.content.CharRange.ptr_c;
|
|
|
|
numbuf[1]=0;
|
|
|
|
appendthis=numbuf;
|
|
|
|
appendlen=1;
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case UPTNumRange:
|
2000-12-06 05:10:31 -05:00
|
|
|
sprintf(numbuf, "%0*d", pat.content.NumRange.padlength, pat.content.NumRange.ptr_n);
|
|
|
|
appendthis = numbuf;
|
2001-08-14 05:16:46 -04:00
|
|
|
appendlen = (int)strlen(numbuf);
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("internal error: invalid pattern type (%d)\n", pat.type);
|
2000-12-06 05:10:31 -05:00
|
|
|
return NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
++filename;
|
|
|
|
}
|
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
|
|
|
}
|