dtprintinfo: Coverity 89561
[oweals/cde.git] / cde / programs / dtsearchpath / dtsp / IconSearchPath.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 /* $XConsortium: IconSearchPath.C /main/3 1995/11/03 12:31:13 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 <stdlib.h>
34 #include "TTFile.h"
35 #include "Options.h"
36
37 extern Options * options;
38
39 /**********************************************************************
40  *
41  * IconSearchPath
42  *
43  *      This constructor creates DTICONSEARCHPATH 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  *              DTSPUSERICON
53  *              User's home directory
54  *              System Administrator's configuration directory
55  *              DTSPSYSICON
56  *              Factory location
57  *
58  **********************************************************************/
59 IconSearchPath::IconSearchPath
60         (
61         CDEEnvironment * user,
62         const char *     envvar,
63         const char *     ist,
64         const char *     znd,
65         const char *     sep
66         ) : SearchPath(user,envvar,sep),
67             first(ist),
68             second(znd)
69 {
70     if (user->DTICONSP()) {
71         if (user->DTUSERICONSP()) {
72             search_path = *user->DTUSERICONSP() + ",";
73             if (user->DTUSERAPPSP())
74                 search_path += *user->DTUSERAPPSP() + ",";
75             search_path += user->HOME() + ",";
76             if (!user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
77                 !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
78                 search_path += user->SysAdmConfig() + ",";
79             search_path += *user->DTICONSP() + ",";
80             if (user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
81                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
82                 search_path.replace(user->OS()->LocalHost() + ":,","");
83             if (user->DTAPPSP())
84                 search_path += *user->DTAPPSP() + ",";
85             search_path += user->SysAdmConfig() + "," +
86                            user->FactoryInstall();
87         }
88         else {
89             if (user->DTUSERAPPSP())
90                 search_path = *user->DTUSERAPPSP() + ",";
91             search_path += user->HOME() + ",";
92             if (!user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
93                 !user->DTAPPSP()->contains(user->OS()->LocalHost(),",",":"))
94                 search_path += user->SysAdmConfig() + ",";
95             search_path += *user->DTICONSP() + ",";
96             if (user->DTICONSP()->contains(user->OS()->LocalHost(),",",":") &&
97                 user->DTAPPSP()->contains(user->SysAdmConfig(),",",","))
98                 search_path.replace(user->OS()->LocalHost() + ":,","");
99             if (user->DTAPPSP())
100                 search_path += *user->DTAPPSP() + ",";
101             search_path += user->SysAdmConfig() + "," +
102                            user->FactoryInstall();
103         }
104     }
105     else if (user->DTUSERICONSP()) {
106         search_path = *user->DTUSERICONSP() + ",";
107         if (user->DTUSERAPPSP())
108             search_path += *user->DTUSERAPPSP() + ",";
109         search_path += *user->DTAPPSP();
110     }
111     else {
112         if (user->DTUSERAPPSP())
113             search_path = *user->DTUSERAPPSP() + ",";
114         search_path += *user->DTAPPSP();
115     }
116
117     // NormalizePath should remove duplicates
118     AddPredefinedPath();
119
120     if (options->CheckingUser())
121         search_path = *user->DTAPPSP();
122
123     NormalizePath();
124     TraversePath();
125 }
126
127
128 /************************************************************************
129  *  MakeIconSearchPath()
130  *
131  *     Given a search path element (host name:path name pair), construct
132  *     the appropriate path for this particular desktop subsystem:
133  *
134  *     DTICONSEARCHPATH
135  *
136  *     A path is constructed so that each host:/path pair is appended.
137  ************************************************************************/
138 void IconSearchPath::MakePath
139         (
140         const CString & pair
141         ) 
142 {
143     CTokenizedString element(pair,":");
144     CString host_element = element.next();
145     CString path_element = element.next();
146
147     if (path_element.contains(user->HOME())) {
148         if (host_element == user->OS()->LocalHost()) {
149             if (user->OS()->isDirectory(path_element + "/icons")
150                  || options->dontOptimize()) {
151                 AddToPath (path_element + "/icons/%B%M" + first);
152                 AddToPath (path_element + "/icons/%B%M" + second);
153                 AddToPath (path_element + "/icons/%B");
154             }
155         }
156         else {
157             CString dir(ConstructPath(path_element + "/icons", &host_element));
158             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
159                 AddToPath (dir + "/%B%M" + first);
160                 AddToPath (dir + "/%B%M" + second);
161                 AddToPath (dir + "/%B");
162             }
163         }
164     }
165     else {
166         if (host_element == user->OS()->LocalHost()) {
167             CString dirname(path_element);
168             if (validSearchPath(dirname)) {
169                 dirname += "/icons";
170                 if (user->OS()->isDirectory (dirname)
171                      || options->dontOptimize()) {
172                     AddToPath (dirname + "/%L/%B%M" + first);
173                     AddToPath (dirname + "/%L/%B%M" + second);
174                     AddToPath (dirname + "/%L/%B");
175                 }
176                 dirname += "/C";
177             }
178             if (user->OS()->isDirectory (dirname) || options->dontOptimize()) {
179                 AddToPath (dirname + "/%B%M" + first);
180                 AddToPath (dirname + "/%B%M" + second);
181                 AddToPath (dirname + "/%B");
182             }           
183         }
184         else {
185             CString dir = ConstructPath(path_element, &host_element);
186             if (validSearchPath(dir)) {
187                 dir += "/icons";
188                 if (user->OS()->isDirectory (dir) || options->dontOptimize()) {
189                     AddToPath (dir + "/%L/%B%M" + first);
190                     AddToPath (dir + "/%L/%B%M" + second);
191                     AddToPath (dir + "/%L/%B");
192                 }
193                 dir += "/C";
194             }
195             if (user->OS()->isDirectory(dir) || options->dontOptimize()) {
196                 AddToPath (dir + "/%B%M" + first);
197                 AddToPath (dir + "/%B%M" + second);
198                 AddToPath (dir + "/%B");
199             }
200         }
201     }
202 }
203
204 /*********************************************************************
205  *  validSearchPath()
206  *
207  *    This member function verifies that the path in question is a
208  *    standard CDE location, i.e. /etc/dt/appconfig or /usr/dt/appconfig
209  *    so that the appropriate massaging can take place.
210  *
211  *********************************************************************/
212 int IconSearchPath::validSearchPath
213         (
214         const CString & st
215         ) const
216 {
217     if (st == user->SysAdmConfig())     // ...,/etc/dt/appconfig,...
218         return 1;
219
220     if (st == user->FactoryInstall())   // ...,/usr/dt/appconfig,...
221         return 1;
222
223     // ...,/nfs/machine/etc/dt/appconfig,...
224
225     if (st.contains(user->SysAdmConfig(),"",Separator().data()))
226         return 1;
227
228     // ...,/nfs/machine/usr/dt/appconfig,...
229
230     if (st.contains(user->FactoryInstall(),"",Separator().data()))
231         return 1;
232
233     // If this is an APP-specified path, it also needs to be massaged.
234     // The elements in the APP paths were originally specified using 
235     // host:/path, but were converted to /path for Icon searchpaths
236     // so use the comma as the leader and the trailer.
237
238     if (user->DTAPPSP() && 
239         user->DTAPPSP()->contains (st, ",", ","))
240         return 1;
241
242     if (user->DTUSERAPPSP() && 
243         user->DTUSERAPPSP()->contains (st, ",", ","))
244         return 1;
245
246     return 0;
247 }