1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_port_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* The Implementation of the port type: */
26 /****************************************************************************/
28 #include "new_string.h"
29 #include "port_dictionary.h"
32 #include "variables.h"
34 /****************************************************************************/
36 /* Port methods (internal): */
38 /****************************************************************************/
43 char *(*get_proc)(port *, char **);
47 if (p->status & INPUT_CLOSED) {
48 var_set_variable("error",
49 "Attempt to read from a port whose input has been closed");
50 return(string_Copy(""));
55 var_set_variable("error",
56 "Attempt to read from a port which does not support reading");
57 return(string_Copy(""));
60 result = get_proc(p, &error);
62 var_set_variable("error", error);
63 return(string_Copy(""));
73 char *(*put_proc)(port *, char *, int);
76 if (p->status & OUTPUT_CLOSED) {
77 var_set_variable("error",
78 "Attempt to write to a port whose output has been closed");
84 var_set_variable("error",
85 "Attempt to write to a port which does not support writing");
89 error = put_proc(p, data, length);
91 var_set_variable("error", error);
95 port_close_input(port *p)
97 char *(*close_input_proc)(port *);
100 if (p->status & INPUT_CLOSED)
102 p->status |= INPUT_CLOSED;
104 close_input_proc = p->close_input;
105 if (!close_input_proc)
108 if (error = close_input_proc(p))
109 var_set_variable("error", error);
113 port_close_output(port *p)
115 char *(*close_output_proc)(port *);
118 if (p->status & OUTPUT_CLOSED)
120 p->status |= OUTPUT_CLOSED;
122 close_output_proc = p->close_output;
123 if (!close_output_proc)
126 if (error = close_output_proc(p))
127 var_set_variable("error", error);
130 /****************************************************************************/
132 /* Code to implement a namespace of ports: */
134 /****************************************************************************/
137 * port_dict - the dictionary mapping portnames to ports
140 static port_dictionary port_dict = NULL;
144 * Modifies: all ports
145 * Effects: Closes all existing ports. Must be called before
146 * any other port call is made.
150 close_port_from_binding(port_dictionary_binding *b)
152 port_close_input(&(b->value));
153 port_close_output(&(b->value));
160 port_dictionary_Enumerate(port_dict, close_port_from_binding);
161 port_dictionary_Destroy(port_dict);
164 port_dict = port_dictionary_Create(31);
170 * port *create_named_port(string name)
171 * Modifies: the port named name
172 * Requires: init_ports has been called
173 * Effects: If a port with name name already exists, it is first
174 * closed (& destroyed). A new unfilled in port is then
175 * created and assigned the name name. Its address is
176 * then returned. It is up to the caller to fill in its
177 * various fields correctly.
181 create_named_port(string name)
184 port_dictionary_binding *binding;
186 binding = port_dictionary_Define(port_dict, name, &already_exists);
187 if (already_exists) {
188 port_close_input(&(binding->value));
189 port_close_output(&(binding->value));
192 return(&(binding->value));
198 * port *get_named_port(string name)
199 * Requires: init_ports has been called
200 * Effects: If there is a port by name name, returns a pointer to
201 * it. Otherwise returns NULL.
205 get_named_port(string name)
207 port_dictionary_binding *binding;
209 binding = port_dictionary_Lookup(port_dict, name);
213 return(&(binding->value));
216 /****************************************************************************/
218 /* External interface to named ports: */
220 /****************************************************************************/
223 * string read_from_port(string name)
224 * Requires: init_ports has been called
225 * Modifies: the port named name if any, $error
226 * Effects: If a port by name name does not exist, sets $error to
227 * "No such port" & returns "". Otherwise, attempts to
228 * read from that port. If an error occurs, $error is
229 * set to the error message and "" returned. Otherwise
230 * the read string is returned. The returned string is
231 * on the heap & must be eventually freed.
235 read_from_port(string name)
239 if (!(p = get_named_port(name))) {
240 var_set_variable("error", "No such port");
241 return(string_Copy(""));
248 * void write_on_port(string name, char *text, int length)
249 * Requires: init_ports has been called, length>=0
250 * Modifies: the port named name if any, $error
251 * Effects: If a port by name name does not exist, sets $error to
252 * "No such port" & returns. Otherwise, attempts to
253 * write text[0..length-1] on that port. If an error
254 * occurs, $error is set to the error message.
258 write_on_port(string name,
264 if (!(p = get_named_port(name))) {
265 var_set_variable("error", "No such port");
269 port_put(p, text, length);
273 * void close_port_input(string name)
274 * Requires: init_ports has been called
275 * Modifies: the port named name if any, $error
276 * Effects: If a port by name name does not exist, sets $error to
277 * "No such port" & returns. Otherwise, closes the
278 * input part of the port by name name. When both a
279 * port's input & output parts have been closed, the
280 * port is deleted to save space. If an error
281 * occurs, $error is set to the error message.
285 close_port_input(string name)
287 port_dictionary_binding *binding;
289 binding = port_dictionary_Lookup(port_dict, name);
293 port_close_input(&(binding->value));
294 if (binding->value.status == PORT_CLOSED)
295 port_dictionary_Delete(port_dict, binding);
299 * void close_port_output(string name)
300 * Requires: init_ports has been called
301 * Modifies: the port named name if any, $error
302 * Effects: If a port by name name does not exist, sets $error to
303 * "No such port" & returns. Otherwise, closes the
304 * output part of the port by name name. When both a
305 * port's input & output parts have been closed, the
306 * port is deleted to save space. If an error
307 * occurs, $error is set to the error message.
311 close_port_output(string name)
313 port_dictionary_binding *binding;
315 binding = port_dictionary_Lookup(port_dict, name);
319 port_close_output(&(binding->value));
320 if (binding->value.status == PORT_CLOSED)
321 port_dictionary_Delete(port_dict, binding);
324 /****************************************************************************/
326 /* Code to implement a port given some FILE *'s: */
328 /****************************************************************************/
334 char buffer[10000]; /* <<<>>> */
336 if (!p->data.file.input_connector) {
337 *error_p = "Attempt to read past end of file";
343 if (!fgets(buffer, 9999, p->data.file.input_connector)) {
345 *error_p = strerror(errno);
347 *error_p = "Attempt to read past end of file";
353 return(string_Copy(buffer));
361 if (!p->data.file.output_connector)
365 fwrite(text, 1, length, p->data.file.output_connector);
366 fflush(p->data.file.output_connector);
369 return(strerror(errno));
375 close_file_input(port *p)
378 if (p->data.file.input_connector) {
379 fclose(p->data.file.input_connector);
380 p->data.file.input_connector = 0;
384 return(strerror(errno));
390 close_file_output(port *p)
393 if (p->data.file.output_connector) {
394 fclose(p->data.file.output_connector);
395 p->data.file.output_connector = 0;
399 return(strerror(errno));
404 void create_port_from_files(string name,
408 port *p = create_named_port(name);
410 #if !defined(__HIGHC__)
411 p->get = input ? get_file : NULL;
412 p->put = output ? put_file : NULL;
414 /* RT compiler (hc2.1y) bug workaround */
424 p->close_input = close_file_input;
425 p->close_output = close_file_output;
427 p->data.file.input_connector = input;
428 p->data.file.output_connector = output;
431 /****************************************************************************/
433 /* Code for creating various types of FILE * ports: */
435 /****************************************************************************/
438 create_subprocess_port(string name,
442 int to_child_descriptors[2];
443 int to_parent_descriptors[2];
447 /* <<<>>> (file leak) */
448 if (pipe(to_child_descriptors)!=0 || pipe(to_parent_descriptors)!=0)
453 fprintf(stderr, "zwgc: error while attempting to fork: ");
456 } else if (pid == 0) { /* in child */
459 dup2(to_child_descriptors[0], 0);
460 dup2(to_parent_descriptors[1], 1);
461 close(to_child_descriptors[1]);
462 close(to_parent_descriptors[0]);
464 execvp(argv[0], argv);
465 fprintf(stderr,"zwgc: unable to exec %s: ", argv[0]);
470 fcntl(to_parent_descriptors[0], F_SETFD, 1);
471 fcntl(to_child_descriptors[1], F_SETFD, 1);
472 in = fdopen(to_parent_descriptors[0],"r");
473 out = fdopen(to_child_descriptors[1],"w");
474 close(to_child_descriptors[0]);
475 close(to_parent_descriptors[1]);
477 create_port_from_files(name, in, out);
481 create_file_append_port(string name,
489 oumask = umask(077); /* allow read/write for us only */
490 out = fopen(filename, "a");
491 (void) umask(oumask);
493 var_set_variable("error", strerror(errno));
497 create_port_from_files(name, 0, out);
501 create_file_input_port(string name,
507 in = fopen(filename, "r");
509 var_set_variable("error", strerror(errno));
513 create_port_from_files(name, in, 0);
517 create_file_output_port(string name,
525 oumask = umask(077); /* allow read/write for us only */
526 out = fopen(filename, "w");
527 (void) umask(oumask);
529 var_set_variable("error", strerror(errno));
533 create_port_from_files(name, 0, out);
536 /****************************************************************************/
538 /* Code to implement a port given a filter function: */
540 /****************************************************************************/
548 if (string_stack_empty(p->data.filter.waiting_packets)) {
549 *error_p = "Attempt to read from port when no data available";
553 result = string_stack_top(p->data.filter.waiting_packets);
554 string_stack_pop(p->data.filter.waiting_packets);
566 if (p->status & INPUT_CLOSED)
569 input = convert_nulls_to_newlines(text, length);
570 output = (*(p->data.filter.filter))(input);
572 string_stack_push(p->data.filter.waiting_packets, output);
577 close_filter_input(port *p)
579 while (!string_stack_empty(p->data.filter.waiting_packets))
580 string_stack_pop(p->data.filter.waiting_packets);
587 close_filter_output(port *p)
593 create_port_from_filter(string name,
594 string (*filter)(string))
596 port *p = create_named_port(name);
600 p->close_input = close_filter_input;
601 p->close_output = close_filter_output;
603 p->data.filter.waiting_packets = string_stack_create();
604 p->data.filter.filter = filter;
607 /****************************************************************************/
609 /* Code to implement a port given an output function: */
611 /****************************************************************************/
621 input = convert_nulls_to_newlines(text, length);
622 error = p->data.output.output(input);
629 close_output(port *p)
635 create_port_from_output_proc(string name,
636 char *(*output)(string))
638 #ifdef SABER /* Yes, it's another ANSI incompatiblity */
641 port *p = create_named_port(name);
645 p = create_named_port(name);
650 p->close_input = close_output;
651 p->close_output = close_output;
653 p->data.output.output = output;