1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
7 * Authors: Hadi Bargi Rangin bargi@dots.physics.orst.edu
8 * Bill Barry barryb@dots.physics.orst.edu
9 * Randy Lundquist randyl@dots.physics.orst.edu
11 * Modifications Copyright (c) 1995 by
12 * Science Access Project, Oregon State University.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING); if not, write to the
27 * Free Software Foundation, Inc.,
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30 ****************************************************************
33 #include <sys/types.h>
39 # include <sys/sysmacros.h>
44 # include <sys/ioctl.h>
54 # include <sys/stropts.h>
57 #if defined(SYSV) && !defined(ISC)
58 # include <sys/utsname.h>
61 #if defined(sequent) || defined(SVR4)
62 # include <sys/resource.h>
63 #endif /* sequent || SVR4 */
67 # include <sys/sioctl.h>
71 #if (defined(AUX) || defined(_AUX_SOURCE)) && defined(POSIX)
74 #if defined(USE_LOCALE) || defined(ENCODINGS)
77 #if defined(HAVE_NL_LANGINFO) && defined(ENCODINGS)
78 # include <langinfo.h>
86 #include "patchlevel.h"
89 * At the moment we only need the real password if the
90 * builtin lock is used. Therefore disable SHADOWPW if
91 * we do not really need it (kind of security thing).
100 #endif /* SHADOWPW */
102 #include "logfile.h" /* islogfile, logfflush */
109 extern char Term
[], screenterm
[], **environ
, Termcap
[];
111 int VBellWait
, MsgWait
, MsgMinWait
, SilenceWait
;
113 extern struct acluser
*users
;
114 extern struct display
*displays
, *display
;
117 extern int visual_bell
;
119 extern unsigned char mark_key_tab
[];
121 extern char version
[];
122 extern char DefaultShell
[];
124 extern char *zmodem_sendcmd
;
125 extern char *zmodem_recvcmd
;
127 extern struct layout
*layout_last
;
133 extern struct NewWindow nwin_undef
, nwin_default
, nwin_options
;
136 static struct passwd
*getpwbyname
__P((char *, struct passwd
*));
137 static void SigChldHandler
__P((void));
138 static sigret_t SigChld
__P(SIGPROTOARG
);
139 static sigret_t SigInt
__P(SIGPROTOARG
);
140 static sigret_t CoreDump
__P(SIGPROTOARG
);
141 static sigret_t FinitHandler
__P(SIGPROTOARG
);
142 static void DoWait
__P((void));
143 static void serv_read_fn
__P((struct event
*, char *));
144 static void serv_select_fn
__P((struct event
*, char *));
145 static void logflush_fn
__P((struct event
*, char *));
146 static void backtick_filter
__P((struct backtick
*));
147 static void backtick_fn
__P((struct event
*, char *));
148 static char *runbacktick
__P((struct backtick
*, int *, time_t));
149 static int IsSymbol
__P((char *, char *));
150 static char *ParseChar
__P((char *, char *));
151 static int ParseEscape
__P((char *));
152 static char *pad_expand
__P((char *, char *, int, int));
154 static void fds
__P((void));
157 int nversion
; /* numerical version, used for secondary DA */
165 struct mode attach_Mode
;
167 char SockPath
[MAXPATHLEN
+ 2 * MAXSTR
];
168 char *SockName
; /* SockName is pointer in SockPath */
169 char *SockMatch
= NULL
; /* session id command line argument */
170 int ServerSocket
= -1;
171 struct event serv_read
;
172 struct event serv_select
;
173 struct event logflushev
;
175 char **NewEnv
= NULL
;
177 char *RcFileName
= NULL
;
180 char *screenlogfile
; /* filename layout */
181 int log_flush
= 10; /* flush interval in seconds */
182 int logtstamp_on
= 0; /* tstamp disabled */
183 char *logtstamp_string
; /* stamp layout */
184 int logtstamp_after
= 120; /* first tstamp after 120s */
185 char *hardcopydir
= NULL
;
187 char *VisualBellString
;
188 char *ActivityString
;
193 char *PowDetachString
;
201 int iflag
, rflag
, dflag
, lsflag
, quietflag
, wipeflag
, xflag
;
212 int tty_oldmode
= -1;
215 char HostName
[MAXSTR
];
216 int MasterPid
, PanicPid
;
217 int real_uid
, real_gid
, eff_uid
, eff_gid
;
219 int ZombieKey_destroy
, ZombieKey_resurrect
, ZombieKey_onerror
;
220 char *preselect
= NULL
; /* only used in Attach() */
223 char *screenencodings
;
232 struct layer
*flayer
;
235 struct win
*console_window
;
244 char strnomem
[] = "Out of memory.";
247 static int InterruptPlease
;
248 static int GotSigChld
;
251 lf_secreopen(name
, wantfd
, l
)
259 if (((got_fd
= secopen(name
, O_WRONLY
| O_CREAT
| O_APPEND
, 0666)) < 0) ||
260 lf_move_fd(got_fd
, wantfd
) < 0)
263 debug1("lf_secreopen: failed for %s\n", name
);
266 l
->st
->st_ino
= l
->st
->st_dev
= 0;
267 debug2("lf_secreopen: %d = %s\n", wantfd
, name
);
271 /********************************************************************/
272 /********************************************************************/
273 /********************************************************************/
276 static struct passwd
*
277 getpwbyname(name
, ppp
)
283 struct spwd
*sss
= NULL
;
284 static char *spw
= NULL
;
287 if (!ppp
&& !(ppp
= getpwnam(name
)))
290 /* Do password sanity check..., allow ##user for SUN_C2 security */
295 if (ppp
->pw_passwd
[0] == '#' && ppp
->pw_passwd
[1] == '#' &&
296 strcmp(ppp
->pw_passwd
+ 2, ppp
->pw_name
) == 0)
300 char c
= ppp
->pw_passwd
[n
];
301 if (!(c
== '.' || c
== '/' || c
== '$' ||
302 (c
>= '0' && c
<= '9') ||
303 (c
>= 'a' && c
<= 'z') ||
304 (c
>= 'A' && c
<= 'Z')))
309 /* try to determine real password */
310 if (n
< 13 && sss
== 0)
312 sss
= getspnam(ppp
->pw_name
);
317 ppp
->pw_passwd
= spw
= SaveStr(sss
->sp_pwdp
);
318 endspent(); /* this should delete all buffers ... */
321 endspent(); /* this should delete all buffers ... */
327 if (ppp
->pw_passwd
&& strlen(ppp
->pw_passwd
) == 13 + 11)
328 ppp
->pw_passwd
[13] = 0; /* beware of linux's long passwords */
343 char socknamebuf
[2 * MAXSTR
];
345 char *myname
= (ac
== 0) ? "screen" : av
[0];
348 #ifdef _MODE_T /* (jw) */
353 #if defined(SYSV) && !defined(ISC)
354 struct utsname utsnam
;
356 struct NewWindow nwin
;
357 int detached
= 0; /* start up detached */
361 char *script_file
= 0;
364 #if (defined(AUX) || defined(_AUX_SOURCE)) && defined(POSIX)
365 setcompat(COMPAT_POSIX
|COMPAT_BSDPROT
); /* turn on seteuid support */
367 #if defined(sun) && defined(SVR4)
369 /* Solaris' login blocks SIGHUP! This is _very bad_ */
372 sigprocmask(SIG_SETMASK
, &sset
, 0);
377 * First, close all unused descriptors
378 * (otherwise, we might have problems with the select() call)
384 sprintf(version
, "%d.%.2d.%.2d%s (%s) %s", REV
, VERS
,
385 PATCHLEVEL
, STATE
, ORIGIN
, DATE
);
386 nversion
= REV
* 10000 + VERS
* 100 + PATCHLEVEL
;
387 debug2("-- screen debug started %s (%s)\n", *av
, version
);
401 debug("NAMEDPIPE\n");
403 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
404 debug("Window size changing enabled\n");
434 debug1("NAME_MAX = %d\n", NAME_MAX
);
437 BellString
= SaveStr("Bell in window %n");
438 VisualBellString
= SaveStr(" Wuff, Wuff!! ");
439 ActivityString
= SaveStr("Activity in window %n");
440 screenlogfile
= SaveStr("screenlog.%n");
441 logtstamp_string
= SaveStr("-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n");
442 hstatusstring
= SaveStr("%h");
443 captionstring
= SaveStr("%3n %t");
444 timestring
= SaveStr("%c:%s %M %d %H%? %l%?");
445 wlisttit
= SaveStr("Num Name%=Flags");
446 wliststr
= SaveStr("%3n %t%=%f");
448 BufferFile
= SaveStr(DEFAULT_BUFFERFILE
);
454 default_startup
= (ac
> 1) ? 0 : 1;
456 VBellWait
= VBELLWAIT
* 1000;
457 MsgWait
= MSGWAIT
* 1000;
458 MsgMinWait
= MSGMINWAIT
* 1000;
459 SilenceWait
= SILENCEWAIT
;
464 zmodem_sendcmd
= SaveStr("!!! sz -vv -b ");
465 zmodem_recvcmd
= SaveStr("!!! rz -vv -b -E");
469 CompileKeys((char *)0, 0, mark_key_tab
);
473 screenencodings
= SaveStr(SCREENENCODINGS
);
476 nwin_options
= nwin_undef
;
477 strcpy(screenterm
, "screen");
479 logreopen_register(lf_secreopen
);
482 /* if this is a login screen, assume -RR */
491 ShellProg
= SaveStr(DefaultShell
); /* to prevent nasty circles */
496 if (--ac
> 0 && *ap
== '-')
498 if (ap
[1] == '-' && ap
[2] == 0)
504 if (ap
[1] == '-' && !strcmp(ap
, "--version"))
505 Panic(0, "Screen version %s", version
);
506 if (ap
[1] == '-' && !strcmp(ap
, "--help"))
507 exit_with_usage(myname
, NULL
, NULL
);
508 while (ap
&& *ap
&& *++ap
)
513 nwin_options
.aflag
= 1;
518 case 'p': /* preselect */
524 exit_with_usage(myname
, "Specify a window to preselect with -p", NULL
);
531 bd
.bd_start_braille
= 1;
540 exit_with_usage(myname
, "Specify an alternate rc-filename with -c", NULL
);
549 exit_with_usage(myname
, "Specify command characters with -e", NULL
);
553 Panic(0, "Two characters are required with -e option, not '%s'.", ap
);
562 nwin_options
.flowflag
= FLOW_NOW
* 0;
569 nwin_options
.flowflag
= FLOW_NOW
* 1;
572 nwin_options
.flowflag
= FLOW_AUTOFLAG
;
575 exit_with_usage(myname
, "Unknown flow option -%s", --ap
);
580 exit_with_usage(myname
, NULL
, NULL
);
581 nwin_options
.histheight
= atoi(*++av
);
582 if (nwin_options
.histheight
< 0)
583 exit_with_usage(myname
, "-h: %s: negative scrollback size?", *av
);
588 case 't': /* title, the former AkA == -k */
590 exit_with_usage(myname
, "Specify a new window-name with -t", NULL
);
591 nwin_options
.aka
= *++av
;
599 nwin_options
.lflag
= 0;
606 nwin_options
.lflag
= 1;
609 nwin_options
.lflag
= 3;
612 case 'i': /* -list */
614 if (ac
> 1 && !SockMatch
)
622 exit_with_usage(myname
, "%s: Unknown suboption to -l", --ap
);
628 if (ac
> 1 && !SockMatch
)
635 nwin_options
.Lflag
= 1;
640 case 'O': /* to be (or not to be?) deleted. jw. */
645 exit_with_usage(myname
, "Specify terminal-type with -T", NULL
);
646 if (strlen(*++av
) < 20)
647 strcpy(screenterm
, *av
);
649 Panic(0, "-T: terminal name too long. (max. 20 char)");
650 nwin_options
.term
= screenterm
;
660 if (ac
> 1 && *av
[1] != '-' && !SockMatch
)
664 debug2("rflag=%d, SockMatch=%s\n", dflag
, SockMatch
);
672 rflag
+= (*ap
== 'R') ? 2 : 1;
683 if (*av
[1] != '-' && !SockMatch
)
687 debug2("dflag=%d, SockMatch=%s\n", dflag
, SockMatch
);
694 exit_with_usage(myname
, "Specify shell with -s", NULL
);
697 ShellProg
= SaveStr(*++av
);
698 debug1("ShellProg: '%s'\n", ShellProg
);
704 exit_with_usage(myname
, "Specify session-name with -S", NULL
);
708 exit_with_usage(myname
, "Empty session-name?", NULL
);
714 Panic(0, "Screen version %s", version
);
718 nwin_options
.encoding
= nwin_options
.encoding
== -1 ? UTF8
: 0;
723 exit_with_usage(myname
, "Specify lua script file with -u", NULL
);
726 script_file
= SaveStr(*++av
);
729 exit_with_usage(myname
, "Unknown option %s", --ap
);
741 if (eff_uid
!= real_uid
)
743 /* if running with s-bit, we must install a special signal
744 * handler routine that resets the s-bit, so that we get a
747 #ifdef SIGBUS /* OOPS, linux has no bus errors! */
748 signal(SIGBUS
, CoreDump
);
750 signal(SIGSEGV
, CoreDump
);
754 setlocale(LC_ALL
, "");
757 if (nwin_options
.encoding
== -1)
759 /* ask locale if we should start in UTF-8 mode */
760 # ifdef HAVE_NL_LANGINFO
762 setlocale(LC_CTYPE
, "");
764 nwin_options
.encoding
= FindEncoding(nl_langinfo(CODESET
));
765 debug1("locale says encoding = %d\n", nwin_options
.encoding
);
769 if (((s
= getenv("LC_ALL")) || (s
= getenv("LC_CTYPE")) ||
770 (s
= getenv("LANG"))) && InStr(s
, "UTF-8"))
771 nwin_options
.encoding
= UTF8
;
773 debug1("environment says encoding=%d\n", nwin_options
.encoding
);
777 if (SockMatch
&& strlen(SockMatch
) >= MAXSTR
)
778 Panic(0, "Ridiculously long socketname - try again.");
779 if (cmdflag
&& !rflag
&& !dflag
&& !xflag
)
781 if (!cmdflag
&& dflag
&& mflag
&& !(rflag
|| xflag
))
785 nwin
.encoding
= nwin_undef
.encoding
; /* let screenrc overwrite it */
790 /* make the write() calls return -1 on all errors */
793 * Ronald F. Guilmette, Oct 29 '94, bug-gnu-utils@prep.ai.mit.edu:
794 * It appears that in System V Release 4, UNIX, if you are writing
795 * an output file and you exceed the currently set file size limit,
796 * you _don't_ just get the call to `write' returning with a
797 * failure code. Rather, you get a signal called `SIGXFSZ' which,
798 * if neither handled nor ignored, will cause your program to crash
801 signal(SIGXFSZ
, SIG_IGN
);
805 signal(SIGPIPE
, SIG_IGN
);
812 sh
= getenv("SHELL");
813 ShellProg
= SaveStr(sh
? sh
: DefaultShell
);
815 ShellArgs
[0] = ShellProg
;
816 home
= getenv("HOME");
817 if (!mflag
&& !SockMatch
)
820 if (sty
&& *sty
== 0)
825 if (!(nethackflag
= (getenv("NETHACKOPTIONS") != NULL
)))
827 char nethackrc
[MAXPATHLEN
];
829 if (home
&& (strlen(home
) < (MAXPATHLEN
- 20)))
831 sprintf(nethackrc
,"%s/.nethackrc", home
);
832 nethackflag
= !access(nethackrc
, F_OK
);
838 own_uid
= multi_uid
= real_uid
;
839 if (SockMatch
&& (sockp
= index(SockMatch
, '/')))
842 Panic(0, "Must run suid root for multiuser support.");
845 SockMatch
= sockp
+ 1;
849 if ((mppp
= getpwnam(multi
)) == (struct passwd
*)0)
850 Panic(0, "Cannot identify account '%s'.", multi
);
851 multi_uid
= mppp
->pw_uid
;
852 multi_home
= SaveStr(mppp
->pw_dir
);
853 if (strlen(multi_home
) > MAXPATHLEN
- 10)
854 Panic(0, "home directory path too long");
856 /* always fake multi attach mode */
864 if (SockMatch
&& *SockMatch
== 0)
866 #endif /* MULTIUSER */
868 if ((LoginName
= getlogin()) && LoginName
[0] != '\0')
870 if ((ppp
= getpwnam(LoginName
)) != (struct passwd
*) 0)
871 if ((int)ppp
->pw_uid
!= real_uid
)
872 ppp
= (struct passwd
*) 0;
876 if ((ppp
= getpwuid(real_uid
)) == 0)
878 Panic(0, "getpwuid() can't identify your account!");
881 LoginName
= ppp
->pw_name
;
883 LoginName
= SaveStr(LoginName
);
885 ppp
= getpwbyname(LoginName
, ppp
);
887 #if !defined(SOCKDIR) && defined(MULTIUSER)
888 if (multi
&& !multiattach
)
890 if (home
&& strcmp(home
, ppp
->pw_dir
))
891 Panic(0, "$HOME must match passwd entry for multiuser screens.");
895 if (home
== 0 || *home
== '\0')
897 if (strlen(LoginName
) > 20)
898 Panic(0, "LoginName too long - sorry.");
900 if (multi
&& strlen(multi
) > 20)
901 Panic(0, "Screen owner name too long - sorry.");
903 if (strlen(home
) > MAXPATHLEN
- 25)
904 Panic(0, "$HOME too long - sorry.");
907 if (!detached
&& !lsflag
&& !cmdflag
&& !(dflag
&& !mflag
&& !rflag
&& !xflag
) && !(!mflag
&& !SockMatch
&& sty
))
913 /* ttyname implies isatty */
914 if (!(attach_tty
= ttyname(0)))
915 Panic(0, "Must be connected to a terminal.");
916 if (strlen(attach_tty
) >= MAXPATHLEN
)
917 Panic(0, "TtyName too long - sorry.");
918 if (stat(attach_tty
, &st
))
919 Panic(errno
, "Cannot access '%s'", attach_tty
);
921 tty_mode
= (int)st
.st_mode
& 0777;
925 fl
= fcntl(0, F_GETFL
, 0);
926 if (fl
!= -1 && (fl
& (O_RDWR
|O_RDONLY
|O_WRONLY
)) == O_RDWR
)
931 if ((n
= secopen(attach_tty
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
932 Panic(0, "Cannot open your terminal '%s' - please check.", attach_tty
);
935 debug2("attach_tty is %s, attach_fd is %d\n", attach_tty
, attach_fd
);
937 if ((attach_term
= getenv("TERM")) == 0 || *attach_term
== 0)
938 Panic(0, "Please set a terminal type.");
939 if (strlen(attach_term
) > sizeof(D_termname
) - 1)
940 Panic(0, "$TERM too long - sorry.");
941 GetTTY(0, &attach_Mode
);
942 #ifdef DEBUGGGGGGGGGGGGGGG
943 DebugTTY(&attach_Mode
);
948 oumask
= umask(0); /* well, unsigned never fails? jw. */
950 if ((oumask
= (int)umask(0)) == -1)
951 Panic(errno
, "Cannot change umask to zero");
953 SockDir
= getenv("SCREENDIR");
956 if (strlen(SockDir
) >= MAXPATHLEN
- 1)
957 Panic(0, "Ridiculously long $SCREENDIR - try again.");
960 Panic(0, "No $SCREENDIR with multi screens, please.");
967 sprintf(SockPath
, "%s/.screen", multi_home
);
971 sprintf(SockPath
, "%s/S-%s", SockDir
, multi
);
980 sprintf(SockPath
, "%s/.screen", home
);
986 if (access(SockDir
, F_OK
))
988 debug1("SockDir '%s' missing ...\n", SockDir
);
989 if (UserContext() > 0)
991 if (mkdir(SockDir
, 0700))
995 if (UserStatus() <= 0)
996 Panic(0, "Cannot make directory '%s'.", SockDir
);
998 if (SockDir
!= SockPath
)
999 strcpy(SockPath
, SockDir
);
1005 if (lstat(SockDir
, &st
))
1007 n
= (eff_uid
== 0 && (real_uid
|| eff_gid
== real_gid
)) ? 0755 :
1008 (eff_gid
!= real_gid
) ? 0775 :
1014 if (mkdir(SockDir
, n
) == -1)
1015 Panic(errno
, "Cannot make directory '%s'", SockDir
);
1019 if (!S_ISDIR(st
.st_mode
))
1020 Panic(0, "'%s' must be a directory.", SockDir
);
1021 if (eff_uid
== 0 && real_uid
&& (int)st
.st_uid
!= eff_uid
)
1022 Panic(0, "Directory '%s' must be owned by root.", SockDir
);
1023 n
= (eff_uid
== 0 && (real_uid
|| (st
.st_mode
& 0775) != 0775)) ? 0755 :
1024 (eff_gid
== (int)st
.st_gid
&& eff_gid
!= real_gid
) ? 0775 :
1026 if (((int)st
.st_mode
& 0777) != n
)
1027 Panic(0, "Directory '%s' must have mode %03o.", SockDir
, n
);
1029 sprintf(SockPath
, "%s/S-%s", SockDir
, LoginName
);
1030 if (access(SockPath
, F_OK
))
1032 if (mkdir(SockPath
, 0700) == -1)
1033 Panic(errno
, "Cannot make directory '%s'", SockPath
);
1034 (void) chown(SockPath
, real_uid
, real_gid
);
1040 if (stat(SockPath
, &st
) == -1)
1041 Panic(errno
, "Cannot access %s", SockPath
);
1043 if (!S_ISDIR(st
.st_mode
))
1044 Panic(0, "%s is not a directory.", SockPath
);
1048 if ((int)st
.st_uid
!= multi_uid
)
1049 Panic(0, "%s is not the owner of %s.", multi
, SockPath
);
1054 if ((int)st
.st_uid
!= real_uid
)
1055 Panic(0, "You are not the owner of %s.", SockPath
);
1057 if ((st
.st_mode
& 0777) != 0700)
1058 Panic(0, "Directory %s must have mode 700.", SockPath
);
1059 if (SockMatch
&& index(SockMatch
, '/'))
1060 Panic(0, "Bad session name '%s'", SockMatch
);
1061 SockName
= SockPath
+ strlen(SockPath
) + 1;
1063 (void) umask(oumask
);
1064 debug2("SockPath: %s SockMatch: %s\n", SockPath
, SockMatch
? SockMatch
: "NULL");
1066 #if defined(SYSV) && !defined(ISC)
1067 if (uname(&utsnam
) == -1)
1068 Panic(errno
, "uname");
1069 strncpy(HostName
, utsnam
.nodename
, sizeof(utsnam
.nodename
) < MAXSTR
? sizeof(utsnam
.nodename
) : MAXSTR
- 1);
1070 HostName
[sizeof(utsnam
.nodename
) < MAXSTR
? sizeof(utsnam
.nodename
) : MAXSTR
- 1] = '\0';
1072 (void) gethostname(HostName
, MAXSTR
);
1073 HostName
[MAXSTR
- 1] = '\0';
1075 if ((ap
= index(HostName
, '.')) != NULL
)
1084 real_uid
= multi_uid
;
1090 i
= FindSocket((int *)NULL
, &fo
, &oth
, SockMatch
);
1092 exit(8 + (fo
? ((oth
|| i
) ? 2 : 1) : 0) + i
);
1094 Panic(0, "No Sockets found in %s.\n", SockPath
);
1095 Panic(0, "%d Socket%s in %s.\n", fo
, fo
> 1 ? "s" : "", SockPath
);
1098 signal(SIG_BYE
, AttacherFinit
); /* prevent races */
1101 /* attach_tty is not mandatory */
1102 if ((attach_tty
= ttyname(0)) == 0)
1104 if (strlen(attach_tty
) >= MAXPATHLEN
)
1105 Panic(0, "TtyName too long - sorry.");
1107 Panic(0, "Please specify a command.");
1112 SendCmdMessage(sty
, SockMatch
, av
);
1115 else if (rflag
|| xflag
)
1117 debug("screen -r: - is there anybody out there?\n");
1118 if (Attach(MSG_ATTACH
))
1125 Panic(0, "Can't create sessions of other users.");
1127 debug("screen -r: backend not responding -- still crying\n");
1129 else if (dflag
&& !mflag
)
1131 (void) Attach(MSG_DETACH
);
1132 Msg(0, "[%s %sdetached.]\n", SockName
, (dflag
> 1 ? "power " : ""));
1136 if (!SockMatch
&& !mflag
&& sty
)
1142 nwin_options
.args
= av
;
1143 SendCreateMsg(sty
, &nwin
);
1147 nwin_compose(&nwin_default
, &nwin_options
, &nwin_default
);
1149 if (!detached
|| dflag
!= 2)
1157 Panic(errno
, "fork");
1165 sprintf(socknamebuf
, "%d.%s", MasterPid
, SockMatch
);
1167 sprintf(socknamebuf
, "%d.%s.%s", MasterPid
, stripdev(attach_tty
), HostName
);
1168 for (ap
= socknamebuf
; *ap
; ap
++)
1172 if (strlen(socknamebuf
) > NAME_MAX
)
1173 socknamebuf
[NAME_MAX
] = 0;
1175 sprintf(SockPath
+ strlen(SockPath
), "/%s", socknamebuf
);
1185 PanicPid
= getppid();
1187 if (DefaultEsc
== -1)
1188 DefaultEsc
= Ctrl('a');
1189 if (DefaultMetaEsc
== -1)
1190 DefaultMetaEsc
= 'a';
1192 ap
= av0
+ strlen(av0
) - 1;
1195 if (!strncmp("screen", ap
, 6))
1197 strncpy(ap
, "SCREEN", 6); /* name this process "SCREEN-BACKEND" */
1209 if (dfp
&& dfp
!= stderr
)
1211 sprintf(buf
, "%s/SCREEN.%d", DEBUGDIR
, (int)getpid());
1212 if ((dfp
= fopen(buf
, "w")) == NULL
)
1215 (void) chmod(buf
, 0666);
1220 if (attach_fd
== -1)
1222 if ((n
= secopen(attach_tty
, O_RDWR
, 0)) < 0)
1223 Panic(0, "Cannot reopen '%s' - please check.", attach_tty
);
1230 freopen("/dev/null", "r", stdin
);
1231 freopen("/dev/null", "w", stdout
);
1236 freopen("/dev/null", "w", stderr
);
1237 debug("-- screen.back debug started\n");
1240 * This guarantees that the session owner is listed, even when we
1241 * start detached. From now on we should not refer to 'LoginName'
1242 * any more, use users->u_name instead.
1244 if (UserAdd(LoginName
, (char *)0, (struct acluser
**)0) < 0)
1245 Panic(0, "Could not create user info");
1248 if (MakeDisplay(LoginName
, attach_tty
, attach_term
, n
, getppid(), &attach_Mode
) == 0)
1249 Panic(0, "Could not alloc display");
1252 D_encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
: 0;
1253 debug1("D_encoding = %d\n", D_encoding
);
1259 /* user started us with -S option */
1260 sprintf(socknamebuf
, "%d.%s", (int)getpid(), SockMatch
);
1264 sprintf(socknamebuf
, "%d.%s.%s", (int)getpid(), stripdev(attach_tty
),
1267 for (ap
= socknamebuf
; *ap
; ap
++)
1271 if (strlen(socknamebuf
) > NAME_MAX
)
1273 debug2("Socketname %s truncated to %d chars\n", socknamebuf
, NAME_MAX
);
1274 socknamebuf
[NAME_MAX
] = 0;
1277 sprintf(SockPath
+ strlen(SockPath
), "/%s", socknamebuf
);
1279 ServerSocket
= MakeServerSocket();
1286 ScriptSource(script_file
);
1292 # ifdef ALLOW_SYSSCREENRC
1293 if ((ap
= getenv("SYSSCREENRC")))
1294 (void)StartRc(ap
, 0);
1297 (void)StartRc(ETCSCREENRC
, 0);
1299 (void)StartRc(RcFileName
, 0);
1303 # endif /* UTNOKEEP */
1304 # endif /* UTMPOK */
1307 if (InitTermcap(0, 0))
1309 debug("Could not init termcap - exiting\n");
1310 fcntl(D_userfd
, F_SETFL
, 0); /* Flush sets FNBLOCK */
1313 Kill(D_userpid
, SIG_BYE
);
1316 MakeDefaultCanvas();
1328 signal(SIGHUP
, SigHup
);
1329 signal(SIGINT
, FinitHandler
);
1330 signal(SIGQUIT
, FinitHandler
);
1331 signal(SIGTERM
, FinitHandler
);
1333 signal(SIGTTIN
, SIG_IGN
);
1334 signal(SIGTTOU
, SIG_IGN
);
1340 SetMode(&D_OldMode
, &D_NewMode
, D_flow
, iflag
);
1341 /* Note: SetMode must be called _before_ FinishRc. */
1342 SetTTY(D_userfd
, &D_NewMode
);
1343 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
1344 Msg(errno
, "Warning: NBLOCK fcntl failed");
1347 brktty(-1); /* just try */
1348 signal(SIGCHLD
, SigChld
);
1350 # ifdef ALLOW_SYSSCREENRC
1351 if ((ap
= getenv("SYSSCREENRC")))
1355 FinishRc(ETCSCREENRC
);
1357 FinishRc(RcFileName
);
1359 debug2("UID %d EUID %d\n", (int)getuid(), (int)geteuid());
1360 if (windows
== NULL
)
1362 debug("We open one default window, as screenrc did not specify one.\n");
1363 if (MakeWindow(&nwin
) == -1)
1365 Msg(0, "Sorry, could not find a PTY.");
1376 if (display
&& default_startup
)
1377 display_copyright();
1378 signal(SIGINT
, SigInt
);
1379 if (rflag
&& (rflag
& 1) == 0 && !quietflag
)
1381 Msg(0, "New screen...");
1385 serv_read
.type
= EV_READ
;
1386 serv_read
.fd
= ServerSocket
;
1387 serv_read
.handler
= serv_read_fn
;
1390 serv_select
.pri
= -10;
1391 serv_select
.type
= EV_ALWAYS
;
1392 serv_select
.handler
= serv_select_fn
;
1393 evenq(&serv_select
);
1395 logflushev
.type
= EV_TIMEOUT
;
1396 logflushev
.handler
= logflush_fn
;
1404 WindowDied(p
, wstat
, wstat_valid
)
1411 if (ZombieKey_destroy
&& ZombieKey_onerror
&& wstat_valid
&&
1412 WIFEXITED(wstat
) && WEXITSTATUS(wstat
) == 0)
1415 if (ZombieKey_destroy
&& !killit
)
1417 char buf
[100], *s
, reason
[100];
1421 if (WIFEXITED(wstat
))
1422 if (WEXITSTATUS(wstat
))
1423 sprintf(reason
, "terminated with exit status %d", WEXITSTATUS(wstat
));
1425 sprintf(reason
, "terminated normally");
1426 else if (WIFSIGNALED(wstat
))
1427 sprintf(reason
, "terminated with signal %d%s", WTERMSIG(wstat
),
1429 WCOREDUMP(wstat
) ? " (core file generated)" : "");
1434 sprintf(reason
, "detached from window");
1439 s
[strlen(s
) - 1] = '\0';
1440 debug3("window %d (%s) going into zombie state fd %d",
1441 p
->w_number
, p
->w_title
, p
->w_ptyfd
);
1443 if (p
->w_slot
!= (slot_t
)0 && p
->w_slot
!= (slot_t
)-1)
1446 p
->w_slot
= 0; /* "detached" */
1451 p
->w_deadpid
= p
->w_pid
;
1454 /* p->w_y = p->w_bot; */
1455 p
->w_y
= MFindUsedLine(p
, p
->w_bot
, 1);
1456 sprintf(buf
, "\n\r=== Command %s (%s) ===", reason
, s
? s
: "?");
1457 WriteString(p
, buf
, strlen(buf
));
1458 WindowChanged(p
, 'f');
1479 signal(SIGCHLD
, SigChld
);
1482 if (stat(SockPath
, &st
) == -1)
1484 debug1("SigChldHandler: Yuck! cannot stat '%s'\n", SockPath
);
1485 if (!RecoverSocket())
1487 debug("SCREEN cannot recover from corrupt Socket, bye\n");
1491 debug1("'%s' reconstructed\n", SockPath
);
1494 debug2("SigChldHandler: stat '%s' o.k. (%03o)\n", SockPath
, (int)st
.st_mode
);
1500 debug("SigChld()\n");
1508 /* Hangup all displays */
1509 while ((display
= displays
) != 0)
1515 * the backend's Interrupt handler
1516 * we cannot insert the intrc directly, as we never know
1525 debug("SigInt()\n");
1526 if (fore
&& displays
)
1528 # if defined(TERMIO) || defined(POSIX)
1529 ibuf
= displays
->d_OldMode
.tio
.c_cc
[VINTR
];
1531 ibuf
= displays
->d_OldMode
.m_tchars
.t_intrc
;
1534 write(fore
->w_ptyfd
, &ibuf
, 1);
1537 signal(SIGINT
, SigInt
);
1538 debug("SigInt() careful\n");
1539 InterruptPlease
= 1;
1547 struct display
*disp
;
1550 #if defined(SYSVSIGS) && defined(SIGHASARG)
1551 signal(sigsig
, SIG_IGN
);
1557 sprintf(buf
, "\r\n[screen caught signal %d.%s]\r\n", sigsig
,
1559 sprintf(buf
, "\r\n[screen caught a fatal signal.%s]\r\n",
1561 #if defined(SHADOWPW) && !defined(DEBUG) && !defined(DUMPSHADOW)
1563 #else /* SHADOWPW && !DEBUG */
1565 #endif /* SHADOWPW && !DEBUG */
1567 for (disp
= displays
; disp
; disp
= disp
->d_next
)
1569 fcntl(disp
->d_userfd
, F_SETFL
, 0);
1570 SetTTY(disp
->d_userfd
, &D_OldMode
);
1571 write(disp
->d_userfd
, buf
, strlen(buf
));
1572 Kill(disp
->d_userpid
, SIG_BYE
);
1574 #if defined(SHADOWPW) && !defined(DEBUG) && !defined(DUMPSHADOW)
1575 Kill(getpid(), SIGKILL
);
1577 #else /* SHADOWPW && !DEBUG */
1579 #endif /* SHADOWPW && !DEBUG */
1587 struct win
*p
, *next
;
1596 while ((pid
= waitpid(-1, &wstat
, WNOHANG
| WUNTRACED
)) > 0)
1600 * From: rouilj@sni-usa.com (John Rouillard)
1601 * note that WUNTRACED is not documented to work, but it is defined in
1602 * /usr/include/sys/wait.h, so it may work
1604 while ((pid
= wait2(&wstat
, WNOHANG
| WUNTRACED
)) > 0)
1605 # else /* USE_WAIT2 */
1606 while ((pid
= wait3(&wstat
, WNOHANG
| WUNTRACED
, (struct rusage
*) 0)) > 0)
1607 # endif /* USE_WAIT2 */
1610 while ((pid
= wait(&wstat
)) < 0)
1614 #endif /* BSDJOBS */
1616 for (p
= windows
; p
; p
= next
)
1619 if ( (p
->w_pid
&& pid
== p
->w_pid
) ||
1620 (p
->w_deadpid
&& pid
== p
->w_deadpid
) )
1622 /* child has ceased to exist */
1626 if (WIFSTOPPED(wstat
))
1628 debug3("Window %d pid %d: WIFSTOPPED (sig %d)\n", p
->w_number
, pid
, WSTOPSIG(wstat
));
1630 if (WSTOPSIG(wstat
) == SIGTTIN
)
1632 Msg(0, "Suspended (tty input)");
1637 if (WSTOPSIG(wstat
) == SIGTTOU
)
1639 Msg(0, "Suspended (tty output)");
1643 /* Try to restart process */
1644 Msg(0, "Child has been stopped, restarting.");
1645 if (killpg(pid
, SIGCONT
))
1651 WindowDied(p
, wstat
, 1);
1656 if (p
->w_pwin
&& pid
== p
->w_pwin
->p_pid
)
1658 debug2("pseudo of win Nr %d died. pid == %d\n", p
->w_number
, p
->w_pwin
->p_pid
);
1666 debug1("pid %d not found - hope that's ok\n", pid
);
1673 FinitHandler SIGDEFARG
1676 debug1("FinitHandler called, sig %d.\n", sigsig
);
1678 debug("FinitHandler called.\n");
1688 signal(SIGCHLD
, SIG_DFL
);
1689 signal(SIGHUP
, SIG_IGN
);
1690 debug1("Finit(%d);\n", i
);
1696 struct win
*p
= windows
;
1697 windows
= windows
->w_next
;
1700 if (ServerSocket
!= -1)
1702 debug1("we unlink(%s)\n", SockPath
);
1707 (void) unlink(SockPath
);
1713 for (display
= displays
; display
; display
= display
->d_next
)
1721 AddStr("[screen is terminating]\r\n");
1723 SetTTY(D_userfd
, &D_OldMode
);
1724 fcntl(D_userfd
, F_SETFL
, 0);
1726 Kill(D_userpid
, SIG_BYE
);
1729 * we _cannot_ call eexit(i) here,
1730 * instead of playing with the Socket above. Sigh.
1740 if (ServerSocket
!= -1)
1742 debug1("we unlink(%s)\n", SockPath
);
1745 (void) unlink(SockPath
);
1755 debug1("Hangup %x\n", display
);
1761 if (auto_detach
|| displays
->d_next
)
1768 * Detach now has the following modes:
1769 *D_DETACH SIG_BYE detach backend and exit attacher
1770 *D_HANGUP SIG_BYE detach backend and exit attacher
1771 *D_STOP SIG_STOP stop attacher (and detach backend)
1772 *D_REMOTE SIG_BYE remote detach -- reattach to new attacher
1773 *D_POWER SIG_POWER_BYE power detach -- attacher kills his parent
1774 *D_REMOTE_POWER SIG_POWER_BYE remote power detach -- both
1775 *D_LOCK SIG_LOCK lock the attacher
1777 * we always remove our utmp slots. (even when "lock" or "stop")
1778 * Note: Take extra care here, we may be called by interrupt!
1791 signal(SIGHUP
, SIG_IGN
);
1792 debug1("Detach(%d)\n", mode
);
1804 AddStr("[detached]\r\n");
1812 #ifdef REMOTE_DETACH
1814 AddStr("[remote detached]\r\n");
1820 AddStr("[power detached]\r\n");
1821 if (PowDetachString
)
1823 AddStr(PowDetachString
);
1826 sign
= SIG_POWER_BYE
;
1828 #ifdef REMOTE_DETACH
1829 case D_REMOTE_POWER
:
1830 AddStr("[remote power detached]\r\n");
1831 if (PowDetachString
)
1833 AddStr(PowDetachString
);
1836 sign
= SIG_POWER_BYE
;
1843 /* tell attacher to lock terminal with a lockprg. */
1847 if (displays
->d_next
== 0)
1849 for (p
= windows
; p
; p
= p
->w_next
)
1851 if (p
->w_slot
!= (slot_t
) -1 && !(p
->w_lflag
& 2))
1855 * Set the slot to 0 to get the window
1858 p
->w_slot
= (slot_t
) 0;
1862 if (mode
!= D_HANGUP
)
1865 if (displays
->d_next
== 0 && console_window
)
1867 if (TtyGrabConsole(console_window
->w_ptyfd
, 0, "detach"))
1869 debug("could not release console - killing window\n");
1870 KillWindow(console_window
);
1871 display
= displays
; /* restore display */
1877 ReleaseAutoWritelock(display
, D_fore
);
1879 D_user
->u_detachwin
= D_fore
->w_number
;
1880 D_user
->u_detachotherwin
= D_other
? D_other
->w_number
: -1;
1882 AutosaveLayout(D_layout
);
1883 layout_last
= D_layout
;
1884 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1886 p
= Layer2Window(cv
->c_layer
);
1887 SetCanvasWindow(cv
, 0);
1889 WindowChanged(p
, 'u');
1893 debug2("display: %#x displays: %#x\n", (unsigned int)display
, (unsigned int)displays
);
1896 /* Flag detached-ness */
1899 * tell father what to do. We do that after we
1900 * freed the tty, thus getty feels more comfortable on hpux
1901 * if it was a power detach.
1904 debug2("Detach: Signal %d to Attacher(%d)!\n", sign
, pid
);
1905 debug("Detach returns, we are successfully detached.\n");
1906 signal(SIGHUP
, SigHup
);
1916 return strncmp(e
, s
, l
) == 0 && e
[l
] == '=';
1922 register char **op
, **np
;
1923 static char stybuf
[MAXSTR
];
1925 for (op
= environ
; *op
; ++op
)
1928 free((char *)NewEnv
);
1929 NewEnv
= np
= (char **) malloc((unsigned) (op
- environ
+ 7 + 1) * sizeof(char **));
1932 sprintf(stybuf
, "STY=%s", strlen(SockName
) <= MAXSTR
- 5 ? SockName
: "?");
1933 *np
++ = stybuf
; /* NewEnv[0] */
1934 *np
++ = Term
; /* NewEnv[1] */
1935 np
++; /* room for SHELL */
1937 np
+= 2; /* room for TERMCAP and WINDOW */
1939 np
+= 4; /* room for TERMCAP WINDOW LINES COLUMNS */
1942 for (op
= environ
; *op
; ++op
)
1944 if (!IsSymbol(*op
, "TERM") && !IsSymbol(*op
, "TERMCAP")
1945 && !IsSymbol(*op
, "STY") && !IsSymbol(*op
, "WINDOW")
1946 && !IsSymbol(*op
, "SCREENCAP") && !IsSymbol(*op
, "SHELL")
1947 && !IsSymbol(*op
, "LINES") && !IsSymbol(*op
, "COLUMNS")
1956 #if defined(USEVARARGS) && defined(__STDC__)
1957 Msg(int err
, char *fmt
, VA_DOTS
)
1959 Msg(err
, fmt
, VA_DOTS
)
1966 char buf
[MAXPATHLEN
*2];
1971 (void)vsnprintf(p
, sizeof(buf
) - 100, fmt
, VA_ARGS(ap
));
1978 strncpy(p
, strerror(err
), buf
+ sizeof(buf
) - p
- 1);
1979 buf
[sizeof(buf
) - 1] = 0;
1981 debug2("Msg('%s') (%#x);\n", buf
, (unsigned int)display
);
1983 if (display
&& displays
)
1987 for (display
= displays
; display
; display
= display
->d_next
)
1992 /* no displays but a display - must have forked.
1993 * send message to backend!
1995 char *tty
= D_usertty
;
1996 struct display
*olddisplay
= display
;
1997 display
= 0; /* only send once */
1998 SendErrorMsg(tty
, buf
);
1999 display
= olddisplay
;
2002 printf("%s\r\n", buf
);
2006 * Call FinitTerm for all displays, write a message to each and call eexit();
2010 #if defined(USEVARARGS) && defined(__STDC__)
2011 Panic(int err
, char *fmt
, VA_DOTS
)
2013 Panic(err
, fmt
, VA_DOTS
)
2020 char buf
[MAXPATHLEN
*2];
2025 (void)vsnprintf(p
, sizeof(buf
) - 100, fmt
, VA_ARGS(ap
));
2032 strncpy(p
, strerror(err
), buf
+ sizeof(buf
) - p
- 1);
2033 buf
[sizeof(buf
) - 1] = 0;
2035 debug3("Panic('%s'); display=%x displays=%x\n", buf
, display
, displays
);
2036 if (displays
== 0 && display
== 0)
2038 printf("%s\r\n", buf
);
2040 Kill(PanicPid
, SIG_BYE
);
2042 else if (displays
== 0)
2044 /* no displays but a display - must have forked.
2045 * send message to backend!
2047 char *tty
= D_usertty
;
2049 SendErrorMsg(tty
, buf
);
2054 for (display
= displays
; display
; display
= display
->d_next
)
2063 SetTTY(D_userfd
, &D_OldMode
);
2064 fcntl(D_userfd
, F_SETFL
, 0);
2065 write(D_userfd
, buf
, strlen(buf
));
2066 write(D_userfd
, "\n", 1);
2069 Kill(D_userpid
, SIG_BYE
);
2072 if (tty_oldmode
>= 0)
2075 if (setuid(own_uid
))
2076 xseteuid(own_uid
); /* may be a loop. sigh. */
2080 debug1("Panic: changing back modes from %s\n", attach_tty
);
2081 chmod(attach_tty
, tty_oldmode
);
2089 * '^' is allowed as an escape mechanism for control characters. jw.
2091 * Added time insertion using ideas/code from /\ndy Jones
2092 * (andy@lingua.cltr.uq.OZ.AU) - thanks a lot!
2097 static const char days
[] = "SunMonTueWedThuFriSat";
2098 static const char months
[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
2101 static char winmsg_buf
[MAXSTR
];
2102 #define MAX_WINMSG_REND 16 /* rendition changes */
2103 static int winmsg_rend
[MAX_WINMSG_REND
];
2104 static int winmsg_rendpos
[MAX_WINMSG_REND
];
2105 static int winmsg_numrend
;
2108 pad_expand(buf
, p
, numpad
, padlen
)
2117 padlen
= padlen
- (p
- buf
); /* space for rent */
2120 pn2
= pn
= p
+ padlen
;
2124 if (r
&& p
- buf
== winmsg_rendpos
[r
- 1])
2126 winmsg_rendpos
[--r
] = pn
- buf
;
2133 i
= numpad
> 0 ? (padlen
+ numpad
- 1) / numpad
: 0;
2144 struct backtick
*next
;
2149 char result
[MAXSTR
];
2156 struct backtick
*backticks
;
2160 struct backtick
*bt
;
2165 for (p
= q
= bt
->result
; (c
= (unsigned char)*p
++) != 0;)
2169 if (c
>= ' ' || c
== '\005')
2176 backtick_fn(ev
, data
)
2180 struct backtick
*bt
;
2183 bt
= (struct backtick
*)data
;
2184 debug1("backtick_fn for #%d\n", bt
->num
);
2186 l
= read(ev
->fd
, bt
->buf
+ i
, MAXSTR
- i
);
2189 debug1("EOF on backtick #%d\n", bt
->num
);
2195 debug1("read %d bytes\n", l
);
2197 for (j
= 0; j
< l
; j
++)
2198 if (bt
->buf
[i
- j
- 1] == '\n')
2202 for (k
= i
- j
- 2; k
>= 0; k
--)
2203 if (bt
->buf
[k
] == '\n')
2206 bcopy(bt
->buf
+ k
, bt
->result
, i
- j
- k
);
2207 bt
->result
[i
- j
- k
- 1] = 0;
2208 backtick_filter(bt
);
2209 WindowChanged(0, '`');
2211 if (j
== l
&& i
== MAXSTR
)
2219 bcopy(bt
->buf
+ i
- j
, bt
->buf
, j
);
2226 setbacktick(num
, lifespan
, tick
, cmdv
)
2232 struct backtick
**btp
, *bt
;
2235 debug1("setbacktick called for backtick #%d\n", num
);
2236 for (btp
= &backticks
; (bt
= *btp
) != 0; btp
= &bt
->next
)
2243 for (v
= bt
->cmdv
; *v
; v
++)
2260 bt
= (struct backtick
*)malloc(sizeof *bt
);
2266 bzero(bt
, sizeof(*bt
));
2272 bt
->lifespan
= lifespan
;
2279 if (bt
->tick
== 0 && bt
->lifespan
== 0)
2281 debug("setbacktick: continuous mode\n");
2282 bt
->buf
= (char *)malloc(MAXSTR
);
2286 setbacktick(num
, 0, 0, (char **)0);
2289 bt
->ev
.type
= EV_READ
;
2290 bt
->ev
.fd
= readpipe(bt
->cmdv
);
2291 bt
->ev
.handler
= backtick_fn
;
2292 bt
->ev
.data
= (char *)bt
;
2299 runbacktick(bt
, tickp
, now
)
2300 struct backtick
*bt
;
2307 debug1("runbacktick called for backtick #%d\n", bt
->num
);
2308 if (bt
->tick
&& (!*tickp
|| bt
->tick
< *tickp
))
2310 if ((bt
->lifespan
== 0 && bt
->tick
== 0) || now
< bt
->bestbefore
)
2312 debug1("returning old result (%d)\n", bt
->lifespan
);
2315 f
= readpipe(bt
->cmdv
);
2319 while ((l
= read(f
, bt
->result
+ i
, sizeof(bt
->result
) - i
)) > 0)
2321 debug1("runbacktick: read %d bytes\n", l
);
2323 for (j
= 1; j
< l
; j
++)
2324 if (bt
->result
[i
- j
- 1] == '\n')
2326 if (j
== l
&& i
== sizeof(bt
->result
))
2328 j
= sizeof(bt
->result
) / 2;
2333 bcopy(bt
->result
+ i
- j
, bt
->result
, j
);
2338 bt
->result
[sizeof(bt
->result
) - 1] = '\n';
2339 if (i
&& bt
->result
[i
- 1] == '\n')
2341 debug1("runbacktick: finished, %d bytes\n", i
);
2343 backtick_filter(bt
);
2345 bt
->bestbefore
= now2
+ bt
->lifespan
;
2350 MakeWinMsgEv(str
, win
, esc
, padlen
, ev
, rec
)
2360 register char *p
= winmsg_buf
;
2370 int qmflag
= 0, omflag
= 0, qmnumrend
= 0;
2377 struct backtick
*bt
;
2379 if (winmsg_numrend
>= 0)
2382 winmsg_numrend
= -winmsg_numrend
;
2387 gettimeofday(&now
, NULL
);
2388 for (; *s
&& (l
= winmsg_buf
+ MAXSTR
- 1 - p
) > 0; s
++, p
++)
2394 if (*s
!= '^' && *s
>= 64)
2419 if (*++s
== esc
) /* double escape ? */
2421 if ((plusflg
= *s
== '+') != 0)
2423 if ((minusflg
= *s
== '-') != 0)
2425 if ((zeroflg
= *s
== '0') != 0)
2428 while(*s
>= '0' && *s
<= '9')
2429 num
= num
* 10 + (*s
++ - '0');
2430 if ((longflg
= *s
== 'L') != 0)
2438 if ((!qmflag
&& !omflag
) || omflag
== 1)
2441 if (qmnumrend
< winmsg_numrend
)
2442 winmsg_numrend
= qmnumrend
;
2448 qmnumrend
= winmsg_numrend
;
2449 qmflag
= omflag
= 0;
2455 if (qmflag
&& omflag
!= 1)
2459 qmnumrend
= winmsg_numrend
;
2464 if (qmnumrend
< winmsg_numrend
)
2465 winmsg_numrend
= qmnumrend
;
2469 case 'd': case 'D': case 'm': case 'M': case 'y': case 'Y':
2470 case 'a': case 'A': case 's': case 'c': case 'C':
2475 time_t nowsec
= now
.tv_sec
;
2476 tm
= localtime(&nowsec
);
2479 if (!tick
|| tick
> 3600)
2484 sprintf(p
, "%02d", tm
->tm_mday
% 100);
2488 strftime(p
, l
, (longflg
? "%A" : "%a"), tm
);
2490 sprintf(p
, "%3.3s", days
+ 3 * tm
->tm_wday
);
2494 sprintf(p
, "%02d", tm
->tm_mon
+ 1);
2498 strftime(p
, l
, (longflg
? "%B" : "%b"), tm
);
2500 sprintf(p
, "%3.3s", months
+ 3 * tm
->tm_mon
);
2504 sprintf(p
, "%02d", tm
->tm_year
% 100);
2507 sprintf(p
, "%04d", tm
->tm_year
+ 1900);
2510 sprintf(p
, tm
->tm_hour
>= 12 ? "pm" : "am");
2513 sprintf(p
, tm
->tm_hour
>= 12 ? "PM" : "AM");
2516 sprintf(p
, "%02d", tm
->tm_sec
);
2520 sprintf(p
, zeroflg
? "%02d:%02d" : "%2d:%02d", tm
->tm_hour
, tm
->tm_min
);
2521 if (!tick
|| tick
> 60)
2525 sprintf(p
, zeroflg
? "%02d:%02d" : "%2d:%02d", (tm
->tm_hour
+ 11) % 12 + 1, tm
->tm_min
);
2526 if (!tick
|| tick
> 60)
2546 if (!tick
|| tick
> 60)
2555 if (rec
>= 10 || (*s
== 'h' && (win
== 0 || win
->w_hstatus
== 0 || *win
->w_hstatus
== 0)))
2562 for (bt
= backticks
; bt
; bt
= bt
->next
)
2572 char savebuf
[sizeof(winmsg_buf
)];
2574 int oldnumrend
= winmsg_numrend
;
2577 strcpy(savebuf
, winmsg_buf
);
2578 winmsg_numrend
= -winmsg_numrend
;
2579 MakeWinMsgEv(*s
== 'h' ? win
->w_hstatus
: runbacktick(bt
, &oldtick
, now
.tv_sec
), win
, '\005', 0, (struct event
*)0, rec
+ 1);
2580 debug2("oldtick=%d tick=%d\n", oldtick
, tick
);
2581 if (!tick
|| oldtick
< tick
)
2583 if ((int)strlen(winmsg_buf
) < l
)
2584 strcat(savebuf
, winmsg_buf
);
2585 strcpy(winmsg_buf
, savebuf
);
2586 while (oldnumrend
< winmsg_numrend
)
2587 winmsg_rendpos
[oldnumrend
++] += p
- winmsg_buf
;
2596 struct win
*oldfore
= 0;
2604 ss
= AddWindows(p
, l
- 1, (*s
== 'w' ? 0 : 1) | (longflg
? 0 : 2) | (plusflg
? 4 : 0), win
? win
->w_number
: -1);
2617 AddOtherUsers(p
, l
- 1, win
);
2625 AddWindowFlags(p
, l
- 1, win
);
2632 if (win
&& (int)strlen(win
->w_title
) < l
)
2634 strcpy(p
, win
->w_title
);
2644 for (i
= 0; i
< 127; i
++)
2645 if (s
[i
] && s
[i
] != '}')
2649 if (s
[i
] == '}' && winmsg_numrend
< MAX_WINMSG_REND
)
2653 debug1("MakeWinMsg attrcolor %s\n", rbuf
);
2654 if (i
!= 1 || rbuf
[0] != '-')
2655 r
= ParseAttrColor(rbuf
, (char *)0, 0);
2656 if (r
!= -1 || (i
== 1 && rbuf
[0] == '-'))
2658 winmsg_rend
[winmsg_numrend
] = r
;
2659 winmsg_rendpos
[winmsg_numrend
] = p
- winmsg_buf
;
2669 if ((int)strlen(HostName
) < l
)
2671 strcpy(p
, HostName
);
2680 if (display
&& ((ev
&& ev
== &D_forecv
->c_captev
) || (!ev
&& win
&& win
== D_fore
)))
2681 minusflg
= !minusflg
;
2686 truncpos
= p
- winmsg_buf
;
2687 truncper
= num
> 100 ? 100 : num
;
2688 trunclong
= longflg
;
2694 if (num
|| zeroflg
|| plusflg
|| longflg
|| (*s
!= '='))
2696 /* expand all pads */
2699 num
= (plusflg
? lastpad
: padlen
) - num
;
2700 if (!plusflg
&& padlen
== 0)
2701 num
= p
- winmsg_buf
;
2706 if (*s
!= '=' && num
== 0 && !plusflg
)
2711 num
= p
- winmsg_buf
;
2713 num
= (padlen
- (plusflg
? lastpad
: 0)) * num
/ 100;
2719 if (num
> MAXSTR
- 1)
2722 p
= pad_expand(winmsg_buf
, p
, numpad
, num
);
2724 if (p
- winmsg_buf
> num
&& !longflg
)
2733 trunc
= lastpad
+ truncper
* (num
- lastpad
) / 100;
2736 if (trunc
< lastpad
)
2738 left
= truncpos
- trunc
;
2739 if (left
> p
- winmsg_buf
- num
)
2740 left
= p
- winmsg_buf
- num
;
2741 debug1("lastpad = %d, ", lastpad
);
2742 debug3("truncpos = %d, trunc = %d, left = %d\n", truncpos
, trunc
, left
);
2745 if (left
+ lastpad
> p
- winmsg_buf
)
2746 left
= p
- winmsg_buf
- lastpad
;
2747 if (p
- winmsg_buf
- lastpad
- left
> 0)
2748 bcopy(winmsg_buf
+ lastpad
+ left
, winmsg_buf
+ lastpad
, p
- winmsg_buf
- lastpad
- left
);
2751 while (r
&& winmsg_rendpos
[r
- 1] > lastpad
)
2754 winmsg_rendpos
[r
] -= left
;
2755 if (winmsg_rendpos
[r
] < lastpad
)
2756 winmsg_rendpos
[r
] = lastpad
;
2760 if (p
- winmsg_buf
> lastpad
)
2761 winmsg_buf
[lastpad
] = '.';
2762 if (p
- winmsg_buf
> lastpad
+ 1)
2763 winmsg_buf
[lastpad
+ 1] = '.';
2764 if (p
- winmsg_buf
> lastpad
+ 2)
2765 winmsg_buf
[lastpad
+ 2] = '.';
2768 if (p
- winmsg_buf
> num
)
2770 p
= winmsg_buf
+ num
;
2773 if (num
- 1 >= lastpad
)
2775 if (num
- 2 >= lastpad
)
2777 if (num
- 3 >= lastpad
)
2781 while (r
&& winmsg_rendpos
[r
- 1] > num
)
2782 winmsg_rendpos
[--r
] = num
;
2786 if (lastpad
> p
- winmsg_buf
)
2787 lastpad
= p
- winmsg_buf
;
2788 debug1("lastpad now %d\n", lastpad
);
2792 while (p
- winmsg_buf
< num
)
2794 lastpad
= p
- winmsg_buf
;
2797 debug1("lastpad2 now %d\n", lastpad
);
2803 *p
= 127; /* internal pad representation */
2817 sprintf(p
, "%*s", num
, num
> 1 ? "--" : "-");
2819 sprintf(p
, "%*d", num
, win
->w_number
);
2826 if (qmpos
&& !qmflag
)
2831 if (padlen
> MAXSTR
- 1)
2832 padlen
= MAXSTR
- 1;
2833 p
= pad_expand(winmsg_buf
, p
, numpad
, padlen
);
2837 evdeq(ev
); /* just in case */
2838 ev
->timeout
.tv_sec
= 0;
2839 ev
->timeout
.tv_usec
= 0;
2843 now
.tv_usec
= 100000;
2847 now
.tv_sec
+= tick
- (now
.tv_sec
% tick
);
2849 debug2("NEW timeout %d %d\n", ev
->timeout
.tv_sec
, tick
);
2855 MakeWinMsg(s
, win
, esc
)
2860 return MakeWinMsgEv(s
, win
, esc
, 0, (struct event
*)0, 0);
2864 PutWinMsg(s
, start
, max
)
2870 struct mchar rendstack
[MAX_WINMSG_REND
];
2873 if (s
!= winmsg_buf
)
2875 /* sorry, no fancy coloring available */
2876 debug1("PutWinMsg %s plain\n", s
);
2889 debug2("PutWinMsg %s start attr %x\n", s
, rend
.attr
);
2890 for (i
= 0; i
< winmsg_numrend
&& max
> 0; i
++)
2892 if (p
> winmsg_rendpos
[i
] || winmsg_rendpos
[i
] > l
)
2894 if (p
< winmsg_rendpos
[i
])
2896 n
= winmsg_rendpos
[i
] - p
;
2913 rend
= rendstack
[--rendstackn
];
2917 rendstack
[rendstackn
++] = rend
;
2918 ApplyAttrColor(r
, &rend
);
2920 SetRendition(&rend
);
2948 if ((j
= open("/dev/null", 0)) >= 0)
2955 while (dup(++i
) < 0 && errno
!= EBADF
)
2957 debug1(" [%d]\n", i
);
2970 serv_read_fn(ev
, data
)
2974 debug("Knock - knock!\n");
2979 serv_select_fn(ev
, data
)
2985 debug("serv_select_fn called\n");
2986 /* XXX: messages?? */
2991 if (InterruptPlease
)
2993 debug("Backend received interrupt\n");
2994 /* This approach is rather questionable in a multi-display
2996 if (fore
&& displays
)
2998 #if defined(TERMIO) || defined(POSIX)
2999 char ibuf
= displays
->d_OldMode
.tio
.c_cc
[VINTR
];
3001 char ibuf
= displays
->d_OldMode
.m_tchars
.t_intrc
;
3004 write(W_UWP(fore
) ? fore
->w_pwin
->p_ptyfd
: fore
->w_ptyfd
,
3006 debug1("Backend wrote interrupt to %d", fore
->w_number
);
3007 debug1("%s\n", W_UWP(fore
) ? " (pseudowin)" : "");
3009 write(fore
->w_ptyfd
, &ibuf
, 1);
3010 debug1("Backend wrote interrupt to %d\n", fore
->w_number
);
3013 InterruptPlease
= 0;
3016 for (p
= windows
; p
; p
= p
->w_next
)
3018 if (p
->w_bell
== BELL_FOUND
|| p
->w_bell
== BELL_VISUAL
)
3021 int visual
= p
->w_bell
== BELL_VISUAL
|| visual_bell
;
3022 p
->w_bell
= BELL_ON
;
3023 for (display
= displays
; display
; display
= display
->d_next
)
3025 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3026 if (cv
->c_layer
->l_bottom
== &p
->w_layer
)
3030 p
->w_bell
= BELL_DONE
;
3031 Msg(0, "%s", MakeWinMsg(BellString
, p
, '%'));
3033 else if (visual
&& !D_VB
&& (!D_status
|| !D_status_bell
))
3035 Msg(0, "%s", VisualBellString
);
3039 debug1("using vbell timeout %d\n", VBellWait
);
3040 SetTimeout(&D_statusev
, VBellWait
);
3044 /* don't annoy the user with two messages */
3045 if (p
->w_monitor
== MON_FOUND
)
3046 p
->w_monitor
= MON_DONE
;
3047 WindowChanged(p
, 'f');
3049 if (p
->w_monitor
== MON_FOUND
)
3052 p
->w_monitor
= MON_ON
;
3053 for (display
= displays
; display
; display
= display
->d_next
)
3055 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3056 if (cv
->c_layer
->l_bottom
== &p
->w_layer
)
3059 continue; /* user already sees window */
3061 if (!(ACLBYTE(p
->w_mon_notify
, D_user
->u_id
) & ACLBIT(D_user
->u_id
)))
3062 continue; /* user doesn't care */
3064 Msg(0, "%s", MakeWinMsg(ActivityString
, p
, '%'));
3065 p
->w_monitor
= MON_DONE
;
3067 WindowChanged(p
, 'f');
3071 for (display
= displays
; display
; display
= display
->d_next
)
3074 if (D_status
== STATUS_ON_WIN
)
3076 /* XXX: should use display functions! */
3077 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3081 /* normalize window, see resize.c */
3082 lx
= cv
->c_layer
->l_x
;
3083 ly
= cv
->c_layer
->l_y
;
3084 if (lx
== cv
->c_layer
->l_width
)
3086 if (ly
+ cv
->c_yoff
< cv
->c_ys
)
3088 int i
, n
= cv
->c_ys
- (ly
+ cv
->c_yoff
);
3089 cv
->c_yoff
= cv
->c_ys
- ly
;
3090 RethinkViewportOffsets(cv
);
3091 if (n
> cv
->c_layer
->l_height
)
3092 n
= cv
->c_layer
->l_height
;
3094 LScrollV(flayer
, -n
, 0, flayer
->l_height
- 1, 0);
3095 LayRedisplayLine(-1, -1, -1, 1);
3096 for (i
= 0; i
< n
; i
++)
3097 LayRedisplayLine(i
, 0, flayer
->l_width
- 1, 1);
3098 if (cv
== cv
->c_display
->d_forecv
)
3102 else if (ly
+ cv
->c_yoff
> cv
->c_ye
)
3104 int i
, n
= ly
+ cv
->c_yoff
- cv
->c_ye
;
3105 cv
->c_yoff
= cv
->c_ye
- ly
;
3106 RethinkViewportOffsets(cv
);
3107 if (n
> cv
->c_layer
->l_height
)
3108 n
= cv
->c_layer
->l_height
;
3110 LScrollV(flayer
, n
, 0, cv
->c_layer
->l_height
- 1, 0);
3111 LayRedisplayLine(-1, -1, -1, 1);
3112 for (i
= 0; i
< n
; i
++)
3113 LayRedisplayLine(i
+ flayer
->l_height
- n
, 0, flayer
->l_width
- 1, 1);
3114 if (cv
== cv
->c_display
->d_forecv
)
3118 if (lx
+ cv
->c_xoff
< cv
->c_xs
)
3120 int i
, n
= cv
->c_xs
- (lx
+ cv
->c_xoff
);
3121 if (n
< (cv
->c_xe
- cv
->c_xs
+ 1) / 2)
3122 n
= (cv
->c_xe
- cv
->c_xs
+ 1) / 2;
3123 if (cv
->c_xoff
+ n
> cv
->c_xs
)
3124 n
= cv
->c_xs
- cv
->c_xoff
;
3126 RethinkViewportOffsets(cv
);
3127 if (n
> cv
->c_layer
->l_width
)
3128 n
= cv
->c_layer
->l_width
;
3130 LayRedisplayLine(-1, -1, -1, 1);
3131 for (i
= 0; i
< flayer
->l_height
; i
++)
3133 LScrollH(flayer
, -n
, i
, 0, flayer
->l_width
- 1, 0, 0);
3134 LayRedisplayLine(i
, 0, n
- 1, 1);
3136 if (cv
== cv
->c_display
->d_forecv
)
3140 else if (lx
+ cv
->c_xoff
> cv
->c_xe
)
3142 int i
, n
= lx
+ cv
->c_xoff
- cv
->c_xe
;
3143 if (n
< (cv
->c_xe
- cv
->c_xs
+ 1) / 2)
3144 n
= (cv
->c_xe
- cv
->c_xs
+ 1) / 2;
3145 if (cv
->c_xoff
- n
+ cv
->c_layer
->l_width
- 1 < cv
->c_xe
)
3146 n
= cv
->c_xoff
+ cv
->c_layer
->l_width
- 1 - cv
->c_xe
;
3148 RethinkViewportOffsets(cv
);
3149 if (n
> cv
->c_layer
->l_width
)
3150 n
= cv
->c_layer
->l_width
;
3152 LayRedisplayLine(-1, -1, -1, 1);
3153 for (i
= 0; i
< flayer
->l_height
; i
++)
3155 LScrollH(flayer
, n
, i
, 0, flayer
->l_width
- 1, 0, 0);
3156 LayRedisplayLine(i
, flayer
->l_width
- n
, flayer
->l_width
- 1, 1);
3158 if (cv
== cv
->c_display
->d_forecv
)
3165 for (display
= displays
; display
; display
= display
->d_next
)
3167 if (D_status
== STATUS_ON_WIN
|| D_cvlist
== 0 || D_cvlist
->c_next
== 0)
3169 debug1("serv_select_fn: Restore on cv %#x\n", (int)D_forecv
);
3170 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
3175 logflush_fn(ev
, data
)
3183 if (!islogfile(NULL
))
3184 return; /* no more logfiles */
3186 n
= log_flush
? log_flush
: (logtstamp_after
+ 4) / 5;
3189 SetTimeout(ev
, n
* 1000);
3190 evenq(ev
); /* re-enqueue ourself */
3194 /* write fancy time-stamp */
3195 for (p
= windows
; p
; p
= p
->w_next
)
3199 p
->w_logsilence
+= n
;
3200 if (p
->w_logsilence
< logtstamp_after
)
3202 if (p
->w_logsilence
- n
>= logtstamp_after
)
3204 buf
= MakeWinMsg(logtstamp_string
, p
, '%');
3205 logfwrite(p
->w_log
, buf
, strlen(buf
));
3210 * Interprets ^?, ^@ and other ^-control-char notation.
3211 * Interprets \ddd octal notation
3213 * The result is placed in *cp, p is advanced behind the parsed expression and
3222 if (*p
== '^' && p
[1])
3232 else if (*p
== '\\' && *++p
<= '7' && *p
>= '0')
3236 *cp
= *cp
* 8 + *p
- '0';
3237 while (*++p
<= '7' && *p
>= '0');
3248 unsigned char buf
[2];
3251 SetEscape((struct acluser
*)0, -1, -1);
3254 if ((p
= ParseChar(p
, (char *)buf
)) == NULL
||
3255 (p
= ParseChar(p
, (char *)buf
+1)) == NULL
|| *p
)
3257 SetEscape((struct acluser
*)0, buf
[0], buf
[1]);