gnome-terminal insists on receiving the selection as COMPOUND_TEXT
[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);
6169c758 95 if (pty_argv)
96 execvp(pty_argv[0], pty_argv);
97 else
98 execl(getenv("SHELL"), getenv("SHELL"), NULL);
054d8535 99 /*
100 * If we're here, exec has gone badly foom.
101 */
102 perror("exec");
103 exit(127);
104 } else {
105 close(slavefd);
106 }
107
1709795f 108 return NULL;
109}
110
111/*
112 * Called to send data down the pty.
113 */
114static int pty_send(char *buf, int len)
115{
054d8535 116 while (len > 0) {
117 int ret = write(pty_master_fd, buf, len);
118 if (ret < 0) {
119 perror("write pty master");
120 exit(1);
121 }
122 buf += ret;
123 len -= ret;
124 }
1709795f 125 return 0;
126}
127
128/*
129 * Called to query the current socket sendability status.
130 */
131static int pty_sendbuffer(void)
132{
133 return 0;
134}
135
136/*
137 * Called to set the size of the window
138 */
139static void pty_size(void)
140{
88e6b9ca 141 struct winsize size;
142
143 size.ws_row = (unsigned short)rows;
144 size.ws_col = (unsigned short)cols;
145 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
1709795f 146 return;
147}
148
149/*
150 * Send special codes.
151 */
152static void pty_special(Telnet_Special code)
153{
154 /* Do nothing! */
155 return;
156}
157
158static Socket pty_socket(void)
159{
160 return NULL; /* shouldn't ever be needed */
161}
162
163static int pty_sendok(void)
164{
165 return 1;
166}
167
168static void pty_unthrottle(int backlog)
169{
170 /* do nothing */
171}
172
173static int pty_ldisc(int option)
174{
175 return 0; /* neither editing nor echoing */
176}
177
178static int pty_exitcode(void)
179{
180 /* Shouldn't ever be required */
181 return 0;
182}
183
184Backend pty_backend = {
185 pty_init,
186 pty_send,
187 pty_sendbuffer,
188 pty_size,
189 pty_special,
190 pty_socket,
191 pty_exitcode,
192 pty_sendok,
193 pty_ldisc,
194 pty_unthrottle,
195 1
196};