error.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* functions for reporting errors and maybe exiting
  2. Copyright (C) 2006 Dennis Furey
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include <avm/common.h>
  16. #include <avm/error.h>
  17. extern char *xstrerror ();
  18. /* the name of the program that is to appear at the beginning of error
  19. messages */
  20. static char *program_name = NULL;
  21. #define default_program_name (program_name?program_name:"avram")
  22. void
  23. avm_set_program_name (path)
  24. char *path;
  25. /* This sets the program name to be used in error messages of the
  26. form program-name: message; if none is set, avram is used by
  27. default. */
  28. {
  29. if (program_name)
  30. free (program_name);
  31. if (!(program_name = (char *) malloc (strlen (path) + 1)))
  32. avm_error ("memory overflow (code 3)");
  33. strcpy (program_name, path);
  34. }
  35. char *
  36. avm_program_name ()
  37. /* This returns the address of the program name used in error
  38. messages. */
  39. {
  40. return default_program_name;
  41. }
  42. void
  43. avm_internal_error (code)
  44. int code;
  45. /* This reports an internal error with the given code and
  46. exits. */
  47. {
  48. fprintf (stderr, "%s: virtual machine internal error (code %d)\n",default_program_name, code);
  49. exit (EXIT_FAILURE);
  50. }
  51. void
  52. avm_warning (message)
  53. char *message;
  54. /* This prints the messae but doesn't exit. */
  55. {
  56. fprintf (stderr, "%s: %s\n", default_program_name, message);
  57. }
  58. void
  59. avm_error (message)
  60. char *message;
  61. /* This prints the message and exits. */
  62. {
  63. avm_warning (message);
  64. exit (EXIT_FAILURE);
  65. }
  66. void
  67. avm_reclamation_failure (entity, count)
  68. char *entity;
  69. counter count;
  70. /* This non-fatally reports unreclaimed storage; entity is the
  71. type of storage and count is the number of units. */
  72. {
  73. fprintf (stderr, "%s: %d unreclaimed %s\n", default_program_name, (int) count, entity);
  74. }
  75. void
  76. avm_non_fatal_io_error (message, filename, reason)
  77. char *message;
  78. char *filename;
  79. int reason;
  80. /* This reports an i/o error associated with the file name for
  81. the given reason, but doesn't exit. */
  82. {
  83. if (reason)
  84. {
  85. #if HAVE_STRERROR
  86. fprintf (stderr, "%s: %s %s; %s\n", default_program_name, message,filename, xstrerror (reason));
  87. #else
  88. fprintf (stderr, "%s: %s %s\n", default_program_name, message,filename);
  89. #endif
  90. }
  91. else
  92. fprintf (stderr, "%s: %s %s\n", default_program_name, message,filename);
  93. }
  94. void
  95. avm_fatal_io_error (message, filename, reason)
  96. char *message;
  97. char *filename;
  98. int reason;
  99. /* This reports an i/o error and exits. */
  100. {
  101. avm_non_fatal_io_error (message, filename, reason);
  102. exit (EXIT_FAILURE);
  103. }