]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zwgc/zephyr.c
This commit was generated by cvs2svn to compensate for changes in r127,
[1ts-debian.git] / zwgc / zephyr.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: zephyr.c,v 1.12 2000/10/13 23:07:31 ghudson Exp $
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_zephyr_c[] = "$Id: zephyr.c,v 1.12 2000/10/13 23:07:31 ghudson Exp $";
18 #endif
19
20 #include <zephyr/mit-copyright.h>
21
22 /****************************************************************************/
23 /*                                                                          */
24 /*               Module containing code dealing with zephyr:                */
25 /*                                                                          */
26 /****************************************************************************/
27
28 #include <zephyr/zephyr.h>
29 #include <sys/socket.h>
30 #include "new_string.h"
31 #include "zephyr.h"
32 #include "error.h"
33 #include "mux.h"
34 #include "subscriptions.h"
35 #include "variables.h"
36 #include "pointer.h"
37 #include "main.h"
38 #ifndef X_DISPLAY_MISSING
39 #include "X_driver.h"
40 #endif
41
42 #ifdef DEBUG
43 extern int zwgc_debug;
44 #endif /* DEBUG */
45
46 /*
47  *  Internal Routine:
48  *
49  *    string get_zwgc_port_number_filename()
50  *        Effects: Returns the filename that the zwgc port # is/should be
51  *                 stored in, based on the user's uid & the environment
52  *                 variable WGFILE.  The returned string points into a
53  *                 static buffer that may change on further calls to this
54  *                 routine or getenv.  The returned string should not be
55  *                 modified in any way.
56  */
57
58 static string get_zwgc_port_number_filename()
59 {
60     static char buffer[40];
61     char *temp;
62     char *getenv();
63
64     if (temp = getenv("WGFILE"))
65       return(temp);
66     else {
67         sprintf(buffer, "/tmp/wg.%d", getuid());
68         return(buffer);
69     }
70 }
71
72 /*
73  *
74  */
75
76 static void handle_zephyr_input(notice_handler)
77      void (*notice_handler)();
78 {
79     ZNotice_t *notice;
80     struct sockaddr_in from;
81     int complete_packets_ready;
82
83     for (;;) {
84         errno = 0;
85         if ( (complete_packets_ready=ZPending()) < 0 )
86           FATAL_TRAP( errno, "while calling ZPending()" );
87     
88         if (complete_packets_ready==0)
89           return;
90
91         notice = malloc(sizeof(ZNotice_t));
92
93         TRAP( ZReceiveNotice(notice, &from), "while getting zephyr notice" );
94         if (!error_code) {
95             notice->z_auth = ZCheckAuthentication(notice, &from);
96             notice_handler(notice);
97         }
98     }
99 }
100
101 static int zephyr_inited = 0;
102     
103 /*
104  *
105  */
106
107 void zephyr_init(notice_handler)
108      void (*notice_handler)();
109 {
110     unsigned short port = 0;           /* Use any old port */
111     char *temp;
112     char *exposure;
113     char *tty = NULL;
114     FILE *port_file;
115
116     /*
117      * Initialize zephyr.  If error, print error message & exit.
118      */
119     FATAL_TRAP( ZInitialize(), "while initializing Zephyr" );
120     FATAL_TRAP( ZOpenPort(&port), "while opening Zephyr port" );
121
122     /*
123      * Save away our port number in a special place so that zctl and
124      * other clients can send us control messages: <<<>>>
125      */
126     temp = get_zwgc_port_number_filename();
127     errno = 0;
128     port_file = fopen(temp, "r");
129     if (port_file) {
130         fprintf(stderr, "zwgc: windowgram file already exists.  If you are\n");
131         fprintf(stderr, "zwgc: not already running zwgc, delete %s\n", temp);
132         fprintf(stderr, "zwgc: and try again.\n");
133         exit(1);
134     }
135
136     /* Set hostname and tty for locations.  If we support X, use the
137      * display string for the default tty name. */
138     if (location_override)
139         tty = location_override;
140 #ifndef X_DISPLAY_MISSING
141     else if (dpy)
142         tty = DisplayString(dpy);
143 #endif
144     error_code = ZInitLocationInfo(NULL, tty);
145     TRAP( error_code, "while initializing location information" );
146
147     /*
148      * Retrieve the user's desired exposure level (from the zephyr variable
149      * "exposure"), convert it to the proper internal form then 
150      * set the user's location using it.  If the exposure level is
151      * not one of the allowed ones, print an error and treat it as
152      * EXPOSE_NONE.
153      */
154
155     error_code = set_exposure("*", exposure = ZGetVariable("exposure"));
156     if (error_code)
157       TRAP( error_code, "while setting location" );
158
159     /*
160      * If the exposure level isn't EXPOSE_NONE, turn on receiving notices.
161      * (this involves reading in the subscription file, etc.)
162      */
163     if (string_Neq(exposure, EXPOSE_NONE))
164       zwgc_startup();
165
166     /*
167      * Set $galaxy to our galaxy and $user to our zephyr username:
168      */
169     var_set_variable("galaxy", ZGetDefaultGalaxy());
170     var_set_variable("user", ZGetSender());
171
172     /*
173      * <<<>>>
174      */
175     mux_add_input_source(ZGetFD(), (void (*)())handle_zephyr_input,
176                          notice_handler);
177     zephyr_inited = 1;
178
179     port_file = fopen(temp, "w");
180     if (port_file) {
181         fprintf(port_file, "%d\n", port);
182         fclose(port_file);
183     } else {
184         fprintf(stderr, "zwgc: error while opening %s for writing: ", temp);
185         perror("");
186     }
187
188     return;
189 }
190
191 /*
192  *
193  */
194
195 void finalize_zephyr() /* <<<>>> */
196 {
197     string temp;
198     int i, cnt;
199     char *galaxy;
200
201     if (zephyr_inited) {
202         /*
203          * Remove the file containing our port # since it is no longer needed:
204          */
205         errno = 0;
206         temp = get_zwgc_port_number_filename();
207         unlink(temp);
208         if (errno) {
209             fprintf(stderr, "zwgc: error while trying to delete %s: ", temp);
210             perror("");
211         }
212
213         /*
214          * Cancel our subscriptions, unset our location, and close our zephyr
215          * connection:
216          */
217
218         TRAP(ZGetGalaxyCount(&cnt), "while getting galaxy count");
219         if (error_code)
220             return;
221
222         for (i=0; i<cnt; i++) {
223             TRAP(ZGetGalaxyName(i, &galaxy), "while getting galaxy name");
224             if (error_code)
225                 continue;
226 #ifdef DEBUG
227             if (zwgc_debug) {
228                 TRAP( ZCancelSubscriptions(galaxy, 0),
229                       "while canceling subscriptions" );
230                 TRAP( ZUnsetLocation(galaxy), "while unsetting location" );
231             } else {
232 #endif /* DEBUG */
233                 (void) ZCancelSubscriptions(galaxy, 0);
234                 (void) ZUnsetLocation(galaxy);
235 #ifdef DEBUG
236             }
237 #endif /* DEBUG */
238         }
239         ZClosePort();
240     }
241     return;
242 }