Tooltalk fixes for OpenBSD. This consists mainly of #ifdefs, casts and some small...
[oweals/cde.git] / cde / lib / tt / lib / tttk / tttkpattern.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 //%%  (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: tttkpattern.C /main/3 1995/10/23 10:33:41 rswiston $                                                        
28 /*
29  * @(#)tttkpattern.C    1.3 93/09/07
30  *
31  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
32  */
33 #include "api/c/tt_c.h"
34 #include "tttk/tttk.h"
35 #include "tttk/tttkmessage.h"
36 #include "tttk/tttk2free.h"
37 #include "tttk/tttkutils.h"
38
39 Tt_status
40 _tttk_patterns_destroy(
41         Tt_pattern             *pats
42 )
43 {
44         if (pats == 0) {
45                 return TT_OK;
46         }
47         Tt_status worst = tt_ptr_error( pats );
48         if (worst != TT_OK) {
49                 return worst;
50         }
51         Tt_pattern *pats2free = pats;
52         while (*pats != 0) {
53                 if (tt_ptr_error( *pats ) == TT_OK) {
54                         Tt_status status = tt_pattern_destroy( *pats );
55                         if (status != TT_OK) {
56                                 worst = status;
57                         }
58                 }
59                 pats++;
60         }
61         if (pats2free != 0) {
62                 free( (caddr_t)pats2free );
63         }
64         return worst;
65 }
66
67 //
68 // This is the only pattern callback that we use.  It retrieves and
69 // calls the op-specific internal unmarshalling routine, which in turn
70 // will call the user's op-specific callback.
71 //
72 static Tt_callback_action
73 _ttDtPatternCB(
74         Tt_message msg,
75         Tt_pattern pat
76 )
77 {
78         // XXX optimization: use 1 key, store a struct w/ all 3 datums
79         _TtDtMessageCB cb = (_TtDtMessageCB)
80                 tt_pattern_user( pat, _TttkCBKey );
81         Tt_status status = tt_ptr_error( cb );
82         if (status != TT_OK) {
83                 return TT_CALLBACK_PROCESSED;
84         }
85         if (cb == 0) {
86                 return TT_CALLBACK_CONTINUE;
87         }
88         void *clientCB = tt_pattern_user( pat, _TttkClientCBKey );
89         status = tt_ptr_error( clientCB );
90         if (status != TT_OK) {
91                 return TT_CALLBACK_PROCESSED;
92         }
93         // clientCB is allowed to be 0. cf. TTDT_QUIT in ttdt_session_join
94         void *clientData = tt_pattern_user( pat, _TttkClientDataKey );
95         status = tt_ptr_error( clientData );
96         if (status != TT_OK) {
97                 return TT_CALLBACK_PROCESSED;
98         }
99         msg = (*cb)( msg, pat, clientCB, clientData );
100         return _ttDtCallbackAction( msg );
101 }
102
103 Tt_pattern
104 _ttDtPatternCreate(
105         Tt_category             category,
106         Tt_scope                theScope,
107         int                     addDefaultSess,
108         const char             *file,
109         Tttk_op                 op,
110         _TtDtMessageCB          cb,
111         void                   *clientCB,
112         void                   *clientData,
113         int                     registerIt
114 )
115 {
116         Tt_pattern pat = tt_pattern_create();
117         Tt_status status = tt_ptr_error( pat );
118         if (status != TT_OK) {
119                 return pat;
120         }
121         _TttkItem2Free fuse( pat );
122         status = tt_pattern_category_set( pat, category );
123         if (status != TT_OK) {
124                 return (Tt_pattern)tt_error_pointer( status );
125         }
126         // we only observe notices and only handle requests
127         if (category == TT_HANDLE) {
128                 status = tt_pattern_class_add( pat, TT_REQUEST );
129         }
130         if (category == TT_OBSERVE) {
131                 status = tt_pattern_class_add( pat, TT_NOTICE );
132         }
133         if (status != TT_OK) {
134                 return (Tt_pattern)tt_error_pointer( status );
135         }
136         status = tt_pattern_scope_add( pat, theScope );
137         if (status != TT_OK) {
138                 return (Tt_pattern)tt_error_pointer( status );
139         }
140         if (addDefaultSess) {
141                 char *sess = tt_default_session();
142                 status = tt_pattern_session_add( pat, sess );
143                 tt_free( sess );
144                 if (status != TT_OK) {
145                         return (Tt_pattern)tt_error_pointer( status );
146                 }
147         }
148         if (file != 0) {
149                 status = tt_pattern_file_add( pat, file );
150                 if (status != TT_OK) {
151                         return (Tt_pattern)tt_error_pointer( status );
152                 }
153         }
154         if (op != 0) {
155                 status = tt_pattern_op_add( pat, _ttDtOp( op ) );
156                 if (status != TT_OK) {
157                         return (Tt_pattern)tt_error_pointer( status );
158                 }
159         }
160         status = tt_pattern_callback_add( pat, _ttDtPatternCB );
161         if (status != TT_OK) {
162                 return (Tt_pattern)tt_error_pointer( status );
163         }
164         status = tt_pattern_user_set( pat, _TttkCBKey, (void *)cb );
165         if (status != TT_OK) {
166                 return (Tt_pattern)tt_error_pointer( status );
167         }
168         status = tt_pattern_user_set( pat, _TttkClientCBKey, clientCB );
169         if (status != TT_OK) {
170                 return (Tt_pattern)tt_error_pointer( status );
171         }
172         status = tt_pattern_user_set( pat, _TttkClientDataKey, clientData );
173         if (status != TT_OK) {
174                 return (Tt_pattern)tt_error_pointer( status );
175         }
176         if (registerIt) {
177                 status = tt_pattern_register( pat );
178                 if (status != TT_OK) {
179                         return (Tt_pattern)tt_error_pointer( status );
180                 }
181         }
182         fuse = (caddr_t)0;
183         return pat;
184 }
185
186 Tt_message
187 _tttk_pattern_contract(
188         Tt_pattern pat
189 )
190 {
191         void *p = tt_pattern_user( pat, _TttkContractKey );
192         Tt_status status = tt_ptr_error( p );
193         if (status != TT_OK) {
194                 return 0;
195         }
196         return (Tt_message)p;
197 }