Resolve many build warnings
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / schema / object_dict.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: object_dict.C /main/7 1996/09/13 20:48:20 cde-hal $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "schema/object_dict.h"
52 #include "utility/randomize.h"
53 #include "utility/db_version.h"
54 #include "misc/unique_id.h"
55
56 desc* desc_ptr = 0;
57 desc* last_desc_ptr = 0;
58
59 char replace_string[PATHSIZ];
60 int replace_string_len;
61
62 //extern int yyparse();
63 //extern int yyrestart(FILE*);
64 //extern FILE *yyin;
65
66 extern int schemaparse();
67 extern int schemarestart(FILE*);
68 extern FILE *schemain;
69
70 object_dict::object_dict() :
71    v_dict(desc_name_eq, desc_name_ls), v_desc_ptr(0)
72 {
73    v_db_path[0] = 0;
74 }
75
76 object_dict::~object_dict()
77 {
78    quit_a_base(v_desc_ptr, 0, true);
79 }
80
81 void object_dict::quit_a_base(desc* b, desc* e, Boolean sync)
82 {
83    _quit_stored_objects(b, e);
84    _quit_stores(b, e, sync);
85    _quit_descs(b, e);
86 }
87
88 void object_dict::_quit_descs(desc* begin_ptr, desc* end_ptr)
89 {
90    desc *ptr = begin_ptr;
91    desc *x_ptr = 0;
92
93    while ( ptr != end_ptr ) {
94       x_ptr = ptr;
95       ptr = ptr -> next_desc;
96       delete x_ptr;
97    }
98 }
99
100 extern int g_mode_8_3;
101
102
103 desc* object_dict::init_a_base(char* db_path, char* db_name) 
104 {
105 //MESSAGE(cerr, "object_dict::init_a_base()");
106 //debug(cerr, db_path);
107
108    int len = MIN(strlen(db_path), PATHSIZ - 1);
109    *((char *) memcpy(v_db_path, db_path, len) + len) = '\0';
110
111    desc* x = 0;
112
113    char* schema_path = 0;
114
115    if ( g_mode_8_3 )
116       schema_path = form("%s/%s.%s", v_db_path, db_name, SCHEMA_FILE_SUFFIX);
117    else
118       schema_path = form("%s/%s", v_db_path, SCHEMA_FILE);
119
120    fstream in(schema_path, ios::in);
121
122    if ( !in )
123       throw(streamException(in.rdstate()));
124
125    char schema_header[LBUFSIZ];
126
127    if ( !in.getline(schema_header, LBUFSIZ) ) {
128        throw(streamException(in.rdstate()));
129    }
130
131    if ( strncmp(schema_header+1, "MMDB", 4) != 0 ) {
132       in.close();
133       x = parse(schema_path);
134    } else {
135
136       unsigned int sz = bytes(schema_path) - strlen(schema_header) - 1;
137       char* buf = new char[sz];
138    
139       if ( !in.read(buf, sz) ) 
140          throw(streamException(in.rdstate()));
141
142       in.close();
143    
144       buffer orig(0);
145       orig.set_chunk(buf, sz);
146       orig.set_content_sz(sz);
147    
148       randomize rd(233);
149       rd.restore(orig);
150
151 //fprintf(stderr, "buf=%s\n", buf);
152 //debug(cerr, buf);
153    
154    ////////////////
155    // do the parse
156    ////////////////
157       x = parse(orig);
158
159       delete buf;
160
161    }
162
163    _init(x);
164
165    return x;
166 }
167
168 desc* object_dict::init_a_base(char* define_desc_path, char* db_path, 
169                          char* db_name) 
170 {
171 //MESSAGE(cerr, "object_dict::init a base (define case)");
172 //debug(cerr, define_desc_path);
173 //debug(cerr, db_path);
174 //debug(cerr, db_name);
175
176    int len;
177
178    len = MIN(strlen(db_path), PATHSIZ - 1);
179    *((char *) memcpy(v_db_path, db_path, len) + len) = '\0';
180
181    if ( db_name ) {
182       len = MIN(strlen(db_name), PATHSIZ - 1);
183       *((char *) memcpy(replace_string, db_name, len) + len) = '\0';
184       replace_string_len = strlen(replace_string);
185    } else {
186       replace_string[0] = 0;
187       replace_string_len = 0;
188    } 
189
190
191    fstream in_test(define_desc_path, ios::in);
192
193    if ( ! in_test ) {
194       throw(stringException(form("%s does not exist.", define_desc_path)));
195    }
196
197    unsigned long llen = bytes(define_desc_path)*4;
198
199    in_test.close();
200
201    if ( disk_space(v_db_path) < llen ) {
202        throw(stringException(form("no enough space on %s", v_db_path)));
203    }
204
205 ///////////////////////////////////
206 // define and init the dictionary
207 ///////////////////////////////////
208    desc* x = parse(define_desc_path);
209    _init(x);
210
211 ////////////////////////////
212 // randomize the schema.mmdb
213 ////////////////////////////
214
215    fstream in(define_desc_path, ios::in);
216
217    if ( !in) {
218        debug(cerr, define_desc_path);
219        throw(streamException(in.rdstate()));
220    } 
221
222    unsigned int sz = bytes(define_desc_path);
223    in.close();
224
225    char* schema_buf = new char[sz*3];
226
227    ostringstream* string_out = new ostringstream(schema_buf);
228    if ( !(*string_out) ) {
229        throw(streamException(string_out -> rdstate()));
230    } 
231
232    x -> asciiOutList(*string_out);
233
234    len = MIN((unsigned int) string_out->str().size(), sz*3 - 1);
235    *((char *) memcpy(schema_buf, string_out->str().c_str(), len) + len) = '\0';
236    delete string_out;
237
238    sz = strlen(schema_buf);
239
240    buffer orig(0);
241    orig.set_chunk(schema_buf, sz);
242    orig.set_content_sz(sz);
243
244    randomize rd(233);
245    rd.scramble(orig);
246
247 /////////////////////////////
248 // save the output to db_path
249 /////////////////////////////
250    fstream out(form("%s/%s.%s", db_path, db_name, SCHEMA_FILE_SUFFIX), ios::out);
251 // fstream out(form("%s/%s.%s", db_path, db_name, SCHEMA_FILE_SUFFIX), ios::out, open_file_prot());
252
253    if ( !out ) {
254        MESSAGE(cerr, form("bad file name: %s", db_path));
255        throw(streamException(out.rdstate()));
256    } 
257
258    char* desc_header = form("#MMDB\t%d\t%d\n", MAJOR, MINOR);
259    if ( !out.write(desc_header, strlen(desc_header)) ) {
260        MESSAGE(cerr, form("write descirption head in %s failed", db_path));
261        throw(streamException(out.rdstate()));
262    }
263
264    if ( !out.write(schema_buf, strlen(schema_buf)) ) {
265        MESSAGE(cerr, form("write descirption in %s failed", db_path));
266        throw(streamException(out.rdstate()));
267    }
268
269    out.close();
270
271    delete schema_buf;
272
273
274    return x;
275 }
276
277 desc* object_dict::parse(buffer& desc_buf)
278 {
279     char unique_nm[PATHSIZ];
280
281     Boolean ok = writeToTmpFile (
282                 unique_nm, 
283                 desc_buf.get_base(), 
284                 desc_buf.content_sz()
285                                 ); 
286
287 //debug(cerr, desc_buf.get_base());
288 //fprintf(stderr, "get_base=%s\n", desc_buf.get_base());
289 //fprintf(stderr, "sz=%d\n", desc_buf.content_sz());
290 //fprintf(stderr, "tmpnm=%s\n", unique_nm);
291
292     if ( ok == false ) {
293 //fprintf(stderr, "write to temp failed\n");
294        throw(stringException("can't prepare the object dictionary."));
295     }
296
297     desc* x = 0;
298
299     mtry {
300        x = parse(unique_nm);
301     }
302
303     mcatch (mmdbException &,e) {
304        del_file(unique_nm);
305        rethrow;
306     } end_try;
307
308     del_file(unique_nm);
309
310     return x;
311 }
312
313 desc* object_dict::parse(char* define_desc_path)
314 {
315     schemain = fopen(define_desc_path, "r");
316
317     if ( schemain == NULL )
318        throw(stringException("open desc file failed"));
319
320     static int ct = 0;
321
322     if ( ct > 0 )
323        schemarestart(schemain);
324     else 
325        ct = 1;
326
327     mtry {
328        if ( schemaparse() != 0 ) {
329           fclose(schemain);
330           throw(stringException("Parsing input failed"));
331        }
332     }
333
334     mcatch (mmdbException &,e) {
335       fclose(schemain);
336       rethrow;
337     } end_try;
338
339     fclose(schemain);
340
341     return desc_ptr;
342 }
343
344
345 void object_dict::_init(desc* x)
346 {
347
348    desc *ptr = x;
349
350    mtry { // init all stores
351       while ( ptr ) {
352          ptr -> init_store(this -> v_db_path);
353          ptr = ptr -> next_desc;
354       }
355    }
356
357    mcatch (mmdbException &,e) {
358       _quit_stores(x, ptr);
359       _quit_descs(x, ptr);
360       rethrow;
361    } end_try;
362
363    ptr = x;
364
365    while ( ptr ) { // add to the dict
366       if ( ptr -> get_store() )
367          v_dict.insert(ptr);     
368       ptr = ptr -> next_desc;
369    }
370
371 //////////////////////////////
372 // init stored objects
373 //////////////////////////////
374
375 //MESSAGE(cerr, "init stored obj:");
376    ptr = x;
377
378    while ( ptr ) {
379       ptr -> init_handler(*this);
380       v_dict.insert(ptr);     
381       ptr = ptr -> next_desc;
382    }
383
384    if ( v_desc_ptr == 0 ) {
385       v_desc_ptr = x;
386    } else {
387       v_last_desc_ptr -> next_desc = x;
388    }
389    v_last_desc_ptr = last_desc_ptr;
390 }
391
392 void object_dict::_quit_stores(desc* start_ptr, desc* end_ptr, Boolean sync)
393 {
394    desc *ptr = start_ptr;
395
396    if ( sync == true ) {
397       while ( ptr != end_ptr ) {
398    
399          mtry {
400             ptr -> sync_store();
401          }
402          mcatch (mmdbException &,e)
403          {
404 #ifdef DEBUG
405             fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
406                                                 __FILE__, __LINE__);
407             rethrow;
408 #endif
409          }
410          end_try;
411    
412          ptr = ptr -> next_desc;
413       }
414    }
415
416    ptr = start_ptr;
417
418    while ( ptr != end_ptr ) {
419       mtry {
420          ptr -> quit_store();
421       }
422
423       mcatch (mmdbException &,e)
424       {
425 #ifdef DEBUG
426             fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
427                                                 __FILE__, __LINE__);
428             rethrow;
429 #endif
430       }
431       end_try;
432
433       ptr = ptr -> next_desc;
434    }
435 }
436
437 void object_dict::_quit_stored_objects(desc* start_ptr, desc* end_ptr)
438 {
439    desc *ptr = start_ptr;
440
441    while ( ptr != end_ptr ) {
442
443       mtry {
444          ptr -> quit_handler();
445 //debug(cerr, *ptr);
446       }
447
448       mcatch (mmdbException &,e)
449       {
450 #ifdef DEBUG
451          fprintf(stderr, "mmdbException caught @ %s line:%d.\n",
452                                                 __FILE__, __LINE__);
453          rethrow;
454 #endif
455       }
456       end_try;
457
458       ptr = ptr -> next_desc;
459    }
460 }
461
462 handler* object_dict::get_handler(const char* obj_name)
463 {
464    stored_object_desc key((char*)obj_name);
465
466    stored_object_desc* x = (stored_object_desc*)v_dict.member(&key);
467
468    if ( x ) {
469       return x -> get_handler();
470    } else {
471       throw(stringException(form("handler %s not in dict", obj_name)));
472       return 0;
473    }
474 }
475    
476 abs_storage* object_dict::get_store(const char* obj_name)
477 {
478    store_desc key((char*)obj_name);
479
480    store_desc* x = (store_desc*)v_dict.member(&key);
481
482    if ( x ) {
483       return x -> get_store();
484    } else {
485       throw(stringException(form("store %s not in dict", obj_name)));
486       return 0;
487    }
488 }
489    
490 void schemaerror( char* errorstr )
491 {
492     extern int linecount;
493     extern char schematext[];    
494
495     if ( strlen( schematext ) > 0 )
496         throw(stringException(form("line %d %s at or before \"%s\"",
497                 linecount, errorstr, schematext
498                         )
499              ));
500
501     else
502         throw(stringException(form("line %d %s illegal-symbol", 
503                          linecount, errorstr 
504                         )
505              ));
506         
507     return;
508 }
509
510 extern "C" {
511 int schemawrap()
512 {
513     return( 1 );
514 }
515 }
516
517 //void yyerror( char* errorstr )
518 //{
519 //    extern int linecount;
520 //    extern char yytext[];    
521 //
522 //    if ( strlen( yytext ) > 0 )
523 //      MESSAGE(cerr, form("line %d %s at or before \"%s\"",
524 //              linecount, errorstr, yytext
525 //                        )
526 //             );
527 //
528 //    else
529 //      MESSAGE(cerr, form("line %d %s illegal-symbol", 
530 //                         linecount, errorstr 
531 //                        )
532 //             );
533 //      
534 //    return;
535 //}
536 //
537 //int yywrap()
538 //{
539 //    extern int linecount;
540 //    extern char *pname;
541 //
542 //#ifdef DEBUG
543 //    MESSAGE(cerr, form("Number of input lines %6d", linecount));
544 //#endif
545 //    
546 //cerr << "Calling yywrap()\n";
547 //    //return( 1 );
548 //    return( 0 );
549 //}