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
|
/*
Fvwm command input interface.
Copyright 1996, Toshi Isogai. No guarantees or warantees or anything
are provided. Use this program at your own risk. Permission to use
this program for any purpose is given,
as long as the copyright is kept intact.
*/
#include "FvwmConsole.h"
#define MYVERSION "1.3"
char *MyName;
int Fd[2]; /* pipe to fvwm */
int Ns; /* socket handles */
char Name[80]; /* name of this program in executable format */
char *S_name; /* socket name */
void server( void );
void DeadPipe( int );
void CloseSocket();
void ErrMsg( char *msg );
void SigHandler( int );
int main(int argc, char *argv[]){
char *tmp, *s;
char client[120];
char **eargv;
int i,j,k;
char *xterm_a[] = { "-title", Name, "-name", Name, "-e", NULL, NULL };
int clpid;
/* Why is this not just put in the initializer of xterm_a?
Apparently, it is a non-standard extension to use a non-constant address (of client)
in an initializer (of xterm_a). */
xterm_a[5] = client;
/* Save the program name - its used for error messages and option parsing */
tmp = argv[0];
s=strrchr(argv[0], '/');
if (s != NULL)
tmp = s + 1;
strcpy( Name, tmp );
MyName = safemalloc(strlen(tmp)+2);
strcpy(MyName,"*");
strcat(MyName, tmp);
/* construct client's name */
strcpy( client, argv[0] );
strcat( client, "C" );
if(argc < FARGS) {
fprintf(stderr,"%s Version %s should only be executed by fvwm!\n",MyName,
MYVERSION);
exit(1);
}
if( ( eargv =(char **)safemalloc((argc+12)*sizeof(char *)) ) == NULL ) {
ErrMsg( "allocation" );
}
/* copy arguments */
eargv[0] = XTERM;
j = 1;
for ( i=FARGS ; i<argc; i++ ) {
if( !strcmp ( argv[i], "-e" ) ) {
i++;
break;
} else {
eargv[j++] = argv[i];
}
}
for ( k=0 ; xterm_a[k] != NULL ; j++, k++ ) {
eargv[j] = xterm_a[k];
}
/* copy rest of -e args */
for( ; i<argc; i++, j++ ) {
eargv[j-1] = argv[i];
}
eargv[j] = NULL;
/* Dead pipes mean fvwm died */
signal (SIGPIPE, DeadPipe);
signal (SIGINT, SigHandler);
signal (SIGQUIT, SigHandler);
Fd[0] = atoi(argv[1]);
Fd[1] = atoi(argv[2]);
/* launch xterm with client */
clpid = fork();
if( clpid < 0) {
ErrMsg("client forking");
}else if(clpid == 0 ) {
execvp( *eargv, eargv );
ErrMsg("exec");
}
server();
return (0);
}
/***********************************************************************
* signal handler
***********************************************************************/
void DeadPipe( int dummy ) {
fprintf(stderr,"%s: dead pipe\n", Name);
CloseSocket();
exit(0);
}
void SigHandler(int dummy) {
CloseSocket();
exit(1);
}
/*********************************************************/
/* close sockets and spawned process */
/*********************************************************/
void CloseSocket() {
send(Ns, C_CLOSE, strlen(C_CLOSE), 0);
close(Ns); /* remove the socket */
unlink( S_name );
}
/*********************************************************/
/* setup server and communicate with fvwm and the client */
/*********************************************************/
void server ( void ) {
struct sockaddr_un sas, csas;
int len, clen; /* length of sockaddr */
char buf[MAX_COMMAND_SIZE]; /* command line buffer */
char *tline;
char ver[40];
fd_set fdset;
unsigned long *body;
unsigned long header[HEADER_SIZE];
char *home;
int s;
int msglen;
/* make a socket */
if( (s = socket(AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
ErrMsg( "socket");
exit(1);
}
/* name the socket */
home = getenv("HOME");
S_name = safemalloc( strlen(home)+ 16);
strcpy(S_name,home);
strcat(S_name,S_NAME);
sas.sun_family = AF_UNIX;
strcpy( sas.sun_path, S_name );
/* bind the above name to the socket */
/* first, erase the old socket */
unlink( S_name );
len = sizeof(sas) - sizeof( sas.sun_path) + strlen( sas.sun_path );
if( bind(s, (struct sockaddr *)&sas,len) < 0 ) {
ErrMsg( "bind" );
exit(1);
}
/* listen to the socket */
/* set backlog to 5 */
if ( listen(s,5) < 0 ) {
ErrMsg( "listen" );
exit(1);
}
/* accept connections */
clen = sizeof(csas);
if(( Ns = accept(s, (struct sockaddr *)&csas, &clen)) < 0 ) {
ErrMsg( "accept");
exit(1);
}
/* send config lines to Client */
tline = NULL;
send(Ns, C_BEG, strlen(C_BEG), 0);
GetConfigLine(Fd,&tline);
while(tline != NULL) {
if(strlen(tline)>1) {
send(Ns, tline, strlen(tline),0);
}
GetConfigLine(Fd,&tline);
}
send(Ns, C_END, strlen(C_END), 0);
strcpy( ver, MyName);
strcat( ver, " Ver. " );
strcat( ver, MYVERSION);
strcat( ver, "\n" );
send(Ns, ver, strlen(ver), 0 );
while (1){
FD_ZERO(&fdset);
FD_SET(Ns, &fdset);
FD_SET(Fd[1], &fdset);
select(FD_SETSIZE,SELECT_TYPE_ARG234 &fdset, 0, 0, NULL);
if (FD_ISSET(Fd[1], &fdset)){
if( ReadFvwmPacket(Fd[1],header,&body) > 0) {
if(header[1] == M_PASS) {
msglen = strlen((char *)&body[3]);
if( msglen > MAX_MESSAGE_SIZE-2 ) {
msglen = MAX_MESSAGE_SIZE-2;
}
send( Ns, (char *)&body[3], msglen, 0 );
}
free(body);
}
}
if (FD_ISSET(Ns, &fdset)){
if( recv( Ns, buf, MAX_COMMAND_SIZE,0 ) == 0 ) {
/* client is terminated */
break;
}
/* process the own unique commands */
SendText(Fd,buf,0); /* send command */
}
}
CloseSocket();
exit(0);
}
/******************************************/
/* print error message on stderr and exit */
/******************************************/
void ErrMsg( char *msg ) {
fprintf( stderr, "%s server error in %s, errno %d\n", Name, msg, errno );
CloseSocket();
exit(1);
}
|