Skip to content

Commit

Permalink
tool_urlglob: unify return codes to use CURLcode
Browse files Browse the repository at this point in the history
There was a mix of GlobCode, CURLcode and ints and they were mostly
passing around CURLcode errors. This change makes the functions use only
CURLcode and removes the GlobCode type completely.
  • Loading branch information
bagder committed Dec 16, 2014
1 parent 9b61060 commit b0670ff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/tool_operate.c
Expand Up @@ -398,7 +398,7 @@ static CURLcode operate_do(struct GlobalConfig *global,

if(!config->globoff && infiles) {
/* Unless explicitly shut off */
res = (CURLcode) glob_url(&inglob, infiles, &infilenum,
res = glob_url(&inglob, infiles, &infilenum,
global->showerror?global->errors:NULL);
if(res) {
Curl_safefree(outfiles);
Expand Down Expand Up @@ -449,7 +449,7 @@ static CURLcode operate_do(struct GlobalConfig *global,
if(!config->globoff) {
/* Unless explicitly shut off, we expand '{...}' and '[...]'
expressions and return total number of URLs in pattern set */
res = (CURLcode) glob_url(&urls, urlnode->url, &urlnum,
res = glob_url(&urls, urlnode->url, &urlnum,
global->showerror?global->errors:NULL);
if(res) {
Curl_safefree(uploadfile);
Expand Down
67 changes: 32 additions & 35 deletions src/tool_urlglob.c
Expand Up @@ -29,18 +29,12 @@

#include "memdebug.h" /* keep this as LAST include */

typedef enum {
GLOB_OK,
GLOB_NO_MEM = CURLE_OUT_OF_MEMORY,
GLOB_ERROR = CURLE_URL_MALFORMAT
} GlobCode;

#define GLOBERROR(string, column, code) \
glob->error = string, glob->pos = column, code

void glob_cleanup(URLGlob* glob);

static GlobCode glob_fixed(URLGlob *glob, char *fixed, size_t len)
static CURLcode glob_fixed(URLGlob *glob, char *fixed, size_t len)
{
URLPattern *pat = &glob->pattern[glob->size];
pat->type = UPTSet;
Expand All @@ -51,16 +45,16 @@ static GlobCode glob_fixed(URLGlob *glob, char *fixed, size_t len)
pat->content.Set.elements = malloc(sizeof(char*));

if(!pat->content.Set.elements)
return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);

pat->content.Set.elements[0] = malloc(len+1);
if(!pat->content.Set.elements[0])
return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);

memcpy(pat->content.Set.elements[0], fixed, len);
pat->content.Set.elements[0][len] = 0;

return GLOB_OK;
return CURLE_OK;
}

/* multiply
Expand All @@ -76,7 +70,7 @@ static int multiply(unsigned long *amount, long with)
return 0;
}

static GlobCode glob_set(URLGlob *glob, char **patternp,
static CURLcode glob_set(URLGlob *glob, char **patternp,
size_t *posp, unsigned long *amount,
int globindex)
{
Expand All @@ -101,19 +95,20 @@ static GlobCode glob_set(URLGlob *glob, char **patternp,
while(!done) {
switch (*pattern) {
case '\0': /* URL ended while set was still open */
return GLOBERROR("unmatched brace", opos, GLOB_ERROR);
return GLOBERROR("unmatched brace", opos, CURLE_URL_MALFORMAT);

case '{':
case '[': /* no nested expressions at this time */
return GLOBERROR("nested brace", *posp, GLOB_ERROR);
return GLOBERROR("nested brace", *posp, CURLE_URL_MALFORMAT);

case '}': /* set element completed */
if(opattern == pattern)
return GLOBERROR("empty string within braces", *posp, GLOB_ERROR);
return GLOBERROR("empty string within braces", *posp,
CURLE_URL_MALFORMAT);

/* add 1 to size since it'll be incremented below */
if(multiply(amount, pat->content.Set.size+1))
return GLOBERROR("range overflow", 0, GLOB_ERROR);
return GLOBERROR("range overflow", 0, CURLE_URL_MALFORMAT);

/* fall-through */
case ',':
Expand All @@ -123,20 +118,20 @@ static GlobCode glob_set(URLGlob *glob, char **patternp,
char **new_arr = realloc(pat->content.Set.elements,
(pat->content.Set.size + 1) * sizeof(char*));
if(!new_arr)
return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);

pat->content.Set.elements = new_arr;
}
else
pat->content.Set.elements = malloc(sizeof(char*));

if(!pat->content.Set.elements)
return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);

pat->content.Set.elements[pat->content.Set.size] =
strdup(glob->glob_buffer);
if(!pat->content.Set.elements[pat->content.Set.size])
return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
++pat->content.Set.size;

if(*pattern == '}') {
Expand All @@ -151,7 +146,7 @@ static GlobCode glob_set(URLGlob *glob, char **patternp,
break;

case ']': /* illegal closing bracket */
return GLOBERROR("unexpected close bracket", *posp, GLOB_ERROR);
return GLOBERROR("unexpected close bracket", *posp, CURLE_URL_MALFORMAT);

case '\\': /* escaped character, skip '\' */
if(pattern[1]) {
Expand All @@ -166,10 +161,10 @@ static GlobCode glob_set(URLGlob *glob, char **patternp,
}

*patternp = pattern; /* return with the new position */
return GLOB_OK;
return CURLE_OK;
}

static GlobCode glob_range(URLGlob *glob, char **patternp,
static CURLcode glob_range(URLGlob *glob, char **patternp,
size_t *posp, unsigned long *amount,
int globindex)
{
Expand Down Expand Up @@ -219,7 +214,7 @@ static GlobCode glob_range(URLGlob *glob, char **patternp,
if((rc != 2) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a')) ||
(step < 0) )
/* the pattern is not well-formed */
return GLOBERROR("bad range", *posp, GLOB_ERROR);
return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);

/* if there was a ":[num]" thing, use that as step or else use 1 */
pat->content.CharRange.step = step;
Expand All @@ -228,7 +223,7 @@ static GlobCode glob_range(URLGlob *glob, char **patternp,

if(multiply(amount, (pat->content.CharRange.max_c -
pat->content.CharRange.min_c + 1)))
return GLOBERROR("range overflow", *posp, GLOB_ERROR);
return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
}
else if(ISDIGIT(*pattern)) {
/* numeric range detected */
Expand Down Expand Up @@ -283,7 +278,7 @@ static GlobCode glob_range(URLGlob *glob, char **patternp,

if(!endp || (min_n > max_n) || (step_n > (max_n - min_n)))
/* the pattern is not well-formed */
return GLOBERROR("bad range", *posp, GLOB_ERROR);
return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);

/* typecasting to ints are fine here since we make sure above that we
are within 31 bits */
Expand All @@ -293,13 +288,13 @@ static GlobCode glob_range(URLGlob *glob, char **patternp,

if(multiply(amount, (pat->content.NumRange.max_n -
pat->content.NumRange.min_n + 1)))
return GLOBERROR("range overflow", *posp, GLOB_ERROR);
return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
}
else
return GLOBERROR("bad range specification", *posp, GLOB_ERROR);
return GLOBERROR("bad range specification", *posp, CURLE_URL_MALFORMAT);

*patternp = pattern;
return GLOB_OK;
return CURLE_OK;
}

static bool peek_ipv6(const char *str, size_t *skip)
Expand Down Expand Up @@ -332,13 +327,13 @@ static bool peek_ipv6(const char *str, size_t *skip)
}
}

static GlobCode glob_parse(URLGlob *glob, char *pattern,
static CURLcode glob_parse(URLGlob *glob, char *pattern,
size_t pos, unsigned long *amount)
{
/* processes a literal string component of a URL
special characters '{' and '[' branch to set/range processing functions
*/
GlobCode res = GLOB_OK;
CURLcode res = CURLE_OK;
int globindex = 0; /* count "actual" globs */

*amount = 1;
Expand All @@ -360,7 +355,8 @@ static GlobCode glob_parse(URLGlob *glob, char *pattern,
break;
}
if(*pattern == '}' || *pattern == ']')
return GLOBERROR("unmatched close brace/bracket", pos, GLOB_ERROR);
return GLOBERROR("unmatched close brace/bracket", pos,
CURLE_URL_MALFORMAT);

/* only allow \ to escape known "special letters" */
if(*pattern == '\\' &&
Expand Down Expand Up @@ -402,12 +398,13 @@ static GlobCode glob_parse(URLGlob *glob, char *pattern,
}

if(++glob->size > GLOB_PATTERN_NUM)
return GLOBERROR("too many globs", pos, GLOB_ERROR);
return GLOBERROR("too many globs", pos, CURLE_URL_MALFORMAT);
}
return res;
}

int glob_url(URLGlob** glob, char* url, unsigned long *urlnum, FILE *error)
CURLcode glob_url(URLGlob** glob, char* url, unsigned long *urlnum,
FILE *error)
{
/*
* We can deal with any-size, just make a buffer with the same length
Expand All @@ -416,7 +413,7 @@ int glob_url(URLGlob** glob, char* url, unsigned long *urlnum, FILE *error)
URLGlob *glob_expand;
unsigned long amount = 0;
char *glob_buffer;
GlobCode res;
CURLcode res;

*glob = NULL;

Expand Down Expand Up @@ -480,7 +477,7 @@ void glob_cleanup(URLGlob* glob)
Curl_safefree(glob);
}

int glob_next_url(char **globbed, URLGlob *glob)
CURLcode glob_next_url(char **globbed, URLGlob *glob)
{
URLPattern *pat;
size_t i;
Expand Down Expand Up @@ -571,7 +568,7 @@ int glob_next_url(char **globbed, URLGlob *glob)
return CURLE_OK;
}

int glob_match_url(char **result, char *filename, URLGlob *glob)
CURLcode glob_match_url(char **result, char *filename, URLGlob *glob)
{
char *target;
size_t allocsize;
Expand Down
8 changes: 4 additions & 4 deletions src/tool_urlglob.h
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -68,9 +68,9 @@ typedef struct {
size_t pos; /* column position of error or 0 */
} URLGlob;

int glob_url(URLGlob**, char*, unsigned long *, FILE *);
int glob_next_url(char **, URLGlob *);
int glob_match_url(char **, char*, URLGlob *);
CURLcode glob_url(URLGlob**, char*, unsigned long *, FILE *);
CURLcode glob_next_url(char **, URLGlob *);
CURLcode glob_match_url(char **, char*, URLGlob *);
void glob_cleanup(URLGlob* glob);

#endif /* HEADER_CURL_TOOL_URLGLOB_H */
Expand Down

0 comments on commit b0670ff

Please sign in to comment.