Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtsearchpath / dtsp / InfoLibSearchPath.C
1 /* $TOG: InfoLibSearchPath.C /main/5 1998/08/17 10:33:55 mgreess $
2 /*
3  * (c) Copyright 1996 Digital Equipment Corporation.
4  * (c) Copyright 1996 Hewlett-Packard Company.
5  * (c) Copyright 1996 International Business Machines Corp.
6  * (c) Copyright 1996 Sun Microsystems, Inc.
7  * (c) Copyright 1996 Novell, Inc. 
8  * (c) Copyright 1996 FUJITSU LIMITED.
9  * (c) Copyright 1996 Hitachi.
10  */
11 #include <stdlib.h>
12 #include "SearchPath.h"
13 #include "Environ.h"
14 #include "TTFile.h"
15 #include "Options.h"
16
17 extern Options * options;
18
19 /**********************************************************************
20  *
21  * InfoLibSearchPath
22  *
23  *      this constructor creates DTINFOLIBSEARCHPATH in a three-step 
24  *      process.
25  *
26  *      1. gathers environment variables and defaults to create a 
27  *         colon-separated list of paths
28  *      2. normalizes the list into host:/path format
29  *      3. builds the final version of the path
30  *
31  *      Hierarchy of search paths:
32  *              DTSPUSERINFOLIB
33  *              DTSPUSERAPPHOSTS
34  *              System Administrator's configuration directory
35  *              DTSPSYSINFOLIB
36  *              DTSPSYSAPPHOSTS
37  *              Factory location
38  *
39  **********************************************************************/
40 InfoLibSearchPath::InfoLibSearchPath
41         (
42         CDEEnvironment * user,
43         const char *     envvar,
44         const char *     sep
45         ) : SearchPath(user, envvar, sep)
46 {
47     CString oldSysAdmConfig = user->sysAdmConfig;
48     CString oldFactoryInstall = user->factoryInstall;
49
50     // Need to re-initialize the defaults.
51     user->sysAdmConfig   = "/etc/dt";
52     user->factoryInstall = "/usr/dt";
53
54     if (user->DTINFOLIBSP()) {
55         if (user->DTUSERINFOLIBSP()) {
56             search_path = *user->DTUSERINFOLIBSP() + ",";
57             if (user->DTUSERAPPSP())
58                 search_path += *user->DTUSERAPPSP() + ",";
59             if (!user->DTINFOLIBSP()->contains(user->OS()->LocalHost(),",",":") &&
60                 (!user->DTAPPSP() ||
61                  !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":")))
62                 search_path += user->SysAdmConfig() + ",";
63             search_path += *user->DTINFOLIBSP() + ",";
64             if (user->DTINFOLIBSP()->contains(user->OS()->LocalHost(),",",":") &&
65                 user->DTAPPSP() &&
66                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
67                 search_path.replace(user->OS()->LocalHost() + ":,","");
68             if (user->DTAPPSP())
69                 search_path += *user->DTAPPSP() + ",";
70             search_path += user->FactoryInstall();
71         }
72         else {
73             if (user->DTUSERAPPSP())
74                 search_path = *user->DTUSERAPPSP() + ",";
75             if (!user->DTINFOLIBSP()->contains(user->OS()->LocalHost(),",",":") &&
76                 (!user->DTAPPSP() ||
77                  !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":")))
78                 search_path += user->SysAdmConfig() + ",";
79             search_path += *user->DTINFOLIBSP() + ",";
80             if (user->DTINFOLIBSP()->contains(user->OS()->LocalHost(),",",":") &&
81                 user->DTAPPSP() &&
82                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
83                 search_path.replace(user->OS()->LocalHost() + ":,","");
84             if (user->DTAPPSP())
85                 search_path += *user->DTAPPSP() + ",";
86             search_path += user->FactoryInstall();
87         }
88     }
89     else if (user->DTUSERINFOLIBSP()) {
90         search_path = *user->DTUSERINFOLIBSP() + ",";
91
92         if (user->DTUSERAPPSP())
93             search_path += *user->DTUSERAPPSP() + ",";
94
95         search_path += user->SysAdmConfig() + ",";
96         if (user->DTAPPSP())
97             search_path += *user->DTAPPSP() + ",";
98         search_path += user->FactoryInstall();
99     }
100     else {
101         if (user->DTUSERAPPSP())
102             search_path = *user->DTUSERAPPSP() + ",";
103
104         search_path += user->SysAdmConfig() + ",";
105         if (user->DTAPPSP())
106             search_path += *user->DTAPPSP() + ",";
107         search_path += user->FactoryInstall();
108     }
109
110     if (options->CheckingUser())
111     {
112         if (user->DTAPPSP())
113             search_path = *user->DTAPPSP();
114     }
115
116     NormalizePath();
117     TraversePath();
118
119     user->sysAdmConfig = oldSysAdmConfig;
120     user->factoryInstall = oldFactoryInstall;
121 }
122
123
124 /***********************************************************************
125  *  MakePath()
126  *
127  *     Given a search path element (host name:path name pair), construct
128  *     the appropriate path for this particular desktop subsystem:
129  *
130  *     DTINFOLIBSEARCHPATH
131  *
132  *     A path is constructed so that each host:/path pair is appended.
133  ***********************************************************************/
134 void InfoLibSearchPath::MakePath
135         (
136         const CString & pair
137         ) 
138 {
139     CTokenizedString element(pair,":");
140     CString host_element = element.next();
141     CString path_element = element.next();
142
143     if (path_element.contains(user->HOME())) {
144         if (host_element == user->OS()->LocalHost()) {
145             CString infolibPath(path_element);
146             if (user->OS()->isDirectory(infolibPath + "/infolib")
147                  || options->dontOptimize()) {
148                 AddToPath (infolibPath + "/infolib/" + user->UserHostDir() + 
149                            "%I.dti");
150                 AddToPath (infolibPath + "/infolib/%L/%I.dti");
151             }
152         }
153         else {
154             CString dir(ConstructPath(path_element + "/infolib", &host_element));
155             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
156                 AddToPath (dir + "/" + user->UserHostDir() + "/%I.dti");
157                 AddToPath (dir + "/%I.dti");
158             }
159         }
160     }
161     else {
162         if (host_element == user->OS()->LocalHost()) {
163             CString infolibPath(path_element);
164             if (validSearchPath(infolibPath)) {
165                 infolibPath += "/infolib";
166                 if (user->OS()->isDirectory(infolibPath)
167                      || options->dontOptimize()) {
168                     AddToPath (infolibPath + "/%L/%I.dti");
169                 }
170                 infolibPath += "/C";
171             }
172             if (user->OS()->isDirectory(infolibPath) || 
173                                 options->dontOptimize()) {
174                 AddToPath (infolibPath + "/%I.dti");
175             }
176         }
177         else {
178             CString dir(ConstructPath(path_element, &host_element));
179             if (validSearchPath(dir)) {
180                 dir += "/infolib";
181                 if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
182                     AddToPath (dir + "/%L/%I.dti");
183                 }
184                 dir += "/C";
185             }
186             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
187                 AddToPath (dir + "/%I.dti");
188             }
189         }
190     }
191 }
192
193 /*********************************************************************
194  *  validSearchPath()
195  *
196  *    This member function verifies that the path in question is a
197  *    standard CDE location, i.e. /etc/dt/appconfig or /usr/dt/appconfig
198  *    so that the appropriate massaging can take place.
199  *
200  *********************************************************************/
201 int InfoLibSearchPath::validSearchPath
202         (
203         const CString & st
204         ) const
205 {
206     if (st == user->SysAdmConfig())     // ...,/etc/dt/appconfig,...
207         return 1;
208
209     if (st == user->FactoryInstall())   // ...,/usr/dt/appconfig,...
210         return 1;
211
212     // ...,/nfs/machine/etc/dt/appconfig,...
213
214     if (st.contains(user->SysAdmConfig(), "", Separator().data()))
215         return 1;
216
217     // ...,/nfs/machine/usr/dt/appconfig,...
218
219     if (st.contains(user->FactoryInstall(),"",Separator().data()))
220         return 1;
221
222     // If this is an APP-specified path, it also needs to be massaged.
223     // The elements in the APP paths were originally specified using 
224     // host:/path, but were converted to /path for InfoLib searchpaths
225     // so use the comma as the leader and the trailer.
226
227     if (user->DTAPPSP() && 
228         user->DTAPPSP()->contains (st, ",", ","))
229         return 1;
230
231     if (user->DTUSERAPPSP() && 
232         user->DTUSERAPPSP()->contains (st, ",", ","))
233         return 1;
234
235     return 0;
236 }