usage.con commit Merge branch 'oh/fix-config-default-user-name-section' into maint-2.3 (1add9ae)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "git-compat-util.h"
   7#include "cache.h"
   8
   9void vreportf(const char *prefix, const char *err, va_list params)
  10{
  11        char msg[4096];
  12        vsnprintf(msg, sizeof(msg), err, params);
  13        fprintf(stderr, "%s%s\n", prefix, msg);
  14}
  15
  16void vwritef(int fd, const char *prefix, const char *err, va_list params)
  17{
  18        char msg[4096];
  19        int len = vsnprintf(msg, sizeof(msg), err, params);
  20        if (len > sizeof(msg))
  21                len = sizeof(msg);
  22
  23        write_in_full(fd, prefix, strlen(prefix));
  24        write_in_full(fd, msg, len);
  25        write_in_full(fd, "\n", 1);
  26}
  27
  28static NORETURN void usage_builtin(const char *err, va_list params)
  29{
  30        vreportf("usage: ", err, params);
  31        exit(129);
  32}
  33
  34static NORETURN void die_builtin(const char *err, va_list params)
  35{
  36        vreportf("fatal: ", err, params);
  37        exit(128);
  38}
  39
  40static void error_builtin(const char *err, va_list params)
  41{
  42        vreportf("error: ", err, params);
  43}
  44
  45static void warn_builtin(const char *warn, va_list params)
  46{
  47        vreportf("warning: ", warn, params);
  48}
  49
  50static int die_is_recursing_builtin(void)
  51{
  52        static int dying;
  53        return dying++;
  54}
  55
  56/* If we are in a dlopen()ed .so write to a global variable would segfault
  57 * (ugh), so keep things static. */
  58static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
  59static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
  60static void (*error_routine)(const char *err, va_list params) = error_builtin;
  61static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
  62static int (*die_is_recursing)(void) = die_is_recursing_builtin;
  63
  64void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
  65{
  66        die_routine = routine;
  67}
  68
  69void set_error_routine(void (*routine)(const char *err, va_list params))
  70{
  71        error_routine = routine;
  72}
  73
  74void set_die_is_recursing_routine(int (*routine)(void))
  75{
  76        die_is_recursing = routine;
  77}
  78
  79void NORETURN usagef(const char *err, ...)
  80{
  81        va_list params;
  82
  83        va_start(params, err);
  84        usage_routine(err, params);
  85        va_end(params);
  86}
  87
  88void NORETURN usage(const char *err)
  89{
  90        usagef("%s", err);
  91}
  92
  93void NORETURN die(const char *err, ...)
  94{
  95        va_list params;
  96
  97        if (die_is_recursing()) {
  98                fputs("fatal: recursion detected in die handler\n", stderr);
  99                exit(128);
 100        }
 101
 102        va_start(params, err);
 103        die_routine(err, params);
 104        va_end(params);
 105}
 106
 107void NORETURN die_errno(const char *fmt, ...)
 108{
 109        va_list params;
 110        char fmt_with_err[1024];
 111        char str_error[256], *err;
 112        int i, j;
 113
 114        if (die_is_recursing()) {
 115                fputs("fatal: recursion detected in die_errno handler\n",
 116                        stderr);
 117                exit(128);
 118        }
 119
 120        err = strerror(errno);
 121        for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
 122                if ((str_error[j++] = err[i++]) != '%')
 123                        continue;
 124                if (j < sizeof(str_error) - 1) {
 125                        str_error[j++] = '%';
 126                } else {
 127                        /* No room to double the '%', so we overwrite it with
 128                         * '\0' below */
 129                        j--;
 130                        break;
 131                }
 132        }
 133        str_error[j] = 0;
 134        snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
 135
 136        va_start(params, fmt);
 137        die_routine(fmt_with_err, params);
 138        va_end(params);
 139}
 140
 141#undef error
 142int error(const char *err, ...)
 143{
 144        va_list params;
 145
 146        va_start(params, err);
 147        error_routine(err, params);
 148        va_end(params);
 149        return -1;
 150}
 151
 152void warning(const char *warn, ...)
 153{
 154        va_list params;
 155
 156        va_start(params, warn);
 157        warn_routine(warn, params);
 158        va_end(params);
 159}