xref: /titanic_41/usr/src/cmd/localedef/messages.c (revision dc41c81f4872a0d4ce52a59cd88a576aeb49183a)
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 2010 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * LC_MESSAGES database generation routines for localedef.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <alloca.h>
27 #include "localedef.h"
28 #include "parser.tab.h"
29 #include "lmessages.h"
30 
31 static struct lc_messages_T msgs;
32 
33 void
34 init_messages(void)
35 {
36 	(void) memset(&msgs, 0, sizeof (msgs));
37 }
38 
39 void
40 add_message(wchar_t *wcs)
41 {
42 	char *str;
43 
44 	if ((str = to_mb_string(wcs)) == NULL) {
45 		INTERR;
46 		return;
47 	}
48 	free(wcs);
49 
50 	switch (last_kw) {
51 	case T_YESSTR:
52 		msgs.yesstr = str;
53 		break;
54 	case T_NOSTR:
55 		msgs.nostr = str;
56 		break;
57 	case T_YESEXPR:
58 		msgs.yesexpr = str;
59 		break;
60 	case T_NOEXPR:
61 		msgs.noexpr = str;
62 		break;
63 	default:
64 		free(str);
65 		INTERR;
66 		break;
67 	}
68 }
69 
70 void
71 dump_messages(void)
72 {
73 	FILE *f;
74 	char *ptr;
75 
76 	if (msgs.yesstr == NULL) {
77 		warn(_("missing field 'yesstr'"));
78 		msgs.yesstr = "";
79 	}
80 	if (msgs.nostr == NULL) {
81 		warn(_("missing field 'nostr'"));
82 		msgs.nostr = "";
83 	}
84 
85 	/*
86 	 * CLDR likes to add : separated lists for yesstr and nostr.
87 	 * Legacy Solaris code does not seem to grok this.  Fix it.
88 	 */
89 	if ((ptr = strchr(msgs.yesstr, ':')) != NULL)
90 		*ptr = 0;
91 	if ((ptr = strchr(msgs.nostr, ':')) != NULL)
92 		*ptr = 0;
93 
94 	if ((f = open_category()) == NULL) {
95 		return;
96 	}
97 
98 	if ((putl_category(msgs.yesexpr, f) == EOF) ||
99 	    (putl_category(msgs.noexpr, f) == EOF) ||
100 	    (putl_category(msgs.yesstr, f) == EOF) ||
101 	    (putl_category(msgs.nostr, f) == EOF)) {
102 		return;
103 	}
104 	close_category(f);
105 }
106