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