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