Merge branch 'cde-fixups-1' of ssh://git.code.sf.net/p/cdesktopenv/code into cde...
[oweals/cde.git] / cde / programs / dtlogin / resource.c
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: resource.c /main/7 1997/03/14 13:45:09 barstow $ */
24 /* (c) Copyright 1997 The Open Group */
25 /*                                                                      *
26  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
27  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
28  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
29  * (c) Copyright 1993, 1994 Novell, Inc.                                *
30  */
31 /*
32  * xdm - display manager daemon
33  *
34  * Copyright 1988 Massachusetts Institute of Technology
35  *
36  * Permission to use, copy, modify, and distribute this software and its
37  * documentation for any purpose and without fee is hereby granted, provided
38  * that the above copyright notice appear in all copies and that both that
39  * copyright notice and this permission notice appear in supporting
40  * documentation, and that the name of M.I.T. not be used in advertising or
41  * publicity pertaining to distribution of the software without specific,
42  * written prior permission.  M.I.T. makes no representations about the
43  * suitability of this software for any purpose.  It is provided "as is"
44  * without express or implied warranty.
45  *
46  * Author:  Keith Packard, MIT X Consortium
47  */
48
49 /*
50  * resource.c
51  */
52
53 # include "dm.h"
54 # include "vgmsg.h"
55 # include <X11/Xresource.h>
56 # include <X11/Xmu/CharSet.h>
57 # include <string.h>
58
59
60   void CleanUpName( char *src, char *dst, int len) ;
61   static char * getFullFileName(char *name, int special, char * lang);
62   extern char * qualifyWithFirst(char *, char *);
63
64 /* XtOffset() hack for ibmrt BandAidCompiler */
65
66 int session_set = False;
67 char    *config;
68
69 char    *servers;
70 int     request_port;
71 int     debugLevel;
72 char    *errorLogFile;
73 int     errorLogSize;
74 int     daemonMode;
75 int     quiet;
76 char    *pidFile;
77 int     lockPidFile;
78 char    *authDir;
79 int     autoRescan;
80 int     removeDomainname;
81 char    *keyFile;
82 char    *accessFile;
83 char    **exportList;    /* List of all export env vars */
84 char    *sysParmsFile;
85 char    *timeZone;
86 char    *fpHead = NULL;
87 char    *fpTail = NULL;
88 int     wakeupInterval;
89 int     langListTimeout;
90 #ifdef DEF_NETWORK_DEV
91 char    *networkDev;
92 #endif
93 #if defined(__osf__)
94 char    *ignoreLocales;
95 #endif
96
97 /*
98  * Dtlogin will accept both Dtlogin and XDM resources. The string variable
99  * "AppName" contains the proper application name to use in looking up
100  * resources.
101  */
102
103 # define DISPLAYMANAGER     "DisplayManager"
104 # define DTLOGIN            "Dtlogin"
105
106 static  char    AppName[16] = DTLOGIN;
107
108
109 # define DM_STRING      0
110 # define DM_INT         1
111 # define DM_BOOL        2
112 # define DM_ARGV        3
113
114 #ifdef BLS
115 #  define DEF_XDM_CONFIG CDE_INSTALLATION_TOP "/lib/X11/Dtlogin/Xconfig"
116 #  define DEF_AUTH_DIR   CDE_INSTALLATION_TOP "/lib/X11/Dtlogin"
117 #  define DEF_KEY_FILE   CDE_INSTALLATION_TOP "/lib/X11/Dtlogin/Xkeys"
118 #endif
119
120
121 /*
122  * the following constants are supposed to be set in the makefile from
123  * parameters set util/imake.includes/site.def (or *.macros in that directory
124  * if it is server-specific).  DO NOT CHANGE THESE DEFINITIONS!
125  */
126 #ifndef DEF_SERVER_LINE 
127 #  ifdef sun 
128 #define DEF_SERVER_LINE ":0 local /usr/openwin/bin/X :0"
129 #  else
130 #define DEF_SERVER_LINE ":0 local /usr/bin/X11/X :0"
131 #  endif /* sun */
132 #endif
133 #ifndef XRDB_PROGRAM
134 #  ifdef sun 
135 #define XRDB_PROGRAM "/usr/openwin/bin/xrdb"
136 #  else
137 #define XRDB_PROGRAM "/usr/bin/X11/xrdb"
138 #  endif /* sun */
139 #endif
140 #ifndef DEF_SESSION
141 #define DEF_SESSION CDE_INSTALLATION_TOP "/bin/Xsession"
142 #endif
143
144 #ifndef DEF_USER_PATH
145 #  ifdef sun 
146 #    define DEF_USER_PATH "/usr/openwin/bin:/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin:."
147 #  else
148 #    define DEF_USER_PATH "/usr/bin/X11:/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin"
149 #  endif /* sun */
150 #endif
151
152 #ifndef DEF_SYSTEM_PATH
153 #  ifdef sun 
154 #    define DEF_SYSTEM_PATH "/usr/openwin/bin:/etc:/bin:/usr/bin"
155 #  else
156 #    define DEF_SYSTEM_PATH "/usr/bin/X11:/etc:/bin:/usr/bin"
157 #  endif /* sun */
158 #endif
159
160 #ifndef DEF_SYSTEM_SHELL
161 #define DEF_SYSTEM_SHELL "/bin/sh"
162 #endif
163
164 #ifndef DEF_FAILSAFE_CLIENT
165 #  ifdef sun 
166 #define DEF_FAILSAFE_CLIENT "/usr/openwin/bin/xterm"
167 #  else
168 #define DEF_FAILSAFE_CLIENT "/usr/bin/X11/xterm"
169 #  endif /* sun */
170 #endif
171
172 #ifndef DEF_XDM_CONFIG
173 #define DEF_XDM_CONFIG "Xconfig"
174 #endif
175
176 #ifndef DEF_CHOOSER
177 #define DEF_CHOOSER CDE_INSTALLATION_TOP "/bin/chooser"
178 #endif
179
180 #ifndef CPP_PROGRAM
181 #define CPP_PROGRAM "/lib/cpp"
182 #endif
183 #ifndef DEF_XDM_AUTH_GEN
184 #define DEF_XDM_AUTH_GEN CDE_CONFIGURATION_TOP "/xdmauthgen"
185 #endif
186 #ifndef DEF_AUTH_NAME
187 #define DEF_AUTH_NAME   "MIT-MAGIC-COOKIE-1"
188 #endif
189 #ifndef DEF_AUTH_DIR
190 #define DEF_AUTH_DIR CDE_CONFIGURATION_TOP
191 #endif
192 #ifndef DEF_USER_AUTH_DIR
193 #define DEF_USER_AUTH_DIR       "/tmp"
194 #endif
195 #ifndef DEF_KEY_FILE
196 #define DEF_KEY_FILE    CDE_CONFIGURATION_TOP "/Xkeys"
197 #endif
198 #ifndef DEF_ACCESS_FILE
199 #define DEF_ACCESS_FILE ""
200 #endif
201 #ifndef DEF_TIMEZONE
202 #define DEF_TIMEZONE    "MST7MDT"
203 #endif
204 #ifndef DEF_SYS_PARMS_FILE
205 #  if defined( __hpux )
206 #    define DEF_SYS_PARMS_FILE  "/etc/src.sh"
207 #  else
208 #    define DEF_SYS_PARMS_FILE  "/etc/TIMEZONE"
209 #  endif
210 #endif
211 #define DEF_UDP_PORT    "177"       /* registered XDMCP port, don't change */
212
213 #ifndef DEF_ENV
214 #  ifdef sun    /* need to set the environment for Sun OpenWindows */
215 #    define DEF_ENV     "OPENWINHOME=/usr/openwin"
216 #  else
217 #    define DEF_ENV     ""
218 #  endif
219 #endif
220
221 #ifndef DEF_LANG        /* LANG default settings for various architectures */
222 #  ifdef __apollo
223 #    define DEF_LANG    "C"
224 #  endif
225 #  ifdef __osf__
226 #    define DEF_LANG    "C"
227 #  endif
228 #  ifdef __hp_osf
229 #    define DEF_LANG    "en_US.88591"
230 #  endif
231 #  ifdef sun    /* default language under Solaris */
232 #    define DEF_LANG    "C" 
233 #  endif
234 #endif
235
236 #ifndef DEF_LANG        /* set LANG to NULL if not already defined         */
237 #define DEF_LANG        ""
238 #endif
239
240 struct dmResources {
241         char    *name, *class;
242         int     type;
243         char    **dm_value;
244         char    *default_value;
245 } DmResources[] = {
246 "servers",      "Servers",      DM_STRING,      &servers,
247                                 DEF_SERVER_LINE,
248 "requestPort",  "RequestPort",  DM_INT,         (char **) &request_port,
249                                 DEF_UDP_PORT,
250 "debugLevel",   "DebugLevel",   DM_INT,         (char **) &debugLevel,
251                                 "0",
252 "errorLogFile", "ErrorLogFile", DM_STRING,      &errorLogFile,
253                                 "",
254 "errorLogSize", "ErrorLogSize", DM_INT,         (char **) &errorLogSize,
255                                 "50",
256 "daemonMode",   "DaemonMode",   DM_BOOL,        (char **) &daemonMode,
257                                 "false",
258 "quiet",        "quiet",        DM_BOOL,        (char **) &quiet,
259                                 "false",
260 "pidFile",      "PidFile",      DM_STRING,      &pidFile,
261                                 "",
262 "lockPidFile",  "LockPidFile",  DM_BOOL,        (char **) &lockPidFile,
263                                 "true",
264 "authDir",      "AuthDir",      DM_STRING,      &authDir,
265                                 DEF_AUTH_DIR,
266 "autoRescan",   "AutoRescan",   DM_BOOL,        (char **) &autoRescan,
267                                 "true",
268 "removeDomainname","RemoveDomainname",DM_BOOL,  (char **) &removeDomainname,
269                                 "true",
270 "keyFile",      "KeyFile",      DM_STRING,      &keyFile,
271                                 DEF_KEY_FILE,
272 "accessFile",   "AccessFile",   DM_STRING,      &accessFile,
273                                 DEF_ACCESS_FILE,
274 /* exportList env var resource    RK   08.17.93  */
275 "exportList",   "ExportList",   DM_ARGV,        (char **) &exportList,
276                                 "",
277 "timeZone",     "TimeZone",     DM_STRING,      &timeZone,
278                                 "",
279 "fontPathHead", "FontPathHead", DM_STRING,      &fpHead,
280                                 "",
281 "fontPathTail", "FontPathTail", DM_STRING,      &fpTail,
282                                 "",
283 "sysParmsFile", "SysParmsFile", DM_STRING,      &sysParmsFile,
284                                 DEF_SYS_PARMS_FILE,
285 "wakeupInterval","WakeupInterval",DM_INT,       (char **) &wakeupInterval,
286                                 "10",
287 "langListTimeout","langListTimeout",DM_INT,     (char **) &langListTimeout,
288                                 "30",
289 #ifdef DEF_NETWORK_DEV
290 "networkDevice","NetworkDevice",DM_STRING,      &networkDev,
291                                 DEF_NETWORK_DEV,
292 #endif
293 #if defined(__osf__)
294 "ignoreLocales", "IgnoreLocales", DM_STRING,    &ignoreLocales,
295                                 "",
296 #endif
297 };
298
299 # define NUM_DM_RESOURCES       (sizeof DmResources / sizeof DmResources[0])
300
301 # define boffset(f)     ((char *) &(((struct display *) 0)->f) - (char *) 0)
302
303 struct displayResources {
304         char    *name, *class;
305         int     type;
306         int     offset;
307         char    *default_value;
308 } DisplayResources[] = {
309
310
311 /* 
312  * resources for managing the server...
313  */
314  
315 "serverAttempts","ServerAttempts", DM_INT,      boffset(serverAttempts),
316                                 "1",
317 "openDelay",    "OpenDelay",    DM_INT,         boffset(openDelay),
318                                 "5",
319 "openRepeat",   "OpenRepeat",   DM_INT,         boffset(openRepeat),
320                                 "5",
321 "openTimeout",  "OpenTimeout",  DM_INT,         boffset(openTimeout),
322                                 "30",
323 "startAttempts","StartAttempts",DM_INT,         boffset(startAttempts),
324                                 "4",
325 "pingInterval", "PingInterval", DM_INT,         boffset(pingInterval),
326                                 "5",
327 "pingTimeout",  "PingTimeout",  DM_INT,         boffset(pingTimeout),
328                                 "5",
329 "terminateServer","TerminateServer",DM_BOOL,    boffset(terminateServer),
330                                 "false",
331 "grabServer",   "GrabServer",   DM_BOOL,        boffset(grabServer),
332                                 "true",
333 "grabTimeout",  "GrabTimeout",  DM_INT,         boffset(grabTimeout),
334                                 "3",
335 "resetSignal",  "Signal",       DM_INT,         boffset(resetSignal),
336                                 "1",    /* SIGHUP */
337 "termSignal",   "Signal",       DM_INT,         boffset(termSignal),
338                                 "15",   /* SIGTERM */
339 "resetForAuth", "ResetForAuth", DM_BOOL,        boffset(resetForAuth),
340                                 "false",
341 "authorize",    "Authorize",    DM_BOOL,        boffset(authorize),
342                                 "true",
343 "authName",     "AuthName",     DM_ARGV,        boffset(authNames),
344                                 DEF_AUTH_NAME,
345 "authFile",     "AuthFile",     DM_STRING,      boffset(authFile),
346                                 "",
347 #if 0
348 "gettyLine",    "GettyLine",    DM_STRING,      boffset(gettyLine),
349                                 "",
350 "gettySpeed",   "GettySpeed",   DM_STRING,      boffset(gettySpeed),
351                                 "9600",
352 #endif
353 /*
354  *  resources which control the session behavior...
355  */
356
357 "resources",    "Resources",    DM_STRING,      boffset(resources),
358                                 "",
359 "xrdb",         "Xrdb",         DM_STRING,      boffset(xrdb),
360                                 XRDB_PROGRAM,
361 "cpp",          "Cpp",          DM_STRING,      boffset(cpp),
362                                 CPP_PROGRAM,
363 "setup",        "Setup",        DM_STRING,      boffset(setup),
364                                 "",
365 "startup",      "Startup",      DM_STRING,      boffset(startup),
366                                 "",
367 "reset",        "Reset",        DM_STRING,      boffset(reset),
368                                 "",
369 "session",      "Session",      DM_STRING,      boffset(session),
370                                 DEF_SESSION,
371 "userPath",     "Path",         DM_STRING,      boffset(userPath),
372                                 DEF_USER_PATH,
373 "systemPath",   "Path",         DM_STRING,      boffset(systemPath),
374                                 DEF_SYSTEM_PATH,
375 "systemShell",  "Shell",        DM_STRING,      boffset(systemShell),
376                                 DEF_SYSTEM_SHELL,
377 "failsafeClient","FailsafeClient",      DM_STRING,      boffset(failsafeClient),
378                                 DEF_FAILSAFE_CLIENT,
379 "userAuthDir",  "UserAuthDir",  DM_STRING,      boffset(userAuthDir),
380                                 DEF_AUTH_DIR,
381 "chooser",      "Chooser",      DM_STRING,      boffset(chooser),
382                                 DEF_CHOOSER,
383 "language",     "Language",     DM_STRING,      boffset(language),
384                                 DEF_LANG,
385 "languageList", "LanguageList", DM_STRING,      boffset(langList),
386                                 "",
387 "environment",  "Environment",  DM_STRING,      boffset(environStr),
388                                 DEF_ENV,
389 "dtlite",       "Dtlite",       DM_BOOL,        boffset(dtlite),
390                                 "false",
391 "xdmMode",      "XdmMode",      DM_BOOL,        boffset(xdmMode),
392                                 "false",
393 "authenticationName","AuthenticationName", DM_STRING,   boffset(verifyName),
394                                 "",
395 "pmSearchPath", "PmSearchPath", DM_STRING,      boffset(pmSearchPath),
396                                 DEF_PM_SEARCH_PATH,
397 "bmSearchPath", "bmSearchPath", DM_STRING,      boffset(bmSearchPath), 
398                                 DEF_BM_SEARCH_PATH,
399 };
400
401 # define NUM_DISPLAY_RESOURCES  (sizeof DisplayResources/sizeof DisplayResources[0])
402
403 XrmDatabase     DmResourceDB;
404
405 int 
406 GetResource( char *name, char *class, int valueType, char **valuep,
407              char *default_value )
408 {
409     char        *type;
410     XrmValue    value;
411     char        *string, *new_string;
412     char        str_buf[50];
413     int len;
414
415
416     if (DmResourceDB && XrmGetResource (DmResourceDB,
417         name, class,
418         &type, &value))
419     {
420         string = value.addr;
421         len = value.size;
422     }
423     else
424     {
425         string = default_value;
426         len = (string == NULL ? 0 : strlen (string));
427     }
428
429     Debug ("%s/%s value %*.*s\n", name, class, len, len, string);
430
431
432     /*
433      *  check if new resource is the same as old...
434      */
435      
436      if (valueType == DM_STRING && *valuep) {
437         if (string != NULL && strlen (*valuep) == len && !strncmp (*valuep, string, len))
438             return 0;
439         else {
440             free (*valuep);
441             *(valuep) = NULL;
442         }
443      }
444  
445
446     switch (valueType) {
447     case DM_STRING:
448         if ( len > 0 ) {
449             new_string = malloc ((unsigned) (len+1));
450             if (!new_string) {
451                 LogOutOfMem(
452                   ReadCatalog(MC_LOG_SET,MC_LOG_GET_RSC,MC_DEF_LOG_GET_RSC));
453                 return 0;
454             }
455             strncpy (new_string, string, len);
456             new_string[len] = '\0';
457             *(valuep) = new_string;
458         }
459         break;
460     case DM_INT:
461         strncpy (str_buf, string, sizeof (str_buf));
462         str_buf[sizeof (str_buf)-1] = '\0';
463         *((int *) valuep) = atoi (str_buf);
464         break;
465     case DM_BOOL:
466         strncpy (str_buf, string, sizeof (str_buf));
467         str_buf[sizeof (str_buf)-1] = '\0';
468         XmuCopyISOLatin1Lowered (str_buf, str_buf);
469         if (!strcmp (str_buf, "true") ||
470             !strcmp (str_buf, "on") ||
471             !strcmp (str_buf, "yes"))
472                 *((int *) valuep) = 1;
473         else if (!strcmp (str_buf, "false") ||
474                  !strcmp (str_buf, "off") ||
475                  !strcmp (str_buf, "no"))
476                 *((int *) valuep) = 0;
477         break;
478     case DM_ARGV:
479         *((char ***) valuep) = parseArgs ((char **) 0, string);
480         break;
481
482     }
483
484     return 1;
485 }
486
487 XrmOptionDescRec configTable [] = {
488 {"-server",     NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
489 {"-udpPort",    NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
490 {"-error",      NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
491 {"-resources",  NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
492 {"-session",    NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
493 {"-debug",      NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
494 {"-xrm",        NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
495 {"-config",     ".configFile",          XrmoptionSepArg,        (caddr_t) NULL }
496 };
497
498 XrmOptionDescRec optionTable [] = {
499 {"-server",     ".servers",             XrmoptionSepArg,        (caddr_t) NULL },
500 {"-udpPort",    ".requestPort",         XrmoptionSepArg,        (caddr_t) NULL },
501 {"-error",      ".errorLogFile",        XrmoptionSepArg,        (caddr_t) NULL },
502 {"-resources",  "*resources",           XrmoptionSepArg,        (caddr_t) NULL },
503 {"-session",    "*session",             XrmoptionSepArg,        (caddr_t) NULL },
504 {"-debug",      "*debugLevel",          XrmoptionSepArg,        (caddr_t) NULL },
505 {"-xrm",        NULL,                   XrmoptionResArg,        (caddr_t) NULL },
506 {"-daemon",     ".daemonMode",          XrmoptionNoArg,         "true"         },
507 {"-nodaemon",   ".daemonMode",          XrmoptionNoArg,         "false"        },
508 {"-quiet",      ".quiet",               XrmoptionNoArg,         "true"        }
509 };
510
511 static int      originalArgc;
512 static char     **originalArgv;
513
514 void 
515 InitResources( int argc, char **argv )
516 {
517         XrmInitialize ();
518         originalArgc = argc;
519         originalArgv = argv;
520         ReinitResources ();
521 }
522
523 void 
524 ReinitResources( void )
525 {
526     int argc;
527     char        **a;
528     char        **argv;
529     XrmDatabase newDB;
530     char * configFileName;
531     char        *type;
532     XrmValue    value;
533
534     argv = (char **) malloc ((originalArgc + 1) * sizeof (char *));
535     if (!argv)
536         LogOutOfMem(
537                 ReadCatalog(MC_LOG_SET,MC_LOG_NO_SPACE,MC_DEF_LOG_NO_SPACE));
538     for (argc = 0; argc < originalArgc; argc++)
539         argv[argc] = originalArgv[argc];
540     argv[argc] = 0;
541     if (DmResourceDB)
542         XrmDestroyDatabase (DmResourceDB);
543     DmResourceDB = XrmGetStringDatabase ("");
544     /* pre-parse the command line to get the -config option, if any */
545     XrmParseCommand (&DmResourceDB, configTable,
546                      sizeof (configTable) / sizeof (configTable[0]),
547                      "Dtlogin", &argc, argv);
548     
549     configFileName = qualifyWithFirst(DEF_XDM_CONFIG,
550                                       CDE_CONFIGURATION_TOP "/config:"
551                                       CDE_INSTALLATION_TOP "/config");
552
553     GetResource ("Dtlogin.configFile", "Dtlogin.ConfigFile",
554                  DM_STRING, &config, configFileName);
555
556     free(configFileName);
557
558
559     
560     newDB = XrmGetFileDatabase ( config );
561     if (newDB)
562     {
563
564         if (DmResourceDB)
565             XrmDestroyDatabase (DmResourceDB);
566         DmResourceDB = newDB;
567     }
568     else if (argc != originalArgc)
569         LogError(
570                 ReadCatalog(MC_LOG_SET,MC_LOG_NO_OPENCFG,MC_DEF_LOG_NO_OPENCFG),
571                 config );
572
573
574
575     /*
576      *  scan the resource database to set the application name...
577      */
578     SetAppName();       
579      
580     XrmParseCommand (&DmResourceDB, optionTable,
581                      sizeof (optionTable) / sizeof (optionTable[0]),
582                      AppName, &argc, argv);
583
584      /* 
585       * test to see if the session variable is set.
586       * for enabling the toggle in the options menu, if it is set..
587       */
588     if((XrmGetResource (DmResourceDB, "Dtlogin*session", "Dtlogin*Session", &type, &value)) == True)
589         session_set = True;
590
591     if (argc > 1)
592     {
593         LogError(ReadCatalog(MC_LOG_SET,MC_LOG_EXTRA_ARG,MC_DEF_LOG_EXTRA_ARG));
594         for (a = argv + 1; *a; a++)
595                 LogError ((unsigned char *)" \"%s\"", *a);
596         LogError ((unsigned char *)"\n");
597     }
598     free (argv);
599 }
600
601 void 
602 LoadDMResources( void )
603 {
604         int     i;
605         char    name[1024], class[1024];
606         FILE    *fp;
607
608         if (servers) {free(servers); servers=NULL;}
609         if (keyFile) {free(keyFile); keyFile=NULL;}
610         if (accessFile) {free(accessFile); accessFile=NULL;}
611
612         for (i = 0; i < NUM_DM_RESOURCES; i++) {
613                 sprintf (name, "%s.%s", AppName, DmResources[i].name);
614                 sprintf (class, "%s.%s", AppName, DmResources[i].class);
615                 GetResource (name, class, DmResources[i].type,
616                               (char **) DmResources[i].dm_value,
617                               DmResources[i].default_value);
618         }
619
620         servers = getFullFileName(servers, 1, NULL);
621         keyFile = getFullFileName(keyFile, 0, NULL);
622         accessFile = getFullFileName(accessFile, 0, NULL);
623
624 #ifndef __apollo        /* set a TZ default for all OS except Domain */
625         if (timeZone == NULL || strlen(timeZone) == 0)
626         {
627          /*
628           *  dynamically determine the timeZone resource default value...
629           */
630           GetSysParms(&timeZone,0,0);
631         }
632 #endif
633
634 }
635
636 void
637 CleanUpName( char *src, char *dst, int len )
638 {
639     while (*src) {
640         if (--len <= 0)
641                 break;
642         switch (*src)
643         {
644         case ':':
645         case '.':
646             *dst++ = '_';
647             break;
648         default:
649             *dst++ = *src;
650         }
651         ++src;
652     }
653     *dst = '\0';
654 }
655
656 void
657 LoadDisplayResources( struct display *d )
658 {
659         int     i;
660         char    name[1024], class[1024];
661         char    dpyName[512], dpyClass[512];
662         char    *lang;
663
664         Debug("Loading display resources for %s\n", d->name);
665         
666         if (d->resources) {free(d->resources); d->resources=NULL;}
667         if (d->setup)     {free(d->setup); d->setup=NULL;}
668         if (d->startup)   {free(d->startup); d->startup=NULL;}
669         if (d->reset)     {free(d->reset); d->reset=NULL;}
670         if (d->session)   {free(d->session); d->session=NULL;}
671         if (d->failsafeClient) {free(d->failsafeClient); d->failsafeClient=NULL;}
672
673         CleanUpName (d->name, dpyName, sizeof (dpyName));
674         CleanUpName (d->class ? d->class : d->name, dpyClass, sizeof (dpyClass));
675
676         if((lang = getenv("LANG")) != NULL)
677            for (i = NUM_DISPLAY_RESOURCES - 1; i >= 0; i--)  {
678                 if(strcmp(DisplayResources[i].name,"language") == 0){
679                   DisplayResources[i].default_value = lang;
680                   break;
681                 }
682            }
683
684
685         for (i = 0; i < NUM_DISPLAY_RESOURCES; i++) {
686                 sprintf (name, "%s.%s.%s", AppName,
687                         dpyName, DisplayResources[i].name);
688                 sprintf (class, "%s.%s.%s", AppName,
689                         dpyClass, DisplayResources[i].class);
690                 GetResource (name, class, DisplayResources[i].type,
691                               (char **) (((char *) d) + DisplayResources[i].offset), DisplayResources[i].default_value);
692         }
693
694 #ifdef _AIX
695     if(d->language == NULL || strlen(d->language) == 0)
696         SetDefaultLanguage(d);
697 #endif /* _AIX */
698
699         d->resources = getFullFileName(d->resources, 2, d->language);
700         d->setup = getFullFileName(d->setup, 0, NULL);
701         d->startup = getFullFileName(d->startup, 0, NULL);
702         d->reset = getFullFileName(d->reset, 0, NULL);
703         d->session = getFullFileName(d->session, 0, NULL);
704         d->failsafeClient = getFullFileName(d->failsafeClient, 0, NULL);
705
706 }
707
708
709         
710 /***************************************************************************
711  *
712  *  SetAppName
713  *
714  *  Probe the resource database to see whether the config file is using
715  *  "Dtlogin" or "DisplayManager" as the application name.
716  *
717  *  If it cannot be determined, "Dtlogin" is used.
718  ***************************************************************************/
719
720 void
721 SetAppName( void )
722 {
723     int         i;
724     char        name[1024], class[1024];
725     char        *type;
726     XrmValue    value;
727
728     for (i = 0; i < NUM_DM_RESOURCES; i++) {
729
730         /*
731          *  try "Dtlogin" (default) ...
732          */
733
734         sprintf (name, "%s.%s", DTLOGIN, DmResources[i].name);
735         sprintf (class, "%s.%s", DTLOGIN, DmResources[i].class);
736
737         if (DmResourceDB && XrmGetResource (DmResourceDB, name, class,
738             &type, &value)) {
739
740             strcpy(AppName, DTLOGIN);
741             break;
742         }
743
744
745         /*
746          *  try "DisplayManager" ...
747          */
748
749         sprintf (name, "%s.%s", DISPLAYMANAGER, DmResources[i].name);
750         sprintf (class, "%s.%s", DISPLAYMANAGER, DmResources[i].class);
751
752         if (DmResourceDB && XrmGetResource (DmResourceDB, name, class,
753             &type, &value)) {
754
755             strcpy(AppName, DISPLAYMANAGER);
756             break;
757         }
758         
759     }
760 }
761
762
763 /***************************************************************************
764  *
765  *  getFullFileName
766  *
767  * Try to produce a fully qualified file name by prepending
768  * /etc/dt/config or /usr/dt/config to a resource file name.
769  * There are 2 special cases:  servers - which can be a command, and
770  *                             resources - which can an imbeded %L.
771  ***************************************************************************/
772 extern char *_ExpandLang( char *string, char *lang );
773
774 static char *
775 getFullFileName(char *name, int special, char *lang)
776 {
777
778 char * newname;
779
780     if (name == NULL)
781         return(NULL);
782
783     /** if the file already starts with a '/' **/
784     /** then just return a copy of it         **/
785     if (name[0] == '/') {
786         if ((newname = (char *)malloc((strlen(name)+1)*sizeof(char))) == NULL) {
787             return (NULL);
788         } else {
789             strcpy(newname, name);
790             return(newname);
791         }
792     }
793
794     switch (special) {
795                  /*********************/
796         case 0:  /** no special case **/
797                  /*********************/
798             newname = qualifyWithFirst(name,
799                                        CDE_CONFIGURATION_TOP "/config:"
800                                        CDE_INSTALLATION_TOP "/config");
801             return (newname);
802
803                  /***************************************/
804         case 1:  /** special handling for servers file **/
805                  /***************************************/
806             if (strchr(name, ':') != NULL) {
807                 /** This is probably a command and not a file name **/
808                 /** so just return a copy of it.                   **/
809                 if ((newname = (char *)malloc((strlen(name)+1)*sizeof(char)))
810                     == NULL) {
811                     return (NULL);
812                 } else {
813                     strcpy(newname, name);
814                     return(newname);
815                 }
816             } else {
817                 newname = qualifyWithFirst(name,
818                                            CDE_CONFIGURATION_TOP "/config:"
819                                            CDE_INSTALLATION_TOP "/config");
820                 return (newname);
821             }
822
823                  /********************************************/
824         case 2:  /** special handling for d->resources file **/
825                  /********************************************/
826             if (strchr(name, '%') == NULL) {
827                 /** no special processing needed **/
828                 newname = qualifyWithFirst(name,
829                                            CDE_CONFIGURATION_TOP "/config:"
830                                            CDE_INSTALLATION_TOP "/config");
831                 return (newname);
832             } else {
833                 char *langString;
834                 char *tempName;
835                 int tempLen;
836
837                 /** need to remember the %L **/
838                 if (lang == NULL)
839                     lang = "C";
840                 langString = _ExpandLang(name, lang);
841                 if ( (tempName = qualifyWithFirst(langString,
842                                            CDE_CONFIGURATION_TOP "/config:"
843                                            CDE_INSTALLATION_TOP "/config") )
844                     == NULL) {
845                     free(langString);
846                     langString = _ExpandLang(name, "C");
847                     if ( (tempName = qualifyWithFirst(langString,
848                                            CDE_CONFIGURATION_TOP "/config:"
849                                            CDE_INSTALLATION_TOP "/config") )
850                         == NULL) {
851                         free(langString);
852                         return(NULL);
853                     }
854                 }
855                 /** We have a fully qualified and expanded file name **/
856                 /** but we need to return a fully qualified but NOT  **/
857                 /** expanded file name.                              **/
858                 tempLen = strlen(tempName) - strlen(langString)
859                              + strlen(name) + 1;
860                 if ((newname = (char *)malloc(tempLen * sizeof(char))) == NULL){
861                     free(langString);
862                     return(tempName);
863                 } else {
864                     tempLen = strlen(tempName) - strlen(langString);
865                     strncpy(newname, tempName, tempLen);
866                     strcpy(newname+tempLen, name);
867                     free (langString);
868                     free (tempName);
869                     return(newname);
870                 }
871             }
872
873         default:
874           /* special is of an unknown value */
875           return(NULL);
876     }
877 }
878