summaryrefslogtreecommitdiffstats
path: root/misc/tcl_gettext.c
blob: f399ca0e1157def936f780fbe97bc2a5c017acbe (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* tcl_gettext - Module implementing gettext interface for Tcl.
   Copyright (C) 1995, 1998 Free Software Foundation, Inc.
   Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, December 1995.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libintl.h>
#include <locale.h>
#include <string.h>

/* Data for Tcl interpreter interface.  */
#include "tcl.h"

/* Prototypes for local functions.  */
static int
tcl_gettext (ClientData client_data, Tcl_Interp *interp, int argc,
	     char *argv[]);
static int
tcl_textdomain (ClientData client_data, Tcl_Interp *interp, int argc,
		char *argv[]);
static int
tcl_bindtextdomain (ClientData client_data, Tcl_Interp *interp, int argc,
		    char *argv[]);


/* Initialization functions.  Called from the tclAppInit.c/tkAppInit.c
   or while the dynamic loading with Tcl7.x, x>= 5.  */
int
Gettext_Init (interp)
     Tcl_Interp *interp;
{
  Tcl_CreateCommand (interp, "gettext", tcl_gettext, (ClientData) 0,
		     (Tcl_CmdDeleteProc *) NULL);
  Tcl_CreateCommand (interp, "textdomain", tcl_textdomain, (ClientData) 0,
		     (Tcl_CmdDeleteProc *) NULL);
  Tcl_CreateCommand (interp, "bindtextdomain", tcl_bindtextdomain,
		     (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);

  return TCL_OK;
}


static int
tcl_gettext (client_data, interp, argc, argv)
     ClientData client_data;
     Tcl_Interp *interp;
     int argc;
     char *argv[];
{
  const char *domainname = NULL;
  int category = LC_MESSAGES;
  const char *msgid;

  /* The pointer which is assigned in the following statement might
     reference an invalid part of the address space.  But we don't use
     this value before we know the pointer is correct.  */
  msgid = argv[1];

  switch (argc)
    {
    case 4:
#ifdef LC_CTYPE
      if (strcmp (argv[3], "LC_CTYPE") == 0)
	category = LC_CTYPE;
      else
#endif
#ifdef LC_COLLATE
	if (strcmp (argv[3], "LC_COLLATE") == 0)
	  category = LC_COLLATE;
	else
#endif
#ifdef LC_MESSAGES
	  if (strcmp (argv[3], "LC_MESSAGES") == 0)
	    category = LC_MESSAGES;
	  else
#endif
#ifdef LC_MONETARY
	    if (strcmp (argv[3], "LC_MONETARY") == 0)
	      category = LC_MONETARY;
	    else
#endif
#ifdef LC_NUMERIC
	      if (strcmp (argv[3], "LC_NUMERIC") == 0)
		category = LC_NUMERIC;
	      else
#endif
#ifdef LC_TIME
		if (strcmp (argv[3], "LC_TIME") == 0)
		  category = LC_TIME;
		else
#endif
		  {
		    interp->result = gettext ("invalid third argument");
		    return TCL_ERROR;
		  }
      /* FALLTHROUGH */

    case 3:
      domainname = argv[1];
      msgid = argv[2];
      /* FALLTHROUGH */

    case 2:
      interp->result = dcgettext (domainname, msgid, category);
      break;

    default:
      interp->result = gettext ("wrong number of arguments");
      return TCL_ERROR;
    }

  return TCL_OK;
}


static int
tcl_textdomain (client_data, interp, argc, argv)
     ClientData client_data;
     Tcl_Interp *interp;
     int argc;
     char *argv[];
{
  if (argc != 2)
    {
      interp->result = gettext ("wrong number of arguments");
      return TCL_ERROR;
    }

  interp->result = textdomain (argv[1]);

  return TCL_OK;
}


static int
tcl_bindtextdomain (client_data, interp, argc, argv)
     ClientData client_data;
     Tcl_Interp *interp;
     int argc;
     char *argv[];
{
  if (argc != 3)
    {
      interp->result = gettext ("wrong number of arguments");
      return TCL_ERROR;
    }

  return bindtextdomain (argv[1], argv[2]) == NULL ? TCL_ERROR : TCL_OK;
}