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
|
/* $Header: /home/yav/catty/fkiss/RCS/bg.c,v 1.10 2000/08/25 06:50:36 yav Exp $
* fkiss background task and signal handling
* written by yav <yav@bigfoot.com>
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
char id_bg[] = "$Id: bg.c,v 1.10 2000/08/25 06:50:36 yav Exp $";
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <stdio.h>
#include "config.h"
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include "headers.h"
#include "fkiss.h"
#include "work.h"
#define PUBLIC_BG_C
#include "extern.h"
/* for System V signal symbol */
#ifndef SIGCHLD
#define SIGCHLD SIGCLD
#endif
static FILE *bgfp = NULL;
static char sound_mark[] = "**sound";
static char play_command[] = "play";
static char shell_command[] = "/bin/sh";
RETSIGTYPE sigchld_handler()
{
wait(NULL);
signal(SIGCHLD, sigchld_handler); /* reset signal handler for SysV */
}
void bg_child()
{
FILE *fp;
int i, s, sound_mark_len;
char *p;
char buf[256];
signal(SIGCHLD, sigchld_handler);
sound_mark_len = strlen(sound_mark);
fp = fdopen(0, "r");
while (fgets(buf, sizeof(buf), fp) != NULL) {
cut_crlf(buf);
if (debug_mode)
fprintf(stderr, "Background:%s\n", buf);
p = buf;
s = 0;
if (strncmp(p, sound_mark, sound_mark_len) == 0) {
p += sound_mark_len + 1; /* skip sound_mark + ' ' */
s = 1;
}
if (fork() == 0) {
/* child process */
switch(s) {
case 1: /* Sound function */
if (sound_mode & 1) {
/* internal sound routine */
i = sound_play(p);
switch (i) {
case 0: /* no error */
exit(0);
case 1: /* sound file open error */
msg("W sound file ``%s'' open error.\n", p);
break;
case 2: /* device open error */
msg("W sound device ``%s'' open error.\n", sound_device);
break;
case 3: /* Not supported format */
msg("W ``%s'' not supported sound format.\n", p);
break;
default:
msg("W unknown sound error (%d)!?\n", i);
break;
}
} else {
execlp(play_command, play_command, p, NULL);
/* play_command exec failed, try shell_command */
execlp(shell_command, shell_command, play_command, p, NULL);
/* shell_command exec failed */
}
break;
default: /* Other function */
system(p);
exit(0);
}
exit(1); /* exec failed */
}
}
exit(0);
}
/* fork background process */
int background()
{
int i;
int pfd[2];
if (pipe(pfd) < 0)
return -1; /* pipe open failed */
i = fork();
if (i < 0)
return -1; /* fork failed */
if (i == 0) {
/* child */
close(0);
dup(pfd[0]);
close(pfd[0]);
close(pfd[1]);
bg_child();
/* NOT REACHED */
}
/* parent */
close(pfd[0]);
bgfp = fdopen(pfd[1], "w");
return 0;
}
int bg_shell(str)
char *str;
{
if (bgfp != NULL) {
fprintf(bgfp, "%s\n", str);
fflush(bgfp);
}
return 0;
}
int bg_play(name)
char *name;
{
if (sound_mode && bgfp != NULL) {
fprintf(bgfp, "%s %s\n", sound_mark, name);
fflush(bgfp);
}
return 0;
}
void bg_exit()
{
if (bgfp != NULL) {
fclose(bgfp);
bgfp = NULL;
wait(NULL);
}
}
RETSIGTYPE signal_handler(sig)
int sig;
{
signal(sig, SIG_IGN); /* for SysV */
if (debug_mode)
printf("fkiss: signal %d!\n", sig);
kiss_exit(1);
}
void setup_signal_handler()
{
int i;
static int trap_signals[] = {
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM,
#ifdef SIGXCPU
SIGXCPU,
#endif
#ifdef SIGXFSZ
SIGXFSZ,
#endif
0
};
for (i = 0; trap_signals[i]; i++) {
/* Don't trap already ignored signals */
if (signal(trap_signals[i], SIG_IGN) != SIG_IGN)
signal(trap_signals[i], signal_handler);
}
}
/* End of file */
|