blob: d76d1c0ff98fc18d46f53ce4014459d5e6b5281b [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Namhyung Kim5f9273d2011-12-13 22:52:03 +09002 * usage.c
3 *
4 * Various reporting routines.
5 * Originally copied from GIT source.
Ingo Molnar07800602009-04-20 15:00:56 +02006 *
7 * Copyright (C) Linus Torvalds, 2005
8 */
9#include "util.h"
10
11static void report(const char *prefix, const char *err, va_list params)
12{
13 char msg[1024];
14 vsnprintf(msg, sizeof(msg), err, params);
Ingo Molnar716c69f2009-06-07 17:31:52 +020015 fprintf(stderr, " %s%s\n", prefix, msg);
Ingo Molnar07800602009-04-20 15:00:56 +020016}
17
18static NORETURN void usage_builtin(const char *err)
19{
Ingo Molnar716c69f2009-06-07 17:31:52 +020020 fprintf(stderr, "\n Usage: %s\n", err);
Ingo Molnar07800602009-04-20 15:00:56 +020021 exit(129);
22}
23
24static NORETURN void die_builtin(const char *err, va_list params)
25{
Ingo Molnar716c69f2009-06-07 17:31:52 +020026 report(" Fatal: ", err, params);
Ingo Molnar07800602009-04-20 15:00:56 +020027 exit(128);
28}
29
30static void error_builtin(const char *err, va_list params)
31{
Ingo Molnar716c69f2009-06-07 17:31:52 +020032 report(" Error: ", err, params);
Ingo Molnar07800602009-04-20 15:00:56 +020033}
34
35static void warn_builtin(const char *warn, va_list params)
36{
Ingo Molnar716c69f2009-06-07 17:31:52 +020037 report(" Warning: ", warn, params);
Ingo Molnar07800602009-04-20 15:00:56 +020038}
39
40/* If we are in a dlopen()ed .so write to a global variable would segfault
41 * (ugh), so keep things static. */
42static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
43static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
44static void (*error_routine)(const char *err, va_list params) = error_builtin;
45static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
46
47void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
48{
49 die_routine = routine;
50}
51
52void usage(const char *err)
53{
54 usage_routine(err);
55}
56
57void die(const char *err, ...)
58{
59 va_list params;
60
61 va_start(params, err);
62 die_routine(err, params);
63 va_end(params);
64}
65
66int error(const char *err, ...)
67{
68 va_list params;
69
70 va_start(params, err);
71 error_routine(err, params);
72 va_end(params);
73 return -1;
74}
75
76void warning(const char *warn, ...)
77{
78 va_list params;
79
80 va_start(params, warn);
81 warn_routine(warn, params);
82 va_end(params);
83}