Repository navigation
Expand file tree
/
Copy pathawesome-client.c
More file actions
247 lines (220 loc) · 5.92 KB
/
Copy pathawesome-client.c
File metadata and controls
247 lines (220 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* awesome-client.c - awesome client, communicate with socket
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
* 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.
*
*/
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "common/socket.h"
#include "common/version.h"
#include "common/util.h"
/* GNU/Hurd workaround */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
struct sockaddr_un *addr;
int csfd;
char *display;
/** Initialize the client and server socket connections.
* If something goes wrong, preserves errno.
* \return 0 if everything worked, 1 otherwise.
*/
static bool
sockets_init(void)
{
if((csfd = socket_getclient()) < 0)
return false;
if(!(addr = socket_getaddr(display)))
return false;
if(connect(csfd, addr, sizeof(struct sockaddr_un)) == -1)
return false;
return true;
}
/** Close the client and server socket connections.
*/
static void
sockets_close(void)
{
close(csfd);
p_delete(&addr);
}
/** Reconnect sockets.
*/
static void
sockets_reconnect(void)
{
warn("connection lost, reconnecting…");
sockets_close();
sockets_init();
}
/** Send a message to awesome.
* \param msg The message.
* \param msg_len The message length.
* \return The errno of sendto().
*/
static int
send_msg(const char *msg, ssize_t msg_len)
{
#ifndef __FreeBSD__
if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOR) == -1)
#else
if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOF) == -1)
#endif
{
switch (errno)
{
case ENOENT:
warn("can't write to %s", addr->sun_path);
break;
case EPIPE:
case ENOTCONN:
case ECONNRESET:
sockets_reconnect();
return send_msg(msg, msg_len);
default:
warn("error sending packet: %s", strerror(errno));
}
return errno;
}
return EXIT_SUCCESS;
}
/** Recieve a message from awesome.
*/
static void
recv_msg(void)
{
ssize_t r;
char buf[1024];
int try = 10;
while(try)
{
r = recv(csfd, buf, sizeof(buf) - 1, MSG_TRUNC | MSG_DONTWAIT);
if (r < 0)
{
if(errno != EAGAIN)
return warn("error recieving from UNIX domain socket: %s", strerror(errno));
try--;
}
else
break;
usleep(100000);
}
if(!try)
sockets_reconnect();
else if(r > 0)
{
buf[r] = '\0';
puts(buf);
}
}
/** Print help and exit(2) with given exit_code.
* \param exit_code The exit code.
* \return Never return.
*/
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);
}
/** Main function of awesome-client.
* \param argc Number of args.
* \param argv Args array.
* \return Value returned by send_msg().
*/
int
main(int argc, char **argv)
{
char buf[1024], *msg, *prompt;
int ret_value = EXIT_SUCCESS;
ssize_t len, msg_len = 1;
if(argc == 2)
{
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);
}
else if(argc > 2)
exit_help(EXIT_SUCCESS);
display = getenv("DISPLAY");
if (!sockets_init())
{
warn("can't connect to UNIX domain socket: %s", strerror(errno));
return EXIT_FAILURE;
}
if(isatty(STDIN_FILENO))
{
a_asprintf(&prompt, "awesome@%s%% ", display ? display : "unknown");
while((msg = readline(prompt)))
if((msg_len = a_strlen(msg)))
{
add_history (msg);
p_realloc(&msg, msg_len + 2);
msg[msg_len] = '\n';
msg[msg_len + 1] = '\0';
if(send_msg(msg, msg_len + 2) == EXIT_SUCCESS)
recv_msg();
p_delete(&msg);
}
}
else
{
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)
{
if((ret_value = send_msg(msg, msg_len)) == EXIT_SUCCESS)
recv_msg();
}
p_delete(&msg);
}
sockets_close();
return ret_value;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80