1 |
pcg |
1.8 |
#include "../config.h" |
2 |
pcg |
1.1 |
#include "rxvtdaemon.h" |
3 |
|
|
|
4 |
pcg |
1.9 |
#include "rxvt.h" |
5 |
|
|
|
6 |
pcg |
1.1 |
#include <cstdio> |
7 |
|
|
#include <cstdlib> |
8 |
|
|
|
9 |
|
|
#include <unistd.h> |
10 |
pcg |
1.3 |
#include <signal.h> |
11 |
pcg |
1.1 |
#include <sys/socket.h> |
12 |
|
|
#include <sys/un.h> |
13 |
|
|
|
14 |
|
|
struct client : rxvt_connection { |
15 |
|
|
client (); |
16 |
|
|
}; |
17 |
|
|
|
18 |
|
|
client::client () |
19 |
|
|
{ |
20 |
|
|
if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
21 |
|
|
{ |
22 |
pcg |
1.5 |
perror ("unable to create communications socket"); |
23 |
pcg |
1.1 |
exit (EXIT_FAILURE); |
24 |
|
|
} |
25 |
|
|
|
26 |
pcg |
1.4 |
char *sockname = rxvt_connection::unix_sockname (); |
27 |
pcg |
1.1 |
sockaddr_un sa; |
28 |
|
|
sa.sun_family = AF_UNIX; |
29 |
pcg |
1.4 |
strcpy (sa.sun_path, sockname); |
30 |
|
|
free (sockname); |
31 |
pcg |
1.1 |
|
32 |
|
|
if (connect (fd, (sockaddr *)&sa, sizeof (sa))) |
33 |
|
|
{ |
34 |
pcg |
1.5 |
perror ("unable to connect to rxvtd"); |
35 |
pcg |
1.1 |
exit (EXIT_FAILURE); |
36 |
|
|
} |
37 |
|
|
} |
38 |
|
|
|
39 |
pcg |
1.3 |
extern char **environ; |
40 |
|
|
|
41 |
pcg |
1.1 |
int |
42 |
pcg |
1.7 |
main (int argc, const char *const *argv) |
43 |
pcg |
1.1 |
{ |
44 |
|
|
client c; |
45 |
|
|
char buf[PATH_MAX]; |
46 |
|
|
|
47 |
pcg |
1.3 |
{ |
48 |
|
|
sigset_t ss; |
49 |
|
|
|
50 |
pcg |
1.6 |
sigemptyset (&ss); |
51 |
pcg |
1.3 |
sigaddset (&ss, SIGHUP); |
52 |
|
|
sigprocmask (SIG_BLOCK, &ss, 0); |
53 |
|
|
} |
54 |
|
|
|
55 |
pcg |
1.1 |
c.send ("NEW"); |
56 |
pcg |
1.7 |
// instead of getcwd we could opendir (".") and pass the fd for fchdir *g* |
57 |
pcg |
1.2 |
c.send ("CWD"), c.send (getcwd (buf, sizeof (buf))); |
58 |
pcg |
1.3 |
|
59 |
|
|
for (char **var = environ; *environ; environ++) |
60 |
|
|
c.send ("ENV"), c.send (*environ); |
61 |
pcg |
1.1 |
|
62 |
|
|
for (int i = 0; i < argc; i++) |
63 |
pcg |
1.2 |
c.send ("ARG"), c.send (argv[i]); |
64 |
|
|
|
65 |
pcg |
1.1 |
c.send ("END"); |
66 |
pcg |
1.8 |
|
67 |
|
|
auto_str tok; |
68 |
|
|
|
69 |
|
|
for (;;) |
70 |
|
|
if (!c.recv (tok)) |
71 |
|
|
{ |
72 |
|
|
fprintf (stderr, "protocol error: unexpected eof from server.\n"); |
73 |
|
|
break; |
74 |
|
|
} |
75 |
|
|
else if (!strcmp (tok, "MSG") && c.recv (tok)) |
76 |
|
|
fprintf (stderr, "%s", (const char *)tok); |
77 |
|
|
else if (!strcmp (tok, "END")) |
78 |
|
|
{ |
79 |
|
|
int success; |
80 |
|
|
if (c.recv (success)) |
81 |
|
|
exit (success ? EXIT_SUCCESS : EXIT_FAILURE); |
82 |
|
|
} |
83 |
|
|
else |
84 |
|
|
{ |
85 |
|
|
fprintf (stderr, "protocol error: received illegal token '%s'.\n", (const char *)tok); |
86 |
|
|
break; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
return EXIT_FAILURE; |
90 |
pcg |
1.1 |
} |
91 |
|
|
|