1 |
#include <stdint.h> |
2 |
#include <cstdlib> |
3 |
#include <cstring> |
4 |
#include <cstdio> |
5 |
|
6 |
#include <unistd.h> |
7 |
#include <sys/utsname.h> |
8 |
#include <limits.h> |
9 |
|
10 |
#include "rxvtdaemon.h" |
11 |
|
12 |
char *rxvt_connection::unix_sockname () |
13 |
{ |
14 |
char name[PATH_MAX]; |
15 |
char *path = getenv ("RXVT_SOCKET"); |
16 |
|
17 |
if (!path) |
18 |
{ |
19 |
struct utsname u; |
20 |
uname (&u); |
21 |
|
22 |
path = getenv ("HOME"); |
23 |
snprintf (name, PATH_MAX, "%s/.rxvt-%s", |
24 |
path ? path : "/tmp", |
25 |
u.nodename); |
26 |
|
27 |
path = name; |
28 |
} |
29 |
|
30 |
return strdup (path); |
31 |
} |
32 |
|
33 |
void rxvt_connection::send (const char *data, int len) |
34 |
{ |
35 |
uint8_t s[2]; |
36 |
|
37 |
s[0] = len >> 8; s[1] = len; |
38 |
|
39 |
write (fd, s, 2); |
40 |
write (fd, data, len); |
41 |
} |
42 |
|
43 |
void rxvt_connection::send (const char *data) |
44 |
{ |
45 |
send (data, strlen (data)); |
46 |
} |
47 |
|
48 |
bool rxvt_connection::recv (auto_str &data, int *len) |
49 |
{ |
50 |
uint8_t s[2]; |
51 |
int l; |
52 |
|
53 |
if (read (fd, s, 2) != 2) |
54 |
return false; |
55 |
|
56 |
l = (s[0] << 8) + s[1]; |
57 |
if (l > 4096) |
58 |
return false; |
59 |
|
60 |
if (len) |
61 |
*len = l; |
62 |
|
63 |
data = new char[l + 1]; |
64 |
|
65 |
if (!data) |
66 |
return false; |
67 |
|
68 |
if (read (fd, data, l) != l) |
69 |
return false; |
70 |
|
71 |
data[l] = 0; |
72 |
|
73 |
return true; |
74 |
} |
75 |
|
76 |
void rxvt_connection::send (int data) |
77 |
{ |
78 |
uint8_t s[4]; |
79 |
|
80 |
s[0] = data >> 24; s[1] = data >> 16; s[2] = data >> 8; s[3] = data; |
81 |
|
82 |
write (fd, s, 4); |
83 |
} |
84 |
|
85 |
bool rxvt_connection::recv (int &data) |
86 |
{ |
87 |
uint8_t s[4]; |
88 |
|
89 |
if (read (fd, s, 4) != 4) |
90 |
return false; |
91 |
|
92 |
data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
93 |
|
94 |
return true; |
95 |
} |
96 |
|
97 |
|
98 |
|