defsubst
[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
7e516288
MV
37\f
38
d013f095
MV
39struct issued_warning {
40 struct issued_warning *prev;
41 const char *message;
42};
43
877514da 44static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
d013f095 45static struct issued_warning *issued_warnings;
65bc1f7a 46static int print_summary = 0;
7e516288
MV
47
48void
49scm_c_issue_deprecation_warning (const char *msg)
50{
65bc1f7a
MV
51 if (!SCM_WARN_DEPRECATED)
52 print_summary = 1;
7e516288 53 else
d013f095
MV
54 {
55 struct issued_warning *iw;
877514da
AW
56
57 scm_i_pthread_mutex_lock (&warn_lock);
d013f095
MV
58 for (iw = issued_warnings; iw; iw = iw->prev)
59 if (!strcmp (iw->message, msg))
c46345e6
AW
60 {
61 msg = NULL;
62 break;
63 }
64 if (msg)
65 {
66 msg = strdup (msg);
67 iw = malloc (sizeof (struct issued_warning));
68 if (msg == NULL || iw == NULL)
69 /* Nothing sensible to do if you can't allocate this small
70 amount of memory. */
71 abort ();
72 iw->message = msg;
73 iw->prev = issued_warnings;
74 issued_warnings = iw;
75 }
877514da 76 scm_i_pthread_mutex_unlock (&warn_lock);
c46345e6
AW
77
78 /* All this dance is to avoid printing to a port inside a mutex,
79 which could recurse and deadlock. */
80 if (msg)
81 {
82 if (scm_gc_running_p)
83 fprintf (stderr, "%s\n", msg);
84 else
85 {
04ec290f 86 scm_puts_unlocked (msg, scm_current_warning_port ());
2c27dd57 87 scm_newline (scm_current_warning_port ());
c46345e6
AW
88 }
89 }
d013f095
MV
90 }
91}
92
93void
94scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
95{
96 va_list ap;
97 char buf[512];
98
99 va_start (ap, msg);
100 vsnprintf (buf, 511, msg, ap);
11c47357 101 va_end (ap);
d013f095
MV
102 buf[511] = '\0';
103 scm_c_issue_deprecation_warning (buf);
7e516288
MV
104}
105
106SCM_DEFINE(scm_issue_deprecation_warning,
107 "issue-deprecation-warning", 0, 0, 1,
108 (SCM msgs),
109 "Output @var{msgs} to @code{(current-error-port)} when this "
110 "is the first call to @code{issue-deprecation-warning} with "
d013f095 111 "this specific @var{msgs}. Do nothing otherwise. "
7e516288
MV
112 "The argument @var{msgs} should be a list of strings; "
113 "they are printed in turn, each one followed by a newline.")
114#define FUNC_NAME s_scm_issue_deprecation_warning
115{
65bc1f7a
MV
116 if (!SCM_WARN_DEPRECATED)
117 print_summary = 1;
7e516288
MV
118 else
119 {
cc95e00a 120 SCM nl = scm_from_locale_string ("\n");
d013f095 121 SCM msgs_nl = SCM_EOL;
7f9994d9 122 char *c_msgs;
d2e53ed6 123 while (scm_is_pair (msgs))
7e516288 124 {
393baa8a 125 if (!scm_is_null (msgs_nl))
d013f095
MV
126 msgs_nl = scm_cons (nl, msgs_nl);
127 msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
128 msgs = SCM_CDR (msgs);
7e516288 129 }
d013f095 130 msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
7f9994d9
MV
131 c_msgs = scm_to_locale_string (msgs_nl);
132 scm_c_issue_deprecation_warning (c_msgs);
133 free (c_msgs);
7e516288
MV
134 }
135 return SCM_UNSPECIFIED;
136}
137#undef FUNC_NAME
138
139static void
140print_deprecation_summary (void)
141{
65bc1f7a 142 if (print_summary)
7e516288
MV
143 {
144 fputs ("\n"
145 "Some deprecated features have been used. Set the environment\n"
146 "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
147 "program to get more information. Set it to \"no\" to suppress\n"
148 "this message.\n", stderr);
149 }
150}
151
7e516288
MV
152SCM_DEFINE(scm_include_deprecated_features,
153 "include-deprecated-features", 0, 0, 0,
154 (),
7bad99fd
MV
155 "Return @code{#t} iff deprecated features should be included "
156 "in public interfaces.")
7e516288
MV
157#define FUNC_NAME s_scm_include_deprecated_features
158{
7888309b 159 return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
7e516288
MV
160}
161#undef FUNC_NAME
162
163
164\f
165
166void
167scm_init_deprecation ()
168{
7e516288
MV
169 const char *level = getenv ("GUILE_WARN_DEPRECATED");
170 if (level == NULL)
887dfa7d 171 level = SCM_WARN_DEPRECATED_DEFAULT;
7e516288 172 if (!strcmp (level, "detailed"))
65bc1f7a 173 SCM_WARN_DEPRECATED = 1;
7e516288 174 else if (!strcmp (level, "no"))
65bc1f7a 175 SCM_WARN_DEPRECATED = 0;
7e516288
MV
176 else
177 {
65bc1f7a 178 SCM_WARN_DEPRECATED = 0;
7e516288
MV
179 atexit (print_deprecation_summary);
180 }
7e516288 181#include "libguile/deprecation.x"
7e516288
MV
182}
183
184/*
185 Local Variables:
186 c-file-style: "gnu"
53befeb7
NJ
187 End:
188 */