blob: 996046a66fe51aa7301afda0dfc8396920b59805 [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"
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -020010#include "debug.h"
Ingo Molnar07800602009-04-20 15:00:56 +020011
12static void report(const char *prefix, const char *err, va_list params)
13{
14 char msg[1024];
15 vsnprintf(msg, sizeof(msg), err, params);
Ingo Molnar716c69f2009-06-07 17:31:52 +020016 fprintf(stderr, " %s%s\n", prefix, msg);
Ingo Molnar07800602009-04-20 15:00:56 +020017}
18
19static NORETURN void usage_builtin(const char *err)
20{
Ingo Molnar716c69f2009-06-07 17:31:52 +020021 fprintf(stderr, "\n Usage: %s\n", err);
Ingo Molnar07800602009-04-20 15:00:56 +020022 exit(129);
23}
24
25static NORETURN void die_builtin(const char *err, va_list params)
26{
Ingo Molnar716c69f2009-06-07 17:31:52 +020027 report(" Fatal: ", err, params);
Ingo Molnar07800602009-04-20 15:00:56 +020028 exit(128);
29}
30
31static void error_builtin(const char *err, va_list params)
32{
Ingo Molnar716c69f2009-06-07 17:31:52 +020033 report(" Error: ", err, params);
Ingo Molnar07800602009-04-20 15:00:56 +020034}
35
36static void warn_builtin(const char *warn, va_list params)
37{
Ingo Molnar716c69f2009-06-07 17:31:52 +020038 report(" Warning: ", warn, params);
Ingo Molnar07800602009-04-20 15:00:56 +020039}
40
41/* If we are in a dlopen()ed .so write to a global variable would segfault
42 * (ugh), so keep things static. */
43static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
Ingo Molnar07800602009-04-20 15:00:56 +020044static void (*error_routine)(const char *err, va_list params) = error_builtin;
45static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
46
Namhyung Kim2690c732015-10-20 00:23:49 +090047void set_warning_routine(void (*routine)(const char *err, va_list params))
48{
49 warn_routine = routine;
50}
51
Ingo Molnar07800602009-04-20 15:00:56 +020052void 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);
Arnaldo Carvalho de Meloe4763432016-03-23 12:32:31 -030062 die_builtin(err, params);
Ingo Molnar07800602009-04-20 15:00:56 +020063 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}