1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: main.c |
2 | * File: main.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: main.C,v 1.6 2003/11/25 17:11:33 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> |
… | |
… | |
65 | blink_ev (this, &rxvt_term::blink_cb), |
65 | blink_ev (this, &rxvt_term::blink_cb), |
66 | #endif |
66 | #endif |
67 | #ifdef POINTER_BLANK |
67 | #ifdef POINTER_BLANK |
68 | pointer_ev (this, &rxvt_term::pointer_cb), |
68 | pointer_ev (this, &rxvt_term::pointer_cb), |
69 | #endif |
69 | #endif |
70 | x_ev (this, &rxvt_term::x_cb) |
70 | x_ev (this, &rxvt_term::x_cb), |
|
|
71 | destroy_ev (this, &rxvt_term::destroy_cb) |
71 | { |
72 | { |
72 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
73 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
73 | } |
74 | } |
74 | |
75 | |
75 | rxvt_term::~rxvt_term () |
76 | rxvt_term::~rxvt_term () |
76 | { |
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 | |
77 | 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; |
78 | } |
124 | } |
79 | |
125 | |
80 | /*----------------------------------------------------------------------*/ |
126 | /*----------------------------------------------------------------------*/ |
81 | /* rxvt_init() */ |
127 | /* rxvt_init() */ |
82 | /* LIBPROTO */ |
128 | /* LIBPROTO */ |
… | |
… | |
91 | delete R; |
137 | delete R; |
92 | return NULL; |
138 | return NULL; |
93 | } |
139 | } |
94 | |
140 | |
95 | 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); |
96 | } |
174 | } |
97 | |
175 | |
98 | bool |
176 | bool |
99 | rxvt_term::init (int argc, const char *const *argv) |
177 | rxvt_term::init (int argc, const char *const *argv) |
100 | { |
178 | { |
… | |
… | |
132 | #endif |
210 | #endif |
133 | |
211 | |
134 | #if 0 |
212 | #if 0 |
135 | #ifdef DEBUG_X |
213 | #ifdef DEBUG_X |
136 | XSynchronize(Xdisplay, True); |
214 | XSynchronize(Xdisplay, True); |
137 | XSetErrorHandler((XErrorHandler) abort); |
|
|
138 | #else |
|
|
139 | XSetErrorHandler((XErrorHandler) rxvt_xerror_handler); |
|
|
140 | #endif |
215 | #endif |
141 | #endif |
216 | #endif |
142 | |
217 | |
143 | #ifdef HAVE_SCROLLBARS |
218 | #ifdef HAVE_SCROLLBARS |
144 | if (Options & Opt_scrollBar) |
219 | if (Options & Opt_scrollBar) |
… | |
… | |
161 | rxvt_init_env (this); |
236 | rxvt_init_env (this); |
162 | rxvt_init_command (this, cmd_argv); |
237 | rxvt_init_command (this, cmd_argv); |
163 | |
238 | |
164 | x_ev.start (Xfd, EVENT_READ); |
239 | x_ev.start (Xfd, EVENT_READ); |
165 | pty_ev.start (cmd_fd, EVENT_READ); |
240 | pty_ev.start (cmd_fd, EVENT_READ); |
|
|
241 | |
|
|
242 | flush (); |
166 | |
243 | |
167 | return true; |
244 | return true; |
168 | } |
245 | } |
169 | |
246 | |
170 | /* ------------------------------------------------------------------------- * |
247 | /* ------------------------------------------------------------------------- * |
… | |
… | |
176 | /* ARGSUSED */ |
253 | /* ARGSUSED */ |
177 | /* EXTPROTO */ |
254 | /* EXTPROTO */ |
178 | RETSIGTYPE |
255 | RETSIGTYPE |
179 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
256 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
180 | { |
257 | { |
181 | dR; |
|
|
182 | int pid, save_errno = errno; |
258 | int pid, save_errno = errno; |
183 | |
|
|
184 | do { |
|
|
185 | errno = 0; |
|
|
186 | } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); |
259 | while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR) |
|
|
260 | ; |
|
|
261 | errno = save_errno; |
187 | |
262 | |
|
|
263 | #if 0 |
188 | if (pid == R->cmd_pid) |
264 | if (pid == R->cmd_pid) |
189 | exit(EXIT_SUCCESS); |
265 | exit (EXIT_SUCCESS); |
190 | |
266 | #endif |
191 | errno = save_errno; |
|
|
192 | signal(SIGCHLD, rxvt_Child_signal); |
|
|
193 | } |
267 | } |
194 | |
268 | |
195 | /* |
269 | /* |
196 | * Catch a fatal signal and tidy up before quitting |
270 | * Catch a fatal signal and tidy up before quitting |
197 | */ |
271 | */ |
… | |
… | |
211 | /* INTPROTO */ |
285 | /* INTPROTO */ |
212 | int |
286 | int |
213 | rxvt_xerror_handler(const Display * display |
287 | rxvt_xerror_handler(const Display * display |
214 | __attribute__ ((unused)), const XErrorEvent * event) |
288 | __attribute__ ((unused)), const XErrorEvent * event) |
215 | { |
289 | { |
216 | dR; |
290 | dR; |
217 | |
291 | |
218 | if (R->allowedxerror == -1) { |
292 | if (R->allowedxerror == -1) |
219 | R->allowedxerror = event->error_code; |
293 | R->allowedxerror = event->error_code; |
220 | return 0; /* ignored anyway */ |
294 | else |
221 | } |
295 | { |
222 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
296 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
223 | event->request_code, event->minor_code, |
297 | event->request_code, event->minor_code, |
224 | event->error_code); |
298 | event->error_code); |
225 | /* XXX: probably should call rxvt_clean_exit() bypassing X routines */ |
299 | |
226 | exit(EXIT_FAILURE); |
300 | R->destroy (); |
227 | /* NOTREACHED */ |
301 | } |
|
|
302 | |
|
|
303 | return 0; |
228 | } |
304 | } |
229 | |
305 | |
230 | /*----------------------------------------------------------------------*/ |
306 | /*----------------------------------------------------------------------*/ |
231 | /* |
307 | /* |
232 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
308 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
233 | * 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 |
234 | */ |
310 | */ |
235 | /* EXTPROTO */ |
311 | /* EXTPROTO */ |
236 | void |
312 | void |
237 | rxvt_clean_exit(void) |
313 | rxvt_clean_exit () |
238 | { |
314 | { |
239 | dR; |
315 | dR; |
240 | |
316 | |
241 | #ifdef DEBUG_SCREEN |
317 | R->destroy (); |
242 | rxvt_scr_release(aR); |
|
|
243 | #endif |
|
|
244 | #ifndef NO_SETOWNER_TTYDEV |
|
|
245 | rxvt_privileged_ttydev(aR_ RESTORE); |
|
|
246 | #endif |
|
|
247 | #ifdef UTMP_SUPPORT |
|
|
248 | rxvt_privileged_utmp(aR_ RESTORE); |
|
|
249 | #endif |
|
|
250 | #ifdef USE_XIM |
|
|
251 | if (R->Input_Context != NULL) { |
|
|
252 | XDestroyIC(R->Input_Context); |
|
|
253 | R->Input_Context = NULL; |
|
|
254 | } |
|
|
255 | #endif |
|
|
256 | } |
318 | } |
257 | |
319 | |
258 | /* ------------------------------------------------------------------------- * |
320 | /* ------------------------------------------------------------------------- * |
259 | * MEMORY ALLOCATION WRAPPERS * |
321 | * MEMORY ALLOCATION WRAPPERS * |
260 | * ------------------------------------------------------------------------- */ |
322 | * ------------------------------------------------------------------------- */ |