Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / usr.bin / sdpquery / sdpquery.c
1 /* $NetBSD: sdpquery.c,v 1.3 2007/03/30 21:25:00 plunky Exp $ */
2 /* $DragonFly: src/usr.bin/sdpquery/sdpquery.c,v 1.1 2008/02/08 14:06:25 hasso Exp $ */
3
4 /*-
5  * Copyright (c) 2006 Itronix Inc.
6  * All rights reserved.
7  *
8  * Written by Iain Hibbert for Itronix Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of Itronix Inc. may not be used to endorse
19  *    or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <assert.h>
36 #include <bluetooth.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <sdp.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "sdpquery.h"
46
47 static void usage(void);
48
49 const char *control_socket;
50
51 static struct command {
52         const char      *command;
53         int             (*handler)(bdaddr_t *, bdaddr_t *, int, char const **);
54         const char      *usage;
55 } commands[] = {
56         { "Browse",     do_sdp_browse,  "[UUID]"        },
57         { "Search",     do_sdp_search,  "<service>"     },
58         { NULL,         NULL,           NULL            }
59 };
60
61 int
62 main(int argc, char *argv[])
63 {
64         bdaddr_t        laddr, raddr;
65         struct command *cmd;
66         int             ch, local;
67
68         bdaddr_copy(&laddr, BDADDR_ANY);
69         bdaddr_copy(&raddr, BDADDR_ANY);
70         control_socket = NULL;
71         local = 0;
72
73         while ((ch = getopt(argc, argv, "a:c:d:hl")) != -1) {
74                 switch (ch) {
75                 case 'a': /* remote address */
76                         if (!bt_aton(optarg, &raddr)) {
77                                 struct hostent  *he = NULL;
78
79                                 if ((he = bt_gethostbyname(optarg)) == NULL)
80                                         errx(EXIT_FAILURE, "%s: %s",
81                                                 optarg, hstrerror(h_errno));
82
83                                 bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
84                         }
85                         break;
86
87                 case 'c':
88                         control_socket = optarg;
89                         break;
90
91                 case 'd': /* local device address */
92                         if (!bt_devaddr(optarg, &laddr))
93                                 err(EXIT_FAILURE, "%s", optarg);
94
95                         break;
96
97                 case 'l': /* local sdpd */
98                         local = 1;
99                         break;
100
101                 case 'h':
102                 default:
103                         usage();
104                         /* NOT REACHED */
105                 }
106         }
107
108         argc -= optind;
109         argv += optind;
110
111         optind = 0;
112         optreset = 1;
113
114         if (argc < 1
115             || (bdaddr_any(&raddr) && !local)
116             || (!bdaddr_any(&raddr) && local))
117                 usage();
118
119         for (cmd = commands ; cmd->command != NULL; cmd++) {
120                 if (strcasecmp(*argv, cmd->command) == 0)
121                         return (*cmd->handler)(&laddr, &raddr, --argc, (char const **)++argv);
122         }
123
124         errx(EXIT_FAILURE, "%s: Unknown Command", *argv);
125 }
126
127 /* Usage */
128 static void
129 usage(void)
130 {
131         struct command *cmd;
132
133         fprintf(stderr,
134                 "Usage: %s [-d device] -a bdaddr <command> [parameters..]\n"
135                 "       %s [-c path] -l <command> [parameters..]\n"
136                 "\n", getprogname(), getprogname());
137
138         fprintf(stderr,
139                 "Where:\n"
140                 "\t-a bdaddr    remote address\n"
141                 "\t-c path      path to control socket\n"
142                 "\t-d device    local device address\n"
143                 "\t-l           connect to the local SDP server via control socket\n"
144                 "\t-h           display usage and quit\n"
145                 "\n"
146                 "Commands:\n");
147
148         for (cmd = commands ; cmd->command != NULL ; cmd++)
149                 fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
150
151         exit(EXIT_FAILURE);
152 }