home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / Initialize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  24.9 KB  |  885 lines

  1. /* $XConsortium: Initialize.c,v 1.203 93/03/15 15:25:28 converse Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /* Make sure all wm properties can make it out of the resource manager */
  28.  
  29. #include "IntrinsicI.h"
  30. #include "StringDefs.h"
  31. #include "CoreP.h"
  32. #include "ShellP.h"
  33. #include <pwd.h>
  34. #include <stdio.h>
  35. #include <X11/Xlocale.h>
  36.  
  37. #if __STDC__
  38. #define Const const
  39. #else
  40. #define Const /**/
  41. #endif
  42.  
  43. #ifndef X_NOT_STDC_ENV
  44. #include <stdlib.h>
  45. #else
  46. extern char *getenv();
  47. #endif
  48.  
  49. extern void _XtConvertInitialize();
  50.  
  51. #if (defined(SUNSHLIB) || defined(AIXSHLIB)) && defined(SHAREDCODE)
  52. /*
  53.  * If used as a shared library, generate code under a different name so that
  54.  * the stub routines in sharedlib.c get loaded into the application binary.
  55.  */
  56. #define XtToolkitInitialize _XtToolkitInitialize
  57. #define XtAppInitialize _XtAppInitialize
  58. #define XtInitialize _XtInitialize
  59. #endif /* (SUNSHLIB || AIXSHLIB) && SHAREDCODE */
  60.  
  61. /*
  62.  * hpux
  63.  * Hand-patched versions of HP-UX prior to version 7.0 can usefully add
  64.  * -DUSE_UNAME in the appropriate config file to get long hostnames.
  65.  */
  66.  
  67. #ifdef USG
  68. #define USE_UNAME
  69. #endif
  70.  
  71. #ifdef USE_UNAME
  72. #include <sys/utsname.h>
  73. #endif
  74.  
  75. /* some unspecified magic number of expected search levels for Xrm */
  76. #define SEARCH_LIST_SIZE 1000
  77.  
  78. /*
  79.  This is a set of default records describing the command line arguments that
  80.  Xlib will parse and set into the resource data base.
  81.  
  82.  This list is applied before the users list to enforce these defaults.  This is
  83.  policy, which the toolkit avoids but I hate differing programs at this level.
  84. */
  85.  
  86. static XrmOptionDescRec Const opTable[] = {
  87. {"+rv",        "*reverseVideo", XrmoptionNoArg,    (XtPointer) "off"},
  88. {"+synchronous","*synchronous",    XrmoptionNoArg,        (XtPointer) "off"},
  89. {"-background",    "*background",    XrmoptionSepArg,    (XtPointer) NULL},
  90. {"-bd",        "*borderColor",    XrmoptionSepArg,    (XtPointer) NULL},
  91. {"-bg",        "*background",    XrmoptionSepArg,    (XtPointer) NULL},
  92. {"-bordercolor","*borderColor",    XrmoptionSepArg,    (XtPointer) NULL},
  93. {"-borderwidth",".borderWidth",    XrmoptionSepArg,    (XtPointer) NULL},
  94. {"-bw",        ".borderWidth",    XrmoptionSepArg,    (XtPointer) NULL},
  95. {"-display",    ".display",     XrmoptionSepArg,    (XtPointer) NULL},
  96. {"-fg",        "*foreground",    XrmoptionSepArg,    (XtPointer) NULL},
  97. {"-fn",        "*font",    XrmoptionSepArg,    (XtPointer) NULL},
  98. {"-font",    "*font",    XrmoptionSepArg,    (XtPointer) NULL},
  99. {"-foreground",    "*foreground",    XrmoptionSepArg,    (XtPointer) NULL},
  100. {"-geometry",    ".geometry",    XrmoptionSepArg,    (XtPointer) NULL},
  101. {"-iconic",    ".iconic",    XrmoptionNoArg,        (XtPointer) "on"},
  102. {"-name",    ".name",    XrmoptionSepArg,    (XtPointer) NULL},
  103. {"-reverse",    "*reverseVideo", XrmoptionNoArg,    (XtPointer) "on"},
  104. {"-rv",        "*reverseVideo", XrmoptionNoArg,    (XtPointer) "on"},
  105. {"-selectionTimeout",
  106.         ".selectionTimeout", XrmoptionSepArg,    (XtPointer) NULL},
  107. {"-synchronous","*synchronous",    XrmoptionNoArg,        (XtPointer) "on"},
  108. {"-title",    ".title",    XrmoptionSepArg,    (XtPointer) NULL},
  109. {"-xnllanguage",".xnlLanguage",    XrmoptionSepArg,    (XtPointer) NULL},
  110. {"-xrm",    NULL,        XrmoptionResArg,    (XtPointer) NULL},
  111. };
  112.  
  113.  
  114. /*
  115.  * _XtGetHostname - emulates gethostname() on non-bsd systems.
  116.  */
  117.  
  118. static int _XtGetHostname (buf, maxlen)
  119.     char *buf;
  120.     int maxlen;
  121. {
  122.     int len;
  123.  
  124. #ifdef USE_UNAME
  125.     struct utsname name;
  126.  
  127.     uname (&name);
  128.     len = strlen (name.nodename);
  129.     if (len >= maxlen) len = maxlen - 1;
  130.     (void) strncpy (buf, name.nodename, len);
  131.     buf[len] = '\0';
  132. #else
  133.     buf[0] = '\0';
  134.     (void) gethostname (buf, maxlen);
  135.     buf [maxlen - 1] = '\0';
  136.     len = strlen(buf);
  137. #endif
  138.     return len;
  139. }
  140.  
  141.  
  142. #ifdef SUNSHLIB
  143. void _XtInherit()
  144. {
  145.     extern void __XtInherit();
  146.     __XtInherit();
  147. }
  148. #define _XtInherit __XtInherit
  149. #endif
  150.  
  151. void _XtInherit()
  152. {
  153.     XtErrorMsg("invalidProcedure","inheritanceProc",XtCXtToolkitError,
  154.             "Unresolved inheritance operation",
  155.               (String *)NULL, (Cardinal *)NULL);
  156. }
  157.  
  158.  
  159. void XtToolkitInitialize()
  160. {
  161.     extern void _XtResourceListInitialize();
  162.  
  163.     /* Resource management initialization */
  164.     XrmInitialize();
  165.     _XtResourceListInitialize();
  166.  
  167.     /* Other intrinsic intialization */
  168.     _XtConvertInitialize();
  169.     _XtEventInitialize();
  170.     _XtTranslateInitialize();
  171. }
  172.  
  173.  
  174. static String XtGetRootDirName(buf)
  175.      String buf;
  176. {
  177. #ifndef X_NOT_POSIX
  178.      uid_t uid;
  179. #else
  180.      int uid;
  181.      extern int getuid();
  182. #ifndef SYSV386
  183.      extern struct passwd *getpwuid(), *getpwnam();
  184. #endif
  185. #endif
  186.      struct passwd *pw;
  187.      static char *ptr = NULL;
  188.  
  189.      if (ptr == NULL) {
  190.     if (!(ptr = getenv("HOME"))) {
  191.         if (ptr = getenv("USER")) pw = getpwnam(ptr);
  192.         else {
  193.         uid = getuid();
  194.          pw = getpwuid(uid);
  195.         }
  196.         if (pw) ptr = pw->pw_dir;
  197.         else {
  198.         ptr = NULL;
  199.         *buf = '\0';
  200.         }
  201.     }
  202.      }
  203.  
  204.      if (ptr)
  205.      (void) strcpy(buf, ptr);
  206.  
  207.      buf += strlen(buf);
  208.      *buf = '/';
  209.      buf++;
  210.      *buf = '\0';
  211.      return buf;
  212. }
  213.  
  214. static void CombineAppUserDefaults(dpy, pdb)
  215.     Display *dpy;
  216.     XrmDatabase *pdb;
  217. {
  218.     char* filename;
  219.     char* path;
  220.     Boolean deallocate = False;
  221.  
  222.     if (!(path = getenv("XUSERFILESEARCHPATH"))) {
  223.     char *old_path;
  224.     char homedir[PATH_MAX];
  225.     XtGetRootDirName(homedir);
  226.     if (!(old_path = getenv("XAPPLRESDIR"))) {
  227.         char *path_default = "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
  228.         if (!(path =
  229.           ALLOCATE_LOCAL(6*strlen(homedir) + strlen(path_default))))
  230.         _XtAllocError(NULL);
  231.         sprintf( path, path_default,
  232.             homedir, homedir, homedir, homedir, homedir, homedir );
  233.     } else {
  234.         char *path_default = "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
  235.         if (!(path =
  236.           ALLOCATE_LOCAL( 6*strlen(old_path) + 2*strlen(homedir)
  237.                  + strlen(path_default))))
  238.         _XtAllocError(NULL);
  239.         sprintf(path, path_default, old_path, old_path, old_path, homedir,
  240.             old_path, old_path, old_path, homedir );
  241.     }
  242.     deallocate = True;
  243.     }
  244.  
  245.     filename = XtResolvePathname(dpy, NULL, NULL, NULL, path, NULL, 0, NULL);
  246.     if (filename) {
  247.     (void)XrmCombineFileDatabase(filename, pdb, False);
  248.     XtFree(filename);
  249.     }
  250.  
  251.     if (deallocate) DEALLOCATE_LOCAL(path);
  252. }
  253.  
  254. static void CombineUserDefaults(dpy, pdb)
  255.     Display *dpy;
  256.     XrmDatabase *pdb;
  257. {
  258.     char *dpy_defaults = XResourceManagerString(dpy);
  259.  
  260.     if (dpy_defaults) {
  261.     XrmCombineDatabase(XrmGetStringDatabase(dpy_defaults), pdb, False);
  262.     } else {
  263.     char filename[PATH_MAX];
  264.     (void) XtGetRootDirName(filename);
  265.     (void) strcat(filename, ".Xdefaults");
  266.     (void)XrmCombineFileDatabase(filename, pdb, False);
  267.     }
  268. }
  269.  
  270. /*ARGSUSED*/
  271. static Bool StoreDBEntry(db, bindings, quarks, type, value, data)
  272.     XrmDatabase        *db;
  273.     XrmBindingList      bindings;
  274.     XrmQuarkList    quarks;
  275.     XrmRepresentation   *type;
  276.     XrmValuePtr        value;
  277.     XPointer        data;
  278. {
  279.     XrmQPutResource((XrmDatabase *)data, bindings, quarks, *type, value);
  280.     return False;
  281. }
  282.  
  283. static XrmDatabase CopyDB(db)
  284.     XrmDatabase db;
  285. {
  286.     XrmDatabase copy = NULL;
  287.     XrmQuark empty = NULLQUARK;
  288.  
  289.     XrmEnumerateDatabase(db, &empty, &empty, XrmEnumAllLevels,
  290.              StoreDBEntry, (XPointer)©);
  291.     return copy;
  292. }
  293.  
  294. /*ARGSUSED*/
  295. static String _XtDefaultLanguageProc(dpy, xnl, closure)
  296.     Display   *dpy;    /* unused */
  297.     String     xnl;
  298.     XtPointer  closure;    /* unused */
  299. {
  300.     if (! setlocale(LC_ALL, xnl))
  301.     XtWarning("locale not supported by C library, locale unchanged");
  302.  
  303.     if (! XSupportsLocale()) {
  304.     XtWarning("locale not supported by Xlib, locale set to C");
  305.     setlocale(LC_ALL, "C");
  306.     }
  307.     if (! XSetLocaleModifiers(""))
  308.     XtWarning("X locale modifiers not supported, using default");
  309.  
  310.     return setlocale(LC_ALL, NULL); /* re-query in case overwritten */
  311. }
  312.  
  313. #if NeedFunctionPrototypes
  314. XtLanguageProc XtSetLanguageProc(
  315.     XtAppContext      app,
  316.     XtLanguageProc    proc,
  317.     XtPointer         closure
  318.     )
  319. #else
  320. XtLanguageProc XtSetLanguageProc(app, proc, closure)
  321.     XtAppContext      app;
  322.     XtLanguageProc    proc;
  323.     XtPointer         closure;
  324. #endif
  325. {
  326.     XtLanguageProc    old;
  327.  
  328.     if (!proc) {
  329.     proc = _XtDefaultLanguageProc;
  330.     closure = NULL;
  331.     }
  332.  
  333.     if (app) {
  334.     /* set langProcRec only for this application context */
  335.         old = app->langProcRec.proc;
  336.         app->langProcRec.proc = proc;
  337.         app->langProcRec.closure = closure;
  338.     } else {    
  339.     /* set langProcRec for all application contexts */
  340.         ProcessContext process = _XtGetProcessContext();
  341.  
  342.         old = process->globalLangProcRec.proc;
  343.     process->globalLangProcRec.proc = proc;
  344.     process->globalLangProcRec.closure = closure;
  345.         app = process->appContextList;
  346.         while (app) {
  347.             app->langProcRec.proc = proc;
  348.             app->langProcRec.closure = closure;
  349.         app = app->next;
  350.         }
  351.     }
  352.     return (old ? old : _XtDefaultLanguageProc);
  353. }
  354.  
  355. XrmDatabase XtScreenDatabase(screen)
  356.     Screen *screen;
  357. {
  358.     Display *dpy;
  359.     int scrno;
  360.     Bool doing_def;
  361.     XrmDatabase db, olddb;
  362.     XtPerDisplay pd;
  363.     Status do_fallback;
  364.     char *scr_resources;
  365.  
  366.     dpy = DisplayOfScreen(screen);
  367.     if (screen == DefaultScreenOfDisplay(dpy)) {
  368.     scrno = DefaultScreen(dpy);
  369.     doing_def = True;
  370.     } else {
  371.     scrno = XScreenNumberOfScreen(screen);
  372.     doing_def = False;
  373.     }
  374.     pd = _XtGetPerDisplay(dpy);
  375.     if (db = pd->per_screen_db[scrno])
  376.     return doing_def ? XrmGetDatabase(dpy) : db;
  377.     scr_resources = XScreenResourceString(screen);
  378.  
  379.     if (ScreenCount(dpy) == 1) {
  380.     db = pd->cmd_db;
  381.     pd->cmd_db = NULL;
  382.     } else {
  383.     db = CopyDB(pd->cmd_db);
  384.     }
  385.     {   /* Environment defaults */
  386.     char    filenamebuf[PATH_MAX];
  387.     char    *filename;
  388.  
  389.     if (!(filename = getenv("XENVIRONMENT"))) {
  390.         int len;
  391.         (void) XtGetRootDirName(filename = filenamebuf);
  392.         (void) strcat(filename, ".Xdefaults-");
  393.         len = strlen(filename);
  394.         (void) _XtGetHostname (filename+len, PATH_MAX-len);
  395.     }
  396.     (void)XrmCombineFileDatabase(filename, &db, False);
  397.     }
  398.     if (scr_resources)
  399.     {   /* Screen defaults */
  400.     XrmCombineDatabase(XrmGetStringDatabase(scr_resources), &db, False);
  401.     XFree(scr_resources);
  402.     }
  403.     /* Server or host defaults */
  404.     if (!pd->server_db)
  405.     CombineUserDefaults(dpy, &db);
  406.     else {
  407.     (void) XrmCombineDatabase(pd->server_db, &db, False);
  408.     pd->server_db = NULL;
  409.     }
  410.  
  411.     if (!db)
  412.     db = XrmGetStringDatabase("");
  413.     pd->per_screen_db[scrno] = db;
  414.     olddb = XrmGetDatabase(dpy);
  415.     /* set database now, for XtResolvePathname to use */
  416.     XrmSetDatabase(dpy, db);
  417.     CombineAppUserDefaults(dpy, &db);
  418.     do_fallback = 1;
  419.     {   /* System app-defaults */
  420.     char    *filename;
  421.  
  422.     if (filename = XtResolvePathname(dpy, "app-defaults",
  423.                      NULL, NULL, NULL, NULL, 0, NULL)) {
  424.         do_fallback = !XrmCombineFileDatabase(filename, &db, False);
  425.         XtFree(filename);
  426.     }
  427.     }
  428.     /* now restore old database, if need be */
  429.     if (!doing_def)
  430.     XrmSetDatabase(dpy, olddb);
  431.     if (do_fallback && pd->appContext->fallback_resources)
  432.     {   /* Fallback defaults */
  433.         XrmDatabase fdb = NULL;
  434.     String *res;
  435.  
  436.     for (res = pd->appContext->fallback_resources; *res; res++)
  437.         XrmPutLineResource(&fdb, *res);
  438.     (void)XrmCombineDatabase(fdb, &db, False);
  439.     }
  440.     return db;
  441. }
  442.  
  443. /*
  444.  * Merge two option tables, allowing the second to over-ride the first,
  445.  * so that ambiguous abbreviations can be noticed.  The merge attempts
  446.  * to make the resulting table lexicographically sorted, but succeeds
  447.  * only if the first source table is sorted.  Though it _is_ recommended
  448.  * (for optimizations later in XrmParseCommand), it is not required
  449.  * that either source table be sorted.
  450.  *
  451.  * Caller is responsible for freeing the returned option table.
  452.  */
  453.  
  454. static void _MergeOptionTables(src1, num_src1, src2, num_src2, dst, num_dst)
  455.     XrmOptionDescRec *src1, *src2;
  456.     Cardinal num_src1, num_src2;
  457.     XrmOptionDescRec **dst;
  458.     Cardinal *num_dst;
  459. {
  460.     XrmOptionDescRec *table, *endP;
  461.     register XrmOptionDescRec *opt1, *opt2, *whereP, *dstP; 
  462.     int i1, i2, dst_len, order;
  463.     Boolean found;
  464.     enum {Check, NotSorted, IsSorted} sort_order = Check;
  465.  
  466.     *dst = table = (XrmOptionDescRec*)
  467.     XtMalloc( sizeof(XrmOptionDescRec) * (num_src1 + num_src2) );
  468.  
  469.     bcopy( src1, table, sizeof(XrmOptionDescRec) * num_src1 );
  470.     if (num_src2 == 0) {
  471.     *num_dst = num_src1;
  472.     return;
  473.     }
  474.     endP = &table[dst_len = num_src1];
  475.     for (opt2 = src2, i2= 0; i2 < num_src2; opt2++, i2++) {
  476.     found = False;
  477.     whereP = endP-1;    /* assume new option goes at the end */
  478.     for (opt1 = table, i1 = 0; i1 < dst_len; opt1++, i1++) {
  479.         /* have to walk the entire new table so new list is ordered
  480.            (if src1 was ordered) */
  481.         if (sort_order == Check && i1 > 0
  482.         && strcmp(opt1->option, (opt1-1)->option) < 0)
  483.         sort_order = NotSorted;
  484.         if ((order = strcmp(opt1->option, opt2->option)) == 0) {
  485.         /* same option names; just overwrite opt1 with opt2 */
  486.         *opt1 = *opt2;
  487.         found = True;
  488.         break;
  489.         }
  490.         /* else */
  491.         if (sort_order == IsSorted && order > 0) {
  492.         /* insert before opt1 to preserve order */
  493.         /* shift rest of table forward to make room for new entry */
  494.         for (dstP = endP++; dstP > opt1; dstP--)
  495.             *dstP = *(dstP-1);
  496.         *opt1 = *opt2;
  497.         dst_len++;
  498.         found = True;
  499.         break;
  500.         }
  501.         /* else */
  502.         if (order < 0)
  503.         /* opt2 sorts after opt1, so remember this position */
  504.         whereP = opt1;
  505.     }
  506.     if (sort_order == Check && i1 == dst_len)
  507.         sort_order = IsSorted;
  508.     if (!found) {
  509.        /* when we get here, whereP points to the last entry in the
  510.           destination that sorts before "opt2".  Shift rest of table
  511.           forward and insert "opt2" after whereP. */
  512.         whereP++;
  513.         for (dstP = endP++; dstP > whereP; dstP--)
  514.         *dstP = *(dstP-1);
  515.         *whereP = *opt2;
  516.         dst_len++;
  517.     }
  518.     }
  519.     *num_dst = dst_len;
  520. }
  521.  
  522.  
  523. /* NOTE: name, class, and type must be permanent strings */
  524. static Boolean _GetResource(dpy, list, name, class, type, value)
  525.     Display *dpy;
  526.     XrmSearchList list;
  527.     String name, class, type;
  528.     XrmValue* value;
  529. {
  530.     XrmRepresentation db_type;
  531.     XrmValue db_value;
  532.     XrmName Qname = XrmPermStringToQuark(name);
  533.     XrmClass Qclass = XrmPermStringToQuark(class);
  534.     XrmRepresentation Qtype = XrmPermStringToQuark(type);
  535.  
  536.     if (XrmQGetSearchResource(list, Qname, Qclass, &db_type, &db_value)) {
  537.     if (db_type == Qtype) {
  538.         if (Qtype == _XtQString)
  539.         *(String*)value->addr = db_value.addr;
  540.         else
  541.         bcopy( db_value.addr, value->addr, value->size );
  542.         return True;
  543.     } else {
  544.         WidgetRec widget; /* hack, hack */
  545.         bzero( &widget, sizeof(widget) );
  546.         widget.core.self = &widget;
  547.         widget.core.widget_class = coreWidgetClass;
  548.         widget.core.screen = (Screen*)DefaultScreenOfDisplay(dpy);
  549.         XtInitializeWidgetClass(coreWidgetClass);
  550.         if (_XtConvert(&widget,db_type,&db_value,Qtype,value,NULL)) {
  551.         return True;
  552.         }
  553.     }
  554.     }
  555.     return False;
  556. }
  557.  
  558. XrmDatabase _XtPreparseCommandLine(urlist, num_urs, argc, argv, applName,
  559.                    displayName, language)
  560.     XrmOptionDescRec *urlist;
  561.     Cardinal num_urs;
  562.     int argc;
  563.     String *argv;
  564.     String *applName, *displayName, *language;    /* return */
  565. {
  566.     XrmDatabase db = 0;
  567.     XrmOptionDescRec *options;
  568.     Cardinal num_options;
  569.     XrmName name_list[3];
  570.     XrmName class_list[3];
  571.     XrmRepresentation type;
  572.     XrmValue val;
  573.     String *targv;
  574.     int targc = argc;
  575.  
  576.     targv = (String *) XtMalloc(sizeof(char *) * argc);
  577.     bcopy(argv, targv, sizeof(char *) * argc);
  578.     _MergeOptionTables(opTable, XtNumber(opTable), urlist, num_urs,
  579.                &options, &num_options);
  580.     name_list[0] = class_list[0] = XrmPermStringToQuark(".");
  581.     name_list[2] = class_list[2] = NULLQUARK;
  582.     XrmParseCommand(&db, options, num_options, ".", &targc, targv);
  583.     if (applName) {
  584.     name_list[1] = XrmPermStringToQuark("name");
  585.     if (XrmQGetResource(db, name_list, name_list, &type, &val) &&
  586.         type == _XtQString)
  587.         *applName = val.addr;
  588.     }
  589.     if (displayName) {
  590.     name_list[1] = XrmPermStringToQuark("display");
  591.     if (XrmQGetResource(db, name_list, name_list, &type, &val) &&
  592.         type == _XtQString)
  593.         *displayName = val.addr;
  594.     }
  595.     if (language) {
  596.     name_list[1] = XrmPermStringToQuark("xnlLanguage");
  597.     class_list[1] = XrmPermStringToQuark("XnlLanguage");
  598.     if (XrmQGetResource(db, name_list, class_list, &type, &val) &&
  599.         type == _XtQString)
  600.         *language = val.addr;
  601.     }
  602.  
  603.     XtFree((char *)targv);
  604.     XtFree((char *)options);
  605.     return db;
  606. }
  607.  
  608.   
  609. static void GetLanguage(dpy, pd)
  610.     Display *dpy;
  611.     XtPerDisplay pd;
  612. {
  613.     XrmRepresentation type;
  614.     XrmValue value;
  615.     XrmName name_list[3];
  616.     XrmName class_list[3];
  617.  
  618.     if (! pd->language) {
  619.     name_list[0] = pd->name;
  620.     name_list[1] = XrmPermStringToQuark("xnlLanguage");
  621.     class_list[0] = pd->class;
  622.     class_list[1] = XrmPermStringToQuark("XnlLanguage");
  623.     name_list[2] = class_list[2] = NULLQUARK;
  624.     if (!pd->server_db)
  625.         CombineUserDefaults(dpy, &pd->server_db);
  626.     if (pd->server_db &&
  627.         XrmQGetResource(pd->server_db,name_list,class_list, &type, &value)
  628.         && type == _XtQString)
  629.         pd->language = (char *) value.addr;
  630.     }
  631.  
  632.     if (pd->appContext->langProcRec.proc) {
  633.     if (! pd->language) pd->language = "";
  634.     pd->language = (*pd->appContext->langProcRec.proc)
  635.         (dpy, pd->language, pd->appContext->langProcRec.closure);
  636.     }
  637.     else if (! pd->language || pd->language[0] == '\0') /* R4 compatibility */
  638.     pd->language = getenv("LANG");
  639.  
  640.     if (pd->language) pd->language = XtNewString(pd->language);
  641. }
  642.  
  643.  
  644. #if NeedFunctionPrototypes
  645. void _XtDisplayInitialize(
  646.     Display *dpy,
  647.         XtPerDisplay pd,
  648.     _Xconst char* name,
  649.     XrmOptionDescRec *urlist,
  650.     Cardinal num_urs,
  651.     int *argc,
  652.     char **argv)
  653. #else
  654. void _XtDisplayInitialize(dpy, pd, name, urlist, num_urs, argc, argv)
  655.     Display *dpy;
  656.         XtPerDisplay pd;
  657.     String name;
  658.     XrmOptionDescRec *urlist;
  659.     Cardinal num_urs;
  660.     int *argc;
  661.     char **argv;
  662. #endif
  663. {
  664.     Boolean tmp_bool;
  665.     XrmValue value;
  666.     XrmOptionDescRec *options;
  667.     Cardinal num_options;
  668.     XrmDatabase db;
  669.     XrmName name_list[2];
  670.     XrmClass class_list[2];
  671.     XrmHashTable* search_list;
  672.     int search_list_size = SEARCH_LIST_SIZE;
  673.  
  674.     GetLanguage(dpy, pd);
  675.  
  676.     /* Parse the command line and remove Xt arguments from argv */
  677.     _MergeOptionTables( opTable, XtNumber(opTable), urlist, num_urs,
  678.                 &options, &num_options );
  679.     XrmParseCommand(&pd->cmd_db, options, num_options, name, argc, argv);
  680.  
  681.     db = XtScreenDatabase(DefaultScreenOfDisplay(dpy));
  682.  
  683.     if (!(search_list = (XrmHashTable*)
  684.                ALLOCATE_LOCAL( SEARCH_LIST_SIZE*sizeof(XrmHashTable))))
  685.         _XtAllocError(NULL);
  686.     name_list[0] = pd->name;
  687.     class_list[0] = pd->class;
  688.     name_list[1] = NULLQUARK;
  689.     class_list[1] = NULLQUARK;
  690.  
  691.     while (!XrmQGetSearchList(db, name_list, class_list,
  692.                   search_list, search_list_size)) {
  693.         XrmHashTable* old = search_list;
  694.         Cardinal size = (search_list_size*=2)*sizeof(XrmHashTable);
  695.         if (!(search_list = (XrmHashTable*)ALLOCATE_LOCAL(size)))
  696.         _XtAllocError(NULL);
  697.         bcopy( (char*)old, (char*)search_list, (size>>1) );
  698.         DEALLOCATE_LOCAL(old);
  699.     }
  700.  
  701.     value.size = sizeof(tmp_bool);
  702.     value.addr = (XtPointer)&tmp_bool;
  703.     if (_GetResource(dpy, search_list, "synchronous", "Synchronous",
  704.              XtRBoolean, &value)) {
  705.         int i;
  706.         Display **dpyP = pd->appContext->list;
  707.         pd->appContext->sync = tmp_bool;
  708.         for (i = pd->appContext->count; i; dpyP++, i--) {
  709.         (void) XSynchronize(*dpyP, (Bool)tmp_bool);
  710.         }
  711.     } else {
  712.         (void) XSynchronize(dpy, (Bool)pd->appContext->sync);
  713.     }
  714.  
  715.     if (_GetResource(dpy, search_list, "reverseVideo", "ReverseVideo",
  716.              XtRBoolean, &value)
  717.             && tmp_bool) {
  718.         pd->rv = True;
  719.     }
  720.  
  721.     value.size = sizeof(pd->multi_click_time);
  722.     value.addr = (XtPointer)&pd->multi_click_time;
  723.     if (!_GetResource(dpy, search_list,
  724.               "multiClickTime", "MultiClickTime",
  725.               XtRInt, &value)) {
  726.         pd->multi_click_time = 200;
  727.     }
  728.  
  729.     value.size = sizeof(pd->appContext->selectionTimeout);
  730.     value.addr = (XtPointer)&pd->appContext->selectionTimeout;
  731.     (void)_GetResource(dpy, search_list,
  732.                "selectionTimeout", "SelectionTimeout",
  733.                XtRInt, &value);
  734.  
  735. #ifndef NO_IDENTIFY_WINDOWS
  736.     value.size = sizeof(pd->appContext->identify_windows);
  737.     value.addr = (XtPointer)&pd->appContext->identify_windows;
  738.     (void)_GetResource(dpy, search_list,
  739.                "xtIdentifyWindows", "XtDebug",
  740.                XtRBoolean, &value);
  741. #endif
  742.  
  743.     XtFree( (XtPointer)options );
  744.     DEALLOCATE_LOCAL( search_list );
  745. }
  746.  
  747. /*    Function Name: XtAppSetFallbackResources
  748.  *    Description: Sets the fallback resource list that will be loaded
  749.  *                   at display initialization time.
  750.  *    Arguments: app_context - the app context.
  751.  *                 specification_list - the resource specification list.
  752.  *    Returns: none.
  753.  */
  754.  
  755. #if NeedFunctionPrototypes
  756. void
  757. XtAppSetFallbackResources(
  758. XtAppContext app_context,
  759. String *specification_list
  760. )
  761. #else
  762. void
  763. XtAppSetFallbackResources(app_context, specification_list)
  764. XtAppContext app_context;
  765. String *specification_list;
  766. #endif
  767. {
  768.     app_context->fallback_resources = specification_list;
  769. }
  770.  
  771. /*    Function Name: XtAppInitialize
  772.  *    Description: A convience routine for Initializing the toolkit.
  773.  *    Arguments: app_context_return - The application context of the
  774.  *                                      application
  775.  *                 application_class  - The class of the application.
  776.  *                 options            - The option list.
  777.  *                 num_options        - The number of options in the above list
  778.  *                 argc_in_out, argv_in_out - number and list of command line
  779.  *                                            arguments.
  780.  *                 fallback_resource  - The fallback list of resources.
  781.  *                 args, num_args     - Arguements to use when creating the 
  782.  *                                      shell widget.
  783.  *    Returns: The shell widget.
  784.  */
  785.     
  786. #if NeedFunctionPrototypes
  787. Widget
  788. XtAppInitialize(
  789. XtAppContext * app_context_return,
  790. _Xconst char* application_class,
  791. XrmOptionDescRec *options,
  792. Cardinal num_options,
  793. int *argc_in_out,
  794. String *argv_in_out,
  795. String *fallback_resources,
  796. ArgList args_in,
  797. Cardinal num_args_in
  798. )
  799. #else
  800. Widget
  801. XtAppInitialize(app_context_return, application_class, options, num_options,
  802.         argc_in_out, argv_in_out, fallback_resources, 
  803.         args_in, num_args_in)
  804. XtAppContext * app_context_return;
  805. String application_class;
  806. XrmOptionDescRec *options;
  807. Cardinal num_options, num_args_in;
  808. int *argc_in_out;
  809. String *argv_in_out, * fallback_resources;     
  810. ArgList args_in;
  811. #endif
  812. {
  813.     XtAppContext app_con;
  814.     Display * dpy;
  815.     register int saved_argc = *argc_in_out;
  816.     Widget root;
  817.     Arg args[3], *merged_args;
  818.     Cardinal num = 0;
  819.     
  820.     XtToolkitInitialize(); /* cannot be moved into _XtAppInit */
  821.     
  822.     dpy = _XtAppInit(&app_con, (String)application_class, options, num_options,
  823.              argc_in_out, &argv_in_out, fallback_resources);
  824.  
  825.     XtSetArg(args[num], XtNscreen, DefaultScreenOfDisplay(dpy)); num++;
  826.     XtSetArg(args[num], XtNargc, saved_argc);                     num++;
  827.     XtSetArg(args[num], XtNargv, argv_in_out);                     num++;
  828.  
  829.     merged_args = XtMergeArgLists(args_in, num_args_in, args, num);
  830.     num += num_args_in;
  831.  
  832.     root = XtAppCreateShell(NULL, application_class, 
  833.                 applicationShellWidgetClass,dpy, merged_args, num);
  834.     
  835.     if (app_context_return)
  836.     *app_context_return = app_con;
  837.  
  838.     XtFree((XtPointer)merged_args);
  839.     XtFree((XtPointer)argv_in_out);
  840.     return(root);
  841. }
  842.  
  843. /*    Function Name: XtInitialize
  844.  *    Description: This function can be used to initialize the toolkit.
  845.  *             It is obsolete; XtAppInitialize is more useful.
  846.  *    Arguments: name - ** UNUSED **
  847.  *                 classname - name of the application class.
  848.  *                 options, num_options - the command line option info.
  849.  *                 argc, argc - the command line args from main().
  850.  *    Returns: a shell widget.
  851.  */
  852.     
  853. /*ARGSUSED*/
  854. #if NeedFunctionPrototypes
  855. Widget 
  856. XtInitialize(
  857. _Xconst char* name,
  858. _Xconst char* classname,
  859. XrmOptionDescRec *options,
  860. Cardinal num_options,
  861. int *argc,
  862. String *argv
  863. )
  864. #else
  865. Widget 
  866. XtInitialize(name, classname, options, num_options, argc, argv)
  867. String name, classname;
  868. XrmOptionDescRec *options;
  869. Cardinal num_options;
  870. String *argv;
  871. int *argc;
  872. #endif
  873. {
  874.     Widget root;
  875.     XtAppContext app_con;
  876.     register ProcessContext process = _XtGetProcessContext();
  877.  
  878.     root = XtAppInitialize(&app_con, classname, options, num_options,
  879.                argc, argv, NULL, NULL, (Cardinal) 0);
  880.  
  881.     process->defaultAppContext = app_con;
  882.     
  883.     return(root);
  884. }
  885.