-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc.c
183 lines (170 loc) · 4.8 KB
/
rc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
void
rc_init(void) {
int line, ignore, i;
char buf[RCLEN + 2], *c;
FILE *f;
c = parsepath(RCPATH);
if(!(f = fopen(c, "r")) && !(f = fopen(RCSAMPLE, "r")))
eprintf(PROGRAM": "RCSAMPLE": No such file or directory\n");
free(c);
for(line = 1, ignore = 0; fgets(buf, RCLEN + 2, f); line++) {
if(buf[strlen(buf) - 1] != '\n') {
if(ignore != line)
fprintf(stderr, PROGRAM": "RCPATH": line %d: "
"Exceed the %d chars limit.\n", line, RCLEN);
ignore = line--;
continue;
}
for(i = 0; buf[i] != '#' && buf[i] != '\n';)
if(buf[++i] == '\'')
while(buf[++i] != '\'');
for(--i; i >= 0 && (buf[i] == ' ' || buf[i] == '\t'); i--);
for(; ++i < (RCLEN + 1); (buf[i] = '\0'));
if(ignore == line || !buf[0])
continue;
c = NULL;
if((strstr(buf, "layout") && buf[strlen(buf) - 1] == ':') ||
isspace(buf[0]))
c = parselayout(buf);
else
c = parseline(buf);
if(c)
fprintf(stderr, PROGRAM": "RCPATH": line %d: %s\n", line, c);
}
fclose(f);
data.tagmask = 0L;
if(!var.tags)
var.tags = 1;
for(i = 0; i < (int)var.tags; i++)
data.tagmask |= (1<<i);
}
void
rc_mask(char *mask, unsigned int *mod, char **key) {
char *str;
unsigned int i, j;
str = estrdup(mask);
for(i = 0, j = 2; str[i]; i++)
if(str[i] == '-')
++j;
char *t[j];
*mod = getmodmask((t[0] = strtok(str, "-")));
for(i = 1; (t[i] = strtok(NULL, "-")); i++)
*mod |= getmodmask(t[i]);
if(key)
*key = estrdup(t[i - 1]);
free(str);
}
unsigned int
getmodmask(char *c) {
switch(c[0]) {
case 'C': return ControlMask;
case 'S': return ShiftMask;
case 'M': return getmodnumber(c[1]);
default : return 0;
}
}
unsigned int
getmodnumber(char c) {
switch(c) {
case '1': return Mod1Mask;
case '4': return Mod4Mask;
case '5': return Mod5Mask;
default : return 0;
}
}
char*
parselayout(char *str) {
int i, j;
char *c;
if(isspace(str[0])) {
for(i = 0, j = 1; str[i]; i++)
if(str[i] == ';')
++j;
for(i = 0; isspace(str[i]); i++);
return layout_add(NULL, str + i, j);
} else if((c = strchr(str, '\''))) {
for(i = 1; c[i] != '\''; i++);
c[i] = '\0';
return layout_add(++c, NULL, 1);
} else
return "Layout name must be \'single quoted\'";
}
char*
parseline(char *str) {
int i, j, k;
Bool b;
if(!str)
return "Nothing to parse.";
for(i = 0, j = 2, b = False; str[i]; i++) {
if(str[i] == '\'')
if(!(b = !b))
++j;
if(!b && !isalnum(str[i]) && isalnum(str[i + 1]))
++j;
}
char t[j][RCLEN + 2], *v[j], start[j], end[j];
for(i = 0; i < j; i++) {
memset(&(t[i]), '\0', sizeof(t[i]));
start[i] = end[i] = '\0';
v[i] = NULL;
}
for(i = 0, j = 0; str[i]; i++) {
if(!isalnum(str[i]) && str[i] != '\'' && str[i] != '<')
continue;
if(!isalnum((start[j] = str[i])))
++i;
end[j] = start[j] == '\'' ? '\'' : (start[j] == '<' ? '>' : ',');
if(isdigit(str[i]) && str[i - 1] == '-')
--i;
for(k = 0; str[i] && str[i] != end[j]; i++, k++) {
if(isalnum(start[j]) && isspace(str[i]))
break;
t[j][k] = str[i];
}
t[j][k] = '\0';
v[j] = t[j];
v[++j] = NULL;
}
return setparsed(start, v);
}
char*
parsepath(char *str) {
int i, j;
char *c, tmp[BUFSIZ];
c = estrdup(str);
for(i = 0; c[i]; i++) {
if(c[i] != '$')
continue;
c[i] = '\0';
memset(&tmp, '\0', sizeof(tmp));
for(j = 0, i++; isalnum(c[i]) || c[i] == '_'; j++, i++)
tmp[j] = c[i];
sprintf(tmp, "%s%s%s", c, getenv(tmp) ? getenv(tmp) : "", c + i);
free(c);
c = estrdup(tmp);
i = -1;
}
return c;
}
char*
setparsed(char *c, char **v) {
char type[2], *argv[2];
unsigned int i;
if(!v[0] || !v[1])
return "Nothing to parse.";
if(isalpha(c[0])) {
argv[0] = v[1];
argv[1] = NULL;
return var_set(v[0], argv);
} else if(c[0] != '<')
return "Invalid first char.";
if(strstr(v[0], "Button"))
return button_add(v[0], v[1]);
memset(&type, '-', sizeof(type));
for(i = 0; v[i] && i < LEN(type); i++)
type[i] = (c[i + 2] == '\'' ? 'c' : (isdigit(c[i + 2]) ? 'i' :
(c[i + 2] == '<' ? 'm' : (isalpha(c[i + 2]) ? 'n' : '-'))));
for(i = 0; i < LEN(argv); i++)
argv[i] = v[i + 2];
return key_add(v[0], v[1], &argv, &type);
}