Fix segfault setting MenuFace pixmap style for menus.
[fvwm.git] / libs / FGettext.c
blobbd215341bd6377b190c7f888e5fb3ce60d373cff
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* ---------------------------- included header files ---------------------- */
18 #include "config.h"
20 #include <stdio.h>
22 #include "safemalloc.h"
23 #include "Strings.h"
24 #include "Parse.h"
25 #include "envvar.h"
26 #include "flist.h"
28 #include "FGettext.h"
29 #include "locale.h"
31 /* ---------------------------- local definitions -------------------------- */
33 /* ---------------------------- local macros ------------------------------- */
35 #define FGP_DOMAIN(l) ((FGettextPath *)l->object)->domain
36 #define FGP_DIR(l) ((FGettextPath *)l->object)->dir
38 /* ---------------------------- imports ------------------------------------ */
40 /* ---------------------------- included code files ------------------------ */
42 /* ---------------------------- local types -------------------------------- */
44 typedef struct
46 char *domain;
47 char *dir;
48 } FGettextPath;
50 /* ---------------------------- forward declarations ----------------------- */
52 /* ---------------------------- local variables ---------------------------- */
54 static int FGettextInitOk = 0;
55 static char *FGDefaultDir = NULL;
56 static char *FGDefaultDomain = NULL;
57 static const char *FGModuleName = NULL;
59 static flist *FGPathList = NULL;
60 static FGettextPath *FGLastPath = NULL;
62 /* ---------------------------- interface functions ------------------------ */
64 static
65 void fgettext_add_one_path(char *path, int position)
67 char *dir,*domain;
68 FGettextPath *tmp;
70 if (!HaveNLSSupport)
72 return;
75 domain = GetQuotedString(path, &dir, ";", NULL, NULL, NULL);
76 if (!dir || dir[0] == '\0' || dir[0] != '/')
78 if (dir)
80 free(dir);
82 CopyString(&dir, FGDefaultDir);
84 if (!domain || domain[0] == '\0')
86 domain = FGDefaultDomain;
89 tmp = (FGettextPath *)safemalloc(sizeof(FGettextPath));
90 tmp->dir = dir;
91 CopyString(&tmp->domain, domain);
93 FGPathList = flist_insert_obj(FGPathList, tmp, position);
96 static void fgettext_free_fgpath_list(void)
98 flist *l = FGPathList;
100 if (!HaveNLSSupport)
102 return;
105 while(l != NULL)
107 if (l->object)
109 if (FGP_DOMAIN(l))
111 free(FGP_DOMAIN(l));
113 if (FGP_DIR(l))
115 free(FGP_DIR(l));
117 free(l->object);
119 l = l->next;
121 FGLastPath = NULL;
122 FGPathList = flist_free_list(FGPathList);
125 /* ---------------------------- interface functions ------------------------ */
127 void FGettextInit(const char *domain, const char *dir, const char *module)
129 char *btd, *td;
131 if (!HaveNLSSupport)
133 return;
135 setlocale (LC_MESSAGES, "");
137 btd = bindtextdomain (domain, dir);
138 td = textdomain (domain);
139 if (!td || !btd)
141 fprintf(
142 stderr,"[%s][FGettextInit]: <<ERROR>> "
143 "gettext initialisation fail!\n",
144 module);
145 return;
147 FGModuleName = module;
148 CopyString(&FGDefaultDir, btd);
149 CopyString(&FGDefaultDomain, td);
150 FGLastPath = (FGettextPath *)safemalloc(sizeof(FGettextPath));
151 CopyString(&FGLastPath->domain, td);
152 CopyString(&FGLastPath->dir, btd);
153 FGPathList = flist_append_obj(FGPathList, FGLastPath);
154 FGettextInitOk = 1;
157 const char *FGettext(char *str)
159 flist *l = FGPathList;
160 const char *s, *dummy;
162 if (!HaveNLSSupport || !FGettextInitOk || FGPathList == NULL ||
163 str == NULL)
165 return str;
168 if (FGLastPath != l->object)
170 dummy = bindtextdomain (FGP_DOMAIN(l), FGP_DIR(l));
171 dummy = textdomain (FGP_DOMAIN(l));
172 FGLastPath = l->object;
174 s = gettext(str);
175 if (s != str)
177 return s;
179 l = l->next;
180 while(l != NULL)
182 dummy = bindtextdomain (FGP_DOMAIN(l), FGP_DIR(l));
183 dummy = textdomain (FGP_DOMAIN(l));
184 FGLastPath = l->object;
185 s = gettext(str);
186 if (s != str)
188 return s;
190 l = l->next;
192 return str;
195 char *FGettextCopy(char *str)
197 const char *trans;
198 char *r = NULL;
200 trans = FGettext(str);
201 if (trans != NULL)
203 CopyString(&r, trans);
205 return r;
208 void FGettextSetLocalePath(const char *path)
210 char *exp_path = NULL;
211 char *before = NULL;
212 char *after, *p, *str;
213 int count;
215 if (!HaveNLSSupport || !FGettextInitOk)
217 return;
220 FGLastPath = NULL;
222 if (path == NULL || path[0] == '\0')
224 fgettext_free_fgpath_list();
225 FGLastPath = (FGettextPath *)safemalloc(sizeof(FGettextPath));
226 CopyString(&FGLastPath->domain, FGDefaultDomain);
227 CopyString(&FGLastPath->dir, FGDefaultDir);
228 FGPathList = flist_append_obj(FGPathList, FGLastPath);
229 FGLastPath = NULL;
230 return;
233 exp_path = envDupExpand(path, 0);
235 if (StrEquals(exp_path,"None"))
237 fgettext_free_fgpath_list();
238 goto bail;
241 after = GetQuotedString(exp_path, &before, "+", NULL, NULL, NULL);
242 if ((after && strchr(after, '+')) || (before && strchr(before, '+')))
244 fprintf(
245 stderr,"[%s][SetLocalePath]: "
246 "To many '+' in locale path specification: %s\n",
247 FGModuleName, path);
248 goto bail;
250 if (!strchr(exp_path, '+'))
252 fgettext_free_fgpath_list();
254 while(after && *after)
256 after = GetQuotedString(after, &p, ":", NULL, NULL, NULL);
257 if (p && *p)
259 fgettext_add_one_path(p,-1);
261 if (p)
263 free(p);
266 count = 0;
267 str = before;
268 while (str && *str)
270 str = GetQuotedString(str, &p, ":", NULL, NULL, NULL);
271 if (p && *p)
273 fgettext_add_one_path(p,count);
274 count++;
276 if (p)
278 free(p);
281 bail:
282 if (before)
284 free(before);
286 if (exp_path)
288 free(exp_path);
293 void FGettextPrintLocalePath(int verbose)
295 flist *l = FGPathList;
297 if (!HaveNLSSupport || !FGettextInitOk)
299 return;
302 fprintf(stderr,"fvwm NLS gettext path:\n");
303 while(l != NULL)
305 fprintf(
306 stderr," dir: %s, domain: %s\n",
307 FGP_DIR(l), FGP_DOMAIN(l));
308 l = l->next;