Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / deprecation.c
CommitLineData
44b76a78 1/* Copyright (C) 2001, 2006, 2010, 2011 Free Software Foundation, Inc.
7e516288 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
7e516288 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
7e516288
MV
18
19\f
20
dbb605f5 21#ifdef HAVE_CONFIG_H
5f0bcfd5
RB
22# include <config.h>
23#endif
24
7e516288 25#include <stdio.h>
fbbdb121 26#include <string.h>
d013f095 27#include <stdarg.h>
7e516288
MV
28
29#include "libguile/_scm.h"
30
31#include "libguile/deprecation.h"
7e516288
MV
32#include "libguile/strings.h"
33#include "libguile/ports.h"
34
22fc179a
HWN
35#include "libguile/private-options.h"
36
37
edb810bb
SJ
38/* Windows defines. */
39#ifdef __MINGW32__
40#define vsnprintf _vsnprintf
41#endif
42
7e516288
MV
43\f
44
d013f095
MV
45struct issued_warning {
46 struct issued_warning *prev;
47 const char *message;
48};
49
877514da 50static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
d013f095 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;
877514da
AW
62
63 scm_i_pthread_mutex_lock (&warn_lock);
d013f095
MV
64 for (iw = issued_warnings; iw; iw = iw->prev)
65 if (!strcmp (iw->message, msg))
c46345e6
AW
66 {
67 msg = NULL;
68 break;
69 }
70 if (msg)
71 {
72 msg = strdup (msg);
73 iw = malloc (sizeof (struct issued_warning));
74 if (msg == NULL || iw == NULL)
75 /* Nothing sensible to do if you can't allocate this small
76 amount of memory. */
77 abort ();
78 iw->message = msg;
79 iw->prev = issued_warnings;
80 issued_warnings = iw;
81 }
877514da 82 scm_i_pthread_mutex_unlock (&warn_lock);
c46345e6
AW
83
84 /* All this dance is to avoid printing to a port inside a mutex,
85 which could recurse and deadlock. */
86 if (msg)
87 {
88 if (scm_gc_running_p)
89 fprintf (stderr, "%s\n", msg);
90 else
91 {
04ec290f 92 scm_puts_unlocked (msg, scm_current_warning_port ());
2c27dd57 93 scm_newline (scm_current_warning_port ());
c46345e6
AW
94 }
95 }
d013f095
MV
96 }
97}
98
99void
100scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
101{
102 va_list ap;
103 char buf[512];
104
105 va_start (ap, msg);
106 vsnprintf (buf, 511, msg, ap);
11c47357 107 va_end (ap);
d013f095
MV
108 buf[511] = '\0';
109 scm_c_issue_deprecation_warning (buf);
7e516288
MV
110}
111
112SCM_DEFINE(scm_issue_deprecation_warning,
113 "issue-deprecation-warning", 0, 0, 1,
114 (SCM msgs),
115 "Output @var{msgs} to @code{(current-error-port)} when this "
116 "is the first call to @code{issue-deprecation-warning} with "
d013f095 117 "this specific @var{msgs}. Do nothing otherwise. "
7e516288
MV
118 "The argument @var{msgs} should be a list of strings; "
119 "they are printed in turn, each one followed by a newline.")
120#define FUNC_NAME s_scm_issue_deprecation_warning
121{
65bc1f7a
MV
122 if (!SCM_WARN_DEPRECATED)
123 print_summary = 1;
7e516288
MV
124 else
125 {
cc95e00a 126 SCM nl = scm_from_locale_string ("\n");
d013f095 127 SCM msgs_nl = SCM_EOL;
7f9994d9 128 char *c_msgs;
d2e53ed6 129 while (scm_is_pair (msgs))
7e516288 130 {
393baa8a 131 if (!scm_is_null (msgs_nl))
d013f095
MV
132 msgs_nl = scm_cons (nl, msgs_nl);
133 msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
134 msgs = SCM_CDR (msgs);
7e516288 135 }
d013f095 136 msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
7f9994d9
MV
137 c_msgs = scm_to_locale_string (msgs_nl);
138 scm_c_issue_deprecation_warning (c_msgs);
139 free (c_msgs);
7e516288
MV
140 }
141 return SCM_UNSPECIFIED;
142}
143#undef FUNC_NAME
144
145static void
146print_deprecation_summary (void)
147{
65bc1f7a 148 if (print_summary)
7e516288
MV
149 {
150 fputs ("\n"
151 "Some deprecated features have been used. Set the environment\n"
152 "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
153 "program to get more information. Set it to \"no\" to suppress\n"
154 "this message.\n", stderr);
155 }
156}
157
7e516288
MV
158SCM_DEFINE(scm_include_deprecated_features,
159 "include-deprecated-features", 0, 0, 0,
160 (),
7bad99fd
MV
161 "Return @code{#t} iff deprecated features should be included "
162 "in public interfaces.")
7e516288
MV
163#define FUNC_NAME s_scm_include_deprecated_features
164{
7888309b 165 return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
7e516288
MV
166}
167#undef FUNC_NAME
168
169
170\f
171
172void
173scm_init_deprecation ()
174{
7e516288
MV
175 const char *level = getenv ("GUILE_WARN_DEPRECATED");
176 if (level == NULL)
887dfa7d 177 level = SCM_WARN_DEPRECATED_DEFAULT;
7e516288 178 if (!strcmp (level, "detailed"))
65bc1f7a 179 SCM_WARN_DEPRECATED = 1;
7e516288 180 else if (!strcmp (level, "no"))
65bc1f7a 181 SCM_WARN_DEPRECATED = 0;
7e516288
MV
182 else
183 {
65bc1f7a 184 SCM_WARN_DEPRECATED = 0;
7e516288
MV
185 atexit (print_deprecation_summary);
186 }
7e516288 187#include "libguile/deprecation.x"
7e516288
MV
188}
189
190/*
191 Local Variables:
192 c-file-style: "gnu"
53befeb7
NJ
193 End:
194 */