8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / hal / tools / hal_find_by_property.c
blob09b1b55e94642c14ad7c6ef8ff3223176ff90fc2
1 /***************************************************************************
2 * CVSID: $Id$
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 **************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <getopt.h>
36 #include <libhal.h>
39 /** Print out program usage.
41 * @param argc Number of arguments given to program
42 * @param argv Arguments given to program
44 static void
45 usage (int argc, char *argv[])
47 fprintf (stderr,
48 "\n"
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 */
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");
69 /** Entry point
71 * @param argc Number of arguments given to program
72 * @param argv Arguments given to program
73 * @return Return code
75 int
76 main (int argc, char *argv[])
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;
88 if (argc <= 1) {
89 usage (argc, argv);
90 return 1;
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}
106 c = getopt_long (argc, argv, "",
107 long_options, &option_index);
108 if (c == -1)
109 break;
111 switch (c) {
112 case 0:
113 opt = long_options[option_index].name;
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);
127 break;
129 default:
130 usage (argc, argv);
131 return 1;
132 break;
136 if (is_version) {
137 printf ("hal-find-by-property " PACKAGE_VERSION "\n");
138 return 0;
141 if (key == NULL || value == NULL) {
142 usage (argc, argv);
143 return 1;
146 dbus_error_init (&error);
147 if ((hal_ctx = libhal_ctx_new ()) == NULL) {
148 fprintf (stderr, "error: libhal_ctx_new\n");
149 return 1;
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;
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");
163 return 1;
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);
172 return 1;
175 if (is_verbose)
176 printf ("Found %d device objects with string property %s = '%s'\n", num_udis, key, value);
178 if (num_udis == 0) {
179 return 1;
182 for (i = 0; i < num_udis; i++) {
183 printf ("%s\n", udis[i]);
186 libhal_free_string_array (udis);
188 return 0;
192 * @}