xref: /dflybsd-src/usr.bin/localedef/messages.c (revision 8aa2b98b887dbc78b4b4c6bce1b62b7506f9360e)
1cd1c6085SJohn Marino /*
2cd1c6085SJohn Marino  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3cd1c6085SJohn Marino  * Copyright 2015 John Marino <draco@marino.st>
4*8aa2b98bSJohn Marino  *
5*8aa2b98bSJohn Marino  * This source code is derived from the illumos localedef command, and
6*8aa2b98bSJohn Marino  * provided under BSD-style license terms by Nexenta Systems, Inc.
7*8aa2b98bSJohn Marino  *
8*8aa2b98bSJohn Marino  * Redistribution and use in source and binary forms, with or without
9*8aa2b98bSJohn Marino  * modification, are permitted provided that the following conditions
10*8aa2b98bSJohn Marino  * are met:
11*8aa2b98bSJohn Marino  *
12*8aa2b98bSJohn Marino  * 1. Redistributions of source code must retain the above copyright
13*8aa2b98bSJohn Marino  *    notice, this list of conditions and the following disclaimer.
14*8aa2b98bSJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
15*8aa2b98bSJohn Marino  *    notice, this list of conditions and the following disclaimer in the
16*8aa2b98bSJohn Marino  *    documentation and/or other materials provided with the distribution.
17*8aa2b98bSJohn Marino  *
18*8aa2b98bSJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*8aa2b98bSJohn Marino  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*8aa2b98bSJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*8aa2b98bSJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22*8aa2b98bSJohn Marino  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*8aa2b98bSJohn Marino  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*8aa2b98bSJohn Marino  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*8aa2b98bSJohn Marino  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*8aa2b98bSJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*8aa2b98bSJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*8aa2b98bSJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
29cd1c6085SJohn Marino  */
30cd1c6085SJohn Marino 
31cd1c6085SJohn Marino /*
32cd1c6085SJohn Marino  * LC_MESSAGES database generation routines for localedef.
33cd1c6085SJohn Marino  */
34cd1c6085SJohn Marino 
35cd1c6085SJohn Marino #include <stdio.h>
36cd1c6085SJohn Marino #include <stdlib.h>
37cd1c6085SJohn Marino #include <errno.h>
38cd1c6085SJohn Marino #include <sys/types.h>
39cd1c6085SJohn Marino #include <string.h>
40cd1c6085SJohn Marino #include <unistd.h>
41cd1c6085SJohn Marino #include "localedef.h"
42cd1c6085SJohn Marino #include "parser.h"
43cd1c6085SJohn Marino #include "lmessages.h"
44cd1c6085SJohn Marino 
45cd1c6085SJohn Marino static struct lc_messages_T msgs;
46cd1c6085SJohn Marino 
47cd1c6085SJohn Marino void
init_messages(void)48cd1c6085SJohn Marino init_messages(void)
49cd1c6085SJohn Marino {
50cd1c6085SJohn Marino 	(void) memset(&msgs, 0, sizeof (msgs));
51cd1c6085SJohn Marino }
52cd1c6085SJohn Marino 
53cd1c6085SJohn Marino void
add_message(wchar_t * wcs)54cd1c6085SJohn Marino add_message(wchar_t *wcs)
55cd1c6085SJohn Marino {
56cd1c6085SJohn Marino 	char *str;
57cd1c6085SJohn Marino 
58cd1c6085SJohn Marino 	if ((str = to_mb_string(wcs)) == NULL) {
59cd1c6085SJohn Marino 		INTERR;
60cd1c6085SJohn Marino 		return;
61cd1c6085SJohn Marino 	}
62cd1c6085SJohn Marino 	free(wcs);
63cd1c6085SJohn Marino 
64cd1c6085SJohn Marino 	switch (last_kw) {
65cd1c6085SJohn Marino 	case T_YESSTR:
66cd1c6085SJohn Marino 		msgs.yesstr = str;
67cd1c6085SJohn Marino 		break;
68cd1c6085SJohn Marino 	case T_NOSTR:
69cd1c6085SJohn Marino 		msgs.nostr = str;
70cd1c6085SJohn Marino 		break;
71cd1c6085SJohn Marino 	case T_YESEXPR:
72cd1c6085SJohn Marino 		msgs.yesexpr = str;
73cd1c6085SJohn Marino 		break;
74cd1c6085SJohn Marino 	case T_NOEXPR:
75cd1c6085SJohn Marino 		msgs.noexpr = str;
76cd1c6085SJohn Marino 		break;
77cd1c6085SJohn Marino 	default:
78cd1c6085SJohn Marino 		free(str);
79cd1c6085SJohn Marino 		INTERR;
80cd1c6085SJohn Marino 		break;
81cd1c6085SJohn Marino 	}
82cd1c6085SJohn Marino }
83cd1c6085SJohn Marino 
84cd1c6085SJohn Marino void
dump_messages(void)85cd1c6085SJohn Marino dump_messages(void)
86cd1c6085SJohn Marino {
87cd1c6085SJohn Marino 	FILE *f;
88cd1c6085SJohn Marino 	char *ptr;
89cd1c6085SJohn Marino 
90cd1c6085SJohn Marino 	if (msgs.yesstr == NULL) {
91cd1c6085SJohn Marino 		warn("missing field 'yesstr'");
92cd1c6085SJohn Marino 		msgs.yesstr = "";
93cd1c6085SJohn Marino 	}
94cd1c6085SJohn Marino 	if (msgs.nostr == NULL) {
95cd1c6085SJohn Marino 		warn("missing field 'nostr'");
96cd1c6085SJohn Marino 		msgs.nostr = "";
97cd1c6085SJohn Marino 	}
98cd1c6085SJohn Marino 
99cd1c6085SJohn Marino 	/*
100cd1c6085SJohn Marino 	 * CLDR likes to add : separated lists for yesstr and nostr.
101cd1c6085SJohn Marino 	 * Legacy Solaris code does not seem to grok this.  Fix it.
102cd1c6085SJohn Marino 	 */
103cd1c6085SJohn Marino 	if ((ptr = strchr(msgs.yesstr, ':')) != NULL)
104cd1c6085SJohn Marino 		*ptr = 0;
105cd1c6085SJohn Marino 	if ((ptr = strchr(msgs.nostr, ':')) != NULL)
106cd1c6085SJohn Marino 		*ptr = 0;
107cd1c6085SJohn Marino 
108cd1c6085SJohn Marino 	if ((f = open_category()) == NULL) {
109cd1c6085SJohn Marino 		return;
110cd1c6085SJohn Marino 	}
111cd1c6085SJohn Marino 
112cd1c6085SJohn Marino 	if ((putl_category(msgs.yesexpr, f) == EOF) ||
113cd1c6085SJohn Marino 	    (putl_category(msgs.noexpr, f) == EOF) ||
114cd1c6085SJohn Marino 	    (putl_category(msgs.yesstr, f) == EOF) ||
115cd1c6085SJohn Marino 	    (putl_category(msgs.nostr, f) == EOF)) {
116cd1c6085SJohn Marino 		return;
117cd1c6085SJohn Marino 	}
118cd1c6085SJohn Marino 	close_category(f);
119cd1c6085SJohn Marino }
120