xref: /illumos-gate/usr/src/cmd/hal/tools/hal_find_by_capability.c (revision 4f364e7c95ee7fd9d5bbeddc1940e92405bb0e72)
1 /***************************************************************************
2  * CVSID: $Id$
3  *
4  * hal_find_by_capability.c : Find hal devices
5  *
6  * Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
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.
14  *
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.
19  *
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
23  *
24  **************************************************************************/
25 
26 
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 
36 #include <libhal.h>
37 
38 
39 /** Print out program usage.
40  *
41  *  @param  argc                Number of arguments given to program
42  *  @param  argv                Arguments given to program
43  */
44 static void
45 usage (int argc, char *argv[])
46 {
47 	fprintf (stderr,
48  "\n"
49  "usage : hal-find-by-capability --capability <capability>\n"
50  "                              [--help] [--verbose] [--version]\n");
51 	fprintf (stderr,
52  "\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"
57  "\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"
61  "from 0.\n"
62  "\n");
63 }
64 
65 /** Entry point
66  *
67  *  @param  argc                Number of arguments given to program
68  *  @param  argv                Arguments given to program
69  *  @return                     Return code
70  */
71 int
72 main (int argc, char *argv[])
73 {
74 	int i;
75 	int num_udis;
76 	char **udis;
77 	char *capability = NULL;
78 	dbus_bool_t is_verbose = FALSE;
79 	dbus_bool_t is_version = FALSE;
80 	DBusError error;
81 	LibHalContext *hal_ctx;
82 
83 	if (argc <= 1) {
84 		usage (argc, argv);
85 		return 1;
86 	}
87 
88 	while (1) {
89 		int c;
90 		int option_index = 0;
91 		const char *opt;
92 		static struct option long_options[] = {
93 			{"capability", 1, NULL, 0},
94 			{"verbose", 0, NULL, 0},
95 			{"version", 0, NULL, 0},
96 			{"help", 0, NULL, 0},
97 			{NULL, 0, NULL, 0}
98 		};
99 
100 		c = getopt_long (argc, argv, "",
101 				 long_options, &option_index);
102 		if (c == -1)
103 			break;
104 
105 		switch (c) {
106 		case 0:
107 			opt = long_options[option_index].name;
108 
109 			if (strcmp (opt, "help") == 0) {
110 				usage (argc, argv);
111 				return 0;
112 			} else if (strcmp (opt, "verbose") == 0) {
113 				is_verbose = TRUE;
114 			} else if (strcmp (opt, "version") == 0) {
115 				is_version = TRUE;
116 			} else if (strcmp (opt, "capability") == 0) {
117 				capability = strdup (optarg);
118 			}
119 			break;
120 
121 		default:
122 			usage (argc, argv);
123 			return 1;
124 			break;
125 		}
126 	}
127 
128 	if (is_version) {
129 		printf ("hal-find-by-capability " PACKAGE_VERSION "\n");
130 		return 0;
131 	}
132 
133 	if (capability == NULL) {
134 		usage (argc, argv);
135 		return 1;
136 	}
137 
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);
142 		return 1;
143 	}
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);
147 		return 1;
148 	}
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);
153 		}
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");
156 		return 1;
157 	}
158 
159 
160 	udis = libhal_find_device_by_capability (hal_ctx, capability, &num_udis, &error);
161 
162 	if (dbus_error_is_set (&error)) {
163 		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
164 		LIBHAL_FREE_DBUS_ERROR (&error);
165 		return 1;
166 	}
167 
168 	if (is_verbose)
169 		printf ("Found %d device objects of capability '%s'\n", num_udis, capability);
170 
171 	if (num_udis == 0) {
172 		return 1;
173 	}
174 
175 	for (i = 0; i < num_udis; i++) {
176 		printf ("%s\n", udis[i]);
177 	}
178 
179 	libhal_free_string_array (udis);
180 
181 	return 0;
182 }
183 
184 /**
185  * @}
186  */
187