make swapnext cycling
[awesome.git] / awesome-client.c
blob8c4866ab708b60e779f7c37aaa7f3c94d683e28f
1 /*
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.
23 #include <errno.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
29 #include "awesome-client.h"
30 #include "util.h"
32 /* GNU/Hurd workaround */
33 #ifndef MSG_NOSIGNAL
34 #define MSG_NOSIGNAL 0
35 #endif
37 static int
38 send_msg(char *msg, ssize_t msg_len)
40 struct sockaddr_un *addr;
41 int csfd, ret_value = EXIT_SUCCESS;
42 csfd = get_client_socket();
43 addr = get_client_addr(getenv("DISPLAY"));
45 if(!addr || csfd < 0)
46 return EXIT_FAILURE;
48 if(sendto(csfd, msg, msg_len, MSG_NOSIGNAL,
49 (const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1)
51 switch (errno)
53 case ENOENT:
54 warn("Can't write to %s\n", addr->sun_path);
55 break;
56 default:
57 perror("error sending datagram");
59 ret_value = errno;
62 close(csfd);
64 p_delete(&addr);
65 return ret_value;
68 int
69 main(void)
71 char buf[1024], *msg;
72 int ret_value = EXIT_SUCCESS;
73 ssize_t len, msg_len = 1;
75 msg = p_new(char, 1);
76 while(fgets(buf, sizeof(buf), stdin))
78 len = a_strlen(buf);
79 if (len < 2 && msg_len > 1)
81 ret_value = send_msg(msg, msg_len);
82 p_delete(&msg);
83 if (ret_value != EXIT_SUCCESS)
84 return ret_value;
85 msg = p_new(char, 1);
86 msg_len = 1;
88 else if (len > 1)
90 msg_len += len;
91 p_realloc(&msg, msg_len);
92 a_strncat(msg, msg_len, buf, len);
96 if (msg_len > 1)
97 ret_value = send_msg(msg, msg_len);
99 p_delete(&msg);
101 return ret_value;
103 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80