Skip to content

Commit

Permalink
avoid using variadic macro
Browse files Browse the repository at this point in the history
This makes it possible to compile trurl with old C89 compilers without
having to pass `-std=gnu99` or similar.
  • Loading branch information
vszakats committed Nov 9, 2023
1 parent 1cf986e commit 65a2e9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ifndef NDEBUG
CFLAGS += -g
endif
MANUAL = trurl.1
#CFLAGS += -pedantic -Wcast-align -Wconversion -Wdeclaration-after-statement -Wdouble-promotion -Wempty-body -Wendif-labels -Wenum-conversion -Wfloat-equal -Wignored-qualifiers -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-format-nonliteral -Wno-long-long -Wno-multichar -Wno-sign-conversion -Wno-system-headers -Wpointer-arith -Wshadow -Wsign-compare -Wstrict-prototypes -Wtype-limits -Wundef -Wunused -Wunused-const-variable -Wvla -Wwrite-strings

PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
Expand Down
68 changes: 43 additions & 25 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,20 @@ static char *curl_url_strerror(CURLUcode error)
}
#endif

static void warnf(char *fmt, ...)
static void warnf_low(char *fmt, va_list ap)
{
va_list ap;
va_start(ap, fmt);
fputs(WARN_PREFIX, stderr);
vfprintf(stderr, fmt, ap);
fputs("\n", stderr);
va_end(ap);
}

#define VERIFY(o, exit_code, ...) \
do { \
if(!o->verify) \
warnf(__VA_ARGS__); \
else { \
/* make sure to terminate the JSON array */ \
if(o->jsonout) \
printf("%s]\n", o->urls ? "\n" : ""); \
errorf(o, exit_code, __VA_ARGS__); \
} \
} while(0)
static void warnf(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
warnf_low(fmt, ap);
va_end(ap);
}

static void help(void)
{
Expand Down Expand Up @@ -300,19 +293,44 @@ static void trurl_cleanup_options(struct option *o)
curl_slist_free_all(o->append_path);
}

static void errorf(struct option *o, int exit_code, char *fmt, ...)
static void errorf_low(char *fmt, va_list ap)
{
va_list ap;
va_start(ap, fmt);
fputs(ERROR_PREFIX, stderr);
vfprintf(stderr, fmt, ap);
fputs("\n" ERROR_PREFIX "Try " PROGNAME " -h for help\n", stderr);
}

static void errorf(struct option *o, int exit_code, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
errorf_low(fmt, ap);
va_end(ap);
trurl_cleanup_options(o);
curl_global_cleanup();
exit(exit_code);
}

static void verify(struct option *o, int exit_code, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if(!o->verify) {
warnf_low(fmt, ap);
va_end(ap);
}
else {
/* make sure to terminate the JSON array */
if(o->jsonout)
printf("%s]\n", o->urls ? "\n" : "");
errorf_low(fmt, ap);
va_end(ap);
trurl_cleanup_options(o);
curl_global_cleanup();
exit(exit_code);
}
}

static char *strurldecode(const char *url, int inlength, int *outlength)
{
return curl_easy_unescape(NULL, inlength ? url : "", inlength,
Expand Down Expand Up @@ -617,7 +635,7 @@ static void showurl(FILE *stream, struct option *o, int modifiers,
CURLUcode rc = geturlpart(o, modifiers, uh, CURLUPART_URL, &url);
if(rc) {
trurl_cleanup_options(o);
VERIFY(o, ERROR_BADURL, "invalid url [%s]", curl_url_strerror(rc));
verify(o, ERROR_BADURL, "invalid url [%s]", curl_url_strerror(rc));
return;
}
fputs(url, stream);
Expand Down Expand Up @@ -889,7 +907,7 @@ static void json(struct option *o, CURLU *uh)
CURLUcode rc = geturlpart(o, 0, uh, CURLUPART_URL, &url);
if(rc) {
trurl_cleanup_options(o);
VERIFY(o, ERROR_BADURL, "invalid url [%s]", curl_url_strerror(rc));
verify(o, ERROR_BADURL, "invalid url [%s]", curl_url_strerror(rc));
return;
}
printf("%s\n {\n \"url\": ", o->urls ? "," : "");
Expand Down Expand Up @@ -1209,14 +1227,14 @@ static void singleurl(struct option *o,
CURLUcode rc = seturl(o, uh, url);
if(rc) {
curl_url_cleanup(uh);
VERIFY(o, ERROR_BADURL, "%s [%s]", curl_url_strerror(rc), url);
verify(o, ERROR_BADURL, "%s [%s]", curl_url_strerror(rc), url);
return;
}
if(o->redirect) {
rc = seturl(o, uh, o->redirect);
if(rc) {
curl_url_cleanup(uh);
VERIFY(o, ERROR_BADURL, "invalid redirection: %s [%s]",
verify(o, ERROR_BADURL, "invalid redirection: %s [%s]",
curl_url_strerror(rc), o->redirect);
return;
}
Expand Down Expand Up @@ -1350,15 +1368,15 @@ static void singleurl(struct option *o,
if(rc) {
if(o->verify) /* only clean up if we're exiting */
curl_url_cleanup(uh);
VERIFY(o, ERROR_URL, "not enough input for a URL");
verify(o, ERROR_URL, "not enough input for a URL");
url_is_invalid = true;
}
else {
rc = seturl(o, uh, ourl);
if(rc) {
if(o->verify) /* only clean up if we're exiting */
curl_url_cleanup(uh);
VERIFY(o, ERROR_BADURL, "%s [%s]", curl_url_strerror(rc),
verify(o, ERROR_BADURL, "%s [%s]", curl_url_strerror(rc),
ourl);
url_is_invalid = true;
}
Expand All @@ -1370,7 +1388,7 @@ static void singleurl(struct option *o,
else {
if(o->verify) /* only clean up if we're exiting */
curl_url_cleanup(uh);
VERIFY(o, ERROR_BADURL, "url became invalid");
verify(o, ERROR_BADURL, "url became invalid");
url_is_invalid = true;
}
}
Expand Down

0 comments on commit 65a2e9a

Please sign in to comment.