]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-set-host-name.c
Fix error message when passing an invalid command line option (Closes #88)
[catta] / avahi-utils / avahi-set-host-name.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <locale.h>
34
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-client/client.h>
40
41 #include "sigint.h"
42
43 typedef enum {
44     COMMAND_UNSPEC, 
45     COMMAND_HELP,
46     COMMAND_VERSION,
47 } Command;
48
49 typedef struct Config {
50     int verbose;
51     Command command;
52 } Config;
53
54 static AvahiSimplePoll *simple_poll = NULL;
55 static AvahiClient *client = NULL;
56
57 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
58     switch (state) {
59         case AVAHI_CLIENT_FAILURE:
60             fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
61             avahi_simple_poll_quit(simple_poll);
62             break;
63             
64         case AVAHI_CLIENT_S_REGISTERING:
65         case AVAHI_CLIENT_S_RUNNING:
66         case AVAHI_CLIENT_S_COLLISION:
67         case AVAHI_CLIENT_CONNECTING:
68             ;
69     }
70 }
71
72 static void help(FILE *f, const char *argv0) {
73     fprintf(f,
74             "%s [options] <new host name>\n\n"
75             "    -h --help            Show this help\n"
76             "    -V --version         Show version\n"
77             "    -v --verbose         Enable verbose mode\n",
78             argv0);
79 }
80
81 static int parse_command_line(Config *c, int argc, char *argv[]) {
82     int o;
83
84     static const struct option long_options[] = {
85         { "help",           no_argument,       NULL, 'h' },
86         { "version",        no_argument,       NULL, 'V' },
87         { "verbose",        no_argument,       NULL, 'v' },
88         { NULL, 0, NULL, 0 }
89     };
90
91     assert(c);
92
93     c->command = COMMAND_UNSPEC;
94     c->verbose = 0;
95
96     opterr = 0;
97     while ((o = getopt_long(argc, argv, "hVv", long_options, NULL)) >= 0) {
98
99         switch(o) {
100             case 'h':
101                 c->command = COMMAND_HELP;
102                 break;
103             case 'V':
104                 c->command = COMMAND_VERSION;
105                 break;
106             case 'v':
107                 c->verbose = 1;
108                 break;
109             default:
110                 fprintf(stderr, "Invalid command line argument: %s\n", argv[optind-1]);
111                 return -1;
112         }
113     }
114
115     if (c->command == COMMAND_UNSPEC) {
116         if (optind != argc-1) {
117             fprintf(stderr, "Invalid number of arguments, expecting exactly one.\n");
118             return -1;
119         }
120     }
121         
122     return 0;
123 }
124
125 int main(int argc, char *argv[]) {
126     int ret = 1, error;
127     Config config;
128     const char *argv0;
129
130     if ((argv0 = strrchr(argv[0], '/')))
131         argv0++;
132     else
133         argv0 = argv[0];
134
135     if (parse_command_line(&config, argc, argv) < 0)
136         goto fail;
137
138     switch (config.command) {
139         case COMMAND_HELP:
140             help(stdout, argv0);
141             ret = 0;
142             break;
143             
144         case COMMAND_VERSION:
145             printf("%s "PACKAGE_VERSION"\n", argv0);
146             ret = 0;
147             break;
148
149         case COMMAND_UNSPEC: 
150             
151             if (!(simple_poll = avahi_simple_poll_new())) {
152                 fprintf(stderr, "Failed to create simple poll object.\n");
153                 goto fail;
154             }
155             
156             if (sigint_install(simple_poll) < 0)
157                 goto fail;
158             
159             if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) {
160                 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
161                 goto fail;
162             }
163
164             if (config.verbose) {
165                 const char *version, *hn;
166
167                 if (!(version = avahi_client_get_version_string(client))) {
168                     fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
169                     goto fail;
170                 }
171
172                 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
173                     fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
174                     goto fail;
175                 }
176                 
177                 fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
178             }
179
180             if (avahi_client_set_host_name(client, argv[optind]) < 0) {
181                 fprintf(stderr, "Failed to create host name resolver: %s\n", avahi_strerror(avahi_client_errno(client)));
182                 goto fail;
183             }
184
185             if (config.verbose) {
186                 const char *hn;
187                 
188                 if (!(hn = avahi_client_get_host_name_fqdn(client))) {
189                     fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
190                     goto fail;
191                 }
192                 
193                 fprintf(stderr, "Host name successfully changed to %s\n", hn);
194             }
195             
196             ret = 0;
197             break;
198     }
199     
200 fail:
201
202     if (client)
203         avahi_client_free(client);
204
205     sigint_uninstall();
206     
207     if (simple_poll)
208         avahi_simple_poll_free(simple_poll);
209
210     return ret;
211 }