File: attach.c

package info (click to toggle)
twin 0.4.0-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,804 kB
  • ctags: 23,904
  • sloc: ansic: 61,860; cpp: 1,023; makefile: 777; sh: 552; lex: 302; yacc: 231
file content (289 lines) | stat: -rw-r--r-- 7,806 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
/*
 *  attach.c  --  connect to a running twin and tell it to attach/detach
 *                from a given display
 *
 *  Copyright (C) 2000 by Massimiliano Ghilardi
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 */

#include <stdio.h>
#include <string.h>
#include <signal.h>

#include "Tw/Tw.h"
#include "Tw/Twerrno.h"
#include "version.h"

static char *MYname;

static void Usage(byte detach) {
    fprintf(stderr, "Usage: %s [OPTIONS] %s\n"
	    "Currently known options: \n"
	    " -h, -help               display this help and exit\n"
	    " -V, -version            output version information and exit\n"
	    " -a                      attach display%s\n"
	    " -d                      detach display%s\n"
	    " -s, -share              allow multiple simultaneous displays (default)\n"
	    " -x, -excl               request exclusive display - detach all others\n"
	    " -v                      verbose output (default)\n"
	    " -q                      quiet - don't report messages from twin server\n"
	    " -f                      force running even with wrong protocol version\n"
	    " -twin@<TWDISPLAY>       specify server to contact instead of $TWDISPLAY\n"
	    " -hw=<display>[,options] start the given display\n"
	    "Currently known display methods: \n"
	    "\tX[@<XDISPLAY>]\n"
	    "\ttwin[@<TWDISPLAY>]\n"
	    "\ttty[@<tty device>]\n"
	    "\tggi[@<ggi display>]\n",
	    MYname, detach ? "" : "-hw=<display> [...]",
	    detach ? "" : " (default)", detach ? " (default)" : "");
}

static TW_VOLATILE byte gotSignals, gotSignalWinch, gotSignalPanic;

#if TW_RETSIGTYPE == void
# define TW_RETFROMSIGNAL
#else
# define TW_RETFROMSIGNAL return 0;
#endif

static TW_RETSIGTYPE SignalWinch(int n) {
    signal(SIGWINCH, SignalWinch);
    gotSignals = gotSignalWinch = TRUE;
    TW_RETFROMSIGNAL
}

static TW_RETSIGTYPE SignalPanic(int n) {
    signal(n, SIG_IGN);
    gotSignals = gotSignalPanic = TRUE;
    TW_RETFROMSIGNAL
}

static void ShowVersion(void) {
    printf("%s " TWIN_VERSION_STR " with socket protocol "
	  TW_PROTOCOL_VERSION_STR "\n", MYname);
}

static byte VersionsMatch(byte force) {
    uldat cv = TW_PROTOCOL_VERSION, lv = TwLibraryVersion(), sv = TwServerVersion();
	
    if (lv != sv || lv != cv) {
	fprintf(stderr, "%s: %s: socket protocol version mismatch!%s\n"
		"          client is %d.%d.%d, library is %d.%d.%d, server is %d.%d.%d\n",
		MYname, (force ? "warning" : "fatal"), (force ? " (ignored)" : ""),
		TWVER_MAJOR(cv), TWVER_MINOR(cv), TWVER_PATCH(cv),
		TWVER_MAJOR(lv), TWVER_MINOR(lv), TWVER_PATCH(lv),
		TWVER_MAJOR(sv), TWVER_MINOR(sv), TWVER_PATCH(sv));
	return FALSE;
    }
    return TRUE;
}

static void InitSignals(void) {
    signal(SIGWINCH,SignalWinch);
    signal(SIGCHLD, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGIO,   SIG_IGN);
#ifndef DONT_TRAP_SIGNALS
    signal(SIGHUP,  SignalPanic);
    signal(SIGINT,  SignalPanic);
    signal(SIGQUIT, SignalPanic);
    signal(SIGILL,  SignalPanic);
    signal(SIGABRT, SignalPanic);
    signal(SIGBUS,  SignalPanic);
    signal(SIGFPE,  SignalPanic);
    signal(SIGSEGV, SignalPanic);
    signal(SIGTERM, SignalPanic);
    signal(SIGXCPU, SignalPanic);
    signal(SIGXFSZ, SignalPanic);
# ifdef SIGPWR
    signal(SIGPWR,  SignalPanic);
# endif
#endif
}

TW_DECL_MAGIC(attach_magic);

int main(int argc, char *argv[]) {
    byte detach = 0, redirect, force = 0, flags = TW_ATTACH_HW_REDIRECT;
    byte *dpy = NULL, *arg = NULL, *tty = ttyname(0);
    byte ret = 0, ourtty = 0, servtty = 0;
    byte *s;
    TW_CONST byte *buff;
    uldat chunk;
    
    MYname = argv[0];
    
    if (strstr(argv[0], "detach"))
	detach = 1;

    while (*++argv) {
	if (!strcmp(*argv, "-V") || !strcmp(*argv, "-version")) {
	    ShowVersion();
	    return 0;
	} else if (!strcmp(*argv, "-h") || !strcmp(*argv, "-help")) {
	    Usage(detach);
	    return 0;
	} else if (!strcmp(*argv, "-x") || !strcmp(*argv, "-excl"))
	    flags |= TW_ATTACH_HW_EXCLUSIVE;
	else if (!strcmp(*argv, "-s") || !strcmp(*argv, "-share"))
	    flags &= ~TW_ATTACH_HW_EXCLUSIVE;
	else if (!strcmp(*argv, "-a"))
	    detach = 0;
	else if (!strcmp(*argv, "-d"))
	    detach = 1;
	else if (!strcmp(*argv, "-v"))
	    flags |= TW_ATTACH_HW_REDIRECT;
	else if (!strcmp(*argv, "-q"))
	    flags &= ~TW_ATTACH_HW_REDIRECT;
	else if (!strcmp(*argv, "-f"))
	    force = 1;
	else if (!strncmp(*argv, "-twin@", 6))
	    dpy = *argv + 6;
	else if (!strncmp(*argv, "-hw=", 4)) {
	    if (!strncmp(*argv+4, "tty", 3)) {
		buff = *argv + 7;
		s = strchr(buff, ',');
		if (s) *s = '\0';
		
		if (!*buff)
		    /* attach twin to our tty */
		    ourtty = 1;
		else if (*buff == '@' && buff[1]) {
		    if (buff[1] == '-') {
			/* tell twin to attach to its tty */
			servtty = 1;
		    } else if (tty) {
			if (!strcmp(buff+1, tty))
			    /* attach twin to our tty */
			    ourtty = 1;
		    } else {
			fprintf(stderr, "%s: ttyname() failed, cannot find controlling tty!\n", MYname);
			return 1;
		    }
		} else {
		    fprintf(stderr, "%s: malformed display hw `%s'\n", MYname, *argv);
		    return 1;
		}
		
		if (s) *s = ',';
		else s = "";
		
		if (ourtty) {
		    buff = getenv("TERM");
		    if (!buff) buff = "";
		    
		    arg = malloc(strlen(tty) + 9 + strlen(s) + (buff ? 6 + strlen(buff) : 0));
		    
		    sprintf(arg, "-hw=tty@%s%s%s%s", tty, (buff ? (byte *)",TERM=" : buff), buff, s);
		} else if (servtty) {
		    arg = malloc(8 + strlen(s));
		    sprintf(arg, "-hw=tty%s", s);
		} else
		    arg = *argv;
	    } else if ((*argv)[4])
		arg = *argv;
	    else {
		Usage(detach);
		return 1;
	    }
	} else {
	    Usage(detach);
	    return 1;
	}
    }
    
    if (detach == 0 && !arg) {
	Usage(detach);
	return 1;
    }
    
    redirect = flags & TW_ATTACH_HW_REDIRECT;

    InitSignals();

    if (TwCheckMagic(attach_magic) && TwOpen(dpy) && TwCreateMsgPort(8, "Twattach", 0, 0, 0)) do {
	
	if (!VersionsMatch(force)) {
	    if (!force) {
		fprintf(stderr, "%s: Aborting. Use option `-f' to ignore versions check.\n", MYname);
		TwClose();
		return 1;
	    }
	}

	if (detach) {
	    return !TwDetachHW(arg ? strlen(arg) : 0, arg);
	}

	TwAttachHW(arg ? strlen(arg) : 0, arg, flags);
	TwFlush();
	
	if (redirect)
	    fprintf(stderr, "reported messages...\n");
	
	for (;;) {
	    buff = TwAttachGetReply(&chunk);
	    if (buff <= (byte *)2) {
		ret = (byte)(size_t)buff;
		break;
	    } else if (buff == (byte *)-1)
		/* libTw panic */
		break;

	    fprintf(stderr, "%.*s", (int)chunk, buff);
	}
	fflush(stderr);
	
	if (TwInPanic())
	    break;

	if (ourtty) {
	    fputs("\033[2J", stdout);
	    fflush(stdout);
	}
	
	/*
	 * twin waits this before grabbing the display...
	 * so we can fflush(stdout) to show all messages
	 * *BEFORE* twin draws on (eventually) the same tty
	 */
	TwAttachConfirm();
    
	if (ret == 2) {
	    /*
	     * twin told us to stay and sit on the display
	     * until it is quitted.
	     */
	    int fd = TwConnectionFd();
	    fd_set fds;
	    FD_ZERO(&fds);
	    
	    while (!gotSignalPanic && !TwInPanic()) {
		while (TwReadMsg(FALSE))
		    ;
		FD_SET(fd, &fds);
		select(fd+1, &fds, NULL, NULL, NULL);
		if (gotSignalWinch)
		    TwNeedResizeDisplay(), TwFlush(), gotSignalWinch = FALSE;
	    }
	} else if (redirect) {
	    if (ret)
		fprintf(stderr, "... ok, twin successfully attached.\n");
	    else
		fprintf(stderr, "... ach, twin failed to attach.\n");
	}
	return !ret;
    } while (0);
    
    chunk = TwErrno;
    fprintf(stderr, "%s: libTw error: %s%s\n", MYname,
	    TwStrError(chunk), TwStrErrorDetail(chunk, TwErrnoDetail));
    return 1;
}