2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: SearchPath.C /main/3 1995/11/03 12:32:30 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 ****************************<+>*************************************/
32 #include "SearchPath.h"
36 extern Options * options;
38 /**********************************************************************
40 * SearchPath() - the constructor simply initializes the member data.
42 **********************************************************************/
43 SearchPath::SearchPath
45 CDEEnvironment * user_,
49 environment_var(envvar),
52 final_search_path(""),
57 /**********************************************************************
59 * ~SearchPath() - the destructor - nothing to do
61 **********************************************************************/
62 SearchPath::~SearchPath()
67 /**********************************************************************
69 * constructPath - this member function takes a host and a path and
70 * either calls the Tooltalk filemapping library or
71 * constructs it in the standard fashion.
73 **********************************************************************/
74 CString SearchPath::ConstructPath
84 if (options->doingTooltalk()) {
87 file = new TTFile(*host,path);
88 if (!file->ttFileOpFailed())
90 if (options->removeAutoMountPoint()) {
91 if (final_path.contains(options->getAutoMountPoint(),
93 final_path.replace(options->getAutoMountPoint(),"");
96 Catch (TTFile::TT_Exception *, file) {
97 final_path = user->OS()->MountPoint() + *host + path;
102 final_path = user->OS()->MountPoint() + *host + path;
110 /*************************************************************************
113 * Given a series of string elements one at a time--either a host
114 * or a path, in any order--tokenize them, and create
115 * a normalized path of the form:
117 * "host:/path,host:/path"
119 * Return this normalized path in $RETURN_STRING.
121 *************************************************************************/
122 void SearchPath::AssembleString
124 const CString & path_element
127 if (parse_state == 1 && norm_search_path.length())
128 if (norm_search_path[norm_search_path.length()-1] != ',')
129 norm_search_path += ",";
131 if (path_element.length() == 0) {
133 if (parse_state == 2) {
134 CString path(save_host + ":" + user->SysAdmConfig());
135 if (!norm_search_path.contains(path, ",", ","))
136 norm_search_path += path;
140 else if (path_element[0] == '/') {
144 if (parse_state == 1) {
145 CString path(user->OS()->LocalHost() + ":" + path_element);
146 if (!norm_search_path.contains(path, ",", ","))
147 norm_search_path += path;
150 else if (parse_state == 2) {
151 CString path(save_host + ":" + path_element);
152 if (!norm_search_path.contains(path, ",", ","))
153 norm_search_path += path;
158 else if (path_element[0] == '>') {
160 // terminator--write out the saved host and default path.
162 CString path(save_host + ":" + user->SysAdmConfig());
163 if (!norm_search_path.contains(path, ",", ","))
164 norm_search_path += path;
172 if (parse_state == 1) {
173 save_host = path_element;
177 else if (parse_state == 2) {
178 CString path(save_host + ":" + user->SysAdmConfig());
179 if (!norm_search_path.contains(path, ",", ","))
180 norm_search_path += path;
181 save_host = path_element;
187 /*****************************************************************
190 * This function takes a complete string and calls the helper
191 * function AssembleString() to build a normalized path.
193 *****************************************************************/
194 void SearchPath::NormalizePath()
198 if (search_path.length()) {
199 CTokenizedString subpath(search_path,":,");
200 CString dirname = subpath.next();
201 while (!subpath.Finished()) {
202 AssembleString (dirname);
203 dirname = subpath.next();
209 if (parse_state == 2)
216 /*********************************************************************
219 * Take the second parameter (if it exists), and add the
220 * first parameter (always assumed) to its end separated by the
221 * specified separator
222 *********************************************************************/
224 void SearchPath::AddToPath
229 if (!final_search_path.isNull()) {
230 if (!final_search_path.contains (path, Separator().data(),
231 Separator().data())) {
232 final_search_path += Separator();
233 final_search_path += path;
237 final_search_path += path;
240 /*********************************************************************
243 * Parse a given search path, using comma (,) and colon (:) as
244 * delimiters. Pass the normalized path to the virtual MakePath.
247 * N.B. We assume that we have a normalized path at this point.
249 *********************************************************************/
250 void SearchPath::TraversePath()
252 if (!norm_search_path.isNull()) {
253 CTokenizedString subpath(norm_search_path,",");
254 CString dirname = subpath.next();
255 while (!dirname.isNull()) {
257 dirname = subpath.next();
263 /*********************************************************************
264 * AddPredefinedPath()
266 * If it is possible that an environment variable can be set
267 * before dtsearchpath is run, check for its existence, and if it
268 * exists, append it to the searchpath.
270 *********************************************************************/
271 void SearchPath::AddPredefinedPath()
273 CString envVar_in(environment_var);
274 envVar_in += "SEARCHPATH";
275 CString envVar_out = user->OS()->getEnvironmentVariable(envVar_in.data());
276 if (!envVar_out.isNull()) {
277 CString specified_path(envVar_out);
278 if (!specified_path.contains(search_path))
279 search_path += Separator() + specified_path;
281 search_path = specified_path;
286 /*********************************************************************
289 * This member function verifies that the path in question is a
290 * standard CDE location, i.e. /etc/dt/appconfig or /usr/dt/appconfig
291 * so that the appropriate massaging can take place.
293 *********************************************************************/
294 int SearchPath::validSearchPath
299 if (st == user->SysAdmConfig())
302 if (st == user->FactoryInstall())
305 // If this is an APP-specified path, it also needs to be massaged.
307 // The elements in the APP paths may be host:/path so use the
308 // colon as the leader.
310 if (user->DTAPPSP() &&
311 user->DTAPPSP()->contains (st, ":", separator.data()))
314 if (user->DTUSERAPPSP() &&
315 user->DTUSERAPPSP()->contains (st, ":", separator.data()))
318 // they may also be there by themselves: "host:,/path" so use the
319 // comma as the leader.
321 if (user->DTAPPSP() &&
322 user->DTAPPSP()->contains (st, ",", separator.data()))
325 if (user->DTUSERAPPSP() &&
326 user->DTUSERAPPSP()->contains (st, ",", separator.data()))
332 /*********************************************************************
335 * This member function verifies whether system environment variable
336 * should be left unmodified, since some other configuraton mechanism
340 int SearchPath::useSystemPath()
345 /*****************************************************************
348 * Export the variable value to the rest of the session.
350 *****************************************************************/
351 void SearchPath::ExportPath()
353 CString env(environment_var);
356 if (!useSystemPath()) {
357 user->OS()->shell()->putToEnv(env, final_search_path.data());
362 /*****************************************************************
365 * In the absence of iostreams, use printf to output information
367 *****************************************************************/
368 void SearchPath::Print()
370 printf("%sSEARCHPATH:\n", GetEnvVar());
371 CString sp(GetSearchPath());
372 if (!useSystemPath() && !sp.isNull()) {
373 CTokenizedString path (sp,Separator().data());
374 CString subpath = path.next();
375 while (!subpath.isNull()) {
376 printf("\t%s\n",subpath.data());
377 subpath = path.next();
383 /****************************************************************
386 * Print the list such that each entry occupys its own line
388 ****************************************************************/
390 #if defined(linux) || defined(CSRG_BASED)
391 void SearchPath::PrettyPrint
396 void SearchPath::PrettyPrint
402 CTokenizedString path (GetSearchPath(), Separator().data());
403 CString subpath = path.next();
404 while (!subpath.isNull()) {
405 #if defined(linux) || defined(CSRG_BASED)
406 os << " " << subpath << std::endl;
408 os << " " << subpath << endl;
410 subpath = path.next();
412 #if defined(linux) || defined(CSRG_BASED)
420 /****************************************************************
423 * Allow SearchPath types to be output using iostreams.
425 ****************************************************************/
427 #if defined(linux) || defined(CSRG_BASED)
428 std::ostream & operator<<
431 const SearchPath & sp
437 const SearchPath & sp
441 #if defined(linux) || defined(CSRG_BASED)
442 os << sp.GetEnvVar() << "SEARCHPATH:" << std::endl;
444 os << sp.GetEnvVar() << "SEARCHPATH:" << endl;