Helpos.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #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. #ifdef __ultrix
  219. CatFileName = strdup("DtHelp.cat");
  220. #else
  221. CatFileName = strdup("DtHelp");
  222. #endif
  223. }
  224. else
  225. {
  226. #ifdef __ultrix
  227. /* If we have a full path, just use it */
  228. if (*catFile == '/')
  229. CatFileName = strdup(catFile);
  230. else
  231. {
  232. /* We don't have a full path, and the ultirx os needs the
  233. * ".cat" extention so let's make sure its there.
  234. */
  235. if (strcmp(&catFile[strlen(catFile) -4], ".cat") == 0)
  236. CatFileName = strdup(catFile);
  237. else
  238. {
  239. /* Create our CatFileName with the extention */
  240. CatFileName = malloc (strlen(catFile) + 5);
  241. /* +5, 1 for NULL, 4 for ".cat" */
  242. strcpy(CatFileName, catFile);
  243. strcat(CatFileName, ".cat");
  244. }
  245. }
  246. #else
  247. /* If we have a full path, just use it */
  248. if (*catFile == '/')
  249. CatFileName = strdup(catFile);
  250. else
  251. {
  252. /* hp-ux os does not work with the ".cat" extention, so
  253. * if one exists, remove it.
  254. */
  255. if (strcmp(&catFile[strlen(catFile) -4], ".cat") != 0)
  256. CatFileName = strdup(catFile);
  257. else
  258. {
  259. /* Create our CatFileName with out the extention */
  260. len = strlen(catFile) -4;
  261. CatFileName = malloc(len +1);
  262. strncpy (CatFileName, catFile, len);
  263. CatFileName[len]= '\0';
  264. }
  265. }
  266. #endif
  267. }
  268. _DtHelpProcessUnlock();
  269. }
  270. #ifndef NO_MESSAGE_CATALOG
  271. /*****************************************************************************
  272. * Function: Boolean _DtHelpGetMessage(
  273. *
  274. *
  275. * Parameters:
  276. *
  277. * Return Value: char *
  278. *
  279. *
  280. * Description: This function will retreive the requested message from the
  281. * cache proper cache creek message catalog file.
  282. *
  283. *****************************************************************************/
  284. char *_DtHelpGetMessage(
  285. int set,
  286. int n,
  287. char *s)
  288. {
  289. char *msg;
  290. char *loc;
  291. nl_catd catopen();
  292. char *catgets();
  293. static int first = 1;
  294. static nl_catd nlmsg_fd;
  295. _DtHelpProcessLock();
  296. if ( first )
  297. {
  298. /* Setup our default message catalog names if none have been set! */
  299. if (CatFileName == NULL)
  300. {
  301. /* Setup the short and long versions */
  302. #ifdef __ultrix
  303. CatFileName = strdup("DtHelp.cat");
  304. #else
  305. CatFileName = strdup("DtHelp");
  306. #endif
  307. }
  308. loc = _DtHelpGetLocale();
  309. if (!loc || !(strcmp (loc, "C")))
  310. /*
  311. * If LANG is not set or if LANG=C, then there
  312. * is no need to open the message catalog - just
  313. * return the built-in string "s".
  314. */
  315. nlmsg_fd = (nl_catd) -1;
  316. else
  317. nlmsg_fd = catopen(CatFileName, NL_CAT_LOCALE);
  318. first = 0;
  319. }
  320. msg=catgets(nlmsg_fd,set,n,s);
  321. _DtHelpProcessUnlock();
  322. return (msg);
  323. }
  324. #endif
  325. /*****************************************************************************
  326. * Function: char * _DtHelpGetLocale(
  327. *
  328. *
  329. * Parameters:
  330. *
  331. * Return Value: char *
  332. *
  333. *
  334. * Description: Returns the value of LC_MESSAGES from the environ.
  335. * If that is NULL, returns the value of LANG form the environ.
  336. * If that is NULL, returns NULL.
  337. *
  338. *****************************************************************************/
  339. char *_DtHelpGetLocale()
  340. {
  341. char *loc;
  342. loc = setlocale(LC_MESSAGES, NULL);
  343. if (NULL == loc || '\0' == loc[0]) loc = getenv("LANG");
  344. if (NULL == loc || '\0' == loc[0]) return NULL;
  345. loc = strdup(loc); /* getenv() returns ptr to private memory */
  346. return loc;
  347. }