dtcalc: change from obsoleted MAXFLOAT to FLT_MAX from std C
[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 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 /* $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 #ifdef __ultrix
257       CatFileName = strdup("DtHelp.cat");  
258 #else
259       CatFileName = strdup("DtHelp");
260 #endif
261     }
262   else
263     { 
264 #ifdef __ultrix
265
266       /* If we have a full path, just use it */
267       if (*catFile == '/')
268         CatFileName = strdup(catFile);
269       else
270         {
271           /* We don't have a full path, and the ultirx os needs the 
272            * ".cat" extention so let's make sure its there.
273            */
274           if (strcmp(&catFile[strlen(catFile) -4], ".cat") == 0)
275             CatFileName = strdup(catFile);
276           else
277             {
278               /* Create our CatFileName with the extention */
279               CatFileName = malloc (strlen(catFile) + 5);    
280                                     /* +5, 1 for NULL, 4 for ".cat" */
281               strcpy(CatFileName, catFile);
282               strcat(CatFileName, ".cat");
283             }
284          }
285
286         
287 #else
288
289       /* If we have a full path, just use it */
290       if (*catFile == '/')
291         CatFileName = strdup(catFile);
292       else
293         {
294           /* hp-ux os does not work with the ".cat" extention, so
295            * if one exists, remove it.
296            */
297           
298            if (strcmp(&catFile[strlen(catFile) -4], ".cat") != 0)
299             CatFileName = strdup(catFile);
300           else
301             {
302               /* Create our CatFileName with out the extention */
303               len = strlen(catFile) -4;
304               CatFileName = malloc(len +1);
305               strncpy (CatFileName, catFile, len);
306               CatFileName[len]= '\0';
307             }
308         }
309 #endif
310     }
311   _DtHelpProcessUnlock();
312 }
313
314
315 \f
316 #ifndef NO_MESSAGE_CATALOG
317 /*****************************************************************************
318  * Function:       Boolean _DtHelpGetMessage(
319  *
320  *
321  * Parameters:     
322  *
323  * Return Value:   char *
324  *
325  *
326  * Description:    This function will retreive the requested message from the
327  *                 cache proper cache creek message catalog file.
328  *
329  *****************************************************************************/
330 char *_DtHelpGetMessage(
331         int set,
332         int n,
333         char *s)
334 {
335    char *msg;
336    char *loc;
337    nl_catd catopen();
338    char *catgets();
339    static int first = 1;
340    static nl_catd nlmsg_fd;
341
342    _DtHelpProcessLock();
343    if ( first ) 
344    {
345
346      /* Setup our default message catalog names if none have been set! */
347      if (CatFileName  == NULL)
348        {
349          /* Setup the short and long versions */
350 #ifdef __ultrix
351          CatFileName = strdup("DtHelp.cat"); 
352 #else 
353          CatFileName = strdup("DtHelp");
354 #endif
355        }
356                 
357      loc = _DtHelpGetLocale();
358      if (!loc || !(strcmp (loc, "C"))) 
359         /*
360          * If LANG is not set or if LANG=C, then there
361          * is no need to open the message catalog - just
362          * return the built-in string "s".
363          */
364         nlmsg_fd = (nl_catd) -1;
365      else
366         nlmsg_fd = catopen(CatFileName, NL_CAT_LOCALE);
367
368      first = 0;
369    }
370    
371    msg=catgets(nlmsg_fd,set,n,s);
372    _DtHelpProcessUnlock();
373    return (msg);
374 }
375 #endif
376
377
378
379 /*****************************************************************************
380  * Function:       char * _DtHelpGetLocale(
381  *
382  *
383  * Parameters:     
384  *
385  * Return Value:   char *
386  *
387  *
388  * Description:    Returns the value of LC_MESSAGES from the environ.
389  *                 If that is NULL, returns the value of LANG form the environ.
390  *                 If that is NULL, returns NULL.
391  *
392  *****************************************************************************/
393 char *_DtHelpGetLocale()
394 {
395     char *loc;
396
397     loc = setlocale(LC_MESSAGES, NULL);
398     if (NULL == loc || '\0' == loc[0]) loc = getenv("LANG");
399     if (NULL == loc || '\0' == loc[0]) return NULL;
400
401     loc = strdup(loc);    /* getenv() returns ptr to private memory */
402     return loc;
403 }
404