2 * awesome-client.c - awesome client, communicate with socket
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 daniel@brinkers.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/socket.h>
33 #include <readline/readline.h>
34 #include <readline/history.h>
36 #include "common/socket.h"
37 #include "common/version.h"
38 #include "common/util.h"
40 /* GNU/Hurd workaround */
42 #define MSG_NOSIGNAL 0
45 struct sockaddr_un
*addr
;
49 /** Initialize the client and server socket connections.
50 * If something goes wrong, preserves errno.
51 * \return 0 if everything worked, 1 otherwise.
56 if((csfd
= socket_getclient()) < 0)
59 if(!(addr
= socket_getaddr(display
)))
62 if(connect(csfd
, addr
, sizeof(struct sockaddr_un
)) == -1)
68 /** Close the client and server socket connections.
77 /** Reconnect sockets.
80 sockets_reconnect(void)
82 warn("connection lost, reconnecting…");
87 /** Send a message to awesome.
88 * \param msg The message.
89 * \param msg_len The message length.
90 * \return The errno of sendto().
93 send_msg(const char *msg
, ssize_t msg_len
)
95 if(send(csfd
, msg
, msg_len
, MSG_NOSIGNAL
| MSG_EOR
) == -1)
100 warn("can't write to %s", addr
->sun_path
);
106 return send_msg(msg
, msg_len
);
108 warn("error sending packet: %s", strerror(errno
));
117 /** Recieve a message from awesome.
128 r
= recv(csfd
, buf
, sizeof(buf
) - 1, MSG_TRUNC
| MSG_DONTWAIT
);
132 return warn("error recieving from UNIX domain socket: %s", strerror(errno
));
150 /** Print help and exit(2) with given exit_code.
151 * \param exit_code The exit code.
152 * \return Never return.
154 static void __attribute__ ((noreturn
))
155 exit_help(int exit_code
)
157 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
158 fprintf(outfile
, "Usage: awesome-client [--version|--help]\n"
159 "In normal operation, give no parameters and issue commands "
160 "on standard input.\n");
164 /** Main function of awesome-client.
165 * \param argc Number of args.
166 * \param argv Args array.
167 * \return Value returned by send_msg().
170 main(int argc
, char **argv
)
172 char buf
[1024], *msg
, *prompt
;
173 int ret_value
= EXIT_SUCCESS
;
174 ssize_t len
, msg_len
= 1;
178 if(!a_strcmp("-v", argv
[1]) || !a_strcmp("--version", argv
[1]))
179 eprint_version("awesome-client");
180 else if(!a_strcmp("-h", argv
[1]) || !a_strcmp("--help", argv
[1]))
181 exit_help(EXIT_SUCCESS
);
184 exit_help(EXIT_SUCCESS
);
186 display
= getenv("DISPLAY");
190 warn("can't connect to UNIX domain socket: %s", strerror(errno
));
194 if(isatty(STDIN_FILENO
))
196 asprintf(&prompt
, "awesome@%s%% ", display
? display
: "unknown");
197 while((msg
= readline(prompt
)))
198 if((msg_len
= a_strlen(msg
)))
201 p_realloc(&msg
, msg_len
+ 2);
203 msg
[msg_len
+ 1] = '\0';
204 if(send_msg(msg
, msg_len
+ 2) == EXIT_SUCCESS
)
211 msg
= p_new(char, 1);
212 while(fgets(buf
, sizeof(buf
), stdin
))
215 if(len
< 2 && msg_len
> 1)
217 ret_value
= send_msg(msg
, msg_len
);
219 if (ret_value
!= EXIT_SUCCESS
)
221 msg
= p_new(char, 1);
227 p_realloc(&msg
, msg_len
);
228 a_strncat(msg
, msg_len
, buf
, len
);
233 if((ret_value
= send_msg(msg
, msg_len
)) == EXIT_SUCCESS
)
243 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80