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>
7 * $Id: zephyr.c,v 1.10 1999/08/13 00:19:52 danw Exp $
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_zephyr_c[] = "$Id: zephyr.c,v 1.10 1999/08/13 00:19:52 danw Exp $";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Module containing code dealing with zephyr: */
26 /****************************************************************************/
28 #include <zephyr/zephyr.h>
29 #include <sys/socket.h>
30 #include "new_string.h"
34 #include "subscriptions.h"
35 #include "variables.h"
38 #ifndef X_DISPLAY_MISSING
43 extern int zwgc_debug;
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.
58 static string get_zwgc_port_number_filename()
60 static char buffer[40];
64 if (temp = getenv("WGFILE"))
67 sprintf(buffer, "/tmp/wg.%d", getuid());
76 static void handle_zephyr_input(notice_handler)
77 void (*notice_handler)();
80 struct sockaddr_in from;
81 int complete_packets_ready;
85 if ( (complete_packets_ready=ZPending()) < 0 )
86 FATAL_TRAP( errno, "while calling ZPending()" );
88 if (complete_packets_ready==0)
91 notice = malloc(sizeof(ZNotice_t));
93 TRAP( ZReceiveNotice(notice, &from), "while getting zephyr notice" );
95 notice->z_auth = ZCheckAuthentication(notice, &from);
96 notice_handler(notice);
101 static int zephyr_inited = 0;
107 void zephyr_init(notice_handler)
108 void (*notice_handler)();
110 unsigned short port = 0; /* Use any old port */
117 * Initialize zephyr. If error, print error message & exit.
119 FATAL_TRAP( ZInitialize(), "while initializing Zephyr" );
120 FATAL_TRAP( ZOpenPort(&port), "while opening Zephyr port" );
123 * Save away our port number in a special place so that zctl and
124 * other clients can send us control messages: <<<>>>
126 temp = get_zwgc_port_number_filename();
128 port_file = fopen(temp, "r");
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");
135 port_file = fopen(temp, "w");
137 fprintf(port_file, "%d\n", port);
140 fprintf(stderr, "zwgc: error while opening %s for writing: ", temp);
144 /* Set hostname and tty for locations. If we support X, use the
145 * display string for the default tty name. */
146 if (location_override)
147 tty = location_override;
148 #ifndef X_DISPLAY_MISSING
150 tty = DisplayString(dpy);
152 error_code = ZInitLocationInfo(NULL, tty);
153 TRAP( error_code, "while initializing location information" );
156 * Retrieve the user's desired exposure level (from the zephyr variable
157 * "exposure"), convert it to the proper internal form then
158 * set the user's location using it. If the exposure level is
159 * not one of the allowed ones, print an error and treat it as
162 if (temp = ZGetVariable("exposure")) {
163 if (!(exposure = ZParseExposureLevel(temp))) {
164 ERROR2("invalid exposure level %s, using exposure level none instead.\n", temp);
165 exposure = EXPOSE_NONE;
168 exposure = EXPOSE_NONE;
169 error_code = ZSetLocation(exposure); /* <<<>>> */
170 if (error_code != ZERR_LOGINFAIL)
171 TRAP( error_code, "while setting location" );
174 * If the exposure level isn't EXPOSE_NONE, turn on recieving notices.
175 * (this involves reading in the subscription file, etc.)
177 if (string_Neq(exposure, EXPOSE_NONE))
181 * Set $realm to our realm and $user to our zephyr username:
183 var_set_variable("realm", ZGetRealm());
184 var_set_variable("user", ZGetSender());
189 mux_add_input_source(ZGetFD(), (void (*)())handle_zephyr_input,
199 void finalize_zephyr() /* <<<>>> */
205 * Remove the file containing our port # since it is no longer needed:
208 temp = get_zwgc_port_number_filename();
211 fprintf(stderr, "zwgc: error while trying to delete %s: ", temp);
216 * Cancel our subscriptions, unset our location, and close our zephyr
221 TRAP( ZCancelSubscriptions(0), "while canceling subscriptions" );
222 TRAP( ZUnsetLocation(), "while unsetting location" );
225 (void) ZCancelSubscriptions(0);
226 (void) ZUnsetLocation();