Bump to 1.49.3
[platform/upstream/help2man.git] / bindtextdomain.c
1 /*
2  * Nasty preload hack to allow message catalogs to be read from the build tree.
3  *
4  * export LD_PRELOAD=/usr/lib/help2man/bindtextdomain.so
5  * export TEXTDOMAIN=program
6  * export LOCALEDIR=${DESTDIR}/usr/share/locale
7  *
8  * Copyright (C) 2012 Free Software Foundation, Inc.
9  *
10  * Copying and distribution of this file, with or without modification,
11  * are permitted in any medium without royalty provided the copyright
12  * notice and this notice are preserved.  This file is offered as-is,
13  * without any warranty.
14  *
15  * Written by Brendan O'Dea <bod@debian.org>
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <dlfcn.h>
24
25 #define PRELOAD "bindtextdomain.so"
26
27 static void die(char const *msg)
28 {
29     fprintf(stderr, PRELOAD ": %s\n", msg);
30     exit(1);
31 }
32
33 static char *e_textdomain = 0;
34 static char *e_localedir = 0;
35 static char *(*r_textdomain)(char const *) = 0;
36 static char *(*r_bindtextdomain)(char const *, char const *) = 0;
37 static char *(*r_bind_textdomain_codeset)(char const *, char const *) = 0;
38
39 void setup()
40 {
41     static int done = 0;
42     if (done++)
43         return;
44
45     if (!(e_textdomain = getenv("TEXTDOMAIN")))
46         die("TEXTDOMAIN not set");
47
48     if (!(e_localedir = getenv("LOCALEDIR")))
49         die("LOCALEDIR not set");
50
51     if (!(r_textdomain = dlsym(RTLD_NEXT, "textdomain")))
52         die("can't find symbol \"textdomain\"");
53
54     if (!(r_bindtextdomain = dlsym(RTLD_NEXT, "bindtextdomain")))
55         die("can't find symbol \"bindtextdomain\"");
56
57     if (!(r_bind_textdomain_codeset = dlsym(RTLD_NEXT,
58                                             "bind_textdomain_codeset")))
59         die("can't find symbol \"bind_textdomain_codeset\"");
60 }
61
62 char *textdomain(char const *domainname)
63 {
64     char *r;
65     setup();
66     r = r_textdomain(domainname);
67     if (domainname && !strcmp(domainname, e_textdomain))
68         r_bindtextdomain(domainname, e_localedir);
69
70     return r;
71 }
72
73 char *bindtextdomain(char const *domainname, char const *dirname)
74 {
75     char const *dir = dirname;
76     setup();
77     if (domainname && !strcmp(domainname, e_textdomain))
78         dir = e_localedir;
79
80     return r_bindtextdomain(domainname, dir);
81 }
82
83 char *bind_textdomain_codeset(char const *domainname, char const *codeset)
84 {
85     char *r;
86     setup();
87     r = r_bind_textdomain_codeset(domainname, codeset);
88     if (domainname && !strcmp(domainname, e_textdomain))
89         r_bindtextdomain(domainname, e_localedir);
90
91     return r;
92 }