dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hal / tools / hal_find_by_capability.c
blob8433003accbe2df294b109548b72e1b8bc4a595e
1 /***************************************************************************
2 * CVSID: $Id$
4 * hal_find_by_capability.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-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");
65 /** Entry point
67 * @param argc Number of arguments given to program
68 * @param argv Arguments given to program
69 * @return Return code
71 int
72 main (int argc, char *argv[])
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;
83 if (argc <= 1) {
84 usage (argc, argv);
85 return 1;
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}
100 c = getopt_long (argc, argv, "",
101 long_options, &option_index);
102 if (c == -1)
103 break;
105 switch (c) {
106 case 0:
107 opt = long_options[option_index].name;
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);
119 break;
121 default:
122 usage (argc, argv);
123 return 1;
124 break;
128 if (is_version) {
129 printf ("hal-find-by-capability " PACKAGE_VERSION "\n");
130 return 0;
133 if (capability == NULL) {
134 usage (argc, argv);
135 return 1;
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;
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;
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);
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;
160 udis = libhal_find_device_by_capability (hal_ctx, capability, &num_udis, &error);
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;
168 if (is_verbose)
169 printf ("Found %d device objects of capability '%s'\n", num_udis, capability);
171 if (num_udis == 0) {
172 return 1;
175 for (i = 0; i < num_udis; i++) {
176 printf ("%s\n", udis[i]);
179 libhal_free_string_array (udis);
181 return 0;
185 * @}