cvsimport
[fvwm.git] / modules / FvwmConsole / FvwmConsoleC.c
blobe6628e9d4729d5443f8889cbd72d6ec237522ac0
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.
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.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* FIXME: The signal handling of this module is largely broken */
19 #include "config.h"
21 #include "FvwmConsole.h"
22 #include "libs/fio.h"
24 int s; /* socket handle */
25 FILE *sp;
26 char *name; /* name of this program at executing time */
27 char *get_line(void);
30 * close socket and exit
32 void sclose(int foo)
34 if (sp != NULL)
36 fclose(sp);
37 sp = NULL;
39 exit(0);
41 SIGNAL_RETURN;
44 RETSIGTYPE ReapChildren(int sig)
46 fvwmReapChildren(sig);
47 sclose(sig);
49 SIGNAL_RETURN;
53 * print error message on stderr
55 void ErrMsg(char *msg)
57 fprintf(stderr, "%s error in %s: %s\n", name , msg, strerror(errno));
58 if (sp != NULL)
60 fclose(sp);
61 sp = NULL;
63 exit(1);
67 * setup socket.
68 * send command to and receive message from the server
70 int main(int argc, char *argv[])
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;
82 signal(SIGCHLD, ReapChildren);
83 signal(SIGPIPE, sclose);
84 signal(SIGINT, sclose);
85 signal(SIGQUIT, sclose);
87 name=strrchr(argv[0], '/');
88 if (name != NULL)
90 name++;
93 /* make a socket */
94 home = getenv("FVWM_USERDIR");
95 s_name = safemalloc(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)
101 ErrMsg ("socket");
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)
110 ErrMsg("connect");
112 sp = fdopen(s, "r");
113 if (sp == NULL)
115 ErrMsg("fdopen");
117 pid = fork();
118 if (pid == -1)
120 ErrMsg("fork");
122 if (pid == 0)
124 /* loop of get user's command and send it to server */
125 while (1)
127 cmd = get_line();
128 if (cmd == NULL)
130 break;
132 clen = strlen(cmd);
133 if (clen == 1)
135 /* empty line */
136 continue;
138 /* send the command including null to the server */
139 usleep(1);
140 fvwm_send(s, cmd, strlen(cmd) + 1, 0);
142 kill(getppid(), SIGKILL);
143 sclose(0);
145 while (fgets(data, MAX_MESSAGE_SIZE, sp))
147 /* get the response */
148 /* ignore config lines */
149 if (!strcmp(data, C_BEG))
151 while (fgets(data, MAX_MESSAGE_SIZE, sp))
153 if (*data == '\0' || !strcmp(data,C_END))
155 break;
158 if (*data != '\0')
160 continue;
163 if (*data == '\0')
165 break;
167 printf("%s",data);
170 return 0;