usage.c 1.8 KB

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