2007-10-27 18:47:13 +02:00
|
|
|
/*
|
|
|
|
* awesome-client.c - awesome client, communicate with socket
|
|
|
|
*
|
2008-01-02 16:59:43 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-10-27 18:47:13 +02:00
|
|
|
* Copyright © 2007 daniel@brinkers.de
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2007-10-27 13:22:47 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2008-01-01 17:33:12 +01:00
|
|
|
#include <unistd.h>
|
2007-10-27 13:22:47 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2008-09-22 15:11:16 +02:00
|
|
|
#include <fcntl.h>
|
2007-10-27 13:22:47 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
2008-02-27 09:07:52 +01:00
|
|
|
#include "common/socket.h"
|
2008-02-27 09:04:17 +01:00
|
|
|
#include "common/version.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2007-10-27 13:22:47 +02:00
|
|
|
|
2007-11-19 20:43:53 +01:00
|
|
|
/* GNU/Hurd workaround */
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2008-09-19 06:48:55 +02:00
|
|
|
struct sockaddr_un *addr;
|
|
|
|
int csfd;
|
|
|
|
|
|
|
|
/** Initialize the client and server socket connections.
|
|
|
|
* If something goes wrong, preserves errno.
|
|
|
|
* \return 0 if everything worked, 1 otherwise.
|
|
|
|
*/
|
2008-09-22 15:11:16 +02:00
|
|
|
static bool
|
2008-09-19 06:48:55 +02:00
|
|
|
sockets_init(void)
|
|
|
|
{
|
2008-09-22 15:11:16 +02:00
|
|
|
if((csfd = socket_getclient()) < 0)
|
|
|
|
return false;
|
|
|
|
|
2009-03-26 19:12:24 +01:00
|
|
|
if(!(addr = socket_open(csfd, SOCKET_MODE_CONNECT)))
|
2008-09-22 15:11:16 +02:00
|
|
|
return false;
|
2008-09-19 06:48:55 +02:00
|
|
|
|
2008-09-22 15:11:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-19 06:48:55 +02:00
|
|
|
|
|
|
|
/** Close the client and server socket connections.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sockets_close(void)
|
|
|
|
{
|
|
|
|
close(csfd);
|
|
|
|
p_delete(&addr);
|
|
|
|
}
|
|
|
|
|
2008-09-22 15:11:16 +02:00
|
|
|
/** Reconnect sockets.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sockets_reconnect(void)
|
|
|
|
{
|
|
|
|
warn("connection lost, reconnecting…");
|
|
|
|
sockets_close();
|
|
|
|
sockets_init();
|
|
|
|
}
|
2008-09-19 06:48:55 +02:00
|
|
|
|
2009-03-17 20:01:38 +01:00
|
|
|
/** Send a message to awesome without handling retries.
|
2008-06-20 08:32:46 +02:00
|
|
|
* \param msg The message.
|
|
|
|
* \param msg_len The message length.
|
2009-03-17 20:01:38 +01:00
|
|
|
* \return The errno of send().
|
2008-03-31 14:18:27 +02:00
|
|
|
*/
|
2008-01-07 19:59:26 +01:00
|
|
|
static int
|
2009-03-17 20:01:38 +01:00
|
|
|
send_msg_raw(const char *msg, ssize_t msg_len)
|
2007-10-27 13:22:47 +02:00
|
|
|
{
|
2008-11-03 14:53:23 +01:00
|
|
|
#ifndef __FreeBSD__
|
2009-03-17 20:01:38 +01:00
|
|
|
return send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOR);
|
2008-11-03 14:53:23 +01:00
|
|
|
#else
|
2009-03-17 20:01:38 +01:00
|
|
|
return send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOF);
|
2008-11-03 14:53:23 +01:00
|
|
|
#endif
|
2009-03-17 20:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Send a message to awesome.
|
|
|
|
* \param msg The message.
|
|
|
|
* \param msg_len The message length.
|
|
|
|
* \return The errno of send().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
send_msg(const char *msg, ssize_t msg_len)
|
|
|
|
{
|
|
|
|
int try = 10;
|
|
|
|
|
2009-03-29 20:26:39 +02:00
|
|
|
while(try && send_msg_raw(msg, msg_len) == -1)
|
2008-01-07 18:30:17 +01:00
|
|
|
{
|
2009-03-29 20:26:39 +02:00
|
|
|
switch(errno)
|
2007-11-17 11:35:20 +01:00
|
|
|
{
|
2008-09-22 15:11:16 +02:00
|
|
|
case EPIPE:
|
2008-09-26 13:35:35 +02:00
|
|
|
case ENOTCONN:
|
|
|
|
case ECONNRESET:
|
2008-09-22 15:11:16 +02:00
|
|
|
sockets_reconnect();
|
2009-03-17 20:01:38 +01:00
|
|
|
try--;
|
|
|
|
break;
|
|
|
|
case ENOENT:
|
|
|
|
warn("can't write to %s", addr->sun_path);
|
|
|
|
return errno;
|
2008-01-07 18:30:17 +01:00
|
|
|
default:
|
2008-09-19 06:48:55 +02:00
|
|
|
warn("error sending packet: %s", strerror(errno));
|
2009-03-17 20:01:38 +01:00
|
|
|
return errno;
|
2008-09-19 06:48:55 +02:00
|
|
|
}
|
2009-03-17 20:01:38 +01:00
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
if(!try)
|
|
|
|
{
|
|
|
|
warn("giving up.");
|
|
|
|
return EXIT_FAILURE;
|
2008-01-07 18:30:17 +01:00
|
|
|
}
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2008-09-19 06:48:55 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2007-11-26 10:49:59 +01:00
|
|
|
|
2008-09-19 06:48:55 +02:00
|
|
|
|
|
|
|
/** Recieve a message from awesome.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
recv_msg(void)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
char buf[1024];
|
2008-09-22 15:11:16 +02:00
|
|
|
int try = 10;
|
2008-09-19 06:48:55 +02:00
|
|
|
|
2008-09-22 15:11:16 +02:00
|
|
|
while(try)
|
2008-09-19 06:48:55 +02:00
|
|
|
{
|
2008-09-22 15:11:16 +02:00
|
|
|
r = recv(csfd, buf, sizeof(buf) - 1, MSG_TRUNC | MSG_DONTWAIT);
|
2009-03-29 20:26:39 +02:00
|
|
|
if(r < 0)
|
2008-09-22 15:11:16 +02:00
|
|
|
{
|
|
|
|
if(errno != EAGAIN)
|
|
|
|
return warn("error recieving from UNIX domain socket: %s", strerror(errno));
|
|
|
|
try--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
usleep(100000);
|
2008-09-19 06:48:55 +02:00
|
|
|
}
|
2008-09-22 15:11:16 +02:00
|
|
|
|
|
|
|
if(!try)
|
|
|
|
sockets_reconnect();
|
2008-09-19 06:48:55 +02:00
|
|
|
else if(r > 0)
|
|
|
|
{
|
|
|
|
buf[r] = '\0';
|
|
|
|
puts(buf);
|
|
|
|
}
|
2008-01-07 19:59:26 +01:00
|
|
|
}
|
|
|
|
|
2008-01-25 11:49:18 +01:00
|
|
|
|
|
|
|
/** Print help and exit(2) with given exit_code.
|
2008-06-20 08:32:46 +02:00
|
|
|
* \param exit_code The exit code.
|
|
|
|
* \return Never return.
|
2008-01-25 11:49:18 +01:00
|
|
|
*/
|
|
|
|
static void __attribute__ ((noreturn))
|
|
|
|
exit_help(int exit_code)
|
|
|
|
{
|
|
|
|
FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
|
|
|
|
fprintf(outfile, "Usage: awesome-client [--version|--help]\n"
|
|
|
|
"In normal operation, give no parameters and issue commands "
|
|
|
|
"on standard input.\n");
|
|
|
|
exit(exit_code);
|
|
|
|
}
|
|
|
|
|
2008-03-31 14:18:27 +02:00
|
|
|
/** Main function of awesome-client.
|
2008-06-20 08:32:46 +02:00
|
|
|
* \param argc Number of args.
|
|
|
|
* \param argv Args array.
|
|
|
|
* \return Value returned by send_msg().
|
2008-03-31 14:18:27 +02:00
|
|
|
*/
|
2008-01-07 19:59:26 +01:00
|
|
|
int
|
2008-03-31 14:18:27 +02:00
|
|
|
main(int argc, char **argv)
|
2008-01-07 19:59:26 +01:00
|
|
|
{
|
2009-03-26 19:12:24 +01:00
|
|
|
char buf[1024], *msg, *prompt, *display;
|
2008-01-07 19:59:26 +01:00
|
|
|
int ret_value = EXIT_SUCCESS;
|
|
|
|
ssize_t len, msg_len = 1;
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
if(argc == 2)
|
2008-01-25 11:49:18 +01:00
|
|
|
{
|
|
|
|
if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
|
|
|
|
eprint_version("awesome-client");
|
|
|
|
else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
|
|
|
|
exit_help(EXIT_SUCCESS);
|
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
else if(argc > 2)
|
2008-01-25 11:49:18 +01:00
|
|
|
exit_help(EXIT_SUCCESS);
|
|
|
|
|
2008-09-22 15:11:16 +02:00
|
|
|
display = getenv("DISPLAY");
|
|
|
|
|
2009-03-29 20:26:39 +02:00
|
|
|
if(!sockets_init())
|
2008-09-19 06:48:55 +02:00
|
|
|
{
|
|
|
|
warn("can't connect to UNIX domain socket: %s", strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
if(isatty(STDIN_FILENO))
|
2008-01-07 19:59:26 +01:00
|
|
|
{
|
2008-12-29 18:57:43 +01:00
|
|
|
a_asprintf(&prompt, "awesome@%s%% ", display ? display : "unknown");
|
2008-05-20 15:39:47 +02:00
|
|
|
while((msg = readline(prompt)))
|
|
|
|
if((msg_len = a_strlen(msg)))
|
|
|
|
{
|
2009-03-17 20:01:38 +01:00
|
|
|
int result;
|
2008-06-19 07:28:12 +02:00
|
|
|
add_history (msg);
|
2008-05-20 15:39:47 +02:00
|
|
|
p_realloc(&msg, msg_len + 2);
|
|
|
|
msg[msg_len] = '\n';
|
|
|
|
msg[msg_len + 1] = '\0';
|
2009-03-17 20:01:38 +01:00
|
|
|
result = send_msg(msg, msg_len + 2);
|
|
|
|
if(result == EXIT_SUCCESS)
|
2008-09-22 15:11:16 +02:00
|
|
|
recv_msg();
|
2009-03-17 20:01:38 +01:00
|
|
|
else if(result == EXIT_FAILURE)
|
|
|
|
break;
|
2008-09-22 11:05:41 +02:00
|
|
|
p_delete(&msg);
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = p_new(char, 1);
|
|
|
|
while(fgets(buf, sizeof(buf), stdin))
|
2008-01-07 19:59:26 +01:00
|
|
|
{
|
2008-05-20 15:39:47 +02:00
|
|
|
len = a_strlen(buf);
|
|
|
|
if(len < 2 && msg_len > 1)
|
|
|
|
{
|
|
|
|
ret_value = send_msg(msg, msg_len);
|
|
|
|
p_delete(&msg);
|
2009-03-29 20:26:39 +02:00
|
|
|
if(ret_value != EXIT_SUCCESS)
|
2008-05-20 15:39:47 +02:00
|
|
|
return ret_value;
|
|
|
|
msg = p_new(char, 1);
|
|
|
|
msg_len = 1;
|
|
|
|
}
|
2009-03-29 20:26:39 +02:00
|
|
|
else if(len > 1)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
|
|
|
msg_len += len;
|
|
|
|
p_realloc(&msg, msg_len);
|
|
|
|
a_strncat(msg, msg_len, buf, len);
|
|
|
|
}
|
2008-01-07 19:59:26 +01:00
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
if(msg_len > 1)
|
2008-09-22 11:10:07 +02:00
|
|
|
{
|
2008-09-22 15:11:16 +02:00
|
|
|
if((ret_value = send_msg(msg, msg_len)) == EXIT_SUCCESS)
|
|
|
|
recv_msg();
|
2008-09-22 11:10:07 +02:00
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
p_delete(&msg);
|
2008-01-07 19:59:26 +01:00
|
|
|
}
|
|
|
|
|
2008-09-19 06:48:55 +02:00
|
|
|
sockets_close();
|
2007-11-17 11:35:20 +01:00
|
|
|
return ret_value;
|
2007-10-27 13:22:47 +02:00
|
|
|
}
|
2008-06-20 08:32:46 +02:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|