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;
363 #if (defined(AUX) || defined(_AUX_SOURCE)) && defined(POSIX)
364 setcompat(COMPAT_POSIX
|COMPAT_BSDPROT
); /* turn on seteuid support */
366 #if defined(sun) && defined(SVR4)
368 /* Solaris' login blocks SIGHUP! This is _very bad_ */
371 sigprocmask(SIG_SETMASK
, &sset
, 0);
376 * First, close all unused descriptors
377 * (otherwise, we might have problems with the select() call)
383 sprintf(version
, "%d.%.2d.%.2d%s (%s) %s", REV
, VERS
,
384 PATCHLEVEL
, STATE
, ORIGIN
, DATE
);
385 nversion
= REV
* 10000 + VERS
* 100 + PATCHLEVEL
;
386 debug2("-- screen debug started %s (%s)\n", *av
, version
);
400 debug("NAMEDPIPE\n");
402 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
403 debug("Window size changing enabled\n");
433 debug1("NAME_MAX = %d\n", NAME_MAX
);
436 BellString
= SaveStr("Bell in window %n");
437 VisualBellString
= SaveStr(" Wuff, Wuff!! ");
438 ActivityString
= SaveStr("Activity in window %n");
439 screenlogfile
= SaveStr("screenlog.%n");
440 logtstamp_string
= SaveStr("-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n");
441 hstatusstring
= SaveStr("%h");
442 captionstring
= SaveStr("%3n %t");
443 timestring
= SaveStr("%c:%s %M %d %H%? %l%?");
444 wlisttit
= SaveStr("Num Name%=Flags");
445 wliststr
= SaveStr("%3n %t%=%f");
447 BufferFile
= SaveStr(DEFAULT_BUFFERFILE
);
453 default_startup
= (ac
> 1) ? 0 : 1;
455 VBellWait
= VBELLWAIT
* 1000;
456 MsgWait
= MSGWAIT
* 1000;
457 MsgMinWait
= MSGMINWAIT
* 1000;
458 SilenceWait
= SILENCEWAIT
;
463 zmodem_sendcmd
= SaveStr("!!! sz -vv -b ");
464 zmodem_recvcmd
= SaveStr("!!! rz -vv -b -E");
468 CompileKeys((char *)0, 0, mark_key_tab
);
472 screenencodings
= SaveStr(SCREENENCODINGS
);
475 nwin_options
= nwin_undef
;
476 strcpy(screenterm
, "screen");
478 logreopen_register(lf_secreopen
);
481 /* if this is a login screen, assume -RR */
490 ShellProg
= SaveStr(DefaultShell
); /* to prevent nasty circles */
495 if (--ac
> 0 && *ap
== '-')
497 if (ap
[1] == '-' && ap
[2] == 0)
503 if (ap
[1] == '-' && !strcmp(ap
, "--version"))
504 Panic(0, "Screen version %s", version
);
505 if (ap
[1] == '-' && !strcmp(ap
, "--help"))
506 exit_with_usage(myname
, NULL
, NULL
);
507 while (ap
&& *ap
&& *++ap
)
512 nwin_options
.aflag
= 1;
517 case 'p': /* preselect */
523 exit_with_usage(myname
, "Specify a window to preselect with -p", NULL
);
530 bd
.bd_start_braille
= 1;
539 exit_with_usage(myname
, "Specify an alternate rc-filename with -c", NULL
);
548 exit_with_usage(myname
, "Specify command characters with -e", NULL
);
552 Panic(0, "Two characters are required with -e option, not '%s'.", ap
);
561 nwin_options
.flowflag
= FLOW_NOW
* 0;
568 nwin_options
.flowflag
= FLOW_NOW
* 1;
571 nwin_options
.flowflag
= FLOW_AUTOFLAG
;
574 exit_with_usage(myname
, "Unknown flow option -%s", --ap
);
579 exit_with_usage(myname
, NULL
, NULL
);
580 nwin_options
.histheight
= atoi(*++av
);
581 if (nwin_options
.histheight
< 0)
582 exit_with_usage(myname
, "-h: %s: negative scrollback size?", *av
);
587 case 't': /* title, the former AkA == -k */
589 exit_with_usage(myname
, "Specify a new window-name with -t", NULL
);
590 nwin_options
.aka
= *++av
;
598 nwin_options
.lflag
= 0;
605 nwin_options
.lflag
= 1;
608 nwin_options
.lflag
= 3;
611 case 'i': /* -list */
613 if (ac
> 1 && !SockMatch
)
621 exit_with_usage(myname
, "%s: Unknown suboption to -l", --ap
);
627 if (ac
> 1 && !SockMatch
)
634 nwin_options
.Lflag
= 1;
639 case 'O': /* to be (or not to be?) deleted. jw. */
644 exit_with_usage(myname
, "Specify terminal-type with -T", NULL
);
645 if (strlen(*++av
) < 20)
646 strcpy(screenterm
, *av
);
648 Panic(0, "-T: terminal name too long. (max. 20 char)");
649 nwin_options
.term
= screenterm
;
659 if (ac
> 1 && *av
[1] != '-' && !SockMatch
)
663 debug2("rflag=%d, SockMatch=%s\n", dflag
, SockMatch
);
671 rflag
+= (*ap
== 'R') ? 2 : 1;
682 if (*av
[1] != '-' && !SockMatch
)
686 debug2("dflag=%d, SockMatch=%s\n", dflag
, SockMatch
);
693 exit_with_usage(myname
, "Specify shell with -s", NULL
);
696 ShellProg
= SaveStr(*++av
);
697 debug1("ShellProg: '%s'\n", ShellProg
);
703 exit_with_usage(myname
, "Specify session-name with -S", NULL
);
707 exit_with_usage(myname
, "Empty session-name?", NULL
);
713 Panic(0, "Screen version %s", version
);
717 nwin_options
.encoding
= nwin_options
.encoding
== -1 ? UTF8
: 0;
722 exit_with_usage(myname
, "Specify lua script file with -u", NULL
);
725 script_file
= SaveStr(*++av
);
728 exit_with_usage(myname
, "Unknown option %s", --ap
);
740 if (eff_uid
!= real_uid
)
742 /* if running with s-bit, we must install a special signal
743 * handler routine that resets the s-bit, so that we get a
746 #ifdef SIGBUS /* OOPS, linux has no bus errors! */
747 signal(SIGBUS
, CoreDump
);
749 signal(SIGSEGV
, CoreDump
);
753 setlocale(LC_ALL
, "");
756 if (nwin_options
.encoding
== -1)
758 /* ask locale if we should start in UTF-8 mode */
759 # ifdef HAVE_NL_LANGINFO
761 setlocale(LC_CTYPE
, "");
763 nwin_options
.encoding
= FindEncoding(nl_langinfo(CODESET
));
764 debug1("locale says encoding = %d\n", nwin_options
.encoding
);
768 if (((s
= getenv("LC_ALL")) || (s
= getenv("LC_CTYPE")) ||
769 (s
= getenv("LANG"))) && InStr(s
, "UTF-8"))
770 nwin_options
.encoding
= UTF8
;
772 debug1("environment says encoding=%d\n", nwin_options
.encoding
);
776 if (SockMatch
&& strlen(SockMatch
) >= MAXSTR
)
777 Panic(0, "Ridiculously long socketname - try again.");
778 if (cmdflag
&& !rflag
&& !dflag
&& !xflag
)
780 if (!cmdflag
&& dflag
&& mflag
&& !(rflag
|| xflag
))
784 nwin
.encoding
= nwin_undef
.encoding
; /* let screenrc overwrite it */
789 /* make the write() calls return -1 on all errors */
792 * Ronald F. Guilmette, Oct 29 '94, bug-gnu-utils@prep.ai.mit.edu:
793 * It appears that in System V Release 4, UNIX, if you are writing
794 * an output file and you exceed the currently set file size limit,
795 * you _don't_ just get the call to `write' returning with a
796 * failure code. Rather, you get a signal called `SIGXFSZ' which,
797 * if neither handled nor ignored, will cause your program to crash
800 signal(SIGXFSZ
, SIG_IGN
);
804 signal(SIGPIPE
, SIG_IGN
);
811 sh
= getenv("SHELL");
812 ShellProg
= SaveStr(sh
? sh
: DefaultShell
);
814 ShellArgs
[0] = ShellProg
;
815 home
= getenv("HOME");
818 if (!(nethackflag
= (getenv("NETHACKOPTIONS") != NULL
)))
820 char nethackrc
[MAXPATHLEN
];
822 if (home
&& (strlen(home
) < (MAXPATHLEN
- 20)))
824 sprintf(nethackrc
,"%s/.nethackrc", home
);
825 nethackflag
= !access(nethackrc
, F_OK
);
831 own_uid
= multi_uid
= real_uid
;
832 if (SockMatch
&& (sockp
= index(SockMatch
, '/')))
835 Panic(0, "Must run suid root for multiuser support.");
838 SockMatch
= sockp
+ 1;
842 if ((mppp
= getpwnam(multi
)) == (struct passwd
*)0)
843 Panic(0, "Cannot identify account '%s'.", multi
);
844 multi_uid
= mppp
->pw_uid
;
845 multi_home
= SaveStr(mppp
->pw_dir
);
846 if (strlen(multi_home
) > MAXPATHLEN
- 10)
847 Panic(0, "home directory path too long");
849 /* always fake multi attach mode */
857 if (SockMatch
&& *SockMatch
== 0)
859 #endif /* MULTIUSER */
861 if ((LoginName
= getlogin()) && LoginName
[0] != '\0')
863 if ((ppp
= getpwnam(LoginName
)) != (struct passwd
*) 0)
864 if ((int)ppp
->pw_uid
!= real_uid
)
865 ppp
= (struct passwd
*) 0;
869 if ((ppp
= getpwuid(real_uid
)) == 0)
871 Panic(0, "getpwuid() can't identify your account!");
874 LoginName
= ppp
->pw_name
;
876 LoginName
= SaveStr(LoginName
);
878 ppp
= getpwbyname(LoginName
, ppp
);
880 #if !defined(SOCKDIR) && defined(MULTIUSER)
881 if (multi
&& !multiattach
)
883 if (home
&& strcmp(home
, ppp
->pw_dir
))
884 Panic(0, "$HOME must match passwd entry for multiuser screens.");
888 if (home
== 0 || *home
== '\0')
890 if (strlen(LoginName
) > 20)
891 Panic(0, "LoginName too long - sorry.");
893 if (multi
&& strlen(multi
) > 20)
894 Panic(0, "Screen owner name too long - sorry.");
896 if (strlen(home
) > MAXPATHLEN
- 25)
897 Panic(0, "$HOME too long - sorry.");
900 if (!detached
&& !lsflag
&& !cmdflag
&& !(dflag
&& !mflag
&& !rflag
&& !xflag
))
906 /* ttyname implies isatty */
907 if (!(attach_tty
= ttyname(0)))
908 Panic(0, "Must be connected to a terminal.");
909 if (strlen(attach_tty
) >= MAXPATHLEN
)
910 Panic(0, "TtyName too long - sorry.");
911 if (stat(attach_tty
, &st
))
912 Panic(errno
, "Cannot access '%s'", attach_tty
);
914 tty_mode
= (int)st
.st_mode
& 0777;
918 fl
= fcntl(0, F_GETFL
, 0);
919 if (fl
!= -1 && (fl
& (O_RDWR
|O_RDONLY
|O_WRONLY
)) == O_RDWR
)
924 if ((n
= secopen(attach_tty
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
925 Panic(0, "Cannot open your terminal '%s' - please check.", attach_tty
);
928 debug2("attach_tty is %s, attach_fd is %d\n", attach_tty
, attach_fd
);
930 if ((attach_term
= getenv("TERM")) == 0 || *attach_term
== 0)
931 Panic(0, "Please set a terminal type.");
932 if (strlen(attach_term
) > sizeof(D_termname
) - 1)
933 Panic(0, "$TERM too long - sorry.");
934 GetTTY(0, &attach_Mode
);
935 #ifdef DEBUGGGGGGGGGGGGGGG
936 DebugTTY(&attach_Mode
);
941 oumask
= umask(0); /* well, unsigned never fails? jw. */
943 if ((oumask
= (int)umask(0)) == -1)
944 Panic(errno
, "Cannot change umask to zero");
946 SockDir
= getenv("SCREENDIR");
949 if (strlen(SockDir
) >= MAXPATHLEN
- 1)
950 Panic(0, "Ridiculously long $SCREENDIR - try again.");
953 Panic(0, "No $SCREENDIR with multi screens, please.");
960 sprintf(SockPath
, "%s/.screen", multi_home
);
964 sprintf(SockPath
, "%s/S-%s", SockDir
, multi
);
973 sprintf(SockPath
, "%s/.screen", home
);
979 if (access(SockDir
, F_OK
))
981 debug1("SockDir '%s' missing ...\n", SockDir
);
982 if (UserContext() > 0)
984 if (mkdir(SockDir
, 0700))
988 if (UserStatus() <= 0)
989 Panic(0, "Cannot make directory '%s'.", SockDir
);
991 if (SockDir
!= SockPath
)
992 strcpy(SockPath
, SockDir
);
998 if (lstat(SockDir
, &st
))
1000 n
= (eff_uid
== 0 && (real_uid
|| eff_gid
== real_gid
)) ? 0755 :
1001 (eff_gid
!= real_gid
) ? 0775 :
1007 if (mkdir(SockDir
, n
) == -1)
1008 Panic(errno
, "Cannot make directory '%s'", SockDir
);
1012 if (!S_ISDIR(st
.st_mode
))
1013 Panic(0, "'%s' must be a directory.", SockDir
);
1014 if (eff_uid
== 0 && real_uid
&& (int)st
.st_uid
!= eff_uid
)
1015 Panic(0, "Directory '%s' must be owned by root.", SockDir
);
1016 n
= (eff_uid
== 0 && (real_uid
|| (st
.st_mode
& 0775) != 0775)) ? 0755 :
1017 (eff_gid
== (int)st
.st_gid
&& eff_gid
!= real_gid
) ? 0775 :
1019 if (((int)st
.st_mode
& 0777) != n
)
1020 Panic(0, "Directory '%s' must have mode %03o.", SockDir
, n
);
1022 sprintf(SockPath
, "%s/S-%s", SockDir
, LoginName
);
1023 if (access(SockPath
, F_OK
))
1025 if (mkdir(SockPath
, 0700) == -1)
1026 Panic(errno
, "Cannot make directory '%s'", SockPath
);
1027 (void) chown(SockPath
, real_uid
, real_gid
);
1033 if (stat(SockPath
, &st
) == -1)
1034 Panic(errno
, "Cannot access %s", SockPath
);
1036 if (!S_ISDIR(st
.st_mode
))
1037 Panic(0, "%s is not a directory.", SockPath
);
1041 if ((int)st
.st_uid
!= multi_uid
)
1042 Panic(0, "%s is not the owner of %s.", multi
, SockPath
);
1047 if ((int)st
.st_uid
!= real_uid
)
1048 Panic(0, "You are not the owner of %s.", SockPath
);
1050 if ((st
.st_mode
& 0777) != 0700)
1051 Panic(0, "Directory %s must have mode 700.", SockPath
);
1052 if (SockMatch
&& index(SockMatch
, '/'))
1053 Panic(0, "Bad session name '%s'", SockMatch
);
1054 SockName
= SockPath
+ strlen(SockPath
) + 1;
1056 (void) umask(oumask
);
1057 debug2("SockPath: %s SockMatch: %s\n", SockPath
, SockMatch
? SockMatch
: "NULL");
1059 #if defined(SYSV) && !defined(ISC)
1060 if (uname(&utsnam
) == -1)
1061 Panic(errno
, "uname");
1062 strncpy(HostName
, utsnam
.nodename
, sizeof(utsnam
.nodename
) < MAXSTR
? sizeof(utsnam
.nodename
) : MAXSTR
- 1);
1063 HostName
[sizeof(utsnam
.nodename
) < MAXSTR
? sizeof(utsnam
.nodename
) : MAXSTR
- 1] = '\0';
1065 (void) gethostname(HostName
, MAXSTR
);
1066 HostName
[MAXSTR
- 1] = '\0';
1068 if ((ap
= index(HostName
, '.')) != NULL
)
1077 real_uid
= multi_uid
;
1083 i
= FindSocket((int *)NULL
, &fo
, &oth
, SockMatch
);
1085 exit(8 + (fo
? ((oth
|| i
) ? 2 : 1) : 0) + i
);
1087 Panic(0, "No Sockets found in %s.\n", SockPath
);
1088 Panic(0, "%d Socket%s in %s.\n", fo
, fo
> 1 ? "s" : "", SockPath
);
1091 signal(SIG_BYE
, AttacherFinit
); /* prevent races */
1096 /* attach_tty is not mandatory */
1097 if ((attach_tty
= ttyname(0)) == 0)
1099 if (strlen(attach_tty
) >= MAXPATHLEN
)
1100 Panic(0, "TtyName too long - sorry.");
1102 Panic(0, "Please specify a command.");
1107 if (!mflag
&& !SockMatch
)
1109 sty
= getenv("STY");
1110 if (sty
&& *sty
== 0)
1113 SendCmdMessage(sty
, SockMatch
, av
);
1116 else if (rflag
|| xflag
)
1118 debug("screen -r: - is there anybody out there?\n");
1119 if (Attach(MSG_ATTACH
))
1126 Panic(0, "Can't create sessions of other users.");
1128 debug("screen -r: backend not responding -- still crying\n");
1130 else if (dflag
&& !mflag
)
1132 (void) Attach(MSG_DETACH
);
1133 Msg(0, "[%s %sdetached.]\n", SockName
, (dflag
> 1 ? "power " : ""));
1137 if (!SockMatch
&& !mflag
)
1141 if ((sty
= getenv("STY")) != 0 && *sty
!= '\0')
1147 nwin_options
.args
= av
;
1148 SendCreateMsg(sty
, &nwin
);
1153 nwin_compose(&nwin_default
, &nwin_options
, &nwin_default
);
1155 if (!detached
|| dflag
!= 2)
1163 Panic(errno
, "fork");
1171 sprintf(socknamebuf
, "%d.%s", MasterPid
, SockMatch
);
1173 sprintf(socknamebuf
, "%d.%s.%s", MasterPid
, stripdev(attach_tty
), HostName
);
1174 for (ap
= socknamebuf
; *ap
; ap
++)
1178 if (strlen(socknamebuf
) > NAME_MAX
)
1179 socknamebuf
[NAME_MAX
] = 0;
1181 sprintf(SockPath
+ strlen(SockPath
), "/%s", socknamebuf
);
1191 PanicPid
= getppid();
1193 if (DefaultEsc
== -1)
1194 DefaultEsc
= Ctrl('a');
1195 if (DefaultMetaEsc
== -1)
1196 DefaultMetaEsc
= 'a';
1198 ap
= av0
+ strlen(av0
) - 1;
1201 if (!strncmp("screen", ap
, 6))
1203 strncpy(ap
, "SCREEN", 6); /* name this process "SCREEN-BACKEND" */
1215 if (dfp
&& dfp
!= stderr
)
1217 sprintf(buf
, "%s/SCREEN.%d", DEBUGDIR
, (int)getpid());
1218 if ((dfp
= fopen(buf
, "w")) == NULL
)
1221 (void) chmod(buf
, 0666);
1226 if (attach_fd
== -1)
1228 if ((n
= secopen(attach_tty
, O_RDWR
, 0)) < 0)
1229 Panic(0, "Cannot reopen '%s' - please check.", attach_tty
);
1236 freopen("/dev/null", "r", stdin
);
1237 freopen("/dev/null", "w", stdout
);
1242 freopen("/dev/null", "w", stderr
);
1243 debug("-- screen.back debug started\n");
1246 * This guarantees that the session owner is listed, even when we
1247 * start detached. From now on we should not refer to 'LoginName'
1248 * any more, use users->u_name instead.
1250 if (UserAdd(LoginName
, (char *)0, (struct acluser
**)0) < 0)
1251 Panic(0, "Could not create user info");
1254 if (MakeDisplay(LoginName
, attach_tty
, attach_term
, n
, getppid(), &attach_Mode
) == 0)
1255 Panic(0, "Could not alloc display");
1258 D_encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
: 0;
1259 debug1("D_encoding = %d\n", D_encoding
);
1265 /* user started us with -S option */
1266 sprintf(socknamebuf
, "%d.%s", (int)getpid(), SockMatch
);
1270 sprintf(socknamebuf
, "%d.%s.%s", (int)getpid(), stripdev(attach_tty
),
1273 for (ap
= socknamebuf
; *ap
; ap
++)
1277 if (strlen(socknamebuf
) > NAME_MAX
)
1279 debug2("Socketname %s truncated to %d chars\n", socknamebuf
, NAME_MAX
);
1280 socknamebuf
[NAME_MAX
] = 0;
1283 sprintf(SockPath
+ strlen(SockPath
), "/%s", socknamebuf
);
1285 ServerSocket
= MakeServerSocket();
1292 ScriptSource(script_file
);
1298 # ifdef ALLOW_SYSSCREENRC
1299 if ((ap
= getenv("SYSSCREENRC")))
1300 (void)StartRc(ap
, 0);
1303 (void)StartRc(ETCSCREENRC
, 0);
1305 (void)StartRc(RcFileName
, 0);
1309 # endif /* UTNOKEEP */
1310 # endif /* UTMPOK */
1313 if (InitTermcap(0, 0))
1315 debug("Could not init termcap - exiting\n");
1316 fcntl(D_userfd
, F_SETFL
, 0); /* Flush sets FNBLOCK */
1319 Kill(D_userpid
, SIG_BYE
);
1322 MakeDefaultCanvas();
1334 signal(SIGHUP
, SigHup
);
1335 signal(SIGINT
, FinitHandler
);
1336 signal(SIGQUIT
, FinitHandler
);
1337 signal(SIGTERM
, FinitHandler
);
1339 signal(SIGTTIN
, SIG_IGN
);
1340 signal(SIGTTOU
, SIG_IGN
);
1346 SetMode(&D_OldMode
, &D_NewMode
, D_flow
, iflag
);
1347 /* Note: SetMode must be called _before_ FinishRc. */
1348 SetTTY(D_userfd
, &D_NewMode
);
1349 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
1350 Msg(errno
, "Warning: NBLOCK fcntl failed");
1353 brktty(-1); /* just try */
1354 signal(SIGCHLD
, SigChld
);
1356 # ifdef ALLOW_SYSSCREENRC
1357 if ((ap
= getenv("SYSSCREENRC")))
1361 FinishRc(ETCSCREENRC
);
1363 FinishRc(RcFileName
);
1365 debug2("UID %d EUID %d\n", (int)getuid(), (int)geteuid());
1366 if (windows
== NULL
)
1368 debug("We open one default window, as screenrc did not specify one.\n");
1369 if (MakeWindow(&nwin
) == -1)
1371 Msg(0, "Sorry, could not find a PTY.");
1382 if (display
&& default_startup
)
1383 display_copyright();
1384 signal(SIGINT
, SigInt
);
1385 if (rflag
&& (rflag
& 1) == 0 && !quietflag
)
1387 Msg(0, "New screen...");
1391 serv_read
.type
= EV_READ
;
1392 serv_read
.fd
= ServerSocket
;
1393 serv_read
.handler
= serv_read_fn
;
1396 serv_select
.pri
= -10;
1397 serv_select
.type
= EV_ALWAYS
;
1398 serv_select
.handler
= serv_select_fn
;
1399 evenq(&serv_select
);
1401 logflushev
.type
= EV_TIMEOUT
;
1402 logflushev
.handler
= logflush_fn
;
1410 WindowDied(p
, wstat
, wstat_valid
)
1417 if (ZombieKey_destroy
&& ZombieKey_onerror
&& wstat_valid
&&
1418 WIFEXITED(wstat
) && WEXITSTATUS(wstat
) == 0)
1421 if (ZombieKey_destroy
&& !killit
)
1423 char buf
[100], *s
, reason
[100];
1427 if (WIFEXITED(wstat
))
1428 if (WEXITSTATUS(wstat
))
1429 sprintf(reason
, "terminated with exit status %d", WEXITSTATUS(wstat
));
1431 sprintf(reason
, "terminated normally");
1432 else if (WIFSIGNALED(wstat
))
1433 sprintf(reason
, "terminated with signal %d%s", WTERMSIG(wstat
),
1435 WCOREDUMP(wstat
) ? " (core file generated)" : "");
1440 sprintf(reason
, "detached from window");
1445 s
[strlen(s
) - 1] = '\0';
1446 debug3("window %d (%s) going into zombie state fd %d",
1447 p
->w_number
, p
->w_title
, p
->w_ptyfd
);
1449 if (p
->w_slot
!= (slot_t
)0 && p
->w_slot
!= (slot_t
)-1)
1452 p
->w_slot
= 0; /* "detached" */
1457 p
->w_deadpid
= p
->w_pid
;
1460 /* p->w_y = p->w_bot; */
1461 p
->w_y
= MFindUsedLine(p
, p
->w_bot
, 1);
1462 sprintf(buf
, "\n\r=== Command %s (%s) ===", reason
, s
? s
: "?");
1463 WriteString(p
, buf
, strlen(buf
));
1464 WindowChanged(p
, 'f');
1485 signal(SIGCHLD
, SigChld
);
1488 if (stat(SockPath
, &st
) == -1)
1490 debug1("SigChldHandler: Yuck! cannot stat '%s'\n", SockPath
);
1491 if (!RecoverSocket())
1493 debug("SCREEN cannot recover from corrupt Socket, bye\n");
1497 debug1("'%s' reconstructed\n", SockPath
);
1500 debug2("SigChldHandler: stat '%s' o.k. (%03o)\n", SockPath
, (int)st
.st_mode
);
1506 debug("SigChld()\n");
1514 /* Hangup all displays */
1515 while ((display
= displays
) != 0)
1521 * the backend's Interrupt handler
1522 * we cannot insert the intrc directly, as we never know
1531 debug("SigInt()\n");
1532 if (fore
&& displays
)
1534 # if defined(TERMIO) || defined(POSIX)
1535 ibuf
= displays
->d_OldMode
.tio
.c_cc
[VINTR
];
1537 ibuf
= displays
->d_OldMode
.m_tchars
.t_intrc
;
1540 write(fore
->w_ptyfd
, &ibuf
, 1);
1543 signal(SIGINT
, SigInt
);
1544 debug("SigInt() careful\n");
1545 InterruptPlease
= 1;
1553 struct display
*disp
;
1556 #if defined(SYSVSIGS) && defined(SIGHASARG)
1557 signal(sigsig
, SIG_IGN
);
1563 sprintf(buf
, "\r\n[screen caught signal %d.%s]\r\n", sigsig
,
1565 sprintf(buf
, "\r\n[screen caught a fatal signal.%s]\r\n",
1567 #if defined(SHADOWPW) && !defined(DEBUG) && !defined(DUMPSHADOW)
1569 #else /* SHADOWPW && !DEBUG */
1571 #endif /* SHADOWPW && !DEBUG */
1573 for (disp
= displays
; disp
; disp
= disp
->d_next
)
1575 fcntl(disp
->d_userfd
, F_SETFL
, 0);
1576 SetTTY(disp
->d_userfd
, &D_OldMode
);
1577 write(disp
->d_userfd
, buf
, strlen(buf
));
1578 Kill(disp
->d_userpid
, SIG_BYE
);
1580 #if defined(SHADOWPW) && !defined(DEBUG) && !defined(DUMPSHADOW)
1581 Kill(getpid(), SIGKILL
);
1583 #else /* SHADOWPW && !DEBUG */
1585 #endif /* SHADOWPW && !DEBUG */
1593 struct win
*p
, *next
;
1602 while ((pid
= waitpid(-1, &wstat
, WNOHANG
| WUNTRACED
)) > 0)
1606 * From: rouilj@sni-usa.com (John Rouillard)
1607 * note that WUNTRACED is not documented to work, but it is defined in
1608 * /usr/include/sys/wait.h, so it may work
1610 while ((pid
= wait2(&wstat
, WNOHANG
| WUNTRACED
)) > 0)
1611 # else /* USE_WAIT2 */
1612 while ((pid
= wait3(&wstat
, WNOHANG
| WUNTRACED
, (struct rusage
*) 0)) > 0)
1613 # endif /* USE_WAIT2 */
1616 while ((pid
= wait(&wstat
)) < 0)
1620 #endif /* BSDJOBS */
1622 for (p
= windows
; p
; p
= next
)
1625 if ( (p
->w_pid
&& pid
== p
->w_pid
) ||
1626 (p
->w_deadpid
&& pid
== p
->w_deadpid
) )
1628 /* child has ceased to exist */
1632 if (WIFSTOPPED(wstat
))
1634 debug3("Window %d pid %d: WIFSTOPPED (sig %d)\n", p
->w_number
, pid
, WSTOPSIG(wstat
));
1636 if (WSTOPSIG(wstat
) == SIGTTIN
)
1638 Msg(0, "Suspended (tty input)");
1643 if (WSTOPSIG(wstat
) == SIGTTOU
)
1645 Msg(0, "Suspended (tty output)");
1649 /* Try to restart process */
1650 Msg(0, "Child has been stopped, restarting.");
1651 if (killpg(pid
, SIGCONT
))
1657 WindowDied(p
, wstat
, 1);
1662 if (p
->w_pwin
&& pid
== p
->w_pwin
->p_pid
)
1664 debug2("pseudo of win Nr %d died. pid == %d\n", p
->w_number
, p
->w_pwin
->p_pid
);
1672 debug1("pid %d not found - hope that's ok\n", pid
);
1679 FinitHandler SIGDEFARG
1682 debug1("FinitHandler called, sig %d.\n", sigsig
);
1684 debug("FinitHandler called.\n");
1694 signal(SIGCHLD
, SIG_DFL
);
1695 signal(SIGHUP
, SIG_IGN
);
1696 debug1("Finit(%d);\n", i
);
1702 struct win
*p
= windows
;
1703 windows
= windows
->w_next
;
1706 if (ServerSocket
!= -1)
1708 debug1("we unlink(%s)\n", SockPath
);
1713 (void) unlink(SockPath
);
1719 for (display
= displays
; display
; display
= display
->d_next
)
1727 AddStr("[screen is terminating]\r\n");
1729 SetTTY(D_userfd
, &D_OldMode
);
1730 fcntl(D_userfd
, F_SETFL
, 0);
1732 Kill(D_userpid
, SIG_BYE
);
1735 * we _cannot_ call eexit(i) here,
1736 * instead of playing with the Socket above. Sigh.
1746 if (ServerSocket
!= -1)
1748 debug1("we unlink(%s)\n", SockPath
);
1751 (void) unlink(SockPath
);
1761 debug1("Hangup %x\n", display
);
1767 if (auto_detach
|| displays
->d_next
)
1774 * Detach now has the following modes:
1775 *D_DETACH SIG_BYE detach backend and exit attacher
1776 *D_HANGUP SIG_BYE detach backend and exit attacher
1777 *D_STOP SIG_STOP stop attacher (and detach backend)
1778 *D_REMOTE SIG_BYE remote detach -- reattach to new attacher
1779 *D_POWER SIG_POWER_BYE power detach -- attacher kills his parent
1780 *D_REMOTE_POWER SIG_POWER_BYE remote power detach -- both
1781 *D_LOCK SIG_LOCK lock the attacher
1783 * we always remove our utmp slots. (even when "lock" or "stop")
1784 * Note: Take extra care here, we may be called by interrupt!
1797 signal(SIGHUP
, SIG_IGN
);
1798 debug1("Detach(%d)\n", mode
);
1810 AddStr("[detached]\r\n");
1818 #ifdef REMOTE_DETACH
1820 AddStr("[remote detached]\r\n");
1826 AddStr("[power detached]\r\n");
1827 if (PowDetachString
)
1829 AddStr(PowDetachString
);
1832 sign
= SIG_POWER_BYE
;
1834 #ifdef REMOTE_DETACH
1835 case D_REMOTE_POWER
:
1836 AddStr("[remote power detached]\r\n");
1837 if (PowDetachString
)
1839 AddStr(PowDetachString
);
1842 sign
= SIG_POWER_BYE
;
1849 /* tell attacher to lock terminal with a lockprg. */
1853 if (displays
->d_next
== 0)
1855 for (p
= windows
; p
; p
= p
->w_next
)
1857 if (p
->w_slot
!= (slot_t
) -1 && !(p
->w_lflag
& 2))
1861 * Set the slot to 0 to get the window
1864 p
->w_slot
= (slot_t
) 0;
1868 if (mode
!= D_HANGUP
)
1871 if (displays
->d_next
== 0 && console_window
)
1873 if (TtyGrabConsole(console_window
->w_ptyfd
, 0, "detach"))
1875 debug("could not release console - killing window\n");
1876 KillWindow(console_window
);
1877 display
= displays
; /* restore display */
1883 ReleaseAutoWritelock(display
, D_fore
);
1885 D_user
->u_detachwin
= D_fore
->w_number
;
1886 D_user
->u_detachotherwin
= D_other
? D_other
->w_number
: -1;
1888 AutosaveLayout(D_layout
);
1889 layout_last
= D_layout
;
1890 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1892 p
= Layer2Window(cv
->c_layer
);
1893 SetCanvasWindow(cv
, 0);
1895 WindowChanged(p
, 'u');
1899 debug2("display: %#x displays: %#x\n", (unsigned int)display
, (unsigned int)displays
);
1902 /* Flag detached-ness */
1905 * tell father what to do. We do that after we
1906 * freed the tty, thus getty feels more comfortable on hpux
1907 * if it was a power detach.
1910 debug2("Detach: Signal %d to Attacher(%d)!\n", sign
, pid
);
1911 debug("Detach returns, we are successfully detached.\n");
1912 signal(SIGHUP
, SigHup
);
1922 return strncmp(e
, s
, l
) == 0 && e
[l
] == '=';
1928 register char **op
, **np
;
1929 static char stybuf
[MAXSTR
];
1931 for (op
= environ
; *op
; ++op
)
1934 free((char *)NewEnv
);
1935 NewEnv
= np
= (char **) malloc((unsigned) (op
- environ
+ 7 + 1) * sizeof(char **));
1938 sprintf(stybuf
, "STY=%s", strlen(SockName
) <= MAXSTR
- 5 ? SockName
: "?");
1939 *np
++ = stybuf
; /* NewEnv[0] */
1940 *np
++ = Term
; /* NewEnv[1] */
1941 np
++; /* room for SHELL */
1943 np
+= 2; /* room for TERMCAP and WINDOW */
1945 np
+= 4; /* room for TERMCAP WINDOW LINES COLUMNS */
1948 for (op
= environ
; *op
; ++op
)
1950 if (!IsSymbol(*op
, "TERM") && !IsSymbol(*op
, "TERMCAP")
1951 && !IsSymbol(*op
, "STY") && !IsSymbol(*op
, "WINDOW")
1952 && !IsSymbol(*op
, "SCREENCAP") && !IsSymbol(*op
, "SHELL")
1953 && !IsSymbol(*op
, "LINES") && !IsSymbol(*op
, "COLUMNS")
1962 #if defined(USEVARARGS) && defined(__STDC__)
1963 Msg(int err
, char *fmt
, VA_DOTS
)
1965 Msg(err
, fmt
, VA_DOTS
)
1972 char buf
[MAXPATHLEN
*2];
1977 (void)vsnprintf(p
, sizeof(buf
) - 100, fmt
, VA_ARGS(ap
));
1984 strncpy(p
, strerror(err
), buf
+ sizeof(buf
) - p
- 1);
1985 buf
[sizeof(buf
) - 1] = 0;
1987 debug2("Msg('%s') (%#x);\n", buf
, (unsigned int)display
);
1989 if (display
&& displays
)
1993 for (display
= displays
; display
; display
= display
->d_next
)
1998 /* no displays but a display - must have forked.
1999 * send message to backend!
2001 char *tty
= D_usertty
;
2002 struct display
*olddisplay
= display
;
2003 display
= 0; /* only send once */
2004 SendErrorMsg(tty
, buf
);
2005 display
= olddisplay
;
2008 printf("%s\r\n", buf
);
2012 * Call FinitTerm for all displays, write a message to each and call eexit();
2016 #if defined(USEVARARGS) && defined(__STDC__)
2017 Panic(int err
, char *fmt
, VA_DOTS
)
2019 Panic(err
, fmt
, VA_DOTS
)
2026 char buf
[MAXPATHLEN
*2];
2031 (void)vsnprintf(p
, sizeof(buf
) - 100, fmt
, VA_ARGS(ap
));
2038 strncpy(p
, strerror(err
), buf
+ sizeof(buf
) - p
- 1);
2039 buf
[sizeof(buf
) - 1] = 0;
2041 debug3("Panic('%s'); display=%x displays=%x\n", buf
, display
, displays
);
2042 if (displays
== 0 && display
== 0)
2044 printf("%s\r\n", buf
);
2046 Kill(PanicPid
, SIG_BYE
);
2048 else if (displays
== 0)
2050 /* no displays but a display - must have forked.
2051 * send message to backend!
2053 char *tty
= D_usertty
;
2055 SendErrorMsg(tty
, buf
);
2060 for (display
= displays
; display
; display
= display
->d_next
)
2069 SetTTY(D_userfd
, &D_OldMode
);
2070 fcntl(D_userfd
, F_SETFL
, 0);
2071 write(D_userfd
, buf
, strlen(buf
));
2072 write(D_userfd
, "\n", 1);
2075 Kill(D_userpid
, SIG_BYE
);
2078 if (tty_oldmode
>= 0)
2081 if (setuid(own_uid
))
2082 xseteuid(own_uid
); /* may be a loop. sigh. */
2086 debug1("Panic: changing back modes from %s\n", attach_tty
);
2087 chmod(attach_tty
, tty_oldmode
);
2095 * '^' is allowed as an escape mechanism for control characters. jw.
2097 * Added time insertion using ideas/code from /\ndy Jones
2098 * (andy@lingua.cltr.uq.OZ.AU) - thanks a lot!
2103 static const char days
[] = "SunMonTueWedThuFriSat";
2104 static const char months
[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
2107 static char winmsg_buf
[MAXSTR
];
2108 #define MAX_WINMSG_REND 16 /* rendition changes */
2109 static int winmsg_rend
[MAX_WINMSG_REND
];
2110 static int winmsg_rendpos
[MAX_WINMSG_REND
];
2111 static int winmsg_numrend
;
2114 pad_expand(buf
, p
, numpad
, padlen
)
2123 padlen
= padlen
- (p
- buf
); /* space for rent */
2126 pn2
= pn
= p
+ padlen
;
2130 if (r
&& p
- buf
== winmsg_rendpos
[r
- 1])
2132 winmsg_rendpos
[--r
] = pn
- buf
;
2139 i
= numpad
> 0 ? (padlen
+ numpad
- 1) / numpad
: 0;
2150 struct backtick
*next
;
2155 char result
[MAXSTR
];
2162 struct backtick
*backticks
;
2166 struct backtick
*bt
;
2171 for (p
= q
= bt
->result
; (c
= (unsigned char)*p
++) != 0;)
2175 if (c
>= ' ' || c
== '\005')
2182 backtick_fn(ev
, data
)
2186 struct backtick
*bt
;
2189 bt
= (struct backtick
*)data
;
2190 debug1("backtick_fn for #%d\n", bt
->num
);
2192 l
= read(ev
->fd
, bt
->buf
+ i
, MAXSTR
- i
);
2195 debug1("EOF on backtick #%d\n", bt
->num
);
2201 debug1("read %d bytes\n", l
);
2203 for (j
= 0; j
< l
; j
++)
2204 if (bt
->buf
[i
- j
- 1] == '\n')
2208 for (k
= i
- j
- 2; k
>= 0; k
--)
2209 if (bt
->buf
[k
] == '\n')
2212 bcopy(bt
->buf
+ k
, bt
->result
, i
- j
- k
);
2213 bt
->result
[i
- j
- k
- 1] = 0;
2214 backtick_filter(bt
);
2215 WindowChanged(0, '`');
2217 if (j
== l
&& i
== MAXSTR
)
2225 bcopy(bt
->buf
+ i
- j
, bt
->buf
, j
);
2232 setbacktick(num
, lifespan
, tick
, cmdv
)
2238 struct backtick
**btp
, *bt
;
2241 debug1("setbacktick called for backtick #%d\n", num
);
2242 for (btp
= &backticks
; (bt
= *btp
) != 0; btp
= &bt
->next
)
2249 for (v
= bt
->cmdv
; *v
; v
++)
2266 bt
= (struct backtick
*)malloc(sizeof *bt
);
2272 bzero(bt
, sizeof(*bt
));
2278 bt
->lifespan
= lifespan
;
2285 if (bt
->tick
== 0 && bt
->lifespan
== 0)
2287 debug("setbacktick: continuous mode\n");
2288 bt
->buf
= (char *)malloc(MAXSTR
);
2292 setbacktick(num
, 0, 0, (char **)0);
2295 bt
->ev
.type
= EV_READ
;
2296 bt
->ev
.fd
= readpipe(bt
->cmdv
);
2297 bt
->ev
.handler
= backtick_fn
;
2298 bt
->ev
.data
= (char *)bt
;
2305 runbacktick(bt
, tickp
, now
)
2306 struct backtick
*bt
;
2313 debug1("runbacktick called for backtick #%d\n", bt
->num
);
2314 if (bt
->tick
&& (!*tickp
|| bt
->tick
< *tickp
))
2316 if ((bt
->lifespan
== 0 && bt
->tick
== 0) || now
< bt
->bestbefore
)
2318 debug1("returning old result (%d)\n", bt
->lifespan
);
2321 f
= readpipe(bt
->cmdv
);
2325 while ((l
= read(f
, bt
->result
+ i
, sizeof(bt
->result
) - i
)) > 0)
2327 debug1("runbacktick: read %d bytes\n", l
);
2329 for (j
= 1; j
< l
; j
++)
2330 if (bt
->result
[i
- j
- 1] == '\n')
2332 if (j
== l
&& i
== sizeof(bt
->result
))
2334 j
= sizeof(bt
->result
) / 2;
2339 bcopy(bt
->result
+ i
- j
, bt
->result
, j
);
2344 bt
->result
[sizeof(bt
->result
) - 1] = '\n';
2345 if (i
&& bt
->result
[i
- 1] == '\n')
2347 debug1("runbacktick: finished, %d bytes\n", i
);
2349 backtick_filter(bt
);
2351 bt
->bestbefore
= now2
+ bt
->lifespan
;
2356 MakeWinMsgEv(str
, win
, esc
, padlen
, ev
, rec
)
2366 register char *p
= winmsg_buf
;
2376 int qmflag
= 0, omflag
= 0, qmnumrend
= 0;
2383 struct backtick
*bt
;
2385 if (winmsg_numrend
>= 0)
2388 winmsg_numrend
= -winmsg_numrend
;
2393 gettimeofday(&now
, NULL
);
2394 for (; *s
&& (l
= winmsg_buf
+ MAXSTR
- 1 - p
) > 0; s
++, p
++)
2400 if (*s
!= '^' && *s
>= 64)
2425 if (*++s
== esc
) /* double escape ? */
2427 if ((plusflg
= *s
== '+') != 0)
2429 if ((minusflg
= *s
== '-') != 0)
2431 if ((zeroflg
= *s
== '0') != 0)
2434 while(*s
>= '0' && *s
<= '9')
2435 num
= num
* 10 + (*s
++ - '0');
2436 if ((longflg
= *s
== 'L') != 0)
2444 if ((!qmflag
&& !omflag
) || omflag
== 1)
2447 if (qmnumrend
< winmsg_numrend
)
2448 winmsg_numrend
= qmnumrend
;
2454 qmnumrend
= winmsg_numrend
;
2455 qmflag
= omflag
= 0;
2461 if (qmflag
&& omflag
!= 1)
2465 qmnumrend
= winmsg_numrend
;
2470 if (qmnumrend
< winmsg_numrend
)
2471 winmsg_numrend
= qmnumrend
;
2475 case 'd': case 'D': case 'm': case 'M': case 'y': case 'Y':
2476 case 'a': case 'A': case 's': case 'c': case 'C':
2481 time_t nowsec
= now
.tv_sec
;
2482 tm
= localtime(&nowsec
);
2485 if (!tick
|| tick
> 3600)
2490 sprintf(p
, "%02d", tm
->tm_mday
% 100);
2494 strftime(p
, l
, (longflg
? "%A" : "%a"), tm
);
2496 sprintf(p
, "%3.3s", days
+ 3 * tm
->tm_wday
);
2500 sprintf(p
, "%02d", tm
->tm_mon
+ 1);
2504 strftime(p
, l
, (longflg
? "%B" : "%b"), tm
);
2506 sprintf(p
, "%3.3s", months
+ 3 * tm
->tm_mon
);
2510 sprintf(p
, "%02d", tm
->tm_year
% 100);
2513 sprintf(p
, "%04d", tm
->tm_year
+ 1900);
2516 sprintf(p
, tm
->tm_hour
>= 12 ? "pm" : "am");
2519 sprintf(p
, tm
->tm_hour
>= 12 ? "PM" : "AM");
2522 sprintf(p
, "%02d", tm
->tm_sec
);
2526 sprintf(p
, zeroflg
? "%02d:%02d" : "%2d:%02d", tm
->tm_hour
, tm
->tm_min
);
2527 if (!tick
|| tick
> 60)
2531 sprintf(p
, zeroflg
? "%02d:%02d" : "%2d:%02d", (tm
->tm_hour
+ 11) % 12 + 1, tm
->tm_min
);
2532 if (!tick
|| tick
> 60)
2552 if (!tick
|| tick
> 60)
2561 if (rec
>= 10 || (*s
== 'h' && (win
== 0 || win
->w_hstatus
== 0 || *win
->w_hstatus
== 0)))
2568 for (bt
= backticks
; bt
; bt
= bt
->next
)
2578 char savebuf
[sizeof(winmsg_buf
)];
2580 int oldnumrend
= winmsg_numrend
;
2583 strcpy(savebuf
, winmsg_buf
);
2584 winmsg_numrend
= -winmsg_numrend
;
2585 MakeWinMsgEv(*s
== 'h' ? win
->w_hstatus
: runbacktick(bt
, &oldtick
, now
.tv_sec
), win
, '\005', 0, (struct event
*)0, rec
+ 1);
2586 debug2("oldtick=%d tick=%d\n", oldtick
, tick
);
2587 if (!tick
|| oldtick
< tick
)
2589 if ((int)strlen(winmsg_buf
) < l
)
2590 strcat(savebuf
, winmsg_buf
);
2591 strcpy(winmsg_buf
, savebuf
);
2592 while (oldnumrend
< winmsg_numrend
)
2593 winmsg_rendpos
[oldnumrend
++] += p
- winmsg_buf
;
2602 struct win
*oldfore
= 0;
2610 ss
= AddWindows(p
, l
- 1, (*s
== 'w' ? 0 : 1) | (longflg
? 0 : 2) | (plusflg
? 4 : 0), win
? win
->w_number
: -1);
2623 AddOtherUsers(p
, l
- 1, win
);
2631 AddWindowFlags(p
, l
- 1, win
);
2638 if (win
&& (int)strlen(win
->w_title
) < l
)
2640 strcpy(p
, win
->w_title
);
2650 for (i
= 0; i
< 127; i
++)
2651 if (s
[i
] && s
[i
] != '}')
2655 if (s
[i
] == '}' && winmsg_numrend
< MAX_WINMSG_REND
)
2659 debug1("MakeWinMsg attrcolor %s\n", rbuf
);
2660 if (i
!= 1 || rbuf
[0] != '-')
2661 r
= ParseAttrColor(rbuf
, (char *)0, 0);
2662 if (r
!= -1 || (i
== 1 && rbuf
[0] == '-'))
2664 winmsg_rend
[winmsg_numrend
] = r
;
2665 winmsg_rendpos
[winmsg_numrend
] = p
- winmsg_buf
;
2675 if ((int)strlen(HostName
) < l
)
2677 strcpy(p
, HostName
);
2686 if (display
&& ((ev
&& ev
== &D_forecv
->c_captev
) || (!ev
&& win
&& win
== D_fore
)))
2687 minusflg
= !minusflg
;
2692 truncpos
= p
- winmsg_buf
;
2693 truncper
= num
> 100 ? 100 : num
;
2694 trunclong
= longflg
;
2700 if (num
|| zeroflg
|| plusflg
|| longflg
|| (*s
!= '='))
2702 /* expand all pads */
2705 num
= (plusflg
? lastpad
: padlen
) - num
;
2706 if (!plusflg
&& padlen
== 0)
2707 num
= p
- winmsg_buf
;
2712 if (*s
!= '=' && num
== 0 && !plusflg
)
2717 num
= p
- winmsg_buf
;
2719 num
= (padlen
- (plusflg
? lastpad
: 0)) * num
/ 100;
2725 if (num
> MAXSTR
- 1)
2728 p
= pad_expand(winmsg_buf
, p
, numpad
, num
);
2730 if (p
- winmsg_buf
> num
&& !longflg
)
2739 trunc
= lastpad
+ truncper
* (num
- lastpad
) / 100;
2742 if (trunc
< lastpad
)
2744 left
= truncpos
- trunc
;
2745 if (left
> p
- winmsg_buf
- num
)
2746 left
= p
- winmsg_buf
- num
;
2747 debug1("lastpad = %d, ", lastpad
);
2748 debug3("truncpos = %d, trunc = %d, left = %d\n", truncpos
, trunc
, left
);
2751 if (left
+ lastpad
> p
- winmsg_buf
)
2752 left
= p
- winmsg_buf
- lastpad
;
2753 if (p
- winmsg_buf
- lastpad
- left
> 0)
2754 bcopy(winmsg_buf
+ lastpad
+ left
, winmsg_buf
+ lastpad
, p
- winmsg_buf
- lastpad
- left
);
2757 while (r
&& winmsg_rendpos
[r
- 1] > lastpad
)
2760 winmsg_rendpos
[r
] -= left
;
2761 if (winmsg_rendpos
[r
] < lastpad
)
2762 winmsg_rendpos
[r
] = lastpad
;
2766 if (p
- winmsg_buf
> lastpad
)
2767 winmsg_buf
[lastpad
] = '.';
2768 if (p
- winmsg_buf
> lastpad
+ 1)
2769 winmsg_buf
[lastpad
+ 1] = '.';
2770 if (p
- winmsg_buf
> lastpad
+ 2)
2771 winmsg_buf
[lastpad
+ 2] = '.';
2774 if (p
- winmsg_buf
> num
)
2776 p
= winmsg_buf
+ num
;
2779 if (num
- 1 >= lastpad
)
2781 if (num
- 2 >= lastpad
)
2783 if (num
- 3 >= lastpad
)
2787 while (r
&& winmsg_rendpos
[r
- 1] > num
)
2788 winmsg_rendpos
[--r
] = num
;
2792 if (lastpad
> p
- winmsg_buf
)
2793 lastpad
= p
- winmsg_buf
;
2794 debug1("lastpad now %d\n", lastpad
);
2798 while (p
- winmsg_buf
< num
)
2800 lastpad
= p
- winmsg_buf
;
2803 debug1("lastpad2 now %d\n", lastpad
);
2809 *p
= 127; /* internal pad representation */
2823 sprintf(p
, "%*s", num
, num
> 1 ? "--" : "-");
2825 sprintf(p
, "%*d", num
, win
->w_number
);
2832 if (qmpos
&& !qmflag
)
2837 if (padlen
> MAXSTR
- 1)
2838 padlen
= MAXSTR
- 1;
2839 p
= pad_expand(winmsg_buf
, p
, numpad
, padlen
);
2843 evdeq(ev
); /* just in case */
2844 ev
->timeout
.tv_sec
= 0;
2845 ev
->timeout
.tv_usec
= 0;
2849 now
.tv_usec
= 100000;
2853 now
.tv_sec
+= tick
- (now
.tv_sec
% tick
);
2855 debug2("NEW timeout %d %d\n", ev
->timeout
.tv_sec
, tick
);
2861 MakeWinMsg(s
, win
, esc
)
2866 return MakeWinMsgEv(s
, win
, esc
, 0, (struct event
*)0, 0);
2870 PutWinMsg(s
, start
, max
)
2876 struct mchar rendstack
[MAX_WINMSG_REND
];
2879 if (s
!= winmsg_buf
)
2881 /* sorry, no fancy coloring available */
2882 debug1("PutWinMsg %s plain\n", s
);
2895 debug2("PutWinMsg %s start attr %x\n", s
, rend
.attr
);
2896 for (i
= 0; i
< winmsg_numrend
&& max
> 0; i
++)
2898 if (p
> winmsg_rendpos
[i
] || winmsg_rendpos
[i
] > l
)
2900 if (p
< winmsg_rendpos
[i
])
2902 n
= winmsg_rendpos
[i
] - p
;
2919 rend
= rendstack
[--rendstackn
];
2923 rendstack
[rendstackn
++] = rend
;
2924 ApplyAttrColor(r
, &rend
);
2926 SetRendition(&rend
);
2954 if ((j
= open("/dev/null", 0)) >= 0)
2961 while (dup(++i
) < 0 && errno
!= EBADF
)
2963 debug1(" [%d]\n", i
);
2976 serv_read_fn(ev
, data
)
2980 debug("Knock - knock!\n");
2985 serv_select_fn(ev
, data
)
2991 debug("serv_select_fn called\n");
2992 /* XXX: messages?? */
2997 if (InterruptPlease
)
2999 debug("Backend received interrupt\n");
3000 /* This approach is rather questionable in a multi-display
3002 if (fore
&& displays
)
3004 #if defined(TERMIO) || defined(POSIX)
3005 char ibuf
= displays
->d_OldMode
.tio
.c_cc
[VINTR
];
3007 char ibuf
= displays
->d_OldMode
.m_tchars
.t_intrc
;
3010 write(W_UWP(fore
) ? fore
->w_pwin
->p_ptyfd
: fore
->w_ptyfd
,
3012 debug1("Backend wrote interrupt to %d", fore
->w_number
);
3013 debug1("%s\n", W_UWP(fore
) ? " (pseudowin)" : "");
3015 write(fore
->w_ptyfd
, &ibuf
, 1);
3016 debug1("Backend wrote interrupt to %d\n", fore
->w_number
);
3019 InterruptPlease
= 0;
3022 for (p
= windows
; p
; p
= p
->w_next
)
3024 if (p
->w_bell
== BELL_FOUND
|| p
->w_bell
== BELL_VISUAL
)
3027 int visual
= p
->w_bell
== BELL_VISUAL
|| visual_bell
;
3028 p
->w_bell
= BELL_ON
;
3029 for (display
= displays
; display
; display
= display
->d_next
)
3031 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3032 if (cv
->c_layer
->l_bottom
== &p
->w_layer
)
3036 p
->w_bell
= BELL_DONE
;
3037 Msg(0, "%s", MakeWinMsg(BellString
, p
, '%'));
3039 else if (visual
&& !D_VB
&& (!D_status
|| !D_status_bell
))
3041 Msg(0, "%s", VisualBellString
);
3045 debug1("using vbell timeout %d\n", VBellWait
);
3046 SetTimeout(&D_statusev
, VBellWait
);
3050 /* don't annoy the user with two messages */
3051 if (p
->w_monitor
== MON_FOUND
)
3052 p
->w_monitor
= MON_DONE
;
3053 WindowChanged(p
, 'f');
3055 if (p
->w_monitor
== MON_FOUND
)
3058 p
->w_monitor
= MON_ON
;
3059 for (display
= displays
; display
; display
= display
->d_next
)
3061 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3062 if (cv
->c_layer
->l_bottom
== &p
->w_layer
)
3065 continue; /* user already sees window */
3067 if (!(ACLBYTE(p
->w_mon_notify
, D_user
->u_id
) & ACLBIT(D_user
->u_id
)))
3068 continue; /* user doesn't care */
3070 Msg(0, "%s", MakeWinMsg(ActivityString
, p
, '%'));
3071 p
->w_monitor
= MON_DONE
;
3073 WindowChanged(p
, 'f');
3077 for (display
= displays
; display
; display
= display
->d_next
)
3080 if (D_status
== STATUS_ON_WIN
)
3082 /* XXX: should use display functions! */
3083 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
3087 /* normalize window, see resize.c */
3088 lx
= cv
->c_layer
->l_x
;
3089 ly
= cv
->c_layer
->l_y
;
3090 if (lx
== cv
->c_layer
->l_width
)
3092 if (ly
+ cv
->c_yoff
< cv
->c_ys
)
3094 int i
, n
= cv
->c_ys
- (ly
+ cv
->c_yoff
);
3095 cv
->c_yoff
= cv
->c_ys
- ly
;
3096 RethinkViewportOffsets(cv
);
3097 if (n
> cv
->c_layer
->l_height
)
3098 n
= cv
->c_layer
->l_height
;
3100 LScrollV(flayer
, -n
, 0, flayer
->l_height
- 1, 0);
3101 LayRedisplayLine(-1, -1, -1, 1);
3102 for (i
= 0; i
< n
; i
++)
3103 LayRedisplayLine(i
, 0, flayer
->l_width
- 1, 1);
3104 if (cv
== cv
->c_display
->d_forecv
)
3108 else if (ly
+ cv
->c_yoff
> cv
->c_ye
)
3110 int i
, n
= ly
+ cv
->c_yoff
- cv
->c_ye
;
3111 cv
->c_yoff
= cv
->c_ye
- ly
;
3112 RethinkViewportOffsets(cv
);
3113 if (n
> cv
->c_layer
->l_height
)
3114 n
= cv
->c_layer
->l_height
;
3116 LScrollV(flayer
, n
, 0, cv
->c_layer
->l_height
- 1, 0);
3117 LayRedisplayLine(-1, -1, -1, 1);
3118 for (i
= 0; i
< n
; i
++)
3119 LayRedisplayLine(i
+ flayer
->l_height
- n
, 0, flayer
->l_width
- 1, 1);
3120 if (cv
== cv
->c_display
->d_forecv
)
3124 if (lx
+ cv
->c_xoff
< cv
->c_xs
)
3126 int i
, n
= cv
->c_xs
- (lx
+ cv
->c_xoff
);
3127 if (n
< (cv
->c_xe
- cv
->c_xs
+ 1) / 2)
3128 n
= (cv
->c_xe
- cv
->c_xs
+ 1) / 2;
3129 if (cv
->c_xoff
+ n
> cv
->c_xs
)
3130 n
= cv
->c_xs
- cv
->c_xoff
;
3132 RethinkViewportOffsets(cv
);
3133 if (n
> cv
->c_layer
->l_width
)
3134 n
= cv
->c_layer
->l_width
;
3136 LayRedisplayLine(-1, -1, -1, 1);
3137 for (i
= 0; i
< flayer
->l_height
; i
++)
3139 LScrollH(flayer
, -n
, i
, 0, flayer
->l_width
- 1, 0, 0);
3140 LayRedisplayLine(i
, 0, n
- 1, 1);
3142 if (cv
== cv
->c_display
->d_forecv
)
3146 else if (lx
+ cv
->c_xoff
> cv
->c_xe
)
3148 int i
, n
= lx
+ cv
->c_xoff
- cv
->c_xe
;
3149 if (n
< (cv
->c_xe
- cv
->c_xs
+ 1) / 2)
3150 n
= (cv
->c_xe
- cv
->c_xs
+ 1) / 2;
3151 if (cv
->c_xoff
- n
+ cv
->c_layer
->l_width
- 1 < cv
->c_xe
)
3152 n
= cv
->c_xoff
+ cv
->c_layer
->l_width
- 1 - cv
->c_xe
;
3154 RethinkViewportOffsets(cv
);
3155 if (n
> cv
->c_layer
->l_width
)
3156 n
= cv
->c_layer
->l_width
;
3158 LayRedisplayLine(-1, -1, -1, 1);
3159 for (i
= 0; i
< flayer
->l_height
; i
++)
3161 LScrollH(flayer
, n
, i
, 0, flayer
->l_width
- 1, 0, 0);
3162 LayRedisplayLine(i
, flayer
->l_width
- n
, flayer
->l_width
- 1, 1);
3164 if (cv
== cv
->c_display
->d_forecv
)
3171 for (display
= displays
; display
; display
= display
->d_next
)
3173 if (D_status
== STATUS_ON_WIN
|| D_cvlist
== 0 || D_cvlist
->c_next
== 0)
3175 debug1("serv_select_fn: Restore on cv %#x\n", (int)D_forecv
);
3176 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
3181 logflush_fn(ev
, data
)
3189 if (!islogfile(NULL
))
3190 return; /* no more logfiles */
3192 n
= log_flush
? log_flush
: (logtstamp_after
+ 4) / 5;
3195 SetTimeout(ev
, n
* 1000);
3196 evenq(ev
); /* re-enqueue ourself */
3200 /* write fancy time-stamp */
3201 for (p
= windows
; p
; p
= p
->w_next
)
3205 p
->w_logsilence
+= n
;
3206 if (p
->w_logsilence
< logtstamp_after
)
3208 if (p
->w_logsilence
- n
>= logtstamp_after
)
3210 buf
= MakeWinMsg(logtstamp_string
, p
, '%');
3211 logfwrite(p
->w_log
, buf
, strlen(buf
));
3216 * Interprets ^?, ^@ and other ^-control-char notation.
3217 * Interprets \ddd octal notation
3219 * The result is placed in *cp, p is advanced behind the parsed expression and
3228 if (*p
== '^' && p
[1])
3238 else if (*p
== '\\' && *++p
<= '7' && *p
>= '0')
3242 *cp
= *cp
* 8 + *p
- '0';
3243 while (*++p
<= '7' && *p
>= '0');
3254 unsigned char buf
[2];
3257 SetEscape((struct acluser
*)0, -1, -1);
3260 if ((p
= ParseChar(p
, (char *)buf
)) == NULL
||
3261 (p
= ParseChar(p
, (char *)buf
+1)) == NULL
|| *p
)
3263 SetEscape((struct acluser
*)0, buf
[0], buf
[1]);