[comp.windows.x] XGetDefault

tsf@theory.cs.cmu.edu (Timothy Freeman) (06/30/87)

Here's the fix for getting XGetDefault to pay attention to its first
argument every time its called:

[b.ergo]/usr/misc/X/Xlib% sccs sccsdiff -r1.1 -r1.2 -c3 XGetDefault.c
*** /tmp/geta17084	Mon Jun 29 19:01:59 1987
--- /tmp/getb17084	Mon Jun 29 19:02:00 1987
***************
*** 31,66 ****
  	char bal;			/* for avl use			*/
  } *head;
  
! char *XGetDefault(prog, name)
! 	register char *name;		/* name of option program wants */
  	char *prog;			/* name of program for option	*/
  {					/* to get, for example, "font"  */
  	static nent = -1;		/* have we been here before?	*/
! 	register struct ent *cur;	/* current entry being examined */
! 	register int cmp;
! 	char namebuf[64];
! 	register char *pv = namebuf;
  
! 	strncpy(namebuf, name, sizeof(namebuf));
  	while (*pv) {			/* convert upper to lower	*/
  		if (isupper(*pv))  *pv += 040;
  		pv++;
  	}
  	if (nent == -1)
! 		nent = ReadFile(prog);/* if not, parse the file.*/
  	if (nent == 0)
  		return(NULL);
! 	cur = head;
! 	do {
! 		if ((cmp = strcmp(namebuf, cur->oname)) == 0)
! 			return(cur->value);
! 		cur = cmp > 0 ? cur->right : cur->left;
! 	} while (cur != NULL);
! 	return(NULL);			/* if no match, let him know	*/
  }
  
! static ReadFile(prog)
! 	char *prog;			/* program name to match 	    */
  {
  	register char *point,*colon;	/* where in the line the keys are   */
  	register char *oname, *val;	/* new memory for valid option line */
--- 31,83 ----
  	char bal;			/* for avl use			*/
  } *head;
  
! static char *lookup (head, name)
!      struct ent *head;
!      char *name;
! {
!   register struct ent *cur = head;
!   register int cmp;
!   do {
!     if ((cmp = strcmp(name, cur->oname)) == 0)
!       return(cur->value);
!     cur = cmp > 0 ? cur->right : cur->left;
!   } while (cur != NULL);
!   return (NULL);
! }
! 
! char *XGetDefault(prog, ULname)
! 	register char *ULname;		/* name of option program wants */
  	char *prog;			/* name of program for option	*/
  {					/* to get, for example, "font"  */
  	static nent = -1;		/* have we been here before?	*/
! 	char *result;	/* current entry being examined */
! 	char name [BUFSIZ];
! 	char namebuf[BUFSIZ];
! 	register char *pv = name;
  
! 	strcpy (name, ULname);
  	while (*pv) {			/* convert upper to lower	*/
  		if (isupper(*pv))  *pv += 040;
  		pv++;
  	}
+ 	if ((pv = rindex(prog,'/')) != NULL)
+ 		prog = pv + 1;	/* if full path, get last component */
  	if (nent == -1)
! 		nent = ReadFile();/* if not, parse the file.*/
  	if (nent == 0)
  		return(NULL);
! 	/* First try looking up "prog.name". */
! 	strcpy (namebuf, prog);
! 	strcat (namebuf, ".");
! 	strcat (namebuf, name);
! 	if (result = lookup (head, namebuf)) return (result);
! 	/* Now try ".name". */
! 	strcpy (namebuf, ".");
! 	strcat (namebuf, name);
! 	return(lookup (head, namebuf));
  }
  
! static ReadFile()
  {
  	register char *point,*colon;	/* where in the line the keys are   */
  	register char *oname, *val;	/* new memory for valid option line */
***************
*** 74,81 ****
  	char *getenv();
  	char *home = getenv("HOME");
  
- 	if ((pv = rindex(prog,'/')) != NULL)
- 		prog = pv + 1;	/* if full path, get last component */
      for(first = 1 ; first >= 0 ; first--) {
  	if(first)	/* Use any defaults in XDEFAULTS. */
  		fptr = fopen(XDEFAULTS, "r");
--- 91,96 ----
***************
*** 93,108 ****
  		colon = index(line,':');
  		if ( (point == NULL) || (colon == NULL) || (colon < point) )
  			continue;	/* both . and : required on line*/
- 		*point = 0;
- 		if ( point != line )	/* check all chars up to '.' 	*/
- 			if (strcmp(line, prog) != 0)
- 				continue;
- 
- 		/* 
- 		 * ok, we've passed all the tests, so it is a valid option for
- 		 * this program, or is global option.
- 		 */
- 
  		len = strlen(colon);
  		if(colon[len-1] == '\n')
  			colon[len-1] = '\0';	/* braindamaged fgets call */
--- 108,113 ----
***************
*** 109,122 ****
  		/*
  		 * allocate space for text
  		 */
! 		point++;
! 		len = colon - point;
  		for(colon++ ; isspace(*colon) ; colon++); /* skip over spaces */
  		if((oname = malloc(len + strlen(colon) + 2)) == NULL) {
  			fprintf(stderr, "ReadFile: Out of memory\n");
  			exit(1);
  		}
! 		strncpy(oname, point, len);
  		oname[len] = 0;
  		pv = oname;
  		while (*pv) {		/* convert upper to lower	*/
--- 114,126 ----
  		/*
  		 * allocate space for text
  		 */
! 		len = colon - line;
  		for(colon++ ; isspace(*colon) ; colon++); /* skip over spaces */
  		if((oname = malloc(len + strlen(colon) + 2)) == NULL) {
  			fprintf(stderr, "ReadFile: Out of memory\n");
  			exit(1);
  		}
! 		strncpy(oname, line, len);
  		oname[len] = 0;
  		pv = oname;
  		while (*pv) {		/* convert upper to lower	*/
-- 
Tim Freeman

Arpanet: tsf@theory.cs.cmu.edu
Uucp:    ...!seismo!theory.cs.cmu.edu!tsf