xref: /illumos-gate/usr/src/cmd/hal/tools/hal_find_by_property.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /***************************************************************************
2  * CVSID: $Id$
3  *
4  * hal_find_by_property.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-property --key <key> --string <value>\n"
50  "                              [--help] [--verbose] [--version]\n");
51 
52 /** @todo support other property types a'la hal-[get|set]-property */
53 
54 	fprintf (stderr,
55  "\n"
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"
61  "\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"
66  "\n");
67 }
68 
69 /** Entry point
70  *
71  *  @param  argc                Number of arguments given to program
72  *  @param  argv                Arguments given to program
73  *  @return                     Return code
74  */
75 int
76 main (int argc, char *argv[])
77 {
78 	int i;
79 	int num_udis;
80 	char **udis;
81 	char *key = NULL;
82 	char *value = NULL;
83 	dbus_bool_t is_verbose = FALSE;
84 	dbus_bool_t is_version = FALSE;
85 	DBusError error;
86 	LibHalContext *hal_ctx;
87 
88 	if (argc <= 1) {
89 		usage (argc, argv);
90 		return 1;
91 	}
92 
93 	while (1) {
94 		int c;
95 		int option_index = 0;
96 		const char *opt;
97 		static struct option long_options[] = {
98 			{"key", 1, NULL, 0},
99 			{"string", 1, NULL, 0},
100 			{"verbose", 0, NULL, 0},
101 			{"version", 0, NULL, 0},
102 			{"help", 0, NULL, 0},
103 			{NULL, 0, NULL, 0}
104 		};
105 
106 		c = getopt_long (argc, argv, "",
107 				 long_options, &option_index);
108 		if (c == -1)
109 			break;
110 
111 		switch (c) {
112 		case 0:
113 			opt = long_options[option_index].name;
114 
115 			if (strcmp (opt, "help") == 0) {
116 				usage (argc, argv);
117 				return 0;
118 			} else if (strcmp (opt, "verbose") == 0) {
119 				is_verbose = TRUE;
120 			} else if (strcmp (opt, "version") == 0) {
121 				is_version = TRUE;
122 			} else if (strcmp (opt, "key") == 0) {
123 				key = strdup (optarg);
124 			} else if (strcmp (opt, "string") == 0) {
125 				value = strdup (optarg);
126 			}
127 			break;
128 
129 		default:
130 			usage (argc, argv);
131 			return 1;
132 			break;
133 		}
134 	}
135 
136 	if (is_version) {
137 		printf ("hal-find-by-property " PACKAGE_VERSION "\n");
138 		return 0;
139 	}
140 
141 	if (key == NULL || value == NULL) {
142 		usage (argc, argv);
143 		return 1;
144 	}
145 
146 	dbus_error_init (&error);
147 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
148 		fprintf (stderr, "error: libhal_ctx_new\n");
149 		return 1;
150 	}
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);
154 		return 1;
155 	}
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);
160 		}
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");
163 		return 1;
164 	}
165 
166 
167 	udis = libhal_manager_find_device_string_match (hal_ctx, key, value, &num_udis, &error);
168 
169 	if (dbus_error_is_set (&error)) {
170 		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
171 		LIBHAL_FREE_DBUS_ERROR (&error);
172 		return 1;
173 	}
174 
175 	if (is_verbose)
176 		printf ("Found %d device objects with string property %s = '%s'\n", num_udis, key, value);
177 
178 	if (num_udis == 0) {
179 		return 1;
180 	}
181 
182 	for (i = 0; i < num_udis; i++) {
183 		printf ("%s\n", udis[i]);
184 	}
185 
186 	libhal_free_string_array (udis);
187 
188 	return 0;
189 }
190 
191 /**
192  * @}
193  */
194