WindowMaker: New function wGetWorkspaceNumber
[wmaker-crm.git] / util / wmmenugen_misc.c
blob694ef8bf825ae5d5bdc55f13d4fe9e346afddeb6
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * miscellaneous 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 <libgen.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include <WINGs/WUtil.h>
29 static char *terminals[] = {
30 "x-terminal-emulator", "aterm","eterm", "gnome-terminal", "konsole",
31 "kterm", "mlterm", "rxvt", "mrxvt", "pterm", "xterm", "dtterm",
32 NULL
35 /* pick a terminal emulator by finding the first existing entry of `terminals'
36 * in $PATH. the returned pointer should be wfreed later.
37 * if $WMMENU_TERMINAL exists in the environment, it's value overrides this
38 * detection.
40 char *find_terminal_emulator(void)
42 char *path, *t, *ret;
43 int i;
45 path = t = ret = NULL;
47 t = getenv("WMMENU_TERMINAL");
48 if (t) {
49 ret = wstrdup(t);
50 return ret;
53 path = getenv("PATH");
54 if (!path)
55 return NULL;
57 for (i = 0; terminals[i]; i++) {
58 t = wfindfile(path, terminals[i]);
59 if (t)
60 break;
63 if (t)
64 ret = wstrdup(basename(t));
65 else
66 ret = wstrdup(t);
68 wfree(t);
70 return ret;
73 /* tokenize `what' (LC_MESSAGES or LANG if `what' is NULL) in the form of
74 * `language[_territory][.codeset][@modifier]' into separate language, country,
75 * encoding, modifier components, which are allocated on demand and should be
76 * wfreed later. components that do not exist in `what' are set to NULL.
78 void parse_locale(const char *what, char **language, char **country, char **encoding, char **modifier)
80 char *e, *p;
82 *language = *country = *encoding = *modifier = NULL;
84 if (what == NULL) {
85 e = getenv("LC_MESSAGES");
86 if (e == NULL) {
87 e = getenv("LANG"); /* this violates the spec */
88 if (e == NULL)
89 return;
91 e = wstrdup(e);
92 } else {
93 e = wstrdup(what);
96 if (strlen(e) == 0 ||
97 strcmp(e, "POSIX") == 0 ||
98 strcmp(e, "C") == 0)
99 goto out;
101 p = strchr(e, '@');
102 if (p) {
103 *modifier = wstrdup(p + 1);
104 *p = '\0';
107 p = strchr(e, '.');
108 if (p) {
109 *encoding = wstrdup(p + 1);
110 *p = '\0';
113 p = strchr(e, '_');
114 if (p) {
115 *country = wstrdup(p + 1);
116 *p = '\0';
119 if (strlen(e) > 0)
120 *language = wstrdup(e);
122 out:
123 free(e);
124 return;
128 /* determine whether (first token of) given file is in $PATH
130 Bool fileInPath(const char *file)
132 char *p, *t;
133 static char *path = NULL;
135 if (!file || !*file)
136 return False;
138 /* if it's an absolute path spec, don't override the user.
139 * s/he might just know better.
141 if (*file == '/')
142 return True;
144 /* if it has a directory separator at random places,
145 * we might know better.
147 p = strchr(file, '/');
148 if (p)
149 return False;
151 if (!path) {
152 path = getenv("PATH");
153 if (!path)
154 return False;
157 p = wstrdup(file);
158 t = strpbrk(p, " \t");
159 if (t)
160 *t = '\0';
162 t = wfindfile(path, p);
163 wfree(p);
165 if (t) {
166 wfree(t);
167 return True;
170 return False;