1 /***************************************************************************
4 * hal_find_by_capability.c : Find hal devices
6 * Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 **************************************************************************/
39 /** Print out program usage.
41 * @param argc Number of arguments given to program
42 * @param argv Arguments given to program
45 usage (int argc
, char *argv
[])
49 "usage : hal-find-by-capability --capability <capability>\n"
50 " [--help] [--verbose] [--version]\n");
53 " --capability HAL Device Capability to search for\n"
54 " --verbose Be verbose\n"
55 " --version Show version and exit\n"
56 " --help Show this information and exit\n"
58 "This program prints the Unique Device Identifiers for HAL device\n"
59 "objects of a given capability on stdout and exits with exit code 0\n"
60 "If there is an error, the program exits with an exit code different\n"
67 * @param argc Number of arguments given to program
68 * @param argv Arguments given to program
72 main (int argc
, char *argv
[])
77 char *capability
= NULL
;
78 dbus_bool_t is_verbose
= FALSE
;
79 dbus_bool_t is_version
= FALSE
;
81 LibHalContext
*hal_ctx
;
92 static struct option long_options
[] = {
93 {"capability", 1, NULL
, 0},
94 {"verbose", 0, NULL
, 0},
95 {"version", 0, NULL
, 0},
100 c
= getopt_long (argc
, argv
, "",
101 long_options
, &option_index
);
107 opt
= long_options
[option_index
].name
;
109 if (strcmp (opt
, "help") == 0) {
112 } else if (strcmp (opt
, "verbose") == 0) {
114 } else if (strcmp (opt
, "version") == 0) {
116 } else if (strcmp (opt
, "capability") == 0) {
117 capability
= strdup (optarg
);
129 printf ("hal-find-by-capability " PACKAGE_VERSION
"\n");
133 if (capability
== NULL
) {
138 dbus_error_init (&error
);
139 if ((hal_ctx
= libhal_ctx_new ()) == NULL
) {
140 fprintf (stderr
, "error: libhal_ctx_new\n");
141 LIBHAL_FREE_DBUS_ERROR (&error
);
144 if (!libhal_ctx_set_dbus_connection (hal_ctx
, dbus_bus_get (DBUS_BUS_SYSTEM
, &error
))) {
145 fprintf (stderr
, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error
.name
, error
.message
);
146 LIBHAL_FREE_DBUS_ERROR (&error
);
149 if (!libhal_ctx_init (hal_ctx
, &error
)) {
150 if (dbus_error_is_set(&error
)) {
151 fprintf (stderr
, "error: libhal_ctx_init: %s: %s\n", error
.name
, error
.message
);
152 LIBHAL_FREE_DBUS_ERROR (&error
);
154 fprintf (stderr
, "Could not initialise connection to hald.\n"
155 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
160 udis
= libhal_find_device_by_capability (hal_ctx
, capability
, &num_udis
, &error
);
162 if (dbus_error_is_set (&error
)) {
163 fprintf (stderr
, "error: %s: %s\n", error
.name
, error
.message
);
164 LIBHAL_FREE_DBUS_ERROR (&error
);
169 printf ("Found %d device objects of capability '%s'\n", num_udis
, capability
);
175 for (i
= 0; i
< num_udis
; i
++) {
176 printf ("%s\n", udis
[i
]);
179 libhal_free_string_array (udis
);