1 /* $XConsortium: SearchPath.C /main/3 1995/11/03 12:32:30 rswiston $ */
2 /*******************************************************************
3 ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
4 ** All rights are reserved. Copying or other reproduction of this
5 ** program except for archival purposes is prohibited without prior
6 ** written consent of Hewlett-Packard Company.
7 ********************************************************************
8 ****************************<+>*************************************/
10 #include "SearchPath.h"
14 extern Options * options;
16 /**********************************************************************
18 * SearchPath() - the constructor simply initializes the member data.
20 **********************************************************************/
21 SearchPath::SearchPath
23 CDEEnvironment * user_,
27 environment_var(envvar),
30 final_search_path(""),
35 /**********************************************************************
37 * ~SearchPath() - the destructor - nothing to do
39 **********************************************************************/
40 SearchPath::~SearchPath()
45 /**********************************************************************
47 * constructPath - this member function takes a host and a path and
48 * either calls the Tooltalk filemapping library or
49 * constructs it in the standard fashion.
51 **********************************************************************/
52 CString SearchPath::ConstructPath
62 if (options->doingTooltalk()) {
65 file = new TTFile(*host,path);
66 if (!file->ttFileOpFailed())
68 if (options->removeAutoMountPoint()) {
69 if (final_path.contains(options->getAutoMountPoint(),
71 final_path.replace(options->getAutoMountPoint(),"");
74 Catch (TTFile::TT_Exception *, file) {
75 final_path = user->OS()->MountPoint() + *host + path;
80 final_path = user->OS()->MountPoint() + *host + path;
88 /*************************************************************************
91 * Given a series of string elements one at a time--either a host
92 * or a path, in any order--tokenize them, and create
93 * a normalized path of the form:
95 * "host:/path,host:/path"
97 * Return this normalized path in $RETURN_STRING.
99 *************************************************************************/
100 void SearchPath::AssembleString
102 const CString & path_element
105 if (parse_state == 1 && norm_search_path.length())
106 if (norm_search_path[norm_search_path.length()-1] != ',')
107 norm_search_path += ",";
109 if (path_element.length() == 0) {
111 if (parse_state == 2) {
112 CString path(save_host + ":" + user->SysAdmConfig());
113 if (!norm_search_path.contains(path, ",", ","))
114 norm_search_path += path;
118 else if (path_element[0] == '/') {
122 if (parse_state == 1) {
123 CString path(user->OS()->LocalHost() + ":" + path_element);
124 if (!norm_search_path.contains(path, ",", ","))
125 norm_search_path += path;
128 else if (parse_state == 2) {
129 CString path(save_host + ":" + path_element);
130 if (!norm_search_path.contains(path, ",", ","))
131 norm_search_path += path;
136 else if (path_element[0] == '>') {
138 // terminator--write out the saved host and default path.
140 CString path(save_host + ":" + user->SysAdmConfig());
141 if (!norm_search_path.contains(path, ",", ","))
142 norm_search_path += path;
150 if (parse_state == 1) {
151 save_host = path_element;
155 else if (parse_state == 2) {
156 CString path(save_host + ":" + user->SysAdmConfig());
157 if (!norm_search_path.contains(path, ",", ","))
158 norm_search_path += path;
159 save_host = path_element;
165 /*****************************************************************
168 * This function takes a complete string and calls the helper
169 * function AssembleString() to build a normalized path.
171 *****************************************************************/
172 void SearchPath::NormalizePath()
176 if (search_path.length()) {
177 CTokenizedString subpath(search_path,":,");
178 CString dirname = subpath.next();
179 while (!subpath.Finished()) {
180 AssembleString (dirname);
181 dirname = subpath.next();
187 if (parse_state == 2)
194 /*********************************************************************
197 * Take the second parameter (if it exists), and add the
198 * first parameter (always assumed) to its end separated by the
199 * specified separator
200 *********************************************************************/
202 void SearchPath::AddToPath
207 if (!final_search_path.isNull()) {
208 if (!final_search_path.contains (path, Separator().data(),
209 Separator().data())) {
210 final_search_path += Separator();
211 final_search_path += path;
215 final_search_path += path;
218 /*********************************************************************
221 * Parse a given search path, using comma (,) and colon (:) as
222 * delimiters. Pass the normalized path to the virtual MakePath.
225 * N.B. We assume that we have a normalized path at this point.
227 *********************************************************************/
228 void SearchPath::TraversePath()
230 if (!norm_search_path.isNull()) {
231 CTokenizedString subpath(norm_search_path,",");
232 CString dirname = subpath.next();
233 while (!dirname.isNull()) {
235 dirname = subpath.next();
241 /*********************************************************************
242 * AddPredefinedPath()
244 * If it is possible that an environment variable can be set
245 * before dtsearchpath is run, check for its existence, and if it
246 * exists, append it to the searchpath.
248 *********************************************************************/
249 void SearchPath::AddPredefinedPath()
251 CString envVar_in(environment_var);
252 envVar_in += "SEARCHPATH";
253 CString envVar_out = user->OS()->getEnvironmentVariable(envVar_in.data());
254 if (!envVar_out.isNull()) {
255 CString specified_path(envVar_out);
256 if (!specified_path.contains(search_path))
257 search_path += Separator() + specified_path;
259 search_path = specified_path;
264 /*********************************************************************
267 * This member function verifies that the path in question is a
268 * standard CDE location, i.e. /etc/dt/appconfig or /usr/dt/appconfig
269 * so that the appropriate massaging can take place.
271 *********************************************************************/
272 int SearchPath::validSearchPath
277 if (st == user->SysAdmConfig())
280 if (st == user->FactoryInstall())
283 // If this is an APP-specified path, it also needs to be massaged.
285 // The elements in the APP paths may be host:/path so use the
286 // colon as the leader.
288 if (user->DTAPPSP() &&
289 user->DTAPPSP()->contains (st, ":", separator.data()))
292 if (user->DTUSERAPPSP() &&
293 user->DTUSERAPPSP()->contains (st, ":", separator.data()))
296 // they may also be there by themselves: "host:,/path" so use the
297 // comma as the leader.
299 if (user->DTAPPSP() &&
300 user->DTAPPSP()->contains (st, ",", separator.data()))
303 if (user->DTUSERAPPSP() &&
304 user->DTUSERAPPSP()->contains (st, ",", separator.data()))
310 /*****************************************************************
313 * Export the variable value to the rest of the session.
315 *****************************************************************/
316 void SearchPath::ExportPath()
318 CString env(environment_var);
321 user->OS()->shell()->putToEnv(env, final_search_path.data());
325 /*****************************************************************
328 * In the absence of iostreams, use printf to output information
330 *****************************************************************/
331 void SearchPath::Print()
333 printf("%sSEARCHPATH:\n", GetEnvVar());
334 CString sp(GetSearchPath());
336 CTokenizedString path (sp,Separator().data());
337 CString subpath = path.next();
338 while (!subpath.isNull()) {
339 printf("\t%s\n",subpath.data());
340 subpath = path.next();
346 /****************************************************************
349 * Print the list such that each entry occupys its own line
351 ****************************************************************/
353 void SearchPath::PrettyPrint
358 CTokenizedString path (GetSearchPath(), Separator().data());
359 CString subpath = path.next();
360 while (!subpath.isNull()) {
361 os << " " << subpath << endl;
362 subpath = path.next();
368 /****************************************************************
371 * Allow SearchPath types to be output using iostreams.
373 ****************************************************************/
378 const SearchPath & sp
381 os << sp.GetEnvVar() << "SEARCHPATH:" << endl;