[go: up one dir, main page]

File: language.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (186 lines) | stat: -rw-r--r-- 4,320 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *	cook - file construction tool
 *	Copyright (C) 1997 Peter Miller;
 *	All rights reserved.
 *
 *	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 of the License, 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, USA.
 *
 * MANIFEST: functions to manipulate languages
 */

#include <ac/locale.h>
#include <ac/libintl.h>
#include <ac/stdlib.h>

#include <env.h>
#include <error.h>
#include <language.h>
#include <libdir.h>
#include <progname.h>


enum state_ty
{
	state_uninitialized,
	state_C,
	state_human
};
typedef enum state_ty state_ty;

static state_ty state;


/*
 * NAME
 *	language_init - initialize language functions
 *
 * DESCRIPTION
 *	The language_init function must be called at the start of the
 *	program, to set the various locale features.
 *
 *	This function must be called after the setuid initialization.
 *	If you forget to call me, all bets are off.
 */

void
language_init()
{
	char	*lib;
	char	*package;

	if (state != state_uninitialized)
		return;
	state = state_C;

	/*
	 * Default the error message language to English if not set.
	 * Since we expect to be using GNU Gettext, only set the LANGUAGE
	 * environment variable.
	 */
	if (!getenv("LANGUAGE") && !getenv("LANG"))
		env_set("LANGUAGE", "en");

	/*
	 * Set the locale to the default (as defined by the environment
	 * variables) and set the message domain information.
	 */
	package = progname_get();
#ifdef DEBUG
	if (!package || !*package)
		fatal_raw("you must call progname_set before language_init (bug)");
#endif
	lib = getenv("COOK_MESSAGE_LIBRARY");
	if (!lib || !*lib)
		lib = library_directory_get();
#ifdef HAVE_SETLOCALE
#ifdef HAVE_GETTEXT
	setlocale(LC_ALL, "");
	bindtextdomain(package, lib);
	textdomain(package);
#endif /* HAVE_GETTEXT */

	/*
	 * set the main body of the program use use the C locale
	 */
	setlocale(LC_ALL, "C");
#endif /* HAVE_SETLOCALE */
}


/*
 * NAME
 *	language_human - set for human conversation
 *
 * DESCRIPTION
 *	The language_human function must be called to change the general
 *	mode over to the default locale (usually dictated by the LANG
 *	environment variable, et al).
 *
 *	The language_human and language_C functions MUST bracket human
 *	interactions, otherwise the mostly-english C locale will be
 *	used.  The default locale through-out the program is otherwise
 *	assumed to be C.
 */

void
language_human()
{
#ifdef DEBUG
	switch (state)
	{
	case state_uninitialized:
		fatal_raw("you must call language_init() in main (bug)");

	case state_human:
		fatal_raw("unbalanced language_human() call (bug)");

	case state_C:
		break;
	}
#endif
	state = state_human;
#ifdef HAVE_SETLOCALE
#ifdef HAVE_GETTEXT
	/*
	 * only need to flap the locale about like this
	 * if we are using the gettext function
	 */
	setlocale(LC_ALL, "");
#endif /* HAVE_GETTEXT */
#endif /* HAVE_SETLOCALE */
}


/*
 * NAME
 *	language_C - set for program conversation
 *
 * DESCRIPTION
 *	The language_C function must be called to restore the locale to
 *	C, so that all the non-human stuff will work.
 *
 *	The language_human and language_C functions MUST bracket human
 *	interactions, otherwise the mostly-english C locale will be
 *	used.  The default locale through-out the program is otherwise
 *	assumed to be C.
 */

void
language_C()
{
#ifdef DEBUG
	switch (state)
	{
	case state_uninitialized:
		fatal_raw("you must call language_init() in main (bug)");

	case state_C:
		fatal_raw("unbalanced language_C() call (bug)");

	case state_human:
		break;
	}
#endif
	state = state_C;
#ifdef HAVE_SETLOCALE
#ifdef HAVE_GETTEXT
	/*
	 * only need to flap the locale about like this
	 * if we are using the gettext function
	 */
	setlocale(LC_ALL, "C");
#endif /* HAVE_GETTEXT */
#endif /* HAVE_SETLOCALE */
}