WPrefs: Hot corner preferences
[wmaker-crm.git] / util / wmmenugen_parse_wmconfig.c
blob82ba01032775d02c4cdde1bb0747707407e07de4
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * Wmconfig <http://www.arrishq.net/> parser functions
6 * Copyright (c) 2010. Tamas Tevesz <ice@extreme.hu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <sys/types.h>
24 #include <sys/stat.h>
26 #include <ctype.h>
27 #include <ftw.h>
28 #if DEBUG
29 #include <errno.h>
30 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "wmmenugen.h"
38 typedef struct {
39 char *Name;
40 char *Exec;
41 char *Category;
42 char *Restart;
43 int Flags;
44 } WMConfigMenuEntry;
46 static Bool wmc_to_wm(WMConfigMenuEntry **wmc, WMMenuEntry **wm);
47 static void parse_wmconfig_line(char **label, char **key, char **value, const char *line);
48 static void init_wmconfig_storage(WMConfigMenuEntry **wmc);
51 void parse_wmconfig(const char *file, cb_add_menu_entry *addWMMenuEntryCallback)
53 FILE *fp;
54 char buf[1024];
55 char *p, *lastlabel, *label, *key, *value;
56 WMConfigMenuEntry *wmc;
57 WMMenuEntry *wm;
59 lastlabel = label = key = value = NULL;
61 fp = fopen(file, "r");
62 if (!fp) {
63 #if DEBUG
64 fprintf(stderr, "Error opening file %s: %s\n", file, strerror(errno));
65 #endif
66 return;
69 wmc = (WMConfigMenuEntry *)wmalloc(sizeof(WMConfigMenuEntry));
70 wmc->Name = NULL;
71 wmc->Exec = NULL;
72 wmc->Category = NULL;
73 wmc->Restart = NULL;
74 wmc->Flags = 0;
76 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
78 memset(buf, 0, sizeof(buf));
80 while (fgets(buf, sizeof(buf), fp)) {
82 p = buf;
84 /* skip whitespaces */
85 while (isspace(*p))
86 p++;
87 /* skip comments, empty lines */
88 if (*p == '\r' || *p == '\n' || *p == '#') {
89 memset(buf, 0, sizeof(buf));
90 continue;
92 /* trim crlf */
93 buf[strcspn(buf, "\r\n")] = '\0';
94 if (strlen(buf) == 0)
95 continue;
97 parse_wmconfig_line(&label, &key, &value, p);
99 if (label && strlen(label) == 0) {
100 wfree(label);
101 continue;
103 if (!lastlabel && label)
104 lastlabel = wstrdup(label);
106 if (strcmp(lastlabel, label) != 0) {
107 if (wmc_to_wm(&wmc, &wm)) {
108 (*addWMMenuEntryCallback)(wm);
109 init_wmconfig_storage(&wmc);
112 wfree(lastlabel);
113 lastlabel = wstrdup(label);
115 wfree(label);
117 if (key && value) {
118 if (strcmp(key, "name") == 0)
119 wmc->Name = value;
120 else if (strcmp(key, "exec") == 0)
121 wmc->Exec = value;
122 else if (strcmp(key, "group") == 0)
123 wmc->Category = value;
124 else if (strcmp(key, "restart") == 0)
125 wmc->Restart = value;
126 else if (strcmp(key, "terminal") == 0)
127 wmc->Flags |= F_TERMINAL;
130 wfree(key);
133 fclose(fp);
135 wfree(lastlabel);
137 if (wmc_to_wm(&wmc, &wm)) {
138 (*addWMMenuEntryCallback)(wm);
139 init_wmconfig_storage(&wmc);
143 /* an example to illustrate validateFilename.
144 * with wmconfig, no special handling is needed
146 Bool wmconfig_validate_file(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
148 (void)filename;
149 (void)st;
150 (void)tflags;
151 (void)ftw;
153 return True;
154 #if 0 /* not dead code, example */
156 /* or we could have gone intro extremes */
157 char *base_name;
158 Bool ret;
160 (void)tflags;
162 base_name = wstrdup(filename + ftw->base);
163 ret = True;
165 if (!S_ISREG(st->st_mode) || /* not a regular file */
166 (st->st_uid != 0 && st->st_uid != getuid()) || /* bad guy injected this file */
167 strpbrk(base_name, ".") || /* wmconfig typically has no extension */
168 st->st_size >= 128 * 131072 || /* noone writes wmconfig files > 128K */
169 st->st_size == 0 || /* nor empty ones */
170 ftw->level > 16) /* how did we get this deep? */
171 ret = False;
173 wfree(base_name);
175 return ret;
176 #endif
179 /* get a line allocating label, key and value as necessary */
180 static void parse_wmconfig_line(char **label, char **key, char **value, const char *line)
182 const char *p;
183 int kstart, kend;
185 p = line;
186 *label = *key = *value = NULL;
187 kstart = kend = 0;
189 /* skip whitespace */
190 while (isspace(*(p + kstart)))
191 kstart++;
193 kend = kstart;
194 /* find end of label */
195 while (*(p + kend) && !isspace(*(p + kend)))
196 kend++;
198 /* label */
199 *label = wstrndup(p + kstart, kend - kstart);
200 kstart = kend + 1;
202 /* skip whitespace */
203 while (isspace(*(p + kstart)))
204 kstart++;
206 kend = kstart;
207 /* find end of key */
208 while (*(p + kend) && !isspace(*(p + kend)))
209 kend++;
211 /* key */
212 *key = wstrndup(p + kstart, kend - kstart);
213 kstart = kend + 1;
215 /* skip until after " */
216 while (*(p + kstart) && *(p + kstart) != '"')
217 kstart++;
218 kstart++;
220 kend = kstart;
221 /* skip until " */
222 while (*(p + kend) && *(p + kend) != '"')
223 kend++;
225 /* value */
226 *value = wstrndup(p + kstart, kend - kstart);
229 /* normalize and convert one wmconfig-format entry to wm format */
230 static Bool wmc_to_wm(WMConfigMenuEntry **wmc, WMMenuEntry **wm)
232 char *p;
233 size_t slen;
235 /* only Exec is mandatory, and it's better exist in a known place */
236 if (!((*wmc)->Exec &&
237 *(*wmc)->Exec &&
238 fileInPath((*wmc)->Exec)))
239 return False;
241 /* normalize Exec: wmconfig tends to stick an ampersand
242 * at the end of everything, which we don't need */
243 slen = strlen((*wmc)->Exec) - 1;
244 p = (*wmc)->Exec;
245 while (slen > 0 && (isspace(*(p + slen)) || *(p + slen) == '&'))
246 *(p + slen--) = '\0';
248 /* if there's no Name, use the first word of Exec; still better
249 * than nothing. i realize it's highly arguable whether `xterm' from
250 * `xterm -e "ssh dev push-to-prod"' is helpful or not, but since
251 * the alternative is to completely lose the entry, i opt for this.
252 * you could just fix the descriptor file to have a label <G> */
253 if (!(*wmc)->Name) {
254 (*wmc)->Name = wstrdup((*wmc)->Exec);
255 p = strchr((*wmc)->Name, ' ');
256 if (p)
257 *p = '\0';
260 /* if there's no Category, use "Applications"; apparently "no category"
261 * can manifest both as no `group' descriptor at all, or a group
262 * descriptor of "" */
263 if (!(*wmc)->Category || !*(*wmc)->Category)
264 (*wmc)->Category = wstrdup("Applications");
266 /* the `restart' type is used for restart, restart other
267 * wm and quit current wm too. separate these cases. */
268 if ((*wmc)->Restart) {
269 if (strcmp((*wmc)->Restart, "restart") == 0)
270 (*wmc)->Flags |= F_RESTART_SELF;
271 else if (strcmp((*wmc)->Restart, "quit") == 0)
272 (*wmc)->Flags |= F_QUIT;
273 else
274 (*wmc)->Flags |= F_RESTART_OTHER;
277 (*wm)->Name = (*wmc)->Name;
278 (*wm)->CmdLine = (*wmc)->Exec;
279 (*wm)->SubMenu = (*wmc)->Category;
280 (*wm)->Flags = (*wmc)->Flags;
282 return True;
285 static void init_wmconfig_storage(WMConfigMenuEntry **wmc)
287 if ((*wmc)->Category)
288 wfree((*wmc)->Category);
289 (*wmc)->Category = NULL;
290 if ((*wmc)->Name)
291 wfree((*wmc)->Name);
292 (*wmc)->Name = NULL;
293 if ((*wmc)->Restart)
294 wfree((*wmc)->Restart);
295 (*wmc)->Restart = NULL;
296 (*wmc)->Flags = 0;