source: trunk/athena/bin/attach/zephyr.c @ 6255

Revision 6255, 3.0 KB checked in by probe, 32 years ago (diff)
Accomodate POSIX-style signal handling
Line 
1/*      Created by:     Robert French
2 *
3 *      $Source: /afs/dev.mit.edu/source/repository/athena/bin/attach/zephyr.c,v $
4 *      $Author: probe $
5 *
6 *      Copyright (c) 1988 by the Massachusetts Institute of Technology.
7 */
8
9#ifndef lint
10static char rcsid_zephyr_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/attach/zephyr.c,v 1.9 1992-07-31 13:24:57 probe Exp $";
11#endif
12
13#include "attach.h"
14#ifdef ZEPHYR
15#include <zephyr/zephyr.h>
16#include <setjmp.h>
17#include <signal.h>
18
19#ifdef __HIGHC__
20#define min(x,y)        _min(x,y)
21#else
22#define min(x,y)        ((x)<(y)?(x):(y))
23#endif
24
25#define ZEPHYR_MAXONEPACKET 5
26Code_t ZSubscribeTo(), ZUnsubscribeTo();
27
28static ZSubscription_t subs[ZEPHYR_MAXSUBS];
29static int num_subs = 0;
30
31static jmp_buf  timeout;
32
33static sig_catch zephyr_timeout()
34{
35        longjmp(timeout, 1);
36}
37
38static int zephyr_op(func)
39    Code_t (*func)();
40{
41    static int inited = 0;
42    static int wgport = 0;
43    int count, count2;
44    ZSubscription_t shortsubs[ZEPHYR_MAXONEPACKET];
45    Code_t retval;
46#ifdef POSIX
47    struct sigaction newsig, oldsig;
48#else
49    sig_catch   (*oldsig)();
50#endif
51
52    if (setjmp(timeout))
53            return 1;           /* We timed out, punt */
54
55#ifdef POSIX
56    newsig.sa_handler = zephyr_timeout;
57    sigemptyset(&newsig.sa_mask);
58    sigaddset(&newsig.sa_mask, SIGALRM);
59    newsig.sa_flags = 0;
60    sigaction(SIGALRM, &newsig, &oldsig);
61#else
62    oldsig = signal(SIGALRM, zephyr_timeout);
63#endif
64    alarm(ZEPHYR_TIMEOUT);
65   
66    if (inited < 0)
67        return 1;
68
69    if (!num_subs)
70        return 0;       /* Can't lose if doing nothing */
71
72    if (!inited) {
73        if ((retval = ZInitialize()) != ZERR_NONE) {
74            com_err(progname, retval, "while intializing Zephyr library");
75            inited = -1;
76            return 1;
77        }
78        if ((wgport = ZGetWGPort()) == -1) {
79            /*
80             * Be quiet about windowgram lossage
81             */
82            inited = -1;
83            return 1;
84        }
85        inited = 1;
86    }
87
88    for (count=0; count<num_subs; count += ZEPHYR_MAXONEPACKET) {
89        for (count2=0; count2<min(ZEPHYR_MAXONEPACKET,num_subs-count); count2++)
90            shortsubs[count2] = subs[count+count2];
91        if ((retval = (func)(shortsubs,
92                             min(ZEPHYR_MAXONEPACKET,num_subs-count),
93                             wgport)) != ZERR_NONE) {
94            fprintf(stderr, "Error while subscribing: %s\n",
95                    error_message(retval));
96            inited = -1;
97            return 1;
98        }
99    }
100    alarm(0);
101#ifdef POSIX
102    sigaction(SIGALRM, &oldsig, (struct sigaction *)0);
103#else
104    (void) signal(SIGALRM, oldsig);
105#endif
106    return 0;
107}
108
109void zephyr_addsub(class)
110    const char *class;
111{
112    if (num_subs == ZEPHYR_MAXSUBS)
113        return;
114   
115    if (debug_flag)
116            printf("Subscribing to zephyr instance %s.\n", class);
117    subs[num_subs].zsub_recipient = "*";
118    subs[num_subs].zsub_classinst = strdup(class);
119    subs[num_subs].zsub_class = ZEPHYR_CLASS;
120    num_subs++;
121}
122
123int zephyr_sub(iszinit)
124int iszinit;
125{
126    if(zephyr_op(ZSubscribeTo) && iszinit)
127      {
128        error_status = ERR_ZINITZLOSING;
129        return FAILURE;
130      }
131    return SUCCESS;
132}
133
134int zephyr_unsub(iszinit)
135int iszinit;
136{
137    if(zephyr_op(ZUnsubscribeTo) && iszinit)
138      {
139        error_status = ERR_ZINITZLOSING;
140        return FAILURE;
141      }
142    return SUCCESS;
143}
144#endif
Note: See TracBrowser for help on using the repository browser.