DtSvc/DtUtil1: fix implicit function declarations
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / DbLoad.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 libraries 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  * +SNOTICE
25  * 
26  * $XConsortium: DbLoad.c /main/7 1996/08/28 14:38:07 rswiston $
27  * 
28  * RESTRICTED CONFIDENTIAL INFORMATION:
29  * 
30  * The information in this document is subject to special restrictions in a
31  * confidential disclosure agreement bertween HP, IBM, Sun, USL, SCO and
32  * Univel.  Do not distribute this document outside HP, IBM, Sun, USL, SCO,
33  * or Univel wihtout Sun's specific written approval.  This documment and all
34  * copies and derivative works thereof must be returned or destroyed at Sun's
35  * request.
36  * 
37  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
38  * 
39  * +ENOTICE
40  */
41  
42 /*                                                                      *
43  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
44  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
45  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
46  * (c) Copyright 1993, 1994 Novell, Inc.                                *
47  */
48
49 #include <stdio.h>
50 #include <sys/types.h>
51
52 #ifdef __hpux
53 #include <ndir.h>
54 #else
55
56 #if defined(sun) || defined(CSRG_BASED)
57 #include <dirent.h>
58 #else
59 #include <sys/dir.h>
60 #endif                          /* sun || CSRD_BASED */
61
62 #endif                          /* __hpux */
63
64 #include <ctype.h>
65 #include <string.h>
66
67 #ifdef NLS16
68 #include <limits.h>
69 #endif
70
71 #include <sys/stat.h>
72 #include <sys/param.h>          /* MAXPATHLEN, MAXHOSTNAMELEN */
73 #include <X11/Xlib.h>
74 #include <X11/Intrinsic.h>
75 #include <X11/StringDefs.h>
76 #include <Dt/DtP.h>
77 #include <Dt/Connect.h>
78 #include <Dt/FileUtil.h>
79 #include <Dt/DtNlUtils.h>
80 #include <Dt/Action.h>
81 #include <Dt/ActionDbP.h>
82 #include <Dt/ActionP.h>
83 #include <Dt/ActionFind.h>
84 #include <Dt/DbUtil.h>
85 #include <Dt/DtPStrings.h>
86 #include <Dt/Utility.h>
87
88 #include <Dt/DtsDb.h>
89 #include <Dt/Dts.h>
90
91 #include "myassertP.h"
92 #include "DtSvcLock.h"
93
94 extern  void    _DtDtsDCConverter(DtDtsDbField * fields,
95                DtDbPathId pathId,
96                char *hostPrefix,
97                Boolean rejectionStatus);
98 extern  void    _DtDtsDAConverter(DtDtsDbField * fields,
99                DtDbPathId pathId,
100                char *hostPrefix,
101                Boolean rejectionStatus);
102
103 extern  void    _DtDtsSeqReset(void);
104 extern  int     _DtDtsNextDCSeq(void);
105 extern  int     _DtDtsNextDASeq(void);
106 extern  int     _DtDtsMMUnLoad(void);
107 extern  void    _DtFreeActionDB(void);
108
109 char **_DtsDbListDb(void);
110
111 int      use_in_memory_db = False;
112
113 static void
114 _DtOAConverter(DtDtsDbField * fields,
115                DtDbPathId pathId,
116                char *hostPrefix,
117                Boolean rejectionStatus)
118 {
119         DtDtsDbDatabase *dc_db;
120         DtDtsDbDatabase *da_db;
121         DtDtsDbRecord  *dc_rec;
122         DtDtsDbRecord  *da_rec;
123         DtDtsDbField   *fld;
124
125         _DtSvcProcessLock();    
126         dc_db = _DtDtsDbGet(DtDTS_DC_NAME);
127         da_db = _DtDtsDbGet(DtDTS_DA_NAME);
128         if ( _DtDtsDbGetRecordByName(dc_db,
129                                 fields[0].fieldValue) ||
130                 _DtDtsDbGetRecordByName(da_db, 
131                                 fields[0].fieldValue))
132         {
133                 _DtSvcProcessUnlock();
134                 return;
135         }
136
137         /*
138          * Synthesize criteria record -- for this action
139          */
140         dc_rec = _DtDtsDbAddRecord(_DtDtsDbGet(DtDTS_DC_NAME));
141         dc_rec->recordName = XrmStringToQuark(fields[0].fieldValue);
142         dc_rec->seq = _DtDtsNextDCSeq();
143         dc_rec->pathId = (int)pathId;
144         fld = _DtDtsDbAddField(dc_rec);
145         fld->fieldName = XrmStringToQuark(DtDTS_NAME_PATTERN);
146         fld->fieldValue = strdup(fields[0].fieldValue);
147         fld = _DtDtsDbAddField(dc_rec);
148         fld->fieldName = XrmStringToQuark(DtDTS_MODE);
149         fld->fieldValue = strdup("fx");
150         fld = _DtDtsDbAddField(dc_rec);
151         fld->fieldName = XrmStringToQuark(DtDTS_DATA_ATTRIBUTES_NAME);
152         fld->fieldValue = strdup(fields[0].fieldValue);
153         /*
154          * Mark the criteria record as synthetic.
155          */
156         fld = _DtDtsDbAddField(dc_rec);
157         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_SYNTHETIC);
158         fld->fieldValue = strdup("True");
159
160         /*
161          * Synthesize attribute record -- for this action
162          */
163         da_rec = _DtDtsDbAddRecord(_DtDtsDbGet(DtDTS_DA_NAME));
164         da_rec->recordName = XrmStringToQuark(fields[0].fieldValue);
165         da_rec->seq = _DtDtsNextDASeq();
166         da_rec->pathId = (int)pathId;
167         fld = _DtDtsDbAddField(da_rec);
168         fld->fieldName = XrmStringToQuark(DtDTS_DA_ACTION_LIST);
169         fld->fieldValue = strdup(fields[0].fieldValue);
170         fld = _DtDtsDbAddField(da_rec);
171         fld->fieldName = XrmStringToQuark(DtDTS_DA_DATA_HOST);
172         fld->fieldValue = strdup(hostPrefix);
173
174         /*
175          * Mark the attribute record as synthetic.
176          */
177         fld = _DtDtsDbAddField(da_rec);
178         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_SYNTHETIC);
179         fld->fieldValue = strdup("True");
180
181         fld = _DtDtsDbAddField(da_rec);
182         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_ACTION);
183         fld->fieldValue = strdup("True");
184
185         fld = _DtDtsDbAddField(da_rec);
186         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_TEXT);
187         fld->fieldValue = strdup("False");
188
189         /*
190          * Use the action name as the default copy_to action
191          */
192         fld = _DtDtsDbAddField(da_rec);
193         fld->fieldName = XrmStringToQuark(DtDTS_DA_COPY_TO_ACTION);
194         fld->fieldValue = strdup(fields[0].fieldValue);
195         _DtSvcProcessUnlock();
196 }
197
198
199 /******************************************************************************
200  *
201  * DtDbLoad -
202  *
203  *   Reads in the file types and action databases.
204  *   From the Default DtDatabaseDirPaths. 
205  *
206  *****************************************************************************/
207
208 void _DtDbLoad(DtDirPaths *dirs)
209 {
210         _DtDtsMMUnLoad();
211 }
212
213 void
214 DtDbLoad(void)
215 {
216         DtDirPaths      *dirs;
217
218         /* Load the requested database files */
219         dirs = _DtGetDatabaseDirPaths();
220         _DtDbLoad(dirs);
221         _DtFreeDatabaseDirPaths(dirs);
222 }
223
224 /******************************************************************************
225  *
226  * _DtDbLoad(dirs) -
227  *
228  *   Reads in the file types and action databases.
229  *   From the the given directory path.  This function is not part of the
230  *   public API but may be used internally to avoid repeated generation of
231  *   the default databasedir path.
232  *
233  *****************************************************************************/
234 int
235 _DtDtsMMCreateDb(DtDirPaths *dirs, const char *CacheFile, int override)
236 {
237         DtDbRecordDesc  recordDescriptions[3];
238         DtDbConverter   criteriaConverters[2];
239         DtDbConverter   attributesConverters[2];
240         DtDbConverter   actionConverters[3];
241         static  int     beenCalled = 0;
242         char            **list;
243         int             i;
244
245         _DtSvcProcessLock();
246
247         use_in_memory_db = TRUE;
248
249         if ( beenCalled ) {
250                 /*
251                  * Free up existing database.
252                  */
253                 _DtDtsSeqReset();
254                 DtDtsRelease();
255                 _DtFreeActionDB();
256
257         }
258         beenCalled = 1;
259
260         /*
261          * Initialize the databases
262          * -- this call will initializae the Object databases then
263          *    add the action database.
264          */
265         (void) _DtDtsDbAddDatabase(_DtACTION_NAME);
266         
267         if ( !dirs )
268         {
269                 myassert(dirs); /* register an assertion failure */
270                 _DtSvcProcessUnlock();
271                 return(0);
272         }
273
274         /* Build up the record descriptions */
275         criteriaConverters[0] = (DtDbConverter) _DtDtsDCConverter;
276         criteriaConverters[1] = NULL;
277         recordDescriptions[0].recordKeyword = DtDTS_DC_NAME;
278         recordDescriptions[0].maxFields = _DtMAX_NUM_FIELDS;
279         recordDescriptions[0].converters = criteriaConverters;
280
281         /* Build up the ot record descriptions */
282         attributesConverters[0] = (DtDbConverter) _DtDtsDAConverter;
283         attributesConverters[1] = NULL;
284         recordDescriptions[1].recordKeyword = DtDTS_DA_NAME;
285         recordDescriptions[1].maxFields = _DtMAX_NUM_FIELDS;
286         recordDescriptions[1].converters = attributesConverters;
287
288         actionConverters[0] = (DtDbConverter) _DtActionConverter;
289         actionConverters[1] = (DtDbConverter) _DtOAConverter;
290         actionConverters[2] = NULL;
291         recordDescriptions[2].recordKeyword = _DtACTION_NAME;
292         recordDescriptions[2].maxFields = _ActDb_MAX_NUM_FIELDS;
293         recordDescriptions[2].converters = actionConverters;
294
295
296         _DtDbRead(dirs, ".dt", recordDescriptions, 3);
297
298         _DtSortActionDb();
299
300         /* 
301          * we may eventually want to return a count of the new records.
302          * for now we return a non-zero value to register success.
303          */
304
305         if ((!_DtDtsMMCreateFile(dirs, CacheFile)) ||
306             (!_DtDtsMMapDB(CacheFile)))
307         {
308                 _DtSvcProcessUnlock();
309                 return(0);
310         }
311         if(!override)
312         {
313                 unlink(CacheFile);
314         }       
315
316         /* now that we have built the databases delete the tmp Db memory
317            used for it (Too, bad we can't delete the memory associcated
318            with the Quarks) */
319         list = (char **)_DtsDbListDb();
320         for(i = 0; list[i]; i++)
321         {
322                 _DtDtsDbDeleteDb(_DtDtsDbGet(list[i]));
323                 free(list[i]);
324         }
325         free(list);
326         use_in_memory_db = FALSE;
327
328         _DtSvcProcessUnlock();
329         return(1);
330 }