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