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, _DBUS_INT_MAX))
107 * Returns the method string of an address entry.
109 * @param entry the entry.
110 * @returns a string describing the method. This string
114 dbus_address_entry_get_method (DBusAddressEntry *entry)
118 _dbus_string_get_const_data (&entry->method, &method);
124 * Returns a value from a key of an entry.
126 * @param entry the entry.
127 * @param key the key.
128 * @returns the key value. This string must not be fred.
131 dbus_address_entry_get_value (DBusAddressEntry *entry,
134 DBusList *values, *keys;
136 keys = _dbus_list_get_first_link (&entry->keys);
137 values = _dbus_list_get_first_link (&entry->values);
141 _dbus_assert (values != NULL);
143 if (_dbus_string_equal_c_str (keys->data, key))
147 _dbus_string_get_const_data (values->data, &str);
150 keys = _dbus_list_get_next_link (&entry->keys, keys);
151 values = _dbus_list_get_next_link (&entry->values, values);
158 * Parses an address string of the form:
160 * method:key=value,key=value;method:key=value
162 * @todo document address format in the specification
164 * @todo need to be able to escape ';' and ',' in the
165 * key values, and the parsing needs to handle that.
167 * @param address the address.
168 * @param entry return location to an array of entries.
169 * @param array_len return location for array length.
170 * @param result return location for result code.
171 * @returns #TRUE on success, #FALSE otherwise.
174 dbus_parse_address (const char *address,
175 DBusAddressEntry ***entry,
177 DBusResultCode *result)
180 int pos, end_pos, len, i;
181 DBusList *entries, *link;
182 DBusAddressEntry **entry_array;
184 _dbus_string_init_const (&str, address);
188 len = _dbus_string_get_length (&str);
192 DBusAddressEntry *entry;
196 entry = create_entry ();
199 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
204 /* Append the entry */
205 if (!_dbus_list_append (&entries, entry))
207 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
208 dbus_address_entry_free (entry);
212 /* Look for a semi-colon */
213 if (!_dbus_string_find (&str, pos, ";", &end_pos))
216 /* Look for the colon : */
217 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
219 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
223 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
225 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
231 while (pos < end_pos)
233 int comma_pos, equals_pos;
235 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
238 if (!_dbus_string_find (&str, pos, "=", &equals_pos) ||
239 equals_pos == pos || equals_pos + 1 == end_pos)
241 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
249 key = dbus_new0 (DBusString, 1);
253 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
257 value = dbus_new0 (DBusString, 1);
260 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
265 if (!_dbus_string_init (key, _DBUS_INT_MAX))
267 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
274 if (!_dbus_string_init (value, _DBUS_INT_MAX))
276 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
277 _dbus_string_free (key);
284 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
286 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
287 _dbus_string_free (key);
288 _dbus_string_free (value);
295 if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0))
297 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
298 _dbus_string_free (key);
299 _dbus_string_free (value);
306 if (!_dbus_list_append (&entry->keys, key))
308 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
309 _dbus_string_free (key);
310 _dbus_string_free (value);
317 if (!_dbus_list_append (&entry->values, value))
319 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
320 _dbus_string_free (value);
333 *array_len = _dbus_list_get_length (&entries);
335 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
339 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
344 entry_array [*array_len] = NULL;
346 link = _dbus_list_get_first_link (&entries);
350 entry_array[i] = link->data;
352 link = _dbus_list_get_next_link (&entries, link);
355 _dbus_list_clear (&entries);
356 *entry = entry_array;
358 dbus_set_result (result, DBUS_RESULT_SUCCESS);
363 link = _dbus_list_get_first_link (&entries);
366 dbus_address_entry_free (link->data);
367 link = _dbus_list_get_next_link (&entries, link);
377 #ifdef DBUS_BUILD_TESTS
378 #include "dbus-test.h"
381 _dbus_address_test (void)
383 DBusAddressEntry **entries;
385 DBusResultCode result;
387 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
388 &entries, &len, &result))
389 _dbus_assert_not_reached ("could not parse address");
390 _dbus_assert (len == 2);
391 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
392 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
393 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
395 dbus_address_entries_free (entries);
397 /* Different possible errors */
398 if (dbus_parse_address ("foo", &entries, &len, &result))
399 _dbus_assert_not_reached ("Parsed incorrect address.");
401 if (dbus_parse_address ("foo:bar", &entries, &len, &result))
402 _dbus_assert_not_reached ("Parsed incorrect address.");
404 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &result))
405 _dbus_assert_not_reached ("Parsed incorrect address.");
407 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &result))
408 _dbus_assert_not_reached ("Parsed incorrect address.");
410 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &result))
411 _dbus_assert_not_reached ("Parsed incorrect address.");
413 if (dbus_parse_address ("foo:=foo", &entries, &len, &result))
414 _dbus_assert_not_reached ("Parsed incorrect address.");
416 if (dbus_parse_address ("foo:foo=", &entries, &len, &result))
417 _dbus_assert_not_reached ("Parsed incorrect address.");