Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / libABil / gil_loadfile.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 /*
25  *      $XConsortium: gil_loadfile.c /main/3 1995/11/06 18:29:14 rswiston $
26  */
27
28 /*
29  * Routines to load in GUIDE intermediate language (GIL) files.
30  */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ab_private/util.h>
39 #include <ab_private/abio.h>
40 #include <ab_private/trav.h>
41 #include <ab_private/gil.h>
42 #include "loadP.h"
43 #include "load.h"
44 #include "../libABobj/obj_utils.h"
45 #include "gilP.h"
46
47 /*
48  * Read a GIL file into memory.
49  */
50 ABObj
51 gil_load_file(
52               STRING gil_file,
53               FILE * inFile,
54               ABObj project
55 )
56 {
57     int                 return_value = 0;
58     int                 rc = 0; /* r turn code */
59     STRING              errmsg = NULL;
60     ABObj               obj = NULL;
61     ABObj               interface = NULL;
62     AB_FILE_TYPE        file_type = AB_FILE_UNDEF;
63     FILE               *gilInFile = NULL;
64
65     /* printf("gil_load_file(%s)\n", gil_file); */
66
67     if (gil_file == NULL)
68     {
69         return 0;
70     }
71
72     abil_loadmsg_clear();
73     abil_loadmsg_set_line_number_callback(NULL);        /*don't report line#*/
74     abil_loadmsg_set_file(gil_file);
75
76     /*
77      * Create the root node for this beastie.
78      */
79     interface = obj_scoped_find_or_create_undef(project, 
80                         ab_ident_from_file_name(gil_file), AB_TYPE_MODULE);
81     if (interface == NULL)
82     {
83         abil_print_load_err(ERR_NO_MEMORY);
84         goto epilogue;
85     }
86
87     obj_set_file(interface, gil_file);
88     obj_set_is_defined(interface, TRUE);
89
90     /*
91      * Open the input file.
92      */
93     if (inFile != NULL)
94     {
95         gilInFile = inFile;
96     }
97     else
98     {
99         if ((errmsg = abio_open_gil_input(gil_file, &gilInFile)) != NULL)
100         {
101             util_error(errmsg);
102             return_value = -1;
103             goto epilogue;
104         }
105         if (!util_be_silent())
106             fprintf(stderr, "%s: reading %s\n",
107                     util_get_program_name(), gil_file);
108     }
109
110     /*
111      * Find start of first object (should be interface)
112      */
113     if (abio_get_eof(gilInFile))
114     {
115         abil_print_load_err(ERR_EOF);
116         return_value = -1;
117         goto epilogue;
118     }
119     if (!abio_gil_get_object_begin(gilInFile))
120     {
121         abil_print_load_err(ERR_WANT_OBJECT);
122         return_value = -1;
123         goto epilogue;
124     }
125
126     /*
127      * Loop reading the input file until there are no more objects.
128      */
129     while (!abio_get_file_end(gilInFile))
130     {
131         if (abio_get_eof(gilInFile))
132         {
133             abil_print_load_err(ERR_EOF);
134             goto epilogue;
135         }
136
137         if (!abio_gil_get_object_begin(gilInFile))
138         {
139             abil_print_load_err(ERR_WANT_OBJECT);
140             goto epilogue;
141         }
142
143         if ((rc = gilP_load_object(gilInFile, interface)) < 0)
144         {
145             return_value = rc;
146             goto epilogue;
147         }
148     }
149
150     if (inFile == NULL)         /* don't close passed-in stream */
151     {
152         abio_close_input(gilInFile);
153     }
154
155 epilogue:
156     if (return_value < 0)
157     {
158         if (interface != NULL)
159         {
160             obj_destroy(interface);
161             interface = NULL;
162         }
163     }
164     if (interface != NULL)
165     {
166         obj_set_is_defined(interface, TRUE);
167     }
168     return interface;
169 }                               /* gil_load_file */
170
171
172 /*
173  * Loads in a GIL project file - DOES NOT LOAD IN THE INTERFACES IN
174  * THE PROJECT FILE.
175  */
176 int
177 gil_load_project_file(
178                       STRING project_file,
179                       FILE * inFile,
180                       ABObj *projectOutPtr
181 )
182 {
183     int                 return_value = 0;
184     int                 rc = 0; /* r turn code */
185     STRING              errmsg = NULL;
186     ABObj               project = obj_create(AB_TYPE_PROJECT, NULL);
187     FILE               *gilInFile = NULL;
188
189     util_dprintf(1,
190                  "gil_load_project_file(%s)\n", util_strsafe(project_file));
191
192     abil_loadmsg_clear();
193     abil_loadmsg_set_line_number_callback(NULL);        /*don't report line#*/
194     abil_loadmsg_set_file(project_file);
195
196     if (project == NULL)
197     {
198         abil_print_load_err(ERR_NO_MEMORY);
199         goto epilogue;
200     }
201
202     project->info.project.is_default = FALSE;   /* spec'd by user */
203     obj_set_file(project, strdup(project_file));
204     obj_set_name(project, ab_ident_from_file_name(project_file));
205
206     /*
207      * Some bonehead decided that a project file should consist of only one
208      * object and not have the surrounding parens like a GIL file.  So, we
209      * can't use load_interface.
210      */
211     if (inFile != NULL)
212     {
213         gilInFile = inFile;
214     }
215     else
216     {
217         if ((errmsg = abio_open_gil_input(project_file, &gilInFile)) != NULL)
218         {
219             util_error(errmsg);
220             return_value = -1;
221             goto epilogue;
222         }
223         if (!util_be_silent())
224         {
225             fprintf(stderr, "%s: reading %s\n",
226                     util_get_program_name(), project_file);
227         }
228     }
229
230     /*
231      * Find start of project object
232      */
233     if (abio_get_eof(gilInFile))
234     {
235         abil_print_load_err(ERR_EOF);
236         return_value = -1;
237         goto epilogue;
238     }
239     if (!abio_gil_get_object_begin(gilInFile))
240     {
241         abil_print_load_err(ERR_WANT_OBJECT);
242         return_value = -1;
243         goto epilogue;
244     }
245
246     /*
247      * The project is it's own parent...
248      */
249     if ((rc = gilP_load_object2(gilInFile, project, project)) < 0)
250     {
251         return_value = rc;
252         goto epilogue;
253     }
254     if (inFile == NULL)
255     {
256         abio_close_input(gilInFile);
257     }
258
259 epilogue:
260     if (return_value < 0)
261     {
262         obj_destroy(project);
263         project = NULL;
264     }
265
266     *projectOutPtr = project;
267     return return_value;
268 }
269
270
271 int
272 gil_load_project_file_and_resolve_all(
273                                       STRING project_file,
274                                       FILE * inFile,
275                                       ABObj *projectOutPtr
276 )
277 {
278     int                 return_value = 0;
279     int                 rc = 0;
280     ABObj               project = NULL;
281     int                 i = 0;
282     ABObj               interface = NULL;
283     AB_TRAVERSAL        trav;
284
285     if ((rc = gil_load_project_file(project_file, inFile, &project)) < 0)
286     {
287         return_value = rc;
288         goto epilogue;
289     }
290
291     /*
292      * Load in the interfaces referenced in the project file
293      */
294     for (trav_open(&trav, project, AB_TRAV_MODULES | AB_TRAV_MOD_SAFE);
295         (interface = trav_next(&trav)) != NULL; )
296     {
297         if (!obj_is_defined(interface))
298         {
299             if (gil_load_file(obj_get_file(interface), NULL, project) == NULL)
300             {
301                 return_value = -1;
302                 goto epilogue;
303             }
304         }
305     }
306     trav_close(&trav);
307
308 epilogue:
309     if (return_value < 0)
310     {
311         obj_destroy(project);
312     }
313     (*projectOutPtr) = project;
314     return return_value;
315 }
316