1 /***************************************************************************
4 * hal_find_by_property.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-property --key <key> --string <value>\n"
50 " [--help] [--verbose] [--version]\n");
52 /** @todo support other property types a'la hal-[get|set]-property */
56 " --key Key of the property to check\n"
57 " --string String value of property\n"
58 " --verbose Be verbose\n"
59 " --version Show version and exit\n"
60 " --help Show this information and exit\n"
62 "This program prints the Unique Device Identifiers for HAL device\n"
63 "objects where a given property assumes a given value. On success\n"
64 "the program exists with exit code 0. If there is an error, the\n"
65 "program exits with an exit code different from 0.\n"
71 * @param argc Number of arguments given to program
72 * @param argv Arguments given to program
76 main (int argc
, char *argv
[])
83 dbus_bool_t is_verbose
= FALSE
;
84 dbus_bool_t is_version
= FALSE
;
86 LibHalContext
*hal_ctx
;
97 static struct option long_options
[] = {
99 {"string", 1, NULL
, 0},
100 {"verbose", 0, NULL
, 0},
101 {"version", 0, NULL
, 0},
102 {"help", 0, NULL
, 0},
106 c
= getopt_long (argc
, argv
, "",
107 long_options
, &option_index
);
113 opt
= long_options
[option_index
].name
;
115 if (strcmp (opt
, "help") == 0) {
118 } else if (strcmp (opt
, "verbose") == 0) {
120 } else if (strcmp (opt
, "version") == 0) {
122 } else if (strcmp (opt
, "key") == 0) {
123 key
= strdup (optarg
);
124 } else if (strcmp (opt
, "string") == 0) {
125 value
= strdup (optarg
);
137 printf ("hal-find-by-property " PACKAGE_VERSION
"\n");
141 if (key
== NULL
|| value
== NULL
) {
146 dbus_error_init (&error
);
147 if ((hal_ctx
= libhal_ctx_new ()) == NULL
) {
148 fprintf (stderr
, "error: libhal_ctx_new\n");
151 if (!libhal_ctx_set_dbus_connection (hal_ctx
, dbus_bus_get (DBUS_BUS_SYSTEM
, &error
))) {
152 fprintf (stderr
, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error
.name
, error
.message
);
153 LIBHAL_FREE_DBUS_ERROR (&error
);
156 if (!libhal_ctx_init (hal_ctx
, &error
)) {
157 if (dbus_error_is_set(&error
)) {
158 fprintf (stderr
, "error: libhal_ctx_init: %s: %s\n", error
.name
, error
.message
);
159 LIBHAL_FREE_DBUS_ERROR (&error
);
161 fprintf (stderr
, "Could not initialise connection to hald.\n"
162 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
167 udis
= libhal_manager_find_device_string_match (hal_ctx
, key
, value
, &num_udis
, &error
);
169 if (dbus_error_is_set (&error
)) {
170 fprintf (stderr
, "error: %s: %s\n", error
.name
, error
.message
);
171 LIBHAL_FREE_DBUS_ERROR (&error
);
176 printf ("Found %d device objects with string property %s = '%s'\n", num_udis
, key
, value
);
182 for (i
= 0; i
< num_udis
; i
++) {
183 printf ("%s\n", udis
[i
]);
186 libhal_free_string_array (udis
);