Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / lib / DtSvc / DtEncap / local.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 libraries 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:         local.c $TOG: local.c /main/5 1999/10/14 15:05:57 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_timeval   /* Needed for "struct timeval" from <time.h>. */
36 #define __need_fd_set
37
38 #include <bms/sbport.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <signal.h>
42
43 #include <SPC/spcP.h>
44 #include <SPC/spc-proto.h>
45
46 /*
47  **
48  ** Note that the close routines call the parent method AFTER the
49  ** work done for the child method.  This is because the parent method
50  ** will do all the deallocation.
51  **
52 */
53
54 /*----------------------------------------------------------------------+*/
55 int close_local_channel_object(SPC_Channel_Ptr channel)
56 /*----------------------------------------------------------------------+*/
57 {
58   Wire *wirelist;
59   int result;
60   
61   for(wirelist=channel->wire_list; wirelist; wirelist=wirelist->next){
62     spc_close(wirelist->fd[READ_SIDE]);
63     spc_close(wirelist->fd[WRITE_SIDE]);
64     SPC_XtRemoveInput(&wirelist->read_toolkit_id, SPC_Input);
65     SPC_XtRemoveInput(&wirelist->except_toolkit_id, SPC_Exception);
66   }
67
68   call_parent_method(channel, close, (channel), result);
69
70   if(result==SPC_ERROR)
71     return(SPC_ERROR);
72
73   return(TRUE);
74 }
75
76 /*----------------------------------------------------------------------+*/
77 int write_local_channel_object(SPC_Channel_Ptr channel,
78                                XeString buffer,
79                                int nbytes)
80 /*----------------------------------------------------------------------+*/
81   
82 {
83   int result;
84   
85   call_parent_method(channel,
86                      write,
87                      (channel, buffer, nbytes),
88                      result);
89
90   if(result==SPC_ERROR)
91     return(SPC_ERROR);
92   result = SPC_Write_Chars(channel->file_descs[STDIN], buffer, nbytes);
93   if(result==ERROR) {
94     SPC_Error(SPC_Writing);
95     return(SPC_ERROR);
96   }
97   
98   return(result);
99 }
100
101 /* the function exec_proc_local_channel_object is defined in spc-exec.c */
102
103 /*----------------------------------------------------------------------+*/
104 int signal_local_channel_object (SPC_Channel_Ptr channel,
105                                  int sig)
106 /*----------------------------------------------------------------------+*/
107
108 {
109   int result;
110   
111   call_parent_method(channel, signal, (channel, sig), result);
112
113   if(result==SPC_ERROR)
114     return(SPC_ERROR);
115
116   if(sig == SIGKILL || IS_SPCIO_SIGNAL_PGRP(channel->IOMode))
117     result=kill(-(channel->pid), sig);
118   else
119     result=kill(channel->pid, sig);
120
121   if(result==ERROR)
122     return(errno!=ESRCH);
123
124   return(TRUE);
125 }
126
127 /*----------------------------------------------------------------------+*/
128 int local_channel_object_wait_for_termination(SPC_Channel_Ptr channel)
129 /*----------------------------------------------------------------------+*/
130 {
131
132   int result;
133   
134   call_parent_method(channel, wait_for_termination, (channel), result);
135   
136   if(result==SPC_ERROR)
137     return(SPC_ERROR);
138   
139   /* Do we need to check for remote channel input here? */
140   
141   while(IS_ACTIVE(channel)) {
142     sigset_t mask;
143     sigemptyset(&mask);
144     /* the SIGCLD signal handler will take care of us here */
145     sigsuspend(&mask);
146   }
147
148   return(TRUE);
149   
150 }
151
152 /*----------------------------------------------------------------------+*/
153 int remove_logfile_local_channel_object(SPC_Channel_Ptr channel)
154 /*----------------------------------------------------------------------+*/
155 {
156   int result;
157
158   call_parent_method(channel, remove_logfile, (channel), result);
159   
160   if(unlink(channel->logfile)==ERROR) {
161     SPC_Error(SPC_Unlink_Logfile);
162     return(SPC_ERROR);
163   }
164
165   /* This is malloc'ed memory from open_noio_channel_object() and tempnam() */
166   XeFree(channel->logfile);
167   
168   return(TRUE);
169 }
170
171 extern SPC_Channel_Ptr spc_activation_list;
172
173 /* All this routine does is to look up the channel, and
174    call the generic input handler routine */
175
176 /*----------------------------------------------------------------------+*/
177 void local_channel_object_input_handler(void * client_data,
178                                    int *source,
179                                    SPCInputId * UNUSED_PARM(id))
180 /*----------------------------------------------------------------------+*/
181 {
182
183 /* WARNING!!! This routine is NOT XPG3 compliant.  The timeval struct */
184 /*            is the problem here.                                    */
185
186   SPC_Channel_Ptr channel=(SPC_Channel_Ptr) client_data;
187   int fd=(*source);
188   int connector;
189   int len;
190   fd_set read_fd_vect, except_fd_vect;
191   SPC_Channel_Ptr tmp, this_ptr;
192   struct timeval timeout;                 /* Not part of XPG3 !!! */
193
194   /* This ^&@$#% select is here to get around an X toolkit bug */
195
196   FD_ZERO(&read_fd_vect);
197   FD_ZERO(&except_fd_vect);
198
199   FD_SET(fd, &read_fd_vect);
200   FD_SET(fd, &except_fd_vect);
201
202   timeout.tv_sec = 0;
203   timeout.tv_usec = 0;
204   
205 #if defined(SVR4) || defined(__hpux) || defined(__OpenBSD__) || defined(__linux__)
206   select(max_fds, (fd_set*)&read_fd_vect, NULL, (fd_set*)&except_fd_vect, &timeout);
207 #else
208   /* UX has select defined with int*, not fd_set* parms */
209   select(max_fds, (int*)&read_fd_vect, NULL, (int*)&except_fd_vect, &timeout);
210 #endif
211   if(! (FD_ISSET(fd, &read_fd_vect) || FD_ISSET(fd, &except_fd_vect))) {
212     return /* (FALSE) */;
213   }
214
215   /* The following is to get around an apparent Xt bug where sometimes
216      the client data pointer passed to me is not the one I was expecting.
217      */
218   
219   tmp = spc_activation_list;
220   this_ptr = NULL;
221   while(tmp) {
222     if((fd == Stdin(tmp)) || (fd == Stderr(tmp)))
223       this_ptr = tmp;
224     tmp = tmp->next;
225   }
226   
227   if(this_ptr == NULL)
228     this_ptr = channel;
229   if(this_ptr != channel)
230     channel = this_ptr;
231   
232   if((connector=SPC_fd_to_connector(channel, fd)) == ERROR) {
233     SPC_Error(SPC_Bad_Fd);
234     return /* (SPC_ERROR) */;
235   }
236   len = SPC_Input_Handler(channel, connector);
237   return /* (len) */;
238 }
239
240 int local_channel_object_send_eof(SPC_Channel_Ptr channel)
241 {
242   Wire *wire = channel->wires[STDIN];
243
244   spc_close(wire->fd[READ_SIDE]);
245   spc_close(wire->fd[WRITE_SIDE]);
246   SPC_XtRemoveInput(&wire->read_toolkit_id, SPC_Input);
247   SPC_XtRemoveInput(&wire->except_toolkit_id, SPC_Exception);
248
249   return(TRUE);
250 }