/contrib/groff/src/libs/libgroff/error.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 142 lines · 110 code · 13 blank · 19 comment · 12 complexity · 19d984a04bf64326041d2d0f948a9254 MD5 · raw file

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992, 2003 Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "errarg.h"
  20. #include "error.h"
  21. extern void fatal_error_exit();
  22. enum error_type { WARNING, ERROR, FATAL };
  23. static void do_error_with_file_and_line(const char *filename,
  24. const char *source_filename,
  25. int lineno,
  26. error_type type,
  27. const char *format,
  28. const errarg &arg1,
  29. const errarg &arg2,
  30. const errarg &arg3)
  31. {
  32. int need_space = 0;
  33. if (program_name) {
  34. fprintf(stderr, "%s:", program_name);
  35. need_space = 1;
  36. }
  37. if (lineno >= 0 && filename != 0) {
  38. if (strcmp(filename, "-") == 0)
  39. filename = "<standard input>";
  40. if (source_filename != 0)
  41. fprintf(stderr, "%s (%s):%d:", filename, source_filename, lineno);
  42. else
  43. fprintf(stderr, "%s:%d:", filename, lineno);
  44. need_space = 1;
  45. }
  46. switch (type) {
  47. case FATAL:
  48. fputs("fatal error:", stderr);
  49. need_space = 1;
  50. break;
  51. case ERROR:
  52. break;
  53. case WARNING:
  54. fputs("warning:", stderr);
  55. need_space = 1;
  56. break;
  57. }
  58. if (need_space)
  59. fputc(' ', stderr);
  60. errprint(format, arg1, arg2, arg3);
  61. fputc('\n', stderr);
  62. fflush(stderr);
  63. if (type == FATAL)
  64. fatal_error_exit();
  65. }
  66. static void do_error(error_type type,
  67. const char *format,
  68. const errarg &arg1,
  69. const errarg &arg2,
  70. const errarg &arg3)
  71. {
  72. do_error_with_file_and_line(current_filename, current_source_filename,
  73. current_lineno, type, format, arg1, arg2, arg3);
  74. }
  75. void error(const char *format,
  76. const errarg &arg1,
  77. const errarg &arg2,
  78. const errarg &arg3)
  79. {
  80. do_error(ERROR, format, arg1, arg2, arg3);
  81. }
  82. void warning(const char *format,
  83. const errarg &arg1,
  84. const errarg &arg2,
  85. const errarg &arg3)
  86. {
  87. do_error(WARNING, format, arg1, arg2, arg3);
  88. }
  89. void fatal(const char *format,
  90. const errarg &arg1,
  91. const errarg &arg2,
  92. const errarg &arg3)
  93. {
  94. do_error(FATAL, format, arg1, arg2, arg3);
  95. }
  96. void error_with_file_and_line(const char *filename,
  97. int lineno,
  98. const char *format,
  99. const errarg &arg1,
  100. const errarg &arg2,
  101. const errarg &arg3)
  102. {
  103. do_error_with_file_and_line(filename, 0, lineno,
  104. ERROR, format, arg1, arg2, arg3);
  105. }
  106. void warning_with_file_and_line(const char *filename,
  107. int lineno,
  108. const char *format,
  109. const errarg &arg1,
  110. const errarg &arg2,
  111. const errarg &arg3)
  112. {
  113. do_error_with_file_and_line(filename, 0, lineno,
  114. WARNING, format, arg1, arg2, arg3);
  115. }
  116. void fatal_with_file_and_line(const char *filename,
  117. int lineno,
  118. const char *format,
  119. const errarg &arg1,
  120. const errarg &arg2,
  121. const errarg &arg3)
  122. {
  123. do_error_with_file_and_line(filename, 0, lineno,
  124. FATAL, format, arg1, arg2, arg3);
  125. }