"latin1" -> "Latin-1".
[bpt/guile.git] / libguile / deprecation.c
CommitLineData
877514da 1/* Copyright (C) 2001, 2006, 2010 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))
877514da 66 goto done;
d013f095
MV
67 if (scm_gc_running_p)
68 fprintf (stderr, "%s\n", msg);
69 else
70 {
71 scm_puts (msg, scm_current_error_port ());
72 scm_newline (scm_current_error_port ());
73 }
74 msg = strdup (msg);
fc240b46 75 iw = malloc (sizeof (struct issued_warning));
d013f095 76 if (msg == NULL || iw == NULL)
877514da 77 goto done;
d013f095
MV
78 iw->message = msg;
79 iw->prev = issued_warnings;
80 issued_warnings = iw;
877514da
AW
81
82 done:
83 scm_i_pthread_mutex_unlock (&warn_lock);
d013f095
MV
84 }
85}
86
87void
88scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
89{
90 va_list ap;
91 char buf[512];
92
93 va_start (ap, msg);
94 vsnprintf (buf, 511, msg, ap);
11c47357 95 va_end (ap);
d013f095
MV
96 buf[511] = '\0';
97 scm_c_issue_deprecation_warning (buf);
7e516288
MV
98}
99
100SCM_DEFINE(scm_issue_deprecation_warning,
101 "issue-deprecation-warning", 0, 0, 1,
102 (SCM msgs),
103 "Output @var{msgs} to @code{(current-error-port)} when this "
104 "is the first call to @code{issue-deprecation-warning} with "
d013f095 105 "this specific @var{msgs}. Do nothing otherwise. "
7e516288
MV
106 "The argument @var{msgs} should be a list of strings; "
107 "they are printed in turn, each one followed by a newline.")
108#define FUNC_NAME s_scm_issue_deprecation_warning
109{
65bc1f7a
MV
110 if (!SCM_WARN_DEPRECATED)
111 print_summary = 1;
7e516288
MV
112 else
113 {
cc95e00a 114 SCM nl = scm_from_locale_string ("\n");
d013f095 115 SCM msgs_nl = SCM_EOL;
7f9994d9 116 char *c_msgs;
d2e53ed6 117 while (scm_is_pair (msgs))
7e516288 118 {
d013f095
MV
119 if (msgs_nl != SCM_EOL)
120 msgs_nl = scm_cons (nl, msgs_nl);
121 msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
122 msgs = SCM_CDR (msgs);
7e516288 123 }
d013f095 124 msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
7f9994d9
MV
125 c_msgs = scm_to_locale_string (msgs_nl);
126 scm_c_issue_deprecation_warning (c_msgs);
127 free (c_msgs);
7e516288
MV
128 }
129 return SCM_UNSPECIFIED;
130}
131#undef FUNC_NAME
132
133static void
134print_deprecation_summary (void)
135{
65bc1f7a 136 if (print_summary)
7e516288
MV
137 {
138 fputs ("\n"
139 "Some deprecated features have been used. Set the environment\n"
140 "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
141 "program to get more information. Set it to \"no\" to suppress\n"
142 "this message.\n", stderr);
143 }
144}
145
7e516288
MV
146SCM_DEFINE(scm_include_deprecated_features,
147 "include-deprecated-features", 0, 0, 0,
148 (),
7bad99fd
MV
149 "Return @code{#t} iff deprecated features should be included "
150 "in public interfaces.")
7e516288
MV
151#define FUNC_NAME s_scm_include_deprecated_features
152{
7888309b 153 return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
7e516288
MV
154}
155#undef FUNC_NAME
156
157
158\f
159
160void
161scm_init_deprecation ()
162{
7e516288
MV
163 const char *level = getenv ("GUILE_WARN_DEPRECATED");
164 if (level == NULL)
887dfa7d 165 level = SCM_WARN_DEPRECATED_DEFAULT;
7e516288 166 if (!strcmp (level, "detailed"))
65bc1f7a 167 SCM_WARN_DEPRECATED = 1;
7e516288 168 else if (!strcmp (level, "no"))
65bc1f7a 169 SCM_WARN_DEPRECATED = 0;
7e516288
MV
170 else
171 {
65bc1f7a 172 SCM_WARN_DEPRECATED = 0;
7e516288
MV
173 atexit (print_deprecation_summary);
174 }
7e516288 175#include "libguile/deprecation.x"
7e516288
MV
176}
177
178/*
179 Local Variables:
180 c-file-style: "gnu"
53befeb7
NJ
181 End:
182 */