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