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_standard_ports_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Code to setup standard ports: */
26 /****************************************************************************/
28 #include "new_memory.h"
30 #include "variables.h"
33 #include <zephyr/zephyr.h>
35 extern string tty_filter();
36 extern int tty_filter_init();
38 #ifndef X_DISPLAY_MISSING
39 extern char *X_driver();
40 extern int X_driver_init();
49 char *plain_driver(input)
52 string processed_input = tty_filter(input, 0);
54 fputs(processed_input, stdout);
56 free(processed_input);
64 char *tty_driver(input)
67 string processed_input = tty_filter(input, 1);
69 fputs(processed_input, stdout);
71 free(processed_input);
79 string noop_filter(input)
89 string plain_filter(input)
92 return(tty_filter(input, 0));
99 string fancy_filter(input)
102 return(tty_filter(input, 1));
109 static struct standard_port_info {
112 * 0 = ok to use as the default output port
113 * 1 = not ok to use as the default output port
117 #define DEFAULT_NOTOK 1
120 int port_setup_status;
123 #define OUTPUT_DESC 1
125 #define OUTPUT_PROC 3
129 } standard_port_info_table[] = {
130 #ifndef X_DISPLAY_MISSING
131 { "X", DEFAULT_OK, X_driver_init, OUTPUT_PROC, X_driver, 0},
132 { "tty", DEFAULT_NOTOK, tty_filter_init, OUTPUT_PROC, tty_driver, 0},
134 { "tty", DEFAULT_OK, tty_filter_init, OUTPUT_PROC, tty_driver, 0},
136 { "plain", DEFAULT_NOTOK, tty_filter_init, OUTPUT_PROC, plain_driver, 0},
137 { "stdout", DEFAULT_NOTOK, NULL, OUTPUT_DESC, NULL, 1},
138 { "stderr", DEFAULT_NOTOK, NULL, OUTPUT_DESC, NULL, 2},
140 { "stdin", DEFAULT_NOTOK, NULL, INPUT_DESC, NULL, 0},
141 { "loopback", DEFAULT_NOTOK, NULL, FILTER, noop_filter, 0},
142 { "plain_filter", DEFAULT_NOTOK, tty_filter_init, FILTER, plain_filter, 0},
143 { "tty_filter", DEFAULT_NOTOK, tty_filter_init, FILTER, fancy_filter, 0},
145 { NULL, DISABLED, NULL, FILTER, NULL, 0} };
151 static struct standard_port_info *get_standard_port_info(port_name)
154 struct standard_port_info *p;
156 for (p=standard_port_info_table; p->port_name; p++)
157 if (string_Eq(p->port_name, port_name) && p->port_setup_status!=DISABLED)
166 * int boolean_value_of(string text)
167 * Effects: If text represents yes/true/on, return 1. If text
168 * representes no/false/off, return 0. Otherwise,
172 static int boolean_value_of(text)
176 return(-1); /* not set */
177 if (!strcasecmp("yes", text) || !strcasecmp("y", text) ||
178 !strcasecmp("true", text) || !strcasecmp("t", text) ||
179 !strcasecmp("on", text))
181 else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
182 !strcasecmp("false", text) || !strcasecmp("f", text) ||
183 !strcasecmp("off", text))
193 void init_standard_ports(pargc, argv)
197 struct standard_port_info *p;
198 string first_working_port = "";
199 string default_port = "";
200 char **new, **current;
204 * Process argument list handling "-disable <port>" and
205 * "-default <output port>" arguments, as well as "-ttymode"
207 for (new=current=argv+1; *current; current++) {
208 if (string_Eq((string) *current, "-disable")) {
209 current++; *pargc -= 2;
212 if (p = get_standard_port_info((string) *current))
213 p->port_setup_status = DISABLED;
214 } else if (string_Eq((string) *current, "-default")) {
215 current++; *pargc -= 2;
218 default_port = (string) *current;
219 if (p = get_standard_port_info((string) *current))
220 p->port_setup_status = DEFAULT_OK;
221 } else if (string_Eq((string) *current, "-ttymode")) {
222 default_port = (string) "tty";
224 if (p = get_standard_port_info(default_port)) {
225 p->port_setup_status = DEFAULT_OK;
226 if (p = get_standard_port_info ((string) "X"))
227 p->port_setup_status = DISABLED;
234 fallback = boolean_value_of(ZGetVariable("fallback"));
236 * Initialize all non-disabled ports. If a port reports an error,
237 * disable that port. Set default_port if not already set
238 * by the -default argument to the first non-disabled port.
240 for (p = standard_port_info_table; p->port_name; p++) {
241 if (p->port_setup_status == DISABLED)
244 if (p->port_init && (*(p->port_init))(p->port_name,
247 p->port_setup_status = DISABLED;
252 /* we are doing fallback, make DEFAULT_NOTOK ports OK */
253 p->port_setup_status = DEFAULT_OK;
255 if (!*first_working_port)
256 first_working_port = p->port_name;
259 create_port_from_files(p->port_name, fdopen(p->setup_arg, "r"),0);
263 create_port_from_files(p->port_name, 0, fdopen(p->setup_arg, "w"));
267 create_port_from_filter(p->port_name, p->function);
271 create_port_from_output_proc(p->port_name, p->function);
276 if (!default_port[0]) {
277 /* no default port has been set */
278 for (p = get_standard_port_info(first_working_port); p->port_name; p++)
279 if ((p->port_setup_status == DEFAULT_OK))
282 var_set_variable("output_driver", p->port_name);
283 else { /* no suitable default has been found */
284 if (fallback == -1) /* complain, since indeterminate */
286 "To receive Zephyrgrams, (type `%s -ttymode').\n",
291 var_set_variable("output_driver", default_port);