1 /* Copyright (c) 2008, 2009
2  *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3  *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4  *      Micah Cowan (micah@cowan.name)
5  *      Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6  * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7  *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8  *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9  * Copyright (c) 1987 Oliver Laumann
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program (see the file COPYING); if not, see
23  * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
25  *
26  ****************************************************************
27  * $Id$ GNU
28  */
29 
30 #include "os.h"
31 
32 #if defined(__STDC__)
33 # ifndef __P
34 #  define __P(a) a
35 # endif
36 #else
37 # ifndef __P
38 #  define __P(a) ()
39 # endif
40 # define const
41 #endif
42 
43 #include "osdef.h"
44 
45 #include "ansi.h"
46 #include "sched.h"
47 #include "acls.h"
48 #include "comm.h"
49 #include "layer.h"
50 #include "term.h"
51 
52 
53 #ifdef DEBUG
54 # define STATIC		/* a function that the debugger should see */
55 #else
56 # define STATIC static
57 #endif
58 
59 #ifdef DEBUG
60 # define DEBUGDIR "/tmp/debug"
61 # define debugf(a)       do {if(dfp){fprintf a;fflush(dfp);}} while (0)
62 # define debug(x)        debugf((dfp,x))
63 # define debug1(x,a)     debugf((dfp,x,a))
64 # define debug2(x,a,b)   debugf((dfp,x,a,b))
65 # define debug3(x,a,b,c) debugf((dfp,x,a,b,c))
66   extern FILE *dfp;
67 #else
68 # define debugf(a)       do {} while (0)
69 # define debug(x)        debugf(x)
70 # define debug1(x,a)     debugf(x)
71 # define debug2(x,a,b)   debugf(x)
72 # define debug3(x,a,b,c) debugf(x)
73 #endif
74 
75 #ifndef DEBUG
76 # define NOASSERT
77 #endif
78 
79 #ifndef NOASSERT
80 # if defined(__STDC__)
81 #  define ASSERT(lousy_cpp) do {if (!(lousy_cpp)) {if (!dfp) opendebug(0, 1);debug2("ASSERT("#lousy_cpp") failed file %s line %d\n", __FILE__, __LINE__);abort();}} while (0)
82 # else
83 #  define ASSERT(lousy_cpp) do {if (!(lousy_cpp)) {if (!dfp) opendebug(0, 1);debug2("ASSERT(lousy_cpp) failed file %s line %d\n", __FILE__, __LINE__);abort();}} while (0)
84 # endif
85 #else
86 # define ASSERT(lousy_cpp) do {} while (0)
87 #endif
88 
89 /* here comes my own Free: jw. */
90 #define Free(a) {if ((a) == 0) abort(); else free((void *)(a)); (a)=0;}
91 
92 #define Ctrl(c) ((c)&037)
93 
94 #define MAXSTR		768
95 #define MAXARGS 	64
96 #define MSGWAIT 	5
97 #define MSGMINWAIT 	1
98 #define SILENCEWAIT	30
99 
100 /*
101  * if a nasty user really wants to try a history of 3000 lines on all 10
102  * windows, he will allocate 8 MegaBytes of memory, which is quite enough.
103  */
104 #define MAXHISTHEIGHT		3000
105 #define DEFAULTHISTHEIGHT	100
106 #if defined(NAME_MAX) && NAME_MAX < 16
107 # define DEFAULT_BUFFERFILE     "/tmp/screen-xchg"
108 #else
109 # define DEFAULT_BUFFERFILE	"/tmp/screen-exchange"
110 #endif
111 
112 
113 #if defined(hpux) && !(defined(VSUSP) && defined(VDSUSP) && defined(VWERASE) && defined(VLNEXT))
114 # define HPUX_LTCHARS_HACK
115 #endif
116 
117 struct mode
118 {
119 #ifdef POSIX
120   struct termios tio;
121 # ifdef HPUX_LTCHARS_HACK
122   struct ltchars m_ltchars;
123 # endif /* HPUX_LTCHARS_HACK */
124 #else /* POSIX */
125 # ifdef TERMIO
126   struct termio tio;
127 #  ifdef CYTERMIO
128   int m_mapkey;
129   int m_mapscreen;
130   int m_backspace;
131 #  endif
132 # else /* TERMIO */
133   struct sgttyb m_ttyb;
134   struct tchars m_tchars;
135   struct ltchars m_ltchars;
136   int m_ldisc;
137   int m_lmode;
138 # endif /* TERMIO */
139 #endif /* POSIX */
140 #if defined(KANJI) && defined(TIOCKSET)
141   struct jtchars m_jtchars;
142   int m_knjmode;
143 #endif
144 };
145 
146 
147 /* #include "logfile.h" */	/* (requires stat.h) struct logfile */
148 #include "image.h"
149 #include "canvas.h"
150 #include "display.h"
151 #include "window.h"
152 
153 /*
154  * Parameters for the Detach() routine
155  */
156 #define D_DETACH	0
157 #define D_STOP		1
158 #define D_REMOTE	2
159 #define D_POWER 	3
160 #define D_REMOTE_POWER	4
161 #define D_LOCK		5
162 #define D_HANGUP	6
163 
164 /*
165  * Here are the messages the attacher sends to the backend
166  */
167 #define MSG_CREATE	0
168 #define MSG_ERROR	1
169 #define MSG_ATTACH	2
170 #define MSG_CONT	3
171 #define MSG_DETACH	4
172 #define MSG_POW_DETACH	5
173 #define MSG_WINCH	6
174 #define MSG_HANGUP	7
175 #define MSG_COMMAND	8
176 #define MSG_QUERY       9
177 
178 /*
179  * versions of struct msg:
180  * 0:	screen version 3.6.6 (version count introduced)
181  * 1:	screen version 4.1.0devel	(revisions e3fc19a upto 8147d08)
182  * 					 A few revisions after 8147d08 incorrectly
183  * 					 carried version 1, but should have carried 2.
184  * 2:	screen version 4.1.0devel	(revisions 8b46d8a upto YYYYYYY)
185  * 3:	screen version 4.2.0		(was incorrectly originally. Patched here)
186  * 4:	screen version 4.2.1		(bumped once again due to changed terminal and login length)
187  * 5: 	screen version 4.4.0		(fix screenterm size)
188  */
189 #define MSG_VERSION	5
190 
191 #define MSG_REVISION	(('m'<<24) | ('s'<<16) | ('g'<<8) | MSG_VERSION)
192 struct msg
193 {
194   int protocol_revision;	/* reduce harm done by incompatible messages */
195   int type;
196   char m_tty[MAXPATHLEN];	/* ttyname */
197   union
198     {
199       struct
200 	{
201 	  int lflag;
202 	  int aflag;
203 	  int flowflag;
204 	  int hheight;		/* size of scrollback buffer */
205 	  int nargs;
206 	  char line[MAXPATHLEN];
207 	  char dir[MAXPATHLEN];
208 	  char screenterm[MAXTERMLEN + 1];	/* is screen really "screen" ? */
209 	}
210       create;
211       struct
212 	{
213 	  char auser[MAXLOGINLEN + 1];	/* username */
214 	  int apid;		/* pid of frontend */
215 	  int adaptflag;	/* adapt window size? */
216 	  int lines, columns;	/* display size */
217 	  char preselect[20];
218 	  int esc;		/* his new escape character unless -1 */
219 	  int meta_esc;		/* his new meta esc character unless -1 */
220 	  char envterm[MAXTERMLEN + 1];	/* terminal type */
221 	  int encoding;		/* encoding of display */
222 	  int detachfirst;      /* whether to detach remote sessions first */
223 	}
224       attach;
225       struct
226 	{
227 	  char duser[MAXLOGINLEN + 1];	/* username */
228 	  int dpid;		/* pid of frontend */
229 	}
230       detach;
231       struct
232 	{
233 	  char auser[MAXLOGINLEN + 1];	/* username */
234 	  int nargs;
235 	  char cmd[MAXPATHLEN];	/* command */
236 	  int apid;		/* pid of frontend */
237 	  char preselect[20];
238 	  char writeback[MAXPATHLEN];  /* The socket to write the result.
239 					  Only used for MSG_QUERY */
240 	}
241       command;
242       char message[MAXPATHLEN * 2];
243     } m;
244 };
245 
246 /*
247  * And the signals the attacher receives from the backend
248  */
249 #define SIG_BYE		SIGHUP
250 #define SIG_POWER_BYE	SIGUSR1
251 #define SIG_LOCK	SIGUSR2
252 #define SIG_STOP	SIGTSTP
253 #ifdef SIGIO
254 #define SIG_NODEBUG	SIGIO		/* triggerd by command 'debug off' */
255 #endif
256 
257 
258 #define BELL		(Ctrl('g'))
259 #define VBELLWAIT	1 /* No. of seconds a vbell will be displayed */
260 
261 #define BELL_ON		0 /* No bell has occurred in the window */
262 #define BELL_FOUND	1 /* A bell has occurred, but user not yet notified */
263 #define BELL_DONE	2 /* A bell has occured, user has been notified */
264 
265 #define BELL_VISUAL	3 /* A bell has occured in fore win, notify him visually */
266 
267 #define MON_OFF 	0 /* Monitoring is off in the window */
268 #define MON_ON		1 /* No activity has occurred in the window */
269 #define MON_FOUND	2 /* Activity has occured, but user not yet notified */
270 #define MON_DONE	3 /* Activity has occured, user has been notified */
271 
272 #define DUMP_TERMCAP	0 /* WriteFile() options */
273 #define DUMP_HARDCOPY	1
274 #define DUMP_EXCHANGE	2
275 #define DUMP_SCROLLBACK 3
276 
277 #define SILENCE_OFF	0 /* Not checking for silence */
278 #define SILENCE_ON	1 /* Window being monitored for silence */
279 #define SILENCE_FOUND   2 /* Window is silent */
280 #define SILENCE_DONE    3 /* Window is silent and user is notified */
281 
282 extern char strnomem[];
283 
284 /*
285  * line modes used by Input()
286  */
287 #define INP_COOKED	0
288 #define INP_NOECHO	1
289 #define INP_RAW		2
290 #define INP_EVERY	4
291 
292 
293 #ifdef MULTIUSER
294 struct acl
295 {
296   struct acl *next;
297   char *name;
298 };
299 #endif
300 
301 /* register list */
302 #define MAX_PLOP_DEFS 256
303 
304 struct baud_values
305 {
306   int idx;	/* the index in the bsd-is padding lookup table */
307   int bps;	/* bits per seconds */
308   int sym;	/* symbol defined in ttydev.h */
309 };
310 
311 /*
312  * windowlist orders
313  */
314 #define WLIST_NUM 0
315 #define WLIST_MRU 1
316 #define WLIST_NESTED 2
317