File: main-con.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (337 lines) | stat: -rw-r--r-- 7,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> /* getenv */
#include <unistd.h>
#include <fcntl.h>      /* open */
#include <sys/select.h> /* select */
#include <errno.h>
#include <string.h> /* memcpy */
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>

#if defined(__CYGWIN__) || defined(__MSYS__)
#ifndef BINDIR
#define BINDIR "/bin"
#endif
#define MLLIBEXECDIR BINDIR
#else
#ifndef LIBEXECDIR
#define LIBEXECDIR "/usr/local/libexec"
#endif
#define MLLIBEXECDIR LIBEXECDIR "/mlterm"
#endif

/* --- static variables --- */

static int connect_fd = -1;
static int pty_fd = -1;
static struct termios std_tio;

/* --- static functions --- */

static void exit_cb(void) {
  tcsetattr(pty_fd, TCSANOW, &std_tio);
}

static void sig_child(int sig) { waitpid(-1, NULL, WNOHANG); }

static void help(void) { printf("Usage: mlclient-con [options]\n"); }

static char *get_path(char *file) {
  char *home;
  char *path;
  char *p;

  if (!(home = getenv("HOME")) || !(path = malloc(strlen(home) + 16 + strlen(file) + 1))) {
    return NULL;
  }

  sprintf(path, "%s/.config/mlterm/%s", home, file);
  p = strrchr(path, '/');
  if (p > path + strlen(home) + 15) {
    struct stat st;

    *p = '\0';
    if (stat(path, &st) == 0) {
      *p = '/';

      return path;
    }
  }

  sprintf(path, "%s/.mlterm/%s", home, file);

  return path;
}

static void debug_printf(const char *format, ...) {
  va_list arg_list;
  static char *path;
  FILE* fp;

  va_start(arg_list, format);

  if (!path && !(path = get_path("msg.log"))) {
    return;
  }

  if ((fp = fopen(path, "a"))) {
    vfprintf(fp, format, arg_list);
    fclose(fp);
  }
}

static void sig_winch(int sig) {
  struct winsize ws;

  if (ioctl(pty_fd, TIOCGWINSZ, &ws) == 0) {
    char msg[10 + 11 * 4 + 1];

    sprintf(msg, "\x1b[8;%d;%d;4;%d;%dt", ws.ws_row, ws.ws_col, ws.ws_ypixel, ws.ws_xpixel);
    write(connect_fd, msg, strlen(msg));
  }

  signal(SIGWINCH, sig_winch);
}

static int set_cloexec(int fd) {
  int old_flags;

  if ((old_flags = fcntl(fd, F_GETFD)) == -1) {
    return 0;
  }

  if (!(old_flags & FD_CLOEXEC) && fcntl(fd, F_SETFD, old_flags | FD_CLOEXEC) == -1) {
    return 0;
  }

  return 1;
}

static int connect_to_server(int argc, char **argv) {
  char *path;
  struct sockaddr_un servaddr;
  int count;
  struct termios tio;

  if (!(path = get_path("socket-con"))) {
    return 0;
  }

  if ((connect_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
    free(path);

    return 0;
  }

  set_cloexec(connect_fd);

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sun_family = AF_LOCAL;
  strcpy(servaddr.sun_path, path);
  free(path);
  path = servaddr.sun_path;

  fcntl(connect_fd, F_SETFL, fcntl(connect_fd, F_GETFL, 0) & ~O_NONBLOCK);

  if (connect(connect_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
    pid_t pid;

    unlink(path);

    signal(SIGCHLD, sig_child);

    if ((pid = fork()) == 0) {
      char **new_argv;

      if (!(new_argv = alloca((argc + 2 + 1) * sizeof(char*)))) {
        exit(1);
      }

      /* Remove command path */
      argv++;
      argc--;

      count = 0;
      new_argv[count++] = "mlterm-con-server";
      new_argv[count++] = "-j";
      new_argv[count++] = "genuine";
      for (; count < argc + 3; count++) {
        new_argv[count] = argv[count - 3];
      }
      new_argv[count] = NULL;

      execv(MLLIBEXECDIR "/mlterm-con-server", new_argv);
      exit(-1);
    }

    if (pid < 0) {
      close(connect_fd);

      return 0;
    }

    for (count = 0; count < 100; count++) {
      struct stat st;

      usleep(10000); /* waiting for mlterm-con-server to call bind(). */
      if (stat(path, &st) == 0) {
        /* XXX Following processing is for Oracle Solaris 11. */
        usleep(50000); /* waiting for mlterm-con-server to become a daemon and to call listen(). */
        if (connect(connect_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == 0) {
          goto connected;
        }
        break;
      }
    }

    close(connect_fd);

    return 0;
  }

connected:
  if ((pty_fd = open(ttyname(STDIN_FILENO), O_RDWR)) == -1) {
    close(connect_fd);

    return 0;
  }

  set_cloexec(pty_fd);

  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);

  tio = std_tio;
#ifdef IMAXBEL
  tio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
#else
  /* IMAXBEL is not defined on HaikuOS */
  tio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | ISTRIP);
#endif
  tio.c_iflag |= IGNBRK;
  tio.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONLRET);
#ifdef ECHOPRT
  tio.c_lflag &=
      ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ECHOCTL | ISIG);
#else
  /* ECHOPRT is not defined on cygwin. */
  tio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOKE | ECHOCTL | ISIG);
#endif
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 0;

  tcsetattr(pty_fd, TCSANOW, &tio);

  atexit(exit_cb);

  write(connect_fd, argv[0], strlen(argv[0]));
  for (count = 1; count < argc; count++) {
    write(connect_fd, " \"", 2);
    write(connect_fd, argv[count], strlen(argv[count]));
    write(connect_fd, "\"", 1);
  }
  write(connect_fd, "\n", 1);

  sig_winch(SIGWINCH);

  return 1;
}

/* --- global functions --- */

int main(int argc, char **argv) {
  fd_set fds;
  int maxfd;

  if (argc == 2 && strcmp(argv[1], "-h") == 0) {
    help();

    return 0;
  }

  tcgetattr(STDIN_FILENO, &std_tio);

  if (!connect_to_server(argc, argv)) {
    debug_printf("Failed to connect to mlterm-con-server.\n");
    return -1;
  }

  while (1) {
    char buf[1024];
    ssize_t len;

    FD_ZERO(&fds);

    FD_SET(pty_fd, &fds);
    FD_SET(connect_fd, &fds);

    if (pty_fd > connect_fd) {
      maxfd = pty_fd;
    } else {
      maxfd = connect_fd;
    }

    if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
      /* received signal */
      continue;
    }

    if (FD_ISSET(pty_fd, &fds)) {
      fcntl(pty_fd, F_SETFL, fcntl(pty_fd, F_GETFL, 0) | O_NONBLOCK);

      while ((len = read(pty_fd, buf, sizeof(buf))) > 0) {
        /* XXX */
        if (buf[0] == 0x7f && std_tio.c_cc[VERASE] == 0x7f) {
          /* Convert to BackSpace */
          buf[0] = 0x08;
        }

        write(connect_fd, buf, len);
      }

#if 0
      if (len == -1 && errno != EAGAIN) {
        break;
      }
#endif

      fcntl(pty_fd, F_SETFL, fcntl(pty_fd, F_GETFL, 0) & ~O_NONBLOCK);
    }

    if (FD_ISSET(connect_fd, &fds)) {
      fcntl(connect_fd, F_SETFL, fcntl(connect_fd, F_GETFL, 0) | O_NONBLOCK);

      while ((len = read(connect_fd, buf, sizeof(buf))) > 0) {
        write(pty_fd, buf, len);
      }

      /*
       * XXX
       * Even if read() has no problem, len is -1 and errno is ECONNRESET on fedora 24,
       * mlterm-con exits by SIGPIPE.
       */
#if 0
      if (len == -1 && errno != EAGAIN) {
        break;
      }
#endif

      fcntl(connect_fd, F_SETFL, fcntl(connect_fd, F_GETFL, 0) & ~O_NONBLOCK);
    }
  }

  tcsetattr(pty_fd, TCSANOW, &std_tio);

  return 0;
}