1 /* Copyright (C) 2001, 2005, 2006, 2009-2012, 2016, 2018
2  *   Free Software Foundation, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3 of
7  * the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 
20 
21 
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 
30 #include "libguile/_scm.h"
31 
32 #include "libguile/deprecation.h"
33 #include "libguile/strings.h"
34 #include "libguile/ports.h"
35 
36 #include "libguile/private-options.h"
37 
38 
39 
40 struct issued_warning {
41   struct issued_warning *prev;
42   const char *message;
43 };
44 
45 static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
46 static struct issued_warning *issued_warnings;
47 static int print_summary = 0;
48 
49 void
scm_c_issue_deprecation_warning(const char * msg)50 scm_c_issue_deprecation_warning (const char *msg)
51 {
52   if (!SCM_WARN_DEPRECATED)
53     print_summary = 1;
54   else
55     {
56       struct issued_warning *iw;
57 
58       scm_i_pthread_mutex_lock (&warn_lock);
59       for (iw = issued_warnings; iw; iw = iw->prev)
60 	if (!strcmp (iw->message, msg))
61 	  {
62             msg = NULL;
63             break;
64           }
65       if (msg)
66         {
67           msg = strdup (msg);
68           iw = malloc (sizeof (struct issued_warning));
69           if (msg == NULL || iw == NULL)
70             /* Nothing sensible to do if you can't allocate this small
71                amount of memory.  */
72             abort ();
73           iw->message = msg;
74           iw->prev = issued_warnings;
75           issued_warnings = iw;
76         }
77       scm_i_pthread_mutex_unlock (&warn_lock);
78 
79       /* All this dance is to avoid printing to a port inside a mutex,
80          which could recurse and deadlock.  */
81       if (msg)
82         {
83           if (scm_gc_running_p)
84             fprintf (stderr, "%s\n", msg);
85           else
86             {
87               scm_puts (msg, scm_current_warning_port ());
88               scm_newline (scm_current_warning_port ());
89             }
90         }
91     }
92 }
93 
94 void
scm_c_issue_deprecation_warning_fmt(const char * msg,...)95 scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
96 {
97   va_list ap;
98   char buf[512];
99 
100   va_start (ap, msg);
101   vsnprintf (buf, 511, msg, ap);
102   va_end (ap);
103   buf[511] = '\0';
104   scm_c_issue_deprecation_warning (buf);
105 }
106 
107 SCM_DEFINE(scm_issue_deprecation_warning,
108 	   "issue-deprecation-warning", 0, 0, 1,
109 	   (SCM msgs),
110 	   "Output @var{msgs} to @code{(current-error-port)} when this "
111 	   "is the first call to @code{issue-deprecation-warning} with "
112 	   "this specific @var{msgs}.  Do nothing otherwise. "
113 	   "The argument @var{msgs} should be a list of strings; "
114 	   "they are printed in turn, each one followed by a newline.")
115 #define FUNC_NAME s_scm_issue_deprecation_warning
116 {
117   if (!SCM_WARN_DEPRECATED)
118     print_summary = 1;
119   else
120     {
121       SCM nl = scm_from_utf8_string ("\n");
122       SCM msgs_nl = SCM_EOL;
123       char *c_msgs;
124       while (scm_is_pair (msgs))
125 	{
126 	  if (!scm_is_null (msgs_nl))
127 	    msgs_nl = scm_cons (nl, msgs_nl);
128 	  msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
129 	  msgs = SCM_CDR (msgs);
130 	}
131       msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
132       c_msgs = scm_to_locale_string (msgs_nl);
133       scm_c_issue_deprecation_warning (c_msgs);
134       free (c_msgs);
135     }
136   return SCM_UNSPECIFIED;
137 }
138 #undef FUNC_NAME
139 
140 static void
print_deprecation_summary(void)141 print_deprecation_summary (void)
142 {
143   if (print_summary)
144     {
145       fputs ("\n"
146 	     "Some deprecated features have been used.  Set the environment\n"
147              "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
148 	     "program to get more information.  Set it to \"no\" to suppress\n"
149 	     "this message.\n", stderr);
150     }
151 }
152 
153 SCM_DEFINE(scm_include_deprecated_features,
154 	   "include-deprecated-features", 0, 0, 0,
155 	   (),
156 	   "Return @code{#t} iff deprecated features should be included "
157            "in public interfaces.")
158 #define FUNC_NAME s_scm_include_deprecated_features
159 {
160   return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
161 }
162 #undef FUNC_NAME
163 
164 
165 
166 
167 void
scm_init_deprecation()168 scm_init_deprecation ()
169 {
170   const char *level = getenv ("GUILE_WARN_DEPRECATED");
171   if (level == NULL)
172     level = SCM_WARN_DEPRECATED_DEFAULT;
173   if (!strcmp (level, "detailed"))
174     SCM_WARN_DEPRECATED = 1;
175   else if (!strcmp (level, "no"))
176     SCM_WARN_DEPRECATED = 0;
177   else
178     {
179       SCM_WARN_DEPRECATED = 0;
180       atexit (print_deprecation_summary);
181     }
182 #include "libguile/deprecation.x"
183 }
184 
185 /*
186   Local Variables:
187   c-file-style: "gnu"
188   End:
189  */
190