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 1.2
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"
30 * @defgroup DBusAddress Address parsing
32 * @brief Parsing addresses of D-BUS servers.
36 struct DBusAddressEntry
45 dbus_address_entry_free (DBusAddressEntry *entry)
49 _dbus_string_free (&entry->method);
51 link = _dbus_list_get_first_link (&entry->keys);
54 _dbus_string_free (link->data);
55 dbus_free (link->data);
57 link = _dbus_list_get_next_link (&entry->keys, link);
59 _dbus_list_clear (&entry->keys);
61 link = _dbus_list_get_first_link (&entry->values);
64 _dbus_string_free (link->data);
65 dbus_free (link->data);
67 link = _dbus_list_get_next_link (&entry->values, link);
69 _dbus_list_clear (&entry->values);
76 * Frees a #NULL-terminated array of address entries.
78 * @param entries the array.
81 dbus_address_entries_free (DBusAddressEntry **entries)
85 for (i = 0; entries[i] != NULL; i++)
86 dbus_address_entry_free (entries[i]);
90 static DBusAddressEntry *
93 DBusAddressEntry *entry;
95 entry = dbus_new0 (DBusAddressEntry, 1);
100 if (!_dbus_string_init (&entry->method))
110 * Returns the method string of an address entry.
112 * @param entry the entry.
113 * @returns a string describing the method. This string
117 dbus_address_entry_get_method (DBusAddressEntry *entry)
119 return _dbus_string_get_const_data (&entry->method);
123 * Returns a value from a key of an entry.
125 * @param entry the entry.
126 * @param key the key.
127 * @returns the key value. This string must not be fred.
130 dbus_address_entry_get_value (DBusAddressEntry *entry,
133 DBusList *values, *keys;
135 keys = _dbus_list_get_first_link (&entry->keys);
136 values = _dbus_list_get_first_link (&entry->values);
140 _dbus_assert (values != NULL);
142 if (_dbus_string_equal_c_str (keys->data, key))
143 return _dbus_string_get_const_data (values->data);
145 keys = _dbus_list_get_next_link (&entry->keys, keys);
146 values = _dbus_list_get_next_link (&entry->values, values);
153 * Parses an address string of the form:
155 * method:key=value,key=value;method:key=value
157 * @todo document address format in the specification
159 * @todo need to be able to escape ';' and ',' in the
160 * key values, and the parsing needs to handle that.
162 * @param address the address.
163 * @param entry return location to an array of entries.
164 * @param array_len return location for array length.
165 * @param error address where an error can be returned.
166 * @returns #TRUE on success, #FALSE otherwise.
169 dbus_parse_address (const char *address,
170 DBusAddressEntry ***entry,
175 int pos, end_pos, len, i;
176 DBusList *entries, *link;
177 DBusAddressEntry **entry_array;
179 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
181 _dbus_string_init_const (&str, address);
185 len = _dbus_string_get_length (&str);
189 DBusAddressEntry *entry;
193 entry = create_entry ();
196 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
201 /* Append the entry */
202 if (!_dbus_list_append (&entries, entry))
204 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
205 dbus_address_entry_free (entry);
209 /* Look for a semi-colon */
210 if (!_dbus_string_find (&str, pos, ";", &end_pos))
213 /* Look for the colon : */
214 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
216 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
220 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
222 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
228 while (pos < end_pos)
230 int comma_pos, equals_pos;
232 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
235 if (!_dbus_string_find (&str, pos, "=", &equals_pos) ||
236 equals_pos == pos || equals_pos + 1 == end_pos)
238 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
239 "'=' character not found or has no value following it");
247 key = dbus_new0 (DBusString, 1);
251 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
255 value = dbus_new0 (DBusString, 1);
258 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
263 if (!_dbus_string_init (key))
265 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
272 if (!_dbus_string_init (value))
274 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
275 _dbus_string_free (key);
282 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
284 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
285 _dbus_string_free (key);
286 _dbus_string_free (value);
293 if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0))
295 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
296 _dbus_string_free (key);
297 _dbus_string_free (value);
304 if (!_dbus_list_append (&entry->keys, key))
306 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
307 _dbus_string_free (key);
308 _dbus_string_free (value);
315 if (!_dbus_list_append (&entry->values, value))
317 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
318 _dbus_string_free (value);
331 *array_len = _dbus_list_get_length (&entries);
333 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
337 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
342 entry_array [*array_len] = NULL;
344 link = _dbus_list_get_first_link (&entries);
348 entry_array[i] = link->data;
350 link = _dbus_list_get_next_link (&entries, link);
353 _dbus_list_clear (&entries);
354 *entry = entry_array;
360 link = _dbus_list_get_first_link (&entries);
363 dbus_address_entry_free (link->data);
364 link = _dbus_list_get_next_link (&entries, link);
367 _dbus_list_clear (&entries);
376 #ifdef DBUS_BUILD_TESTS
377 #include "dbus-test.h"
380 _dbus_address_test (void)
382 DBusAddressEntry **entries;
386 dbus_error_init (&error);
388 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
389 &entries, &len, &error))
390 _dbus_assert_not_reached ("could not parse address");
391 _dbus_assert (len == 2);
392 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
393 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
394 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
396 dbus_address_entries_free (entries);
398 /* Different possible errors */
399 if (dbus_parse_address ("foo", &entries, &len, &error))
400 _dbus_assert_not_reached ("Parsed incorrect address.");
402 dbus_error_free (&error);
404 if (dbus_parse_address ("foo:bar", &entries, &len, &error))
405 _dbus_assert_not_reached ("Parsed incorrect address.");
407 dbus_error_free (&error);
409 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
410 _dbus_assert_not_reached ("Parsed incorrect address.");
412 dbus_error_free (&error);
414 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
415 _dbus_assert_not_reached ("Parsed incorrect address.");
417 dbus_error_free (&error);
419 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
420 _dbus_assert_not_reached ("Parsed incorrect address.");
422 dbus_error_free (&error);
424 if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
425 _dbus_assert_not_reached ("Parsed incorrect address.");
427 dbus_error_free (&error);
429 if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
430 _dbus_assert_not_reached ("Parsed incorrect address.");
432 dbus_error_free (&error);