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