Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / libABil / bil_loadact.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  *      $XConsortium: bil_loadact.c /main/3 1995/11/06 18:24:00 rswiston $
25  *
26  * @(#)bil_loadact.c    1.47 02 Feb 1995
27  *
28  *      RESTRICTED CONFIDENTIAL INFORMATION:
29  *      
30  *      The information in this document is subject to special
31  *      restrictions in a confidential disclosure agreement between
32  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
33  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
34  *      Sun's specific written approval.  This document and all copies
35  *      and derivative works thereof must be returned or destroyed at
36  *      Sun's request.
37  *
38  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  *
40  */
41
42
43 /*
44  *  billdact.c - load action and handler attributes
45  */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <ab_private/trav.h>
51 #include <ab_private/util.h>
52 #include <ab/util_types.h>
53 #include "../libABobj/obj_utils.h"
54 #include "load.h"
55 #include "bil_loadattP.h"
56 #include "bilP.h"
57
58 /*static int    add_user_handler(ABObj obj, ABObj module,
59                                 ISTRING handler, AB_WHEN when);
60 static int      install_action(ABObj obj, ABObj module, ABObj action);*/
61
62 static int      install_action(ABObj obj, ABObj module, ABObj action);
63 #ifdef BOGUS
64 static ABObj    find_or_create_target(ABObj obj, ABObj module,
65                         ISTRING parent_name, ISTRING name);
66 #endif /* BOGUS */
67
68 #define nset_att(a) (bilP_load_set_current_att(a))
69
70 /*************************************************************************
71 **                                                                      **
72 **                                                                      **
73 **              General actions                                         **
74 **                                                                      **
75 **                                                                      **
76 **************************************************************************/
77
78
79 /*
80  * Get the source object.
81  */
82 STRING
83 bilP_load_att_from(BIL_TOKEN valueToken)
84 {
85     ABObj       newObj = NULL;
86     ABObj       src_module = NULL;
87     STRING      stringValue = bilP_load_get_value();
88  
89 #ifdef DEBUG
90     util_dprintf(4, "bilP_load_att_from: %d/%s\n",
91                 valueToken,
92                 str_safe(bilP_token_to_string(valueToken)));
93 #endif
94
95     nset_att(AB_BIL_FROM);
96     if (valueToken == AB_BIL_APPLICATION)
97     {
98         bilP_load.obj->info.action.from = bilP_load.project;
99     }
100     else if (valueToken == AB_BIL_VALUE_IDENT)
101     {
102         /* This will either find the correct ABObj for the connection
103          * source or it will create a proxy object for it.  If the
104          * connection is a cross-module one, this will create both
105          * the source module and the source object if they have not
106          * yet been loaded into the tree.  In this case, the dummy
107          * module and object will be handled when the bil file is
108          * read in and the name of the module is encountered.  See
109          * bilP_load_att_name() in bil_loadatt.c.
110          */
111         newObj = obj_scoped_find_or_create_undef(bilP_load.module, 
112                         stringValue, AB_TYPE_UNKNOWN);
113         src_module = obj_get_module(newObj);
114
115         /* bilP_load.obj is the action created in bilP_load_att_class().
116          * If the connection is a cross-module one, it is stored off of
117          * the project object, otherwise it is stored off of the module.
118          */
119         bilP_load.obj->info.action.from = newObj;
120     }
121     else if (valueToken == AB_BIL_NIL)
122     {
123     }
124     else
125     {
126         abil_print_load_err(ERR_WANT_NAME);
127     }
128     return NULL;
129 }
130
131
132 /*
133  * Get the destination object.
134  */
135 STRING
136 bilP_load_att_to(BIL_TOKEN valueToken)
137 {
138     ABObj       toObj= NULL; 
139     STRING      stringValue= bilP_load_get_value(); 
140  
141 #ifdef DEBUG
142     util_dprintf(3, "bilP_load_att_to: %d/%s\n",
143                 valueToken,
144                 str_safe(bilP_token_to_string(valueToken)));
145 #endif
146
147     nset_att(AB_BIL_TO);
148
149     if (valueToken == AB_BIL_VALUE_IDENT) 
150     { 
151         /* This will either find the correct ABObj for the connection
152          * source or it will create a proxy object for it.  If the
153          * connection is a cross-module one, this will create both
154          * the source module and the source object if they have not
155          * yet been loaded into the tree.  In this case, the dummy
156          * module and object will be handled when the bil file is
157          * read in and the name of the module is encountered.  See
158          * bilP_load_att_name() in bil_loadatt.c.
159          */
160         toObj = obj_scoped_find_or_create_undef(bilP_load.module, 
161                         stringValue, AB_TYPE_UNKNOWN);
162         bilP_load.obj->info.action.to = toObj;
163     } 
164     else if (valueToken == AB_BIL_NIL) 
165     { 
166     } 
167     else 
168     {  
169         abil_print_load_err(ERR_WANT_NAME);
170     }
171     return NULL;
172 }
173
174
175 /*
176  * Get the when part of a connection.
177  */
178 STRING
179 bilP_load_att_when(BIL_TOKEN valueToken)
180 {
181         AB_WHEN                 when= AB_WHEN_UNDEF;
182
183 #ifdef DEBUG
184     util_dprintf(3, "bilP_load_att_when: %d/%s\n",
185                 valueToken,
186                 str_safe(bilP_token_to_string(valueToken)));
187 #endif
188
189 /*
190         abil_loadmsg_set_att(":when");
191 */
192         nset_att(AB_BIL_WHEN);
193
194         when = bilP_token_to_when(valueToken);
195         if (when == AB_WHEN_UNDEF)
196         {
197             abil_print_load_err(ERR_WANT_NAME);
198         }
199
200         /* If the source object is a radiobox or
201          * checkbox item and the when is :activate,
202          * convert it to be :toggle. This is done
203          * to maintain backwards compatibility.
204          */
205         if (obj_is_choice_item(bilP_load.obj->info.action.from) &&
206             !obj_is_option_menu(obj_get_root(
207                 obj_get_parent(bilP_load.obj->info.action.from))))
208         {
209             if (when == AB_WHEN_ACTIVATED)
210                 when = AB_WHEN_TOGGLED;    
211         }
212         bilP_load.obj->info.action.when = when;
213
214         return NULL;
215 }
216
217 /*
218  * Get the action_type for an action holder
219  */
220 STRING
221 bilP_load_att_action_type(BIL_TOKEN valueToken)
222 {
223     AB_FUNC_TYPE func_type = bilP_token_to_func_type(valueToken);
224     
225     util_dprintf(3, "bilP_load_att_action: %d/%s\n",
226                 valueToken,
227                 str_safe(bilP_token_to_string(valueToken)));
228
229     nset_att(AB_BIL_ACTION_TYPE);
230     if (func_type == AB_FUNC_UNDEF)
231     {
232         abil_print_load_err(ERR_UNKNOWN_OBJ);
233         return NULL;
234     }
235     obj_set_func_type(bilP_load.obj, func_type);
236     return NULL;
237 }
238
239 STRING
240 bilP_load_att_action(BIL_TOKEN valueToken)
241 {
242     STRING      stringValue = bilP_load_get_value();
243
244 #ifdef DEBUG
245     util_dprintf(3, "bilP_load_att_action: %d/%s\n",
246                 valueToken,
247                 str_safe(bilP_token_to_string(valueToken)));
248 #endif
249
250     nset_att(AB_BIL_ACTION);
251
252     /* Check if this is user defined */
253     if (valueToken == AB_BIL_VALUE_IDENT)
254     {
255         obj_set_func_name(bilP_load.obj, stringValue);
256     }
257     /* Check if this is execute code */
258     else 
259         if (valueToken == AB_BIL_VALUE_STRING)
260         {
261             obj_set_func_code(bilP_load.obj, stringValue);
262         }
263         else 
264             {
265                 AB_BUILTIN_ACTION       action = AB_STDACT_UNDEF;
266                 action = bilP_token_to_builtin_action(valueToken);
267                 obj_set_func_builtin(bilP_load.obj, action);
268             }
269     return NULL;
270 }
271
272 /*
273  * Get the arg_type of an action_holder
274  */
275 STRING
276 bilP_load_att_arg_type(BIL_TOKEN valueToken)
277 {
278     AB_ARG_TYPE arg_type = bilP_token_to_arg_type(valueToken);
279
280 #ifdef DEBUG
281     util_dprintf(3, "bilP_load_att_arg_type: %d/%s\n",
282                 valueToken,
283                 str_safe(bilP_token_to_string(valueToken)));
284 #endif
285
286     nset_att(AB_BIL_ARG_TYPE);
287
288     if (arg_type == AB_ARG_UNDEF)
289     {
290         abil_print_load_err(ERR_UNKNOWN_OBJ);
291         return NULL;
292     }
293     obj_set_arg_type(bilP_load.obj, arg_type);
294     return NULL;
295 }
296
297
298 static int
299 install_action(ABObj obj, ABObj module, ABObj action)
300 {
301         ABObj   oldaction= NULL;
302         ABObj   project= obj_get_project(module);
303
304         /*
305          * For efficiency, we check this module first to avoid searching
306          * the entire project unless necessary.
307          */
308
309         oldaction= obj_find_action(module, action);
310         if (oldaction == NULL)
311         {
312                 oldaction= obj_find_action(project, action);
313         }
314
315         if (oldaction != NULL)
316         {
317                 /* we've seen this action, before */
318                 obj_destroy(action);
319                 action= oldaction;
320                 if (obj_get_module(action) != module)
321                 {
322                         /*
323                          *  The action is in another module.  Move
324                          *  it to the project
325                          */
326                         if (!obj_is_project(action->parent))
327                         {
328                                 obj_unparent(action);
329                                 obj_add_action(project, action);
330                         }
331                 }
332         }
333         else
334         {
335                 /* insert it into the current module */
336                 obj_add_action(module, action);
337         }
338
339         return 0;
340 }
341
342
343 #ifdef BOGUS
344 /*
345  * Finds the target matching the given description, creating it if necessary.
346  *
347  * Assumes: strings are pointers to allocated space. Sets strings to NULL,
348  *          if the values are used.
349  *
350  * Assumes that obj may not be in the object tree yet, and may return
351  *         it as the target.
352  */
353 static ABObj
354 find_or_create_target(ABObj obj, ABObj module, 
355                         ISTRING parent_name, 
356                         ISTRING obj_name )
357 {
358         STRING  target_parent_name[256];
359         STRING  target_name[256];
360         ABObj   target_parent= NULL;
361         ABObj   target= NULL;
362
363         /* util_dprintf(3, "find_or_create_target('%s' '%s' '%s')\n",
364                         nullstr(parent_name),
365                         nullstr(obj_name), nullstr(item_label)); */
366
367         *target_parent_name= 0;
368         *target_name= 0;
369         if (obj_name == NULL)
370         {
371                 return NULL;
372         }
373         if (parent_name != NULL)
374         {
375                 /* we have parent name and object name */
376                 strcpy((STRING)target_parent_name, istr_string(parent_name));
377                 strcpy((STRING)target_name, istr_string(obj_name));
378         }
379         else
380         {
381                 /* object name only (what a concept!) */
382                 strcpy((STRING)target_name, istr_string(obj_name));
383         }
384
385         /*
386          * Find target parent
387          */
388         if (*target_parent_name == 0)
389         {
390                 target_parent= module;
391         }
392         else
393         {
394                 if (istr_equalstr(obj->name, (STRING)target_parent_name))
395                 {
396                         target_parent= obj;
397                 }
398                 else
399                 {
400                         target_parent= obj_scoped_find_or_create_undef(module, 
401                                         (STRING)target_parent_name,
402                                         AB_TYPE_UNKNOWN);
403                 }
404         }
405
406         /*
407          * Find target
408          */
409         if (istr_equalstr(obj->name, (STRING)target_name))
410         {
411                 target= obj;
412         }
413         else
414         {
415                 target= obj_scoped_find_or_create_undef(target_parent, 
416                                 (STRING)target_name, AB_TYPE_UNKNOWN);
417         }
418         return target;
419 }
420 #endif /* BOGUS */
421
422 STRING
423 bilP_load_att_arg_value(BIL_TOKEN valueToken)
424 {
425     STRING      stringValue = bilP_load_get_value();
426
427 #ifdef DEBUG
428     util_dprintf(3, "bilP_load_att_arg_value: %d/%s\n",
429                 valueToken,
430                 str_safe(bilP_token_to_string(valueToken)));
431 #endif
432
433     nset_att(AB_BIL_ARG_VALUE);
434
435     /* Check if arg is an integer */
436     if (valueToken == AB_BIL_VALUE_INT) 
437     { 
438 #ifdef DEBUG
439         util_dprintf(0, "\targ is an int = %d\n", atoi(stringValue));
440 #endif
441         obj_set_arg_int(bilP_load.obj, atoi(stringValue));
442     }
443     /* Check if arg is a float */
444     else
445         if (valueToken == AB_BIL_VALUE_FLOAT)
446         {
447             float       fval;
448             fval = (float) atof(stringValue);
449 #ifdef DEBUG
450 util_dprintf(3, "\targ is a float = %f\n", fval);
451 #endif
452             obj_set_arg_float(bilP_load.obj, fval);
453         }
454         else
455             if (valueToken == AB_BIL_VALUE_STRING)
456             {
457                 ISTRING     istringValue = bilP_get_string();
458 #ifdef DEBUG
459 util_dprintf(3, "\targ is an istring = %s\n", istr_string(istringValue));
460 #endif
461                 obj_set_arg_string(bilP_load.obj, stringValue);
462             }
463     return NULL;
464 }