usage.con commit ref-filter: move get_head_description() from branch.c (d4919bb)
   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 (*get_error_routine(void))(const char *err, va_list params)
  74{
  75        return error_routine;
  76}
  77
  78void set_warn_routine(void (*routine)(const char *warn, va_list params))
  79{
  80        warn_routine = routine;
  81}
  82
  83void (*get_warn_routine(void))(const char *warn, va_list params)
  84{
  85        return warn_routine;
  86}
  87
  88void set_die_is_recursing_routine(int (*routine)(void))
  89{
  90        die_is_recursing = routine;
  91}
  92
  93void set_error_handle(FILE *fh)
  94{
  95        error_handle = fh;
  96        tweaked_error_buffering = 0;
  97}
  98
  99void NORETURN usagef(const char *err, ...)
 100{
 101        va_list params;
 102
 103        va_start(params, err);
 104        usage_routine(err, params);
 105        va_end(params);
 106}
 107
 108void NORETURN usage(const char *err)
 109{
 110        usagef("%s", err);
 111}
 112
 113void NORETURN die(const char *err, ...)
 114{
 115        va_list params;
 116
 117        if (die_is_recursing()) {
 118                fputs("fatal: recursion detected in die handler\n", stderr);
 119                exit(128);
 120        }
 121
 122        va_start(params, err);
 123        die_routine(err, params);
 124        va_end(params);
 125}
 126
 127static const char *fmt_with_err(char *buf, int n, const char *fmt)
 128{
 129        char str_error[256], *err;
 130        int i, j;
 131
 132        err = strerror(errno);
 133        for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
 134                if ((str_error[j++] = err[i++]) != '%')
 135                        continue;
 136                if (j < sizeof(str_error) - 1) {
 137                        str_error[j++] = '%';
 138                } else {
 139                        /* No room to double the '%', so we overwrite it with
 140                         * '\0' below */
 141                        j--;
 142                        break;
 143                }
 144        }
 145        str_error[j] = 0;
 146        snprintf(buf, n, "%s: %s", fmt, str_error);
 147        return buf;
 148}
 149
 150void NORETURN die_errno(const char *fmt, ...)
 151{
 152        char buf[1024];
 153        va_list params;
 154
 155        if (die_is_recursing()) {
 156                fputs("fatal: recursion detected in die_errno handler\n",
 157                        stderr);
 158                exit(128);
 159        }
 160
 161        va_start(params, fmt);
 162        die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
 163        va_end(params);
 164}
 165
 166#undef error_errno
 167int error_errno(const char *fmt, ...)
 168{
 169        char buf[1024];
 170        va_list params;
 171
 172        va_start(params, fmt);
 173        error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
 174        va_end(params);
 175        return -1;
 176}
 177
 178#undef error
 179int error(const char *err, ...)
 180{
 181        va_list params;
 182
 183        va_start(params, err);
 184        error_routine(err, params);
 185        va_end(params);
 186        return -1;
 187}
 188
 189void warning_errno(const char *warn, ...)
 190{
 191        char buf[1024];
 192        va_list params;
 193
 194        va_start(params, warn);
 195        warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
 196        va_end(params);
 197}
 198
 199void warning(const char *warn, ...)
 200{
 201        va_list params;
 202
 203        va_start(params, warn);
 204        warn_routine(warn, params);
 205        va_end(params);
 206}