1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: main.c |
2 | * File: main.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: main.C,v 1.5 2003/11/25 15:44:38 pcg Exp $ |
4 | * $Id: main.C,v 1.8 2003/11/27 10:12:10 pcg Exp $ |
5 | * |
5 | * |
6 | * All portions of code are copyright by their respective author/s. |
6 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> |
7 | * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> |
8 | * - original version |
8 | * - original version |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
… | |
… | |
58 | { |
58 | { |
59 | free (p); |
59 | free (p); |
60 | } |
60 | } |
61 | |
61 | |
62 | rxvt_term::rxvt_term () |
62 | rxvt_term::rxvt_term () |
63 | : pty_ev (this, &rxvt_term::pty_cb), |
63 | : pty_ev (this, &rxvt_term::pty_cb), |
64 | #ifdef CURSOR_BLINK |
64 | #ifdef CURSOR_BLINK |
65 | blink_ev (this, &rxvt_term::blink_cb), |
65 | blink_ev (this, &rxvt_term::blink_cb), |
66 | #endif |
66 | #endif |
|
|
67 | #ifdef POINTER_BLANK |
|
|
68 | pointer_ev (this, &rxvt_term::pointer_cb), |
|
|
69 | #endif |
67 | x_ev (this, &rxvt_term::x_cb) |
70 | x_ev (this, &rxvt_term::x_cb), |
|
|
71 | destroy_ev (this, &rxvt_term::destroy_cb) |
68 | { |
72 | { |
69 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
73 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
70 | } |
74 | } |
71 | |
75 | |
72 | rxvt_term::~rxvt_term () |
76 | rxvt_term::~rxvt_term () |
73 | { |
77 | { |
|
|
78 | rxvt_scr_release (this); |
|
|
79 | |
|
|
80 | #ifndef NO_SETOWNER_TTYDEV |
|
|
81 | rxvt_privileged_ttydev (this, RESTORE); |
|
|
82 | #endif |
|
|
83 | #ifdef UTMP_SUPPORT |
|
|
84 | rxvt_privileged_utmp (this, RESTORE); |
|
|
85 | #endif |
|
|
86 | #ifdef USE_XIM |
|
|
87 | if (Input_Context != NULL) |
|
|
88 | { |
|
|
89 | XDestroyIC (Input_Context); |
|
|
90 | Input_Context = NULL; |
|
|
91 | } |
|
|
92 | #endif |
|
|
93 | |
|
|
94 | if (cmd_fd >= 0) |
|
|
95 | close (cmd_fd); |
|
|
96 | |
|
|
97 | if (Xfd >= 0) |
|
|
98 | XCloseDisplay (Xdisplay); |
|
|
99 | |
74 | delete PixColors; |
100 | delete PixColors; |
|
|
101 | } |
|
|
102 | |
|
|
103 | void |
|
|
104 | rxvt_term::destroy () |
|
|
105 | { |
|
|
106 | pty_ev.stop (); |
|
|
107 | x_ev.stop (); |
|
|
108 | #ifdef CURSOR_BLINK |
|
|
109 | blink_ev.stop (); |
|
|
110 | #endif |
|
|
111 | #ifdef POINTER_BLANK |
|
|
112 | pointer_ev.stop (); |
|
|
113 | #endif |
|
|
114 | |
|
|
115 | destroy_ev.start (0); |
|
|
116 | } |
|
|
117 | |
|
|
118 | void |
|
|
119 | rxvt_term::destroy_cb (time_watcher &w) |
|
|
120 | { |
|
|
121 | SET_R (this); |
|
|
122 | |
|
|
123 | delete this; |
75 | } |
124 | } |
76 | |
125 | |
77 | /*----------------------------------------------------------------------*/ |
126 | /*----------------------------------------------------------------------*/ |
78 | /* rxvt_init() */ |
127 | /* rxvt_init() */ |
79 | /* LIBPROTO */ |
128 | /* LIBPROTO */ |
… | |
… | |
88 | delete R; |
137 | delete R; |
89 | return NULL; |
138 | return NULL; |
90 | } |
139 | } |
91 | |
140 | |
92 | return R; |
141 | return R; |
|
|
142 | } |
|
|
143 | |
|
|
144 | /* EXTPROTO */ |
|
|
145 | void |
|
|
146 | rxvt_init_signals () |
|
|
147 | { |
|
|
148 | /* install exit handler for cleanup */ |
|
|
149 | #if 0 |
|
|
150 | #ifdef HAVE_ATEXIT |
|
|
151 | atexit(rxvt_clean_exit); |
|
|
152 | #else |
|
|
153 | # ifdef HAVE_ON_EXIT |
|
|
154 | on_exit(rxvt_clean_exit, NULL); /* non-ANSI exit handler */ |
|
|
155 | # endif |
|
|
156 | #endif |
|
|
157 | #endif |
|
|
158 | |
|
|
159 | struct sigaction sa; |
|
|
160 | |
|
|
161 | sigfillset (&sa.sa_mask); |
|
|
162 | sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; |
|
|
163 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGHUP , &sa, 0); //TODO, also: SIGPIPE |
|
|
164 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); |
|
|
165 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); |
|
|
166 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); |
|
|
167 | sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); |
|
|
168 | |
|
|
169 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
|
|
170 | /* signal (SIGURG, SIG_DFL); */ |
|
|
171 | |
|
|
172 | XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); |
|
|
173 | //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler); |
93 | } |
174 | } |
94 | |
175 | |
95 | bool |
176 | bool |
96 | rxvt_term::init (int argc, const char *const *argv) |
177 | rxvt_term::init (int argc, const char *const *argv) |
97 | { |
178 | { |
… | |
… | |
129 | #endif |
210 | #endif |
130 | |
211 | |
131 | #if 0 |
212 | #if 0 |
132 | #ifdef DEBUG_X |
213 | #ifdef DEBUG_X |
133 | XSynchronize(Xdisplay, True); |
214 | XSynchronize(Xdisplay, True); |
134 | XSetErrorHandler((XErrorHandler) abort); |
|
|
135 | #else |
|
|
136 | XSetErrorHandler((XErrorHandler) rxvt_xerror_handler); |
|
|
137 | #endif |
215 | #endif |
138 | #endif |
216 | #endif |
139 | |
217 | |
140 | #ifdef HAVE_SCROLLBARS |
218 | #ifdef HAVE_SCROLLBARS |
141 | if (Options & Opt_scrollBar) |
219 | if (Options & Opt_scrollBar) |
… | |
… | |
158 | rxvt_init_env (this); |
236 | rxvt_init_env (this); |
159 | rxvt_init_command (this, cmd_argv); |
237 | rxvt_init_command (this, cmd_argv); |
160 | |
238 | |
161 | x_ev.start (Xfd, EVENT_READ); |
239 | x_ev.start (Xfd, EVENT_READ); |
162 | pty_ev.start (cmd_fd, EVENT_READ); |
240 | pty_ev.start (cmd_fd, EVENT_READ); |
|
|
241 | |
|
|
242 | flush (); |
163 | |
243 | |
164 | return true; |
244 | return true; |
165 | } |
245 | } |
166 | |
246 | |
167 | /* ------------------------------------------------------------------------- * |
247 | /* ------------------------------------------------------------------------- * |
… | |
… | |
173 | /* ARGSUSED */ |
253 | /* ARGSUSED */ |
174 | /* EXTPROTO */ |
254 | /* EXTPROTO */ |
175 | RETSIGTYPE |
255 | RETSIGTYPE |
176 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
256 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
177 | { |
257 | { |
178 | dR; |
|
|
179 | int pid, save_errno = errno; |
258 | int pid, save_errno = errno; |
180 | |
|
|
181 | do { |
|
|
182 | errno = 0; |
|
|
183 | } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); |
259 | while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR) |
|
|
260 | ; |
|
|
261 | errno = save_errno; |
184 | |
262 | |
|
|
263 | #if 0 |
185 | if (pid == R->cmd_pid) |
264 | if (pid == R->cmd_pid) |
186 | exit(EXIT_SUCCESS); |
265 | exit (EXIT_SUCCESS); |
187 | |
266 | #endif |
188 | errno = save_errno; |
|
|
189 | signal(SIGCHLD, rxvt_Child_signal); |
|
|
190 | } |
267 | } |
191 | |
268 | |
192 | /* |
269 | /* |
193 | * Catch a fatal signal and tidy up before quitting |
270 | * Catch a fatal signal and tidy up before quitting |
194 | */ |
271 | */ |
… | |
… | |
208 | /* INTPROTO */ |
285 | /* INTPROTO */ |
209 | int |
286 | int |
210 | rxvt_xerror_handler(const Display * display |
287 | rxvt_xerror_handler(const Display * display |
211 | __attribute__ ((unused)), const XErrorEvent * event) |
288 | __attribute__ ((unused)), const XErrorEvent * event) |
212 | { |
289 | { |
213 | dR; |
290 | dR; |
214 | |
291 | |
215 | if (R->allowedxerror == -1) { |
292 | if (R->allowedxerror == -1) |
216 | R->allowedxerror = event->error_code; |
293 | R->allowedxerror = event->error_code; |
217 | return 0; /* ignored anyway */ |
294 | else |
218 | } |
295 | { |
219 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
296 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
220 | event->request_code, event->minor_code, |
297 | event->request_code, event->minor_code, |
221 | event->error_code); |
298 | event->error_code); |
222 | /* XXX: probably should call rxvt_clean_exit() bypassing X routines */ |
299 | |
223 | exit(EXIT_FAILURE); |
300 | R->destroy (); |
224 | /* NOTREACHED */ |
301 | } |
|
|
302 | |
|
|
303 | return 0; |
225 | } |
304 | } |
226 | |
305 | |
227 | /*----------------------------------------------------------------------*/ |
306 | /*----------------------------------------------------------------------*/ |
228 | /* |
307 | /* |
229 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
308 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
230 | * TODO: if debugging, this should free up any known resources if we can |
309 | * TODO: if debugging, this should free up any known resources if we can |
231 | */ |
310 | */ |
232 | /* EXTPROTO */ |
311 | /* EXTPROTO */ |
233 | void |
312 | void |
234 | rxvt_clean_exit(void) |
313 | rxvt_clean_exit () |
235 | { |
314 | { |
236 | dR; |
315 | dR; |
237 | |
316 | |
238 | #ifdef DEBUG_SCREEN |
317 | R->destroy (); |
239 | rxvt_scr_release(aR); |
|
|
240 | #endif |
|
|
241 | #ifndef NO_SETOWNER_TTYDEV |
|
|
242 | rxvt_privileged_ttydev(aR_ RESTORE); |
|
|
243 | #endif |
|
|
244 | #ifdef UTMP_SUPPORT |
|
|
245 | rxvt_privileged_utmp(aR_ RESTORE); |
|
|
246 | #endif |
|
|
247 | #ifdef USE_XIM |
|
|
248 | if (R->Input_Context != NULL) { |
|
|
249 | XDestroyIC(R->Input_Context); |
|
|
250 | R->Input_Context = NULL; |
|
|
251 | } |
|
|
252 | #endif |
|
|
253 | } |
318 | } |
254 | |
319 | |
255 | /* ------------------------------------------------------------------------- * |
320 | /* ------------------------------------------------------------------------- * |
256 | * MEMORY ALLOCATION WRAPPERS * |
321 | * MEMORY ALLOCATION WRAPPERS * |
257 | * ------------------------------------------------------------------------- */ |
322 | * ------------------------------------------------------------------------- */ |