deprecation.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/deprecation.h"
  26. #include "libguile/strings.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/private-options.h"
  29. /* Windows defines. */
  30. #ifdef __MINGW32__
  31. #define vsnprintf _vsnprintf
  32. #endif
  33. struct issued_warning {
  34. struct issued_warning *prev;
  35. const char *message;
  36. };
  37. static struct issued_warning *issued_warnings;
  38. static int print_summary = 0;
  39. void
  40. scm_c_issue_deprecation_warning (const char *msg)
  41. {
  42. if (!SCM_WARN_DEPRECATED)
  43. print_summary = 1;
  44. else
  45. {
  46. struct issued_warning *iw;
  47. for (iw = issued_warnings; iw; iw = iw->prev)
  48. if (!strcmp (iw->message, msg))
  49. return;
  50. if (scm_gc_running_p)
  51. fprintf (stderr, "%s\n", msg);
  52. else
  53. {
  54. scm_puts (msg, scm_current_error_port ());
  55. scm_newline (scm_current_error_port ());
  56. }
  57. msg = strdup (msg);
  58. iw = malloc (sizeof (struct issued_warning));
  59. if (msg == NULL || iw == NULL)
  60. return;
  61. iw->message = msg;
  62. iw->prev = issued_warnings;
  63. issued_warnings = iw;
  64. }
  65. }
  66. void
  67. scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
  68. {
  69. va_list ap;
  70. char buf[512];
  71. va_start (ap, msg);
  72. vsnprintf (buf, 511, msg, ap);
  73. va_end (ap);
  74. buf[511] = '\0';
  75. scm_c_issue_deprecation_warning (buf);
  76. }
  77. SCM_DEFINE(scm_issue_deprecation_warning,
  78. "issue-deprecation-warning", 0, 0, 1,
  79. (SCM msgs),
  80. "Output @var{msgs} to @code{(current-error-port)} when this "
  81. "is the first call to @code{issue-deprecation-warning} with "
  82. "this specific @var{msgs}. Do nothing otherwise. "
  83. "The argument @var{msgs} should be a list of strings; "
  84. "they are printed in turn, each one followed by a newline.")
  85. #define FUNC_NAME s_scm_issue_deprecation_warning
  86. {
  87. if (!SCM_WARN_DEPRECATED)
  88. print_summary = 1;
  89. else
  90. {
  91. SCM nl = scm_from_locale_string ("\n");
  92. SCM msgs_nl = SCM_EOL;
  93. char *c_msgs;
  94. while (scm_is_pair (msgs))
  95. {
  96. if (msgs_nl != SCM_EOL)
  97. msgs_nl = scm_cons (nl, msgs_nl);
  98. msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
  99. msgs = SCM_CDR (msgs);
  100. }
  101. msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
  102. c_msgs = scm_to_locale_string (msgs_nl);
  103. scm_c_issue_deprecation_warning (c_msgs);
  104. free (c_msgs);
  105. }
  106. return SCM_UNSPECIFIED;
  107. }
  108. #undef FUNC_NAME
  109. static void
  110. print_deprecation_summary (void)
  111. {
  112. if (print_summary)
  113. {
  114. fputs ("\n"
  115. "Some deprecated features have been used. Set the environment\n"
  116. "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
  117. "program to get more information. Set it to \"no\" to suppress\n"
  118. "this message.\n", stderr);
  119. }
  120. }
  121. SCM_DEFINE(scm_include_deprecated_features,
  122. "include-deprecated-features", 0, 0, 0,
  123. (),
  124. "Return @code{#t} iff deprecated features should be included "
  125. "in public interfaces.")
  126. #define FUNC_NAME s_scm_include_deprecated_features
  127. {
  128. return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
  129. }
  130. #undef FUNC_NAME
  131. void
  132. scm_init_deprecation ()
  133. {
  134. const char *level = getenv ("GUILE_WARN_DEPRECATED");
  135. if (level == NULL)
  136. level = SCM_WARN_DEPRECATED_DEFAULT;
  137. if (!strcmp (level, "detailed"))
  138. SCM_WARN_DEPRECATED = 1;
  139. else if (!strcmp (level, "no"))
  140. SCM_WARN_DEPRECATED = 0;
  141. else
  142. {
  143. SCM_WARN_DEPRECATED = 0;
  144. atexit (print_deprecation_summary);
  145. }
  146. #include "libguile/deprecation.x"
  147. }
  148. /*
  149. Local Variables:
  150. c-file-style: "gnu"
  151. End:
  152. */