texi2txt: add support for enumerated lists
[wmaker-crm.git] / util / wmmenugen.c
blob0c55d51b5fd716b057172c4a0fd1fdc7fc57f2f6
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 #include <sys/types.h>
22 #include <sys/stat.h>
24 #include <ctype.h>
25 #include <ftw.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
32 #include "wmmenugen.h"
34 static void addWMMenuEntryCallback(WMMenuEntry *aEntry);
35 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data);
36 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
37 static int menuSortFunc(const void *left, const void *right);
38 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata);
39 static WMTreeNode *findPositionInMenu(const char *submenu);
42 typedef void fct_parse_menufile(const char *file, cb_add_menu_entry *addWMMenuEntryCallback);
43 typedef Bool fct_validate_filename(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
46 static WMArray *plMenuNodes;
47 static const char *terminal;
48 static fct_parse_menufile *parse;
49 static fct_validate_filename *validateFilename;
51 static const char *prog_name;
53 /* Global Variables from wmmenugen.h */
54 WMTreeNode *menu;
55 char *env_lang, *env_ctry, *env_enc, *env_mod;
57 int main(int argc, char **argv)
59 struct stat st;
60 int i;
61 int *previousDepth;
63 prog_name = argv[0];
64 plMenuNodes = WMCreateArray(8); /* grows on demand */
65 menu = (WMTreeNode *)NULL;
66 parse = NULL;
67 validateFilename = NULL;
69 /* assemblePLMenuFunc passes this around */
70 previousDepth = (int *)wmalloc(sizeof(int));
71 *previousDepth = -1;
73 /* currently this is used only by the xdg parser, but it might be useful
74 * in the future localizing other menus, so it won't hurt to have it here.
76 parse_locale(NULL, &env_lang, &env_ctry, &env_enc, &env_mod);
77 terminal = find_terminal_emulator();
79 if (argc < 3) {
80 fprintf(stderr, "Usage: %s -parser:<parser> fspec [fpsec...] "
81 "[-parser:<parser> fspec [fpsec...]...]\n", prog_name);
82 fputs( "Known parsers: xdg wmconfig\n", stderr);
83 return 1;
86 for (i = 1; i < argc; i++)
88 if (strncmp(argv[i], "-parser:", 8) == 0) {
89 if (strcmp(argv[i] + 8, "xdg") == 0) {
90 #if DEBUG
91 fputs("Using parser \"xdg\"\n", stderr);
92 #endif
93 parse = &parse_xdg;
94 } else if (strcmp(argv[i] + 8, "wmconfig") == 0) {
95 #if DEBUG
96 fputs("Using parser \"wmconfig\"\n", stderr);
97 #endif
98 parse = &parse_wmconfig;
99 validateFilename = &wmconfig_validate_file;
100 } else {
101 fprintf(stderr, "%s: Unknown parser \"%s\"\n", prog_name, argv[i] + 8);
103 continue;
106 if (parse) {
107 if (stat(argv[i], &st) == -1) {
108 fprintf(stderr, "%s: unable to stat \"%s\"\n", prog_name, argv[i]);
109 } else if (S_ISREG(st.st_mode)) {
110 parse(argv[i], addWMMenuEntryCallback);
111 } else if (S_ISDIR(st.st_mode)) {
112 nftw(argv[i], dirParseFunc, 16, FTW_PHYS);
113 } else {
114 fprintf(stderr, "%s: \"%s\" is not a file or directory\n", prog_name, argv[i]);
116 } else {
117 fprintf(stderr, "%s: argument \"%s\" with no valid parser\n", prog_name, argv[i]);
121 if (!menu) {
122 fprintf(stderr, "%s: parsers failed to create a valid menu\n", prog_name);
123 return 1;
126 WMSortTree(menu, menuSortFunc);
127 WMTreeWalk(menu, assemblePLMenuFunc, previousDepth, True);
129 i = WMGetArrayItemCount(plMenuNodes);
130 if (i > 2) { /* more than one submenu unprocessed is almost certainly an error */
131 fprintf(stderr, "%s: unprocessed levels on the stack. fishy.\n", prog_name);
132 return 1;
133 } else if (i > 1 ) { /* possibly the top-level attachment is not yet done */
134 WMPropList *first, *next;
135 next = WMPopFromArray(plMenuNodes);
136 first = WMPopFromArray(plMenuNodes);
137 WMAddToPLArray(first, next);
138 WMAddToArray(plMenuNodes, first);
141 printf("%s\n", WMGetPropListDescription((WMPropList *)WMGetFromArray(plMenuNodes, 0), True));
143 return 0;
146 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
148 (void)st;
149 (void)tflags;
150 (void)ftw;
152 if (validateFilename &&
153 !validateFilename(filename, st, tflags, ftw))
154 return 0;
156 parse(filename, addWMMenuEntryCallback);
157 return 0;
160 /* upon fully deducing one particular menu entry, parsers call back to this
161 * function to have said menu entry added to the wm menu. initializes wm menu
162 * with a root element if needed.
164 static void addWMMenuEntryCallback(WMMenuEntry *aEntry)
166 WMMenuEntry *wm;
167 WMTreeNode *at;
169 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry)); /* this entry */
170 at = (WMTreeNode *)NULL; /* will be a child of this entry */
172 if (!menu) {
173 WMMenuEntry *root;
175 root = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
176 root->Name = "Applications";
177 root->CmdLine = NULL;
178 root->SubMenu = NULL;
179 root->Flags = 0;
180 menu = WMCreateTreeNode(root);
183 if (aEntry->SubMenu)
184 at = findPositionInMenu(aEntry->SubMenu);
186 if (!at)
187 at = menu;
189 wm->Flags = aEntry->Flags;
190 wm->Name = wstrdup(aEntry->Name);
191 wm->CmdLine = wstrdup(aEntry->CmdLine);
192 wm->SubMenu = NULL;
193 WMAddItemToTree(at, wm);
197 /* creates the proplist menu out of the abstract menu representation in `menu'.
199 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data)
201 WMMenuEntry *wm;
202 WMPropList *pl;
203 int pDepth, cDepth;
205 wm = (WMMenuEntry *)WMGetDataForTreeNode(aNode);
206 cDepth = WMGetTreeNodeDepth(aNode);
207 pDepth = *(int *)data;
209 if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
210 WMPropList *last, *but; /* merge the tail up to the current position */
211 int i;
212 for (i = pDepth - cDepth; i > 0; i--) {
213 last = WMPopFromArray(plMenuNodes);
214 but = WMPopFromArray(plMenuNodes);
215 WMAddToPLArray(but, last);
216 WMAddToArray(plMenuNodes, but);
220 if (!wm->CmdLine) { /* new submenu */
221 WMAddToArray(plMenuNodes, WMCreatePLArray(WMCreatePLString(wm->Name), NULL));
222 } else { /* new menu item */
223 pl = WMPopFromArray(plMenuNodes);
224 if (wm->Flags & F_RESTART_OTHER) { /* RESTART, somewm */
225 char buf[1024];
226 memset(buf, 0, sizeof(buf));
227 snprintf(buf, sizeof(buf), "%s %s", _("Restart"), wm->Name);
228 WMAddToPLArray(pl, WMCreatePLArray(
229 WMCreatePLString(buf),
230 WMCreatePLString("RESTART"),
231 WMCreatePLString(wm->CmdLine),
232 NULL)
234 } else if (wm->Flags & F_RESTART_SELF) {/* RESTART */
235 WMAddToPLArray(pl, WMCreatePLArray(
236 WMCreatePLString(_("Restart Window Maker")),
237 WMCreatePLString("RESTART"),
238 NULL)
240 } else if (wm->Flags & F_QUIT) { /* EXIT */
241 WMAddToPLArray(pl, WMCreatePLArray(
242 WMCreatePLString(_("Exit Window Maker")),
243 WMCreatePLString("EXIT"),
244 NULL)
246 } else { /* plain simple command */
247 char buf[1024];
249 memset(buf, 0, sizeof(buf));
250 if (wm->Flags & F_TERMINAL) /* XXX: quoting! */
251 snprintf(buf, sizeof(buf), "%s -e \"%s\"", terminal, wm->CmdLine);
252 else
253 snprintf(buf, sizeof(buf), "%s", wm->CmdLine);
255 WMAddToPLArray(pl, WMCreatePLArray(
256 WMCreatePLString(wm->Name),
257 WMCreatePLString("SHEXEC"),
258 WMCreatePLString(buf),
259 NULL)
262 WMAddToArray(plMenuNodes, pl);
265 *(int *)data = cDepth;
266 return;
269 /* sort the menu tree; callback for WMSortTree()
271 static int menuSortFunc(const void *left, const void *right)
273 WMMenuEntry *leftwm;
274 WMMenuEntry *rightwm;
276 leftwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)left);
277 rightwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)right);
279 /* submenus first */
280 if (!leftwm->CmdLine && rightwm->CmdLine)
281 return -1;
282 if (leftwm->CmdLine && !rightwm->CmdLine)
283 return 1;
285 /* the rest lexicographically */
286 return strcasecmp(leftwm->Name, rightwm->Name);
290 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
291 * creates `submenu' path (anchored to the root) along the way.
293 static WMTreeNode *findPositionInMenu(const char *submenu)
295 char *q;
296 WMMenuEntry *wm;
297 WMTreeNode *node, *pnode;
298 char buf[1024];
300 /* qualify submenu with "Applications/" (the root node) */
301 memset(buf, 0, sizeof(buf));
302 snprintf(buf, sizeof(buf), "Applications/%s", submenu);
304 /* start at the root */
305 node = menu;
307 q = strtok(buf, "/");
308 while (q) {
309 pnode = node;
310 node = WMFindInTreeWithDepthLimit(pnode, nodeFindSubMenuByNameFunc, q, 1);
311 if (!node) {
312 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
313 wm->Flags = 0;
314 wm->Name = wstrdup(q);
315 wm->CmdLine = NULL;
316 wm->SubMenu = NULL;
317 node = WMAddNodeToTree(pnode, WMCreateTreeNode(wm));
319 q = strtok(NULL, "/");
322 return node;
325 /* find node where Name = cdata and node is a submenu
327 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata)
329 WMMenuEntry *wm;
331 wm = (WMMenuEntry *)item;
333 if (wm->CmdLine) /* if it has a cmdline, it can't be a submenu */
334 return 0;
336 return strcmp(wm->Name, (const char *)cdata) == 0;