File: l10n_read.c

package info (click to toggle)
xview 3.2p1.4-28.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 26,680 kB
  • ctags: 34,403
  • sloc: ansic: 241,397; yacc: 1,435; sh: 1,086; makefile: 148; lex: 76; perl: 54; asm: 50; cpp: 15
file content (303 lines) | stat: -rw-r--r-- 6,511 bytes parent folder | download | duplicates (7)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#ifndef lint
static char *sccsid = "@(#)l10n_read.c 1.6 92/03/10";
#endif

/*
 * l10n_read.c - Reader for l10n configuration file(s)
 */

#include	<sys/types.h>
#include	<sys/param.h>
#include	<locale.h>
#ifdef OW_I18N
#include	<widec.h>
#endif 
#include	<stdio.h>
#include	<string.h>
#include	"props.h"
#include	"l10n_props.h"


#define	MAX_LINE_LENGTH		256

#define	NAME_SEPARATOR		'='
#define	FIELD_SEPARATOR		'|'
#define	ITEM_SEPARATOR		';'


#ifndef __linux__
#ifndef ultrix
extern char	*malloc();
#endif
extern char	*calloc();
#endif


/*
 * FIX_ME! This routine must be sophisticated enough to allow more
 * flexible syntax in file.  Such as allow the white space between
 * each token.
 */
int
l10n_config_read(locale, file_name, a_list)
	char			*locale;
	char			*file_name;
	l10n_config_list_t	*a_list;
{
#ifdef OW_I18N
	register wchar_t	*p1, *p2;
#else
	register char		*p1, *p2;
#endif

	register l10n_config_list_t	*list;
	register l10n_config_list_item_t *item;
	l10n_config_list_item_t	*item_new;

	register FILE		*config_file;
	int			i;
#ifdef OW_I18N
	wchar_t			*dft;
#else
	char			*dft;
#endif
	char			*mbs;
	int			lineno;
	int			slotno;
	char			fullpath[MAXPATHLEN];
#ifdef OW_I18N
	wchar_t			line[MAX_LINE_LENGTH+1];
#else
	char			line[MAX_LINE_LENGTH+1];
#endif
	int			rcode = -1;


	/*  
	 * Find path for localization configuration files under
	 * $OPENWINHOME/share/locale/<locale>/props
	 */
#if 1
	/* martin-2.buck@student.uni-ulm.de */
	if (getenv("OPENWINHOME")) {
		sprintf(fullpath, "%s/share/locale/%s/props/%s",
			getenv("OPENWINHOME"), locale, file_name);
	} else {
#ifdef OPENWINHOME_DEFAULT
		sprintf(fullpath, "%s/share/locale/%s/props/%s",
			OPENWINHOME_DEFAULT, locale, file_name);
#else
		sprintf(fullpath, "%s/share/locale/%s/props/%s",
			"/usr/openwin", locale, file_name);
#endif
	}
#else
	sprintf(fullpath, "%s/share/locale/%s/props/%s",
		getenv("OPENWINHOME"), locale, file_name);
#endif

	if ((config_file = fopen(fullpath, "r")) == NULL)
	{
		perror(fullpath);
		goto fileerr_ret;
	}

	/* 
	 * Parse configuration file. 
	 */
#ifdef OW_I18N
	for (lineno = 1; fgetws(line, MAX_LINE_LENGTH, config_file) != NULL; lineno++)
#else
	for (lineno = 1; fgets(line, MAX_LINE_LENGTH, config_file) != NULL; lineno++)
#endif
	{
		p1 = line;
		if (*p1 == '#' || *p1 == '\n')
			continue;

		/*
		 * Pickup the category name.
		 */
#ifdef OW_I18N
		if ((p2 = wschr(p1, NAME_SEPARATOR)) == NULL)
#else
		if ((p2 = strchr(p1, NAME_SEPARATOR)) == NULL)
#endif
		{
			fprintf(stderr, (char *)LOCALIZE("Bad format in %s (line#%d): missing name separator '%c')\n"),
					fullpath, lineno, NAME_SEPARATOR);
			goto ret;
		}

		/*
		 * Looking for the category name in list...
		 */
		*p2 = 0;	/* Overwrite '=' with string terminator */
		for (list = a_list; list->name != NULL; list++)
#ifdef OW_I18N
			if (wscmp(list->name, p1) == 0)
#else
			if (strcmp(list->name, p1) == 0)
#endif 
				break;
		if (list->name == NULL)
		{
#ifdef OW_I18N
			fprintf(stderr, (char *)LOCALIZE("Bad format in %s (line#%d): Unknown category name [%ws]\n"),
					fullpath, lineno, p1);
#else
			fprintf(stderr, (char *)LOCALIZE("Bad format in %s (line#%d): Unknown category name [%s]\n"),
					fullpath, lineno, p1);
#endif
			goto ret;
		}
		p1 = ++p2;

		/*
		 * Picking up the default value.
		 */
#ifdef OW_I18N
		if ((p2 = wschr(p1, ITEM_SEPARATOR)) == NULL)
#else
		if ((p2 = strchr(p1, ITEM_SEPARATOR)) == NULL)
#endif
		{
			fprintf(stderr, (char *)LOCALIZE("Bad format in %s (line#%d): no default value\n"),
					fullpath, lineno);
			goto ret;
		}
		*p2++ = 0;
		dft = p1;	/* Keep it for now */
		p1 = p2;


		/*
		 * Picking up the items.
		 */
		item = NULL;
		for (slotno = 0; *p1 != '\n' && *p1 != 0; slotno++)
		{
			/*
			 * Allocate new space.
			 */
			item_new = (l10n_config_list_item_t *)
				calloc(1, sizeof (l10n_config_list_item_t));
			if (item == NULL)
				list->items = item_new;
			else
				item->next = item_new;
			item = item_new;

			/*
			 * Picking up the "value".
			 */
#ifdef OW_I18N
			if ((p2 = wschr(p1, FIELD_SEPARATOR)) == NULL)
#else
			if ((p2 = strchr(p1, FIELD_SEPARATOR)) == NULL)
#endif
			{
				fprintf(stderr, (char *)LOCALIZE("Bad format in %s (line#%d): missing field separator '%c'\n"),
					fullpath, lineno, FIELD_SEPARATOR);
				goto ret;
			}
			*p2++ = 0;
#ifdef OW_I18N
			item->value = malloc(wslen(p1) * sizeof(wchar_t) + 1);
			wstostr(item->value, p1);
#else
			item->value = malloc(strlen(p1) * sizeof(char) + 1);
			strcpy(item->value, p1);
#endif
#ifdef OW_I18N
			if (dft != NULL && wscmp(p1, dft) == 0)
#else
			if (dft != NULL && strcmp(p1, dft) == 0)
#endif
			{
				list->default_value = slotno;
				dft = NULL;
			}
			p1 = p2;


			/*
			 * Picking up the label.
			 */
#ifdef OW_I18N
			if ((p2 = wschr(p1, ITEM_SEPARATOR)) != NULL
			 || (p2 = wschr(p1, '\n')) != NULL)
#else
			if ((p2 = strchr(p1, ITEM_SEPARATOR)) != NULL
			 || (p2 = strchr(p1, '\n')) != NULL)
#endif
			{
				if (*p2 != ITEM_SEPARATOR)
					slotno = -1;
				*p2++ = 0;
			}
#ifdef OW_I18N
			if (list->convert_label != NULL)
				item->label = (*(list->convert_label))
							(item->value, p1);
			else
				item->label = wsdup(p1);
#else
			item->label = strdup(p1);

#endif
			if (slotno < 0)
				break;
			p1 = p2;
		}
		if (dft != NULL)
		{
#ifdef OW_I18N
			fprintf(stderr, LOCALIZE("Invalid default value in %s (line#%d): [%ws]\n"),
				fullpath, lineno, dft);
#else
			fprintf(stderr, (char *)LOCALIZE("Invalid default value in %s (line#%d): [%s]\n"),
				fullpath, lineno, dft);

#endif
			goto ret;
		}
		if (defaults_exists(list->class, list->class) == TRUE)
		{
			mbs = defaults_get_string(list->class, list->class, "");
			for (i = 0, item = list->items;
			     item != NULL;
			     i++, item = item->next)
				if (strcmp(item->value, mbs) == 0)
				{
					list->current_value = i;
					break;
				}
			if (item == NULL)
			{
#ifdef notdef
				/*
				 * This could happen all the time, if
				 * you switch the "Basic Setting" from
				 * English to Japanese.  So, I'm
				 * decided do it silently.
				 */
				fprintf(stderr, LOCALIZE("Bad configuration: %s should not be %s for basic setting %s\n"),
						list->class, mbs, file_name);
#endif
				list->current_value = list->default_value;
			}
		}
		else
			list->current_value = list->default_value;
		list->initial_value = list->current_value;
	}
	if (lineno > 1) /* Make sure null-file returns failure */
		rcode = 0;

ret:
	(void) fclose(config_file);
fileerr_ret:

	return rcode;
}