usage.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #include "util.h"
  7. static void report(const char *prefix, const char *err, va_list params)
  8. {
  9. char msg[1024];
  10. vsnprintf(msg, sizeof(msg), err, params);
  11. fprintf(stderr, " %s%s\n", prefix, msg);
  12. }
  13. static NORETURN void usage_builtin(const char *err)
  14. {
  15. fprintf(stderr, "\n Usage: %s\n", err);
  16. exit(129);
  17. }
  18. static NORETURN void die_builtin(const char *err, va_list params)
  19. {
  20. report(" Fatal: ", err, params);
  21. exit(128);
  22. }
  23. static void error_builtin(const char *err, va_list params)
  24. {
  25. report(" Error: ", err, params);
  26. }
  27. static void warn_builtin(const char *warn, va_list params)
  28. {
  29. report(" Warning: ", warn, params);
  30. }
  31. /* If we are in a dlopen()ed .so write to a global variable would segfault
  32. * (ugh), so keep things static. */
  33. static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
  34. static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
  35. static void (*error_routine)(const char *err, va_list params) = error_builtin;
  36. static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
  37. void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
  38. {
  39. die_routine = routine;
  40. }
  41. void usage(const char *err)
  42. {
  43. usage_routine(err);
  44. }
  45. void die(const char *err, ...)
  46. {
  47. va_list params;
  48. va_start(params, err);
  49. die_routine(err, params);
  50. va_end(params);
  51. }
  52. int error(const char *err, ...)
  53. {
  54. va_list params;
  55. va_start(params, err);
  56. error_routine(err, params);
  57. va_end(params);
  58. return -1;
  59. }
  60. void warning(const char *warn, ...)
  61. {
  62. va_list params;
  63. va_start(params, warn);
  64. warn_routine(warn, params);
  65. va_end(params);
  66. }