Skip to content

Commit

Permalink
cleanup from bagder comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmealey committed Oct 29, 2023
1 parent 33c4509 commit 21e3193
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static void warnf(char *fmt, ...)
/* make sure to terminate the JSON array */ \
if(o->jsonout) \
printf("%s]\n", o->urls ? "\n" : ""); \
trurl_errorf(o, exit_code, __VA_ARGS__); \
errorf(o, exit_code, __VA_ARGS__); \
} \
} while(0)

Expand Down Expand Up @@ -290,21 +290,15 @@ static void trurl_cleanup_options(struct option *o)
{
if(!o)
return;
if(o->url_list)
curl_slist_free_all(o->url_list);
if(o->set_list)
curl_slist_free_all(o->set_list);
if(o->iter_list)
curl_slist_free_all(o->iter_list);
if(o->append_query)
curl_slist_free_all(o->append_query);
if(o->trim_list)
curl_slist_free_all(o->trim_list);
if(o->append_path)
curl_slist_free_all(o->append_path);
curl_slist_free_all(o->url_list);
curl_slist_free_all(o->set_list);
curl_slist_free_all(o->iter_list);
curl_slist_free_all(o->append_query);
curl_slist_free_all(o->trim_list);
curl_slist_free_all(o->append_path);
}

static void trurl_errorf(struct option *o, int exit_code, char *fmt, ...)
static void errorf(struct option *o, int exit_code, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
Expand Down Expand Up @@ -336,11 +330,11 @@ static void urlfile(struct option *o, const char *file)
{
FILE *f;
if(o->url)
trurl_errorf(o, ERROR_FLAG, "only one --url-file is supported");
errorf(o, ERROR_FLAG, "only one --url-file is supported");
if(strcmp("-", file)) {
f = fopen(file, "rt");
if(!f)
trurl_errorf(o, ERROR_FILE, "--url-file %s not found", file);
errorf(o, ERROR_FILE, "--url-file %s not found", file);
o->urlopen = true;
}
else
Expand Down Expand Up @@ -393,7 +387,7 @@ static void appendadd(struct option *o,
else if(!strncmp("query=", arg, 6))
queryadd(o, arg + 6);
else
trurl_errorf(o, ERROR_APPEND, "--append unsupported component: %s", arg);
errorf(o, ERROR_APPEND, "--append unsupported component: %s", arg);
}

static void setadd(struct option *o,
Expand Down Expand Up @@ -429,7 +423,7 @@ static bool checkoptarg(struct option *o, const char *str,
{
if(!strcmp(str, given)) {
if(!arg)
trurl_errorf(o, ERROR_ARG, "Missing argument for %s", str);
errorf(o, ERROR_ARG, "Missing argument for %s", str);
return true;
}
return false;
Expand Down Expand Up @@ -471,15 +465,15 @@ static int getarg(struct option *o,
}
else if(checkoptarg(o, "--redirect", flag, arg)) {
if(o->redirect)
trurl_errorf(o, ERROR_FLAG, "only one --redirect is supported");
errorf(o, ERROR_FLAG, "only one --redirect is supported");
o->redirect = arg;
*usedarg = true;
}
else if(checkoptarg(o, "--query-separator", flag, arg)) {
if(o->qsep)
trurl_errorf(o, ERROR_FLAG, "only one --query-separator is supported");
errorf(o, ERROR_FLAG, "only one --query-separator is supported");
if(strlen(arg) != 1)
trurl_errorf(o, ERROR_FLAG,
errorf(o, ERROR_FLAG,
"only single-letter query separators are supported");
o->qsep = arg;
*usedarg = true;
Expand All @@ -490,16 +484,16 @@ static int getarg(struct option *o,
}
else if(checkoptarg("-g", flag, arg) || checkoptarg("--get", flag, arg)) {
if(o->format)
trurl_errorf(o, ERROR_FLAG, "only one --get is supported");
errorf(o, ERROR_FLAG, "only one --get is supported");
if(o->jsonout)
trurl_errorf(o, ERROR_FLAG,
errorf(o, ERROR_FLAG,
"--get is mututally exclusive with --json");
o->format = arg;
*usedarg = true;
}
else if(!strcmp("--json", flag)) {
if(o->format)
trurl_errorf(o, ERROR_FLAG, "--json is mututally exclusive with --get");
errorf(o, ERROR_FLAG, "--json is mututally exclusive with --get");
o->jsonout = true;
}
else if(!strcmp("--verify", flag))
Expand Down Expand Up @@ -713,7 +707,7 @@ static void get(struct option *o, CURLU *uh)
queryall);
}
else if(!vlen)
trurl_errorf(o, ERROR_GET, "Bad --get syntax: %s", start);
errorf(o, ERROR_GET, "Bad --get syntax: %s", start);
else if(!strncmp(ptr, "url", vlen))
showurl(stream, o, mods, uh);
else {
Expand Down Expand Up @@ -808,11 +802,11 @@ static const struct var *setone(struct option *o,
found = true;
}
if(!found)
trurl_errorf(o, ERROR_SET,
errorf(o, ERROR_SET,
"unknown component: %.*s", (int)vlen, setline);
}
else
trurl_errorf(o, ERROR_SET, "invalid --set syntax: %s", setline);
errorf(o, ERROR_SET, "invalid --set syntax: %s", setline);
return v;
}

Expand All @@ -827,7 +821,7 @@ static unsigned int set(CURLU *uh,
v = setone(o, uh, setline);
if(v) {
if(mask & (1 << v->part))
trurl_errorf(o, ERROR_SET,
errorf(o, ERROR_SET,
"duplicate --set for component %s", v->name);
mask |= (1 << v->part);
}
Expand Down Expand Up @@ -944,7 +938,7 @@ static void trim(struct option *o)
char *instr = node->data;
if(strncmp(instr, "query", 5))
/* for now we can only trim query components */
trurl_errorf(o, ERROR_TRIM, "Unsupported trim component: %s", instr);
errorf(o, ERROR_TRIM, "Unsupported trim component: %s", instr);
char *ptr = strchr(instr, '=');
if(ptr && (ptr > instr)) {
/* 'ptr' should be a fixed string or a pattern ending with an
Expand Down Expand Up @@ -1203,7 +1197,7 @@ static void singleurl(struct option *o,
if(!uh) {
uh = curl_url();
if(!uh)
trurl_errorf(o, ERROR_MEM, "out of memory");
errorf(o, ERROR_MEM, "out of memory");
if(url) {
CURLUcode rc = seturl(o, uh, url);
if(rc) {
Expand Down Expand Up @@ -1245,7 +1239,7 @@ static void singleurl(struct option *o,
part = iter->data;
sep = strchr(part, '=');
if(!sep)
trurl_errorf(o, ERROR_ITER, "wrong iterate syntax");
errorf(o, ERROR_ITER, "wrong iterate syntax");
plen = sep - part;
if(sep[-1] == ':') {
urlencode = false;
Expand All @@ -1258,16 +1252,16 @@ static void singleurl(struct option *o,
v = comp2var(part, plen);
if(!v) {
curl_url_cleanup(uh);
trurl_errorf(o, ERROR_ITER, "bad component for iterate");
errorf(o, ERROR_ITER, "bad component for iterate");
}
if(iinfo->varmask & (1<<v->part)) {
curl_url_cleanup(uh);
trurl_errorf(o, ERROR_ITER,
errorf(o, ERROR_ITER,
"duplicate component for iterate: %s", v->name);
}
if(setmask & (1 << v->part)) {
curl_url_cleanup(uh);
trurl_errorf(o, ERROR_ITER,
errorf(o, ERROR_ITER,
"duplicate --iterate and --set for component %s",
v->name);
}
Expand Down Expand Up @@ -1416,7 +1410,7 @@ int main(int argc, const char **argv)
if(!o.end_of_options && argv[0][0] == '-') {
/* dash-dash prefixed */
if(getarg(&o, argv[0], argv[1], &usedarg))
trurl_errorf(&o, ERROR_FLAG, "unknown option: %s", argv[0]);
errorf(&o, ERROR_FLAG, "unknown option: %s", argv[0]);
}
else {
/* this is a URL */
Expand Down

0 comments on commit 21e3193

Please sign in to comment.