Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / raima / inittab.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  *   COMPONENT_NAME: austext
25  *
26  *   FUNCTIONS: alloc_dict
27  *              initcurr
28  *              inittab
29  *
30  *   ORIGINS: 27,157
31  *
32  *   This module contains IBM CONFIDENTIAL code. -- (IBM
33  *   Confidential Restricted when combined with the aggregated
34  *   modules for this product)
35  *
36  *   OBJECT CODE ONLY SOURCE MATERIALS
37  *   (C) COPYRIGHT International Business Machines Corp. 1995, 1996
38  *   All Rights Reserved
39  *   US Government Users Restricted Rights - Use, duplication or
40  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
41  */
42 /*-----------------------------------------------------------------------
43  $XConsortium: inittab.c /main/5 1996/08/12 12:34:15 cde-ibm $
44    inittab.c -- db_VISTA table initialization module.
45
46    (C) Copyright 1987 by Raima Corporation.
47 -----------------------------------------------------------------------*/
48
49 /* ********************** EDIT HISTORY *******************************
50
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
63  *
64  * $Log$
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.
68  *
69  * Revision 1.2  1995/10/13  18:48:16  miker
70  * Change hardcoded dbfile[] size from 48 to DtSrFILENMLEN.
71  *
72  * Revision 1.1  1995/10/13  18:48:00  miker
73  * Original vista source.
74  */
75
76 #include <stdio.h>
77 #include <fcntl.h>
78 #include "vista.h"
79 #include "dbtype.h"
80 #include "inittab.h"
81 #include "dbswab.h"
82
83 #define DEBUG_INITTAB
84 int     debugging_inittab =     FALSE;
85 int     dump_init_tables =      FALSE;
86
87
88 /* Internal function prototypes */
89 static int alloc_dict(P0);
90 static int initcurr(P0);
91
92
93
94
95 /* Initialize database tables
96 */
97 inittab()
98 {
99    register int dbt_lc;                 /* loop control */
100    register INT i, j;
101    int key_offset = 0, key_count;
102    char dbfile[DtSrFILENMLEN], dbd_ver[DBD_COMPAT_LEN + 1];
103    char dbname[FILENMLEN];      /* Temporary working space */
104    int dbf;
105    FILE_ENTRY FAR *file_ptr;
106    FIELD_ENTRY FAR *fld_ptr;
107 #ifndef  ONE_DB
108 #define  DB_ENABLE   1
109 #else
110 #define  DB_ENABLE   0
111 #endif
112 #ifndef  NO_TIMESTAMP
113 #define  TS_ENABLE   1
114 #else
115 #define  TS_ENABLE   0
116 #endif
117 #if   DB_ENABLE | TS_ENABLE
118    RECORD_ENTRY FAR *rec_ptr;
119    SET_ENTRY FAR *set_ptr;
120 #endif
121 #ifndef  ONE_DB
122    MEMBER_ENTRY FAR *mem_ptr;
123    SORT_ENTRY FAR *srt_ptr;
124    KEY_ENTRY FAR *key_ptr;
125 #endif
126
127 #ifndef  NO_TIMESTAMP
128    db_tsrecs = db_tssets = FALSE;
129 #endif
130    size_ft = size_rt = size_st = size_mt = size_srt = size_fd = size_kt = 0;
131
132    /* compute individual dictionary sizes and offsets */
133 #ifndef  ONE_DB
134    for (dbt_lc = 0, curr_db_table = &db_table[old_no_of_dbs]; 
135         dbt_lc < no_of_dbs; ++dbt_lc, ++curr_db_table) {
136 #endif
137
138       /* form database dictionary name */
139       if ( DB_REF(db_path[0]) )
140          strcpy(dbname, DB_REF(db_path));
141       else
142          dbname[0] = '\0';
143
144 #ifdef DEBUG_INITTAB
145       if (debugging_inittab) {
146             printf (__FILE__"100 inittab: path='%s' dbname='%s'\n",
147                 dbname, DB_REF(db_name));
148             fflush (stdout);
149       }
150 #endif
151       if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
152          return( dberr(S_NAMELEN) );
153       strcat(dbname, DB_REF(db_name));
154       if (con_dbd(dbfile, dbname, get_element(dbdpath, dbt_lc)) != S_OKAY)
155          return( dberr(db_status) );
156
157       /*----------------- PASS 1 -------------------
158        * In this first pass, only opening to determine
159        * required table sizes, so opening read-only is ok.
160        */
161       if ( (dbf = open_b(dbfile, O_RDONLY)) < 0 )
162          return( dberr( S_INVDB ) );
163
164       /* Read in and verify the dictionary version */
165       DB_READ(dbf, dbd_ver, DBD_COMPAT_LEN);
166       dbd_ver[DBD_COMPAT_LEN] = '\0';
167       for ( i=0; i<size_compat; i++ ) {
168          if ( strcmp( dbd_ver, compat_dbd[i] ) == 0 ) goto goodver;
169       }
170
171       /* Incompatible dictionary file */
172       close( dbf );
173       return( dberr( S_INCOMPAT ) );
174 goodver:
175
176       /* Read in database page size */
177       DB_READ(dbf, (char FAR *)&DB_REF(Page_size), sizeof(INT));
178       NTOHS (DB_REF(Page_size));
179
180       /* Read in table sizes */
181       DB_READ(dbf, (char FAR *)&DB_REF(Size_ft), sizeof(INT));
182       NTOHS (DB_REF(Size_ft));
183       DB_READ(dbf, (char FAR *)&DB_REF(Size_rt), sizeof(INT));
184       NTOHS (DB_REF(Size_rt));
185       DB_READ(dbf, (char FAR *)&DB_REF(Size_fd), sizeof(INT));
186       NTOHS (DB_REF(Size_fd));
187       DB_READ(dbf, (char FAR *)&DB_REF(Size_st), sizeof(INT));
188       NTOHS (DB_REF(Size_st));
189       DB_READ(dbf, (char FAR *)&DB_REF(Size_mt), sizeof(INT));
190       NTOHS (DB_REF(Size_mt));
191       DB_READ(dbf, (char FAR *)&DB_REF(Size_srt), sizeof(INT));
192       NTOHS (DB_REF(Size_srt));
193       DB_READ(dbf, (char FAR *)&DB_REF(Size_kt), sizeof(INT));
194       NTOHS (DB_REF(Size_kt));
195       close(dbf);       /* end of PASS 1 */
196
197 #ifdef DEBUG_INITTAB
198       if (debugging_inittab) {
199          printf (__FILE__"152 sizes: pg=%d ft=%d rt=%d fd=%d\n"
200             "  st=%d mt=%d srt=%d kt=%d\n",
201             (int) DB_REF(Page_size),
202             (int) DB_REF(Size_ft),
203             (int) DB_REF(Size_rt),
204             (int) DB_REF(Size_fd),
205             (int) DB_REF(Size_st),
206             (int) DB_REF(Size_mt),
207             (int) DB_REF(Size_srt),
208             (int) DB_REF(Size_kt)
209             );
210           fflush (stdout);
211       }
212 #endif
213
214       DB_REF(sysdba) = NULL_DBA;
215
216 #ifndef  ONE_DB
217       /* update merged dictionary offsets and sizes */
218       if ( curr_db_table->Page_size > page_size ) {
219             page_size = curr_db_table->Page_size;
220 #ifdef DEBUG_INITTAB
221             if (debugging_inittab) {
222                 printf (__FILE__"191 db's page_size-->%d (largest = %d)\n",
223                     (int)page_size, (int)largest_page);
224                 fflush(stdout);
225             }
226 #endif
227       }
228
229       curr_db_table->ft_offset = size_ft;
230       size_ft += curr_db_table->Size_ft;
231       curr_db_table->rt_offset = size_rt;
232       size_rt += curr_db_table->Size_rt;
233       curr_db_table->fd_offset = size_fd;
234       size_fd += curr_db_table->Size_fd;
235       curr_db_table->st_offset = size_st;
236       size_st += curr_db_table->Size_st;
237       curr_db_table->mt_offset = size_mt;
238       size_mt += curr_db_table->Size_mt;
239       curr_db_table->srt_offset = size_srt;
240       size_srt += curr_db_table->Size_srt;
241       curr_db_table->kt_offset = size_kt;
242       size_kt += curr_db_table->Size_kt;
243    }
244 #endif
245    /* allocate dictionary space */
246    if ( alloc_dict() != S_OKAY ) return( db_status );
247
248    /* read in and adjust dictionary entries for each database */
249 #ifndef  ONE_DB
250    for (dbt_lc = 0, curr_db_table = &db_table[old_no_of_dbs]; 
251         dbt_lc < no_of_dbs; 
252         ++dbt_lc, ++curr_db_table) {
253 #endif
254
255       /* form database dictionary name */
256       if ( DB_REF(db_path[0]) )
257          strcpy(dbname, DB_REF(db_path));
258       else
259          dbname[0] = '\0';
260       if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
261          return( dberr(S_NAMELEN) );
262       strcat(dbname,DB_REF(db_name));
263       if (con_dbd(dbfile,dbname,get_element(dbdpath, dbt_lc)) != S_OKAY)
264          return( dberr(db_status) );
265 #ifdef DEBUG_INITTAB
266       if (dump_init_tables) {
267         printf (__FILE__"247 Tables for database '%s':\n", dbfile);
268         fflush (stdout);
269       }
270 #endif
271
272       /*----------------- PASS 2 -------------------
273        * Second pass just loads allocated tables,
274        * so opening .dbd file read-only is still ok.
275        */
276       dbf = open_b (dbfile, O_RDONLY);
277       DB_LSEEK(dbf, DBD_COMPAT_LEN + 8L*sizeof(INT), 0);
278
279       /*----------------- FILE TABLE -------------------*/
280       DB_READ(dbf, (char FAR *)&file_table[ORIGIN(ft_offset)],
281             (DB_REF(Size_ft)*sizeof(FILE_ENTRY)));
282       /* Invalid if sizeof(xxxx_ENTRY) diff on each machine */
283       for (     i = 0, file_ptr = &file_table[ORIGIN(ft_offset)];
284                 i < DB_REF(Size_ft);
285                 i++, file_ptr++) {
286             /* Byte swap each INT on LITTLE_ENDIAN machines */
287             NTOHS (file_ptr->ft_slots);
288             NTOHS (file_ptr->ft_slsize);
289             NTOHS (file_ptr->ft_pgsize);
290             NTOHS (file_ptr->ft_flags);
291 #ifdef DEBUG_INITTAB
292             if (dump_init_tables) {
293                 printf (" FILE#%d: ty=%c slts=%2d slsz=%3d pgsz=%d '%s'\n",
294                     (int)i,
295                     file_ptr->ft_type,
296                     (int)file_ptr->ft_slots,
297                     (int)file_ptr->ft_slsize,
298                     (int)file_ptr->ft_pgsize,
299                     file_ptr->ft_name);
300                 fflush (stdout);
301             }
302 #endif
303       }
304
305       /*----------------- RECORD TABLE -------------------*/
306       DB_READ(dbf, (char FAR *)&record_table[ORIGIN(rt_offset)],
307            (DB_REF(Size_rt)*sizeof(RECORD_ENTRY)));
308       for (     i = 0, rec_ptr = &record_table[ORIGIN(rt_offset)];
309                 i < DB_REF(Size_rt);
310                 i++, rec_ptr++) {
311             /* Byte swap each INT on LITTLE_ENDIAN machines */
312             NTOHS (rec_ptr->rt_file);
313             NTOHS (rec_ptr->rt_len);
314             NTOHS (rec_ptr->rt_data);
315             NTOHS (rec_ptr->rt_fields);
316             NTOHS (rec_ptr->rt_fdtot);
317             NTOHS (rec_ptr->rt_flags);
318 #ifdef DEBUG_INITTAB
319             if (dump_init_tables) {
320                 printf (
321                     " REC #%d: fil=%d len=%3d data=%2d fld1=%2d flds=%2d\n",
322                     (int)i,
323                     (int)rec_ptr->rt_file,
324                     (int)rec_ptr->rt_len,
325                     (int)rec_ptr->rt_data,
326                     (int)rec_ptr->rt_fields,
327                     (int)rec_ptr->rt_fdtot);
328                 fflush (stdout);
329             }
330 #endif
331       }
332
333       /*----------------- FIELD TABLE -------------------*/
334       DB_READ(dbf, (char FAR *)&field_table[ORIGIN(fd_offset)],
335            (DB_REF(Size_fd)*sizeof(FIELD_ENTRY)));
336       for (     i = 0, fld_ptr = &field_table[ORIGIN(fd_offset)];
337                 i < DB_REF(Size_fd);
338                 i++, fld_ptr++) {
339             /* Byte swap each INT on LITTLE_ENDIAN machines */
340             NTOHS (fld_ptr->fd_len);
341             NTOHS (fld_ptr->fd_keyfile);
342             NTOHS (fld_ptr->fd_keyno);
343             NTOHS (fld_ptr->fd_ptr);
344             NTOHS (fld_ptr->fd_rec);
345             NTOHS (fld_ptr->fd_flags);
346             for (j=0;  j<MAXDIMS;  j++)
347                 NTOHS (fld_ptr->fd_dim[j]);
348 #ifdef DEBUG_INITTAB
349             if (dump_init_tables) {
350                 if (i == 0)
351                     puts ("         key typ len kfil key# ofs rec# flg dims");
352                 printf (
353                     " FLD#%2d  %c   %c  %3d  %d    %d   %3d  %d    %x",
354                     (int)i,
355                     fld_ptr->fd_key,
356                     fld_ptr->fd_type,
357                     (int)fld_ptr->fd_len,
358                     (int)fld_ptr->fd_keyfile,
359                     (int)fld_ptr->fd_keyno,
360                     (int)fld_ptr->fd_ptr,
361                     (int)fld_ptr->fd_rec,
362                     (int)fld_ptr->fd_flags);
363                 for (j=0; j<MAXDIMS; j++)
364                     if (fld_ptr->fd_dim[j])
365                         printf (" %d:%d", j, (int)fld_ptr->fd_dim[j]);
366                 putchar ('\n');
367                 fflush (stdout);
368             }
369 #endif
370       }
371
372       /*----------------- SET TABLE -------------------*/
373       DB_READ(dbf, (char FAR *)&set_table[ORIGIN(st_offset)],
374            (DB_REF(Size_st)*sizeof(SET_ENTRY)));
375       for (     i = 0, set_ptr = &set_table[ORIGIN(st_offset)];
376                 i < DB_REF(Size_st);
377                 i++, set_ptr++) {
378             /* Byte swap each INT on LITTLE_ENDIAN machines */
379             NTOHS (set_ptr->st_order);
380             NTOHS (set_ptr->st_own_rt);
381             NTOHS (set_ptr->st_own_ptr);
382             NTOHS (set_ptr->st_members);
383             NTOHS (set_ptr->st_memtot);
384             NTOHS (set_ptr->st_flags);
385 #ifdef DEBUG_INITTAB
386             if (dump_init_tables) {
387                 printf (
388                     " SET #%d: ord=%c owner=%d ownofs=%2d mem1=%d mems=%d\n",
389                     (int)i,
390                     (char)set_ptr->st_order,
391                     (int)set_ptr->st_own_rt,
392                     (int)set_ptr->st_own_ptr,
393                     (int)set_ptr->st_members,
394                     (int)set_ptr->st_memtot);
395                 fflush (stdout);
396             }
397 #endif
398       }
399
400       /*----------------- MEMBER TABLE -------------------*/
401       DB_READ(dbf, (char FAR *)&member_table[ORIGIN(mt_offset)],
402            (DB_REF(Size_mt)*sizeof(MEMBER_ENTRY)));
403       for (     i = 0, mem_ptr = &member_table[ORIGIN(mt_offset)];
404                 i < DB_REF(Size_mt);
405                 i++, mem_ptr++) {
406             /* Byte swap each INT on LITTLE_ENDIAN machines */
407             NTOHS (mem_ptr->mt_record);
408             NTOHS (mem_ptr->mt_mem_ptr);
409             NTOHS (mem_ptr->mt_sort_fld);
410             NTOHS (mem_ptr->mt_totsf);
411 #ifdef DEBUG_INITTAB
412             if (dump_init_tables) {
413                 printf (
414                     " MEM #%d: rec=%d ofs=%d sort1=%d sorts=%d\n",
415                     (int)i,
416                     (int)mem_ptr->mt_record,
417                     (int)mem_ptr->mt_mem_ptr,
418                     (int)mem_ptr->mt_sort_fld,
419                     (int)mem_ptr->mt_totsf);
420                 fflush (stdout);
421             }
422 #endif
423       }
424
425
426       DB_READ(dbf, (char FAR *)&sort_table[ORIGIN(srt_offset)],
427            (DB_REF(Size_srt)*sizeof(SORT_ENTRY)));
428       /* Member sort tables not used by DtSearch @@@ */
429       if (DB_REF(Size_srt)) {
430             /* Byte swap each INT on LITTLE_ENDIAN machines */
431             srt_ptr = &sort_table[ORIGIN(srt_offset)];
432             NTOHS (srt_ptr->se_fld);
433             NTOHS (srt_ptr->se_set);
434       }
435
436       DB_READ(dbf, (char FAR *)&key_table[ORIGIN(kt_offset)],
437            (DB_REF(Size_kt)*sizeof(KEY_ENTRY)));
438       /* Compound key tables not used by DtSearch @@@ */
439       if (DB_REF(Size_kt)) {
440             /* Byte swap each INT on LITTLE_ENDIAN machines */
441             key_ptr = &key_table[ORIGIN(kt_offset)];
442             NTOHS (key_ptr->kt_key);
443             NTOHS (key_ptr->kt_field);
444             NTOHS (key_ptr->kt_ptr);
445             NTOHS (key_ptr->kt_sort);
446       }
447
448       close(dbf);       /* end of PASS 2 */
449 #ifdef DEBUG_INITTAB
450       dump_init_tables = FALSE;
451 #endif
452 #ifndef  ONE_DB
453       curr_db_table->key_offset = key_offset;
454 #endif
455
456       /* update file table path entries */
457       if ( DB_REF(db_path[0]) || dbfpath[0] ) {
458          for (i = 0, file_ptr = &file_table[ORIGIN(ft_offset)];
459               i < DB_REF(Size_ft);
460               ++i, ++file_ptr) {
461
462             /* Construct the data/key file name */
463             if ( DB_REF(db_path[0]) )
464                strcpy(dbname, DB_REF(db_path));
465             else
466                dbname[0] = '\0';
467             if (strlen(dbname) + strlen(DB_REF(db_name)) >= FILENMLEN+4)
468                return( dberr(S_NAMELEN) );
469             strcat(dbname, DB_REF(db_name));
470             if (con_dbf(dbfile, file_ptr->ft_name, dbname,
471                get_element(dbfpath, dbt_lc)) != S_OKAY)
472                return( dberr(db_status) );
473
474             /* Save new name in dictionary */
475             strcpy(file_ptr->ft_name, dbfile);
476          } 
477       }
478 #if   DB_ENABLE | TS_ENABLE
479       /* adjust record table entries */
480       for (i = ORIGIN(rt_offset), rec_ptr = &record_table[ORIGIN(rt_offset)];
481            i < ORIGIN(rt_offset) + DB_REF(Size_rt);
482            ++i, ++rec_ptr) {
483 #ifndef  ONE_DB
484          rec_ptr->rt_file += curr_db_table->ft_offset;
485          rec_ptr->rt_fields += curr_db_table->fd_offset;
486 #endif
487 #ifndef  NO_TIMESTAMP
488          if ( rec_ptr->rt_flags & TIMESTAMPED ) {
489             db_tsrecs = TRUE;
490 #ifdef ONE_DB
491             break;
492 #endif
493          }
494 #endif
495       }
496 #endif
497       /* adjust field table entries */
498       for (key_count = 0, i = ORIGIN(fd_offset), 
499               fld_ptr = &field_table[ORIGIN(fd_offset)];
500            i < ORIGIN(fd_offset) + DB_REF(Size_fd);
501            ++i, ++fld_ptr) {
502 #ifndef  ONE_DB
503          fld_ptr->fd_rec += curr_db_table->rt_offset;
504 #endif
505          if ( fld_ptr->fd_key != NOKEY ) {
506             fld_ptr->fd_keyno += key_offset;
507             ++key_count;
508 #ifndef  ONE_DB
509             fld_ptr->fd_keyfile += curr_db_table->ft_offset;
510             if ( fld_ptr->fd_type == 'k' )
511                fld_ptr->fd_ptr += curr_db_table->kt_offset;
512 #endif
513          }
514       }
515       key_offset += key_count;
516
517 #if   DB_ENABLE | TS_ENABLE
518       /* adjust set table entries */
519       for (i = ORIGIN(st_offset), set_ptr = &set_table[ORIGIN(st_offset)];
520            i < ORIGIN(st_offset) + DB_REF(Size_st);
521            ++i, ++set_ptr) {
522 #ifndef  ONE_DB
523          set_ptr->st_own_rt += curr_db_table->rt_offset;
524          set_ptr->st_members += curr_db_table->mt_offset;
525 #endif
526 #ifndef  NO_TIMESTAMP
527          if ( set_ptr->st_flags & TIMESTAMPED ) {
528             db_tssets = TRUE;
529 #ifdef ONE_DB
530             break;
531 #endif
532          }
533 #endif
534       }
535 #endif
536
537 #ifndef  ONE_DB
538       /* adjust member table entries */
539       for (i = curr_db_table->mt_offset, 
540               mem_ptr = &member_table[curr_db_table->mt_offset];
541            i < curr_db_table->mt_offset + curr_db_table->Size_mt;
542            ++i, ++mem_ptr) {
543          mem_ptr->mt_record += curr_db_table->rt_offset;
544          mem_ptr->mt_sort_fld += curr_db_table->srt_offset;
545       }
546
547       /* adjust sort table entries */
548       for (i = curr_db_table->srt_offset, 
549               srt_ptr = &sort_table[curr_db_table->srt_offset];
550            i < curr_db_table->srt_offset + curr_db_table->Size_srt;
551            ++i, ++srt_ptr) {
552          srt_ptr->se_fld += curr_db_table->fd_offset;
553          srt_ptr->se_set += curr_db_table->st_offset;
554       }
555
556       /* adjust key table entries */
557       for (i = curr_db_table->kt_offset, 
558               key_ptr = &key_table[curr_db_table->kt_offset];
559            i < curr_db_table->kt_offset + curr_db_table->Size_kt;
560            ++i, ++key_ptr) {
561          key_ptr->kt_key += curr_db_table->fd_offset;
562          key_ptr->kt_field += curr_db_table->fd_offset;
563       }
564    }  /* end loop for each database */
565 #endif
566    initcurr();
567    return( db_status );
568 }
569
570
571
572 /* Allocate space for dictionary
573 */
574 static int alloc_dict()
575 {
576    int old_size;
577    int new_size;
578    int extra_file = 0;
579 #ifndef ONE_DB
580    DB_ENTRY FAR *db_ptr;
581 #endif
582
583    /* allocate and initialize file_table */
584 #ifndef NO_TRANS
585    if ( use_ovfl ) {
586       extra_file = 1;
587    }
588 #endif
589
590 #ifndef ONE_DB
591    if ( old_no_of_dbs == 0 ) {
592       old_size_ft = 0;
593       old_size_fd = 0;
594       old_size_st = 0;
595       old_size_mt = 0;
596       old_size_srt = 0;
597       old_size_kt = 0;
598       old_size_rt = 0;
599    }
600    else {
601       db_ptr = &db_table[old_no_of_dbs];
602       old_size_ft = db_ptr->Size_ft + db_ptr->ft_offset + extra_file;
603       old_size_fd = db_ptr->Size_fd + db_ptr->fd_offset;
604       old_size_st = db_ptr->Size_st + db_ptr->st_offset;
605       old_size_mt = db_ptr->Size_mt + db_ptr->mt_offset;
606       old_size_srt = db_ptr->Size_srt + db_ptr->srt_offset;
607       old_size_kt = db_ptr->Size_kt + db_ptr->kt_offset;
608       old_size_rt = db_ptr->Size_rt + db_ptr->rt_offset;
609    }
610 #endif
611
612    new_size = (size_ft + extra_file) * sizeof(FILE_ENTRY);
613    old_size = old_size_ft * sizeof(FILE_ENTRY);
614    if ( ALLOC_TABLE(&db_global.File_table, new_size, old_size, "file_table")
615                                                                 != S_OKAY ) {
616       return( db_status );
617    }
618
619    /* allocate record_table */
620    new_size = size_rt * sizeof(RECORD_ENTRY);
621    old_size = old_size_rt * sizeof(RECORD_ENTRY);
622    if ( ALLOC_TABLE(&db_global.Record_table, new_size, old_size, "record_table")
623                                                                 != S_OKAY ) {
624       return( db_status );
625    }
626
627    /* allocate field_table */
628    new_size = size_fd * sizeof(FIELD_ENTRY);
629    old_size = old_size_fd * sizeof(FIELD_ENTRY);
630    if ( ALLOC_TABLE(&db_global.Field_table, new_size, old_size, "field_table")
631                                                                 != S_OKAY ) {
632       return( db_status );
633    }
634
635    /* allocate set table */
636    if ( size_st ) {
637       new_size = size_st * sizeof(SET_ENTRY);
638       old_size = old_size_st * sizeof(SET_ENTRY);
639       if ( ALLOC_TABLE(&db_global.Set_table, new_size, old_size, "set_table")
640                                                                 != S_OKAY ) {
641          return( db_status );
642       }
643    } else set_table = NULL;
644
645    /* allocate member_table */
646    if ( size_mt ) {
647       new_size = size_mt * sizeof(MEMBER_ENTRY);
648       old_size = old_size_mt * sizeof(MEMBER_ENTRY);
649       if ( ALLOC_TABLE(&db_global.Member_table, new_size, old_size, "member_table")
650                                                                 != S_OKAY ) {
651          return( db_status );
652       }
653    } else member_table = NULL;
654
655    /* allocate sort_table */
656    if ( size_srt ) {
657       new_size = size_srt * sizeof(SORT_ENTRY);
658       old_size = old_size_srt * sizeof(SORT_ENTRY);
659       if ( ALLOC_TABLE(&db_global.Sort_table, new_size, old_size, "sort_table")
660                                                                 != S_OKAY ) {
661          return( db_status );
662       }
663    } else sort_table = NULL;
664
665    /* allocate key_table */
666    if ( size_kt ) {
667       new_size = size_kt * sizeof(KEY_ENTRY);
668       old_size = old_size_kt * sizeof(KEY_ENTRY);
669       if ( ALLOC_TABLE(&db_global.Key_table, new_size, old_size, "key_table")
670                                                                 != S_OKAY ) {
671          return( db_status );
672       }
673    } else key_table = NULL;
674
675    return( db_status = S_OKAY );
676 }
677
678 /* Initialize currency tables 
679 */
680 static int initcurr()
681 {
682    register int dbt_lc;                 /* loop control */
683    register int rec, i;
684    RECORD_ENTRY FAR *rec_ptr;
685    SET_ENTRY FAR *set_ptr;
686    DB_ADDR FAR *co_ptr;
687    int old_size;
688    int new_size;
689
690    /* Initialize current record and type */
691 #ifndef  ONE_DB
692    for (dbt_lc = no_of_dbs, curr_db_table = &db_table[old_no_of_dbs],
693                                 curr_rn_table = &rn_table[old_no_of_dbs];
694         --dbt_lc >= 0; ++curr_db_table, ++curr_rn_table) {
695       DB_REF(curr_dbt_rec) = NULL_DBA;
696 #endif
697       RN_REF(rn_dba)   = NULL_DBA;
698       RN_REF(rn_type)  = -1;
699 #ifndef ONE_DB
700    }
701 #endif
702
703    if ( size_st ) {
704       new_size = size_st * sizeof(DB_ADDR);
705       old_size = old_size_st * sizeof(DB_ADDR);
706       if ( ALLOC_TABLE(&db_global.Curr_own, new_size, old_size, "curr_own")
707                                                                 != S_OKAY ) {
708          return( db_status );
709       }
710       if ( ALLOC_TABLE(&db_global.Curr_mem, new_size, old_size, "curr_mem")
711                                                                 != S_OKAY ) {
712          return( db_status );
713       }
714 #ifndef  NO_TIMESTAMP
715       new_size = size_st * sizeof(ULONG);
716       old_size = old_size_st * sizeof(ULONG);
717       if ( db_tsrecs ) {
718          if ( ALLOC_TABLE(&db_global.Co_time, new_size, old_size, "co_time")
719                                                                 != S_OKAY ) {
720             return( db_status );
721          }
722          if ( ALLOC_TABLE(&db_global.Cm_time, new_size, old_size, "cm_time")
723                                                                 != S_OKAY ) {
724             return( db_status );
725          }
726       }
727       if ( db_tssets ) {
728          if ( ALLOC_TABLE(&db_global.Cs_time, new_size, old_size, "cs_time")
729                                                                 != S_OKAY ) {
730             return( db_status );
731          }
732       }
733 #endif
734       /* for each db make system record as curr_own of its sets */
735 #ifndef  ONE_DB
736       for (dbt_lc = no_of_dbs, curr_db_table = &db_table[old_no_of_dbs]; 
737            --dbt_lc >= 0; ++curr_db_table) {
738 #endif
739          for (rec = ORIGIN(rt_offset), 
740                  rec_ptr = &record_table[ORIGIN(rt_offset)];
741               rec < ORIGIN(rt_offset) + DB_REF(Size_rt);
742               ++rec, ++rec_ptr) {
743             if (rec_ptr->rt_fdtot == -1) { 
744                /* found system record */
745                curr_rec = ((FILEMASK & NUM2EXT(rec_ptr->rt_file, ft_offset))
746                                                             << FILESHIFT) | 1L;
747                /* make system record current of sets it owns */
748                for (i = ORIGIN(st_offset), 
749                        set_ptr = &set_table[ORIGIN(st_offset)],
750                        co_ptr = &curr_own[ORIGIN(st_offset)];
751                     i < ORIGIN(st_offset) + DB_REF(Size_st);
752                     ++i, ++set_ptr, ++co_ptr) {
753                   if (set_ptr->st_own_rt == rec) {
754                      *co_ptr = curr_rec;
755                   }
756                }
757                DB_REF(sysdba) = curr_rec;
758 #ifndef ONE_DB
759                DB_REF(curr_dbt_rec) = curr_rec;
760 #endif
761                break;
762             }
763          }
764 #ifndef  ONE_DB
765       }
766 #endif
767    }
768    else {
769       curr_own = NULL;
770       curr_mem = NULL;
771    }
772 #ifndef  ONE_DB
773    curr_db = 0;
774    MEM_LOCK(&db_global.Db_table);
775    curr_db_table = db_table;
776    MEM_LOCK(&db_global.Rn_table);
777    curr_rn_table = rn_table;
778    setdb_on = FALSE;
779    curr_rec = DB_REF(curr_dbt_rec);
780 #endif
781    return( db_status = S_OKAY );
782 }
783 /* vpp -nOS2 -dUNIX -nBSD -nVANILLA_BSD -nVMS -nMEMLOCK -nWINDOWS -nFAR_ALLOC -f/usr/users/master/config/nonwin inittab.c */