wmmenugen: Add file name validator function
[wmaker-crm.git] / util / wmmenugen.c
blobc6d5099bcf7896d190b2a5c4ed0972db17864dab
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * Copyright (c) 2010. Tamas Tevesz <ice@extreme.hu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #if __GLIBC__ && \
22 (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) || \
23 !_XOPEN_SOURCE
24 #define _XOPEN_SOURCE 500 /* nftw */
25 #endif
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <ftw.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <unistd.h>
38 #include "wmmenugen.h"
40 static void addWMMenuEntryCallback(WMMenuEntry *aEntry);
41 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data);
42 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
43 static int menuSortFunc(const void *left, const void *right);
44 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata);
45 static WMTreeNode *findPositionInMenu(char *submenu);
46 static void (*parse)(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
47 static Bool (*validateFilename)(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
49 static WMArray *plMenuNodes;
50 char *terminal;
52 extern char *__progname;
54 int main(int argc, char **argv)
56 struct stat st;
57 int i;
58 int *previousDepth;
60 plMenuNodes = WMCreateArray(8); /* grows on demand */
61 menu = (WMTreeNode *)NULL;
62 parse = NULL;
63 validateFilename = NULL;
65 /* assemblePLMenuFunc passes this around */
66 previousDepth = (int *)wmalloc(sizeof(int));
67 *previousDepth = -1;
69 /* currently this is used only by the xdg parser, but it might be useful
70 * in the future localizing other menus, so it won't hurt to have it here.
72 parse_locale(NULL, &env_lang, &env_ctry, &env_enc, &env_mod);
73 terminal = find_terminal_emulator();
75 if (argc < 3) {
76 fprintf(stderr, "Usage: %s -parser:<parser> fspec [fpsec...] "
77 "[-parser:<parser> fspec [fpsec...]...]\n", __progname);
78 fputs( "Known parsers: xdg wmconfig\n", stderr);
79 return 1;
82 for (i = 1; i < argc; i++)
84 if (strncmp(argv[i], "-parser:", 8) == 0) {
85 if (strcmp(argv[i] + 8, "xdg") == 0) {
86 #if DEBUG
87 fputs("Using parser \"xdg\"\n", stderr);
88 #endif
89 parse = &parse_xdg;
90 } else if (strcmp(argv[i] + 8, "wmconfig") == 0) {
91 #if DEBUG
92 fputs("Using parser \"wmconfig\"\n", stderr);
93 #endif
94 parse = &parse_wmconfig;
95 validateFilename = &wmconfig_validate_file;
96 } else {
97 fprintf(stderr, "%s: Unknown parser \"%s\"\n", __progname, argv[i] + 8);
99 continue;
102 if (parse) {
103 if (stat(argv[i], &st) == -1) {
104 fprintf(stderr, "%s: unable to stat \"%s\"\n", __progname, argv[i]);
105 } else if (S_ISREG(st.st_mode)) {
106 parse(argv[i], addWMMenuEntryCallback);
107 } else if (S_ISDIR(st.st_mode)) {
108 nftw(argv[i], dirParseFunc, 16, FTW_PHYS);
109 } else {
110 fprintf(stderr, "%s: \"%s\" is not a file or directory\n", __progname, argv[i]);
112 } else {
113 fprintf(stderr, "%s: argument \"%s\" with no valid parser\n", __progname, argv[i]);
117 if (!menu) {
118 fprintf(stderr, "%s: parsers failed to create a valid menu\n", __progname);
119 return 1;
122 WMSortTree(menu, menuSortFunc);
123 WMTreeWalk(menu, assemblePLMenuFunc, previousDepth, True);
125 i = WMGetArrayItemCount(plMenuNodes);
126 if (i > 2) { /* more than one submenu unprocessed is almost certainly an error */
127 fprintf(stderr, "%s: unprocessed levels on the stack. fishy.\n", __progname);
128 return 1;
129 } else if (i > 1 ) { /* possibly the top-level attachment is not yet done */
130 WMPropList *first, *next;
131 next = WMPopFromArray(plMenuNodes);
132 first = WMPopFromArray(plMenuNodes);
133 WMAddToPLArray(first, next);
134 WMAddToArray(plMenuNodes, first);
137 printf("%s\n", WMGetPropListDescription((WMPropList *)WMGetFromArray(plMenuNodes, 0), True));
139 return 0;
142 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
144 (void)st;
145 (void)tflags;
146 (void)ftw;
148 if (validateFilename &&
149 !validateFilename(filename, st, tflags, ftw))
150 return 0;
152 parse(filename, addWMMenuEntryCallback);
153 return 0;
156 /* upon fully deducing one particular menu entry, parsers call back to this
157 * function to have said menu entry added to the wm menu. initializes wm menu
158 * with a root element if needed.
160 static void addWMMenuEntryCallback(WMMenuEntry *aEntry)
162 WMMenuEntry *wm;
163 WMTreeNode *at;
165 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry)); /* this entry */
166 at = (WMTreeNode *)NULL; /* will be a child of this entry */
168 if (!menu) {
169 WMMenuEntry *root;
171 root = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
172 root->Name = "Applications";
173 root->CmdLine = NULL;
174 root->SubMenu = NULL;
175 root->Flags = 0;
176 menu = WMCreateTreeNode(root);
179 if (aEntry->SubMenu)
180 at = findPositionInMenu(wstrdup(aEntry->SubMenu));
182 if (!at)
183 at = menu;
185 wm->Flags = aEntry->Flags;
186 wm->Name = wstrdup(aEntry->Name);
187 wm->CmdLine = wstrdup(aEntry->CmdLine);
188 wm->SubMenu = NULL;
189 WMAddItemToTree(at, wm);
193 /* creates the proplist menu out of the abstract menu representation in `menu'.
195 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data)
197 WMMenuEntry *wm;
198 WMPropList *pl;
199 int pDepth, cDepth;
201 wm = (WMMenuEntry *)WMGetDataForTreeNode(aNode);
202 cDepth = WMGetTreeNodeDepth(aNode);
203 pDepth = *(int *)data;
205 if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
206 WMPropList *last, *but; /* merge the tail up to the current position */
207 int i;
208 for (i = pDepth - cDepth; i > 0; i--) {
209 last = WMPopFromArray(plMenuNodes);
210 but = WMPopFromArray(plMenuNodes);
211 WMAddToPLArray(but, last);
212 WMAddToArray(plMenuNodes, but);
216 if (!wm->CmdLine) { /* new submenu */
217 WMAddToArray(plMenuNodes, WMCreatePLArray(WMCreatePLString(wm->Name), NULL));
218 } else { /* new menu item */
219 pl = WMPopFromArray(plMenuNodes);
220 if (wm->Flags & F_RESTART_OTHER) { /* RESTART, somewm */
221 char buf[1024];
222 memset(buf, 0, sizeof(buf));
223 snprintf(buf, sizeof(buf), "%s %s", _("Restart"), wm->Name);
224 WMAddToPLArray(pl, WMCreatePLArray(
225 WMCreatePLString(buf),
226 WMCreatePLString("RESTART"),
227 WMCreatePLString(wm->CmdLine),
228 NULL)
230 } else if (wm->Flags & F_RESTART_SELF) {/* RESTART */
231 WMAddToPLArray(pl, WMCreatePLArray(
232 WMCreatePLString(_("Restart Window Maker")),
233 WMCreatePLString("RESTART"),
234 NULL)
236 } else if (wm->Flags & F_QUIT) { /* EXIT */
237 WMAddToPLArray(pl, WMCreatePLArray(
238 WMCreatePLString(_("Exit Window Maker")),
239 WMCreatePLString("EXIT"),
240 NULL)
242 } else { /* plain simple command */
243 char buf[1024];
244 memset(buf, 0, sizeof(buf));
245 if (wm->Flags & F_TERMINAL) /* XXX: quoting! */
246 snprintf(buf, sizeof(buf), "%s -e \"%s\"", terminal, wm->CmdLine);
247 else
248 snprintf(buf, sizeof(buf), "%s", wm->CmdLine);
249 WMAddToPLArray(pl, WMCreatePLArray(
250 WMCreatePLString(wm->Name),
251 WMCreatePLString("SHEXEC"),
252 WMCreatePLString(buf),
253 NULL)
256 WMAddToArray(plMenuNodes, pl);
259 *(int *)data = cDepth;
260 return;
263 /* sort the menu tree; callback for WMSortTree()
265 static int menuSortFunc(const void *left, const void *right)
267 WMMenuEntry *leftwm;
268 WMMenuEntry *rightwm;
270 leftwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)left);
271 rightwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)right);
273 /* submenus first */
274 if (!leftwm->CmdLine && rightwm->CmdLine)
275 return -1;
276 if (leftwm->CmdLine && !rightwm->CmdLine)
277 return 1;
279 /* the rest lexicographically */
280 return strcasecmp(leftwm->Name, rightwm->Name);
284 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
285 * creates `submenu' path (anchored to the root) along the way.
287 static WMTreeNode *findPositionInMenu(char *submenu)
289 char *q;
290 WMMenuEntry *wm;
291 WMTreeNode *node, *pnode;
292 char buf[1024];
294 /* qualify submenu with "Applications/" (the root node) */
295 memset(buf, 0, sizeof(buf));
296 snprintf(buf, sizeof(buf), "Applications/%s", submenu);
298 /* start at the root */
299 node = menu;
301 q = strtok(buf, "/");
302 while (q) {
303 pnode = node;
304 node = WMFindInTreeWithDepthLimit(pnode, nodeFindSubMenuByNameFunc, q, 1);
305 if (!node) {
306 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
307 wm->Flags = 0;
308 wm->Name = wstrdup(q);
309 wm->CmdLine = NULL;
310 wm->SubMenu = NULL;
311 node = WMAddNodeToTree(pnode, WMCreateTreeNode(wm));
313 q = strtok(NULL, "/");
316 return node;
319 /* find node where Name = cdata and node is a submenu
321 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata)
323 WMMenuEntry *wm;
325 wm = (WMMenuEntry *)item;
327 if (wm->CmdLine) /* if it has a cmdline, it can't be a submenu */
328 return 0;
330 return strcmp(wm->Name, (const char *)cdata) == 0;