xref: /illumos-gate/usr/src/cmd/localedef/messages.c (revision eb6b10e69fa5ba733da194d3ad71a0e63338be29)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
14  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
15  */
16 
17 /*
18  * LC_MESSAGES database generation routines for localedef.
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <alloca.h>
28 #include "localedef.h"
29 #include "parser.tab.h"
30 #include "lmessages.h"
31 
32 static struct lc_messages msgs;
33 
34 void
35 init_messages(void)
36 {
37 	(void) memset(&msgs, 0, sizeof (msgs));
38 }
39 
40 void
41 add_message(wchar_t *wcs)
42 {
43 	char *str;
44 
45 	if ((str = to_mb_string(wcs)) == NULL) {
46 		INTERR;
47 		return;
48 	}
49 	free(wcs);
50 
51 	switch (last_kw) {
52 	case T_YESSTR:
53 		msgs.yesstr = str;
54 		break;
55 	case T_NOSTR:
56 		msgs.nostr = str;
57 		break;
58 	case T_YESEXPR:
59 		msgs.yesexpr = str;
60 		break;
61 	case T_NOEXPR:
62 		msgs.noexpr = str;
63 		break;
64 	default:
65 		free(str);
66 		INTERR;
67 		break;
68 	}
69 }
70 
71 void
72 dump_messages(void)
73 {
74 	FILE *f;
75 	char *ptr;
76 
77 	if (msgs.yesstr == NULL) {
78 		warn(_("missing field 'yesstr'"));
79 		msgs.yesstr = "";
80 	}
81 	if (msgs.nostr == NULL) {
82 		warn(_("missing field 'nostr'"));
83 		msgs.nostr = "";
84 	}
85 
86 	/*
87 	 * CLDR likes to add : separated lists for yesstr and nostr.
88 	 * Legacy Solaris code does not seem to grok this.  Fix it.
89 	 */
90 	if ((ptr = strchr(msgs.yesstr, ':')) != NULL)
91 		*ptr = 0;
92 	if ((ptr = strchr(msgs.nostr, ':')) != NULL)
93 		*ptr = 0;
94 
95 	if ((f = open_category()) == NULL) {
96 		return;
97 	}
98 
99 	if ((putl_category(msgs.yesexpr, f) == EOF) ||
100 	    (putl_category(msgs.noexpr, f) == EOF) ||
101 	    (putl_category(msgs.yesstr, f) == EOF) ||
102 	    (putl_category(msgs.nostr, f) == EOF)) {
103 		return;
104 	}
105 	close_category(f);
106 }
107