2 This file is part of PulseAudio.
4 Copyright 2008 Joao Paulo Rechi Vita
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulsecore/core-util.h>
27 #include <modules/dbus-util.h>
29 #include "bluetooth-util.h"
37 struct pa_bluetooth_discovery
{
38 DBusConnection
*connection
;
39 PA_LLIST_HEAD(pa_dbus_pending
, pending
);
43 /* If mode == MODE_FIND look for a specific device by its address.
44 If mode == MODE_GET look for a specific device by its path. */
45 const char *looking_for
;
46 pa_bluetooth_device
*found_device
;
48 /* If looking_for is NULL we do long-time discovery */
50 pa_bluetooth_device_callback_t callback
;
51 struct userdata
*userdata
;
54 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
57 u
= pa_xnew(pa_bluetooth_uuid
, 1);
58 u
->uuid
= pa_xstrdup(uuid
);
59 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
64 static void uuid_free(pa_bluetooth_uuid
*u
) {
71 static pa_bluetooth_device
* device_new(const char *path
) {
72 pa_bluetooth_device
*d
;
74 d
= pa_xnew(pa_bluetooth_device
, 1);
76 d
->device_info_valid
= d
->audio_sink_info_valid
= d
->headset_info_valid
= 0;
81 d
->path
= pa_xstrdup(path
);
84 d
->device_connected
= -1;
85 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
90 d
->audio_sink_connected
= -1;
92 d
->headset_connected
= -1;
97 void pa_bluetooth_device_free(pa_bluetooth_device
*d
) {
102 while ((u
= d
->uuids
)) {
103 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
110 pa_xfree(d
->address
);
114 static pa_bool_t
device_is_loaded(pa_bluetooth_device
*d
) {
117 return d
->device_info_valid
&& d
->audio_sink_info_valid
&& d
->headset_info_valid
;
120 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
123 pa_assert(d
->device_info_valid
);
124 pa_assert(d
->audio_sink_info_valid
);
125 pa_assert(d
->headset_info_valid
);
127 return d
->device_info_valid
> 0 &&
128 (d
->audio_sink_info_valid
> 0 || d
->headset_info_valid
> 0);
131 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
133 DBusMessageIter variant_i
;
139 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
140 pa_log("Property name not a string.");
144 dbus_message_iter_get_basic(i
, &key
);
146 if (!dbus_message_iter_next(i
)) {
147 pa_log("Property value missing");
151 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
152 pa_log("Property value not a variant.");
156 dbus_message_iter_recurse(i
, &variant_i
);
158 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
160 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
162 case DBUS_TYPE_STRING
: {
165 dbus_message_iter_get_basic(&variant_i
, &value
);
167 if (pa_streq(key
, "Name")) {
169 d
->name
= pa_xstrdup(value
);
170 } else if (pa_streq(key
, "Alias")) {
172 d
->alias
= pa_xstrdup(value
);
173 } else if (pa_streq(key
, "Address")) {
174 pa_xfree(d
->address
);
175 d
->address
= pa_xstrdup(value
);
178 /* pa_log_debug("Value %s", value); */
183 case DBUS_TYPE_BOOLEAN
: {
186 dbus_message_iter_get_basic(&variant_i
, &value
);
188 if (pa_streq(key
, "Paired"))
190 else if (pa_streq(key
, "Connected"))
191 d
->device_connected
= !!value
;
192 else if (pa_streq(key
, "Trusted"))
193 d
->trusted
= !!value
;
195 /* pa_log_debug("Value %s", pa_yes_no(value)); */
200 case DBUS_TYPE_UINT32
: {
203 dbus_message_iter_get_basic(&variant_i
, &value
);
205 if (pa_streq(key
, "Class"))
206 d
->class = (int) value
;
208 /* pa_log_debug("Value %u", (unsigned) value); */
213 case DBUS_TYPE_ARRAY
: {
216 dbus_message_iter_recurse(&variant_i
, &ai
);
218 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
219 pa_streq(key
, "UUIDs")) {
221 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
222 pa_bluetooth_uuid
*node
;
225 dbus_message_iter_get_basic(&ai
, &value
);
226 node
= uuid_new(value
);
227 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
229 if (!dbus_message_iter_next(&ai
))
241 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *connected
, DBusMessageIter
*i
) {
243 DBusMessageIter variant_i
;
246 pa_assert(connected
);
249 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
250 pa_log("Property name not a string.");
254 dbus_message_iter_get_basic(i
, &key
);
256 if (!dbus_message_iter_next(i
)) {
257 pa_log("Property value missing");
261 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
262 pa_log("Property value not a variant.");
266 dbus_message_iter_recurse(i
, &variant_i
);
268 /* pa_log_debug("Parsing property org.bluez.{AudioSink|Headset}.%s", key); */
270 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
272 case DBUS_TYPE_BOOLEAN
: {
275 dbus_message_iter_get_basic(&variant_i
, &value
);
277 if (pa_streq(key
, "Connected"))
278 *connected
= !!value
;
280 /* pa_log_debug("Value %s", pa_yes_no(value)); */
289 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t good
) {
293 if (y
->mode
!= MODE_DISCOVER
)
296 if (!device_is_loaded(d
))
299 if (!device_is_audio(d
))
302 y
->callback(y
->userdata
, d
, good
);
306 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
308 DBusMessageIter arg_i
, element_i
;
310 pa_bluetooth_device
*d
;
311 pa_bluetooth_discovery
*y
;
314 pa_assert_se(p
= userdata
);
315 pa_assert_se(y
= p
->context_data
);
316 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
318 /* pa_log_debug("Got %s.GetProperties response for %s", */
319 /* dbus_message_get_interface(p->message), */
320 /* dbus_message_get_path(p->message)); */
324 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
326 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
327 d
->device_info_valid
= valid
;
328 else if (dbus_message_is_method_call(p
->message
, "org.bluez.Headset", "GetProperties"))
329 d
->headset_info_valid
= valid
;
330 else if (dbus_message_is_method_call(p
->message
, "org.bluez.AudioSink", "GetProperties"))
331 d
->audio_sink_info_valid
= valid
;
333 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
335 if (!dbus_message_is_error(r
, DBUS_ERROR_UNKNOWN_METHOD
))
336 pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r
));
341 if (!dbus_message_iter_init(r
, &arg_i
)) {
342 pa_log("GetProperties reply has no arguments.");
346 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
347 pa_log("GetProperties argument is not an array.");
351 dbus_message_iter_recurse(&arg_i
, &element_i
);
352 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
354 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
355 DBusMessageIter dict_i
;
357 dbus_message_iter_recurse(&element_i
, &dict_i
);
359 if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
360 if (parse_device_property(y
, d
, &dict_i
) < 0)
363 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
364 if (parse_audio_property(y
, &d
->headset_connected
, &dict_i
) < 0)
367 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
368 if (parse_audio_property(y
, &d
->audio_sink_connected
, &dict_i
) < 0)
373 if (!dbus_message_iter_next(&element_i
))
378 run_callback(y
, d
, TRUE
);
380 dbus_message_unref(r
);
382 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
383 pa_dbus_pending_free(p
);
386 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
) {
388 DBusPendingCall
*call
;
393 pa_assert_se(dbus_connection_send_with_reply(y
->connection
, m
, &call
, -1));
395 p
= pa_dbus_pending_new(m
, call
, y
, d
);
396 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
397 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
402 static void found_device(pa_bluetooth_discovery
*y
, const char* path
) {
404 pa_bluetooth_device
*d
;
409 d
= device_new(path
);
411 if (y
->mode
== MODE_DISCOVER
) {
412 pa_assert(y
->devices
);
413 pa_hashmap_put(y
->devices
, d
->path
, d
);
415 pa_assert(!y
->found_device
);
419 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
420 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
422 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Headset", "GetProperties"));
423 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
425 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.AudioSink", "GetProperties"));
426 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
429 static void list_devices_reply(DBusPendingCall
*pending
, void *userdata
) {
435 pa_bluetooth_discovery
*y
;
441 pa_assert_se(p
= userdata
);
442 pa_assert_se(y
= p
->context_data
);
443 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
445 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
446 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r
));
450 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
451 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e
.message
);
456 for (i
= 0; i
< num
; ++i
)
457 found_device(y
, paths
[i
]);
462 dbus_free_string_array (paths
);
464 dbus_message_unref(r
);
466 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
467 pa_dbus_pending_free(p
);
470 static void find_device_reply(DBusPendingCall
*pending
, void *userdata
) {
475 pa_bluetooth_discovery
*y
;
481 pa_assert_se(p
= userdata
);
482 pa_assert_se(y
= p
->context_data
);
483 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
485 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
486 pa_log("Error from FindDevice reply: %s", dbus_message_get_error_name(r
));
490 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
491 pa_log("org.bluez.Adapter.FindDevice returned an error: '%s'\n", e
.message
);
494 found_device(y
, path
);
497 dbus_message_unref(r
);
499 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
500 pa_dbus_pending_free(p
);
503 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
506 if (y
->mode
== MODE_FIND
) {
507 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "FindDevice"));
509 pa_assert_se(dbus_message_append_args(m
,
510 DBUS_TYPE_STRING
, &y
->looking_for
,
513 send_and_add_to_pending(y
, NULL
, m
, find_device_reply
);
516 pa_assert(y
->mode
== MODE_DISCOVER
);
518 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "ListDevices"));
519 send_and_add_to_pending(y
, NULL
, m
, list_devices_reply
);
523 static void list_adapters_reply(DBusPendingCall
*pending
, void *userdata
) {
529 pa_bluetooth_discovery
*y
;
535 pa_assert_se(p
= userdata
);
536 pa_assert_se(y
= p
->context_data
);
537 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
539 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
540 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r
));
544 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
545 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e
.message
);
550 for (i
= 0; i
< num
; ++i
)
551 found_adapter(y
, paths
[i
]);
556 dbus_free_string_array (paths
);
558 dbus_message_unref(r
);
560 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
561 pa_dbus_pending_free(p
);
564 static void list_adapters(pa_bluetooth_discovery
*y
) {
568 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
569 send_and_add_to_pending(y
, NULL
, m
, list_adapters_reply
);
572 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
574 pa_bluetooth_discovery
*y
;
579 pa_assert_se(y
= userdata
);
581 dbus_error_init(&err
);
583 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
584 dbus_message_get_interface(m
),
585 dbus_message_get_path(m
),
586 dbus_message_get_member(m
));
588 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
590 pa_bluetooth_device
*d
;
592 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
593 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
597 pa_log_debug("Device %s removed", path
);
599 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
601 pa_assert_se(y
->mode
== MODE_DISCOVER
);
602 run_callback(y
, d
, FALSE
);
604 pa_bluetooth_device_free(d
);
607 return DBUS_HANDLER_RESULT_HANDLED
;
609 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
612 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
613 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
617 pa_log_debug("Device %s created", path
);
619 found_device(y
, path
);
620 return DBUS_HANDLER_RESULT_HANDLED
;
622 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
625 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
626 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
630 pa_log_debug("Adapter %s created", path
);
632 found_adapter(y
, path
);
633 return DBUS_HANDLER_RESULT_HANDLED
;
635 } else if (dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
636 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
637 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
639 pa_bluetooth_device
*d
;
641 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
642 DBusMessageIter arg_i
;
644 if (!dbus_message_iter_init(m
, &arg_i
)) {
645 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
649 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
650 if (parse_device_property(y
, d
, &arg_i
) < 0)
653 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
654 if (parse_audio_property(y
, &d
->headset_connected
, &arg_i
) < 0)
657 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
658 if (parse_audio_property(y
, &d
->audio_sink_connected
, &arg_i
) < 0)
662 pa_assert_se(y
->mode
== MODE_DISCOVER
);
663 run_callback(y
, d
, TRUE
);
666 return DBUS_HANDLER_RESULT_HANDLED
;
670 dbus_error_free(&err
);
672 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
675 pa_bluetooth_device
* pa_bluetooth_find_device(DBusConnection
*c
, const char* address
) {
676 pa_bluetooth_discovery y
;
678 memset(&y
, 0, sizeof(y
));
680 y
.looking_for
= address
;
682 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
.pending
);
686 pa_dbus_sync_pending_list(&y
.pending
);
687 pa_assert(!y
.pending
);
689 if (y
.found_device
) {
690 pa_assert(device_is_loaded(y
.found_device
));
692 if (!device_is_audio(y
.found_device
)) {
693 pa_bluetooth_device_free(y
.found_device
);
698 return y
.found_device
;
701 pa_bluetooth_device
* pa_bluetooth_get_device(DBusConnection
*c
, const char* path
) {
702 pa_bluetooth_discovery y
;
704 memset(&y
, 0, sizeof(y
));
707 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
.pending
);
709 found_device(&y
, path
);
711 pa_dbus_sync_pending_list(&y
.pending
);
712 pa_assert(!y
.pending
);
714 if (y
.found_device
) {
715 pa_assert(device_is_loaded(y
.found_device
));
717 if (!device_is_audio(y
.found_device
)) {
718 pa_bluetooth_device_free(y
.found_device
);
723 return y
.found_device
;
726 pa_bluetooth_discovery
* pa_bluetooth_discovery_new(DBusConnection
*c
, pa_bluetooth_device_callback_t cb
, struct userdata
*u
) {
728 pa_bluetooth_discovery
*y
;
733 dbus_error_init(&err
);
735 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
736 y
->mode
= MODE_DISCOVER
;
740 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
741 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
743 /* dynamic detection of bluetooth audio devices */
744 if (!dbus_connection_add_filter(c
, filter_cb
, y
, NULL
)) {
745 pa_log_error("Failed to add filter function");
749 if (pa_dbus_add_matches(
751 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
752 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
753 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
754 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
755 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
756 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL
) < 0) {
757 pa_log("Failed to add D-Bus matches: %s", err
.message
);
766 dbus_error_free(&err
);
770 void pa_bluetooth_discovery_free(pa_bluetooth_discovery
*y
) {
771 pa_bluetooth_device
*d
;
775 pa_dbus_free_pending_list(&y
->pending
);
778 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
779 run_callback(y
, d
, FALSE
);
780 pa_bluetooth_device_free(d
);
783 pa_hashmap_free(y
->devices
, NULL
, NULL
);
787 pa_dbus_remove_matches(y
->connection
,
788 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
789 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
790 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
791 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
792 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
793 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
794 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL
);
796 dbus_connection_remove_filter(y
->connection
, filter_cb
, y
);
800 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
803 pa_dbus_sync_pending_list(&y
->pending
);
806 const char*pa_bluetooth_get_form_factor(uint32_t class) {
810 static const char * const table
[] = {
821 if (((class >> 8) & 31) != 4)
824 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
830 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
835 char *pa_bluetooth_cleanup_name(const char *name
) {
837 pa_bool_t space
= FALSE
;
841 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
844 t
= pa_xstrdup(name
);
846 for (s
= d
= t
; *s
; s
++) {
848 if (*s
<= 32 || *s
>= 127 || *s
== '_') {