1 |
#include "rxvtlib.h" |
2 |
#include "rxvtdaemon.h" |
3 |
#include "iom.h" |
4 |
|
5 |
#include <cstdio> |
6 |
#include <cstdlib> |
7 |
|
8 |
#include <unistd.h> |
9 |
#include <sys/socket.h> |
10 |
#include <sys/un.h> |
11 |
|
12 |
struct server : rxvt_connection { |
13 |
void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
14 |
|
15 |
server (int fd) |
16 |
: read_ev (this, &server::read_cb) |
17 |
{ |
18 |
this->fd = fd; |
19 |
read_ev.start (fd, EVENT_READ); |
20 |
} |
21 |
|
22 |
void err (); |
23 |
}; |
24 |
|
25 |
struct listener { |
26 |
int fd; |
27 |
|
28 |
void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
29 |
|
30 |
listener (); |
31 |
}; |
32 |
|
33 |
listener::listener () |
34 |
: accept_ev (this, &listener::accept_cb) |
35 |
{ |
36 |
if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
37 |
{ |
38 |
perror ("unable to create listening socket"); |
39 |
exit (EXIT_FAILURE); |
40 |
} |
41 |
|
42 |
sockaddr_un sa; |
43 |
|
44 |
sa.sun_family = AF_UNIX; |
45 |
strcpy (sa.sun_path, rxvt_connection::unix_sockname ()); |
46 |
|
47 |
unlink (rxvt_connection::unix_sockname ()); |
48 |
|
49 |
if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
50 |
{ |
51 |
perror ("unable to bind listening socket"); |
52 |
exit (EXIT_FAILURE); |
53 |
} |
54 |
|
55 |
if (listen (fd, 5)) |
56 |
{ |
57 |
perror ("unable to listen on socket"); |
58 |
exit (EXIT_FAILURE); |
59 |
} |
60 |
|
61 |
accept_ev.start (fd, EVENT_READ); |
62 |
} |
63 |
|
64 |
void listener::accept_cb (io_watcher &w, short revents) |
65 |
{ |
66 |
int fd2 = accept (fd, 0, 0); |
67 |
|
68 |
if (fd2 >= 0) |
69 |
new server (fd2); |
70 |
} |
71 |
|
72 |
void server::err () |
73 |
{ |
74 |
close (fd); |
75 |
delete this; |
76 |
} |
77 |
|
78 |
void server::read_cb (io_watcher &w, short revents) |
79 |
{ |
80 |
auto_str tok; |
81 |
|
82 |
if (recv (tok)) |
83 |
{ |
84 |
if (!strcmp (tok, "NEW")) |
85 |
{ |
86 |
auto_str display, cwd; |
87 |
simplevec<auto_str> argv; |
88 |
|
89 |
for (;;) |
90 |
{ |
91 |
if (!recv (tok)) |
92 |
return err (); |
93 |
|
94 |
if (!strcmp (tok, "END")) |
95 |
break; |
96 |
else if (!strcmp (tok, "DISPLAY") && recv (display)) |
97 |
; |
98 |
else if (!strcmp (tok, "CWD") && recv (cwd)) |
99 |
; |
100 |
else if (!strcmp (tok, "ARG") && recv (tok)) |
101 |
argv.push_back (tok); |
102 |
else |
103 |
return err (); |
104 |
} |
105 |
|
106 |
// TODO: no setenv, please |
107 |
setenv ("DISPLAY", display.get (), 1); |
108 |
|
109 |
rxvt_init (argv.size (), reinterpret_cast<char **>(argv.begin ())); |
110 |
dR; |
111 |
rxvt_main_loop (aR); |
112 |
} |
113 |
else |
114 |
return err (); |
115 |
} |
116 |
else |
117 |
return err (); |
118 |
} |
119 |
|
120 |
int |
121 |
main(int argc, const char *const *argv) |
122 |
{ |
123 |
listener l; |
124 |
iom.loop (); |
125 |
|
126 |
#if 0 |
127 |
if (rxvt_init(argc, argv) == NULL) |
128 |
return EXIT_FAILURE; |
129 |
|
130 |
dR; |
131 |
rxvt_main_loop(aR); /* main processing loop */ |
132 |
#endif |
133 |
return EXIT_SUCCESS; |
134 |
} |