libDtSvc: Resolve 89 compiler warnings.
[oweals/cde.git] / cde / lib / DtSvc / DtEncap / spc-xt.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * File:         spc-xt.c $TOG: spc-xt.c /main/6 1998/03/16 14:41:02 mgreess $
25  * Language:     C
26  *
27  * (c) Copyright 1989, Hewlett-Packard Company, all rights reserved.
28  *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #define __need_fd_set
36
37 #include <bms/sbport.h> /* NOTE: sbport.h must be the first include. */
38 #include <time.h>
39 #include <SPC/spcP.h>
40 /* #include <bms/SbEvent.h>  */ /* This file now included by spcP.h */
41 #include "DtSvcLock.h"
42
43 /* Externals */
44
45 extern int break_on_termination;
46
47 /* Utility functions */
48
49 /* First, declarations */
50
51 typedef struct {
52   SbInputId                     read_id;
53   SbInputId                     except_id;
54 } SPC_Callback_Struct;
55
56 static SPC_Callback_Struct **SPC_Fd_Mapping = NULL;
57
58 #define SPC_LOOKUP_FD_MAPPING(fd)   SPC_Fd_Mapping[(fd)]
59
60 static Boolean spc_xe_termination_flag;
61
62 /*-----------------------------------------------------------------------+*/
63 static int SPC_AddInput(int                     source,
64                  SPC_Callback_Condition         condition,
65                  SbInputId                      id)
66 /*-----------------------------------------------------------------------+*/
67 {
68   SPC_Callback_Struct *structptr = NULL;
69
70   _DtSvcProcessLock();
71   if (SPC_Fd_Mapping == NULL) {
72     SPC_Fd_Mapping = (SPC_Callback_Struct **) 
73                      XeMalloc (FD_SETSIZE * sizeof (SPC_Callback_Struct *));
74     memset(SPC_Fd_Mapping, 0, FD_SETSIZE * sizeof(SPC_Callback_Struct *));
75   }
76   structptr=SPC_LOOKUP_FD_MAPPING(source);
77
78   if(!structptr) {
79     structptr=(SPC_Callback_Struct *) XeMalloc(sizeof(SPC_Callback_Struct));
80     SPC_LOOKUP_FD_MAPPING(source)=structptr;
81   }
82   _DtSvcProcessUnlock();
83
84   switch (condition) {
85
86   case SPC_Input:
87   case SPC_Terminator:
88   case SPC_Client:
89     structptr->read_id   = id;
90     break;
91
92   case SPC_Exception:
93     structptr->except_id = id;
94     break;
95
96   default:
97     break;
98   }
99
100   return(source);
101 }
102
103
104 /*-----------------------------------------------------------------------+*/
105 static SbInputId SPC_RemoveInput(int                     source,
106                           SPC_Callback_Condition condition)
107 /*-----------------------------------------------------------------------+*/
108 {
109   SPC_Callback_Struct *structptr = NULL;
110   
111   _DtSvcProcessLock();
112   if (SPC_Fd_Mapping == NULL) {
113     SPC_Fd_Mapping = (SPC_Callback_Struct **) 
114                      XeMalloc (FD_SETSIZE * sizeof (SPC_Callback_Struct *));
115     memset(SPC_Fd_Mapping, 0, FD_SETSIZE * sizeof(SPC_Callback_Struct *));
116   }
117   structptr=SPC_LOOKUP_FD_MAPPING(source);
118   _DtSvcProcessUnlock();
119
120   switch(condition) {
121     
122   case SPC_Input:
123   case SPC_Terminator:
124   case SPC_Client:
125      return structptr->read_id;
126
127   case SPC_Exception:
128      return structptr->except_id;
129     
130   }
131
132   return 0;
133 }
134   
135 /*-----------------------------------------------------------------------+*/
136 static SPC_Select(void )
137 /*-----------------------------------------------------------------------+*/
138 {
139     break_on_termination=TRUE;
140
141     _DtSvcProcessLock();
142     spc_xe_termination_flag= FALSE;
143
144     /* Use a function pointer so we don't have explict dependancy */
145     /* on libXe.a                                                   */
146     /* ---------------------------------------------------------- */
147     if (SbMainLoopUntil_hookfn == NULL)
148       (void) fprintf (stderr, "Error: SbMainLoopUntil = NULL\n");
149     else
150       (*SbMainLoopUntil_hookfn)(&spc_xe_termination_flag);
151     _DtSvcProcessUnlock();
152
153     break_on_termination=FALSE;
154     return(TRUE);
155 }
156
157
158 /*-----------------------------------------------------------------------+*/
159 int SPC_Wait_For_Termination(SPC_Channel_Ptr channel)
160 /*-----------------------------------------------------------------------+*/
161 {
162   int result;
163   
164   call_parent_method(channel, wait_for_termination, (channel), result);
165   
166   if(result==SPC_ERROR) return(SPC_ERROR);
167
168   do {
169
170     if(SPC_Select() == SPC_ERROR)
171       return(SPC_ERROR);
172     
173   } while(IS_ACTIVE(channel));
174   
175   return(TRUE);
176   
177 }
178
179
180 /*-----------------------------------------------------------------------+*/
181 void SPC_XtBreak(void)
182 /*-----------------------------------------------------------------------+*/
183 {
184   _DtSvcProcessLock();
185   if(!spc_xe_termination_flag) {
186      spc_xe_termination_flag = TRUE;
187      if (SbBreakMainLoop_hookfn == NULL)
188        (void) fprintf (stderr, "Error: SbBreakMainLoop = NULL\n");
189      else
190        (*SbBreakMainLoop_hookfn)();      
191   } 
192   _DtSvcProcessUnlock();
193 }
194
195
196   
197 /*-----------------------------------------------------------------------+*/
198 void SPC_XtAddInput(SPC_Channel_Ptr channel,
199                     int *id_addr,
200                     int fd,
201                     spc_handler_func_type handler,
202                     SPC_Callback_Condition condition)
203 /*-----------------------------------------------------------------------+*/
204 {
205   SbInputId id;
206   
207   switch(condition) {
208     
209   case SPC_Input:
210   case SPC_Terminator:
211   case SPC_Client:
212     /* fprintf(stderr, "SPC add input/terminator for %d\n", fd); */
213     if (SbAddInput_hookfn == NULL)
214       (void) fprintf (stderr, "Error: SbAddInput = NULL\n");
215     else
216       id = (*SbAddInput_hookfn)(fd, handler, channel);
217     break;
218     
219   case SPC_Exception:
220     /* fprintf(stderr, "SPC add exception for %d\n", fd); */
221     if (SbAddException_hookfn == NULL)
222       (void) fprintf (stderr, "Error: SbAddException = NULL\n");
223     else
224       id = (*SbAddException_hookfn)(fd, handler, channel);
225     break;
226     
227   }
228   
229   *id_addr=SPC_AddInput(fd, condition, id);
230 }
231
232
233 /*-----------------------------------------------------------------------+*/
234 void SPC_XtRemoveInput(int *id_addr, 
235                        SPC_Callback_Condition condition)
236 /*-----------------------------------------------------------------------+*/
237 {
238   if((*id_addr) != -1) {
239     
240     switch(condition) {
241       
242     case SPC_Input:
243     case SPC_Terminator:
244     case SPC_Client:
245       /* fprintf(stderr, "SPC remove input/terminator\n"); */
246       if (SbRemoveInput_hookfn == NULL)
247         (void) fprintf (stderr, "Error: SbRemoveInput = NULL\n");
248       else
249         (*SbRemoveInput_hookfn)(SPC_RemoveInput(*id_addr, condition));
250       break;
251       
252     case SPC_Exception:
253       /* fprintf(stderr, "SPC remove exception"); */
254       if (SbRemoveException_hookfn == NULL)
255         (void) fprintf (stderr, "Error: SbRemoveException = NULL\n");
256       else
257         (*SbRemoveException_hookfn)(SPC_RemoveInput(*id_addr, condition));
258       break;
259       
260     }
261     
262     *id_addr=(-1);
263   }
264 }