Update Serbian translation from master branch
[wmaker-crm.git] / util / wmmenugen_misc.c
blob161a32e09bf429d21523a7d695014a169257f18c
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>
30 static const char *const terminals[] = {
31 "x-terminal-emulator", /* Debian wrapper to launch user's prefered X terminal */
32 "aterm", /* AfterStep's X terminal, which provides "transparency" */
33 "eterm", /* Enlightenment's terminal, designed for eye-candyness (and efficiency) */
34 "gnome-terminal", /* GNOME project's terminal */
35 "konsole", /* KDE project's terminals */
36 "kterm", /* a Multi-Lingual Terminal based on xterm, originally by Katsuya Sano */
37 "mlterm", /* a Multi-Lingual Terminal emulator written from scratch */
38 "rxvt", /* a slimmed-down xterm */
39 "mrxvt", /* rxvt with support for tabs amongst other things */
40 "pterm", /* terminal based on PuTTY, a popular SSH client for Windows */
41 "xterm", /* the standard terminal provided by the X Window System */
42 "dtterm" /* provided by CDE, a frequent Desktop Environment in proprietary UNIXs */
45 /* pick a terminal emulator by finding the first existing entry of `terminals'
46 * in $PATH. the returned pointer should be wfreed later.
47 * if $WMMENU_TERMINAL exists in the environment, it's value overrides this
48 * detection.
50 char *find_terminal_emulator(void)
52 char *path, *t;
53 int i;
55 t = getenv("WMMENU_TERMINAL");
56 if (t)
57 return wstrdup(t);
59 path = getenv("PATH");
60 if (!path)
61 return NULL;
63 for (i = 0; i < wlengthof(terminals); i++) {
64 t = wfindfile(path, terminals[i]);
65 if (t) {
66 wfree(t);
67 return wstrdup(terminals[i]);
71 return NULL;
74 /* tokenize `what' (LC_MESSAGES or LANG if `what' is NULL) in the form of
75 * `language[_territory][.codeset][@modifier]' into separate language, country,
76 * encoding, modifier components, which are allocated on demand and should be
77 * wfreed later. components that do not exist in `what' are set to NULL.
79 void parse_locale(const char *what, char **language, char **country, char **encoding, char **modifier)
81 char *e, *p;
83 *language = *country = *encoding = *modifier = NULL;
85 if (what == NULL) {
86 e = getenv("LC_MESSAGES");
87 if (e == NULL) {
88 e = getenv("LANG"); /* this violates the spec */
89 if (e == NULL)
90 return;
92 e = wstrdup(e);
93 } else {
94 e = wstrdup(what);
97 if (strlen(e) == 0 ||
98 strcmp(e, "POSIX") == 0 ||
99 strcmp(e, "C") == 0)
100 goto out;
102 p = strchr(e, '@');
103 if (p) {
104 *modifier = wstrdup(p + 1);
105 *p = '\0';
108 p = strchr(e, '.');
109 if (p) {
110 *encoding = wstrdup(p + 1);
111 *p = '\0';
114 p = strchr(e, '_');
115 if (p) {
116 *country = wstrdup(p + 1);
117 *p = '\0';
120 if (strlen(e) > 0)
121 *language = wstrdup(e);
123 out:
124 free(e);
125 return;
129 /* determine whether (first token of) given file is in $PATH
131 Bool fileInPath(const char *file)
133 char *p, *t;
134 static char *path = NULL;
136 if (!file || !*file)
137 return False;
139 /* if it's an absolute path spec, don't override the user.
140 * s/he might just know better.
142 if (*file == '/')
143 return True;
145 /* if it has a directory separator at random places,
146 * we might know better.
148 p = strchr(file, '/');
149 if (p)
150 return False;
152 if (!path) {
153 path = getenv("PATH");
154 if (!path)
155 return False;
158 p = wstrdup(file);
159 t = strpbrk(p, " \t");
160 if (t)
161 *t = '\0';
163 t = wfindfile(path, p);
164 wfree(p);
166 if (t) {
167 wfree(t);
168 return True;
171 return False;