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