mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-21 13:28:05 +01:00
wmmenugen: Add file name validator function
Add optional interface parsers may choose to implement that, when a directory is being scanned for files to parse, may, based on the file name, decide whether or not said file should be parsed. Signed-off-by: Tamas TEVESZ <ice@extreme.hu>
This commit is contained in:
committed by
Carlos R. Mafra
parent
5377ec8fba
commit
a23b72c32a
@@ -44,6 +44,7 @@ static int menuSortFunc(const void *left, const void *right);
|
|||||||
static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata);
|
static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata);
|
||||||
static WMTreeNode *findPositionInMenu(char *submenu);
|
static WMTreeNode *findPositionInMenu(char *submenu);
|
||||||
static void (*parse)(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
static void (*parse)(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
||||||
|
static Bool (*validateFilename)(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
|
||||||
|
|
||||||
static WMArray *plMenuNodes;
|
static WMArray *plMenuNodes;
|
||||||
char *terminal;
|
char *terminal;
|
||||||
@@ -59,6 +60,7 @@ int main(int argc, char **argv)
|
|||||||
plMenuNodes = WMCreateArray(8); /* grows on demand */
|
plMenuNodes = WMCreateArray(8); /* grows on demand */
|
||||||
menu = (WMTreeNode *)NULL;
|
menu = (WMTreeNode *)NULL;
|
||||||
parse = NULL;
|
parse = NULL;
|
||||||
|
validateFilename = NULL;
|
||||||
|
|
||||||
/* assemblePLMenuFunc passes this around */
|
/* assemblePLMenuFunc passes this around */
|
||||||
previousDepth = (int *)wmalloc(sizeof(int));
|
previousDepth = (int *)wmalloc(sizeof(int));
|
||||||
@@ -90,6 +92,7 @@ int main(int argc, char **argv)
|
|||||||
fputs("Using parser \"wmconfig\"\n", stderr);
|
fputs("Using parser \"wmconfig\"\n", stderr);
|
||||||
#endif
|
#endif
|
||||||
parse = &parse_wmconfig;
|
parse = &parse_wmconfig;
|
||||||
|
validateFilename = &wmconfig_validate_file;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%s: Unknown parser \"%s\"\n", __progname, argv[i] + 8);
|
fprintf(stderr, "%s: Unknown parser \"%s\"\n", __progname, argv[i] + 8);
|
||||||
}
|
}
|
||||||
@@ -142,6 +145,10 @@ static int dirParseFunc(const char *filename, const struct stat *st, int tflags,
|
|||||||
(void)tflags;
|
(void)tflags;
|
||||||
(void)ftw;
|
(void)ftw;
|
||||||
|
|
||||||
|
if (validateFilename &&
|
||||||
|
!validateFilename(filename, st, tflags, ftw))
|
||||||
|
return 0;
|
||||||
|
|
||||||
parse(filename, addWMMenuEntryCallback);
|
parse(filename, addWMMenuEntryCallback);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -196,7 +203,7 @@ static void assemblePLMenuFunc(WMTreeNode *aNode, void *data)
|
|||||||
pDepth = *(int *)data;
|
pDepth = *(int *)data;
|
||||||
|
|
||||||
if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
|
if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
|
||||||
WMPropList *last, *but; /* merge the differences */
|
WMPropList *last, *but; /* merge the tail up to the current position */
|
||||||
int i;
|
int i;
|
||||||
for (i = pDepth - cDepth; i > 0; i--) {
|
for (i = pDepth - cDepth; i > 0; i--) {
|
||||||
last = WMPopFromArray(plMenuNodes);
|
last = WMPopFromArray(plMenuNodes);
|
||||||
|
|||||||
@@ -57,3 +57,5 @@ Bool fileInPath(const char *file);
|
|||||||
*/
|
*/
|
||||||
void parse_xdg(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
void parse_xdg(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
||||||
void parse_wmconfig(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
void parse_wmconfig(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
|
||||||
|
Bool wmconfig_validate_file(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
|
||||||
|
|
||||||
|
|||||||
@@ -20,14 +20,24 @@
|
|||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if __GLIBC__ && \
|
||||||
|
(_XOPEN_SOURCE && _XOPEN_SOURCE < 500) || \
|
||||||
|
!_XOPEN_SOURCE
|
||||||
|
#define _XOPEN_SOURCE 500 /* nftw */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <ftw.h>
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#endif
|
#endif
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "wmmenugen.h"
|
#include "wmmenugen.h"
|
||||||
|
|
||||||
@@ -129,6 +139,42 @@ void parse_wmconfig(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* an example to illustrate validateFilename.
|
||||||
|
* with wmconfig, no special handling is needed
|
||||||
|
*/
|
||||||
|
Bool wmconfig_validate_file(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
|
||||||
|
{
|
||||||
|
(void)filename;
|
||||||
|
(void)st;
|
||||||
|
(void)tflags;
|
||||||
|
(void)ftw;
|
||||||
|
|
||||||
|
return True;
|
||||||
|
#if 0 /* not dead code, example */
|
||||||
|
|
||||||
|
/* or we could have gone intro extremes */
|
||||||
|
char *base_name;
|
||||||
|
Bool ret;
|
||||||
|
|
||||||
|
(void)tflags;
|
||||||
|
|
||||||
|
base_name = wstrdup(filename + ftw->base);
|
||||||
|
ret = True;
|
||||||
|
|
||||||
|
if (!S_ISREG(st->st_mode) || /* not a regular file */
|
||||||
|
(st->st_uid != 0 && st->st_uid != getuid()) || /* bad guy injected this file */
|
||||||
|
strpbrk(base_name, ".") || /* wmconfig typically has no extension */
|
||||||
|
st->st_size >= 128 * 131072 || /* noone writes wmconfig files > 128K */
|
||||||
|
st->st_size == 0 || /* nor empty ones */
|
||||||
|
ftw->level > 16) /* how did we get this deep? */
|
||||||
|
ret = False;
|
||||||
|
|
||||||
|
wfree(base_name);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/* get a line allocating label, key and value as necessary */
|
/* get a line allocating label, key and value as necessary */
|
||||||
static void parse_wmconfig_line(char **label, char **key, char **value, char *line)
|
static void parse_wmconfig_line(char **label, char **key, char **value, char *line)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,7 +34,17 @@
|
|||||||
* Basic validation of the .desktop file is done.
|
* Basic validation of the .desktop file is done.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if __GLIBC__ && \
|
||||||
|
(_XOPEN_SOURCE && _XOPEN_SOURCE < 500) || \
|
||||||
|
!_XOPEN_SOURCE
|
||||||
|
#define _XOPEN_SOURCE 500 /* nftw */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <ftw.h>
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user