Rearrange the python scripting support a bit.
[screen-lua.git] / src / pty.c
blobdef7f9388195cfaf07db3cca7510204e0ff4b08c
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
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.
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.
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 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <signal.h>
34 #include "config.h"
35 #include "screen.h"
37 #ifndef sun
38 # include <sys/ioctl.h>
39 #endif
41 /* for solaris 2.1, Unixware (SVR4.2) and possibly others */
42 #ifdef HAVE_STROPTS_H
43 # include <sys/stropts.h>
44 #endif
46 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
47 # include <sys/ttold.h>
48 #endif
50 #ifdef ISC
51 # include <sys/tty.h>
52 # include <sys/sioctl.h>
53 # include <sys/pty.h>
54 #endif
56 #ifdef sgi
57 # include <sys/sysmacros.h>
58 #endif /* sgi */
60 #include "extern.h"
63 * if no PTYRANGE[01] is in the config file, we pick a default
65 #ifndef PTYRANGE0
66 # define PTYRANGE0 "qpr"
67 #endif
68 #ifndef PTYRANGE1
69 # define PTYRANGE1 "0123456789abcdef"
70 #endif
72 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
73 #ifdef M_UNIX
74 # undef HAVE_SVR4_PTYS
75 #endif
77 extern int eff_uid;
79 /* used for opening a new pty-pair: */
80 static char PtyName[32], TtyName[32];
82 #if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
83 # ifdef hpux
84 static char PtyProto[] = "/dev/ptym/ptyXY";
85 static char TtyProto[] = "/dev/pty/ttyXY";
86 # else
87 # ifdef M_UNIX
88 static char PtyProto[] = "/dev/ptypXY";
89 static char TtyProto[] = "/dev/ttypXY";
90 # else
91 static char PtyProto[] = "/dev/ptyXY";
92 static char TtyProto[] = "/dev/ttyXY";
93 # endif
94 # endif /* hpux */
95 #endif
97 static void initmaster __P((int));
99 #if defined(sun)
100 /* sun's utmp_update program opens the salve side, thus corrupting
102 int pty_preopen = 1;
103 #else
104 int pty_preopen = 0;
105 #endif
108 * Open all ptys with O_NOCTTY, just to be on the safe side
109 * (RISCos mips breaks otherwise)
111 #ifndef O_NOCTTY
112 # define O_NOCTTY 0
113 #endif
115 /***************************************************************/
117 static void
118 initmaster(f)
119 int f;
121 #ifdef POSIX
122 tcflush(f, TCIOFLUSH);
123 #else
124 # ifdef TIOCFLUSH
125 (void) ioctl(f, TIOCFLUSH, (char *) 0);
126 # endif
127 #endif
128 #ifdef LOCKPTY
129 (void) ioctl(f, TIOCEXCL, (char *) 0);
130 #endif
133 void
134 InitPTY(f)
135 int f;
137 if (f < 0)
138 return;
139 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
140 if (ioctl(f, I_PUSH, "ptem"))
141 Panic(errno, "InitPTY: cannot I_PUSH ptem");
142 if (ioctl(f, I_PUSH, "ldterm"))
143 Panic(errno, "InitPTY: cannot I_PUSH ldterm");
144 # ifdef sun
145 if (ioctl(f, I_PUSH, "ttcompat"))
146 Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
147 # endif
148 #endif
151 /***************************************************************/
153 #if defined(OSX) && !defined(PTY_DONE)
154 #define PTY_DONE
156 OpenPTY(ttyn)
157 char **ttyn;
159 register int f;
160 if ((f = open_controlling_pty(TtyName)) < 0)
161 return -1;
162 initmaster(f);
163 *ttyn = TtyName;
164 return f;
166 #endif
168 /***************************************************************/
170 #if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
171 #define PTY_DONE
173 OpenPTY(ttyn)
174 char **ttyn;
176 char *m, *s;
177 register int f;
179 if ((f = getpseudotty(&s, &m)) < 0)
180 return -1;
181 #ifdef _SEQUENT_
182 fvhangup(s);
183 #endif
184 strncpy(PtyName, m, sizeof(PtyName));
185 strncpy(TtyName, s, sizeof(TtyName));
186 initmaster(f);
187 *ttyn = TtyName;
188 return f;
190 #endif
192 /***************************************************************/
194 #if defined(__sgi) && !defined(PTY_DONE)
195 #define PTY_DONE
197 OpenPTY(ttyn)
198 char **ttyn;
200 int f;
201 char *name, *_getpty();
202 sigret_t (*sigcld)__P(SIGPROTOARG);
205 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
206 * exec() /usr/adm/mkpts
208 sigcld = signal(SIGCHLD, SIG_DFL);
209 name = _getpty(&f, O_RDWR | O_NONBLOCK, 0600, 0);
210 signal(SIGCHLD, sigcld);
212 if (name == 0)
213 return -1;
214 initmaster(f);
215 *ttyn = name;
216 return f;
218 #endif
220 /***************************************************************/
222 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
223 #define PTY_DONE
225 OpenPTY(ttyn)
226 char **ttyn;
228 register int f;
229 struct stat buf;
231 strcpy(PtyName, "/dev/ptc");
232 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
233 return -1;
234 if (fstat(f, &buf) < 0)
236 close(f);
237 return -1;
239 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
240 initmaster(f);
241 *ttyn = TtyName;
242 return f;
244 #endif
246 /***************************************************************/
248 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
249 #define PTY_DONE
251 OpenPTY(ttyn)
252 char **ttyn;
254 register int f;
255 char *m, *ptsname();
256 int unlockpt __P((int)), grantpt __P((int));
257 #if defined(HAVE_GETPT) && defined(linux)
258 int getpt __P((void));
259 #endif
260 sigret_t (*sigcld)__P(SIGPROTOARG);
262 strcpy(PtyName, "/dev/ptmx");
263 #if defined(HAVE_GETPT) && defined(linux)
264 if ((f = getpt()) == -1)
265 #else
266 if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
267 #endif
268 return -1;
271 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
272 * exec()s pt_chmod
274 sigcld = signal(SIGCHLD, SIG_DFL);
275 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
277 signal(SIGCHLD, sigcld);
278 close(f);
279 return -1;
281 signal(SIGCHLD, sigcld);
282 strncpy(TtyName, m, sizeof(TtyName));
283 initmaster(f);
284 *ttyn = TtyName;
285 return f;
287 #endif
289 /***************************************************************/
291 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
292 #define PTY_DONE
295 OpenPTY(ttyn)
296 char **ttyn;
298 register int f;
300 /* a dumb looking loop replaced by mycrofts code: */
301 strcpy (PtyName, "/dev/ptc");
302 if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
303 return -1;
304 strncpy(TtyName, ttyname(f), sizeof(TtyName));
305 if (eff_uid && access(TtyName, R_OK | W_OK))
307 close(f);
308 return -1;
310 initmaster(f);
311 # ifdef _IBMR2
312 pty_preopen = 1;
313 # endif
314 *ttyn = TtyName;
315 return f;
317 #endif
319 /***************************************************************/
321 #if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
322 #define PTY_DONE
324 OpenPTY(ttyn)
325 char **ttyn;
327 int f, s;
328 if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
329 return -1;
330 close(s);
331 initmaster(f);
332 pty_preopen = 1;
333 *ttyn = TtyName;
334 return f;
336 #endif
338 /***************************************************************/
340 #ifndef PTY_DONE
342 OpenPTY(ttyn)
343 char **ttyn;
345 register char *p, *q, *l, *d;
346 register int f;
348 debug("OpenPTY: Using BSD style ptys.\n");
349 strcpy(PtyName, PtyProto);
350 strcpy(TtyName, TtyProto);
351 for (p = PtyName; *p != 'X'; p++)
353 for (q = TtyName; *q != 'X'; q++)
355 for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
357 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
359 debug1("OpenPTY tries '%s'\n", PtyName);
360 if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
361 continue;
362 q[0] = *l;
363 q[1] = *d;
364 if (eff_uid && access(TtyName, R_OK | W_OK))
366 close(f);
367 continue;
369 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
370 /* Hack to ensure that the slave side of the pty is
371 * unused. May not work in anything other than SunOS4.1
374 int pgrp;
376 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
377 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
379 close(f);
380 continue;
383 #endif
384 initmaster(f);
385 *ttyn = TtyName;
386 return f;
389 return -1;
391 #endif