1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin/*
23da2e3ebchin * generate <lc.h> implementation tables from lc.tab
24da2e3ebchin * this must make it through vanilla cc with no -last
25da2e3ebchin *
26da2e3ebchin *	# comment
27da2e3ebchin *	:charset:
28da2e3ebchin *		code	name	ms-codepage
29da2e3ebchin *	:language:
30da2e3ebchin *		code	name	alt1|alt2...	charset|... attr1|attr2|...
31da2e3ebchin *		...
32da2e3ebchin *	:territory:
33da2e3ebchin *		code	name	lang1|lang2...
34da2e3ebchin *	:abbreviation:
35da2e3ebchin */
36da2e3ebchin
37da2e3ebchin#include <stdio.h>
38da2e3ebchin#include <ctype.h>
39da2e3ebchin#ifdef __STDC__
40da2e3ebchin#include <stdlib.h>
41da2e3ebchin#include <string.h>
42da2e3ebchin#endif
43da2e3ebchin
44da2e3ebchintypedef struct Link_s
45da2e3ebchin{
46da2e3ebchin	struct Link_s*		next;
47da2e3ebchin	char*			code;
48da2e3ebchin	int			index;
49da2e3ebchin} Link_t;
50da2e3ebchin
51da2e3ebchintypedef struct Table_s
52da2e3ebchin{
53da2e3ebchin	Link_t*			root;
54da2e3ebchin	int			count;
55da2e3ebchin} Table_t;
56da2e3ebchin
57da2e3ebchintypedef struct Abbreviation_s
58da2e3ebchin{
59da2e3ebchin	Link_t			link;
60da2e3ebchin	char*			value;
61da2e3ebchin} Abbreviation_t;
62da2e3ebchin
63da2e3ebchintypedef struct Attribute_s
64da2e3ebchin{
65da2e3ebchin	Link_t			link;
66da2e3ebchin} Attribute_t;
67da2e3ebchin
68da2e3ebchintypedef struct Attribute_list_s
69da2e3ebchin{
70da2e3ebchin	struct Attribute_list_s*next;
71da2e3ebchin	Attribute_t*		attribute;
72da2e3ebchin} Attribute_list_t;
73da2e3ebchin
74da2e3ebchintypedef struct Charset_s
75da2e3ebchin{
76da2e3ebchin	Link_t			link;
77da2e3ebchin	char*			alternates;
78da2e3ebchin	char*			ms;
79da2e3ebchin} Charset_t;
80da2e3ebchin
81da2e3ebchintypedef struct Language_s
82da2e3ebchin{
83da2e3ebchin	Link_t			link;
84da2e3ebchin	char*			name;
85da2e3ebchin	char*			alternates;
86da2e3ebchin	Charset_t*		charset;
87da2e3ebchin	Attribute_list_t*	attributes;
88da2e3ebchin} Language_t;
89da2e3ebchin
90da2e3ebchintypedef struct Language_list_s
91da2e3ebchin{
92da2e3ebchin	struct Language_list_s*	next;
93da2e3ebchin	Language_t*		language;
94da2e3ebchin} Language_list_t;
95da2e3ebchin
96da2e3ebchintypedef struct Territory_s
97da2e3ebchin{
98da2e3ebchin	Link_t			link;
99da2e3ebchin	char*			name;
100da2e3ebchin	Language_list_t*	languages;
101da2e3ebchin	int			primary;
102da2e3ebchin	int			index;
103da2e3ebchin} Territory_t;
104da2e3ebchin
105da2e3ebchintypedef struct Map_s
106da2e3ebchin{
107da2e3ebchin	Link_t			link;
108da2e3ebchin	Language_t*		language;
109da2e3ebchin	Territory_t*		territory;
110da2e3ebchin	Charset_t*		charset;
111da2e3ebchin	Attribute_t*		attribute;
112da2e3ebchin} Map_t;
113da2e3ebchin
114da2e3ebchinstatic struct State_s
115da2e3ebchin{
116da2e3ebchin	Table_t			attribute;
117da2e3ebchin	Table_t			charset;
118da2e3ebchin	Table_t			language;
119da2e3ebchin	Table_t			territory;
120da2e3ebchin	Table_t			map;
121da2e3ebchin} state;
122da2e3ebchin
123da2e3ebchin#define INIT		0
124da2e3ebchin#define CHARSET		1
125da2e3ebchin#define LANGUAGE	2
126da2e3ebchin#define TERRITORY	3
127da2e3ebchin#define MAP		4
128da2e3ebchin
129da2e3ebchin#define elementsof(x)	(sizeof(x)/sizeof(x[0]))
130da2e3ebchin#define newof(p,t,n,x)	((t*)malloc(sizeof(t)*(n)+(x)))
131da2e3ebchin
132da2e3ebchinstatic Link_t*
133da2e3ebchin#if defined(__STDC__) || defined(__cplusplus)
134da2e3ebchinenter(register Table_t* tab, register Link_t* v)
135da2e3ebchin#else
136da2e3ebchinenter(tab, v)
137da2e3ebchinregister Table_t*	tab;
138da2e3ebchinregister Link_t*	v;
139da2e3ebchin#endif
140da2e3ebchin{
141da2e3ebchin	register Link_t*	x;
142da2e3ebchin	register Link_t*	p;
143da2e3ebchin
144da2e3ebchin	for (p = 0, x = tab->root; x; p = x, x = x->next)
145da2e3ebchin		if (!strcmp(x->code, v->code))
146da2e3ebchin			return x;
147da2e3ebchin	if (p)
148da2e3ebchin		p->next = v;
149da2e3ebchin	else
150da2e3ebchin		tab->root = v;
151da2e3ebchin	v->next = 0;
152da2e3ebchin	v->index = tab->count++;
153da2e3ebchin	return v;
154da2e3ebchin}
155da2e3ebchin
156da2e3ebchinstatic Link_t*
157da2e3ebchin#if defined(__STDC__) || defined(__cplusplus)
158da2e3ebchinlookup(register Table_t* tab, register char* s)
159da2e3ebchin#else
160da2e3ebchinlookup(tab, s)
161da2e3ebchinregister Table_t*	tab;
162da2e3ebchinregister char*		s;
163da2e3ebchin#endif
164da2e3ebchin{
165da2e3ebchin	register Link_t*	x;
166da2e3ebchin
167da2e3ebchin	for (x = tab->root; x; x = x->next)
168da2e3ebchin		if (!strcmp(x->code, s))
169da2e3ebchin			return x;
170da2e3ebchin	return 0;
171da2e3ebchin}
172da2e3ebchin
173da2e3ebchinstatic char*
174da2e3ebchin#if defined(__STDC__) || defined(__cplusplus)
175da2e3ebchincopy(char** p, register char* f)
176da2e3ebchin#else
177da2e3ebchincopy(p, f)
178da2e3ebchinchar**		p;
179da2e3ebchinregister char*	f;
180da2e3ebchin#endif
181da2e3ebchin{
182da2e3ebchin	register char*	t;
183da2e3ebchin	char*		b;
184da2e3ebchin
185da2e3ebchin	if (!f)
186da2e3ebchin		return 0;
187da2e3ebchin	b = t = *p;
188da2e3ebchin	while (*t++ = *f++);
189da2e3ebchin	*p = t;
190da2e3ebchin	return b;
191da2e3ebchin}
192da2e3ebchin
193da2e3ebchinstatic void
194da2e3ebchin#if defined(__STDC__) || defined(__cplusplus)
195da2e3ebchinmacro(FILE* f, char* p1, char* p2, char* p3)
196da2e3ebchin#else
197da2e3ebchinmacro(f, p1, p2, p3)
198da2e3ebchinFILE*		f;
199da2e3ebchinchar*		p1;
200da2e3ebchinchar*		p2;
201da2e3ebchinchar*		p3;
202da2e3ebchin#endif
203da2e3ebchin{
204da2e3ebchin	register int	c;
205da2e3ebchin	register char*	s;
206da2e3ebchin	register char*	b;
207da2e3ebchin	register char*	e;
208da2e3ebchin	int		i;
209da2e3ebchin	int		m;
210da2e3ebchin	int		n;
211da2e3ebchin	char*		part[4];
212da2e3ebchin	char		buf[128];
213da2e3ebchin
214da2e3ebchin	part[0] = p1;
215da2e3ebchin	part[1] = p2;
216da2e3ebchin	part[2] = p3;
217da2e3ebchin	part[3] = 0;
218da2e3ebchin	n = 0;
219da2e3ebchin	fprintf(f, "\n");
220da2e3ebchin	do
221da2e3ebchin	{
222da2e3ebchin		i = m = 0;
223da2e3ebchin		b = buf;
224da2e3ebchin		e = &buf[sizeof(buf)-1];
225da2e3ebchin		while (b < e)
226da2e3ebchin		{
227da2e3ebchin			if (!(s = part[i++]))
228da2e3ebchin				break;
229da2e3ebchin			if (i > 1)
230da2e3ebchin				*b++ = '_';
231da2e3ebchin			while ((c = *s++) && b < e)
232da2e3ebchin			{
233da2e3ebchin				if (c == '|')
234da2e3ebchin				{
235da2e3ebchin					part[i-1] = s;
236da2e3ebchin					m = 1;
237da2e3ebchin					break;
238da2e3ebchin				}
239da2e3ebchin				else if (islower(c))
240da2e3ebchin					c = toupper(c);
241da2e3ebchin				else if (!isalnum(c))
242da2e3ebchin					c = '_';
243da2e3ebchin				*b++ = c;
244da2e3ebchin			}
245da2e3ebchin		}
246da2e3ebchin		*b = 0;
247da2e3ebchin		fprintf(f, "#ifdef %s\n%s,\n#else\n", buf, buf);
248da2e3ebchin		n++;
249da2e3ebchin	} while (m);
250da2e3ebchin	fprintf(f, "0,\n");
251da2e3ebchin	while (n-- > 0)
252da2e3ebchin		fprintf(f, "#endif\n");
253da2e3ebchin}
254da2e3ebchin
255da2e3ebchin#if defined(__STDC__) || defined(__cplusplus)
256da2e3ebchinint
257da2e3ebchinmain(int argc, char** argv)
258da2e3ebchin#else
259da2e3ebchinint
260da2e3ebchinmain(argc, argv)
261da2e3ebchinint		argc;
262da2e3ebchinchar**		argv;
263da2e3ebchin#endif
264da2e3ebchin{
265da2e3ebchin	register char*		s;
266da2e3ebchin	register char**		vp;
267da2e3ebchin	register char**		ve;
268da2e3ebchin	Attribute_t*		ap;
269da2e3ebchin	Attribute_list_t*	al;
270da2e3ebchin	Attribute_list_t*	az;
271da2e3ebchin	Charset_t*		cp;
272da2e3ebchin	Territory_t*		tp;
273da2e3ebchin	Language_t*		lp;
274da2e3ebchin	Language_list_t*	ll;
275da2e3ebchin	Language_list_t*	lz;
276da2e3ebchin	Map_t*			mp;
277da2e3ebchin	char*			b;
278da2e3ebchin	char*			f;
279da2e3ebchin	char*			command;
280da2e3ebchin	char*			hdr;
281da2e3ebchin	char*			lib;
282da2e3ebchin	FILE*			hf;
283da2e3ebchin	FILE*			lf;
284da2e3ebchin	int			c;
285da2e3ebchin	int			i;
286da2e3ebchin	int			line;
287da2e3ebchin	int			type;
288da2e3ebchin	int			language_attribute_max;
289da2e3ebchin	int			territory_language_max;
290da2e3ebchin	char*			arg[5];
291da2e3ebchin	char			buf[1024];
292da2e3ebchin
293da2e3ebchin	command = *argv++;
294da2e3ebchin	line = 0;
295da2e3ebchin	if (!(hdr = *argv++) || !(lib = *argv++) || *argv)
296da2e3ebchin	{
2977c2fbfbApril Chin		fprintf(stderr, "%s: { hdr lib tab } arguments expected\n", command);
298da2e3ebchin		return 1;
299da2e3ebchin	}
300da2e3ebchin	if (!(hf = fopen(hdr, "w")))
301da2e3ebchin	{
302da2e3ebchin		fprintf(stderr, "%s: %s: cannot write\n", command, hdr);
303da2e3ebchin		return 1;
304da2e3ebchin	}
305da2e3ebchin	if (!(lf = fopen(lib, "w")))
306da2e3ebchin	{
307da2e3ebchin		fprintf(stderr, "%s: %s: cannot write\n", command, lib);
308da2e3ebchin		return 1;
309da2e3ebchin	}
310da2e3ebchin	type = 0;
311da2e3ebchin	language_attribute_max = 0;
312da2e3ebchin	territory_language_max = 0;
313da2e3ebchin	state.language.count = 2;
314da2e3ebchin	state.territory.count = 2;
315da2e3ebchin	ve = &arg[elementsof(arg)];
316da2e3ebchin	fprintf(hf, "/* : : generated by %s : : */\n", command);
317da2e3ebchin	fprintf(hf, "#pragma prototyped\n");
318da2e3ebchin	fprintf(hf, "\n");
319da2e3ebchin	fprintf(hf, "#ifndef _LC_H\n");
320da2e3ebchin	fprintf(hf, "#define _LC_H\t\t\t1\n");
321da2e3ebchin	fprintf(hf, "\n");
322da2e3ebchin	fprintf(hf, "#include <ast.h>\n");
323da2e3ebchin	fprintf(hf, "\n");
324da2e3ebchin	fprintf(hf, "#define LC_abbreviated\t\t0x00001\n");
325da2e3ebchin	fprintf(hf, "#define LC_checked\t\t0x00002\n");
32634f9b3eRoland Mainz	fprintf(hf, "#define LC_debug\t\t0x00004\n");
32734f9b3eRoland Mainz	fprintf(hf, "#define LC_default\t\t0x00008\n");
32834f9b3eRoland Mainz	fprintf(hf, "#define LC_defined\t\t0x00010\n");
329da2e3ebchin	fprintf(hf, "#define LC_local\t\t0x00020\n");
330da2e3ebchin	fprintf(hf, "#define LC_primary\t\t0x00040\n");
331da2e3ebchin	fprintf(hf, "#define LC_qualified\t\t0x00080\n");
332da2e3ebchin	fprintf(hf, "#define LC_undefined\t\t0x00100\n");
33334f9b3eRoland Mainz	fprintf(hf, "#define LC_utf8\t\t\t0x00200\n");
33434f9b3eRoland Mainz	fprintf(hf, "#define LC_verbose\t\t0x00400\n");
3353e14f97Roger A. Faulkner	fprintf(hf, "#define LC_setlocale\t\t\t0x10000\n");
3363e14f97Roger A. Faulkner	fprintf(hf, "#define LC_setenv\t\t\t0x20000\n");
3373e14f97Roger A. Faulkner	fprintf(hf, "#define LC_user\t\t\t0x40000\n");
338da2e3ebchin	fprintf(lf, "/* : : generated by %s : : */\n", command);
3397c2fbfbApril Chin	fprintf(lf, "\n");
3407c2fbfbApril Chin	fprintf(lf, "#include \"lclib.h\"\n");
3417c2fbfbApril Chin	fprintf(lf, "#include \"lclang.h\"\n");
3427c2fbfbApril Chin	fprintf(lf, "\n");
343da2e3ebchin	while (s = fgets(buf, sizeof(buf), stdin))
344da2e3ebchin	{
345da2e3ebchin		line++;
346da2e3ebchin		while (isspace(*s))
347da2e3ebchin			s++;
348da2e3ebchin		if (!*s || *s == '#')
349da2e3ebchin			continue;
350da2e3ebchin		b = s;
351da2e3ebchin		vp = arg;
352da2e3ebchin		for (;;)
353da2e3ebchin		{
354da2e3ebchin			for (*vp++ = s; *s && !isspace(*s); s++);
355da2e3ebchin			if (!*s)
356da2e3ebchin				break;
357da2e3ebchin			for (*s++ = 0; isspace(*s); s++);
358da2e3ebchin			if (!strcmp(*(vp - 1), "-"))
359da2e3ebchin				*(vp - 1) = 0;
360da2e3ebchin			if (!*s || vp >= ve)
361da2e3ebchin				break;
362da2e3ebchin		}
363da2e3ebchin		while (vp < ve)
364da2e3ebchin			*vp++ = 0;
365da2e3ebchin		if (*arg[0] == ':')
366da2e3ebchin		{
367da2e3ebchin			if (!strcmp(arg[0], ":map:"))
368da2e3ebchin			{
369da2e3ebchin				if (type != TERRITORY)
370da2e3ebchin				{
371da2e3ebchin					fprintf(stderr, "%s: %d: %s: must be specified after :territory:\n", command, line, arg[0]);
372da2e3ebchin					return 1;
373da2e3ebchin				}
374da2e3ebchin				type = MAP;
375da2e3ebchin				continue;
376da2e3ebchin			}
377da2e3ebchin			else if (!strcmp(arg[0], ":charset:"))
378da2e3ebchin			{
379da2e3ebchin				if (type != INIT)
380da2e3ebchin				{
381da2e3ebchin					fprintf(stderr, "%s: %d: %s must be specified first\n", command, line, arg[0]);
382da2e3ebchin					return 1;
383da2e3ebchin				}
384da2e3ebchin				type = CHARSET;
385da2e3ebchin				continue;
386da2e3ebchin			}
387da2e3ebchin			else if (!strcmp(arg[0], ":territory:"))
388da2e3ebchin			{
389da2e3ebchin				if (type != LANGUAGE)
390da2e3ebchin				{
391da2e3ebchin					fprintf(stderr, "%s: %d: %s: must be specified after :language:\n", command, line, arg[0]);
392da2e3ebchin					return 1;
393da2e3ebchin				}
394da2e3ebchin				type = TERRITORY;
395da2e3ebchin				continue;
396da2e3ebchin			}
397da2e3ebchin			else if (!strcmp(arg[0], ":language:"))
398da2e3ebchin			{
399da2e3ebchin				if (type != CHARSET)
400da2e3ebchin				{
401da2e3ebchin					fprintf(stderr, "%s: %d: %s must be specified after :charset:\n", command, line, arg[0]);
402da2e3ebchin					return 1;
403da2e3ebchin				}
404da2e3ebchin				type = LANGUAGE;
405da2e3ebchin				continue;
406da2e3ebchin			}
407da2e3ebchin			else
408da2e3ebchin			{
409da2e3ebchin				fprintf(stderr, "%s: %d: %s invalid\n", command, line, arg[0]);
410da2e3ebchin				return 1;
411da2e3ebchin			}
412da2e3ebchin		}
413da2e3ebchin		if (!arg[1])
414da2e3ebchin		{
415da2e3ebchin			fprintf(stderr, "%s: %d: at least two arguments expected\n", command, line);
416da2e3ebchin			return 1;
417da2e3ebchin		}
418da2e3ebchin		switch (type)
419da2e3ebchin		{
420da2e3ebchin		case CHARSET:
421da2e3ebchin			if (!(cp = newof(0, Charset_t, 1, s - b + 1)))
422da2e3ebchin			{
423da2e3ebchin				fprintf(stderr, "%s: %d: out of space\n", command, line);
424da2e3ebchin				return 1;
425da2e3ebchin			}
426da2e3ebchin			b = (char*)(cp + 1);
427da2e3ebchin			cp->link.code = copy(&b, arg[0]);
428da2e3ebchin			cp->alternates = copy(&b, arg[1]);
429da2e3ebchin			cp->ms = copy(&b, arg[2]);
430da2e3ebchin			if (cp != (Charset_t*)enter(&state.charset, (Link_t*)cp))
431da2e3ebchin			{
432da2e3ebchin				fprintf(stderr, "%s: %d: %s: duplicate charset\n", command, line, cp->link.code);
433da2e3ebchin				return 1;
434da2e3ebchin			}
435da2e3ebchin			break;
436da2e3ebchin		case TERRITORY:
437da2e3ebchin			if (!(tp = newof(0, Territory_t, 1, s - b + 1)))
438da2e3ebchin			{
439da2e3ebchin				fprintf(stderr, "%s: %d: out of space\n", command, line);
440da2e3ebchin				return 1;
441da2e3ebchin			}
442da2e3ebchin			b = (char*)(tp + 1);
443da2e3ebchin			tp->link.code = copy(&b, arg[0]);
444da2e3ebchin			tp->name = copy(&b, arg[1]);
445da2e3ebchin			tp->languages = 0;
446da2e3ebchin			if (s = copy(&b, arg[2]))
447da2e3ebchin			{
448da2e3ebchin				i = 0;
449da2e3ebchin				while (*(b = s))
450da2e3ebchin				{
451da2e3ebchin					for (; *s && *s != ':' && *s != '|'; s++);
452da2e3ebchin					if (c = *s)
453da2e3ebchin						*s++ = 0;
454da2e3ebchin					if (!(lp = (Language_t*)lookup(&state.language, b)))
455da2e3ebchin					{
456da2e3ebchin						fprintf(stderr, "%s: %d: %s: unknown language\n", command, line, b);
457da2e3ebchin						return 1;
458da2e3ebchin					}
459da2e3ebchin					if (!(ll = newof(0, Language_list_t, 1, 0)))
460da2e3ebchin					{
461da2e3ebchin						fprintf(stderr, "%s: %d: out of space\n", command, line);
462da2e3ebchin						return 1;
463da2e3ebchin					}
464da2e3ebchin					if (!tp->languages)
465da2e3ebchin						tp->languages = ll;
466da2e3ebchin					else
467da2e3ebchin						lz->next = ll;
468da2e3ebchin					lz = ll;
469da2e3ebchin					ll->language = lp;
470da2e3ebchin					ll->next = 0;
471da2e3ebchin					i++;
472da2e3ebchin					if (c == ':')
473da2e3ebchin					{
474da2e3ebchin						for (b = s; *s && *s != '|'; s++);
475da2e3ebchin						if (*s)
476da2e3ebchin							*s++ = 0;
477da2e3ebchin						if (!strcmp(b, "primary"))
478da2e3ebchin							tp->primary = 1;
479da2e3ebchin					}
480da2e3ebchin				}
481da2e3ebchin				if (territory_language_max < i)
482da2e3ebchin					territory_language_max = i;
483da2e3ebchin			}
484da2e3ebchin			if (tp != (Territory_t*)enter(&state.territory, (Link_t*)tp))
485da2e3ebchin			{
486da2e3ebchin				fprintf(stderr, "%s: %d: %s: duplicate territory\n", command, line, tp->link.code);
487da2e3ebchin				return 1;
488da2e3ebchin			}
489da2e3ebchin			break;
490da2e3ebchin		case LANGUAGE:
491da2e3ebchin			if (!(lp = newof(0, Language_t, 1, s - b + 1)))
492da2e3ebchin			{
493da2e3ebchin				fprintf(stderr, "%s: %d: out of space\n", command, line);
494da2e3ebchin				return 1;
495da2e3ebchin			}
496da2e3ebchin			b = (char*)(lp + 1);
497da2e3ebchin			lp->link.code = copy(&b, arg[0]);
498da2e3ebchin			lp->name = copy(&b, arg[1]);
499da2e3ebchin			lp->alternates = copy(&b, arg[2]);
500da2e3ebchin			if (!arg[3])
501da2e3ebchin				lp->charset = 0;
502da2e3ebchin			else if (!(lp->charset = (Charset_t*)lookup(&state.charset, arg[3])))
503da2e3ebchin			{
504da2e3ebchin				fprintf(stderr, "%s: %d: %s: unknown charset\n", command, line, arg[3]);
505da2e3ebchin				return 1;
506da2e3ebchin			}
507da2e3ebchin			lp->attributes = 0;
508da2e3ebchin			if (s = copy(&b, arg[4]))
509da2e3ebchin			{
510da2e3ebchin				i = 0;
5117c2fbfbApril Chin				fprintf(lf, "\nconst Lc_attribute_t attribute_%s[] =\n{\n", lp->link.code);
512da2e3ebchin				while (*(b = s))
513da2e3ebchin				{
514da2e3ebchin					for (f = 0; *s && *s != '|'; s++)
515da2e3ebchin						if (*s == ':')
516da2e3ebchin						{
517da2e3ebchin							*s++ = 0;
518da2e3ebchin							f = s;
519da2e3ebchin						}
520da2e3ebchin					if (*s)
521da2e3ebchin						*s++ = 0;
522da2e3ebchin					fprintf(lf, "{\"%s\",", b);
523da2e3ebchin					if (f)
524da2e3ebchin						fprintf(lf, "LC_%s,", f);
525da2e3ebchin					else
526da2e3ebchin						fprintf(lf, "0,");
527da2e3ebchin					if (!(ap = newof(0, Attribute_t, 1, 0)))
528da2e3ebchin					{
529da2e3ebchin						fprintf(stderr, "%s: %d: out of space\n", command, line);
530da2e3ebchin						return 1;
531da2e3ebchin					}
532da2e3ebchin					ap->link.code = b;
533da2e3ebchin					ap->link.index = i++;
534da2e3ebchin					if (!(al = newof(0, Attribute_list_t, 1, 0)))
535da2e3ebchin					{
536da2e3ebchin						fprintf(stderr, "%s: %d: out of space\n", command, line);
537da2e3ebchin						return 1;
538da2e3ebchin					}
539da2e3ebchin					if (!lp->attributes)
540da2e3ebchin						lp->attributes = al;
541da2e3ebchin					else
542da2e3ebchin						az->next = al;
543da2e3ebchin					az = al;
544da2e3ebchin					al->attribute = ap;
545da2e3ebchin					al->next = 0;
546da2e3ebchin					macro(lf, "SUBLANG", lp->name, b);
547da2e3ebchin					fprintf(lf, "\n},\n");
548da2e3ebchin				}
549da2e3ebchin				if (language_attribute_max < i)
550da2e3ebchin					language_attribute_max = i;
551da2e3ebchin				fprintf(lf, "};\n");
552da2e3ebchin			}
553da2e3ebchin			if (lp != (Language_t*)enter(&state.language, (Link_t*)lp))
554da2e3ebchin			{
555da2e3ebchin				fprintf(stderr, "%s: %d: %s: duplicate language\n", command, line, lp->link.code);
556da2e3ebchin				return 1;
557da2e3ebchin			}
558da2e3ebchin			break;
559da2e3ebchin		case MAP:
560da2e3ebchin			if (!(mp = newof(0, Map_t, 1, s - b + 1)))
561da2e3ebchin			{
562da2e3ebchin				fprintf(stderr, "%s: %d: out of space\n", command, line);
563da2e3ebchin				return 1;
564da2e3ebchin			}
565da2e3ebchin			b = (char*)(mp + 1);
566da2e3ebchin			mp->link.code = copy(&b, arg[0]);
567da2e3ebchin			if (!arg[2])
568da2e3ebchin			{
569da2e3ebchin				fprintf(stderr, "%s: %d: territory code expected\n", command, line);
570da2e3ebchin				return 1;
571da2e3ebchin			}
572da2e3ebchin			if (!(mp->language = (Language_t*)lookup(&state.language, arg[1])))
573da2e3ebchin			{
574da2e3ebchin				fprintf(stderr, "%s: %d: %s: unknown language\n", command, line, arg[1]);
575da2e3ebchin				return 1;
576da2e3ebchin			}
577da2e3ebchin			if (!(mp->territory = (Territory_t*)lookup(&state.territory, arg[2])))
578da2e3ebchin			{
579da2e3ebchin				fprintf(stderr, "%s: %d: %s: unknown territory\n", command, line, arg[2]);
580da2e3ebchin				return 1;
581da2e3ebchin			}
582da2e3ebchin			if (!arg[3])
583da2e3ebchin				mp->charset = 0;
584da2e3ebchin			else if (!(mp->charset = (Charset_t*)lookup(&state.charset, arg[3])))
585da2e3ebchin			{
586da2e3ebchin				fprintf(stderr, "%s: %d: %s: unknown charset\n", command, line, arg[3]);
587da2e3ebchin				return 1;
588da2e3ebchin			}
589da2e3ebchin			mp->attribute = 0;
590da2e3ebchin			if (arg[4])
591da2e3ebchin			{
592da2e3ebchin				for (al = mp->language->attributes; al; al = al->next)
593da2e3ebchin					if (!strcmp(al->attribute->link.code, arg[4]))
594da2e3ebchin					{
595da2e3ebchin						mp->attribute = al->attribute;
596da2e3ebchin						break;
597da2e3ebchin					}
598da2e3ebchin				if (!mp->attribute)
599da2e3ebchin				{
600da2e3ebchin					fprintf(stderr, "%s: %d: %s: unknown attribute\n", command, line, arg[4]);
601da2e3ebchin					return 1;
602da2e3ebchin				}
603da2e3ebchin			}
604da2e3ebchin			if (mp != (Map_t*)enter(&state.map, (Link_t*)mp))
605da2e3ebchin			{
606da2e3ebchin				fprintf(stderr, "%s: %d: %s: duplicate map\n", command, line, mp->link.code);
607da2e3ebchin				return 1;
608da2e3ebchin			}
609da2e3ebchin			break;
610da2e3ebchin		}
611da2e3ebchin	}
612da2e3ebchin	fprintf(hf, "#define LC_language_attribute_max\t\t%d\n", language_attribute_max);
613da2e3ebchin	fprintf(hf, "#define LC_territory_language_max\t\t%d\n", territory_language_max);
614