1: #include <X/mit-copyright.h>
2:
3: /* $Header: XGetDefault.c,v 10.5 86/02/01 15:34:44 tony Rel $ */
4: /* Copyright (c) 1985, Massachusetts Institute of Technology */
5:
6: /*
7: * This routine returns options out of the X user preferences file
8: * found in the user's home directory. It either returns a pointer to
9: * the option or returns NULL if option not set. It is patterned after
10: * Andrew's file format (why be different for the sake of being different?).
11: * Andrew's code was NOT examined before writing this routine.
12: * It parses lines of the format "progname.option:value" and returns a pointer
13: * to value.
14: */
15:
16: #include <stdio.h>
17: #include <strings.h>
18: #include <ctype.h>
19:
20: #define XOPTIONFILE "/.Xdefaults" /* name in home directory of options
21: file. */
22: extern char *malloc();
23:
24: static struct ent {
25: struct ent *next; /* next option entry in chain */
26: char *oname; /* option name */
27: char *value; /* value for that option */
28: } *head; /* head of list of options */
29:
30: char *XGetDefault(prog, name)
31: register char *name; /* name of option program wants */
32: char *prog; /* name of program for option */
33: { /* to get, for example, "font" */
34: static nent = -1; /* have we been here before? */
35: register struct ent *cur; /* current entry being examined */
36: char namebuf[64];
37: register char *pv = namebuf;
38:
39: strncpy(namebuf,name,sizeof(namebuf));
40: while (*pv) { /* convert upper to lower */
41: if (isupper(*pv)) *pv += 040;
42: pv += 1;
43: }
44: if (nent == -1) nent = ReadFile(prog);/* if not, parse the file.*/
45: if (nent == 0) return(NULL);
46: cur = head;
47: do {
48: if (strcmp(namebuf,cur->oname) == 0) return(cur->value);
49: cur = cur->next;
50: } while (cur != NULL);
51: return(NULL); /* if no match, let him know */
52: }
53:
54: static ReadFile(prog)
55: char *prog; /* program name to match */
56: {
57: FILE *fptr; /* preferences file */
58: char fname[BUFSIZ]; /* longer than any conceivable size */
59: char line[BUFSIZ]; /* line buffer for each line of file*/
60: register char *point,*colon; /* where in the line the keys are */
61: register struct ent *buf; /* new memory for valid option line */
62: register char *pv; /* pointer to value for lowering */
63: int len, nchars;
64: int nentries = 0; /* number of entries found */
65: char *getenv();
66: char *home = getenv("HOME");
67:
68: if (rindex(prog,'/') != NULL) prog = rindex(prog, '/') + 1;
69: /* if full path, get last component */
70: if (home == NULL) return(0);
71:
72: strcpy(fname, home); /* form full path name of file */
73: strcat(fname, XOPTIONFILE);
74:
75: if ((fptr = fopen(fname, "r")) == NULL) return(0);
76:
77: while ( fgets(line, sizeof(line), fptr) != NULL ) {
78: if (line[0] == '#') continue; /* comment? */
79: point = index(line,'.');
80: colon = index(line,':');
81: if ( (point == NULL) || (colon == NULL) || (colon < point) )
82: continue; /* both . and : required on line*/
83: if ( point != line ) /* check all chars up to '.' */
84: if (strncmp(line, prog, point - line) != 0) continue;
85:
86: /*
87: * ok, we've passed all the tests, so it is a valid option for
88: * this program, or is global option.
89: */
90:
91: len = strlen(line);
92: if(line[len-1] == '\n') line[len-1] = '\0';
93: /* braindamaged fgets call */
94: nchars = len - (point - line);
95: /*
96: * allocate space for data structure and text in one call
97: */
98: if ( (buf = (struct ent *)malloc (sizeof(struct ent) + nchars))
99: == NULL) {
100: fprintf(stderr, "ReadFile: Out of memory\n");
101: exit(1);
102: }
103: len += 1;
104: *colon = '\0'; /* seperate string */
105: /* chain in the new buffer */
106: bcopy (point+1, buf+1, nchars);
107: buf->oname = (char *) (buf + 1);
108: buf->value = buf->oname + (colon - point);
109: pv = buf->oname;
110: while (*pv) { /* convert upper to lower */
111: if (isupper(*pv)) *pv += 040;
112: pv += 1;
113: }
114: while(isspace(*buf->value)) buf->value++; /* skip over spaces*/
115: buf->next = head;
116: head = buf;
117: nentries += 1;
118: }
119: fclose(fptr);
120: return(nentries);
121: }
Defined functions
XGetDefault
defined in line
30; used 157 times
- in /usr/src/new/X/XMenu/XMenuCreate.c line
178,
184,
191,
197,
210,
223,
232,
239-242(2),
255,
268-274(3),
281-284(2),
297,
310-313(2)
- in /usr/src/new/X/Xlib/XCreate.c line
82,
91,
97-124(8)
- in /usr/src/new/X/Xlib/XCreateTerm.c line
89,
97,
106-136(9)
- in /usr/src/new/X/Xlib/Xlib.h line
354
- in /usr/src/new/X/bitmap/bitmap.c line
222-233(7)
- in /usr/src/new/X/comsat/comsat.c line
421-437(11)
- in /usr/src/new/X/hacks/maze/maze.c line
78-85(5)
- in /usr/src/new/X/libnest/nest.c line
59-64(2),
235
- in /usr/src/new/X/xclock/xclock.c line
127-144(9),
151
- in /usr/src/new/X/xdvi/dv.c line
143-152(7)
- in /usr/src/new/X/xfd/xfd.c line
49-57(6)
- in /usr/src/new/X/xgedit/gx.c line
270-279(6)
- in /usr/src/new/X/ximpv/impv.c line
148-156(6)
- in /usr/src/new/X/xload/xload.c line
201-215(10)
- in /usr/src/new/X/xnwm/xnwm.c line
149-175(9),
183-192(4)
- in /usr/src/new/X/xsetroot/xsetroot.c line
99-105(3)
- in /usr/src/new/X/xshell/xshell.c line
194-218(10),
226,
588
- in /usr/src/new/X/xterm/main.c line
105-141(13)
- in /usr/src/new/X/xwm/main.c line
92-98(3),
104,
110
Defined variables
head
defined in line
28; used 3 times
Defined struct's
ent
defined in line
24; used 10 times
Defined macros