~ubuntu-branches/ubuntu/trusty/malaga/trusty-proposed

« back to all changes in this revision

Viewing changes to source/transmit.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2005-01-10 11:52:04 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110115204-hpgncw5pb0m1t8i6
Tags: 6.13-5
debian/control (malaga-doc Recommends): Suggest gv as a
postscript-viewer instead of ghostview.  (Closes: #289701).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Malaga, a system for Natural Language Analysis.
2
 
 * Copyright (C) 1995-1999 Bjoern Beutel
3
 
 *
4
 
 * Bjoern Beutel
5
 
 * Universitaet Erlangen-Nuernberg
6
 
 * Abteilung fuer Computerlinguistik
7
 
 * Bismarckstrasse 12
8
 
 * D-91054 Erlangen
9
 
 * e-mail: malaga@linguistik.uni-erlangen.de 
10
 
 *
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.
15
 
 *
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.
20
 
 *
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 */
24
 
 
25
 
/* description ==============================================================*/
26
 
 
27
 
/* This module invokes and manages the transmit process from malaga. */
28
 
 
29
 
/* includes =================================================================*/
30
 
 
31
 
#define _XOPEN_SOURCE
32
 
#define _XOPEN_SOURCE_EXTENDED 1
33
 
#include <stdio.h>
34
 
#include <string.h>
35
 
#include <errno.h>
36
 
#include <stdlib.h>
37
 
#include <setjmp.h>
38
 
#include <sys/types.h>
39
 
#include <unistd.h>
40
 
#include <sys/wait.h>
41
 
#include <signal.h>
42
 
#include <pwd.h>
43
 
#include <fcntl.h>
44
 
#include "basic.h"
45
 
#include "pools.h"
46
 
#include "values.h"
47
 
#include "files.h"
48
 
#include "input.h"
49
 
#include "symbols.h"
50
 
#include "commands.h"
51
 
#include "value_parser.h"
52
 
#include "rule_type.h"
53
 
#include "rules.h"
54
 
 
55
 
#undef GLOBAL
56
 
#define GLOBAL
57
 
 
58
 
#include "transmit.h"
59
 
 
60
 
/* variables ================================================================*/
61
 
 
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 */
66
 
 
67
 
/* functions ================================================================*/
68
 
 
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. */
72
 
{
73
 
  int to_transmit_fd[2];
74
 
  int from_transmit_fd[2];
75
 
 
76
 
  if (process_id != 0)
77
 
  {
78
 
    if (waitpid (process_id, 0, WNOHANG) == 0)
79
 
      return;
80
 
 
81
 
    process_id = 0;
82
 
    close_stream (&transmit_input_stream, NULL);
83
 
    close_stream (&transmit_output_stream, NULL);
84
 
  }
85
 
 
86
 
  if (pipe (to_transmit_fd) == -1 || pipe (from_transmit_fd) == -1)
87
 
    error ("can't create pipe to transmit process: %s", strerror (errno));
88
 
    
89
 
  if (transmit_command_line == NULL) 
90
 
    error ("missing transmit command line");
91
 
 
92
 
  switch (process_id = fork ())
93
 
  {
94
 
  case -1:
95
 
    error ("can't create transmit process: %s", strerror (errno));
96
 
    break;
97
 
    
98
 
  case 0:
99
 
  {
100
 
    string_t arguments, argument;
101
 
    string_t *args;
102
 
    int_t num_args, i;
103
 
 
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]);
110
 
 
111
 
    /* Count arguments. */
112
 
    num_args = 0;
113
 
    arguments = transmit_command_line;
114
 
    while (*arguments != EOS)
115
 
    {
116
 
      argument = parse_word (&arguments);
117
 
      free_mem (&argument);
118
 
      num_args++;
119
 
    }
120
 
 
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);
126
 
    args[i] = NULL;
127
 
    
128
 
    execvp (args[0], args);
129
 
    fprintf (stderr, "can't start transmit process \"%s\": %s\n", 
130
 
             args[0], strerror (errno));
131
 
    exit (1);
132
 
  }
133
 
  default:
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));
140
 
  }
141
 
}
142
 
 
143
 
/*---------------------------------------------------------------------------*/
144
 
 
145
 
LOCAL void stop_transmit_process (void)
146
 
/* Stop the Malaga transmit process. */
147
 
{
148
 
  close_stream (&transmit_input_stream, NULL);
149
 
  close_stream (&transmit_output_stream, NULL);
150
 
  if (process_id != 0)
151
 
  {
152
 
    kill (process_id, SIGTERM);
153
 
    process_id = 0;
154
 
  }
155
 
}
156
 
 
157
 
/*---------------------------------------------------------------------------*/
158
 
 
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>. */
165
 
{
166
 
  string_t input_line, value_string;
167
 
 
168
 
  start_transmit_process ();
169
 
 
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);
175
 
 
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);
180
 
}
181
 
 
182
 
/*---------------------------------------------------------------------------*/
183
 
 
184
 
GLOBAL void init_transmit (void)
185
 
/* Initialise this module. */
186
 
{
187
 
  transmit = local_transmit;
188
 
}
189
 
 
190
 
/*---------------------------------------------------------------------------*/
191
 
 
192
 
GLOBAL void terminate_transmit (void)
193
 
/* Terminate this module. */
194
 
{
195
 
  stop_transmit_process ();
196
 
  transmit = NULL;
197
 
}
198
 
 
199
 
/*---------------------------------------------------------------------------*/
200
 
 
201
 
LOCAL void do_transmit_option (string_t arguments)
202
 
/* Set the command line to start the transmit process. */
203
 
{
204
 
  if (*arguments == EOS)
205
 
    printf ("transmit: \"%s\"\n", 
206
 
            (transmit_command_line == NULL) ? "" : transmit_command_line);
207
 
  else
208
 
  {
209
 
    stop_transmit_process ();
210
 
    free_mem (&transmit_command_line);
211
 
    transmit_command_line = parse_word (&arguments);
212
 
  }
213
 
 
214
 
  parse_end (arguments);
215
 
}
216
 
 
217
 
GLOBAL command_t transmit_option =
218
 
{
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"
222
 
};
223
 
 
224
 
/*---------------------------------------------------------------------------*/
225
 
 
226
 
LOCAL void do_transmit (string_t arguments)
227
 
/* Communicate with the transmit process. */
228
 
{
229
 
  string_t result;
230
 
 
231
 
  parse_value_string (arguments);
232
 
  local_transmit ();
233
 
  result = value_to_readable (value_stack[--top], FALSE);
234
 
  printf ("%s\n", result);
235
 
  free_mem (&result);
236
 
}
237
 
 
238
 
GLOBAL command_t transmit_command =
239
 
{
240
 
  "transmit", do_transmit,
241
 
  "Usage: transmit <value>\n"
242
 
  "Send <value> to the transmit process and display the result.\n"
243
 
};
244
 
 
245
 
/* end of file ==============================================================*/