error.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com) */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "errarg.h"
  8. #include "error.h"
  9. extern void fatal_error_exit();
  10. enum error_type { WARNING, ERROR, FATAL };
  11. static void
  12. do_error_with_file_and_line(const char *filename, int lineno,
  13. error_type type,
  14. const char *format,
  15. const errarg &arg1,
  16. const errarg &arg2,
  17. const errarg &arg3)
  18. {
  19. int need_space = 0;
  20. if (program_name)
  21. {
  22. fprintf(stderr, "%s:", program_name);
  23. need_space = 1;
  24. }
  25. if (lineno >= 0 && filename != 0)
  26. {
  27. if (strcmp(filename, "-") == 0)
  28. filename = "<standard input>";
  29. fprintf(stderr, "%s:%d:", filename, lineno);
  30. need_space = 1;
  31. }
  32. switch (type)
  33. {
  34. case FATAL:
  35. fputs("fatal error:", stderr);
  36. need_space = 1;
  37. break;
  38. case ERROR:
  39. break;
  40. case WARNING:
  41. fputs("warning:", stderr);
  42. need_space = 1;
  43. break;
  44. }
  45. if (need_space)
  46. fputc(' ', stderr);
  47. errprint(format, arg1, arg2, arg3);
  48. fputc('\n', stderr);
  49. fflush(stderr);
  50. if (type == FATAL)
  51. fatal_error_exit();
  52. }
  53. static void
  54. do_error (error_type type, const char *format,
  55. const errarg &arg1, const errarg &arg2, const errarg &arg3)
  56. {
  57. do_error_with_file_and_line(current_filename, current_lineno,
  58. type, format, arg1, arg2, arg3);
  59. }
  60. void
  61. error(const char *format,
  62. const errarg &arg1,
  63. const errarg &arg2,
  64. const errarg &arg3)
  65. {
  66. do_error(ERROR, format, arg1, arg2, arg3);
  67. }
  68. void
  69. warning(const char *format,
  70. const errarg &arg1,
  71. const errarg &arg2,
  72. const errarg &arg3)
  73. {
  74. do_error(WARNING, format, arg1, arg2, arg3);
  75. }
  76. void
  77. fatal(const char *format,
  78. const errarg &arg1,
  79. const errarg &arg2,
  80. const errarg &arg3)
  81. {
  82. do_error(FATAL, format, arg1, arg2, arg3);
  83. }
  84. void
  85. error_with_file_and_line(const char *filename,
  86. int lineno,
  87. const char *format,
  88. const errarg &arg1,
  89. const errarg &arg2,
  90. const errarg &arg3)
  91. {
  92. do_error_with_file_and_line(filename, lineno,
  93. ERROR, format, arg1, arg2, arg3);
  94. }
  95. void
  96. warning_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, lineno,
  104. WARNING, format, arg1, arg2, arg3);
  105. }
  106. void
  107. fatal_with_file_and_line(const char *filename,
  108. int lineno,
  109. const char *format,
  110. const errarg &arg1,
  111. const errarg &arg2,
  112. const errarg &arg3)
  113. {
  114. do_error_with_file_and_line(filename, lineno,
  115. FATAL, format, arg1, arg2, arg3);
  116. }