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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2007-10-27 18:47:13 +02:00
|
|
|
#include "awesome-client.h"
|
2007-10-27 13:22:47 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2007-11-19 20:43:53 +01:00
|
|
|
/* GNU/Hurd workaround */
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2008-01-07 19:59:26 +01:00
|
|
|
static int
|
|
|
|
send_msg(char *msg, ssize_t msg_len)
|
2007-10-27 13:22:47 +02:00
|
|
|
{
|
2007-10-29 16:14:50 +01:00
|
|
|
struct sockaddr_un *addr;
|
2007-11-17 11:35:20 +01:00
|
|
|
int csfd, ret_value = EXIT_SUCCESS;
|
2007-10-29 16:14:50 +01:00
|
|
|
csfd = get_client_socket();
|
2007-11-27 13:37:32 +01:00
|
|
|
addr = get_client_addr(getenv("DISPLAY"));
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2007-10-29 16:20:11 +01:00
|
|
|
if(!addr || csfd < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2008-01-07 19:59:26 +01:00
|
|
|
if(sendto(csfd, msg, msg_len, MSG_NOSIGNAL,
|
2008-01-07 18:30:17 +01:00
|
|
|
(const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1)
|
|
|
|
{
|
|
|
|
switch (errno)
|
2007-11-17 11:35:20 +01:00
|
|
|
{
|
2008-01-07 18:30:17 +01:00
|
|
|
case ENOENT:
|
|
|
|
warn("Can't write to %s\n", addr->sun_path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
perror("error sending datagram");
|
|
|
|
}
|
|
|
|
ret_value = errno;
|
|
|
|
}
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2007-11-26 10:49:59 +01:00
|
|
|
close(csfd);
|
|
|
|
|
2007-10-29 16:23:05 +01:00
|
|
|
p_delete(&addr);
|
2008-01-07 19:59:26 +01:00
|
|
|
return ret_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
char buf[1024], *msg;
|
|
|
|
int ret_value = EXIT_SUCCESS;
|
|
|
|
ssize_t len, msg_len = 1;
|
|
|
|
|
|
|
|
msg = p_new(char, 1);
|
|
|
|
while(fgets(buf, sizeof(buf), stdin))
|
|
|
|
{
|
|
|
|
len = a_strlen(buf);
|
|
|
|
if (len < 2 && msg_len > 1)
|
|
|
|
{
|
|
|
|
ret_value = send_msg(msg, msg_len);
|
|
|
|
p_delete(&msg);
|
|
|
|
if (ret_value != EXIT_SUCCESS)
|
|
|
|
return ret_value;
|
|
|
|
msg = p_new(char, 1);
|
|
|
|
msg_len = 1;
|
|
|
|
}
|
|
|
|
else if (len > 1)
|
|
|
|
{
|
|
|
|
msg_len += len;
|
|
|
|
p_realloc(&msg, msg_len);
|
|
|
|
a_strncat(msg, msg_len, buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg_len > 1)
|
|
|
|
ret_value = send_msg(msg, msg_len);
|
|
|
|
|
|
|
|
p_delete(&msg);
|
2007-10-29 16:23:05 +01:00
|
|
|
|
2007-11-17 11:35:20 +01:00
|
|
|
return ret_value;
|
2007-10-27 13:22:47 +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
|