home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xrm_Defaults.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  8KB  |  263 lines

  1. /* $TOG: GetDflt.c /main/56.0 1998/05/12 11:19:09 kaleb $ */
  2.  
  3. /***********************************************************
  4.  
  5. Copyright (c) 1987, 1988  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. Except as contained in this notice, the name of the X Consortium shall not be
  25. used in advertising or otherwise to promote the sale, use or other dealings
  26. in this Software without prior written authorization from the X Consortium.
  27.  
  28.  
  29. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  30.  
  31.                         All Rights Reserved
  32.  
  33. Permission to use, copy, modify, and distribute this software and its 
  34. documentation for any purpose and without fee is hereby granted, 
  35. provided that the above copyright notice appear in all copies and that
  36. both that copyright notice and this permission notice appear in 
  37. supporting documentation, and that the name of Digital not be
  38. used in advertising or publicity pertaining to distribution of the
  39. software without specific, written prior permission.  
  40.  
  41. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  42. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  43. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  44. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  45. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  46. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  47. SOFTWARE.
  48.  
  49. ******************************************************************/
  50. /* $XFree86: xc/lib/X11/GetDflt.c,v 3.10.2.6 1998/05/20 05:06:16 dawes Exp $ */
  51.  
  52. #include "Xlib_private.h"
  53. #include <X11/Xos.h>
  54. #include <X11/Xresource.h>
  55.  
  56. #ifndef X_NOT_POSIX
  57. #ifdef _POSIX_SOURCE
  58. #include <limits.h>
  59. #else
  60. #define _POSIX_SOURCE
  61. #include <limits.h>
  62. #undef _POSIX_SOURCE
  63. #endif
  64. #endif
  65. #ifndef PATH_MAX
  66. #ifdef WIN32
  67. #define PATH_MAX 512
  68. #else
  69. #include <sys/param.h>
  70. #endif
  71. #ifndef PATH_MAX
  72. #ifdef MAXPATHLEN
  73. #define PATH_MAX MAXPATHLEN
  74. #else
  75. #define PATH_MAX 1024
  76. #endif
  77. #endif
  78. #endif
  79.  
  80. #ifdef XTHREADS
  81. #include <X11/Xthreads.h>
  82. #endif
  83. #ifndef WIN32
  84. #define X_INCLUDE_PWD_H
  85. #define XOS_USE_XLIB_LOCKING
  86. #include <X11/Xos_r.h>
  87. #endif
  88. #include <stdio.h>
  89. #include <ctype.h>
  90.  
  91. #ifdef X_NOT_STDC_ENV
  92. extern char *getenv();
  93. #endif
  94.  
  95. /*ARGSUSED*/
  96. static char *GetHomeDir (dest, len)
  97.     char *dest;
  98.     int len;
  99. {
  100.     DBUG_ENTER("GetHomeDir")
  101. #ifdef WIN32
  102.     register char *ptr;
  103.     char* users = "/users/";
  104.  
  105.     if (len <= 0 || dest == NULL)
  106.     return NULL;
  107.  
  108.     if ((ptr = getenv("HOME"))) {
  109.     (void) strncpy(dest, ptr, len-1);
  110.     dest[len-1] = '\0';
  111.     } else if ((ptr = getenv("USERNAME"))) {
  112.     (void) strcpy (dest, users);
  113.     (void) strncat (dest, ptr, len - strlen (users));
  114.     dest[len-1] = '\0';
  115.     } else
  116.     *dest = '\0';
  117. #else
  118.     _Xgetpwparams pwparams;
  119.     struct passwd *pw;
  120.     register char *ptr;
  121.  
  122.     if (len <= 0 || dest == NULL)
  123.     DBUG_RETURN(NULL);
  124.  
  125.     if ((ptr = getenv("HOME"))) {
  126.     (void) strncpy(dest, ptr, len-1);
  127.     dest[len-1] = '\0';
  128.     } else {
  129.     if ((ptr = getenv("USER")))
  130.         pw = _XGetpwnam(ptr,pwparams);
  131.     else
  132.         pw = _XGetpwuid(getuid(),pwparams);
  133.     if (pw != NULL) {
  134.         (void) strncpy(dest, pw->pw_dir, len-1);
  135.         dest[len-1] = '\0';
  136.     } else
  137.         *dest = '\0';
  138.     }
  139. #endif
  140.     DBUG_RETURN(dest);
  141. }
  142.  
  143.  
  144. static XrmDatabase InitDefaults (dpy)
  145.     Display *dpy;            /* display for defaults.... */
  146. {
  147.     DBUG_ENTER("InitDefaults")
  148.     XrmDatabase userdb;
  149.     XrmDatabase xdb;
  150.     char fname[PATH_MAX];               /* longer than any conceivable size */
  151.     char *xenv;
  152.  
  153.     XrmInitialize();
  154.  
  155.     /*
  156.      * See lib/Xt/Initialize.c
  157.      *
  158.      * First, get the defaults from the server; if none, then load from
  159.      * ~/.Xdefaults.  Next, if there is an XENVIRONMENT environment variable,
  160.      * then load that file.
  161.      */
  162.  
  163.     if (dpy->xdefaults == NULL) {
  164.     char *slashDotXdefaults = "/.Xdefaults";
  165.  
  166.     (void) GetHomeDir (fname, PATH_MAX - strlen (slashDotXdefaults) - 1);
  167.     (void) strcat (fname, slashDotXdefaults);
  168.     xdb = XrmGetFileDatabase (fname);
  169.     } else {
  170.     xdb = XrmGetStringDatabase(dpy->xdefaults);
  171.     }
  172.  
  173.     if (!(xenv = getenv ("XENVIRONMENT"))) {
  174.     char *slashDotXdefaultsDash = "/.Xdefaults-";
  175.     int len;
  176.  
  177.     (void) GetHomeDir (fname, PATH_MAX - strlen (slashDotXdefaultsDash) - 1);
  178.     (void) strcat (fname, slashDotXdefaultsDash);
  179.     len = strlen (fname);
  180.     (void) _XGetHostname (fname+len, PATH_MAX-len);
  181.     xenv = fname;
  182.     }
  183.     userdb = XrmGetFileDatabase (xenv);
  184.     XrmMergeDatabases (userdb, &xdb);
  185.     DBUG_RETURN(xdb);
  186.  
  187. #ifdef old
  188.     if (fname[0] != '\0') userdb =  XrmGetFileDatabase(fname);
  189.     xdb = XrmGetStringDatabase(dpy->xdefaults);
  190.     XrmMergeDatabases(userdb, &xdb);
  191.     return xdb;
  192. #endif
  193. }
  194.  
  195. #if NeedFunctionPrototypes
  196. char *XGetDefault(
  197.     Display *dpy,            /* display for defaults.... */
  198.     char _Xconst *prog,        /* name of program for option    */
  199.     register _Xconst char *name)    /* name of option program wants */
  200. #else
  201. char *XGetDefault(dpy, prog, name)
  202.     Display *dpy;            /* display for defaults.... */
  203.     char *prog;            /* name of program for option    */
  204.     register char *name;        /* name of option program wants */
  205. #endif
  206. {                    /* to get, for example, "font"  */
  207.     DBUG_ENTER("XGetDefault")
  208.     XrmName names[3];
  209.     XrmClass classes[3];
  210.     XrmRepresentation fromType;
  211.     XrmValue result;
  212.     char *progname;
  213. #ifdef WIN32
  214.     char *progname2;
  215. #endif
  216. #ifdef __EMX__
  217.     char *progname2;
  218.     char *dotpos;
  219. #endif
  220.  
  221.     /*
  222.      * strip path off of program name (XXX - this is OS specific)
  223.      */
  224.     progname = strrchr (prog, '/');
  225. #ifdef WIN32
  226.     progname2 = strrchr (prog, '\\');
  227.     if (progname2 && (!progname || progname < progname2))
  228.         progname = progname2;
  229. #endif
  230. #ifdef __EMX__  /* Very similar to WIN32 */
  231.     progname2 = strrchr (prog, '\\');
  232.     if (progname2 && (!progname || progname < progname2))
  233.         progname = progname2;
  234.     dotpos = strrchr (prog, '.');
  235.     if (dotpos && (dotpos>progname2)) *dotpos='\0';
  236. #endif  /* We take out the .exe suffix  */
  237.  
  238.     if (progname)
  239.         progname++;
  240.     else
  241.         progname = (char *)prog;
  242.  
  243.     /*
  244.      * see if database has ever been initialized.  Lookups can be done
  245.      * without locks held.
  246.      */
  247.     LockDisplay(dpy);
  248.     if (dpy->db == NULL) {
  249.         dpy->db = InitDefaults(dpy);
  250.         }
  251.     UnlockDisplay(dpy);
  252.  
  253.     names[0] = XrmStringToName(progname);
  254.     names[1] = XrmStringToName(name);
  255.     names[2] = NULLQUARK;
  256.     classes[0] = XrmStringToClass("Program");
  257.     classes[1] = XrmStringToClass("Name");
  258.     classes[2] = NULLQUARK;
  259.     (void)XrmQGetResource(dpy->db, names, classes, &fromType, &result);
  260.     DBUG_RETURN(result.addr);
  261. }
  262.  
  263.