BUG's page
[awesome.git] / awesome-client.c
blob31601cf2f175b49d29ac82277480bf70bfd20326
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 "common/socket.h"
30 #include "common/version.h"
31 #include "common/util.h"
33 /* GNU/Hurd workaround */
34 #ifndef MSG_NOSIGNAL
35 #define MSG_NOSIGNAL 0
36 #endif
38 /** Send a message to awesome.
39 * \param msg the message
40 * \param msg_len the message length
41 * \return errno of sendto()
43 static int
44 send_msg(char *msg, ssize_t msg_len)
46 struct sockaddr_un *addr;
47 int csfd, ret_value = EXIT_SUCCESS;
48 csfd = socket_getclient();
49 addr = socket_getaddr(getenv("DISPLAY"));
51 if(!addr || csfd < 0)
52 return EXIT_FAILURE;
54 if(sendto(csfd, msg, msg_len, MSG_NOSIGNAL,
55 (const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1)
57 switch (errno)
59 case ENOENT:
60 warn("can't write to %s\n", addr->sun_path);
61 break;
62 default:
63 warn("error sending datagram: %s\n", strerror(errno));
65 ret_value = errno;
68 close(csfd);
70 p_delete(&addr);
71 return ret_value;
75 /** Print help and exit(2) with given exit_code.
76 * \param exit_code exit code
77 * \return never return
79 static void __attribute__ ((noreturn))
80 exit_help(int exit_code)
82 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
83 fprintf(outfile, "Usage: awesome-client [--version|--help]\n"
84 "In normal operation, give no parameters and issue commands "
85 "on standard input.\n");
86 exit(exit_code);
89 /** Main function of awesome-client.
90 * \param argc number of args
91 * \param argv args array
92 * \return value returned by send_msg()
94 int
95 main(int argc, char **argv)
97 char buf[1024], *msg;
98 int ret_value = EXIT_SUCCESS;
99 ssize_t len, msg_len = 1;
101 if (argc < 2)
103 /* no args to parse, nothing to do */
105 else if (argc == 2)
107 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
108 eprint_version("awesome-client");
109 else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
110 exit_help(EXIT_SUCCESS);
112 else
114 exit_help(EXIT_SUCCESS);
117 msg = p_new(char, 1);
118 while(fgets(buf, sizeof(buf), stdin))
120 len = a_strlen(buf);
121 if (len < 2 && msg_len > 1)
123 ret_value = send_msg(msg, msg_len);
124 p_delete(&msg);
125 if (ret_value != EXIT_SUCCESS)
126 return ret_value;
127 msg = p_new(char, 1);
128 msg_len = 1;
130 else if (len > 1)
132 msg_len += len;
133 p_realloc(&msg, msg_len);
134 a_strncat(msg, msg_len, buf, len);
138 if (msg_len > 1)
139 ret_value = send_msg(msg, msg_len);
141 p_delete(&msg);
143 return ret_value;
145 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80