Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / deprecation.c
1 /* Copyright (C) 2001, 2006, 2010, 2011 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
19 \f
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28
29 #include "libguile/_scm.h"
30
31 #include "libguile/deprecation.h"
32 #include "libguile/strings.h"
33 #include "libguile/ports.h"
34
35 #include "libguile/private-options.h"
36
37
38 /* Windows defines. */
39 #ifdef __MINGW32__
40 #define vsnprintf _vsnprintf
41 #endif
42
43 \f
44
45 struct issued_warning {
46 struct issued_warning *prev;
47 const char *message;
48 };
49
50 static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
51 static struct issued_warning *issued_warnings;
52 static int print_summary = 0;
53
54 void
55 scm_c_issue_deprecation_warning (const char *msg)
56 {
57 if (!SCM_WARN_DEPRECATED)
58 print_summary = 1;
59 else
60 {
61 struct issued_warning *iw;
62
63 scm_i_pthread_mutex_lock (&warn_lock);
64 for (iw = issued_warnings; iw; iw = iw->prev)
65 if (!strcmp (iw->message, msg))
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 }
82 scm_i_pthread_mutex_unlock (&warn_lock);
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 {
92 scm_puts_unlocked (msg, scm_current_warning_port ());
93 scm_newline (scm_current_warning_port ());
94 }
95 }
96 }
97 }
98
99 void
100 scm_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);
107 va_end (ap);
108 buf[511] = '\0';
109 scm_c_issue_deprecation_warning (buf);
110 }
111
112 SCM_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 "
117 "this specific @var{msgs}. Do nothing otherwise. "
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 {
122 if (!SCM_WARN_DEPRECATED)
123 print_summary = 1;
124 else
125 {
126 SCM nl = scm_from_locale_string ("\n");
127 SCM msgs_nl = SCM_EOL;
128 char *c_msgs;
129 while (scm_is_pair (msgs))
130 {
131 if (!scm_is_null (msgs_nl))
132 msgs_nl = scm_cons (nl, msgs_nl);
133 msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
134 msgs = SCM_CDR (msgs);
135 }
136 msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
137 c_msgs = scm_to_locale_string (msgs_nl);
138 scm_c_issue_deprecation_warning (c_msgs);
139 free (c_msgs);
140 }
141 return SCM_UNSPECIFIED;
142 }
143 #undef FUNC_NAME
144
145 static void
146 print_deprecation_summary (void)
147 {
148 if (print_summary)
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
158 SCM_DEFINE(scm_include_deprecated_features,
159 "include-deprecated-features", 0, 0, 0,
160 (),
161 "Return @code{#t} iff deprecated features should be included "
162 "in public interfaces.")
163 #define FUNC_NAME s_scm_include_deprecated_features
164 {
165 return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
166 }
167 #undef FUNC_NAME
168
169
170 \f
171
172 void
173 scm_init_deprecation ()
174 {
175 const char *level = getenv ("GUILE_WARN_DEPRECATED");
176 if (level == NULL)
177 level = SCM_WARN_DEPRECATED_DEFAULT;
178 if (!strcmp (level, "detailed"))
179 SCM_WARN_DEPRECATED = 1;
180 else if (!strcmp (level, "no"))
181 SCM_WARN_DEPRECATED = 0;
182 else
183 {
184 SCM_WARN_DEPRECATED = 0;
185 atexit (print_deprecation_summary);
186 }
187 #include "libguile/deprecation.x"
188 }
189
190 /*
191 Local Variables:
192 c-file-style: "gnu"
193 End:
194 */