1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-address.c Server address parser.
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 2.0
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.
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.
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
25 #include "dbus-address.h"
26 #include "dbus-internals.h"
27 #include "dbus-list.h"
28 #include "dbus-string.h"
31 * @defgroup DBusAddressInternals Address parsing
32 * @ingroup DBusInternals
33 * @brief Implementation of parsing addresses of D-BUS servers.
39 * Internals of DBusAddressEntry
41 struct DBusAddressEntry
43 DBusString method; /**< The address type (unix, tcp, etc.) */
45 DBusList *keys; /**< List of keys */
46 DBusList *values; /**< List of values */
49 /** @} */ /* End of internals */
52 dbus_address_entry_free (DBusAddressEntry *entry)
56 _dbus_string_free (&entry->method);
58 link = _dbus_list_get_first_link (&entry->keys);
61 _dbus_string_free (link->data);
62 dbus_free (link->data);
64 link = _dbus_list_get_next_link (&entry->keys, link);
66 _dbus_list_clear (&entry->keys);
68 link = _dbus_list_get_first_link (&entry->values);
71 _dbus_string_free (link->data);
72 dbus_free (link->data);
74 link = _dbus_list_get_next_link (&entry->values, link);
76 _dbus_list_clear (&entry->values);
82 * @defgroup DBusAddress Address parsing
84 * @brief Parsing addresses of D-BUS servers.
90 * Frees a #NULL-terminated array of address entries.
92 * @param entries the array.
95 dbus_address_entries_free (DBusAddressEntry **entries)
99 for (i = 0; entries[i] != NULL; i++)
100 dbus_address_entry_free (entries[i]);
104 static DBusAddressEntry *
107 DBusAddressEntry *entry;
109 entry = dbus_new0 (DBusAddressEntry, 1);
114 if (!_dbus_string_init (&entry->method))
124 * Returns the method string of an address entry.
126 * @param entry the entry.
127 * @returns a string describing the method. This string
131 dbus_address_entry_get_method (DBusAddressEntry *entry)
133 return _dbus_string_get_const_data (&entry->method);
137 * Returns a value from a key of an entry.
139 * @param entry the entry.
140 * @param key the key.
141 * @returns the key value. This string must not be fred.
144 dbus_address_entry_get_value (DBusAddressEntry *entry,
147 DBusList *values, *keys;
149 keys = _dbus_list_get_first_link (&entry->keys);
150 values = _dbus_list_get_first_link (&entry->values);
154 _dbus_assert (values != NULL);
156 if (_dbus_string_equal_c_str (keys->data, key))
157 return _dbus_string_get_const_data (values->data);
159 keys = _dbus_list_get_next_link (&entry->keys, keys);
160 values = _dbus_list_get_next_link (&entry->values, values);
167 * Parses an address string of the form:
169 * method:key=value,key=value;method:key=value
171 * @todo document address format in the specification
173 * @todo need to be able to escape ';' and ',' in the
174 * key values, and the parsing needs to handle that.
176 * @param address the address.
177 * @param entry return location to an array of entries.
178 * @param array_len return location for array length.
179 * @param error address where an error can be returned.
180 * @returns #TRUE on success, #FALSE otherwise.
183 dbus_parse_address (const char *address,
184 DBusAddressEntry ***entry,
189 int pos, end_pos, len, i;
190 DBusList *entries, *link;
191 DBusAddressEntry **entry_array;
193 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
195 _dbus_string_init_const (&str, address);
199 len = _dbus_string_get_length (&str);
203 DBusAddressEntry *entry;
207 entry = create_entry ();
210 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
215 /* Append the entry */
216 if (!_dbus_list_append (&entries, entry))
218 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
219 dbus_address_entry_free (entry);
223 /* Look for a semi-colon */
224 if (!_dbus_string_find (&str, pos, ";", &end_pos))
227 /* Look for the colon : */
228 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
230 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
234 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
236 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
242 while (pos < end_pos)
244 int comma_pos, equals_pos;
246 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
249 if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
250 equals_pos == pos || equals_pos + 1 == comma_pos)
252 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
253 "'=' character not found or has no value following it");
261 key = dbus_new0 (DBusString, 1);
265 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
269 value = dbus_new0 (DBusString, 1);
272 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
277 if (!_dbus_string_init (key))
279 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
286 if (!_dbus_string_init (value))
288 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
289 _dbus_string_free (key);
296 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
298 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
299 _dbus_string_free (key);
300 _dbus_string_free (value);
307 if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0))
309 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
310 _dbus_string_free (key);
311 _dbus_string_free (value);
318 if (!_dbus_list_append (&entry->keys, key))
320 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
321 _dbus_string_free (key);
322 _dbus_string_free (value);
329 if (!_dbus_list_append (&entry->values, value))
331 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
332 _dbus_string_free (value);
345 *array_len = _dbus_list_get_length (&entries);
347 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
351 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
356 entry_array [*array_len] = NULL;
358 link = _dbus_list_get_first_link (&entries);
362 entry_array[i] = link->data;
364 link = _dbus_list_get_next_link (&entries, link);
367 _dbus_list_clear (&entries);
368 *entry = entry_array;
374 link = _dbus_list_get_first_link (&entries);
377 dbus_address_entry_free (link->data);
378 link = _dbus_list_get_next_link (&entries, link);
381 _dbus_list_clear (&entries);
388 /** @} */ /* End of public API */
390 #ifdef DBUS_BUILD_TESTS
391 #include "dbus-test.h"
394 _dbus_address_test (void)
396 DBusAddressEntry **entries;
400 dbus_error_init (&error);
402 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
403 &entries, &len, &error))
404 _dbus_assert_not_reached ("could not parse address");
405 _dbus_assert (len == 2);
406 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
407 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
408 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
410 dbus_address_entries_free (entries);
412 /* Different possible errors */
413 if (dbus_parse_address ("foo", &entries, &len, &error))
414 _dbus_assert_not_reached ("Parsed incorrect address.");
416 dbus_error_free (&error);
418 if (dbus_parse_address ("foo:bar", &entries, &len, &error))
419 _dbus_assert_not_reached ("Parsed incorrect address.");
421 dbus_error_free (&error);
423 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
424 _dbus_assert_not_reached ("Parsed incorrect address.");
426 dbus_error_free (&error);
428 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
429 _dbus_assert_not_reached ("Parsed incorrect address.");
431 dbus_error_free (&error);
433 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
434 _dbus_assert_not_reached ("Parsed incorrect address.");
436 dbus_error_free (&error);
438 if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
439 _dbus_assert_not_reached ("Parsed incorrect address.");
441 dbus_error_free (&error);
443 if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
444 _dbus_assert_not_reached ("Parsed incorrect address.");
446 dbus_error_free (&error);
448 if (dbus_parse_address ("foo:foo,bar=baz", &entries, &len, &error))
449 _dbus_assert_not_reached ("Parsed incorrect address.");
451 dbus_error_free (&error);