|
|
1 | #include "../config.h" |
1 | #include "rxvtlib.h" |
2 | #include "rxvt.h" |
2 | #include "rxvtdaemon.h" |
3 | #include "rxvtdaemon.h" |
3 | #include "iom.h" |
4 | #include "iom.h" |
4 | |
5 | |
5 | #include <cstdio> |
6 | #include <cstdio> |
6 | #include <cstdlib> |
7 | #include <cstdlib> |
… | |
… | |
16 | #include <cerrno> |
17 | #include <cerrno> |
17 | |
18 | |
18 | extern char **environ; |
19 | extern char **environ; |
19 | |
20 | |
20 | struct server : rxvt_connection { |
21 | struct server : rxvt_connection { |
|
|
22 | log_callback log_cb; |
|
|
23 | |
21 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
24 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
|
|
25 | void log_msg (const char *msg); |
22 | |
26 | |
23 | server (int fd) |
27 | server (int fd) |
24 | : read_ev (this, &server::read_cb) |
28 | : read_ev (this, &server::read_cb), |
|
|
29 | log_cb (this, &server::log_msg) |
25 | { |
30 | { |
26 | this->fd = fd; |
31 | this->fd = fd; |
27 | read_ev.start (fd, EVENT_READ); |
32 | read_ev.start (fd, EVENT_READ); |
28 | } |
33 | } |
29 | |
34 | |
… | |
… | |
79 | |
84 | |
80 | if (fd2 >= 0) |
85 | if (fd2 >= 0) |
81 | new server (fd2); |
86 | new server (fd2); |
82 | } |
87 | } |
83 | |
88 | |
|
|
89 | void server::log_msg (const char *msg) |
|
|
90 | { |
|
|
91 | send ("MSG"), send (msg); |
|
|
92 | } |
|
|
93 | |
84 | void server::err (const char *format, ...) |
94 | void server::err (const char *format, ...) |
85 | { |
95 | { |
86 | if (format) |
96 | if (format) |
87 | { |
97 | { |
88 | char err[1024]; |
98 | char err[1024]; |
… | |
… | |
90 | va_list ap; |
100 | va_list ap; |
91 | va_start (ap, format); |
101 | va_start (ap, format); |
92 | vsnprintf (err, 1024, format, ap); |
102 | vsnprintf (err, 1024, format, ap); |
93 | va_end (ap); |
103 | va_end (ap); |
94 | |
104 | |
95 | send ("ERR"), send (err); |
105 | send ("MSG"), send (err); |
96 | } |
106 | } |
97 | |
107 | |
|
|
108 | send ("END", 0); |
98 | close (fd); |
109 | close (fd); |
99 | delete this; |
110 | delete this; |
100 | } |
111 | } |
101 | |
112 | |
102 | void server::read_cb (io_watcher &w, short revents) |
113 | void server::read_cb (io_watcher &w, short revents) |
… | |
… | |
105 | |
116 | |
106 | if (recv (tok)) |
117 | if (recv (tok)) |
107 | { |
118 | { |
108 | if (!strcmp (tok, "NEW")) |
119 | if (!strcmp (tok, "NEW")) |
109 | { |
120 | { |
110 | stringvec argv; |
121 | stringvec *argv = new stringvec; |
111 | stringvec envv; |
122 | stringvec *envv = new stringvec; |
112 | |
123 | |
113 | for (;;) |
124 | for (;;) |
114 | { |
125 | { |
115 | if (!recv (tok)) |
126 | if (!recv (tok)) |
116 | return err (); |
127 | return err (); |
117 | |
128 | |
118 | if (!strcmp (tok, "END")) |
129 | if (!strcmp (tok, "END")) |
119 | break; |
130 | break; |
120 | else if (!strcmp (tok, "ENV") && recv (tok)) |
131 | else if (!strcmp (tok, "ENV") && recv (tok)) |
121 | envv.push_back (tok.get ()); |
132 | envv->push_back (tok.get ()); |
122 | else if (!strcmp (tok, "CWD") && recv (tok)) |
133 | else if (!strcmp (tok, "CWD") && recv (tok)) |
123 | { |
134 | { |
124 | if (chdir (tok)) |
135 | if (chdir (tok)) |
125 | err ("unable to change to working directory to '%s': %s", |
136 | err ("unable to change to working directory to '%s': %s", |
126 | (char *)tok, strerror (errno)); |
137 | (char *)tok, strerror (errno)); |
127 | } |
138 | } |
128 | else if (!strcmp (tok, "ARG") && recv (tok)) |
139 | else if (!strcmp (tok, "ARG") && recv (tok)) |
129 | argv.push_back (tok.get ()); |
140 | argv->push_back (tok.get ()); |
130 | else |
141 | else |
131 | return err ("protocol error: unexpected NEW token"); |
142 | return err ("protocol error: unexpected NEW token"); |
132 | } |
143 | } |
133 | |
144 | |
134 | envv.push_back (0); |
145 | envv->push_back (0); |
135 | |
146 | |
136 | { |
147 | { |
137 | char **old_environ = environ; |
148 | char **old_environ = environ; |
138 | environ = envv.begin (); |
149 | environ = envv->begin (); |
139 | |
150 | |
|
|
151 | rxvt_term *term = new rxvt_term; |
|
|
152 | |
|
|
153 | term->log_hook = &log_cb; |
|
|
154 | term->argv = argv; |
|
|
155 | term->envv = envv; |
|
|
156 | |
|
|
157 | bool success; |
|
|
158 | |
|
|
159 | try |
|
|
160 | { |
140 | rxvt_init (argv.size (), argv.begin ()); |
161 | success = term->init (argv->size (), argv->begin ()); |
|
|
162 | } |
|
|
163 | catch (const class rxvt_failure_exception &e) |
|
|
164 | { |
|
|
165 | success = false; |
|
|
166 | } |
141 | |
167 | |
142 | environ = old_environ; |
168 | environ = old_environ; |
143 | envv.clear (); // can't yet save the env 'cause rxvt modifies it : ( |
169 | |
|
|
170 | if (!success) |
|
|
171 | term->destroy (); |
|
|
172 | |
|
|
173 | send ("END"); send (success ? 1 : 0); |
144 | } |
174 | } |
145 | } |
175 | } |
146 | else |
176 | else |
147 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
177 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
148 | } |
178 | } |