/* 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. */ /* 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "FvwmConsole.h" 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_pre[] = { "-title", Name, "-name", Name, NULL }; char *xterm_post[] = { "-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_post[1] = 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, VERSION); exit(1); } if( ( eargv =(char **)safemalloc((argc+12)*sizeof(char *)) ) == NULL ) { ErrMsg( "allocation" ); } /* copy arguments */ eargv[0] = XTERM; j = 1; for ( k=0 ; xterm_pre[k] != NULL ; j++, k++ ) { eargv[j] = xterm_pre[k]; } for ( i=FARGS ; i1) { send(Ns, tline, strlen(tline),0); send(Ns, "\n", 1, 0); } GetConfigLine(Fd,&tline); } send(Ns, C_END, strlen(C_END), 0); strcpy( ver, MyName); strcat( ver, " version " ); strcat( ver, VERSION); 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_FD_SET_CAST &fdset, 0, 0, NULL); if (FD_ISSET(Fd[1], &fdset)){ FvwmPacket* packet = ReadFvwmPacket(Fd[1]); if ( packet == NULL ) { CloseSocket(); exit(0); } else { if (packet->type == M_PASS) { msglen = strlen((char *)&(packet->body[3])); if ( msglen > MAX_MESSAGE_SIZE-2 ) { msglen = MAX_MESSAGE_SIZE-2; } send( Ns, (char *)&(packet->body[3]), msglen, 0 ); } } } if (FD_ISSET(Ns, &fdset)){ if( recv( Ns, buf, MAX_COMMAND_SIZE,0 ) == 0 ) { /* client is terminated */ close(Ns); unlink(S_name); exit(0); } /* process the own unique commands */ SendText(Fd,buf,0); /* send command */ } } } /******************************************/ /* 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); }