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