2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * COMPONENT_NAME: austext
26 * FUNCTIONS: alloc_dict
32 * This module contains IBM CONFIDENTIAL code. -- (IBM
33 * Confidential Restricted when combined with the aggregated
34 * modules for this product)
36 * OBJECT CODE ONLY SOURCE MATERIALS
37 * (C) COPYRIGHT International Business Machines Corp. 1995, 1996
39 * US Government Users Restricted Rights - Use, duplication or
40 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
42 /*-----------------------------------------------------------------------
43 $XConsortium: inittab.c /main/5 1996/08/12 12:34:15 cde-ibm $
44 inittab.c -- db_VISTA table initialization module.
46 (C) Copyright 1987 by Raima Corporation.
47 -----------------------------------------------------------------------*/
49 /* ********************** EDIT HISTORY *******************************
51 SCR DATE INI DESCRIPTION
52 ----- --------- --- -----------------------------------------------------
53 101 21-Jun-88 RSC Initialized rn_type and rn_dba
54 23-Jun-88 RSC Make const_dbd, const_dbf unique to 7 chars
55 274 30-Jun-88 RSC Initialization of curr_rec was goofy (erroneous)
56 115 19-Jul-88 RSC Integrate VAX/VMS changes into code
57 04-Aug-88 RTK MULTI_TASK changes
58 18-Aug-88 RSC moved rn_type/dba to separate table.
59 417 31-Aug-88 RSC Initialized curr_rn_table.
60 420 19-Oct-88 RSC key_count was initialized wrong causing memory overwrites
61 438 06-Jan-89 RSC need to explicitly initialize size_xx
62 532 06-Jan-89 RSC Fixes to compile with ONE_DB
65 * Revision 1.3 1995/10/17 19:19:59 miker
66 * Changed .dbd file open mode from O_RDWR to O_RDONLY.
67 * When initializing tables only need to read .dbd file.
69 * Revision 1.2 1995/10/13 18:48:16 miker
70 * Change hardcoded dbfile[] size from 48 to DtSrFILENMLEN.
72 * Revision 1.1 1995/10/13 18:48:00 miker
73 * Original vista source.
84 int debugging_inittab = FALSE;
85 int dump_init_tables = FALSE;
88 /* Internal function prototypes */
89 static int alloc_dict(void);
90 static int initcurr(void);
95 /* Initialize database tables
100 int dbt_lc; /* loop control */
102 int key_offset = 0, key_count;
103 char dbfile[DtSrFILENMLEN], dbd_ver[DBD_COMPAT_LEN + 1];
104 char dbname[FILENMLEN]; /* Temporary working space */
106 FILE_ENTRY *file_ptr;
107 FIELD_ENTRY *fld_ptr;
110 #if DB_ENABLE | TS_ENABLE
111 RECORD_ENTRY *rec_ptr;
114 MEMBER_ENTRY *mem_ptr;
118 size_ft = size_rt = size_st = size_mt = size_srt = size_fd = size_kt = 0;
120 /* compute individual dictionary sizes and offsets */
121 for (dbt_lc = 0, curr_db_table = &db_table[old_no_of_dbs];
122 dbt_lc < no_of_dbs; ++dbt_lc, ++curr_db_table) {
124 /* form database dictionary name */
125 if ( DB_REF(db_path[0]) )
126 strcpy(dbname, DB_REF(db_path));
131 if (debugging_inittab) {
132 printf (__FILE__"100 inittab: path='%s' dbname='%s'\n",
133 dbname, DB_REF(db_name));
137 if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
138 return( dberr(S_NAMELEN) );
139 strcat(dbname, DB_REF(db_name));
140 if (con_dbd(dbfile, dbname, get_element(dbdpath, dbt_lc)) != S_OKAY)
141 return( dberr(db_status) );
143 /*----------------- PASS 1 -------------------
144 * In this first pass, only opening to determine
145 * required table sizes, so opening read-only is ok.
147 if ( (dbf = open_b(dbfile, O_RDONLY)) < 0 )
148 return( dberr( S_INVDB ) );
150 /* Read in and verify the dictionary version */
151 DB_READ(dbf, dbd_ver, DBD_COMPAT_LEN);
152 dbd_ver[DBD_COMPAT_LEN] = '\0';
153 for ( i=0; i<size_compat; i++ ) {
154 if ( strcmp( dbd_ver, compat_dbd[i] ) == 0 ) goto goodver;
157 /* Incompatible dictionary file */
159 return( dberr( S_INCOMPAT ) );
162 /* Read in database page size */
163 DB_READ(dbf, (char *)&DB_REF(Page_size), sizeof(INT));
164 NTOHS (DB_REF(Page_size));
166 /* Read in table sizes */
167 DB_READ(dbf, (char *)&DB_REF(Size_ft), sizeof(INT));
168 NTOHS (DB_REF(Size_ft));
169 DB_READ(dbf, (char *)&DB_REF(Size_rt), sizeof(INT));
170 NTOHS (DB_REF(Size_rt));
171 DB_READ(dbf, (char *)&DB_REF(Size_fd), sizeof(INT));
172 NTOHS (DB_REF(Size_fd));
173 DB_READ(dbf, (char *)&DB_REF(Size_st), sizeof(INT));
174 NTOHS (DB_REF(Size_st));
175 DB_READ(dbf, (char *)&DB_REF(Size_mt), sizeof(INT));
176 NTOHS (DB_REF(Size_mt));
177 DB_READ(dbf, (char *)&DB_REF(Size_srt), sizeof(INT));
178 NTOHS (DB_REF(Size_srt));
179 DB_READ(dbf, (char *)&DB_REF(Size_kt), sizeof(INT));
180 NTOHS (DB_REF(Size_kt));
181 close(dbf); /* end of PASS 1 */
184 if (debugging_inittab) {
185 printf (__FILE__"152 sizes: pg=%d ft=%d rt=%d fd=%d\n"
186 " st=%d mt=%d srt=%d kt=%d\n",
187 (int) DB_REF(Page_size),
188 (int) DB_REF(Size_ft),
189 (int) DB_REF(Size_rt),
190 (int) DB_REF(Size_fd),
191 (int) DB_REF(Size_st),
192 (int) DB_REF(Size_mt),
193 (int) DB_REF(Size_srt),
194 (int) DB_REF(Size_kt)
200 DB_REF(sysdba) = NULL_DBA;
202 /* update merged dictionary offsets and sizes */
203 if ( curr_db_table->Page_size > page_size ) {
204 page_size = curr_db_table->Page_size;
206 if (debugging_inittab) {
207 printf (__FILE__"191 db's page_size-->%d (largest = %d)\n",
208 (int)page_size, (int)largest_page);
214 curr_db_table->ft_offset = size_ft;
215 size_ft += curr_db_table->Size_ft;
216 curr_db_table->rt_offset = size_rt;
217 size_rt += curr_db_table->Size_rt;
218 curr_db_table->fd_offset = size_fd;
219 size_fd += curr_db_table->Size_fd;
220 curr_db_table->st_offset = size_st;
221 size_st += curr_db_table->Size_st;
222 curr_db_table->mt_offset = size_mt;
223 size_mt += curr_db_table->Size_mt;
224 curr_db_table->srt_offset = size_srt;
225 size_srt += curr_db_table->Size_srt;
226 curr_db_table->kt_offset = size_kt;
227 size_kt += curr_db_table->Size_kt;
229 /* allocate dictionary space */
230 if ( alloc_dict() != S_OKAY ) return( db_status );
232 /* read in and adjust dictionary entries for each database */
233 for (dbt_lc = 0, curr_db_table = &db_table[old_no_of_dbs];
235 ++dbt_lc, ++curr_db_table) {
237 /* form database dictionary name */
238 if ( DB_REF(db_path[0]) )
239 strcpy(dbname, DB_REF(db_path));
242 if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
243 return( dberr(S_NAMELEN) );
244 strcat(dbname,DB_REF(db_name));
245 if (con_dbd(dbfile,dbname,get_element(dbdpath, dbt_lc)) != S_OKAY)
246 return( dberr(db_status) );
248 if (dump_init_tables) {
249 printf (__FILE__"247 Tables for database '%s':\n", dbfile);
254 /*----------------- PASS 2 -------------------
255 * Second pass just loads allocated tables,
256 * so opening .dbd file read-only is still ok.
258 dbf = open_b (dbfile, O_RDONLY);
259 DB_LSEEK(dbf, (off_t)(DBD_COMPAT_LEN + 8L*sizeof(INT)), 0);
261 /*----------------- FILE TABLE -------------------*/
262 DB_READ(dbf, (char *)&file_table[ORIGIN(ft_offset)],
263 (DB_REF(Size_ft)*sizeof(FILE_ENTRY)));
264 /* Invalid if sizeof(xxxx_ENTRY) diff on each machine */
265 for ( i = 0, file_ptr = &file_table[ORIGIN(ft_offset)];
268 /* Byte swap each INT on LITTLE_ENDIAN machines */
269 NTOHS (file_ptr->ft_slots);
270 NTOHS (file_ptr->ft_slsize);
271 NTOHS (file_ptr->ft_pgsize);
272 NTOHS (file_ptr->ft_flags);
274 if (dump_init_tables) {
275 printf (" FILE#%d: ty=%c slts=%2d slsz=%3d pgsz=%d '%s'\n",
278 (int)file_ptr->ft_slots,
279 (int)file_ptr->ft_slsize,
280 (int)file_ptr->ft_pgsize,
287 /*----------------- RECORD TABLE -------------------*/
288 DB_READ(dbf, (char *)&record_table[ORIGIN(rt_offset)],
289 (DB_REF(Size_rt)*sizeof(RECORD_ENTRY)));
290 for ( i = 0, rec_ptr = &record_table[ORIGIN(rt_offset)];
293 /* Byte swap each INT on LITTLE_ENDIAN machines */
294 NTOHS (rec_ptr->rt_file);
295 NTOHS (rec_ptr->rt_len);
296 NTOHS (rec_ptr->rt_data);
297 NTOHS (rec_ptr->rt_fields);
298 NTOHS (rec_ptr->rt_fdtot);
299 NTOHS (rec_ptr->rt_flags);
301 if (dump_init_tables) {
303 " REC #%d: fil=%d len=%3d data=%2d fld1=%2d flds=%2d\n",
305 (int)rec_ptr->rt_file,
306 (int)rec_ptr->rt_len,
307 (int)rec_ptr->rt_data,
308 (int)rec_ptr->rt_fields,
309 (int)rec_ptr->rt_fdtot);
315 /*----------------- FIELD TABLE -------------------*/
316 DB_READ(dbf, (char *)&field_table[ORIGIN(fd_offset)],
317 (DB_REF(Size_fd)*sizeof(FIELD_ENTRY)));
318 for ( i = 0, fld_ptr = &field_table[ORIGIN(fd_offset)];
321 /* Byte swap each INT on LITTLE_ENDIAN machines */
322 NTOHS (fld_ptr->fd_len);
323 NTOHS (fld_ptr->fd_keyfile);
324 NTOHS (fld_ptr->fd_keyno);
325 NTOHS (fld_ptr->fd_ptr);
326 NTOHS (fld_ptr->fd_rec);
327 NTOHS (fld_ptr->fd_flags);
328 for (j=0; j<MAXDIMS; j++)
329 NTOHS (fld_ptr->fd_dim[j]);
331 if (dump_init_tables) {
333 puts (" key typ len kfil key# ofs rec# flg dims");
335 " FLD#%2d %c %c %3d %d %d %3d %d %x",
339 (int)fld_ptr->fd_len,
340 (int)fld_ptr->fd_keyfile,
341 (int)fld_ptr->fd_keyno,
342 (int)fld_ptr->fd_ptr,
343 (int)fld_ptr->fd_rec,
344 (int)fld_ptr->fd_flags);
345 for (j=0; j<MAXDIMS; j++)
346 if (fld_ptr->fd_dim[j])
347 printf (" %d:%d", j, (int)fld_ptr->fd_dim[j]);
354 /*----------------- SET TABLE -------------------*/
355 DB_READ(dbf, (char *)&set_table[ORIGIN(st_offset)],
356 (DB_REF(Size_st)*sizeof(SET_ENTRY)));
357 for ( i = 0, set_ptr = &set_table[ORIGIN(st_offset)];
360 /* Byte swap each INT on LITTLE_ENDIAN machines */
361 NTOHS (set_ptr->st_order);
362 NTOHS (set_ptr->st_own_rt);
363 NTOHS (set_ptr->st_own_ptr);
364 NTOHS (set_ptr->st_members);
365 NTOHS (set_ptr->st_memtot);
366 NTOHS (set_ptr->st_flags);
368 if (dump_init_tables) {
370 " SET #%d: ord=%c owner=%d ownofs=%2d mem1=%d mems=%d\n",
372 (char)set_ptr->st_order,
373 (int)set_ptr->st_own_rt,
374 (int)set_ptr->st_own_ptr,
375 (int)set_ptr->st_members,
376 (int)set_ptr->st_memtot);
382 /*----------------- MEMBER TABLE -------------------*/
383 DB_READ(dbf, (char *)&member_table[ORIGIN(mt_offset)],
384 (DB_REF(Size_mt)*sizeof(MEMBER_ENTRY)));
385 for ( i = 0, mem_ptr = &member_table[ORIGIN(mt_offset)];
388 /* Byte swap each INT on LITTLE_ENDIAN machines */
389 NTOHS (mem_ptr->mt_record);
390 NTOHS (mem_ptr->mt_mem_ptr);
391 NTOHS (mem_ptr->mt_sort_fld);
392 NTOHS (mem_ptr->mt_totsf);
394 if (dump_init_tables) {
396 " MEM #%d: rec=%d ofs=%d sort1=%d sorts=%d\n",
398 (int)mem_ptr->mt_record,
399 (int)mem_ptr->mt_mem_ptr,
400 (int)mem_ptr->mt_sort_fld,
401 (int)mem_ptr->mt_totsf);
408 DB_READ(dbf, (char *)&sort_table[ORIGIN(srt_offset)],
409 (DB_REF(Size_srt)*sizeof(SORT_ENTRY)));
410 /* Member sort tables not used by DtSearch @@@ */
411 if (DB_REF(Size_srt)) {
412 /* Byte swap each INT on LITTLE_ENDIAN machines */
413 srt_ptr = &sort_table[ORIGIN(srt_offset)];
414 NTOHS (srt_ptr->se_fld);
415 NTOHS (srt_ptr->se_set);
418 DB_READ(dbf, (char *)&key_table[ORIGIN(kt_offset)],
419 (DB_REF(Size_kt)*sizeof(KEY_ENTRY)));
420 /* Compound key tables not used by DtSearch @@@ */
421 if (DB_REF(Size_kt)) {
422 /* Byte swap each INT on LITTLE_ENDIAN machines */
423 key_ptr = &key_table[ORIGIN(kt_offset)];
424 NTOHS (key_ptr->kt_key);
425 NTOHS (key_ptr->kt_field);
426 NTOHS (key_ptr->kt_ptr);
427 NTOHS (key_ptr->kt_sort);
430 close(dbf); /* end of PASS 2 */
432 dump_init_tables = FALSE;
434 curr_db_table->key_offset = key_offset;
436 /* update file table path entries */
437 if ( DB_REF(db_path[0]) || dbfpath[0] ) {
438 for (i = 0, file_ptr = &file_table[ORIGIN(ft_offset)];
442 /* Construct the data/key file name */
443 if ( DB_REF(db_path[0]) )
444 strcpy(dbname, DB_REF(db_path));
447 if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
448 return( dberr(S_NAMELEN) );
449 strcat(dbname, DB_REF(db_name));
450 if (con_dbf(dbfile, file_ptr->ft_name, dbname,
451 get_element(dbfpath, dbt_lc)) != S_OKAY)
452 return( dberr(db_status) );
454 /* Save new name in dictionary */
455 strcpy(file_ptr->ft_name, dbfile);
458 #if DB_ENABLE | TS_ENABLE
459 /* adjust record table entries */
460 for (i = ORIGIN(rt_offset), rec_ptr = &record_table[ORIGIN(rt_offset)];
461 i < ORIGIN(rt_offset) + DB_REF(Size_rt);
463 rec_ptr->rt_file += curr_db_table->ft_offset;
464 rec_ptr->rt_fields += curr_db_table->fd_offset;
467 /* adjust field table entries */
468 for (key_count = 0, i = ORIGIN(fd_offset),
469 fld_ptr = &field_table[ORIGIN(fd_offset)];
470 i < ORIGIN(fd_offset) + DB_REF(Size_fd);
472 fld_ptr->fd_rec += curr_db_table->rt_offset;
473 if ( fld_ptr->fd_key != NOKEY ) {
474 fld_ptr->fd_keyno += key_offset;
476 fld_ptr->fd_keyfile += curr_db_table->ft_offset;
477 if ( fld_ptr->fd_type == 'k' )
478 fld_ptr->fd_ptr += curr_db_table->kt_offset;
481 key_offset += key_count;
483 #if DB_ENABLE | TS_ENABLE
484 /* adjust set table entries */
485 for (i = ORIGIN(st_offset), set_ptr = &set_table[ORIGIN(st_offset)];
486 i < ORIGIN(st_offset) + DB_REF(Size_st);
488 set_ptr->st_own_rt += curr_db_table->rt_offset;
489 set_ptr->st_members += curr_db_table->mt_offset;
493 /* adjust member table entries */
494 for (i = curr_db_table->mt_offset,
495 mem_ptr = &member_table[curr_db_table->mt_offset];
496 i < curr_db_table->mt_offset + curr_db_table->Size_mt;
498 mem_ptr->mt_record += curr_db_table->rt_offset;
499 mem_ptr->mt_sort_fld += curr_db_table->srt_offset;
502 /* adjust sort table entries */
503 for (i = curr_db_table->srt_offset,
504 srt_ptr = &sort_table[curr_db_table->srt_offset];
505 i < curr_db_table->srt_offset + curr_db_table->Size_srt;
507 srt_ptr->se_fld += curr_db_table->fd_offset;
508 srt_ptr->se_set += curr_db_table->st_offset;
511 /* adjust key table entries */
512 for (i = curr_db_table->kt_offset,
513 key_ptr = &key_table[curr_db_table->kt_offset];
514 i < curr_db_table->kt_offset + curr_db_table->Size_kt;
516 key_ptr->kt_key += curr_db_table->fd_offset;
517 key_ptr->kt_field += curr_db_table->fd_offset;
519 } /* end loop for each database */
526 /* Allocate space for dictionary
528 static int alloc_dict(void)
535 /* allocate and initialize file_table */
537 if ( old_no_of_dbs == 0 ) {
547 db_ptr = &db_table[old_no_of_dbs];
548 old_size_ft = db_ptr->Size_ft + db_ptr->ft_offset + extra_file;
549 old_size_fd = db_ptr->Size_fd + db_ptr->fd_offset;
550 old_size_st = db_ptr->Size_st + db_ptr->st_offset;
551 old_size_mt = db_ptr->Size_mt + db_ptr->mt_offset;
552 old_size_srt = db_ptr->Size_srt + db_ptr->srt_offset;
553 old_size_kt = db_ptr->Size_kt + db_ptr->kt_offset;
554 old_size_rt = db_ptr->Size_rt + db_ptr->rt_offset;
557 new_size = (size_ft + extra_file) * sizeof(FILE_ENTRY);
558 old_size = old_size_ft * sizeof(FILE_ENTRY);
559 if ( ALLOC_TABLE(&db_global.File_table, new_size, old_size, "file_table")
564 /* allocate record_table */
565 new_size = size_rt * sizeof(RECORD_ENTRY);
566 old_size = old_size_rt * sizeof(RECORD_ENTRY);
567 if ( ALLOC_TABLE(&db_global.Record_table, new_size, old_size, "record_table")
572 /* allocate field_table */
573 new_size = size_fd * sizeof(FIELD_ENTRY);
574 old_size = old_size_fd * sizeof(FIELD_ENTRY);
575 if ( ALLOC_TABLE(&db_global.Field_table, new_size, old_size, "field_table")
580 /* allocate set table */
582 new_size = size_st * sizeof(SET_ENTRY);
583 old_size = old_size_st * sizeof(SET_ENTRY);
584 if ( ALLOC_TABLE(&db_global.Set_table, new_size, old_size, "set_table")
588 } else set_table = NULL;
590 /* allocate member_table */
592 new_size = size_mt * sizeof(MEMBER_ENTRY);
593 old_size = old_size_mt * sizeof(MEMBER_ENTRY);
594 if ( ALLOC_TABLE(&db_global.Member_table, new_size, old_size, "member_table")
598 } else member_table = NULL;
600 /* allocate sort_table */
602 new_size = size_srt * sizeof(SORT_ENTRY);
603 old_size = old_size_srt * sizeof(SORT_ENTRY);
604 if ( ALLOC_TABLE(&db_global.Sort_table, new_size, old_size, "sort_table")
608 } else sort_table = NULL;
610 /* allocate key_table */
612 new_size = size_kt * sizeof(KEY_ENTRY);
613 old_size = old_size_kt * sizeof(KEY_ENTRY);
614 if ( ALLOC_TABLE(&db_global.Key_table, new_size, old_size, "key_table")
618 } else key_table = NULL;
620 return( db_status = S_OKAY );
623 /* Initialize currency tables
625 static int initcurr(void)
627 int dbt_lc; /* loop control */
629 RECORD_ENTRY *rec_ptr;
635 /* Initialize current record and type */
636 for (dbt_lc = no_of_dbs, curr_db_table = &db_table[old_no_of_dbs],
637 curr_rn_table = &rn_table[old_no_of_dbs];
638 --dbt_lc >= 0; ++curr_db_table, ++curr_rn_table) {
639 DB_REF(curr_dbt_rec) = NULL_DBA;
640 RN_REF(rn_dba) = NULL_DBA;
641 RN_REF(rn_type) = -1;
645 new_size = size_st * sizeof(DB_ADDR);
646 old_size = old_size_st * sizeof(DB_ADDR);
647 if ( ALLOC_TABLE(&db_global.Curr_own, new_size, old_size, "curr_own")
651 if ( ALLOC_TABLE(&db_global.Curr_mem, new_size, old_size, "curr_mem")
655 /* for each db make system record as curr_own of its sets */
656 for (dbt_lc = no_of_dbs, curr_db_table = &db_table[old_no_of_dbs];
657 --dbt_lc >= 0; ++curr_db_table) {
658 for (rec = ORIGIN(rt_offset),
659 rec_ptr = &record_table[ORIGIN(rt_offset)];
660 rec < ORIGIN(rt_offset) + DB_REF(Size_rt);
662 if (rec_ptr->rt_fdtot == -1) {
663 /* found system record */
664 curr_rec = ((FILEMASK & NUM2EXT(rec_ptr->rt_file, ft_offset))
666 /* make system record current of sets it owns */
667 for (i = ORIGIN(st_offset),
668 set_ptr = &set_table[ORIGIN(st_offset)],
669 co_ptr = &curr_own[ORIGIN(st_offset)];
670 i < ORIGIN(st_offset) + DB_REF(Size_st);
671 ++i, ++set_ptr, ++co_ptr) {
672 if (set_ptr->st_own_rt == rec) {
676 DB_REF(sysdba) = curr_rec;
677 DB_REF(curr_dbt_rec) = curr_rec;
688 MEM_LOCK(&db_global.Db_table);
689 curr_db_table = db_table;
690 MEM_LOCK(&db_global.Rn_table);
691 curr_rn_table = rn_table;
693 curr_rec = DB_REF(curr_dbt_rec);
694 return( db_status = S_OKAY );
696 /* vpp -nOS2 -dUNIX -nBSD -nVANILLA_BSD -nVMS -nMEMLOCK -nWINDOWS -nFAR_ALLOC -f/usr/users/master/config/nonwin inittab.c */