summaryrefslogtreecommitdiff
path: root/bindtextdomain.c
blob: 755e9f1fe86cd894cb51c14da37cb49376ade74f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Nasty preload hack to allow message catalogs to be read from the build tree.
 *
 * export LD_PRELOAD=/usr/lib/help2man/bindtextdomain.so
 * export TEXTDOMAIN=program
 * export LOCALEDIR=${DESTDIR}/usr/share/locale
 *
 * Copyright (C) 2012 Free Software Foundation, Inc.
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.  This file is offered as-is,
 * without any warranty.
 *
 * Written by Brendan O'Dea <bod@debian.org>
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#define PRELOAD "bindtextdomain.so"

static void die(char const *msg)
{
    fprintf(stderr, PRELOAD ": %s\n", msg);
    exit(1);
}

static char *e_textdomain = 0;
static char *e_localedir = 0;
static char *(*r_textdomain)(char const *) = 0;
static char *(*r_bindtextdomain)(char const *, char const *) = 0;
static char *(*r_bind_textdomain_codeset)(char const *, char const *) = 0;

void setup()
{
    static int done = 0;
    if (done++)
        return;

    if (!(e_textdomain = getenv("TEXTDOMAIN")))
	die("TEXTDOMAIN not set");

    if (!(e_localedir = getenv("LOCALEDIR")))
	die("LOCALEDIR not set");

    if (!(r_textdomain = dlsym(RTLD_NEXT, "textdomain")))
	die("can't find symbol \"textdomain\"");

    if (!(r_bindtextdomain = dlsym(RTLD_NEXT, "bindtextdomain")))
	die("can't find symbol \"bindtextdomain\"");

    if (!(r_bind_textdomain_codeset = dlsym(RTLD_NEXT,
    					    "bind_textdomain_codeset")))
	die("can't find symbol \"bind_textdomain_codeset\"");
}

char *textdomain(char const *domainname)
{
    char *r;
    setup();
    r = r_textdomain(domainname);
    if (domainname && !strcmp(domainname, e_textdomain))
        r_bindtextdomain(domainname, e_localedir);

    return r;
}

char *bindtextdomain(char const *domainname, char const *dirname)
{
    char const *dir = dirname;
    setup();
    if (domainname && !strcmp(domainname, e_textdomain))
        dir = e_localedir;

    return r_bindtextdomain(domainname, dir);
}

char *bind_textdomain_codeset(char const *domainname, char const *codeset)
{
    char *r;
    setup();
    r = r_bind_textdomain_codeset(domainname, codeset);
    if (domainname && !strcmp(domainname, e_textdomain))
        r_bindtextdomain(domainname, e_localedir);

    return r;
}