ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/init.C
Revision: 1.238
Committed: Fri Jan 4 17:57:28 2008 UTC (16 years, 4 months ago) by ayin
Content type: text/plain
Branch: MAIN
Changes since 1.237: +0 -5 lines
Log Message:
Move leftptr_cursor from rxvt_term to scrollBar_t.

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