dtlogin: Resolve some coverity issues, including those thought previously fixed
[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        empty[] = "";
413     char        str_buf[50];
414     int len;
415
416
417     if (DmResourceDB && XrmGetResource (DmResourceDB,
418         name, class,
419         &type, &value))
420     {
421         string = value.addr;
422         len = value.size;
423     }
424     else
425     {
426         if(default_value) {
427                 string = default_value;
428                 len = strlen (string);
429         } else {
430                 string = empty;
431                 len = 0;
432         }
433     }
434
435     Debug ("%s/%s value %*.*s\n", name, class, len, len, string);
436
437
438     /*
439      *  check if new resource is the same as old...
440      */
441      
442      if (valueType == DM_STRING && *valuep) {
443         if (string != NULL && strlen (*valuep) == len && !strncmp (*valuep, string, len))
444             return 0;
445         else {
446             free (*valuep);
447             *(valuep) = NULL;
448         }
449      }
450  
451
452     switch (valueType) {
453     case DM_STRING:
454         if ( len > 0 ) {
455             new_string = malloc ((unsigned) (len+1));
456             if (!new_string) {
457                 LogOutOfMem(
458                   ReadCatalog(MC_LOG_SET,MC_LOG_GET_RSC,MC_DEF_LOG_GET_RSC));
459                 return 0;
460             }
461             strncpy (new_string, string, len);
462             new_string[len] = '\0';
463             *(valuep) = new_string;
464         }
465         break;
466     case DM_INT:
467         strncpy (str_buf, string, sizeof (str_buf));
468         str_buf[sizeof (str_buf)-1] = '\0';
469         *((int *) valuep) = atoi (str_buf);
470         break;
471     case DM_BOOL:
472         strncpy (str_buf, string, sizeof (str_buf));
473         str_buf[sizeof (str_buf)-1] = '\0';
474         XmuCopyISOLatin1Lowered (str_buf, str_buf);
475         if (!strcmp (str_buf, "true") ||
476             !strcmp (str_buf, "on") ||
477             !strcmp (str_buf, "yes"))
478                 *((int *) valuep) = 1;
479         else if (!strcmp (str_buf, "false") ||
480                  !strcmp (str_buf, "off") ||
481                  !strcmp (str_buf, "no"))
482                 *((int *) valuep) = 0;
483         break;
484     case DM_ARGV:
485         *((char ***) valuep) = parseArgs ((char **) 0, string);
486         break;
487
488     }
489
490     return 1;
491 }
492
493 XrmOptionDescRec configTable [] = {
494 {"-server",     NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
495 {"-udpPort",    NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
496 {"-error",      NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
497 {"-resources",  NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
498 {"-session",    NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
499 {"-debug",      NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
500 {"-xrm",        NULL,                   XrmoptionSkipArg,       (caddr_t) NULL },
501 {"-config",     ".configFile",          XrmoptionSepArg,        (caddr_t) NULL }
502 };
503
504 XrmOptionDescRec optionTable [] = {
505 {"-server",     ".servers",             XrmoptionSepArg,        (caddr_t) NULL },
506 {"-udpPort",    ".requestPort",         XrmoptionSepArg,        (caddr_t) NULL },
507 {"-error",      ".errorLogFile",        XrmoptionSepArg,        (caddr_t) NULL },
508 {"-resources",  "*resources",           XrmoptionSepArg,        (caddr_t) NULL },
509 {"-session",    "*session",             XrmoptionSepArg,        (caddr_t) NULL },
510 {"-debug",      "*debugLevel",          XrmoptionSepArg,        (caddr_t) NULL },
511 {"-xrm",        NULL,                   XrmoptionResArg,        (caddr_t) NULL },
512 {"-daemon",     ".daemonMode",          XrmoptionNoArg,         "true"         },
513 {"-nodaemon",   ".daemonMode",          XrmoptionNoArg,         "false"        },
514 {"-quiet",      ".quiet",               XrmoptionNoArg,         "true"        }
515 };
516
517 static int      originalArgc;
518 static char     **originalArgv;
519
520 void 
521 InitResources( int argc, char **argv )
522 {
523         XrmInitialize ();
524         originalArgc = argc;
525         originalArgv = argv;
526         ReinitResources ();
527 }
528
529 void 
530 ReinitResources( void )
531 {
532     int argc;
533     char        **a;
534     char        **argv;
535     XrmDatabase newDB;
536     char * configFileName;
537     char        *type;
538     XrmValue    value;
539
540     argv = (char **) malloc ((originalArgc + 1) * sizeof (char *));
541     if (!argv)
542         LogOutOfMem(
543                 ReadCatalog(MC_LOG_SET,MC_LOG_NO_SPACE,MC_DEF_LOG_NO_SPACE));
544     for (argc = 0; argc < originalArgc; argc++)
545         argv[argc] = originalArgv[argc];
546     argv[argc] = 0;
547     if (DmResourceDB)
548         XrmDestroyDatabase (DmResourceDB);
549     DmResourceDB = XrmGetStringDatabase ("");
550     /* pre-parse the command line to get the -config option, if any */
551     XrmParseCommand (&DmResourceDB, configTable,
552                      sizeof (configTable) / sizeof (configTable[0]),
553                      "Dtlogin", &argc, argv);
554     
555     configFileName = qualifyWithFirst(DEF_XDM_CONFIG,
556                                       CDE_CONFIGURATION_TOP "/config:"
557                                       CDE_INSTALLATION_TOP "/config");
558
559     GetResource ("Dtlogin.configFile", "Dtlogin.ConfigFile",
560                  DM_STRING, &config, configFileName);
561
562     free(configFileName);
563
564
565     
566     newDB = XrmGetFileDatabase ( config );
567     if (newDB)
568     {
569
570         if (DmResourceDB)
571             XrmDestroyDatabase (DmResourceDB);
572         DmResourceDB = newDB;
573     }
574     else if (argc != originalArgc)
575         LogError(
576                 ReadCatalog(MC_LOG_SET,MC_LOG_NO_OPENCFG,MC_DEF_LOG_NO_OPENCFG),
577                 config );
578
579
580
581     /*
582      *  scan the resource database to set the application name...
583      */
584     SetAppName();       
585      
586     XrmParseCommand (&DmResourceDB, optionTable,
587                      sizeof (optionTable) / sizeof (optionTable[0]),
588                      AppName, &argc, argv);
589
590      /* 
591       * test to see if the session variable is set.
592       * for enabling the toggle in the options menu, if it is set..
593       */
594     if((XrmGetResource (DmResourceDB, "Dtlogin*session", "Dtlogin*Session", &type, &value)) == True)
595         session_set = True;
596
597     if (argc > 1)
598     {
599         LogError(ReadCatalog(MC_LOG_SET,MC_LOG_EXTRA_ARG,MC_DEF_LOG_EXTRA_ARG));
600         for (a = argv + 1; *a; a++)
601                 LogError ((unsigned char *)" \"%s\"", *a);
602         LogError ((unsigned char *)"\n");
603     }
604     free (argv);
605 }
606
607 void 
608 LoadDMResources( void )
609 {
610         int     i;
611         char    name[1024], class[1024];
612         FILE    *fp;
613
614         if (servers) {free(servers); servers=NULL;}
615         if (keyFile) {free(keyFile); keyFile=NULL;}
616         if (accessFile) {free(accessFile); accessFile=NULL;}
617
618         for (i = 0; i < NUM_DM_RESOURCES; i++) {
619                 sprintf (name, "%s.%s", AppName, DmResources[i].name);
620                 sprintf (class, "%s.%s", AppName, DmResources[i].class);
621                 GetResource (name, class, DmResources[i].type,
622                               (char **) DmResources[i].dm_value,
623                               DmResources[i].default_value);
624         }
625
626         servers = getFullFileName(servers, 1, NULL);
627         keyFile = getFullFileName(keyFile, 0, NULL);
628         accessFile = getFullFileName(accessFile, 0, NULL);
629
630 #ifndef __apollo        /* set a TZ default for all OS except Domain */
631         if (timeZone == NULL || strlen(timeZone) == 0)
632         {
633          /*
634           *  dynamically determine the timeZone resource default value...
635           */
636           GetSysParms(&timeZone,0,0);
637         }
638 #endif
639
640 }
641
642 void
643 CleanUpName( char *src, char *dst, int len )
644 {
645     while (*src) {
646         if (--len <= 0)
647                 break;
648         switch (*src)
649         {
650         case ':':
651         case '.':
652             *dst++ = '_';
653             break;
654         default:
655             *dst++ = *src;
656         }
657         ++src;
658     }
659     *dst = '\0';
660 }
661
662 void
663 LoadDisplayResources( struct display *d )
664 {
665         int     i;
666         char    name[1024], class[1024];
667         char    dpyName[512], dpyClass[512];
668         char    *lang;
669
670         Debug("Loading display resources for %s\n", d->name);
671         
672         if (d->resources) {free(d->resources); d->resources=NULL;}
673         if (d->setup)     {free(d->setup); d->setup=NULL;}
674         if (d->startup)   {free(d->startup); d->startup=NULL;}
675         if (d->reset)     {free(d->reset); d->reset=NULL;}
676         if (d->session)   {free(d->session); d->session=NULL;}
677         if (d->failsafeClient) {free(d->failsafeClient); d->failsafeClient=NULL;}
678
679         CleanUpName (d->name, dpyName, sizeof (dpyName));
680         CleanUpName (d->class ? d->class : d->name, dpyClass, sizeof (dpyClass));
681
682         if((lang = getenv("LANG")) != NULL)
683            for (i = NUM_DISPLAY_RESOURCES - 1; i >= 0; i--)  {
684                 if(strcmp(DisplayResources[i].name,"language") == 0){
685                   DisplayResources[i].default_value = lang;
686                   break;
687                 }
688            }
689
690
691         for (i = 0; i < NUM_DISPLAY_RESOURCES; i++) {
692                 sprintf (name, "%s.%s.%s", AppName,
693                         dpyName, DisplayResources[i].name);
694                 sprintf (class, "%s.%s.%s", AppName,
695                         dpyClass, DisplayResources[i].class);
696                 GetResource (name, class, DisplayResources[i].type,
697                               (char **) (((char *) d) + DisplayResources[i].offset), DisplayResources[i].default_value);
698         }
699
700 #ifdef _AIX
701     if(d->language == NULL || strlen(d->language) == 0)
702         SetDefaultLanguage(d);
703 #endif /* _AIX */
704
705         d->resources = getFullFileName(d->resources, 2, d->language);
706         d->setup = getFullFileName(d->setup, 0, NULL);
707         d->startup = getFullFileName(d->startup, 0, NULL);
708         d->reset = getFullFileName(d->reset, 0, NULL);
709         d->session = getFullFileName(d->session, 0, NULL);
710         d->failsafeClient = getFullFileName(d->failsafeClient, 0, NULL);
711
712 }
713
714
715         
716 /***************************************************************************
717  *
718  *  SetAppName
719  *
720  *  Probe the resource database to see whether the config file is using
721  *  "Dtlogin" or "DisplayManager" as the application name.
722  *
723  *  If it cannot be determined, "Dtlogin" is used.
724  ***************************************************************************/
725
726 void
727 SetAppName( void )
728 {
729     int         i;
730     char        name[1024], class[1024];
731     char        *type;
732     XrmValue    value;
733
734     for (i = 0; i < NUM_DM_RESOURCES; i++) {
735
736         /*
737          *  try "Dtlogin" (default) ...
738          */
739
740         sprintf (name, "%s.%s", DTLOGIN, DmResources[i].name);
741         sprintf (class, "%s.%s", DTLOGIN, DmResources[i].class);
742
743         if (DmResourceDB && XrmGetResource (DmResourceDB, name, class,
744             &type, &value)) {
745
746             strcpy(AppName, DTLOGIN);
747             break;
748         }
749
750
751         /*
752          *  try "DisplayManager" ...
753          */
754
755         sprintf (name, "%s.%s", DISPLAYMANAGER, DmResources[i].name);
756         sprintf (class, "%s.%s", DISPLAYMANAGER, DmResources[i].class);
757
758         if (DmResourceDB && XrmGetResource (DmResourceDB, name, class,
759             &type, &value)) {
760
761             strcpy(AppName, DISPLAYMANAGER);
762             break;
763         }
764         
765     }
766 }
767
768
769 /***************************************************************************
770  *
771  *  getFullFileName
772  *
773  * Try to produce a fully qualified file name by prepending
774  * /etc/dt/config or /usr/dt/config to a resource file name.
775  * There are 2 special cases:  servers - which can be a command, and
776  *                             resources - which can an imbeded %L.
777  ***************************************************************************/
778 extern char *_ExpandLang( char *string, char *lang );
779
780 static char *
781 getFullFileName(char *name, int special, char *lang)
782 {
783
784 char * newname;
785
786     if (name == NULL)
787         return(NULL);
788
789     /** if the file already starts with a '/' **/
790     /** then just return a copy of it         **/
791     if (name[0] == '/') {
792         if ((newname = (char *)malloc((strlen(name)+1)*sizeof(char))) == NULL) {
793             return (NULL);
794         } else {
795             strcpy(newname, name);
796             return(newname);
797         }
798     }
799
800     switch (special) {
801                  /*********************/
802         case 0:  /** no special case **/
803                  /*********************/
804             newname = qualifyWithFirst(name,
805                                        CDE_CONFIGURATION_TOP "/config:"
806                                        CDE_INSTALLATION_TOP "/config");
807             return (newname);
808
809                  /***************************************/
810         case 1:  /** special handling for servers file **/
811                  /***************************************/
812             if (strchr(name, ':') != NULL) {
813                 /** This is probably a command and not a file name **/
814                 /** so just return a copy of it.                   **/
815                 if ((newname = (char *)malloc((strlen(name)+1)*sizeof(char)))
816                     == NULL) {
817                     return (NULL);
818                 } else {
819                     strcpy(newname, name);
820                     return(newname);
821                 }
822             } else {
823                 newname = qualifyWithFirst(name,
824                                            CDE_CONFIGURATION_TOP "/config:"
825                                            CDE_INSTALLATION_TOP "/config");
826                 return (newname);
827             }
828
829                  /********************************************/
830         case 2:  /** special handling for d->resources file **/
831                  /********************************************/
832             if (strchr(name, '%') == NULL) {
833                 /** no special processing needed **/
834                 newname = qualifyWithFirst(name,
835                                            CDE_CONFIGURATION_TOP "/config:"
836                                            CDE_INSTALLATION_TOP "/config");
837                 return (newname);
838             } else {
839                 char *langString;
840                 char *tempName;
841                 int tempLen;
842
843                 /** need to remember the %L **/
844                 if (lang == NULL)
845                     lang = "C";
846                 langString = _ExpandLang(name, lang);
847                 if ( (tempName = qualifyWithFirst(langString,
848                                            CDE_CONFIGURATION_TOP "/config:"
849                                            CDE_INSTALLATION_TOP "/config") )
850                     == NULL) {
851                     free(langString);
852                     langString = _ExpandLang(name, "C");
853                     if ( (tempName = qualifyWithFirst(langString,
854                                            CDE_CONFIGURATION_TOP "/config:"
855                                            CDE_INSTALLATION_TOP "/config") )
856                         == NULL) {
857                         free(langString);
858                         return(NULL);
859                     }
860                 }
861                 /** We have a fully qualified and expanded file name **/
862                 /** but we need to return a fully qualified but NOT  **/
863                 /** expanded file name.                              **/
864                 tempLen = strlen(tempName) - strlen(langString)
865                              + strlen(name) + 1;
866                 if ((newname = (char *)malloc(tempLen * sizeof(char))) == NULL){
867                     free(langString);
868                     return(tempName);
869                 } else {
870                     tempLen = strlen(tempName) - strlen(langString);
871                     strncpy(newname, tempName, tempLen);
872                     strcpy(newname+tempLen, name);
873                     free (langString);
874                     free (tempName);
875                     return(newname);
876                 }
877             }
878
879         default:
880           /* special is of an unknown value */
881           return(NULL);
882     }
883 }
884