Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / ausdopen.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: austext_dopen
27  *
28  *   ORIGINS: 27
29  *
30  *
31  *   (C) COPYRIGHT International Business Machines Corp. 1994,1995
32  *   All Rights Reserved
33  *   Licensed Materials - Property of IBM
34  *   US Government Users Restricted Rights - Use, duplication or
35  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
36  */
37 /*************************** AUSDOPEN.C **************************
38  * $XConsortium: ausdopen.c /main/6 1996/08/12 13:17:32 cde-ibm $
39  * April 1994.
40  * Austext_dopen() performs vista d_open() for an AusText database.
41  * Optionally sets vista page cache size, renames the files,
42  * opens the database, and optionally reads the dbrec.
43  * Intended to be used by offline programs like cravel, cborodin,
44  * and various utilities.  Similar to opendblk.c.
45  *
46  * $Log$
47  * Revision 2.3  1995/10/25  22:22:45  miker
48  * Added prolog.
49  *
50  * Revision 2.2  1995/10/19  20:20:51  miker
51  * Deleted all RENFILE calls--database files no longer have to be renamed.
52  *
53  * Revision 2.1  1995/09/22  19:04:52  miker
54  * Freeze DtSearch 0.1, AusText 2.1.8
55  *
56  * Revision 1.5  1995/08/31  21:53:28  miker
57  * Minor changes for DtSearch.
58  *
59  * Revision 1.4  1995/05/30  18:19:21  miker
60  * write all err msgs to aa_stderr (write_msg) rather than stdout.
61  */
62 #include "SearchP.h"
63 #include "vista.h"
64
65 #define PROGNAME        "AUSDOPEN"
66
67
68 /****************************************/
69 /*                                      */
70 /*             austext_dopen            */
71 /*                                      */
72 /****************************************/
73 /* dbname:      1 - 8 char database name.
74  * path:        Optional path prefix for .dbd file.
75  *              If NULL is passed, default is vista
76  *              default (current working directory).
77  * d2xpath:     Used only for mrclean; should be NULL for all other callers.
78  *              Specifies path prefix for rename of d2x files only.
79  * cache:       Optional vista paging size.
80  *              If zero is passed, default is global CACHE_SIZE.
81  *              If < 16 specified, silently adjusted up to minimum 16.
82  *              If cache specified is not a power of 2,
83  *              cache adjusted upward to nearest power of 2.
84  * dbrec:       If NULL is passed, dbrec is not read.  Otherwise
85  *              database's dbrec is read into passed buffer.
86  *
87  * RETURNS:     TRUE if all went well.
88  *              FALSE and writes error msg to ausapi_msglist if could not open.
89  *              (vista abort prints out its own error messages).
90  */
91 int     austext_dopen (
92                 char    *dbname,
93                 char    *path,
94                 char    *d2xpath,
95                 int     cache,
96                 DBREC   *dbrec)
97 {
98     char            dbdbuf[2048];
99     char            d2xbuf[2048];
100     int             i;
101     char            sprintbuf[2048];
102     char           *d2xptr, *ptr, *src;
103
104     /* Test dbname */
105     if (dbname == NULL) {
106 INVALID_DBNAME:
107         sprintf (sprintbuf, catgets (dtsearch_catd, 13, 348,
108             "%s Invalid database name '%s'."),
109             PROGNAME"348", NULLORSTR(dbname));
110         DtSearchAddMessage (sprintbuf);
111         return FALSE;
112     }
113     i = strlen (dbname);
114     if (i < 1 || i > 8)
115         goto INVALID_DBNAME;
116
117     /* Test cache, silently rounding up to nearest power of 2.
118      * 2^4 = 16 = minimum cache.  2^12 = 4096 = maximum cache.
119      */
120     if (cache == 0)
121         cache = CACHE_SIZE;
122     else {
123         for (i = 4; i < 12; i++)
124             if (1 << i >= cache)
125                 break;
126         cache = 1 << i;
127     }
128
129     /* If mrclean needs special d2x renames, build them now.
130      * (d2xptr is where the ".d2x" extensions will be copied.)
131      */
132     if (d2xpath) {
133         d2xptr = d2xbuf;
134         for (i = 0; i < sizeof (d2xbuf) - 14; i++) {
135             if (d2xpath[i] == 0)
136                 break;
137             *d2xptr++ = d2xpath[i];
138         }
139         if (i > 0 && *(d2xptr - 1) != LOCAL_SLASH)
140             *d2xptr++ = LOCAL_SLASH;
141         src = dbname;
142         while (*src != 0)
143             *d2xptr++ = *src++;
144     }
145
146     /* Copy path, if any, to name buffer leaving room for the slash
147      * which the caller may not have originally specified,
148      * the 8 char database name, the 3 char file name extensions,
149      * and the terminating \0.  Then set 'ptr' to the place
150      * where the dbdname should be appended.
151      */
152     ptr = dbdbuf;
153     if (path != NULL) {
154         for (i = 0; i < sizeof (dbdbuf) - 14; i++) {
155             if (path[i] == 0)
156                 break;
157             *ptr++ = path[i];
158         }
159         if (i > 0 && *(ptr - 1) != LOCAL_SLASH)
160             *ptr++ = LOCAL_SLASH;
161     }
162
163     /* Now concatenate the dbname and set ptr to where
164      * the file name extensions should go.
165      */
166     src = dbname;
167     while (*src != 0)
168         *ptr++ = *src++;
169
170     /* Specify the cache size and open the database.
171      * I use the original d_open() call so I can print
172      * a good error msg if it fails.
173      */
174     *ptr = 0;   /* no extension used for .dbd file in OPEN */
175     SETPAGES (PROGNAME "283", cache, 4);
176     d_open (dbdbuf, "o");
177     if (db_status != S_OKAY) {
178         sprintf (sprintbuf, catgets (dtsearch_catd, 13, 379,
179             "%s Could not open database '%s':\n  %s."),
180             PROGNAME"379", dbdbuf, vista_msg (PROGNAME"379"));
181         DtSearchAddMessage (sprintbuf);
182         return FALSE;
183     }
184
185     /* From here on, emergency exits MUST close the database. */
186     austext_exit_dbms = (void (*) (int)) d_close;
187
188     /* If requested, read the dbrec into caller's buffer. */
189     if (dbrec != NULL) {
190         RECFRST (PROGNAME "285", OR_DBREC, 0);  /* seqtl retrieval */
191         if (db_status != S_OKAY) {
192 NO_DBREC:
193             sprintf (sprintbuf,
194                 PROGNAME "289 Database '%s' has not been initialized.",
195                 dbname);
196             DtSearchAddMessage (sprintbuf);
197             return FALSE;
198         }
199         RECREAD (PROGNAME "302", dbrec, 0);
200         if (db_status != S_OKAY)
201             goto NO_DBREC;
202         swab_dbrec (dbrec, NTOH);
203     }
204
205     return TRUE;
206 }  /* austext_dopen() */
207
208 /*************************** AUSDOPEN.C **************************/