1*2912Sartem /***************************************************************************
2*2912Sartem * CVSID: $Id$
3*2912Sartem *
4*2912Sartem * hal_find_by_property.c : Find hal devices
5*2912Sartem *
6*2912Sartem * Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
7*2912Sartem *
8*2912Sartem * Licensed under the Academic Free License version 2.1
9*2912Sartem *
10*2912Sartem * This program is free software; you can redistribute it and/or modify
11*2912Sartem * it under the terms of the GNU General Public License as published by
12*2912Sartem * the Free Software Foundation; either version 2 of the License, or
13*2912Sartem * (at your option) any later version.
14*2912Sartem *
15*2912Sartem * This program is distributed in the hope that it will be useful,
16*2912Sartem * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*2912Sartem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*2912Sartem * GNU General Public License for more details.
19*2912Sartem *
20*2912Sartem * You should have received a copy of the GNU General Public License
21*2912Sartem * along with this program; if not, write to the Free Software
22*2912Sartem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*2912Sartem *
24*2912Sartem **************************************************************************/
25*2912Sartem
26*2912Sartem
27*2912Sartem #ifdef HAVE_CONFIG_H
28*2912Sartem # include <config.h>
29*2912Sartem #endif
30*2912Sartem
31*2912Sartem #include <stdio.h>
32*2912Sartem #include <string.h>
33*2912Sartem #include <unistd.h>
34*2912Sartem #include <getopt.h>
35*2912Sartem
36*2912Sartem #include <libhal.h>
37*2912Sartem
38*2912Sartem
39*2912Sartem /** Print out program usage.
40*2912Sartem *
41*2912Sartem * @param argc Number of arguments given to program
42*2912Sartem * @param argv Arguments given to program
43*2912Sartem */
44*2912Sartem static void
usage(int argc,char * argv[])45*2912Sartem usage (int argc, char *argv[])
46*2912Sartem {
47*2912Sartem fprintf (stderr,
48*2912Sartem "\n"
49*2912Sartem "usage : hal-find-by-property --key <key> --string <value>\n"
50*2912Sartem " [--help] [--verbose] [--version]\n");
51*2912Sartem
52*2912Sartem /** @todo support other property types a'la hal-[get|set]-property */
53*2912Sartem
54*2912Sartem fprintf (stderr,
55*2912Sartem "\n"
56*2912Sartem " --key Key of the property to check\n"
57*2912Sartem " --string String value of property\n"
58*2912Sartem " --verbose Be verbose\n"
59*2912Sartem " --version Show version and exit\n"
60*2912Sartem " --help Show this information and exit\n"
61*2912Sartem "\n"
62*2912Sartem "This program prints the Unique Device Identifiers for HAL device\n"
63*2912Sartem "objects where a given property assumes a given value. On success\n"
64*2912Sartem "the program exists with exit code 0. If there is an error, the\n"
65*2912Sartem "program exits with an exit code different from 0.\n"
66*2912Sartem "\n");
67*2912Sartem }
68*2912Sartem
69*2912Sartem /** Entry point
70*2912Sartem *
71*2912Sartem * @param argc Number of arguments given to program
72*2912Sartem * @param argv Arguments given to program
73*2912Sartem * @return Return code
74*2912Sartem */
75*2912Sartem int
main(int argc,char * argv[])76*2912Sartem main (int argc, char *argv[])
77*2912Sartem {
78*2912Sartem int i;
79*2912Sartem int num_udis;
80*2912Sartem char **udis;
81*2912Sartem char *key = NULL;
82*2912Sartem char *value = NULL;
83*2912Sartem dbus_bool_t is_verbose = FALSE;
84*2912Sartem dbus_bool_t is_version = FALSE;
85*2912Sartem DBusError error;
86*2912Sartem LibHalContext *hal_ctx;
87*2912Sartem
88*2912Sartem if (argc <= 1) {
89*2912Sartem usage (argc, argv);
90*2912Sartem return 1;
91*2912Sartem }
92*2912Sartem
93*2912Sartem while (1) {
94*2912Sartem int c;
95*2912Sartem int option_index = 0;
96*2912Sartem const char *opt;
97*2912Sartem static struct option long_options[] = {
98*2912Sartem {"key", 1, NULL, 0},
99*2912Sartem {"string", 1, NULL, 0},
100*2912Sartem {"verbose", 0, NULL, 0},
101*2912Sartem {"version", 0, NULL, 0},
102*2912Sartem {"help", 0, NULL, 0},
103*2912Sartem {NULL, 0, NULL, 0}
104*2912Sartem };
105*2912Sartem
106*2912Sartem c = getopt_long (argc, argv, "",
107*2912Sartem long_options, &option_index);
108*2912Sartem if (c == -1)
109*2912Sartem break;
110*2912Sartem
111*2912Sartem switch (c) {
112*2912Sartem case 0:
113*2912Sartem opt = long_options[option_index].name;
114*2912Sartem
115*2912Sartem if (strcmp (opt, "help") == 0) {
116*2912Sartem usage (argc, argv);
117*2912Sartem return 0;
118*2912Sartem } else if (strcmp (opt, "verbose") == 0) {
119*2912Sartem is_verbose = TRUE;
120*2912Sartem } else if (strcmp (opt, "version") == 0) {
121*2912Sartem is_version = TRUE;
122*2912Sartem } else if (strcmp (opt, "key") == 0) {
123*2912Sartem key = strdup (optarg);
124*2912Sartem } else if (strcmp (opt, "string") == 0) {
125*2912Sartem value = strdup (optarg);
126*2912Sartem }
127*2912Sartem break;
128*2912Sartem
129*2912Sartem default:
130*2912Sartem usage (argc, argv);
131*2912Sartem return 1;
132*2912Sartem break;
133*2912Sartem }
134*2912Sartem }
135*2912Sartem
136*2912Sartem if (is_version) {
137*2912Sartem printf ("hal-find-by-property " PACKAGE_VERSION "\n");
138*2912Sartem return 0;
139*2912Sartem }
140*2912Sartem
141*2912Sartem if (key == NULL || value == NULL) {
142*2912Sartem usage (argc, argv);
143*2912Sartem return 1;
144*2912Sartem }
145*2912Sartem
146*2912Sartem dbus_error_init (&error);
147*2912Sartem if ((hal_ctx = libhal_ctx_new ()) == NULL) {
148*2912Sartem fprintf (stderr, "error: libhal_ctx_new\n");
149*2912Sartem return 1;
150*2912Sartem }
151*2912Sartem if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
152*2912Sartem fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
153*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error);
154*2912Sartem return 1;
155*2912Sartem }
156*2912Sartem if (!libhal_ctx_init (hal_ctx, &error)) {
157*2912Sartem if (dbus_error_is_set(&error)) {
158*2912Sartem fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
159*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error);
160*2912Sartem }
161*2912Sartem fprintf (stderr, "Could not initialise connection to hald.\n"
162*2912Sartem "Normally this means the HAL daemon (hald) is not running or not ready.\n");
163*2912Sartem return 1;
164*2912Sartem }
165*2912Sartem
166*2912Sartem
167*2912Sartem udis = libhal_manager_find_device_string_match (hal_ctx, key, value, &num_udis, &error);
168*2912Sartem
169*2912Sartem if (dbus_error_is_set (&error)) {
170*2912Sartem fprintf (stderr, "error: %s: %s\n", error.name, error.message);
171*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error);
172*2912Sartem return 1;
173*2912Sartem }
174*2912Sartem
175*2912Sartem if (is_verbose)
176*2912Sartem printf ("Found %d device objects with string property %s = '%s'\n", num_udis, key, value);
177*2912Sartem
178*2912Sartem if (num_udis == 0) {
179*2912Sartem return 1;
180*2912Sartem }
181*2912Sartem
182*2912Sartem for (i = 0; i < num_udis; i++) {
183*2912Sartem printf ("%s\n", udis[i]);
184*2912Sartem }
185*2912Sartem
186*2912Sartem libhal_free_string_array (udis);
187*2912Sartem
188*2912Sartem return 0;
189*2912Sartem }
190*2912Sartem
191*2912Sartem /**
192*2912Sartem * @}
193*2912Sartem */
194