📄 scriptcntl.c
字号:
/* voifax low level scripting control library * Copyright (C) 2004 Simone Freddio & Andrea Emanuelli * * 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 "scriptcntl.h"#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/ioctl.h>int sc_launch(t_scriptcntl *scrc, char *scrname){ char module[] = "sc_launch"; char env_read[50]; char env_write[50]; char env_pid[100]; int parentpid; /* Crea le due pipe per il colloquio con lo script */ pipe(scrc->pipe_to_script); pipe(scrc->pipe_from_script); parentpid = getpid(); /* Genera un child */ if ((scrc->script_pid = fork()) == 0) { /* Imposta le variabili d'ambiente per il colloquio dati */ sprintf(env_write, "%s=%d", FD_WRITE, scrc->pipe_from_script[1]); putenv(env_write); sprintf(env_read, "%s=%d", FD_READ, scrc->pipe_to_script[0]); putenv(env_read); sprintf(env_pid, "%s=%d", VOICE_PID, parentpid); putenv(env_pid); /* Esegue lo script specificato */ if (execl(scrname, scrname, NULL) == -1) { TRC(TRC_ERR, module, "Error: cannot execute the following script:"); TRC(TRC_ERR, module, scrname); } /* Simula l'invio di un comando fittizio da parte dello script */ write(scrc->pipe_from_script[1], "SCRIPT_EXEC_ERROR\n", 18); } if (scrc->script_pid == -1) { TRC(TRC_ERR, module, "Error on fork"); return false; } return true;}int sc_send(t_scriptcntl *sc, char *cmd){ char module[] = "sc_send"; char trc[200]; int ret; char *cmd_to_send = malloc(strlen(cmd) + 2); sprintf(trc, "Sending '%s' to script", cmd); TRC(TRC_INFO, module, trc); /* Aggiunta \n alla stringa */ sprintf(cmd_to_send, "%s\n", cmd); /* Invio dati sul pipe */ ret = write(sc->pipe_to_script[1], cmd_to_send, strlen(cmd_to_send)) == -1 ? false : true; free(cmd_to_send); return ret;}int sc_receive(t_scriptcntl *sc, char *buf, int maxlen){ char module[] = "sc_receive"; int ret;/* char *trc = malloc(maxlen + 200);*/ memset(buf, 0, maxlen); ret = read(sc->pipe_from_script[0], buf, maxlen);/* sprintf(trc, "Received '%s' from script", buf); TRC(TRC_INFO, module, trc); free(trc);*/ return ret == -1 ? false : true;}int sc_receiveready(t_scriptcntl *sc){ int nbytes = 0; /* Controllo quantita' dati in coda al pipe */ ioctl(sc->pipe_from_script[0], FIONREAD, &nbytes); return nbytes != 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -