Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtsearchpath / dtsp / AppSearchPath.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: AppSearchPath.C /main/2 1995/07/17 14:08:52 drk $ */
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 "Options.h"
35 #include "TTFile.h"
36
37 extern Options * options;
38
39 /**********************************************************************
40  *
41  * AppSearchPath - this constructor creates DTAPPSEARCHPATH in a three-
42  *                 step process.
43  *
44  *                      1. gathers environment variables and defaults
45  *                         to create a comma-separated list of paths
46  *                      2. normalizes the list into host:/path format
47  *                      3. builds the final version of the path
48  *
49  *                 Hierarchy of search paths:
50  *                      DTSPUSERAPPHOSTS
51  *                      User's home directory
52  *                      System Administrator's configuration directory
53  *                      DTSPSYSAPPHOSTS
54  *                      Factory location
55  *
56  **********************************************************************/
57 AppSearchPath::AppSearchPath
58         (
59         CDEEnvironment * user,
60         const char *     envvar,
61         const char *     sep
62         ) : SearchPath(user, envvar, sep)
63 {
64     if (user->DTAPPSP()) {
65         if (user->DTUSERAPPSP()) {
66             search_path = *user->DTUSERAPPSP() + "," +
67                           user->HOME() + ",";
68             if (!user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
69                 search_path += user->SysAdmConfig() + ",";
70             search_path += *user->DTAPPSP() + "," +
71                            user->FactoryInstall();
72         }
73         else {
74             search_path = user->HOME() + ",";
75             if (!user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
76                 search_path += user->SysAdmConfig() + ",";
77             search_path += *user->DTAPPSP() + "," +
78                            user->FactoryInstall();
79         }
80     }
81
82     else if (user->DTUSERAPPSP())
83         search_path = *user->DTUSERAPPSP() + "," + user->DefaultSearchPath();
84
85     else
86         search_path = user->DefaultSearchPath();
87
88     if (options->CheckingUser())
89         search_path = user->DefaultSearchPath();
90
91     // Now convert the initial list to host:/path format
92
93     NormalizePath();
94
95     // Convert the host:/path list to a colon-separated list of
96     // valid paths
97
98     TraversePath();
99
100 }
101
102 /************************************************************************
103  *  MakePath()
104  *
105  *     Given a search path element (host name:path name pair), construct
106  *     the appropriate path for this particular desktop subsystem:
107  *
108  *     DTAPPSEARCHPATH
109  *
110  *     A path is constructed so that each host:/path pair is appended.
111  ************************************************************************/
112 void AppSearchPath::MakePath
113         (
114         const CString & pair
115         ) 
116 {
117     CTokenizedString element(pair,":,");
118     CString host_element = element.next();
119     CString path_element = element.next();
120
121     if (host_element == user->OS()->LocalHost()) {
122         if (user->OS()->isDirectory(path_element) || options->dontOptimize())
123             AddToPath (path_element);
124     }
125     else {
126         CString dir(ConstructPath(path_element, &host_element));
127         if (user->OS()->isDirectory(dir) || options->dontOptimize())
128             AddToPath (dir);
129     }
130 }
131
132
133 void AppSearchPath::FixUp()
134 {
135     if (!final_search_path.isNull()) {
136         CTokenizedString subpath(final_search_path,",");
137         CString dirname = subpath.next();
138         CString result_sp(dirname);
139         while (1) {
140             result_sp += "/appmanager";
141             if (!dirname.contains(user->HOME()))
142                 result_sp += "/%L:" + dirname + "/appmanager/C";
143             dirname = subpath.next();
144             if (dirname.isNull())
145                 break;
146             result_sp += ":" + dirname;
147         }
148         final_search_path = result_sp;
149         setSeparator(":");
150     }
151 }