1
/* This file is part of Malaga, a system for Natural Language Analysis.
2
* Copyright (C) 1995-1999 Bjoern Beutel
5
* Universitaet Erlangen-Nuernberg
6
* Abteilung fuer Computerlinguistik
9
* e-mail: malaga@linguistik.uni-erlangen.de
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25
/* description ==============================================================*/
27
/* This module invokes and manages the transmit process from malaga. */
29
/* includes =================================================================*/
32
#define _XOPEN_SOURCE_EXTENDED 1
38
#include <sys/types.h>
51
#include "value_parser.h"
52
#include "rule_type.h"
60
/* variables ================================================================*/
62
LOCAL string_t transmit_command_line; /* command line for transmit output */
63
LOCAL FILE *transmit_input_stream; /* to read data from transmit process */
64
LOCAL FILE *transmit_output_stream; /* to write data to transmit process */
65
LOCAL pid_t process_id; /* ID of transmit process */
67
/* functions ================================================================*/
69
LOCAL void start_transmit_process (void)
70
/* Start the Malaga transmit process by executing <transmit_command_line>
71
* if it is not already running. */
73
int to_transmit_fd[2];
74
int from_transmit_fd[2];
78
if (waitpid (process_id, 0, WNOHANG) == 0)
82
close_stream (&transmit_input_stream, NULL);
83
close_stream (&transmit_output_stream, NULL);
86
if (pipe (to_transmit_fd) == -1 || pipe (from_transmit_fd) == -1)
87
error ("can't create pipe to transmit process: %s", strerror (errno));
89
if (transmit_command_line == NULL)
90
error ("missing transmit command line");
92
switch (process_id = fork ())
95
error ("can't create transmit process: %s", strerror (errno));
100
string_t arguments, argument;
104
dup2 (to_transmit_fd[0], STDIN_FILENO);
105
close (to_transmit_fd[0]);
106
close (to_transmit_fd[1]);
107
dup2 (from_transmit_fd[1], STDOUT_FILENO);
108
close (from_transmit_fd[0]);
109
close (from_transmit_fd[1]);
111
/* Count arguments. */
113
arguments = transmit_command_line;
114
while (*arguments != EOS)
116
argument = parse_word (&arguments);
117
free_mem (&argument);
121
/* Create argument vector. */
122
args = new_vector (sizeof (string_t), num_args + 1);
123
arguments = transmit_command_line;
124
for (i = 0; i < num_args; i++)
125
args[i] = parse_word (&arguments);
128
execvp (args[0], args);
129
fprintf (stderr, "can't start transmit process \"%s\": %s\n",
130
args[0], strerror (errno));
134
close (to_transmit_fd[0]);
135
transmit_output_stream = fdopen (to_transmit_fd[1], "w");
136
close (from_transmit_fd[1]);
137
transmit_input_stream = fdopen (from_transmit_fd[0], "r");
138
if (transmit_input_stream == NULL || transmit_output_stream == NULL)
139
error ("can't open data stream: %s", strerror (errno));
143
/*---------------------------------------------------------------------------*/
145
LOCAL void stop_transmit_process (void)
146
/* Stop the Malaga transmit process. */
148
close_stream (&transmit_input_stream, NULL);
149
close_stream (&transmit_output_stream, NULL);
152
kill (process_id, SIGTERM);
157
/*---------------------------------------------------------------------------*/
159
LOCAL void local_transmit (void)
160
/* STACK EFFECTS: <value> -> <new_value>
161
* Start the Malaga transmit process by executing <transmit_command_line>
162
* if it is not already running.
163
* Send <value>, in text format, to the transmit process and receive an answer
164
* value in text format, convert it into internal format as <new_value>. */
166
string_t input_line, value_string;
168
start_transmit_process ();
170
/* Send <argument> to transmit process. */
171
value_string = value_to_readable (value_stack[--top], TRUE);
172
fprintf (transmit_output_stream, "%s\n", value_string);
173
fflush (transmit_output_stream);
174
free_mem (&value_string);
176
/* Read result and convert it to value. */
177
input_line = read_line (transmit_input_stream);
178
parse_value_string (input_line);
179
free_mem (&input_line);
182
/*---------------------------------------------------------------------------*/
184
GLOBAL void init_transmit (void)
185
/* Initialise this module. */
187
transmit = local_transmit;
190
/*---------------------------------------------------------------------------*/
192
GLOBAL void terminate_transmit (void)
193
/* Terminate this module. */
195
stop_transmit_process ();
199
/*---------------------------------------------------------------------------*/
201
LOCAL void do_transmit_option (string_t arguments)
202
/* Set the command line to start the transmit process. */
204
if (*arguments == EOS)
205
printf ("transmit: \"%s\"\n",
206
(transmit_command_line == NULL) ? "" : transmit_command_line);
209
stop_transmit_process ();
210
free_mem (&transmit_command_line);
211
transmit_command_line = parse_word (&arguments);
214
parse_end (arguments);
217
GLOBAL command_t transmit_option =
219
"transmit", do_transmit_option,
220
"Usage: set transmit \"<transmit_command_line>\"\n"
221
"Set the command line that is used to start the transmit process.\n"
224
/*---------------------------------------------------------------------------*/
226
LOCAL void do_transmit (string_t arguments)
227
/* Communicate with the transmit process. */
231
parse_value_string (arguments);
233
result = value_to_readable (value_stack[--top], FALSE);
234
printf ("%s\n", result);
238
GLOBAL command_t transmit_command =
240
"transmit", do_transmit,
241
"Usage: transmit <value>\n"
242
"Send <value> to the transmit process and display the result.\n"
245
/* end of file ==============================================================*/