1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, see: <http://www.gnu.org/licenses/>
14  */
15 
16 /* FIXME: The signal handling of this module is largely broken */
17 
18 #include "config.h"
19 
20 #include "FvwmConsole.h"
21 #include "libs/fio.h"
22 
23 int  s;    /* socket handle */
24 FILE *sp;
25 char *name;  /* name of this program at executing time */
26 char *get_line(void);
27 
28 /*
29  *  close socket and exit
30  */
sclose(int foo)31 void sclose(int foo)
32 {
33 	if (sp != NULL)
34 	{
35 		fclose(sp);
36 		sp = NULL;
37 	}
38 	exit(0);
39 
40 	SIGNAL_RETURN;
41 }
42 
ReapChildren(int sig)43 RETSIGTYPE ReapChildren(int sig)
44 {
45 	fvwmReapChildren(sig);
46 	sclose(sig);
47 
48 	SIGNAL_RETURN;
49 }
50 
51 /*
52  * print error message on stderr
53  */
ErrMsg(char * msg)54 void ErrMsg(char *msg)
55 {
56 	fvwm_debug(__func__, "%s error in %s: %s\n", name , msg,
57 		   strerror(errno));
58 	if (sp != NULL)
59 	{
60 		fclose(sp);
61 		sp = NULL;
62 	}
63 	exit(1);
64 }
65 
66 /*
67  * setup socket.
68  * send command to and receive message from the server
69  */
main(int argc,char * argv[])70 int main(int argc, char *argv[])
71 {
72 	char *cmd;
73 	char data[MAX_MESSAGE_SIZE];
74 	int  len;  /* length of socket address */
75 	struct sockaddr_un sas;
76 	int  clen; /* command length */
77 	int  pid;  /* child process id */
78 	char *home;
79 	char *s_name;
80 	int rc;
81 
82 	signal(SIGCHLD, ReapChildren);
83 	signal(SIGPIPE, sclose);
84 	signal(SIGINT, sclose);
85 	signal(SIGQUIT, sclose);
86 
87 	name=strrchr(argv[0], '/');
88 	if (name != NULL)
89 	{
90 		name++;
91 	}
92 
93 	/* make a socket */
94 	home = getenv("FVWM_USERDIR");
95 	s_name = fxmalloc(strlen(home) + sizeof(S_NAME) + 1);
96 	strcpy(s_name, home);
97 	strcat(s_name, S_NAME);
98 	s = socket(AF_UNIX, SOCK_STREAM, 0);
99 	if (s < 0)
100 	{
101 		ErrMsg ("socket");
102 	}
103 	/* name the socket and obtain the size of it*/
104 	sas.sun_family = AF_UNIX;
105 	strcpy(sas.sun_path, s_name);
106 	len = sizeof(sas) - sizeof(sas.sun_path) + strlen(sas.sun_path);
107 	rc = connect(s, (struct sockaddr *)&sas, len);
108 	if (rc < 0)
109 	{
110 		ErrMsg("connect");
111 	}
112 	sp = fdopen(s, "r");
113 	if (sp == NULL)
114 	{
115 		ErrMsg("fdopen");
116 	}
117 	pid = fork();
118 	if (pid == -1)
119 	{
120 		ErrMsg("fork");
121 	}
122 	if (pid == 0)
123 	{
124 		/* loop of get user's command and send it to server */
125 		while (1)
126 		{
127 			cmd = get_line();
128 			if (cmd == NULL)
129 			{
130 				break;
131 			}
132 			clen = strlen(cmd);
133 			if (clen == 1)
134 			{
135 				/* empty line */
136 				continue;
137 			}
138 			/* send the command including null to the server */
139 			usleep(1);
140 			fvwm_send(s, cmd, strlen(cmd) + 1, 0);
141 		}
142 		kill(getppid(), SIGKILL);
143 		sclose(0);
144 	}
145 	while (fgets(data, MAX_MESSAGE_SIZE, sp))
146 	{
147 		/* get the response */
148 		/* ignore config lines */
149 		if (!strcmp(data, C_BEG))
150 		{
151 			while (fgets(data, MAX_MESSAGE_SIZE, sp))
152 			{
153 				if (*data == '\0' || !strcmp(data,C_END))
154 				{
155 					break;
156 				}
157 			}
158 			if (*data != '\0')
159 			{
160 				continue;
161 			}
162 		}
163 		if (*data == '\0')
164 		{
165 			break;
166 		}
167 		printf("%s",data);
168 	}
169 
170 	return 0;
171 }
172