2
0

Helpos.c 9.5 KB

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