2003-03-24 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-internals.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-internals.c  random utility stuff (internal to D-BUS implementation)
3  *
4  * Copyright (C) 2002, 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-internals.h"
24 #include "dbus-protocol.h"
25 #include "dbus-test.h"
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34
35 /**
36  * @defgroup DBusInternals D-BUS internal implementation details
37  * @brief Documentation useful when developing or debugging D-BUS itself.
38  * 
39  */
40
41 /**
42  * @defgroup DBusInternalsUtils Utilities and portability
43  * @ingroup DBusInternals
44  * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
45  * @{
46  */
47
48 /**
49  * @def _dbus_assert
50  *
51  * Aborts with an error message if the condition is false.
52  * 
53  * @param condition condition which must be true.
54  */
55
56 /**
57  * @def _dbus_assert_not_reached
58  *
59  * Aborts with an error message if called.
60  * The given explanation will be printed.
61  * 
62  * @param explanation explanation of what happened if the code was reached.
63  */
64
65 /**
66  * @def _DBUS_N_ELEMENTS
67  *
68  * Computes the number of elements in a fixed-size array using
69  * sizeof().
70  *
71  * @param array the array to count elements in.
72  */
73
74 /**
75  * @def _DBUS_POINTER_TO_INT
76  *
77  * Safely casts a void* to an integer; should only be used on void*
78  * that actually contain integers, for example one created with
79  * _DBUS_INT_TO_POINTER.  Only guaranteed to preserve 32 bits.
80  * (i.e. it's used to store 32-bit ints in pointers, but
81  * can't be used to store 64-bit pointers in ints.)
82  *
83  * @param pointer pointer to extract an integer from.
84  */
85 /**
86  * @def _DBUS_INT_TO_POINTER
87  *
88  * Safely stuffs an integer into a pointer, to be extracted later with
89  * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
90  *
91  * @param integer the integer to stuff into a pointer.
92  */
93 /**
94  * @def _DBUS_ZERO
95  *
96  * Sets all bits in an object to zero.
97  *
98  * @param object the object to be zeroed.
99  */
100 /**
101  * @def _DBUS_INT_MIN
102  *
103  * Minimum value of type "int"
104  */
105 /**
106  * @def _DBUS_INT_MAX
107  *
108  * Maximum value of type "int"
109  */
110 /**
111  * @def _DBUS_MAX_SUN_PATH_LENGTH
112  *
113  * Maximum length of the path to a UNIX domain socket,
114  * sockaddr_un::sun_path member. POSIX requires that all systems
115  * support at least 100 bytes here, including the nul termination.
116  * We use 99 for the max value to allow for the nul.
117  *
118  * We could probably also do sizeof (addr.sun_path)
119  * but this way we are the same on all platforms
120  * which is probably a good idea.
121  */
122
123 /**
124  * @typedef DBusForeachFunction
125  * 
126  * Used to iterate over each item in a collection, such as
127  * a DBusList.
128  */
129
130 /**
131  * @def _DBUS_LOCK_NAME
132  *
133  * Expands to name of a global lock variable.
134  */
135
136 /**
137  * @def _DBUS_DEFINE_GLOBAL_LOCK
138  *
139  * Defines a global lock variable with the given name.
140  * The lock must be added to the list to initialize
141  * in dbus_threads_init().
142  */
143
144 /**
145  * @def _DBUS_DECLARE_GLOBAL_LOCK
146  *
147  * Expands to declaration of a global lock defined
148  * with _DBUS_DEFINE_GLOBAL_LOCK.
149  * The lock must be added to the list to initialize
150  * in dbus_threads_init().
151  */
152
153 /**
154  * @def _DBUS_LOCK
155  *
156  * Locks a global lock
157  */
158
159 /**
160  * @def _DBUS_UNLOCK
161  *
162  * Unlocks a global lock
163  */
164
165 /**
166  * Fixed "out of memory" error message, just to avoid
167  * making up a different string every time and wasting
168  * space.
169  */
170 const char _dbus_no_memory_message[] = "Not enough memory";
171
172 /**
173  * Prints a warning message to stderr.
174  *
175  * @param format printf-style format string.
176  */
177 void
178 _dbus_warn (const char *format,
179             ...)
180 {
181   /* FIXME not portable enough? */
182   va_list args;
183
184   va_start (args, format);
185   vfprintf (stderr, format, args);
186   va_end (args);
187 }
188
189 /**
190  * Prints a warning message to stderr
191  * if the user has enabled verbose mode.
192  * This is the real function implementation,
193  * use _dbus_verbose() macro in code.
194  *
195  * @param format printf-style format string.
196  */
197 void
198 _dbus_verbose_real (const char *format,
199                     ...)
200 {
201   va_list args;
202   static dbus_bool_t verbose = TRUE;
203   static dbus_bool_t initted = FALSE;
204
205   /* things are written a bit oddly here so that
206    * in the non-verbose case we just have the one
207    * conditional and return immediately.
208    */
209   if (!verbose)
210     return;
211   
212   if (!initted)
213     {
214       verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
215       initted = TRUE;
216       if (!verbose)
217         return;
218     }
219   
220   va_start (args, format);
221   vfprintf (stderr, format, args);
222   va_end (args);
223 }
224
225 /**
226  * Duplicates a string. Result must be freed with
227  * dbus_free(). Returns #NULL if memory allocation fails.
228  * If the string to be duplicated is #NULL, returns #NULL.
229  * 
230  * @param str string to duplicate.
231  * @returns newly-allocated copy.
232  */
233 char*
234 _dbus_strdup (const char *str)
235 {
236   int len;
237   char *copy;
238   
239   if (str == NULL)
240     return NULL;
241   
242   len = strlen (str);
243
244   copy = dbus_malloc (len + 1);
245   if (copy == NULL)
246     return NULL;
247
248   memcpy (copy, str, len + 1);
249   
250   return copy;
251 }
252
253 /**
254  * Returns a string describing the given type.
255  *
256  * @param type the type to describe
257  * @returns a constant string describing the type
258  */
259 const char *
260 _dbus_type_to_string (int type)
261 {
262   switch (type)
263     {
264     case DBUS_TYPE_INVALID:
265       return "invalid";
266     case DBUS_TYPE_NIL:
267       return "nil";
268     case DBUS_TYPE_BOOLEAN:
269       return "boolean";
270     case DBUS_TYPE_INT32:
271       return "int32";
272     case DBUS_TYPE_UINT32:
273       return "uint32";
274     case DBUS_TYPE_DOUBLE:
275       return "double";
276     case DBUS_TYPE_STRING:
277       return "string";
278     case DBUS_TYPE_BOOLEAN_ARRAY:
279       return "boolean array";
280     case DBUS_TYPE_INT32_ARRAY:
281       return "int32 array";
282     case DBUS_TYPE_UINT32_ARRAY:
283       return "uint32 array";
284     case DBUS_TYPE_DOUBLE_ARRAY:
285       return "double array";
286     case DBUS_TYPE_BYTE_ARRAY:
287       return "byte array";
288     case DBUS_TYPE_STRING_ARRAY:
289       return "string array";
290     default:
291       return "unknown";
292     }
293 }
294
295 /** @} */