Mercurial > vim
annotate src/pty.c @ 3143:314d9368069e v7.3.342
updated for version 7.3.342
Problem: Code not in Vim style.
Solution: Fix the style. (Elias Diem)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Thu, 20 Oct 2011 18:24:22 +0200 |
parents | 107b03fdf1ad |
children | 7b59c2c032ca |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 /* | |
10 * The stuff in this file mostly comes from the "screen" program. | |
11 * Included with permission from Juergen Weigert. | |
12 * Copied from "pty.c". "putenv.c" was used for putenv() in misc2.c. | |
13 * | |
14 * It has been modified to work better with Vim. | |
15 * The parts that are not used in Vim have been deleted. | |
16 * See the "screen" sources for the complete stuff. | |
17 */ | |
18 | |
19 /* Copyright (c) 1993 | |
20 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de) | |
21 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de) | |
22 * Copyright (c) 1987 Oliver Laumann | |
23 * | |
24 * This program is free software; you can redistribute it and/or modify | |
25 * it under the terms of the GNU General Public License as published by | |
26 * the Free Software Foundation; either version 2, or (at your option) | |
27 * any later version. | |
28 * | |
29 * This program is distributed in the hope that it will be useful, | |
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 * GNU General Public License for more details. | |
33 * | |
34 * You should have received a copy of the GNU General Public License | |
35 * along with this program (see the file COPYING); if not, write to the | |
36 * Free Software Foundation, Inc., | |
37 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
38 */ | |
39 | |
40 #include "vim.h" | |
41 | |
42 #include <signal.h> | |
43 | |
44 #ifdef __CYGWIN32__ | |
45 # include <sys/termios.h> | |
46 #endif | |
47 | |
1030 | 48 #ifdef HAVE_SYS_IOCTL_H |
7 | 49 # include <sys/ioctl.h> |
50 #endif | |
51 | |
52 #if HAVE_STROPTS_H | |
53 #include <sys/types.h> | |
54 #ifdef sinix | |
55 #define buf_T __system_buf_t__ | |
56 #endif | |
57 #include <stropts.h> | |
58 #ifdef sinix | |
59 #undef buf_T | |
60 #endif | |
61 # ifdef sun | |
62 # include <sys/conf.h> | |
63 # endif | |
64 #endif | |
65 | |
1030 | 66 #ifdef HAVE_UNISTD_H |
7 | 67 # include <unistd.h> |
68 #endif | |
69 | |
70 #if HAVE_TERMIO_H | |
71 # include <termio.h> | |
72 #else | |
1030 | 73 # ifdef HAVE_TERMIOS_H |
7 | 74 # include <termios.h> |
75 # endif | |
76 #endif | |
77 | |
78 #if HAVE_SYS_STREAM_H | |
79 # include <sys/stream.h> | |
80 #endif | |
81 | |
82 #if HAVE_SYS_PTEM_H | |
83 # include <sys/ptem.h> | |
84 #endif | |
85 | |
86 #if !defined(sun) && !defined(VMS) && !defined(MACOS) | |
87 # include <sys/ioctl.h> | |
88 #endif | |
89 | |
90 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL) | |
91 # include <sys/ttold.h> | |
92 #endif | |
93 | |
94 #ifdef ISC | |
95 # include <sys/tty.h> | |
96 # include <sys/sioctl.h> | |
97 # include <sys/pty.h> | |
98 #endif | |
99 | |
100 #ifdef sgi | |
101 # include <sys/sysmacros.h> | |
102 #endif | |
103 | |
104 #if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux) | |
105 # define hpux | |
106 #endif | |
107 | |
108 /* | |
109 * if no PTYRANGE[01] is in the config file, we pick a default | |
110 */ | |
111 #ifndef PTYRANGE0 | |
112 # define PTYRANGE0 "qprs" | |
113 #endif | |
114 #ifndef PTYRANGE1 | |
115 # define PTYRANGE1 "0123456789abcdef" | |
116 #endif | |
117 | |
118 /* SVR4 pseudo ttys don't seem to work with SCO-5 */ | |
119 #ifdef M_UNIX | |
120 # undef HAVE_SVR4_PTYS | |
121 #endif | |
122 | |
123 static void initmaster __ARGS((int)); | |
124 | |
125 /* | |
2834 | 126 * Open all ptys with O_NOCTTY, just to be on the safe side. |
7 | 127 */ |
128 #ifndef O_NOCTTY | |
129 # define O_NOCTTY 0 | |
130 #endif | |
131 | |
132 static void | |
133 initmaster(f) | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1703
diff
changeset
|
134 int f UNUSED; |
7 | 135 { |
136 #ifndef VMS | |
137 # ifdef POSIX | |
138 tcflush(f, TCIOFLUSH); | |
139 # else | |
140 # ifdef TIOCFLUSH | |
141 (void)ioctl(f, TIOCFLUSH, (char *) 0); | |
142 # endif | |
143 # endif | |
144 # ifdef LOCKPTY | |
145 (void)ioctl(f, TIOCEXCL, (char *) 0); | |
146 # endif | |
147 #endif | |
148 } | |
149 | |
150 /* | |
151 * This causes a hang on some systems, but is required for a properly working | |
152 * pty on others. Needs to be tuned... | |
153 */ | |
154 int | |
155 SetupSlavePTY(fd) | |
156 int fd; | |
157 { | |
158 if (fd < 0) | |
159 return 0; | |
160 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX) | |
161 # if defined(HAVE_SYS_PTEM_H) || defined(hpux) | |
162 if (ioctl(fd, I_PUSH, "ptem") != 0) | |
163 return -1; | |
164 # endif | |
165 if (ioctl(fd, I_PUSH, "ldterm") != 0) | |
166 return -1; | |
167 # ifdef sun | |
168 if (ioctl(fd, I_PUSH, "ttcompat") != 0) | |
169 return -1; | |
170 # endif | |
171 #endif | |
172 return 0; | |
173 } | |
174 | |
175 | |
176 #if defined(OSX) && !defined(PTY_DONE) | |
177 #define PTY_DONE | |
178 int | |
179 OpenPTY(ttyn) | |
180 char **ttyn; | |
181 { | |
182 int f; | |
183 static char TtyName[32]; | |
184 | |
185 if ((f = open_controlling_pty(TtyName)) < 0) | |
186 return -1; | |
187 initmaster(f); | |
188 *ttyn = TtyName; | |
189 return f; | |
190 } | |
191 #endif | |
192 | |
193 #if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \ | |
194 && !defined(PTY_DONE) | |
195 #define PTY_DONE | |
196 int | |
197 OpenPTY(ttyn) | |
198 char **ttyn; | |
199 { | |
200 char *m, *s; | |
201 int f; | |
202 /* used for opening a new pty-pair: */ | |
203 static char PtyName[32]; | |
204 static char TtyName[32]; | |
205 | |
206 if ((f = getpseudotty(&s, &m)) < 0) | |
207 return -1; | |
208 #ifdef _SEQUENT_ | |
209 fvhangup(s); | |
210 #endif | |
2760 | 211 vim_strncpy((char_u *)PtyName, (char_u *)m, sizeof(PtyName) - 1); |
212 vim_strncpy((char_u *)TtyName, (char_u *)s, sizeof(TtyName) - 1); | |
7 | 213 initmaster(f); |
214 *ttyn = TtyName; | |
215 return f; | |
216 } | |
217 #endif | |
218 | |
219 #if defined(__sgi) && !defined(PTY_DONE) | |
220 #define PTY_DONE | |
221 int | |
222 OpenPTY(ttyn) | |
223 char **ttyn; | |
224 { | |
225 int f; | |
226 char *name; | |
227 RETSIGTYPE (*sigcld)__ARGS(SIGPROTOARG); | |
228 | |
229 /* | |
230 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and | |
231 * exec() /usr/adm/mkpts | |
232 */ | |
233 sigcld = signal(SIGCHLD, SIG_DFL); | |
234 name = _getpty(&f, O_RDWR | O_NONBLOCK | O_EXTRA, 0600, 0); | |
235 signal(SIGCHLD, sigcld); | |
236 | |
237 if (name == 0) | |
238 return -1; | |
239 initmaster(f); | |
240 *ttyn = name; | |
241 return f; | |
242 } | |
243 #endif | |
244 | |
245 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE) | |
246 #define PTY_DONE | |
247 int | |
248 OpenPTY(ttyn) | |
249 char **ttyn; | |
250 { | |
251 int f; | |
252 struct stat buf; | |
253 /* used for opening a new pty-pair: */ | |
254 static char TtyName[32]; | |
255 | |
256 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0) | |
257 return -1; | |
258 if (mch_fstat(f, &buf) < 0) | |
259 { | |
260 close(f); | |
261 return -1; | |
262 } | |
263 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev)); | |
264 initmaster(f); | |
265 *ttyn = TtyName; | |
266 return f; | |
267 } | |
268 #endif | |
269 | |
1703 | 270 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) && !defined(MACOS_X) |
7 | 271 |
1703 | 272 /* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work! |
273 * Same for Mac OS X Leopard. */ | |
7 | 274 #define PTY_DONE |
275 int | |
276 OpenPTY(ttyn) | |
277 char **ttyn; | |
278 { | |
279 int f; | |
2397
d5976fe4349d
Fix for compiler warning about function prototype in pty.c.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
280 char *m; |
d5976fe4349d
Fix for compiler warning about function prototype in pty.c.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
281 char *(ptsname __ARGS((int))); |
d5976fe4349d
Fix for compiler warning about function prototype in pty.c.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
282 int unlockpt __ARGS((int)); |
d5976fe4349d
Fix for compiler warning about function prototype in pty.c.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
283 int grantpt __ARGS((int)); |
7 | 284 RETSIGTYPE (*sigcld)__ARGS(SIGPROTOARG); |
285 /* used for opening a new pty-pair: */ | |
286 static char TtyName[32]; | |
287 | |
288 if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1) | |
289 return -1; | |
290 | |
291 /* | |
292 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and | |
293 * exec()s pt_chmod | |
294 */ | |
295 sigcld = signal(SIGCHLD, SIG_DFL); | |
296 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f)) | |
297 { | |
298 signal(SIGCHLD, sigcld); | |
299 close(f); | |
300 return -1; | |
301 } | |
302 signal(SIGCHLD, sigcld); | |
2760 | 303 vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1); |
7 | 304 initmaster(f); |
305 *ttyn = TtyName; | |
306 return f; | |
307 } | |
308 #endif | |
309 | |
310 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE) | |
311 #define PTY_DONE | |
312 | |
313 #ifdef _IBMR2 | |
314 int aixhack = -1; | |
315 #endif | |
316 | |
317 int | |
318 OpenPTY(ttyn) | |
319 char **ttyn; | |
320 { | |
321 int f; | |
322 /* used for opening a new pty-pair: */ | |
323 static char TtyName[32]; | |
324 | |
325 /* a dumb looking loop replaced by mycrofts code: */ | |
326 if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0) | |
327 return -1; | |
2760 | 328 vim_strncpy((char_u *)TtyName, (char_u *)ttyname(f), sizeof(TtyName) - 1); |
1076 | 329 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK)) |
7 | 330 { |
331 close(f); | |
332 return -1; | |
333 } | |
334 initmaster(f); | |
335 # ifdef _IBMR2 | |
336 if (aixhack >= 0) | |
337 close(aixhack); | |
338 if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0) | |
339 { | |
340 close(f); | |
341 return -1; | |
342 } | |
343 # endif | |
344 *ttyn = TtyName; | |
345 return f; | |
346 } | |
347 #endif | |
348 | |
349 #ifndef PTY_DONE | |
350 | |
351 # ifdef hpux | |
352 static char PtyProto[] = "/dev/ptym/ptyXY"; | |
353 static char TtyProto[] = "/dev/pty/ttyXY"; | |
354 # else | |
355 # ifdef __BEOS__ | |
356 static char PtyProto[] = "/dev/pt/XY"; | |
357 static char TtyProto[] = "/dev/tt/XY"; | |
358 # else | |
359 static char PtyProto[] = "/dev/ptyXY"; | |
360 static char TtyProto[] = "/dev/ttyXY"; | |
361 # endif | |
362 # endif | |
363 | |
364 int | |
365 OpenPTY(ttyn) | |
366 char **ttyn; | |
367 { | |
368 char *p, *q, *l, *d; | |
369 int f; | |
370 /* used for opening a new pty-pair: */ | |
371 static char PtyName[32]; | |
372 static char TtyName[32]; | |
373 | |
374 strcpy(PtyName, PtyProto); | |
375 strcpy(TtyName, TtyProto); | |
376 for (p = PtyName; *p != 'X'; p++) | |
377 ; | |
378 for (q = TtyName; *q != 'X'; q++) | |
379 ; | |
380 for (l = PTYRANGE0; (*p = *l) != '\0'; l++) | |
381 { | |
382 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++) | |
383 { | |
384 #if !defined(MACOS) || defined(USE_CARBONIZED) | |
385 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1) | |
386 #else | |
387 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA)) == -1) | |
388 #endif | |
389 continue; | |
390 q[0] = *l; | |
391 q[1] = *d; | |
392 #ifndef MACOS | |
1076 | 393 if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK)) |
7 | 394 { |
395 close(f); | |
396 continue; | |
397 } | |
398 #endif | |
399 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3) | |
400 /* Hack to ensure that the slave side of the pty is | |
401 * unused. May not work in anything other than SunOS4.1 | |
402 */ | |
403 { | |
404 int pgrp; | |
405 | |
406 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */ | |
407 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO) | |
408 { | |
409 close(f); | |
410 continue; | |
411 } | |
412 } | |
413 #endif | |
414 initmaster(f); | |
415 *ttyn = TtyName; | |
416 return f; | |
417 } | |
418 } | |
419 return -1; | |
420 } | |
421 #endif |