186470930SIngo Molnar /* 25f9273d6SNamhyung Kim * usage.c 35f9273d6SNamhyung Kim * 45f9273d6SNamhyung Kim * Various reporting routines. 55f9273d6SNamhyung Kim * Originally copied from GIT source. 686470930SIngo Molnar * 786470930SIngo Molnar * Copyright (C) Linus Torvalds, 2005 886470930SIngo Molnar */ 986470930SIngo Molnar #include "util.h" 100d37aa34SArnaldo Carvalho de Melo #include "debug.h" 1186470930SIngo Molnar 1286470930SIngo Molnar static void report(const char *prefix, const char *err, va_list params) 1386470930SIngo Molnar { 1486470930SIngo Molnar char msg[1024]; 1586470930SIngo Molnar vsnprintf(msg, sizeof(msg), err, params); 1686470930SIngo Molnar fprintf(stderr, " %s%s\n", prefix, msg); 1786470930SIngo Molnar } 1886470930SIngo Molnar 196c346643SArnaldo Carvalho de Melo static __noreturn void usage_builtin(const char *err) 2086470930SIngo Molnar { 21716c69feSIngo Molnar fprintf(stderr, "\n Usage: %s\n", err); 2286470930SIngo Molnar exit(129); 2386470930SIngo Molnar } 2486470930SIngo Molnar 256c346643SArnaldo Carvalho de Melo static __noreturn void die_builtin(const char *err, va_list params) 2686470930SIngo Molnar { 27716c69feSIngo Molnar report(" Fatal: ", err, params); 2886470930SIngo Molnar exit(128); 2986470930SIngo Molnar } 3086470930SIngo Molnar 3186470930SIngo Molnar static void error_builtin(const char *err, va_list params) 3286470930SIngo Molnar { 33716c69feSIngo Molnar report(" Error: ", err, params); 3486470930SIngo Molnar } 3586470930SIngo Molnar 3686470930SIngo Molnar static void warn_builtin(const char *warn, va_list params) 3786470930SIngo Molnar { 38716c69feSIngo Molnar report(" Warning: ", warn, params); 3986470930SIngo Molnar } 4086470930SIngo Molnar 4186470930SIngo Molnar /* If we are in a dlopen()ed .so write to a global variable would segfault 4286470930SIngo Molnar * (ugh), so keep things static. */ 436c346643SArnaldo Carvalho de Melo static void (*usage_routine)(const char *err) __noreturn = usage_builtin; 4486470930SIngo Molnar static void (*error_routine)(const char *err, va_list params) = error_builtin; 4586470930SIngo Molnar static void (*warn_routine)(const char *err, va_list params) = warn_builtin; 4686470930SIngo Molnar 472690c730SNamhyung Kim void set_warning_routine(void (*routine)(const char *err, va_list params)) 482690c730SNamhyung Kim { 492690c730SNamhyung Kim warn_routine = routine; 502690c730SNamhyung Kim } 512690c730SNamhyung Kim 5286470930SIngo Molnar void usage(const char *err) 5386470930SIngo Molnar { 5486470930SIngo Molnar usage_routine(err); 5586470930SIngo Molnar } 5686470930SIngo Molnar 5786470930SIngo Molnar void die(const char *err, ...) 5886470930SIngo Molnar { 5986470930SIngo Molnar va_list params; 6086470930SIngo Molnar 6186470930SIngo Molnar va_start(params, err); 62e4763438SArnaldo Carvalho de Melo die_builtin(err, params); 6386470930SIngo Molnar va_end(params); 6486470930SIngo Molnar } 6586470930SIngo Molnar 6686470930SIngo Molnar int error(const char *err, ...) 6786470930SIngo Molnar { 6886470930SIngo Molnar va_list params; 6986470930SIngo Molnar 7086470930SIngo Molnar va_start(params, err); 7186470930SIngo Molnar error_routine(err, params); 7286470930SIngo Molnar va_end(params); 7386470930SIngo Molnar return -1; 7486470930SIngo Molnar } 7586470930SIngo Molnar 7686470930SIngo Molnar void warning(const char *warn, ...) 7786470930SIngo Molnar { 7886470930SIngo Molnar va_list params; 7986470930SIngo Molnar 8086470930SIngo Molnar va_start(params, warn); 8186470930SIngo Molnar warn_routine(warn, params); 8286470930SIngo Molnar va_end(params); 8386470930SIngo Molnar } 84