Helpos.c 9.5 KB

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