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