common.h
author Tero Marttila <terom@fixme.fi>
Tue, 17 Jun 2008 16:39:55 +0300
changeset 19 d18606bb6f20
parent 15 e7f0697814dc
child 20 7512207c9041
permissions -rw-r--r--
a working threaded sliced render, plus modifications to other modules to use this in web_main

committer: Tero Marttila <terom@fixme.fi>

/*
 * error handling
 */

void _generic_err (const char *func, int perr, const char *fmt, ...)
        __attribute__ ((format (printf, 3, 4)));

// needs to be defined as its own function for the noreturn attribute
void _generic_err_exit (const char *func, int perr, const char *fmt, ...)
        __attribute__ ((format (printf, 3, 4)))
        __attribute__ ((noreturn));


// various kinds of ways to handle an error, 2**3 of them, *g*
#define error(...)                  _generic_err(       NULL, 0, __VA_ARGS__)
#define err_exit(...)               _generic_err_exit(  NULL, 0, __VA_ARGS__)
#define perr(...)                   _generic_err(       NULL, 1, __VA_ARGS__)
#define perr_exit(...)              _generic_err_exit(  NULL, 1, __VA_ARGS__)
#define err_func(func, ...)         _generic_err(       func, 0, __VA_ARGS__)
#define err_func_exit(func, ...)    _generic_err_exit(  func, 0, __VA_ARGS__)
#define perr_func(func, ...)        _generic_err(       func, 1, __VA_ARGS__)
#define perr_func_exit(func, ...)   _generic_err_exit(  func, 1, __VA_ARGS__)

// error(func + colon + msg, ...) + goto error
#define ERROR(...) do { err_func(__func__, __VA_ARGS__); goto error; } while (0)
#define PERROR(...) do { perr_func(__func__, __VA_ARGS__); goto error; } while (0)
#define FATAL(...) err_func_exit(__func__, __VA_ARGS__)
#define PFATAL(...) perr_func_exit(__func__, __VA_ARGS__)
#define WARNING(...) err_func(__func__, __VA_ARGS__)
#define PWARNING(...) perr_func(__func__, __VA_ARGS__)

/*
 * Parse a host:port string.
 *
 * Valid formats:
 *  host
 *  host:port
 *  [host]
 *  [host]:port
 *
 * The contents of the given hostport string *will* be modified.
 *
 * The value of *port will be set to NULL if no port was given.
 *
 * Returns 0 and sets *host if succesfull, nonzero otherwise.
 *
 */
int parse_hostport (char *hostport, char **host, char **port);