ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/init.C
Revision: 1.226
Committed: Tue Dec 11 17:42:04 2007 UTC (16 years, 5 months ago) by ayin
Content type: text/plain
Branch: MAIN
Changes since 1.225: +2 -0 lines
Log Message:
Headers cleanup.

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * File: init.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
7 * - original version
8 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
9 * - extensive modifications
10 * Copyright (c) 1996 Chuck Blake <cblake@BBN.COM>
11 * Copyright (c) 1997 mj olesen <olesen@me.queensu.ca>
12 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
13 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
14 * - extensive modifications
15 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
16 * - QNX support
17 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *---------------------------------------------------------------------*/
33 /*
34 * Initialisation routines.
35 */
36
37 #include "../config.h" /* NECESSARY */
38 #include "rxvt.h" /* NECESSARY */
39 #include "rxvtutil.h"
40 #include "init.h"
41
42 #include <limits>
43
44 #include <csignal>
45
46 #include <fcntl.h>
47
48 #ifdef HAVE_XSETLOCALE
49 # define X_LOCALE
50 # include <X11/Xlocale.h>
51 #else
52 # ifdef HAVE_SETLOCALE
53 # include <clocale>
54 # endif
55 #endif
56
57 #ifdef HAVE_NL_LANGINFO
58 # include <langinfo.h>
59 #endif
60
61 #ifdef DISPLAY_IS_IP
62 /* On Solaris link with -lsocket and -lnsl */
63 #include <sys/types.h>
64 #include <sys/socket.h>
65
66 /* these next two are probably only on Sun (not Solaris) */
67 #ifdef HAVE_SYS_SOCKIO_H
68 #include <sys/sockio.h>
69 #endif
70 #ifdef HAVE_SYS_BYTEORDER_H
71 #include <sys/byteorder.h>
72 #endif
73
74 #include <netinet/in.h>
75 #include <arpa/inet.h>
76 #include <net/if.h>
77 #include <net/if_arp.h>
78
79 static char *
80 rxvt_network_display (const char *display)
81 {
82 char buffer[1024], *rval = NULL;
83 struct ifconf ifc;
84 struct ifreq *ifr;
85 int i, skfd;
86
87 if (display[0] != ':' && strncmp (display, "unix:", 5))
88 return (char *) display; /* nothing to do */
89
90 ifc.ifc_len = sizeof (buffer); /* Get names of all ifaces */
91 ifc.ifc_buf = buffer;
92
93 if ((skfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
94 {
95 perror ("socket");
96 return NULL;
97 }
98
99 if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0)
100 {
101 perror ("SIOCGIFCONF");
102 close (skfd);
103 return NULL;
104 }
105
106 for (i = 0, ifr = ifc.ifc_req;
107 i < (ifc.ifc_len / sizeof (struct ifreq));
108 i++, ifr++)
109 {
110 struct ifreq ifr2;
111
112 strcpy (ifr2.ifr_name, ifr->ifr_name);
113
114 if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0)
115 {
116 unsigned long addr;
117 struct sockaddr_in *p_addr;
118
119 p_addr = (struct sockaddr_in *) &ifr2.ifr_addr;
120 addr = htonl ((unsigned long)p_addr->sin_addr.s_addr);
121
122 /*
123 * not "0.0.0.0" or "127.0.0.1" - so format the address
124 */
125 if (addr && addr != 0x7F000001)
126 {
127 char *colon = strchr (display, ':');
128
129 if (colon == NULL)
130 colon = ":0.0";
131
132 rval = rxvt_malloc (strlen (colon) + 16);
133 sprintf (rval, "%d.%d.%d.%d%s",
134 (int) ((addr >> 030) & 0xFF),
135 (int) ((addr >> 020) & 0xFF),
136 (int) ((addr >> 010) & 0xFF),
137 (int) (addr & 0xFF), colon);
138 break;
139 }
140 }
141 }
142
143 close (skfd);
144
145 return rval;
146 }
147 #endif
148
149 const char *const def_colorName[] =
150 {
151 COLOR_FOREGROUND,
152 COLOR_BACKGROUND,
153 /* low-intensity colors */
154 "rgb:00/00/00", // 0: black (Black)
155 "rgb:cd/00/00", // 1: red (Red3)
156 "rgb:00/cd/00", // 2: green (Green3)
157 "rgb:cd/cd/00", // 3: yellow (Yellow3)
158 "rgb:00/00/cd", // 4: blue (Blue3)
159 "rgb:cd/00/cd", // 5: magenta (Magenta3)
160 "rgb:00/cd/cd", // 6: cyan (Cyan3)
161 # ifdef XTERM_COLORS
162 "rgb:e5/e5/e5", // 7: white (Grey90)
163 # else
164 "rgb:fa/eb/d7", // 7: white (AntiqueWhite)
165 # endif
166 /* high-intensity colors */
167 # ifdef XTERM_COLORS
168 "rgb:4d/4d/4d", // 8: bright black (Grey30)
169 # else
170 "rgb:40/40/40", // 8: bright black (Grey25)
171 # endif
172 "rgb:ff/00/00", // 1/9: bright red (Reed)
173 "rgb:00/ff/00", // 2/10: bright green (Green)
174 "rgb:ff/ff/00", // 3/11: bright yellow (Yellow)
175 "rgb:00/00/ff", // 4/12: bright blue (Blue)
176 "rgb:ff/00/ff", // 5/13: bright magenta (Magenta)
177 "rgb:00/ff/ff", // 6/14: bright cyan (Cyan)
178 "rgb:ff/ff/ff", // 7/15: bright white (White)
179
180 // 88 xterm colours
181 "rgb:00/00/00",
182 "rgb:00/00/8b",
183 "rgb:00/00/cd",
184 "rgb:00/00/ff",
185 "rgb:00/8b/00",
186 "rgb:00/8b/8b",
187 "rgb:00/8b/cd",
188 "rgb:00/8b/ff",
189 "rgb:00/cd/00",
190 "rgb:00/cd/8b",
191 "rgb:00/cd/cd",
192 "rgb:00/cd/ff",
193 "rgb:00/ff/00",
194 "rgb:00/ff/8b",
195 "rgb:00/ff/cd",
196 "rgb:00/ff/ff",
197 "rgb:8b/00/00",
198 "rgb:8b/00/8b",
199 "rgb:8b/00/cd",
200 "rgb:8b/00/ff",
201 "rgb:8b/8b/00",
202 "rgb:8b/8b/8b",
203 "rgb:8b/8b/cd",
204 "rgb:8b/8b/ff",
205 "rgb:8b/cd/00",
206 "rgb:8b/cd/8b",
207 "rgb:8b/cd/cd",
208 "rgb:8b/cd/ff",
209 "rgb:8b/ff/00",
210 "rgb:8b/ff/8b",
211 "rgb:8b/ff/cd",
212 "rgb:8b/ff/ff",
213 "rgb:cd/00/00",
214 "rgb:cd/00/8b",
215 "rgb:cd/00/cd",
216 "rgb:cd/00/ff",
217 "rgb:cd/8b/00",
218 "rgb:cd/8b/8b",
219 "rgb:cd/8b/cd",
220 "rgb:cd/8b/ff",
221 "rgb:cd/cd/00",
222 "rgb:cd/cd/8b",
223 "rgb:cd/cd/cd",
224 "rgb:cd/cd/ff",
225 "rgb:cd/ff/00",
226 "rgb:cd/ff/8b",
227 "rgb:cd/ff/cd",
228 "rgb:cd/ff/ff",
229 "rgb:ff/00/00",
230 "rgb:ff/00/8b",
231 "rgb:ff/00/cd",
232 "rgb:ff/00/ff",
233 "rgb:ff/8b/00",
234 "rgb:ff/8b/8b",
235 "rgb:ff/8b/cd",
236 "rgb:ff/8b/ff",
237 "rgb:ff/cd/00",
238 "rgb:ff/cd/8b",
239 "rgb:ff/cd/cd",
240 "rgb:ff/cd/ff",
241 "rgb:ff/ff/00",
242 "rgb:ff/ff/8b",
243 "rgb:ff/ff/cd",
244 "rgb:ff/ff/ff",
245 "rgb:2e/2e/2e",
246 "rgb:5c/5c/5c",
247 "rgb:73/73/73",
248 "rgb:8b/8b/8b",
249 "rgb:a2/a2/a2",
250 "rgb:b9/b9/b9",
251 "rgb:d0/d0/d0",
252 "rgb:e7/e7/e7",
253
254 #ifndef NO_CURSORCOLOR
255 COLOR_CURSOR_BACKGROUND,
256 COLOR_CURSOR_FOREGROUND,
257 #endif /* ! NO_CURSORCOLOR */
258 NULL, /* Color_pointer_fg */
259 NULL, /* Color_pointer_bg */
260 NULL, /* Color_border */
261 #ifndef NO_BOLD_UNDERLINE_REVERSE
262 NULL, /* Color_BD */
263 NULL, /* Color_IT */
264 NULL, /* Color_UL */
265 NULL, /* Color_RV */
266 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
267 #if ENABLE_FRILLS
268 NULL, // Color_underline
269 #endif
270 #ifdef OPTION_HC
271 NULL,
272 #endif
273 #ifdef KEEP_SCROLLCOLOR
274 COLOR_SCROLLBAR,
275 COLOR_SCROLLTROUGH,
276 #endif /* KEEP_SCROLLCOLOR */
277 #if ENABLE_TRANSPARENCY
278 NULL,
279 #endif
280 #if OFF_FOCUS_FADING
281 "rgb:00/00/00",
282 #endif
283 };
284
285 bool
286 rxvt_term::init_vars ()
287 {
288 pix_colors = //
289 pix_colors_focused = new rxvt_color [TOTAL_COLORS];
290 #ifdef OFF_FOCUS_FADING
291 pix_colors_unfocused = new rxvt_color [TOTAL_COLORS];
292 #endif
293
294
295 MEvent.time = CurrentTime;
296 MEvent.button = AnyButton;
297 want_refresh = 1;
298 priv_modes = SavedModes = PrivMode_Default;
299 ncol = 80;
300 nrow = 24;
301 int_bwidth = INTERNALBORDERWIDTH;
302 ext_bwidth = EXTERNALBORDERWIDTH;
303 lineSpace = LINESPACE;
304 saveLines = SAVELINES;
305 numpix_colors = TOTAL_COLORS;
306
307 refresh_type = SLOW_REFRESH;
308
309 oldcursor.row = oldcursor.col = -1;
310 last_bot = last_state = -1;
311
312 set_option (Opt_scrollBar);
313 set_option (Opt_scrollTtyOutput);
314 set_option (Opt_jumpScroll);
315 set_option (Opt_skipScroll);
316 set_option (Opt_secondaryScreen);
317 set_option (Opt_secondaryScroll);
318 set_option (Opt_pastableTabs);
319 set_option (Opt_intensityStyles);
320 set_option (Opt_iso14755_52);
321
322 return true;
323 }
324
325 void
326 rxvt_term::init_secondary ()
327 {
328 int i;
329
330 /*
331 * Close all unused file descriptors
332 * We don't want them, we don't need them.
333 */
334 if ((i = open ("/dev/null", O_RDONLY)) < 0)
335 {
336 /* TODO: BOO HISS */
337 dup2 (STDERR_FILENO, STDIN_FILENO);
338 }
339 else if (i != STDIN_FILENO)
340 {
341 dup2 (i, STDIN_FILENO);
342 close (i);
343 }
344
345 dup2 (STDERR_FILENO, STDOUT_FILENO);
346
347 #if 0 // schmorp sayz closing filies is murder
348 for (i = STDERR_FILENO + 1; i < num_fds; i++)
349 {
350 #ifdef __sgi /* Alex Coventry says we need 4 & 7 too */
351 if (i == 4 || i == 7)
352 continue;
353 #endif
354 close (i);
355 }
356 #endif
357 }
358
359 /*----------------------------------------------------------------------*/
360 const char **
361 rxvt_term::init_resources (int argc, const char *const *argv)
362 {
363 int i, r_argc;
364 const char **cmd_argv, **r_argv;
365
366 /*
367 * Look for -e option. Find => split and make cmd_argv[] of command args
368 */
369 for (r_argc = 0; r_argc < argc; r_argc++)
370 if (!strcmp (argv[r_argc], "-e"))
371 break;
372
373 if (r_argc == argc)
374 cmd_argv = NULL;
375 else if (!argv[r_argc + 1])
376 rxvt_fatal ("option '-e' requires an argument, aborting.\n");
377 else
378 {
379 cmd_argv = (const char **)rxvt_malloc (sizeof (char *) * (argc - r_argc));
380
381 for (i = 0; i < argc - r_argc - 1; i++)
382 cmd_argv[i] = (const char *)argv[i + r_argc + 1];
383
384 cmd_argv[i] = NULL;
385 }
386
387 r_argv = (const char **)rxvt_malloc (sizeof (char *) * (r_argc + 1));
388
389 for (i = 0; i < r_argc; i++)
390 r_argv[i] = (const char *)argv[i];
391
392 r_argv[i] = NULL;
393
394 rs[Rs_name] = rxvt_basename (argv[0]);
395
396 /*
397 * Open display, get options/resources and create the window
398 */
399
400 if ((rs[Rs_display_name] = getenv ("DISPLAY")) == NULL)
401 rs[Rs_display_name] = ":0";
402
403 get_options (r_argc, r_argv);
404
405 if (!(display = displays.get (rs[Rs_display_name])))
406 {
407 free (r_argv);
408 rxvt_fatal ("can't open display %s, aborting.\n", rs[Rs_display_name]);
409 }
410
411 // using a local pointer decreases code size a lot
412 xa = display->xa;
413
414 set (display);
415 extract_resources ();
416
417 #if XFT
418 if (rs[Rs_depth])
419 select_visual (strtol (rs[Rs_depth], 0, 0));
420 #endif
421
422 #ifdef HAVE_AFTERIMAGE
423 asv = create_asvisual_for_id (dpy, display->screen, depth, XVisualIDFromVisual (visual), cmap, NULL);
424 #endif
425 free (r_argv);
426
427 for (int i = NUM_RESOURCES; i--; )
428 if (rs [i] == resval_undef)
429 rs [i] = 0;
430
431 #if ENABLE_PERL
432 if (!rs[Rs_perl_ext_1])
433 rs[Rs_perl_ext_1] = "default";
434
435 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
436 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
437 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
438 {
439 rxvt_perl.init (this);
440 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
441 }
442 #endif
443
444 /*
445 * set any defaults not already set
446 */
447 if (cmd_argv && cmd_argv[0])
448 {
449 if (!rs[Rs_title])
450 rs[Rs_title] = rxvt_basename (cmd_argv[0]);
451 if (!rs[Rs_iconName])
452 rs[Rs_iconName] = rs[Rs_title];
453 }
454 else
455 {
456 if (!rs[Rs_title])
457 rs[Rs_title] = rs[Rs_name];
458 if (!rs[Rs_iconName])
459 rs[Rs_iconName] = rs[Rs_name];
460 }
461
462 if (rs[Rs_saveLines] && (i = atoi (rs[Rs_saveLines])) >= 0)
463 saveLines = min (i, MAX_SAVELINES);
464
465 #if ENABLE_FRILLS
466 if (rs[Rs_int_bwidth] && (i = atoi (rs[Rs_int_bwidth])) >= 0)
467 int_bwidth = min (i, std::numeric_limits<int16_t>::max ());
468
469 if (rs[Rs_ext_bwidth] && (i = atoi (rs[Rs_ext_bwidth])) >= 0)
470 ext_bwidth = min (i, std::numeric_limits<int16_t>::max ());
471
472 if (rs[Rs_lineSpace] && (i = atoi (rs[Rs_lineSpace])) >= 0)
473 lineSpace = min (i, std::numeric_limits<int16_t>::max ());
474 #endif
475
476 #ifdef POINTER_BLANK
477 if (rs[Rs_pointerBlankDelay] && (i = atoi (rs[Rs_pointerBlankDelay])) >= 0)
478 pointerBlankDelay = i;
479 else
480 pointerBlankDelay = 2;
481 #endif
482
483 /* no point having a scrollbar without having any scrollback! */
484 if (!saveLines)
485 set_option (Opt_scrollBar, 0);
486
487 #ifdef PRINTPIPE
488 if (!rs[Rs_print_pipe])
489 rs[Rs_print_pipe] = PRINTPIPE;
490 #endif
491
492 if (!rs[Rs_cutchars])
493 rs[Rs_cutchars] = CUTCHARS;
494
495 #ifndef NO_BACKSPACE_KEY
496 if (!rs[Rs_backspace_key])
497 # ifdef DEFAULT_BACKSPACE
498 rs[Rs_backspace_key] = DEFAULT_BACKSPACE;
499 # else
500 rs[Rs_backspace_key] = "DEC"; /* can toggle between \010 or \177 */
501 # endif
502 #endif
503
504 #ifndef NO_DELETE_KEY
505 if (!rs[Rs_delete_key])
506 # ifdef DEFAULT_DELETE
507 rs[Rs_delete_key] = DEFAULT_DELETE;
508 # else
509 rs[Rs_delete_key] = "\033[3~";
510 # endif
511 #endif
512
513 #ifdef HAVE_SCROLLBARS
514 setup_scrollbar (rs[Rs_scrollBar_align], rs[Rs_scrollstyle], rs[Rs_scrollBar_thickness]);
515 #endif
516
517 #ifdef XTERM_REVERSE_VIDEO
518 /* this is how xterm implements reverseVideo */
519 if (option (Opt_reverseVideo))
520 {
521 if (!rs[Rs_color + Color_fg])
522 rs[Rs_color + Color_fg] = def_colorName[Color_bg];
523
524 if (!rs[Rs_color + Color_bg])
525 rs[Rs_color + Color_bg] = def_colorName[Color_fg];
526 }
527 #endif
528
529 for (i = 0; i < NRS_COLORS; i++)
530 if (!rs[Rs_color + i])
531 rs[Rs_color + i] = def_colorName[i];
532
533 #ifndef XTERM_REVERSE_VIDEO
534 /* this is how we implement reverseVideo */
535 if (option (Opt_reverseVideo))
536 ::swap (rs[Rs_color + Color_fg], rs[Rs_color + Color_bg]);
537 #endif
538
539 /* convenient aliases for setting fg/bg to colors */
540 color_aliases (Color_fg);
541 color_aliases (Color_bg);
542 #ifndef NO_CURSORCOLOR
543 color_aliases (Color_cursor);
544 color_aliases (Color_cursor2);
545 #endif /* NO_CURSORCOLOR */
546 color_aliases (Color_pointer_fg);
547 color_aliases (Color_pointer_bg);
548 color_aliases (Color_border);
549 #ifndef NO_BOLD_UNDERLINE_REVERSE
550 color_aliases (Color_BD);
551 color_aliases (Color_UL);
552 color_aliases (Color_RV);
553 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
554
555 if (!rs[Rs_color + Color_border])
556 rs[Rs_color + Color_border] = rs[Rs_color + Color_bg];
557
558 return cmd_argv;
559 }
560
561 /*----------------------------------------------------------------------*/
562 void
563 rxvt_term::init_env ()
564 {
565 int i;
566 char *val;
567
568 #ifdef DISPLAY_IS_IP
569 /* Fixup display_name for export over pty to any interested terminal
570 * clients via "ESC[7n" (e.g. shells). Note we use the pure IP number
571 * (for the first non-loopback interface) that we get from
572 * rxvt_network_display (). This is more "name-resolution-portable", if you
573 * will, and probably allows for faster x-client startup if your name
574 * server is beyond a slow link or overloaded at client startup. Of
575 * course that only helps the shell's child processes, not us.
576 *
577 * Giving out the display_name also affords a potential security hole
578 */
579 val = rxvt_network_display (rs[Rs_display_name]);
580 rs[Rs_display_name] = (const char *)val;
581
582 if (val == NULL)
583 #endif /* DISPLAY_IS_IP */
584 val = XDisplayString (dpy);
585
586 if (rs[Rs_display_name] == NULL)
587 rs[Rs_display_name] = val; /* use broken `:0' value */
588
589 i = strlen (val);
590 env_display = (char *)rxvt_malloc (i + 9);
591
592 sprintf (env_display, "DISPLAY=%s", val);
593
594 sprintf (env_windowid, "WINDOWID=%lu", (unsigned long)parent[0]);
595
596 /* add entries to the environment:
597 * @ DISPLAY: in case we started with -display
598 * @ WINDOWID: X window id number of the window
599 * @ COLORTERM: terminal sub-name and also indicates its color
600 * @ TERM: terminal name
601 * @ TERMINFO: path to terminfo directory
602 * @ COLORFGBG: fg;bg color codes
603 */
604 putenv (env_display);
605 putenv (env_windowid);
606
607 if (env_colorfgbg)
608 putenv (env_colorfgbg);
609
610 #ifdef RXVT_TERMINFO
611 putenv ("TERMINFO=" RXVT_TERMINFO);
612 #endif
613
614 if (depth <= 2)
615 putenv ("COLORTERM=" COLORTERMENV "-mono");
616 else
617 putenv ("COLORTERM=" COLORTERMENVFULL);
618
619 if (rs[Rs_term_name] != NULL)
620 {
621 env_term = (char *)rxvt_malloc (strlen (rs[Rs_term_name]) + 6);
622 sprintf (env_term, "TERM=%s", rs[Rs_term_name]);
623 putenv (env_term);
624 }
625 else
626 putenv ("TERM=" TERMENV);
627
628 #ifdef HAVE_UNSETENV
629 /* avoid passing old settings and confusing term size */
630 unsetenv ("LINES");
631 unsetenv ("COLUMNS");
632 unsetenv ("TERMCAP"); /* terminfo should be okay */
633 #endif /* HAVE_UNSETENV */
634 }
635
636 /*----------------------------------------------------------------------*/
637 /*
638 * This is more or less stolen straight from XFree86 xterm.
639 * This should support all European type languages.
640 */
641 void
642 rxvt_term::set_locale (const char *locale)
643 {
644 set_environ (envv);
645
646 #if HAVE_XSETLOCALE || HAVE_SETLOCALE
647 free (this->locale);
648 this->locale = setlocale (LC_CTYPE, locale);
649
650 if (!this->locale)
651 {
652 if (*locale)
653 {
654 rxvt_warn ("unable to set locale \"%s\", using C locale instead.\n", locale);
655 setlocale (LC_CTYPE, "C");
656 }
657 else
658 rxvt_warn ("default locale unavailable, check LC_* and LANG variables. Continuing.\n");
659
660 this->locale = "C";
661 }
662
663
664 this->locale = strdup (this->locale);
665 SET_LOCALE (this->locale);
666 mbstate.reset ();
667 #endif
668
669 #if HAVE_NL_LANGINFO
670 char *codeset = strdup (nl_langinfo (CODESET));
671 // /^UTF.?8/i
672 enc_utf8 = (codeset[0] == 'U' || codeset[0] == 'u')
673 && (codeset[1] == 'T' || codeset[1] == 't')
674 && (codeset[2] == 'F' || codeset[2] == 'f')
675 && (codeset[3] == '8' || codeset[4] == '8');
676 free (codeset);
677 #else
678 enc_utf8 = 0;
679 #endif
680 }
681
682 void
683 rxvt_term::init_xlocale ()
684 {
685 set_environ (envv);
686
687 #ifdef USE_XIM
688 if (!locale)
689 rxvt_warn ("setting locale failed, working without locale support.\n");
690 else
691 {
692 set_string_property (xa[XA_WM_LOCALE_NAME], locale);
693
694 if (!XSupportsLocale ())
695 {
696 rxvt_warn ("the locale is not supported by Xlib, working without locale support.\n");
697 return;
698 }
699
700 im_ev.start (display);
701
702 /* see if we can connect already */
703 im_cb ();
704 }
705 #endif
706 }
707
708 /*----------------------------------------------------------------------*/
709 void
710 rxvt_term::init_command (const char *const *argv)
711 {
712 /*
713 * Initialize the command connection.
714 * This should be called after the X server connection is established.
715 */
716
717 #ifdef META8_OPTION
718 meta_char = option (Opt_meta8) ? 0x80 : C0_ESC;
719 #endif
720
721 get_ourmods ();
722
723 if (!option (Opt_scrollTtyOutput))
724 priv_modes |= PrivMode_TtyOutputInh;
725 if (option (Opt_scrollTtyKeypress))
726 priv_modes |= PrivMode_Keypress;
727 if (!option (Opt_jumpScroll))
728 priv_modes |= PrivMode_smoothScroll;
729
730 #ifndef NO_BACKSPACE_KEY
731 if (strcmp (rs[Rs_backspace_key], "DEC") == 0)
732 priv_modes |= PrivMode_HaveBackSpace;
733 #endif
734
735 /* add value for scrollBar */
736 if (scrollBar.state)
737 {
738 priv_modes |= PrivMode_scrollBar;
739 SavedModes |= PrivMode_scrollBar;
740 }
741
742 run_command (argv);
743 }
744
745 /*----------------------------------------------------------------------*/
746 void
747 rxvt_term::Get_Colours ()
748 {
749 int i;
750
751 #ifdef OFF_FOCUS_FADING
752 pix_colors = pix_colors_focused;
753 #endif
754
755 for (i = 0; i < (depth <= 2 ? 2 : NRS_COLORS); i++)
756 {
757 const char *name = rs[Rs_color + i];
758
759 if (!name)
760 continue;
761
762 rxvt_color xcol;
763
764 if (!set_color (xcol, name))
765 {
766 #ifndef XTERM_REVERSE_VIDEO
767 if (i < 2 && option (Opt_reverseVideo))
768 name = def_colorName [1 - i];
769 else
770 #endif
771 name = def_colorName [i];
772
773 if (!name)
774 continue;
775
776 if (!set_color (xcol, name))
777 {
778 switch (i)
779 {
780 case Color_fg:
781 case Color_bg:
782 rxvt_warn ("unable to get foreground/background colour, continuing.\n");
783 name = "";
784 break;
785 #ifndef NO_CURSORCOLOR
786 case Color_cursor2:
787 #endif
788 case Color_pointer_fg:
789 name = rs[Rs_color + Color_fg];
790 xcol.set (this, name);
791 break;
792 default:
793 name = rs[Rs_color + Color_bg];
794 xcol.set (this, name);
795 break;
796 }
797 }
798 }
799
800 pix_colors[i] = xcol;
801 rs[Rs_color + i] = name;
802 }
803
804 if (depth <= 2)
805 {
806 if (!rs[Rs_color + Color_pointer_fg]) alias_color (Color_pointer_fg, Color_fg);
807 if (!rs[Rs_color + Color_pointer_bg]) alias_color (Color_pointer_bg, Color_bg);
808 if (!rs[Rs_color + Color_border] ) alias_color (Color_border, Color_fg);
809 }
810
811 /*
812 * get scrollBar shadow colors
813 *
814 * The calculations of topShadow/bottomShadow values are adapted
815 * from the fvwm window manager.
816 */
817 #ifdef KEEP_SCROLLCOLOR
818 if (depth <= 2)
819 {
820 /* Monochrome */
821 alias_color (Color_scroll, Color_fg);
822 alias_color (Color_topShadow, Color_bg);
823 alias_color (Color_bottomShadow, Color_bg);
824 }
825 else
826 {
827 pix_colors [Color_scroll].fade (this, 50, pix_colors [Color_bottomShadow]);
828
829 rgba cscroll;
830 pix_colors [Color_scroll].get (cscroll);
831
832 /* topShadowColor */
833 if (!pix_colors[Color_topShadow].set (this,
834 rgba (
835 min ((int)rgba::MAX_CC, max (cscroll.r / 5, cscroll.r) * 7 / 5),
836 min ((int)rgba::MAX_CC, max (cscroll.g / 5, cscroll.g) * 7 / 5),
837 min ((int)rgba::MAX_CC, max (cscroll.b / 5, cscroll.b) * 7 / 5),
838 cscroll.a)
839 ))
840 alias_color (Color_topShadow, Color_White);
841 }
842 #endif /* KEEP_SCROLLCOLOR */
843
844 #ifdef OFF_FOCUS_FADING
845 for (i = 0; i < (depth <= 2 ? 2 : NRS_COLORS); i++)
846 update_fade_color (i);
847 #endif
848 }
849
850 /*----------------------------------------------------------------------*/
851 /* color aliases, fg/bg bright-bold */
852 void
853 rxvt_term::color_aliases (int idx)
854 {
855 if (rs[Rs_color + idx] && isdigit (* (rs[Rs_color + idx])))
856 {
857 int i = atoi (rs[Rs_color + idx]);
858
859 if (i >= 8 && i <= 15)
860 /* bright colors */
861 rs[Rs_color + idx] = rs[Rs_color + minBrightCOLOR + i - 8];
862 else if (i >= 0 && i <= 7)
863 /* normal colors */
864 rs[Rs_color + idx] = rs[Rs_color + minCOLOR + i];
865 }
866 }
867
868 /*----------------------------------------------------------------------*/
869 /*
870 * Probe the modifier keymap to get the Meta (Alt) and Num_Lock settings
871 * Use resource ``modifier'' to override the Meta modifier
872 */
873 void
874 rxvt_term::get_ourmods ()
875 {
876 int i, j, k;
877 int requestedmeta, realmeta, realalt;
878 const char *cm, *rsmod;
879 XModifierKeymap *map;
880 KeyCode *kc;
881 const unsigned int modmasks[] =
882 {
883 Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
884 };
885
886 requestedmeta = realmeta = realalt = 0;
887 rsmod = rs[Rs_modifier];
888
889 if (rsmod
890 && strcasecmp (rsmod, "mod1") >= 0 && strcasecmp (rsmod, "mod5") <= 0)
891 requestedmeta = rsmod[3] - '0';
892
893 map = XGetModifierMapping (dpy);
894 kc = map->modifiermap;
895
896 for (i = 1; i < 6; i++)
897 {
898 k = (i + 2) * map->max_keypermod; /* skip shift/lock/control */
899
900 for (j = map->max_keypermod; j--; k++)
901 {
902 if (kc[k] == 0)
903 break;
904
905 switch (XKeycodeToKeysym (dpy, kc[k], 0))
906 {
907 case XK_Num_Lock:
908 ModNumLockMask = modmasks[i - 1];
909 continue;
910
911 case XK_ISO_Level3_Shift:
912 ModLevel3Mask = modmasks[i - 1];
913 continue;
914
915 case XK_Meta_L:
916 case XK_Meta_R:
917 cm = "meta";
918 realmeta = i;
919 break;
920
921 case XK_Alt_L:
922 case XK_Alt_R:
923 cm = "alt";
924 realalt = i;
925 break;
926
927 case XK_Super_L:
928 case XK_Super_R:
929 cm = "super";
930 break;
931
932 case XK_Hyper_L:
933 case XK_Hyper_R:
934 cm = "hyper";
935 break;
936
937 default:
938 continue;
939 }
940
941 if (rsmod && strncasecmp (rsmod, cm, strlen (cm)) == 0)
942 requestedmeta = i;
943 }
944 }
945
946 XFreeModifiermap (map);
947
948 i = requestedmeta ? requestedmeta
949 : realmeta ? realmeta
950 : realalt ? realalt
951 : 0;
952
953 if (i)
954 ModMetaMask = modmasks[i - 1];
955 }
956
957 /*----------------------------------------------------------------------*/
958 /* rxvt_Create_Windows () - Open and map the window */
959 void
960 rxvt_term::create_windows (int argc, const char *const *argv)
961 {
962 XClassHint classHint;
963 XWMHints wmHint;
964 #if ENABLE_FRILLS
965 MWMHints mwmhints = { };
966 #endif
967 XGCValues gcvalue;
968 XSetWindowAttributes attributes;
969 Window top, parent;
970
971 dLocal (Display *, dpy);
972
973 /* grab colors before netscape does */
974 Get_Colours ();
975
976 if (!set_fonts ())
977 rxvt_fatal ("unable to load base fontset, please specify a valid one using -fn, aborting.\n");
978
979 parent = display->root;
980
981 attributes.override_redirect = !!option (Opt_override_redirect);
982
983 #if ENABLE_FRILLS
984 if (option (Opt_borderLess))
985 {
986 if (XInternAtom (dpy, "_MOTIF_WM_INFO", True) == None)
987 {
988 // rxvt_warn("Window Manager does not support MWM hints. Bypassing window manager control for borderless window.\n");
989 attributes.override_redirect = true;
990 }
991 else
992 {
993 mwmhints.flags = MWM_HINTS_DECORATIONS;
994 }
995 }
996 #endif
997
998 #if ENABLE_XEMBED
999 if (rs[Rs_embed])
1000 {
1001 XWindowAttributes wattr;
1002
1003 parent = strtol (rs[Rs_embed], 0, 0);
1004
1005 if (!XGetWindowAttributes (dpy, parent, &wattr))
1006 rxvt_fatal ("invalid window-id specified with -embed, aborting.\n");
1007
1008 window_calc (wattr.width, wattr.height);
1009 }
1010 #endif
1011
1012 window_calc (0, 0);
1013
1014 /* sub-window placement & size in rxvt_resize_subwindows () */
1015 attributes.background_pixel = pix_colors_focused [Color_border];
1016 attributes.border_pixel = pix_colors_focused [Color_border];
1017 attributes.colormap = cmap;
1018
1019 top = XCreateWindow (dpy, parent,
1020 szHint.x, szHint.y,
1021 szHint.width, szHint.height,
1022 ext_bwidth,
1023 depth, InputOutput, visual,
1024 CWColormap | CWBackPixel | CWBorderPixel | CWOverrideRedirect,
1025 &attributes);
1026
1027 this->parent[0] = top;
1028
1029 old_width = szHint.width;
1030 old_height = szHint.height;
1031
1032 process_xterm_seq (XTerm_title, rs[Rs_title], CHAR_ST);
1033 process_xterm_seq (XTerm_iconName, rs[Rs_iconName], CHAR_ST);
1034
1035 classHint.res_name = (char *)rs[Rs_name];
1036 classHint.res_class = (char *)RESCLASS;
1037
1038 wmHint.flags = InputHint | StateHint | WindowGroupHint;
1039 wmHint.input = True;
1040 wmHint.initial_state = option (Opt_iconic) ? IconicState : NormalState;
1041 wmHint.window_group = top;
1042
1043 XmbSetWMProperties (dpy, top, NULL, NULL, (char **)argv, argc,
1044 &szHint, &wmHint, &classHint);
1045
1046 #if ENABLE_FRILLS
1047 if (mwmhints.flags)
1048 XChangeProperty (dpy, top, xa[XA_MOTIF_WM_HINTS], xa[XA_MOTIF_WM_HINTS], 32,
1049 PropModeReplace, (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS);
1050 #endif
1051
1052 Atom protocols[] = {
1053 xa[XA_WM_DELETE_WINDOW],
1054 #if ENABLE_EWMH
1055 xa[XA_NET_WM_PING],
1056 #endif
1057 };
1058
1059 XSetWMProtocols (dpy, top, protocols, sizeof (protocols) / sizeof (protocols[0]));
1060
1061 #if ENABLE_FRILLS
1062 if (rs[Rs_transient_for])
1063 XSetTransientForHint (dpy, top, (Window)strtol (rs[Rs_transient_for], 0, 0));
1064 #endif
1065
1066 #if ENABLE_EWMH
1067 long pid = getpid ();
1068
1069 XChangeProperty (dpy, top,
1070 xa[XA_NET_WM_PID], XA_CARDINAL, 32,
1071 PropModeReplace, (unsigned char *)&pid, 1);
1072
1073 // _NET_WM_WINDOW_TYPE is NORMAL, which is the default
1074 #endif
1075
1076 XSelectInput (dpy, top,
1077 KeyPressMask
1078 #if (MOUSE_WHEEL && MOUSE_SLIP_WHEELING) || ENABLE_FRILLS || ISO_14755
1079 | KeyReleaseMask
1080 #endif
1081 | FocusChangeMask | VisibilityChangeMask
1082 | ExposureMask | StructureNotifyMask);
1083
1084 termwin_ev.start (display, top);
1085
1086 /* vt cursor: Black-on-White is standard, but this is more popular */
1087 TermWin_cursor = XCreateFontCursor (dpy, XC_xterm);
1088
1089 #ifdef HAVE_SCROLLBARS
1090 /* cursor scrollBar: Black-on-White */
1091 leftptr_cursor = XCreateFontCursor (dpy, XC_left_ptr);
1092 #endif
1093
1094 /* the vt window */
1095 vt = XCreateSimpleWindow (dpy, top,
1096 window_vt_x, window_vt_y,
1097 width, height,
1098 0,
1099 pix_colors_focused[Color_fg],
1100 pix_colors_focused[Color_bg]);
1101
1102 attributes.bit_gravity = NorthWestGravity;
1103 XChangeWindowAttributes (dpy, vt, CWBitGravity, &attributes);
1104
1105 vt_emask = ExposureMask | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask;
1106
1107 if (option (Opt_pointerBlank))
1108 vt_emask |= PointerMotionMask;
1109 else
1110 vt_emask |= Button1MotionMask | Button3MotionMask;
1111
1112 vt_select_input ();
1113
1114 vt_ev.start (display, vt);
1115
1116 /* graphics context for the vt window */
1117 gcvalue.foreground = pix_colors[Color_fg];
1118 gcvalue.background = pix_colors[Color_bg];
1119 gcvalue.graphics_exposures = 0;
1120
1121 gc = XCreateGC (dpy, vt,
1122 GCForeground | GCBackground | GCGraphicsExposures,
1123 &gcvalue);
1124
1125 drawable = new rxvt_drawable (this, vt);
1126
1127 #ifdef RXVT_SCROLLBAR
1128 gcvalue.foreground = pix_colors[Color_topShadow];
1129 topShadowGC = XCreateGC (dpy, vt, GCForeground, &gcvalue);
1130 gcvalue.foreground = pix_colors[Color_bottomShadow];
1131 botShadowGC = XCreateGC (dpy, vt, GCForeground, &gcvalue);
1132 gcvalue.foreground = pix_colors[ (depth <= 2 ? Color_fg : Color_scroll)];
1133 scrollbarGC = XCreateGC (dpy, vt, GCForeground, &gcvalue);
1134 #endif
1135
1136 #ifdef OFF_FOCUS_FADING
1137 // initially we are in unfocused state
1138 if (rs[Rs_fade])
1139 pix_colors = pix_colors_unfocused;
1140 #endif
1141
1142 pointer_unblank ();
1143 scr_recolour ();
1144 }
1145
1146 /* ------------------------------------------------------------------------- *
1147 * GET TTY CURRENT STATE *
1148 * ------------------------------------------------------------------------- */
1149 void
1150 rxvt_get_ttymode (ttymode_t *tio, int erase)
1151 {
1152 /*
1153 * standard System V termios interface
1154 */
1155 if (GET_TERMIOS (STDIN_FILENO, tio) < 0)
1156 {
1157 // return error - use system defaults,
1158 // where possible, and zero elsewhere
1159 memset (tio, 0, sizeof (ttymode_t));
1160
1161 tio->c_cc[VINTR] = CINTR;
1162 tio->c_cc[VQUIT] = CQUIT;
1163 tio->c_cc[VERASE] = CERASE;
1164 #ifdef VERASE2
1165 tio->c_cc[VERASE2] = CERASE2;
1166 #endif
1167 tio->c_cc[VKILL] = CKILL;
1168 tio->c_cc[VSTART] = CSTART;
1169 tio->c_cc[VSTOP] = CSTOP;
1170 tio->c_cc[VSUSP] = CSUSP;
1171 # ifdef VDSUSP
1172 tio->c_cc[VDSUSP] = CDSUSP;
1173 # endif
1174 # ifdef VREPRINT
1175 tio->c_cc[VREPRINT] = CRPRNT;
1176 # endif
1177 # ifdef VDISCRD
1178 tio->c_cc[VDISCRD] = CFLUSH;
1179 # endif
1180 # ifdef VWERSE
1181 tio->c_cc[VWERSE] = CWERASE;
1182 # endif
1183 # ifdef VLNEXT
1184 tio->c_cc[VLNEXT] = CLNEXT;
1185 # endif
1186 }
1187
1188 tio->c_cc[VEOF] = CEOF;
1189 tio->c_cc[VEOL] = VDISABLE;
1190 # ifdef VEOL2
1191 tio->c_cc[VEOL2] = VDISABLE;
1192 # endif
1193 # ifdef VSWTC
1194 tio->c_cc[VSWTC] = VDISABLE;
1195 # endif
1196 # ifdef VSWTCH
1197 tio->c_cc[VSWTCH] = VDISABLE;
1198 # endif
1199 # if VMIN != VEOF
1200 tio->c_cc[VMIN] = 1;
1201 # endif
1202 # if VTIME != VEOL
1203 tio->c_cc[VTIME] = 0;
1204 # endif
1205
1206 if (erase != -1)
1207 tio->c_cc[VERASE] = (char)erase;
1208
1209 /* input modes */
1210 tio->c_iflag = (BRKINT | IGNPAR | ICRNL
1211 # ifdef IMAXBEL
1212 | IMAXBEL
1213 # endif
1214 | IXON);
1215
1216 /* output modes */
1217 tio->c_oflag = (OPOST | ONLCR);
1218
1219 /* control modes */
1220 tio->c_cflag = (CS8 | CREAD);
1221
1222 /* line discipline modes */
1223 tio->c_lflag = (ISIG | ICANON | IEXTEN | ECHO
1224 # if defined (ECHOCTL) && defined (ECHOKE)
1225 | ECHOCTL | ECHOKE
1226 # endif
1227 | ECHOE | ECHOK);
1228
1229 /*
1230 * Debugging
1231 */
1232 #ifdef DEBUG_TTYMODE
1233 /* c_iflag bits */
1234 fprintf (stderr, "Input flags\n");
1235
1236 /* cpp token stringize doesn't work on all machines <sigh> */
1237 # define FOO(flag,name) \
1238 if ((tio->c_iflag) & flag) \
1239 fprintf (stderr, "%s ", name)
1240
1241 /* c_iflag bits */
1242 FOO (IGNBRK, "IGNBRK");
1243 FOO (BRKINT, "BRKINT");
1244 FOO (IGNPAR, "IGNPAR");
1245 FOO (PARMRK, "PARMRK");
1246 FOO (INPCK, "INPCK");
1247 FOO (ISTRIP, "ISTRIP");
1248 FOO (INLCR, "INLCR");
1249 FOO (IGNCR, "IGNCR");
1250 FOO (ICRNL, "ICRNL");
1251 FOO (IXON, "IXON");
1252 FOO (IXOFF, "IXOFF");
1253 # ifdef IUCLC
1254 FOO (IUCLC, "IUCLC");
1255 # endif
1256 # ifdef IXANY
1257 FOO (IXANY, "IXANY");
1258 # endif
1259 # ifdef IMAXBEL
1260 FOO (IMAXBEL, "IMAXBEL");
1261 # endif
1262
1263 fprintf (stderr, "\n");
1264
1265 # undef FOO
1266 # define FOO(entry, name) \
1267 fprintf (stderr, "%-8s = %#04o\n", name, tio->c_cc [entry])
1268
1269 FOO (VINTR, "VINTR");
1270 FOO (VQUIT, "VQUIT");
1271 FOO (VERASE, "VERASE");
1272 FOO (VKILL, "VKILL");
1273 FOO (VEOF, "VEOF");
1274 FOO (VEOL, "VEOL");
1275 # ifdef VEOL2
1276 FOO (VEOL2, "VEOL2");
1277 # endif
1278 # ifdef VSWTC
1279 FOO (VSWTC, "VSWTC");
1280 # endif
1281 # ifdef VSWTCH
1282 FOO (VSWTCH, "VSWTCH");
1283 # endif
1284 FOO (VSTART, "VSTART");
1285 FOO (VSTOP, "VSTOP");
1286 FOO (VSUSP, "VSUSP");
1287 # ifdef VDSUSP
1288 FOO (VDSUSP, "VDSUSP");
1289 # endif
1290 # ifdef VREPRINT
1291 FOO (VREPRINT, "VREPRINT");
1292 # endif
1293 # ifdef VDISCRD
1294 FOO (VDISCRD, "VDISCRD");
1295 # endif
1296 # ifdef VWERSE
1297 FOO (VWERSE, "VWERSE");
1298 # endif
1299 # ifdef VLNEXT
1300 FOO (VLNEXT, "VLNEXT");
1301 # endif
1302
1303 fprintf (stderr, "\n");
1304 # undef FOO
1305 #endif /* DEBUG_TTYMODE */
1306 }
1307
1308 /*----------------------------------------------------------------------*/
1309 /*
1310 * Run the command in a subprocess and return a file descriptor for the
1311 * master end of the pseudo-teletype pair with the command talking to
1312 * the slave.
1313 */
1314 void
1315 rxvt_term::run_command (const char *const *argv)
1316 {
1317 #if ENABLE_FRILLS
1318 if (rs[Rs_pty_fd])
1319 {
1320 pty->pty = atoi (rs[Rs_pty_fd]);
1321
1322 if (pty->pty >= 0)
1323 {
1324 if (getfd_hook)
1325 pty->pty = (*getfd_hook) (pty->pty);
1326
1327 if (pty->pty < 0 || fcntl (pty->pty, F_SETFL, O_NONBLOCK))
1328 rxvt_fatal ("unusable pty-fd filehandle, aborting.\n");
1329 }
1330 }
1331 else
1332 #endif
1333 if (!pty->get ())
1334 rxvt_fatal ("can't initialize pseudo-tty, aborting.\n");
1335
1336 int er;
1337
1338 #ifndef NO_BACKSPACE_KEY
1339 if (rs[Rs_backspace_key][0] && !rs[Rs_backspace_key][1])
1340 er = rs[Rs_backspace_key][0];
1341 else if (strcmp (rs[Rs_backspace_key], "DEC") == 0)
1342 er = '\177'; /* the initial state anyway */
1343 else
1344 #endif
1345 er = -1;
1346
1347 rxvt_get_ttymode (&tio, er);
1348 SET_TERMIOS (pty->tty, &tio); /* init terminal attributes */
1349 pty->set_utf8_mode (enc_utf8);
1350
1351 /* set initial window size */
1352 tt_winch ();
1353
1354 #if ENABLE_FRILLS
1355 if (rs[Rs_pty_fd])
1356 return;
1357 #endif
1358
1359 /* spin off the command interpreter */
1360 switch (cmd_pid = fork ())
1361 {
1362 case -1:
1363 {
1364 cmd_pid = 0;
1365 rxvt_fatal ("can't fork, aborting.\n");
1366 }
1367 case 0:
1368 init_env ();
1369
1370 if (!pty->make_controlling_tty ())
1371 fprintf (stderr, "%s: could not obtain control of tty.", RESNAME);
1372 else
1373 {
1374 /* Reopen stdin, stdout and stderr over the tty file descriptor */
1375 dup2 (pty->tty, STDIN_FILENO);
1376 dup2 (pty->tty, STDOUT_FILENO);
1377 dup2 (pty->tty, STDERR_FILENO);
1378
1379 // close all our file handles that we do no longer need
1380 for (rxvt_term **t = termlist.begin (); t < termlist.end (); t++)
1381 {
1382 if ((*t)->pty->pty > 2) close ((*t)->pty->pty);
1383 if ((*t)->pty->tty > 2) close ((*t)->pty->tty);
1384 }
1385
1386 run_child (argv);
1387 fprintf (stderr, "%s: unable to exec child.", RESNAME);
1388 }
1389
1390 _exit (EXIT_FAILURE);
1391
1392 default:
1393 if (!option (Opt_utmpInhibit))
1394 pty->login (cmd_pid, option (Opt_loginShell), rs[Rs_display_name]);
1395
1396 pty->close_tty ();
1397
1398 child_ev.start (cmd_pid);
1399
1400 HOOK_INVOKE ((this, HOOK_CHILD_START, DT_INT, cmd_pid, DT_END));
1401 break;
1402 }
1403 }
1404
1405 /* ------------------------------------------------------------------------- *
1406 * CHILD PROCESS OPERATIONS *
1407 * ------------------------------------------------------------------------- */
1408 /*
1409 * The only open file descriptor is the slave tty - so no error messages.
1410 * returns are fatal
1411 */
1412 int
1413 rxvt_term::run_child (const char *const *argv)
1414 {
1415 char *login;
1416
1417 if (option (Opt_console))
1418 {
1419 /* be virtual console, fail silently */
1420 #ifdef TIOCCONS
1421 unsigned int on = 1;
1422
1423 ioctl (STDIN_FILENO, TIOCCONS, &on);
1424 #elif defined (SRIOCSREDIR)
1425 int fd;
1426
1427 fd = open (CONSOLE, O_WRONLY, 0);
1428 if (fd >= 0)
1429 if (ioctl (fd, SRIOCSREDIR, NULL) < 0)
1430 close (fd);
1431 #endif /* SRIOCSREDIR */
1432 }
1433
1434 /* reset signals and spin off the command interpreter */
1435 signal (SIGINT, SIG_DFL);
1436 signal (SIGQUIT, SIG_DFL);
1437 signal (SIGCHLD, SIG_DFL);
1438 signal (SIGHUP, SIG_DFL);
1439 signal (SIGPIPE, SIG_DFL);
1440 /*
1441 * mimick login's behavior by disabling the job control signals
1442 * a shell that wants them can turn them back on
1443 */
1444 #ifdef SIGTSTP
1445 signal (SIGTSTP, SIG_IGN);
1446 signal (SIGTTIN, SIG_IGN);
1447 signal (SIGTTOU, SIG_IGN);
1448 #endif /* SIGTSTP */
1449
1450 // unblock signals (signals are blocked by iom.C
1451 sigset_t ss;
1452 sigemptyset (&ss);
1453 sigprocmask (SIG_SETMASK, &ss, 0);
1454
1455 /* command interpreter path */
1456 if (argv)
1457 {
1458 # ifdef DEBUG_CMD
1459 int i;
1460
1461 for (i = 0; argv[i]; i++)
1462 fprintf (stderr, "argv [%d] = \"%s\"\n", i, argv[i]);
1463 # endif
1464
1465 execvp (argv[0], (char *const *)argv);
1466 /* no error message: STDERR is closed! */
1467 }
1468 else
1469 {
1470 const char *argv0, *shell;
1471
1472 if ((shell = getenv ("SHELL")) == NULL || *shell == '\0')
1473 shell = "/bin/sh";
1474
1475 argv0 = (const char *)rxvt_basename (shell);
1476
1477 if (option (Opt_loginShell))
1478 {
1479 login = (char *)rxvt_malloc (strlen (argv0) + 2);
1480
1481 login[0] = '-';
1482 strcpy (&login[1], argv0);
1483 argv0 = login;
1484 }
1485
1486 execlp (shell, argv0, (char *)0);
1487 /* no error message: STDERR is closed! */
1488 }
1489
1490 return -1;
1491 }
1492
1493 /*----------------------- end-of-file (C source) -----------------------*/