Merge branch 'master' into cde-next
[oweals/cde.git] / cde / lib / DtHelp / Helpos.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 /* $TOG: Helpos.c /main/13 1998/07/30 12:08:32 mgreess $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  **
27  **   File:        Dtos.c
28  **
29  **   Project:     Rivers Project
30  **
31  **   Description: All system/os dependent calls are placed in here. 
32  ** 
33  **
34  ** WARNING:  Do NOT put any Xt or Xm dependencies in this code.
35  **
36  **  (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
37  **
38  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
39  **  (c) Copyright 1993, 1994 International Business Machines Corp.
40  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
41  **  (c) Copyright 1993, 1994 Novell, Inc.
42  ****************************************************************************
43  ************************************<+>*************************************/
44
45
46 #include <sys/param.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #define X_INCLUDE_PWD_H
51 #define XOS_USE_XT_LOCKING
52 #include <X11/Xos_r.h>
53 #include <unistd.h>
54 #ifndef _SUN_OS /* don't need the nl_types.h file */
55 # include <nl_types.h>
56 #endif /* ! _SUN_OS */
57
58 #include <X11/Intrinsic.h>
59 #include <X11/Shell.h>
60 #include <Xm/Xm.h>
61
62 /* Dt Includes */
63 #include <Dt/Help.h>
64 #include <Dt/DtNlUtils.h>
65
66 #include "HelpP.h"
67 #include "HelpI.h"
68 #include "HelposI.h"
69 #include "Lock.h"
70
71 #ifndef NL_CAT_LOCALE
72 static const int NL_CAT_LOCALE = 0;
73 #endif
74
75
76
77 /* Global Message Catalog file names */
78 static char *CatFileName=NULL;
79
80
81 \f
82 /*****************************************************************************
83  * Function:       Boolean _DtHelpOSGetHomeDirName(
84  *
85  *
86  * Parameters:    Output string, size of output string 
87  *
88  * Return Value:    String.
89  *
90  *
91  * Description: 
92  *
93  *****************************************************************************/
94 void _DtHelpOSGetHomeDirName(
95     String outptr,
96     size_t len)
97
98 /* outptr is allocated outside this function, just filled here. */
99 /* this solution leads to less change in the current (mwm) code */
100 {
101    int uid;
102    static char *ptr = NULL;
103    _Xgetpwparams        pwd_buf;
104    struct passwd *      pwd_ret;
105
106     _DtHelpProcessLock();
107     if (ptr == NULL) {
108         if((ptr = (char *)getenv("HOME")) == NULL) {
109             if((ptr = (char *)getenv("USER")) != NULL) 
110                 pwd_ret = _XGetpwnam(ptr, pwd_buf);
111             else {
112                 uid = getuid();
113                 pwd_ret = _XGetpwuid(uid, pwd_buf);
114             }
115             if (pwd_ret != NULL) 
116                 ptr = pwd_ret->pw_dir;
117             else 
118                 ptr = NULL;
119         }
120     }
121
122     if (ptr) {
123         strncpy(outptr, ptr, len);
124         outptr[len-1] = '\0';           /* Make sure it is Null terminated */
125     }
126     else 
127         outptr[0] = '\0' ;
128     _DtHelpProcessUnlock();
129 }
130
131
132
133
134
135 \f
136 /*****************************************************************************
137  * Function:       _DtHelpGetUserSearchPath(
138  *
139  *
140  * Parameters:     
141  *
142  * Return Value:    String, owned by caller.
143  *
144  *
145  * Description: 
146  *                  Gets the user search path for use
147  *                  when searching for a volume.
148  *                  Takes path from the environment, 
149  *                  or uses the default path. 
150  *
151  *****************************************************************************/
152 String _DtHelpGetUserSearchPath(void)
153 {
154   String path;
155   char homedir[MAXPATHLEN];
156   String localPath;
157   extern char * _DtCliSrvGetDtUserSession();  /* in libCliSrv */
158
159    localPath = (char *)getenv (DtUSER_FILE_SEARCH_ENV);
160    if (localPath  == NULL) 
161    {
162        char * session;
163
164        /* Use our default path */
165        _DtHelpOSGetHomeDirName(homedir, sizeof(homedir));
166        session = _DtCliSrvGetDtUserSession();
167
168        path = calloc(1, 3*strlen(session) + 6*strlen(homedir) +
169                         strlen(DtDEFAULT_USER_PATH_FORMAT));
170        sprintf(path, DtDEFAULT_USER_PATH_FORMAT, 
171                      homedir, session, homedir, session, homedir, session,
172                      homedir, homedir, homedir);
173        free(session);
174        /* homedir is a local array */
175
176        /* Return our expanded default path */
177        return(path);
178    }
179    else
180    {
181        /* Make a local copy for us */
182        localPath = strdup(localPath);
183
184        /* Just Use our local path */
185        return (localPath);
186    }
187
188 }
189
190
191 \f
192 /*****************************************************************************
193  * Function:       _DtHelpGetSystemSearchPath(
194  *
195  *
196  * Parameters:     
197  *
198  * Return Value:    String, owned by caller.
199  *
200  *
201  * Description: 
202  *                  Gets the system search path for use
203  *                  when searching for a volume.
204  *                  Takes path from the environment, 
205  *                  or uses the default path. 
206  *
207  *****************************************************************************/
208 String _DtHelpGetSystemSearchPath(void)
209 {
210    char * path;
211
212    /* try to retrieve env var value */
213    path = getenv(DtSYS_FILE_SEARCH_ENV);
214
215    /* if fail, use default */
216    if (NULL == path)
217       path = DtDEFAULT_SYSTEM_PATH;
218
219    return strdup(path);
220 }
221
222
223
224
225 /*****************************************************************************
226  * Function:       void DtHelpSetCatalogName(char *catFile);
227  *
228  *
229  * Parameters:     catFile   Specifies the name of the message catalog file
230  *                           to use.  
231  *
232  * Return Value:   void
233  *
234  *
235  * Description:    This function will set the name of the message catalog file
236  *                 within the Dt help system environment. 
237  *      
238  *                 The new name must be of the format <name>.cat and the file
239  *                 must be installed such that the NLS search path can find it.
240  *           
241  *                 If this function is not called then the default value of 
242  *                 Dt.cat will be used.
243  *
244  *
245  *****************************************************************************/
246 void DtHelpSetCatalogName(
247         char* catFile)
248 {
249   int len;
250
251   _DtHelpProcessLock();
252   /* Setup our Message Catalog file names */
253   if (catFile == NULL)
254     {
255       /* Setup the short and long versions */
256       CatFileName = strdup("DtHelp");
257     }
258   else
259     {
260
261       /* If we have a full path, just use it */
262       if (*catFile == '/')
263         CatFileName = strdup(catFile);
264       else
265         {
266           /* hp-ux os does not work with the ".cat" extention, so
267            * if one exists, remove it.
268            */
269
270            if (strcmp(&catFile[strlen(catFile) -4], ".cat") != 0)
271             CatFileName = strdup(catFile);
272           else
273             {
274               /* Create our CatFileName with out the extention */
275               len = strlen(catFile) -4;
276               CatFileName = malloc(len +1);
277               strncpy (CatFileName, catFile, len);
278               CatFileName[len]= '\0';
279             }
280         }
281     }
282   _DtHelpProcessUnlock();
283 }
284
285
286 \f
287 #ifndef NO_MESSAGE_CATALOG
288 /*****************************************************************************
289  * Function:       Boolean _DtHelpGetMessage(
290  *
291  *
292  * Parameters:     
293  *
294  * Return Value:   char *
295  *
296  *
297  * Description:    This function will retreive the requested message from the
298  *                 cache proper cache creek message catalog file.
299  *
300  *****************************************************************************/
301 char *_DtHelpGetMessage(
302         int set,
303         int n,
304         char *s)
305 {
306    char *msg;
307    char *loc;
308    nl_catd catopen();
309    char *catgets();
310    static int first = 1;
311    static nl_catd nlmsg_fd;
312
313    _DtHelpProcessLock();
314    if ( first )
315    {
316
317      /* Setup our default message catalog names if none have been set! */
318      if (CatFileName  == NULL)
319        {
320          /* Setup the short and long versions */
321          CatFileName = strdup("DtHelp");
322        }
323
324      loc = _DtHelpGetLocale();
325      if (!loc || !(strcmp (loc, "C")))
326         /*
327          * If LANG is not set or if LANG=C, then there
328          * is no need to open the message catalog - just
329          * return the built-in string "s".
330          */
331         nlmsg_fd = (nl_catd) -1;
332      else
333         nlmsg_fd = catopen(CatFileName, NL_CAT_LOCALE);
334
335      first = 0;
336    }
337    
338    msg=catgets(nlmsg_fd,set,n,s);
339    _DtHelpProcessUnlock();
340    return (msg);
341 }
342 #endif
343
344
345
346 /*****************************************************************************
347  * Function:       char * _DtHelpGetLocale(
348  *
349  *
350  * Parameters:     
351  *
352  * Return Value:   char *
353  *
354  *
355  * Description:    Returns the value of LC_MESSAGES from the environ.
356  *                 If that is NULL, returns the value of LANG form the environ.
357  *                 If that is NULL, returns NULL.
358  *
359  *****************************************************************************/
360 char *_DtHelpGetLocale(void)
361 {
362     char *loc;
363
364     loc = setlocale(LC_MESSAGES, NULL);
365     if (NULL == loc || '\0' == loc[0]) loc = getenv("LANG");
366     if (NULL == loc || '\0' == loc[0]) return NULL;
367
368     loc = strdup(loc);    /* getenv() returns ptr to private memory */
369     return loc;
370 }
371