slerr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* error handling common to all routines. */
  2. /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
  3. * This file is part of the S-Lang library.
  4. *
  5. * You may distribute under the terms of either the GNU General Public
  6. * License or the Perl Artistic License.
  7. */
  8. #include "slinclud.h"
  9. #include "slang.h"
  10. #include "_slang.h"
  11. void (*SLang_VMessage_Hook) (char *, va_list);
  12. void (*SLang_Error_Hook)(char *);
  13. void (*SLang_Exit_Error_Hook)(char *, va_list);
  14. volatile int SLang_Error = 0;
  15. char *SLang_Error_Message;
  16. volatile int SLKeyBoard_Quit = 0;
  17. static char *get_error_string (void)
  18. {
  19. char *str;
  20. if (!SLang_Error) SLang_Error = SL_UNKNOWN_ERROR;
  21. if (SLang_Error_Message != NULL) str = SLang_Error_Message;
  22. else switch(SLang_Error)
  23. {
  24. case SL_NOT_IMPLEMENTED: str = "Not Implemented"; break;
  25. case SL_APPLICATION_ERROR: str = "Application Error"; break;
  26. case SL_VARIABLE_UNINITIALIZED: str = "Variable Uninitialized"; break;
  27. case SL_MALLOC_ERROR : str = "Malloc Error"; break;
  28. case SL_INTERNAL_ERROR: str = "Internal Error"; break;
  29. case SL_STACK_OVERFLOW: str = "Stack Overflow"; break;
  30. case SL_STACK_UNDERFLOW: str = "Stack Underflow"; break;
  31. case SL_INTRINSIC_ERROR: str = "Intrinsic Error"; break;
  32. case SL_USER_BREAK: str = "User Break"; break;
  33. case SL_UNDEFINED_NAME: str = "Undefined Name"; break;
  34. case SL_SYNTAX_ERROR: str = "Syntax Error"; break;
  35. case SL_DUPLICATE_DEFINITION: str = "Duplicate Definition"; break;
  36. case SL_TYPE_MISMATCH: str = "Type Mismatch"; break;
  37. case SL_READONLY_ERROR: str = "Variable is read-only"; break;
  38. case SL_DIVIDE_ERROR: str = "Divide by zero"; break;
  39. case SL_OBJ_NOPEN: str = "Object not opened"; break;
  40. case SL_OBJ_UNKNOWN: str = "Object unknown"; break;
  41. case SL_INVALID_PARM: str = "Invalid Parameter"; break;
  42. case SL_TYPE_UNDEFINED_OP_ERROR:
  43. str = "Operation not defined for datatype"; break;
  44. case SL_USER_ERROR:
  45. str = "User Error"; break;
  46. case SL_USAGE_ERROR:
  47. str = "Illegal usage of function";
  48. break;
  49. case SL_FLOATING_EXCEPTION:
  50. str = "Floating Point Exception";
  51. break;
  52. case SL_UNKNOWN_ERROR:
  53. default: str = "Unknown Error Code";
  54. }
  55. SLang_Error_Message = NULL;
  56. return str;
  57. }
  58. void SLang_doerror (char *error)
  59. {
  60. char *str = NULL;
  61. char *err;
  62. char *malloced_err_buf;
  63. char err_buf [1024];
  64. malloced_err_buf = NULL;
  65. if (((SLang_Error == SL_USER_ERROR)
  66. || (SLang_Error == SL_USAGE_ERROR))
  67. && (error != NULL) && (*error != 0))
  68. err = error;
  69. else
  70. {
  71. char *sle = "S-Lang Error: ";
  72. unsigned int len = 0;
  73. char *fmt = "%s%s%s";
  74. str = get_error_string ();
  75. if ((error == NULL) || (*error == 0))
  76. error = "";
  77. else if (SLang_Error == SL_UNKNOWN_ERROR)
  78. /* Do not display an unknown error message if error is non-NULL */
  79. str = "";
  80. else {
  81. fmt = "%s%s: %s";
  82. len = 2; /* ": " */
  83. }
  84. len += strlen (sle) + strlen (str) + strlen(error) + 1 /* trailing 0 */;
  85. err = err_buf;
  86. if (len > sizeof (err_buf))
  87. {
  88. if (NULL == (malloced_err_buf = SLmalloc (len)))
  89. err = NULL;
  90. else
  91. err = malloced_err_buf;
  92. }
  93. if (err != NULL) sprintf (err, fmt, sle, str, error);
  94. else err = "Out of memory";
  95. }
  96. if (SLang_Error_Hook == NULL)
  97. {
  98. fputs (err, stderr);
  99. fputs("\r\n", stderr);
  100. fflush (stderr);
  101. }
  102. else
  103. (*SLang_Error_Hook)(err);
  104. SLfree (malloced_err_buf);
  105. }
  106. void SLang_verror (int err_code, char *fmt, ...)
  107. {
  108. va_list ap;
  109. char err [1024];
  110. if (err_code == 0) err_code = SL_INTRINSIC_ERROR;
  111. if (SLang_Error == 0) SLang_Error = err_code;
  112. if (fmt != NULL)
  113. {
  114. va_start(ap, fmt);
  115. (void) _SLvsnprintf (err, sizeof (err), fmt, ap);
  116. fmt = err;
  117. va_end(ap);
  118. }
  119. SLang_doerror (fmt);
  120. }
  121. void SLang_exit_error (char *fmt, ...)
  122. {
  123. va_list ap;
  124. va_start (ap, fmt);
  125. if (SLang_Exit_Error_Hook != NULL)
  126. {
  127. (*SLang_Exit_Error_Hook) (fmt, ap);
  128. exit (1);
  129. }
  130. if (fmt != NULL)
  131. {
  132. vfprintf (stderr, fmt, ap);
  133. fputs ("\r\n", stderr);
  134. fflush (stderr);
  135. }
  136. va_end (ap);
  137. exit (1);
  138. }
  139. void SLang_vmessage (char *fmt, ...)
  140. {
  141. va_list ap;
  142. if (fmt == NULL)
  143. return;
  144. va_start (ap, fmt);
  145. if (SLang_VMessage_Hook != NULL)
  146. (*SLang_VMessage_Hook) (fmt, ap);
  147. else
  148. {
  149. vfprintf (stdout, fmt, ap);
  150. fputs ("\r\n", stdout);
  151. }
  152. va_end (ap);
  153. }