]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/standard_ports.c
f679f4e4d5f81d7246b29801b1da99ce8cda2cee
[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 char *tty_filter(string, int);
36 extern int tty_filter_init(char *, char, int *, char **);
37
38 #ifndef X_DISPLAY_MISSING
39 extern char *X_driver(string);
40 extern int X_driver_init(char *, char, int *, char **);
41 #endif
42
43 extern void usage(void);
44
45 /*
46  *
47  */
48
49 char *
50 plain_driver(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 *
65 tty_driver(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
80 noop_filter(string input)
81 {
82     return(input);
83 }
84
85 /*
86  *
87  */
88
89 string
90 plain_filter(string input)
91 {
92     return(tty_filter(input, 0));
93 }
94
95 /*
96  *
97  */
98
99 string
100 fancy_filter(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)(char *, char, int *, char **);
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)(string);
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 *
152 get_standard_port_info(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
173 boolean_value_of(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(int *pargc,
194                          char **argv)
195 {
196     struct standard_port_info *p;
197     string first_working_port = "";
198     string default_port = "";
199     char **new, **current;
200     int fallback;
201
202     /*
203      * Process argument list handling "-disable <port>" and
204      * "-default <output port>" arguments, as well as "-ttymode"
205      */
206     for (new=current=argv+1; *current; current++) {
207         if (string_Eq((string) *current, "-disable")) {
208             current++; *pargc -= 2;
209             if (!*current)
210               usage();
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;
215             if (!*current)
216               usage();
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";
222             (*pargc)--;
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;
227             }
228         } else
229           *(new++) = *current;
230     }
231     *new = *current;
232
233     fallback = boolean_value_of(ZGetVariable("fallback"));
234     /*
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.
238      */
239     for (p = standard_port_info_table; p->port_name; p++) {
240         if (p->port_setup_status == DISABLED)
241           continue;
242
243         if (p->port_init && (*(p->port_init))(p->port_name,
244                                               *first_working_port,
245                                               pargc, argv)) {
246             p->port_setup_status = DISABLED;
247             continue;
248         }
249
250         if (fallback == 1) {
251             /* we are doing fallback,  make DEFAULT_NOTOK ports OK */
252             p->port_setup_status = DEFAULT_OK;
253         }
254         if (!*first_working_port)
255           first_working_port = p->port_name;
256         switch (p->type) {
257           case INPUT_DESC:
258             create_port_from_files(p->port_name, fdopen(p->setup_arg, "r"),0);
259             break;
260
261           case OUTPUT_DESC:
262             create_port_from_files(p->port_name, 0, fdopen(p->setup_arg, "w"));
263             break;
264
265           case FILTER:
266             create_port_from_filter(p->port_name, p->function);
267             break;
268
269           case OUTPUT_PROC:
270             create_port_from_output_proc(p->port_name, p->function);
271             break;
272         }
273     }
274
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))
279                 break;
280         if (p->port_name)
281             var_set_variable("output_driver", p->port_name);
282         else { /* no suitable default has been found */
283             if (fallback == -1)         /* complain, since indeterminate */
284                 ERROR2(
285 "To receive Zephyrgrams, (type `%s -ttymode').\n",
286                       progname);
287             exit(1);
288         }
289     } else
290         var_set_variable("output_driver", default_port);
291
292 }