This is a fix for mwmparse.y so that it behaves more correctly
as described in "OSF/Motif Programmer's Reference."

As the manual explicitly states that if Mwm*configFile resource
is defined to be, say, ".mwmrc", mwm looks for the current directly,
there is a patch for Mwm.ad also.

--- mwmparse.y-0.93.14  Fri Aug  3 04:45:28 2001
+++ mwmparse.y  Sat Oct 27 16:04:01 2001
@@ -1122,98 +1122,97 @@
 /*
  * find the config file
  */
+/*
+ * The "OSF/Motif Programmer's Reference [1]"  didn't mention what to do if; a. 
+configFile
+ *  is NOT defined,   b. configFile IS defined but it does not refer to a proper file
+ *  (mulformed or unreadable), c. configFile does begin with "~" but not "~/".
+ * The code below deals with these cases for conveniences.
+ *
+ * [1] 
+(http://w3.pppl.gov/misc/motif/MotifUserGuide/en_US/Understanding_the_Resource_Description_File.html)
+ */
+ 
 static Boolean
-find_config_file2(const char *the_file) {
-    if (verbose)
-       printf("Checking for config file '%s'\n", the_file);
-    if (access(the_file, R_OK) == 0)
-       return True;
-    return False;
-}
-
-
-static Boolean
-find_config_file3(char *buf, const char *cfile) {
-    char *lang, *ptr;
-
-    /* Shall overrun check be done for each strcat()??? */
-    strcat(buf, "/");
-
-    lang = getenv("LANG");
-    if (!lang)
-       lang = "";
-
-    ptr = buf + strlen(buf);
-
-    if (strlen(lang) != 0) {
-       strcat(buf, lang);
-       strcat(buf, "/");
-       strcat(buf, cfile);
-       if (find_config_file2(buf)) {
-           return True;
-       }
-    }
-
-    strcpy(ptr, cfile);
-    if (find_config_file2(buf)) {
-       return True;
-    }
-    return False;
-}
-
+find_config_file(char *buf)
+{
+    char *head = NULL;
+    char *middle = NULL;
+    char *ptr;
+    char tmpbuf[MAX_PATH_LEN];
+    struct stat *st;
 
-static Boolean
-find_config_file(char *buf, const char *cfile) {
-    char *home;
+#if defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)
+    struct passwd *passwd;
+#endif
 
-    home = getenv("HOME");
+    ptr =  Mwm.config_file;
 
-    if (!home)
-       home = getcwd(NULL, MAX_PATH_LEN);
+    if(strlen(Mwm.config_file) > MAX_PATH_LEN)
+       return False;
 
-    switch (*cfile) {
+    switch (Mwm.config_file[0])
+    {
     case '/':
-       if (find_config_file2(cfile)) {
-           strcpy(buf, cfile);
-           return True;
-       }
+       head = "";
+       ptr = Mwm.config_file;
        break;
     case '~':
-       if (*(cfile+1) == '/') {
-           strcpy(buf, home);
-           strcat(buf, "/");
-           strcat(buf, cfile+2);
-           if (find_config_file2(buf)) {
-               return True;
-           }
+       ptr++;
+       if (Mwm.config_file[1] == '/')
+       {   /* ~ => $HOME */
+           head = getenv("HOME");
+           middle = getenv("LANG");
        }
-       else {
-           /* XXX function not implemented yet */
-           if (verbose)
-               printf("~user notation is not implemented.\n");
+       else
+#if defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)       
+       {   /* ~hoge => hoge's home */
+           int idx;
+
+           idx = strcspn(ptr, "/");
+           if(*(ptr + idx) == '\0')
+               head = NULL;
+           else
+           {
+               strncpy(tmpbuf, ptr, idx);
+               if( (passwd = getpwnam(tmpbuf)) ) {
+                   head = strcpy(tmpbuf, passwd->pw_dir);
+                   ptr += idx;
+               }
+               else
+                   head = NULL; /* no such user "hoge" */
+           }
        }
        break;
-    default: /* check for home dir */
-       strcpy(buf, home);
-       if (find_config_file3(buf, cfile))
-           return True;
-    }  /* switch (*cfile) */
 
-
-    strcpy(buf, home);
-    if (find_config_file3(buf, HOME_MWMRC))
-       return True;
-#ifndef __EMX__
-    strcpy(buf, MWM_DDIR);
 #else
-    strcpy(buf, (char*)__XOS2RedirRoot("/XFree86/lib/X11/mwm"));
+       ;  /* no <pwd.h>, no ~hoge */
+       head = NULL;
+       break;
 #endif
-    if (find_config_file3(buf, SYSTEM_MWMRC))
-       return True;
 
-    return False;
+    default:
+        /* search for the current dir */
+       head = getcwd(buf, MAX_PATH_LEN);
+       break;
+
+    } /* switch (Mwm.config_file[0]) */
+
+
+    if(!head || strlen(head) + strlen(ptr) > MAX_PATH_LEN)
+       sprintf(buf, "%s/%s", MWM_DDIR, SYSTEM_MWMRC);  /* should be ok */
+    else
+    {
+       sprintf(buf, "%s%s", head, ptr);
+       if(middle && strlen(head) + 1 + strlen(middle) + strlen(ptr) <= MAX_PATH_LEN)
+           {
+               sprintf(tmpbuf, "%s%s%s%s", head, "/", middle, ptr);        
+               if(stat(tmpbuf, st) == 0)
+                   strcpy(buf, tmpbuf);
+           }
+    }
+    return True;
 }
 
+
 char
 mwm_getc(void) {
     char c;
@@ -1300,7 +1299,7 @@
 #endif
 
     ptr = Mwm.config_file;
-    if (!find_config_file(buf, Mwm.config_file)) {
+    if (!find_config_file(buf)) {
        yyerror("Cannot find configuration file.  "
                "Using builtin defaults.\n");
 
--- Mwm.ad-0.93.14      Thu Nov 23 02:11:45 2000
+++ Mwm.ad      Fri Oct 26 16:34:57 2001
@@ -27,7 +27,7 @@
 *clickTime:            100
 *clientAutoPlace:      True
 *colormapFocusPolicy:  keyboard
-*configFile:           .mwmrc
+*configFile:           ~/.mwmrc
 *deiconifyKeyFocus:    True
 !*doubleClickTime:     "varies"
 !*edgeScrollX:         0

Reply via email to