Merge branch 'cde-fixups-1' of ssh://git.code.sf.net/p/cdesktopenv/code into cde...
[oweals/cde.git] / cde / programs / ttsnoop / tt_c++.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 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.
27 //%%  $XConsortium: tt_c++.C /main/3 1995/11/06 17:14:22 rswiston $
28
29 #include <string.h>
30 #include <Tt/tttk.h>
31 #include "tt_c++.h"
32
33 char *
34 tt_status_string(
35         Tt_status status
36 )
37 {
38         char *string = tt_status_message( status );
39         if (string == 0) {
40                 return 0;
41         }
42         char *tab = strchr( string, '\t' );
43         if (tab != 0) {
44                 *tab = '\0';
45         }
46         return string;
47 }
48
49 ostream &
50 operator<<(
51         ostream & sink,
52         Tt_status status2Print
53 )
54 {
55         char *string = tt_status_string( status2Print );
56         sink << string;
57         tt_free( string );
58         return sink;
59 }
60
61 ostream &
62 operator<<(
63         ostream & sink,
64         Tt_scope scope
65 )
66 {
67         char *string = 0;
68         switch (scope) {
69             case TT_SCOPE_NONE:         string = "TT_SCOPE_NONE";       break;
70             case TT_SESSION:            string = "TT_SESSION";          break;
71             case TT_FILE:               string = "TT_FILE";             break;
72             case TT_BOTH:               string = "TT_BOTH";             break;
73             case TT_FILE_IN_SESSION:    string = "TT_FILE_IN_SESSION";  break;
74         }
75         if (string != 0) {
76                 sink << string;
77         } else {
78                 sink << "(Tt_scope)" << (int)scope;
79         }
80         return sink;
81 }
82
83 ostream &
84 operator<<(
85         ostream & sink,
86         Tt_class _class
87 )
88 {
89         char *string = 0;
90         switch (_class) {
91             case TT_CLASS_UNDEFINED:    string = "TT_CLASS_UNDEFINED";  break;
92             case TT_NOTICE:             string = "TT_NOTICE";           break;
93             case TT_REQUEST:            string = "TT_REQUEST";          break;
94             case TT_OFFER:              string = "TT_OFFER";            break;
95         }
96         if (string != 0) {
97                 sink << string;
98         } else {
99                 sink << "(Tt_class)" << (int)_class;
100         }
101         return sink;
102 }
103
104 ostream &
105 operator<<(
106         ostream & sink,
107         Tt_mode mode
108 )
109 {
110         char *string = 0;
111         switch (mode) {
112             case TT_MODE_UNDEFINED:     string = "TT_MODE_UNDEFINED";   break;
113             case TT_IN:                 string = "TT_IN";               break;
114             case TT_OUT:                string = "TT_OUT";              break;
115             case TT_INOUT:              string = "TT_INOUT";            break;
116         }
117         if (string != 0) {
118                 sink << string;
119         } else {
120                 sink << "(Tt_mode)" << (int)mode;
121         }
122         return sink;
123 }
124
125 ostream &
126 operator<<(
127         ostream & sink,
128         Tt_address address
129 )
130 {
131         char *string = 0;
132         switch (address) {
133             case TT_PROCEDURE:          string = "TT_PROCEDURE";        break;
134             case TT_OBJECT:             string = "TT_OBJECT";           break;
135             case TT_HANDLER:            string = "TT_HANDLER";          break;
136             case TT_OTYPE:              string = "TT_OTYPE";            break;
137         }
138         if (string != 0) {
139                 sink << string;
140         } else {
141                 sink << "(Tt_address)" << (int)address;
142         }
143         return sink;
144 }
145
146 ostream &
147 operator<<(
148         ostream & sink,
149         Tt_callback_action action
150 )
151 {
152         char *string = 0;
153         switch (action) {
154             case TT_CALLBACK_CONTINUE:  string = "TT_CALLBACK_CONTINUE";break;
155             case TT_CALLBACK_PROCESSED: string = "TT_CALLBACK_PROCESSED";break;
156         }
157         if (string != 0) {
158                 sink << string;
159         } else {
160                 sink << "(Tt_callback_action)" << (int)action;
161         }
162         return sink;
163 }
164
165 Tt_message
166 tt_message_copy(
167         Tt_message msg1
168 )
169 {
170         if (tt_ptr_error( msg1 ) != TT_OK) {
171                 return msg1;
172         }
173         Tt_message msg2 = tt_message_create();
174         Tt_status status = tt_ptr_error( msg2 );
175         if (tt_ptr_error( msg2 ) != TT_OK) {
176                 return msg2;
177         }
178         int contexts = tt_message_contexts_count( msg1 );
179         status = tt_int_error( contexts );
180         if (status != TT_OK) {
181                 tt_message_destroy( msg2 );
182                 return (Tt_message)tt_error_pointer( status );
183         }
184         for (int context = 0; context < contexts; context++) {
185                 char *slot = tt_message_context_slotname( msg1, context );
186                 int ival;
187                 status = tt_message_context_ival( msg1, slot, &ival );
188                 switch (status) {
189                         unsigned char *bval;
190                         int len;
191                     case TT_OK:
192                         tt_message_icontext_set( msg2, slot, ival );
193                         break;
194                     case TT_ERR_NUM:
195                         tt_message_context_bval( msg1, slot, &bval, &len );
196                         tt_message_bcontext_set( msg2, slot, bval, len );
197                         tt_free( (caddr_t)bval );
198                         break;
199                 }
200                 tt_free( slot );
201         }
202         int args = tt_message_args_count( msg1 );
203         status = tt_int_error( args );
204         if (status != TT_OK) {
205                 tt_message_destroy( msg2 );
206                 return (Tt_message)tt_error_pointer( status );
207         }
208         for (int arg = 0; arg < args; arg++) {
209                 Tt_mode mode = tt_message_arg_mode( msg1, arg );
210                 char *vtype = tt_message_arg_type( msg1, arg );
211                 int ival;
212                 status = tt_message_arg_ival( msg1, arg, &ival );
213                 switch (status) {
214                         unsigned char *bval;
215                         int len;
216                     case TT_OK:
217                         tt_message_iarg_add( msg2, mode, vtype, ival );
218                         break;
219                     case TT_ERR_NUM:
220                         tt_message_arg_bval( msg1, arg, &bval, &len );
221                         tt_message_barg_add( msg2, mode, vtype, bval, len );
222                         tt_free( (caddr_t)bval );
223                         break;
224                 }
225                 tt_free( vtype );
226         }
227
228         tt_message_class_set( msg2, tt_message_class( msg1 ));
229
230         char *file = tt_message_file( msg1 );
231         if ((tt_ptr_error( file ) == TT_OK) && (file != 0)) {
232                 tt_message_file_set( msg2, file );
233                 tt_free( file );
234         }
235
236         char *object = tt_message_object( msg1 );
237         if ((tt_ptr_error( object ) == TT_OK) && (object != 0)) {
238                 tt_message_object_set( msg2, object );
239                 tt_free( object );
240         }
241
242         char *op = tt_message_op( msg1 );
243         if ((tt_ptr_error( op ) == TT_OK) && (op != 0)) {
244                 tt_message_op_set( msg2, op );
245                 tt_free( op );
246         }
247
248         char *otype = tt_message_otype( msg1 );
249         if ((tt_ptr_error( otype ) == TT_OK) && (otype != 0)) {
250                 tt_message_otype_set( msg2, otype );
251                 tt_free( otype );
252         }
253
254         tt_message_address_set( msg2, tt_message_address( msg1 ));
255
256         // XXX skip handler, handler_ptype, sender
257
258         tt_message_scope_set( msg2, tt_message_scope( msg1 ));
259
260         char *sender_ptype = tt_message_sender_ptype( msg1 );
261         if ((tt_ptr_error( sender_ptype ) == TT_OK) && (sender_ptype != 0)) {
262                 tt_message_sender_ptype_set( msg2, sender_ptype );
263                 tt_free( sender_ptype );
264         }
265
266         char *session = tt_message_session( msg1 );
267         if ((tt_ptr_error( session ) == TT_OK) && (session != 0)) {
268                 tt_message_session_set( msg2, session );
269                 tt_free( session );
270         }
271
272         tt_message_status_set( msg2, tt_message_status( msg1 ));
273
274         char *status_string = tt_message_status_string( msg1 );
275         if ((tt_ptr_error( status_string ) == TT_OK) && (status_string != 0)) {
276                 tt_message_status_string_set( msg2, status_string );
277                 tt_free( status_string );
278         }
279
280         return msg2;
281 }
282
283 Tt_message
284 tttk_message_create(
285         const char *optext
286 )
287 {
288         Tttk_op op = tttk_string_op( optext );
289         Tt_class theClass;
290         switch (op) {
291             default:
292                 theClass = TT_REQUEST;
293                 break;
294             case TTDT_CREATED:
295             case TTDT_DELETED:
296             case TTDT_MODIFIED:
297             case TTDT_REVERTED:
298             case TTDT_MOVED:
299             case TTDT_SAVED:
300             case TTDT_STARTED:
301             case TTDT_STOPPED:
302             case TTDT_STATUS:
303                 theClass = TT_NOTICE;
304                 break;
305         }
306         Tt_scope scope;
307         switch (op) {
308             default:
309                 scope = TT_SESSION;
310                 break;
311             case TTDT_CREATED:
312             case TTDT_DELETED:
313             case TTDT_MODIFIED:
314             case TTDT_REVERTED:
315             case TTDT_MOVED:
316             case TTDT_SAVED:
317             case TTDT_GET_MODIFIED:
318             case TTDT_SAVE:
319             case TTDT_REVERT:
320                 scope = TT_BOTH;
321                 break;
322         }
323         Tt_message msg = tttk_message_create( 0, theClass, scope,
324                                               0, optext, 0 );
325         Tt_mode mode = TT_INOUT;
326         switch (op) {
327             default:
328                 break;
329             case TTDT_MOVED:
330                 tt_message_arg_add( msg, TT_IN, Tttk_file, 0 );
331                 // fall through
332             case TTDT_CREATED:
333             case TTDT_DELETED:
334             case TTDT_MODIFIED:
335             case TTDT_REVERTED:
336             case TTDT_SAVED:
337             case TTDT_GET_MODIFIED:
338             case TTDT_SAVE:
339             case TTDT_REVERT:
340                 tt_message_arg_add( msg, TT_IN, Tttk_file, 0 );
341                 break;
342             case TTDT_GET_SYSINFO:
343                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
344                 // fall through
345             case TTDT_GET_STATUS:
346                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
347                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
348                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
349                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
350                 break;
351             case TTDT_STATUS:
352                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
353                 // fall through
354             case TTDT_STARTED:
355             case TTDT_STOPPED:
356                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
357                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
358                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
359                 break;
360             case TTDT_GET_ICONIFIED:
361             case TTDT_GET_MAPPED:
362                 tt_message_arg_add( msg, TT_OUT, Tttk_boolean, 0 );
363                 break;
364             case TTDT_QUIT:
365                 tt_message_arg_add( msg, TT_IN, Tttk_boolean, 0 );
366                 // fall through
367             case TTDT_SET_MAPPED:
368             case TTDT_SET_ICONIFIED:
369                 tt_message_arg_add( msg, TT_IN, Tttk_boolean, 0 );
370                 break;
371             case TTDT_GET_GEOMETRY:
372                 mode = TT_OUT;
373             case TTDT_SET_GEOMETRY:
374                 tt_message_arg_add( msg, mode, Tttk_width, 0 );
375                 tt_message_arg_add( msg, mode, Tttk_height, 0 );
376                 tt_message_arg_add( msg, mode, Tttk_xoffset, 0 );
377                 tt_message_arg_add( msg, mode, Tttk_yoffset, 0 );
378                 break;
379             case TTDT_GET_XINFO:
380                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
381                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
382                 tt_message_arg_add( msg, TT_OUT, Tttk_integer, 0 );
383                 break;
384             case TTDT_GET_SITUATION:
385                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
386                 break;
387             case TTDT_SIGNAL:
388             case TTDT_SET_SITUATION:
389                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
390                 break;
391             case TTDT_DO_COMMAND:
392             case TTDT_GET_ENVIRONMENT:
393             case TTDT_GET_LOCALE:
394                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
395                 tt_message_arg_add( msg, TT_OUT, Tttk_string, 0 );
396                 break;
397             case TTDT_SET_LOCALE:
398             case TTDT_SET_ENVIRONMENT:
399                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
400                 tt_message_arg_add( msg, TT_IN, Tttk_string, 0 );
401                 break;
402         }
403         return msg;
404 }
405
406 int
407 tt_procid_fd(
408         const char *procid
409 )
410 {
411         char *defaultProcid = tt_default_procid();
412         tt_default_procid_set( procid );
413         int val2return = tt_fd();
414         if (! tt_is_err( tt_ptr_error( defaultProcid ))) {
415                 tt_default_procid_set( defaultProcid );
416         }
417         return val2return;
418 }