• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file error.c
3  * \brief Error code handling routines
4  * \author Jaroslav Kysela <perex@perex.cz>
5  * \date 1998-2001
6  *
7  * Error code handling routines.
8  */
9 /*
10  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
11  *
12  *  snd_strerror routine needs to be recoded for the locale support
13  *
14  *
15  *   This library is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU Lesser General Public License as
17  *   published by the Free Software Foundation; either version 2.1 of
18  *   the License, or (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU Lesser General Public License for more details.
24  *
25  *   You should have received a copy of the GNU Lesser General Public
26  *   License along with this library; if not, write to the Free Software
27  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  *
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include "local.h"
36 
37 /**
38  * Array of error codes in US ASCII.
39  */
40 static const char *snd_error_codes[] =
41 {
42 	"Sound protocol is not compatible"
43 };
44 
45 /**
46  * \brief Returns the message for an error code.
47  * \param errnum The error code number, which must be a system error code
48  *               or an ALSA error code.
49  * \return The ASCII description of the given numeric error code.
50  */
snd_strerror(int errnum)51 const char *snd_strerror(int errnum)
52 {
53 	if (errnum < 0)
54 		errnum = -errnum;
55 	if (errnum < SND_ERROR_BEGIN)
56 		return (const char *) strerror(errnum);
57 	errnum -= SND_ERROR_BEGIN;
58 	if ((unsigned int) errnum >= sizeof(snd_error_codes) / sizeof(const char *))
59 		 return "Unknown error";
60 	return snd_error_codes[errnum];
61 }
62 
63 #ifndef DOC_HIDDEN
64 #ifdef HAVE___THREAD
65 #define TLS_PFX		__thread
66 #else
67 #define TLS_PFX		/* NOP */
68 #endif
69 #endif
70 
71 static TLS_PFX snd_local_error_handler_t local_error = NULL;
72 
73 /**
74  * \brief Install local error handler
75  * \param func The local error handler function
76  * \retval Previous local error handler function
77  */
snd_lib_error_set_local(snd_local_error_handler_t func)78 snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
79 {
80 	snd_local_error_handler_t old = local_error;
81 	local_error = func;
82 	return old;
83 }
84 
85 /**
86  * \brief The default error handler function.
87  * \param file The filename where the error was hit.
88  * \param line The line number.
89  * \param function The function name.
90  * \param err The error code.
91  * \param fmt The message (including the format characters).
92  * \param ... Optional arguments.
93  *
94  * If a local error function has been installed for the current thread by
95  * \ref snd_lib_error_set_local, it is called. Otherwise, prints the error
96  * message including location to \c stderr.
97  */
snd_lib_error_default(const char * file,int line,const char * function,int err,const char * fmt,...)98 static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
99 {
100 	va_list arg;
101 	va_start(arg, fmt);
102 	if (local_error) {
103 		local_error(file, line, function, err, fmt, arg);
104 		va_end(arg);
105 		return;
106 	}
107 	fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
108 	vfprintf(stderr, fmt, arg);
109 	if (err)
110 		fprintf(stderr, ": %s", snd_strerror(err));
111 	putc('\n', stderr);
112 	va_end(arg);
113 }
114 
115 /**
116  * \ingroup Error
117  * Pointer to the error handler function.
118  * For internal use only.
119  */
120 snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
121 
122 /**
123  * \brief Sets the error handler.
124  * \param handler The pointer to the new error handler function.
125  *
126  * This function sets a new error handler, or (if \c handler is \c NULL)
127  * the default one which prints the error messages to \c stderr.
128  */
snd_lib_error_set_handler(snd_lib_error_handler_t handler)129 int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
130 {
131 	snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
132 #ifndef NDEBUG
133 	if (snd_lib_error != snd_lib_error_default)
134 		snd_err_msg = snd_lib_error;
135 #endif
136 	return 0;
137 }
138 
139 /**
140  * \brief Returns the ALSA sound library version in ASCII format
141  * \return The ASCII description of the used ALSA sound library.
142  */
snd_asoundlib_version(void)143 const char *snd_asoundlib_version(void)
144 {
145 	return SND_LIB_VERSION_STR;
146 }
147 
148 #ifndef NDEBUG
149 /*
150  * internal error handling
151  */
snd_err_msg_default(const char * file,int line,const char * function,int err,const char * fmt,...)152 static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
153 {
154 	va_list arg;
155 	const char *verbose;
156 
157 	verbose = getenv("LIBASOUND_DEBUG");
158 	if (! verbose || ! *verbose)
159 		return;
160 	va_start(arg, fmt);
161 	fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
162 	vfprintf(stderr, fmt, arg);
163 	if (err)
164 		fprintf(stderr, ": %s", snd_strerror(err));
165 	putc('\n', stderr);
166 	va_end(arg);
167 #ifdef ALSA_DEBUG_ASSERT
168 	verbose = getenv("LIBASOUND_DEBUG_ASSERT");
169 	if (verbose && *verbose)
170 		assert(0);
171 #endif
172 }
173 
174 /**
175  * The ALSA error message handler
176  */
177 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
178 
179 #endif
180 
181 /**
182  * \brief Copy a C-string into a sized buffer
183  * \param dst Where to copy the string to
184  * \param src Where to copy the string from
185  * \param size Size of destination buffer
186  * \retval The source string length
187  *
188  * The result is always a valid NUL-terminated string that fits
189  * in the buffer (unless, of course, the buffer size is zero).
190  * It does not pad out the result like strncpy() does.
191  */
snd_strlcpy(char * dst,const char * src,size_t size)192 size_t snd_strlcpy(char *dst, const char *src, size_t size)
193 {
194   size_t ret = strlen(src);
195   if (size) {
196     size_t len = ret >= size ? size - 1 : ret;
197     memcpy(dst, src, len);
198     dst[len] = '\0';
199   }
200   return ret;
201 }
202