]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/standard_ports.c
526e02cdb53f557fa42e0d6748db8ef7ed4b06c8
[1ts-debian.git] / zephyr / zwgc / standard_ports.c
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
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_standard_ports_c[] = "$Id$";
18 #endif
19
20 #include <zephyr/mit-copyright.h>
21
22 /****************************************************************************/
23 /*                                                                          */
24 /*                        Code to setup standard ports:                     */
25 /*                                                                          */
26 /****************************************************************************/
27
28 #include "new_memory.h"
29 #include "port.h"
30 #include "variables.h"
31 #include "error.h"
32 #include "main.h"
33 #include <zephyr/zephyr.h>
34
35 extern string tty_filter();
36 extern int tty_filter_init();
37
38 #ifndef X_DISPLAY_MISSING
39 extern char *X_driver();
40 extern int X_driver_init();
41 #endif
42
43 extern void usage();
44
45 /*
46  *
47  */
48
49 char *plain_driver(input)
50      string input;
51 {
52     string processed_input = tty_filter(input, 0);
53
54     fputs(processed_input, stdout);
55     fflush(stdout);
56     free(processed_input);
57     return(NULL);
58 }
59
60 /*
61  *
62  */
63
64 char *tty_driver(input)
65      string input;
66 {
67     string processed_input = tty_filter(input, 1);
68
69     fputs(processed_input, stdout);
70     fflush(stdout);
71     free(processed_input);
72     return(NULL);
73 }
74
75 /*
76  *
77  */
78
79 string noop_filter(input)
80      string input;
81 {
82     return(input);
83 }
84
85 /*
86  *
87  */
88
89 string plain_filter(input)
90      string input;
91 {
92     return(tty_filter(input, 0));
93 }
94
95 /*
96  *
97  */
98
99 string fancy_filter(input)
100      string input;
101 {
102     return(tty_filter(input, 1));
103 }
104
105 /*
106  *
107  */
108
109 static struct standard_port_info {
110     char *port_name;
111 /*
112  * 0 = ok to use as the default output port
113  * 1 = not ok to use as the default output port
114  * 2 = disabled
115  */
116 #define DEFAULT_OK      0
117 #define DEFAULT_NOTOK   1
118 #define DISABLED        2
119
120     int port_setup_status;
121     int (*port_init)();
122 #define  INPUT_DESC  0
123 #define  OUTPUT_DESC 1
124 #define  FILTER      2
125 #define  OUTPUT_PROC 3
126     int type;
127     char *(*function)();
128     int setup_arg;
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},
133 #else
134 { "tty",          DEFAULT_OK, tty_filter_init, OUTPUT_PROC, tty_driver,  0},
135 #endif
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},
139
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},
144
145 { NULL,           DISABLED, NULL,            FILTER,      NULL,         0} };
146
147 /*
148  * <<<>>>
149  */
150
151 static struct standard_port_info *get_standard_port_info(port_name)
152      string port_name;
153 {
154     struct standard_port_info *p;
155
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)
158         return(p);
159
160     return(NULL);
161 }
162
163 /*
164  *  Internal Routine:
165  *
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,
169  *                  returns -1.
170  */
171
172 static int boolean_value_of(text)
173      string text;
174 {
175     if (!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))
180       return(1);
181     else if (!strcasecmp("no", text) || !strcasecmp("n", text) ||
182         !strcasecmp("false", text) || !strcasecmp("f", text) ||
183         !strcasecmp("off", text))
184       return(0);
185     else
186       return(-1);
187 }
188
189 /*
190  *
191  */
192
193 void init_standard_ports(pargc, argv)
194      int *pargc;
195      char **argv;
196 {
197     struct standard_port_info *p;
198     string first_working_port = "";
199     string default_port = "";
200     char **new, **current;
201     int fallback;
202
203     /*
204      * Process argument list handling "-disable <port>" and
205      * "-default <output port>" arguments, as well as "-ttymode"
206      */
207     for (new=current=argv+1; *current; current++) {
208         if (string_Eq((string) *current, "-disable")) {
209             current++; *pargc -= 2;
210             if (!*current)
211               usage();
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;
216             if (!*current)
217               usage();
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";
223             (*pargc)--;
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;
228             }
229         } else
230           *(new++) = *current;
231     }
232     *new = *current;
233
234     fallback = boolean_value_of(ZGetVariable("fallback"));
235     /*
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.
239      */
240     for (p = standard_port_info_table; p->port_name; p++) {
241         if (p->port_setup_status == DISABLED)
242           continue;
243
244         if (p->port_init && (*(p->port_init))(p->port_name,
245                                               *first_working_port,
246                                               pargc, argv)) {
247             p->port_setup_status = DISABLED;
248             continue;
249         }
250
251         if (fallback == 1) {
252             /* we are doing fallback,  make DEFAULT_NOTOK ports OK */
253             p->port_setup_status = DEFAULT_OK;
254         }
255         if (!*first_working_port)
256           first_working_port = p->port_name;
257         switch (p->type) {
258           case INPUT_DESC:
259             create_port_from_files(p->port_name, fdopen(p->setup_arg, "r"),0);
260             break;
261
262           case OUTPUT_DESC:
263             create_port_from_files(p->port_name, 0, fdopen(p->setup_arg, "w"));
264             break;
265
266           case FILTER:
267             create_port_from_filter(p->port_name, p->function);
268             break;
269
270           case OUTPUT_PROC:
271             create_port_from_output_proc(p->port_name, p->function);
272             break;
273         }
274     }
275
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))
280                 break;
281         if (p->port_name)
282             var_set_variable("output_driver", p->port_name);
283         else { /* no suitable default has been found */
284             if (fallback == -1)         /* complain, since indeterminate */
285                 ERROR2(
286 "To receive Zephyrgrams, (type `%s -ttymode').\n",
287                       progname);
288             exit(1);
289         }
290     } else
291         var_set_variable("output_driver", default_port);
292
293 }