dtprintinfo: Coverity 89561
[oweals/cde.git] / cde / programs / dtsearchpath / dtsp / ManSearchPath.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: ManSearchPath.C /main/3 1995/11/03 12:31:46 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 "TTFile.h"
34 #include "Options.h"
35
36 extern Options * options;
37
38 ManSearchPath::ManSearchPath
39         (
40         CDEEnvironment * user,
41         const char *     envvar,
42         const char *     sep
43         ) : SearchPath(user, envvar, sep)
44 {
45     if (user->DTMANPATH())
46         search_path = user->FactoryManPath() + "," + *user->DTMANPATH();
47     else
48         search_path = user->FactoryManPath();
49
50     // add OS manpath now so duplicate path elements can be removed by Normalize
51     // Normalize should recognize both ':' and ',' as separators.
52
53     if (!user->OS()->MANPATH().isNull())
54         search_path += ":" + user->OS()->MANPATH();
55
56     NormalizePath();
57     TraversePath();
58 }
59
60
61 /************************************************************************
62  *  MakePath()
63  *
64  *     Given a search path element (host name:path name pair), construct
65  *     the appropriate path for this particular desktop subsystem:
66  *
67  *     DTMANPATH
68  *
69  *     A path is constructed so that each host:/path pair is appended.
70  ************************************************************************/
71 void ManSearchPath::MakePath
72         (
73         const CString & pair
74         ) 
75 {
76     CTokenizedString element(pair,":");
77     CString host_element = element.next();
78     CString path_element = element.next();
79
80     if (host_element == user->OS()->LocalHost()) {
81         if (user->OS()->isDirectory(path_element) || options->dontOptimize())
82             AddToPath (path_element);
83     }
84     else {
85         CString dirname(ConstructPath(path_element, &host_element));
86         if (user->OS()->isDirectory(dirname) || options->dontOptimize())
87             AddToPath (dirname);
88     }
89 }
90
91 #if defined(__FreeBSD__)
92 /*****************************************************************
93  *  useSystemPath()
94  *
95  *         Check whether to leave MANPATH unmodified (or unset)
96  *
97  */
98 int ManSearchPath::useSystemPath()
99 {
100      if (user->OS()->getEnvironmentVariable("MANPATH").isNull()) {
101        return 1;
102      } else {
103        return 0;
104      }
105 }
106 #endif
107
108 /*****************************************************************
109  *  ExportPath()
110  *
111  *         Export the variable value to the rest of the session.
112  *
113  *****************************************************************/
114 void ManSearchPath::ExportPath()
115 {
116     if (!useSystemPath()) {
117       CString env(GetEnvVar());
118       user->OS()->shell()->putToEnv(env,
119                        final_search_path.data());
120     }
121 }
122
123
124 void ManSearchPath::Print()
125 {
126     printf("%s:\n", GetEnvVar());
127     CString sp(GetSearchPath());
128     if (!useSystemPath() && !sp.isNull()) {
129         CTokenizedString path (sp,Separator().data());
130         CString subpath = path.next();
131         while (!subpath.isNull()) {
132             printf("\t%s\n",subpath.data());
133             subpath = path.next();
134         }
135         printf("\n");
136     }
137 }
138
139 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
140 std::ostream & operator<< 
141         (
142         std::ostream & os, 
143         const ManSearchPath & sp
144         )
145 #else
146 ostream & operator<< 
147         (
148         ostream & os, 
149         const ManSearchPath & sp
150         )
151 #endif
152 {
153 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
154     os << sp.GetEnvVar() << std::endl;
155 #else
156     os << sp.GetEnvVar() << endl;
157 #endif
158     sp.PrettyPrint(os);
159     return os;
160 }