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 char *tty_filter(string, int);
36 extern int tty_filter_init(char *, char, int *, char **);
38 #ifndef X_DISPLAY_MISSING
39 extern char *X_driver(string);
40 extern int X_driver_init(char *, char, int *, char **);
43 extern void usage(void);
50 plain_driver(string input)
52 string processed_input = tty_filter(input, 0);
54 fputs(processed_input, stdout);
56 free(processed_input);
65 tty_driver(string input)
67 string processed_input = tty_filter(input, 1);
69 fputs(processed_input, stdout);
71 free(processed_input);
80 noop_filter(string input)
90 plain_filter(string input)
92 return(tty_filter(input, 0));
100 fancy_filter(string 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;
121 int (*port_init)(char *, char, int *, char **);
123 #define OUTPUT_DESC 1
125 #define OUTPUT_PROC 3
127 char *(*function)(string);
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 *
152 get_standard_port_info(string 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,
173 boolean_value_of(string 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(int *pargc,
196 struct standard_port_info *p;
197 string first_working_port = "";
198 string default_port = "";
199 char **new, **current;
203 * Process argument list handling "-disable <port>" and
204 * "-default <output port>" arguments, as well as "-ttymode"
206 for (new=current=argv+1; *current; current++) {
207 if (string_Eq((string) *current, "-disable")) {
208 current++; *pargc -= 2;
211 if (p = get_standard_port_info((string) *current))
212 p->port_setup_status = DISABLED;
213 } else if (string_Eq((string) *current, "-default")) {
214 current++; *pargc -= 2;
217 default_port = (string) *current;
218 if (p = get_standard_port_info((string) *current))
219 p->port_setup_status = DEFAULT_OK;
220 } else if (string_Eq((string) *current, "-ttymode")) {
221 default_port = (string) "tty";
223 if (p = get_standard_port_info(default_port)) {
224 p->port_setup_status = DEFAULT_OK;
225 if (p = get_standard_port_info ((string) "X"))
226 p->port_setup_status = DISABLED;
233 fallback = boolean_value_of(ZGetVariable("fallback"));
235 * Initialize all non-disabled ports. If a port reports an error,
236 * disable that port. Set default_port if not already set
237 * by the -default argument to the first non-disabled port.
239 for (p = standard_port_info_table; p->port_name; p++) {
240 if (p->port_setup_status == DISABLED)
243 if (p->port_init && (*(p->port_init))(p->port_name,
246 p->port_setup_status = DISABLED;
251 /* we are doing fallback, make DEFAULT_NOTOK ports OK */
252 p->port_setup_status = DEFAULT_OK;
254 if (!*first_working_port)
255 first_working_port = p->port_name;
258 create_port_from_files(p->port_name, fdopen(p->setup_arg, "r"),0);
262 create_port_from_files(p->port_name, 0, fdopen(p->setup_arg, "w"));
266 create_port_from_filter(p->port_name, p->function);
270 create_port_from_output_proc(p->port_name, p->function);
275 if (!default_port[0]) {
276 /* no default port has been set */
277 for (p = get_standard_port_info(first_working_port); p->port_name; p++)
278 if ((p->port_setup_status == DEFAULT_OK))
281 var_set_variable("output_driver", p->port_name);
282 else { /* no suitable default has been found */
283 if (fallback == -1) /* complain, since indeterminate */
285 "To receive Zephyrgrams, (type `%s -ttymode').\n",
290 var_set_variable("output_driver", default_port);