xref: /onnv-gate/usr/src/cmd/hal/tools/hal_find_by_capability.c (revision 2912:85ea316d9c18)
1*2912Sartem /***************************************************************************
2*2912Sartem  * CVSID: $Id$
3*2912Sartem  *
4*2912Sartem  * hal_find_by_capability.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-capability --capability <capability>\n"
50*2912Sartem  "                              [--help] [--verbose] [--version]\n");
51*2912Sartem 	fprintf (stderr,
52*2912Sartem  "\n"
53*2912Sartem  "        --capability     HAL Device Capability to search for\n"
54*2912Sartem  "        --verbose        Be verbose\n"
55*2912Sartem  "        --version        Show version and exit\n"
56*2912Sartem  "        --help           Show this information and exit\n"
57*2912Sartem  "\n"
58*2912Sartem  "This program prints the Unique Device Identifiers for HAL device\n"
59*2912Sartem  "objects of a given capability on stdout and exits with exit code 0\n"
60*2912Sartem  "If there is an error, the program exits with an exit code different\n"
61*2912Sartem  "from 0.\n"
62*2912Sartem  "\n");
63*2912Sartem }
64*2912Sartem 
65*2912Sartem /** Entry point
66*2912Sartem  *
67*2912Sartem  *  @param  argc                Number of arguments given to program
68*2912Sartem  *  @param  argv                Arguments given to program
69*2912Sartem  *  @return                     Return code
70*2912Sartem  */
71*2912Sartem int
main(int argc,char * argv[])72*2912Sartem main (int argc, char *argv[])
73*2912Sartem {
74*2912Sartem 	int i;
75*2912Sartem 	int num_udis;
76*2912Sartem 	char **udis;
77*2912Sartem 	char *capability = NULL;
78*2912Sartem 	dbus_bool_t is_verbose = FALSE;
79*2912Sartem 	dbus_bool_t is_version = FALSE;
80*2912Sartem 	DBusError error;
81*2912Sartem 	LibHalContext *hal_ctx;
82*2912Sartem 
83*2912Sartem 	if (argc <= 1) {
84*2912Sartem 		usage (argc, argv);
85*2912Sartem 		return 1;
86*2912Sartem 	}
87*2912Sartem 
88*2912Sartem 	while (1) {
89*2912Sartem 		int c;
90*2912Sartem 		int option_index = 0;
91*2912Sartem 		const char *opt;
92*2912Sartem 		static struct option long_options[] = {
93*2912Sartem 			{"capability", 1, NULL, 0},
94*2912Sartem 			{"verbose", 0, NULL, 0},
95*2912Sartem 			{"version", 0, NULL, 0},
96*2912Sartem 			{"help", 0, NULL, 0},
97*2912Sartem 			{NULL, 0, NULL, 0}
98*2912Sartem 		};
99*2912Sartem 
100*2912Sartem 		c = getopt_long (argc, argv, "",
101*2912Sartem 				 long_options, &option_index);
102*2912Sartem 		if (c == -1)
103*2912Sartem 			break;
104*2912Sartem 
105*2912Sartem 		switch (c) {
106*2912Sartem 		case 0:
107*2912Sartem 			opt = long_options[option_index].name;
108*2912Sartem 
109*2912Sartem 			if (strcmp (opt, "help") == 0) {
110*2912Sartem 				usage (argc, argv);
111*2912Sartem 				return 0;
112*2912Sartem 			} else if (strcmp (opt, "verbose") == 0) {
113*2912Sartem 				is_verbose = TRUE;
114*2912Sartem 			} else if (strcmp (opt, "version") == 0) {
115*2912Sartem 				is_version = TRUE;
116*2912Sartem 			} else if (strcmp (opt, "capability") == 0) {
117*2912Sartem 				capability = strdup (optarg);
118*2912Sartem 			}
119*2912Sartem 			break;
120*2912Sartem 
121*2912Sartem 		default:
122*2912Sartem 			usage (argc, argv);
123*2912Sartem 			return 1;
124*2912Sartem 			break;
125*2912Sartem 		}
126*2912Sartem 	}
127*2912Sartem 
128*2912Sartem 	if (is_version) {
129*2912Sartem 		printf ("hal-find-by-capability " PACKAGE_VERSION "\n");
130*2912Sartem 		return 0;
131*2912Sartem 	}
132*2912Sartem 
133*2912Sartem 	if (capability == NULL) {
134*2912Sartem 		usage (argc, argv);
135*2912Sartem 		return 1;
136*2912Sartem 	}
137*2912Sartem 
138*2912Sartem 	dbus_error_init (&error);
139*2912Sartem 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
140*2912Sartem 		fprintf (stderr, "error: libhal_ctx_new\n");
141*2912Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
142*2912Sartem 		return 1;
143*2912Sartem 	}
144*2912Sartem 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
145*2912Sartem 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
146*2912Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
147*2912Sartem 		return 1;
148*2912Sartem 	}
149*2912Sartem 	if (!libhal_ctx_init (hal_ctx, &error)) {
150*2912Sartem 		if (dbus_error_is_set(&error)) {
151*2912Sartem 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
152*2912Sartem 			LIBHAL_FREE_DBUS_ERROR (&error);
153*2912Sartem 		}
154*2912Sartem 		fprintf (stderr, "Could not initialise connection to hald.\n"
155*2912Sartem 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
156*2912Sartem 		return 1;
157*2912Sartem 	}
158*2912Sartem 
159*2912Sartem 
160*2912Sartem 	udis = libhal_find_device_by_capability (hal_ctx, capability, &num_udis, &error);
161*2912Sartem 
162*2912Sartem 	if (dbus_error_is_set (&error)) {
163*2912Sartem 		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
164*2912Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
165*2912Sartem 		return 1;
166*2912Sartem 	}
167*2912Sartem 
168*2912Sartem 	if (is_verbose)
169*2912Sartem 		printf ("Found %d device objects of capability '%s'\n", num_udis, capability);
170*2912Sartem 
171*2912Sartem 	if (num_udis == 0) {
172*2912Sartem 		return 1;
173*2912Sartem 	}
174*2912Sartem 
175*2912Sartem 	for (i = 0; i < num_udis; i++) {
176*2912Sartem 		printf ("%s\n", udis[i]);
177*2912Sartem 	}
178*2912Sartem 
179*2912Sartem 	libhal_free_string_array (udis);
180*2912Sartem 
181*2912Sartem 	return 0;
182*2912Sartem }
183*2912Sartem 
184*2912Sartem /**
185*2912Sartem  * @}
186*2912Sartem  */
187