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
)
96 if(send(csfd
, msg
, msg_len
, MSG_NOSIGNAL
| MSG_EOR
) == -1)
98 if(send(csfd
, msg
, msg_len
, MSG_NOSIGNAL
| MSG_EOF
) == -1)
104 warn("can't write to %s", addr
->sun_path
);
110 return send_msg(msg
, msg_len
);
112 warn("error sending packet: %s", strerror(errno
));
121 /** Recieve a message from awesome.
132 r
= recv(csfd
, buf
, sizeof(buf
) - 1, MSG_TRUNC
| MSG_DONTWAIT
);
136 return warn("error recieving from UNIX domain socket: %s", strerror(errno
));
154 /** Print help and exit(2) with given exit_code.
155 * \param exit_code The exit code.
156 * \return Never return.
158 static void __attribute__ ((noreturn
))
159 exit_help(int exit_code
)
161 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
162 fprintf(outfile
, "Usage: awesome-client [--version|--help]\n"
163 "In normal operation, give no parameters and issue commands "
164 "on standard input.\n");
168 /** Main function of awesome-client.
169 * \param argc Number of args.
170 * \param argv Args array.
171 * \return Value returned by send_msg().
174 main(int argc
, char **argv
)
176 char buf
[1024], *msg
, *prompt
;
177 int ret_value
= EXIT_SUCCESS
;
178 ssize_t len
, msg_len
= 1;
182 if(!a_strcmp("-v", argv
[1]) || !a_strcmp("--version", argv
[1]))
183 eprint_version("awesome-client");
184 else if(!a_strcmp("-h", argv
[1]) || !a_strcmp("--help", argv
[1]))
185 exit_help(EXIT_SUCCESS
);
188 exit_help(EXIT_SUCCESS
);
190 display
= getenv("DISPLAY");
194 warn("can't connect to UNIX domain socket: %s", strerror(errno
));
198 if(isatty(STDIN_FILENO
))
200 a_asprintf(&prompt
, "awesome@%s%% ", display
? display
: "unknown");
201 while((msg
= readline(prompt
)))
202 if((msg_len
= a_strlen(msg
)))
205 p_realloc(&msg
, msg_len
+ 2);
207 msg
[msg_len
+ 1] = '\0';
208 if(send_msg(msg
, msg_len
+ 2) == EXIT_SUCCESS
)
215 msg
= p_new(char, 1);
216 while(fgets(buf
, sizeof(buf
), stdin
))
219 if(len
< 2 && msg_len
> 1)
221 ret_value
= send_msg(msg
, msg_len
);
223 if (ret_value
!= EXIT_SUCCESS
)
225 msg
= p_new(char, 1);
231 p_realloc(&msg
, msg_len
);
232 a_strncat(msg
, msg_len
, buf
, len
);
237 if((ret_value
= send_msg(msg
, msg_len
)) == EXIT_SUCCESS
)
247 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80