Window title configurability: -T to set it from the command line,
[u/mdw/putty] / unix / pty.c
CommitLineData
054d8535 1#define _XOPEN_SOURCE
88e6b9ca 2#define _XOPEN_SOURCE_EXTENDED
054d8535 3#include <features.h>
4
1709795f 5#include <stdio.h>
6#include <stdlib.h>
054d8535 7#include <string.h>
8#include <unistd.h>
9#include <fcntl.h>
88e6b9ca 10#include <termios.h>
11#include <sys/ioctl.h>
1709795f 12
13#include "putty.h"
14
15#ifndef FALSE
16#define FALSE 0
17#endif
18#ifndef TRUE
19#define TRUE 1
20#endif
21
054d8535 22int pty_master_fd;
6169c758 23char **pty_argv;
054d8535 24
1709795f 25static void pty_size(void);
26
1709795f 27/*
28 * Called to set up the pty.
29 *
30 * Returns an error message, or NULL on success.
31 *
32 * Also places the canonical host name into `realhost'. It must be
33 * freed by the caller.
34 */
35static char *pty_init(char *host, int port, char **realhost, int nodelay)
36{
054d8535 37 int slavefd;
38 char name[FILENAME_MAX];
39 pid_t pid;
40
41 pty_master_fd = open("/dev/ptmx", O_RDWR);
42
43 if (pty_master_fd < 0) {
44 perror("/dev/ptmx: open");
45 exit(1);
46 }
47
48 if (grantpt(pty_master_fd) < 0) {
49 perror("grantpt");
50 exit(1);
51 }
52
53 if (unlockpt(pty_master_fd) < 0) {
54 perror("unlockpt");
55 exit(1);
56 }
57
58 name[FILENAME_MAX-1] = '\0';
59 strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
60
61 slavefd = open(name, O_RDWR);
62 if (slavefd < 0) {
63 perror("slave pty: open");
88e6b9ca 64 exit(1);
054d8535 65 }
66
67 /*
68 * Fork and execute the command.
69 */
70 pid = fork();
71 if (pid < 0) {
72 perror("fork");
88e6b9ca 73 exit(1);
054d8535 74 }
75
76 if (pid == 0) {
77 int i;
78 /*
79 * We are the child.
80 */
81 close(pty_master_fd);
82 close(0);
83 close(1);
84 close(2);
85 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
86 dup2(slavefd, 0);
87 dup2(slavefd, 1);
88 dup2(slavefd, 2);
89 setsid();
90 setpgrp();
6169c758 91 tcsetpgrp(slavefd, getpgrp());
054d8535 92 /* Close everything _else_, for tidiness. */
93 for (i = 3; i < 1024; i++)
94 close(i);
fe9548aa 95 {
96 char term_env_var[10 + sizeof(cfg.termtype)];
97 sprintf(term_env_var, "TERM=%s", cfg.termtype);
98 putenv(term_env_var);
99 }
6169c758 100 if (pty_argv)
101 execvp(pty_argv[0], pty_argv);
102 else
103 execl(getenv("SHELL"), getenv("SHELL"), NULL);
054d8535 104 /*
105 * If we're here, exec has gone badly foom.
106 */
107 perror("exec");
108 exit(127);
109 } else {
110 close(slavefd);
111 }
112
1709795f 113 return NULL;
114}
115
116/*
117 * Called to send data down the pty.
118 */
119static int pty_send(char *buf, int len)
120{
054d8535 121 while (len > 0) {
122 int ret = write(pty_master_fd, buf, len);
123 if (ret < 0) {
124 perror("write pty master");
125 exit(1);
126 }
127 buf += ret;
128 len -= ret;
129 }
1709795f 130 return 0;
131}
132
133/*
134 * Called to query the current socket sendability status.
135 */
136static int pty_sendbuffer(void)
137{
138 return 0;
139}
140
141/*
142 * Called to set the size of the window
143 */
144static void pty_size(void)
145{
88e6b9ca 146 struct winsize size;
147
148 size.ws_row = (unsigned short)rows;
149 size.ws_col = (unsigned short)cols;
150 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
1709795f 151 return;
152}
153
154/*
155 * Send special codes.
156 */
157static void pty_special(Telnet_Special code)
158{
159 /* Do nothing! */
160 return;
161}
162
163static Socket pty_socket(void)
164{
165 return NULL; /* shouldn't ever be needed */
166}
167
168static int pty_sendok(void)
169{
170 return 1;
171}
172
173static void pty_unthrottle(int backlog)
174{
175 /* do nothing */
176}
177
178static int pty_ldisc(int option)
179{
180 return 0; /* neither editing nor echoing */
181}
182
183static int pty_exitcode(void)
184{
185 /* Shouldn't ever be required */
186 return 0;
187}
188
189Backend pty_backend = {
190 pty_init,
191 pty_send,
192 pty_sendbuffer,
193 pty_size,
194 pty_special,
195 pty_socket,
196 pty_exitcode,
197 pty_sendok,
198 pty_ldisc,
199 pty_unthrottle,
200 1
201};